pi-advisor-flow 0.13.1 → 0.13.2-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/index.js +14 -7
  3. package/extensions/index.ts +6 -0
  4. package/package.json +9 -9
  5. package/scripts/check-bun-version.mjs +9 -0
  6. package/src/attachments.ts +36 -0
  7. package/src/child-session.ts +1 -0
  8. package/src/commands/activation-preparation.ts +12 -0
  9. package/src/commands/activation.ts +43 -0
  10. package/src/commands/advisor-tools.ts +3 -0
  11. package/src/commands/lifecycle.ts +15 -0
  12. package/src/commands/manual-command.ts +19 -0
  13. package/src/commands/manual-consultation.ts +20 -0
  14. package/src/commands/manual-progress.ts +13 -0
  15. package/src/commands/model-commands.ts +4 -0
  16. package/src/commands/model-options.ts +18 -0
  17. package/src/commands/model-picker.ts +16 -0
  18. package/src/commands/outcome-stats.ts +9 -0
  19. package/src/commands/registration.ts +1 -0
  20. package/src/commands/renderers.ts +14 -0
  21. package/src/commands/runtime.ts +5 -0
  22. package/src/commands/settings-commands.ts +18 -0
  23. package/src/commands/settings-persistence.ts +2 -0
  24. package/src/commands.ts +1 -0
  25. package/src/config/args.ts +7 -0
  26. package/src/config/defaults.ts +1 -0
  27. package/src/config/schema.ts +1 -0
  28. package/src/config/state.ts +64 -1
  29. package/src/config/storage.ts +37 -0
  30. package/src/config/types.ts +9 -0
  31. package/src/config/validation.ts +7 -0
  32. package/src/config.ts +6 -0
  33. package/src/content-utils.ts +5 -0
  34. package/src/conversation.ts +39 -0
  35. package/src/follow-up.ts +8 -0
  36. package/src/git.ts +8 -0
  37. package/src/herdr-block.ts +7 -0
  38. package/src/herdr-shared.ts +23 -0
  39. package/src/herdr.ts +18 -0
  40. package/src/image-attachments.ts +24 -0
  41. package/src/image-validation.ts +58 -0
  42. package/src/images.ts +24 -0
  43. package/src/jev/base-url.ts +14 -0
  44. package/src/jev/client.ts +49 -0
  45. package/src/jev/decisions-client.ts +14 -0
  46. package/src/jev/decisions-response.ts +38 -0
  47. package/src/jev/endpoint-key-store.ts +18 -0
  48. package/src/jev/key-store.ts +28 -0
  49. package/src/jev/ledger.ts +30 -12
  50. package/src/jev/openai-key-store.ts +17 -0
  51. package/src/jev/questions.ts +15 -0
  52. package/src/jev/state.ts +5 -0
  53. package/src/jev/transport.ts +35 -0
  54. package/src/model-stream.ts +32 -0
  55. package/src/outcome-stats.ts +16 -0
  56. package/src/outcomes.ts +31 -0
  57. package/src/pi-settings.ts +9 -0
  58. package/src/preferences.ts +26 -0
  59. package/src/redaction.ts +8 -0
  60. package/src/scout-context.ts +17 -0
  61. package/src/scout-curation.ts +12 -0
  62. package/src/scout-groups.ts +47 -0
  63. package/src/scout-protocol.ts +17 -0
  64. package/src/scout-reconstruct.ts +12 -0
  65. package/src/scout.ts +47 -0
  66. package/src/session-context.ts +11 -0
  67. package/src/session-state.ts +73 -13
  68. package/src/tool-result-cap.ts +11 -0
  69. package/src/tools/consult-context.ts +24 -0
  70. package/src/tools/consultation.ts +43 -0
  71. package/src/tools/gate-policy.ts +18 -0
  72. package/src/tools/gate-protocol.ts +22 -0
  73. package/src/tools/jev-filter.ts +26 -0
  74. package/src/tools/jev-turn-gate.ts +17 -0
  75. package/src/tools/loop-gate.ts +25 -0
  76. package/src/tools/model-access.ts +12 -0
  77. package/src/tools/outage-notifier.ts +4 -0
  78. package/src/tools/prompts.ts +17 -0
  79. package/src/tools/register-ask-advisor.ts +44 -0
  80. package/src/tools/register-lifecycle.ts +32 -0
  81. package/src/tools/register-outcome.ts +7 -0
  82. package/src/tools/register-renderers.ts +14 -0
  83. package/src/tools/registration.ts +3 -0
  84. package/src/tools/render-advisor-result.ts +44 -0
  85. package/src/tools/render-common.ts +10 -0
  86. package/src/tools/scout-status.ts +34 -0
  87. package/src/tools/session.ts +3 -0
  88. package/src/tools.ts +8 -0
  89. package/src/ui/jev-filter-submenu.ts +21 -0
  90. package/src/ui/jev-provider-submenu-view.ts +22 -0
  91. package/src/ui/jev-provider-submenu.ts +81 -0
  92. package/src/ui/jev-setup-support.ts +26 -0
  93. package/src/ui/manual-dialog-render.ts +20 -0
  94. package/src/ui/manual-dialog.ts +38 -0
  95. package/src/ui/masked-input.ts +2 -0
  96. package/src/ui/searchable-model-list.ts +28 -0
  97. package/src/ui/settings-formatting.ts +26 -0
  98. package/src/ui/settings-items.ts +11 -0
  99. package/src/ui/settings-list-adapter.ts +13 -0
  100. package/src/ui/settings-mutations.ts +60 -0
  101. package/src/ui/settings-selector.ts +30 -0
  102. package/src/ui/text-setting-submenu.ts +9 -0
  103. package/src/ui-guard.ts +1 -0
  104. package/src/ui.ts +3 -0
  105. package/src/usage.ts +31 -0
@@ -23,6 +23,7 @@ import { sessionContextEntries } from "./session-context.ts";
23
23
 
24
24
  // Re-exports preserve scout-context's historical public import surface.
25
25
  export { reconstructScoutConversation } from "./scout-reconstruct.ts";
26
+
26
27
  export type {
27
28
  BuildScoutManifestOptions,
28
29
  ScoutManifest,
@@ -81,6 +82,7 @@ const requiredOverflow = (
81
82
  reason: "required-group-overflow",
82
83
  };
83
84
  }
85
+
84
86
  if (
85
87
  caps.maxConversationChars !== undefined &&
86
88
  contentChars(required) > caps.maxConversationChars
@@ -92,6 +94,7 @@ const requiredOverflow = (
92
94
  reason: "required-group-overflow",
93
95
  };
94
96
  }
97
+
95
98
  return undefined;
96
99
  };
97
100
 
@@ -121,9 +124,11 @@ const fitToBudget = (
121
124
  ): ScoutManifestResult => {
122
125
  const { groups, protocolOmittedBytes, protocolOmittedCount } = built;
123
126
  const availableCount = groups.length + protocolOmittedCount;
127
+
124
128
  const availableBytes =
125
129
  groups.reduce((sum, group) => sum + groupWireBytes(group), 0) +
126
130
  protocolOmittedBytes;
131
+
127
132
  if (caps.maxManifestBytes <= 0) {
128
133
  return {
129
134
  manifest: {
@@ -136,16 +141,21 @@ const fitToBudget = (
136
141
  ok: true,
137
142
  };
138
143
  }
144
+
139
145
  const required = groups.filter((group) => group.required);
140
146
  const overflow = requiredOverflow(required, caps);
147
+
141
148
  if (overflow) {
142
149
  return overflow;
143
150
  }
151
+
144
152
  const selected = groups.filter(
145
153
  (group) => group.required || group.bytes <= caps.maxGroupBytes
146
154
  );
155
+
147
156
  while (!fits(selected, caps)) {
148
157
  const optionalIndex = selected.findIndex((group) => !group.required);
158
+
149
159
  if (optionalIndex === -1) {
150
160
  return {
151
161
  message: "Required Scout context exceeds fixed manifest limits.",
@@ -153,10 +163,13 @@ const fitToBudget = (
153
163
  reason: "required-group-overflow",
154
164
  };
155
165
  }
166
+
156
167
  selected.splice(optionalIndex, 1);
157
168
  }
169
+
158
170
  const selectedIds = new Set(selected.map((group) => group.id));
159
171
  const omitted = groups.filter((group) => !selectedIds.has(group.id));
172
+
160
173
  return {
161
174
  manifest: {
162
175
  availableBytes,
@@ -178,12 +191,16 @@ export const buildScoutManifest = (
178
191
  const entries = sessionContextEntries(ctx);
179
192
  const caps = resolveCaps(options);
180
193
  const indexed = indexToolCalls(entries);
194
+
181
195
  if (!indexed.ok) {
182
196
  return indexed;
183
197
  }
198
+
184
199
  const built = buildGroups(entries, indexed.index, caps);
200
+
185
201
  if (!built.ok) {
186
202
  return built;
187
203
  }
204
+
188
205
  return fitToBudget(built, caps);
189
206
  };
@@ -30,15 +30,18 @@ export const curateAdvisorConversation = async (
30
30
  if (!enabled) {
31
31
  return { conversation: legacyConversation };
32
32
  }
33
+
33
34
  if (maxChars !== undefined && maxChars <= 0) {
34
35
  return { conversation: "" };
35
36
  }
37
+
36
38
  const built = buildScoutManifest(ctx, {
37
39
  currentInvocationId,
38
40
  imageNonce,
39
41
  maxConversationChars: maxChars,
40
42
  maxManifestBytes: SCOUT_MANIFEST_MAX_BYTES,
41
43
  });
44
+
42
45
  if (!built.ok) {
43
46
  const scout: Exclude<ScoutOutcome, { cancelled: true }> = {
44
47
  category: built.reason,
@@ -53,20 +56,27 @@ export const curateAdvisorConversation = async (
53
56
  model: executorRef,
54
57
  ok: false,
55
58
  };
59
+
56
60
  onScout?.({ outcome: scout, type: "fallback" });
61
+
57
62
  return { conversation: legacyConversation, scout };
58
63
  }
64
+
59
65
  const outcome = await runScout(ctx, built.manifest, signal, onScout);
66
+
60
67
  if (!outcome.ok && outcome.cancelled) {
61
68
  throw signal?.reason instanceof Error
62
69
  ? signal.reason
63
70
  : new Error("Advisor operation cancelled during Scout.");
64
71
  }
72
+
65
73
  if (outcome.ok) {
66
74
  const selected = new Set(outcome.selection.selectedIds);
75
+
67
76
  const selectedEntryIds = built.manifest.groups
68
77
  .filter((group) => group.required || selected.has(group.id))
69
78
  .flatMap((group) => group.entryIds ?? []);
79
+
70
80
  const conversation =
71
81
  maxChars === undefined
72
82
  ? outcome.conversation
@@ -76,7 +86,9 @@ export const curateAdvisorConversation = async (
76
86
  outcome.selection.synthesis,
77
87
  maxChars
78
88
  );
89
+
79
90
  return { conversation, scout: outcome, selectedEntryIds };
80
91
  }
92
+
81
93
  return { conversation: legacyConversation, scout: outcome };
82
94
  };
@@ -33,6 +33,7 @@ const boundedLabel = (value: string) =>
33
33
 
34
34
  const labelFor = (kind: ScoutGroupKind, content: string) => {
35
35
  const preview = boundedLabel(content.replace(SPEAKER_PREFIX, ""));
36
+
36
37
  const prefix: Record<ScoutGroupKind, string> = {
37
38
  assistant: "Executor",
38
39
  compaction: "Compaction summary",
@@ -40,6 +41,7 @@ const labelFor = (kind: ScoutGroupKind, content: string) => {
40
41
  "tool-exchange": "Tool exchange",
41
42
  user: "User request",
42
43
  };
44
+
43
45
  return boundedLabel(`${prefix[kind]}: ${preview || "(no text)"}`);
44
46
  };
45
47
 
@@ -87,11 +89,13 @@ const createGroup = (
87
89
  const pendingAdvisorArguments = (part: RecordValue): RecordValue => {
88
90
  const value = isRecord(part.arguments) ? part.arguments : {};
89
91
  const allowed: RecordValue = {};
92
+
90
93
  for (const key of ["gitContext", "question"]) {
91
94
  if (key in value) {
92
95
  allowed[key] = value[key];
93
96
  }
94
97
  }
98
+
95
99
  return allowed;
96
100
  };
97
101
 
@@ -108,7 +112,9 @@ const pendingInvocationDisclosure = (
108
112
  if (!isRecord(entry.message)) {
109
113
  return disclosed;
110
114
  }
115
+
111
116
  const message: RecordValue = entry.message;
117
+
112
118
  const content = contentParts(message.content).map((part) => {
113
119
  if (
114
120
  !isRecord(part) ||
@@ -118,8 +124,10 @@ const pendingInvocationDisclosure = (
118
124
  ) {
119
125
  return part;
120
126
  }
127
+
121
128
  return { ...part, arguments: pendingAdvisorArguments(part) };
122
129
  });
130
+
123
131
  return conversationEntry(
124
132
  { ...entry, message: { ...message, content } },
125
133
  toolResultMaxLines,
@@ -161,6 +169,7 @@ const adjacentResultMismatch = (
161
169
  ) {
162
170
  return `Tool result at context entry ${index + 1} does not match its adjacent assistant group.`;
163
171
  }
172
+
164
173
  return undefined;
165
174
  };
166
175
 
@@ -177,20 +186,26 @@ const collectResults = (
177
186
  ): string | undefined => {
178
187
  for (const [callIndex, callId] of callIds.entries()) {
179
188
  const resultMatch = resultsByCall.get(callId)?.[0];
189
+
180
190
  if (!resultMatch) {
181
191
  missing.add(callId);
182
192
  continue;
183
193
  }
194
+
184
195
  if (resultMatch.index <= index) {
185
196
  return `Tool result at context entry ${resultMatch.index} precedes its assistant call.`;
186
197
  }
198
+
187
199
  const resultMessage = resultMatch.entry.message;
188
200
  const call = calls[callIndex];
189
201
  const expectedName = isString(call.name) ? call.name : "unknown";
202
+
190
203
  if (!isRecord(resultMessage) || resultMessage.toolName !== expectedName) {
191
204
  return `Tool result at context entry ${resultMatch.index} conflicts with call ${callId}.`;
192
205
  }
206
+
193
207
  consumedResultIndexes.add(resultMatch.index);
208
+
194
209
  const resultText = conversationEntry(
195
210
  resultMatch.entry,
196
211
  caps.toolResultMaxLines,
@@ -200,15 +215,18 @@ const collectResults = (
200
215
  true,
201
216
  caps.imageNonce
202
217
  );
218
+
203
219
  if (resultText) {
204
220
  resultParts.push(resultText);
205
221
  }
222
+
206
223
  resultEntryIds.push(
207
224
  isString(resultMatch.entry.id)
208
225
  ? resultMatch.entry.id
209
226
  : String(resultMatch.index)
210
227
  );
211
228
  }
229
+
212
230
  return undefined;
213
231
  };
214
232
 
@@ -224,17 +242,20 @@ const missingOutcome = (
224
242
  caps: DisclosureCaps
225
243
  ): ToolExchangeOutcome => {
226
244
  const { currentInvocationId } = caps;
245
+
227
246
  const pendingCurrentInvocation =
228
247
  currentInvocationId !== undefined &&
229
248
  missing.size === 1 &&
230
249
  missing.has(currentInvocationId) &&
231
250
  callIds.includes(currentInvocationId);
251
+
232
252
  if (!pendingCurrentInvocation) {
233
253
  return {
234
254
  bytes: byteLength([disclosed, ...resultParts].join("\n\n")),
235
255
  kind: "omitted",
236
256
  };
237
257
  }
258
+
238
259
  const pendingDisclosed = pendingInvocationDisclosure(
239
260
  entry,
240
261
  currentInvocationId,
@@ -245,6 +266,7 @@ const missingOutcome = (
245
266
  caps.imageNonce,
246
267
  disclosed
247
268
  );
269
+
248
270
  return {
249
271
  group: createGroup(
250
272
  index,
@@ -275,18 +297,22 @@ const toolExchangeGroup = (
275
297
  ): ToolExchangeOutcome => {
276
298
  const calls = toolCalls(message);
277
299
  const callIds = calls.map(toolCallId).filter(isString);
300
+
278
301
  const adjacentFailure = adjacentResultMismatch(
279
302
  immediate,
280
303
  index,
281
304
  callIds,
282
305
  indexed.callOwners
283
306
  );
307
+
284
308
  if (adjacentFailure) {
285
309
  return { kind: "invalid", message: adjacentFailure };
286
310
  }
311
+
287
312
  const missing = new Set<string>();
288
313
  const resultParts: string[] = [];
289
314
  const resultEntryIds: string[] = [];
315
+
290
316
  const matchFailure = collectResults(
291
317
  calls,
292
318
  callIds,
@@ -298,9 +324,11 @@ const toolExchangeGroup = (
298
324
  resultParts,
299
325
  resultEntryIds
300
326
  );
327
+
301
328
  if (matchFailure) {
302
329
  return { kind: "invalid", message: matchFailure };
303
330
  }
331
+
304
332
  if (missing.size > 0) {
305
333
  return missingOutcome(
306
334
  entry,
@@ -314,6 +342,7 @@ const toolExchangeGroup = (
314
342
  caps
315
343
  );
316
344
  }
345
+
317
346
  return {
318
347
  group: createGroup(
319
348
  index,
@@ -356,12 +385,14 @@ const toolResultOutcome = (
356
385
  if (consumedResultIndexes.has(index)) {
357
386
  return { kind: "skipped" };
358
387
  }
388
+
359
389
  if (ownerOf(message, callOwners)) {
360
390
  return {
361
391
  kind: "invalid",
362
392
  message: `Tool result at context entry ${index} precedes or conflicts with its retained call.`,
363
393
  };
364
394
  }
395
+
365
396
  // Retained results without their call are unavailable optional evidence
366
397
  // and are never offered to Scout.
367
398
  return { bytes: byteLength(disclosed), kind: "omitted" };
@@ -381,13 +412,16 @@ const groupForEntry = (
381
412
  caps: DisclosureCaps
382
413
  ): EntryOutcome => {
383
414
  const entryId = isString(entry.id) ? entry.id : String(index);
415
+
384
416
  if (entry.type !== "message" || !isRecord(entry.message)) {
385
417
  return {
386
418
  group: createGroup(index, [entryId], "compaction", disclosed, false),
387
419
  kind: "group",
388
420
  };
389
421
  }
422
+
390
423
  const { message } = entry;
424
+
391
425
  if (message.role === "user") {
392
426
  return {
393
427
  group: createGroup(
@@ -400,6 +434,7 @@ const groupForEntry = (
400
434
  kind: "group",
401
435
  };
402
436
  }
437
+
403
438
  if (message.role === "toolResult") {
404
439
  return toolResultOutcome(
405
440
  message,
@@ -409,15 +444,18 @@ const groupForEntry = (
409
444
  consumedResultIndexes
410
445
  );
411
446
  }
447
+
412
448
  if (message.role !== "assistant") {
413
449
  return { kind: "skipped" };
414
450
  }
451
+
415
452
  if (!hasCalls(message)) {
416
453
  return {
417
454
  group: createGroup(index, [entryId], "assistant", disclosed, false),
418
455
  kind: "group",
419
456
  };
420
457
  }
458
+
421
459
  return toolExchangeGroup(
422
460
  entry,
423
461
  message,
@@ -448,11 +486,14 @@ export const buildGroups = (
448
486
  const consumedResultIndexes = new Set<number>();
449
487
  let protocolOmittedBytes = 0;
450
488
  let protocolOmittedCount = 0;
489
+
451
490
  for (let index = 0; index < entries.length; index += 1) {
452
491
  const entry = entries[index];
492
+
453
493
  if (!isRecord(entry)) {
454
494
  continue;
455
495
  }
496
+
456
497
  const disclosed = conversationEntry(
457
498
  entry,
458
499
  caps.toolResultMaxLines,
@@ -462,11 +503,14 @@ export const buildGroups = (
462
503
  true,
463
504
  caps.imageNonce
464
505
  );
506
+
465
507
  if (!disclosed) {
466
508
  continue;
467
509
  }
510
+
468
511
  const next = entries[index + 1];
469
512
  const immediate = isRecord(next) ? next : undefined;
513
+
470
514
  const outcome = groupForEntry(
471
515
  entry,
472
516
  index,
@@ -476,9 +520,11 @@ export const buildGroups = (
476
520
  consumedResultIndexes,
477
521
  caps
478
522
  );
523
+
479
524
  if (outcome.kind === "invalid") {
480
525
  return invalid(outcome.message);
481
526
  }
527
+
482
528
  if (outcome.kind === "omitted") {
483
529
  protocolOmittedCount += 1;
484
530
  protocolOmittedBytes += outcome.bytes;
@@ -486,6 +532,7 @@ export const buildGroups = (
486
532
  groups.push(outcome.group);
487
533
  }
488
534
  }
535
+
489
536
  return {
490
537
  groups,
491
538
  ok: true,
@@ -31,16 +31,19 @@ const indexAssistantCalls = (
31
31
  ): InvalidProtocolResult | undefined => {
32
32
  for (const call of toolCalls(message)) {
33
33
  const id = toolCallId(call);
34
+
34
35
  if (!id || callOwners.has(id)) {
35
36
  return invalid(
36
37
  `Assistant tool calls at context entry ${index} have missing or duplicate IDs.`
37
38
  );
38
39
  }
40
+
39
41
  callOwners.set(id, {
40
42
  index,
41
43
  name: isString(call.name) ? call.name : "unknown",
42
44
  });
43
45
  }
46
+
44
47
  return undefined;
45
48
  };
46
49
 
@@ -51,14 +54,17 @@ const indexToolResult = (
51
54
  resultsByCall: Map<string, { entry: RecordValue; index: number }[]>
52
55
  ): InvalidProtocolResult | undefined => {
53
56
  const id = message.toolCallId;
57
+
54
58
  if (!isString(id)) {
55
59
  return invalid(
56
60
  `Tool result at context entry ${index} has no tool-call ID.`
57
61
  );
58
62
  }
63
+
59
64
  const results = resultsByCall.get(id) ?? [];
60
65
  results.push({ entry, index });
61
66
  resultsByCall.set(id, results);
67
+
62
68
  return undefined;
63
69
  };
64
70
 
@@ -72,15 +78,19 @@ const indexEntry = (
72
78
  if (entry.type !== "message" || !isRecord(entry.message)) {
73
79
  return undefined;
74
80
  }
81
+
75
82
  if (entry.message.role === "user" && textFrom(entry.message.content)) {
76
83
  state.latestUserIndex = index;
77
84
  }
85
+
78
86
  if (entry.message.role === "assistant") {
79
87
  return indexAssistantCalls(entry.message, index, callOwners);
80
88
  }
89
+
81
90
  if (entry.message.role === "toolResult") {
82
91
  return indexToolResult(entry, entry.message, index, resultsByCall);
83
92
  }
93
+
84
94
  return undefined;
85
95
  };
86
96
 
@@ -90,13 +100,17 @@ export const indexToolCalls = (
90
100
  entries: unknown[]
91
101
  ): { index: ToolCallIndex; ok: true } | InvalidProtocolResult => {
92
102
  const callOwners = new Map<string, { index: number; name: string }>();
103
+
93
104
  const resultsByCall = new Map<
94
105
  string,
95
106
  { entry: RecordValue; index: number }[]
96
107
  >();
108
+
97
109
  const state = { latestUserIndex: -1 };
110
+
98
111
  for (let index = 0; index < entries.length; index += 1) {
99
112
  const entry = entries[index];
113
+
100
114
  if (isRecord(entry)) {
101
115
  const failure = indexEntry(
102
116
  entry,
@@ -105,16 +119,19 @@ export const indexToolCalls = (
105
119
  resultsByCall,
106
120
  state
107
121
  );
122
+
108
123
  if (failure) {
109
124
  return failure;
110
125
  }
111
126
  }
112
127
  }
128
+
113
129
  for (const [id, results] of resultsByCall) {
114
130
  if (results.length > 1) {
115
131
  return invalid(`Tool call ${id} has duplicate result messages.`);
116
132
  }
117
133
  }
134
+
118
135
  return {
119
136
  index: {
120
137
  callOwners,
@@ -2,12 +2,15 @@ import type { ScoutManifest } from "./scout-types.ts";
2
2
 
3
3
  const prefixWithinCharBudget = (value: string, maxChars: number) => {
4
4
  let result = "";
5
+
5
6
  for (const character of value) {
6
7
  if (result.length + character.length > maxChars) {
7
8
  break;
8
9
  }
10
+
9
11
  result += character;
10
12
  }
13
+
11
14
  return result;
12
15
  };
13
16
 
@@ -18,27 +21,36 @@ export const reconstructScoutConversation = (
18
21
  maxChars = Number.MAX_SAFE_INTEGER
19
22
  ): string => {
20
23
  const selected = new Set(selectedIds);
24
+
21
25
  const evidence = manifest.groups
22
26
  .filter((group) => group.required || selected.has(group.id))
23
27
  .toSorted((left, right) => left.originalIndex - right.originalIndex)
24
28
  .map((group) => group.content);
29
+
25
30
  const evidenceText = evidence.join("\n\n");
31
+
26
32
  if (maxChars <= 0) {
27
33
  return "";
28
34
  }
35
+
29
36
  if (evidenceText.length >= maxChars) {
30
37
  return prefixWithinCharBudget(evidenceText, maxChars);
31
38
  }
39
+
32
40
  const inference = synthesis?.trim()
33
41
  ? `[Scout synthesis — untrusted, non-authoritative inference; not evidence]\n${synthesis.trim()}`
34
42
  : undefined;
43
+
35
44
  if (!inference) {
36
45
  return evidenceText;
37
46
  }
47
+
38
48
  const separator = evidenceText ? "\n\n" : "";
39
49
  const remaining = maxChars - evidenceText.length - separator.length;
50
+
40
51
  if (remaining <= 0) {
41
52
  return evidenceText;
42
53
  }
54
+
43
55
  return `${evidenceText}${separator}${prefixWithinCharBudget(inference, remaining)}`;
44
56
  };