dsh-session-handoff: Relay Memory Before Session Expiration

Introduction

Long DSH sessions eventually reach a size where native compaction stops working. The mechanism behind this deserves precise explanation, as it is the cause of permanent failure:

  • Compaction replays the entire shadowed region as input, submitting it to a single summarization request.
  • Upstream providers enforce a hard cap on the prompt of a single request.
  • Once the session exceeds this cap, the compaction request itself exceeds the cap.
  • Therefore, compaction fails every time, never healing the overflow.

The session then becomes impossible to compact, impossible to continue, and—because simple handoff implementations construct summary requests in the same way—impossible to hand off. This is a deadlock. /handoff exists to break it.

Overview

This is a DSH plugin for transferring memories from an old session to a new one before the old session becomes unusable.

  • Maintainer: iuuuuuuuu
  • Category: Memory
  • License: MIT
  • Core Value: Solving the deadlock problem caused by context overflow in sessions, achieving memory preservation through summarization and handoff.

Installation and Activation

Installing the plugin requires specifying the target profile.

dsh plugin --profile <name> add /path/to/dsh-session-handoff

DSH must be restarted after installation to take effect. This is dictated by DSH’s loader caching mechanism: without a restart, modifications to plugin source code or newly added plugins will not take effect in the running host.

After restart, the log should display the installation success marker:

[session-handoff] installed: /handoff registered, pressure monitor active (upstream limit 1048576)

Core Features

Context Pressure Monitoring

The plugin measures the session’s token pressure at every turn boundary and compares it against two independent limits, using the tighter one as the binding limit:

  1. Model Context Window: Read from the model configuration used by the session, representing the size accepted by the model itself.
  2. Upstream Prompt Limit: A configuration item representing the provider’s acceptance cap for a single request.

The latter is the actual cause of session collapse and cannot be derived from the former. When pressure crosses the policy ladder, the plugin injects a notification into the session, informing it of the measured value, the binding limit, and the exit path. This notification is model-facing context, does not spontaneously start a new turn, and is throttled for both growth and time.

Automatic Session Handoff

Automatic handoff is enabled by default; automatic archiving is disabled by default.

Automatic handoff is triggered when a session becomes unusable due to three independent reasons:

  1. Pressure: Measured pressure reaches the threshold.
  2. Timeout: The session duration becomes too long.
  3. Session Death: The upstream service returns an error.

Handoff runs in a transaction: Read session -> Extract four layers of memory -> Bounded summarization -> Create new session inheriting routing and working directory -> Deliver seed as the new session’s first turn -> Archive old session -> Release old session event tree. The new session starts working immediately.

Memory Preservation

The design question is what to carry. The plugin extracts four layers of memory, ordered by cost:

  1. Verbatim Facts: Regex extraction, exact, zero cost.
  2. Previous Checkpoints: compaction/end summaries successfully written by the session, exact, zero cost.
  3. Recent Transcription: The tail end of the conversation, verbatim, exact, zero cost.
  4. Narrative Summary: Model-generated summary, paraphrased, requires one processing pass.

The first three exact layers are never discarded to make room for the paraphrased layer; only the paraphrased layer is truncated, and only in the middle. The verbatim tail receives a reserved share of the seed budget rather than competing with prose.

Credential Sanitization

Verbatim layers are copies that replicate what the session has echoed. To prevent credential leakage, the plugin sanitizes them by shape before writing: removing bearer tokens, sk- / ghp_ / github_pat_ / xox / AKIA keys, JWTs, PEM private keys, password= assignments, and inline connection string passwords. The facts layer is also sanitized because paths may contain keys.

Typical Usage

Use the following commands for manual operation:

/handoff           hand this conversation off to a fresh session
/handoff-status    show the measurement, the binding ceiling, and the trigger state

Executing /handoff prints a report:

Handed off `session-9ef22e43-…` to `session-e7d70ba3-…`.

- read: 32286 messages, 117541 events
- memory: 240 facts, 0 checkpoints
- summarize: 70 chunk(s), source=map-reduce, 18422 chars
- seed: 21544 chars (~5386 tokens)
- create: session-e7d70ba3-…
- archive: session-9ef22e43-…

The source session is archived; it stays readable in the sidebar.

Note: The archiving operation is performed last, so if early steps fail, the source session remains intact and available.

Implementation Details and Caveats

Difference between Archiving and Release

Archiving is a visibility change: the session ID is added to the archived set, hiding it from the default view in the sidebar. Its logs remain on disk, remaining readable, and unarchiving restores its exact position. Archiving does not delete anything, nor does it release memory.

Release rule: A session can only be released after it has been archived.

Release calls SessionStore.remove(id), executing the store’s official detachment lifecycle. This removes the session from ctx.sessions (the session event tree) but not from ctx.agents (Agents). The Agent registry is a plain Map with no public removal method, and it is called by the Agent loop, which the plugin cannot invoke. Therefore, releasing a session removes the event tree, but its Agent still holds a reference to the tree, and the disk log remains unchanged.

Applicable Scenarios and Notes

This plugin is suitable for long conversation scenarios, especially when upstream providers have strict limits on single requests while the model configuration’s context window is much larger than this limit.

  • Installation Effectiveness: DSH must be restarted after installation.
  • Memory Management: Understand that archiving only changes visibility, release only removes the session tree, and Agents still hold references.
  • Permissions: The plugin runs with the permissions of the current DSH process; check the source code and license before installation.

Conclusion

The /handoff plugin solves the deadlock problem of DSH sessions during context overflow through proactive context pressure monitoring and memory preservation mechanisms. It transforms “unable to continue” conversations into inheritable seeds, ensuring workflows are not interrupted by the inflation of a single session.

  • Directory Page: https://www.skillhub.cn/plugins/iuuuuuuuu/dsh-session-handoff
  • GitHub Repository: https://github.com/iuuuuuuuu/dsh-session-handoff
羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye