dsh-continual-evolve 0.7.0 → 0.7.2

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.
package/lib/llm-text.js CHANGED
@@ -11,40 +11,57 @@ import { EVOLVE_MESSAGE_SOURCE } from "./message-source.js";
11
11
  */
12
12
  export async function streamText(ctx, opts) {
13
13
  const assembler = new BlockAssembler();
14
- for await (const chunk of ctx.llm.stream({
15
- provider: opts.provider,
16
- model: opts.model,
17
- system: opts.system,
18
- messages: [
19
- createUserMessage({
20
- content: [{ type: "text", text: opts.prompt }],
21
- source: EVOLVE_MESSAGE_SOURCE,
22
- }),
23
- ],
24
- reasoningEffort: ReasoningEffortId("off"),
25
- maxTokens: opts.maxTokens ?? 8000,
26
- ...(opts.signal ? { signal: opts.signal } : {}),
27
- })) {
28
- assembler.push(chunk);
14
+ let outcome = "error";
15
+ try {
16
+ for await (const chunk of ctx.llm.stream({
17
+ provider: opts.provider,
18
+ model: opts.model,
19
+ system: opts.system,
20
+ messages: [
21
+ ...(opts.prefixMessages ?? []),
22
+ createUserMessage({
23
+ content: [{ type: "text", text: opts.prompt }],
24
+ source: EVOLVE_MESSAGE_SOURCE,
25
+ }),
26
+ ],
27
+ reasoningEffort: ReasoningEffortId("off"),
28
+ maxTokens: opts.maxTokens ?? 8000,
29
+ ...(opts.signal ? { signal: opts.signal } : {}),
30
+ })) {
31
+ assembler.push(chunk);
32
+ }
33
+ const finish = assembler.finish;
34
+ if (finish.kind === "error") {
35
+ outcome = "error";
36
+ throw new Error(`evolve: LLM call failed: ${finish.failure?.message ?? "unknown"}`);
37
+ }
38
+ if (finish.kind === "aborted") {
39
+ outcome = "aborted";
40
+ throw new Error("evolve: LLM call aborted");
41
+ }
42
+ if (finish.kind === "max-tokens") {
43
+ outcome = "max-tokens";
44
+ throw new Error("evolve: LLM output budget exhausted (max-tokens)");
45
+ }
46
+ const text = assembler
47
+ .blocks()
48
+ .filter((block) => block.type === "text")
49
+ .map((block) => block.text)
50
+ .join("\n");
51
+ if (text.length === 0) {
52
+ outcome = "empty";
53
+ throw new Error("evolve: LLM produced no text output");
54
+ }
55
+ outcome = "success";
56
+ return text;
29
57
  }
30
- const finish = assembler.finish;
31
- if (finish.kind === "error") {
32
- throw new Error(`evolve: LLM call failed: ${finish.failure?.message ?? "unknown"}`);
58
+ finally {
59
+ try {
60
+ opts.onUsage?.({ ...(assembler.usage ? { usage: assembler.usage } : {}), outcome });
61
+ }
62
+ catch {
63
+ // Usage observation is diagnostic; its failure must not alter the model call.
64
+ }
33
65
  }
34
- if (finish.kind === "aborted") {
35
- throw new Error("evolve: LLM call aborted");
36
- }
37
- if (finish.kind === "max-tokens") {
38
- throw new Error("evolve: LLM output budget exhausted (max-tokens)");
39
- }
40
- const text = assembler
41
- .blocks()
42
- .filter((block) => block.type === "text")
43
- .map((block) => block.text)
44
- .join("\n");
45
- if (text.length === 0) {
46
- throw new Error("evolve: LLM produced no text output");
47
- }
48
- return text;
49
66
  }
50
67
  //# sourceMappingURL=llm-text.js.map
package/lib/planner.d.ts CHANGED
@@ -9,6 +9,8 @@
9
9
  import type { Context } from "@deepseek-ai/cordis";
10
10
  import type { Agent } from "@deepseek-ai/dsh-agent";
11
11
  import type { HarnessState, RefinementProposal, RefinementResult } from "./types.js";
12
+ import { type PrefixCacheOptions } from "./prefix-cache.js";
13
+ import { type TokenUsageTarget } from "./token-usage.js";
12
14
  export declare const PLANNER_SYSTEM_PROMPT = "You are the /evolve continual harness subsystem.\n\nYour job is to improve the editable continual harness state. Instead of\nsummarizing the conversation you emit precise Create, Update, or Delete edits\nto reusable state: prompt notes, memories, skills, and subagent specs.\n\nRules:\n- The base system prompt is immutable and MUST NOT be rewritten (never edit id \"base_system_prompt\").\n- Prefer small evidence-backed edits. If no useful edit is justified, return an empty edits array.\n- prompt = narrow behavioral policy addendums; memory = ONE FACT per entry, typed via metadata.memoryType\n- (user = who the user is; feedback = a pitfall/correction or confirmed approach, MUST carry Why + How to apply;\n- project = work goal/constraint not derivable from the repo; reference = URL/dashboard/ticket pointer).\n- Memory rules: one fact per entry (title = one-line retrieval hook, content = the fact alone);\n- never save what the repo already records (code structure, past fixes, git history, AGENTS.md) or what only\n- matters to this conversation \u2014 save the non-obvious point instead; before creating, update the existing\n- entry when one already covers the fact (creates carry metadata {memoryType} or they are rejected);\n- skill = repeatable procedures (must carry a python reference {type:\"python\", import, callable}\n and an arguments object); subagent = reusable delegation roles.\n- Skill entries are authored to the DSH skill quality standard\n (skill-creator, distilled from the official deepseek-harness 11 skills;\n the full facts are provided in the <skill_quality_standard> block below):\n only for a REAL trigger scenario grounded in the trajectory\n (who, in what real task, what signal) \u2014 never invent one to pad the store;\n never duplicate the official 11 skills or existing entries; content is a\n SKILL.md document (frontmatter routing with \"use when / do not use when\"\n description, boundary declaration, prerequisites and exclusions, layered\n information, verifiable completion criteria). Self-check every proposed\n skill against the 7 structural features and state the result in its\n reason field.\n- Repeated multi-step workflows (session start/end routines, recurring\n wrap-up or handoff procedures) may be proposed as guidance skills:\n kind=skill, skill_kind=\"guidance\", content = a SKILL.md document (no\n python reference \u2014 executable skills keep requiring reference +\n arguments). Only propose with repeated evidence in the trajectory, never\n for one-off flows. Guidance skills materialize as discoverable SKILL.md\n files under <skillsRoot>/<kebab-name>/SKILL.md and are always offered to\n the user for a decision before they land.\n- Local edits are session-scoped; global edits persist across sessions.\n- Gap C2: EVERY edit MUST include a \"blastRadius\" field indicating how broadly\n the edit applies: \"general\" (cross-project tactical rule, applies everywhere),\n \"project\" (valid for the current project/repo), or \"session\" (one-off,\n specific to this session's context). The review gate validates that\n local-scope edits use \"session\" or \"project\", and global-scope edits use\n \"general\" or \"project\". When in doubt, prefer narrower blast radius.\n- Ground every edit in evidence: the session trajectory (recent direct user\n messages) is provided when available; prefer edits backed by it over\n speculation, and never invent preferences the user did not express.\n- Stale entries (superseded by newer ones, never referenced in recent\n trajectories, obsolete facts): propose action \"archive\" instead of\n \"delete\" \u2014 archive hides the entry from injection while keeping its data\n restorable; it requires only kind + id.\n- Output JSON only, exactly this shape:\n{\n \"summary\": \"one sentence\",\n \"rationale\": \"why these edits are justified by the evidence\",\n \"expectedOutcome\": \"what should improve and how to validate it\",\n \"edits\": [\n {\n \"action\": \"create|update|delete\",\n \"kind\": \"prompt|memory|skill|subagent\",\n \"id\": \"stable id for update/delete, optional for create\",\n \"title\": \"required for create/update except delete\",\n \"content\": \"required for create/update except delete\",\n \"path\": \"optional grouping path\",\n \"reference\": {\"type\":\"python\",\"import\":\"pkg.mod\",\"callable\":\"fn\"} ,\n \"arguments\": {\"name\": {\"type\":\"string\",\"required\":true,\"description\":\"...\"}},\n \"skill_kind\": \"executable|guidance (optional; skill kind only \u2014 guidance = SKILL.md document without reference)\",\n \"metadata\": {},\n \"reason\": \"why this edit is useful\",\n \"blastRadius\": \"general|project|session\"\n }\n ]\n}";
13
15
  export interface PlanOptions {
14
16
  agent: Agent;
@@ -22,6 +24,11 @@ export interface PlanOptions {
22
24
  * every planning call is grounded in what the user actually said.
23
25
  */
24
26
  trajectory?: string;
27
+ /**
28
+ * Explicit session-trajectory rows for prefix-cache Route A. When supplied,
29
+ * the planner sees only the scheduler snapshot rather than the full session.
30
+ */
31
+ trajectoryEvents?: readonly unknown[];
25
32
  /**
26
33
  * Skills root to read the skill-creator template facts from
27
34
  * (`<root>/skill-creator/references/template.md`). When omitted or the
@@ -32,6 +39,16 @@ export interface PlanOptions {
32
39
  global?: boolean;
33
40
  signal?: AbortSignal;
34
41
  maxOutputTokens?: number;
42
+ /**
43
+ * Prefix-cache routing for the planning input. Route A prepends a
44
+ * session-derived message prefix and drops the auto-extracted
45
+ * trajectory block (an explicitly passed `trajectory` is always kept);
46
+ * Route B keeps the legacy flat-text input. Absent → auto-detect with
47
+ * the default budget.
48
+ */
49
+ prefixCache?: PrefixCacheOptions;
50
+ /** Direct-call token ledger destination; the planner phase is fixed by this module. */
51
+ tokenUsage?: TokenUsageTarget;
35
52
  }
36
53
  export declare function planWithLlm(ctx: Context, options: PlanOptions): Promise<RefinementProposal>;
37
54
  //# sourceMappingURL=planner.d.ts.map
package/lib/planner.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import { parseProposal } from "./plan.js";
2
2
  import { formatHarnessStateForPrompt, historyForPrompt } from "./render.js";
3
- import { recentUserText } from "./inject.js";
3
+ import { recentUserText, sessionEventsOf } from "./inject.js";
4
+ import { buildPrefixMessages, detectPlannerRoute, resolvePrefixCache } from "./prefix-cache.js";
4
5
  import { skillQualityGuide } from "./skillquality.js";
5
6
  import { streamText } from "./llm-text.js";
7
+ import { createTokenUsageObserver } from "./token-usage.js";
6
8
  export const PLANNER_SYSTEM_PROMPT = `You are the /evolve continual harness subsystem.
7
9
 
8
10
  Your job is to improve the editable continual harness state. Instead of
@@ -84,10 +86,22 @@ export async function planWithLlm(ctx, options) {
84
86
  const scopeInstruction = options.global
85
87
  ? "Requested scope: global. Only propose stable cross-session lessons, durable preferences, reusable skills/subagents, or explicitly project-qualified facts."
86
88
  : "Requested scope: local. Prefer session-scoped edits for current task progress; global entries are read-only context — do not propose update/delete for them.";
87
- // Ground the plan in the caller's session: the trajectory block is the
88
- // most recent direct user messages ("" when none qualify — the block is
89
- // then omitted entirely, keeping an empty trajectory zero-cost).
90
- const trajectory = options.trajectory ?? recentUserText(agent);
89
+ // Ground the plan in the caller's session: Route A carries the context
90
+ // as a session-derived message prefix (cache-eligible on providers with
91
+ // prompt caching) and drops the auto-extracted trajectory block; Route B
92
+ // keeps the legacy flat-text block. An empty prefix falls back to B.
93
+ const events = options.trajectoryEvents ?? sessionEventsOf(agent);
94
+ const routing = resolvePrefixCache(options.prefixCache);
95
+ const route = detectPlannerRoute(events, routing.mode);
96
+ let prefixMessages = [];
97
+ if (route === "A") {
98
+ prefixMessages = buildPrefixMessages(events, {
99
+ ...(agent.options.provider ? { provider: agent.options.provider } : {}),
100
+ ...(agent.options.model ? { model: agent.options.model } : {}),
101
+ maxChars: routing.maxChars,
102
+ });
103
+ }
104
+ const trajectory = prefixMessages.length > 0 ? (options.trajectory ?? "") : (options.trajectory ?? recentUserText(agent));
91
105
  // The skill quality standard is always present: the skill-creator
92
106
  // template facts when installed, the builtin distilled guide otherwise
93
107
  // (~1KB — planning is low-frequency, and the standard keeps skill
@@ -111,6 +125,10 @@ export async function planWithLlm(ctx, options) {
111
125
  prompt: userPrompt,
112
126
  maxTokens: options.maxOutputTokens ?? 8000,
113
127
  signal: options.signal,
128
+ ...(prefixMessages.length > 0 ? { prefixMessages } : {}),
129
+ ...(options.tokenUsage
130
+ ? { onUsage: createTokenUsageObserver(options.tokenUsage, "planner", agent.options.provider, agent.options.model) }
131
+ : {}),
114
132
  });
115
133
  return parseProposal(text);
116
134
  }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Prefix-cache-aware planning input routing (Route A/B).
3
+ *
4
+ * The planner and the review gate re-send session context on every call as
5
+ * flat text. On providers with prompt caching that context is re-billed at
6
+ * full price each time. Route A instead prepends session-derived messages —
7
+ * the same evidence in structured form, eligible for cache-read pricing —
8
+ * and drops the redundant flat trajectory block. Route B keeps the legacy
9
+ * flat-text input byte-for-byte.
10
+ *
11
+ * Adapted from dsh-continual-harness (Route A/B + cache-detect): this port
12
+ * deliberately does NOT reproduce the host loop's byte-identical cache key
13
+ * (no deriveMessages/tools/sessionId plumbing on this seam), so a hit needs
14
+ * a provider that keys on the message-prefix content. Sources are stamped
15
+ * honestly and every reduction is documented below.
16
+ */
17
+ import { type AssistantMessage, type UserMessage } from "@deepseek-ai/dsh-llm";
18
+ /** Planning input shape: A = session-derived message prefix, B = flat-text trajectory. */
19
+ export type PlannerRoute = "A" | "B";
20
+ /** Routing mode: auto-detect from cache evidence, always prefix, or never. */
21
+ export type PlannerPrefixCacheMode = "auto" | "session" | "off";
22
+ /** Per-call routing overrides; absent fields fall back to the resolved defaults. */
23
+ export interface PrefixCacheOptions {
24
+ mode?: PlannerPrefixCacheMode;
25
+ maxChars?: number;
26
+ }
27
+ /** Default session-prefix budget for one Route A input, in characters. */
28
+ export declare const DEFAULT_PREFIX_MAX_CHARS = 12000;
29
+ /**
30
+ * Resolve per-call overrides against the defaults.
31
+ *
32
+ * @param opts - sparse per-call overrides.
33
+ * @returns the effective mode (default "auto") and char budget.
34
+ * @throws When maxChars is present but not a positive safe integer (fail loud on misconfiguration).
35
+ */
36
+ export declare function resolvePrefixCache(opts?: PrefixCacheOptions): {
37
+ mode: PlannerPrefixCacheMode;
38
+ maxChars: number;
39
+ };
40
+ /**
41
+ * True when any assistant message in this session reported cache-read
42
+ * tokens — evidence the provider can serve a warm prefix.
43
+ *
44
+ * @param events - session log rows, newest last.
45
+ * @returns whether cache evidence exists (false on an empty/unreadable log).
46
+ */
47
+ export declare function hasCacheEvidence(events: readonly unknown[]): boolean;
48
+ /**
49
+ * Pick the planning input route for one call.
50
+ *
51
+ * @param events - session log rows, newest last.
52
+ * @param mode - routing mode (default "auto").
53
+ * @returns "A" when the session prefix should carry the context, else "B".
54
+ */
55
+ export declare function detectPlannerRoute(events: readonly unknown[], mode?: PlannerPrefixCacheMode): PlannerRoute;
56
+ /**
57
+ * Rebuild a tail-biased session prefix from the event log.
58
+ *
59
+ * Inclusion mirrors the trajectory grounding (only direct human `user`
60
+ * speech plus the model's own assistant text), so injected plugin context
61
+ * and tool results never echo back into the planning input. Whole messages
62
+ * are dropped oldest-first over budget; a single over-budget message keeps
63
+ * its tail.
64
+ *
65
+ * @param events - session log rows, newest last.
66
+ * @param opts - provider/model stamps the assistant messages (the calling
67
+ * agent's own route); maxChars caps the prefix (default {@link DEFAULT_PREFIX_MAX_CHARS}).
68
+ * @returns the prefix messages, oldest first ([] when nothing qualifies —
69
+ * callers fall back to Route B).
70
+ * @throws When assistant text exists but provider/model is missing (an
71
+ * assistant message cannot be honestly stamped without its route).
72
+ */
73
+ export declare function buildPrefixMessages(events: readonly unknown[], opts: {
74
+ provider?: string;
75
+ model?: string;
76
+ maxChars?: number;
77
+ }): (UserMessage | AssistantMessage)[];
78
+ //# sourceMappingURL=prefix-cache.d.ts.map
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Prefix-cache-aware planning input routing (Route A/B).
3
+ *
4
+ * The planner and the review gate re-send session context on every call as
5
+ * flat text. On providers with prompt caching that context is re-billed at
6
+ * full price each time. Route A instead prepends session-derived messages —
7
+ * the same evidence in structured form, eligible for cache-read pricing —
8
+ * and drops the redundant flat trajectory block. Route B keeps the legacy
9
+ * flat-text input byte-for-byte.
10
+ *
11
+ * Adapted from dsh-continual-harness (Route A/B + cache-detect): this port
12
+ * deliberately does NOT reproduce the host loop's byte-identical cache key
13
+ * (no deriveMessages/tools/sessionId plumbing on this seam), so a hit needs
14
+ * a provider that keys on the message-prefix content. Sources are stamped
15
+ * honestly and every reduction is documented below.
16
+ */
17
+ import { createAssistantMessage, createUserMessage } from "@deepseek-ai/dsh-llm";
18
+ /** Default session-prefix budget for one Route A input, in characters. */
19
+ export const DEFAULT_PREFIX_MAX_CHARS = 12000;
20
+ /**
21
+ * Resolve per-call overrides against the defaults.
22
+ *
23
+ * @param opts - sparse per-call overrides.
24
+ * @returns the effective mode (default "auto") and char budget.
25
+ * @throws When maxChars is present but not a positive safe integer (fail loud on misconfiguration).
26
+ */
27
+ export function resolvePrefixCache(opts) {
28
+ const mode = opts?.mode ?? "auto";
29
+ const maxChars = opts?.maxChars ?? DEFAULT_PREFIX_MAX_CHARS;
30
+ if (!Number.isSafeInteger(maxChars) || maxChars < 1) {
31
+ throw new Error(`evolve: prefix-cache maxChars must be a positive integer, got ${String(opts?.maxChars)}`);
32
+ }
33
+ return { mode, maxChars };
34
+ }
35
+ /**
36
+ * True when any assistant message in this session reported cache-read
37
+ * tokens — evidence the provider can serve a warm prefix.
38
+ *
39
+ * @param events - session log rows, newest last.
40
+ * @returns whether cache evidence exists (false on an empty/unreadable log).
41
+ */
42
+ export function hasCacheEvidence(events) {
43
+ for (const raw of events) {
44
+ if (typeof raw !== "object" || raw === null)
45
+ continue;
46
+ const event = raw;
47
+ if (event.type !== "assistant/message")
48
+ continue;
49
+ const read = event.data?.usage?.cacheReadTokens;
50
+ if (typeof read === "number" && read > 0)
51
+ return true;
52
+ }
53
+ return false;
54
+ }
55
+ /**
56
+ * Pick the planning input route for one call.
57
+ *
58
+ * @param events - session log rows, newest last.
59
+ * @param mode - routing mode (default "auto").
60
+ * @returns "A" when the session prefix should carry the context, else "B".
61
+ */
62
+ export function detectPlannerRoute(events, mode) {
63
+ const resolved = mode ?? "auto";
64
+ if (resolved === "session")
65
+ return "A";
66
+ if (resolved === "off")
67
+ return "B";
68
+ return hasCacheEvidence(events) ? "A" : "B";
69
+ }
70
+ /**
71
+ * Extract one message's text without depending on the dsh-llm ContentBlock
72
+ * type: string blocks pass through, object blocks contribute their `text`
73
+ * field when present. Non-text blocks (tool calls, images) are dropped.
74
+ */
75
+ function blockText(content) {
76
+ if (typeof content === "string")
77
+ return content;
78
+ if (!Array.isArray(content))
79
+ return "";
80
+ const parts = [];
81
+ for (const block of content) {
82
+ if (typeof block === "string") {
83
+ parts.push(block);
84
+ }
85
+ else if (block !== null && typeof block === "object" && "text" in block && typeof block.text === "string") {
86
+ parts.push(block.text);
87
+ }
88
+ }
89
+ return parts.filter((text) => text.length > 0).join(" ");
90
+ }
91
+ /**
92
+ * Rebuild a tail-biased session prefix from the event log.
93
+ *
94
+ * Inclusion mirrors the trajectory grounding (only direct human `user`
95
+ * speech plus the model's own assistant text), so injected plugin context
96
+ * and tool results never echo back into the planning input. Whole messages
97
+ * are dropped oldest-first over budget; a single over-budget message keeps
98
+ * its tail.
99
+ *
100
+ * @param events - session log rows, newest last.
101
+ * @param opts - provider/model stamps the assistant messages (the calling
102
+ * agent's own route); maxChars caps the prefix (default {@link DEFAULT_PREFIX_MAX_CHARS}).
103
+ * @returns the prefix messages, oldest first ([] when nothing qualifies —
104
+ * callers fall back to Route B).
105
+ * @throws When assistant text exists but provider/model is missing (an
106
+ * assistant message cannot be honestly stamped without its route).
107
+ */
108
+ export function buildPrefixMessages(events, opts) {
109
+ const maxChars = opts.maxChars ?? DEFAULT_PREFIX_MAX_CHARS;
110
+ if (!Number.isSafeInteger(maxChars) || maxChars < 1) {
111
+ throw new Error(`evolve: prefix-cache maxChars must be a positive integer, got ${String(opts.maxChars)}`);
112
+ }
113
+ const pairs = [];
114
+ for (let i = events.length - 1; i >= 0; i -= 1) {
115
+ const raw = events[i];
116
+ if (typeof raw !== "object" || raw === null)
117
+ continue;
118
+ const event = raw;
119
+ const isUser = event.type === "user/message";
120
+ const isAssistant = event.type === "assistant/message";
121
+ if (!isUser && !isAssistant)
122
+ continue;
123
+ if (isUser) {
124
+ const kind = event.data?.source?.kind;
125
+ if (kind !== undefined && kind !== "user")
126
+ continue;
127
+ }
128
+ const text = blockText(event.data?.content).trim();
129
+ if (text.length === 0)
130
+ continue;
131
+ pairs.push({ role: isUser ? "user" : "assistant", text });
132
+ }
133
+ if (pairs.length === 0)
134
+ return [];
135
+ // Collected newest-first; restore conversation order before budgeting.
136
+ pairs.reverse();
137
+ // Tail-biased budget: drop oldest whole messages over budget; a lone
138
+ // over-budget message keeps its tail.
139
+ let total = pairs.reduce((sum, pair) => sum + pair.text.length, 0);
140
+ while (pairs.length > 1 && total > maxChars) {
141
+ const dropped = pairs.shift();
142
+ total -= dropped?.text.length ?? 0;
143
+ }
144
+ const oldest = pairs[0];
145
+ if (oldest !== undefined && total > maxChars) {
146
+ oldest.text = oldest.text.slice(-maxChars);
147
+ }
148
+ const messages = [];
149
+ for (const pair of pairs) {
150
+ if (pair.role === "user") {
151
+ messages.push(createUserMessage({
152
+ source: { kind: "user" },
153
+ content: [{ type: "text", text: pair.text }],
154
+ }));
155
+ }
156
+ else {
157
+ if (!opts.provider || !opts.model) {
158
+ throw new Error("evolve: prefix-cache needs the agent provider/model to stamp assistant messages");
159
+ }
160
+ messages.push(createAssistantMessage({
161
+ source: { provider: opts.provider, model: opts.model },
162
+ content: [{ type: "text", text: pair.text }],
163
+ }));
164
+ }
165
+ }
166
+ return messages;
167
+ }
168
+ //# sourceMappingURL=prefix-cache.js.map
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Serial latest-pending scheduler for automatic review snapshots.
3
+ *
4
+ * This is the DSH equivalent of ZCode's memory extraction scheduler: one
5
+ * runner per session, at most one running snapshot, and one replaceable
6
+ * pending snapshot while the runner is busy. A cursor advances only after a
7
+ * successful or no-op execution; errors and aborts leave it untouched so the
8
+ * next snapshot retries the same durable boundary.
9
+ */
10
+ /** Terminal state of one scheduled snapshot. */
11
+ export type ReviewSchedulerStatus = "success" | "no-op" | "error" | "aborted";
12
+ /** A scheduler execution receives the exact snapshot and its owner signal. */
13
+ export interface ReviewSchedulerExecution<TSnapshot> {
14
+ readonly snapshot: TSnapshot;
15
+ readonly signal: AbortSignal;
16
+ }
17
+ /** Public scheduler surface used by the auto-review driver and tests. */
18
+ export interface ReviewScheduler<TSnapshot> {
19
+ /** Wait until the current run and all coalesced pending work settle. */
20
+ drain(): Promise<void>;
21
+ /** Last successfully processed or skipped snapshot boundary. */
22
+ getCursor(): string | undefined;
23
+ /** Whether a run or a replaceable pending snapshot exists. */
24
+ hasPendingWork(): boolean;
25
+ /** Submit a snapshot (or an async acquisition) without blocking the caller. */
26
+ schedule(snapshot: TSnapshot | Promise<TSnapshot>): void;
27
+ /** Abort the owner signal, drop pending work, and make later schedules no-ops. */
28
+ shutdown(): void;
29
+ }
30
+ /**
31
+ * Create a scheduler with ZCode-compatible coalescing and cursor semantics.
32
+ *
33
+ * @param execute - one serial extraction/review operation
34
+ * @param cursorOf - extracts the durable boundary from a snapshot
35
+ */
36
+ export declare function createReviewScheduler<TSnapshot>(execute: (input: ReviewSchedulerExecution<TSnapshot>) => Promise<ReviewSchedulerStatus>, cursorOf: (snapshot: TSnapshot) => string | undefined): ReviewScheduler<TSnapshot>;
37
+ //# sourceMappingURL=review-scheduler.d.ts.map
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Serial latest-pending scheduler for automatic review snapshots.
3
+ *
4
+ * This is the DSH equivalent of ZCode's memory extraction scheduler: one
5
+ * runner per session, at most one running snapshot, and one replaceable
6
+ * pending snapshot while the runner is busy. A cursor advances only after a
7
+ * successful or no-op execution; errors and aborts leave it untouched so the
8
+ * next snapshot retries the same durable boundary.
9
+ */
10
+ /**
11
+ * Create a scheduler with ZCode-compatible coalescing and cursor semantics.
12
+ *
13
+ * @param execute - one serial extraction/review operation
14
+ * @param cursorOf - extracts the durable boundary from a snapshot
15
+ */
16
+ export function createReviewScheduler(execute, cursorOf) {
17
+ let cursor;
18
+ let latestPending;
19
+ let running;
20
+ let shuttingDown = false;
21
+ const shutdownController = new AbortController();
22
+ const processSnapshot = async (snapshot) => {
23
+ if (shuttingDown || shutdownController.signal.aborted)
24
+ return;
25
+ let status;
26
+ try {
27
+ status = await execute({ snapshot, signal: shutdownController.signal });
28
+ }
29
+ catch {
30
+ // A failed acquisition/extraction is deliberately contained. The
31
+ // cursor remains unchanged and the next snapshot retries it.
32
+ return;
33
+ }
34
+ if (!shuttingDown && (status === "success" || status === "no-op")) {
35
+ cursor = cursorOf(snapshot);
36
+ }
37
+ };
38
+ const run = async (first) => {
39
+ try {
40
+ let current = first;
41
+ while (current && !shuttingDown) {
42
+ const acquisition = await waitForSnapshotAcquisitionOrShutdown(current, shutdownController.signal);
43
+ if (acquisition.status === "shutdown" || shuttingDown)
44
+ break;
45
+ if (acquisition.status === "acquired") {
46
+ await processSnapshot(acquisition.snapshot);
47
+ }
48
+ current = shuttingDown ? undefined : latestPending;
49
+ latestPending = undefined;
50
+ }
51
+ }
52
+ finally {
53
+ if (shuttingDown)
54
+ latestPending = undefined;
55
+ running = undefined;
56
+ }
57
+ };
58
+ return {
59
+ async drain() {
60
+ while (running) {
61
+ await running;
62
+ }
63
+ },
64
+ getCursor() {
65
+ return cursor;
66
+ },
67
+ hasPendingWork() {
68
+ return running !== undefined || latestPending !== undefined;
69
+ },
70
+ schedule(snapshot) {
71
+ if (shuttingDown)
72
+ return;
73
+ const acquisition = acquireSnapshot(snapshot);
74
+ if (running) {
75
+ // Replace, never queue: a burst of turns contributes one latest
76
+ // boundary and the next extraction consumes all intervening rows.
77
+ latestPending = acquisition;
78
+ return;
79
+ }
80
+ running = run(acquisition);
81
+ },
82
+ shutdown() {
83
+ if (shuttingDown)
84
+ return;
85
+ shuttingDown = true;
86
+ latestPending = undefined;
87
+ shutdownController.abort();
88
+ },
89
+ };
90
+ }
91
+ function acquireSnapshot(snapshot) {
92
+ return Promise.resolve(snapshot).then((value) => ({ status: "acquired", snapshot: value }), () => ({ status: "error" }));
93
+ }
94
+ function waitForSnapshotAcquisitionOrShutdown(acquisition, signal) {
95
+ if (signal.aborted)
96
+ return Promise.resolve({ status: "shutdown" });
97
+ return new Promise((resolve) => {
98
+ const cleanup = () => signal.removeEventListener("abort", onAbort);
99
+ const onAbort = () => {
100
+ cleanup();
101
+ resolve({ status: "shutdown" });
102
+ };
103
+ signal.addEventListener("abort", onAbort, { once: true });
104
+ void acquisition.then((result) => {
105
+ cleanup();
106
+ resolve(result);
107
+ });
108
+ });
109
+ }
110
+ //# sourceMappingURL=review-scheduler.js.map
package/lib/review.d.ts CHANGED
@@ -8,12 +8,14 @@
8
8
  import type { Context } from "@deepseek-ai/cordis";
9
9
  import type { Agent } from "@deepseek-ai/dsh-agent";
10
10
  import type { HarnessState, RefinementResult } from "./types.js";
11
+ import { type PrefixCacheOptions } from "./prefix-cache.js";
12
+ import { type TokenUsageTarget } from "./token-usage.js";
11
13
  export interface AutoRefineReview {
12
14
  shouldRefine: boolean;
13
15
  rationale: string;
14
16
  instructions?: string;
15
17
  }
16
- export type AutoRefineReason = "turn_interval" | "compact" | "goal_blocked";
18
+ export type AutoRefineReason = "turn_snapshot" | "turn_interval" | "compact" | "goal_blocked";
17
19
  export interface AutoRefineReviewContext {
18
20
  reason: AutoRefineReason;
19
21
  turnsSinceLastReview: number;
@@ -25,11 +27,22 @@ export interface ReviewOptions {
25
27
  context: AutoRefineReviewContext;
26
28
  /** Serialized trajectory text; when absent the gate is skipped by the caller. */
27
29
  trajectory?: string;
30
+ /** Incremental surface rows captured for this turn snapshot. */
31
+ trajectoryEvents?: readonly unknown[];
28
32
  signal?: AbortSignal;
29
33
  budgetTokens?: number;
30
34
  /** Gap C1: optional provider/model override for the review gate (cheaper model). */
31
35
  overrideProvider?: string;
32
36
  overrideModel?: string;
37
+ /**
38
+ * Prefix-cache routing for the gate input. Route A prepends a
39
+ * session-derived message prefix and drops the flat `<conversation>`
40
+ * block (same source, structured form); Route B keeps it. Absent →
41
+ * auto-detect with the default budget.
42
+ */
43
+ prefixCache?: PrefixCacheOptions;
44
+ /** Direct-call token ledger destination; the review phase is fixed by this module. */
45
+ tokenUsage?: TokenUsageTarget;
33
46
  }
34
47
  export declare const AUTO_REVIEW_SYSTEM_PROMPT = "You are the automatic /evolve review gate.\n\nDecide whether this checkpoint should run /evolve. Auto /evolve writes local\nharness state by default, so approve when the trajectory contains evidence\nuseful to this session's future turns: a repeated failure, a reusable tactic,\na repeated delegation role, a durable fact or preference, a user correction\nthat should persist, or a narrow behavioral policy.\n\nThe current harness state below includes GLOBAL entries (scope=global) plus\nthis session's local entries (scope=local). When a topic is already covered\nby a global entry, do NOT approve a local duplicate of it \u2014 decline and say\nin the rationale that the topic is already covered globally.\n\nReject one-off noise, unsupported hypotheses, transient tool outputs, and\nrequests that carry no reusable content.\n\nStale local entries (superseded, long-unused, obsolete facts) are a valid\nrefine target: approve with instructions naming the entry ids, and tell the\nplanner to archive them (archive hides from injection, data stays restorable)\nrather than delete.\n\nSkill-related trajectories (the evidence concerns creating or improving a\nskill entry) are judged against the DSH skill quality standard (skill-audit\ndimensions: frontmatter routing, the 7 structural features, paragraph\nskeleton, no duplication of the official 11 skills or covered skills).\nApprove only when the trajectory shows a REAL trigger scenario and the\nresulting skill would meet the standard; otherwise decline and say in the\nrationale what must improve \u2014 drafting follows skill-creator, and the\nplanner receives the standard as its <skill_quality_standard> block.\n\nRepeated multi-step workflows (session start/end routines, recurring\nwrap-up or handoff procedures) are a valid refine target: approve with\ninstructions telling the planner to propose a guidance skill (kind=skill,\nskill_kind=guidance \u2014 a SKILL.md document, no python reference). Only\npropose when the same workflow recurs in the trajectory \u2014 never for\none-off flows. Auto-created skills are always offered to the user for a\ndecision before they land; the gate never writes a skill silently.\n\nReturn JSON only:\n{\n \"shouldRefine\": true|false,\n \"rationale\": \"short reason\",\n \"instructions\": \"optional concise instructions for /evolve if shouldRefine is true\"\n}";
35
48
  /** Parse the gate's JSON reply. */