picocode-core 0.9.211 → 0.9.212
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 +46 -2
- 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
|
|
@@ -154,6 +156,46 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
154
156
|
// Claude Code's messages are handled one at a time, in order
|
|
155
157
|
let chain = Promise.resolve()
|
|
156
158
|
|
|
159
|
+
// Claude's subagents, by the id of the Task call that started each,
|
|
160
|
+
// shown in pico's activity list as they work. A subagent's own traffic
|
|
161
|
+
// carries that id as its parent; it may start subagents of its own
|
|
162
|
+
const subagents = new Map()
|
|
163
|
+
const startSubagent = (block) => {
|
|
164
|
+
if (!host.subagents || !SUBAGENT_TOOLS.has(block.name)) return
|
|
165
|
+
// a subagent runs on the session's model unless its call names another
|
|
166
|
+
const handle = host.subagents.start({ description: block.input?.description, prompt: block.input?.prompt ?? '', model: `${CLAUDE_CODE_PROVIDER}/${block.input?.model ?? model}` })
|
|
167
|
+
subagents.set(block.id, { handle })
|
|
168
|
+
}
|
|
169
|
+
// `extra` is the structured result, whose report is free of the frame
|
|
170
|
+
// Claude Code wraps it in for the model
|
|
171
|
+
const finishSubagent = (block, extra) => {
|
|
172
|
+
const sub = subagents.get(block.tool_use_id)
|
|
173
|
+
if (!sub) return
|
|
174
|
+
subagents.delete(block.tool_use_id)
|
|
175
|
+
const text = Array.isArray(extra?.content) ? toolResultText(extra.content) : toolResultText(block.content)
|
|
176
|
+
sub.handle.finish(block.is_error ? { error: text || 'failed', interrupted } : { result: text })
|
|
177
|
+
}
|
|
178
|
+
const onSubagent = (message) => {
|
|
179
|
+
const sub = subagents.get(message.parent_tool_use_id)
|
|
180
|
+
if (!sub) return
|
|
181
|
+
if (message.type === 'assistant') {
|
|
182
|
+
for (const block of message.message?.content ?? []) {
|
|
183
|
+
if (block.type === 'text' && block.text) sub.handle.event({ type: 'content', content: block.text })
|
|
184
|
+
if (block.type !== 'tool_use') continue
|
|
185
|
+
const { name, args } = picoTool(block.name, block.input, host.cwd)
|
|
186
|
+
sub.handle.event({ type: 'tool_executing', call: { id: block.id, type: 'function', function: { name, arguments: JSON.stringify(args) } }, at: Date.now() })
|
|
187
|
+
startSubagent(block)
|
|
188
|
+
}
|
|
189
|
+
} else if (message.type === 'user') {
|
|
190
|
+
const blocks = (Array.isArray(message.message?.content) ? message.message.content : []).filter((block) => block.type === 'tool_result')
|
|
191
|
+
for (const block of blocks) {
|
|
192
|
+
const text = toolResultText(block.content)
|
|
193
|
+
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 })
|
|
194
|
+
finishSubagent(block, blocks.length === 1 ? message.tool_use_result : null)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
157
199
|
// offered: pico's tools that Claude Code has no version of its own. A
|
|
158
200
|
// call through the bridge lands on the tool_use Claude announced for
|
|
159
201
|
// it (same name, same arguments, oldest first), once every message
|
|
@@ -179,6 +221,7 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
179
221
|
if (bridged) unclaimed.push({ id: block.id, name, args: block.input ?? {} })
|
|
180
222
|
onStream({ type: 'tool_calls_ready', calls: [call] })
|
|
181
223
|
onStream({ type: 'tool_executing', call })
|
|
224
|
+
startSubagent(block)
|
|
182
225
|
}
|
|
183
226
|
if (!raws.has(id)) countUsage(id, message.message?.usage)
|
|
184
227
|
}
|
|
@@ -194,6 +237,7 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
194
237
|
const waiting = unclaimed.findIndex((candidate) => candidate.id === use.id)
|
|
195
238
|
if (waiting >= 0) unclaimed.splice(waiting, 1)
|
|
196
239
|
const text = toolResultText(block.content)
|
|
240
|
+
finishSubagent(block, blocks.length === 1 ? message.tool_use_result : null)
|
|
197
241
|
collected.push({ role: 'tool', tool_call_id: use.id, content: text })
|
|
198
242
|
// a bridged call the bridge never ran (Claude refused it first)
|
|
199
243
|
// has no entry yet, so it gets one like any other call
|
|
@@ -219,8 +263,7 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
219
263
|
const outcome = new Promise((resolve) => {
|
|
220
264
|
entry.onMessage = (message) => {
|
|
221
265
|
chain = chain.then(async () => {
|
|
222
|
-
|
|
223
|
-
if (message.parent_tool_use_id) return
|
|
266
|
+
if (message.parent_tool_use_id) return onSubagent(message)
|
|
224
267
|
if (message.type === 'system' && message.subtype === 'init') entry.claudeSessionId = message.session_id
|
|
225
268
|
else if (message.type === 'stream_event') {
|
|
226
269
|
const event = message.event
|
|
@@ -263,6 +306,7 @@ export function createClaudeCodeEngine({ command = claudeCommand(), idleMs = IDL
|
|
|
263
306
|
entry.onMessage = null
|
|
264
307
|
entry.onExit = null
|
|
265
308
|
flushAssistant()
|
|
309
|
+
for (const sub of subagents.values()) sub.handle.finish({ interrupted: true })
|
|
266
310
|
// a call cut off by an interrupt still needs a result, or the history
|
|
267
311
|
// is invalid for whichever model reads it next
|
|
268
312
|
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
|
|