@moretea-labs/forge 1.7.1 → 1.8.0
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/AGENTS.md +58 -62
- package/CHANGELOG.md +39 -0
- package/CLAUDE.md +42 -62
- package/README.md +2 -2
- package/SKILL.md +0 -1
- package/THIRD_PARTY_NOTICES.md +5 -2
- package/adapters/browser/index.ts +2 -0
- package/adapters/chatgpt/browser-delivery-host.ts +71 -0
- package/adapters/chatgpt/browser-delivery-runtime.ts +783 -0
- package/adapters/chatgpt/controller-binding-store.ts +55 -0
- package/adapters/chatgpt/controller-host.ts +119 -0
- package/adapters/chatgpt/controller-round-host.ts +78 -0
- package/adapters/chatgpt/controller-round-settlement-store.ts +45 -0
- package/adapters/chatgpt/provider-delivery.ts +113 -0
- package/adapters/chatgpt/work-conversation-binding-store.ts +192 -0
- package/adapters/chatgpt/wsl-bridge-delivery-host.ts +63 -0
- package/adapters/computer/desktop-operator-contract.ts +7 -0
- package/adapters/computer/desktop-operator-discovery.ts +201 -0
- package/adapters/computer/desktop-operator-negotiation.ts +174 -0
- package/adapters/computer/desktop-operator-provider.ts +312 -0
- package/adapters/computer/index.ts +1 -0
- package/adapters/computer/interaction-target-authority.ts +944 -0
- package/adapters/controller-process/binding-store.ts +34 -0
- package/adapters/controller-process/controller-host.ts +26 -0
- package/adapters/mcp/access-mode.ts +132 -0
- package/adapters/mcp/audit.ts +32 -0
- package/adapters/mcp/auth.ts +407 -0
- package/adapters/mcp/controller-round-compatibility.ts +184 -0
- package/adapters/mcp/frozen-client-semantic-compatibility.ts +292 -0
- package/adapters/mcp/instructions.ts +36 -0
- package/adapters/mcp/legacy-context.ts +18 -0
- package/adapters/mcp/multi-repository.ts +437 -0
- package/adapters/mcp/oauth.ts +391 -0
- package/adapters/mcp/paths.ts +140 -0
- package/adapters/mcp/policy.ts +232 -0
- package/adapters/mcp/proxy-env.ts +62 -0
- package/adapters/mcp/readiness-projection.ts +24 -0
- package/adapters/mcp/redaction.ts +21 -0
- package/adapters/mcp/repo.ts +24 -0
- package/adapters/mcp/runtime-gateway/artifact-adapter.ts +52 -0
- package/adapters/mcp/runtime-gateway/context-adapter.ts +829 -0
- package/adapters/mcp/runtime-gateway/controller-authority-adapter.ts +191 -0
- package/adapters/mcp/runtime-gateway/engineering-preconditions.ts +147 -0
- package/adapters/mcp/runtime-gateway/execution-control-adapter.ts +28 -0
- package/adapters/mcp/runtime-gateway/execution-tools.ts +207 -0
- package/adapters/mcp/runtime-gateway/filesystem-adapter.ts +38 -0
- package/adapters/mcp/runtime-gateway/gateway-execution-adapter.ts +411 -0
- package/adapters/mcp/runtime-gateway/legacy-ios-tool-adapter.ts +105 -0
- package/adapters/mcp/runtime-gateway/maintenance-adapter.ts +44 -0
- package/adapters/mcp/runtime-gateway/model-adapter.ts +63 -0
- package/adapters/mcp/runtime-gateway/persisted-check-process.ts +3 -0
- package/adapters/mcp/runtime-gateway/plugin-adapter.ts +308 -0
- package/adapters/mcp/runtime-gateway/process-tools.ts +289 -0
- package/adapters/mcp/runtime-gateway/protected-computer-adapter.ts +76 -0
- package/adapters/mcp/runtime-gateway/recovery-adapter.ts +268 -0
- package/adapters/mcp/runtime-gateway/recovery-client-adapter.ts +70 -0
- package/adapters/mcp/runtime-gateway/recovery-tool-contract.ts +99 -0
- package/adapters/mcp/runtime-gateway/repository-compat-adapter.ts +110 -0
- package/adapters/mcp/runtime-gateway/result-adapter.ts +108 -0
- package/adapters/mcp/runtime-gateway/router.ts +728 -0
- package/adapters/mcp/runtime-gateway/runtime-observation-adapter.ts +1251 -0
- package/adapters/mcp/runtime-gateway/runtime-tool-definitions.ts +634 -0
- package/adapters/mcp/runtime-gateway/runtime-tool-shared.ts +27 -0
- package/adapters/mcp/runtime-gateway/runtime-tools.ts +68 -0
- package/adapters/mcp/runtime-gateway/scheduler-adapter.ts +186 -0
- package/adapters/mcp/runtime-gateway/shared-adapter.ts +25 -0
- package/adapters/mcp/runtime-gateway/status-inbox-adapter.ts +870 -0
- package/adapters/mcp/runtime-gateway/work-adapter.ts +3186 -0
- package/adapters/mcp/runtime-gateway/work-compat-adapter.ts +502 -0
- package/adapters/mcp/runtime-gateway/work-validation-reconciler.ts +1 -0
- package/adapters/mcp/server.ts +1048 -0
- package/adapters/mcp/setup.ts +791 -0
- package/adapters/mcp/tool-capability-cache.ts +87 -0
- package/adapters/mcp/tool-mapping/access-tools.ts +387 -0
- package/adapters/mcp/tool-mapping/legacy-tool-service.ts +5265 -0
- package/adapters/mcp/tool-mapping/post-finalize-work-attribution.ts +150 -0
- package/adapters/mcp/tool-mapping/repository-tools.ts +1691 -0
- package/adapters/mcp/tool-mapping/tools.ts +9 -0
- package/adapters/mcp/tool-mapping/umbrella-tools.ts +117 -0
- package/adapters/mcp/toolset-names.ts +74 -0
- package/adapters/mcp/toolset-selection.ts +41 -0
- package/adapters/mcp/toolset.ts +336 -0
- package/adapters/mcp/transports/http.ts +1456 -0
- package/adapters/mcp/transports/session-registry.ts +513 -0
- package/adapters/mcp/transports/stdio.ts +8 -0
- package/adapters/mcp/tunnels/openai-secure-tunnel.ts +139 -0
- package/adapters/mcp/types.ts +41 -0
- package/assets/AGENTS.md +2 -16
- package/assets/CLAUDE.md +2 -16
- package/assets/hooks/AGENTS.md +3 -14
- package/assets/hooks/CLAUDE.md +3 -14
- package/assets/hooks/hook-input.sh +9 -2
- package/assets/hooks/lib/workflow-state.sh +28 -10
- package/assets/hooks/prompt-guard.sh +63 -37
- package/assets/hooks/security-sentinel.sh +2 -1
- package/assets/hooks/session-start-context.sh +7 -5
- package/assets/hooks/stop-orchestrator.sh +13 -3
- package/assets/initializer-question-pack.v4.json +11 -7
- package/assets/partials/05-workflow.partial.md +18 -73
- package/assets/partials/07-footer.partial.md +1 -1
- package/assets/partials-agents/02-operating-mode.partial.md +1 -2
- package/assets/partials-agents/04-task-protocol.partial.md +14 -59
- package/assets/partials-agents/06-quality-safety.partial.md +11 -16
- package/assets/partials-agents/08-deep-docs.partial.md +1 -2
- package/assets/plan-map.json +11 -11
- package/assets/plugin-registry.v1.json +9 -3
- package/assets/plugin-trust-policy.v1.json +6 -0
- package/assets/recovery/ForgeRecovery.ps1 +100 -0
- package/assets/recovery/forge-wsl-rescue +271 -0
- package/assets/reference-configs/development-protocol.md +11 -15
- package/assets/reference-configs/document-generation.md +1 -1
- package/assets/reference-configs/global-working-rules.md +9 -1
- package/assets/reference-configs/harness-overview.md +22 -112
- package/assets/reference-configs/hook-operations.md +1 -1
- package/assets/reference-configs/sprint-contracts.md +2 -2
- package/assets/skill-version.json +2 -2
- package/assets/templates/helpers/architecture-event.ts +3 -34
- package/assets/templates/helpers/architecture-queue.sh +61 -10
- package/assets/templates/helpers/archive-workflow.sh +90 -180
- package/assets/templates/helpers/capability-config.ts +8 -11
- package/assets/templates/helpers/capability-resolver.ts +37 -19
- package/assets/templates/helpers/capture-plan.sh +2 -2
- package/assets/templates/helpers/check-architecture-sync.sh +1 -9
- package/assets/templates/helpers/check-task-sync.sh +82 -11
- package/assets/templates/helpers/check-task-workflow.sh +1 -3
- package/assets/templates/helpers/context-contract-sync.sh +1 -12
- package/assets/templates/helpers/contract-worktree.sh +26 -24
- package/assets/templates/helpers/ensure-task-workflow.sh +6 -8
- package/assets/templates/helpers/inspect-project-state.ts +1 -1
- package/assets/templates/helpers/migrate-project-template.sh +1 -1
- package/assets/templates/helpers/migrate-workflow-docs.ts +53 -239
- package/assets/templates/helpers/new-plan.sh +2 -2
- package/assets/templates/helpers/plan-to-todo.sh +3 -31
- package/assets/templates/helpers/sprint-backlog.sh +1 -1
- package/assets/templates/helpers/switch-plan.sh +1 -1
- package/assets/templates/helpers/workflow-contract.ts +1 -0
- package/assets/workflow-contract.v1.json +63 -171
- package/assets/workflows/xiaohongshu/xiaohongshu.collect-metrics.v1.draft.json +118 -0
- package/assets/workflows/xiaohongshu/xiaohongshu.publish-image-note.v1.draft.json +316 -0
- package/assets/workflows/xiaohongshu/xiaohongshu.publish-long-text.v1.draft.json +324 -0
- package/docs/architecture/CURRENT.md +148 -13
- package/docs/architecture/EVOLUTION.md +13 -0
- package/docs/architecture/decisions/20260801-controller-home-sqlite-state.md +2 -1
- package/docs/architecture/decisions/20260831-independent-windows-wsl-host-rescue.md +59 -0
- package/docs/architecture/decisions/20260904-ios-device-migration-baseline.md +120 -0
- package/docs/architecture/decisions/20260904-repository-runtime-data-boundary.md +42 -0
- package/docs/architecture/decisions/20260907-assistant-learning-loop.md +35 -0
- package/docs/architecture/decisions/20260907-controller-round-transition-policy.md +296 -0
- package/docs/architecture/decisions/20260907-maintenance-diagnosis-bounded-read.md +39 -0
- package/docs/architecture/domains/verification.md +7 -0
- package/docs/architecture/domains/workflow-engine.md +1 -1
- package/docs/architecture/index.md +3 -3
- package/docs/architecture/modules/controller-runtime/transport-lifecycle.md +19 -8
- package/docs/architecture/requests/2026-09-04-computer-plugin-workflow-taxonomy.md +124 -0
- package/docs/forge-chatgpt-mcp-setup.md +5 -3
- package/docs/forge-plugin-management.md +20 -4
- package/docs/forge-runtime-self-healing-loop.md +2 -0
- package/docs/operations/benchmark-cross-version-freeze.md +38 -0
- package/docs/operations/controller-browser-plugin.md +3 -1
- package/docs/operations/homebrew.md +2 -2
- package/docs/operations/platform-support.md +16 -8
- package/docs/operations/platform-support.zh-CN.md +2 -2
- package/docs/operations/standalone-disaster-recovery.md +26 -1
- package/docs/operations/uu-remote-rescue.md +41 -0
- package/docs/operations/windows-wsl-host-rescue.md +133 -0
- package/docs/public-usage-guide.md +1 -1
- package/docs/public-usage-guide.zh-CN.md +1 -1
- package/docs/tutorials/01-install-and-start.md +1 -1
- package/docs/tutorials/01-install-and-start.zh-CN.md +1 -1
- package/docs/tutorials/02-connect-chatgpt.md +4 -4
- package/docs/tutorials/02-connect-chatgpt.zh-CN.md +4 -4
- package/docs/wiki/Implementation.md +1 -1
- package/package.json +23 -14
- package/packages/kernel/controller/api/index.ts +2 -0
- package/packages/kernel/controller/application/controller-round-service.ts +29 -0
- package/packages/kernel/controller/application/controller-service.ts +30 -0
- package/packages/kernel/controller/domain/controller-round-transition-policy.ts +308 -0
- package/packages/kernel/controller/domain/controller-round.ts +107 -0
- package/packages/kernel/controller/domain/execution-quality.ts +133 -0
- package/packages/kernel/controller/domain/types.ts +56 -0
- package/packages/kernel/controller/index.ts +1 -0
- package/packages/kernel/controller/infrastructure/controller-binding-store.ts +84 -0
- package/packages/kernel/controller/infrastructure/controller-round-store.ts +1384 -0
- package/packages/kernel/controller/infrastructure/controller-session-store.ts +578 -0
- package/packages/kernel/controller/ports/controller-host.ts +16 -0
- package/packages/kernel/goal/api/index.ts +1 -0
- package/packages/kernel/goal/domain/types.ts +40 -0
- package/packages/kernel/goal/index.ts +1 -0
- package/packages/kernel/identity/api/index.ts +5 -0
- package/packages/kernel/identity/application/identity-service.ts +67 -0
- package/packages/kernel/identity/domain/scope.ts +63 -0
- package/packages/kernel/identity/domain/semantic-sync.ts +81 -0
- package/packages/kernel/identity/domain/types.ts +44 -0
- package/packages/kernel/identity/domain/workspace.ts +56 -0
- package/packages/kernel/identity/index.ts +1 -0
- package/packages/kernel/identity/infrastructure/identity-store.ts +75 -0
- package/packages/kernel/memory/api/index.ts +5 -0
- package/packages/kernel/memory/application/experience-service.ts +118 -0
- package/packages/kernel/memory/application/outcome-service.ts +77 -0
- package/packages/kernel/memory/domain/experience.ts +103 -0
- package/packages/kernel/memory/domain/operational-prior.ts +92 -0
- package/packages/kernel/memory/index.ts +1 -0
- package/packages/kernel/progression/api/index.ts +2 -0
- package/packages/kernel/progression/application/projector.ts +211 -0
- package/packages/kernel/progression/domain/types.ts +116 -0
- package/packages/kernel/progression/index.ts +1 -0
- package/packages/kernel/scheduler/api/index.ts +6 -0
- package/packages/kernel/scheduler/application/continuation-service.ts +212 -0
- package/packages/kernel/scheduler/application/eligibility.ts +252 -0
- package/packages/kernel/scheduler/application/schedule-service.ts +24 -0
- package/packages/kernel/scheduler/application/settlement.ts +197 -0
- package/packages/kernel/scheduler/domain/continuation.ts +38 -0
- package/packages/kernel/scheduler/domain/schedule.ts +149 -0
- package/packages/kernel/scheduler/index.ts +1 -0
- package/packages/kernel/scheduler/infrastructure/continuation-dispatch-store.ts +38 -0
- package/packages/kernel/scheduler/infrastructure/schedule-store.ts +785 -0
- package/packages/kernel/work/api/index.ts +15 -0
- package/packages/kernel/work/application/work-service.ts +148 -0
- package/packages/kernel/work/domain/admission-policy.ts +34 -0
- package/packages/kernel/work/domain/check-receipt.ts +43 -0
- package/packages/kernel/work/domain/context-closure-receipt.ts +32 -0
- package/packages/kernel/work/domain/engineering-contracts.ts +157 -0
- package/packages/kernel/work/domain/engineering-design.ts +281 -0
- package/packages/kernel/work/domain/engineering-profile.ts +242 -0
- package/packages/kernel/work/domain/execution-concurrency.ts +254 -0
- package/packages/kernel/work/domain/execution-snapshot.ts +33 -0
- package/packages/kernel/work/domain/implementation-review.ts +510 -0
- package/packages/kernel/work/domain/project-engineering-contract.ts +202 -0
- package/packages/kernel/work/domain/repository-completion-receipt.ts +73 -0
- package/packages/kernel/work/domain/state-machine.ts +259 -0
- package/packages/kernel/work/domain/types.ts +528 -0
- package/packages/kernel/work/index.ts +1 -0
- package/packages/kernel/work/infrastructure/work-contract-store.ts +1876 -0
- package/packages/kernel/work/ports/work-contract-store.ts +18 -0
- package/packages/plugin-runtime/browser/index.ts +3 -0
- package/packages/plugin-runtime/browser/provider-registry.ts +110 -0
- package/packages/plugin-runtime/browser/runtime-contract.ts +150 -0
- package/packages/plugin-runtime/browser/session-authority.ts +61 -0
- package/packages/plugin-runtime/computer/index.ts +6 -0
- package/packages/plugin-runtime/computer/provider-error.ts +26 -0
- package/packages/plugin-runtime/computer/provider-registration.ts +52 -0
- package/packages/plugin-runtime/computer/provider-registry.ts +63 -0
- package/packages/plugin-runtime/computer/provider.ts +19 -0
- package/packages/plugin-runtime/computer/target-authority.ts +190 -0
- package/packages/plugin-runtime/computer/target-persistence.ts +54 -0
- package/packages/plugin-runtime/external/index.ts +1 -0
- package/packages/plugin-runtime/external/unix-jsonl-transport.ts +457 -0
- package/packages/plugin-runtime/index.ts +3 -0
- package/packages/protocols/browser/index.ts +1 -0
- package/packages/protocols/browser/session.ts +98 -0
- package/packages/protocols/computer/contract.ts +199 -0
- package/packages/protocols/computer/index.ts +1 -0
- package/packages/protocols/failure/contract.ts +158 -0
- package/packages/protocols/failure/index.ts +1 -0
- package/packages/protocols/handoff/index.ts +1 -0
- package/packages/protocols/handoff/status.ts +22 -0
- package/packages/protocols/mcp/execution-context.ts +11 -0
- package/packages/protocols/mcp/index.ts +2 -0
- package/packages/protocols/mcp/tool-contract.ts +42 -0
- package/packages/workflow-runtime/api/index.ts +2 -0
- package/packages/workflow-runtime/application/interpreter.ts +342 -0
- package/packages/workflow-runtime/domain/workflow-asset.ts +223 -0
- package/packages/workflow-runtime/index.ts +1 -0
- package/scripts/assemble-template.ts +1 -1
- package/scripts/benchmark-computer-hotpath.ts +255 -0
- package/scripts/benchmark-controller-round2.ts +54 -2
- package/scripts/benchmark-ios-agent-device.ts +3 -6
- package/scripts/benchmark-mcp-transport.ts +3 -4
- package/scripts/benchmark-operational-memory-activation.ts +235 -0
- package/scripts/benchmark-operational-shadow.ts +28 -0
- package/scripts/bootstrap-runtime-maintenance-recovery.sh +4 -1
- package/scripts/canary-chatgpt-provider-delivery.ts +97 -0
- package/scripts/canary-controller-semantic-wait.ts +195 -0
- package/scripts/canary-global-hook.sh +2 -2
- package/scripts/check-mcp-compatibility.ts +173 -7
- package/scripts/check-platform-support.mjs +28 -2
- package/scripts/check-public-docs.sh +1 -1
- package/scripts/check-release-readiness.sh +1 -1
- package/scripts/check-repository-hygiene.mjs +92 -0
- package/scripts/check-runtime-architecture.mjs +1206 -95
- package/scripts/check-source-duplication.mjs +9 -1
- package/scripts/check-tarball-install-smoke.sh +98 -15
- package/scripts/controller-ngrok-rotation.sh +5 -4
- package/scripts/create-project-dirs.sh +10 -74
- package/scripts/forge-local-recovery-helper.mjs +204 -0
- package/scripts/forge-uu-remote-rescue-helper.d.mts +31 -0
- package/scripts/forge-uu-remote-rescue-helper.mjs +409 -0
- package/scripts/forge-windows-host-recovery-helper.mjs +136 -0
- package/scripts/hook-shim.sh +2 -2
- package/scripts/init-project.sh +12 -70
- package/scripts/install-desktop-operator-registration.ts +4 -2
- package/scripts/install-independent-windows-wsl-recovery.ts +125 -0
- package/scripts/install-standalone-recovery.ts +122 -10
- package/scripts/install-uu-remote-rescue-registration.ts +74 -0
- package/scripts/install-windows-host-recovery-registration.ts +22 -0
- package/scripts/lib/controller-home-env.sh +6 -3
- package/scripts/lib/project-init-lib.sh +39 -582
- package/scripts/migrate-chatgpt-automation-schedules.ts +2 -2
- package/scripts/migrate-windows-wsl-controller-home.ts +306 -0
- package/scripts/open-source-audit-allowlist.txt +12 -1
- package/scripts/portability-bindings.mjs +70 -0
- package/scripts/run-bounded-command.mjs +55 -0
- package/scripts/run-bun-test-file.ts +22 -6
- package/scripts/run-governed-gate.ts +4 -1
- package/scripts/smoke-mcp-http-runtime.ts +1 -1
- package/scripts/smoke-runtime-recovery.ts +34 -1
- package/scripts/smoke-schedule-engine.ts +1 -1
- package/scripts/stage-runtime-release.ts +6 -1
- package/scripts/test-governance.ts +51 -2
- package/src/cli/agent-jobs/job-manager.ts +10 -1
- package/src/cli/chatgpt-browser/bind-server.ts +2 -1
- package/src/cli/chatgpt-browser/binding.ts +20 -10
- package/src/cli/chatgpt-browser/bridge-extension.ts +75 -0
- package/src/cli/chatgpt-browser/bridge-provider.ts +376 -11
- package/src/cli/chatgpt-browser/engine.ts +10 -11
- package/src/cli/chatgpt-browser/native-provider.ts +24 -83
- package/src/cli/chatgpt-browser/oracle-provider.ts +5 -3
- package/src/cli/chatgpt-browser/types.ts +1 -0
- package/src/cli/commands/bootstrap-control.ts +334 -0
- package/src/cli/commands/brain-assistant.ts +38 -0
- package/src/cli/commands/brain.ts +193 -40
- package/src/cli/commands/chatgpt.ts +127 -0
- package/src/cli/commands/computer.ts +437 -0
- package/src/cli/commands/doctor.ts +18 -1
- package/src/cli/commands/init-hook.ts +71 -61
- package/src/cli/commands/init.ts +3 -1
- package/src/cli/commands/mcp.ts +16 -10
- package/src/cli/commands/plugin.ts +38 -12
- package/src/cli/commands/recovery.ts +493 -80
- package/src/cli/commands/runtime.ts +9 -4
- package/src/cli/commands/setup-profile.ts +89 -39
- package/src/cli/commands/status.ts +2 -1
- package/src/cli/controller/check-runner.ts +487 -98
- package/src/cli/controller/context/types.ts +7 -1
- package/src/cli/controller/context-pack.ts +23 -3
- package/src/cli/controller/issue-store.ts +1 -1
- package/src/cli/controller/systemd-user.ts +110 -0
- package/src/cli/controller/types.ts +10 -70
- package/src/cli/controller/worklog.ts +67 -13
- package/src/cli/editing/edit-operation-contract.ts +92 -0
- package/src/cli/editing/edit-session.ts +289 -13
- package/src/cli/github/plugin.ts +17 -12
- package/src/cli/hook/route-registry.ts +3 -5
- package/src/cli/hook/runtime.ts +49 -3
- package/src/cli/index.ts +3 -0
- package/src/cli/local-bridge/connector-freshness.ts +2 -2
- package/src/cli/local-bridge/console-automations.ts +2 -2
- package/src/cli/local-bridge/facade-api.ts +41 -14
- package/src/cli/local-bridge/job-store.ts +1 -1
- package/src/cli/local-bridge/server.ts +24 -27
- package/src/cli/mcp/access-mode.ts +2 -132
- package/src/cli/mcp/access-tools.ts +2 -387
- package/src/cli/mcp/audit.ts +2 -32
- package/src/cli/mcp/auth.ts +2 -404
- package/src/cli/mcp/instructions.ts +2 -34
- package/src/cli/mcp/legacy-context.ts +2 -16
- package/src/cli/mcp/legacy-tool-service.ts +2 -5308
- package/src/cli/mcp/multi-repository.ts +2 -436
- package/src/cli/mcp/oauth.ts +2 -375
- package/src/cli/mcp/openai-secure-tunnel.ts +2 -0
- package/src/cli/mcp/paths.ts +2 -140
- package/src/cli/mcp/policy.ts +2 -232
- package/src/cli/mcp/post-finalize-work-attribution.ts +2 -150
- package/src/cli/mcp/proxy-env.ts +2 -62
- package/src/cli/mcp/readiness-projection.ts +2 -24
- package/src/cli/mcp/redaction.ts +2 -21
- package/src/cli/mcp/repo.ts +2 -24
- package/src/cli/mcp/repository-tools.ts +2 -1585
- package/src/cli/mcp/server.ts +2 -951
- package/src/cli/mcp/setup.ts +2 -737
- package/src/cli/mcp/tool-capability-cache.ts +2 -87
- package/src/cli/mcp/tool-contract.ts +2 -14
- package/src/cli/mcp/tools.ts +2 -9
- package/src/cli/mcp/toolset-names.ts +2 -70
- package/src/cli/mcp/toolset-selection.ts +2 -41
- package/src/cli/mcp/toolset.ts +2 -336
- package/src/cli/mcp/transports/AGENTS.md +1 -10
- package/src/cli/mcp/transports/CLAUDE.md +1 -10
- package/src/cli/mcp/transports/http.ts +2 -1414
- package/src/cli/mcp/transports/session-registry.ts +2 -490
- package/src/cli/mcp/transports/stdio.ts +2 -8
- package/src/cli/mcp/types.ts +2 -41
- package/src/cli/mcp/umbrella-tools.ts +2 -117
- package/src/cli/repositories/command-executor.ts +64 -18
- package/src/cli/repositories/command-scope.ts +117 -1
- package/src/cli/repositories/controller-home.ts +119 -1
- package/src/cli/repositories/identity.ts +13 -0
- package/src/cli/repositories/registry.ts +68 -49
- package/src/cli/repositories/repository-snapshot.ts +53 -7
- package/src/cli/repositories/runtime-storage.ts +27 -2
- package/src/cli/repositories/safe-patch.ts +34 -38
- package/src/cli/repositories/selected-path-actions.ts +22 -2
- package/src/cli/repositories/structured-git.ts +105 -25
- package/src/cli/runtime/helper-runner.ts +7 -0
- package/src/cli/runtime-invocation.ts +8 -1
- package/src/cli/tools/codegraph.ts +44 -25
- package/src/core/adoption/manifest-templates.ts +1 -1
- package/src/core/adoption/plan.ts +3 -26
- package/src/core/adoption/workflow-contract-plan.ts +17 -9
- package/src/effects/process-runner.ts +8 -14
- package/src/runtime/context/assistant-context.ts +154 -0
- package/src/runtime/context/assistant-work-context.ts +147 -0
- package/src/runtime/context/codegraph-cache-boundary.ts +255 -0
- package/src/runtime/context/codegraph-read-provider.ts +55 -22
- package/src/runtime/context/codegraph-sidecar.cjs +8 -1
- package/src/runtime/context/context-closure.ts +268 -0
- package/src/runtime/context/context-plane.ts +211 -0
- package/src/runtime/context/project-engineering-contract.ts +60 -0
- package/src/runtime/control-plane/bootstrap/capability-provider.ts +74 -0
- package/src/runtime/control-plane/bootstrap/control-api.ts +57 -0
- package/src/runtime/control-plane/bootstrap/index.ts +5 -0
- package/src/runtime/control-plane/bootstrap/state-machine.ts +141 -0
- package/src/runtime/control-plane/bootstrap/store.ts +21 -0
- package/src/runtime/control-plane/bootstrap/types.ts +87 -0
- package/src/runtime/control-plane/cleanup-artifact-retention.ts +328 -0
- package/src/runtime/control-plane/codegraph-cache-retention.ts +164 -0
- package/src/runtime/control-plane/concurrency/work-execution-concurrency.ts +320 -0
- package/src/runtime/control-plane/execution/content-equivalent-commit-authority.ts +169 -0
- package/src/runtime/control-plane/execution/controller-authority-recovery.ts +350 -0
- package/src/runtime/control-plane/execution/direct-canonical-work-reconciliation.ts +85 -0
- package/src/runtime/control-plane/execution/direct-edit-work-completion.ts +417 -100
- package/src/runtime/control-plane/execution/edit-validation-coordinator.ts +103 -15
- package/src/runtime/control-plane/execution/implementation-review-content.ts +95 -0
- package/src/runtime/control-plane/execution/repository-work-attribution.ts +2 -2
- package/src/runtime/control-plane/execution/retained-work-resume.ts +336 -6
- package/src/runtime/control-plane/execution/validation.ts +28 -3
- package/src/runtime/control-plane/execution/work-authority-reconciler.ts +116 -0
- package/src/runtime/control-plane/execution/work-completion-authority.ts +44 -2
- package/src/runtime/control-plane/execution/work-evidence-policy.ts +34 -3
- package/src/runtime/control-plane/execution/work-execution-support.ts +42 -22
- package/src/runtime/control-plane/execution/work-finalization-service.ts +1103 -166
- package/src/runtime/control-plane/execution/work-handle-authority.ts +267 -16
- package/src/runtime/control-plane/execution/work-handle-store.ts +77 -0
- package/src/runtime/control-plane/execution/work-head-settlement.ts +75 -0
- package/src/runtime/control-plane/execution/work-lifecycle-audit.ts +145 -23
- package/src/runtime/control-plane/execution/work-operation-service.ts +49 -13
- package/src/runtime/control-plane/execution/work-preparation-service.ts +31 -15
- package/src/runtime/control-plane/execution/work-remote-delivery.ts +1 -1
- package/src/runtime/control-plane/execution/work-task-receipt.ts +20 -1
- package/src/runtime/control-plane/execution/work-terminal-cleanup.ts +407 -76
- package/src/runtime/control-plane/execution/work-validation-reconciler.ts +88 -18
- package/src/runtime/control-plane/execution/work-verification-context.ts +1 -1
- package/src/runtime/control-plane/execution/work-verification-service.ts +258 -4
- package/src/runtime/control-plane/execution/work-verification-snapshot.ts +200 -15
- package/src/runtime/control-plane/facade/capability-registry.ts +21 -10
- package/src/runtime/control-plane/facade/controller-round-relay.ts +3 -1118
- package/src/runtime/control-plane/facade/controller-session-store.ts +2 -488
- package/src/runtime/control-plane/facade/goal-workloop-access.ts +6 -3
- package/src/runtime/control-plane/facade/goal-workloop.ts +972 -137
- package/src/runtime/control-plane/facade/handoff-inbox-application.ts +158 -0
- package/src/runtime/control-plane/facade/handoff-inbox-store.ts +10 -0
- package/src/runtime/control-plane/facade/index.ts +4 -2
- package/src/runtime/control-plane/facade/operation-digest.ts +22 -58
- package/src/runtime/control-plane/facade/plan-contract-store.ts +1004 -38
- package/src/runtime/control-plane/facade/repository-work-admission.ts +19 -1
- package/src/runtime/control-plane/facade/requirement-authority.ts +218 -0
- package/src/runtime/control-plane/facade/requirement-board.ts +6 -1
- package/src/runtime/control-plane/facade/rh-work-operation-contract.ts +48 -0
- package/src/runtime/control-plane/facade/semantic-admission.ts +8 -16
- package/src/runtime/control-plane/facade/suggested-actions.ts +4 -1
- package/src/runtime/control-plane/facade/types.ts +189 -532
- package/src/runtime/control-plane/facade/work-admission-policy.ts +84 -35
- package/src/runtime/control-plane/facade/work-contract-store.ts +2 -1043
- package/src/runtime/control-plane/facade/work-implementation-review.ts +2 -0
- package/src/runtime/control-plane/facade/work-state-machine.ts +2 -250
- package/src/runtime/control-plane/generated-cache-retention.ts +239 -18
- package/src/runtime/control-plane/global-scheduler/durable-admission.ts +12 -1
- package/src/runtime/control-plane/global-scheduler/maintenance.ts +76 -13
- package/src/runtime/control-plane/global-scheduler/periodic-cleanup-entry.ts +55 -0
- package/src/runtime/control-plane/global-scheduler/periodic-cleanup-process.ts +98 -0
- package/src/runtime/control-plane/global-scheduler/reconciliation.ts +1 -1
- package/src/runtime/control-plane/global-scheduler/scheduler-worker-entry.ts +10 -0
- package/src/runtime/control-plane/global-scheduler/scheduler.ts +173 -81
- package/src/runtime/control-plane/global-scheduler/worker-launch.ts +18 -6
- package/src/runtime/control-plane/launcher/chatgpt-round-continuation.ts +397 -0
- package/src/runtime/control-plane/launcher/chatgpt-work-binding-store.ts +2 -177
- package/src/runtime/control-plane/launcher/chatgpt-work-continuation.ts +301 -560
- package/src/runtime/control-plane/launcher/provider-mcp-bootstrap.ts +20 -4
- package/src/runtime/control-plane/launcher/thin-launcher.ts +50 -4
- package/src/runtime/control-plane/lifecycle-retention-metrics.ts +60 -0
- package/src/runtime/control-plane/persistence/experience-store.ts +169 -0
- package/src/runtime/control-plane/persistence/operational-prior-store.ts +348 -0
- package/src/runtime/control-plane/persistence/requirement-store.ts +30 -22
- package/src/runtime/control-plane/persistence/sqlite-store.ts +545 -76
- package/src/runtime/control-plane/persistence/workflow-content-store.ts +98 -0
- package/src/runtime/control-plane/persistence/workflow-registry-store.ts +186 -0
- package/src/runtime/control-plane/persistence/workflow-run-store.ts +123 -0
- package/src/runtime/control-plane/release-retention.ts +56 -16
- package/src/runtime/control-plane/repository-namespace-retention.ts +208 -0
- package/src/runtime/control-plane/routing/route-policy.ts +19 -0
- package/src/runtime/control-plane/runtime-cleanup.ts +574 -37
- package/src/runtime/control-plane/runtime-generation.ts +6 -0
- package/src/runtime/control-plane/workspace/semantic-sync-service.ts +465 -0
- package/src/runtime/control-plane/workspace/workspace-store.ts +80 -0
- package/src/runtime/diagnostics/incident-repair.ts +406 -0
- package/src/runtime/diagnostics/mcp-timing.ts +125 -6
- package/src/runtime/diagnostics/performance.ts +6 -14
- package/src/runtime/diagnostics/read-only-tool.ts +1 -1
- package/src/runtime/evidence/artifact-store.ts +65 -2
- package/src/runtime/evidence/operational-shadow.ts +38 -0
- package/src/runtime/evidence/process-check-receipt.ts +5 -43
- package/src/runtime/evidence/sensitive-output.ts +4 -4
- package/src/runtime/execution/jobs/receipt-recovery.ts +1 -1
- package/src/runtime/execution/jobs/store.ts +121 -2
- package/src/runtime/execution/managed-workspace.ts +55 -5
- package/src/runtime/execution/process-runtime/canary.ts +19 -5
- package/src/runtime/execution/process-runtime/check-classification.ts +48 -0
- package/src/runtime/execution/process-runtime/check-facade.ts +23 -9
- package/src/runtime/execution/process-runtime/check-receipt.ts +1 -1
- package/src/runtime/execution/process-runtime/check-result-retention.ts +132 -0
- package/src/runtime/execution/process-runtime/check-result.ts +73 -2
- package/src/runtime/execution/process-runtime/check-runner-sidecar.ts +42 -3
- package/src/runtime/execution/process-runtime/check-storage.ts +187 -0
- package/src/runtime/execution/process-runtime/command-facade.ts +38 -87
- package/src/runtime/execution/process-runtime/index.ts +1 -0
- package/src/runtime/execution/process-runtime/interactive-admission.ts +2 -1
- package/src/runtime/execution/process-runtime/lightweight-managed.ts +32 -1
- package/src/runtime/execution/process-runtime/persisted-check.ts +128 -21
- package/src/runtime/execution/process-runtime/resource-claims.ts +60 -3
- package/src/runtime/execution/process-runtime/runtime.ts +132 -15
- package/src/runtime/execution/process-runtime/types.ts +22 -0
- package/src/runtime/execution/workers/executor.ts +16 -13
- package/src/runtime/execution/workers/worker-entry.ts +1 -1
- package/src/runtime/gateway/mcp/execution-tools.ts +2 -207
- package/src/runtime/gateway/mcp/legacy-ios-tool-adapter.ts +2 -61
- package/src/runtime/gateway/mcp/persisted-check-process.ts +2 -3
- package/src/runtime/gateway/mcp/process-tools.ts +2 -289
- package/src/runtime/gateway/mcp/router.ts +2 -1125
- package/src/runtime/gateway/mcp/runtime-tool-definitions.ts +2 -565
- package/src/runtime/gateway/mcp/runtime-tools.ts +2 -6651
- package/src/runtime/gateway/mcp/work-validation-reconciler.ts +2 -1
- package/src/runtime/maintenance/cleanup.ts +38 -24
- package/src/runtime/platform/browser-product-discovery.ts +174 -0
- package/src/runtime/platform/computer-platform.ts +14 -0
- package/src/runtime/platform/executable-discovery.ts +54 -0
- package/src/runtime/platform/fetch-http-server.ts +90 -0
- package/src/runtime/platform/service-manager.ts +138 -0
- package/src/runtime/platform/windows-host-discovery.ts +118 -0
- package/src/runtime/platform/windows-recovery-deployment.ts +67 -0
- package/src/runtime/plugins/action-application.ts +135 -0
- package/src/runtime/plugins/app-store-connect-adapter.ts +9 -21
- package/src/runtime/plugins/browser-adapter.ts +140 -100
- package/src/runtime/plugins/browser-automation-service.ts +15 -36
- package/src/runtime/plugins/browser-macos-bridge.ts +69 -21
- package/src/runtime/plugins/browser-manifest-surface.ts +30 -6
- package/src/runtime/plugins/browser-native-session-service.ts +1 -1
- package/src/runtime/plugins/browser-provider-registry.ts +2 -109
- package/src/runtime/plugins/browser-registration.ts +16 -0
- package/src/runtime/plugins/browser-runtime-contract.ts +2 -154
- package/src/runtime/plugins/browser-runtime.ts +41 -9
- package/src/runtime/plugins/browser-session-authority.ts +101 -291
- package/src/runtime/plugins/browser-session-legacy-migration.ts +203 -0
- package/src/runtime/plugins/browser-session-store.ts +229 -36
- package/src/runtime/plugins/browser-session-types.ts +2 -98
- package/src/runtime/plugins/capability-authorization-grants.ts +17 -0
- package/src/runtime/plugins/catalog-trust.ts +82 -0
- package/src/runtime/plugins/computer-registration.ts +839 -0
- package/src/runtime/plugins/config-store.ts +119 -0
- package/src/runtime/plugins/desktop-operator-external-policy.ts +932 -0
- package/src/runtime/plugins/desktop-operator-registration.ts +111 -8
- package/src/runtime/plugins/errors.ts +8 -1
- package/src/runtime/plugins/external-adapter.ts +45 -322
- package/src/runtime/plugins/external-provider-policy.ts +48 -0
- package/src/runtime/plugins/external-registration.ts +9 -1
- package/src/runtime/plugins/external-unix-socket-probe.cjs +9 -3
- package/src/runtime/plugins/external-unix-socket.ts +64 -193
- package/src/runtime/plugins/first-party-external-registration.ts +163 -0
- package/src/runtime/plugins/first-party-registry.ts +3 -1
- package/src/runtime/plugins/github-adapter.ts +1 -2
- package/src/runtime/plugins/gmail-adapter.ts +6 -6
- package/src/runtime/plugins/google-calendar-adapter.ts +6 -6
- package/src/runtime/plugins/google-shared.ts +31 -46
- package/src/runtime/plugins/google-tasks-adapter.ts +6 -6
- package/src/runtime/plugins/ios-adapter.ts +1 -2
- package/src/runtime/plugins/local-system-adapter.ts +180 -0
- package/src/runtime/plugins/macos-capability-broker.ts +15 -105
- package/src/runtime/plugins/resend-adapter.ts +15 -24
- package/src/runtime/plugins/store.ts +493 -51
- package/src/runtime/plugins/types.ts +8 -1
- package/src/runtime/plugins/uu-remote-rescue-registration.ts +155 -0
- package/src/runtime/plugins/windows-host-recovery-install.ts +68 -0
- package/src/runtime/plugins/windows-host-recovery-registration.ts +85 -0
- package/src/runtime/plugins/xiaohongshu-publish.ts +86 -430
- package/src/runtime/projections/controller-context.ts +12 -6
- package/src/runtime/projections/git-status-sampler.ts +73 -22
- package/src/runtime/projections/materialized-view.ts +14 -8
- package/src/runtime/recovery/capability-action-application.ts +228 -0
- package/src/runtime/recovery/index.ts +3 -0
- package/src/runtime/recovery/lifecycle-inventory.ts +338 -0
- package/src/runtime/recovery/local-jobs-repair.ts +14 -14
- package/src/runtime/recovery/maintenance-executor.ts +358 -76
- package/src/runtime/recovery/quarantine-retention.ts +256 -0
- package/src/runtime/release/identity.ts +6 -0
- package/src/runtime/resources/leases/store.ts +1 -1
- package/src/runtime/root/active-execution-power-assertion.ts +139 -0
- package/src/runtime/root/assistant-learning-loop.ts +45 -0
- package/src/runtime/root/browser-session-composition.ts +59 -0
- package/src/runtime/root/computer-composition.ts +191 -0
- package/src/runtime/root/computer-target-composition.ts +16 -0
- package/src/runtime/root/computer-target-persistence.ts +39 -0
- package/src/runtime/root/controller-round-composition.ts +107 -0
- package/src/runtime/root/controller-services.ts +2 -0
- package/src/runtime/root/entry.ts +4 -3
- package/src/runtime/root/gateway-adapter.ts +3 -3
- package/src/runtime/root/local-bridge.ts +5 -2
- package/src/runtime/root/mcp-transport.ts +5 -5
- package/src/runtime/root/package-connector-service.ts +108 -56
- package/src/runtime/root/package-runtime-release.ts +86 -18
- package/src/runtime/root/package-runtime-service.ts +63 -86
- package/src/runtime/root/release-execution-canary.ts +71 -0
- package/src/runtime/root/release-loader.ts +68 -0
- package/src/runtime/root/release-manifest.ts +106 -2
- package/src/runtime/root/release-materialize.ts +103 -4
- package/src/runtime/root/release-store.ts +31 -2
- package/src/runtime/root/runtime.ts +240 -25
- package/src/runtime/root/scheduled-controller-composition.ts +51 -0
- package/src/runtime/root/scheduler.ts +12 -4
- package/src/runtime/root/service-runner.ts +1 -1
- package/src/runtime/root/service.ts +41 -16
- package/src/runtime/root/status.ts +1 -0
- package/src/runtime/root/types.ts +24 -1
- package/src/runtime/shared/local-bridge-surface.ts +1 -1
- package/src/runtime/shared/process-environment.ts +19 -4
- package/src/runtime/shared/storage-capacity.ts +68 -0
- package/src/runtime/standalone-recovery/controller-home-migration.ts +1116 -0
- package/src/runtime/standalone-recovery/core.ts +984 -230
- package/src/runtime/standalone-recovery/entry.ts +182 -51
- package/src/runtime/standalone-recovery/independent-host-rescue.ts +172 -0
- package/src/runtime/standalone-recovery/installer.ts +212 -104
- package/src/runtime/standalone-recovery/mcp-server.ts +243 -0
- package/src/runtime/standalone-recovery/mutation-identity-contract.ts +21 -0
- package/src/runtime/standalone-recovery/operation-lock.ts +160 -0
- package/src/runtime/standalone-recovery/performance.ts +135 -0
- package/src/runtime/standalone-recovery/release.ts +5 -0
- package/src/runtime/watchdog/workflow-watchdog.ts +1 -1
- package/src/runtime/workflow/schedules/chatgpt-automation-migration.ts +1 -1
- package/src/runtime/workflow/schedules/engine.ts +191 -343
- package/src/runtime/workflow/schedules/pending-activation.ts +1 -1
- package/src/runtime/workflow/schedules/settlement.ts +2 -191
- package/src/runtime/workflow/schedules/store.ts +2 -607
- package/src/runtime/workflow/schedules/types.ts +2 -127
- package/src/runtime/workflow/schedules/work-continuation.ts +180 -15
- package/src/runtime/workflows/first-party/xiaohongshu.ts +122 -0
- package/src/runtime/workflows/runtime.ts +486 -0
- package/src/testing/test-governance.ts +90 -29
- package/assets/templates/helpers/archive-architecture-request.sh +0 -244
- package/docs/architecture/history/README.md +0 -7
- package/docs/architecture/history/chatgpt-handoff-facade.md +0 -397
- package/docs/architecture/history/global-hook-runtime.md +0 -166
- package/docs/architecture/history/ios-semantic-automation-provider-v2.md +0 -170
- package/docs/architecture/requests/archive/2026/20260528-154740-ai-hooks-ai-hooks-hook-input-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-154816-assets-hooks-assets-hooks-hook-input-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-161851-ai-harness-workflow-contract-json-ai-harness-workflow-contract-json.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-161853-scripts-check-task-workflow-sh-scripts-check-task-workflow-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-161911-assets-workflow-contract-v1-json-assets-workflow-contract-v1-json.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-222416-ai-hooks-ai-hooks-hook-input-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-222444-ai-hooks-ai-hooks-pre-edit-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-222457-ai-hooks-ai-hooks-pre-edit-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-222648-ai-hooks-ai-hooks-pre-edit-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-222705-ai-hooks-ai-hooks-worktree-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-222726-ai-hooks-ai-hooks-prompt-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-222727-ai-hooks-ai-hooks-prompt-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-224736-ai-hooks-ai-hooks-hook-input-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-233259-ai-hooks-ai-hooks-prompt-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-233335-assets-hooks-assets-hooks-prompt-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-233456-ai-hooks-ai-hooks-prompt-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-233457-assets-hooks-assets-hooks-prompt-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-233637-ai-hooks-ai-hooks-prompt-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260528-233638-assets-hooks-assets-hooks-prompt-guard-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260529-000322-root-package-json.md +0 -59
- package/docs/architecture/requests/archive/2026/20260529-004238-ai-hooks-ai-hooks-lib-workflow-state-sh.md +0 -59
- package/docs/architecture/requests/archive/2026/20260529-020654-ai-harness-policy-json-ai-harness-policy-json.md +0 -63
- package/docs/architecture/requests/archive/2026/20260529-094236-ai-hooks-ai-hooks-prompt-guard-sh.md +0 -60
- package/docs/architecture/requests/archive/2026/20260529-094323-assets-hooks-assets-hooks-prompt-guard-sh.md +0 -60
- package/docs/architecture/requests/archive/2026/20260529-094331-ai-hooks-ai-hooks-prompt-guard-sh.md +0 -60
- package/docs/architecture/requests/archive/2026/20260529-094339-assets-hooks-assets-hooks-prompt-guard-sh.md +0 -60
- package/docs/architecture/requests/archive/2026/20260529-094430-assets-hooks-assets-hooks-session-start-context-sh.md +0 -60
- package/docs/architecture/requests/archive/2026/20260529-094446-ai-hooks-ai-hooks-session-start-context-sh.md +0 -60
- package/docs/architecture/requests/archive/2026/20260613-002040-runtime-harness-hook-adapters.md +0 -68
- package/docs/architecture/requests/archive/2026/root.md +0 -68
- package/docs/architecture/requests/archive/2026/runtime-harness-hook-adapters.md +0 -76
- package/docs/architecture/requests/archive/2026/verification-evals-checks.md +0 -68
- package/docs/architecture/requests/archive/2026/workflow-engine-contract-assets.md +0 -69
- package/scripts/archive-architecture-request.sh +0 -4
- package/scripts/run-skill-evals.ts +0 -1022
package/src/cli/mcp/server.ts
CHANGED
|
@@ -1,951 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
4
|
-
import { CallToolRequestSchema, ListToolsRequestSchema, type Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
5
|
-
import { mcpServerInstructions } from './instructions';
|
|
6
|
-
import { buildMcpToolDefinitions, callMcpTool, type CallToolResult, type McpToolContext } from './tools';
|
|
7
|
-
import { createLegacyMcpToolContext } from './legacy-context';
|
|
8
|
-
import {
|
|
9
|
-
buildMultiRepositoryToolDefinitions,
|
|
10
|
-
callMultiRepositoryTool,
|
|
11
|
-
createMcpToolContext as createMultiRepositoryToolContext,
|
|
12
|
-
type McpServerOptions,
|
|
13
|
-
type MultiRepositoryMcpToolContext,
|
|
14
|
-
} from './multi-repository';
|
|
15
|
-
import { callAccessTool } from './access-tools';
|
|
16
|
-
import { callRepositoryToolWithPostFinalizeAttribution } from './post-finalize-work-attribution';
|
|
17
|
-
import { callRuntimeTool } from '../../runtime/gateway/mcp/runtime-tools';
|
|
18
|
-
import { callExecutionTool } from '../../runtime/gateway/mcp/execution-tools';
|
|
19
|
-
import { callProcessTool } from '../../runtime/gateway/mcp/process-tools';
|
|
20
|
-
import { injectDurableCommandFields, isGatewayIsolatedTool, routeDurableMcpCall } from '../../runtime/gateway/mcp/router';
|
|
21
|
-
import { controllerExposureSnapshot, isControllerToolExposed } from './toolset';
|
|
22
|
-
import { randomUUID } from 'crypto';
|
|
23
|
-
import { readFileSync } from 'fs';
|
|
24
|
-
import { join } from 'path';
|
|
25
|
-
import { observeRuntimeStatus } from '../../runtime/root/status';
|
|
26
|
-
import { readRuntimeReleaseAuthority } from '../../runtime/root/release-store';
|
|
27
|
-
import { getRuntimeWriteClaim } from '../../runtime/root/write-fence';
|
|
28
|
-
import { recordMcpIncident, recordMcpTiming, type McpTimingTrace } from '../../runtime/diagnostics/mcp-timing';
|
|
29
|
-
import { FORGE_VERSION, forgeToolSurfaceFingerprint } from '../controller/runtime-config';
|
|
30
|
-
|
|
31
|
-
export type { McpServerOptions } from './multi-repository';
|
|
32
|
-
export { buildMultiRepositoryToolDefinitions, callMultiRepositoryTool } from './multi-repository';
|
|
33
|
-
|
|
34
|
-
type ServerToolContext = McpToolContext | MultiRepositoryMcpToolContext;
|
|
35
|
-
|
|
36
|
-
/** A per-session schema read directly from the Canonical Runtime's tools/list. */
|
|
37
|
-
export interface CanonicalRuntimeToolSchema {
|
|
38
|
-
definitions: Tool[];
|
|
39
|
-
toolNames: string[];
|
|
40
|
-
fingerprint: string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function isMultiRepositoryContext(ctx: ServerToolContext): ctx is MultiRepositoryMcpToolContext {
|
|
44
|
-
return 'controllerHome' in ctx;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function recordRequestId(args: Record<string, unknown>, rpcId: string | number | undefined): string {
|
|
48
|
-
const explicit = typeof args.request_id === 'string' ? args.request_id.trim() : '';
|
|
49
|
-
return explicit || `mcp-rpc:${rpcId === undefined ? randomUUID() : String(rpcId)}`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function firstTimingString(...values: unknown[]): string | undefined {
|
|
53
|
-
for (const value of values) {
|
|
54
|
-
if (typeof value === 'string' && value.trim()) return value.trim();
|
|
55
|
-
}
|
|
56
|
-
return undefined;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function timingIdentity(value: CallToolResult | undefined): {
|
|
60
|
-
repoId?: string;
|
|
61
|
-
workId?: string;
|
|
62
|
-
processId?: string;
|
|
63
|
-
route?: string;
|
|
64
|
-
} {
|
|
65
|
-
if (!value?.structuredContent || typeof value.structuredContent !== 'object' || Array.isArray(value.structuredContent)) return {};
|
|
66
|
-
const structured = value.structuredContent as Record<string, unknown>;
|
|
67
|
-
const work = structured.work && typeof structured.work === 'object' && !Array.isArray(structured.work)
|
|
68
|
-
? structured.work as Record<string, unknown>
|
|
69
|
-
: undefined;
|
|
70
|
-
const process = structured.process && typeof structured.process === 'object' && !Array.isArray(structured.process)
|
|
71
|
-
? structured.process as Record<string, unknown>
|
|
72
|
-
: undefined;
|
|
73
|
-
const resultRef = structured.resultRef && typeof structured.resultRef === 'object' && !Array.isArray(structured.resultRef)
|
|
74
|
-
? structured.resultRef as Record<string, unknown>
|
|
75
|
-
: undefined;
|
|
76
|
-
return {
|
|
77
|
-
repoId: firstTimingString(structured.repoId, structured.repo_id, work?.repositoryId, work?.repoId),
|
|
78
|
-
workId: firstTimingString(structured.workId, structured.work_id, work?.workId),
|
|
79
|
-
processId: firstTimingString(structured.processId, structured.process_id, process?.processId, resultRef?.processId),
|
|
80
|
-
route: firstTimingString(structured.route, structured.path, structured.mode, process?.route),
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function tracedResult(
|
|
85
|
-
value: CallToolResult | undefined,
|
|
86
|
-
traceId: string,
|
|
87
|
-
requestId: string,
|
|
88
|
-
serverTiming?: { startedAt: string; durationMs: number },
|
|
89
|
-
): CallToolResult | undefined {
|
|
90
|
-
if (!value || !value.structuredContent || typeof value.structuredContent !== 'object' || Array.isArray(value.structuredContent)) return value;
|
|
91
|
-
const originalStructuredContent = value.structuredContent as Record<string, unknown>;
|
|
92
|
-
const structuredContent = {
|
|
93
|
-
...originalStructuredContent,
|
|
94
|
-
responseMeta: {
|
|
95
|
-
...(originalStructuredContent.responseMeta && typeof originalStructuredContent.responseMeta === 'object'
|
|
96
|
-
? originalStructuredContent.responseMeta as Record<string, unknown>
|
|
97
|
-
: {}),
|
|
98
|
-
traceId,
|
|
99
|
-
requestId,
|
|
100
|
-
...(serverTiming ? { serverStartedAt: serverTiming.startedAt, serverDurationMs: serverTiming.durationMs } : {}),
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
return {
|
|
104
|
-
...value,
|
|
105
|
-
structuredContent,
|
|
106
|
-
content: value.content.map((block, index) => index === 0 && block.type === 'text'
|
|
107
|
-
? { ...block, text: JSON.stringify(structuredContent) }
|
|
108
|
-
: block),
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async function traceControllerMcpRequest(
|
|
113
|
-
ctx: MultiRepositoryMcpToolContext,
|
|
114
|
-
name: string,
|
|
115
|
-
args: Record<string, unknown>,
|
|
116
|
-
rpcId: string | number | undefined,
|
|
117
|
-
handler: (requestId: string, traceId: string, phaseTimings: Partial<McpTimingTrace>) => Promise<CallToolResult>,
|
|
118
|
-
): Promise<CallToolResult> {
|
|
119
|
-
const startedAtWall = new Date().toISOString();
|
|
120
|
-
const startedAt = performance.now();
|
|
121
|
-
const traceId = randomUUID();
|
|
122
|
-
const requestId = recordRequestId(args, rpcId);
|
|
123
|
-
let outcome: 'ok' | 'error' | 'exception' = 'ok';
|
|
124
|
-
let errorCode: string | undefined;
|
|
125
|
-
let executionIdentity: ReturnType<typeof timingIdentity> = {};
|
|
126
|
-
const phaseTimings: Partial<McpTimingTrace> = {};
|
|
127
|
-
try {
|
|
128
|
-
const value = await handler(requestId, traceId, phaseTimings);
|
|
129
|
-
executionIdentity = timingIdentity(value);
|
|
130
|
-
if (value?.isError) {
|
|
131
|
-
outcome = 'error';
|
|
132
|
-
const error = value.structuredContent && typeof value.structuredContent === 'object'
|
|
133
|
-
? (value.structuredContent as Record<string, unknown>).error
|
|
134
|
-
: undefined;
|
|
135
|
-
if (error && typeof error === 'object' && !Array.isArray(error)) {
|
|
136
|
-
errorCode = typeof (error as Record<string, unknown>).code === 'string'
|
|
137
|
-
? (error as Record<string, unknown>).code as string
|
|
138
|
-
: 'MCP_TOOL_ERROR';
|
|
139
|
-
recordMcpIncident(ctx.controllerHome, {
|
|
140
|
-
traceId,
|
|
141
|
-
requestId,
|
|
142
|
-
...(rpcId === undefined ? {} : { rpcId }),
|
|
143
|
-
tool: name,
|
|
144
|
-
kind: 'tool_error',
|
|
145
|
-
code: errorCode,
|
|
146
|
-
message: typeof (error as Record<string, unknown>).message === 'string'
|
|
147
|
-
? (error as Record<string, unknown>).message as string
|
|
148
|
-
: 'MCP tool returned an error.',
|
|
149
|
-
...(typeof (value.structuredContent as Record<string, unknown>).repoId === 'string'
|
|
150
|
-
? { repoId: (value.structuredContent as Record<string, unknown>).repoId as string }
|
|
151
|
-
: {}),
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
const serverDurationMs = Math.round((performance.now() - startedAt) * 100) / 100;
|
|
156
|
-
return tracedResult(value, traceId, requestId, { startedAt: startedAtWall, durationMs: serverDurationMs }) as CallToolResult;
|
|
157
|
-
} catch (error) {
|
|
158
|
-
outcome = 'exception';
|
|
159
|
-
errorCode = 'MCP_REQUEST_EXCEPTION';
|
|
160
|
-
recordMcpIncident(ctx.controllerHome, {
|
|
161
|
-
traceId,
|
|
162
|
-
requestId,
|
|
163
|
-
...(rpcId === undefined ? {} : { rpcId }),
|
|
164
|
-
tool: name,
|
|
165
|
-
kind: 'exception',
|
|
166
|
-
code: errorCode,
|
|
167
|
-
message: error instanceof Error ? error.message : String(error),
|
|
168
|
-
});
|
|
169
|
-
throw error;
|
|
170
|
-
} finally {
|
|
171
|
-
recordMcpTiming(ctx.controllerHome, {
|
|
172
|
-
tool: name,
|
|
173
|
-
traceId,
|
|
174
|
-
requestId,
|
|
175
|
-
...(rpcId === undefined ? {} : { rpcId }),
|
|
176
|
-
layer: ctx.runtimeSourceRoot ? 'canonical_runtime' : 'public_gateway',
|
|
177
|
-
startedAt: startedAtWall,
|
|
178
|
-
outcome,
|
|
179
|
-
...(errorCode ? { errorCode } : {}),
|
|
180
|
-
...executionIdentity,
|
|
181
|
-
...phaseTimings,
|
|
182
|
-
totalToolDurationMs: Math.round((performance.now() - startedAt) * 100) / 100,
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
export function createMcpToolContext(
|
|
188
|
-
opts: McpServerOptions & { profile?: "controller" },
|
|
189
|
-
): MultiRepositoryMcpToolContext;
|
|
190
|
-
export function createMcpToolContext(opts: McpServerOptions): ServerToolContext;
|
|
191
|
-
export function createMcpToolContext(opts: McpServerOptions): ServerToolContext {
|
|
192
|
-
const profile = opts.profile ?? 'controller';
|
|
193
|
-
if (profile !== 'controller') return createLegacyMcpToolContext(opts);
|
|
194
|
-
const repo = opts.repo?.trim() === '.' ? undefined : opts.repo;
|
|
195
|
-
return createMultiRepositoryToolContext({ ...opts, repo });
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function canonicalRuntimeEndpoint(endpoint: string | undefined): URL {
|
|
199
|
-
if (!endpoint) throw new Error('CANONICAL_RUNTIME_ENDPOINT_UNAVAILABLE');
|
|
200
|
-
const url = new URL(endpoint);
|
|
201
|
-
const host = url.hostname.replace(/^\[|\]$/g, '');
|
|
202
|
-
if (!['127.0.0.1', 'localhost', '::1'].includes(host)) {
|
|
203
|
-
throw new Error(`CANONICAL_RUNTIME_ENDPOINT_NOT_LOOPBACK: ${url.origin}`);
|
|
204
|
-
}
|
|
205
|
-
return url;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function canonicalRuntimeToken(ctx: MultiRepositoryMcpToolContext): string {
|
|
209
|
-
const path = join(ctx.controllerHome, 'mcp', 'runtime-token');
|
|
210
|
-
const token = readFileSync(path, 'utf8').trim();
|
|
211
|
-
if (!token) throw new Error('CANONICAL_RUNTIME_TOKEN_UNAVAILABLE');
|
|
212
|
-
return token;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export const CANONICAL_RUNTIME_CONNECT_TIMEOUT_MS = 5_000;
|
|
216
|
-
/**
|
|
217
|
-
* The loopback connection should fail fast, but a valid Canonical Runtime tool
|
|
218
|
-
* call may legitimately outlive five seconds (for example Work finalization).
|
|
219
|
-
* Do not let the thin Public Gateway report a false timeout while authoritative
|
|
220
|
-
* work continues successfully behind it.
|
|
221
|
-
*/
|
|
222
|
-
export const CANONICAL_RUNTIME_TOOL_CALL_TIMEOUT_MS = 120_000;
|
|
223
|
-
export const CANONICAL_RUNTIME_HANDOFF_WAIT_MS = 20_000;
|
|
224
|
-
export const CANONICAL_RUNTIME_HANDOFF_RETRY_INTERVAL_MS = 150;
|
|
225
|
-
const CANONICAL_RUNTIME_HANDOFF_SIGNAL_TTL_MS = 90_000;
|
|
226
|
-
|
|
227
|
-
export interface CanonicalRuntimeReleaseHandoffObservation {
|
|
228
|
-
authorityReleaseId?: string;
|
|
229
|
-
authorityCommittedAt?: string;
|
|
230
|
-
runtimeReleaseId?: string;
|
|
231
|
-
runtimeRunning: boolean;
|
|
232
|
-
runtimeReady: boolean;
|
|
233
|
-
runtimeStartedAt?: string;
|
|
234
|
-
runtimeUpdatedAt?: string;
|
|
235
|
-
recoveryActivationInProgress?: boolean;
|
|
236
|
-
nowMs: number;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
function timestampIsRecent(value: string | undefined, nowMs: number, maxAgeMs: number): boolean {
|
|
240
|
-
if (!value) return false;
|
|
241
|
-
const observedAt = Date.parse(value);
|
|
242
|
-
if (!Number.isFinite(observedAt)) return false;
|
|
243
|
-
const ageMs = nowMs - observedAt;
|
|
244
|
-
return ageMs >= -5_000 && ageMs <= maxAgeMs;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Recognize only a bounded whole-Runtime release handoff. An old dead Runtime
|
|
249
|
-
* must not keep the public Gateway waiting forever, so every transition signal
|
|
250
|
-
* is time-bounded. This is observability, never release authority.
|
|
251
|
-
*/
|
|
252
|
-
export function canonicalRuntimeReleaseHandoffInProgress(
|
|
253
|
-
input: CanonicalRuntimeReleaseHandoffObservation,
|
|
254
|
-
): boolean {
|
|
255
|
-
if (!input.authorityReleaseId) return false;
|
|
256
|
-
const sameRelease = input.runtimeReleaseId === input.authorityReleaseId;
|
|
257
|
-
const authorityRecent = timestampIsRecent(
|
|
258
|
-
input.authorityCommittedAt,
|
|
259
|
-
input.nowMs,
|
|
260
|
-
CANONICAL_RUNTIME_HANDOFF_SIGNAL_TTL_MS,
|
|
261
|
-
);
|
|
262
|
-
|
|
263
|
-
// The release authority switches only after the old Runtime has completely
|
|
264
|
-
// stopped. Until the new Runtime publishes matching status, this mismatch is
|
|
265
|
-
// the strongest handoff signal. Missing status is equivalent to mismatch only
|
|
266
|
-
// while the authority commit is recent.
|
|
267
|
-
if (!sameRelease && authorityRecent) return true;
|
|
268
|
-
|
|
269
|
-
// The new Runtime owns the target release but has not completed scheduler +
|
|
270
|
-
// MCP end-to-end readiness yet.
|
|
271
|
-
if (sameRelease && input.runtimeRunning && !input.runtimeReady) {
|
|
272
|
-
return timestampIsRecent(
|
|
273
|
-
input.runtimeStartedAt ?? input.runtimeUpdatedAt,
|
|
274
|
-
input.nowMs,
|
|
275
|
-
CANONICAL_RUNTIME_HANDOFF_SIGNAL_TTL_MS,
|
|
276
|
-
);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
// Bridge the intentional bootout -> authority-publish gap only while the
|
|
280
|
-
// standalone Recovery transaction explicitly owns the activation lock. A
|
|
281
|
-
// freshly crashed Runtime without that lock is an outage and stays fail-fast.
|
|
282
|
-
if (sameRelease && !input.runtimeRunning) {
|
|
283
|
-
return input.recoveryActivationInProgress === true;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
return false;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
export interface CanonicalRuntimeHandoffWaitResult {
|
|
290
|
-
waited: boolean;
|
|
291
|
-
settled: boolean;
|
|
292
|
-
observations: number;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
export async function waitForCanonicalRuntimeReleaseHandoff(
|
|
296
|
-
handoffInProgress: () => boolean,
|
|
297
|
-
options: {
|
|
298
|
-
maxWaitMs?: number;
|
|
299
|
-
intervalMs?: number;
|
|
300
|
-
now?: () => number;
|
|
301
|
-
sleep?: (ms: number) => Promise<void>;
|
|
302
|
-
} = {},
|
|
303
|
-
): Promise<CanonicalRuntimeHandoffWaitResult> {
|
|
304
|
-
const maxWaitMs = Math.max(0, options.maxWaitMs ?? CANONICAL_RUNTIME_HANDOFF_WAIT_MS);
|
|
305
|
-
const intervalMs = Math.max(1, options.intervalMs ?? CANONICAL_RUNTIME_HANDOFF_RETRY_INTERVAL_MS);
|
|
306
|
-
const now = options.now ?? Date.now;
|
|
307
|
-
const sleep = options.sleep ?? ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)));
|
|
308
|
-
let observations = 1;
|
|
309
|
-
if (!handoffInProgress()) return { waited: false, settled: true, observations };
|
|
310
|
-
const deadline = now() + maxWaitMs;
|
|
311
|
-
let waited = false;
|
|
312
|
-
while (now() < deadline) {
|
|
313
|
-
waited = true;
|
|
314
|
-
await sleep(Math.min(intervalMs, Math.max(1, deadline - now())));
|
|
315
|
-
observations += 1;
|
|
316
|
-
if (!handoffInProgress()) return { waited, settled: true, observations };
|
|
317
|
-
}
|
|
318
|
-
observations += 1;
|
|
319
|
-
return { waited, settled: !handoffInProgress(), observations };
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
export async function retryCanonicalRuntimeConnectDuringHandoff<T>(
|
|
323
|
-
connect: () => Promise<T>,
|
|
324
|
-
handoffInProgress: () => boolean,
|
|
325
|
-
options: Parameters<typeof waitForCanonicalRuntimeReleaseHandoff>[1] = {},
|
|
326
|
-
): Promise<T> {
|
|
327
|
-
const initialWait = await waitForCanonicalRuntimeReleaseHandoff(handoffInProgress, options);
|
|
328
|
-
if (initialWait.waited && !initialWait.settled) {
|
|
329
|
-
// Preserve the ordinary connect failure shape after the bounded handoff
|
|
330
|
-
// window rather than manufacturing a new proxy-only error.
|
|
331
|
-
return await connect();
|
|
332
|
-
}
|
|
333
|
-
try {
|
|
334
|
-
return await connect();
|
|
335
|
-
} catch (error) {
|
|
336
|
-
// Retrying is safe only because connect() has not dispatched a tool call.
|
|
337
|
-
// Once client.callTool() begins, callers must never use this helper.
|
|
338
|
-
if (!handoffInProgress()) throw error;
|
|
339
|
-
const waited = await waitForCanonicalRuntimeReleaseHandoff(handoffInProgress, options);
|
|
340
|
-
if (!waited.settled) throw error;
|
|
341
|
-
return await connect();
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
function recoveryRuntimeReleaseActivationInProgress(controllerHome: string): boolean {
|
|
346
|
-
try {
|
|
347
|
-
const lock = JSON.parse(readFileSync(join(controllerHome, 'recovery', 'locks', 'operation.lock'), 'utf8')) as {
|
|
348
|
-
action?: string;
|
|
349
|
-
acquiredAt?: string;
|
|
350
|
-
};
|
|
351
|
-
if (lock.action !== 'activate_runtime_release' && lock.action !== 'stage_and_activate_runtime_release') return false;
|
|
352
|
-
return timestampIsRecent(lock.acquiredAt, Date.now(), CANONICAL_RUNTIME_HANDOFF_SIGNAL_TTL_MS);
|
|
353
|
-
} catch {
|
|
354
|
-
return false;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
function canonicalRuntimeReleaseHandoffActive(controllerHome: string): boolean {
|
|
359
|
-
const runtime = observeRuntimeStatus(controllerHome);
|
|
360
|
-
if (runtime.running && runtime.ready) return false;
|
|
361
|
-
const authority = readRuntimeReleaseAuthority(controllerHome);
|
|
362
|
-
return canonicalRuntimeReleaseHandoffInProgress({
|
|
363
|
-
authorityReleaseId: authority?.active.releaseId,
|
|
364
|
-
authorityCommittedAt: authority?.committedAt,
|
|
365
|
-
runtimeReleaseId: runtime.snapshot?.releaseId,
|
|
366
|
-
runtimeRunning: runtime.running,
|
|
367
|
-
runtimeReady: runtime.ready,
|
|
368
|
-
runtimeStartedAt: runtime.snapshot?.startedAt,
|
|
369
|
-
runtimeUpdatedAt: runtime.snapshot?.updatedAt,
|
|
370
|
-
recoveryActivationInProgress: recoveryRuntimeReleaseActivationInProgress(controllerHome),
|
|
371
|
-
nowMs: Date.now(),
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
const CANONICAL_RUNTIME_FORWARD_META_KEY = 'forgeRuntimeForwarding';
|
|
377
|
-
|
|
378
|
-
type ForwardedControllerType = NonNullable<McpToolContext['controllerType']>;
|
|
379
|
-
|
|
380
|
-
interface CanonicalRuntimeForwardingIdentity {
|
|
381
|
-
principalId?: string;
|
|
382
|
-
sessionId?: string;
|
|
383
|
-
controllerType?: ForwardedControllerType;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
function boundedForwardedIdentity(value: unknown): string | undefined {
|
|
387
|
-
if (typeof value !== 'string') return undefined;
|
|
388
|
-
const normalized = value.trim();
|
|
389
|
-
return normalized ? normalized.slice(0, 512) : undefined;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
export function canonicalRuntimeForwardingIdentity(meta: unknown): CanonicalRuntimeForwardingIdentity {
|
|
393
|
-
if (!meta || typeof meta !== 'object' || Array.isArray(meta)) return {};
|
|
394
|
-
const envelope = (meta as Record<string, unknown>)[CANONICAL_RUNTIME_FORWARD_META_KEY];
|
|
395
|
-
if (!envelope || typeof envelope !== 'object' || Array.isArray(envelope)) return {};
|
|
396
|
-
const record = envelope as Record<string, unknown>;
|
|
397
|
-
const controllerType = boundedForwardedIdentity(record.controllerType);
|
|
398
|
-
return {
|
|
399
|
-
principalId: boundedForwardedIdentity(record.principalId),
|
|
400
|
-
sessionId: boundedForwardedIdentity(record.sessionId),
|
|
401
|
-
...(controllerType && ['chatgpt', 'codex', 'claude', 'grok', 'human'].includes(controllerType)
|
|
402
|
-
? { controllerType: controllerType as ForwardedControllerType }
|
|
403
|
-
: {}),
|
|
404
|
-
};
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
function canonicalRuntimeForwardingMeta(ctx: MultiRepositoryMcpToolContext): Record<string, unknown> {
|
|
408
|
-
return {
|
|
409
|
-
[CANONICAL_RUNTIME_FORWARD_META_KEY]: {
|
|
410
|
-
...(ctx.principalId?.trim() ? { principalId: ctx.principalId.trim() } : {}),
|
|
411
|
-
...(ctx.sessionId?.trim() ? { sessionId: ctx.sessionId.trim() } : {}),
|
|
412
|
-
...(ctx.controllerType ? { controllerType: ctx.controllerType } : {}),
|
|
413
|
-
},
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
function canonicalRuntimeRequestContext(baseContext: ServerToolContext, meta: unknown): ServerToolContext {
|
|
418
|
-
if (!isMultiRepositoryContext(baseContext) || !baseContext.runtimeSourceRoot) return baseContext;
|
|
419
|
-
const forwarded = canonicalRuntimeForwardingIdentity(meta);
|
|
420
|
-
return {
|
|
421
|
-
...baseContext,
|
|
422
|
-
...(forwarded.principalId ? { principalId: forwarded.principalId } : {}),
|
|
423
|
-
...(forwarded.sessionId ? { sessionId: forwarded.sessionId } : {}),
|
|
424
|
-
...(forwarded.controllerType ? { controllerType: forwarded.controllerType } : {}),
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
export interface CanonicalRuntimeProxyIdentity {
|
|
429
|
-
endpoint: URL;
|
|
430
|
-
token: string;
|
|
431
|
-
runtimeInstanceId: string;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
function canonicalRuntimeProxyIdentity(ctx: MultiRepositoryMcpToolContext): CanonicalRuntimeProxyIdentity {
|
|
435
|
-
const snapshot = observeRuntimeStatus(ctx.controllerHome).snapshot;
|
|
436
|
-
if (!snapshot?.runtimeInstanceId) throw new Error('CANONICAL_RUNTIME_IDENTITY_UNAVAILABLE');
|
|
437
|
-
return {
|
|
438
|
-
endpoint: canonicalRuntimeEndpoint(snapshot.endpoint),
|
|
439
|
-
token: canonicalRuntimeToken(ctx),
|
|
440
|
-
runtimeInstanceId: snapshot.runtimeInstanceId,
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
export function sameCanonicalRuntimeProxyIdentity(
|
|
445
|
-
left: CanonicalRuntimeProxyIdentity,
|
|
446
|
-
right: CanonicalRuntimeProxyIdentity,
|
|
447
|
-
): boolean {
|
|
448
|
-
return left.endpoint.href === right.endpoint.href
|
|
449
|
-
&& left.token === right.token
|
|
450
|
-
&& left.runtimeInstanceId === right.runtimeInstanceId;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
export function canonicalRuntimeToolCallIsReplaySafe(name: string, args: Record<string, unknown>): boolean {
|
|
454
|
-
if (name !== 'repository_command_execute') return false;
|
|
455
|
-
const workId = typeof args.work_id === 'string' ? args.work_id.trim() : '';
|
|
456
|
-
const requestId = typeof args.request_id === 'string' ? args.request_id.trim() : '';
|
|
457
|
-
return Boolean(workId && requestId);
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
export function canonicalRuntimeToolCallFailureIsTransient(error: unknown): boolean {
|
|
461
|
-
const message = error instanceof Error ? `${error.name}: ${error.message}` : String(error ?? '');
|
|
462
|
-
return /Connection closed|socket[^\n]*closed|ECONNRESET|ECONNREFUSED|ETIMEDOUT|EPIPE|UND_ERR_SOCKET|fetch failed|MCP_REQUEST_FAILED/i.test(message);
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
export async function callCanonicalRuntimeToolWithReplay<T>(input: {
|
|
466
|
-
name: string;
|
|
467
|
-
args: Record<string, unknown>;
|
|
468
|
-
call: () => Promise<T>;
|
|
469
|
-
reconnect: () => Promise<void>;
|
|
470
|
-
}): Promise<T> {
|
|
471
|
-
try {
|
|
472
|
-
return await input.call();
|
|
473
|
-
} catch (error) {
|
|
474
|
-
if (!canonicalRuntimeToolCallIsReplaySafe(input.name, input.args)
|
|
475
|
-
|| !canonicalRuntimeToolCallFailureIsTransient(error)) {
|
|
476
|
-
throw error;
|
|
477
|
-
}
|
|
478
|
-
await input.reconnect();
|
|
479
|
-
return await input.call();
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
export interface CanonicalRuntimeProxy {
|
|
484
|
-
listTools(): Promise<{ tools: Tool[] }>;
|
|
485
|
-
callTool(ctx: MultiRepositoryMcpToolContext, name: string, args: Record<string, unknown>, timing?: MutableGatewayProxyTiming): Promise<CallToolResult>;
|
|
486
|
-
close(): Promise<void>;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
/**
|
|
490
|
-
* The public Gateway has no schema cache authority. It obtains each Connector
|
|
491
|
-
* session's schema from the Runtime that will execute its calls. When the HTTP
|
|
492
|
-
* Gateway owns a shared proxy, discovery and tool calls reuse the same loopback
|
|
493
|
-
* MCP session instead of paying an initialize/connect round trip per outer
|
|
494
|
-
* Connector session.
|
|
495
|
-
*/
|
|
496
|
-
export async function readCanonicalRuntimeToolSchema(
|
|
497
|
-
ctx: MultiRepositoryMcpToolContext,
|
|
498
|
-
sharedProxy?: CanonicalRuntimeProxy,
|
|
499
|
-
): Promise<CanonicalRuntimeToolSchema> {
|
|
500
|
-
const proxy = sharedProxy ?? createCanonicalRuntimeProxy(ctx);
|
|
501
|
-
try {
|
|
502
|
-
const response = await proxy.listTools();
|
|
503
|
-
const definitions = response.tools as Tool[];
|
|
504
|
-
const toolNames = definitions
|
|
505
|
-
.map((tool) => tool.name)
|
|
506
|
-
.filter((name): name is string => typeof name === 'string' && name.length > 0)
|
|
507
|
-
.sort();
|
|
508
|
-
return {
|
|
509
|
-
definitions,
|
|
510
|
-
toolNames,
|
|
511
|
-
fingerprint: forgeToolSurfaceFingerprint(definitions),
|
|
512
|
-
};
|
|
513
|
-
} finally {
|
|
514
|
-
if (!sharedProxy) await proxy.close();
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
type GatewayProxyTiming = Pick<McpTimingTrace,
|
|
519
|
-
| 'gatewayProxyResolveMs'
|
|
520
|
-
| 'gatewayProxyConnectMs'
|
|
521
|
-
| 'gatewayProxyCallMs'
|
|
522
|
-
| 'gatewayProxyCanonicalDispatchLagMs'
|
|
523
|
-
| 'gatewayProxyCanonicalDurationMs'
|
|
524
|
-
| 'gatewayProxyReturnMs'
|
|
525
|
-
| 'gatewayProxyConnectionState'
|
|
526
|
-
>;
|
|
527
|
-
|
|
528
|
-
type MutableGatewayProxyTiming = Partial<GatewayProxyTiming>;
|
|
529
|
-
|
|
530
|
-
function roundedMs(value: number): number {
|
|
531
|
-
return Math.round(Math.max(0, value) * 100) / 100;
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
export function deriveCanonicalForwardingTiming(input: {
|
|
535
|
-
gatewayCallStartedAtMs: number;
|
|
536
|
-
gatewayCallDurationMs: number;
|
|
537
|
-
response: CallToolResult;
|
|
538
|
-
}): Pick<GatewayProxyTiming, 'gatewayProxyCanonicalDispatchLagMs' | 'gatewayProxyCanonicalDurationMs' | 'gatewayProxyReturnMs'> {
|
|
539
|
-
const structured = input.response.structuredContent;
|
|
540
|
-
const responseMeta = structured && typeof structured === 'object' && !Array.isArray(structured)
|
|
541
|
-
&& (structured as Record<string, unknown>).responseMeta
|
|
542
|
-
&& typeof (structured as Record<string, unknown>).responseMeta === 'object'
|
|
543
|
-
? (structured as Record<string, unknown>).responseMeta as Record<string, unknown>
|
|
544
|
-
: undefined;
|
|
545
|
-
const canonicalStartedAt = typeof responseMeta?.serverStartedAt === 'string'
|
|
546
|
-
? Date.parse(responseMeta.serverStartedAt)
|
|
547
|
-
: Number.NaN;
|
|
548
|
-
const canonicalDurationMs = typeof responseMeta?.serverDurationMs === 'number'
|
|
549
|
-
? responseMeta.serverDurationMs
|
|
550
|
-
: Number.NaN;
|
|
551
|
-
if (!Number.isFinite(canonicalStartedAt) || !Number.isFinite(canonicalDurationMs)) return {};
|
|
552
|
-
const dispatchLagMs = roundedMs(canonicalStartedAt - input.gatewayCallStartedAtMs);
|
|
553
|
-
const boundedCanonicalDurationMs = roundedMs(canonicalDurationMs);
|
|
554
|
-
return {
|
|
555
|
-
gatewayProxyCanonicalDispatchLagMs: dispatchLagMs,
|
|
556
|
-
gatewayProxyCanonicalDurationMs: boundedCanonicalDurationMs,
|
|
557
|
-
gatewayProxyReturnMs: roundedMs(input.gatewayCallDurationMs - dispatchLagMs - boundedCanonicalDurationMs),
|
|
558
|
-
};
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
export const DEFAULT_CANONICAL_RUNTIME_PROXY_LANES = 8;
|
|
562
|
-
export const MAX_CANONICAL_RUNTIME_PROXY_LANES = 16;
|
|
563
|
-
|
|
564
|
-
export function canonicalRuntimeProxyLaneLimit(raw = process.env.FORGE_CANONICAL_RUNTIME_PROXY_LANES): number {
|
|
565
|
-
const parsed = Number(raw);
|
|
566
|
-
if (!Number.isInteger(parsed) || parsed < 1) return DEFAULT_CANONICAL_RUNTIME_PROXY_LANES;
|
|
567
|
-
return Math.min(parsed, MAX_CANONICAL_RUNTIME_PROXY_LANES);
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
export type CanonicalRuntimeLaneClass = 'interactive' | 'wait';
|
|
571
|
-
|
|
572
|
-
export interface CanonicalRuntimeLaneScheduler {
|
|
573
|
-
acquire(laneClass?: CanonicalRuntimeLaneClass): Promise<number>;
|
|
574
|
-
release(laneId: number): void;
|
|
575
|
-
close(): void;
|
|
576
|
-
size(): number;
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
export const RESERVED_INTERACTIVE_CANONICAL_RUNTIME_PROXY_LANES = 2;
|
|
580
|
-
|
|
581
|
-
/**
|
|
582
|
-
* Lease one inner Runtime MCP session exclusively to each in-flight request.
|
|
583
|
-
* Long process_wait calls are capped below total capacity so they can never
|
|
584
|
-
* consume every lane. Interactive waiters are handed released lanes first.
|
|
585
|
-
*/
|
|
586
|
-
export function createCanonicalRuntimeLaneScheduler(maxLanes = canonicalRuntimeProxyLaneLimit()): CanonicalRuntimeLaneScheduler {
|
|
587
|
-
const boundedMax = Math.max(1, Math.min(Math.trunc(maxLanes), MAX_CANONICAL_RUNTIME_PROXY_LANES));
|
|
588
|
-
const maxWaitLanes = boundedMax <= RESERVED_INTERACTIVE_CANONICAL_RUNTIME_PROXY_LANES
|
|
589
|
-
? Math.max(1, boundedMax - 1)
|
|
590
|
-
: boundedMax - RESERVED_INTERACTIVE_CANONICAL_RUNTIME_PROXY_LANES;
|
|
591
|
-
const idle: number[] = [];
|
|
592
|
-
const waiters: Array<{ laneClass: CanonicalRuntimeLaneClass; resolve: (laneId: number) => void; reject: (error: Error) => void }> = [];
|
|
593
|
-
const activeClasses = new Map<number, CanonicalRuntimeLaneClass>();
|
|
594
|
-
let waitLaneCount = 0;
|
|
595
|
-
let laneCount = 0;
|
|
596
|
-
let closed = false;
|
|
597
|
-
|
|
598
|
-
const activate = (laneId: number, laneClass: CanonicalRuntimeLaneClass): number => {
|
|
599
|
-
activeClasses.set(laneId, laneClass);
|
|
600
|
-
if (laneClass === 'wait') waitLaneCount += 1;
|
|
601
|
-
return laneId;
|
|
602
|
-
};
|
|
603
|
-
|
|
604
|
-
const nextEligibleWaiterIndex = (): number => {
|
|
605
|
-
const interactiveIndex = waiters.findIndex((waiter) => waiter.laneClass === 'interactive');
|
|
606
|
-
if (interactiveIndex >= 0) return interactiveIndex;
|
|
607
|
-
return waitLaneCount < maxWaitLanes ? waiters.findIndex((waiter) => waiter.laneClass === 'wait') : -1;
|
|
608
|
-
};
|
|
609
|
-
|
|
610
|
-
return {
|
|
611
|
-
async acquire(laneClass: CanonicalRuntimeLaneClass = 'interactive'): Promise<number> {
|
|
612
|
-
if (closed) throw new Error('CANONICAL_RUNTIME_PROXY_CLOSED');
|
|
613
|
-
if (laneClass === 'wait' && waitLaneCount >= maxWaitLanes) {
|
|
614
|
-
return await new Promise<number>((resolve, reject) => waiters.push({ laneClass, resolve, reject }));
|
|
615
|
-
}
|
|
616
|
-
const available = idle.shift();
|
|
617
|
-
if (available !== undefined) return activate(available, laneClass);
|
|
618
|
-
if (laneCount < boundedMax) return activate(laneCount++, laneClass);
|
|
619
|
-
return await new Promise<number>((resolve, reject) => waiters.push({ laneClass, resolve, reject }));
|
|
620
|
-
},
|
|
621
|
-
release(laneId: number): void {
|
|
622
|
-
if (closed) return;
|
|
623
|
-
const previousClass = activeClasses.get(laneId);
|
|
624
|
-
activeClasses.delete(laneId);
|
|
625
|
-
if (previousClass === 'wait') waitLaneCount = Math.max(0, waitLaneCount - 1);
|
|
626
|
-
const waiterIndex = nextEligibleWaiterIndex();
|
|
627
|
-
if (waiterIndex >= 0) {
|
|
628
|
-
const [waiter] = waiters.splice(waiterIndex, 1);
|
|
629
|
-
waiter.resolve(activate(laneId, waiter.laneClass));
|
|
630
|
-
return;
|
|
631
|
-
}
|
|
632
|
-
idle.push(laneId);
|
|
633
|
-
},
|
|
634
|
-
close(): void {
|
|
635
|
-
if (closed) return;
|
|
636
|
-
closed = true;
|
|
637
|
-
const error = new Error('CANONICAL_RUNTIME_PROXY_CLOSED');
|
|
638
|
-
for (const waiter of waiters.splice(0)) waiter.reject(error);
|
|
639
|
-
activeClasses.clear();
|
|
640
|
-
idle.length = 0;
|
|
641
|
-
},
|
|
642
|
-
size(): number {
|
|
643
|
-
return laneCount;
|
|
644
|
-
},
|
|
645
|
-
};
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
interface CanonicalRuntimeProxyLane {
|
|
649
|
-
current?: { identity: CanonicalRuntimeProxyIdentity; client: Client };
|
|
650
|
-
connecting?: Promise<{ identity: CanonicalRuntimeProxyIdentity; client: Client }>;
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
export function createCanonicalRuntimeProxy(ctx: MultiRepositoryMcpToolContext): CanonicalRuntimeProxy {
|
|
654
|
-
const scheduler = createCanonicalRuntimeLaneScheduler();
|
|
655
|
-
const lanes = new Map<number, CanonicalRuntimeProxyLane>();
|
|
656
|
-
let closed = false;
|
|
657
|
-
|
|
658
|
-
const laneState = (laneId: number): CanonicalRuntimeProxyLane => {
|
|
659
|
-
const existing = lanes.get(laneId);
|
|
660
|
-
if (existing) return existing;
|
|
661
|
-
const created: CanonicalRuntimeProxyLane = {};
|
|
662
|
-
lanes.set(laneId, created);
|
|
663
|
-
return created;
|
|
664
|
-
};
|
|
665
|
-
|
|
666
|
-
const closeLane = async (laneId: number, expectedClient?: Client): Promise<void> => {
|
|
667
|
-
const lane = lanes.get(laneId);
|
|
668
|
-
if (!lane) return;
|
|
669
|
-
if (lane.connecting) {
|
|
670
|
-
await lane.connecting.catch(() => undefined);
|
|
671
|
-
lane.connecting = undefined;
|
|
672
|
-
}
|
|
673
|
-
if (!lane.current || (expectedClient && lane.current.client !== expectedClient)) return;
|
|
674
|
-
const closing = lane.current.client;
|
|
675
|
-
lane.current = undefined;
|
|
676
|
-
await closing.close().catch(() => undefined);
|
|
677
|
-
};
|
|
678
|
-
|
|
679
|
-
const closeAllLanes = async (): Promise<void> => {
|
|
680
|
-
await Promise.all(Array.from(lanes.keys()).map(async (laneId) => await closeLane(laneId)));
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
const connect = async (identity: CanonicalRuntimeProxyIdentity): Promise<{ identity: CanonicalRuntimeProxyIdentity; client: Client }> => {
|
|
684
|
-
const connectOnce = async (): Promise<{ identity: CanonicalRuntimeProxyIdentity; client: Client }> => {
|
|
685
|
-
const abort = new AbortController();
|
|
686
|
-
const timeout = setTimeout(() => abort.abort(new Error('CANONICAL_RUNTIME_TIMEOUT')), CANONICAL_RUNTIME_CONNECT_TIMEOUT_MS);
|
|
687
|
-
const headers: Record<string, string> = {
|
|
688
|
-
Authorization: `Bearer ${identity.token}`,
|
|
689
|
-
};
|
|
690
|
-
const transport = new StreamableHTTPClientTransport(identity.endpoint, {
|
|
691
|
-
requestInit: { headers, signal: abort.signal },
|
|
692
|
-
});
|
|
693
|
-
const client = new Client({ name: 'forge-public-gateway-proxy', version: '1.0.0' });
|
|
694
|
-
try {
|
|
695
|
-
await client.connect(transport);
|
|
696
|
-
return { identity, client };
|
|
697
|
-
} catch (error) {
|
|
698
|
-
await client.close().catch(() => undefined);
|
|
699
|
-
throw error;
|
|
700
|
-
} finally {
|
|
701
|
-
clearTimeout(timeout);
|
|
702
|
-
}
|
|
703
|
-
};
|
|
704
|
-
return await retryCanonicalRuntimeConnectDuringHandoff(
|
|
705
|
-
connectOnce,
|
|
706
|
-
() => canonicalRuntimeReleaseHandoffActive(ctx.controllerHome),
|
|
707
|
-
);
|
|
708
|
-
};
|
|
709
|
-
|
|
710
|
-
const clientForCurrentRuntime = async (laneId: number, timing: MutableGatewayProxyTiming): Promise<Client> => {
|
|
711
|
-
const lane = laneState(laneId);
|
|
712
|
-
const resolveStarted = performance.now();
|
|
713
|
-
const identity = canonicalRuntimeProxyIdentity(ctx);
|
|
714
|
-
timing.gatewayProxyResolveMs = roundedMs(performance.now() - resolveStarted);
|
|
715
|
-
if (lane.current && sameCanonicalRuntimeProxyIdentity(lane.current.identity, identity)) {
|
|
716
|
-
timing.gatewayProxyConnectionState = 'reused';
|
|
717
|
-
timing.gatewayProxyConnectMs = 0;
|
|
718
|
-
return lane.current.client;
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
if (lane.connecting) {
|
|
722
|
-
const connectStarted = performance.now();
|
|
723
|
-
const pending = await lane.connecting;
|
|
724
|
-
timing.gatewayProxyConnectMs = roundedMs(performance.now() - connectStarted);
|
|
725
|
-
if (sameCanonicalRuntimeProxyIdentity(pending.identity, identity)) {
|
|
726
|
-
timing.gatewayProxyConnectionState = 'coalesced_connect';
|
|
727
|
-
return pending.client;
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
const reconnect = Boolean(lane.current);
|
|
732
|
-
await closeLane(laneId);
|
|
733
|
-
const connectStarted = performance.now();
|
|
734
|
-
lane.connecting = connect(identity);
|
|
735
|
-
try {
|
|
736
|
-
lane.current = await lane.connecting;
|
|
737
|
-
timing.gatewayProxyConnectMs = roundedMs(performance.now() - connectStarted);
|
|
738
|
-
timing.gatewayProxyConnectionState = reconnect ? 'identity_reconnect' : 'cold_connect';
|
|
739
|
-
return lane.current.client;
|
|
740
|
-
} finally {
|
|
741
|
-
lane.connecting = undefined;
|
|
742
|
-
}
|
|
743
|
-
};
|
|
744
|
-
|
|
745
|
-
const prepareLane = async (
|
|
746
|
-
timing: MutableGatewayProxyTiming,
|
|
747
|
-
laneClass: CanonicalRuntimeLaneClass = 'interactive',
|
|
748
|
-
): Promise<{ laneId: number; client: Client }> => {
|
|
749
|
-
if (closed) throw new Error('CANONICAL_RUNTIME_PROXY_CLOSED');
|
|
750
|
-
const handoff = await waitForCanonicalRuntimeReleaseHandoff(
|
|
751
|
-
() => canonicalRuntimeReleaseHandoffActive(ctx.controllerHome),
|
|
752
|
-
);
|
|
753
|
-
if (handoff.waited) await closeAllLanes();
|
|
754
|
-
const laneId = await scheduler.acquire(laneClass);
|
|
755
|
-
try {
|
|
756
|
-
return { laneId, client: await clientForCurrentRuntime(laneId, timing) };
|
|
757
|
-
} catch (error) {
|
|
758
|
-
scheduler.release(laneId);
|
|
759
|
-
throw error;
|
|
760
|
-
}
|
|
761
|
-
};
|
|
762
|
-
|
|
763
|
-
return {
|
|
764
|
-
async listTools() {
|
|
765
|
-
const { laneId, client } = await prepareLane({});
|
|
766
|
-
try {
|
|
767
|
-
return await client.listTools() as { tools: Tool[] };
|
|
768
|
-
} catch (error) {
|
|
769
|
-
await closeLane(laneId, client);
|
|
770
|
-
throw error;
|
|
771
|
-
} finally {
|
|
772
|
-
scheduler.release(laneId);
|
|
773
|
-
}
|
|
774
|
-
},
|
|
775
|
-
async callTool(callerContext, name, args, timing = {}) {
|
|
776
|
-
const prepared = await prepareLane(timing, name === 'process_wait' ? 'wait' : 'interactive');
|
|
777
|
-
const laneId = prepared.laneId;
|
|
778
|
-
let activeClient = prepared.client;
|
|
779
|
-
const gatewayCallStartedAtMs = Date.now();
|
|
780
|
-
const callStarted = performance.now();
|
|
781
|
-
const forwardedRequest = {
|
|
782
|
-
name,
|
|
783
|
-
arguments: args,
|
|
784
|
-
_meta: canonicalRuntimeForwardingMeta(callerContext),
|
|
785
|
-
} as Parameters<Client['callTool']>[0];
|
|
786
|
-
try {
|
|
787
|
-
// The outer Connector session is already bound to the Canonical Runtime
|
|
788
|
-
// schema and fenced by the Runtime-published fingerprint before dispatch.
|
|
789
|
-
// Caller identity stays request-scoped while the inner MCP lane is reused.
|
|
790
|
-
// Only keyed Work-attributed Process Runtime commands may replay after a
|
|
791
|
-
// transient inner disconnect: the stable request_id resolves to the
|
|
792
|
-
// original Process instead of spawning the side effect twice.
|
|
793
|
-
const response = await callCanonicalRuntimeToolWithReplay({
|
|
794
|
-
name,
|
|
795
|
-
args,
|
|
796
|
-
call: async () => await activeClient.callTool(
|
|
797
|
-
forwardedRequest,
|
|
798
|
-
undefined,
|
|
799
|
-
{ timeout: CANONICAL_RUNTIME_TOOL_CALL_TIMEOUT_MS },
|
|
800
|
-
),
|
|
801
|
-
reconnect: async () => {
|
|
802
|
-
await closeLane(laneId, activeClient);
|
|
803
|
-
activeClient = await clientForCurrentRuntime(laneId, timing);
|
|
804
|
-
},
|
|
805
|
-
});
|
|
806
|
-
const result = response as unknown as CallToolResult;
|
|
807
|
-
timing.gatewayProxyCallMs = roundedMs(performance.now() - callStarted);
|
|
808
|
-
Object.assign(timing, deriveCanonicalForwardingTiming({
|
|
809
|
-
gatewayCallStartedAtMs,
|
|
810
|
-
gatewayCallDurationMs: timing.gatewayProxyCallMs,
|
|
811
|
-
response: result,
|
|
812
|
-
}));
|
|
813
|
-
return result;
|
|
814
|
-
} catch (error) {
|
|
815
|
-
timing.gatewayProxyCallMs = roundedMs(performance.now() - callStarted);
|
|
816
|
-
// Poison only the failed lane. Other hot lanes remain available so one
|
|
817
|
-
// Runtime/session failure cannot collapse the whole Gateway proxy pool.
|
|
818
|
-
await closeLane(laneId, activeClient);
|
|
819
|
-
throw error;
|
|
820
|
-
} finally {
|
|
821
|
-
scheduler.release(laneId);
|
|
822
|
-
}
|
|
823
|
-
},
|
|
824
|
-
async close() {
|
|
825
|
-
if (closed) return;
|
|
826
|
-
closed = true;
|
|
827
|
-
scheduler.close();
|
|
828
|
-
await closeAllLanes();
|
|
829
|
-
},
|
|
830
|
-
};
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
function canonicalRuntimeSchemaMatchesGateway(ctx: MultiRepositoryMcpToolContext): boolean {
|
|
834
|
-
const runtimeFingerprint = observeRuntimeStatus(ctx.controllerHome).snapshot?.toolSurfaceFingerprint;
|
|
835
|
-
// Older Runtime status records do not have a schema fence. Preserve a
|
|
836
|
-
// diagnosable compatibility path until the Runtime has been upgraded.
|
|
837
|
-
return !runtimeFingerprint || runtimeFingerprint === controllerExposureSnapshot(ctx).fingerprint;
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
function toolSurfaceMismatchResult(): CallToolResult {
|
|
841
|
-
const value = {
|
|
842
|
-
error: {
|
|
843
|
-
code: 'MCP_TOOL_SURFACE_MISMATCH',
|
|
844
|
-
message: 'Gateway and Canonical Runtime tool surfaces differ. The session must reinitialize against a matching Runtime release.',
|
|
845
|
-
recoverable: true,
|
|
846
|
-
action: 'reinitialize',
|
|
847
|
-
},
|
|
848
|
-
};
|
|
849
|
-
return { content: [{ type: 'text', text: JSON.stringify(value) }], structuredContent: value, isError: true };
|
|
850
|
-
}
|
|
851
|
-
|
|
852
|
-
export function createForgeMcpServerFromContext(
|
|
853
|
-
baseContext: ServerToolContext,
|
|
854
|
-
runtimeSchema?: CanonicalRuntimeToolSchema,
|
|
855
|
-
sharedRuntimeProxy?: CanonicalRuntimeProxy,
|
|
856
|
-
): Server {
|
|
857
|
-
const server = new Server(
|
|
858
|
-
{ name: 'forge-mcp', version: FORGE_VERSION },
|
|
859
|
-
{ capabilities: { tools: { listChanged: true } }, instructions: mcpServerInstructions(baseContext.policy.profile) },
|
|
860
|
-
);
|
|
861
|
-
const ownsRuntimeProxy = isMultiRepositoryContext(baseContext) && !getRuntimeWriteClaim() && !sharedRuntimeProxy;
|
|
862
|
-
const runtimeProxy = isMultiRepositoryContext(baseContext) && !getRuntimeWriteClaim()
|
|
863
|
-
? sharedRuntimeProxy ?? createCanonicalRuntimeProxy(baseContext)
|
|
864
|
-
: undefined;
|
|
865
|
-
server.onclose = () => {
|
|
866
|
-
if (ownsRuntimeProxy) void runtimeProxy?.close();
|
|
867
|
-
};
|
|
868
|
-
// The connector process can outlive the canonical Runtime release it proxies.
|
|
869
|
-
// Refresh tools after every MCP session initialization so clients do not keep
|
|
870
|
-
// a stale schema when the bounded Runtime surface changes behind the gateway.
|
|
871
|
-
server.oninitialized = () => {
|
|
872
|
-
void server.sendToolListChanged().catch(() => undefined);
|
|
873
|
-
};
|
|
874
|
-
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
875
|
-
if (isMultiRepositoryContext(baseContext) && !runtimeSchema && !getRuntimeWriteClaim() && !canonicalRuntimeSchemaMatchesGateway(baseContext)) {
|
|
876
|
-
throw new Error('MCP_TOOL_SURFACE_MISMATCH: Gateway source does not match the Canonical Runtime schema.');
|
|
877
|
-
}
|
|
878
|
-
return {
|
|
879
|
-
tools: runtimeSchema?.definitions ?? (isMultiRepositoryContext(baseContext)
|
|
880
|
-
? controllerExposureSnapshot(baseContext).definitions
|
|
881
|
-
: buildMcpToolDefinitions(baseContext.policy, { enableChatgptBrowser: baseContext.enableChatgptBrowser === true })),
|
|
882
|
-
};
|
|
883
|
-
});
|
|
884
|
-
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
|
885
|
-
const name = request.params.name;
|
|
886
|
-
const args = (request.params.arguments ?? {}) as Record<string, unknown>;
|
|
887
|
-
const forwardedBaseContext = canonicalRuntimeRequestContext(
|
|
888
|
-
baseContext,
|
|
889
|
-
(request.params as { _meta?: unknown })._meta,
|
|
890
|
-
);
|
|
891
|
-
const ctx: ServerToolContext = { ...forwardedBaseContext, signal: extra.signal };
|
|
892
|
-
if (isMultiRepositoryContext(ctx)) {
|
|
893
|
-
const rpcId = (request as unknown as { id?: unknown }).id;
|
|
894
|
-
return traceControllerMcpRequest(ctx, name, args, typeof rpcId === 'string' || typeof rpcId === 'number' ? rpcId : undefined, async (requestId, _traceId, phaseTimings) => {
|
|
895
|
-
// The public Gateway only exposes its stable facade schema. It may
|
|
896
|
-
// proxy execution, but it never discovers one Runtime schema and
|
|
897
|
-
// validates calls against another source checkout.
|
|
898
|
-
if (!getRuntimeWriteClaim() && !runtimeSchema && !canonicalRuntimeSchemaMatchesGateway(ctx)) {
|
|
899
|
-
return toolSurfaceMismatchResult();
|
|
900
|
-
}
|
|
901
|
-
if (runtimeSchema && !runtimeSchema.toolNames.includes(name)) {
|
|
902
|
-
return toolSurfaceMismatchResult();
|
|
903
|
-
}
|
|
904
|
-
if (!runtimeSchema && !isControllerToolExposed(ctx, name)) {
|
|
905
|
-
const value = {
|
|
906
|
-
error: {
|
|
907
|
-
code: 'TOOL_NOT_FOUND',
|
|
908
|
-
message: `${name} is not registered by this forge build. Tool availability is independent of Request vs Full Access.`,
|
|
909
|
-
},
|
|
910
|
-
};
|
|
911
|
-
return { content: [{ type: 'text', text: JSON.stringify(value, null, 2) }], structuredContent: value, isError: true };
|
|
912
|
-
}
|
|
913
|
-
// The public OAuth Gateway is intentionally thin. If this process does
|
|
914
|
-
// not own the Canonical Runtime write claim, forward the complete tool
|
|
915
|
-
// call to the loopback Runtime MCP endpoint. This prevents source-checkout
|
|
916
|
-
// Gateway processes from acquiring Process Runtime leases or evaluating
|
|
917
|
-
// Runtime source coherence against their own checkout.
|
|
918
|
-
if (!getRuntimeWriteClaim()) {
|
|
919
|
-
const forwardedArgs = typeof args.request_id === 'string' && args.request_id.trim()
|
|
920
|
-
? args
|
|
921
|
-
: { ...args, request_id: requestId };
|
|
922
|
-
if (runtimeProxy && runtimeSchema) return runtimeProxy.callTool(ctx, name, forwardedArgs, phaseTimings);
|
|
923
|
-
if (runtimeProxy && observeRuntimeStatus(ctx.controllerHome).ready) return runtimeProxy.callTool(ctx, name, forwardedArgs, phaseTimings);
|
|
924
|
-
}
|
|
925
|
-
const accessResult = callAccessTool(ctx, name, args);
|
|
926
|
-
if (accessResult) return accessResult;
|
|
927
|
-
const executionResult = await callExecutionTool(ctx, name, args);
|
|
928
|
-
if (executionResult) return executionResult;
|
|
929
|
-
const processResult = await callProcessTool(ctx, name, args);
|
|
930
|
-
if (processResult) return processResult;
|
|
931
|
-
if (isGatewayIsolatedTool(name)) {
|
|
932
|
-
const isolatedResult = await routeDurableMcpCall(ctx, name, args, { allowReadOnly: true, forceDurable: true });
|
|
933
|
-
if (isolatedResult) return isolatedResult;
|
|
934
|
-
}
|
|
935
|
-
const runtimeResult = await callRuntimeTool(ctx, name, args);
|
|
936
|
-
if (runtimeResult) return runtimeResult;
|
|
937
|
-
const durableResult = await routeDurableMcpCall(ctx, name, args);
|
|
938
|
-
if (durableResult) return durableResult;
|
|
939
|
-
const repositoryResult = await callRepositoryToolWithPostFinalizeAttribution(ctx.controllerHome, name, args, ctx);
|
|
940
|
-
if (repositoryResult) return repositoryResult;
|
|
941
|
-
return callMultiRepositoryTool(ctx, name, args);
|
|
942
|
-
});
|
|
943
|
-
}
|
|
944
|
-
return callMcpTool(ctx, name, args);
|
|
945
|
-
});
|
|
946
|
-
return server;
|
|
947
|
-
}
|
|
948
|
-
|
|
949
|
-
export function createForgeMcpServer(opts: McpServerOptions): Server {
|
|
950
|
-
return createForgeMcpServerFromContext(createMcpToolContext(opts));
|
|
951
|
-
}
|
|
1
|
+
/** @deprecated Kernel V2 compatibility shim. MCP adapter owns this surface. */
|
|
2
|
+
export * from '../../../adapters/mcp/server';
|