@moxxy/sdk 0.14.2 → 0.14.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.
Files changed (44) hide show
  1. package/dist/compactor-helpers.d.ts.map +1 -1
  2. package/dist/compactor-helpers.js +9 -5
  3. package/dist/compactor-helpers.js.map +1 -1
  4. package/dist/mode/collect-stream.d.ts +68 -0
  5. package/dist/mode/collect-stream.d.ts.map +1 -0
  6. package/dist/mode/collect-stream.js +181 -0
  7. package/dist/mode/collect-stream.js.map +1 -0
  8. package/dist/mode/project-messages.d.ts +84 -0
  9. package/dist/mode/project-messages.d.ts.map +1 -0
  10. package/dist/mode/project-messages.js +392 -0
  11. package/dist/mode/project-messages.js.map +1 -0
  12. package/dist/mode/single-shot.d.ts +17 -0
  13. package/dist/mode/single-shot.d.ts.map +1 -0
  14. package/dist/mode/single-shot.js +53 -0
  15. package/dist/mode/single-shot.js.map +1 -0
  16. package/dist/mode/stable-hash.d.ts +7 -0
  17. package/dist/mode/stable-hash.d.ts.map +1 -0
  18. package/dist/mode/stable-hash.js +20 -0
  19. package/dist/mode/stable-hash.js.map +1 -0
  20. package/dist/mode/stuck-loop.d.ts +39 -0
  21. package/dist/mode/stuck-loop.d.ts.map +1 -0
  22. package/dist/mode/stuck-loop.js +51 -0
  23. package/dist/mode/stuck-loop.js.map +1 -0
  24. package/dist/mode-helpers.d.ts +15 -175
  25. package/dist/mode-helpers.d.ts.map +1 -1
  26. package/dist/mode-helpers.js +14 -666
  27. package/dist/mode-helpers.js.map +1 -1
  28. package/dist/tunnel.d.ts.map +1 -1
  29. package/dist/tunnel.js +11 -1
  30. package/dist/tunnel.js.map +1 -1
  31. package/package.json +1 -1
  32. package/src/compactor-helpers.test.ts +21 -0
  33. package/src/compactor-helpers.ts +10 -5
  34. package/src/mode/collect-stream.ts +247 -0
  35. package/src/mode/project-messages.test.ts +121 -0
  36. package/src/mode/project-messages.ts +461 -0
  37. package/src/mode/single-shot.ts +63 -0
  38. package/src/mode/stable-hash.ts +20 -0
  39. package/src/mode/stuck-loop.ts +89 -0
  40. package/src/mode-helpers.ts +32 -850
  41. package/src/mode.test.ts +20 -0
  42. package/src/token-accounting.test.ts +90 -0
  43. package/src/tunnel.test.ts +21 -0
  44. package/src/tunnel.ts +10 -1
@@ -0,0 +1,461 @@
1
+ import type { ContentBlock, ProviderMessage } from '../provider.js';
2
+ import type { ModeContext } from '../mode.js';
3
+ import type { Skill } from '../skill.js';
4
+ import type { CompactionEvent, MoxxyEvent, UserPromptEvent } from '../events.js';
5
+ import {
6
+ computeElisionState,
7
+ conversationalStub,
8
+ conversationalStubbed,
9
+ toolResultBytes,
10
+ toolResultStub,
11
+ toolResultStubbed,
12
+ type ElisionState,
13
+ } from '../elision-state.js';
14
+ import { isToolDisplayResult } from '../tool-display.js';
15
+
16
+ /** Appended to the system prompt while elision is active (see projection). */
17
+ export const ELISION_SYSTEM_NOTE =
18
+ 'Context note: to stay within budget, older turns may appear as stubs like ' +
19
+ '`[output elided — recall("id") to view]` or `[elided user turn · recall({ seq: N })]`. ' +
20
+ 'These are NOT the real content — call the `recall` tool with the given id/seq to fetch ' +
21
+ 'the full text before relying on any detail from an elided turn. Recent turns are always ' +
22
+ 'shown verbatim.';
23
+
24
+ /**
25
+ * Compose a model-facing system prompt that includes any base prompt
26
+ * plus a COMPACT skill index (name + description + triggers only).
27
+ *
28
+ * Lazy-loading design: the body is intentionally NOT inlined. The model
29
+ * matches user intent against the description/triggers, then calls the
30
+ * `load_skill` tool to fetch the body of the skill it picked. This keeps
31
+ * the system prompt small even with many skills installed and avoids
32
+ * paying for skill bodies the model never actually follows.
33
+ */
34
+ export function buildSystemPromptWithSkills(
35
+ baseSystemPrompt: string | undefined,
36
+ skills: ReadonlyArray<Skill>,
37
+ ): string | undefined {
38
+ if (skills.length === 0) return baseSystemPrompt;
39
+ const header =
40
+ `## Available skills\n\n` +
41
+ `Each line below is a pre-authored playbook for a specific intent. ` +
42
+ `When the user's request matches one of these (by name, description, ` +
43
+ `or triggers), call \`load_skill({ name: "<skill-name>" })\` FIRST to ` +
44
+ `fetch the full instructions, then follow them verbatim. Prefer using ` +
45
+ `a skill over re-deriving the workflow with ad-hoc tools.\n`;
46
+ const entries = skills
47
+ .map((s) => {
48
+ const fm = s.frontmatter;
49
+ const triggerHint = fm.triggers?.length
50
+ ? ` (triggers: ${fm.triggers.map((t) => `"${t}"`).join(', ')})`
51
+ : '';
52
+ return `- **${fm.name}** — ${fm.description}${triggerHint}`;
53
+ })
54
+ .join('\n');
55
+ const skillBlock = `${header}\n${entries}`;
56
+ return baseSystemPrompt ? `${baseSystemPrompt}\n\n${skillBlock}` : skillBlock;
57
+ }
58
+
59
+ export interface ProjectMessagesOptions {
60
+ /** Optional system prompt; emitted as the first message when set. */
61
+ readonly systemPrompt?: string;
62
+ /** Optional trailing user message — useful for plan-execute's "Focus on this step now: X". */
63
+ readonly trailingUserText?: string;
64
+ }
65
+
66
+ interface CompactionRange {
67
+ readonly from: number;
68
+ readonly to: number;
69
+ readonly summary: string;
70
+ }
71
+
72
+ function activeCompactionRanges(events: ReadonlyArray<MoxxyEvent>): ReadonlyArray<CompactionRange> {
73
+ return events
74
+ .filter((event): event is CompactionEvent =>
75
+ event.type === 'compaction' &&
76
+ event.tokensSaved > 0 &&
77
+ event.summary.trim().length > 0 &&
78
+ event.replacedRange[0] <= event.replacedRange[1],
79
+ )
80
+ .map((event) => ({
81
+ from: event.replacedRange[0],
82
+ to: event.replacedRange[1],
83
+ summary: event.summary,
84
+ }));
85
+ }
86
+
87
+ function eventInCompactionRange(
88
+ seq: number,
89
+ ranges: ReadonlyArray<CompactionRange>,
90
+ ): CompactionRange | null {
91
+ for (const range of ranges) {
92
+ if (seq >= range.from && seq <= range.to) return range;
93
+ }
94
+ return null;
95
+ }
96
+
97
+ /**
98
+ * A compaction lookup that answers "which range (if any) contains `seq`" in
99
+ * O(log ranges) instead of {@link eventInCompactionRange}'s O(ranges) linear
100
+ * scan per event. Compaction ranges are non-overlapping ascending seq prefixes,
101
+ * so a seq belongs to at most one range and binary search over the
102
+ * sorted-by-`from` array returns the SAME range the linear first-match did —
103
+ * byte-identical projection.
104
+ *
105
+ * Defensive fallback: if the ranges are NOT strictly non-overlapping (which the
106
+ * compaction invariant forbids, but a hand-crafted/corrupt log could violate),
107
+ * we keep the exact linear first-match semantics so the projection can never
108
+ * diverge from the old code.
109
+ */
110
+ function makeCompactionLookup(
111
+ ranges: ReadonlyArray<CompactionRange>,
112
+ ): (seq: number) => CompactionRange | null {
113
+ if (ranges.length === 0) return () => null;
114
+ if (ranges.length === 1) {
115
+ const only = ranges[0]!;
116
+ return (seq) => (seq >= only.from && seq <= only.to ? only : null);
117
+ }
118
+ // Sort a copy by `from` (stable enough — ranges are non-overlapping). Verify
119
+ // the non-overlap invariant on the sorted copy; only then is binary search
120
+ // provably equivalent to the linear first-match.
121
+ const sorted = [...ranges].sort((a, b) => a.from - b.from);
122
+ let nonOverlapping = true;
123
+ for (let i = 1; i < sorted.length; i++) {
124
+ if (sorted[i]!.from <= sorted[i - 1]!.to) {
125
+ nonOverlapping = false;
126
+ break;
127
+ }
128
+ }
129
+ if (!nonOverlapping) return (seq) => eventInCompactionRange(seq, ranges);
130
+ return (seq) => {
131
+ // Largest `from <= seq`, then a single containment check.
132
+ let lo = 0;
133
+ let hi = sorted.length - 1;
134
+ let cand = -1;
135
+ while (lo <= hi) {
136
+ const mid = (lo + hi) >> 1;
137
+ if (sorted[mid]!.from <= seq) {
138
+ cand = mid;
139
+ lo = mid + 1;
140
+ } else {
141
+ hi = mid - 1;
142
+ }
143
+ }
144
+ if (cand < 0) return null;
145
+ const range = sorted[cand]!;
146
+ return seq <= range.to ? range : null;
147
+ };
148
+ }
149
+
150
+ /**
151
+ * Pure projection of a single `user_prompt` event to its content blocks.
152
+ *
153
+ * Extracted from {@link projectMessages} as an independently testable sub-step
154
+ * (u123-5). Returns either the collapsed stub (when the event is an elided
155
+ * conversational turn) or the full text + attachment-expanded blocks — exactly
156
+ * what the inline switch arm produced. Caller decides how to wrap it as a
157
+ * message / record the stable prefix.
158
+ */
159
+ export function projectUserPrompt(event: UserPromptEvent, el: ElisionState): ContentBlock[] {
160
+ // Elided + conversational: collapse to a stub (anchor/tiny kept full).
161
+ if (conversationalStubbed(event, el)) {
162
+ return [{ type: 'text', text: conversationalStub('user', event.seq) }];
163
+ }
164
+ const blocks: ContentBlock[] = [{ type: 'text', text: event.text }];
165
+ if (event.attachments) {
166
+ for (const att of event.attachments) {
167
+ if (att.kind === 'image') {
168
+ blocks.push({
169
+ type: 'image',
170
+ mediaType: att.mediaType ?? 'image/png',
171
+ data: att.content,
172
+ });
173
+ } else if (att.kind === 'document') {
174
+ blocks.push({
175
+ type: 'document',
176
+ mediaType: att.mediaType ?? 'application/pdf',
177
+ data: att.content,
178
+ ...(att.name ? { name: att.name } : {}),
179
+ });
180
+ } else {
181
+ blocks.push({
182
+ type: 'text',
183
+ text: `[${att.kind}${att.name ? ` ${att.name}` : ''}]\n${att.content}`,
184
+ });
185
+ }
186
+ }
187
+ }
188
+ return blocks;
189
+ }
190
+
191
+ /**
192
+ * Precompute the set of callIds that have a matching tool_result (or
193
+ * tool_call_denied) somewhere in the log. Used to synthesize a fallback
194
+ * `[interrupted]` tool_result for orphan tool_use blocks when the assistant
195
+ * message gets flushed.
196
+ *
197
+ * Without this fallback the provider rejects the whole conversation with
198
+ * "assistant message with 'tool_calls' must be followed by tool messages
199
+ * responding to each 'tool_call_id'". Orphans typically appear after a
200
+ * cancelled turn, an aborted process, or a tool exception that bypassed the
201
+ * loop's tool_result emit path.
202
+ *
203
+ * Extracted as a pure precompute (u123-5); returns a fresh mutable Set the
204
+ * projection augments as it synthesizes orphan results (so a repeated orphan
205
+ * across groups is only emitted once).
206
+ */
207
+ export function resolvedCallIdSet(events: ReadonlyArray<MoxxyEvent>): Set<string> {
208
+ const resolvedCallIds = new Set<string>();
209
+ for (const e of events) {
210
+ if (e.type === 'tool_result' || e.type === 'tool_call_denied') {
211
+ resolvedCallIds.add(e.callId);
212
+ }
213
+ }
214
+ return resolvedCallIds;
215
+ }
216
+
217
+ /**
218
+ * Project the session's event log to a flat list of ProviderMessages
219
+ * suitable for handing to `provider.stream`. Used by every loop strategy.
220
+ *
221
+ * Handles user_prompt, assistant_message, tool_call_requested (grouped
222
+ * into a single assistant message of tool_use blocks), and tool_result.
223
+ * Other event types are passed through as a no-op.
224
+ *
225
+ * This is THE projection every loop strategy uses; it honors compaction
226
+ * events, turn-boundary elision, and the orphan-tool_use fallback. It lives in
227
+ * the SDK so loop plugins stay independent of core.
228
+ */
229
+ export interface ProjectedMessages {
230
+ readonly messages: ProviderMessage[];
231
+ /**
232
+ * Index (into `messages`) of the last message belonging to the stable,
233
+ * byte-identical prefix — i.e. produced entirely from events at or below the
234
+ * elision high-water mark (which only advances on whole-turn boundaries, so
235
+ * the cut never splits a message). -1 when no elision is active. The
236
+ * `stable-prefix` cache strategy places its long-lived cross-turn breakpoint
237
+ * here; see {@link collectProviderStream}'s `stablePrefixIndex` option.
238
+ */
239
+ readonly stablePrefixIndex: number;
240
+ }
241
+
242
+ export function projectMessagesFromLog(
243
+ ctx: Pick<ModeContext, 'log'>,
244
+ opts: ProjectMessagesOptions = {},
245
+ ): ProviderMessage[] {
246
+ return projectMessages(ctx, opts).messages;
247
+ }
248
+
249
+ /**
250
+ * Same projection as {@link projectMessagesFromLog} but also reports the
251
+ * stable-prefix boundary so the active cache strategy can place a cross-turn
252
+ * breakpoint. Modes that build messages this way should thread the returned
253
+ * `stablePrefixIndex` into {@link collectProviderStream}.
254
+ */
255
+ export function projectMessages(
256
+ ctx: Pick<ModeContext, 'log'>,
257
+ opts: ProjectMessagesOptions = {},
258
+ ): ProjectedMessages {
259
+ const allEvents = ctx.log.slice();
260
+ const compactions = activeCompactionRanges(allEvents);
261
+ const compactionFor = makeCompactionLookup(compactions);
262
+ const emittedCompactions = new Set<CompactionRange>();
263
+ const el = computeElisionState(allEvents);
264
+
265
+ const messages: ProviderMessage[] = [];
266
+ // The stable prefix is every message produced from events at/below the
267
+ // elision HWM. Record the latest such message index as we push.
268
+ let stablePrefixIndex = -1;
269
+ const recordStable = (maxSeq: number): void => {
270
+ if (el.hwm >= 0 && maxSeq >= 0 && maxSeq <= el.hwm) {
271
+ stablePrefixIndex = messages.length - 1;
272
+ }
273
+ };
274
+ if (opts.systemPrompt) {
275
+ // When elision is active, tell the model that older turns may be shown as
276
+ // stubs and how to expand them — so it recalls instead of hallucinating.
277
+ // Constant text → busts the system cache once (when elision starts), stable
278
+ // thereafter.
279
+ const sysText = el.hwm >= 0 ? `${opts.systemPrompt}\n\n${ELISION_SYSTEM_NOTE}` : opts.systemPrompt;
280
+ messages.push({ role: 'system', content: [{ type: 'text', text: sysText }] });
281
+ }
282
+ // Pre-scan: build the set of callIds that have a matching tool_result
283
+ // (or tool_call_denied) somewhere in the log. Used to synthesize a
284
+ // fallback `[interrupted]` tool_result for orphan tool_use blocks
285
+ // when the assistant message gets flushed.
286
+ const resolvedCallIds = resolvedCallIdSet(allEvents);
287
+
288
+ let pendingAssistant: ProviderMessage | null = null;
289
+ let pendingAssistantMaxSeq = -1;
290
+ // Reasoning block awaiting attachment to the current assistant turn. Only set
291
+ // for REPLAYABLE reasoning (Anthropic signature / redacted-or-Codex encrypted
292
+ // blob) — render-only reasoning is never sent back. Attached as content[0] of
293
+ // the turn's assistant message (Anthropic requires the signed thinking block
294
+ // first on an interleaved-thinking tool-use continuation; a missing/unsigned
295
+ // one is a hard 400). Dropped at any turn/compaction boundary it doesn't reach.
296
+ let pendingReasoning: Extract<ContentBlock, { type: 'reasoning' }> | null = null;
297
+ let pendingReasoningSeq = -1;
298
+ const flush = (): void => {
299
+ if (!pendingAssistant) return;
300
+ const flushed = pendingAssistant;
301
+ const groupMaxSeq = pendingAssistantMaxSeq;
302
+ pendingAssistant = null;
303
+ pendingAssistantMaxSeq = -1;
304
+ messages.push(flushed);
305
+ recordStable(groupMaxSeq);
306
+ // Synthesize fallback tool_result messages for any tool_use blocks
307
+ // whose callId never resolved in the event log. Has to land
308
+ // immediately after the assistant message (and before any
309
+ // subsequent user_prompt / assistant_message) so the provider sees
310
+ // a clean assistant→tool-result chain.
311
+ for (const block of flushed.content) {
312
+ if (block.type === 'tool_use' && !resolvedCallIds.has(block.id)) {
313
+ messages.push({
314
+ role: 'tool_result',
315
+ content: [
316
+ {
317
+ type: 'tool_result',
318
+ toolUseId: block.id,
319
+ content: '[tool call did not return a result — possibly interrupted or cancelled]',
320
+ isError: true,
321
+ },
322
+ ],
323
+ });
324
+ recordStable(groupMaxSeq);
325
+ // Mark synthesized so we don't double-emit if the same orphan
326
+ // appears in multiple groups (defensive — shouldn't normally
327
+ // happen since each tool_call_requested has a unique callId).
328
+ resolvedCallIds.add(block.id);
329
+ }
330
+ }
331
+ };
332
+
333
+ for (const e of allEvents) {
334
+ const compaction = compactionFor(e.seq);
335
+ if (compaction) {
336
+ if (!emittedCompactions.has(compaction)) {
337
+ emittedCompactions.add(compaction);
338
+ flush();
339
+ pendingReasoning = null;
340
+ messages.push({
341
+ role: 'user',
342
+ content: [{ type: 'text', text: `[summary of earlier turns]\n${compaction.summary}` }],
343
+ });
344
+ recordStable(compaction.to);
345
+ }
346
+ continue;
347
+ }
348
+
349
+ switch (e.type) {
350
+ case 'user_prompt': {
351
+ flush();
352
+ pendingReasoning = null;
353
+ messages.push({ role: 'user', content: projectUserPrompt(e, el) });
354
+ recordStable(e.seq);
355
+ break;
356
+ }
357
+ case 'reasoning_message': {
358
+ // Render-only reasoning (no signature/encrypted) is never replayed —
359
+ // it exists only for the live/scrollback "Thinking" view. Replayable
360
+ // reasoning is stashed for content[0] of this turn's assistant message.
361
+ if (e.signature || e.encrypted) {
362
+ pendingReasoning = {
363
+ type: 'reasoning',
364
+ text: e.content,
365
+ ...(e.signature ? { signature: e.signature } : {}),
366
+ ...(e.redacted ? { redacted: true } : {}),
367
+ ...(e.encrypted ? { encrypted: e.encrypted } : {}),
368
+ };
369
+ pendingReasoningSeq = e.seq;
370
+ }
371
+ break;
372
+ }
373
+ case 'assistant_message':
374
+ flush();
375
+ if (conversationalStubbed(e, el)) {
376
+ pendingReasoning = null;
377
+ messages.push({
378
+ role: 'assistant',
379
+ content: [{ type: 'text', text: conversationalStub('assistant', e.seq) }],
380
+ });
381
+ recordStable(e.seq);
382
+ break;
383
+ }
384
+ // A tool-only turn can log an assistant_message with empty content
385
+ // (end_turn + tool calls, no prose). Projecting it as an empty text
386
+ // block makes some providers (Anthropic) reject the NEXT request and
387
+ // permanently wedges the session. Skip the block — the turn's
388
+ // tool_use blocks are projected from tool_call_requested events —
389
+ // which also un-wedges historical logs that already contain one.
390
+ if (e.content.trim().length === 0) {
391
+ pendingReasoning = null;
392
+ recordStable(e.seq);
393
+ break;
394
+ }
395
+ {
396
+ const content: Array<ProviderMessage['content'][number]> = [];
397
+ if (pendingReasoning) {
398
+ content.push(pendingReasoning);
399
+ pendingReasoning = null;
400
+ }
401
+ content.push({ type: 'text', text: e.content });
402
+ messages.push({ role: 'assistant', content });
403
+ }
404
+ recordStable(e.seq);
405
+ break;
406
+ case 'tool_call_requested': {
407
+ if (!pendingAssistant) {
408
+ // Seed the assistant turn so the signed reasoning block is content[0],
409
+ // ahead of every tool_use (Anthropic's interleaved-thinking ordering).
410
+ pendingAssistant = { role: 'assistant', content: pendingReasoning ? [pendingReasoning] : [] };
411
+ if (pendingReasoning) {
412
+ pendingAssistantMaxSeq = Math.max(pendingAssistantMaxSeq, pendingReasoningSeq);
413
+ pendingReasoning = null;
414
+ }
415
+ }
416
+ pendingAssistantMaxSeq = Math.max(pendingAssistantMaxSeq, e.seq);
417
+ (pendingAssistant.content as Array<ProviderMessage['content'][number]>).push({
418
+ type: 'tool_use',
419
+ id: e.callId,
420
+ name: e.name,
421
+ input: e.input,
422
+ });
423
+ break;
424
+ }
425
+ case 'tool_result': {
426
+ flush();
427
+ // Stub bulky old tool output to a recall-able marker (decision shared
428
+ // with estimateContextTokens via toolResultStubbed).
429
+ let text: string;
430
+ if (toolResultStubbed(e, el)) {
431
+ const recalled = el.recalledCallIds.has(e.callId) || el.recalledSeqs.has(e.seq);
432
+ text = toolResultStub(e.callId, toolResultBytes(e.output), recalled);
433
+ } else if (e.error) {
434
+ text = `[error:${e.error.kind}] ${e.error.message}`;
435
+ } else if (isToolDisplayResult(e.output)) {
436
+ // Rich result (e.g. a file diff): the model only needs the short
437
+ // `forModel` summary — the structured `display` is for channels.
438
+ text = e.output.forModel;
439
+ } else {
440
+ text = typeof e.output === 'string' ? e.output : JSON.stringify(e.output ?? '');
441
+ }
442
+ messages.push({
443
+ role: 'tool_result',
444
+ content: [{ type: 'tool_result', toolUseId: e.callId, content: text, isError: !e.ok }],
445
+ });
446
+ recordStable(e.seq);
447
+ break;
448
+ }
449
+ default:
450
+ break;
451
+ }
452
+ }
453
+ flush();
454
+
455
+ if (opts.trailingUserText) {
456
+ // The trailing step nudge is volatile (changes per step), never part of
457
+ // the stable prefix — don't record it.
458
+ messages.push({ role: 'user', content: [{ type: 'text', text: opts.trailingUserText }] });
459
+ }
460
+ return { messages, stablePrefixIndex };
461
+ }
@@ -0,0 +1,63 @@
1
+ import type { ProviderMessage } from '../provider.js';
2
+ import type { ModeContext } from '../mode.js';
3
+ import { runCompactionIfNeeded } from '../compactor-helpers.js';
4
+ import { runElisionIfNeeded } from '../elision-helpers.js';
5
+ import { usageEventFields } from '../token-accounting.js';
6
+ import { collectProviderStream } from './collect-stream.js';
7
+
8
+ /**
9
+ * Run a single-shot (no-tools) provider turn — the shape every planner /
10
+ * synthesis phase shares. Runs context management (compaction + elision),
11
+ * emits the `provider_request` bookend, streams the response with tools
12
+ * disabled, then emits either an `error` event (returning `null`) or the
13
+ * `provider_response` bookend (returning the collected text).
14
+ *
15
+ * Replaces the ~40-line block each mode phase used to inline; centralizing it
16
+ * keeps event emission uniform and means a fix here (e.g. always running
17
+ * elision) lands for every loop strategy at once.
18
+ */
19
+ export async function runSingleShotTurn(
20
+ ctx: ModeContext,
21
+ messages: ReadonlyArray<ProviderMessage>,
22
+ opts: { maxTokens?: number } = {},
23
+ ): Promise<string | null> {
24
+ await runCompactionIfNeeded(ctx);
25
+ await runElisionIfNeeded(ctx);
26
+
27
+ await ctx.emit({
28
+ type: 'provider_request',
29
+ sessionId: ctx.sessionId,
30
+ turnId: ctx.turnId,
31
+ source: 'system',
32
+ provider: ctx.provider.name,
33
+ model: ctx.model,
34
+ });
35
+
36
+ const { text, usage, error } = await collectProviderStream(ctx, messages, {
37
+ includeTools: false,
38
+ ...(opts.maxTokens !== undefined ? { maxTokens: opts.maxTokens } : {}),
39
+ });
40
+ if (error) {
41
+ await ctx.emit({
42
+ type: 'error',
43
+ sessionId: ctx.sessionId,
44
+ turnId: ctx.turnId,
45
+ source: 'system',
46
+ kind: error.retryable ? 'retryable' : 'fatal',
47
+ message: error.message,
48
+ });
49
+ return null;
50
+ }
51
+
52
+ await ctx.emit({
53
+ type: 'provider_response',
54
+ sessionId: ctx.sessionId,
55
+ turnId: ctx.turnId,
56
+ source: 'system',
57
+ provider: ctx.provider.name,
58
+ model: ctx.model,
59
+ ...usageEventFields(usage),
60
+ });
61
+
62
+ return text;
63
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Stable, key-order-canonical hash of a tool call's input, so `{a:1,b:2}` and
3
+ * `{b:2,a:1}` produce the same key. Use for any "have I seen this call before"
4
+ * comparison — a raw `JSON.stringify` is NOT order-stable.
5
+ */
6
+ export function stableHash(input: unknown): string {
7
+ return canonicalize(input);
8
+ }
9
+
10
+ function canonicalize(value: unknown): string {
11
+ if (value === null || value === undefined) return 'null';
12
+ if (typeof value !== 'object') return JSON.stringify(value);
13
+ if (Array.isArray(value)) {
14
+ return '[' + value.map(canonicalize).join(',') + ']';
15
+ }
16
+ const entries = Object.entries(value as Record<string, unknown>).sort(
17
+ ([a], [b]) => (a < b ? -1 : a > b ? 1 : 0),
18
+ );
19
+ return '{' + entries.map(([k, v]) => JSON.stringify(k) + ':' + canonicalize(v)).join(',') + '}';
20
+ }
@@ -0,0 +1,89 @@
1
+ import { stableHash } from './stable-hash.js';
2
+
3
+ /**
4
+ * Sliding-window detector for "model keeps making the same tool call".
5
+ *
6
+ * When the same `(toolName, input)` pair appears `repeatThreshold` times in
7
+ * the last `windowSize` calls, the model is almost certainly stuck — polling a
8
+ * tool that returns the same thing, mis-handling an error, etc. Bail early
9
+ * instead of burning through the iteration cap.
10
+ *
11
+ * Shared across every loop strategy so detection is uniform — previously each
12
+ * mode re-rolled this, and one copy used a non-canonical `JSON.stringify`
13
+ * signature that silently missed key-reordered repeats.
14
+ */
15
+ export interface StuckSignal {
16
+ /** True when the loop guard should trip. */
17
+ readonly stuck: boolean;
18
+ /** Repeat count behind the trip — for the error message. */
19
+ readonly count: number;
20
+ /**
21
+ * `exact` = the same (tool, full-input) repeated `repeatThreshold` times.
22
+ * `near` = the same (tool, identity arg — url / file_path / command / …)
23
+ * repeated `nearThreshold` times while only volatile args (maxBytes,
24
+ * timeoutMs) varied. Catches the "refetch the same URL with a bigger
25
+ * maxBytes over and over" loop the exact check sails past.
26
+ */
27
+ readonly kind: 'exact' | 'near';
28
+ }
29
+
30
+ export interface StuckLoopDetector {
31
+ readonly windowSize: number;
32
+ readonly repeatThreshold: number;
33
+ /** Record the call and report whether the loop guard should trip. */
34
+ record(toolName: string, input: unknown): StuckSignal;
35
+ }
36
+
37
+ /** Identity arguments that pin "the same target" across volatile-arg variation,
38
+ * best-first. The first present string field wins. */
39
+ const IDENTITY_ARG_KEYS = ['url', 'file_path', 'path', 'command', 'cmd', 'query', 'pattern'];
40
+
41
+ function identityArg(input: unknown): string | null {
42
+ if (!input || typeof input !== 'object' || Array.isArray(input)) return null;
43
+ const o = input as Record<string, unknown>;
44
+ for (const k of IDENTITY_ARG_KEYS) {
45
+ const v = o[k];
46
+ if (typeof v === 'string' && v.trim().length > 0) return `${k}=${v}`;
47
+ }
48
+ return null;
49
+ }
50
+
51
+ export function createStuckLoopDetector(
52
+ opts: {
53
+ windowSize?: number;
54
+ repeatThreshold?: number;
55
+ nearWindowSize?: number;
56
+ nearThreshold?: number;
57
+ } = {},
58
+ ): StuckLoopDetector {
59
+ const windowSize = opts.windowSize ?? 8;
60
+ const repeatThreshold = opts.repeatThreshold ?? 3;
61
+ // Near-dups need a higher count + a wider window (they're spread out across a
62
+ // burst of other calls), and tolerate a couple of legit "bigger refetch" tries.
63
+ const nearWindowSize = opts.nearWindowSize ?? Math.max(windowSize * 2, 16);
64
+ const nearThreshold = opts.nearThreshold ?? Math.max(repeatThreshold + 2, 5);
65
+ const recent: string[] = [];
66
+ const recentNear: string[] = [];
67
+ return {
68
+ windowSize,
69
+ repeatThreshold,
70
+ record(toolName, input): StuckSignal {
71
+ const key = `${toolName}|${stableHash(input)}`;
72
+ recent.push(key);
73
+ if (recent.length > windowSize) recent.shift();
74
+ const exactCount = recent.filter((k) => k === key).length;
75
+ if (exactCount >= repeatThreshold) return { stuck: true, count: exactCount, kind: 'exact' };
76
+
77
+ let nearCount = 0;
78
+ const id = identityArg(input);
79
+ if (id !== null) {
80
+ const nearKey = `${toolName}|${id}`;
81
+ recentNear.push(nearKey);
82
+ if (recentNear.length > nearWindowSize) recentNear.shift();
83
+ nearCount = recentNear.filter((k) => k === nearKey).length;
84
+ if (nearCount >= nearThreshold) return { stuck: true, count: nearCount, kind: 'near' };
85
+ }
86
+ return { stuck: false, count: Math.max(exactCount, nearCount), kind: 'exact' };
87
+ },
88
+ };
89
+ }