@piagent/platform 1.3.0 → 1.4.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/CHANGELOG.md +56 -3
- package/README.md +3 -3
- package/architecture/layers.json +96 -1
- package/docs/README.md +1 -0
- package/docs/architecture.md +1 -0
- package/docs/authority-manifest.md +1 -0
- package/docs/auto-delegation-policy.md +1 -0
- package/docs/capability-packs.md +10 -2
- package/docs/command-reference-vietnamese.md +5 -4
- package/docs/context-engine.md +1 -0
- package/docs/context-window-policy.md +1 -0
- package/docs/distribution-standard.md +8 -7
- package/docs/herdr-workflow.md +1 -0
- package/docs/mcp-and-tools.md +14 -9
- package/docs/memory-policy.md +1 -0
- package/docs/model-options.md +1 -0
- package/docs/oauth-providers.md +1 -0
- package/docs/operator-manual-vietnamese.md +15 -12
- package/docs/package-architecture-notes.md +1 -0
- package/docs/project-adapters.md +1 -0
- package/docs/project-onboarding.md +1 -0
- package/docs/publishing-for-teams.md +1 -0
- package/docs/quality-benchmark.md +1 -0
- package/docs/quickstart-vietnamese.md +3 -2
- package/docs/release-install-policy.md +11 -7
- package/docs/runtime-harness-standard.md +1 -0
- package/docs/runtime-policy-design.md +1 -0
- package/docs/runtime-quality-baseline.md +1 -0
- package/docs/security-redaction-benchmark.md +1 -0
- package/docs/security-threat-model.md +2 -1
- package/docs/subagent-orchestration-capabilities.md +1 -0
- package/docs/subagents-and-multiagent.md +1 -0
- package/docs/task-implementation-contract.md +1 -0
- package/docs/task-lifecycle-vietnamese.md +1 -0
- package/docs/team-onboarding.md +3 -2
- package/docs/third-party-neutrality.md +26 -0
- package/docs/usage-observability.md +1 -0
- package/docs/vercel-docs-site.md +1 -0
- package/docs/workflow-recipes.md +1 -0
- package/docs-site/architecture.html +1 -1
- package/docs-site/benchmark.html +1 -1
- package/docs-site/commands.html +1 -1
- package/docs-site/context-engine.html +1 -1
- package/docs-site/en/architecture.html +1 -1
- package/docs-site/en/benchmark.html +1 -1
- package/docs-site/en/commands.html +1 -1
- package/docs-site/en/context-engine.html +1 -1
- package/docs-site/en/index.html +1 -1
- package/docs-site/en/mcp-approval.html +1 -1
- package/docs-site/en/mcp-auth.html +1 -1
- package/docs-site/en/mcp-servers.html +1 -1
- package/docs-site/en/mcp.html +1 -1
- package/docs-site/en/permissions.html +1 -1
- package/docs-site/en/profiles.html +1 -1
- package/docs-site/en/quickstart.html +1 -1
- package/docs-site/en/runtime.html +1 -1
- package/docs-site/en/scope.html +1 -1
- package/docs-site/en/security.html +1 -1
- package/docs-site/en/team.html +1 -1
- package/docs-site/en/whats-new.html +1 -1
- package/docs-site/en/workflows.html +1 -1
- package/docs-site/index.html +1 -1
- package/docs-site/mcp-approval.html +1 -1
- package/docs-site/mcp-auth.html +1 -1
- package/docs-site/mcp-servers.html +1 -1
- package/docs-site/mcp.html +1 -1
- package/docs-site/permissions.html +1 -1
- package/docs-site/profiles.html +1 -1
- package/docs-site/quickstart.html +1 -1
- package/docs-site/runtime.html +1 -1
- package/docs-site/scope.html +1 -1
- package/docs-site/security.html +1 -1
- package/docs-site/team.html +1 -1
- package/docs-site/whats-new.html +1 -1
- package/docs-site/workflows.html +1 -1
- package/evals/fixtures/mutation-provenance-record.invalid.json +17 -0
- package/evals/fixtures/mutation-provenance-record.valid.json +27 -0
- package/evals/fixtures/piagent-webui/approval-v1.invalid.json +54 -0
- package/evals/fixtures/piagent-webui/approval-v1.valid.json +58 -0
- package/evals/fixtures/piagent-webui/attachment-v1.invalid.json +25 -0
- package/evals/fixtures/piagent-webui/attachment-v1.valid.json +25 -0
- package/evals/fixtures/piagent-webui/capabilities-v1.invalid.json +51 -0
- package/evals/fixtures/piagent-webui/capabilities-v1.valid.json +122 -0
- package/evals/fixtures/piagent-webui/commit-summary-v1.invalid.json +10 -0
- package/evals/fixtures/piagent-webui/commit-summary-v1.valid.json +25 -0
- package/evals/fixtures/piagent-webui/control-command-v1.invalid.json +37 -0
- package/evals/fixtures/piagent-webui/control-command-v1.valid.json +35 -0
- package/evals/fixtures/piagent-webui/diff-v1.invalid.json +13 -0
- package/evals/fixtures/piagent-webui/diff-v1.valid.json +141 -0
- package/evals/fixtures/piagent-webui/gateway-capabilities-v1.invalid.json +35 -0
- package/evals/fixtures/piagent-webui/gateway-capabilities-v1.valid.json +35 -0
- package/evals/fixtures/piagent-webui/gateway-protocol-v1.invalid.json +14 -0
- package/evals/fixtures/piagent-webui/gateway-protocol-v1.valid.json +14 -0
- package/evals/fixtures/piagent-webui/handoff-history-v1.invalid.json +16 -0
- package/evals/fixtures/piagent-webui/handoff-history-v1.valid.json +16 -0
- package/evals/fixtures/piagent-webui/model-catalog-v1.invalid.json +31 -0
- package/evals/fixtures/piagent-webui/model-catalog-v1.valid.json +43 -0
- package/evals/fixtures/piagent-webui/queue-v1.invalid.json +29 -0
- package/evals/fixtures/piagent-webui/queue-v1.valid.json +44 -0
- package/evals/fixtures/piagent-webui/recovery-history-v1.invalid.json +17 -0
- package/evals/fixtures/piagent-webui/recovery-history-v1.valid.json +20 -0
- package/evals/fixtures/piagent-webui/release-monitor-v1.invalid.json +12 -0
- package/evals/fixtures/piagent-webui/release-monitor-v1.valid.json +29 -0
- package/evals/fixtures/piagent-webui/review-state-v1.invalid.json +25 -0
- package/evals/fixtures/piagent-webui/review-state-v1.valid.json +38 -0
- package/evals/fixtures/piagent-webui/runtime-event-v2.invalid.json +49 -0
- package/evals/fixtures/piagent-webui/runtime-event-v2.valid.json +49 -0
- package/evals/fixtures/piagent-webui/session-catalog-v1.invalid.json +17 -0
- package/evals/fixtures/piagent-webui/session-catalog-v1.valid.json +58 -0
- package/evals/fixtures/piagent-webui/session-command-v1.invalid.json +19 -0
- package/evals/fixtures/piagent-webui/session-command-v1.valid.json +19 -0
- package/evals/fixtures/piagent-webui/snapshot-v1.invalid.json +5 -0
- package/evals/fixtures/piagent-webui/snapshot-v1.valid.json +191 -0
- package/evals/fixtures/piagent-webui/source-change-v1.invalid.json +57 -0
- package/evals/fixtures/piagent-webui/source-change-v1.valid.json +98 -0
- package/evals/fixtures/piagent-webui/source-mutation-v1.invalid.json +19 -0
- package/evals/fixtures/piagent-webui/source-mutation-v1.valid.json +38 -0
- package/evals/fixtures/piagent-webui/source-revert-v1.invalid.json +20 -0
- package/evals/fixtures/piagent-webui/source-revert-v1.valid.json +62 -0
- package/evals/fixtures/piagent-webui/subagent-tree-v1.invalid.json +26 -0
- package/evals/fixtures/piagent-webui/subagent-tree-v1.valid.json +39 -0
- package/evals/fixtures/piagent-webui/task-index-v1.invalid.json +15 -0
- package/evals/fixtures/piagent-webui/task-index-v1.valid.json +15 -0
- package/evals/fixtures/piagent-webui/task-timeline-v1.invalid.json +14 -0
- package/evals/fixtures/piagent-webui/task-timeline-v1.valid.json +16 -0
- package/evals/fixtures/piagent-webui/transcript-v1.invalid.json +35 -0
- package/evals/fixtures/piagent-webui/transcript-v1.valid.json +55 -0
- package/evals/fixtures/task-baseline-manifest.invalid.json +16 -0
- package/evals/fixtures/task-baseline-manifest.valid.json +41 -0
- package/evals/fixtures/verifier-file-snapshot.invalid.json +21 -0
- package/evals/fixtures/verifier-file-snapshot.valid.json +21 -0
- package/package.json +30 -3
- package/packages/piagent-core/benchmark/benchmark-candidate.d.ts +1 -0
- package/packages/piagent-core/benchmark/benchmark-forensics.d.ts +1 -0
- package/packages/piagent-core/benchmark/benchmark-ledger.d.ts +4 -0
- package/packages/piagent-core/benchmark/benchmark-record-validation.d.ts +1 -0
- package/packages/piagent-core/benchmark/benchmark-runtime.d.ts +1 -0
- package/packages/piagent-core/capabilities/capability-core.js +2 -2
- package/packages/piagent-core/capabilities/capability-verification-cache.js +280 -0
- package/packages/piagent-core/capabilities/runtime-integrity.js +153 -1
- package/packages/piagent-core/extensions/document-intake.ts +24 -10
- package/packages/piagent-core/extensions/piagent-guard.ts +90 -73
- package/packages/piagent-core/extensions/redaction-core.d.ts +2 -0
- package/packages/piagent-core/extensions/state-retention.js +77 -44
- package/packages/piagent-core/extensions/task-journal.js +30 -1
- package/packages/piagent-core/extensions/task-state.js +33 -36
- package/packages/piagent-core/mcp/mcp-server-catalog.js +5 -5
- package/packages/piagent-core/package.json +1 -1
- package/packages/piagent-core/runtime/hooks/session-start-hook.ts +2 -2
- package/packages/piagent-core/runtime/hooks/tool-call-hook.ts +5 -2
- package/packages/piagent-core/runtime/hooks/tool-result-hook.ts +20 -19
- package/packages/piagent-core/runtime/hooks/tool-result-metadata.ts +13 -0
- package/packages/piagent-core/runtime/input/chat-images.ts +2 -2
- package/packages/piagent-core/runtime/inspection/activity-event-adapter.ts +94 -0
- package/packages/piagent-core/runtime/inspection/approval-broker.ts +318 -0
- package/packages/piagent-core/runtime/inspection/commit-summary-projection.ts +59 -0
- package/packages/piagent-core/runtime/inspection/context-telemetry-inspection.ts +37 -0
- package/packages/piagent-core/runtime/inspection/criteria-links.ts +295 -0
- package/packages/piagent-core/runtime/inspection/diff-projection.ts +499 -0
- package/packages/piagent-core/runtime/inspection/git-filter-safety.ts +38 -0
- package/packages/piagent-core/runtime/inspection/git-numstat-adapter.ts +71 -0
- package/packages/piagent-core/runtime/inspection/git-status-adapter.ts +493 -0
- package/packages/piagent-core/runtime/inspection/git-tree-adapter.ts +108 -0
- package/packages/piagent-core/runtime/inspection/mcp-control.ts +10 -0
- package/packages/piagent-core/runtime/inspection/mutation-provenance-contract.ts +131 -0
- package/packages/piagent-core/runtime/inspection/mutation-provenance-recorder.ts +48 -0
- package/packages/piagent-core/runtime/inspection/mutation-provenance-store.ts +161 -0
- package/packages/piagent-core/runtime/inspection/project-runtime-inspection.ts +36 -0
- package/packages/piagent-core/runtime/inspection/review-state-contract.ts +108 -0
- package/packages/piagent-core/runtime/inspection/review-state-projection.ts +93 -0
- package/packages/piagent-core/runtime/inspection/review-state-store.ts +112 -0
- package/packages/piagent-core/runtime/inspection/runtime-event-store.ts +262 -0
- package/packages/piagent-core/runtime/inspection/same-process-bridge-proof.ts +103 -0
- package/packages/piagent-core/runtime/inspection/source-change-projection.ts +471 -0
- package/packages/piagent-core/runtime/inspection/source-evidence-contract.ts +139 -0
- package/packages/piagent-core/runtime/inspection/source-evidence-store.ts +315 -0
- package/packages/piagent-core/runtime/inspection/source-handoff-store.ts +70 -0
- package/packages/piagent-core/runtime/inspection/source-inspection-plan.ts +34 -0
- package/packages/piagent-core/runtime/inspection/source-mutation-projection.ts +164 -0
- package/packages/piagent-core/runtime/inspection/source-mutation-store.ts +168 -0
- package/packages/piagent-core/runtime/inspection/source-open-target.ts +38 -0
- package/packages/piagent-core/runtime/inspection/source-revert-projection.ts +147 -0
- package/packages/piagent-core/runtime/inspection/task-baseline-start-capture.ts +22 -0
- package/packages/piagent-core/runtime/inspection/task-compaction-history.ts +160 -0
- package/packages/piagent-core/runtime/inspection/task-control-journal.ts +131 -0
- package/packages/piagent-core/runtime/inspection/task-handoff-history.ts +95 -0
- package/packages/piagent-core/runtime/inspection/task-provenance-projection.ts +60 -0
- package/packages/piagent-core/runtime/inspection/task-recovery-timeline.ts +88 -0
- package/packages/piagent-core/runtime/inspection/task-run-index.ts +87 -0
- package/packages/piagent-core/runtime/inspection/task-source-projection.ts +401 -0
- package/packages/piagent-core/runtime/inspection/task-subagent-tree.ts +101 -0
- package/packages/piagent-core/runtime/inspection/text-diff.ts +60 -0
- package/packages/piagent-core/runtime/inspection/verifier-snapshot-contract.ts +114 -0
- package/packages/piagent-core/runtime/inspection/verifier-snapshot-store.ts +189 -0
- package/packages/piagent-core/runtime/inspection/webui-snapshot.ts +320 -0
- package/packages/piagent-core/runtime/inspection/workspace-file-reader.ts +132 -0
- package/packages/piagent-core/runtime/inspection/workspace-source-projection.ts +96 -0
- package/packages/piagent-core/runtime/inspection/zero-turn-conformance.ts +175 -0
- package/packages/piagent-core/runtime/orchestration/owned-work-budget.ts +68 -4
- package/packages/piagent-core/runtime/policy/source-index-transaction.ts +173 -0
- package/packages/piagent-core/runtime/policy/source-mutation-guard-binding.ts +42 -0
- package/packages/piagent-core/runtime/policy/source-mutation-guard.ts +184 -0
- package/packages/piagent-core/runtime/policy/source-worktree-transaction.ts +105 -0
- package/packages/piagent-core/runtime/product/activity-inspector-footer.ts +5 -3
- package/packages/piagent-core/runtime/product/activity-inspector.ts +85 -343
- package/packages/piagent-core/runtime/recovery/resume-state.ts +38 -1
- package/packages/piagent-core/runtime/registration/activity-inspector-command.ts +68 -14
- package/packages/piagent-core/runtime/registration/task-start-tool.ts +13 -14
- package/packages/piagent-core/runtime/session/model-authorship-state.ts +31 -3
- package/packages/piagent-core/runtime/session/runtime-state.ts +11 -1
- package/packages/piagent-webui/dist/client/assets/index-DjMntaK5.js +172 -0
- package/packages/piagent-webui/dist/client/assets/index-DwKCWbwd.css +1 -0
- package/packages/piagent-webui/dist/client/index.html +16 -0
- package/packages/piagent-webui/extension/attachment-store.ts +323 -0
- package/packages/piagent-webui/extension/held-message-queue.ts +311 -0
- package/packages/piagent-webui/extension/lifecycle-controller.ts +408 -0
- package/packages/piagent-webui/extension/lifecycle-event-adapter.ts +23 -0
- package/packages/piagent-webui/extension/piagent-webui.ts +470 -0
- package/packages/piagent-webui/extension/review-controller.ts +160 -0
- package/packages/piagent-webui/extension/same-session-bridge.ts +500 -0
- package/packages/piagent-webui/extension/session-options-controller.ts +281 -0
- package/packages/piagent-webui/extension/session-stream-adapter.ts +249 -0
- package/packages/piagent-webui/extension/source-mutation-controller.ts +215 -0
- package/packages/piagent-webui/extension/source-open-controller.ts +136 -0
- package/packages/piagent-webui/extension/source-revert-controller.ts +189 -0
- package/packages/piagent-webui/extension/terminal-session-adapter.ts +73 -0
- package/packages/piagent-webui/extension/vscode-handoff.ts +51 -0
- package/packages/piagent-webui/gateway/control-socket.ts +92 -0
- package/packages/piagent-webui/gateway/gateway-events.ts +60 -0
- package/packages/piagent-webui/gateway/gateway-protocol-service.ts +64 -0
- package/packages/piagent-webui/gateway/gateway-service.ts +215 -0
- package/packages/piagent-webui/gateway/gateway-session-stream.ts +110 -0
- package/packages/piagent-webui/gateway/mcp-auth-broker.ts +158 -0
- package/packages/piagent-webui/gateway/native-project-picker.ts +29 -0
- package/packages/piagent-webui/gateway/pi-host.ts +41 -0
- package/packages/piagent-webui/gateway/profile-state.ts +1 -0
- package/packages/piagent-webui/gateway/project-registry.ts +108 -0
- package/packages/piagent-webui/gateway/provider-auth-broker.ts +172 -0
- package/packages/piagent-webui/gateway/session-catalog.ts +128 -0
- package/packages/piagent-webui/gateway/session-command-controller.ts +237 -0
- package/packages/piagent-webui/gateway/session-command-store.ts +224 -0
- package/packages/piagent-webui/gateway/session-inspection-registry.ts +204 -0
- package/packages/piagent-webui/gateway/session-lease-store.ts +1 -0
- package/packages/piagent-webui/gateway/session-metadata-store.ts +152 -0
- package/packages/piagent-webui/gateway/session-runtime-supervisor.ts +485 -0
- package/packages/piagent-webui/ownership/gateway-runtime-context.ts +8 -0
- package/packages/piagent-webui/ownership/profile-state.ts +117 -0
- package/packages/piagent-webui/ownership/session-lease-store.ts +329 -0
- package/packages/piagent-webui/ownership/session-refs.ts +8 -0
- package/packages/piagent-webui/package.json +33 -0
- package/packages/piagent-webui/server/README.md +4 -0
- package/packages/piagent-webui/server/benchmark-release-monitor.ts +230 -0
- package/packages/piagent-webui/server/core-inspection-provider.ts +403 -0
- package/packages/piagent-webui/server/gateway-websocket.ts +143 -0
- package/packages/piagent-webui/server/http-security.ts +27 -0
- package/packages/piagent-webui/server/ipc-read-model-client.ts +89 -0
- package/packages/piagent-webui/server/loopback-server.ts +368 -0
- package/packages/piagent-webui/server/read-model-provider.ts +37 -0
- package/packages/piagent-webui/server/read-only-router.ts +105 -0
- package/packages/piagent-webui/server/session-auth.ts +88 -0
- package/packages/piagent-webui/server/sidecar-main.ts +86 -0
- package/packages/piagent-webui/server/sse-hub.ts +76 -0
- package/packages/piagent-webui/server/static-bundle.ts +40 -0
- package/packages/piagent-webui/server/transcript-projection.ts +110 -0
- package/schemas/mutation-provenance-record.schema.json +100 -0
- package/schemas/piagent-webui/approval-v1.schema.json +654 -0
- package/schemas/piagent-webui/attachment-v1.schema.json +267 -0
- package/schemas/piagent-webui/capabilities-v1.schema.json +729 -0
- package/schemas/piagent-webui/catalog-v1.json +240 -0
- package/schemas/piagent-webui/commit-summary-v1.schema.json +63 -0
- package/schemas/piagent-webui/common-v1.schema.json +479 -0
- package/schemas/piagent-webui/control-command-v1.schema.json +1780 -0
- package/schemas/piagent-webui/diff-v1.schema.json +826 -0
- package/schemas/piagent-webui/gateway-capabilities-v1.schema.json +136 -0
- package/schemas/piagent-webui/gateway-protocol-v1.schema.json +456 -0
- package/schemas/piagent-webui/handoff-history-v1.schema.json +92 -0
- package/schemas/piagent-webui/model-catalog-v1.schema.json +95 -0
- package/schemas/piagent-webui/queue-v1.schema.json +99 -0
- package/schemas/piagent-webui/recovery-history-v1.schema.json +144 -0
- package/schemas/piagent-webui/release-monitor-v1.schema.json +173 -0
- package/schemas/piagent-webui/review-state-v1.schema.json +206 -0
- package/schemas/piagent-webui/runtime-event-v2.schema.json +5206 -0
- package/schemas/piagent-webui/session-catalog-v1.schema.json +222 -0
- package/schemas/piagent-webui/session-command-v1.schema.json +390 -0
- package/schemas/piagent-webui/snapshot-v1.schema.json +2622 -0
- package/schemas/piagent-webui/source-change-v1.schema.json +763 -0
- package/schemas/piagent-webui/source-mutation-v1.schema.json +96 -0
- package/schemas/piagent-webui/source-revert-v1.schema.json +130 -0
- package/schemas/piagent-webui/subagent-tree-v1.schema.json +88 -0
- package/schemas/piagent-webui/task-index-v1.schema.json +74 -0
- package/schemas/piagent-webui/task-timeline-v1.schema.json +49 -0
- package/schemas/piagent-webui/transcript-v1.schema.json +243 -0
- package/schemas/task-baseline-manifest.schema.json +122 -0
- package/schemas/verifier-file-snapshot.schema.json +81 -0
- package/scripts/check-architecture.mjs +1 -0
- package/scripts/check-doc-languages.mjs +19 -2
- package/scripts/check-third-party-neutrality.mjs +43 -0
- package/scripts/install-global.sh +1 -1
- package/scripts/piagent-cli.mjs +17 -3
- package/scripts/piagent-dashboard.mjs +208 -0
- package/scripts/piagent-webui-launcher.mjs +121 -0
- package/scripts/typescript-loader.mjs +26 -1
- package/scripts/verify-local.sh +253 -0
- package/scripts/verify-release-identity.mjs +4 -1
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import { redactSensitiveText } from "../../piagent-core/extensions/redaction-core.js";
|
|
4
|
+
import {
|
|
5
|
+
chatContentDigest,
|
|
6
|
+
controlActionDigest,
|
|
7
|
+
type BridgeIdentity,
|
|
8
|
+
type BridgeRevisions,
|
|
9
|
+
type ChatReceipt,
|
|
10
|
+
type NewOperationChatCommand,
|
|
11
|
+
type SameSessionPiBridge
|
|
12
|
+
} from "./same-session-bridge.ts";
|
|
13
|
+
|
|
14
|
+
export const WEBUI_QUEUE_ENTRY_TYPE = "piagent-webui-held-queue-v1";
|
|
15
|
+
const REF = /^[A-Za-z0-9][A-Za-z0-9._~-]{0,159}$/;
|
|
16
|
+
const PUBLIC_REF = /^[A-Za-z0-9][A-Za-z0-9._:@~-]{0,159}$/;
|
|
17
|
+
const REVISION = /^[A-Za-z0-9][A-Za-z0-9._:~-]{0,159}$/;
|
|
18
|
+
const DIGEST = /^sha256:[a-f0-9]{64}$/;
|
|
19
|
+
const IDEMPOTENCY = /^[A-Za-z0-9._~-]{32,128}$/;
|
|
20
|
+
const MAX_ITEMS = 100;
|
|
21
|
+
|
|
22
|
+
type QueueAction = "queue.update" | "queue.delete" | "queue.dispatch";
|
|
23
|
+
export type QueueControlReceipt = Omit<ChatReceipt, "action" | "resultCode"> & {
|
|
24
|
+
action: "chat.send" | QueueAction;
|
|
25
|
+
resultCode: ChatReceipt["resultCode"] | "held" | "updated" | "deleted";
|
|
26
|
+
};
|
|
27
|
+
type QueueCommand = {
|
|
28
|
+
schemaVersion: 1; version: "piagent-webui-control-v1"; messageType: "command"; commandId: string; idempotencyKey: string;
|
|
29
|
+
requestedAt: string; expiresAt: string; capabilityScope: "control.chat"; action: "chat.send" | QueueAction; actionDigest: string;
|
|
30
|
+
identity: BridgeIdentity; expectedRevisions: BridgeRevisions & { workspacePreimage: null; indexPreimage: null; patchPreimage: null };
|
|
31
|
+
payload: Record<string, unknown>;
|
|
32
|
+
};
|
|
33
|
+
type QueueItem = {
|
|
34
|
+
queueItemRef: string; messageRequestId: string; text: string; attachmentRefs: string[]; contentDigest: string;
|
|
35
|
+
state: "held" | "quarantined"; createdAt: string; updatedAt: string; reasonCode: string | null;
|
|
36
|
+
};
|
|
37
|
+
type QueueProjectionItem = {
|
|
38
|
+
queueItemRef: string; messageRequestId: string; position: number; state: "held" | "quarantined"; preview: string; redacted: boolean;
|
|
39
|
+
truncated: boolean; attachmentCount: number; previewDigest: string; createdAt: string; updatedAt: string; reasonCode: string | null;
|
|
40
|
+
};
|
|
41
|
+
type QueueProjection = {
|
|
42
|
+
schemaVersion: 1; version: "piagent-webui-queue-v1"; generatedAt: string; identity: BridgeIdentity; revision: BridgeRevisions;
|
|
43
|
+
state: "ready" | "unavailable"; heldCount: number; quarantinedCount: number; items: QueueProjectionItem[];
|
|
44
|
+
reasonCode: string | null;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
function sha(value: string): string { return `sha256:${createHash("sha256").update(value).digest("hex")}`; }
|
|
48
|
+
function canonical(value: unknown): string {
|
|
49
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
50
|
+
if (value && typeof value === "object") return `{${Object.entries(value as Record<string, unknown>).sort(([a], [b]) => a.localeCompare(b))
|
|
51
|
+
.map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`).join(",")}}`;
|
|
52
|
+
return JSON.stringify(value);
|
|
53
|
+
}
|
|
54
|
+
function copy<T>(value: T): T { return structuredClone(value); }
|
|
55
|
+
function same(left: unknown, right: unknown): boolean { return canonical(left) === canonical(right); }
|
|
56
|
+
function exactKeys(value: unknown, keys: string[]): boolean {
|
|
57
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value)
|
|
58
|
+
&& same(Object.keys(value as object).sort(), [...keys].sort()));
|
|
59
|
+
}
|
|
60
|
+
function timestamp(value: unknown): value is string {
|
|
61
|
+
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(value)) return false;
|
|
62
|
+
const parsed = Date.parse(value);
|
|
63
|
+
return Number.isFinite(parsed) && new Date(parsed).toISOString() === value;
|
|
64
|
+
}
|
|
65
|
+
function validIdentity(value: unknown): value is BridgeIdentity {
|
|
66
|
+
if (!exactKeys(value, ["projectRef", "runtimeInstanceId", "sessionRef", "taskId", "taskRunId", "agentOperationId", "toolCallId"])) return false;
|
|
67
|
+
const identity = value as BridgeIdentity;
|
|
68
|
+
return [identity.projectRef, identity.runtimeInstanceId, identity.sessionRef].every((item) => typeof item === "string" && REF.test(item))
|
|
69
|
+
&& [identity.taskId, identity.taskRunId].every((item) => item === null || PUBLIC_REF.test(item))
|
|
70
|
+
&& (identity.agentOperationId === null || REF.test(identity.agentOperationId)) && identity.toolCallId === null
|
|
71
|
+
&& (identity.taskRunId === null || identity.taskId !== null);
|
|
72
|
+
}
|
|
73
|
+
function validRevisions(value: unknown): value is QueueCommand["expectedRevisions"] {
|
|
74
|
+
if (!exactKeys(value, ["runtimeRevision", "taskRevision", "controlRevision", "workspaceRevision", "indexRevision", "approvalRevision",
|
|
75
|
+
"sessionOptionRevision", "queueRevision", "workspacePreimage", "indexPreimage", "patchPreimage"])) return false;
|
|
76
|
+
const revisions = value as QueueCommand["expectedRevisions"];
|
|
77
|
+
return REVISION.test(revisions.runtimeRevision) && REVISION.test(revisions.queueRevision ?? "")
|
|
78
|
+
&& [revisions.taskRevision, revisions.controlRevision, revisions.workspaceRevision, revisions.indexRevision, revisions.approvalRevision,
|
|
79
|
+
revisions.sessionOptionRevision].every((item) => item === null || REVISION.test(item))
|
|
80
|
+
&& revisions.workspacePreimage === null && revisions.indexPreimage === null && revisions.patchPreimage === null;
|
|
81
|
+
}
|
|
82
|
+
function safePreview(text: string): { preview: string; redacted: boolean; truncated: boolean } {
|
|
83
|
+
const cleaned = text.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]/g, "");
|
|
84
|
+
const redacted = redactSensitiveText(cleaned);
|
|
85
|
+
return { preview: redacted.text.slice(0, 4_000), redacted: redacted.redacted, truncated: redacted.text.length > 4_000 };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export class HeldMessageQueue {
|
|
89
|
+
readonly #bridge: SameSessionPiBridge;
|
|
90
|
+
readonly #appendEntry: (customType: string, data: unknown) => void;
|
|
91
|
+
readonly #now: () => Date;
|
|
92
|
+
#sessionRef: string | null = null;
|
|
93
|
+
#lastIdentity: BridgeIdentity | null = null;
|
|
94
|
+
#lastRevisions: BridgeRevisions | null = null;
|
|
95
|
+
#items: QueueItem[] = [];
|
|
96
|
+
#receiptsByKey = new Map<string, QueueControlReceipt>();
|
|
97
|
+
#receiptsByCommand = new Map<string, QueueControlReceipt>();
|
|
98
|
+
#serial: Promise<unknown> = Promise.resolve();
|
|
99
|
+
|
|
100
|
+
constructor(options: { bridge: SameSessionPiBridge; appendEntry(customType: string, data: unknown): void; now?: () => Date }) {
|
|
101
|
+
this.#bridge = options.bridge; this.#appendEntry = options.appendEntry; this.#now = options.now ?? (() => new Date());
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
reset(): void {
|
|
105
|
+
this.#sessionRef = null; this.#lastIdentity = null; this.#lastRevisions = null; this.#items = [];
|
|
106
|
+
this.#receiptsByKey.clear(); this.#receiptsByCommand.clear();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#sync(): ReturnType<SameSessionPiBridge["snapshot"]> {
|
|
110
|
+
const snapshot = this.#bridge.snapshot();
|
|
111
|
+
if (snapshot.identity && snapshot.revisions) {
|
|
112
|
+
if (this.#sessionRef && this.#sessionRef !== snapshot.identity.sessionRef) this.reset();
|
|
113
|
+
this.#sessionRef = snapshot.identity.sessionRef; this.#lastIdentity = copy(snapshot.identity); this.#lastRevisions = copy(snapshot.revisions);
|
|
114
|
+
}
|
|
115
|
+
return snapshot;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
#projectItem(item: QueueItem, position: number): QueueProjectionItem {
|
|
119
|
+
const preview = safePreview(item.text);
|
|
120
|
+
return { queueItemRef: item.queueItemRef, messageRequestId: item.messageRequestId, position, state: item.state, ...preview,
|
|
121
|
+
attachmentCount: item.attachmentRefs.length, previewDigest: sha(canonical({ ...preview, attachmentCount: item.attachmentRefs.length })),
|
|
122
|
+
createdAt: item.createdAt, updatedAt: item.updatedAt,
|
|
123
|
+
reasonCode: item.reasonCode };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#evidence(command: QueueCommand, receipt: QueueControlReceipt, item: QueueItem, position: number, mutationRef: string): Record<string, unknown> {
|
|
127
|
+
return { schemaVersion: 1, mutationRef, commandId: command.commandId, action: command.action, resultCode: receipt.resultCode,
|
|
128
|
+
queueRevision: receipt.observedRevisionsAfter.queueRevision, item: this.#projectItem(item, position) };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
projection(): QueueProjection {
|
|
132
|
+
const snapshot = this.#sync(), ready = snapshot.state === "ready" && Boolean(snapshot.identity && snapshot.revisions);
|
|
133
|
+
if (!this.#lastIdentity || !this.#lastRevisions) throw new Error("webui-held-queue-identity-unavailable");
|
|
134
|
+
const items = ready ? this.#items.map((item, position) => this.#projectItem(item, position)) : [];
|
|
135
|
+
return { schemaVersion: 1, version: "piagent-webui-queue-v1", generatedAt: this.#now().toISOString(), identity: copy(snapshot.identity ?? this.#lastIdentity),
|
|
136
|
+
revision: copy(snapshot.revisions ?? this.#lastRevisions), state: ready ? "ready" : "unavailable",
|
|
137
|
+
heldCount: items.filter((item) => item.state === "held").length, quarantinedCount: items.filter((item) => item.state === "quarantined").length,
|
|
138
|
+
items, reasonCode: ready ? null : "bridge-binding-not-ready" };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
snapshot(): { heldCount: number; quarantinedCount: number; queueRevision: string | null } {
|
|
142
|
+
const projection = this.projection();
|
|
143
|
+
return { heldCount: projection.heldCount, quarantinedCount: projection.quarantinedCount, queueRevision: projection.revision.queueRevision };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
execute(command: unknown): Promise<QueueControlReceipt> {
|
|
147
|
+
const run = this.#serial.then(() => this.#execute(command), () => this.#execute(command));
|
|
148
|
+
this.#serial = run.catch(() => undefined); return run;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async #execute(input: unknown): Promise<QueueControlReceipt> {
|
|
152
|
+
const command = input as QueueCommand;
|
|
153
|
+
if (command?.action === "chat.send" && (command.payload as any)?.delivery !== "hold") return this.#bridge.execute(command as unknown as NewOperationChatCommand);
|
|
154
|
+
const snapshot = this.#sync(), structural = this.#validate(command);
|
|
155
|
+
if (snapshot.state !== "ready" || !snapshot.identity || !snapshot.revisions)
|
|
156
|
+
return this.#reject(command, "resync-required", "bridge-binding-not-ready", "The current Pi session binding is unavailable.");
|
|
157
|
+
if (structural) return this.#reject(command, "invalid-command", structural, "The queue command is invalid.");
|
|
158
|
+
const keyDigest = sha(command.idempotencyKey), old = this.#receiptsByKey.get(keyDigest) ?? this.#receiptsByCommand.get(command.commandId);
|
|
159
|
+
if (old) {
|
|
160
|
+
if (old.commandId !== command.commandId || old.actionDigest !== command.actionDigest || old.idempotencyKeyDigest !== keyDigest)
|
|
161
|
+
return this.#reject(command, "idempotency-payload-mismatch", "idempotency-payload-mismatch", "The command key is already bound to another payload.");
|
|
162
|
+
return { ...copy(old), deduplicated: true };
|
|
163
|
+
}
|
|
164
|
+
const now = this.#now().getTime();
|
|
165
|
+
if (Date.parse(command.requestedAt) > now + 30_000) return this.#reject(command, "invalid-command", "requested-at-in-future", "The command timestamp is not yet valid.");
|
|
166
|
+
if (now > Date.parse(command.expiresAt)) return this.#reject(command, "expired", "command-expired", "The command expired before execution.");
|
|
167
|
+
if (!same(command.identity, snapshot.identity)) return this.#reject(command, "identity-mismatch", "identity-mismatch", "The command does not target this Pi session.");
|
|
168
|
+
if (!["runtimeRevision", "taskRevision", "controlRevision", "queueRevision"].every((key) =>
|
|
169
|
+
(command.expectedRevisions as any)[key] === (snapshot.revisions as any)[key]))
|
|
170
|
+
return this.#reject(command, "stale-revision", "stale-revision", "The queue changed before this command.");
|
|
171
|
+
if (command.action === "chat.send") return this.#hold(command);
|
|
172
|
+
if (command.action === "queue.update") return this.#update(command);
|
|
173
|
+
if (command.action === "queue.delete") return this.#delete(command);
|
|
174
|
+
return this.#dispatch(command);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#validate(command: QueueCommand): string | null {
|
|
178
|
+
if (!command || typeof command !== "object" || command.schemaVersion !== 1 || command.version !== "piagent-webui-control-v1"
|
|
179
|
+
|| command.messageType !== "command" || command.capabilityScope !== "control.chat"
|
|
180
|
+
|| !["chat.send", "queue.update", "queue.delete", "queue.dispatch"].includes(command.action)) return "unsupported-command-shape";
|
|
181
|
+
if (!exactKeys(command, ["schemaVersion", "version", "messageType", "commandId", "idempotencyKey", "requestedAt", "expiresAt", "capabilityScope",
|
|
182
|
+
"action", "actionDigest", "identity", "expectedRevisions", "payload"]) || !validIdentity(command.identity) || !validRevisions(command.expectedRevisions)
|
|
183
|
+
|| !REF.test(command.commandId) || !IDEMPOTENCY.test(command.idempotencyKey) || !timestamp(command.requestedAt) || !timestamp(command.expiresAt)
|
|
184
|
+
|| Date.parse(command.requestedAt) > Date.parse(command.expiresAt) || Date.parse(command.expiresAt) - Date.parse(command.requestedAt) > 300_000)
|
|
185
|
+
return "invalid-command-metadata";
|
|
186
|
+
const payload = command.payload;
|
|
187
|
+
if (command.action === "chat.send") {
|
|
188
|
+
if (!exactKeys(payload, ["messageRequestId", "capabilityAction", "delivery", "text", "attachmentRefs", "contentDigest"])
|
|
189
|
+
|| payload.capabilityAction !== "hold" || payload.delivery !== "hold" || typeof payload.messageRequestId !== "string" || !REF.test(payload.messageRequestId)
|
|
190
|
+
|| typeof payload.text !== "string" || payload.text.length < 1 || Buffer.byteLength(payload.text) > 65_536 || payload.text.includes("\0")
|
|
191
|
+
|| !Array.isArray(payload.attachmentRefs) || payload.attachmentRefs.length > 0 || payload.contentDigest !== chatContentDigest(payload as any)) return "invalid-hold-payload";
|
|
192
|
+
} else if (command.action === "queue.update") {
|
|
193
|
+
if (!exactKeys(payload, ["queueItemRef", "text", "attachmentRefs", "contentDigest"]) || typeof payload.queueItemRef !== "string" || !REF.test(payload.queueItemRef)
|
|
194
|
+
|| typeof payload.text !== "string" || payload.text.length < 1 || Buffer.byteLength(payload.text) > 65_536 || payload.text.includes("\0")
|
|
195
|
+
|| !Array.isArray(payload.attachmentRefs) || payload.attachmentRefs.length > 0 || payload.contentDigest !== chatContentDigest(payload as any)) return "invalid-update-payload";
|
|
196
|
+
} else if (command.action === "queue.delete") {
|
|
197
|
+
if (!exactKeys(payload, ["queueItemRef"]) || typeof payload.queueItemRef !== "string" || !REF.test(payload.queueItemRef)) return "invalid-delete-payload";
|
|
198
|
+
} else if (!exactKeys(payload, ["queueItemRef", "messageRequestId"]) || typeof payload.queueItemRef !== "string" || !REF.test(payload.queueItemRef)
|
|
199
|
+
|| typeof payload.messageRequestId !== "string" || !REF.test(payload.messageRequestId)) return "invalid-dispatch-payload";
|
|
200
|
+
if (!DIGEST.test(command.actionDigest) || command.actionDigest !== controlActionDigest(command)) return "action-digest-mismatch";
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
#hold(command: QueueCommand): QueueControlReceipt {
|
|
205
|
+
if (this.#items.length >= MAX_ITEMS) return this.#reject(command, "capability-unavailable", "held-queue-full", "The held message queue is full.");
|
|
206
|
+
if (this.#items.some((item) => item.messageRequestId === command.payload.messageRequestId))
|
|
207
|
+
return this.#reject(command, "replay", "message-request-replay", "This held message request already exists.");
|
|
208
|
+
const at = this.#now().toISOString(), item: QueueItem = { queueItemRef: this.#opaqueRef("queue-item"),
|
|
209
|
+
messageRequestId: command.payload.messageRequestId as string, text: command.payload.text as string, attachmentRefs: [],
|
|
210
|
+
contentDigest: command.payload.contentDigest as string, state: "held", createdAt: at, updatedAt: at, reasonCode: null };
|
|
211
|
+
let receipt: QueueControlReceipt | null = null;
|
|
212
|
+
const mutationRef = this.#opaqueRef("queue-mutation");
|
|
213
|
+
const committed = this.#bridge.commitQueueMutation((before, after) => {
|
|
214
|
+
receipt = this.#receipt(command, before, after, "accepted", "held", null, null);
|
|
215
|
+
this.#appendEntry(WEBUI_QUEUE_ENTRY_TYPE, this.#evidence(command, receipt, item, this.#items.length, mutationRef));
|
|
216
|
+
});
|
|
217
|
+
if (!committed || !receipt) return this.#reject(command, "capability-unavailable", "queue-evidence-unavailable", "The held message could not be recorded.");
|
|
218
|
+
this.#items.push(item); return this.#remember(receipt);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
#update(command: QueueCommand): QueueControlReceipt {
|
|
222
|
+
const index = this.#items.findIndex((item) => item.queueItemRef === command.payload.queueItemRef);
|
|
223
|
+
if (index < 0 || this.#items[index].state !== "held") return this.#reject(command, "capability-unavailable", "queue-item-unavailable", "The held message is unavailable.");
|
|
224
|
+
const projection = this.#projectItem(this.#items[index], index);
|
|
225
|
+
if (projection.redacted || projection.truncated)
|
|
226
|
+
return this.#reject(command, "capability-unavailable", "queue-item-preview-not-editable", "A redacted or truncated held message cannot be edited from its preview.");
|
|
227
|
+
const next = { ...this.#items[index], text: command.payload.text as string, attachmentRefs: [], contentDigest: command.payload.contentDigest as string,
|
|
228
|
+
updatedAt: this.#now().toISOString() };
|
|
229
|
+
return this.#commitSettled(command, "updated", next, index, false);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
#delete(command: QueueCommand): QueueControlReceipt {
|
|
233
|
+
const index = this.#items.findIndex((item) => item.queueItemRef === command.payload.queueItemRef);
|
|
234
|
+
if (index < 0) return this.#reject(command, "capability-unavailable", "queue-item-unavailable", "The held message is unavailable.");
|
|
235
|
+
return this.#commitSettled(command, "deleted", this.#items[index], index, true);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
#commitSettled(command: QueueCommand, resultCode: "updated" | "deleted", item: QueueItem, index: number, remove: boolean): QueueControlReceipt {
|
|
239
|
+
const mutationRef = this.#opaqueRef("queue-mutation"); let receipt: QueueControlReceipt | null = null;
|
|
240
|
+
const committed = this.#bridge.commitQueueMutation((before, after) => {
|
|
241
|
+
receipt = this.#receipt(command, before, after, "settled", resultCode, null, mutationRef);
|
|
242
|
+
this.#appendEntry(WEBUI_QUEUE_ENTRY_TYPE, this.#evidence(command, receipt, item, index, mutationRef));
|
|
243
|
+
});
|
|
244
|
+
if (!committed || !receipt) return this.#reject(command, "capability-unavailable", "queue-evidence-unavailable", "The queue change could not be recorded.");
|
|
245
|
+
if (remove) this.#items.splice(index, 1); else this.#items[index] = item;
|
|
246
|
+
return this.#remember(receipt);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async #dispatch(command: QueueCommand): Promise<QueueControlReceipt> {
|
|
250
|
+
const index = this.#items.findIndex((item) => item.queueItemRef === command.payload.queueItemRef), beforeSnapshot = this.#bridge.snapshot();
|
|
251
|
+
if (index < 0 || this.#items[index].state !== "held" || !beforeSnapshot.identity || !beforeSnapshot.revisions)
|
|
252
|
+
return this.#reject(command, "capability-unavailable", "queue-item-unavailable", "The held message is unavailable.");
|
|
253
|
+
if (beforeSnapshot.liveness === "unknown") return this.#reject(command, "capability-unavailable", "agent-liveness-unknown", "Pi operation state is unknown.");
|
|
254
|
+
const item = this.#items[index], payload = { messageRequestId: command.payload.messageRequestId as string, capabilityAction: "send" as const,
|
|
255
|
+
delivery: beforeSnapshot.liveness === "running" ? "follow-up" as const : "new-operation" as const,
|
|
256
|
+
text: item.text, attachmentRefs: item.attachmentRefs, contentDigest: item.contentDigest };
|
|
257
|
+
const inner: NewOperationChatCommand = { schemaVersion: 1, version: "piagent-webui-control-v1", messageType: "command",
|
|
258
|
+
commandId: this.#ref("queue-dispatch", command.commandId, command.actionDigest), idempotencyKey: `queue-dispatch.${sha(command.idempotencyKey).slice(7)}`,
|
|
259
|
+
requestedAt: command.requestedAt, expiresAt: command.expiresAt, capabilityScope: "control.chat", action: "chat.send", actionDigest: "",
|
|
260
|
+
identity: copy(beforeSnapshot.identity), expectedRevisions: { ...copy(beforeSnapshot.revisions), workspacePreimage: null, indexPreimage: null, patchPreimage: null }, payload };
|
|
261
|
+
inner.actionDigest = controlActionDigest(inner);
|
|
262
|
+
const innerReceipt = await this.#bridge.execute(inner);
|
|
263
|
+
if (innerReceipt.phase === "rejected") return this.#reject(command, innerReceipt.resultCode as ChatReceipt["resultCode"], innerReceipt.error?.code ?? "dispatch-rejected",
|
|
264
|
+
innerReceipt.error?.message ?? "The held message was not dispatched.");
|
|
265
|
+
const uncertain = innerReceipt.phase === "uncertain", removed = !uncertain;
|
|
266
|
+
const nextItem = { ...item, state: "quarantined" as const, updatedAt: this.#now().toISOString(), reasonCode: "dispatch-acknowledgement-ambiguous" };
|
|
267
|
+
const mutationRef = this.#opaqueRef("queue-mutation"); let receipt: QueueControlReceipt | null = null;
|
|
268
|
+
const committed = this.#bridge.commitQueueMutation((before, after) => {
|
|
269
|
+
receipt = this.#receipt(command, copy(beforeSnapshot.revisions!), after, innerReceipt.phase, innerReceipt.resultCode as ChatReceipt["resultCode"],
|
|
270
|
+
innerReceipt.error, innerReceipt.settlementEvidenceRef, innerReceipt.identity);
|
|
271
|
+
this.#appendEntry(WEBUI_QUEUE_ENTRY_TYPE, this.#evidence(command, receipt, uncertain ? nextItem : item, index, mutationRef));
|
|
272
|
+
});
|
|
273
|
+
if (!committed || !receipt) {
|
|
274
|
+
this.#items[index] = nextItem;
|
|
275
|
+
return this.#receipt(command, copy(beforeSnapshot.revisions), this.#bridge.snapshot().revisions ?? copy(beforeSnapshot.revisions), "uncertain", "dispatch-unknown",
|
|
276
|
+
{ code: "queue-settlement-evidence-unavailable", message: "Dispatch may have occurred; the queue item was quarantined.", retryable: false }, null,
|
|
277
|
+
innerReceipt.identity);
|
|
278
|
+
}
|
|
279
|
+
if (removed) this.#items.splice(index, 1); else this.#items[index] = nextItem;
|
|
280
|
+
return this.#remember(receipt);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
#receipt(command: QueueCommand, before: BridgeRevisions, after: BridgeRevisions, phase: ChatReceipt["phase"], resultCode: QueueControlReceipt["resultCode"],
|
|
284
|
+
error: ChatReceipt["error"], settlementEvidenceRef: string | null, identity: BridgeIdentity = command.identity): QueueControlReceipt {
|
|
285
|
+
return { schemaVersion: 1, version: "piagent-webui-control-v1", messageType: "receipt", commandId: command.commandId,
|
|
286
|
+
idempotencyKeyDigest: sha(command.idempotencyKey), action: command.action, actionDigest: command.actionDigest, identity: copy(identity), phase, resultCode,
|
|
287
|
+
requestedAt: command.requestedAt, settledAt: ["requested", "accepted"].includes(phase) ? null : this.#now().toISOString(),
|
|
288
|
+
observedRevisionsBefore: copy(before), observedRevisionsAfter: copy(after), deduplicated: false, auditRef: null,
|
|
289
|
+
settlementEvidenceRef: phase === "settled" ? settlementEvidenceRef : null, error };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
#reject(command: QueueCommand, resultCode: QueueControlReceipt["resultCode"], code: string, message: string): QueueControlReceipt {
|
|
293
|
+
const snapshot = this.#sync(), fallbackIdentity = snapshot.identity ?? this.#lastIdentity ?? { projectRef: "project.unavailable", runtimeInstanceId: "runtime.unavailable",
|
|
294
|
+
sessionRef: "session.unavailable", taskId: null, taskRunId: null, agentOperationId: null, toolCallId: null };
|
|
295
|
+
const fallbackRevisions = snapshot.revisions ?? this.#lastRevisions ?? { runtimeRevision: "runtime-rev.unavailable", taskRevision: null, controlRevision: null,
|
|
296
|
+
workspaceRevision: null, indexRevision: null, approvalRevision: null, sessionOptionRevision: null, queueRevision: null };
|
|
297
|
+
const safeAction = ["chat.send", "queue.update", "queue.delete", "queue.dispatch"].includes(command?.action) ? command.action : "chat.send";
|
|
298
|
+
return { schemaVersion: 1, version: "piagent-webui-control-v1", messageType: "receipt", commandId: REF.test(command?.commandId ?? "") ? command.commandId : this.#ref("command", code),
|
|
299
|
+
idempotencyKeyDigest: IDEMPOTENCY.test(command?.idempotencyKey ?? "") ? sha(command.idempotencyKey) : sha("invalid-idempotency-key"), action: safeAction,
|
|
300
|
+
actionDigest: DIGEST.test(command?.actionDigest ?? "") ? command.actionDigest : sha(code), identity: validIdentity(command?.identity) ? copy(command.identity) : copy(fallbackIdentity),
|
|
301
|
+
phase: "rejected", resultCode, requestedAt: timestamp(command?.requestedAt) ? command.requestedAt : this.#now().toISOString(), settledAt: this.#now().toISOString(),
|
|
302
|
+
observedRevisionsBefore: copy(fallbackRevisions), observedRevisionsAfter: copy(fallbackRevisions), deduplicated: resultCode === "replay", auditRef: null,
|
|
303
|
+
settlementEvidenceRef: null, error: { code, message, retryable: false } };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
#remember(receipt: QueueControlReceipt): QueueControlReceipt {
|
|
307
|
+
this.#receiptsByKey.set(receipt.idempotencyKeyDigest, copy(receipt)); this.#receiptsByCommand.set(receipt.commandId, copy(receipt)); return receipt;
|
|
308
|
+
}
|
|
309
|
+
#opaqueRef(prefix: string): string { return `${prefix}.${randomBytes(24).toString("hex")}`; }
|
|
310
|
+
#ref(prefix: string, ...parts: string[]): string { return `${prefix}.${createHash("sha256").update(parts.join("\0")).digest("hex").slice(0, 40)}`; }
|
|
311
|
+
}
|