@phren/agent 0.3.19 → 0.3.21

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 (67) hide show
  1. package/dist/agent-loop/index.js +185 -47
  2. package/dist/agent-loop/stream.js +74 -66
  3. package/dist/agent-loop/types.js +2 -3
  4. package/dist/attach-images.js +57 -0
  5. package/dist/bin.js +7 -2
  6. package/dist/commands/info.js +7 -3
  7. package/dist/commands/model.js +55 -13
  8. package/dist/commands/session.js +79 -18
  9. package/dist/commands.js +24 -3
  10. package/dist/config.js +89 -10
  11. package/dist/context/clear-tool-results.js +43 -0
  12. package/dist/context/compactor.js +13 -3
  13. package/dist/context/pruner.js +29 -11
  14. package/dist/context/usage.js +26 -0
  15. package/dist/cost.js +68 -10
  16. package/dist/headless.js +47 -1
  17. package/dist/index.js +335 -127
  18. package/dist/mcp-client.js +38 -1
  19. package/dist/mcp-prompts.js +94 -0
  20. package/dist/memory/chat.js +70 -0
  21. package/dist/memory/context.js +1 -1
  22. package/dist/model-overrides.js +17 -0
  23. package/dist/models-list.js +73 -0
  24. package/dist/models.js +42 -15
  25. package/dist/multi/child-entry.js +18 -28
  26. package/dist/multi/ipc-hooks.js +34 -0
  27. package/dist/multi/spawner.js +49 -3
  28. package/dist/permissions/allowlist.js +19 -4
  29. package/dist/permissions/checker.js +37 -20
  30. package/dist/permissions/kernel-sandbox.js +32 -10
  31. package/dist/permissions/rules.js +173 -0
  32. package/dist/permissions/shell-classify.js +173 -0
  33. package/dist/plan.js +6 -2
  34. package/dist/providers/anthropic.js +119 -75
  35. package/dist/providers/codex.js +10 -9
  36. package/dist/providers/ollama.js +5 -2
  37. package/dist/providers/openai-compat.js +219 -92
  38. package/dist/providers/openrouter.js +12 -7
  39. package/dist/providers/resolve.js +47 -1
  40. package/dist/providers/retry.js +20 -7
  41. package/dist/providers/types.js +31 -0
  42. package/dist/repl.js +17 -1
  43. package/dist/session/persist.js +9 -4
  44. package/dist/session/preview.js +99 -0
  45. package/dist/settings.js +14 -0
  46. package/dist/structured-output.js +85 -0
  47. package/dist/system-prompt.js +117 -14
  48. package/dist/tools/apply-patch.js +15 -1
  49. package/dist/tools/edit-file.js +8 -1
  50. package/dist/tools/file-state.js +88 -0
  51. package/dist/tools/glob.js +53 -35
  52. package/dist/tools/grep.js +139 -53
  53. package/dist/tools/lint-test.js +24 -0
  54. package/dist/tools/read-file.js +2 -0
  55. package/dist/tools/registry.js +8 -1
  56. package/dist/tools/search-support.js +141 -0
  57. package/dist/tools/shell.js +266 -41
  58. package/dist/tools/spawn-agent.js +15 -3
  59. package/dist/tools/syntax-check.js +116 -0
  60. package/dist/tools/write-file.js +11 -3
  61. package/dist/tui/components/App.js +5 -4
  62. package/dist/tui/components/Banner.js +1 -1
  63. package/dist/tui/components/ListPicker.js +32 -0
  64. package/dist/tui/ink-entry.js +79 -15
  65. package/dist/user-hooks.js +26 -4
  66. package/package.json +3 -2
  67. package/dist/memory/context-flush.js +0 -24
@@ -1,21 +1,40 @@
1
1
  import { createSpinner, formatTurnHeader, formatToolCall } from "../spinner.js";
2
- import { shouldPrune } from "../context/pruner.js";
2
+ import { planToolResultClearing } from "../context/clear-tool-results.js";
3
+ import { contextTokens, reportedContext } from "../context/usage.js";
3
4
  import { compactWithLlm } from "../context/compactor.js";
4
5
  import { estimateMessageTokens } from "../context/token-counter.js";
5
6
  import { isContextOverflowError, withRetry } from "../providers/retry.js";
6
- import { checkFlushNeeded } from "../memory/context-flush.js";
7
7
  import { injectPlanPrompt, requestPlanApproval } from "../plan.js";
8
+ import { READ_ONLY_TOOLS } from "../permissions/checker.js";
9
+ import { attachImages } from "../attach-images.js";
10
+ import { modelSupportsVision } from "../models.js";
11
+ import * as path from "path";
8
12
  import { detectLintCommand, detectTestCommand } from "../tools/lint-test.js";
9
13
  import { createCheckpoint } from "../checkpoint.js";
10
14
  import { resetRepeatChain } from "../guards/repeat-tool-reminder.js";
11
15
  import { runLifecycleHooks } from "../user-hooks.js";
16
+ import { recordTokenUsage } from "../cost.js";
12
17
  import { createSession } from "./types.js";
13
- import { consumeStream, executeToolBlocks, prefetchFirst, runToolsConcurrently } from "./stream.js";
18
+ import { consumeStream, executeToolBlocks, runToolsConcurrently } from "./stream.js";
14
19
  export { createSession };
15
20
  /**
16
21
  * If the history ends with an assistant message whose tool calls have no
17
22
  * results, append a cancelled result for each. Returns how many were closed.
18
23
  */
24
+ /**
25
+ * Up to `max` chars of a check's output: the start (where compilers put the
26
+ * first error) and the end (where test runners put the summary).
27
+ */
28
+ /** Files a type checker looks at (matched anywhere in a path or patch text). */
29
+ const TYPED_SOURCE = /\.(?:ts|tsx|mts|cts|js|jsx|mjs|cjs|py|pyi)\b/;
30
+ function headAndTail(output, max) {
31
+ if (output.length <= max)
32
+ return output;
33
+ const head = Math.floor(max * 0.4);
34
+ return `${output.slice(0, head)}\n… [${output.length - max} chars cut] …\n${output.slice(output.length - (max - head))}`;
35
+ }
36
+ /** How many times one turn's Stop hooks may send the model back. */
37
+ const MAX_STOP_BLOCKS = 5;
19
38
  export function closeDanglingToolUses(session, reason = "Cancelled by user.") {
20
39
  const messages = session.messages;
21
40
  const last = messages[messages.length - 1];
@@ -37,6 +56,8 @@ export async function runTurn(userInput, session, config, hooks) {
37
56
  const { provider, registry, maxTurns, verbose, costTracker } = config;
38
57
  let systemPrompt = config.systemPrompt;
39
58
  const toolDefs = registry.getDefinitions();
59
+ // While a plan is pending the model may look but not touch.
60
+ const planToolDefs = toolDefs.filter((d) => READ_ONLY_TOOLS.has(d.name));
40
61
  const spinner = createSpinner();
41
62
  const useStream = typeof provider.chatStream === "function";
42
63
  const status = hooks?.onStatus ?? ((msg) => process.stderr.write(msg));
@@ -52,12 +73,33 @@ export async function runTurn(userInput, session, config, hooks) {
52
73
  closeDanglingToolUses(session);
53
74
  // Direct user input resets the repeat-call chain (repetition across it is not a loop)
54
75
  resetRepeatChain(session.repeatChain);
76
+ // UserPromptSubmit hooks may block the prompt (exit 2) or add context to it.
77
+ const hookConfig = config.hookConfig ?? null;
78
+ const promptHooks = hookConfig ? await runLifecycleHooks(hookConfig, "UserPromptSubmit", { prompt: userInput }) : null;
79
+ if (promptHooks?.blocked) {
80
+ (hooks?.onStatus ?? ((msg) => process.stderr.write(msg)))(`\x1b[33m[prompt blocked by a UserPromptSubmit hook: ${promptHooks.reason}]\x1b[0m\n`);
81
+ return { text: "", turns: 0, toolCalls: 0, stopReason: "hook_blocked" };
82
+ }
83
+ const promptText = promptHooks?.context ? `${userInput}\n\n<user-prompt-submit-hook>\n${promptHooks.context}\n</user-prompt-submit-hook>` : userInput;
84
+ // Image paths in the prompt (typed or dropped onto the terminal) go along as images.
85
+ let promptContent = promptText;
86
+ if (modelSupportsVision(provider.name, provider.model ?? "")) {
87
+ const withImages = attachImages(promptText, process.cwd());
88
+ promptContent = withImages.content;
89
+ if (withImages.attached.length > 0)
90
+ status(`\x1b[2m[attached ${withImages.attached.map((f) => path.basename(f)).join(", ")}]\x1b[0m\n`);
91
+ if (withImages.skipped.length > 0)
92
+ status(`\x1b[33m[not attached: ${withImages.skipped.join(", ")}]\x1b[0m\n`);
93
+ }
55
94
  // Append user message to the durable log
56
- session.log.append("user/message", {
57
- message: { role: "user", content: userInput },
95
+ const prompted = session.log.append("user/message", {
96
+ message: { role: "user", content: promptContent },
58
97
  source: "user",
59
98
  turn: session.turns,
60
99
  });
100
+ // Phren Hook streams the text block being written from this sidecar; the
101
+ // turn is named by the time of the prompt that started it.
102
+ const preview = config.livePreview?.(session.log.header.sessionId);
61
103
  let turnToolCalls = 0;
62
104
  const turnStart = session.turns;
63
105
  // A refusal applies to automatic checks for this entire user turn.
@@ -65,10 +107,9 @@ export async function runTurn(userInput, session, config, hooks) {
65
107
  // One overflow-driven compaction per model call; reset after a success.
66
108
  let overflowRecovered = false;
67
109
  const signal = hooks?.signal;
68
- const hookConfig = config.hookConfig ?? null;
69
- if (hookConfig) {
70
- await runLifecycleHooks(hookConfig, "UserPromptSubmit", { prompt: userInput });
71
- }
110
+ // Times a Stop hook sent the model back to work in this turn.
111
+ let stopBlocks = 0;
112
+ let stopHooksRan = false;
72
113
  // Why the loop ended; stays "max_turns" if the turn cap runs out.
73
114
  let endReason = "max_turns";
74
115
  while (session.turns - turnStart < maxTurns) {
@@ -86,21 +127,15 @@ export async function runTurn(userInput, session, config, hooks) {
86
127
  if (verbose && session.turns > turnStart) {
87
128
  status(`\n${formatTurnHeader(session.turns + 1, turnToolCalls)}\n`);
88
129
  }
89
- // Check if context flush is needed (one-time per session) — must run before pruning
90
- const contextLimit = provider.contextWindow ?? 200_000;
91
- const flushPrompt = checkFlushNeeded(systemPrompt, session.messages, session.flushConfig);
92
- if (flushPrompt) {
93
- session.log.append("user/message", {
94
- message: { role: "user", content: flushPrompt },
95
- source: "system",
96
- turn: session.turns,
97
- });
98
- if (verbose)
99
- status("[context flush injected]\n");
100
- }
101
130
  // Prune context if approaching limit — LLM checkpoint with knowledge
102
- // promotion, degrading to the regex summary on any failure.
131
+ // promotion, degrading to the regex summary on any failure. (There is no
132
+ // separate "summarize what you learned" prompt: injected mid-task, the
133
+ // model answered it and ended the turn, and compaction already promotes
134
+ // knowledge to phren out of band.)
135
+ const contextLimit = provider.contextWindow ?? 200_000;
103
136
  const compactHistory = async (keepRecentTurns, trigger) => {
137
+ if (hookConfig)
138
+ await runLifecycleHooks(hookConfig, "PreCompact", { trigger: "auto" });
104
139
  const preCount = session.messages.length;
105
140
  const preTokens = estimateMessageTokens(session.messages);
106
141
  const result = await compactWithLlm(provider, systemPrompt, session.messages, {
@@ -111,6 +146,7 @@ export async function runTurn(userInput, session, config, hooks) {
111
146
  pruneConfig: { contextLimit, keepRecentTurns },
112
147
  signal,
113
148
  verbose,
149
+ tools: toolDefs,
114
150
  });
115
151
  if (!result)
116
152
  return false;
@@ -127,11 +163,23 @@ export async function runTurn(userInput, session, config, hooks) {
127
163
  status(`\x1b[2m[context compacted (${mode}${trigger}): ${preCount} → ${postCount} messages, ~${fmtPre} → ~${fmtPost} tokens, ${reduction}% reduction${routed}]\x1b[0m\n`);
128
164
  return true;
129
165
  };
130
- if (shouldPrune(systemPrompt, session.messages, { contextLimit })) {
131
- await compactHistory(6, "");
166
+ // Past 75% of the window (by the provider's own count when there is
167
+ // one), first clear old tool output; compact only if that frees too little.
168
+ const usedTokens = () => contextTokens(systemPrompt, session.messages, session.log, session.reportedContext);
169
+ if (usedTokens() > contextLimit * 0.75) {
170
+ const before = usedTokens();
171
+ const cleared = planToolResultClearing(session.messages);
172
+ for (const { index, message } of cleared)
173
+ session.log.replaceMessageRange(index, index, message);
174
+ const after = usedTokens();
175
+ if (cleared.length > 0) {
176
+ status(`\x1b[2m[cleared ${cleared.length === 1 ? "old tool output in 1 message" : `old tool output in ${cleared.length} messages`}: ~${Math.round(before / 1000)}k → ~${Math.round(after / 1000)}k tokens]\x1b[0m\n`);
177
+ }
178
+ if (after > contextLimit * 0.6)
179
+ await compactHistory(6, "");
132
180
  }
133
181
  // For plan mode first turn, pass empty tools so LLM can't call any
134
- const turnTools = planPending ? [] : toolDefs;
182
+ const turnTools = planPending ? planToolDefs : toolDefs;
135
183
  // Model-visible means logged: everything the provider is about to see
136
184
  // must be reconstructable from the event log. Cheap relative to a model
137
185
  // call; opt out with PHREN_AGENT_NO_INVARIANT=1 if it ever matters.
@@ -140,20 +188,48 @@ export async function runTurn(userInput, session, config, hooks) {
140
188
  }
141
189
  let assistantContent;
142
190
  let stopReason;
191
+ let invalidToolCalls = [];
192
+ let usage;
143
193
  try {
144
194
  if (useStream) {
145
- // Streaming path — retry the initial connection. The async generator does
146
- // no work until first read, so the first next() runs inside withRetry.
147
- const opening = await withRetry(async () => {
148
- const iterator = provider.chatStream(systemPrompt, session.messages, turnTools, signal)[Symbol.asyncIterator]();
149
- const first = await iterator.next();
150
- return { iterator, first };
151
- }, undefined, verbose, signal);
195
+ // Streaming path — the whole request is retried, not just opening it:
196
+ // a stream that drops, stalls into an incomplete end, or reports a
197
+ // retryable failure mid-response is requested again. Nothing has run
198
+ // yet (tools execute after the stream completes), so the only cost is
199
+ // the partial text already shown, dropped before the backoff wait.
152
200
  const onReasoningDelta = hooks?.onReasoningDelta ??
153
201
  (verbose ? (text) => process.stderr.write(`\x1b[2m${text}\x1b[0m`) : undefined);
154
- const result = await consumeStream(prefetchFirst(opening.iterator, opening.first), costTracker, { onTextDelta: hooks?.onTextDelta, onReasoningDelta, providerName: provider.name }, signal);
202
+ const onTextDelta = hooks?.onTextDelta ?? process.stdout.write.bind(process.stdout);
203
+ // Text this attempt has shown, for UIs that cannot take it back.
204
+ let shown = "";
205
+ const onRetry = () => {
206
+ if (hooks?.onStreamRetry) {
207
+ hooks.onStreamRetry();
208
+ }
209
+ else if (shown && !shown.endsWith("\n")) {
210
+ // Plain stdout (the REPL, one-shot) cannot erase the abandoned
211
+ // text; end its line so the restarted reply begins on its own.
212
+ onTextDelta("\n");
213
+ }
214
+ status(shown
215
+ ? "\x1b[33m[model request failed; retrying, the reply starts again]\x1b[0m\n"
216
+ : "\x1b[33m[model request failed; retrying]\x1b[0m\n");
217
+ shown = "";
218
+ // The phone's live preview drops the abandoned attempt's text too.
219
+ preview?.clear();
220
+ };
221
+ const result = await withRetry(async () => {
222
+ preview?.start(prompted.time);
223
+ return consumeStream(provider.chatStream(systemPrompt, session.messages, turnTools, signal), costTracker, {
224
+ onTextDelta: (text) => { shown += text; onTextDelta(text); preview?.append(text); },
225
+ onReasoningDelta,
226
+ providerName: provider.name,
227
+ }, signal);
228
+ }, { onRetry }, verbose, signal);
155
229
  assistantContent = result.content;
156
230
  stopReason = result.stop_reason;
231
+ invalidToolCalls = result.invalidToolCalls;
232
+ usage = result.usage;
157
233
  }
158
234
  else {
159
235
  // Batch path
@@ -162,9 +238,11 @@ export async function runTurn(userInput, session, config, hooks) {
162
238
  spinner.stop();
163
239
  assistantContent = response.content;
164
240
  stopReason = response.stop_reason;
241
+ invalidToolCalls = response.invalidToolCalls ?? [];
242
+ usage = response.usage;
165
243
  // Track cost from batch response
166
244
  if (costTracker && response.usage) {
167
- costTracker.recordUsage(response.usage.input_tokens, response.usage.output_tokens);
245
+ recordTokenUsage(costTracker, response.usage);
168
246
  }
169
247
  // Print text blocks (streaming already prints inline)
170
248
  for (const block of assistantContent) {
@@ -183,6 +261,7 @@ export async function runTurn(userInput, session, config, hooks) {
183
261
  }
184
262
  catch (err) {
185
263
  spinner.stop();
264
+ preview?.clear();
186
265
  // The token estimate is approximate; when the provider itself says the
187
266
  // prompt is too long, compact harder (keep 2 turns) and retry once.
188
267
  if (!overflowRecovered && !signal?.aborted && isContextOverflowError(err)) {
@@ -212,6 +291,9 @@ export async function runTurn(userInput, session, config, hooks) {
212
291
  stop_reason: stopReason,
213
292
  turn: session.turns,
214
293
  });
294
+ session.reportedContext = reportedContext(usage, session.log) ?? session.reportedContext;
295
+ // Only after the message is in the log, so a reader never sees neither.
296
+ preview?.clear();
215
297
  session.turns++;
216
298
  hooks?.onAssistantMessage?.(assistantContent, stopReason);
217
299
  // Abort check after LLM response. Tool calls the model already made must
@@ -226,8 +308,9 @@ export async function runTurn(userInput, session, config, hooks) {
226
308
  if (verbose && costTracker) {
227
309
  status(`\x1b[2m cost: ${costTracker.formatCost()}\x1b[0m\n`);
228
310
  }
229
- // Plan mode gate: after first response, ask for approval
230
- if (planPending) {
311
+ // Plan mode gate: once the model presents its plan (a reply without tool
312
+ // calls), ask for approval. Read-only calls before that just run.
313
+ if (planPending && stopReason === "end_turn") {
231
314
  const approve = hooks?.onPlanApproval ?? requestPlanApproval;
232
315
  const { approved, feedback } = await approve();
233
316
  if (signal?.aborted) {
@@ -239,7 +322,7 @@ export async function runTurn(userInput, session, config, hooks) {
239
322
  ? `The user rejected the plan with feedback: ${feedback}\nPlease revise your plan.`
240
323
  : "The user rejected the plan. Task aborted.";
241
324
  if (feedback) {
242
- // Revisions remain in plan mode, with tools disabled, until approved.
325
+ // Revisions remain in plan mode, read-only, until approved.
243
326
  session.log.append("user/message", {
244
327
  message: { role: "user", content: msg },
245
328
  source: "user",
@@ -260,9 +343,13 @@ export async function runTurn(userInput, session, config, hooks) {
260
343
  });
261
344
  continue;
262
345
  }
263
- // If max_tokens, warn user and inject continuation prompt
346
+ // If max_tokens, warn user and inject continuation prompt. Complete tool
347
+ // calls in the truncated response are not run (the response may have been
348
+ // cut mid-batch), but each still needs a result or every later request
349
+ // carries an unpaired tool_use.
264
350
  if (stopReason === "max_tokens") {
265
351
  status("\x1b[33m[response truncated: max_tokens reached, requesting continuation]\x1b[0m\n");
352
+ closeDanglingToolUses(session, "Not executed: your response was truncated at max_tokens. Issue the call again if it is still needed.");
266
353
  session.log.append("user/message", {
267
354
  message: { role: "user", content: "Your response was truncated due to length. Please continue where you left off." },
268
355
  source: "system",
@@ -272,15 +359,36 @@ export async function runTurn(userInput, session, config, hooks) {
272
359
  }
273
360
  // If no tool use, we're done
274
361
  if (stopReason !== "tool_use") {
362
+ // A Stop hook that exits 2 sends the model back with its reason (tests
363
+ // still failing, say); stop_hook_active tells it it already did once.
364
+ if (hookConfig && stopBlocks < MAX_STOP_BLOCKS && !signal?.aborted) {
365
+ const stop = await runLifecycleHooks(hookConfig, "Stop", { stop_hook_active: stopBlocks > 0 });
366
+ if (stop.blocked) {
367
+ stopBlocks++;
368
+ status(`\x1b[2m[Stop hook: ${stop.reason}]\x1b[0m\n`);
369
+ session.log.append("user/message", {
370
+ message: { role: "user", content: `A Stop hook asked you to keep going: ${stop.reason}` },
371
+ source: "system",
372
+ turn: session.turns,
373
+ });
374
+ continue;
375
+ }
376
+ stopHooksRan = true;
377
+ }
275
378
  endReason = "end_turn";
276
379
  break;
277
380
  }
278
- // Execute tool calls with concurrency
381
+ // Execute tool calls with concurrency. Calls whose arguments were not
382
+ // valid JSON are answered with an error instead of being run.
279
383
  const toolUseBlocks = assistantContent.filter((b) => b.type === "tool_use");
384
+ const invalidById = new Map(invalidToolCalls.map((call) => [call.id, call]));
385
+ // Only offered the read-only tools, a model can still name another one.
386
+ const planRefused = new Set(planPending ? toolUseBlocks.filter((b) => !READ_ONLY_TOOLS.has(b.name)).map((b) => b.id) : []);
387
+ const runnableBlocks = toolUseBlocks.filter((b) => !invalidById.has(b.id) && !planRefused.has(b.id));
280
388
  // Checkpoint BEFORE the mutating batch: the tool names are known now, and
281
389
  // the snapshot must be the true pre-turn tree so /rewind can restore it.
282
390
  const mutatingTools = new Set(["edit_file", "multi_edit", "apply_patch", "write_file"]);
283
- const hasMutation = toolUseBlocks.some(b => mutatingTools.has(b.name));
391
+ const hasMutation = runnableBlocks.some(b => mutatingTools.has(b.name));
284
392
  if (hasMutation) {
285
393
  createCheckpoint(process.cwd(), `turn-${session.turns}`);
286
394
  }
@@ -295,7 +403,7 @@ export async function runTurn(userInput, session, config, hooks) {
295
403
  }
296
404
  if (!hooks?.onToolStart)
297
405
  spinner.start(`Running ${toolUseBlocks.length} tool${toolUseBlocks.length > 1 ? "s" : ""}...`);
298
- const { results: toolResults, toolCallCount } = await executeToolBlocks(toolUseBlocks, {
406
+ const { results: executedResults, toolCallCount } = await executeToolBlocks(runnableBlocks, {
299
407
  registry, verbose, status, hooks, signal,
300
408
  antiPatterns: session.antiPatterns,
301
409
  captureState: session.captureState,
@@ -305,8 +413,25 @@ export async function runTurn(userInput, session, config, hooks) {
305
413
  });
306
414
  if (!hooks?.onToolStart)
307
415
  spinner.stop();
308
- session.toolCalls += toolCallCount;
309
- turnToolCalls += toolCallCount;
416
+ // Results in the model's call order, invalid calls answered in place.
417
+ const resultById = new Map(executedResults.flatMap((r) => (r.type === "tool_result" ? [[r.tool_use_id, r]] : [])));
418
+ const toolResults = toolUseBlocks.map((block) => {
419
+ if (planRefused.has(block.id)) {
420
+ const output = `Not run: plan mode allows only read-only tools until the user approves the plan. Finish the plan without calling ${block.name}.`;
421
+ hooks?.onToolEnd?.(block.name, block.input, output, true, 0);
422
+ return { type: "tool_result", tool_use_id: block.id, content: output, is_error: true };
423
+ }
424
+ const invalid = invalidById.get(block.id);
425
+ if (!invalid)
426
+ return resultById.get(block.id);
427
+ const sample = invalid.raw.length > 300 ? `${invalid.raw.slice(0, 300)}…` : invalid.raw;
428
+ const output = `Not run: the arguments for ${block.name} were not valid JSON (${invalid.error}). ` +
429
+ `Call the tool again with a JSON object of arguments. You sent: ${sample}`;
430
+ hooks?.onToolEnd?.(block.name, block.input, output, true, 0);
431
+ return { type: "tool_result", tool_use_id: block.id, content: output, is_error: true };
432
+ });
433
+ session.toolCalls += toolCallCount + invalidById.size + planRefused.size;
434
+ turnToolCalls += toolCallCount + invalidById.size + planRefused.size;
310
435
  // Only successful write/edit results justify checks; requested, denied,
311
436
  // failed, or cancelled mutations must never launch a follow-up command.
312
437
  const successfulMutation = toolUseBlocks.some((block) => mutatingTools.has(block.name)
@@ -316,12 +441,23 @@ export async function runTurn(userInput, session, config, hooks) {
316
441
  const cwd = registry.permissionConfig.projectRoot;
317
442
  const lintCmd = config.lintTestConfig.lintCmd ?? detectLintCommand(cwd);
318
443
  const testCmd = config.lintTestConfig.testCmd ?? detectTestCommand(cwd);
444
+ // The type check first: it is quick, and tests can't pass while it
445
+ // fails. Only after an edit to a typed source file (a README edit
446
+ // doesn't need it).
447
+ const touchedTyped = toolUseBlocks.some((block) => mutatingTools.has(block.name)
448
+ && TYPED_SOURCE.test(block.name === "apply_patch" ? String(block.input.patch ?? "") : String(block.input.path ?? "")));
449
+ const typecheckCmd = touchedTyped ? config.lintTestConfig.typecheckCmd : undefined;
319
450
  const lintFailures = [];
320
- for (const cmd of new Set([lintCmd, testCmd].filter(Boolean))) {
451
+ let typesFailed = false;
452
+ for (const cmd of new Set([typecheckCmd, lintCmd, testCmd].filter(Boolean))) {
321
453
  if (signal?.aborted)
322
454
  break;
323
455
  if (deniedChecks.has(cmd))
324
456
  continue;
457
+ if (typesFailed && cmd === testCmd && cmd !== lintCmd) {
458
+ lintFailures.push(`Tests (${cmd}) were not run: fix the type errors first.`);
459
+ continue;
460
+ }
325
461
  const input = { command: cmd, cwd, timeout: 60_000, description: "Verify the completed edit" };
326
462
  hooks?.onToolStart?.("shell", input, 1);
327
463
  // The same registry and scheduler preserve shell approval, hooks,
@@ -338,7 +474,9 @@ export async function runTurn(userInput, session, config, hooks) {
338
474
  if (check.is_error) {
339
475
  if (verbose)
340
476
  status(`\x1b[33m[post-edit check failed: ${cmd}]\x1b[0m\n`);
341
- lintFailures.push(`Post-edit check failed (${cmd}):\n${check.output.slice(0, 2000)}`);
477
+ if (cmd === typecheckCmd)
478
+ typesFailed = true;
479
+ lintFailures.push(`Post-edit check failed (${cmd}):\n${headAndTail(check.output, 2000)}`);
342
480
  }
343
481
  }
344
482
  if (lintFailures.length > 0) {
@@ -378,8 +516,8 @@ export async function runTurn(userInput, session, config, hooks) {
378
516
  else if (lastAssistant && typeof lastAssistant.content === "string") {
379
517
  text = lastAssistant.content;
380
518
  }
381
- if (hookConfig) {
382
- await runLifecycleHooks(hookConfig, "Stop", {});
519
+ if (hookConfig && !stopHooksRan) {
520
+ await runLifecycleHooks(hookConfig, "Stop", { stop_hook_active: stopBlocks > 0 });
383
521
  }
384
522
  return { text, turns: session.turns - turnStart, toolCalls: turnToolCalls, stopReason: signal?.aborted ? "aborted" : endReason };
385
523
  }
@@ -1,4 +1,7 @@
1
+ import { partialUsage } from "../providers/types.js";
2
+ import { parseToolArguments } from "../providers/openai-compat.js";
1
3
  import { chainKey, recordCall } from "../guards/repeat-tool-reminder.js";
4
+ import { recordTokenUsage } from "../cost.js";
2
5
  import { searchErrorRecovery } from "../memory/error-recovery.js";
3
6
  import { analyzeAndCapture } from "../memory/auto-capture.js";
4
7
  import { DIFF_MARKER } from "../multi/diff-renderer.js";
@@ -124,6 +127,7 @@ export async function consumeStream(stream, costTracker, hooks, signal) {
124
127
  const providerName = typeof hooks === "function" ? undefined : hooks?.providerName;
125
128
  const content = [];
126
129
  let stop_reason = "end_turn";
130
+ let usage;
127
131
  let currentText = "";
128
132
  let currentReasoning = "";
129
133
  // Map block index -> tool state for Anthropic-style index-based IDs
@@ -148,74 +152,79 @@ export async function consumeStream(stream, costTracker, hooks, signal) {
148
152
  });
149
153
  currentReasoning = "";
150
154
  };
151
- for await (const delta of stream) {
152
- if (signal?.aborted)
153
- break;
154
- if (delta.type === "text_delta") {
155
- (onTextDelta ?? process.stdout.write.bind(process.stdout))(delta.text);
156
- currentText += delta.text;
157
- }
158
- else if (delta.type === "reasoning_delta") {
159
- // Preserve stream order: a reasoning segment starting after visible
160
- // text closes that text block first (providers normally emit
161
- // reasoning before text, so this is the uncommon direction).
162
- if (currentText && !currentReasoning) {
163
- content.push({ type: "text", text: currentText });
164
- currentText = "";
165
- }
166
- onReasoningDelta?.(delta.text);
167
- currentReasoning += delta.text;
168
- }
169
- else if (delta.type === "reasoning_end") {
170
- flushReasoning(delta);
171
- }
172
- else if (delta.type === "tool_use_start") {
173
- // Flush accumulated reasoning (without a reasoning_end, e.g. compat
174
- // providers that only emit deltas), then text.
175
- flushReasoning();
176
- if (currentText) {
177
- content.push({ type: "text", text: currentText });
178
- currentText = "";
155
+ try {
156
+ for await (const delta of stream) {
157
+ if (signal?.aborted)
158
+ break;
159
+ if (delta.type === "text_delta") {
160
+ (onTextDelta ?? process.stdout.write.bind(process.stdout))(delta.text);
161
+ currentText += delta.text;
179
162
  }
180
- toolsByIndex.set(delta.id, { id: delta.id, name: delta.name, jsonParts: [] });
181
- }
182
- else if (delta.type === "tool_use_delta") {
183
- const tool = toolsByIndex.get(delta.id);
184
- if (tool)
185
- tool.jsonParts.push(delta.json);
186
- }
187
- else if (delta.type === "tool_use_end") {
188
- const tool = toolsByIndex.get(delta.id);
189
- if (tool) {
190
- const jsonStr = tool.jsonParts.join("");
191
- let input = null;
192
- try {
193
- const parsed = JSON.parse(jsonStr);
194
- if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
195
- input = parsed;
196
- }
163
+ else if (delta.type === "reasoning_delta") {
164
+ // Preserve stream order: a reasoning segment starting after visible
165
+ // text closes that text block first (providers normally emit
166
+ // reasoning before text, so this is the uncommon direction).
167
+ if (currentText && !currentReasoning) {
168
+ content.push({ type: "text", text: currentText });
169
+ currentText = "";
197
170
  }
198
- catch { /* fall through to malformed handling */ }
199
- if (input) {
200
- content.push({ type: "tool_use", id: tool.id, name: tool.name, input });
171
+ onReasoningDelta?.(delta.text);
172
+ currentReasoning += delta.text;
173
+ }
174
+ else if (delta.type === "reasoning_end") {
175
+ flushReasoning(delta);
176
+ }
177
+ else if (delta.type === "tool_use_start") {
178
+ // Flush accumulated reasoning (without a reasoning_end, e.g. compat
179
+ // providers that only emit deltas), then text.
180
+ flushReasoning();
181
+ if (currentText) {
182
+ content.push({ type: "text", text: currentText });
183
+ currentText = "";
201
184
  }
202
- else {
203
- // Defer: a malformed call must not become an orphan tool_use on a
204
- // truncated (max_tokens) or text turn, where runTurn never executes
205
- // it. If the turn does end as tool_use, emit it with empty input so
206
- // the tool runs and produces the matching tool_result.
207
- malformedTools.push({ id: tool.id, name: tool.name });
208
- process.stderr.write(`\x1b[33m[warning] Malformed tool_use JSON for ${tool.name} (${tool.id}); using empty input\x1b[0m\n`);
185
+ toolsByIndex.set(delta.id, { id: delta.id, name: delta.name, jsonParts: [] });
186
+ }
187
+ else if (delta.type === "tool_use_delta") {
188
+ const tool = toolsByIndex.get(delta.id);
189
+ if (tool)
190
+ tool.jsonParts.push(delta.json);
191
+ }
192
+ else if (delta.type === "tool_use_end") {
193
+ const tool = toolsByIndex.get(delta.id);
194
+ if (tool) {
195
+ const jsonStr = tool.jsonParts.join("");
196
+ const parsed = parseToolArguments(jsonStr);
197
+ if ("input" in parsed) {
198
+ content.push({ type: "tool_use", id: tool.id, name: tool.name, input: parsed.input });
199
+ }
200
+ else {
201
+ // Defer: a malformed call must not become an orphan tool_use on a
202
+ // truncated (max_tokens) or text turn, where runTurn never answers
203
+ // it. If the turn does end as tool_use, emit it with empty input and
204
+ // report it, so runTurn answers it with an error instead of running
205
+ // the tool with arguments the model never gave.
206
+ malformedTools.push({ id: tool.id, name: tool.name, raw: jsonStr, error: parsed.error });
207
+ process.stderr.write(`\x1b[33m[warning] Malformed tool_use JSON for ${tool.name} (${tool.id}); returning an error to the model\x1b[0m\n`);
208
+ }
209
209
  }
210
210
  }
211
- }
212
- else if (delta.type === "done") {
213
- stop_reason = delta.stop_reason;
214
- if (costTracker && delta.usage) {
215
- costTracker.recordUsage(delta.usage.input_tokens, delta.usage.output_tokens);
211
+ else if (delta.type === "done") {
212
+ stop_reason = delta.stop_reason;
213
+ usage = delta.usage;
214
+ if (costTracker && delta.usage) {
215
+ recordTokenUsage(costTracker, delta.usage);
216
+ }
216
217
  }
217
218
  }
218
219
  }
220
+ catch (err) {
221
+ // A failed attempt is retried, but what the provider reported it billed
222
+ // still counts toward --budget.
223
+ const usage = partialUsage(err);
224
+ if (costTracker && usage)
225
+ recordTokenUsage(costTracker, usage);
226
+ throw err;
227
+ }
219
228
  // Flush remaining reasoning, then text
220
229
  flushReasoning();
221
230
  if (currentText) {
@@ -225,14 +234,13 @@ export async function consumeStream(stream, costTracker, hooks, signal) {
225
234
  content.push({ type: "text", text: currentText });
226
235
  }
227
236
  // Only a genuine tool_use turn can safely carry a synthesized tool block;
228
- // runTurn executes it and pairs the result. On any other stop reason the
237
+ // runTurn pairs it with an error result. On any other stop reason the
229
238
  // block is dropped so the durable history never holds an orphan tool_use.
230
- if (stop_reason === "tool_use") {
231
- for (const tool of malformedTools) {
232
- content.push({ type: "tool_use", id: tool.id, name: tool.name, input: {} });
233
- }
239
+ const invalidToolCalls = stop_reason === "tool_use" ? malformedTools : [];
240
+ for (const tool of invalidToolCalls) {
241
+ content.push({ type: "tool_use", id: tool.id, name: tool.name, input: {} });
234
242
  }
235
- return { content, stop_reason };
243
+ return { content, stop_reason, invalidToolCalls, usage };
236
244
  }
237
245
  /**
238
246
  * The model-visible part of a tool output. File tools append a full
@@ -1,10 +1,10 @@
1
1
  import { createCaptureState } from "../memory/auto-capture.js";
2
2
  import { AntiPatternTracker } from "../memory/anti-patterns.js";
3
- import { createFlushConfig } from "../memory/context-flush.js";
4
3
  import { SessionLog } from "../session/log.js";
5
4
  import { createRepeatChain } from "../guards/repeat-tool-reminder.js";
6
5
  import { randomUUID } from "crypto";
7
- export function createSession(contextLimit, options) {
6
+ /** The context limit argument is unused (compaction reads the provider's window); kept for callers. */
7
+ export function createSession(_contextLimit, options) {
8
8
  const log = options?.log ??
9
9
  new SessionLog({
10
10
  sessionId: `mem-${randomUUID()}`,
@@ -20,7 +20,6 @@ export function createSession(contextLimit, options) {
20
20
  toolCalls: 0,
21
21
  captureState: createCaptureState(),
22
22
  antiPatterns: new AntiPatternTracker(),
23
- flushConfig: createFlushConfig(contextLimit ?? 200_000),
24
23
  repeatChain: createRepeatChain(),
25
24
  };
26
25
  }