picocode-core 0.9.211 → 0.9.213
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/agents.js +64 -1
- package/src/claude-code/README.md +2 -2
- package/src/claude-code/cli.js +8 -1
- package/src/claude-code/engine.js +52 -4
- package/src/controller.js +5 -1
package/package.json
CHANGED
package/src/agents.js
CHANGED
|
@@ -31,6 +31,7 @@ function restoredAgent(data, at) {
|
|
|
31
31
|
error: null,
|
|
32
32
|
usage: null,
|
|
33
33
|
options: data,
|
|
34
|
+
external: !!data.external,
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
37
|
|
|
@@ -156,6 +157,66 @@ export function createAgentManager({ run, concurrency = 8, onChange = () => {},
|
|
|
156
157
|
return agent
|
|
157
158
|
}
|
|
158
159
|
|
|
160
|
+
// an agent another runtime runs, such as a Claude Code subagent: listed,
|
|
161
|
+
// recorded and shown like pico's own, but fed and finished from outside
|
|
162
|
+
function track(options) {
|
|
163
|
+
const id = String(nextId++)
|
|
164
|
+
let resolve
|
|
165
|
+
const done = new Promise((r) => { resolve = r })
|
|
166
|
+
const at = Date.now()
|
|
167
|
+
const agent = {
|
|
168
|
+
id,
|
|
169
|
+
description: options.description || options.prompt?.slice(0, 80) || `agent ${id}`,
|
|
170
|
+
prompt: options.prompt || '',
|
|
171
|
+
model: options.model || null,
|
|
172
|
+
role: 'worker',
|
|
173
|
+
parentId: null,
|
|
174
|
+
sessionId: options.sessionId || null,
|
|
175
|
+
sessionFile: options.sessionFile || null,
|
|
176
|
+
tools: [],
|
|
177
|
+
status: 'running',
|
|
178
|
+
createdAt: at,
|
|
179
|
+
updatedAt: at,
|
|
180
|
+
startedAt: at,
|
|
181
|
+
endedAt: null,
|
|
182
|
+
events: [],
|
|
183
|
+
result: '',
|
|
184
|
+
error: null,
|
|
185
|
+
usage: null,
|
|
186
|
+
controller: new AbortController(),
|
|
187
|
+
resolve,
|
|
188
|
+
done,
|
|
189
|
+
options,
|
|
190
|
+
external: true,
|
|
191
|
+
generation,
|
|
192
|
+
}
|
|
193
|
+
agents.set(id, agent)
|
|
194
|
+
onCreate(agent)
|
|
195
|
+
changed()
|
|
196
|
+
const live = () => agent.generation === generation && agent.status === 'running'
|
|
197
|
+
return {
|
|
198
|
+
id,
|
|
199
|
+
event(event) {
|
|
200
|
+
if (!live()) return
|
|
201
|
+
agent.events.push(event)
|
|
202
|
+
if (event.type === 'usage') agent.usage = event.usage
|
|
203
|
+
agent.updatedAt = Date.now()
|
|
204
|
+
onEvent(agent, event)
|
|
205
|
+
changed()
|
|
206
|
+
},
|
|
207
|
+
finish({ result = '', error = null, interrupted = false } = {}) {
|
|
208
|
+
if (!live()) return
|
|
209
|
+
agent.result = result
|
|
210
|
+
agent.error = error
|
|
211
|
+
agent.status = error ? 'failed' : interrupted ? 'cancelled' : 'completed'
|
|
212
|
+
agent.endedAt = Date.now()
|
|
213
|
+
onFinish(agent)
|
|
214
|
+
changed()
|
|
215
|
+
resolve(agent)
|
|
216
|
+
},
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
159
220
|
function restore(events) {
|
|
160
221
|
generation++
|
|
161
222
|
for (const agent of agents.values()) {
|
|
@@ -176,6 +237,7 @@ export function createAgentManager({ run, concurrency = 8, onChange = () => {},
|
|
|
176
237
|
|
|
177
238
|
return {
|
|
178
239
|
start,
|
|
240
|
+
track,
|
|
179
241
|
restore,
|
|
180
242
|
clear: () => restore([]),
|
|
181
243
|
get: (id) => agents.get(String(id)) || null,
|
|
@@ -196,7 +258,8 @@ export function createAgentManager({ run, concurrency = 8, onChange = () => {},
|
|
|
196
258
|
},
|
|
197
259
|
cancel(id) {
|
|
198
260
|
const agent = agents.get(String(id))
|
|
199
|
-
|
|
261
|
+
// an external agent stops only with whatever runs it
|
|
262
|
+
if (!agent || agent.external || ['completed', 'failed', 'cancelled'].includes(agent.status)) return false
|
|
200
263
|
if (agent.status === 'queued') {
|
|
201
264
|
agent.status = 'cancelled'
|
|
202
265
|
agent.endedAt = Date.now()
|
|
@@ -31,13 +31,13 @@ A Claude subscription can't be used as an API key. The supported way to use it i
|
|
|
31
31
|
- **Subscription only.** `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are removed from the child's environment. Either one would make Claude Code bill the API, silently. The models only appear when `claude auth status` reports a claude.ai sign-in.
|
|
32
32
|
- **No permission prompts.** Claude Code runs with `--dangerously-skip-permissions`, as pico does. Questions go through pico's `ask_user`, so `AskUserQuestion` is disallowed, and so are `EnterWorktree` and `ExitWorktree` because the host owns worktrees.
|
|
33
33
|
- **Compaction.** Claude Code compacts its own context, so pico doesn't auto-compact these sessions. A manual compact summarizes with the same model and restarts the Claude session from that summary.
|
|
34
|
+
- **Subagents.** Claude's subagents show in pico's activity list, with their tool calls and reports, through the agent manager's `track`. Background tasks are turned off (`CLAUDE_CODE_DISABLE_BACKGROUND_TASKS`): work that outlives a turn would be invisible to pico, and the idle stop would kill it. Subagents still run in parallel, and the turn waits for them.
|
|
34
35
|
|
|
35
36
|
## Not covered
|
|
36
37
|
|
|
37
|
-
- What happens inside a Claude `Task` subagent: only the call and its result are shown.
|
|
38
38
|
- pico's own subagents and deliberation. Claude Code has its own.
|
|
39
39
|
- The subscription's rate-limit status.
|
|
40
40
|
|
|
41
41
|
## Checking it
|
|
42
42
|
|
|
43
|
-
`test/claude-code.test.js` runs the engine against a scripted Claude Code; no model is called. `node scripts/claude-code-smoke.js [model]` runs real turns through the installed `claude` (haiku by default): an edit, bash, ask_user, an interrupt, and a resume after restart. Run it after upgrading Claude Code, since the stream-json control protocol is the Agent SDK's and isn't separately documented.
|
|
43
|
+
`test/claude-code.test.js` runs the engine against a scripted Claude Code; no model is called. `node scripts/claude-code-smoke.js [model]` runs real turns through the installed `claude` (haiku by default): an edit, bash, ask_user, an interrupt, parallel subagents, and a resume after restart. Run it after upgrading Claude Code, since the stream-json control protocol is the Agent SDK's and isn't separately documented.
|
package/src/claude-code/cli.js
CHANGED
|
@@ -33,6 +33,9 @@ export function claudeArgs({ model, effort, resume, fork, mcpConfig, appendSyste
|
|
|
33
33
|
// Claude Code itself
|
|
34
34
|
'--dangerously-skip-permissions',
|
|
35
35
|
'--disallowedTools', DISALLOWED.join(','),
|
|
36
|
+
// a subagent's text and thinking, not only its tool calls, so pico can
|
|
37
|
+
// show what it is doing
|
|
38
|
+
'--forward-subagent-text',
|
|
36
39
|
]
|
|
37
40
|
if (oneShot) args.push('--tools', '', '--no-session-persistence', '--strict-mcp-config')
|
|
38
41
|
if (model) args.push('--model', model)
|
|
@@ -45,7 +48,11 @@ export function claudeArgs({ model, effort, resume, fork, mcpConfig, appendSyste
|
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
export function spawnClaude({ command = 'claude', cwd, env, onMessage, onExit, ...options }) {
|
|
48
|
-
|
|
51
|
+
// background tasks keep running after a turn ends, where pico cannot see
|
|
52
|
+
// them and the idle stop would kill them; in the foreground, subagents
|
|
53
|
+
// still run in parallel, and the turn waits for them
|
|
54
|
+
const childEnv = { ...claudeEnv({ ...process.env, ...env }), CLAUDE_CODE_DISABLE_BACKGROUND_TASKS: '1' }
|
|
55
|
+
const child = spawn(command, claudeArgs(options), { cwd, env: childEnv, stdio: ['pipe', 'pipe', 'pipe'] })
|
|
49
56
|
const pending = new Map()
|
|
50
57
|
let stderr = ''
|
|
51
58
|
let exited = false
|
|
@@ -20,6 +20,8 @@ import { claudeCommand, cliModel, CLAUDE_CODE_PROVIDER } from './models.js'
|
|
|
20
20
|
import { addUsage, COVERED_TOOLS, emptyUsage, fingerprint, picoTool, seedText, splitTurn, toolEntry, transcriptText, toolResultText, usageOf, userBlocks } from './translate.js'
|
|
21
21
|
|
|
22
22
|
const IDLE_MS = 10 * 60 * 1000
|
|
23
|
+
// Claude Code's tools that run a subagent
|
|
24
|
+
const SUBAGENT_TOOLS = new Set(['Task', 'Agent'])
|
|
23
25
|
// how long an interrupted turn may take to wind down before the process
|
|
24
26
|
// is stopped instead
|
|
25
27
|
const INTERRUPT_GRACE_MS = 10000
|
|
@@ -62,7 +64,9 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
62
64
|
entry.bridge = await bridge()
|
|
63
65
|
entry.model = model
|
|
64
66
|
entry.effort = effort
|
|
65
|
-
|
|
67
|
+
// a process that was replaced still exits, and may still speak, after
|
|
68
|
+
// the next one starts; only the current process reaches the turn
|
|
69
|
+
const proc = spawn({
|
|
66
70
|
command,
|
|
67
71
|
cwd,
|
|
68
72
|
env,
|
|
@@ -72,13 +76,15 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
72
76
|
fork,
|
|
73
77
|
mcpConfig: entry.bridge.mcpConfig(),
|
|
74
78
|
appendSystemPrompt: systemPrompt(contextFiles),
|
|
75
|
-
onMessage: (message) => entry.onMessage?.(message),
|
|
79
|
+
onMessage: (message) => { if (entry.proc === proc) entry.onMessage?.(message) },
|
|
76
80
|
onExit: (exit) => {
|
|
81
|
+
if (entry.proc !== proc) return
|
|
77
82
|
entry.proc = null
|
|
78
83
|
entry.lastExit = exit
|
|
79
84
|
entry.onExit?.(exit)
|
|
80
85
|
},
|
|
81
86
|
})
|
|
87
|
+
entry.proc = proc
|
|
82
88
|
}
|
|
83
89
|
|
|
84
90
|
async function run({ history, tools, recorder, modelName, effort, signal, onStream = () => {}, beforeRequest, host = {} }) {
|
|
@@ -154,6 +160,46 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
154
160
|
// Claude Code's messages are handled one at a time, in order
|
|
155
161
|
let chain = Promise.resolve()
|
|
156
162
|
|
|
163
|
+
// Claude's subagents, by the id of the Task call that started each,
|
|
164
|
+
// shown in pico's activity list as they work. A subagent's own traffic
|
|
165
|
+
// carries that id as its parent; it may start subagents of its own
|
|
166
|
+
const subagents = new Map()
|
|
167
|
+
const startSubagent = (block) => {
|
|
168
|
+
if (!host.subagents || !SUBAGENT_TOOLS.has(block.name)) return
|
|
169
|
+
// a subagent runs on the session's model unless its call names another
|
|
170
|
+
const handle = host.subagents.start({ description: block.input?.description, prompt: block.input?.prompt ?? '', model: `${CLAUDE_CODE_PROVIDER}/${block.input?.model ?? model}` })
|
|
171
|
+
subagents.set(block.id, { handle })
|
|
172
|
+
}
|
|
173
|
+
// `extra` is the structured result, whose report is free of the frame
|
|
174
|
+
// Claude Code wraps it in for the model
|
|
175
|
+
const finishSubagent = (block, extra) => {
|
|
176
|
+
const sub = subagents.get(block.tool_use_id)
|
|
177
|
+
if (!sub) return
|
|
178
|
+
subagents.delete(block.tool_use_id)
|
|
179
|
+
const text = Array.isArray(extra?.content) ? toolResultText(extra.content) : toolResultText(block.content)
|
|
180
|
+
sub.handle.finish(block.is_error ? { error: text || 'failed', interrupted } : { result: text })
|
|
181
|
+
}
|
|
182
|
+
const onSubagent = (message) => {
|
|
183
|
+
const sub = subagents.get(message.parent_tool_use_id)
|
|
184
|
+
if (!sub) return
|
|
185
|
+
if (message.type === 'assistant') {
|
|
186
|
+
for (const block of message.message?.content ?? []) {
|
|
187
|
+
if (block.type === 'text' && block.text) sub.handle.event({ type: 'content', content: block.text })
|
|
188
|
+
if (block.type !== 'tool_use') continue
|
|
189
|
+
const { name, args } = picoTool(block.name, block.input, host.cwd)
|
|
190
|
+
sub.handle.event({ type: 'tool_executing', call: { id: block.id, type: 'function', function: { name, arguments: JSON.stringify(args) } }, at: Date.now() })
|
|
191
|
+
startSubagent(block)
|
|
192
|
+
}
|
|
193
|
+
} else if (message.type === 'user') {
|
|
194
|
+
const blocks = (Array.isArray(message.message?.content) ? message.message.content : []).filter((block) => block.type === 'tool_result')
|
|
195
|
+
for (const block of blocks) {
|
|
196
|
+
const text = toolResultText(block.content)
|
|
197
|
+
sub.handle.event(block.is_error ? { type: 'tool_error', call: { id: block.tool_use_id }, error: text } : { type: 'tool_complete', call: { id: block.tool_use_id }, result: text })
|
|
198
|
+
finishSubagent(block, blocks.length === 1 ? message.tool_use_result : null)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
157
203
|
// offered: pico's tools that Claude Code has no version of its own. A
|
|
158
204
|
// call through the bridge lands on the tool_use Claude announced for
|
|
159
205
|
// it (same name, same arguments, oldest first), once every message
|
|
@@ -179,6 +225,7 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
179
225
|
if (bridged) unclaimed.push({ id: block.id, name, args: block.input ?? {} })
|
|
180
226
|
onStream({ type: 'tool_calls_ready', calls: [call] })
|
|
181
227
|
onStream({ type: 'tool_executing', call })
|
|
228
|
+
startSubagent(block)
|
|
182
229
|
}
|
|
183
230
|
if (!raws.has(id)) countUsage(id, message.message?.usage)
|
|
184
231
|
}
|
|
@@ -194,6 +241,7 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
194
241
|
const waiting = unclaimed.findIndex((candidate) => candidate.id === use.id)
|
|
195
242
|
if (waiting >= 0) unclaimed.splice(waiting, 1)
|
|
196
243
|
const text = toolResultText(block.content)
|
|
244
|
+
finishSubagent(block, blocks.length === 1 ? message.tool_use_result : null)
|
|
197
245
|
collected.push({ role: 'tool', tool_call_id: use.id, content: text })
|
|
198
246
|
// a bridged call the bridge never ran (Claude refused it first)
|
|
199
247
|
// has no entry yet, so it gets one like any other call
|
|
@@ -219,8 +267,7 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
219
267
|
const outcome = new Promise((resolve) => {
|
|
220
268
|
entry.onMessage = (message) => {
|
|
221
269
|
chain = chain.then(async () => {
|
|
222
|
-
|
|
223
|
-
if (message.parent_tool_use_id) return
|
|
270
|
+
if (message.parent_tool_use_id) return onSubagent(message)
|
|
224
271
|
if (message.type === 'system' && message.subtype === 'init') entry.claudeSessionId = message.session_id
|
|
225
272
|
else if (message.type === 'stream_event') {
|
|
226
273
|
const event = message.event
|
|
@@ -263,6 +310,7 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
263
310
|
entry.onMessage = null
|
|
264
311
|
entry.onExit = null
|
|
265
312
|
flushAssistant()
|
|
313
|
+
for (const sub of subagents.values()) sub.handle.finish({ interrupted: true })
|
|
266
314
|
// a call cut off by an interrupt still needs a result, or the history
|
|
267
315
|
// is invalid for whichever model reads it next
|
|
268
316
|
for (const use of uses.values()) collected.push({ role: 'tool', tool_call_id: use.id, content: '[interrupted before it finished]' })
|
package/src/controller.js
CHANGED
|
@@ -346,12 +346,14 @@ export function createController({ boot, run = runTurn, compactRun = compactHist
|
|
|
346
346
|
concurrency: 8,
|
|
347
347
|
defaults: () => ({ model: boot.researchModel }),
|
|
348
348
|
onChange: bumpActivity,
|
|
349
|
-
onCreate: (agent) => persist(makeEvent('agent_start', { agentId: agent.id, description: agent.description, prompt: agent.prompt, model: agent.model, role: agent.role, sessionId: agent.sessionId, sessionFile: agent.sessionFile, tools: agent.tools })),
|
|
349
|
+
onCreate: (agent) => persist(makeEvent('agent_start', { agentId: agent.id, description: agent.description, prompt: agent.prompt, model: agent.model, role: agent.role, sessionId: agent.sessionId, sessionFile: agent.sessionFile, tools: agent.tools, ...(agent.external ? { external: true } : {}) })),
|
|
350
350
|
onEvent: (agent, event) => {
|
|
351
351
|
if (['tool_executing', 'tool_complete', 'tool_error', 'usage'].includes(event.type)) persist(makeEvent('agent_event', { agentId: agent.id, event }))
|
|
352
352
|
},
|
|
353
353
|
onFinish: (agent) => {
|
|
354
354
|
persist(makeEvent('agent_result', { agentId: agent.id, result: agent.result, usage: agent.usage, interrupted: agent.status === 'cancelled', error: agent.error }))
|
|
355
|
+
// an external agent's result already went to whatever ran it
|
|
356
|
+
if (agent.external) return
|
|
355
357
|
noteSystem(`Agent ${agent.id} (${agent.description}) finished with status ${agent.status}. Its result is ready. Call agent_collect with id ${agent.id} before finishing your response.`, { wake: true, agentId: agent.id, sessionId: agent.sessionId, sessionFile: agent.sessionFile })
|
|
356
358
|
},
|
|
357
359
|
// this closure outlives any single boot: /connect and project switches
|
|
@@ -718,6 +720,8 @@ export function createController({ boot, run = runTurn, compactRun = compactHist
|
|
|
718
720
|
env: boot.env,
|
|
719
721
|
contextFiles: boot.startupContext?.files ?? [],
|
|
720
722
|
engineState: savedEngineState(engine),
|
|
723
|
+
// subagents the engine runs itself, shown in the activity list
|
|
724
|
+
subagents: { start: (options) => agents.track({ ...options, sessionId: state.session?.id, sessionFile: state.session?.file }) },
|
|
721
725
|
}
|
|
722
726
|
}
|
|
723
727
|
|