dsh-lifecycle-cache-cleaner: KV Cache Lifecycle Management for DSH Local Agent

Introduction

After running for extended periods, DSH’s (DeepSeek Harness) local Agent accumulates large amounts of GPU memory in its KV cache. If stale context isn’t cleaned up promptly after rebuilding, response latency degrades. Meanwhile, session directories keep piling up, impacting both disk usage and startup speed. DSH itself lacks a unified cache cleanup interface, leaving it up to users to manage on their own. dsh-lifecycle-cache-cleaner addresses three key issues: residual context after rebuilds, session accumulation, and the need to repeatedly instruct the Agent on naming conventions, tone, and long-term directives.

Plugin Overview

  • Plugin name: dsh-lifecycle-cache-cleaner
  • Maintainer: ccr-wer
  • Category: Model Inference
  • Core constraint: If no local deployment is detected, no cleanup is performed (fail-closed). All cleanup operations go through a single entry point lib/core/release-gate.js; no other code calls destroyEngine().

Installation & Activation

Node >= 18.17 is required before installation. This package serves both as a library and as an installable DSH bundle:

Plugins → Add plugin → select this directory
# or let the plugin manager run install_bundle

No DSH restart is needed after installation: tools, sidebar entries, and the plugin page card are all hot-mounted. If the UI doesn’t refresh, simply reload the page.

Status Card & Cleanup Entry

On the Plugins → this plugin page, you’ll find a status card displaying engine state, gate decision, and memory usage, along with a “Release Engine” button.

  • Status data comes from the same-origin read-only route GET /lifecycle-cache/api/status; release goes through POST /clean.
  • There is no gate logic in the browser; the sole decision point is lib/core/release-gate.js.
  • When the gate does not pass, the button is disabled with a reason shown (skip_absent / skip_unknown / skip_no_lease / skip_foreign / no_destroy).

Session Management

At the bottom of the sidebar → Sessions, you can see a list of sessions with their titles, last activity time, and size. Deleting a session doesn’t remove it outright; it’s moved to a trash bin and can be restored.

  • Titles are read from the session/title event in the session log. If unavailable, a short ID is displayed.
  • Deletion flow:
    1. The directory is renamed to lifecycle-cache/trash/<timestamp>__<workspace>__session-<id>/.
    2. The current session and any session that has been written to within the last sessionDeleteMinIdleMs (default 2 minutes) are rejected.
    3. A lease release + gate check is immediately performed.
    4. The host workspace index is updated (sessionIds in workspace.json) + projection cache is removed.
    5. The client is notified to remove the row (api-session/removed).

If there are legacy sessions that exist in the index but are missing from disk, a notice will appear at the top of the panel: “N stale entries in the navigation page.” Click “Repair stale entries” to remove them (POST /sessions/repair, idempotent).

Personalization & Memory

At the bottom of the sidebar → Personalization, you can configure:

  • userName: How it addresses you (60 characters)
  • assistantName: What it calls itself (60 characters)
  • style: Response style, enumerated (default / concise / detailed)
  • instructions: Long-term instructions (4000 characters)
  • persona: Persona (4000 characters)

These fields are manually set by the user. The model has no write access and cannot modify them independently.

At the bottom of the sidebar → Memory, you can view two layers of memory:

  • Shared memory: shared across all workspaces
  • Workspace memory: one set per workspace

The top of the panel displays “Last injection: Shared X/Y · Workspace A/B.” Entries pushed out by the budget are highlighted in yellow.

The model proposes memory entries via the lifecycle_memory_propose tool. They only enter a “pending confirmation” queue and are written after the user accepts them. During memory retrieval, entries are ranked by relevance to the current query, and the number of entries dropped due to budget is visible in the panel.

Gate & Cleanup Rules

The core principle of the plugin is “no local deployment detected = no cleanup”:

  • Probe result absent → no cleanup (log tidying only)
  • Probe result unknown → no cleanup (fail-closed)
  • Probe result present + ownership verified → engine manipulation is allowed

Session deletion uses ownership: 'lease' (a lease must exist and all three elements must match); engines built by this package use ownership: 'adapter-exclusive'. If an adapter does not implement probeEngine(), it will never perform cleanup.

Configuration Example

- id: lifecycle-cache-cleaner
  name: 'dsh-lifecycle-cache-cleaner'
  config:
    rootDir: 'C:\\Users\\you\\.dsh'   # Falls back to DSH_HOME, then ~/.dsh if not set
    ownership: adapter-exclusive       # or lease
    engine:
      kind: auto                       # auto | ollama | llamacpp | none
      baseUrl: 'http://127.0.0.1:11434'
    releaseOnDelete: true              # Whether to release the engine via gate after session deletion
    releaseOnCompact: true             # Whether to clean via gate after successful context compaction
    compactDebounceMs: 1500            # Debounce window for consecutive compactions
    watchIntervalMs: 5000              # Session directory scan interval
    watchSessions: true                # Set to false to disable watching entirely
    sessionDeleteMinIdleMs: 120000     # Reject deletion if written to within the last 2 minutes; 0 = disable

Use Cases & Notes

This plugin is suitable for developers who need fine-grained lifecycle management of local model KV caches and sessions. Its gate mechanism guarantees “no deployment detected = no cleanup,” preventing accidental deletion. Before use, it is recommended to review the source code and license to ensure compatibility with your DSH deployment environment.

Conclusion

dsh-lifecycle-cache-cleaner provides a unified KV cache lifecycle management solution, including status monitoring, session cleanup, personalization settings, and workspace memory. If you’re struggling with GPU memory usage and session accumulation, consider installing and configuring it.

羽毛球分组比赛记分
小程序二维码

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

Xiaoye