notesoup 0.44.0 → 0.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +1 -0
  2. package/cordis.patch.yml +14 -0
  3. package/dist/src/dsh/budget.d.ts +20 -0
  4. package/dist/src/dsh/budget.js +34 -0
  5. package/dist/src/dsh/engine.d.ts +22 -0
  6. package/dist/src/dsh/engine.js +193 -0
  7. package/dist/src/dsh/history.d.ts +6 -0
  8. package/dist/src/dsh/history.js +73 -0
  9. package/dist/src/dsh/index.d.ts +22 -0
  10. package/dist/src/dsh/index.js +63 -0
  11. package/dist/src/dsh/preflight.d.ts +8 -0
  12. package/dist/src/dsh/preflight.js +35 -0
  13. package/dist/src/dsh/tools.d.ts +3 -0
  14. package/dist/src/dsh/tools.js +111 -0
  15. package/dist/src/dsh/window.d.ts +46 -0
  16. package/dist/src/dsh/window.js +124 -0
  17. package/dist/test/dependencies.test.js +27 -1
  18. package/dist/test/dsh-cli.test.d.ts +1 -0
  19. package/dist/test/dsh-cli.test.js +44 -0
  20. package/dist/test/dsh-compaction-parity.test.d.ts +1 -0
  21. package/dist/test/dsh-compaction-parity.test.js +143 -0
  22. package/dist/test/dsh.test.d.ts +1 -0
  23. package/dist/test/dsh.test.js +323 -0
  24. package/dist/test/helpers/dependency-graph.js +3 -3
  25. package/dist/test/helpers/dsh-messages.d.ts +29 -0
  26. package/dist/test/helpers/dsh-messages.js +33 -0
  27. package/dist/test/helpers/dsh.d.ts +17 -0
  28. package/dist/test/helpers/dsh.js +50 -0
  29. package/docs/architecture.md +4 -1
  30. package/docs/dsh.md +61 -0
  31. package/docs/dsh.profile.patch.yml +34 -0
  32. package/mods/notesoup/.claude-plugin/plugin.json +1 -1
  33. package/package.json +63 -4
  34. package/src/dsh/budget.ts +35 -0
  35. package/src/dsh/engine.ts +160 -0
  36. package/src/dsh/history.ts +57 -0
  37. package/src/dsh/index.ts +69 -0
  38. package/src/dsh/preflight.ts +29 -0
  39. package/src/dsh/tools.ts +107 -0
  40. package/src/dsh/window.ts +126 -0
  41. package/mods/pi-context/dist/claude/helper.js +0 -9324
@@ -0,0 +1,126 @@
1
+ import type { Agent } from "@deepseek-ai/dsh-agent";
2
+ import { appendPluginRecord, pluginRecordOf, type Session, type SessionEvent } from "@deepseek-ai/dsh-session";
3
+ import { isCompactCheckpointSource } from "@deepseek-ai/dsh-compaction";
4
+ import { homedir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { createHash } from "node:crypto";
7
+ import { loadNotesSnapshot } from "../boot/snapshot.js";
8
+ import { renderBootBlock } from "../boot/render.js";
9
+ import { CONTINUATION } from "../budget/text.js";
10
+ import { contentText } from "../history/history.js";
11
+ import { slugify } from "../notes/paths.js";
12
+ import { resolveProjectHome } from "../notes/project-home.js";
13
+ import { snapshotNotesIdentity, type NotesIdentity } from "../notes/identity.js";
14
+
15
+ export const WINDOW_RECORD = "plugin:notesoup/window";
16
+ export const BOOT_SECTION = "notesoup:boot";
17
+ const BOOT_OPEN = "<notesoup-boot>\n";
18
+ const BOOT_CLOSE = "\n</notesoup-boot>";
19
+ const CHECKPOINT_END = "\n\n<notesoup-continuation>\n";
20
+ const TOOLS = { notes: "notes_*", notesList: "notes_list", history: "history_*", remaining: "get_context_remaining", wipe: "clear_memory" };
21
+ export type WindowSnapshot = Readonly<{ version: 1; window: number; windowId: string; openedAt: number; boot: string; notesDigest: string }>;
22
+ export type WindowState = { snapshot: WindowSnapshot; pendingReset: boolean; warned: boolean; reminded: boolean };
23
+ export type NotesConfig = { notesHome?: string; agentName?: string };
24
+
25
+ export function restoreWarnings(state: WindowState, events: readonly SessionEvent[]): void {
26
+ state.warned = false;
27
+ state.reminded = false;
28
+ for (const event of events) {
29
+ if (event.type === "user/message" && event.data.source.kind === "notesoup-budget" && event.data.source.windowId === state.snapshot.windowId) {
30
+ if (event.data.source.phase === "warning") state.warned = true;
31
+ else state.reminded = true;
32
+ }
33
+ }
34
+ }
35
+
36
+ declare module "@deepseek-ai/dsh-session/types" {
37
+ interface PluginRecordMap { "plugin:notesoup/window": WindowSnapshot }
38
+ }
39
+
40
+ export function windowId(sessionId: string, window: number): string { return `dsh:${sessionId}:${window}`; }
41
+ function digest(boot: string): string { return createHash("sha256").update(boot).digest("hex"); }
42
+ export function checkpointText(snapshot: WindowSnapshot): string { return snapshot.boot + CHECKPOINT_END + CONTINUATION + "\n</notesoup-continuation>"; }
43
+
44
+ export function recordSnapshot(event: SessionEvent): WindowSnapshot | undefined {
45
+ const record = pluginRecordOf(event);
46
+ if (record?.type !== WINDOW_RECORD || typeof record.data !== "object" || record.data === null) return undefined;
47
+ const value = record.data as Partial<WindowSnapshot>;
48
+ if (value.version !== 1 || !Number.isSafeInteger(value.window) || value.window! < 0 || typeof value.windowId !== "string" || typeof value.openedAt !== "number" || typeof value.boot !== "string" || value.notesDigest !== digest(value.boot)) return undefined;
49
+ return Object.freeze(value as WindowSnapshot);
50
+ }
51
+
52
+ /** The checkpoint is authoritative when an ignorable record was lost or the transaction's close failed. */
53
+ export function checkpointSnapshot(event: SessionEvent): WindowSnapshot | undefined {
54
+ if (event.type !== "user/message" || !isCompactCheckpointSource(event.data.source)) return undefined;
55
+ const text = contentText(event.data.content);
56
+ const end = text.lastIndexOf(CHECKPOINT_END);
57
+ if (end < 0 || !text.startsWith(BOOT_OPEN)) return undefined;
58
+ return snapshotFromBoot(text.slice(0, end), event.time);
59
+ }
60
+
61
+ function systemSnapshot(event: SessionEvent): WindowSnapshot | undefined {
62
+ if (event.type !== "system/message") return undefined;
63
+ const text = contentText(event.data.message.content);
64
+ const start = text.indexOf(BOOT_OPEN);
65
+ const end = text.lastIndexOf(BOOT_CLOSE);
66
+ if (start < 0 || end < 0) return undefined;
67
+ return snapshotFromBoot(text.slice(start, end + BOOT_CLOSE.length), event.time);
68
+ }
69
+
70
+ function snapshotFromBoot(boot: string, openedAt: number): WindowSnapshot | undefined {
71
+ const id = /^Current context window id: (dsh:.*:(\d+))$/m.exec(boot);
72
+ if (!id || !Number.isSafeInteger(Number(id[2]))) return undefined;
73
+ return Object.freeze({ version: 1, window: Number(id[2]), windowId: id[1]!, openedAt, boot, notesDigest: digest(boot) });
74
+ }
75
+
76
+ export function recoverSnapshot(events: readonly SessionEvent[]): WindowSnapshot | undefined {
77
+ let latest: WindowSnapshot | undefined;
78
+ for (const event of events) {
79
+ const candidate = checkpointSnapshot(event) ?? recordSnapshot(event) ?? systemSnapshot(event);
80
+ if (candidate && (!latest || candidate.window >= latest.window)) latest = candidate;
81
+ }
82
+ return latest;
83
+ }
84
+
85
+ /** Identity is extracted here, never in the filesystem domain. */
86
+ export function notesIdentity(agent: Pick<Agent, "session" | "options">, config: NotesConfig): NotesIdentity {
87
+ const home = config.notesHome ?? join(homedir(), ".agents", "notes");
88
+ return snapshotNotesIdentity({
89
+ home,
90
+ sessionId: agent.session.id,
91
+ projectKey: resolveProjectHome(agent.session.header.cwd ?? process.cwd(), home).name,
92
+ agent: slugify(config.agentName ?? "dsh"),
93
+ model: slugify(agent.session.requestHeader()?.config.model ?? agent.options.model ?? "unknown"),
94
+ });
95
+ }
96
+
97
+ /** Engine-owned, per-session state; prompt reads are synchronous and never read notes again. */
98
+ export class Windows {
99
+ private states = new WeakMap<Session, WindowState>();
100
+ constructor(readonly config: NotesConfig) {}
101
+ get(session: Session): WindowState {
102
+ const state = this.states.get(session);
103
+ if (!state) throw Object.assign(new Error("notesoup: window missing; initialize at agent/created before sending a request."), { code: "WINDOW_MISSING" });
104
+ return state;
105
+ }
106
+ async adopt(agent: Agent, signal?: AbortSignal): Promise<WindowState> {
107
+ const existing = this.states.get(agent.session);
108
+ if (existing) return existing;
109
+ const restored = recoverSnapshot(agent.session.snapshotEvents());
110
+ const snapshot = restored ?? await this.mint(agent, 0, agent.session.header.createdAt);
111
+ signal?.throwIfAborted();
112
+ if (!restored) appendPluginRecord(agent.session, WINDOW_RECORD, snapshot);
113
+ const state = { snapshot, pendingReset: false, warned: false, reminded: false };
114
+ restoreWarnings(state, agent.session.snapshotEvents());
115
+ this.states.set(agent.session, state);
116
+ return state;
117
+ }
118
+ async mint(agent: Pick<Agent, "session" | "options">, window: number, openedAt: number): Promise<WindowSnapshot> {
119
+ const identity = notesIdentity(agent, this.config);
120
+ const notes = await loadNotesSnapshot(identity, openedAt);
121
+ const previous = this.states.get(agent.session)?.snapshot;
122
+ const firstWindowId = previous?.boot.match(/^First context window id: (.+)$/m)?.[1] ?? windowId(agent.session.id, 0);
123
+ const boot = BOOT_OPEN + renderBootBlock({ agentName: identity.agent, modelName: identity.model, firstWindowId, currentWindowId: windowId(agent.session.id, window), ...(window ? { previousWindowId: previous?.windowId ?? windowId(agent.session.id, window - 1) } : {}), notes, tools: TOOLS }) + BOOT_CLOSE;
124
+ return Object.freeze({ version: 1, window, windowId: windowId(agent.session.id, window), openedAt, boot, notesDigest: digest(boot) });
125
+ }
126
+ }