Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 93 additions & 3 deletions bun.lock

Large diffs are not rendered by default.

450 changes: 450 additions & 0 deletions docs/deepseek-plan.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"opencode:check": "bun scripts/opencode-live.ts",
"opencode:smoke": "bun scripts/opencode-smoke.ts",
"opencode:compact": "bun scripts/opencode-compact.ts",
"pack": "vp run --filter ./packages/core prepack && vp run --filter ./packages/pi prepack && vp run --filter ./packages/opencode prepack",
"pack": "vp run --filter ./packages/core prepack && vp run --filter ./packages/pi prepack && vp run --filter ./packages/deepseek prepack && vp run --filter ./packages/opencode prepack",
"changeset": "changeset",
"version": "changeset version && bun scripts/sync-skill-versions.ts && vp fmt packages/*/CHANGELOG.md",
"version": "changeset version && rm -f bun.lock && bun install && bun scripts/sync-skill-versions.ts && vp fmt packages/*/CHANGELOG.md",
"release": "changeset publish",
"prepare": "vp config || true",
"eval": "bun scripts/eval-long-context.ts"
Expand Down
13 changes: 13 additions & 0 deletions packages/deepseek/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
MIT License

This software is an original implementation of the Lossless Context Management
(LCM) architecture, specified in the paper "Lossless Context Management" by
Clint Ehrlich and Theodore Blackman, Voltropy PBC
(https://papers.voltropy.com/LCM). The paper is credited as the algorithmic
specification; all code in this repository was written from that spec.

The "smart zone" framing (quality degradation at a token budget, not the
window limit) is credited to Matt Pocock, AI Coding Dictionary
(https://www.aihero.dev/ai-coding-dictionary/smart-zone).

Copyright (c) 2026 stainless-code
27 changes: 27 additions & 0 deletions packages/deepseek/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# `deepseek-lossless` (working name)

Lossless Context Management for the DeepSeek harness, built on `lossless-core`.
Status: evaluation spike on a branch. It implements the plan in
`docs/deepseek-plan.md` phase by phase, and `docs/roadmap.md` item 10
tracks this work until the live-proof gate is met.

## Pins

The adapter compiles against these exact published versions, which match the
pinned local checkout (release `dsh-0.1.7-alpha.2`, vendor `4.0.4`):

- `@deepseek-ai/dsh-agent@0.1.7-alpha.2`
- `@deepseek-ai/dsh-commands@0.1.7-alpha.2`
- `@deepseek-ai/dsh-compaction@0.1.7-alpha.2`
- `@deepseek-ai/dsh-compaction-basic@0.1.7-alpha.2`
- `@deepseek-ai/dsh-llm@0.1.7-alpha.2`
- `@deepseek-ai/dsh-session@0.1.7-alpha.2`
- `@deepseek-ai/dsh-session-projection@0.1.7-alpha.2` (tests only)
- `@deepseek-ai/dsh-system-prompt@0.1.7-alpha.2` (tests only)
- `@deepseek-ai/dsh-token-meter@0.1.7-alpha.2` (tests only)
- `@deepseek-ai/dsh-tools@0.1.7-alpha.2`
- `@deepseek-ai/cordis@4.0.4`

The checkout lives outside this repo and is never vendored here. Every phase
log names the commit it ran against, because the harness warns of breaking
changes.
60 changes: 60 additions & 0 deletions packages/deepseek/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "deepseek-lossless",
"version": "0.1.0",
"description": "Lossless Context Management for the DeepSeek harness: long sessions keep working past the point where the model would forget, and every verbatim message stays searchable and readable.",
"keywords": [
"coding-agent",
"context",
"deepseek",
"dsh-plugin",
"lcm",
"memory"
],
"homepage": "https://github.com/stainless-code/lossless#readme",
"bugs": {
"url": "https://github.com/stainless-code/lossless/issues"
},
"license": "MIT",
"author": "stainless-code",
"repository": {
"type": "git",
"url": "git+https://github.com/stainless-code/lossless.git",
"directory": "packages/deepseek"
},
"files": [
"dist",
"CHANGELOG.md"
],
"type": "module",
"main": "./dist/index.mjs",
"exports": {
".": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
}
},
"scripts": {
"pack:watch": "vp pack --watch",
"prepack": "vp pack",
"prepublishOnly": "vp check && vp test"
},
"dependencies": {
"@deepseek-ai/cordis": "4.0.5",
"@deepseek-ai/dsh-agent": "0.2.1-rc.2",
"@deepseek-ai/dsh-commands": "0.2.1-rc.2",
"@deepseek-ai/dsh-compaction": "0.2.1-rc.2",
"@deepseek-ai/dsh-compaction-basic": "0.2.1-rc.2",
"@deepseek-ai/dsh-llm": "0.2.1-rc.2",
"@deepseek-ai/dsh-session": "0.2.1-rc.2",
"@deepseek-ai/dsh-tools": "0.2.1-rc.2",
"lossless-core": "workspace:*"
},
"devDependencies": {
"@deepseek-ai/dsh-session-projection": "0.2.1-rc.2",
"@deepseek-ai/dsh-system-prompt": "0.2.1-rc.2",
"@deepseek-ai/dsh-token-meter": "0.2.1-rc.2"
},
"engines": {
"node": ">=22.19.0"
}
}
103 changes: 103 additions & 0 deletions packages/deepseek/src/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import type { Context } from "@deepseek-ai/cordis";
import type { CommandInvocation, CommandResult } from "@deepseek-ai/dsh-commands";
import { CommandDefinitionId } from "@deepseek-ai/dsh-commands/brand";
import { ManualCompactionError } from "@deepseek-ai/dsh-compaction";
import type { LcmStore } from "lossless-core";

export interface LcmCommandDeps {
readonly stores: () => ReadonlyMap<string, LcmStore>;
}

const USAGE = "Usage: /lcm [status|compact]";

/** Fail loudly if a locally closed union gains an unhandled member. */
function assertNever(value: never): never {
throw new Error(`unknown manual compaction error code: ${String(value)}`);
}

/** Convert expected capability failures into concise human-only outcomes. */
function expectedFailure(error: ManualCompactionError): CommandResult {
switch (error.code) {
case "busy":
return {
kind: "error",
text: "Compaction is unavailable because this process has an active compaction, or the agent is not idle.",
};
case "cancelled":
return { kind: "error", text: "Compaction cancelled." };
case "changed":
return {
kind: "error",
text: "The history selected for compaction changed before it could be replaced. The attempt is recorded in the session log.",
};
case "summary":
return {
kind: "error",
text: "Compaction could not produce a useful summary. The attempt is recorded in the session log.",
};
case "commit":
return {
kind: "error",
text: "Compaction did not finish cleanly; some session history may have changed. Inspect the current session state before retrying.",
};
case "persistence":
return {
kind: "error",
text: "Compaction finished, but the session could not be saved.",
};
default:
return assertNever(error.code);
}
}

async function handleCompact(ctx: Context, invocation: CommandInvocation): Promise<CommandResult> {
try {
const result = await ctx.compaction.compactNow(
invocation.agent,
invocation.signal,
invocation.commandId,
);
if (result === null) return { kind: "success", text: "No compactable history yet." };
return {
kind: "success",
text: `Compacted ${result.shadowedSeqs.length} history items (~${result.shadowedTokenCount} tokens).`,
sourceEventSeq: result.summarySeq,
};
} catch (error: unknown) {
if (invocation.signal.aborted) return { kind: "error", text: "Compaction cancelled." };
if (error instanceof ManualCompactionError) return expectedFailure(error);
throw error;
}
}

function handleStatus(deps: LcmCommandDeps): CommandResult {
const stores = deps.stores();
if (stores.size === 0) return { kind: "success", text: "LCM has mirrored no sessions yet." };
const lines = [...stores].map(([id, store]) => {
const messages = store.allMessages().length;
const summaries = store.allSummaries().length;
return `${id}: ${messages} messages, ${summaries} summaries`;
});
return { kind: "success", text: lines.join("\n") };
}

/** The `/lcm` command: `status` reports the mirror, `compact` forces one
* manual compaction through whichever backend the composition mounted. */
export function lcmCommandDefinition(ctx: Context, deps: LcmCommandDeps) {
return {
definitionId: CommandDefinitionId("deepseek-lossless"),
name: "lcm",
description: "Lossless session memory: status and manual compaction",
handler: (invocation: CommandInvocation): Promise<CommandResult> => {
const verb = invocation.rawInput.trim().split(/\s+/, 1)[0] ?? "";
if (verb === "" || verb === "status") return Promise.resolve(handleStatus(deps));
if (verb === "compact") return handleCompact(ctx, invocation);
return Promise.resolve({ kind: "error", text: USAGE });
},
};
}

/** Register `/lcm` on a composition with a commands service. Returns its disposer. */
export function registerLcmCommand(ctx: Context, deps: LcmCommandDeps): () => void {
return ctx.commands.register(lcmCommandDefinition(ctx, deps));
}
Loading
Loading