@phren/agent 0.1.15 → 0.3.6

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 (110) hide show
  1. package/README.md +86 -0
  2. package/dist/agent-loop/index.js +239 -65
  3. package/dist/agent-loop/stream.js +212 -29
  4. package/dist/agent-loop/types.js +15 -2
  5. package/dist/checkpoint.js +103 -22
  6. package/dist/commands/info.js +30 -15
  7. package/dist/commands/model.js +6 -2
  8. package/dist/commands/review.js +183 -0
  9. package/dist/commands/session.js +70 -7
  10. package/dist/commands.js +168 -12
  11. package/dist/config.js +87 -10
  12. package/dist/context/compactor.js +239 -0
  13. package/dist/context/pruner.js +39 -13
  14. package/dist/context/token-counter.js +33 -1
  15. package/dist/custom-commands.js +69 -0
  16. package/dist/guards/repeat-tool-reminder.js +75 -0
  17. package/dist/headless.js +115 -0
  18. package/dist/herdr-hooks.js +71 -0
  19. package/dist/index.js +351 -48
  20. package/dist/mcp-client.js +434 -73
  21. package/dist/mcp-oauth.js +121 -0
  22. package/dist/memory/anti-patterns.js +7 -3
  23. package/dist/memory/auto-capture.js +7 -3
  24. package/dist/memory/context.js +92 -73
  25. package/dist/memory/project-context.js +24 -5
  26. package/dist/memory/provenance.js +15 -0
  27. package/dist/memory/review-triage.js +166 -0
  28. package/dist/memory/session.js +36 -1
  29. package/dist/models.js +152 -19
  30. package/dist/multi/child-entry.js +65 -10
  31. package/dist/multi/diff-renderer.js +3 -3
  32. package/dist/multi/markdown.js +125 -27
  33. package/dist/multi/provider-manager.js +8 -0
  34. package/dist/multi/publish.js +72 -0
  35. package/dist/multi/spawner.js +45 -4
  36. package/dist/multi/syntax-highlight.js +25 -4
  37. package/dist/multi/types.js +1 -1
  38. package/dist/permissions/allowlist.js +86 -13
  39. package/dist/permissions/checker.js +42 -7
  40. package/dist/permissions/kernel-sandbox.js +197 -0
  41. package/dist/permissions/prompt.js +10 -1
  42. package/dist/permissions/sandbox.js +40 -17
  43. package/dist/providers/anthropic.js +154 -7
  44. package/dist/providers/codex-auth.js +28 -0
  45. package/dist/providers/codex.js +134 -23
  46. package/dist/providers/history.js +35 -0
  47. package/dist/providers/ollama.js +37 -6
  48. package/dist/providers/openai-compat.js +171 -74
  49. package/dist/providers/openrouter.js +47 -17
  50. package/dist/providers/replay.js +62 -0
  51. package/dist/providers/resolve.js +41 -7
  52. package/dist/providers/retry.js +38 -5
  53. package/dist/providers/types.js +9 -1
  54. package/dist/repl.js +47 -5
  55. package/dist/session/log.js +217 -0
  56. package/dist/session/persist.js +190 -0
  57. package/dist/settings.js +42 -26
  58. package/dist/system-prompt.js +18 -2
  59. package/dist/tools/apply-patch.js +355 -0
  60. package/dist/tools/edit-engine.js +287 -0
  61. package/dist/tools/edit-file.js +86 -27
  62. package/dist/tools/grep.js +25 -10
  63. package/dist/tools/lint-test.js +0 -22
  64. package/dist/tools/mcp-resources.js +51 -6
  65. package/dist/tools/phren-finding.js +12 -4
  66. package/dist/tools/phren-search.js +7 -2
  67. package/dist/tools/read-file.js +37 -9
  68. package/dist/tools/read-image.js +76 -0
  69. package/dist/tools/registry.js +27 -5
  70. package/dist/tools/shell.js +134 -71
  71. package/dist/tools/skill.js +20 -20
  72. package/dist/tools/spawn-agent.js +77 -13
  73. package/dist/tools/update-plan.js +48 -0
  74. package/dist/tools/web-fetch.js +110 -12
  75. package/dist/tools/web-search.js +4 -4
  76. package/dist/tui/components/App.js +98 -26
  77. package/dist/tui/components/ApprovalPanel.js +8 -0
  78. package/dist/tui/components/InputArea.js +5 -5
  79. package/dist/tui/components/ModelPicker.js +43 -0
  80. package/dist/tui/components/PhrenInput.js +10 -5
  81. package/dist/tui/components/PlanReview.js +7 -0
  82. package/dist/tui/components/ShortcutHelp.js +5 -0
  83. package/dist/tui/components/StatusBar.js +46 -20
  84. package/dist/tui/components/SteerQueue.js +1 -1
  85. package/dist/tui/components/ThinkingIndicator.js +6 -4
  86. package/dist/tui/components/ToolCall.js +10 -4
  87. package/dist/tui/components/ToolDetail.js +21 -0
  88. package/dist/tui/help.js +6 -0
  89. package/dist/tui/hooks/useKeyboardShortcuts.js +15 -1
  90. package/dist/tui/index.js +0 -1
  91. package/dist/tui/ink-entry.js +317 -33
  92. package/dist/tui/tool-render.js +1 -1
  93. package/dist/user-hooks.js +171 -0
  94. package/package.json +14 -8
  95. package/dist/multi/multi-commands.js +0 -170
  96. package/dist/multi/multi-events.js +0 -98
  97. package/dist/multi/multi-render.js +0 -147
  98. package/dist/multi/pane.js +0 -28
  99. package/dist/multi/tui-multi.js +0 -200
  100. package/dist/permissions/privacy.js +0 -248
  101. package/dist/tui/components/AgentMessage.js +0 -5
  102. package/dist/tui/components/ChatMessage.js +0 -23
  103. package/dist/tui/components/CodeBlock.js +0 -8
  104. package/dist/tui/components/Separator.js +0 -7
  105. package/dist/tui/components/StreamingText.js +0 -5
  106. package/dist/tui/components/UserMessage.js +0 -5
  107. package/dist/tui/menu-mode.js +0 -86
  108. package/dist/tui/mouse.js +0 -71
  109. package/dist/tui/terminal-control.js +0 -62
  110. package/dist/tui.js +0 -781
@@ -1,4 +1,5 @@
1
1
  import { endSessionRecord, findMostRecentSummaryWithProject, incrementSessionStateCounter, loadLastSessionMessages as loadSharedLastSessionMessages, loadLastSessionSnapshot as loadSharedLastSessionSnapshot, saveSessionMessages as saveSharedSessionMessages, startSessionRecord, } from "@phren/cli/session/artifacts";
2
+ import { addNote } from "@phren/cli/data/notes";
2
3
  export function startSession(ctx) {
3
4
  return startSessionRecord(ctx.phrenPath, {
4
5
  project: ctx.project ?? undefined,
@@ -8,11 +9,45 @@ export function startSession(ctx) {
8
9
  export function endSession(ctx, sessionId, summary) {
9
10
  endSessionRecord(ctx.phrenPath, sessionId, summary);
10
11
  }
12
+ function truncateLine(text, max) {
13
+ const oneLine = text.replace(/\s+/g, " ").trim();
14
+ return oneLine.length > max ? `${oneLine.slice(0, max)}…` : oneLine;
15
+ }
16
+ /**
17
+ * Mirror a session summary into the project's notes. Notes are FTS-indexed
18
+ * but non-injectable — past sessions become searchable via phren_search with
19
+ * zero prompt-leak risk. Best effort, never throws.
20
+ */
21
+ export function writeSessionNote(ctx, opts) {
22
+ if (!ctx.project)
23
+ return;
24
+ try {
25
+ const parts = [`[agent session${opts.sessionId ? ` ${opts.sessionId.slice(0, 8)}` : ""}]`];
26
+ if (opts.task)
27
+ parts.push(`task: ${truncateLine(opts.task, 200)}`);
28
+ if (opts.outcome)
29
+ parts.push(`outcome: ${truncateLine(opts.outcome, 500)}`);
30
+ if (parts.length === 1)
31
+ return;
32
+ addNote(ctx.phrenPath, ctx.project, parts.join(" — "));
33
+ }
34
+ catch {
35
+ // best effort
36
+ }
37
+ }
11
38
  export function incrementSessionCounter(phrenPath, sessionId, counter) {
12
39
  incrementSessionStateCounter(phrenPath, sessionId, counter);
13
40
  }
14
41
  export function getPriorSummary(ctx) {
15
- return findMostRecentSummaryWithProject(ctx.phrenPath, ctx.project ?? undefined).summary;
42
+ const lookup = findMostRecentSummaryWithProject(ctx.phrenPath, ctx.project ?? undefined);
43
+ if (!lookup.summary)
44
+ return null;
45
+ // The CLI lookup falls back to the most recent summary from ANY project when
46
+ // the current project has none. A different project's session summary is more
47
+ // likely to mislead than help, so drop it rather than inject it unlabeled.
48
+ if (ctx.project && lookup.project && lookup.project !== ctx.project)
49
+ return null;
50
+ return { summary: lookup.summary, project: lookup.project, endedAt: lookup.endedAt };
16
51
  }
17
52
  export function saveSessionMessages(phrenPath, sessionId, messages, project) {
18
53
  saveSharedSessionMessages(phrenPath, sessionId, messages, project);
package/dist/models.js CHANGED
@@ -2,39 +2,54 @@ export const REASONING_LEVELS = ["low", "medium", "high", "xhigh"];
2
2
  const BUILTIN_MODELS = {
3
3
  anthropic: [
4
4
  {
5
- id: "claude-sonnet-4-20250514",
5
+ id: "claude-sonnet-5",
6
+ vision: true,
6
7
  provider: "anthropic",
7
- label: "Sonnet 4",
8
- contextWindow: 200_000,
9
- maxOutputTokens: 16_384,
10
- reasoningDefault: "medium",
11
- reasoningRange: ["low", "medium", "high"],
8
+ label: "Sonnet 5",
9
+ contextWindow: 1_000_000,
10
+ maxOutputTokens: 32_768,
11
+ reasoningDefault: "high",
12
+ reasoningRange: ["low", "medium", "high", "xhigh"],
12
13
  pricing: { inputPer1M: 3, outputPer1M: 15 },
13
14
  },
14
15
  {
15
- id: "claude-opus-4-20250514",
16
+ id: "claude-opus-5",
17
+ vision: true,
16
18
  provider: "anthropic",
17
- label: "Opus 4",
18
- contextWindow: 200_000,
19
+ label: "Opus 5",
20
+ contextWindow: 1_000_000,
19
21
  maxOutputTokens: 32_768,
20
22
  reasoningDefault: "high",
21
23
  reasoningRange: ["low", "medium", "high", "xhigh"],
22
- pricing: { inputPer1M: 15, outputPer1M: 75 },
24
+ pricing: { inputPer1M: 5, outputPer1M: 25 },
25
+ },
26
+ {
27
+ id: "claude-opus-4-8",
28
+ vision: true,
29
+ provider: "anthropic",
30
+ label: "Opus 4.8",
31
+ contextWindow: 1_000_000,
32
+ maxOutputTokens: 32_768,
33
+ reasoningDefault: "high",
34
+ reasoningRange: ["low", "medium", "high", "xhigh"],
35
+ pricing: { inputPer1M: 5, outputPer1M: 25 },
23
36
  },
24
37
  {
25
38
  id: "claude-haiku-4-5-20251001",
39
+ vision: true,
26
40
  provider: "anthropic",
27
41
  label: "Haiku 4.5",
28
42
  contextWindow: 200_000,
29
- maxOutputTokens: 8_192,
43
+ maxOutputTokens: 16_384,
30
44
  reasoningDefault: null,
31
45
  reasoningRange: [],
32
- pricing: { inputPer1M: 0.8, outputPer1M: 4 },
46
+ pricing: { inputPer1M: 1, outputPer1M: 5 },
33
47
  },
34
48
  ],
35
49
  openrouter: [
36
50
  {
37
51
  id: "anthropic/claude-sonnet-4-20250514",
52
+ vision: true,
38
53
  provider: "openrouter",
39
54
  label: "Sonnet 4",
40
55
  contextWindow: 200_000,
@@ -45,6 +60,7 @@ const BUILTIN_MODELS = {
45
60
  },
46
61
  {
47
62
  id: "anthropic/claude-opus-4-20250514",
63
+ vision: true,
48
64
  provider: "openrouter",
49
65
  label: "Opus 4",
50
66
  contextWindow: 200_000,
@@ -55,6 +71,7 @@ const BUILTIN_MODELS = {
55
71
  },
56
72
  {
57
73
  id: "openai/gpt-4o",
74
+ vision: true,
58
75
  provider: "openrouter",
59
76
  label: "GPT-4o",
60
77
  contextWindow: 128_000,
@@ -65,6 +82,7 @@ const BUILTIN_MODELS = {
65
82
  },
66
83
  {
67
84
  id: "openai/o4-mini",
85
+ vision: true,
68
86
  provider: "openrouter",
69
87
  label: "o4-mini",
70
88
  contextWindow: 128_000,
@@ -75,6 +93,7 @@ const BUILTIN_MODELS = {
75
93
  },
76
94
  {
77
95
  id: "google/gemini-2.5-pro",
96
+ vision: true,
78
97
  provider: "openrouter",
79
98
  label: "Gemini 2.5 Pro",
80
99
  contextWindow: 1_000_000,
@@ -85,6 +104,7 @@ const BUILTIN_MODELS = {
85
104
  },
86
105
  {
87
106
  id: "google/gemini-2.5-flash",
107
+ vision: true,
88
108
  provider: "openrouter",
89
109
  label: "Gemini 2.5 Flash",
90
110
  contextWindow: 1_000_000,
@@ -93,28 +113,60 @@ const BUILTIN_MODELS = {
93
113
  reasoningRange: ["low", "medium", "high"],
94
114
  pricing: { inputPer1M: 0.15, outputPer1M: 0.6 },
95
115
  },
116
+ {
117
+ id: "deepseek/deepseek-v4.1-flash",
118
+ vision: true,
119
+ provider: "openrouter",
120
+ label: "DeepSeek V4.1 Flash",
121
+ contextWindow: 1_000_000,
122
+ maxOutputTokens: 65_536,
123
+ reasoningDefault: "medium",
124
+ reasoningRange: ["low", "medium", "high"],
125
+ pricing: { inputPer1M: 0.14, outputPer1M: 0.42 },
126
+ },
127
+ {
128
+ id: "deepseek/deepseek-v4-pro",
129
+ provider: "openrouter",
130
+ label: "DeepSeek V4 Pro",
131
+ contextWindow: 1_000_000,
132
+ maxOutputTokens: 65_536,
133
+ reasoningDefault: "medium",
134
+ reasoningRange: ["low", "medium", "high"],
135
+ pricing: { inputPer1M: 0.42, outputPer1M: 0.84 },
136
+ },
137
+ {
138
+ id: "deepseek/deepseek-v4-flash",
139
+ provider: "openrouter",
140
+ label: "DeepSeek V4 Flash",
141
+ contextWindow: 1_000_000,
142
+ maxOutputTokens: 65_536,
143
+ reasoningDefault: "medium",
144
+ reasoningRange: ["low", "medium", "high"],
145
+ pricing: { inputPer1M: 0.05, outputPer1M: 0.1 },
146
+ },
96
147
  {
97
148
  id: "deepseek/deepseek-r1",
98
149
  provider: "openrouter",
99
150
  label: "DeepSeek R1",
100
- contextWindow: 128_000,
151
+ contextWindow: 64_000,
101
152
  maxOutputTokens: 8_192,
102
153
  reasoningDefault: "high",
103
154
  reasoningRange: ["medium", "high"],
104
- pricing: { inputPer1M: 0.55, outputPer1M: 2.19 },
155
+ pricing: { inputPer1M: 0.7, outputPer1M: 2.5 },
105
156
  },
106
157
  {
107
- id: "deepseek/deepseek-v3",
158
+ id: "deepseek/deepseek-v3.2",
108
159
  provider: "openrouter",
109
- label: "DeepSeek V3",
110
- contextWindow: 128_000,
160
+ label: "DeepSeek V3.2",
161
+ contextWindow: 163_840,
111
162
  maxOutputTokens: 8_192,
112
163
  reasoningDefault: null,
113
164
  reasoningRange: [],
114
- pricing: { inputPer1M: 0.27, outputPer1M: 1.1 },
165
+ pricing: { inputPer1M: 0.27, outputPer1M: 0.4 },
115
166
  },
116
167
  {
117
168
  id: "meta-llama/llama-4-maverick",
169
+ vision: true,
118
170
  provider: "openrouter",
119
171
  label: "Llama 4 Maverick",
120
172
  contextWindow: 128_000,
@@ -137,6 +189,7 @@ const BUILTIN_MODELS = {
137
189
  openai: [
138
190
  {
139
191
  id: "gpt-5.4",
192
+ vision: true,
140
193
  provider: "openai",
141
194
  label: "GPT-5.4",
142
195
  contextWindow: 1_050_000,
@@ -147,6 +200,7 @@ const BUILTIN_MODELS = {
147
200
  },
148
201
  {
149
202
  id: "gpt-4o",
203
+ vision: true,
150
204
  provider: "openai",
151
205
  label: "GPT-4o",
152
206
  contextWindow: 128_000,
@@ -157,6 +211,7 @@ const BUILTIN_MODELS = {
157
211
  },
158
212
  {
159
213
  id: "o4-mini",
214
+ vision: true,
160
215
  provider: "openai",
161
216
  label: "o4-mini",
162
217
  contextWindow: 200_000,
@@ -167,6 +222,7 @@ const BUILTIN_MODELS = {
167
222
  },
168
223
  {
169
224
  id: "o3",
225
+ vision: true,
170
226
  provider: "openai",
171
227
  label: "o3",
172
228
  contextWindow: 200_000,
@@ -179,6 +235,7 @@ const BUILTIN_MODELS = {
179
235
  "openai-codex": [
180
236
  {
181
237
  id: "gpt-5.4",
238
+ vision: true,
182
239
  provider: "openai-codex",
183
240
  label: "GPT-5.4",
184
241
  contextWindow: 1_050_000,
@@ -189,6 +246,7 @@ const BUILTIN_MODELS = {
189
246
  },
190
247
  {
191
248
  id: "gpt-4o",
249
+ vision: true,
192
250
  provider: "openai-codex",
193
251
  label: "GPT-4o",
194
252
  contextWindow: 128_000,
@@ -199,6 +257,7 @@ const BUILTIN_MODELS = {
199
257
  },
200
258
  {
201
259
  id: "o4-mini",
260
+ vision: true,
202
261
  provider: "openai-codex",
203
262
  label: "o4-mini",
204
263
  contextWindow: 200_000,
@@ -209,6 +268,7 @@ const BUILTIN_MODELS = {
209
268
  },
210
269
  {
211
270
  id: "o3",
271
+ vision: true,
212
272
  provider: "openai-codex",
213
273
  label: "o3",
214
274
  contextWindow: 200_000,
@@ -218,6 +278,28 @@ const BUILTIN_MODELS = {
218
278
  metered: false,
219
279
  },
220
280
  ],
281
+ deepseek: [
282
+ {
283
+ id: "deepseek-flash",
284
+ provider: "deepseek",
285
+ label: "DeepSeek V4.1 Flash (direct)",
286
+ contextWindow: 1_000_000,
287
+ maxOutputTokens: 65_536,
288
+ reasoningDefault: null,
289
+ reasoningRange: ["low", "medium", "high"],
290
+ pricing: { inputPer1M: 0.3, outputPer1M: 1.2 },
291
+ },
292
+ {
293
+ id: "deepseek-v4-pro",
294
+ provider: "deepseek",
295
+ label: "DeepSeek V4 Pro (direct)",
296
+ contextWindow: 1_000_000,
297
+ maxOutputTokens: 65_536,
298
+ reasoningDefault: null,
299
+ reasoningRange: ["low", "medium", "high"],
300
+ pricing: { inputPer1M: 1.32, outputPer1M: 3.96 },
301
+ },
302
+ ],
221
303
  ollama: [
222
304
  {
223
305
  id: "qwen2.5-coder:14b",
@@ -255,6 +337,9 @@ const BUILTIN_MODELS = {
255
337
  ],
256
338
  };
257
339
  const LEGACY_OUTPUT_LIMITS = [
340
+ ["claude-fable-5", 32_768],
341
+ ["claude-opus-5", 32_768],
342
+ ["claude-sonnet-5", 32_768],
258
343
  ["claude-opus-4", 32_768],
259
344
  ["claude-sonnet-4", 16_384],
260
345
  ["claude-haiku-4", 8_192],
@@ -268,14 +353,42 @@ const LEGACY_OUTPUT_LIMITS = [
268
353
  ["o3", 100_000],
269
354
  ["o4-mini", 100_000],
270
355
  ["gemini-2.5", 8_192],
356
+ ["deepseek-v4", 65_536],
271
357
  ["deepseek", 8_192],
272
358
  ["llama-4", 8_192],
273
359
  ["qwen", 8_192],
274
360
  ];
361
+ const LEGACY_CONTEXT_LIMITS = [
362
+ ["claude-fable-5", 1_000_000],
363
+ ["claude-opus-5", 1_000_000],
364
+ ["claude-sonnet-5", 1_000_000],
365
+ ["claude-opus-4", 200_000],
366
+ ["claude-sonnet-4", 200_000],
367
+ ["claude-haiku-4", 200_000],
368
+ ["claude-3", 200_000],
369
+ ["gpt-5", 400_000],
370
+ ["gpt-4.1", 1_000_000],
371
+ ["gpt-4o", 128_000],
372
+ ["gpt-4", 128_000],
373
+ ["o3", 200_000],
374
+ ["o4-mini", 200_000],
375
+ ["gemini", 1_000_000],
376
+ ["deepseek", 128_000],
377
+ ["llama", 128_000],
378
+ ["qwen", 128_000],
379
+ ["mistral", 32_000],
380
+ ];
275
381
  const LEGACY_PRICING = [
382
+ ["claude-fable-5", { inputPer1M: 10, outputPer1M: 50 }],
383
+ ["claude-opus-5", { inputPer1M: 5, outputPer1M: 25 }],
384
+ ["claude-sonnet-5", { inputPer1M: 3, outputPer1M: 15 }],
385
+ ["claude-opus-4-8", { inputPer1M: 5, outputPer1M: 25 }],
386
+ ["claude-opus-4-7", { inputPer1M: 5, outputPer1M: 25 }],
387
+ ["claude-opus-4-6", { inputPer1M: 5, outputPer1M: 25 }],
388
+ ["claude-sonnet-4-6", { inputPer1M: 3, outputPer1M: 15 }],
276
389
  ["claude-opus-4", { inputPer1M: 15, outputPer1M: 75 }],
277
390
  ["claude-sonnet-4", { inputPer1M: 3, outputPer1M: 15 }],
278
- ["claude-haiku-4", { inputPer1M: 0.8, outputPer1M: 4 }],
391
+ ["claude-haiku-4", { inputPer1M: 1, outputPer1M: 5 }],
279
392
  ["claude-3-5-sonnet", { inputPer1M: 3, outputPer1M: 15 }],
280
393
  ["claude-3-5-haiku", { inputPer1M: 0.8, outputPer1M: 4 }],
281
394
  ["claude-3-opus", { inputPer1M: 15, outputPer1M: 75 }],
@@ -292,6 +405,9 @@ const LEGACY_PRICING = [
292
405
  ["openai/gpt-4o", { inputPer1M: 2.5, outputPer1M: 10 }],
293
406
  ["google/gemini-2.5-pro", { inputPer1M: 1.25, outputPer1M: 10 }],
294
407
  ["google/gemini-2.5-flash", { inputPer1M: 0.15, outputPer1M: 0.6 }],
408
+ ["deepseek/deepseek-v4.1-flash", { inputPer1M: 0.15, outputPer1M: 0.6 }],
409
+ ["deepseek/deepseek-v4-flash", { inputPer1M: 0.05, outputPer1M: 0.1 }],
410
+ ["deepseek/deepseek-v4-pro", { inputPer1M: 1.6, outputPer1M: 3.2 }],
295
411
  ["deepseek/deepseek-r1", { inputPer1M: 0.55, outputPer1M: 2.19 }],
296
412
  ["deepseek/deepseek-v3", { inputPer1M: 0.27, outputPer1M: 1.1 }],
297
413
  ["deepseek-r1", { inputPer1M: 0.55, outputPer1M: 2.19 }],
@@ -303,6 +419,7 @@ const LEGACY_PRICING = [
303
419
  ["ollama", { inputPer1M: 0, outputPer1M: 0 }],
304
420
  ];
305
421
  LEGACY_OUTPUT_LIMITS.sort((a, b) => b[0].length - a[0].length);
422
+ LEGACY_CONTEXT_LIMITS.sort((a, b) => b[0].length - a[0].length);
306
423
  LEGACY_PRICING.sort((a, b) => b[0].length - a[0].length);
307
424
  export function normalizeProviderId(provider) {
308
425
  if (!provider)
@@ -313,6 +430,7 @@ export function normalizeProviderId(provider) {
313
430
  provider === "anthropic" ||
314
431
  provider === "openai" ||
315
432
  provider === "openai-codex" ||
433
+ provider === "deepseek" ||
316
434
  provider === "ollama") {
317
435
  return provider;
318
436
  }
@@ -352,6 +470,10 @@ export function getModelMetadata(provider, model) {
352
470
  }
353
471
  return undefined;
354
472
  }
473
+ /** Whether a model accepts image input. Unknown models are text-only (fail closed). */
474
+ export function modelSupportsVision(provider, model) {
475
+ return getModelMetadata(provider, model)?.vision === true;
476
+ }
355
477
  export function getDefaultReasoningEffort(provider, model) {
356
478
  return getModelMetadata(provider, model)?.reasoningDefault ?? undefined;
357
479
  }
@@ -369,6 +491,17 @@ export function lookupMaxOutputTokens(model, provider) {
369
491
  }
370
492
  return 8_192;
371
493
  }
494
+ export function lookupContextWindow(model, provider) {
495
+ const metadata = getModelMetadata(provider, model);
496
+ if (metadata)
497
+ return metadata.contextWindow;
498
+ const lower = model.toLowerCase();
499
+ for (const [prefix, limit] of LEGACY_CONTEXT_LIMITS) {
500
+ if (lower.startsWith(prefix))
501
+ return limit;
502
+ }
503
+ return 200_000;
504
+ }
372
505
  export function lookupPricing(model, provider) {
373
506
  const normalizedProvider = normalizeProviderId(provider);
374
507
  if (normalizedProvider === "openai-codex") {
@@ -8,24 +8,35 @@
8
8
  * 5. Wakes on WakeMessage or DeliverMessage to continue
9
9
  * 6. Shuts down gracefully on ShutdownRequest
10
10
  */
11
+ import { MAX_SPAWN_DEPTH } from "./types.js";
11
12
  import { resolveProvider } from "../providers/resolve.js";
12
13
  import { ToolRegistry } from "../tools/registry.js";
13
14
  import { readFileTool } from "../tools/read-file.js";
14
15
  import { writeFileTool } from "../tools/write-file.js";
15
- import { editFileTool } from "../tools/edit-file.js";
16
- import { shellTool, taskOutputTool, taskStopTool } from "../tools/shell.js";
16
+ import { editFileTool, multiEditTool } from "../tools/edit-file.js";
17
+ import { applyPatchTool } from "../tools/apply-patch.js";
18
+ import { createShellTool, taskOutputTool, taskStopTool } from "../tools/shell.js";
17
19
  import { globTool } from "../tools/glob.js";
18
20
  import { grepTool } from "../tools/grep.js";
21
+ import { createReadImageTool } from "../tools/read-image.js";
22
+ import { modelSupportsVision } from "../models.js";
23
+ import { createWebFetchTool } from "../tools/web-fetch.js";
24
+ import { createWebSearchTool } from "../tools/web-search.js";
25
+ import { updatePlanTool } from "../tools/update-plan.js";
26
+ import { listMcpResourcesTool, readMcpResourceTool } from "../tools/mcp-resources.js";
19
27
  import { createPhrenSearchTool } from "../tools/phren-search.js";
20
28
  import { createPhrenFindingTool } from "../tools/phren-finding.js";
21
29
  import { createPhrenGetTasksTool, createPhrenCompleteTaskTool } from "../tools/phren-tasks.js";
30
+ import { createPhrenAddTaskTool } from "../tools/phren-add-task.js";
22
31
  import { createSkillTool } from "../tools/skill.js";
23
32
  import { gitStatusTool, gitDiffTool, gitCommitTool } from "../tools/git.js";
24
- import { buildPhrenContext } from "../memory/context.js";
25
- import { startSession, endSession } from "../memory/session.js";
26
- import { runAgent } from "../agent-loop.js";
33
+ import { buildPhrenContext, } from "../memory/context.js";
34
+ import { startSession, endSession, } from "../memory/session.js";
35
+ import { runAgent, } from "../agent-loop.js";
27
36
  import { createCostTracker } from "../cost.js";
28
37
  import { getAgentType, applyAgentType } from "./agent-types.js";
38
+ import { AgentSpawner } from "./spawner.js";
39
+ import { createSpawnAgentTool, createSendMessageTool, createListAgentsTool } from "../tools/spawn-agent.js";
29
40
  /** Send a typed message to the parent process. */
30
41
  function send(msg) {
31
42
  if (process.send) {
@@ -85,27 +96,61 @@ async function initAgentState(payload) {
85
96
  const registry = new ToolRegistry();
86
97
  registry.setPermissions({
87
98
  mode: permissions,
88
- allowedPaths: [],
99
+ allowedPaths: payload.allowedPaths ?? [],
89
100
  projectRoot: payload.worktreePath ?? cwd,
101
+ sandboxMode: payload.sandboxMode,
90
102
  });
103
+ // Headless child: an "ask" verdict has no human to answer it, and the
104
+ // default readline prompt would hang forever on a closed stdin. Deny with
105
+ // a clear message instead (children normally run auto-confirm, so this
106
+ // only fires for the ask-category edge cases).
107
+ if (!process.stdin.isTTY) {
108
+ registry.askUser = async (toolName, _input, reason) => {
109
+ process.stderr.write(`[child] denied ${toolName} (needs approval, no interactive user): ${reason}\n`);
110
+ return false;
111
+ };
112
+ }
91
113
  registry.register(readFileTool);
92
114
  registry.register(writeFileTool);
93
115
  registry.register(editFileTool);
94
- registry.register(shellTool);
116
+ registry.register(multiEditTool);
117
+ registry.register(applyPatchTool);
118
+ // Children inherit the kernel write fence via the live registry config
119
+ registry.register(createShellTool(() => registry.permissionConfig));
95
120
  registry.register(taskOutputTool);
96
121
  registry.register(taskStopTool);
97
122
  registry.register(globTool);
98
123
  registry.register(grepTool);
124
+ if (modelSupportsVision(provider.name, provider.model ?? "")) {
125
+ registry.register(createReadImageTool(provider));
126
+ }
127
+ registry.register(createWebFetchTool());
128
+ registry.register(createWebSearchTool());
129
+ registry.register(updatePlanTool);
130
+ registry.register(listMcpResourcesTool);
131
+ registry.register(readMcpResourceTool);
99
132
  if (phrenCtx) {
100
133
  registry.register(createPhrenSearchTool(phrenCtx));
101
134
  registry.register(createPhrenFindingTool(phrenCtx, sessionId));
102
135
  registry.register(createPhrenGetTasksTool(phrenCtx));
103
136
  registry.register(createPhrenCompleteTaskTool(phrenCtx, sessionId));
137
+ registry.register(createPhrenAddTaskTool(phrenCtx, sessionId));
104
138
  registry.register(createSkillTool(phrenCtx));
105
139
  }
106
140
  registry.register(gitStatusTool);
107
141
  registry.register(gitDiffTool);
108
142
  registry.register(gitCommitTool);
143
+ // Cost tracker
144
+ const modelName = provider.model ?? model ?? provider.name;
145
+ const costTracker = createCostTracker(modelName, budget, provider.name);
146
+ let spawner = null;
147
+ const depth = payload.depth ?? 0;
148
+ if (depth < MAX_SPAWN_DEPTH) {
149
+ spawner = new AgentSpawner({ costTracker, depth, getPermissionDefaults: () => registry.permissionConfig });
150
+ registry.register(createSpawnAgentTool(spawner, () => registry.permissionConfig));
151
+ registry.register(createSendMessageTool(spawner));
152
+ registry.register(createListAgentsTool(spawner));
153
+ }
109
154
  // Apply agent type restrictions (tool allow/disallow lists, prompt prefix)
110
155
  if (payload.agentType) {
111
156
  const typeDef = getAgentType(payload.agentType);
@@ -113,9 +158,6 @@ async function initAgentState(payload) {
113
158
  applyAgentType(registry, typeDef);
114
159
  }
115
160
  }
116
- // Cost tracker
117
- const modelName = provider.model ?? model ?? provider.name;
118
- const costTracker = createCostTracker(modelName, budget, provider.name);
119
161
  return {
120
162
  agentId,
121
163
  provider,
@@ -128,12 +170,16 @@ async function initAgentState(payload) {
128
170
  verbose,
129
171
  plan,
130
172
  hooks: createIpcHooks(agentId),
173
+ spawner,
131
174
  pendingDms: [],
132
175
  taskCount: 0,
133
176
  };
134
177
  }
135
178
  /** Run a single task. Returns the result. */
136
179
  async function runTask(state, task) {
180
+ const beforeInput = state.costTracker.totalInputTokens;
181
+ const beforeOutput = state.costTracker.totalOutputTokens;
182
+ const beforeCost = state.costTracker.totalCost;
137
183
  const config = {
138
184
  provider: state.provider,
139
185
  registry: state.registry,
@@ -152,6 +198,9 @@ async function runTask(state, task) {
152
198
  turns: result.turns,
153
199
  toolCalls: result.toolCalls,
154
200
  totalCost: result.totalCost,
201
+ inputTokens: state.costTracker.totalInputTokens - beforeInput,
202
+ outputTokens: state.costTracker.totalOutputTokens - beforeOutput,
203
+ costUsd: state.costTracker.totalCost - beforeCost,
155
204
  };
156
205
  }
157
206
  /** Enter idle state — notify parent, wait for wake/shutdown/message. */
@@ -172,6 +221,9 @@ function shutdown(state) {
172
221
  if (state.phrenCtx && state.sessionId) {
173
222
  endSession(state.phrenCtx, state.sessionId, `Agent shut down after ${state.taskCount} tasks`);
174
223
  }
224
+ const spawner = state.spawner;
225
+ if (spawner)
226
+ void spawner.shutdown().catch(() => { });
175
227
  send({ type: "shutdown_approved", agentId: state.agentId });
176
228
  process.exit(0);
177
229
  }
@@ -335,6 +387,9 @@ process.on("message", (msg) => {
335
387
  send({ type: "error", agentId, error: err instanceof Error ? err.message : String(err) });
336
388
  });
337
389
  });
390
+ process.on("disconnect", () => {
391
+ process.exit(0);
392
+ });
338
393
  // If no IPC channel (run directly), exit with error
339
394
  if (!process.send) {
340
395
  process.stderr.write("child-entry.ts must be spawned via AgentSpawner (requires IPC channel)\n");
@@ -68,7 +68,7 @@ const MAX_FIRST_CHUNK = 20;
68
68
  const CONTEXT_LINES = 3;
69
69
  const SBS_MAX_OUTPUT_LINES = 40;
70
70
  const SBS_COLLAPSE_THRESHOLD = 3;
71
- const SBS_MIN_WIDTH = 70;
71
+ const SBS_MIN_WIDTH = 100;
72
72
  /**
73
73
  * Render a colored inline diff between old and new file content.
74
74
  * Automatically switches to side-by-side when the terminal is wide enough.
@@ -81,8 +81,8 @@ const SBS_MIN_WIDTH = 70;
81
81
  * Capped at 30 output lines. If the diff is larger, shows the first 20
82
82
  * lines plus a "... (N more changes)" trailer.
83
83
  */
84
- export function renderInlineDiff(oldContent, newContent, filePath, colors) {
85
- const termWidth = process.stdout.columns || 80;
84
+ export function renderInlineDiff(oldContent, newContent, filePath, colors, width) {
85
+ const termWidth = width ?? (process.stdout.columns || 80);
86
86
  if (termWidth >= SBS_MIN_WIDTH) {
87
87
  return renderSideBySideDiff(oldContent, newContent, filePath ?? "", termWidth, colors);
88
88
  }