Introduction¶
DeepSeek Harness (DSH) supports creating conversation branches through a fork mechanism. In dsh 0.1.0-rc.6, the fork path performs multiple full deep copies of the entire session event log. For large sessions over 15MB, a single fork can cause hundreds of milliseconds of synchronous blocking, which is enough to interrupt streaming LLM responses (manifesting as TRANSPORT/heartbeat timeout retries) or cause UI stuttering.
The dsh-fork-perf plugin is intended to resolve this performance bottleneck. It reuses already frozen event references in memory, eliminating unnecessary deep-copy overhead and reducing the main fork duration to about 1/18 of the original.
Core Features¶
The plugin primarily provides the following capabilities:
- Zero-copy fork: Uses the
fromRestorechannel to reuse immutable JSON tree references fromdeepFreezein place, skipping a full tree deep copy. - fast init-for: In
PersistenceCoordinator.initFor, replacesstructuredClone(seed)with frozen reference reuse, eliminating copy latency during persistence restore. - Monitoring and API: Provides fork timing and path statistics, and exposes statistics query and runtime configuration interfaces through
/dsh-fork-perf/api. - Full restoration: The
disposemethod can restore all applied patches.
Installation and Enabling¶
Install the plugin via the command line:
dsh plugin --profile web add github:orangeofcarl0-sys/dsh-fork-perf
After installation, restart dsh web to apply the configuration. A successful startup is indicated by [fork-perf] zero-copy fork installed appearing in the logs.
Monitoring and Configuration¶
The plugin provides an HTTP API for monitoring runtime status and adjusting behavior.
API Interface¶
The request format is POST http://127.0.0.1:3080/dsh-fork-perf/api/<method>.
stats.get: Retrieves fork count, zero-copy ratio, fallback count, and recent records (event count, duration, path).stats.reset: Resets statistics counters to zero.config.get/config.set: Gets or sets runtime toggles (such aszeroCopyFork,fastInitFor).
Examples¶
# 获取统计信息
curl -X POST http://127.0.0.1:3080/dsh-fork-perf/api/stats.get
# 禁用零拷贝 fork(回退至官方实现)
curl -X POST http://127.0.0.1:3080/dsh-fork-perf/api/config.set \
-d '{"zeroCopyFork": false}'
Technical Details and Limitations¶
Implementation¶
- Zero-copy fork: The plugin uses the
fromRestorechannel ofSession.prepare(..., { seedSource: 'persistence' })to directly use frozen seed events. Since events have completed JSON boundary validation and deep freezing when entering the source session, reusing references is security-equivalent to deep copying. - fast init-for: The plugin detects the
structuredClone(seed)marker inPersistenceCoordinator.initForand replaces it with reference reuse. If a source-code feature mismatch is detected (for example, structural changes caused by a major version upgrade), it automatically skips the optimization and raises a warning.
Safety and Fallback¶
- Fallback mechanism: It has three layers of fallback capability. The first layer uses the official implementation when probing missing methods; the second layer falls back to the official implementation on runtime exceptions; the third layer can disable the optimization via configuration.
- Patch restoration: The plugin supports the
disposemethod, which can restore all patches and ensure a clean environment.
Limitations¶
- enqueue copying: The per-event
structuredCloneinside theenqueueclosure cannot be safely eliminated at the plugin layer. This is because the write-behind mechanism requires decoupling persistence from the producer; a root-cause fix requires upstream to switch to on-demand snapshots. - Version binding: The patch logic is bound to the internal structure of dsh 0.1.0-rc.6 (such as
_forkSeedandinitForsource-code features). When upgrading to a new major version, feature validation automatically skips the optimization and retains official behavior, requiring re-adaptation.
Use Cases¶
This plugin is suitable for scenarios that require frequent fork operations on large sessions, especially applications that rely on streaming responses or are sensitive to UI responsiveness. Before using it, ensure the DSH version is 0.1.0-rc.6, and note that the plugin runs with the permissions of the current dsh process.