create-windy 0.6.12 → 0.6.14
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/dist/cli.js +572 -269
- package/package.json +1 -1
- package/template/.agents/skills/windy-business-module/SKILL.md +98 -70
- package/template/.windy-template.json +3 -3
- package/template/AGENTS.md +3 -6
- package/template/apps/agent-server/Dockerfile +2 -3
- package/template/apps/agent-server/README.md +38 -54
- package/template/apps/agent-server/pyproject.toml +0 -1
- package/template/apps/agent-server/src/southwind_agent_server/app.py +4 -2
- package/template/apps/agent-server/src/southwind_agent_server/attempt_context.py +191 -0
- package/template/apps/agent-server/src/southwind_agent_server/attempt_context_projection.py +203 -0
- package/template/apps/agent-server/src/southwind_agent_server/client_completion_contracts.py +61 -0
- package/template/apps/agent-server/src/southwind_agent_server/client_completion_governance.py +82 -0
- package/template/apps/agent-server/src/southwind_agent_server/client_completion_routes.py +121 -0
- package/template/apps/agent-server/src/southwind_agent_server/client_completion_runtime.py +158 -0
- package/template/apps/agent-server/src/southwind_agent_server/context_message_digest.py +24 -0
- package/template/apps/agent-server/src/southwind_agent_server/context_version_contracts.py +71 -0
- package/template/apps/agent-server/src/southwind_agent_server/context_version_port.py +307 -0
- package/template/apps/agent-server/src/southwind_agent_server/contracts.py +39 -0
- package/template/apps/agent-server/src/southwind_agent_server/deterministic.py +2 -0
- package/template/apps/agent-server/src/southwind_agent_server/engine.py +9 -1
- package/template/apps/agent-server/src/southwind_agent_server/engine_identity.py +2 -0
- package/template/apps/agent-server/src/southwind_agent_server/fingerprint.py +15 -0
- package/template/apps/agent-server/src/southwind_agent_server/health_routes.py +37 -3
- package/template/apps/agent-server/src/southwind_agent_server/openai_engine.py +46 -26
- package/template/apps/agent-server/src/southwind_agent_server/openai_engine_validation.py +32 -0
- package/template/apps/agent-server/src/southwind_agent_server/openai_provider.py +7 -0
- package/template/apps/agent-server/src/southwind_agent_server/postgres_report_insert.py +5 -2
- package/template/apps/agent-server/src/southwind_agent_server/postgres_report_queries.py +1 -0
- package/template/apps/agent-server/src/southwind_agent_server/postgres_report_store.py +6 -1
- package/template/apps/agent-server/src/southwind_agent_server/postgres_store.py +3 -2
- package/template/apps/agent-server/src/southwind_agent_server/postgres_store_backend.py +1 -0
- package/template/apps/agent-server/src/southwind_agent_server/provider_governance.py +21 -0
- package/template/apps/agent-server/src/southwind_agent_server/report_lifecycle.py +2 -1
- package/template/apps/agent-server/src/southwind_agent_server/report_presenter.py +1 -0
- package/template/apps/agent-server/src/southwind_agent_server/report_routes.py +7 -1
- package/template/apps/agent-server/src/southwind_agent_server/report_store.py +10 -56
- package/template/apps/agent-server/src/southwind_agent_server/report_store_protocol.py +75 -0
- package/template/apps/agent-server/src/southwind_agent_server/report_types.py +9 -2
- package/template/apps/agent-server/src/southwind_agent_server/runtime_budget.py +74 -0
- package/template/apps/agent-server/src/southwind_agent_server/runtime_factory.py +11 -3
- package/template/apps/agent-server/src/southwind_agent_server/service.py +41 -65
- package/template/apps/agent-server/src/southwind_agent_server/service_control.py +64 -0
- package/template/apps/agent-server/src/southwind_agent_server/service_execution.py +3 -0
- package/template/apps/agent-server/src/southwind_agent_server/sqlite_errors.py +2 -0
- package/template/apps/agent-server/src/southwind_agent_server/sqlite_schema.py +9 -1
- package/template/apps/agent-server/src/southwind_agent_server/sqlite_store.py +6 -5
- package/template/apps/agent-server/src/southwind_agent_server/store.py +2 -0
- package/template/apps/agent-server/src/southwind_agent_server/trajectory.py +137 -0
- package/template/apps/agent-server/src/southwind_agent_server/unavailable_engine.py +58 -0
- package/template/apps/agent-server/tests/test_api.py +30 -11
- package/template/apps/agent-server/tests/test_client_completion.py +230 -0
- package/template/apps/agent-server/tests/test_provider_attempt_reporting.py +12 -24
- package/template/apps/agent-server/tests/test_report_resilience.py +2 -0
- package/template/apps/agent-server/tests/test_report_routes.py +58 -1
- package/template/apps/agent-server/tests/test_report_summary_parity.py +1 -0
- package/template/apps/agent-server/tests/test_run_capacity.py +2 -0
- package/template/apps/agent-server/tests/test_runtime_factory_readiness.py +18 -3
- package/template/apps/agent-server/tests/test_trajectory.py +91 -0
- package/template/apps/agent-server/uv.lock +0 -259
- package/template/apps/server/Dockerfile +9 -4
- package/template/apps/server/src/agent/actor-projection-audit.ts +6 -0
- package/template/apps/server/src/agent/agent-run-delegation-policy.ts +115 -0
- package/template/apps/server/src/agent/agent-session-bootstrap.ts +23 -0
- package/template/apps/server/src/agent/{debug-session-facade-provider-failure.test.ts → agent-session-facade-provider-failure.test.ts} +24 -33
- package/template/apps/server/src/agent/{debug-session-facade.test.ts → agent-session-facade.test.ts} +34 -45
- package/template/apps/server/src/agent/{debug-session-facade.ts → agent-session-facade.ts} +50 -57
- package/template/apps/server/src/agent/{debug-session-id.test.ts → agent-session-id.test.ts} +5 -5
- package/template/apps/server/src/agent/{debug-session-id.ts → agent-session-id.ts} +7 -7
- package/template/apps/server/src/agent/{debug-session-provider-reader.ts → agent-session-provider-reader.ts} +18 -15
- package/template/apps/server/src/agent/{debug-session-routes.test.ts → agent-session-routes.test.ts} +16 -16
- package/template/apps/server/src/agent/{debug-session-routes.ts → agent-session-routes.ts} +12 -12
- package/template/apps/server/src/agent/{debug-session-validation.ts → agent-session-validation.ts} +13 -11
- package/template/apps/server/src/agent/client-session/authorization.ts +47 -0
- package/template/apps/server/src/agent/client-session/bootstrap.ts +96 -0
- package/template/apps/server/src/agent/client-session/capabilities.ts +19 -0
- package/template/apps/server/src/agent/client-session/completion.ts +299 -0
- package/template/apps/server/src/agent/client-session/contracts.ts +136 -0
- package/template/apps/server/src/agent/client-session/digest.ts +39 -0
- package/template/apps/server/src/agent/client-session/drizzle-repository.integration.test.ts +170 -0
- package/template/apps/server/src/agent/client-session/drizzle-repository.ts +345 -0
- package/template/apps/server/src/agent/client-session/errors.ts +28 -0
- package/template/apps/server/src/agent/client-session/fixtures/page-agent-1.12.2-request.json +52 -0
- package/template/apps/server/src/agent/client-session/host-auth.test.ts +144 -0
- package/template/apps/server/src/agent/client-session/host-auth.ts +110 -0
- package/template/apps/server/src/agent/client-session/manifest-admission.ts +240 -0
- package/template/apps/server/src/agent/client-session/openai-stream.ts +123 -0
- package/template/apps/server/src/agent/client-session/presentation.ts +46 -0
- package/template/apps/server/src/agent/client-session/remote-provider.test.ts +144 -0
- package/template/apps/server/src/agent/client-session/remote-provider.ts +216 -0
- package/template/apps/server/src/agent/client-session/repository.ts +225 -0
- package/template/apps/server/src/agent/client-session/request.test.ts +69 -0
- package/template/apps/server/src/agent/client-session/request.ts +289 -0
- package/template/apps/server/src/agent/client-session/revocation.ts +23 -0
- package/template/apps/server/src/agent/client-session/routes.ts +315 -0
- package/template/apps/server/src/agent/client-session/runtime-audit.ts +42 -0
- package/template/apps/server/src/agent/client-session/service.test-support.ts +226 -0
- package/template/apps/server/src/agent/client-session/service.test.ts +268 -0
- package/template/apps/server/src/agent/client-session/service.ts +346 -0
- package/template/apps/server/src/agent/context-memory/attempt-context-contracts.ts +60 -0
- package/template/apps/server/src/agent/context-memory/authoritative-source-registry.ts +127 -0
- package/template/apps/server/src/agent/context-memory/context-builder.test.ts +160 -0
- package/template/apps/server/src/agent/context-memory/context-builder.ts +184 -0
- package/template/apps/server/src/agent/context-memory/contracts.ts +284 -0
- package/template/apps/server/src/agent/context-memory/digest.ts +22 -0
- package/template/apps/server/src/agent/context-memory/memory-authorization.ts +82 -0
- package/template/apps/server/src/agent/context-memory/memory-content.ts +69 -0
- package/template/apps/server/src/agent/context-memory/memory-object-registry.ts +87 -0
- package/template/apps/server/src/agent/context-memory/memory-projector-contracts.ts +121 -0
- package/template/apps/server/src/agent/context-memory/memory-projector-validation.ts +155 -0
- package/template/apps/server/src/agent/context-memory/memory-projector.test.ts +19 -0
- package/template/apps/server/src/agent/context-memory/memory-projector.ts +259 -0
- package/template/apps/server/src/agent/context-memory/memory-retrieval-policy.ts +195 -0
- package/template/apps/server/src/agent/context-memory/memory-retriever.test.ts +339 -0
- package/template/apps/server/src/agent/context-memory/memory-retriever.ts +184 -0
- package/template/apps/server/src/agent/context-memory/search-provider-port.test.ts +62 -0
- package/template/apps/server/src/agent/context-memory/search-provider-port.ts +75 -0
- package/template/apps/server/src/agent/context-memory/working-context-expiry.ts +95 -0
- package/template/apps/server/src/agent/context-message-recovery.ts +23 -0
- package/template/apps/server/src/agent/context-version-routes.ts +241 -0
- package/template/apps/server/src/agent/context-version-runtime.ts +332 -0
- package/template/apps/server/src/agent/context-version-support.ts +93 -0
- package/template/apps/server/src/agent/context-version-validation.ts +118 -0
- package/template/apps/server/src/agent/drizzle-run-binding-repository.ts +11 -0
- package/template/apps/server/src/agent/idempotency-key.ts +9 -1
- package/template/apps/server/src/agent/memory-runtime-adapters.ts +266 -0
- package/template/apps/server/src/agent/memory-scope-binding.ts +14 -0
- package/template/apps/server/src/agent/module-ai-port-factory.ts +41 -0
- package/template/apps/server/src/agent/provider-input-governance.ts +19 -0
- package/template/apps/server/src/agent/remote-runtime-response.ts +162 -0
- package/template/apps/server/src/agent/remote-runtime.ts +53 -153
- package/template/apps/server/src/agent/run-audit.ts +2 -2
- package/template/apps/server/src/agent/run-authorization.test.ts +27 -0
- package/template/apps/server/src/agent/run-authorization.ts +60 -1
- package/template/apps/server/src/agent/run-binding-repository.ts +9 -0
- package/template/apps/server/src/agent/run-browser-authorization.ts +79 -0
- package/template/apps/server/src/agent/run-contracts.ts +23 -0
- package/template/apps/server/src/agent/run-facade.ts +139 -139
- package/template/apps/server/src/agent/run-failure-cleanup.ts +58 -0
- package/template/apps/server/src/agent/run-operation-policy.ts +20 -0
- package/template/apps/server/src/agent/run-route-input.ts +19 -1
- package/template/apps/server/src/agent/run-routes.ts +133 -5
- package/template/apps/server/src/agent/run-runtime-request.ts +89 -0
- package/template/apps/server/src/agent/run-session-authorization.ts +95 -0
- package/template/apps/server/src/agent/run-start-preparation.ts +71 -0
- package/template/apps/server/src/agent/runtime-bootstrap-input.ts +42 -0
- package/template/apps/server/src/agent/runtime-bootstrap-report.test.ts +53 -1
- package/template/apps/server/src/agent/runtime-bootstrap.ts +114 -111
- package/template/apps/server/src/agent/runtime-tool-composition.ts +38 -0
- package/template/apps/server/src/agent/session-context-source-support.ts +38 -0
- package/template/apps/server/src/agent/session-context-source.ts +346 -0
- package/template/apps/server/src/agent/session-invalidation/contracts.ts +217 -0
- package/template/apps/server/src/agent/session-invalidation/dispatcher.contract.test.ts +333 -0
- package/template/apps/server/src/agent/session-invalidation/dispatcher.ts +205 -0
- package/template/apps/server/src/agent/session-invalidation/module-authoritative-producer.test.ts +127 -0
- package/template/apps/server/src/agent/session-invalidation/module-authoritative-producer.ts +202 -0
- package/template/apps/server/src/agent/session-invalidation/read-guard.ts +39 -0
- package/template/apps/server/src/agent/session-invalidation/runtime.ts +69 -0
- package/template/apps/server/src/agent/session-invalidation/test-envelopes.ts +24 -0
- package/template/apps/server/src/agent/session-invalidation/worker.contract.test.ts +135 -0
- package/template/apps/server/src/agent/session-invalidation/worker.ts +76 -0
- package/template/apps/server/src/agent/session-log/checkpoint-digest.ts +36 -0
- package/template/apps/server/src/agent/session-log/domain.ts +123 -0
- package/template/apps/server/src/agent/session-log/events.ts +229 -0
- package/template/apps/server/src/agent/session-log/history.ts +143 -0
- package/template/apps/server/src/agent/session-log/in-memory-repository.ts +340 -0
- package/template/apps/server/src/agent/session-log/lifecycle.ts +67 -0
- package/template/apps/server/src/agent/session-log/replay-policy.ts +76 -0
- package/template/apps/server/src/agent/session-log/repository.ts +103 -0
- package/template/apps/server/src/agent/session-log/service.ts +349 -0
- package/template/apps/server/src/agent/session-log/session-log.test.ts +282 -0
- package/template/apps/server/src/agent/session-persistence/drizzle-context-memory.ts +348 -0
- package/template/apps/server/src/agent/session-persistence/drizzle-governed-context.ts +328 -0
- package/template/apps/server/src/agent/session-persistence/drizzle-invalidation-outbox.ts +260 -0
- package/template/apps/server/src/agent/session-persistence/drizzle-memory-projection-deletion.ts +56 -0
- package/template/apps/server/src/agent/session-persistence/drizzle-run-projection-receipt.ts +80 -0
- package/template/apps/server/src/agent/session-persistence/drizzle-session-fork.ts +147 -0
- package/template/apps/server/src/agent/session-persistence/drizzle-session-list.ts +24 -0
- package/template/apps/server/src/agent/session-persistence/drizzle-session-mapping.ts +154 -0
- package/template/apps/server/src/agent/session-persistence/drizzle-session-mutations.ts +64 -0
- package/template/apps/server/src/agent/session-persistence/drizzle-session-repository.ts +335 -0
- package/template/apps/server/src/agent/session-persistence/invalidation-projections.ts +229 -0
- package/template/apps/server/src/agent/session-persistence/payload-codec.ts +90 -0
- package/template/apps/server/src/agent/session-projection-access.ts +155 -0
- package/template/apps/server/src/agent/session-projection-bootstrap.ts +87 -0
- package/template/apps/server/src/agent/session-projection-errors.ts +22 -0
- package/template/apps/server/src/agent/session-projection-facade.ts +349 -0
- package/template/apps/server/src/agent/session-projection-routes.ts +215 -0
- package/template/apps/server/src/agent/session-projection-view.ts +137 -0
- package/template/apps/server/src/agent/session-result-memory.ts +228 -0
- package/template/apps/server/src/agent/session-run-lifecycle.ts +85 -0
- package/template/apps/server/src/agent/session-run-link-contracts.ts +56 -0
- package/template/apps/server/src/agent/session-run-link.ts +343 -0
- package/template/apps/server/src/agent/session-run-recovery.ts +16 -0
- package/template/apps/server/src/agent/session-runtime-composition.ts +265 -0
- package/template/apps/server/src/agent/session-source-chain-authorizer.ts +141 -0
- package/template/apps/server/src/agent/session-terminal-reconciler.ts +100 -0
- package/template/apps/server/src/agent/session-turn-state.ts +49 -0
- package/template/apps/server/src/agent/tool-audit.ts +3 -3
- package/template/apps/server/src/agent/tool-execution-receipt.ts +13 -0
- package/template/apps/server/src/agent/tool-host.ts +41 -4
- package/template/apps/server/src/agent/tool-registry-composition.ts +52 -0
- package/template/apps/server/src/agent/tool-registry.test.ts +5 -5
- package/template/apps/server/src/agent/tool-registry.ts +53 -59
- package/template/apps/server/src/agent/unavailable-routes.ts +49 -5
- package/template/apps/server/src/agent/workflow-durable-runtime-input.ts +32 -0
- package/template/apps/server/src/agent/workflow-runtime-bootstrap.ts +59 -0
- package/template/apps/server/src/agent-workflow/adapter-dispatch.ts +148 -0
- package/template/apps/server/src/agent-workflow/approval-node-adapter.ts +77 -0
- package/template/apps/server/src/agent-workflow/artifact-access-error.ts +9 -0
- package/template/apps/server/src/agent-workflow/artifact-node-adapter.ts +67 -0
- package/template/apps/server/src/agent-workflow/artifact-policy.ts +71 -0
- package/template/apps/server/src/agent-workflow/artifact-source-authorization.ts +85 -0
- package/template/apps/server/src/agent-workflow/authorization.ts +238 -0
- package/template/apps/server/src/agent-workflow/budget-ledger.ts +118 -0
- package/template/apps/server/src/agent-workflow/budget-validation.ts +305 -0
- package/template/apps/server/src/agent-workflow/canonical-agent-run-binding.ts +17 -0
- package/template/apps/server/src/agent-workflow/canonical-agent-run-evidence.ts +142 -0
- package/template/apps/server/src/agent-workflow/canonical-agent-run-port.ts +304 -0
- package/template/apps/server/src/agent-workflow/canonical-agent-run-remote.ts +64 -0
- package/template/apps/server/src/agent-workflow/canonical-json.ts +16 -0
- package/template/apps/server/src/agent-workflow/command-drive.ts +195 -0
- package/template/apps/server/src/agent-workflow/command-error.ts +9 -0
- package/template/apps/server/src/agent-workflow/command-facade.ts +290 -0
- package/template/apps/server/src/agent-workflow/command-input.ts +133 -0
- package/template/apps/server/src/agent-workflow/condition-node-adapter.ts +69 -0
- package/template/apps/server/src/agent-workflow/definition-budget-validation.ts +41 -0
- package/template/apps/server/src/agent-workflow/definition-node-validation.ts +197 -0
- package/template/apps/server/src/agent-workflow/definition-validation.ts +198 -0
- package/template/apps/server/src/agent-workflow/delegated-tool-authorization.ts +15 -0
- package/template/apps/server/src/agent-workflow/delegation-child-delivery.ts +129 -0
- package/template/apps/server/src/agent-workflow/delegation-execution-authorization.ts +218 -0
- package/template/apps/server/src/agent-workflow/delegation-policy-validation.ts +253 -0
- package/template/apps/server/src/agent-workflow/delegation-policy.ts +179 -0
- package/template/apps/server/src/agent-workflow/drizzle-artifact-port.ts +211 -0
- package/template/apps/server/src/agent-workflow/drizzle-artifact-support.ts +152 -0
- package/template/apps/server/src/agent-workflow/drizzle-budget-ledger-settlement.ts +271 -0
- package/template/apps/server/src/agent-workflow/drizzle-budget-ledger-support.ts +226 -0
- package/template/apps/server/src/agent-workflow/drizzle-budget-ledger.ts +189 -0
- package/template/apps/server/src/agent-workflow/drizzle-cancellation-reconcile.ts +177 -0
- package/template/apps/server/src/agent-workflow/drizzle-delegated-tool-authorization.ts +155 -0
- package/template/apps/server/src/agent-workflow/drizzle-delegation-completion.ts +84 -0
- package/template/apps/server/src/agent-workflow/drizzle-delegation-lifecycle.ts +139 -0
- package/template/apps/server/src/agent-workflow/drizzle-delegation-port.ts +288 -0
- package/template/apps/server/src/agent-workflow/drizzle-effect-command-fence.ts +333 -0
- package/template/apps/server/src/agent-workflow/drizzle-evaluation-repository.ts +214 -0
- package/template/apps/server/src/agent-workflow/drizzle-event-validation.ts +43 -0
- package/template/apps/server/src/agent-workflow/drizzle-recovery-admission.ts +115 -0
- package/template/apps/server/src/agent-workflow/drizzle-repository-lineage.ts +103 -0
- package/template/apps/server/src/agent-workflow/drizzle-repository-mappers.ts +317 -0
- package/template/apps/server/src/agent-workflow/drizzle-repository-write.ts +311 -0
- package/template/apps/server/src/agent-workflow/drizzle-repository.ts +285 -0
- package/template/apps/server/src/agent-workflow/drizzle-side-effect-repository.ts +83 -0
- package/template/apps/server/src/agent-workflow/drizzle-write-support.ts +101 -0
- package/template/apps/server/src/agent-workflow/durable-continuation-authorization.ts +109 -0
- package/template/apps/server/src/agent-workflow/durable-continuation-driver.ts +97 -0
- package/template/apps/server/src/agent-workflow/durable-continuation-runtime.ts +173 -0
- package/template/apps/server/src/agent-workflow/durable-job-node-adapter.ts +106 -0
- package/template/apps/server/src/agent-workflow/effect-command-fence.ts +250 -0
- package/template/apps/server/src/agent-workflow/evaluation-evidence-coordinates.ts +225 -0
- package/template/apps/server/src/agent-workflow/evaluation-evidence-metrics.ts +280 -0
- package/template/apps/server/src/agent-workflow/evaluation-evidence-reader.ts +326 -0
- package/template/apps/server/src/agent-workflow/evaluation-metrics-validation.ts +85 -0
- package/template/apps/server/src/agent-workflow/evaluation-projection.ts +70 -0
- package/template/apps/server/src/agent-workflow/evaluation-repository.ts +134 -0
- package/template/apps/server/src/agent-workflow/evaluation-service.ts +292 -0
- package/template/apps/server/src/agent-workflow/evaluation-tool-registration.ts +95 -0
- package/template/apps/server/src/agent-workflow/evaluation-validation.ts +331 -0
- package/template/apps/server/src/agent-workflow/event-validation.ts +277 -0
- package/template/apps/server/src/agent-workflow/governed-tool-schema.ts +55 -0
- package/template/apps/server/src/agent-workflow/in-memory-evaluation-repository.ts +123 -0
- package/template/apps/server/src/agent-workflow/in-memory-mutation-validation.ts +154 -0
- package/template/apps/server/src/agent-workflow/in-memory-repository-support.ts +277 -0
- package/template/apps/server/src/agent-workflow/in-memory-repository.ts +339 -0
- package/template/apps/server/src/agent-workflow/in-memory-side-effect-store.ts +78 -0
- package/template/apps/server/src/agent-workflow/input-receipt.ts +55 -0
- package/template/apps/server/src/agent-workflow/model-node-adapter.ts +86 -0
- package/template/apps/server/src/agent-workflow/model-pricing-catalog.ts +215 -0
- package/template/apps/server/src/agent-workflow/node-adapter-factory.ts +83 -0
- package/template/apps/server/src/agent-workflow/node-adapters.ts +50 -0
- package/template/apps/server/src/agent-workflow/node-execution-ports.ts +185 -0
- package/template/apps/server/src/agent-workflow/node-input-receipt-port.ts +42 -0
- package/template/apps/server/src/agent-workflow/node-result-receipt.ts +50 -0
- package/template/apps/server/src/agent-workflow/platform-node-adapter-support.ts +185 -0
- package/template/apps/server/src/agent-workflow/platform-node-adapters.ts +256 -0
- package/template/apps/server/src/agent-workflow/platform-node-approval.ts +146 -0
- package/template/apps/server/src/agent-workflow/platform-node-direct-adapters.ts +65 -0
- package/template/apps/server/src/agent-workflow/production-delegation-scope.ts +58 -0
- package/template/apps/server/src/agent-workflow/production-node-policies.ts +62 -0
- package/template/apps/server/src/agent-workflow/query-service.ts +198 -0
- package/template/apps/server/src/agent-workflow/release.ts +96 -0
- package/template/apps/server/src/agent-workflow/repository.ts +157 -0
- package/template/apps/server/src/agent-workflow/routes.ts +307 -0
- package/template/apps/server/src/agent-workflow/runtime-composition.ts +308 -0
- package/template/apps/server/src/agent-workflow/scheduler-cancellation-reconcile.ts +253 -0
- package/template/apps/server/src/agent-workflow/scheduler-cancellation-support.ts +145 -0
- package/template/apps/server/src/agent-workflow/scheduler-cancellation.ts +2 -0
- package/template/apps/server/src/agent-workflow/scheduler-context.ts +85 -0
- package/template/apps/server/src/agent-workflow/scheduler-deadline.ts +65 -0
- package/template/apps/server/src/agent-workflow/scheduler-effect-result.ts +205 -0
- package/template/apps/server/src/agent-workflow/scheduler-execution-convergence.ts +174 -0
- package/template/apps/server/src/agent-workflow/scheduler-execution.ts +211 -0
- package/template/apps/server/src/agent-workflow/scheduler-failure.ts +102 -0
- package/template/apps/server/src/agent-workflow/scheduler-join-cancellation.ts +282 -0
- package/template/apps/server/src/agent-workflow/scheduler-mutation-queue.ts +13 -0
- package/template/apps/server/src/agent-workflow/scheduler-node-timeout.ts +139 -0
- package/template/apps/server/src/agent-workflow/scheduler-parallel-dispatch.ts +99 -0
- package/template/apps/server/src/agent-workflow/scheduler-recovery-activation.ts +63 -0
- package/template/apps/server/src/agent-workflow/scheduler-recovery-budget.ts +41 -0
- package/template/apps/server/src/agent-workflow/scheduler-recovery-effect.ts +331 -0
- package/template/apps/server/src/agent-workflow/scheduler-recovery-event-time.ts +26 -0
- package/template/apps/server/src/agent-workflow/scheduler-recovery-failed-policy.ts +129 -0
- package/template/apps/server/src/agent-workflow/scheduler-recovery-node.ts +208 -0
- package/template/apps/server/src/agent-workflow/scheduler-recovery-policy-convergence.ts +140 -0
- package/template/apps/server/src/agent-workflow/scheduler-reservation-convergence.ts +40 -0
- package/template/apps/server/src/agent-workflow/scheduler-side-effect-execution.ts +249 -0
- package/template/apps/server/src/agent-workflow/scheduler-support.ts +235 -0
- package/template/apps/server/src/agent-workflow/scheduler-workflow-cancellation.ts +296 -0
- package/template/apps/server/src/agent-workflow/scheduler.ts +289 -0
- package/template/apps/server/src/agent-workflow/service-completion.ts +102 -0
- package/template/apps/server/src/agent-workflow/service-mutation-identity.ts +94 -0
- package/template/apps/server/src/agent-workflow/service-node-admission.ts +36 -0
- package/template/apps/server/src/agent-workflow/service-node-run-update.ts +55 -0
- package/template/apps/server/src/agent-workflow/service-recovery-events.ts +38 -0
- package/template/apps/server/src/agent-workflow/service-recovery.ts +264 -0
- package/template/apps/server/src/agent-workflow/service-retry.ts +86 -0
- package/template/apps/server/src/agent-workflow/service-run-command.ts +131 -0
- package/template/apps/server/src/agent-workflow/service-state-error.ts +21 -0
- package/template/apps/server/src/agent-workflow/service-state-invariants.ts +44 -0
- package/template/apps/server/src/agent-workflow/service-support.ts +152 -0
- package/template/apps/server/src/agent-workflow/service.ts +307 -0
- package/template/apps/server/src/agent-workflow/side-effect-state.ts +226 -0
- package/template/apps/server/src/agent-workflow/state-machine.ts +321 -0
- package/template/apps/server/src/agent-workflow/subagent-node-adapter.ts +229 -0
- package/template/apps/server/src/agent-workflow/tool-node-adapter.ts +259 -0
- package/template/apps/server/src/agent-workflow/usage.ts +149 -0
- package/template/apps/server/src/agent-workflow/wait-signal-service.ts +147 -0
- package/template/apps/server/src/agent-workflow/workflow-artifact-token.ts +109 -0
- package/template/apps/server/src/agent-workflow/workflow-value-validation.ts +101 -0
- package/template/apps/server/src/agent-workflow/workspace-policy.ts +109 -0
- package/template/apps/server/src/ai-runtime-report/remote-parser.ts +41 -0
- package/template/apps/server/src/ai-runtime-report/service.test.ts +2 -0
- package/template/apps/server/src/app/agent-runtime-composition.ts +38 -0
- package/template/apps/server/src/app/client-agent-host-auth-binding.ts +10 -0
- package/template/apps/server/src/auth/actor-resolver.test.ts +33 -0
- package/template/apps/server/src/auth/actor-resolver.ts +6 -1
- package/template/apps/server/src/auth/composition.ts +36 -0
- package/template/apps/server/src/auth/routes.test-support.ts +2 -0
- package/template/apps/server/src/auth/routes.test.ts +18 -0
- package/template/apps/server/src/auth/runtime.ts +2 -0
- package/template/apps/server/src/auth/service-options.ts +16 -0
- package/template/apps/server/src/auth/service.ts +7 -12
- package/template/apps/server/src/index.ts +35 -37
- package/template/apps/server/src/license/pairing-service.test.ts +1 -1
- package/template/apps/server/src/module-host/agent-session-memory-port.ts +91 -0
- package/template/apps/server/src/module-host/{debug-session-provider-registration.test.ts → agent-session-provider-registration.test.ts} +42 -17
- package/template/apps/server/src/module-host/{debug-session-provider-registration.ts → agent-session-provider-registration.ts} +18 -18
- package/template/apps/server/src/module-host/agent-source-invalidation-port.ts +53 -0
- package/template/apps/server/src/module-host/http-runtime.ts +14 -0
- package/template/apps/server/src/module-host/initialize-contracts.ts +4 -5
- package/template/apps/server/src/module-host/initialize.ts +4 -4
- package/template/apps/server/src/module-host/request-context.test.ts +5 -5
- package/template/apps/server/src/module-host/request-context.ts +8 -0
- package/template/apps/server/src/module-host/route-install-context.ts +65 -0
- package/template/apps/server/src/module-host/route-installer.ts +11 -56
- package/template/apps/server/src/module-host/scoped-host.ts +17 -12
- package/template/apps/server/src/module-host/storage-authorization-binding.test.ts +3 -3
- package/template/apps/server/src/module-host.ts +3 -5
- package/template/apps/server/src/runtime.ts +3 -36
- package/template/apps/server/src/storage/routes-stream-cancellation.test.ts +19 -0
- package/template/apps/server/src/storage/routes.ts +3 -1
- package/template/apps/server/src/system/actor.ts +15 -2
- package/template/apps/server/src/system/authorization-invalidation-bridge.ts +30 -0
- package/template/apps/server/src/system/authorization-invalidation-composition.ts +36 -0
- package/template/apps/server/src/system/drizzle-system.ts +8 -1
- package/template/apps/server/src/system/identity-route-config.ts +9 -1
- package/template/apps/server/src/system/routes.ts +3 -0
- package/template/apps/server/src/system/seed.ts +3 -0
- package/template/apps/server/src/system/user-account-policy.ts +91 -0
- package/template/apps/web/package.json +1 -0
- package/template/apps/web/src/layout/AppSidebar.vue +2 -0
- package/template/apps/web/src/pages/system/AgentSessionPage.vue +73 -0
- package/template/apps/web/src/pages/system/AgentSessionPage.webtest.ts +66 -0
- package/template/apps/web/src/pages/system/AgentWorkflowPage.vue +69 -0
- package/template/apps/web/src/pages/system/components/agent-session/AgentSessionDetail.vue +114 -0
- package/template/apps/web/src/pages/system/components/agent-session/AgentSessionList.vue +84 -0
- package/template/apps/web/src/pages/system/components/agent-session/ContextVersionPanel.vue +119 -0
- package/template/apps/web/src/pages/system/components/agent-session/ContextVersionPanel.webtest.ts +46 -0
- package/template/apps/web/src/pages/system/components/agent-session/MemoryProjectionPanel.vue +163 -0
- package/template/apps/web/src/pages/system/components/agent-session/MemoryProjectionPanel.webtest.ts +62 -0
- package/template/apps/web/src/pages/system/components/agent-session/SessionTimeline.vue +71 -0
- package/template/apps/web/src/pages/system/components/agent-session/SessionTimeline.webtest.ts +34 -0
- package/template/apps/web/src/pages/system/components/agent-session/agent-session-presenter.ts +56 -0
- package/template/apps/web/src/pages/system/components/agent-workflow/WorkflowNodeCard.vue +39 -0
- package/template/apps/web/src/pages/system/components/agent-workflow/WorkflowNodeDetail.vue +172 -0
- package/template/apps/web/src/pages/system/components/agent-workflow/WorkflowRunDetail.vue +151 -0
- package/template/apps/web/src/pages/system/components/agent-workflow/WorkflowRunGraph.vue +96 -0
- package/template/apps/web/src/pages/system/components/agent-workflow/WorkflowRunList.vue +100 -0
- package/template/apps/web/src/pages/system/components/agent-workflow/WorkflowRunSummary.vue +148 -0
- package/template/apps/web/src/pages/system/components/agent-workflow/agent-workflow-presenter.ts +74 -0
- package/template/apps/web/src/pages/system/components/agent-workflow/workflow-graph-layout.ts +108 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeRunDetailDialog.vue +9 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeRunDetailDialog.webtest.ts +22 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeRunTable.webtest.ts +2 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeTrajectory.vue +118 -0
- package/template/apps/web/src/pages/system/composables/useAgentSessions.ts +209 -0
- package/template/apps/web/src/pages/system/composables/useAgentSessions.webtest.ts +138 -0
- package/template/apps/web/src/pages/system/composables/useAgentWorkflowRuns.ts +173 -0
- package/template/apps/web/src/router/index.ts +4 -0
- package/template/apps/web/src/router/platform-admin-route-bindings.ts +6 -0
- package/template/apps/web/src/services/agent-session-api.ts +159 -0
- package/template/apps/web/src/services/agent-session-api.webtest.ts +65 -0
- package/template/apps/web/src/services/agent-workflow-api.ts +181 -0
- package/template/apps/web/src/services/ai-runtime-api.ts +4 -0
- package/template/docker-compose.yml +11 -8
- package/template/docs/adr/0003-agent-session-event-source.md +3 -0
- package/template/docs/architecture/agent-harness.md +464 -0
- package/template/docs/architecture/agent-workflow.md +99 -0
- package/template/docs/architecture/ai-runtime.md +55 -47
- package/template/docs/architecture/module-host.md +4 -3
- package/template/docs/operations/adk-retirement.md +98 -0
- package/template/docs/operations/agent-session-memory.md +162 -0
- package/template/docs/operations/agent-session-troubleshooting.md +196 -0
- package/template/docs/operations/agent-workflow-recovery.md +74 -0
- package/template/docs/operations/incremental-delivery.md +1 -1
- package/template/docs/platform/agent-production-store.md +82 -0
- package/template/docs/platform/agent-run-reattachment.md +52 -0
- package/template/docs/platform/agent-runtime-observability.md +4 -3
- package/template/docs/platform/agent-runtime.md +116 -281
- package/template/docs/platform/agent-tool-approval.md +61 -0
- package/template/docs/platform/background-ai-operations.md +50 -0
- package/template/docs/platform/client-agent-session.md +173 -0
- package/template/docs/platform/embedding-ai-runtime.md +94 -0
- package/template/docs/platform/platform-authentication.md +5 -8
- package/template/package.json +0 -1
- package/template/packages/ai-client/index.ts +1 -0
- package/template/packages/ai-client/package.json +2 -1
- package/template/packages/ai-client/src/agent/client/client.ts +15 -2
- package/template/packages/ai-client/src/agent/client/run-protocol.test.ts +39 -25
- package/template/packages/ai-client/src/agent/client/types.ts +4 -0
- package/template/packages/ai-client/src/agent/contracts/contracts.test.ts +49 -7
- package/template/packages/ai-client/src/agent/contracts/events.ts +61 -0
- package/template/packages/ai-client/src/agent/contracts/runs.ts +1 -0
- package/template/packages/ai-client/src/client-agent/contracts.ts +131 -0
- package/template/packages/ai-client/src/client-agent/http.test.ts +60 -0
- package/template/packages/ai-client/src/client-agent/http.ts +66 -0
- package/template/packages/ai-client/src/client-agent/index.ts +3 -0
- package/template/packages/ai-client/src/client-agent/validation.test.ts +57 -0
- package/template/packages/ai-client/src/client-agent/validation.ts +152 -0
- package/template/packages/config/index.test.ts +18 -0
- package/template/packages/config/package.json +1 -1
- package/template/packages/config/src/platform-validation.ts +8 -0
- package/template/packages/config/src/platform.ts +22 -9
- package/template/packages/database/drizzle/0055_square_yellowjacket.sql +47 -0
- package/template/packages/database/drizzle/0056_tricky_living_mummy.sql +2 -0
- package/template/packages/database/drizzle/0057_mature_doomsday.sql +4 -0
- package/template/packages/database/drizzle/0058_agent_session_memory.sql +159 -0
- package/template/packages/database/drizzle/0059_agent_source_module_key.sql +16 -0
- package/template/packages/database/drizzle/0060_slimy_rocket_raccoon.sql +43 -0
- package/template/packages/database/drizzle/0061_agent_workflow.sql +303 -0
- package/template/packages/database/drizzle/meta/0055_snapshot.json +11071 -0
- package/template/packages/database/drizzle/meta/0056_snapshot.json +11098 -0
- package/template/packages/database/drizzle/meta/0057_snapshot.json +10878 -0
- package/template/packages/database/drizzle/meta/0058_snapshot.json +12294 -0
- package/template/packages/database/drizzle/meta/0059_snapshot.json +12344 -0
- package/template/packages/database/drizzle/meta/0060_snapshot.json +12326 -0
- package/template/packages/database/drizzle/meta/0061_snapshot.json +14395 -0
- package/template/packages/database/drizzle/meta/_journal.json +49 -0
- package/template/packages/database/package.json +1 -1
- package/template/packages/database/src/agent-runtime-schema.test.ts +26 -0
- package/template/packages/database/src/schema/agent-context-message.ts +38 -0
- package/template/packages/database/src/schema/agent-runtime.ts +7 -0
- package/template/packages/database/src/schema/agent-session-receipt.ts +36 -0
- package/template/packages/database/src/schema/agent-session.ts +344 -0
- package/template/packages/database/src/schema/agent-workflow-delegation.ts +126 -0
- package/template/packages/database/src/schema/agent-workflow-effects.ts +213 -0
- package/template/packages/database/src/schema/agent-workflow-evaluation.ts +67 -0
- package/template/packages/database/src/schema/agent-workflow.ts +270 -0
- package/template/packages/database/src/schema/client-agent.ts +95 -0
- package/template/packages/database/src/schema/index.ts +8 -0
- package/template/packages/jobs/package.json +1 -1
- package/template/packages/modules/index.ts +2 -0
- package/template/packages/modules/package.json +1 -1
- package/template/packages/modules/src/ai-client-execution-composition.test.ts +98 -0
- package/template/packages/modules/src/ai-client-execution-policy.ts +54 -0
- package/template/packages/modules/src/ai-operation-composition.ts +2 -0
- package/template/packages/modules/src/manifest.ts +15 -0
- package/template/packages/modules/src/system-admin-routes.ts +16 -0
- package/template/packages/modules/src/system-admin-ui-catalog.ts +4 -1
- package/template/packages/modules/src/system-agent-audit-actions.ts +26 -0
- package/template/packages/modules/src/system-ai-api-bindings.ts +262 -0
- package/template/packages/modules/src/system-api-control-plane.ts +4 -78
- package/template/packages/modules/src/system-audit-actions.ts +113 -0
- package/template/packages/modules/src/system-features.ts +17 -0
- package/template/packages/modules/src/system-module-catalog.ts +21 -11
- package/template/packages/modules/src/system-modules.ts +22 -0
- package/template/packages/modules/src/system-permissions.ts +8 -0
- package/template/packages/server-sdk/index.ts +43 -0
- package/template/packages/server-sdk/package.json +5 -1
- package/template/packages/server-sdk/src/agent-session-memory-port.ts +64 -0
- package/template/packages/server-sdk/src/agent-session-provider.ts +63 -0
- package/template/packages/server-sdk/src/agent-source-invalidation-port.ts +54 -0
- package/template/packages/server-sdk/src/ai-operation-registration.ts +2 -0
- package/template/packages/server-sdk/src/client-agent-session.ts +95 -0
- package/template/packages/server-sdk/src/debug-session-provider.ts +14 -63
- package/template/packages/server-sdk/src/module-host.ts +7 -3
- package/template/packages/server-sdk/src/request-context.ts +6 -0
- package/template/packages/shared/index.ts +3 -0
- package/template/packages/shared/package.json +1 -1
- package/template/packages/shared/src/agent-workflow-definition.ts +113 -0
- package/template/packages/shared/src/agent-workflow-delegation.ts +123 -0
- package/template/packages/shared/src/agent-workflow-evaluation.ts +159 -0
- package/template/packages/shared/src/agent-workflow-events.ts +99 -0
- package/template/packages/shared/src/agent-workflow-run.ts +259 -0
- package/template/packages/shared/src/agent-workflow.ts +3 -0
- package/template/packages/shared/src/ai-runtime-report.ts +25 -0
- package/template/packages/shared/src/rbac.ts +24 -0
- package/template/packages/storage/package.json +1 -1
- package/template/packages/storage/src/upload-streams.test.ts +31 -0
- package/template/packages/storage/src/upload-streams.ts +20 -6
- package/template/apps/agent-server/adk_web_agents/__init__.py +0 -1
- package/template/apps/agent-server/adk_web_agents/canonical_debug/__init__.py +0 -1
- package/template/apps/agent-server/adk_web_agents/canonical_debug/agent.py +0 -145
- package/template/apps/agent-server/adk_web_agents/safe_debug/__init__.py +0 -3
- package/template/apps/agent-server/adk_web_agents/safe_debug/agent.py +0 -40
- package/template/apps/agent-server/adk_web_agents/services.py +0 -25
- package/template/apps/agent-server/project/adk-web-debug.json +0 -4
- package/template/apps/agent-server/src/southwind_agent_server/adk_adapter.py +0 -333
- package/template/apps/agent-server/src/southwind_agent_server/adk_debug.py +0 -348
- package/template/apps/agent-server/src/southwind_agent_server/adk_debug_sessions.py +0 -325
- package/template/apps/agent-server/src/southwind_agent_server/adk_failure.py +0 -13
- package/template/apps/agent-server/src/southwind_agent_server/adk_model.py +0 -304
- package/template/apps/agent-server/src/southwind_agent_server/adk_tools.py +0 -143
- package/template/apps/agent-server/tests/test_adk_adapter.py +0 -307
- package/template/apps/agent-server/tests/test_adk_debug_result_polling.py +0 -139
- package/template/apps/agent-server/tests/test_adk_debug_sessions.py +0 -215
- package/template/apps/agent-server/tests/test_adk_web_agent.py +0 -12
- package/template/apps/agent-server/tests/test_adk_web_canonical_debug.py +0 -282
- package/template/apps/server/src/agent/run-debug-authorization.ts +0 -27
- package/template/apps/server/src/agent/run-facade-adk-debug.test.ts +0 -95
- package/template/apps/server/src/runtime-adk-debug.test.ts +0 -270
- package/template/apps/server/src/runtime-adk-debug.ts +0 -164
- package/template/apps/server/src/runtime-request-actor.test.ts +0 -55
- package/template/apps/server/src/runtime-request-actor.ts +0 -22
- package/template/docker-compose.agent-debug.yml +0 -58
- package/template/scripts/dev-adk-web.ts +0 -107
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
2
|
+
import type { ModuleAiJsonValue } from "@southwind-ai/server-sdk";
|
|
3
|
+
import type {
|
|
4
|
+
ClientSessionAdmissionPort,
|
|
5
|
+
ClientSessionAuditPort,
|
|
6
|
+
ClientSessionCaller,
|
|
7
|
+
ClientSessionFinalResult,
|
|
8
|
+
ClientSessionRecord,
|
|
9
|
+
} from "./contracts.js";
|
|
10
|
+
import { digestJson, digestText, validDigest } from "./digest.js";
|
|
11
|
+
import type { ClientSessionRepository } from "./repository.js";
|
|
12
|
+
import { clientAgentCapabilities } from "./capabilities.js";
|
|
13
|
+
import { authorizeClientSession } from "./authorization.js";
|
|
14
|
+
import { abortClientSession, auditRevokedSessions } from "./revocation.js";
|
|
15
|
+
import {
|
|
16
|
+
ensureTrailingSlash,
|
|
17
|
+
publicLimits,
|
|
18
|
+
sessionView,
|
|
19
|
+
} from "./presentation.js";
|
|
20
|
+
import {
|
|
21
|
+
ClientAgentSessionError,
|
|
22
|
+
clientSessionConflict as conflict,
|
|
23
|
+
clientSessionInvalid as invalid,
|
|
24
|
+
} from "./errors.js";
|
|
25
|
+
import {
|
|
26
|
+
consumeClientAgentCompletion,
|
|
27
|
+
runClientAgentCompletion,
|
|
28
|
+
} from "./completion.js";
|
|
29
|
+
export class GovernedClientAgentSessionService {
|
|
30
|
+
readonly #attemptControllers = new Map<string, AbortController>();
|
|
31
|
+
readonly #recovery: Promise<number>;
|
|
32
|
+
constructor(
|
|
33
|
+
private readonly repository: ClientSessionRepository,
|
|
34
|
+
private readonly admission: ClientSessionAdmissionPort,
|
|
35
|
+
private readonly provider: import("./contracts.js").ClientAgentProviderPort,
|
|
36
|
+
private readonly audit: ClientSessionAuditPort,
|
|
37
|
+
private readonly trustedBaseUrl: string,
|
|
38
|
+
private readonly now: () => number = Date.now,
|
|
39
|
+
private readonly maxTtlMs = 5 * 60_000,
|
|
40
|
+
) {
|
|
41
|
+
const url = new URL(trustedBaseUrl);
|
|
42
|
+
if (url.protocol !== "https:" && url.hostname !== "localhost") {
|
|
43
|
+
throw new Error("Client Agent trusted baseURL 必须使用 HTTPS");
|
|
44
|
+
}
|
|
45
|
+
if (url.username || url.password || url.search || url.hash) {
|
|
46
|
+
throw new Error("Client Agent trusted baseURL 不能包含凭据或参数");
|
|
47
|
+
}
|
|
48
|
+
this.#recovery = repository.recoverInterrupted(this.now());
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async capabilities(signal?: AbortSignal) {
|
|
52
|
+
await this.#recovery;
|
|
53
|
+
return clientAgentCapabilities(this.provider, signal);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async create(input: {
|
|
57
|
+
operationKey: string;
|
|
58
|
+
input: ModuleAiJsonValue;
|
|
59
|
+
clientBindingDigest: string;
|
|
60
|
+
hostSessionDigest: `sha256:${string}`;
|
|
61
|
+
actorId: string;
|
|
62
|
+
}) {
|
|
63
|
+
await this.#recovery;
|
|
64
|
+
if (!(await this.provider.ready())) {
|
|
65
|
+
throw new ClientAgentSessionError(
|
|
66
|
+
"PROVIDER_FAILED",
|
|
67
|
+
"Client Agent Provider 尚未就绪",
|
|
68
|
+
503,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
if (
|
|
72
|
+
!validDigest(input.clientBindingDigest) ||
|
|
73
|
+
!validDigest(input.hostSessionDigest) ||
|
|
74
|
+
!input.actorId.trim()
|
|
75
|
+
) {
|
|
76
|
+
throw invalid("Client Agent binding 或 Actor 无效");
|
|
77
|
+
}
|
|
78
|
+
const admission = await this.admission.admit({
|
|
79
|
+
operationKey: input.operationKey,
|
|
80
|
+
input: input.input,
|
|
81
|
+
actorId: input.actorId,
|
|
82
|
+
clientBindingDigest: input.clientBindingDigest,
|
|
83
|
+
});
|
|
84
|
+
if (
|
|
85
|
+
admission.actorId !== input.actorId ||
|
|
86
|
+
admission.operationKey !== input.operationKey ||
|
|
87
|
+
admission.clientBindingDigest !== input.clientBindingDigest
|
|
88
|
+
) {
|
|
89
|
+
throw new Error("Client Agent admission 返回了不一致的绑定");
|
|
90
|
+
}
|
|
91
|
+
const createdAt = this.now();
|
|
92
|
+
const expiresAt =
|
|
93
|
+
createdAt +
|
|
94
|
+
Math.min(admission.limits.maxDurationSeconds * 1_000, this.maxTtlMs);
|
|
95
|
+
const accessToken = `car_${randomBytes(32).toString("base64url")}`;
|
|
96
|
+
const record: ClientSessionRecord = {
|
|
97
|
+
...admission,
|
|
98
|
+
sessionId: `cas_${randomUUID()}`,
|
|
99
|
+
clientRunId: `carun_${randomUUID()}`,
|
|
100
|
+
tokenHash: digestText(accessToken),
|
|
101
|
+
hostSessionDigest: input.hostSessionDigest,
|
|
102
|
+
modelAlias: this.provider.modelAlias,
|
|
103
|
+
createdAt,
|
|
104
|
+
expiresAt,
|
|
105
|
+
status: "active",
|
|
106
|
+
attemptCount: 0,
|
|
107
|
+
activeRequests: 0,
|
|
108
|
+
pendingAttempts: [],
|
|
109
|
+
receipts: [],
|
|
110
|
+
};
|
|
111
|
+
await this.repository.create(record);
|
|
112
|
+
await this.recordAudit("create", record, "success", {
|
|
113
|
+
inputCommitment: record.inputCommitment,
|
|
114
|
+
authorizationDigest: record.authorizationDigest,
|
|
115
|
+
bindingDigest: record.clientBindingDigest,
|
|
116
|
+
});
|
|
117
|
+
return {
|
|
118
|
+
clientRunId: record.clientRunId,
|
|
119
|
+
sessionId: record.sessionId,
|
|
120
|
+
trustedBaseUrl: new URL(
|
|
121
|
+
`${encodeURIComponent(record.sessionId)}/v1/`,
|
|
122
|
+
ensureTrailingSlash(this.trustedBaseUrl),
|
|
123
|
+
).toString(),
|
|
124
|
+
modelAlias: record.modelAlias,
|
|
125
|
+
limits: publicLimits(record),
|
|
126
|
+
expiresAt: new Date(record.expiresAt).toISOString(),
|
|
127
|
+
accessToken,
|
|
128
|
+
capabilities: ["pageAgentClientRun"] as const,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async complete(
|
|
133
|
+
sessionId: string,
|
|
134
|
+
body: unknown,
|
|
135
|
+
caller: ClientSessionCaller,
|
|
136
|
+
signal?: AbortSignal,
|
|
137
|
+
) {
|
|
138
|
+
return consumeClientAgentCompletion(
|
|
139
|
+
(await this.startCompletion(sessionId, body, caller, signal)).events,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
async startCompletion(
|
|
143
|
+
sessionId: string,
|
|
144
|
+
body: unknown,
|
|
145
|
+
caller: ClientSessionCaller,
|
|
146
|
+
signal?: AbortSignal,
|
|
147
|
+
) {
|
|
148
|
+
await this.#recovery;
|
|
149
|
+
return runClientAgentCompletion({
|
|
150
|
+
sessionId,
|
|
151
|
+
body,
|
|
152
|
+
caller,
|
|
153
|
+
signal,
|
|
154
|
+
current: await this.authorize(sessionId, caller, true),
|
|
155
|
+
repository: this.repository,
|
|
156
|
+
admission: this.admission,
|
|
157
|
+
provider: this.provider,
|
|
158
|
+
controllers: this.#attemptControllers,
|
|
159
|
+
now: this.now,
|
|
160
|
+
audit: (record, outcome, metadata) =>
|
|
161
|
+
this.recordAudit("provider-attempt", record, outcome, metadata),
|
|
162
|
+
revokeAudit: (record, cause) =>
|
|
163
|
+
this.recordAudit("revoke", record, "success", { cause }),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async finalize(
|
|
168
|
+
sessionId: string,
|
|
169
|
+
caller: ClientSessionCaller,
|
|
170
|
+
receiptIds: readonly string[],
|
|
171
|
+
clientResult?: ModuleAiJsonValue,
|
|
172
|
+
) {
|
|
173
|
+
await this.#recovery;
|
|
174
|
+
const record = await this.authorize(sessionId, caller, false);
|
|
175
|
+
const terminalDigest = digestJson({
|
|
176
|
+
receiptIds: [...receiptIds],
|
|
177
|
+
...(clientResult === undefined ? {} : { result: clientResult }),
|
|
178
|
+
});
|
|
179
|
+
if (record.status === "completed") {
|
|
180
|
+
if (record.terminalDigest === terminalDigest) return sessionView(record);
|
|
181
|
+
throw conflict("会话已完成,终态不可改写");
|
|
182
|
+
}
|
|
183
|
+
if (record.status !== "active" || record.activeRequests) {
|
|
184
|
+
throw conflict("会话当前不能 finalize");
|
|
185
|
+
}
|
|
186
|
+
const receipts = [...record.receipts].sort((a, b) => a.attempt - b.attempt);
|
|
187
|
+
if (
|
|
188
|
+
!receipts.length ||
|
|
189
|
+
receiptIds.length !== receipts.length ||
|
|
190
|
+
receipts.some((receipt, index) => receipt.receiptId !== receiptIds[index])
|
|
191
|
+
) {
|
|
192
|
+
throw invalid("Finalize 必须提交本 Run 的完整终末 receipt 链");
|
|
193
|
+
}
|
|
194
|
+
const terminal = receipts.at(-1)!;
|
|
195
|
+
const projected = terminal.canonicalResult;
|
|
196
|
+
const result = projected ?? clientResult;
|
|
197
|
+
if (result === undefined) throw invalid("Finalize 缺少可公开结果");
|
|
198
|
+
await this.admission.validateResult(record.operationKey, result);
|
|
199
|
+
const finalResult: ClientSessionFinalResult = {
|
|
200
|
+
clientRunId: record.clientRunId,
|
|
201
|
+
status: "completed",
|
|
202
|
+
result,
|
|
203
|
+
attestation: {
|
|
204
|
+
source:
|
|
205
|
+
projected === undefined ? "client-projected" : "provider-observed",
|
|
206
|
+
terminalReceiptId: terminal.receiptId,
|
|
207
|
+
providerAttested: projected !== undefined,
|
|
208
|
+
clientProjected: projected === undefined,
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
const completed = await this.repository.transition(
|
|
212
|
+
sessionId,
|
|
213
|
+
"active",
|
|
214
|
+
"completed",
|
|
215
|
+
{
|
|
216
|
+
terminalDigest,
|
|
217
|
+
finalResult,
|
|
218
|
+
},
|
|
219
|
+
);
|
|
220
|
+
if (!completed) {
|
|
221
|
+
const concurrent = await this.repository.find(sessionId);
|
|
222
|
+
if (
|
|
223
|
+
concurrent?.status === "completed" &&
|
|
224
|
+
concurrent.terminalDigest === terminalDigest
|
|
225
|
+
) {
|
|
226
|
+
return sessionView(concurrent);
|
|
227
|
+
}
|
|
228
|
+
throw conflict("Finalize 与其它终态操作冲突");
|
|
229
|
+
}
|
|
230
|
+
await this.recordAudit("finalize", completed, "success", {
|
|
231
|
+
terminalReceiptId: terminal.receiptId,
|
|
232
|
+
clientProjected: projected === undefined,
|
|
233
|
+
});
|
|
234
|
+
return sessionView(completed);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async cancel(
|
|
238
|
+
sessionId: string,
|
|
239
|
+
caller: ClientSessionCaller,
|
|
240
|
+
reason?: string,
|
|
241
|
+
) {
|
|
242
|
+
await this.#recovery;
|
|
243
|
+
const record = await this.authorize(sessionId, caller, false);
|
|
244
|
+
if (record.status === "cancelled") return sessionView(record);
|
|
245
|
+
if (record.status !== "active") throw conflict("终态会话不可取消");
|
|
246
|
+
abortClientSession(this.#attemptControllers, sessionId);
|
|
247
|
+
const cancelled = await this.repository.transition(
|
|
248
|
+
sessionId,
|
|
249
|
+
"active",
|
|
250
|
+
"cancelled",
|
|
251
|
+
{
|
|
252
|
+
terminalDigest: digestText(`cancel\0${reason?.slice(0, 200) ?? ""}`),
|
|
253
|
+
},
|
|
254
|
+
);
|
|
255
|
+
if (!cancelled) {
|
|
256
|
+
const concurrent = await this.repository.find(sessionId);
|
|
257
|
+
if (concurrent?.status === "cancelled") return sessionView(concurrent);
|
|
258
|
+
throw conflict("取消与其它终态操作冲突");
|
|
259
|
+
}
|
|
260
|
+
await this.recordAudit("cancel", cancelled, "success", {
|
|
261
|
+
reasonDigest: digestText(reason?.slice(0, 200) ?? ""),
|
|
262
|
+
});
|
|
263
|
+
return sessionView(cancelled);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async status(sessionId: string, caller: ClientSessionCaller) {
|
|
267
|
+
await this.#recovery;
|
|
268
|
+
return sessionView(await this.authorize(sessionId, caller, false));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async revokeActor(actorId: string): Promise<number> {
|
|
272
|
+
await this.#recovery;
|
|
273
|
+
return auditRevokedSessions({
|
|
274
|
+
records: await this.repository.revokeActor(actorId),
|
|
275
|
+
cause: "actor",
|
|
276
|
+
controllers: this.#attemptControllers,
|
|
277
|
+
audit: (record, cause) =>
|
|
278
|
+
this.recordAudit("revoke", record, "success", { cause }),
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async revokeBinding(
|
|
283
|
+
bindingDigest: `sha256:${string}`,
|
|
284
|
+
actorId: string,
|
|
285
|
+
): Promise<number> {
|
|
286
|
+
await this.#recovery;
|
|
287
|
+
return auditRevokedSessions({
|
|
288
|
+
records: await this.repository.revokeBinding(bindingDigest, actorId),
|
|
289
|
+
cause: "binding",
|
|
290
|
+
controllers: this.#attemptControllers,
|
|
291
|
+
audit: (record, cause) =>
|
|
292
|
+
this.recordAudit("revoke", record, "success", { cause }),
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async revokeHostSession(
|
|
297
|
+
hostSessionDigest: `sha256:${string}`,
|
|
298
|
+
actorId: string,
|
|
299
|
+
): Promise<number> {
|
|
300
|
+
await this.#recovery;
|
|
301
|
+
return auditRevokedSessions({
|
|
302
|
+
records: await this.repository.revokeHostSession(
|
|
303
|
+
hostSessionDigest,
|
|
304
|
+
actorId,
|
|
305
|
+
),
|
|
306
|
+
cause: "host-session",
|
|
307
|
+
controllers: this.#attemptControllers,
|
|
308
|
+
audit: (record, cause) =>
|
|
309
|
+
this.recordAudit("revoke", record, "success", { cause }),
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
private async authorize(
|
|
314
|
+
sessionId: string,
|
|
315
|
+
caller: ClientSessionCaller,
|
|
316
|
+
requireActive: boolean,
|
|
317
|
+
): Promise<ClientSessionRecord> {
|
|
318
|
+
return authorizeClientSession({
|
|
319
|
+
sessionId,
|
|
320
|
+
caller,
|
|
321
|
+
requireActive,
|
|
322
|
+
repository: this.repository,
|
|
323
|
+
admission: this.admission,
|
|
324
|
+
now: this.now,
|
|
325
|
+
abort: () => abortClientSession(this.#attemptControllers, sessionId),
|
|
326
|
+
audit: (record, cause) =>
|
|
327
|
+
this.recordAudit("revoke", record, "success", { cause }),
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private recordAudit(
|
|
332
|
+
action: Parameters<ClientSessionAuditPort["record"]>[0]["action"],
|
|
333
|
+
record: ClientSessionRecord,
|
|
334
|
+
outcome: "success" | "failure",
|
|
335
|
+
metadata: Readonly<Record<string, string | number | boolean>>,
|
|
336
|
+
) {
|
|
337
|
+
return this.audit.record({
|
|
338
|
+
action,
|
|
339
|
+
actorId: record.actorId,
|
|
340
|
+
clientRunId: record.clientRunId,
|
|
341
|
+
operationKey: record.operationKey,
|
|
342
|
+
outcome,
|
|
343
|
+
metadata,
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ModuleAiJsonValue } from "@southwind-ai/server-sdk";
|
|
2
|
+
import type {
|
|
3
|
+
ContextSourceReference,
|
|
4
|
+
ContextVersion,
|
|
5
|
+
} from "./contracts.js";
|
|
6
|
+
|
|
7
|
+
export type GovernedContextMessageRole =
|
|
8
|
+
| "system"
|
|
9
|
+
| "user"
|
|
10
|
+
| "assistant"
|
|
11
|
+
| "tool";
|
|
12
|
+
|
|
13
|
+
export type ContextMessageOriginType =
|
|
14
|
+
| "instructions"
|
|
15
|
+
| "user-prompt"
|
|
16
|
+
| "artifact"
|
|
17
|
+
| "context-source"
|
|
18
|
+
| "provider-output"
|
|
19
|
+
| "tool-receipt";
|
|
20
|
+
|
|
21
|
+
export interface ContextMessageOrigin {
|
|
22
|
+
readonly type: ContextMessageOriginType;
|
|
23
|
+
readonly sourceId: string;
|
|
24
|
+
readonly sourceVersion: string;
|
|
25
|
+
readonly digest: string;
|
|
26
|
+
readonly source?: ContextSourceReference;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface GovernedContextMessage {
|
|
30
|
+
readonly ordinal: number;
|
|
31
|
+
readonly role: GovernedContextMessageRole;
|
|
32
|
+
/** Recoverable whitelist projection; binary Artifact content remains a reference. */
|
|
33
|
+
readonly content: ModuleAiJsonValue;
|
|
34
|
+
/** SHA-256 of canonical JSON for the exact governed Provider message. */
|
|
35
|
+
readonly digest: string;
|
|
36
|
+
/** SHA-256 of the encrypted whitelist recovery projection. */
|
|
37
|
+
readonly recoveryDigest: string;
|
|
38
|
+
readonly origins: readonly ContextMessageOrigin[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ProviderContextEnvelope {
|
|
42
|
+
readonly deploymentKey: string;
|
|
43
|
+
readonly model: string;
|
|
44
|
+
/** SHA-256 of the exact serialized Provider request body. */
|
|
45
|
+
readonly requestDigest: string;
|
|
46
|
+
readonly toolsDigest: string;
|
|
47
|
+
readonly outputSchemaDigest: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface GovernedContextVersion extends ContextVersion {
|
|
51
|
+
readonly provider: ProviderContextEnvelope;
|
|
52
|
+
readonly messages: readonly GovernedContextMessage[];
|
|
53
|
+
readonly committedAt: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface PreparedContextSource {
|
|
57
|
+
readonly reference: ContextSourceReference;
|
|
58
|
+
readonly role: GovernedContextMessageRole;
|
|
59
|
+
readonly content: ModuleAiJsonValue;
|
|
60
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AuthoritativeSourceAccessDecision,
|
|
3
|
+
AuthoritativeSourceAccessPort,
|
|
4
|
+
AuthoritativeSourceAccessQuery,
|
|
5
|
+
SourceInvalidationAppendResult,
|
|
6
|
+
SourceInvalidationEnvelopeV1,
|
|
7
|
+
} from "../session-invalidation/contracts.js";
|
|
8
|
+
import {
|
|
9
|
+
createModuleAuthoritativeInvalidationProducer,
|
|
10
|
+
ModuleAuthoritativeInvalidationUnavailableError,
|
|
11
|
+
type ModuleAuthoritativeInvalidationHostBinding,
|
|
12
|
+
type ModuleAuthoritativeInvalidationProducerPort,
|
|
13
|
+
} from "../session-invalidation/module-authoritative-producer.js";
|
|
14
|
+
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
15
|
+
|
|
16
|
+
export interface ModuleSourceInvalidationTransactionAdapter {
|
|
17
|
+
/** Returns only the module's current authoritative business transaction. */
|
|
18
|
+
currentDatabase(): NodePgDatabase | undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ModuleAuthoritativeSourceAdapterRegistration {
|
|
22
|
+
readonly moduleKey: string;
|
|
23
|
+
/** Must resolve the owning module's current object and authorization state. */
|
|
24
|
+
readonly access: AuthoritativeSourceAccessPort;
|
|
25
|
+
/** Required for ModuleRequestContext lifecycle writes; absence fails closed. */
|
|
26
|
+
readonly invalidationTransaction?: ModuleSourceInvalidationTransactionAdapter;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Immutable startup registry for module-owned object authorization.
|
|
31
|
+
* Missing or duplicate adapters never fall back to a projection or a fake grant.
|
|
32
|
+
*/
|
|
33
|
+
export class ModuleAuthoritativeSourceAccessRegistry
|
|
34
|
+
implements AuthoritativeSourceAccessPort
|
|
35
|
+
{
|
|
36
|
+
readonly #adapters: ReadonlyMap<string, AuthoritativeSourceAccessPort>;
|
|
37
|
+
readonly #transactions: ReadonlyMap<
|
|
38
|
+
string,
|
|
39
|
+
ModuleSourceInvalidationTransactionAdapter
|
|
40
|
+
>;
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
43
|
+
registrations: readonly ModuleAuthoritativeSourceAdapterRegistration[],
|
|
44
|
+
) {
|
|
45
|
+
const adapters = new Map<string, AuthoritativeSourceAccessPort>();
|
|
46
|
+
const transactions = new Map<
|
|
47
|
+
string,
|
|
48
|
+
ModuleSourceInvalidationTransactionAdapter
|
|
49
|
+
>();
|
|
50
|
+
for (const registration of registrations) {
|
|
51
|
+
const moduleKey = registration.moduleKey.trim();
|
|
52
|
+
if (!moduleKey || adapters.has(moduleKey)) {
|
|
53
|
+
throw new Error("权威来源 Adapter 注册无效或重复");
|
|
54
|
+
}
|
|
55
|
+
adapters.set(moduleKey, registration.access);
|
|
56
|
+
if (registration.invalidationTransaction) {
|
|
57
|
+
transactions.set(moduleKey, registration.invalidationTransaction);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
this.#adapters = adapters;
|
|
61
|
+
this.#transactions = transactions;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async authorizeCurrent(
|
|
65
|
+
query: AuthoritativeSourceAccessQuery,
|
|
66
|
+
): Promise<AuthoritativeSourceAccessDecision> {
|
|
67
|
+
const adapter = this.#adapters.get(query.source.moduleKey);
|
|
68
|
+
if (!adapter) return denied();
|
|
69
|
+
try {
|
|
70
|
+
return await adapter.authorizeCurrent(query);
|
|
71
|
+
} catch {
|
|
72
|
+
return denied();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Host-only binding: business code receives a producer with immutable module identity. */
|
|
77
|
+
bindInvalidationProducer(
|
|
78
|
+
binding: ModuleAuthoritativeInvalidationHostBinding,
|
|
79
|
+
): ModuleAuthoritativeInvalidationProducerPort {
|
|
80
|
+
const moduleKey = binding.moduleKey.trim();
|
|
81
|
+
if (!this.#adapters.has(moduleKey)) {
|
|
82
|
+
throw new Error("权威来源 Adapter 未注册");
|
|
83
|
+
}
|
|
84
|
+
return createModuleAuthoritativeInvalidationProducer({
|
|
85
|
+
...binding,
|
|
86
|
+
moduleKey,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Host-only: resolves the current module transaction at append time. */
|
|
91
|
+
bindTransactionalInvalidationProducer(binding: {
|
|
92
|
+
readonly moduleKey: string;
|
|
93
|
+
readonly authorization: NonNullable<
|
|
94
|
+
ModuleAuthoritativeInvalidationHostBinding["authorization"]
|
|
95
|
+
>;
|
|
96
|
+
readonly appendInTransaction: (
|
|
97
|
+
db: NodePgDatabase,
|
|
98
|
+
envelope: SourceInvalidationEnvelopeV1,
|
|
99
|
+
) => Promise<SourceInvalidationAppendResult>;
|
|
100
|
+
}): ModuleAuthoritativeInvalidationProducerPort {
|
|
101
|
+
const moduleKey = binding.moduleKey.trim();
|
|
102
|
+
const transaction = this.#transactions.get(moduleKey);
|
|
103
|
+
if (!this.#adapters.has(moduleKey) || !transaction) {
|
|
104
|
+
throw new ModuleAuthoritativeInvalidationUnavailableError();
|
|
105
|
+
}
|
|
106
|
+
return createModuleAuthoritativeInvalidationProducer({
|
|
107
|
+
moduleKey,
|
|
108
|
+
authorization: binding.authorization,
|
|
109
|
+
append: {
|
|
110
|
+
append: (envelope) => {
|
|
111
|
+
let db: NodePgDatabase | undefined;
|
|
112
|
+
try {
|
|
113
|
+
db = transaction.currentDatabase();
|
|
114
|
+
} catch {
|
|
115
|
+
throw new ModuleAuthoritativeInvalidationUnavailableError();
|
|
116
|
+
}
|
|
117
|
+
if (!db) throw new ModuleAuthoritativeInvalidationUnavailableError();
|
|
118
|
+
return binding.appendInTransaction(db, envelope);
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function denied(): AuthoritativeSourceAccessDecision {
|
|
126
|
+
return { outcome: "denied", reason: "object-forbidden" };
|
|
127
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { buildScopeContext } from "../../data-access/context.js";
|
|
3
|
+
import type { RequestGuardContext } from "../../guards.js";
|
|
4
|
+
import { ContextBuilder, contextDigest } from "./context-builder.js";
|
|
5
|
+
import type {
|
|
6
|
+
ContextBuildRequest,
|
|
7
|
+
ContextSourcePort,
|
|
8
|
+
ContextVersion,
|
|
9
|
+
} from "./contracts.js";
|
|
10
|
+
import { sha256, stableDigest } from "./digest.js";
|
|
11
|
+
|
|
12
|
+
describe("Context Builder", () => {
|
|
13
|
+
test("每次 Model attempt 先记录不可变 Context Version", async () => {
|
|
14
|
+
const recorded: ContextVersion[] = [];
|
|
15
|
+
const source: ContextSourcePort = {
|
|
16
|
+
resolve: async (request) => ({
|
|
17
|
+
role: "system",
|
|
18
|
+
reference: {
|
|
19
|
+
...request,
|
|
20
|
+
version: "event-sequence-7",
|
|
21
|
+
sequence: 7,
|
|
22
|
+
digest: stableDigest({ role: "system", content: "已授权内容" }),
|
|
23
|
+
validity: {
|
|
24
|
+
status: "valid",
|
|
25
|
+
checkedAt: "2026-08-27T00:00:00.000Z",
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
content: "已授权内容",
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
const builder = new ContextBuilder(
|
|
32
|
+
source,
|
|
33
|
+
{
|
|
34
|
+
record: async (version) => {
|
|
35
|
+
expect(Object.isFrozen(version)).toBeTrue();
|
|
36
|
+
recorded.push(version);
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
now: () => new Date("2026-08-27T00:00:01.000Z"),
|
|
41
|
+
idFactory: () => "context-1",
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
const request = buildRequest();
|
|
45
|
+
const built = await builder.build(request, new AbortController().signal);
|
|
46
|
+
|
|
47
|
+
expect(recorded).toHaveLength(1);
|
|
48
|
+
expect(built.version).toEqual(recorded[0]!);
|
|
49
|
+
expect(built.version.sources).toEqual([
|
|
50
|
+
{
|
|
51
|
+
moduleKey: "system.agent",
|
|
52
|
+
kind: "recent-event",
|
|
53
|
+
id: "event-7",
|
|
54
|
+
version: "event-sequence-7",
|
|
55
|
+
sequence: 7,
|
|
56
|
+
digest: stableDigest({ role: "system", content: "已授权内容" }),
|
|
57
|
+
validity: {
|
|
58
|
+
status: "valid",
|
|
59
|
+
checkedAt: "2026-08-27T00:00:00.000Z",
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
]);
|
|
63
|
+
expect(built.version.digest).toBe(
|
|
64
|
+
contextDigest(request, built.version.sources),
|
|
65
|
+
);
|
|
66
|
+
expect(Object.isFrozen(built)).toBeTrue();
|
|
67
|
+
expect(Object.isFrozen(built.sources[0]!.reference.validity)).toBeTrue();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("来源内容摘要不匹配或已过期时拒绝生成版本", async () => {
|
|
71
|
+
let records = 0;
|
|
72
|
+
const builder = new ContextBuilder(
|
|
73
|
+
{
|
|
74
|
+
resolve: async (request) => ({
|
|
75
|
+
role: "system",
|
|
76
|
+
reference: {
|
|
77
|
+
...request,
|
|
78
|
+
version: "v1",
|
|
79
|
+
sequence: 1,
|
|
80
|
+
digest: sha256("另一份内容"),
|
|
81
|
+
validity: {
|
|
82
|
+
status: "valid",
|
|
83
|
+
checkedAt: "2026-08-27T00:00:00.000Z",
|
|
84
|
+
expiresAt: "2026-08-27T00:00:01.000Z",
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
content: "实际内容",
|
|
88
|
+
}),
|
|
89
|
+
},
|
|
90
|
+
{ record: async () => void (records += 1) },
|
|
91
|
+
{ now: () => new Date("2026-08-27T00:00:02.000Z") },
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
await expect(
|
|
95
|
+
builder.build(buildRequest(), new AbortController().signal),
|
|
96
|
+
).rejects.toMatchObject({ code: "SOURCE_INVALID" });
|
|
97
|
+
expect(records).toBe(0);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("Session 事实源写入失败时不向 Model 暴露未记录 Context", async () => {
|
|
101
|
+
const builder = new ContextBuilder(
|
|
102
|
+
{
|
|
103
|
+
resolve: async (request) => ({
|
|
104
|
+
role: "system",
|
|
105
|
+
reference: {
|
|
106
|
+
...request,
|
|
107
|
+
version: "v1",
|
|
108
|
+
sequence: 1,
|
|
109
|
+
digest: stableDigest({ role: "system", content: "内容" }),
|
|
110
|
+
validity: {
|
|
111
|
+
status: "valid",
|
|
112
|
+
checkedAt: "2026-08-27T00:00:00.000Z",
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
content: "内容",
|
|
116
|
+
}),
|
|
117
|
+
},
|
|
118
|
+
{ record: async () => Promise.reject(new Error("store down")) },
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
await expect(
|
|
122
|
+
builder.build(buildRequest(), new AbortController().signal),
|
|
123
|
+
).rejects.toMatchObject({ code: "SOURCE_UNAVAILABLE" });
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
function buildRequest(): ContextBuildRequest {
|
|
128
|
+
return {
|
|
129
|
+
sessionId: "session-1",
|
|
130
|
+
turnId: "turn-1",
|
|
131
|
+
runId: "run-1",
|
|
132
|
+
modelAttemptId: "attempt-1",
|
|
133
|
+
guardContext: guardContext(),
|
|
134
|
+
sources: [
|
|
135
|
+
{ moduleKey: "system.agent", kind: "recent-event", id: "event-7" },
|
|
136
|
+
],
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function guardContext(): RequestGuardContext {
|
|
141
|
+
return {
|
|
142
|
+
requestId: "request-1",
|
|
143
|
+
startedAt: "2026-08-27T00:00:00.000Z",
|
|
144
|
+
actor: {
|
|
145
|
+
id: "actor-1",
|
|
146
|
+
type: "user",
|
|
147
|
+
name: "办案人员",
|
|
148
|
+
roleCodes: ["operator"],
|
|
149
|
+
permissionKeys: ["system.agent-session.read"],
|
|
150
|
+
scopeContext: buildScopeContext("actor-1", "department-1", [
|
|
151
|
+
{
|
|
152
|
+
id: "role-1",
|
|
153
|
+
code: "operator",
|
|
154
|
+
dataScope: "self",
|
|
155
|
+
departmentIds: [],
|
|
156
|
+
},
|
|
157
|
+
]),
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
}
|