dsh-cross-memory 0.2.2 → 0.2.4
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/README.md +92 -94
- package/docs/DETAIL.md +75 -10
- package/docs/DETAIL.zh.md +74 -13
- package/lib/backfill.js +1 -0
- package/lib/client.js +6 -1
- package/lib/config.js +50 -18
- package/lib/consolidate.js +123 -35
- package/lib/extract.js +133 -25
- package/lib/index.js +210 -45
- package/lib/loops.js +9 -4
- package/lib/render.js +2 -2
- package/lib/search.js +141 -18
- package/lib/storage.js +8 -0
- package/lib/sync.js +96 -1
- package/lib/test/consolidate.test.js +102 -2
- package/lib/test/extract.test.js +59 -2
- package/lib/test/recall.test.js +59 -26
- package/lib/test/retrieval.test.js +46 -1
- package/lib/test/rotation.test.js +5 -2
- package/lib/test/subagent-contract.test.js +8 -0
- package/lib/test/sync.test.js +46 -1
- package/lib/tool.js +9 -1
- package/lib/types/backfill.d.ts +10 -0
- package/lib/types/config.d.ts +49 -15
- package/lib/types/consolidate.d.ts +44 -14
- package/lib/types/extract.d.ts +95 -3
- package/lib/types/index.d.ts +52 -4
- package/lib/types/search.d.ts +68 -0
- package/lib/types/sync.d.ts +12 -0
- package/lib/types/types.d.ts +8 -0
- package/package.json +1 -1
package/lib/config.js
CHANGED
|
@@ -37,10 +37,20 @@ export const DEFAULT_SYNC_INTERVAL_MINUTES = 10;
|
|
|
37
37
|
export const DEFAULT_SYNC_CLOCK = '12:05,18:05';
|
|
38
38
|
/** Default debounce before a burst of memory writes becomes one commit, in seconds. */
|
|
39
39
|
export const DEFAULT_SYNC_DEBOUNCE_SECONDS = 2;
|
|
40
|
-
/**
|
|
41
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Default byte budget for the injected memory summary.
|
|
42
|
+
*
|
|
43
|
+
* 4096 was sized when a store held a few dozen entries. On a real long-lived
|
|
44
|
+
* store the summary is the ONLY projection most conversations ever see, and at
|
|
45
|
+
* 4 KB it could not even list the dozen entries the per-scope cap allowed once
|
|
46
|
+
* the entries were Chinese (measured on the Ornn8 six-persona benchmark: a
|
|
47
|
+
* 5120-byte budget rendered 12 of 149-326 live entries, so 96% of the store was
|
|
48
|
+
* invisible unless the on-demand recall happened to fire). 8192 roughly doubles
|
|
49
|
+
* the rendered set at the cost of one extra cached prefix block per session.
|
|
50
|
+
*/
|
|
51
|
+
export const DEFAULT_MAX_SUMMARY_BYTES = 8192;
|
|
42
52
|
/** Default cap on entries listed in the injected summary per scope. */
|
|
43
|
-
export const DEFAULT_MAX_SUMMARY_ENTRIES =
|
|
53
|
+
export const DEFAULT_MAX_SUMMARY_ENTRIES = 20;
|
|
44
54
|
/**
|
|
45
55
|
* Whether a conversation is told about memories written after its summary.
|
|
46
56
|
*
|
|
@@ -52,6 +62,16 @@ export const DEFAULT_MAX_SUMMARY_ENTRIES = 12;
|
|
|
52
62
|
* paragraph rather than a second summary.
|
|
53
63
|
*/
|
|
54
64
|
export const DEFAULT_INJECT_DELTA = true;
|
|
65
|
+
/**
|
|
66
|
+
* Byte budget for one delta block.
|
|
67
|
+
*
|
|
68
|
+
* 800 was the first guess and it is too tight to be useful: a complete entry
|
|
69
|
+
* renders at 200-350 bytes once the title, kind, tags and a body preview are
|
|
70
|
+
* in, so the block admitted two entries at most and often one. A late
|
|
71
|
+
* conversation that changed topic then got almost nothing. 1600 fits four to
|
|
72
|
+
* six, which is the size of a topic shift rather than of a single note.
|
|
73
|
+
*/
|
|
74
|
+
export const DEFAULT_DELTA_MAX_BYTES = 1600;
|
|
55
75
|
/**
|
|
56
76
|
* How many additional memories one conversation lists by title only.
|
|
57
77
|
*
|
|
@@ -86,9 +106,11 @@ export const DEFAULT_ROTATION_MAX_BYTES = 1600;
|
|
|
86
106
|
* on global entries, and in every session the same six were unrelated to the
|
|
87
107
|
* workspace (a plugin's logger threshold, a sidebar adapter, a model catalog).
|
|
88
108
|
* Four keeps the portable half visible without letting it bury the scope that is
|
|
89
|
-
* actually about this conversation.
|
|
109
|
+
* actually about this conversation. Six follows the per-scope raise: the global
|
|
110
|
+
* half is what every unrelated conversation can use, and four was leaving the
|
|
111
|
+
* same six entries to compete for a budget that now has room.
|
|
90
112
|
*/
|
|
91
|
-
export const DEFAULT_GLOBAL_SUMMARY_ENTRIES =
|
|
113
|
+
export const DEFAULT_GLOBAL_SUMMARY_ENTRIES = 6;
|
|
92
114
|
/**
|
|
93
115
|
* Default byte budget for the global section alone, inside {@link DEFAULT_MAX_SUMMARY_BYTES}.
|
|
94
116
|
*
|
|
@@ -152,12 +174,14 @@ export const DEFAULT_RECALL_MODE = 'on-demand';
|
|
|
152
174
|
* paraphrase that shares two bigrams with a title scores around 9-12, so nothing
|
|
153
175
|
* ever cleared it. The floor is now a strictness knob, and the PRECISION lives in
|
|
154
176
|
* `recallMinTerms` — the structural evidence gate, which a score threshold cannot
|
|
155
|
-
* replace.
|
|
156
|
-
*
|
|
177
|
+
* replace. 6 is deliberately below the 8 a single shared title bigram scores: a
|
|
178
|
+
* one-bigram paraphrase (「北街南街」 against 「南街文具店」) is a real match that
|
|
179
|
+
* the old floor refused, and the term gate plus the hub discount are what keep a
|
|
180
|
+
* shared common word from recalling everything.
|
|
157
181
|
*/
|
|
158
|
-
export const DEFAULT_RECALL_MIN_SCORE =
|
|
182
|
+
export const DEFAULT_RECALL_MIN_SCORE = 6;
|
|
159
183
|
/** Default cap on recall deltas injected into one conversation. */
|
|
160
|
-
export const DEFAULT_RECALL_MAX_PER_CONVERSATION =
|
|
184
|
+
export const DEFAULT_RECALL_MAX_PER_CONVERSATION = 8;
|
|
161
185
|
/**
|
|
162
186
|
* Default byte budget for one recall delta block.
|
|
163
187
|
*
|
|
@@ -166,7 +190,7 @@ export const DEFAULT_RECALL_MAX_PER_CONVERSATION = 4;
|
|
|
166
190
|
* entry costs about 90. At 800 a two-entry block would not fit, which is why
|
|
167
191
|
* this is not 400 as it was when a delta could only ever hold one memory.
|
|
168
192
|
*/
|
|
169
|
-
export const DEFAULT_RECALL_MAX_BYTES =
|
|
193
|
+
export const DEFAULT_RECALL_MAX_BYTES = 2_400;
|
|
170
194
|
/** Default minimum length of a user turn worth a recall decision. */
|
|
171
195
|
export const DEFAULT_RECALL_MIN_QUERY_CHARS = 2;
|
|
172
196
|
/**
|
|
@@ -183,12 +207,16 @@ export const DEFAULT_RAW_RECALL_EXCERPTS = 3;
|
|
|
183
207
|
/**
|
|
184
208
|
* Default number of distinct strong-field terms a recall needs.
|
|
185
209
|
*
|
|
186
|
-
* Two
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
210
|
+
* Two was the old precision gate, but a Chinese paraphrase of a stored fact
|
|
211
|
+
* routinely shares exactly ONE substantive word with it: a benchmark turn asking
|
|
212
|
+
* 「理零票第一步」 against a memory filed as 「整理小票先按日期分堆」 shares only
|
|
213
|
+
* 票, and 「点碗粉加两个小菜」 against 「不吃香菜」 shares only 菜. Two terms made
|
|
214
|
+
* those unreachable no matter how high the relevance score was. One term plus the
|
|
215
|
+
* relevance floor is the gate now; the floor (8) is what keeps a shared common
|
|
216
|
+
* word like 插件 or 日志 from recalling everything, and the entity path in
|
|
217
|
+
* `recallFor` adds a second, stronger signal on top.
|
|
190
218
|
*/
|
|
191
|
-
export const DEFAULT_RECALL_MIN_TERMS =
|
|
219
|
+
export const DEFAULT_RECALL_MIN_TERMS = 1;
|
|
192
220
|
/**
|
|
193
221
|
* Every valid recall mode.
|
|
194
222
|
*
|
|
@@ -332,13 +360,13 @@ export const Config = z.object({
|
|
|
332
360
|
/** Max entries listed for the global scope, which every session shares. */
|
|
333
361
|
globalSummaryEntries: z.number().default(DEFAULT_GLOBAL_SUMMARY_ENTRIES).description('How many memories the GLOBAL section may list, capped below maxSummaryEntries. Both scopes share one byte budget and global renders first, so without this the global half grows to fill whatever the project half leaves (measured: 87% of a small project\'s summary). 0 removes the global section entirely.').volatile(),
|
|
334
362
|
/** Byte budget for the global section alone. */
|
|
335
|
-
globalSummaryBytes: z.number().default(DEFAULT_GLOBAL_SUMMARY_BYTES).description('Byte budget for the GLOBAL section, inside maxSummaryBytes. A count cap is not enough because Chinese entries are ~3 bytes per character:
|
|
363
|
+
globalSummaryBytes: z.number().default(DEFAULT_GLOBAL_SUMMARY_BYTES).description('Byte budget for the GLOBAL section, inside maxSummaryBytes. A count cap is not enough because Chinese entries are ~3 bytes per character: a handful of them still fills a large share of the block. 0 removes the global section entirely.').volatile(),
|
|
336
364
|
/** How many of those entries are reserved for never-listed memories. */
|
|
337
365
|
summaryFreshSlots: z.number().default(DEFAULT_SUMMARY_FRESH_SLOTS).description('How many of each scope\'s summary entries are reserved for memories that have never been listed before, preferring ones written deliberately over extracted ones. 0 leaves selection to the ranking alone, which on a full scope never shows anything new.').volatile(),
|
|
338
366
|
/** How the injected summary is refreshed as the conversation moves on. */
|
|
339
367
|
recallMode: z.string().default(DEFAULT_RECALL_MODE).description('once injects the summary once per conversation; on-demand also injects a small delta when the current turn clearly matches a memory; off disables injection and leaves only the memory tool.').volatile(),
|
|
340
368
|
/** Minimum relevance a recall delta must reach. */
|
|
341
|
-
recallMinScore: z.number().default(DEFAULT_RECALL_MIN_SCORE).description('Minimum relevance a memory must reach before it is injected as a recall delta.
|
|
369
|
+
recallMinScore: z.number().default(DEFAULT_RECALL_MIN_SCORE).description('Minimum relevance a memory must reach before it is injected as a recall delta. Together with recallMinTerms this is the precision gate: with the default of one term, the score floor is what keeps a shared common word like 插件 or 日志 from recalling every memory. Raise it to demand a stronger lexical match.').volatile(),
|
|
342
370
|
/** Cap on recall deltas injected into one conversation. */
|
|
343
371
|
recallMaxPerConversation: z.number().default(DEFAULT_RECALL_MAX_PER_CONVERSATION).description('Maximum recall deltas injected into one conversation, counted per memory. 0 disables them.').volatile(),
|
|
344
372
|
/** Byte budget for one recall delta block. */
|
|
@@ -349,7 +377,7 @@ export const Config = z.object({
|
|
|
349
377
|
/** Shortest user turn worth a recall decision. */
|
|
350
378
|
recallMinQueryChars: z.number().default(DEFAULT_RECALL_MIN_QUERY_CHARS).description('Shortest user turn, in characters, that may trigger a recall delta. Short acknowledgements are skipped instead of scanning the store.').volatile(),
|
|
351
379
|
/** Distinct strong-field terms a recall needs before it is credited. */
|
|
352
|
-
recallMinTerms: z.number().default(DEFAULT_RECALL_MIN_TERMS).description('Distinct query terms that must land in the title, keys, tags, or appliesTo before a memory may be recalled on demand.
|
|
380
|
+
recallMinTerms: z.number().default(DEFAULT_RECALL_MIN_TERMS).description('Distinct query terms that must land in the title, keys, tags, or appliesTo before a memory may be recalled on demand. 1 (the default) admits a one-word paraphrase, which a Chinese rewrite of a stored fact routinely is, and lets the relevance floor do the precision work; 2 demands two shared words and misses single-word paraphrases; 0 disables the check entirely.').volatile(),
|
|
353
381
|
/**
|
|
354
382
|
* Whether memories written after the summary was injected get a small delta.
|
|
355
383
|
*
|
|
@@ -357,6 +385,8 @@ export const Config = z.object({
|
|
|
357
385
|
* the `memory` tool and the on-demand recall path still work.
|
|
358
386
|
*/
|
|
359
387
|
injectDelta: z.boolean().default(DEFAULT_INJECT_DELTA).description('Inject a small block when memories are written after the summary was already shown (at most twice per conversation). Off keeps the summary as the only automatic block.').volatile(),
|
|
388
|
+
/** Byte budget for one delta block. */
|
|
389
|
+
deltaMaxBytes: z.number().default(DEFAULT_DELTA_MAX_BYTES).description('Byte budget for one late-injection delta block. One complete entry costs 200-350 bytes, so 800 admitted one or two; raise it for a longer block, lower it to spend fewer bytes per conversation. 0 disables the delta entirely.').volatile(),
|
|
360
390
|
/**
|
|
361
391
|
* How many extra memories one conversation lists by title only.
|
|
362
392
|
*
|
|
@@ -484,6 +514,7 @@ export const MEMORIES_SETTINGS_DEFAULTS = {
|
|
|
484
514
|
recallMinQueryChars: DEFAULT_RECALL_MIN_QUERY_CHARS,
|
|
485
515
|
recallMinTerms: DEFAULT_RECALL_MIN_TERMS,
|
|
486
516
|
injectDelta: DEFAULT_INJECT_DELTA,
|
|
517
|
+
deltaMaxBytes: DEFAULT_DELTA_MAX_BYTES,
|
|
487
518
|
rotationEntries: DEFAULT_ROTATION_ENTRIES,
|
|
488
519
|
rotationEveryTurns: DEFAULT_ROTATION_EVERY_TURNS,
|
|
489
520
|
maxEntriesPerScope: DEFAULT_MAX_ENTRIES_PER_SCOPE,
|
|
@@ -590,6 +621,7 @@ export function normalizeSettings(input) {
|
|
|
590
621
|
recallMinQueryChars: positive(value.recallMinQueryChars, DEFAULT_RECALL_MIN_QUERY_CHARS, 0),
|
|
591
622
|
recallMinTerms: positive(value.recallMinTerms, DEFAULT_RECALL_MIN_TERMS, 0),
|
|
592
623
|
injectDelta: value.injectDelta ?? DEFAULT_INJECT_DELTA,
|
|
624
|
+
deltaMaxBytes: positive(value.deltaMaxBytes, DEFAULT_DELTA_MAX_BYTES, 0),
|
|
593
625
|
rotationEntries: positive(value.rotationEntries, DEFAULT_ROTATION_ENTRIES, 0),
|
|
594
626
|
rotationEveryTurns: positive(value.rotationEveryTurns, DEFAULT_ROTATION_EVERY_TURNS, 0),
|
|
595
627
|
maxEntriesPerScope: positive(value.maxEntriesPerScope, DEFAULT_MAX_ENTRIES_PER_SCOPE),
|
package/lib/consolidate.js
CHANGED
|
@@ -61,6 +61,10 @@ export const CONSOLIDATE_JSON_SCHEMA = {
|
|
|
61
61
|
title: { type: 'string' },
|
|
62
62
|
body: { type: 'string' },
|
|
63
63
|
tags: { type: 'array', items: { type: 'string' } },
|
|
64
|
+
// The prompt's reply template asks for `keys`; a schema that omits it
|
|
65
|
+
// (with `additionalProperties: false`) rejects every prompt-compliant
|
|
66
|
+
// answer, and the child's whole run ends as an error.
|
|
67
|
+
keys: { type: 'array', items: { type: 'string' } },
|
|
64
68
|
appliesTo: { type: 'string' },
|
|
65
69
|
durability: { type: 'string', enum: ['durable', 'snapshot'] },
|
|
66
70
|
asOf: { type: 'string' },
|
|
@@ -132,20 +136,57 @@ export function renderConsolidationInput(entries, projectLabel, currentPersona)
|
|
|
132
136
|
}
|
|
133
137
|
return blocks.join('\n');
|
|
134
138
|
}
|
|
135
|
-
/**
|
|
136
|
-
|
|
139
|
+
/**
|
|
140
|
+
* Try every `{` in a model reply as the start of a JSON object.
|
|
141
|
+
*
|
|
142
|
+
* A reply is not always pure JSON. A reasoning model sketches the reply shape
|
|
143
|
+
* before it answers ("{\"memories\":[...]}"), and prose around the answer can
|
|
144
|
+
* quote a brace too. Slicing from the FIRST `{` to the last `}` read that sketch
|
|
145
|
+
* as if it were the plan, the parse threw, and the pass reported "no plan" while
|
|
146
|
+
* the child had in fact returned one. Every opening brace is a candidate start
|
|
147
|
+
* instead; the first slice the caller accepts wins.
|
|
148
|
+
*
|
|
149
|
+
* @param text - the model's reply.
|
|
150
|
+
* @param accept - decodes one parsed candidate, or returns `undefined` to keep scanning.
|
|
151
|
+
* @returns the first accepted value, or `undefined` when no candidate is usable.
|
|
152
|
+
*/
|
|
153
|
+
function scanJsonObjects(text, accept) {
|
|
137
154
|
const trimmed = text.trim().replace(/^```(?:json)?\s*/iu, '').replace(/```$/u, '').trim();
|
|
138
|
-
const
|
|
139
|
-
const
|
|
140
|
-
if (
|
|
141
|
-
return undefined;
|
|
142
|
-
let parsed;
|
|
143
|
-
try {
|
|
144
|
-
parsed = JSON.parse(trimmed.slice(start, end + 1));
|
|
145
|
-
}
|
|
146
|
-
catch {
|
|
155
|
+
const first = trimmed.indexOf('{');
|
|
156
|
+
const last = trimmed.lastIndexOf('}');
|
|
157
|
+
if (first < 0 || last <= first)
|
|
147
158
|
return undefined;
|
|
159
|
+
for (let start = first; start >= 0 && start < last; start = trimmed.indexOf('{', start + 1)) {
|
|
160
|
+
let parsed;
|
|
161
|
+
try {
|
|
162
|
+
parsed = JSON.parse(trimmed.slice(start, last + 1));
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const accepted = accept(parsed);
|
|
168
|
+
if (accepted !== undefined)
|
|
169
|
+
return accepted;
|
|
148
170
|
}
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
/** Parse and validate the agent's JSON reply into a plan. */
|
|
174
|
+
export function parsePlan(text, knownIds, maxUpserts) {
|
|
175
|
+
return scanJsonObjects(text, (parsed) => planFromReply(parsed, knownIds, maxUpserts));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Decode one parsed reply object into a validated plan.
|
|
179
|
+
*
|
|
180
|
+
* Split out from {@link parsePlan} so the brace scan can try more than one
|
|
181
|
+
* candidate: a slice that parses but carries no plan (an example the model
|
|
182
|
+
* quoted) must not stop the scan.
|
|
183
|
+
*
|
|
184
|
+
* @param parsed - one parsed JSON value from a candidate slice.
|
|
185
|
+
* @param knownIds - ids present in the reviewed page.
|
|
186
|
+
* @param maxUpserts - cap on returned memories.
|
|
187
|
+
* @returns the validated plan, or `undefined` when this value carries none.
|
|
188
|
+
*/
|
|
189
|
+
function planFromReply(parsed, knownIds, maxUpserts) {
|
|
149
190
|
if (typeof parsed !== 'object' || parsed === null)
|
|
150
191
|
return undefined;
|
|
151
192
|
const record = parsed;
|
|
@@ -395,10 +436,21 @@ export async function runConsolidation(seam, request) {
|
|
|
395
436
|
const input = renderConsolidationInput(request.entries, request.projectLabel, request.persona);
|
|
396
437
|
const timeout = AbortSignal.any([request.signal, AbortSignal.timeout(request.timeoutMs)]);
|
|
397
438
|
const prompt = [{ type: 'text', text: `${CONSOLIDATE_SYSTEM}\n\n---\n\n${input}` }];
|
|
398
|
-
const
|
|
399
|
-
if (
|
|
439
|
+
const answer = await runChild(seam, prompt, request, timeout);
|
|
440
|
+
if (answer === undefined) {
|
|
441
|
+
request.onSkip?.('the subagent seam could not run a tool-free child');
|
|
400
442
|
return undefined;
|
|
401
|
-
|
|
443
|
+
}
|
|
444
|
+
const plan = parsePlan(answer.text, known, request.maxUpserts);
|
|
445
|
+
// "No plan" is not one condition: the child may have answered not at all, or
|
|
446
|
+
// answered without the JSON. Naming which one, and how its run ended, is what
|
|
447
|
+
// turns a silent no-op into a diagnosable failure.
|
|
448
|
+
if (plan === undefined) {
|
|
449
|
+
request.onSkip?.(answer.text.trim().length === 0
|
|
450
|
+
? `the child returned no text (stopReason: ${answer.stopReason ?? 'unknown'})`
|
|
451
|
+
: `the child's reply carried no plan (${answer.text.length} chars, stopReason: ${answer.stopReason ?? 'unknown'})`);
|
|
452
|
+
}
|
|
453
|
+
return plan;
|
|
402
454
|
}
|
|
403
455
|
/**
|
|
404
456
|
* Build the spec the 0.2.1-rc.2 seam takes.
|
|
@@ -424,6 +476,11 @@ export function buildActivationSpec(request, prompt, timeout, outputSchema = CON
|
|
|
424
476
|
// descriptor validator BEFORE the child starts, and the symptom is a
|
|
425
477
|
// silently dead consolidation pass rather than a start error.
|
|
426
478
|
label: CONSOLIDATE_LABEL,
|
|
479
|
+
// The result is consumed here, not by the parent conversation: a settlement
|
|
480
|
+
// notice for this child is noise, and when the child misses
|
|
481
|
+
// `structured_output` that notice reads as a failure even though the answer
|
|
482
|
+
// arrived. See {@link ActivationSpec.delivery}.
|
|
483
|
+
delivery: 'caller',
|
|
427
484
|
// Same class of mistake as the label, one field later: the kernel reads
|
|
428
485
|
// `spec.signal` and never `spec.request.signal`, so it has to be here too.
|
|
429
486
|
signal: timeout,
|
|
@@ -445,7 +502,24 @@ export function buildActivationSpec(request, prompt, timeout, outputSchema = CON
|
|
|
445
502
|
};
|
|
446
503
|
}
|
|
447
504
|
/**
|
|
448
|
-
*
|
|
505
|
+
* Read one child's ending as a single string.
|
|
506
|
+
*
|
|
507
|
+
* The kernel reports `stopReason` as a string ("completed", "error",
|
|
508
|
+
* "max-tokens", …); an older seam wrapped it as `{ kind }`. Both are read here
|
|
509
|
+
* because this value is only ever used for a diagnostic line.
|
|
510
|
+
*
|
|
511
|
+
* @param value - the raw `stopReason`.
|
|
512
|
+
* @returns the reason text, or `undefined` when the seam reported none.
|
|
513
|
+
*/
|
|
514
|
+
function stopReasonText(value) {
|
|
515
|
+
if (typeof value === 'string')
|
|
516
|
+
return value;
|
|
517
|
+
if (typeof value === 'object' && value !== null && 'kind' in value && typeof value.kind === 'string')
|
|
518
|
+
return value.kind;
|
|
519
|
+
return undefined;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Ask the seam for one restricted child and return its answer.
|
|
449
523
|
*
|
|
450
524
|
* The 0.2.1-rc.2 seam is preferred, but only with a preset: a child that names
|
|
451
525
|
* no preset inherits the parent's composition, and the parent's tools are what
|
|
@@ -455,7 +529,7 @@ export function buildActivationSpec(request, prompt, timeout, outputSchema = CON
|
|
|
455
529
|
* @param prompt - the child's single user message.
|
|
456
530
|
* @param request - route, caps, and the preset name.
|
|
457
531
|
* @param timeout - the child's deadline.
|
|
458
|
-
* @returns the child's
|
|
532
|
+
* @returns the child's answer, or `undefined` when the seam cannot run it.
|
|
459
533
|
*/
|
|
460
534
|
async function runChild(seam, prompt, request, timeout, outputSchema = CONSOLIDATE_JSON_SCHEMA) {
|
|
461
535
|
const agentOptions = request.provider !== undefined && request.model !== undefined
|
|
@@ -463,7 +537,8 @@ async function runChild(seam, prompt, request, timeout, outputSchema = CONSOLIDA
|
|
|
463
537
|
: undefined;
|
|
464
538
|
if (typeof seam.startActivation === 'function' && request.agentPreset !== undefined) {
|
|
465
539
|
const run = await seam.startActivation(buildActivationSpec(request, prompt, timeout, outputSchema));
|
|
466
|
-
|
|
540
|
+
const result = await run.result;
|
|
541
|
+
return { text: textOf(result), ...stopReasonOf(result) };
|
|
467
542
|
}
|
|
468
543
|
if (typeof seam.start === 'function') {
|
|
469
544
|
const run = await seam.start('spawn', {
|
|
@@ -477,15 +552,32 @@ async function runChild(seam, prompt, request, timeout, outputSchema = CONSOLIDA
|
|
|
477
552
|
// only output is the JSON plan it returns.
|
|
478
553
|
toolFilter: { deny: request.denyTools },
|
|
479
554
|
});
|
|
480
|
-
|
|
555
|
+
const result = await run.result;
|
|
556
|
+
return { text: textOf(result), ...stopReasonOf(result) };
|
|
481
557
|
}
|
|
482
558
|
return undefined;
|
|
483
559
|
}
|
|
560
|
+
/**
|
|
561
|
+
* Pick the run's reported stop reason out of a finished child result.
|
|
562
|
+
* @param result - the child's finished run.
|
|
563
|
+
* @returns the optional `stopReason` property, or nothing when absent.
|
|
564
|
+
*/
|
|
565
|
+
function stopReasonOf(result) {
|
|
566
|
+
const reason = stopReasonText(result.stopReason);
|
|
567
|
+
return reason === undefined ? {} : { stopReason: reason };
|
|
568
|
+
}
|
|
484
569
|
/**
|
|
485
570
|
* Read one child's answer as text.
|
|
486
571
|
*
|
|
487
572
|
* A child that answered through the structured-output tool carries the plan in
|
|
488
573
|
* `structured`; a child that answered in prose carries it in the text blocks.
|
|
574
|
+
*
|
|
575
|
+
* The kernel's `output` is the child's last assistant message *content*, which
|
|
576
|
+
* includes its reasoning blocks. A reasoning block also carries a `text` field,
|
|
577
|
+
* so reading every block that has one glues the child's scratchpad — full of
|
|
578
|
+
* example JSON braces — in front of the plan and the plan no longer parses.
|
|
579
|
+
* Only blocks the model sent as its answer count here.
|
|
580
|
+
*
|
|
489
581
|
* @param result - the child's finished run.
|
|
490
582
|
* @returns the text the plan is parsed from.
|
|
491
583
|
*/
|
|
@@ -493,7 +585,8 @@ function textOf(result) {
|
|
|
493
585
|
if (typeof result.structured === 'object' && result.structured !== null)
|
|
494
586
|
return JSON.stringify(result.structured);
|
|
495
587
|
return result.output
|
|
496
|
-
.map((block) => (typeof block === 'object' && block !== null && 'text' in block && typeof block.text === 'string' ? block.text : ''))
|
|
588
|
+
.map((block) => (typeof block === 'object' && block !== null && 'type' in block && block.type === 'text' && 'text' in block && typeof block.text === 'string' ? block.text : ''))
|
|
589
|
+
.filter((text) => text.length > 0)
|
|
497
590
|
.join('\n');
|
|
498
591
|
}
|
|
499
592
|
/**
|
|
@@ -540,9 +633,14 @@ export async function runConsolidationDirect(llm, request) {
|
|
|
540
633
|
.filter((block) => block.type === 'text')
|
|
541
634
|
.map((block) => block.text)
|
|
542
635
|
.join('\n');
|
|
543
|
-
if (text.trim().length === 0)
|
|
636
|
+
if (text.trim().length === 0) {
|
|
637
|
+
request.onSkip?.('the model returned an empty reply');
|
|
544
638
|
return undefined;
|
|
545
|
-
|
|
639
|
+
}
|
|
640
|
+
const plan = parsePlan(text, known, request.maxUpserts);
|
|
641
|
+
if (plan === undefined)
|
|
642
|
+
request.onSkip?.(`the model's reply carried no plan (${text.length} chars)`);
|
|
643
|
+
return plan;
|
|
546
644
|
}
|
|
547
645
|
/**
|
|
548
646
|
* Choose which memories one consolidation pass reviews.
|
|
@@ -1042,21 +1140,11 @@ export async function adjudicateDisputes(seam, request, disputes) {
|
|
|
1042
1140
|
const reply = await runChild(seam, prompt, request, timeout, ADJUDICATE_JSON_SCHEMA);
|
|
1043
1141
|
if (reply === undefined)
|
|
1044
1142
|
return [];
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
const
|
|
1048
|
-
if (
|
|
1049
|
-
return [];
|
|
1050
|
-
let parsed;
|
|
1051
|
-
try {
|
|
1052
|
-
parsed = JSON.parse(trimmed.slice(start, end + 1));
|
|
1053
|
-
}
|
|
1054
|
-
catch {
|
|
1055
|
-
return [];
|
|
1056
|
-
}
|
|
1057
|
-
if (typeof parsed !== 'object' || parsed === null)
|
|
1143
|
+
// The same brace scan the plan parse uses: the adjudicator's reply can be
|
|
1144
|
+
// wrapped in prose or preceded by a quoted sketch just like the plan's.
|
|
1145
|
+
const record0 = scanJsonObjects(reply.text, (parsed) => typeof parsed === 'object' && parsed !== null ? parsed : undefined);
|
|
1146
|
+
if (record0 === undefined)
|
|
1058
1147
|
return [];
|
|
1059
|
-
const record0 = parsed;
|
|
1060
1148
|
const rows = Array.isArray(record0['decisions']) ? record0['decisions'] : [];
|
|
1061
1149
|
const known = new Set(disputes.map((dispute) => dispute.id));
|
|
1062
1150
|
const verdicts = [];
|