@zvada/agent-server 0.3.22 → 0.3.23
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/CHANGELOG.md +261 -0
- package/dist/types/accounts/chatgpt.d.ts +1 -1
- package/dist/types/accounts/chatgpt.d.ts.map +1 -1
- package/dist/types/accounts/http.d.ts +2 -4
- package/dist/types/accounts/http.d.ts.map +1 -1
- package/dist/types/client/client.d.ts +37 -11
- package/dist/types/client/client.d.ts.map +1 -1
- package/dist/types/client/connection.d.ts +64 -13
- package/dist/types/client/connection.d.ts.map +1 -1
- package/dist/types/client/index.d.ts +1 -1
- package/dist/types/client/index.d.ts.map +1 -1
- package/dist/types/client/logins.d.ts +2 -1
- package/dist/types/client/logins.d.ts.map +1 -1
- package/dist/types/client/streams.d.ts +9 -3
- package/dist/types/client/streams.d.ts.map +1 -1
- package/dist/types/client/turns.d.ts +1 -1
- package/dist/types/client/turns.d.ts.map +1 -1
- package/dist/types/core/agents/acp/acp-agent.d.ts +34 -1
- package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -1
- package/dist/types/core/agents/acp/adapter.d.ts.map +1 -1
- package/dist/types/core/agents/acp/client.d.ts +12 -3
- package/dist/types/core/agents/acp/client.d.ts.map +1 -1
- package/dist/types/core/agents/acp/mappings.d.ts +8 -2
- package/dist/types/core/agents/acp/mappings.d.ts.map +1 -1
- package/dist/types/core/agents/base.d.ts +22 -8
- package/dist/types/core/agents/base.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/adapter.d.ts +7 -0
- package/dist/types/core/agents/claude-code/adapter.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/approvals.d.ts +4 -1
- package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts +16 -5
- package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/events.d.ts +9 -1
- package/dist/types/core/agents/claude-code/events.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/login.d.ts +4 -1
- package/dist/types/core/agents/claude-code/login.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/mcp.d.ts +21 -7
- package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/models.d.ts +5 -1
- package/dist/types/core/agents/claude-code/models.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/options.d.ts +10 -4
- package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/outcome.d.ts +3 -5
- package/dist/types/core/agents/claude-code/outcome.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/results.d.ts +16 -0
- package/dist/types/core/agents/claude-code/results.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/sdk-pair.d.ts +13 -0
- package/dist/types/core/agents/claude-code/sdk-pair.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/session.d.ts +50 -12
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/spend.d.ts +2 -1
- package/dist/types/core/agents/claude-code/spend.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/tools.d.ts +26 -3
- package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/adapter.d.ts +2 -2
- package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/client.d.ts +12 -2
- package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +58 -13
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/commands.d.ts +2 -2
- package/dist/types/core/agents/codex-app-server/commands.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/credentials.d.ts +5 -1
- package/dist/types/core/agents/codex-app-server/credentials.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/effort.d.ts +2 -1
- package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/homes.d.ts +19 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/items.d.ts +27 -2
- package/dist/types/core/agents/codex-app-server/items.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts +4 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/mcp.d.ts +15 -30
- package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/models.d.ts +3 -10
- package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/options.d.ts +27 -4
- package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/plan-approval.d.ts +2 -1
- package/dist/types/core/agents/codex-app-server/plan-approval.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/protocol.d.ts +16 -3
- package/dist/types/core/agents/codex-app-server/protocol.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts +68 -40
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/subagents.d.ts +9 -4
- package/dist/types/core/agents/codex-app-server/subagents.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/thread-settings.d.ts +3 -2
- package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/turn-items.d.ts +2 -1
- package/dist/types/core/agents/codex-app-server/turn-items.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/turn.d.ts +31 -8
- package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/user-login.d.ts +28 -0
- package/dist/types/core/agents/codex-app-server/user-login.d.ts.map +1 -0
- package/dist/types/core/agents/credentials.d.ts +3 -1
- package/dist/types/core/agents/credentials.d.ts.map +1 -1
- package/dist/types/core/agents/diffs.d.ts +9 -0
- package/dist/types/core/agents/diffs.d.ts.map +1 -0
- package/dist/types/core/agents/elicitation.d.ts +3 -2
- package/dist/types/core/agents/elicitation.d.ts.map +1 -1
- package/dist/types/core/agents/login.d.ts +6 -0
- package/dist/types/core/agents/login.d.ts.map +1 -1
- package/dist/types/core/agents/mcp-config.d.ts +0 -4
- package/dist/types/core/agents/mcp-config.d.ts.map +1 -1
- package/dist/types/core/agents/process-pool.d.ts +193 -0
- package/dist/types/core/agents/process-pool.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/adapter.d.ts +68 -0
- package/dist/types/core/agents/scripted/adapter.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/mcp-call.d.ts +56 -0
- package/dist/types/core/agents/scripted/mcp-call.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/scripted-agent.d.ts +73 -0
- package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/scripts.d.ts +29 -0
- package/dist/types/core/agents/scripted/scripts.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/steps.d.ts +132 -0
- package/dist/types/core/agents/scripted/steps.d.ts.map +1 -0
- package/dist/types/core/agents/session-agent.d.ts +49 -6
- package/dist/types/core/agents/session-agent.d.ts.map +1 -1
- package/dist/types/core/diagnostics.d.ts +4 -1
- package/dist/types/core/diagnostics.d.ts.map +1 -1
- package/dist/types/core/index.d.ts +5 -0
- package/dist/types/core/index.d.ts.map +1 -1
- package/dist/types/core/presets.d.ts +32 -6
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/core/processes/lifeline.d.ts +0 -20
- package/dist/types/core/processes/lifeline.d.ts.map +1 -1
- package/dist/types/core/provision/index.d.ts +2 -2
- package/dist/types/core/provision/index.d.ts.map +1 -1
- package/dist/types/core/provision/install-build.d.ts +2 -1
- package/dist/types/core/provision/install-build.d.ts.map +1 -1
- package/dist/types/core/provision/npm.d.ts +20 -8
- package/dist/types/core/provision/npm.d.ts.map +1 -1
- package/dist/types/core/provision/provisioner.d.ts +4 -1
- package/dist/types/core/provision/provisioner.d.ts.map +1 -1
- package/dist/types/core/provision/tools.d.ts +2 -1
- package/dist/types/core/provision/tools.d.ts.map +1 -1
- package/dist/types/core/runtime/agent-runtime.d.ts +16 -8
- package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
- package/dist/types/core/runtime/event-processor.d.ts +5 -1
- package/dist/types/core/runtime/event-processor.d.ts.map +1 -1
- package/dist/types/core/runtime/harness-clis.d.ts +9 -0
- package/dist/types/core/runtime/harness-clis.d.ts.map +1 -1
- package/dist/types/core/runtime/harness-logins.d.ts +33 -5
- package/dist/types/core/runtime/harness-logins.d.ts.map +1 -1
- package/dist/types/core/runtime/model-lists.d.ts +2 -0
- package/dist/types/core/runtime/model-lists.d.ts.map +1 -1
- package/dist/types/core/runtime/permission-policy.d.ts +5 -3
- package/dist/types/core/runtime/permission-policy.d.ts.map +1 -1
- package/dist/types/core/runtime/request-broker.d.ts +2 -1
- package/dist/types/core/runtime/request-broker.d.ts.map +1 -1
- package/dist/types/core/runtime/turn.d.ts +12 -1
- package/dist/types/core/runtime/turn.d.ts.map +1 -1
- package/dist/types/core/utils/deadline.d.ts +6 -0
- package/dist/types/core/utils/deadline.d.ts.map +1 -1
- package/dist/types/core/utils/errors.d.ts +7 -1
- package/dist/types/core/utils/errors.d.ts.map +1 -1
- package/dist/types/operator/operator.d.ts.map +1 -1
- package/dist/types/operator/render.d.ts +6 -4
- package/dist/types/operator/render.d.ts.map +1 -1
- package/dist/types/protocol/auth.d.ts +54 -24
- package/dist/types/protocol/auth.d.ts.map +1 -1
- package/dist/types/protocol/clis.d.ts +14 -7
- package/dist/types/protocol/clis.d.ts.map +1 -1
- package/dist/types/protocol/config.d.ts +2 -0
- package/dist/types/protocol/config.d.ts.map +1 -1
- package/dist/types/protocol/conversation.d.ts +40 -3
- package/dist/types/protocol/conversation.d.ts.map +1 -1
- package/dist/types/protocol/credentials.d.ts +7 -0
- package/dist/types/protocol/credentials.d.ts.map +1 -1
- package/dist/types/protocol/execution.d.ts +2 -1
- package/dist/types/protocol/execution.d.ts.map +1 -1
- package/dist/types/protocol/factories.d.ts +12 -2
- package/dist/types/protocol/factories.d.ts.map +1 -1
- package/dist/types/protocol/harness.d.ts +6 -2
- package/dist/types/protocol/harness.d.ts.map +1 -1
- package/dist/types/protocol/history.d.ts +19 -0
- package/dist/types/protocol/history.d.ts.map +1 -1
- package/dist/types/protocol/lifecycle.d.ts +47 -8
- package/dist/types/protocol/lifecycle.d.ts.map +1 -1
- package/dist/types/protocol/model-catalog.d.ts.map +1 -1
- package/dist/types/protocol/models.d.ts +3 -0
- package/dist/types/protocol/models.d.ts.map +1 -1
- package/dist/types/protocol/parts.d.ts +14 -0
- package/dist/types/protocol/parts.d.ts.map +1 -1
- package/dist/types/protocol/tool-state.d.ts +12 -0
- package/dist/types/protocol/tool-state.d.ts.map +1 -1
- package/dist/types/protocol/wire-api.d.ts +129 -23
- package/dist/types/protocol/wire-api.d.ts.map +1 -1
- package/dist/types/protocol/wire.d.ts +70 -10
- package/dist/types/protocol/wire.d.ts.map +1 -1
- package/dist/types/server/acp/binding.d.ts +6 -1
- package/dist/types/server/acp/binding.d.ts.map +1 -1
- package/dist/types/server/acp/translate.d.ts +33 -7
- package/dist/types/server/acp/translate.d.ts.map +1 -1
- package/dist/types/server/agent-server.d.ts +6 -0
- package/dist/types/server/agent-server.d.ts.map +1 -1
- package/dist/types/server/feedback.d.ts +2 -1
- package/dist/types/server/feedback.d.ts.map +1 -1
- package/dist/types/server/install.d.ts +11 -0
- package/dist/types/server/install.d.ts.map +1 -1
- package/dist/types/server/session-gate.d.ts +2 -1
- package/dist/types/server/session-gate.d.ts.map +1 -1
- package/dist/types/server/session-store.d.ts +1 -0
- package/dist/types/server/session-store.d.ts.map +1 -1
- package/dist/types/server/subscriptions.d.ts +2 -1
- package/dist/types/server/subscriptions.d.ts.map +1 -1
- package/dist/types/server/transports.d.ts.map +1 -1
- package/dist/types/sync/fit.d.ts +8 -7
- package/dist/types/sync/fit.d.ts.map +1 -1
- package/dist/types/sync/history-sqlite.d.ts.map +1 -1
- package/dist/types/sync/index.d.ts +1 -0
- package/dist/types/sync/index.d.ts.map +1 -1
- package/dist/types/sync/journal-sqlite.d.ts +2 -1
- package/dist/types/sync/journal-sqlite.d.ts.map +1 -1
- package/dist/types/sync/journal.d.ts +11 -3
- package/dist/types/sync/journal.d.ts.map +1 -1
- package/dist/types/sync/projection.d.ts +6 -0
- package/dist/types/sync/projection.d.ts.map +1 -1
- package/dist/types/sync/recovery.d.ts +2 -1
- package/dist/types/sync/recovery.d.ts.map +1 -1
- package/dist/types/sync/schema.d.ts +10 -0
- package/dist/types/sync/schema.d.ts.map +1 -1
- package/dist/types/sync/sql.d.ts +2 -1
- package/dist/types/sync/sql.d.ts.map +1 -1
- package/docs/consuming.md +278 -28
- package/docs/deploy.md +36 -8
- package/docs/durable-sessions.md +35 -5
- package/docs/harnesses.md +385 -24
- package/package.json +6 -1
- package/src/accounts/chatgpt.ts +2 -2
- package/src/accounts/http.ts +2 -16
- package/src/client/client.ts +92 -31
- package/src/client/connection.ts +103 -22
- package/src/client/index.ts +1 -0
- package/src/client/logins.ts +1 -1
- package/src/client/streams.ts +12 -4
- package/src/client/turns.ts +1 -1
- package/src/core/agents/acp/acp-agent.ts +181 -52
- package/src/core/agents/acp/adapter.ts +9 -1
- package/src/core/agents/acp/client.ts +16 -4
- package/src/core/agents/acp/mappings.ts +27 -0
- package/src/core/agents/base.ts +25 -10
- package/src/core/agents/claude-code/adapter.ts +47 -17
- package/src/core/agents/claude-code/approvals.ts +6 -5
- package/src/core/agents/claude-code/claude-agent.ts +60 -30
- package/src/core/agents/claude-code/events.ts +11 -1
- package/src/core/agents/claude-code/login.ts +14 -2
- package/src/core/agents/claude-code/mcp.ts +31 -15
- package/src/core/agents/claude-code/models.ts +7 -2
- package/src/core/agents/claude-code/options.ts +11 -3
- package/src/core/agents/claude-code/outcome.ts +10 -2
- package/src/core/agents/claude-code/results.ts +73 -0
- package/src/core/agents/claude-code/sdk-pair.ts +66 -0
- package/src/core/agents/claude-code/session.ts +166 -37
- package/src/core/agents/claude-code/spend.ts +1 -1
- package/src/core/agents/claude-code/tools.ts +108 -12
- package/src/core/agents/codex-app-server/adapter.ts +37 -12
- package/src/core/agents/codex-app-server/client.ts +37 -8
- package/src/core/agents/codex-app-server/codex-agent.ts +115 -50
- package/src/core/agents/codex-app-server/commands.ts +2 -2
- package/src/core/agents/codex-app-server/credentials.ts +10 -5
- package/src/core/agents/codex-app-server/effort.ts +1 -1
- package/src/core/agents/codex-app-server/homes.ts +43 -1
- package/src/core/agents/codex-app-server/items.ts +219 -31
- package/src/core/agents/codex-app-server/login.ts +14 -2
- package/src/core/agents/codex-app-server/mcp.ts +47 -32
- package/src/core/agents/codex-app-server/models.ts +9 -8
- package/src/core/agents/codex-app-server/options.ts +69 -10
- package/src/core/agents/codex-app-server/plan-approval.ts +1 -1
- package/src/core/agents/codex-app-server/protocol.ts +14 -4
- package/src/core/agents/codex-app-server/requests.ts +14 -6
- package/src/core/agents/codex-app-server/session.ts +151 -113
- package/src/core/agents/codex-app-server/subagents.ts +21 -14
- package/src/core/agents/codex-app-server/thread-settings.ts +18 -8
- package/src/core/agents/codex-app-server/turn-items.ts +1 -1
- package/src/core/agents/codex-app-server/turn.ts +79 -32
- package/src/core/agents/codex-app-server/user-login.ts +107 -0
- package/src/core/agents/credentials.ts +5 -2
- package/src/core/agents/diffs.ts +25 -0
- package/src/core/agents/elicitation.ts +2 -2
- package/src/core/agents/login.ts +6 -0
- package/src/core/agents/mcp-config.ts +2 -2
- package/src/core/agents/process-pool.ts +432 -0
- package/src/core/agents/scripted/adapter.ts +142 -0
- package/src/core/agents/scripted/mcp-call.ts +134 -0
- package/src/core/agents/scripted/scripted-agent.ts +418 -0
- package/src/core/agents/scripted/scripts.ts +171 -0
- package/src/core/agents/scripted/steps.ts +127 -0
- package/src/core/agents/session-agent.ts +105 -8
- package/src/core/diagnostics.ts +9 -1
- package/src/core/index.ts +11 -0
- package/src/core/presets.ts +99 -14
- package/src/core/processes/lifeline.ts +99 -25
- package/src/core/provision/index.ts +1 -2
- package/src/core/provision/install-build.ts +4 -1
- package/src/core/provision/npm.ts +309 -97
- package/src/core/provision/provisioner.ts +22 -8
- package/src/core/provision/tools.ts +44 -19
- package/src/core/runtime/agent-runtime.ts +48 -18
- package/src/core/runtime/event-processor.ts +18 -2
- package/src/core/runtime/harness-clis.ts +36 -1
- package/src/core/runtime/harness-logins.ts +82 -31
- package/src/core/runtime/model-lists.ts +1 -1
- package/src/core/runtime/permission-policy.ts +4 -3
- package/src/core/runtime/request-broker.ts +1 -1
- package/src/core/runtime/turn.ts +51 -3
- package/src/core/utils/deadline.ts +23 -0
- package/src/core/utils/errors.ts +11 -4
- package/src/operator/operator.ts +8 -4
- package/src/operator/render.ts +46 -8
- package/src/protocol/auth.ts +30 -23
- package/src/protocol/clis.ts +12 -4
- package/src/protocol/credentials.ts +19 -0
- package/src/protocol/execution.ts +5 -0
- package/src/protocol/factories.ts +25 -3
- package/src/protocol/harness.ts +11 -1
- package/src/protocol/history.ts +20 -0
- package/src/protocol/lifecycle.ts +10 -1
- package/src/protocol/model-catalog.ts +19 -8
- package/src/protocol/parts.ts +12 -1
- package/src/protocol/tool-state.ts +14 -1
- package/src/protocol/wire.ts +14 -0
- package/src/server/acp/binding.ts +113 -23
- package/src/server/acp/translate.ts +161 -8
- package/src/server/agent-server.ts +10 -1
- package/src/server/bin.ts +83 -9
- package/src/server/feedback.ts +1 -1
- package/src/server/install.ts +82 -4
- package/src/server/session-gate.ts +1 -1
- package/src/server/session-store.ts +5 -1
- package/src/server/subscriptions.ts +1 -1
- package/src/server/transports.ts +14 -0
- package/src/sync/fit.ts +115 -28
- package/src/sync/history-sqlite.ts +6 -2
- package/src/sync/index.ts +1 -0
- package/src/sync/journal-sqlite.ts +20 -2
- package/src/sync/journal.ts +10 -2
- package/src/sync/projection.ts +10 -0
- package/src/sync/recovery.ts +11 -4
- package/src/sync/schema.ts +7 -5
- package/src/sync/sql.ts +1 -1
- package/dist/types/core/agents/claude-code/session-pool.d.ts +0 -36
- package/dist/types/core/agents/claude-code/session-pool.d.ts.map +0 -1
- package/dist/types/core/agents/session-pool.d.ts +0 -41
- package/dist/types/core/agents/session-pool.d.ts.map +0 -1
- package/src/core/agents/claude-code/session-pool.ts +0 -136
- package/src/core/agents/session-pool.ts +0 -111
|
@@ -23,6 +23,12 @@ function parentOf(thread: NativeThread): string | undefined {
|
|
|
23
23
|
/** How many threads a managed turn may own before its cleanup gives up. */
|
|
24
24
|
const MAX_DESCENDANTS = 512;
|
|
25
25
|
|
|
26
|
+
/** How long Codex has to answer each request of a managed turn's cleanup. */
|
|
27
|
+
const CLEANUP_REQUEST_MS = 2_000;
|
|
28
|
+
|
|
29
|
+
/** How long a managed turn's whole cleanup may take before Codex's process is ended. */
|
|
30
|
+
const CLEANUP_MS = 5 * CLEANUP_REQUEST_MS;
|
|
31
|
+
|
|
26
32
|
/** Native ownership and cleanup for ONE execution; scheduling stays in Codex. */
|
|
27
33
|
export class CodexTurnSubagents {
|
|
28
34
|
private readonly parents = new Map<string, string>();
|
|
@@ -33,7 +39,6 @@ export class CodexTurnSubagents {
|
|
|
33
39
|
constructor(
|
|
34
40
|
private readonly client: CodexConnection,
|
|
35
41
|
private readonly root: string,
|
|
36
|
-
private readonly timeoutMs: number,
|
|
37
42
|
) {}
|
|
38
43
|
|
|
39
44
|
observe(notification: CodexNotification): void {
|
|
@@ -75,7 +80,7 @@ export class CodexTurnSubagents {
|
|
|
75
80
|
const result = (await this.client.request(
|
|
76
81
|
"thread/read",
|
|
77
82
|
{ threadId: id, includeTurns: true },
|
|
78
|
-
|
|
83
|
+
CLEANUP_REQUEST_MS,
|
|
79
84
|
)) as { thread?: NativeThread };
|
|
80
85
|
if (result.thread?.id !== id)
|
|
81
86
|
throw new Error("Codex did not confirm the requested child thread");
|
|
@@ -104,7 +109,7 @@ export class CodexTurnSubagents {
|
|
|
104
109
|
this.client,
|
|
105
110
|
"thread/loaded/list",
|
|
106
111
|
{ limit: 100 },
|
|
107
|
-
{ timeoutMs:
|
|
112
|
+
{ timeoutMs: CLEANUP_REQUEST_MS },
|
|
108
113
|
)) {
|
|
109
114
|
for (const id of page) this.candidates.add(id);
|
|
110
115
|
if (this.candidates.size > MAX_DESCENDANTS)
|
|
@@ -143,7 +148,7 @@ export class CodexTurnSubagents {
|
|
|
143
148
|
.request(
|
|
144
149
|
"turn/interrupt",
|
|
145
150
|
{ threadId: thread.id, turnId: active?.id ?? "" },
|
|
146
|
-
|
|
151
|
+
CLEANUP_REQUEST_MS,
|
|
147
152
|
)
|
|
148
153
|
.catch(async (error) => {
|
|
149
154
|
if ((await this.read(thread.id)).status?.type !== "idle") throw error;
|
|
@@ -153,15 +158,15 @@ export class CodexTurnSubagents {
|
|
|
153
158
|
throw new Error("Codex child interrupt was not confirmed");
|
|
154
159
|
}
|
|
155
160
|
if (thread.status.type === "notLoaded") continue;
|
|
156
|
-
if ((await listBackgroundTerminals(this.client, thread.id,
|
|
161
|
+
if ((await listBackgroundTerminals(this.client, thread.id, CLEANUP_REQUEST_MS)).length) {
|
|
157
162
|
unfinished = true;
|
|
158
163
|
stoppedWork = true;
|
|
159
164
|
await this.client.request(
|
|
160
165
|
"thread/backgroundTerminals/clean",
|
|
161
166
|
{ threadId: thread.id },
|
|
162
|
-
|
|
167
|
+
CLEANUP_REQUEST_MS,
|
|
163
168
|
);
|
|
164
|
-
if ((await listBackgroundTerminals(this.client, thread.id,
|
|
169
|
+
if ((await listBackgroundTerminals(this.client, thread.id, CLEANUP_REQUEST_MS)).length)
|
|
165
170
|
throw new Error("Codex child terminals did not stop");
|
|
166
171
|
}
|
|
167
172
|
}
|
|
@@ -196,7 +201,10 @@ export class ManagedStop implements TurnStop {
|
|
|
196
201
|
private readonly threadId: string,
|
|
197
202
|
private readonly commands: TurnCommands,
|
|
198
203
|
private readonly signal: AbortSignal,
|
|
199
|
-
/**
|
|
204
|
+
/**
|
|
205
|
+
* How long Codex has to answer the `turn/interrupt` that stops its turn
|
|
206
|
+
* before its process is ended (`cancelGraceMs`).
|
|
207
|
+
*/
|
|
200
208
|
private readonly graceMs: number,
|
|
201
209
|
/** Keep the session from another turn until its process is confirmed stopped. */
|
|
202
210
|
private readonly quarantine: () => void,
|
|
@@ -224,12 +232,11 @@ export class ManagedStop implements TurnStop {
|
|
|
224
232
|
|
|
225
233
|
private finish(native: NativeTurn | undefined): Promise<boolean> {
|
|
226
234
|
this.finishing ??= (async () => {
|
|
227
|
-
const graceMs = this.graceMs;
|
|
228
235
|
let confirmed = true;
|
|
229
236
|
let failure: unknown;
|
|
230
237
|
const deadline = setTimeout(() => {
|
|
231
|
-
void this.client.closeAndWait(
|
|
232
|
-
},
|
|
238
|
+
void this.client.closeAndWait();
|
|
239
|
+
}, CLEANUP_MS);
|
|
233
240
|
try {
|
|
234
241
|
// An error ended Codex's turn for the stream, not for Codex.
|
|
235
242
|
if (!native?.outcome || native.outcome === "error") {
|
|
@@ -238,14 +245,14 @@ export class ManagedStop implements TurnStop {
|
|
|
238
245
|
await this.client.request(
|
|
239
246
|
"turn/interrupt",
|
|
240
247
|
{ threadId: this.threadId, turnId: native.id },
|
|
241
|
-
graceMs,
|
|
248
|
+
this.graceMs,
|
|
242
249
|
);
|
|
243
250
|
}
|
|
244
251
|
const unfinished = await this.subagents.settle();
|
|
245
252
|
// Native terminal registration can land after turn/completed and
|
|
246
253
|
// after an empty terminal inventory. EOF awaits native thread/tool
|
|
247
254
|
// shutdown; an idle snapshot alone cannot release a managed turn.
|
|
248
|
-
confirmed = await this.client.closeAndWait(
|
|
255
|
+
confirmed = await this.client.closeAndWait();
|
|
249
256
|
if (!confirmed) throw new Error("Codex native cleanup was not confirmed");
|
|
250
257
|
if (unfinished && !this.signal.aborted) {
|
|
251
258
|
failure = new Error(
|
|
@@ -254,7 +261,7 @@ export class ManagedStop implements TurnStop {
|
|
|
254
261
|
}
|
|
255
262
|
} catch (error) {
|
|
256
263
|
failure = error;
|
|
257
|
-
confirmed = await this.client.closeAndWait(
|
|
264
|
+
confirmed = await this.client.closeAndWait();
|
|
258
265
|
if (!confirmed) this.quarantine();
|
|
259
266
|
} finally {
|
|
260
267
|
clearTimeout(deadline);
|
|
@@ -15,6 +15,9 @@ import {
|
|
|
15
15
|
import type { CodexThreadOpened } from "./protocol.ts";
|
|
16
16
|
import type { CodexSession } from "./session.ts";
|
|
17
17
|
|
|
18
|
+
/** How long Codex has to put a thread's settings back after a failed turn. */
|
|
19
|
+
const RESTORE_TIMEOUT_MS = 2_000;
|
|
20
|
+
|
|
18
21
|
/**
|
|
19
22
|
* A thread's settings, as the engine knows them: what Codex reported when the
|
|
20
23
|
* process took the thread, and what turns sent since. Codex keeps a turn's
|
|
@@ -42,7 +45,7 @@ export interface ThreadSettings {
|
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
/** What a turn sends Codex to run as its options ask, and what it leaves the thread on. */
|
|
45
|
-
|
|
48
|
+
interface TurnSettings {
|
|
46
49
|
/** The `turn/start` params that carry them. */
|
|
47
50
|
params: Record<string, unknown>;
|
|
48
51
|
/** The thinking level and reported model the turn runs, as far as the engine knows. */
|
|
@@ -168,11 +171,17 @@ export class CodexThread {
|
|
|
168
171
|
if (switched) params.model = model;
|
|
169
172
|
next = { ...next, ...(switched && { model, reportedModel: undefined }), modelSet: false };
|
|
170
173
|
}
|
|
174
|
+
// A model known to have no reasoning control is sent no effort, and the
|
|
175
|
+
// turn reports no level: the thread's effort does not apply to it.
|
|
176
|
+
let noThinking = false;
|
|
171
177
|
if (options.thinkingLevel) {
|
|
172
178
|
const accepts = await modelThinking(session, next.model, known);
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
179
|
+
if (accepts && !accepts.levels.length) noThinking = true;
|
|
180
|
+
else {
|
|
181
|
+
const effort = codexReasoningEffort(options.thinkingLevel, accepts?.levels, accepts?.off);
|
|
182
|
+
params.effort = effort;
|
|
183
|
+
next = { ...next, effort, effortSet: true };
|
|
184
|
+
}
|
|
176
185
|
} else {
|
|
177
186
|
// Once a turn set the thread's effort, the default is worked out for
|
|
178
187
|
// this turn's model. Otherwise, and when Codex cannot say what the
|
|
@@ -190,14 +199,16 @@ export class CodexThread {
|
|
|
190
199
|
}
|
|
191
200
|
// Codex runs the effort it is given, and overrides persist on the
|
|
192
201
|
// thread, so the thread's effort is the one this turn runs.
|
|
193
|
-
const level = codexThinkingLevel(next.effort);
|
|
202
|
+
const level = noThinking ? undefined : codexThinkingLevel(next.effort);
|
|
194
203
|
const collaboration = inCollaborationMode(next, mode);
|
|
195
204
|
return {
|
|
196
205
|
params: { ...params, ...collaboration.params },
|
|
197
|
-
...((level || next.reportedModel) && {
|
|
206
|
+
...((level || next.reportedModel || noThinking) && {
|
|
198
207
|
applied: {
|
|
199
208
|
...(level && { thinkingLevel: level }),
|
|
200
209
|
...(next.reportedModel && { model: next.reportedModel }),
|
|
210
|
+
// The turn's level was not sent: its model has no reasoning control.
|
|
211
|
+
...(noThinking && { notApplied: ["thinkingLevel" as const] }),
|
|
201
212
|
},
|
|
202
213
|
}),
|
|
203
214
|
next: collaboration.next,
|
|
@@ -215,7 +226,6 @@ export class CodexThread {
|
|
|
215
226
|
session: CodexSession,
|
|
216
227
|
previous: ThreadSettings,
|
|
217
228
|
sent: Record<string, unknown>,
|
|
218
|
-
timeoutMs: number,
|
|
219
229
|
): Promise<void> {
|
|
220
230
|
const params: Record<string, unknown> = { threadId: session.threadId };
|
|
221
231
|
if (sent.model !== undefined && previous.model && sent.model !== previous.model)
|
|
@@ -225,7 +235,7 @@ export class CodexThread {
|
|
|
225
235
|
params.effort = previous.effort;
|
|
226
236
|
if (params.model === undefined && params.effort === undefined) return;
|
|
227
237
|
try {
|
|
228
|
-
await session.client.request("thread/settings/update", params,
|
|
238
|
+
await session.client.request("thread/settings/update", params, RESTORE_TIMEOUT_MS);
|
|
229
239
|
} catch {
|
|
230
240
|
return;
|
|
231
241
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type CodexFileChange, type CodexNotification, codexItem } from "./protocol.ts";
|
|
2
2
|
|
|
3
3
|
/** An MCP tool call Codex started and may ask consent for. */
|
|
4
|
-
|
|
4
|
+
interface CodexMcpCall {
|
|
5
5
|
id: string;
|
|
6
6
|
threadId?: string;
|
|
7
7
|
server: string;
|
|
@@ -7,22 +7,16 @@ import type { TurnCommands } from "./commands.ts";
|
|
|
7
7
|
import { CODEX_STOP_UNCONFIRMED, type CodexRawEvent } from "./events.ts";
|
|
8
8
|
import { type UserInput, toUserInput } from "./input.ts";
|
|
9
9
|
import { askPlan } from "./plan-approval.ts";
|
|
10
|
-
import {
|
|
11
|
-
type CodexNotification,
|
|
12
|
-
codexItem,
|
|
13
|
-
codexTurn,
|
|
14
|
-
endsNativeTurn,
|
|
15
|
-
retrying,
|
|
16
|
-
} from "./protocol.ts";
|
|
10
|
+
import { type CodexNotification, codexItem, codexTurn, failing, retrying } from "./protocol.ts";
|
|
17
11
|
import type { CodexTurnScope } from "./requests.ts";
|
|
18
12
|
import type { CodexSession } from "./session.ts";
|
|
19
13
|
import type { ThreadSettings } from "./thread-settings.ts";
|
|
20
14
|
|
|
21
15
|
/**
|
|
22
16
|
* How one of Codex's turns ended: its `turn/completed` status, or `error`
|
|
23
|
-
* when an error Codex would not retry
|
|
17
|
+
* when an error Codex would not retry had no `turn/completed` after it.
|
|
24
18
|
*/
|
|
25
|
-
|
|
19
|
+
type NativeOutcome = "completed" | "interrupted" | "failed" | "error";
|
|
26
20
|
|
|
27
21
|
/** One of Codex's turns, `turn/start` to its end, within an engine turn. */
|
|
28
22
|
export class NativeTurn {
|
|
@@ -34,6 +28,8 @@ export class NativeTurn {
|
|
|
34
28
|
outcome?: NativeOutcome;
|
|
35
29
|
/** The plan it proposed, when it planned. */
|
|
36
30
|
plan?: { itemId: string; text: string };
|
|
31
|
+
/** The error Codex would not retry, held for the stream until Codex's turn ends. */
|
|
32
|
+
failure?: CodexNotification;
|
|
37
33
|
/** Its events took their last: at its end, or once a managed turn's cleanup is done. */
|
|
38
34
|
finished = false;
|
|
39
35
|
private settle!: () => void;
|
|
@@ -82,8 +78,9 @@ export interface TurnStop {
|
|
|
82
78
|
/** Codex's turn ended with `n`: its events end, now or once the stop cleaned up. */
|
|
83
79
|
nativeEnded(native: NativeTurn, n: CodexNotification): void;
|
|
84
80
|
/**
|
|
85
|
-
* Stop for a cancel: Codex's turn, once it began
|
|
86
|
-
*
|
|
81
|
+
* Stop for a cancel: Codex's turn, once it began, else the process it
|
|
82
|
+
* would run on. Resolves whether the stop is confirmed
|
|
83
|
+
* (`CancelResult.confirmed`).
|
|
87
84
|
*/
|
|
88
85
|
cancel(native?: NativeTurn): Promise<boolean>;
|
|
89
86
|
/** The commands whose outcome a turn that ended cancelled leaves unknown. */
|
|
@@ -120,6 +117,10 @@ export class InterruptStop implements TurnStop {
|
|
|
120
117
|
// or by `requestTimeoutMs`): the turn ends cancelled, so what it started
|
|
121
118
|
// is stopped, as after the engine's own cancel.
|
|
122
119
|
if (native.outcome === "interrupted") this.commands.interrupted();
|
|
120
|
+
// Codex failed its turn and never said it ended: the session's next
|
|
121
|
+
// turn/start would steer it and take its turn/completed, so its process
|
|
122
|
+
// is done with.
|
|
123
|
+
if (native.outcome === "error") this.session.client.close();
|
|
123
124
|
}
|
|
124
125
|
|
|
125
126
|
cancel(native?: NativeTurn): Promise<boolean> {
|
|
@@ -128,7 +129,13 @@ export class InterruptStop implements TurnStop {
|
|
|
128
129
|
// on purpose, as after any turn's end. Nothing is stopped.
|
|
129
130
|
if (native?.outcome) return Promise.resolve(true);
|
|
130
131
|
this.commands.interrupted();
|
|
131
|
-
|
|
132
|
+
// No turn was started: ending its process stops all there is.
|
|
133
|
+
if (!native) {
|
|
134
|
+
this.force(undefined);
|
|
135
|
+
return Promise.resolve(true);
|
|
136
|
+
}
|
|
137
|
+
// Started, and not yet named: Codex may be running it.
|
|
138
|
+
if (!native.id) {
|
|
132
139
|
this.force(native);
|
|
133
140
|
return Promise.resolve(false);
|
|
134
141
|
}
|
|
@@ -174,7 +181,7 @@ export class InterruptStop implements TurnStop {
|
|
|
174
181
|
}
|
|
175
182
|
|
|
176
183
|
/** Lets Codex's turn take steers: the agent's `openSteering` and `holdSteering`. */
|
|
177
|
-
|
|
184
|
+
interface TurnSteering {
|
|
178
185
|
/** Codex's turn takes input now: the steers that wait go to it, then each as it comes. */
|
|
179
186
|
open(deliver: DeliverSteer): void;
|
|
180
187
|
/** Codex's turn takes no more: steers wait for its next. */
|
|
@@ -225,7 +232,9 @@ async function steerTurn(
|
|
|
225
232
|
|
|
226
233
|
/** How long an engine turn on Codex waits for it. */
|
|
227
234
|
export interface CodexTurnTiming {
|
|
228
|
-
/** `
|
|
235
|
+
/** `cancelConfirmMs`: how long Codex has to say its turn ended, a cancelled or a failed one. */
|
|
236
|
+
confirmMs: number;
|
|
237
|
+
/** `cancelGraceMs`: how long a cancelled turn has to stop before Codex's process is ended. */
|
|
229
238
|
graceMs: number;
|
|
230
239
|
/** How long a retryable error may go unanswered before the turn is failed. */
|
|
231
240
|
retryStallMs: number;
|
|
@@ -246,6 +255,11 @@ export class CodexTurn {
|
|
|
246
255
|
private native = new NativeTurn();
|
|
247
256
|
/** Codex's turns in this one: a plan's, then the one carrying out its answer. */
|
|
248
257
|
private readonly turnIds = new Set<string>();
|
|
258
|
+
/**
|
|
259
|
+
* The one deadline the turn waits on: while Codex retries, how long it may
|
|
260
|
+
* go silent (`watchStall`); once its turn is failing, how long its
|
|
261
|
+
* turn/completed may take (`failed`). A cancel and the turn's end clear it.
|
|
262
|
+
*/
|
|
249
263
|
private stallTimer?: ReturnType<typeof setTimeout>;
|
|
250
264
|
private acknowledged?: Promise<boolean>;
|
|
251
265
|
private readonly unsubscribe: Array<() => void>;
|
|
@@ -274,10 +288,7 @@ export class CodexTurn {
|
|
|
274
288
|
: (error ?? new Error("codex app-server exited mid-turn")),
|
|
275
289
|
);
|
|
276
290
|
}),
|
|
277
|
-
onAbort(this.signal, () =>
|
|
278
|
-
this.clearStall();
|
|
279
|
-
this.acknowledged ??= this.stop.cancel(this.native);
|
|
280
|
-
}),
|
|
291
|
+
onAbort(this.signal, () => void this.cancel()),
|
|
281
292
|
];
|
|
282
293
|
}
|
|
283
294
|
|
|
@@ -285,8 +296,10 @@ export class CodexTurn {
|
|
|
285
296
|
return this.scope.signal;
|
|
286
297
|
}
|
|
287
298
|
|
|
288
|
-
/**
|
|
289
|
-
|
|
299
|
+
/** Stop Codex's turn for the cancel, once: whether it stopped. */
|
|
300
|
+
cancel(): Promise<boolean> {
|
|
301
|
+
this.clearStall();
|
|
302
|
+
this.acknowledged ??= this.stop.cancel(this.native);
|
|
290
303
|
return this.acknowledged;
|
|
291
304
|
}
|
|
292
305
|
|
|
@@ -320,7 +333,7 @@ export class CodexTurn {
|
|
|
320
333
|
!this.signal.aborted &&
|
|
321
334
|
(native.outcome === "failed" || native.outcome === "error")
|
|
322
335
|
)
|
|
323
|
-
await thread.restore(this.session, previous, params
|
|
336
|
+
await thread.restore(this.session, previous, params);
|
|
324
337
|
let next = yield* askPlan(this, native);
|
|
325
338
|
while (next) {
|
|
326
339
|
native = this.start(new NativeTurn(), next.params, next.settings, next.sandbox);
|
|
@@ -367,7 +380,7 @@ export class CodexTurn {
|
|
|
367
380
|
): NativeTurn {
|
|
368
381
|
const { session } = this;
|
|
369
382
|
if (this.signal.aborted) throw new Error("turn aborted");
|
|
370
|
-
if (session.
|
|
383
|
+
if (session.startedBeforeResume()) {
|
|
371
384
|
session.client.close();
|
|
372
385
|
throw new Error("Codex process was invalidated before turn admission");
|
|
373
386
|
}
|
|
@@ -375,21 +388,27 @@ export class CodexTurn {
|
|
|
375
388
|
this.native = native;
|
|
376
389
|
session.thread.settings = settings;
|
|
377
390
|
session.sandbox = sandbox;
|
|
378
|
-
session.
|
|
391
|
+
const environments = session.environments && { environments: session.environments };
|
|
392
|
+
session.client.request("turn/start", { ...params, ...environments }).then(
|
|
379
393
|
() => {
|
|
380
394
|
this.accepted = true;
|
|
381
395
|
},
|
|
382
396
|
(error: unknown) => {
|
|
383
|
-
// Codex refused it: the thread is as it was.
|
|
384
|
-
|
|
385
|
-
if (
|
|
397
|
+
// Codex refused it: the thread is as it was.
|
|
398
|
+
const refused = error instanceof CodexRpcError;
|
|
399
|
+
if (refused) {
|
|
386
400
|
session.thread.settings = previous.settings;
|
|
387
401
|
session.sandbox = previous.sandbox;
|
|
388
|
-
}
|
|
389
|
-
// A cancelled turn ends by its cancel.
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
402
|
+
}
|
|
403
|
+
// A cancelled turn ends by its cancel. Any other ends with this error,
|
|
404
|
+
// before the close below could end it as "exited mid-turn".
|
|
405
|
+
if (!this.signal.aborted) {
|
|
406
|
+
native.queue.push({ method: "error", params: { message: String(error) } });
|
|
407
|
+
native.finish();
|
|
408
|
+
}
|
|
409
|
+
// Anything but a refusal (no answer in time) leaves the thread's
|
|
410
|
+
// settings unknown, so this process is done with.
|
|
411
|
+
if (!refused) session.client.close();
|
|
393
412
|
},
|
|
394
413
|
);
|
|
395
414
|
return native;
|
|
@@ -430,11 +449,15 @@ export class CodexTurn {
|
|
|
430
449
|
break;
|
|
431
450
|
}
|
|
432
451
|
}
|
|
433
|
-
if (
|
|
452
|
+
if (n.method === "turn/completed") {
|
|
434
453
|
this.ended(native, n);
|
|
435
454
|
return;
|
|
436
455
|
}
|
|
437
456
|
if (native.finished) return;
|
|
457
|
+
if (failing(n)) {
|
|
458
|
+
this.failed(native, n);
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
438
461
|
native.queue.push(n);
|
|
439
462
|
this.watchStall(n);
|
|
440
463
|
}
|
|
@@ -447,7 +470,28 @@ export class CodexTurn {
|
|
|
447
470
|
this.steering.open((request) => steerTurn(client, threadId, id, request));
|
|
448
471
|
}
|
|
449
472
|
|
|
473
|
+
/**
|
|
474
|
+
* Codex's turn failed. It ends at its own turn/completed, which Codex sends
|
|
475
|
+
* after: ended here, Codex's turn would still be running, and the session's
|
|
476
|
+
* next turn/start would steer it and end on its turn/completed. Without one
|
|
477
|
+
* within `confirmMs`, it ends with the error, and its process with it. The
|
|
478
|
+
* wait runs from the first failure: a later one changes nothing, or a run
|
|
479
|
+
* of them would keep the turn open. After a cancel, its interrupt's grace
|
|
480
|
+
* governs.
|
|
481
|
+
*/
|
|
482
|
+
// @ref LLP 0013#f11-a-failed-codex-turn-ended-the-next-one — no native turn outlives its engine turn
|
|
483
|
+
private failed(native: NativeTurn, n: CodexNotification): void {
|
|
484
|
+
if (native.failure) return;
|
|
485
|
+
native.failure = n;
|
|
486
|
+
if (this.signal.aborted) return;
|
|
487
|
+
this.clearStall();
|
|
488
|
+
this.stallTimer = setTimeout(() => this.ended(native, n), this.timing.confirmMs);
|
|
489
|
+
this.stallTimer.unref?.();
|
|
490
|
+
}
|
|
491
|
+
|
|
450
492
|
private ended(native: NativeTurn, n: CodexNotification): void {
|
|
493
|
+
// The stream hears why Codex's turn failed, then that it ended.
|
|
494
|
+
if (native.failure && native.failure !== n) native.queue.push(native.failure);
|
|
451
495
|
native.end(n);
|
|
452
496
|
this.clearStall();
|
|
453
497
|
// A plan turn's end may only pause this one: steers wait for the turn
|
|
@@ -459,8 +503,11 @@ export class CodexTurn {
|
|
|
459
503
|
/**
|
|
460
504
|
* A retryable error promises more notifications. If none arrive, the
|
|
461
505
|
* subprocess is wedged-but-alive and the turn would hang forever: bound it.
|
|
506
|
+
* A failing turn's deadline is not this one's to move: what Codex sends
|
|
507
|
+
* after its error leaves it as it is.
|
|
462
508
|
*/
|
|
463
509
|
private watchStall(n: CodexNotification): void {
|
|
510
|
+
if (this.native.failure) return;
|
|
464
511
|
this.clearStall();
|
|
465
512
|
if (this.signal.aborted || !retrying(n)) return;
|
|
466
513
|
this.stallTimer = setTimeout(() => {
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { claimsOf } from "../../../protocol/index.ts";
|
|
2
|
+
import { onAbort } from "../../utils/deadline.ts";
|
|
3
|
+
import { AgentServerError } from "../../utils/errors.ts";
|
|
4
|
+
import { isRecord } from "../../utils/is-record.ts";
|
|
5
|
+
import type { CodexConnection } from "./client.ts";
|
|
6
|
+
import type { CodexAuth } from "./credentials.ts";
|
|
7
|
+
|
|
8
|
+
/** What Codex's `getAuthStatus` answers: the login of the home its app-server runs in. */
|
|
9
|
+
export interface CodexAuthStatus {
|
|
10
|
+
/** `chatgpt`, `apikey` (lowercase), another kind, or none when signed out. */
|
|
11
|
+
authMethod?: string | null;
|
|
12
|
+
/** The login's access token, or its key; asked for only to hand it to a session's process. */
|
|
13
|
+
authToken?: string | null;
|
|
14
|
+
requiresOpenaiAuth?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Read the user's own login, renewed by Codex in its own home first when `refreshToken`. */
|
|
18
|
+
export type ReadUserLogin = (
|
|
19
|
+
refreshToken: boolean,
|
|
20
|
+
signal?: AbortSignal,
|
|
21
|
+
) => Promise<CodexAuthStatus>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* One read of the user's own login: a new app-server on the engine's own
|
|
25
|
+
* Codex home (`connect` gives it the engine's environment, never a session's
|
|
26
|
+
* `CODEX_HOME`), asked once and ended at once. Nothing is kept between reads.
|
|
27
|
+
*/
|
|
28
|
+
export function userLoginReader(connect: () => Promise<CodexConnection>): ReadUserLogin {
|
|
29
|
+
return async (refreshToken, signal) => {
|
|
30
|
+
signal?.throwIfAborted();
|
|
31
|
+
const client = await connect();
|
|
32
|
+
const offAbort = onAbort(signal, () => client.close());
|
|
33
|
+
try {
|
|
34
|
+
signal?.throwIfAborted();
|
|
35
|
+
await client.initialize();
|
|
36
|
+
const answer = await client.request("getAuthStatus", { includeToken: true, refreshToken });
|
|
37
|
+
return isRecord(answer) ? (answer as CodexAuthStatus) : {};
|
|
38
|
+
} finally {
|
|
39
|
+
offAbort();
|
|
40
|
+
client.close();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The user has no login Codex can hand to a session's process: they sign in, and the turn is sent again. */
|
|
46
|
+
const notSignedIn = (why: string) =>
|
|
47
|
+
new AgentServerError(
|
|
48
|
+
`${why} Sign in to Codex on this computer (\`codex login\`, or auth/login with no env) and try again.`,
|
|
49
|
+
"credentialsReconnect",
|
|
50
|
+
"auth",
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
/** ChatGPT tokens of an access token: its account and plan, from its own claims. */
|
|
54
|
+
function chatGptTokens(accessToken: string) {
|
|
55
|
+
const auth = claimsOf(accessToken)["https://api.openai.com/auth"];
|
|
56
|
+
const identity = isRecord(auth) ? auth : {};
|
|
57
|
+
const accountId = identity.chatgpt_account_id;
|
|
58
|
+
if (typeof accountId !== "string" || !accountId)
|
|
59
|
+
throw notSignedIn("Codex's ChatGPT login names no account.");
|
|
60
|
+
const plan = identity.chatgpt_plan_type;
|
|
61
|
+
return {
|
|
62
|
+
accessToken,
|
|
63
|
+
chatgptAccountId: accountId,
|
|
64
|
+
...(typeof plan === "string" && plan && { chatgptPlanType: plan }),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const chatGptToken = (status: CodexAuthStatus) =>
|
|
69
|
+
status.authMethod === "chatgpt" && typeof status.authToken === "string" && status.authToken
|
|
70
|
+
? status.authToken
|
|
71
|
+
: undefined;
|
|
72
|
+
|
|
73
|
+
// @ref LLP 0017#the-persons-own-login-in-a-home-of-the-sessions-own — read from Codex, held in memory
|
|
74
|
+
/**
|
|
75
|
+
* The user's own Codex login as a session's process logs in with it: a
|
|
76
|
+
* ChatGPT login as its tokens, held in that process's memory, or an API key.
|
|
77
|
+
* When Codex refuses the token it holds, a fresher one: the login read again,
|
|
78
|
+
* and when that is still the refused token, renewed by Codex in the user's
|
|
79
|
+
* own home under its own lock. No login, or one that cannot be handed over,
|
|
80
|
+
* fails the turn before any process of the session starts.
|
|
81
|
+
*/
|
|
82
|
+
export async function userAuth(read: ReadUserLogin, signal?: AbortSignal): Promise<CodexAuth> {
|
|
83
|
+
const status = await read(false, signal);
|
|
84
|
+
if (status.authMethod === "apikey" && typeof status.authToken === "string" && status.authToken)
|
|
85
|
+
return { type: "apiKey", apiKey: status.authToken };
|
|
86
|
+
const token = chatGptToken(status);
|
|
87
|
+
if (!token) {
|
|
88
|
+
throw notSignedIn(
|
|
89
|
+
status.authMethod
|
|
90
|
+
? `Codex's own login (${status.authMethod}) cannot run a session in a home of its own.`
|
|
91
|
+
: "Codex is not signed in.",
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
let current = chatGptTokens(token);
|
|
95
|
+
return {
|
|
96
|
+
type: "chatgptAuthTokens",
|
|
97
|
+
tokens: current,
|
|
98
|
+
refresh: async () => {
|
|
99
|
+
const again = chatGptToken(await read(false));
|
|
100
|
+
const fresher =
|
|
101
|
+
again && again !== current.accessToken ? again : chatGptToken(await read(true));
|
|
102
|
+
if (!fresher) throw notSignedIn("Codex's ChatGPT login ended.");
|
|
103
|
+
current = chatGptTokens(fresher);
|
|
104
|
+
return current;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
type HarnessCredentials,
|
|
6
6
|
credentialRunsOn,
|
|
7
7
|
} from "../../protocol/index.ts";
|
|
8
|
+
import { untilAborted } from "../utils/deadline.ts";
|
|
8
9
|
import { AgentServerError } from "../utils/errors.ts";
|
|
9
10
|
import { isLoopbackHost } from "../utils/loopback.ts";
|
|
10
11
|
|
|
@@ -22,14 +23,16 @@ export type CredentialSource = (
|
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* The product's credential for a harness, as the kind it runs on: one it
|
|
25
|
-
* cannot run fails the turn here, before anything starts.
|
|
26
|
+
* cannot run fails the turn here, before anything starts. A turn cancelled
|
|
27
|
+
* meanwhile stops waiting at once, whether or not the source heeds `signal`,
|
|
28
|
+
* and an answer that comes after is dropped.
|
|
26
29
|
*/
|
|
27
30
|
export async function credentialFor<H extends keyof HarnessCredentials>(
|
|
28
31
|
source: CredentialSource,
|
|
29
32
|
request: CredentialRequest & { harness: H },
|
|
30
33
|
signal?: AbortSignal,
|
|
31
34
|
): Promise<HarnessCredentials[H] | undefined> {
|
|
32
|
-
const credential = await source(request, signal);
|
|
35
|
+
const credential = await untilAborted(source(request, signal), signal);
|
|
33
36
|
if (!credential) return undefined;
|
|
34
37
|
const runs = credentialRunsOn(request.harness, credential);
|
|
35
38
|
if (runs.ok) return runs.credential;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ToolResultContent } from "../../protocol/index.ts";
|
|
2
|
+
|
|
3
|
+
// @ref LLP 0002#tool-output-has-three-audiences — a diff entry per hunk, as `claude-agent-acp` builds them
|
|
4
|
+
/**
|
|
5
|
+
* One hunk of a unified diff as a `diff` entry: `oldText` is its context and
|
|
6
|
+
* removed lines, `newText` its context and added lines, each joined by a
|
|
7
|
+
* newline. A `` marker is no line of either file,
|
|
8
|
+
* so it is dropped. Undefined for a hunk with no lines.
|
|
9
|
+
*/
|
|
10
|
+
export function hunkDiff(path: string, lines: readonly string[]): ToolResultContent | undefined {
|
|
11
|
+
const oldLines: string[] = [];
|
|
12
|
+
const newLines: string[] = [];
|
|
13
|
+
for (const line of lines) {
|
|
14
|
+
if (line.startsWith("\\")) continue;
|
|
15
|
+
const text = line.slice(1);
|
|
16
|
+
if (line.startsWith("-")) oldLines.push(text);
|
|
17
|
+
else if (line.startsWith("+")) newLines.push(text);
|
|
18
|
+
else {
|
|
19
|
+
oldLines.push(text);
|
|
20
|
+
newLines.push(text);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (!oldLines.length && !newLines.length) return undefined;
|
|
24
|
+
return { type: "diff", path, oldText: oldLines.join("\n"), newText: newLines.join("\n") };
|
|
25
|
+
}
|
|
@@ -29,7 +29,7 @@ export function unanswered(action: "decline" | "cancel"): string {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/** An MCP server's request for input: a form to fill in, or a URL to visit. */
|
|
32
|
-
|
|
32
|
+
interface McpInputRequest {
|
|
33
33
|
mode: "form" | "url";
|
|
34
34
|
message: string;
|
|
35
35
|
/** The server asking, when the harness names it. */
|
|
@@ -58,7 +58,7 @@ export function inputRequest(request: McpInputRequest): ElicitationRequest {
|
|
|
58
58
|
* The user's answer as an MCP server takes it (MCP's `ElicitResult`). A type,
|
|
59
59
|
* not an interface: the SDKs' reply types are indexable.
|
|
60
60
|
*/
|
|
61
|
-
|
|
61
|
+
type McpInputReply = {
|
|
62
62
|
action: "accept" | "decline" | "cancel";
|
|
63
63
|
/** What the user filled in; only an accept carries it. */
|
|
64
64
|
content?: Record<string, string | number | boolean | string[]>;
|
package/src/core/agents/login.ts
CHANGED
|
@@ -45,6 +45,12 @@ export interface CliLogin {
|
|
|
45
45
|
/** Start one of its `methods` (not a `terminal` one); `apiKey` for an `api_key` method. */
|
|
46
46
|
start(method: string, options: LoginOptions & { apiKey?: string }): Promise<NativeLogin>;
|
|
47
47
|
logout(options: LoginOptions): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* The store a turn or a login with this `env` reads its login from: the
|
|
50
|
+
* value of the CLI's store variable, empty when unset. A login changed
|
|
51
|
+
* there restarts that store's sessions alone. Absent: one store.
|
|
52
|
+
*/
|
|
53
|
+
store?(env: Record<string, string> | undefined): string;
|
|
48
54
|
}
|
|
49
55
|
|
|
50
56
|
/** The method a login names, refused unless the harness offers it as a login. */
|
|
@@ -10,7 +10,7 @@ export function snapshotMcpServers(
|
|
|
10
10
|
|
|
11
11
|
// @ref LLP 0001#d15--a-turns-mcp-servers-are-required-unless-it-says-otherwise — the caller's check stays
|
|
12
12
|
/** Whether a turn needs the server: each does unless it says `required: false`. */
|
|
13
|
-
|
|
13
|
+
function mcpServerRequired(server: McpServerConfig | undefined): boolean {
|
|
14
14
|
return server?.required !== false;
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -71,7 +71,7 @@ export function mcpCause(cause: string): string {
|
|
|
71
71
|
|
|
72
72
|
// @ref LLP 0001#d15--a-turns-mcp-servers-are-required-unless-it-says-otherwise — a recoverable error
|
|
73
73
|
/** What a turn says of a server it goes on without. */
|
|
74
|
-
|
|
74
|
+
function mcpUnavailableWarning(name: string, cause: string): ErrorInfo {
|
|
75
75
|
return {
|
|
76
76
|
category: "mcp_unavailable",
|
|
77
77
|
message: `MCP server ${name} could not connect (${cause}); the turn goes on without its tools`,
|