@capybara-hq/kernel 0.1.0-rc.100 → 0.1.0-rc.101
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/dist/conversations/event-log.d.ts +15 -3
- package/dist/conversations/event-log.js +21 -0
- package/dist/runtime/model-call.d.ts +12 -4
- package/dist/runtime/model-call.js +13 -3
- package/dist/runtime/run-drafts.d.ts +30 -8
- package/dist/runtime/run-drafts.js +74 -10
- package/dist/runtime/write-base.d.ts +110 -27
- package/dist/runtime/write-base.js +349 -90
- package/dist/runtime/write-base.service.d.ts +9 -1
- package/dist/runtime/write-base.service.js +39 -5
- package/dist/tools/builtins/sandbox/write.js +3 -1
- package/package.json +1 -1
|
@@ -35,8 +35,10 @@ export declare const conversationTopic: (conversationId: string) => string;
|
|
|
35
35
|
* tells by it whether it has the step's text from the start (the channel outbound only drafts
|
|
36
36
|
* complete text) and drops what a live draft already covers (spec 2026-11-07 §3.2). Streamed
|
|
37
37
|
* tool calls (streaming tool input spec §4, display only): `tool_start` (`callId`, `name`),
|
|
38
|
-
* `tool_input` (`callId`, `text`, `offset` counted per call)
|
|
39
|
-
* `sha256`: the file a streaming `Write` replaces)
|
|
38
|
+
* `tool_input` (`callId`, `text`, `offset` counted per call), `tool_base` (`callId`, `text`,
|
|
39
|
+
* `sha256`: the file a streaming `Write` replaces; `guess` `{ path }` for a guessed candidate)
|
|
40
|
+
* and `tool_base_verdict` (`callId`, `confirmed`: the path closed on a guess, spec 2026-11-27
|
|
41
|
+
* §4.4);
|
|
40
42
|
* - `ui`:插件经 `ctx.ui.invalidate` 告知这个会话的某个界面贡献变了(不进日志、不是模型可见内容),
|
|
41
43
|
* SSE 转发为 `event: ui`,网页据此重新取视图。
|
|
42
44
|
* - `subagent_progress`: after each step of a sub-agent run, on the topic of **every ancestor**
|
|
@@ -56,12 +58,16 @@ export type ConversationBusMessage = {
|
|
|
56
58
|
kind: "delta";
|
|
57
59
|
runId: string;
|
|
58
60
|
step: number;
|
|
59
|
-
delta: "text" | "thinking" | "tool_start" | "tool_input" | "tool_base" | "step_reset";
|
|
61
|
+
delta: "text" | "thinking" | "tool_start" | "tool_input" | "tool_base" | "tool_base_verdict" | "step_reset";
|
|
60
62
|
text?: string;
|
|
61
63
|
offset?: number;
|
|
62
64
|
callId?: string;
|
|
63
65
|
name?: string;
|
|
64
66
|
sha256?: string;
|
|
67
|
+
guess?: {
|
|
68
|
+
path: string;
|
|
69
|
+
};
|
|
70
|
+
confirmed?: boolean;
|
|
65
71
|
} | {
|
|
66
72
|
kind: "ui";
|
|
67
73
|
plugin: string;
|
|
@@ -189,6 +195,12 @@ export declare class EventLog {
|
|
|
189
195
|
latestOfType<T extends ConversationEventType>(conversationId: string, type: T): Promise<Extract<ConversationEvent, {
|
|
190
196
|
type: T;
|
|
191
197
|
}> | undefined>;
|
|
198
|
+
/**
|
|
199
|
+
* The paths of the conversation's newest recorded file states (`tool_result.view.file`), one per
|
|
200
|
+
* path, newest first, at most `limit`: the candidates of a guessed `Write` base (spec 2026-11-27
|
|
201
|
+
* §4.2). Each path's latest state is what {@link latestFileState} returns for it.
|
|
202
|
+
*/
|
|
203
|
+
recentFilePaths(conversationId: string, limit: number): Promise<string[]>;
|
|
192
204
|
/**
|
|
193
205
|
* The file state (`tool_result.view.file`) the latest `Read` / `Write` / `Edit` result of this
|
|
194
206
|
* conversation recorded for `path` (a normalized sandbox path); null when none did. One index
|
|
@@ -338,6 +338,27 @@ let EventLog = class EventLog {
|
|
|
338
338
|
.executeTakeFirst();
|
|
339
339
|
return r && toEvent(r);
|
|
340
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* The paths of the conversation's newest recorded file states (`tool_result.view.file`), one per
|
|
343
|
+
* path, newest first, at most `limit`: the candidates of a guessed `Write` base (spec 2026-11-27
|
|
344
|
+
* §4.2). Each path's latest state is what {@link latestFileState} returns for it.
|
|
345
|
+
*/
|
|
346
|
+
async recentFilePaths(conversationId, limit) {
|
|
347
|
+
const rows = await this.db
|
|
348
|
+
.selectFrom("conversation_events")
|
|
349
|
+
.select([
|
|
350
|
+
sql `payload->'view'->'file'->>'path'`.as("path"),
|
|
351
|
+
sql `max(seq)`.as("last"),
|
|
352
|
+
])
|
|
353
|
+
.where("conversation_id", "=", conversationId)
|
|
354
|
+
.where("type", "=", "tool_result")
|
|
355
|
+
.where(sql `payload->'view'->'file'->>'path' IS NOT NULL`)
|
|
356
|
+
.groupBy(sql `payload->'view'->'file'->>'path'`)
|
|
357
|
+
.orderBy("last", "desc")
|
|
358
|
+
.limit(limit)
|
|
359
|
+
.execute();
|
|
360
|
+
return rows.map((r) => r.path);
|
|
361
|
+
}
|
|
341
362
|
/**
|
|
342
363
|
* The file state (`tool_result.view.file`) the latest `Read` / `Write` / `Edit` result of this
|
|
343
364
|
* conversation recorded for `path` (a normalized sandbox path); null when none did. One index
|
|
@@ -50,18 +50,26 @@ export declare const contextTooLargeText: (model: ModelInfo, target: number, est
|
|
|
50
50
|
* `stream_end` (the step's model stream completed) and `step_end` (the step's `assistant_message`
|
|
51
51
|
* is logged) carry no delta: the run worker's sink writes and then clears the step's live draft on
|
|
52
52
|
* them (spec 2026-11-07 §3.2) and publishes neither. `tool_start` (`callId`, `name`), `tool_input`
|
|
53
|
-
* (`callId`, `text`: a fragment of the call's JSON input)
|
|
54
|
-
* `sha256`: the file a streaming `Write` replaces
|
|
55
|
-
*
|
|
53
|
+
* (`callId`, `text`: a fragment of the call's JSON input), `tool_base` (`callId`, `text`,
|
|
54
|
+
* `sha256`: the file a streaming `Write` replaces; `guess` when it is a guessed candidate, spec
|
|
55
|
+
* 2026-11-27 §4.4) and `tool_base_verdict` (`callId`, `confirmed`: the path closed on a guess) are
|
|
56
|
+
* display only (streaming tool input spec §4): nothing streamed reaches the log, the model or a
|
|
57
|
+
* hook.
|
|
56
58
|
*/
|
|
57
59
|
export type StreamSink = (delta: {
|
|
58
|
-
kind: "text" | "thinking" | "tool_start" | "tool_input" | "tool_base" | "step_reset" | "stream_end" | "step_end";
|
|
60
|
+
kind: "text" | "thinking" | "tool_start" | "tool_input" | "tool_base" | "tool_base_verdict" | "step_reset" | "stream_end" | "step_end";
|
|
59
61
|
runId: string;
|
|
60
62
|
step: number;
|
|
61
63
|
text?: string;
|
|
62
64
|
callId?: string;
|
|
63
65
|
name?: string;
|
|
64
66
|
sha256?: string;
|
|
67
|
+
/** `tool_base`: the base is a guess, the candidate file at `path`. */
|
|
68
|
+
guess?: {
|
|
69
|
+
path: string;
|
|
70
|
+
};
|
|
71
|
+
/** `tool_base_verdict`: whether the path confirmed the guess. */
|
|
72
|
+
confirmed?: boolean;
|
|
65
73
|
}) => void;
|
|
66
74
|
export interface ModelStepDeps {
|
|
67
75
|
db: KernelSchemaDb;
|
|
@@ -1395,7 +1395,7 @@ async function ladder(deps, input, events, runtime, compaction, tools, wrapUpRea
|
|
|
1395
1395
|
sentAt = Date.now();
|
|
1396
1396
|
// this attempt's pending base reads end with its stream (a reset, the end, an abort)
|
|
1397
1397
|
const baseStop = new AbortController();
|
|
1398
|
-
const bases = baseWatcher(deps, input, tools, baseStop.signal);
|
|
1398
|
+
const bases = baseWatcher(deps, input, tools, baseStop.signal, rung.ref);
|
|
1399
1399
|
let outcome;
|
|
1400
1400
|
try {
|
|
1401
1401
|
outcome = await consumeModelStream(provider, sentRequest, info.streamIdleTimeoutMs ?? runtime.streamIdleMs, signal, (ev) => {
|
|
@@ -1668,7 +1668,7 @@ function classify(err) {
|
|
|
1668
1668
|
* chat or sub-agent run with a reader gets one (an eval replay never reads a device). Its reads
|
|
1669
1669
|
* stop when `stop` fires or the run aborts; a base found in time goes out as `tool_base`.
|
|
1670
1670
|
*/
|
|
1671
|
-
function baseWatcher(deps, input, tools, stop) {
|
|
1671
|
+
function baseWatcher(deps, input, tools, stop, model) {
|
|
1672
1672
|
const reader = deps.writeBase;
|
|
1673
1673
|
const kind = input.conversationKind;
|
|
1674
1674
|
if (reader === undefined || (kind !== "chat" && kind !== "subagent"))
|
|
@@ -1678,8 +1678,18 @@ function baseWatcher(deps, input, tools, stop) {
|
|
|
1678
1678
|
reader,
|
|
1679
1679
|
// the resolved tools carry their source (`ResolvedTool`): only the builtin Write reads
|
|
1680
1680
|
inputViewOf: (name) => writeBaseViewOf(tools.list, name),
|
|
1681
|
-
emit: (callId, base) => sink({
|
|
1681
|
+
emit: (callId, base) => sink({
|
|
1682
|
+
kind: "tool_base",
|
|
1683
|
+
runId,
|
|
1684
|
+
step,
|
|
1685
|
+
callId,
|
|
1686
|
+
text: base.text,
|
|
1687
|
+
sha256: base.sha256,
|
|
1688
|
+
...(base.guess !== undefined && { guess: base.guess }),
|
|
1689
|
+
}),
|
|
1690
|
+
verdict: (callId, confirmed) => sink({ kind: "tool_base_verdict", runId, step, callId, confirmed }),
|
|
1682
1691
|
conversationId,
|
|
1692
|
+
model: { provider: model.provider, model: model.model },
|
|
1683
1693
|
signal: AbortSignal.any([input.signal, stop]),
|
|
1684
1694
|
log: deps.log,
|
|
1685
1695
|
...(deps.writeBaseSettleMs !== undefined && { settleMs: deps.writeBaseSettleMs }),
|
|
@@ -23,11 +23,25 @@ export interface DraftTool {
|
|
|
23
23
|
/** The step's {@link DRAFT_TOOL_INPUT_MAX} was reached: later fragments are not in the draft. */
|
|
24
24
|
inputTruncated?: true;
|
|
25
25
|
/** The file a streaming `Write` replaces (streaming tool input spec §4.1). */
|
|
26
|
-
base?:
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
base?: DraftBase;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A streaming `Write`'s base: exact, or a guess (spec 2026-11-27 §4.4) naming the candidate's path
|
|
30
|
+
* until the path's verdict. A rejected guess stays, marked, so a lagging draft never brings it back.
|
|
31
|
+
*/
|
|
32
|
+
export interface DraftBase {
|
|
33
|
+
text: string;
|
|
34
|
+
sha256: string;
|
|
35
|
+
guess?: {
|
|
36
|
+
path: string;
|
|
29
37
|
};
|
|
38
|
+
verdict?: "confirmed" | "rejected";
|
|
30
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* The base a call keeps when `got` arrives (spec 2026-11-27 §4.4): the first one; an exact base
|
|
42
|
+
* replaces a guess not confirmed; the same guess takes a verdict it lacks.
|
|
43
|
+
*/
|
|
44
|
+
export declare function mergeDraftBase(prev: DraftBase | undefined, got: DraftBase | undefined): DraftBase | undefined;
|
|
31
45
|
/** What one step of a run has streamed so far. */
|
|
32
46
|
export interface RunDraft {
|
|
33
47
|
step: number;
|
|
@@ -73,14 +87,22 @@ export declare class RunDrafts {
|
|
|
73
87
|
*/
|
|
74
88
|
appendToolInput(runId: string, step: number, callId: string, text: string): void;
|
|
75
89
|
/**
|
|
76
|
-
* The file a streaming `Write` replaces; kept
|
|
77
|
-
*
|
|
78
|
-
* a call never started.
|
|
90
|
+
* The file a streaming `Write` replaces (exact, or a guess); kept by {@link mergeDraftBase} and
|
|
91
|
+
* only within {@link DRAFT_TOOL_INPUT_MAX}, which bases share with the input (spec: "input plus
|
|
92
|
+
* base"). Like {@link appendToolInput}, it ignores a call never started.
|
|
79
93
|
*/
|
|
80
94
|
setToolBase(runId: string, step: number, callId: string, base: {
|
|
81
95
|
text: string;
|
|
82
96
|
sha256: string;
|
|
97
|
+
guess?: {
|
|
98
|
+
path: string;
|
|
99
|
+
};
|
|
83
100
|
}): void;
|
|
101
|
+
/**
|
|
102
|
+
* The verdict on a call's guessed base (spec 2026-11-27 §4.4): kept on a guess without one; a
|
|
103
|
+
* call without a guessed base ignores it.
|
|
104
|
+
*/
|
|
105
|
+
setToolBaseVerdict(runId: string, step: number, callId: string, confirmed: boolean): void;
|
|
84
106
|
/** The step's held draft, a fresh one when the run moved to another step. */
|
|
85
107
|
private heldFor;
|
|
86
108
|
/** The held draft changed: written now when the last write is a window old, else held. */
|
|
@@ -111,7 +133,7 @@ export declare class RunDrafts {
|
|
|
111
133
|
/** What {@link streamSink} uses. */
|
|
112
134
|
export interface StreamSinkDeps {
|
|
113
135
|
bus: Pick<ExtensionContext["bus"], "publish">;
|
|
114
|
-
drafts: Pick<RunDrafts, "append" | "flush" | "clear" | "startTool" | "appendToolInput" | "setToolBase">;
|
|
136
|
+
drafts: Pick<RunDrafts, "append" | "flush" | "clear" | "startTool" | "appendToolInput" | "setToolBase" | "setToolBaseVerdict">;
|
|
115
137
|
log: Pick<Logger, "warn">;
|
|
116
138
|
}
|
|
117
139
|
/**
|
|
@@ -119,7 +141,7 @@ export interface StreamSinkDeps {
|
|
|
119
141
|
* (`conv.<id>`, shared with `{ kind: "events" }`, so a subscriber on any replica gets them) with
|
|
120
142
|
* `offset`, the characters of that channel (text or thinking) this attempt of the step streamed
|
|
121
143
|
* before the delta, and keeps the step's live draft. Tool calls (streaming tool input spec §4):
|
|
122
|
-
* `tool_start` and `
|
|
144
|
+
* `tool_start`, `tool_base` and `tool_base_verdict` carry no offset, `tool_input`'s `offset` is the characters of that
|
|
123
145
|
* call's input this attempt streamed before the delta. `step_reset` clears the draft and every
|
|
124
146
|
* offset; `stream_end` / `step_end` only flush / clear the draft and are not published. A failed
|
|
125
147
|
* publish is only logged: deltas are drafts, the `assistant_message` in the log is the truth.
|
|
@@ -25,6 +25,26 @@ export const DRAFT_TOOL_INPUT_MAX = 512 * 1024;
|
|
|
25
25
|
export const draftKey = (runId, step) => `capk:run-draft:${runId}:${step}`;
|
|
26
26
|
/** The steps of a run that have a draft (a set, so a reader finds them without a key scan). */
|
|
27
27
|
const stepsKey = (runId) => `capk:run-draft-steps:${runId}`;
|
|
28
|
+
/**
|
|
29
|
+
* The base a call keeps when `got` arrives (spec 2026-11-27 §4.4): the first one; an exact base
|
|
30
|
+
* replaces a guess not confirmed; the same guess takes a verdict it lacks.
|
|
31
|
+
*/
|
|
32
|
+
export function mergeDraftBase(prev, got) {
|
|
33
|
+
if (got === undefined)
|
|
34
|
+
return prev;
|
|
35
|
+
if (prev === undefined)
|
|
36
|
+
return got;
|
|
37
|
+
// an exact base replaces a guess not confirmed: it is only read after a rejection, so a copy
|
|
38
|
+
// that still shows the guess missed the verdict
|
|
39
|
+
if (prev.guess !== undefined && prev.verdict !== "confirmed" && got.guess === undefined)
|
|
40
|
+
return got;
|
|
41
|
+
if (prev.verdict === undefined &&
|
|
42
|
+
got.verdict !== undefined &&
|
|
43
|
+
prev.sha256 === got.sha256 &&
|
|
44
|
+
prev.guess?.path === got.guess?.path)
|
|
45
|
+
return { ...prev, verdict: got.verdict };
|
|
46
|
+
return prev;
|
|
47
|
+
}
|
|
28
48
|
const isRecord = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
|
|
29
49
|
const parseTool = (x) => {
|
|
30
50
|
if (!isRecord(x) || typeof x.id !== "string" || typeof x.name !== "string")
|
|
@@ -38,7 +58,12 @@ const parseTool = (x) => {
|
|
|
38
58
|
t.inputTruncated = true;
|
|
39
59
|
const b = x.base;
|
|
40
60
|
if (isRecord(b) && typeof b.text === "string" && typeof b.sha256 === "string") {
|
|
41
|
-
|
|
61
|
+
const base = { text: b.text, sha256: b.sha256 };
|
|
62
|
+
if (isRecord(b.guess) && typeof b.guess.path === "string")
|
|
63
|
+
base.guess = { path: b.guess.path };
|
|
64
|
+
if (base.guess !== undefined && (b.verdict === "confirmed" || b.verdict === "rejected"))
|
|
65
|
+
base.verdict = b.verdict;
|
|
66
|
+
t.base = base;
|
|
42
67
|
}
|
|
43
68
|
return [t];
|
|
44
69
|
};
|
|
@@ -122,22 +147,39 @@ let RunDrafts = class RunDrafts {
|
|
|
122
147
|
this.changed(runId, h);
|
|
123
148
|
}
|
|
124
149
|
/**
|
|
125
|
-
* The file a streaming `Write` replaces; kept
|
|
126
|
-
*
|
|
127
|
-
* a call never started.
|
|
150
|
+
* The file a streaming `Write` replaces (exact, or a guess); kept by {@link mergeDraftBase} and
|
|
151
|
+
* only within {@link DRAFT_TOOL_INPUT_MAX}, which bases share with the input (spec: "input plus
|
|
152
|
+
* base"). Like {@link appendToolInput}, it ignores a call never started.
|
|
128
153
|
*/
|
|
129
154
|
setToolBase(runId, step, callId, base) {
|
|
130
155
|
const h = this.heldFor(runId, step);
|
|
131
156
|
const t = h.tools.find((x) => x.id === callId);
|
|
132
|
-
if (t === undefined
|
|
157
|
+
if (t === undefined)
|
|
158
|
+
return;
|
|
159
|
+
const got = { text: base.text, sha256: base.sha256 };
|
|
160
|
+
if (base.guess !== undefined)
|
|
161
|
+
got.guess = { path: base.guess.path };
|
|
162
|
+
if (mergeDraftBase(t.base, got) !== got)
|
|
133
163
|
return;
|
|
134
164
|
const bytes = Buffer.byteLength(base.text);
|
|
135
165
|
if (h.toolBytes + bytes > DRAFT_TOOL_INPUT_MAX)
|
|
136
166
|
return;
|
|
137
|
-
t.base =
|
|
167
|
+
t.base = got;
|
|
138
168
|
h.toolBytes += bytes;
|
|
139
169
|
this.changed(runId, h);
|
|
140
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* The verdict on a call's guessed base (spec 2026-11-27 §4.4): kept on a guess without one; a
|
|
173
|
+
* call without a guessed base ignores it.
|
|
174
|
+
*/
|
|
175
|
+
setToolBaseVerdict(runId, step, callId, confirmed) {
|
|
176
|
+
const h = this.heldFor(runId, step);
|
|
177
|
+
const t = h.tools.find((x) => x.id === callId);
|
|
178
|
+
if (t?.base?.guess === undefined || t.base.verdict !== undefined)
|
|
179
|
+
return;
|
|
180
|
+
t.base = { ...t.base, verdict: confirmed ? "confirmed" : "rejected" };
|
|
181
|
+
this.changed(runId, h);
|
|
182
|
+
}
|
|
141
183
|
/** The step's held draft, a fresh one when the run moved to another step. */
|
|
142
184
|
heldFor(runId, step) {
|
|
143
185
|
let h = this.held.get(runId);
|
|
@@ -278,7 +320,7 @@ export { RunDrafts };
|
|
|
278
320
|
* (`conv.<id>`, shared with `{ kind: "events" }`, so a subscriber on any replica gets them) with
|
|
279
321
|
* `offset`, the characters of that channel (text or thinking) this attempt of the step streamed
|
|
280
322
|
* before the delta, and keeps the step's live draft. Tool calls (streaming tool input spec §4):
|
|
281
|
-
* `tool_start` and `
|
|
323
|
+
* `tool_start`, `tool_base` and `tool_base_verdict` carry no offset, `tool_input`'s `offset` is the characters of that
|
|
282
324
|
* call's input this attempt streamed before the delta. `step_reset` clears the draft and every
|
|
283
325
|
* offset; `stream_end` / `step_end` only flush / clear the draft and are not published. A failed
|
|
284
326
|
* publish is only logged: deltas are drafts, the `assistant_message` in the log is the truth.
|
|
@@ -303,7 +345,10 @@ export function streamSink(conversationId, deps) {
|
|
|
303
345
|
}
|
|
304
346
|
const { runId, step } = d;
|
|
305
347
|
const text = d.text ?? "";
|
|
306
|
-
if (d.kind === "tool_start" ||
|
|
348
|
+
if (d.kind === "tool_start" ||
|
|
349
|
+
d.kind === "tool_input" ||
|
|
350
|
+
d.kind === "tool_base" ||
|
|
351
|
+
d.kind === "tool_base_verdict") {
|
|
307
352
|
const callId = d.callId ?? "";
|
|
308
353
|
if (d.kind === "tool_start") {
|
|
309
354
|
const name = d.name ?? "";
|
|
@@ -316,10 +361,29 @@ export function streamSink(conversationId, deps) {
|
|
|
316
361
|
deps.drafts.appendToolInput(runId, step, callId, text);
|
|
317
362
|
void publish({ kind: "delta", runId, step, delta: "tool_input", callId, text, offset });
|
|
318
363
|
}
|
|
364
|
+
else if (d.kind === "tool_base_verdict") {
|
|
365
|
+
const confirmed = d.confirmed === true;
|
|
366
|
+
deps.drafts.setToolBaseVerdict(runId, step, callId, confirmed);
|
|
367
|
+
void publish({ kind: "delta", runId, step, delta: "tool_base_verdict", callId, confirmed });
|
|
368
|
+
}
|
|
319
369
|
else {
|
|
320
370
|
const sha256 = d.sha256 ?? "";
|
|
321
|
-
|
|
322
|
-
|
|
371
|
+
const guess = d.guess;
|
|
372
|
+
deps.drafts.setToolBase(runId, step, callId, {
|
|
373
|
+
text,
|
|
374
|
+
sha256,
|
|
375
|
+
...(guess !== undefined && { guess }),
|
|
376
|
+
});
|
|
377
|
+
void publish({
|
|
378
|
+
kind: "delta",
|
|
379
|
+
runId,
|
|
380
|
+
step,
|
|
381
|
+
delta: "tool_base",
|
|
382
|
+
callId,
|
|
383
|
+
text,
|
|
384
|
+
sha256,
|
|
385
|
+
...(guess !== undefined && { guess }),
|
|
386
|
+
});
|
|
323
387
|
}
|
|
324
388
|
return;
|
|
325
389
|
}
|
|
@@ -6,37 +6,71 @@ import type { ResolvedTool } from "../tools/registry.js";
|
|
|
6
6
|
* holds the step (and a Stop) past this bound for a display-only base.
|
|
7
7
|
*/
|
|
8
8
|
export declare const WRITE_BASE_SETTLE_MS = 3000;
|
|
9
|
+
/** How a watched property's value is capped: dropped past the cap, or only its head kept. */
|
|
10
|
+
export interface WatchedProp {
|
|
11
|
+
name: string;
|
|
12
|
+
/** The most raw characters kept (default {@link VALUE_MAX_CHARS}). */
|
|
13
|
+
maxChars?: number;
|
|
14
|
+
/** `drop` (default): a longer value is no value; `keep-head`: the head stays readable. */
|
|
15
|
+
overflow?: "drop" | "keep-head";
|
|
16
|
+
}
|
|
9
17
|
/**
|
|
10
|
-
* Reads
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
18
|
+
* Reads top-level string properties of a JSON object streamed in fragments, without re-reading the
|
|
19
|
+
* buffer: each {@link feed} scans only the new fragment with the carried state. For each watched
|
|
20
|
+
* property it records when its string value opened and closed (one event counter, so the order of
|
|
21
|
+
* two properties is known even within one fragment), its raw head while open ({@link prefix}),
|
|
22
|
+
* and its value once the closing quote arrived (a half path is not a path). A property of a nested
|
|
23
|
+
* object or array, a non-string value, a top level that is not an object, or a value above its cap
|
|
24
|
+
* in `drop` mode gives nothing. The first occurrence of a key counts.
|
|
15
25
|
*/
|
|
16
|
-
export declare class
|
|
17
|
-
private readonly prop;
|
|
26
|
+
export declare class TopLevelStrings {
|
|
18
27
|
private depth;
|
|
19
28
|
private inString;
|
|
20
29
|
private escaped;
|
|
21
30
|
/** At depth 1, the next string is a key. */
|
|
22
31
|
private expectKey;
|
|
23
|
-
/** What the string being read is. */
|
|
32
|
+
/** What the string being read is: a key, a watched value, or anything else. */
|
|
24
33
|
private role;
|
|
25
|
-
/** The raw text of the key
|
|
26
|
-
private
|
|
34
|
+
/** The raw text of the key being read. */
|
|
35
|
+
private keyRaw;
|
|
27
36
|
/** The top-level key read last, until its `:`; null when unknown or too long. */
|
|
28
37
|
private key;
|
|
29
38
|
/** The key whose value comes next at depth 1. */
|
|
30
39
|
private valueOf;
|
|
31
|
-
private
|
|
32
|
-
private
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
private events;
|
|
41
|
+
private ended;
|
|
42
|
+
private readonly states;
|
|
43
|
+
constructor(props: readonly WatchedProp[]);
|
|
44
|
+
/** Scans `fragment`. */
|
|
45
|
+
feed(fragment: string): void;
|
|
46
|
+
/** The property's value once its string closed; null before (and when there is none). */
|
|
47
|
+
value(prop: string): string | null;
|
|
48
|
+
/** When the property's string value opened and closed (event numbers; 0: not yet). */
|
|
49
|
+
timing(prop: string): {
|
|
50
|
+
openedAt: number;
|
|
51
|
+
closedAt: number;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* The decoded head of the property's string value so far (open or closed), an unfinished escape
|
|
55
|
+
* at its end left out; null when its value never opened or is dead.
|
|
56
|
+
*/
|
|
57
|
+
prefix(prop: string): string | null;
|
|
36
58
|
private step;
|
|
59
|
+
/** The value about to come at depth 1 is not a string: its property gives nothing. */
|
|
60
|
+
private killValueOf;
|
|
37
61
|
private keep;
|
|
38
62
|
private closeString;
|
|
39
|
-
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Reads the value of one top-level string property of a JSON object streamed in fragments
|
|
66
|
+
* ({@link TopLevelStrings} watching that one property).
|
|
67
|
+
*/
|
|
68
|
+
export declare class TopLevelStringProperty {
|
|
69
|
+
private readonly prop;
|
|
70
|
+
private readonly scan;
|
|
71
|
+
constructor(prop: string);
|
|
72
|
+
/** Scans `fragment`; returns the property's value once known, null before (and when none). */
|
|
73
|
+
feed(fragment: string): string | null;
|
|
40
74
|
}
|
|
41
75
|
/** The value of a top-level string property once its closing quote arrived; null before. */
|
|
42
76
|
export declare function closedStringProperty(json: string, prop: string): string | null;
|
|
@@ -57,31 +91,80 @@ export interface WriteBase {
|
|
|
57
91
|
text: string;
|
|
58
92
|
sha256: string;
|
|
59
93
|
}
|
|
60
|
-
/** Reads
|
|
94
|
+
/** Reads `Write` bases; never throws for a missing base. */
|
|
61
95
|
export interface WriteBaseReader {
|
|
96
|
+
/** The base of `path`; null whenever there is none to show. */
|
|
62
97
|
read(input: {
|
|
63
98
|
conversationId: string;
|
|
64
99
|
path: string;
|
|
65
100
|
signal: AbortSignal;
|
|
66
101
|
}): Promise<WriteBase | null>;
|
|
102
|
+
/**
|
|
103
|
+
* The paths of the conversation's newest recorded file states, one per path, newest first, at
|
|
104
|
+
* most `limit` (the candidates of a guessed base).
|
|
105
|
+
*/
|
|
106
|
+
recentPaths(input: {
|
|
107
|
+
conversationId: string;
|
|
108
|
+
limit: number;
|
|
109
|
+
}): Promise<string[]>;
|
|
110
|
+
/** A model's path as `Write` takes it, normalized; null when it gets no base (synchronous). */
|
|
111
|
+
normalize(rawPath: string): string | null;
|
|
67
112
|
}
|
|
113
|
+
/** The most candidate files a guessed base reads (spec 2026-11-27 §4.2). */
|
|
114
|
+
export declare const WRITE_BASE_CANDIDATES = 3;
|
|
115
|
+
/** The non-blank complete lines of `content` after which the guess is chosen (§4.3). */
|
|
116
|
+
export declare const GUESS_PREFIX_LINES = 8;
|
|
117
|
+
/** The least share of the prefix's lines a candidate must contain to be guessed (§4.3). */
|
|
118
|
+
export declare const GUESS_MIN_SCORE = 0.6;
|
|
119
|
+
/** The least lead of the guessed candidate over the runner-up (§4.3). */
|
|
120
|
+
export declare const GUESS_MIN_MARGIN = 0.2;
|
|
68
121
|
/**
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
122
|
+
* The non-blank lines (trimmed) of a streamed text's head: its complete lines, and the last one
|
|
123
|
+
* too once the text is `complete`.
|
|
124
|
+
*/
|
|
125
|
+
export declare function guessLines(text: string, complete: boolean): string[];
|
|
126
|
+
/**
|
|
127
|
+
* The candidate a guessed base shows (§4.3), or null: the share of `lines` found among a
|
|
128
|
+
* candidate's lines (trimmed) is its score; the best wins with at least {@link GUESS_MIN_SCORE}
|
|
129
|
+
* and a lead of {@link GUESS_MIN_MARGIN} over the runner-up. No lines: no guess.
|
|
130
|
+
*/
|
|
131
|
+
export declare function chooseGuess<T extends {
|
|
132
|
+
text: string;
|
|
133
|
+
}>(lines: readonly string[], candidates: readonly T[]): T | null;
|
|
134
|
+
/**
|
|
135
|
+
* Watches one model stream attempt's tool calls (streaming tool input spec §4.1, the guess of spec
|
|
136
|
+
* 2026-11-27 §4): a call whose tool declares `inputView.kind: "file_write"` gets its base read once
|
|
137
|
+
* its path property closed, without blocking the stream. When the call's `content` starts before
|
|
138
|
+
* its path closed, the conversation's newest recorded files are read as candidates, and once
|
|
139
|
+
* enough of the content streamed the one it matches clearly goes out as a guessed base; when the
|
|
140
|
+
* path closes, a verdict confirms or withdraws it at once (a withdrawn guess is followed by the
|
|
141
|
+
* exact read of the real path). A read that resolves after `signal` aborted (a step reset, the
|
|
142
|
+
* stream's end, the run's abort) emits nothing; a read that throws is logged and dropped. The
|
|
143
|
+
* attempt aborts `signal` and then awaits {@link settle}, so no admission starts after its step
|
|
144
|
+
* moves on (each read checks the signal right before its admission) and a read still running
|
|
145
|
+
* normally ends with it. The wait is bounded ({@link WRITE_BASE_SETTLE_MS}): a read whose
|
|
146
|
+
* provider ignores the abort is logged once and left to end in the background. {@link settle}
|
|
147
|
+
* also logs one `write input order` line per watched call.
|
|
77
148
|
*/
|
|
78
149
|
export declare function writeBaseWatcher(deps: {
|
|
79
150
|
reader: WriteBaseReader;
|
|
80
151
|
inputViewOf(toolName: string): ToolInputView | undefined;
|
|
81
|
-
|
|
152
|
+
/** A base: exact, or a guess naming the candidate's path. */
|
|
153
|
+
emit(callId: string, base: WriteBase & {
|
|
154
|
+
guess?: {
|
|
155
|
+
path: string;
|
|
156
|
+
};
|
|
157
|
+
}): void;
|
|
158
|
+
/** The path closed on a guessed base: `confirmed` when it is the guessed file. */
|
|
159
|
+
verdict(callId: string, confirmed: boolean): void;
|
|
82
160
|
conversationId: string;
|
|
161
|
+
/** The model the attempt streams from (only for the log line). */
|
|
162
|
+
model?: {
|
|
163
|
+
provider: string;
|
|
164
|
+
model: string;
|
|
165
|
+
};
|
|
83
166
|
signal: AbortSignal;
|
|
84
|
-
log: Pick<Logger, "warn">;
|
|
167
|
+
log: Pick<Logger, "warn" | "info">;
|
|
85
168
|
/** The bound of {@link settle} in milliseconds (default {@link WRITE_BASE_SETTLE_MS}). */
|
|
86
169
|
settleMs?: number;
|
|
87
170
|
}): {
|
|
@@ -18,42 +18,69 @@ const VALUE_MAX_CHARS = 8192;
|
|
|
18
18
|
*/
|
|
19
19
|
export const WRITE_BASE_SETTLE_MS = 3_000;
|
|
20
20
|
/**
|
|
21
|
-
* Reads
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
21
|
+
* Reads top-level string properties of a JSON object streamed in fragments, without re-reading the
|
|
22
|
+
* buffer: each {@link feed} scans only the new fragment with the carried state. For each watched
|
|
23
|
+
* property it records when its string value opened and closed (one event counter, so the order of
|
|
24
|
+
* two properties is known even within one fragment), its raw head while open ({@link prefix}),
|
|
25
|
+
* and its value once the closing quote arrived (a half path is not a path). A property of a nested
|
|
26
|
+
* object or array, a non-string value, a top level that is not an object, or a value above its cap
|
|
27
|
+
* in `drop` mode gives nothing. The first occurrence of a key counts.
|
|
26
28
|
*/
|
|
27
|
-
export class
|
|
28
|
-
prop;
|
|
29
|
+
export class TopLevelStrings {
|
|
29
30
|
depth = 0;
|
|
30
31
|
inString = false;
|
|
31
32
|
escaped = false;
|
|
32
33
|
/** At depth 1, the next string is a key. */
|
|
33
34
|
expectKey = false;
|
|
34
|
-
/** What the string being read is. */
|
|
35
|
-
role =
|
|
36
|
-
/** The raw text of the key
|
|
37
|
-
|
|
35
|
+
/** What the string being read is: a key, a watched value, or anything else. */
|
|
36
|
+
role = null;
|
|
37
|
+
/** The raw text of the key being read. */
|
|
38
|
+
keyRaw = "";
|
|
38
39
|
/** The top-level key read last, until its `:`; null when unknown or too long. */
|
|
39
40
|
key = null;
|
|
40
41
|
/** The key whose value comes next at depth 1. */
|
|
41
42
|
valueOf = null;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
events = 0;
|
|
44
|
+
ended = false;
|
|
45
|
+
states = new Map();
|
|
46
|
+
constructor(props) {
|
|
47
|
+
for (const spec of props) {
|
|
48
|
+
this.states.set(spec.name, {
|
|
49
|
+
spec,
|
|
50
|
+
state: { raw: "", openedAt: 0, closedAt: 0, value: null, dead: false, cut: false },
|
|
51
|
+
});
|
|
52
|
+
}
|
|
46
53
|
}
|
|
47
|
-
/** Scans `fragment
|
|
54
|
+
/** Scans `fragment`. */
|
|
48
55
|
feed(fragment) {
|
|
49
|
-
if (this.
|
|
50
|
-
return
|
|
56
|
+
if (this.ended)
|
|
57
|
+
return;
|
|
51
58
|
for (const c of fragment) {
|
|
52
59
|
this.step(c);
|
|
53
|
-
if (this.
|
|
60
|
+
if (this.ended)
|
|
54
61
|
break;
|
|
55
62
|
}
|
|
56
|
-
|
|
63
|
+
}
|
|
64
|
+
/** The property's value once its string closed; null before (and when there is none). */
|
|
65
|
+
value(prop) {
|
|
66
|
+
return this.states.get(prop)?.state.value ?? null;
|
|
67
|
+
}
|
|
68
|
+
/** When the property's string value opened and closed (event numbers; 0: not yet). */
|
|
69
|
+
timing(prop) {
|
|
70
|
+
const s = this.states.get(prop)?.state;
|
|
71
|
+
return { openedAt: s?.openedAt ?? 0, closedAt: s?.closedAt ?? 0 };
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The decoded head of the property's string value so far (open or closed), an unfinished escape
|
|
75
|
+
* at its end left out; null when its value never opened or is dead.
|
|
76
|
+
*/
|
|
77
|
+
prefix(prop) {
|
|
78
|
+
const s = this.states.get(prop)?.state;
|
|
79
|
+
if (s === undefined || s.openedAt === 0 || s.dead)
|
|
80
|
+
return null;
|
|
81
|
+
if (s.value !== null)
|
|
82
|
+
return s.value;
|
|
83
|
+
return decodeHead(s.raw);
|
|
57
84
|
}
|
|
58
85
|
step(c) {
|
|
59
86
|
if (this.inString) {
|
|
@@ -74,25 +101,32 @@ export class TopLevelStringProperty {
|
|
|
74
101
|
return;
|
|
75
102
|
}
|
|
76
103
|
switch (c) {
|
|
77
|
-
case '"':
|
|
104
|
+
case '"': {
|
|
78
105
|
this.inString = true;
|
|
79
|
-
this.
|
|
80
|
-
if (this.depth
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
this.role = "
|
|
84
|
-
|
|
85
|
-
|
|
106
|
+
this.role = null;
|
|
107
|
+
if (this.depth !== 1)
|
|
108
|
+
return;
|
|
109
|
+
if (this.expectKey) {
|
|
110
|
+
this.role = { kind: "key" };
|
|
111
|
+
this.keyRaw = "";
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const w = this.valueOf === null ? undefined : this.states.get(this.valueOf);
|
|
115
|
+
if (w !== undefined && w.state.openedAt === 0 && !w.state.dead) {
|
|
116
|
+
w.state.openedAt = ++this.events;
|
|
117
|
+
this.role = { kind: "value", ...w };
|
|
118
|
+
}
|
|
86
119
|
return;
|
|
120
|
+
}
|
|
87
121
|
case "{":
|
|
88
122
|
case "[":
|
|
89
123
|
// the top level must be an object
|
|
90
124
|
if (this.depth === 0 && c === "[") {
|
|
91
|
-
this.
|
|
125
|
+
this.ended = true;
|
|
92
126
|
return;
|
|
93
127
|
}
|
|
94
128
|
if (this.depth === 1)
|
|
95
|
-
this.
|
|
129
|
+
this.killValueOf();
|
|
96
130
|
this.depth++;
|
|
97
131
|
if (this.depth === 1)
|
|
98
132
|
this.expectKey = true;
|
|
@@ -100,9 +134,8 @@ export class TopLevelStringProperty {
|
|
|
100
134
|
case "}":
|
|
101
135
|
case "]":
|
|
102
136
|
this.depth--;
|
|
103
|
-
// the object ended without the property
|
|
104
137
|
if (this.depth <= 0)
|
|
105
|
-
this.
|
|
138
|
+
this.ended = true;
|
|
106
139
|
return;
|
|
107
140
|
case ":":
|
|
108
141
|
if (this.depth === 1) {
|
|
@@ -118,42 +151,83 @@ export class TopLevelStringProperty {
|
|
|
118
151
|
}
|
|
119
152
|
return;
|
|
120
153
|
default:
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
this.
|
|
154
|
+
if (isJsonSpace(c))
|
|
155
|
+
return;
|
|
156
|
+
// a number or literal: a watched property with it has no string value
|
|
157
|
+
if (this.depth === 1)
|
|
158
|
+
this.killValueOf();
|
|
159
|
+
else if (this.depth === 0)
|
|
160
|
+
this.ended = true;
|
|
126
161
|
}
|
|
127
162
|
}
|
|
163
|
+
/** The value about to come at depth 1 is not a string: its property gives nothing. */
|
|
164
|
+
killValueOf() {
|
|
165
|
+
const w = this.valueOf === null ? undefined : this.states.get(this.valueOf);
|
|
166
|
+
if (w !== undefined && w.state.openedAt === 0)
|
|
167
|
+
w.state.dead = true;
|
|
168
|
+
this.valueOf = null;
|
|
169
|
+
}
|
|
128
170
|
keep(c) {
|
|
129
|
-
|
|
171
|
+
const role = this.role;
|
|
172
|
+
if (role === null)
|
|
173
|
+
return;
|
|
174
|
+
if (role.kind === "key") {
|
|
175
|
+
this.keyRaw += c;
|
|
176
|
+
if (this.keyRaw.length > KEY_MAX_CHARS)
|
|
177
|
+
this.role = null;
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const { state, spec } = role;
|
|
181
|
+
if (state.cut)
|
|
182
|
+
return;
|
|
183
|
+
if (state.raw.length >= (spec.maxChars ?? VALUE_MAX_CHARS)) {
|
|
184
|
+
if ((spec.overflow ?? "drop") === "drop") {
|
|
185
|
+
state.dead = true;
|
|
186
|
+
state.raw = "";
|
|
187
|
+
this.role = null;
|
|
188
|
+
}
|
|
189
|
+
else
|
|
190
|
+
state.cut = true;
|
|
130
191
|
return;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
this.stop();
|
|
134
|
-
else if (this.role === "key" && this.raw.length > KEY_MAX_CHARS)
|
|
135
|
-
this.role = "other";
|
|
192
|
+
}
|
|
193
|
+
state.raw += c;
|
|
136
194
|
}
|
|
137
195
|
closeString() {
|
|
138
196
|
this.inString = false;
|
|
139
197
|
const role = this.role;
|
|
140
|
-
this.role =
|
|
198
|
+
this.role = null;
|
|
141
199
|
if (this.depth !== 1)
|
|
142
200
|
return;
|
|
143
201
|
if (this.expectKey) {
|
|
144
202
|
this.expectKey = false;
|
|
145
|
-
this.key = role === "key" ? decode(this.
|
|
203
|
+
this.key = role?.kind === "key" ? decode(this.keyRaw) : null;
|
|
146
204
|
return;
|
|
147
205
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
206
|
+
const w = this.valueOf === null ? undefined : this.states.get(this.valueOf);
|
|
207
|
+
// a watched value that went dead at its cap still closes (its order stays known)
|
|
208
|
+
if (w !== undefined && w.state.openedAt !== 0 && w.state.closedAt === 0) {
|
|
209
|
+
w.state.closedAt = ++this.events;
|
|
210
|
+
if (role?.kind === "value" && !w.state.cut)
|
|
211
|
+
w.state.value = decode(w.state.raw);
|
|
152
212
|
}
|
|
153
213
|
this.valueOf = null;
|
|
154
214
|
}
|
|
155
|
-
|
|
156
|
-
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Reads the value of one top-level string property of a JSON object streamed in fragments
|
|
218
|
+
* ({@link TopLevelStrings} watching that one property).
|
|
219
|
+
*/
|
|
220
|
+
export class TopLevelStringProperty {
|
|
221
|
+
prop;
|
|
222
|
+
scan;
|
|
223
|
+
constructor(prop) {
|
|
224
|
+
this.prop = prop;
|
|
225
|
+
this.scan = new TopLevelStrings([{ name: prop }]);
|
|
226
|
+
}
|
|
227
|
+
/** Scans `fragment`; returns the property's value once known, null before (and when none). */
|
|
228
|
+
feed(fragment) {
|
|
229
|
+
this.scan.feed(fragment);
|
|
230
|
+
return this.scan.value(this.prop);
|
|
157
231
|
}
|
|
158
232
|
}
|
|
159
233
|
/** JSON whitespace (RFC 8259): space, tab, line feed, carriage return. */
|
|
@@ -168,6 +242,18 @@ function decode(raw) {
|
|
|
168
242
|
return null;
|
|
169
243
|
}
|
|
170
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* The decoded head of a JSON string body that may stop anywhere: an unfinished escape at its end
|
|
247
|
+
* (a lone backslash, a short `\u` sequence, half of a surrogate pair) is left out.
|
|
248
|
+
*/
|
|
249
|
+
function decodeHead(raw) {
|
|
250
|
+
for (let cut = 0; cut <= 6 && cut <= raw.length; cut++) {
|
|
251
|
+
const v = decode(raw.slice(0, raw.length - cut));
|
|
252
|
+
if (v !== null)
|
|
253
|
+
return v;
|
|
254
|
+
}
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
171
257
|
/** The value of a top-level string property once its closing quote arrived; null before. */
|
|
172
258
|
export function closedStringProperty(json, prop) {
|
|
173
259
|
return new TopLevelStringProperty(prop).feed(json);
|
|
@@ -183,43 +269,176 @@ export function writeBaseViewOf(tools, name) {
|
|
|
183
269
|
return undefined;
|
|
184
270
|
return t.spec.inputView?.kind === "file_write" ? t.spec.inputView : undefined;
|
|
185
271
|
}
|
|
272
|
+
/** The most candidate files a guessed base reads (spec 2026-11-27 §4.2). */
|
|
273
|
+
export const WRITE_BASE_CANDIDATES = 3;
|
|
274
|
+
/** The non-blank complete lines of `content` after which the guess is chosen (§4.3). */
|
|
275
|
+
export const GUESS_PREFIX_LINES = 8;
|
|
276
|
+
/** The least share of the prefix's lines a candidate must contain to be guessed (§4.3). */
|
|
277
|
+
export const GUESS_MIN_SCORE = 0.6;
|
|
278
|
+
/** The least lead of the guessed candidate over the runner-up (§4.3). */
|
|
279
|
+
export const GUESS_MIN_MARGIN = 0.2;
|
|
280
|
+
/** The raw characters of `content` the scanner keeps for the guess (only its head matters). */
|
|
281
|
+
const CONTENT_HEAD_CHARS = 64 * 1024;
|
|
282
|
+
/**
|
|
283
|
+
* The non-blank lines (trimmed) of a streamed text's head: its complete lines, and the last one
|
|
284
|
+
* too once the text is `complete`.
|
|
285
|
+
*/
|
|
286
|
+
export function guessLines(text, complete) {
|
|
287
|
+
const lines = text.split("\n");
|
|
288
|
+
if (!complete)
|
|
289
|
+
lines.pop();
|
|
290
|
+
return lines.map((l) => l.trim()).filter((l) => l !== "");
|
|
291
|
+
}
|
|
186
292
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
293
|
+
* The candidate a guessed base shows (§4.3), or null: the share of `lines` found among a
|
|
294
|
+
* candidate's lines (trimmed) is its score; the best wins with at least {@link GUESS_MIN_SCORE}
|
|
295
|
+
* and a lead of {@link GUESS_MIN_MARGIN} over the runner-up. No lines: no guess.
|
|
296
|
+
*/
|
|
297
|
+
export function chooseGuess(lines, candidates) {
|
|
298
|
+
if (lines.length === 0)
|
|
299
|
+
return null;
|
|
300
|
+
const scored = candidates
|
|
301
|
+
.map((c) => {
|
|
302
|
+
const have = new Set(c.text.split("\n").map((l) => l.trim()));
|
|
303
|
+
const hits = lines.filter((l) => have.has(l)).length;
|
|
304
|
+
return { c, score: hits / lines.length };
|
|
305
|
+
})
|
|
306
|
+
.sort((a, b) => b.score - a.score);
|
|
307
|
+
const [best, next] = scored;
|
|
308
|
+
if (best === undefined || best.score < GUESS_MIN_SCORE)
|
|
309
|
+
return null;
|
|
310
|
+
if (next !== undefined && best.score - next.score < GUESS_MIN_MARGIN)
|
|
311
|
+
return null;
|
|
312
|
+
return best.c;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Watches one model stream attempt's tool calls (streaming tool input spec §4.1, the guess of spec
|
|
316
|
+
* 2026-11-27 §4): a call whose tool declares `inputView.kind: "file_write"` gets its base read once
|
|
317
|
+
* its path property closed, without blocking the stream. When the call's `content` starts before
|
|
318
|
+
* its path closed, the conversation's newest recorded files are read as candidates, and once
|
|
319
|
+
* enough of the content streamed the one it matches clearly goes out as a guessed base; when the
|
|
320
|
+
* path closes, a verdict confirms or withdraws it at once (a withdrawn guess is followed by the
|
|
321
|
+
* exact read of the real path). A read that resolves after `signal` aborted (a step reset, the
|
|
322
|
+
* stream's end, the run's abort) emits nothing; a read that throws is logged and dropped. The
|
|
323
|
+
* attempt aborts `signal` and then awaits {@link settle}, so no admission starts after its step
|
|
324
|
+
* moves on (each read checks the signal right before its admission) and a read still running
|
|
325
|
+
* normally ends with it. The wait is bounded ({@link WRITE_BASE_SETTLE_MS}): a read whose
|
|
326
|
+
* provider ignores the abort is logged once and left to end in the background. {@link settle}
|
|
327
|
+
* also logs one `write input order` line per watched call.
|
|
195
328
|
*/
|
|
196
329
|
export function writeBaseWatcher(deps) {
|
|
197
|
-
const
|
|
330
|
+
const calls = new Map();
|
|
198
331
|
/** The reads in flight (each removes itself when it finishes). */
|
|
199
332
|
const inFlight = new Set();
|
|
200
333
|
/** Call ids already seen: a repeated start never starts a second read. */
|
|
201
334
|
const seen = new Set();
|
|
202
335
|
let warnedStray = false;
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
deps.
|
|
336
|
+
let logged = false;
|
|
337
|
+
const track = (p) => {
|
|
338
|
+
inFlight.add(p);
|
|
339
|
+
void p.finally(() => inFlight.delete(p));
|
|
340
|
+
};
|
|
341
|
+
const warnFailed = (callId, err) => {
|
|
342
|
+
if (deps.signal.aborted)
|
|
343
|
+
return;
|
|
344
|
+
deps.log.warn("write base not shown: the read failed", {
|
|
345
|
+
conversationId: deps.conversationId,
|
|
346
|
+
callId,
|
|
347
|
+
err,
|
|
348
|
+
});
|
|
349
|
+
};
|
|
350
|
+
const readExact = (callId, path) => {
|
|
351
|
+
track((async () => {
|
|
352
|
+
try {
|
|
353
|
+
const base = await deps.reader.read({
|
|
354
|
+
conversationId: deps.conversationId,
|
|
355
|
+
path,
|
|
356
|
+
signal: deps.signal,
|
|
357
|
+
});
|
|
358
|
+
if (base === null || deps.signal.aborted)
|
|
359
|
+
return;
|
|
360
|
+
deps.emit(callId, base);
|
|
361
|
+
}
|
|
362
|
+
catch (err) {
|
|
363
|
+
warnFailed(callId, err);
|
|
364
|
+
}
|
|
365
|
+
})());
|
|
366
|
+
};
|
|
367
|
+
/** Chooses and emits the guess when enough content streamed and the candidates are read. */
|
|
368
|
+
const tryGuess = (callId, c) => {
|
|
369
|
+
if (c.guess !== "pending" || c.candidates === null || deps.signal.aborted)
|
|
370
|
+
return;
|
|
371
|
+
const head = c.scan.prefix(c.view.content);
|
|
372
|
+
if (head === null)
|
|
373
|
+
return;
|
|
374
|
+
const complete = c.scan.timing(c.view.content).closedAt !== 0;
|
|
375
|
+
const lines = guessLines(head, complete);
|
|
376
|
+
if (!complete && lines.length < GUESS_PREFIX_LINES)
|
|
377
|
+
return;
|
|
378
|
+
if (c.candidates.length === 0) {
|
|
379
|
+
c.guess = "no_candidates";
|
|
380
|
+
return;
|
|
213
381
|
}
|
|
214
|
-
|
|
215
|
-
|
|
382
|
+
const pick = chooseGuess(lines.slice(0, GUESS_PREFIX_LINES), c.candidates.map((x) => ({
|
|
383
|
+
...x,
|
|
384
|
+
text: x.base.text,
|
|
385
|
+
})));
|
|
386
|
+
if (pick === null) {
|
|
387
|
+
c.guess = "no_match";
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
c.guessed = { path: pick.path };
|
|
391
|
+
c.guess = "guessed";
|
|
392
|
+
deps.emit(callId, {
|
|
393
|
+
text: pick.base.text,
|
|
394
|
+
sha256: pick.base.sha256,
|
|
395
|
+
guess: { path: pick.path },
|
|
396
|
+
});
|
|
397
|
+
};
|
|
398
|
+
const readCandidates = (callId, c) => {
|
|
399
|
+
track((async () => {
|
|
400
|
+
try {
|
|
401
|
+
const paths = await deps.reader.recentPaths({
|
|
402
|
+
conversationId: deps.conversationId,
|
|
403
|
+
limit: WRITE_BASE_CANDIDATES,
|
|
404
|
+
});
|
|
405
|
+
const read = await Promise.all(paths.map(async (path) => {
|
|
406
|
+
if (deps.signal.aborted)
|
|
407
|
+
return null;
|
|
408
|
+
const base = await deps.reader.read({
|
|
409
|
+
conversationId: deps.conversationId,
|
|
410
|
+
path,
|
|
411
|
+
signal: deps.signal,
|
|
412
|
+
});
|
|
413
|
+
return base === null ? null : { path, base };
|
|
414
|
+
}));
|
|
415
|
+
if (deps.signal.aborted)
|
|
416
|
+
return;
|
|
417
|
+
c.candidates = read.filter((x) => x !== null);
|
|
418
|
+
// the path may have decided while the candidates were read: then no guess
|
|
419
|
+
if (!c.pathDone)
|
|
420
|
+
tryGuess(callId, c);
|
|
421
|
+
}
|
|
422
|
+
catch (err) {
|
|
423
|
+
if (c.guess === "pending")
|
|
424
|
+
c.guess = "no_candidates";
|
|
425
|
+
warnFailed(callId, err);
|
|
426
|
+
}
|
|
427
|
+
})());
|
|
428
|
+
};
|
|
429
|
+
/** The call's path closed: verdict on a guess, else the exact read as without one. */
|
|
430
|
+
const onPath = (callId, c, rawPath) => {
|
|
431
|
+
c.pathDone = true;
|
|
432
|
+
if (c.guessed !== null) {
|
|
433
|
+
const confirmed = deps.reader.normalize(rawPath) === c.guessed.path;
|
|
434
|
+
c.guess = confirmed ? "confirmed" : "rejected";
|
|
435
|
+
deps.verdict(callId, confirmed);
|
|
436
|
+
if (confirmed)
|
|
216
437
|
return;
|
|
217
|
-
deps.log.warn("write base not shown: the read failed", {
|
|
218
|
-
conversationId: deps.conversationId,
|
|
219
|
-
callId,
|
|
220
|
-
err,
|
|
221
|
-
});
|
|
222
438
|
}
|
|
439
|
+
else if (c.guess === "pending")
|
|
440
|
+
c.guess = "path_first_won";
|
|
441
|
+
readExact(callId, rawPath);
|
|
223
442
|
};
|
|
224
443
|
return {
|
|
225
444
|
onStart(callId, name) {
|
|
@@ -229,26 +448,66 @@ export function writeBaseWatcher(deps) {
|
|
|
229
448
|
const view = deps.inputViewOf(name);
|
|
230
449
|
if (view?.kind !== "file_write")
|
|
231
450
|
return;
|
|
232
|
-
|
|
451
|
+
calls.set(callId, {
|
|
452
|
+
scan: new TopLevelStrings([
|
|
453
|
+
{ name: view.path },
|
|
454
|
+
{ name: view.content, maxChars: CONTENT_HEAD_CHARS, overflow: "keep-head" },
|
|
455
|
+
]),
|
|
456
|
+
view: { path: view.path, content: view.content },
|
|
457
|
+
order: "unknown",
|
|
458
|
+
pathDone: false,
|
|
459
|
+
guess: "none",
|
|
460
|
+
candidates: null,
|
|
461
|
+
guessed: null,
|
|
462
|
+
});
|
|
233
463
|
},
|
|
234
464
|
onInput(callId, fragment) {
|
|
235
|
-
const
|
|
236
|
-
if (
|
|
237
|
-
return;
|
|
238
|
-
if (deps.signal.aborted) {
|
|
239
|
-
watching.delete(callId);
|
|
465
|
+
const c = calls.get(callId);
|
|
466
|
+
if (c === undefined || c.pathDone || deps.signal.aborted)
|
|
240
467
|
return;
|
|
468
|
+
c.scan.feed(fragment);
|
|
469
|
+
const path = c.scan.timing(c.view.path);
|
|
470
|
+
const content = c.scan.timing(c.view.content);
|
|
471
|
+
if (c.order === "unknown") {
|
|
472
|
+
if (content.openedAt !== 0 && (path.closedAt === 0 || content.openedAt < path.closedAt)) {
|
|
473
|
+
c.order = "content_first";
|
|
474
|
+
c.guess = "pending";
|
|
475
|
+
readCandidates(callId, c);
|
|
476
|
+
}
|
|
477
|
+
else if (path.closedAt !== 0)
|
|
478
|
+
c.order = "path_first";
|
|
241
479
|
}
|
|
242
|
-
|
|
243
|
-
|
|
480
|
+
if (path.closedAt !== 0) {
|
|
481
|
+
const raw = c.scan.value(c.view.path);
|
|
482
|
+
// a path too long, or not decodable: no base, as before
|
|
483
|
+
if (raw === null) {
|
|
484
|
+
c.pathDone = true;
|
|
485
|
+
if (c.guessed !== null) {
|
|
486
|
+
c.guess = "rejected";
|
|
487
|
+
deps.verdict(callId, false);
|
|
488
|
+
}
|
|
489
|
+
else if (c.guess === "pending")
|
|
490
|
+
c.guess = "path_first_won";
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
onPath(callId, c, raw);
|
|
244
494
|
return;
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const pending = read(callId, path);
|
|
248
|
-
inFlight.add(pending);
|
|
249
|
-
void pending.finally(() => inFlight.delete(pending));
|
|
495
|
+
}
|
|
496
|
+
tryGuess(callId, c);
|
|
250
497
|
},
|
|
251
498
|
async settle() {
|
|
499
|
+
if (!logged) {
|
|
500
|
+
logged = true;
|
|
501
|
+
for (const c of calls.values()) {
|
|
502
|
+
deps.log.info("write input order", {
|
|
503
|
+
conversationId: deps.conversationId,
|
|
504
|
+
...(deps.model !== undefined && deps.model),
|
|
505
|
+
order: c.order,
|
|
506
|
+
// a guess the stream ended on before its verdict, or one never chosen
|
|
507
|
+
guess: c.guess === "pending" || c.guess === "guessed" ? "unfinished" : c.guess,
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
}
|
|
252
511
|
if (inFlight.size === 0)
|
|
253
512
|
return;
|
|
254
513
|
const boundMs = deps.settleMs ?? WRITE_BASE_SETTLE_MS;
|
|
@@ -18,7 +18,7 @@ export interface WriteBaseReadDeps {
|
|
|
18
18
|
claim(): Readonly<ExecutionClaim> | undefined;
|
|
19
19
|
/** The run's device and actor, or null when the run may not read one now (never binds). */
|
|
20
20
|
runFacts(claim: Readonly<ExecutionClaim>): Promise<WriteBaseRunFacts | null>;
|
|
21
|
-
eventLog: Pick<EventLog, "latestFileState">;
|
|
21
|
+
eventLog: Pick<EventLog, "latestFileState" | "recentFilePaths">;
|
|
22
22
|
sandbox: Pick<SandboxService, "withKernelRead">;
|
|
23
23
|
log: Pick<KernelLogger, "warn" | "debug">;
|
|
24
24
|
}
|
|
@@ -42,6 +42,8 @@ export declare function readWriteBase(deps: WriteBaseReadDeps, input: {
|
|
|
42
42
|
path: string;
|
|
43
43
|
signal: AbortSignal;
|
|
44
44
|
}): Promise<WriteBase | null>;
|
|
45
|
+
/** {@link normalizeWriteBasePath} that gives null instead of throwing (a guess's verdict). */
|
|
46
|
+
export declare function normalizeWritePath(raw: string): string | null;
|
|
45
47
|
/**
|
|
46
48
|
* The failures that only mean "no base now": the device's instance is not running, the read was
|
|
47
49
|
* not admitted (the run moved on, the authority refused it) or its runtime cannot enforce a
|
|
@@ -65,4 +67,10 @@ export declare class WriteBaseService implements WriteBaseReader {
|
|
|
65
67
|
path: string;
|
|
66
68
|
signal: AbortSignal;
|
|
67
69
|
}): Promise<WriteBase | null>;
|
|
70
|
+
/** The conversation's newest recorded file paths (spec 2026-11-27 §4.2); none on a failure. */
|
|
71
|
+
recentPaths(input: {
|
|
72
|
+
conversationId: string;
|
|
73
|
+
limit: number;
|
|
74
|
+
}): Promise<string[]>;
|
|
75
|
+
normalize(rawPath: string): string | null;
|
|
68
76
|
}
|
|
@@ -38,11 +38,8 @@ import { currentExecutionClaim } from "./run-execution.js";
|
|
|
38
38
|
export async function readWriteBase(deps, input) {
|
|
39
39
|
const { conversationId, signal } = input;
|
|
40
40
|
try {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
checkFilePathShape(input.path);
|
|
44
|
-
const path = normalizeSandboxPath(checkToolPath(input.path, "file_path"));
|
|
45
|
-
if (path === "/")
|
|
41
|
+
const path = normalizeWriteBasePath(input.path);
|
|
42
|
+
if (path === null)
|
|
46
43
|
return null;
|
|
47
44
|
const claim = deps.claim();
|
|
48
45
|
if (!claim || claim.conversationId !== conversationId)
|
|
@@ -100,6 +97,27 @@ export async function readWriteBase(deps, input) {
|
|
|
100
97
|
return null;
|
|
101
98
|
}
|
|
102
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* A model's `Write` path as a base read takes it: absolute, shaped and allowed as `Write` checks it
|
|
102
|
+
* (`checkFilePathShape`, `checkToolPath`), normalized; null for a relative path or the root.
|
|
103
|
+
* Throws the `ToolError` `Write` would refuse it with.
|
|
104
|
+
*/
|
|
105
|
+
function normalizeWriteBasePath(raw) {
|
|
106
|
+
if (!raw.startsWith("/"))
|
|
107
|
+
return null;
|
|
108
|
+
checkFilePathShape(raw);
|
|
109
|
+
const path = normalizeSandboxPath(checkToolPath(raw, "file_path"));
|
|
110
|
+
return path === "/" ? null : path;
|
|
111
|
+
}
|
|
112
|
+
/** {@link normalizeWriteBasePath} that gives null instead of throwing (a guess's verdict). */
|
|
113
|
+
export function normalizeWritePath(raw) {
|
|
114
|
+
try {
|
|
115
|
+
return normalizeWriteBasePath(raw);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
103
121
|
/** `data` as UTF-8 text, or null when it is not valid UTF-8. */
|
|
104
122
|
function utf8(data) {
|
|
105
123
|
try {
|
|
@@ -172,6 +190,22 @@ let WriteBaseService = class WriteBaseService {
|
|
|
172
190
|
read(input) {
|
|
173
191
|
return readWriteBase(this.deps, input);
|
|
174
192
|
}
|
|
193
|
+
/** The conversation's newest recorded file paths (spec 2026-11-27 §4.2); none on a failure. */
|
|
194
|
+
async recentPaths(input) {
|
|
195
|
+
try {
|
|
196
|
+
return await this.deps.eventLog.recentFilePaths(input.conversationId, input.limit);
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
this.deps.log.warn("write base candidates not listed", {
|
|
200
|
+
conversationId: input.conversationId,
|
|
201
|
+
err,
|
|
202
|
+
});
|
|
203
|
+
return [];
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
normalize(rawPath) {
|
|
207
|
+
return normalizeWritePath(rawPath);
|
|
208
|
+
}
|
|
175
209
|
};
|
|
176
210
|
WriteBaseService = __decorate([
|
|
177
211
|
Injectable(),
|
|
@@ -20,7 +20,9 @@ const SPEC = {
|
|
|
20
20
|
file_path: {
|
|
21
21
|
type: "string",
|
|
22
22
|
minLength: 1,
|
|
23
|
-
description: "Absolute path of the file, e.g. report.md in the workspace directory."
|
|
23
|
+
description: "Absolute path of the file, e.g. report.md in the workspace directory. Give it before " +
|
|
24
|
+
"`content`: the user sees the write as a live diff against the current file once the " +
|
|
25
|
+
"path is known.",
|
|
24
26
|
},
|
|
25
27
|
content: { type: "string", description: "The complete new content of the file." },
|
|
26
28
|
},
|
package/package.json
CHANGED