@arnilo/prism-hosts 0.0.0-stage → 0.13.1
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 +394 -0
- package/LICENSE +21 -0
- package/README.md +34 -2
- package/acp/README.md +87 -0
- package/acp/src/capabilities.ts +184 -0
- package/acp/src/errors.ts +24 -0
- package/acp/src/host.ts +36 -0
- package/acp/src/index.ts +25 -0
- package/acp/src/limits.ts +49 -0
- package/acp/src/mapper.ts +419 -0
- package/acp/src/permission.ts +37 -0
- package/acp/src/projection.ts +39 -0
- package/acp/src/prompt.ts +170 -0
- package/acp/src/serve.ts +345 -0
- package/acp/src/session-map.ts +49 -0
- package/ag-ui/README.md +56 -0
- package/ag-ui/src/a2ui.ts +570 -0
- package/ag-ui/src/ag-ui-mapper.ts +507 -0
- package/ag-ui/src/encrypted-value.ts +40 -0
- package/ag-ui/src/errors.ts +29 -0
- package/ag-ui/src/handler.ts +642 -0
- package/ag-ui/src/index.ts +177 -0
- package/ag-ui/src/input.ts +190 -0
- package/ag-ui/src/limits.ts +279 -0
- package/ag-ui/src/mcp-apps.ts +256 -0
- package/ag-ui/src/mcp.ts +94 -0
- package/ag-ui/src/projection.ts +201 -0
- package/ag-ui/src/projectors.ts +272 -0
- package/ag-ui/src/renderer/bind.ts +152 -0
- package/ag-ui/src/renderer/core.ts +270 -0
- package/ag-ui/src/renderer/index.ts +165 -0
- package/ag-ui/src/replay.ts +189 -0
- package/ag-ui/src/types.ts +66 -0
- package/channels/README.md +139 -0
- package/channels/src/admission.ts +8 -0
- package/channels/src/approvals.ts +262 -0
- package/channels/src/checkpoint-contract.ts +97 -0
- package/channels/src/delivery.ts +116 -0
- package/channels/src/host-ui.ts +356 -0
- package/channels/src/identity.ts +46 -0
- package/channels/src/index.ts +82 -0
- package/channels/src/limits.ts +49 -0
- package/channels/src/memory-stores.ts +112 -0
- package/channels/src/pairing.ts +178 -0
- package/channels/src/runtime-admit.ts +389 -0
- package/channels/src/runtime-core.ts +621 -0
- package/channels/src/runtime-reconcile.ts +299 -0
- package/channels/src/runtime-turn.ts +664 -0
- package/channels/src/runtime-types.ts +95 -0
- package/channels/src/runtime.ts +86 -0
- package/channels/src/secret-ref.ts +42 -0
- package/channels/src/signal.ts +790 -0
- package/channels/src/state.ts +355 -0
- package/channels/src/telegram.ts +1317 -0
- package/channels/src/types.ts +442 -0
- package/devtools/README.md +92 -0
- package/devtools/bin/prism-devtools.ts +63 -0
- package/devtools/src/args.ts +71 -0
- package/devtools/src/bun.d.ts +27 -0
- package/devtools/src/compare.ts +190 -0
- package/devtools/src/errors.ts +15 -0
- package/devtools/src/index.ts +25 -0
- package/devtools/src/replay.ts +36 -0
- package/devtools/src/security.ts +80 -0
- package/devtools/src/serve.ts +259 -0
- package/devtools/src/ui/assets.ts +108 -0
- package/devtools/src/ui/inspector.ts +182 -0
- package/driver-process/README.md +197 -0
- package/driver-process/src/abort.ts +74 -0
- package/driver-process/src/adapter.ts +211 -0
- package/driver-process/src/driver.ts +303 -0
- package/driver-process/src/errors.ts +17 -0
- package/driver-process/src/extend.ts +72 -0
- package/driver-process/src/index.ts +5 -0
- package/driver-process/src/inspect.ts +33 -0
- package/driver-process/src/session.ts +72 -0
- package/driver-process/src/testing.ts +394 -0
- package/driver-process/src/transports/json-rpc.ts +147 -0
- package/driver-process/src/transports/jsonl-stream.ts +12 -0
- package/driver-process/src/transports/types.ts +11 -0
- package/driver-process/src/turn.ts +106 -0
- package/evals/README.md +843 -0
- package/evals/src/calibration.ts +179 -0
- package/evals/src/citation-integrity.ts +98 -0
- package/evals/src/citations.ts +45 -0
- package/evals/src/compare-experiments.ts +153 -0
- package/evals/src/comparison.ts +230 -0
- package/evals/src/curate.ts +257 -0
- package/evals/src/dataset.ts +81 -0
- package/evals/src/erp-invariants.ts +163 -0
- package/evals/src/errors.ts +23 -0
- package/evals/src/experiment.ts +537 -0
- package/evals/src/failure-injection.ts +77 -0
- package/evals/src/feedback.ts +53 -0
- package/evals/src/index.ts +198 -0
- package/evals/src/injection.ts +247 -0
- package/evals/src/judge-panel.ts +109 -0
- package/evals/src/judge.ts +89 -0
- package/evals/src/limits.ts +50 -0
- package/evals/src/report-record.ts +47 -0
- package/evals/src/rubric-judge.ts +334 -0
- package/evals/src/scenario-runner.ts +317 -0
- package/evals/src/scenarios.ts +81 -0
- package/evals/src/score.ts +328 -0
- package/evals/src/scorer.ts +21 -0
- package/evals/src/state-match.ts +130 -0
- package/evals/src/stats.ts +635 -0
- package/evals/src/threshold.ts +117 -0
- package/evals/src/timeline.ts +38 -0
- package/evals/src/toolless-session.ts +33 -0
- package/evals/src/trace.ts +80 -0
- package/evals/src/trajectory.ts +623 -0
- package/evals/src/types.ts +672 -0
- package/evals/src/user-simulator.ts +101 -0
- package/evals/src/util.ts +207 -0
- package/headless/README.md +172 -0
- package/headless/src/answers.ts +91 -0
- package/headless/src/authority.ts +32 -0
- package/headless/src/errors.ts +36 -0
- package/headless/src/index.ts +17 -0
- package/headless/src/limits.ts +11 -0
- package/headless/src/pending.ts +16 -0
- package/headless/src/policy.ts +36 -0
- package/headless/src/resume.ts +103 -0
- package/headless/src/route.ts +153 -0
- package/headless/src/scheduler/clock.ts +22 -0
- package/headless/src/scheduler/cron.ts +19 -0
- package/headless/src/scheduler/firing.ts +58 -0
- package/headless/src/scheduler/index.ts +399 -0
- package/headless/src/scheduler/lease.ts +38 -0
- package/headless/src/scheduler/missed.ts +12 -0
- package/headless/src/scheduler/state.ts +132 -0
- package/host-builder/README.md +251 -0
- package/host-builder/bin/prism-host-builder.ts +169 -0
- package/host-builder/data/api.json +6197 -0
- package/host-builder/data/errors.json +558 -0
- package/host-builder/data/examples/acp.ts +63 -0
- package/host-builder/data/examples/adapters/conformance.ts +34 -0
- package/host-builder/data/examples/adapters/fixtures/rpc-server.json +26 -0
- package/host-builder/data/examples/adapters/fixtures/stream-cli.json +27 -0
- package/host-builder/data/examples/adapters/fixtures/workspace-config.json +28 -0
- package/host-builder/data/examples/adapters/rpc-server.dialect.ts +218 -0
- package/host-builder/data/examples/adapters/rpc-server.ts +309 -0
- package/host-builder/data/examples/adapters/stream-cli.dialect.ts +165 -0
- package/host-builder/data/examples/adapters/stream-cli.ts +256 -0
- package/host-builder/data/examples/adapters/workspace-config.dialect.ts +172 -0
- package/host-builder/data/examples/adapters/workspace-config.ts +251 -0
- package/host-builder/data/examples/advisor.ts +94 -0
- package/host-builder/data/examples/ag-ui-a2ui.ts +80 -0
- package/host-builder/data/examples/ag-ui-mcp-apps.ts +44 -0
- package/host-builder/data/examples/ag-ui-server.ts +118 -0
- package/host-builder/data/examples/agent-durable-approval.ts +43 -0
- package/host-builder/data/examples/api-key-auth.ts +23 -0
- package/host-builder/data/examples/approval.ts +92 -0
- package/host-builder/data/examples/attention-compiler.ts +81 -0
- package/host-builder/data/examples/autonomous-coding-loop.ts +343 -0
- package/host-builder/data/examples/behavior-evaluation.ts +303 -0
- package/host-builder/data/examples/build-workflow.ts +334 -0
- package/host-builder/data/examples/cache-aware-prompt-assembly.ts +139 -0
- package/host-builder/data/examples/caveman-ponytail.ts +197 -0
- package/host-builder/data/examples/cli.ts +31 -0
- package/host-builder/data/examples/coding-approval.ts +115 -0
- package/host-builder/data/examples/coding-browser-evaluation.ts +208 -0
- package/host-builder/data/examples/coding-compaction.ts +30 -0
- package/host-builder/data/examples/coding-goal-verify.ts +121 -0
- package/host-builder/data/examples/coding-tools-capability-gaps.ts +75 -0
- package/host-builder/data/examples/compaction.ts +36 -0
- package/host-builder/data/examples/connected-slack-mcp.ts +67 -0
- package/host-builder/data/examples/context.ts +29 -0
- package/host-builder/data/examples/custom-adapter.ts +256 -0
- package/host-builder/data/examples/custom-builders.ts +66 -0
- package/host-builder/data/examples/custom-loop.ts +89 -0
- package/host-builder/data/examples/custom-provider.ts +70 -0
- package/host-builder/data/examples/custom-session-store.ts +72 -0
- package/host-builder/data/examples/custom-tools-skills-context.ts +97 -0
- package/host-builder/data/examples/decision-models.ts +98 -0
- package/host-builder/data/examples/define-package.ts +188 -0
- package/host-builder/data/examples/delegate.ts +200 -0
- package/host-builder/data/examples/discover-skills.ts +65 -0
- package/host-builder/data/examples/drive-rag-sync.ts +44 -0
- package/host-builder/data/examples/durable-approval.ts +139 -0
- package/host-builder/data/examples/durable-investigation.ts +93 -0
- package/host-builder/data/examples/durable-loops-and-approvals.ts +94 -0
- package/host-builder/data/examples/enterprise-identity.ts +21 -0
- package/host-builder/data/examples/enterprise-policy-audit.ts +50 -0
- package/host-builder/data/examples/enterprise-work-connectors.ts +103 -0
- package/host-builder/data/examples/eval-injection.ts +73 -0
- package/host-builder/data/examples/eval-judges.ts +145 -0
- package/host-builder/data/examples/evals.ts +86 -0
- package/host-builder/data/examples/evaluation-gate.ts +40 -0
- package/host-builder/data/examples/execution-timeline.ts +173 -0
- package/host-builder/data/examples/external-agent.ts +541 -0
- package/host-builder/data/examples/external-app-db-backed.ts +344 -0
- package/host-builder/data/examples/governance-deny.ts +122 -0
- package/host-builder/data/examples/guardrail-packs.ts +240 -0
- package/host-builder/data/examples/headless-host.ts +245 -0
- package/host-builder/data/examples/hooks-audit-command.mjs +31 -0
- package/host-builder/data/examples/hooks-json.ts +87 -0
- package/host-builder/data/examples/host-builder-agent.ts +150 -0
- package/host-builder/data/examples/host-hooks.ts +71 -0
- package/host-builder/data/examples/host-step-loop-timeline.ts +212 -0
- package/host-builder/data/examples/hosted-sandbox.ts +48 -0
- package/host-builder/data/examples/instruction-injection.ts +127 -0
- package/host-builder/data/examples/jsonl-stores-branching.ts +49 -0
- package/host-builder/data/examples/long-term-memory.ts +158 -0
- package/host-builder/data/examples/manifest.ts +112 -0
- package/host-builder/data/examples/mcp-server.ts +48 -0
- package/host-builder/data/examples/mcp-trust.ts +173 -0
- package/host-builder/data/examples/memory-and-wiki.ts +315 -0
- package/host-builder/data/examples/memory-lifecycle.ts +302 -0
- package/host-builder/data/examples/memory-system.ts +255 -0
- package/host-builder/data/examples/messaging-agent.ts +70 -0
- package/host-builder/data/examples/minimal-host-app.ts +41 -0
- package/host-builder/data/examples/model-router-aggregate-budgets.ts +364 -0
- package/host-builder/data/examples/model-router.ts +106 -0
- package/host-builder/data/examples/obscura.ts +48 -0
- package/host-builder/data/examples/observability.ts +116 -0
- package/host-builder/data/examples/open-connector-sidecar.ts +105 -0
- package/host-builder/data/examples/openai-compatible.ts +70 -0
- package/host-builder/data/examples/package-skills.ts +85 -0
- package/host-builder/data/examples/phase9-coding-intelligence.ts +221 -0
- package/host-builder/data/examples/provider-registration.ts +29 -0
- package/host-builder/data/examples/provider-resolver.ts +39 -0
- package/host-builder/data/examples/rag.ts +40 -0
- package/host-builder/data/examples/remote-agent-server.ts +45 -0
- package/host-builder/data/examples/remote-agent.ts +124 -0
- package/host-builder/data/examples/research-ledger.ts +79 -0
- package/host-builder/data/examples/revocation-propagation.ts +287 -0
- package/host-builder/data/examples/run-feedback.ts +93 -0
- package/host-builder/data/examples/scanned-document-rag.ts +48 -0
- package/host-builder/data/examples/sdk-basics.ts +21 -0
- package/host-builder/data/examples/secure-agent.ts +55 -0
- package/host-builder/data/examples/semantic-memory.ts +52 -0
- package/host-builder/data/examples/serve-tools.ts +495 -0
- package/host-builder/data/examples/session-memory.ts +318 -0
- package/host-builder/data/examples/session-search.ts +131 -0
- package/host-builder/data/examples/session.ts +52 -0
- package/host-builder/data/examples/side-call.ts +68 -0
- package/host-builder/data/examples/signal-agent.ts +9 -0
- package/host-builder/data/examples/skills-progressive-disclosure.ts +88 -0
- package/host-builder/data/examples/skills.ts +19 -0
- package/host-builder/data/examples/sqlite-resume.ts +147 -0
- package/host-builder/data/examples/system-project-prompts.ts +74 -0
- package/host-builder/data/examples/system-prompts.ts +19 -0
- package/host-builder/data/examples/telegram-agent.ts +30 -0
- package/host-builder/data/examples/tool-narrowing-planes.ts +99 -0
- package/host-builder/data/examples/tools.ts +29 -0
- package/host-builder/data/examples/validator.ts +67 -0
- package/host-builder/data/examples/web-no-key.ts +85 -0
- package/host-builder/data/examples/web-research.ts +35 -0
- package/host-builder/data/examples/workflow-event-sink.ts +114 -0
- package/host-builder/data/examples/workflow-loop.ts +93 -0
- package/host-builder/data/examples/workflow-multimodal-document.ts +94 -0
- package/host-builder/data/examples/workflow-parallel-research.ts +111 -0
- package/host-builder/data/examples/workflow-postgres-resume.ts +75 -0
- package/host-builder/data/examples/workflow-research-and-review.ts +98 -0
- package/host-builder/data/examples/workflow-sqlite-resume.ts +163 -0
- package/host-builder/data/examples.json +130 -0
- package/host-builder/skills/prism-host-builder/SKILL.md +85 -0
- package/host-builder/skills/prism-host-builder/references/choosing.md +101 -0
- package/host-builder/skills/prism-host-builder/references/layout.md +52 -0
- package/host-builder/skills/prism-host-builder/references/pitfalls.md +50 -0
- package/host-builder/skills/prism-host-builder/references/recipes.md +129 -0
- package/host-builder/skills/prism-host-builder/references/security.md +52 -0
- package/host-builder/skills/prism-host-builder/references/upgrading.md +67 -0
- package/host-builder/skills/prism-host-builder/references/verification.md +108 -0
- package/host-builder/src/check/child.ts +108 -0
- package/host-builder/src/check/conformance.ts +13 -0
- package/host-builder/src/check/inspect.ts +20 -0
- package/host-builder/src/check/manifest.ts +23 -0
- package/host-builder/src/check/runner.ts +181 -0
- package/host-builder/src/check/targets.ts +65 -0
- package/host-builder/src/errors.ts +35 -0
- package/host-builder/src/index.ts +25 -0
- package/host-builder/src/indexes.ts +145 -0
- package/host-builder/src/install/install.ts +400 -0
- package/host-builder/src/install/targets.ts +50 -0
- package/host-builder/src/lookup/api.ts +127 -0
- package/host-builder/src/lookup/bound.ts +38 -0
- package/host-builder/src/lookup/docs.ts +238 -0
- package/host-builder/src/lookup/errors.ts +92 -0
- package/host-builder/src/lookup/examples.ts +117 -0
- package/host-builder/src/lookup/text.ts +60 -0
- package/host-builder/src/server.ts +300 -0
- package/host-builder/src/version.ts +38 -0
- package/mcp-server/README.md +219 -0
- package/mcp-server/src/bounds.ts +23 -0
- package/mcp-server/src/host.ts +31 -0
- package/mcp-server/src/index.ts +27 -0
- package/mcp-server/src/serve-tools.ts +408 -0
- package/mcp-server/src/serve.ts +329 -0
- package/mcp-server/src/server.ts +331 -0
- package/mcp-server/src/session-map.ts +47 -0
- package/mcp-server/src/skill-resources.ts +85 -0
- package/mcp-server/src/types.ts +215 -0
- package/package.json +130 -4
- package/remote/README.md +244 -0
- package/remote/src/attribution.ts +22 -0
- package/remote/src/auth.ts +30 -0
- package/remote/src/bun-module.d.ts +34 -0
- package/remote/src/channel.ts +86 -0
- package/remote/src/connect.ts +212 -0
- package/remote/src/dispatcher.ts +282 -0
- package/remote/src/errors.ts +59 -0
- package/remote/src/host-ui.ts +152 -0
- package/remote/src/http-gate.ts +43 -0
- package/remote/src/index.ts +53 -0
- package/remote/src/jsonl.ts +69 -0
- package/remote/src/limits.ts +43 -0
- package/remote/src/mirror.ts +90 -0
- package/remote/src/pending.ts +22 -0
- package/remote/src/protocol.ts +107 -0
- package/remote/src/serve.ts +560 -0
- package/remote/src/session-proxy.ts +219 -0
- package/remote/src/sse.ts +38 -0
- package/remote/src/stdio-client.ts +41 -0
- package/remote/src/testing-fixtures/serve-stdio.ts +32 -0
- package/remote/src/testing.ts +230 -0
- package/remote/src/transport.ts +139 -0
- package/remote/src/transports/client.ts +126 -0
- package/remote/src/transports/network.ts +268 -0
- package/remote/src/wire.ts +244 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
// Plan 079 Task 3: durable channel journal on the generic `CheckpointStore` (no new SQL schema).
|
|
2
|
+
// Commit order is explicit because the store offers single-record CAS only:
|
|
3
|
+
// operation save before cursor advance, operation claim before provider work, reply before send.
|
|
4
|
+
// Keys embed connection + ownership + external ids so a foreign lookup misses instead of
|
|
5
|
+
// colliding (the record key itself is scope-free and a mismatched scope throws).
|
|
6
|
+
import type { OwnershipScope } from "@arnilo/prism";
|
|
7
|
+
import type { CheckpointStore } from "./checkpoint-contract.js";
|
|
8
|
+
import type { ChannelOperationRecord, ChannelOperationState } from "./types.js";
|
|
9
|
+
|
|
10
|
+
/** Namespaces are versioned so a schema change is a new namespace, never a silent reinterpretation. */
|
|
11
|
+
export const CHANNEL_JOURNAL_NAMESPACES = {
|
|
12
|
+
binding: "prism.channels.v1.binding",
|
|
13
|
+
operation: "prism.channels.v1.operation",
|
|
14
|
+
cursor: "prism.channels.v1.cursor",
|
|
15
|
+
reply: "prism.channels.v1.reply",
|
|
16
|
+
control: "prism.channels.v1.control",
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
export type ChannelJournalNamespace = (typeof CHANNEL_JOURNAL_NAMESPACES)[keyof typeof CHANNEL_JOURNAL_NAMESPACES];
|
|
20
|
+
|
|
21
|
+
const CATEGORY: Readonly<Record<ChannelJournalNamespace, string>> = {
|
|
22
|
+
[CHANNEL_JOURNAL_NAMESPACES.binding]: "channel-binding",
|
|
23
|
+
[CHANNEL_JOURNAL_NAMESPACES.operation]: "channel-operation",
|
|
24
|
+
[CHANNEL_JOURNAL_NAMESPACES.cursor]: "channel-cursor",
|
|
25
|
+
[CHANNEL_JOURNAL_NAMESPACES.reply]: "channel-reply",
|
|
26
|
+
[CHANNEL_JOURNAL_NAMESPACES.control]: "channel-control",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Operations that still need host attention: a claim that has not settled. A reconciling host
|
|
31
|
+
* resolves these; `execution_unknown`/`abandoned` are filed dead-letters, not open work.
|
|
32
|
+
*/
|
|
33
|
+
export const UNRESOLVED_OPERATION_STATES: readonly ChannelOperationState[] = ["accepted", "executing"];
|
|
34
|
+
|
|
35
|
+
/** Durable binding state for one connection + actor + alias; `generation` is `/new`'s counter. */
|
|
36
|
+
export interface ChannelBindingRecord {
|
|
37
|
+
readonly sessionId: string;
|
|
38
|
+
readonly leafId?: string;
|
|
39
|
+
readonly agentAlias?: string;
|
|
40
|
+
readonly generation: number;
|
|
41
|
+
readonly suspended: boolean;
|
|
42
|
+
/** Durable core correlation for a binding blocked on an approval. */
|
|
43
|
+
readonly suspendedRun?: {
|
|
44
|
+
readonly operationId: string;
|
|
45
|
+
readonly runId: string;
|
|
46
|
+
readonly sessionId: string;
|
|
47
|
+
readonly approvalId?: string;
|
|
48
|
+
};
|
|
49
|
+
readonly updatedAt: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Durable intake cursor; advanced only after the operation record is committed. */
|
|
53
|
+
export interface ChannelCursorRecord {
|
|
54
|
+
readonly admitted: number;
|
|
55
|
+
readonly lastEventId?: string;
|
|
56
|
+
readonly updatedAt: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ChannelJournalEntry<T> {
|
|
60
|
+
readonly key: string;
|
|
61
|
+
readonly version: number;
|
|
62
|
+
readonly record: T;
|
|
63
|
+
readonly updatedAt: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface StoredChannelRecord<T> {
|
|
67
|
+
readonly version: number;
|
|
68
|
+
readonly record: T;
|
|
69
|
+
/** Present when the underlying checkpoint was written under a lease fence. */
|
|
70
|
+
readonly fencingToken?: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface ChannelBindingKeyInput {
|
|
74
|
+
readonly ownership: OwnershipScope;
|
|
75
|
+
readonly connectionId: string;
|
|
76
|
+
readonly externalConversationId: string;
|
|
77
|
+
readonly externalActorId: string;
|
|
78
|
+
readonly threadId?: string;
|
|
79
|
+
readonly agentAlias?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface ChannelStateStore {
|
|
83
|
+
list<T>(input: {
|
|
84
|
+
readonly ownership: OwnershipScope;
|
|
85
|
+
readonly namespace: ChannelJournalNamespace;
|
|
86
|
+
readonly limit: number;
|
|
87
|
+
}): Promise<readonly ChannelJournalEntry<T>[]>;
|
|
88
|
+
remove(input: {
|
|
89
|
+
readonly ownership: OwnershipScope;
|
|
90
|
+
readonly namespace: ChannelJournalNamespace;
|
|
91
|
+
readonly key: string;
|
|
92
|
+
}): Promise<boolean>;
|
|
93
|
+
loadBinding(input: ChannelBindingKeyInput): Promise<StoredChannelRecord<ChannelBindingRecord> | null>;
|
|
94
|
+
/** `null` when a record already exists (concurrent create); never overwrites. */
|
|
95
|
+
createBinding(input: ChannelBindingKeyInput, record: ChannelBindingRecord): Promise<StoredChannelRecord<ChannelBindingRecord> | null>;
|
|
96
|
+
/** `null` on CAS conflict; caller re-reads before retrying a state change. */
|
|
97
|
+
saveBinding(
|
|
98
|
+
input: ChannelBindingKeyInput,
|
|
99
|
+
record: ChannelBindingRecord,
|
|
100
|
+
expectedVersion: number,
|
|
101
|
+
fencingToken?: number,
|
|
102
|
+
): Promise<StoredChannelRecord<ChannelBindingRecord> | null>;
|
|
103
|
+
loadOperation(input: ChannelOperationKeyInput): Promise<StoredChannelRecord<ChannelOperationRecord> | null>;
|
|
104
|
+
/** `null` when this connection + event id was already admitted (durable dedup). */
|
|
105
|
+
createOperation(
|
|
106
|
+
input: ChannelOperationKeyInput,
|
|
107
|
+
record: ChannelOperationRecord,
|
|
108
|
+
): Promise<StoredChannelRecord<ChannelOperationRecord> | null>;
|
|
109
|
+
saveOperation(
|
|
110
|
+
input: ChannelOperationKeyInput,
|
|
111
|
+
record: ChannelOperationRecord,
|
|
112
|
+
expectedVersion: number,
|
|
113
|
+
fencingToken?: number,
|
|
114
|
+
): Promise<StoredChannelRecord<ChannelOperationRecord> | null>;
|
|
115
|
+
/** Drop an operation record that never applied an effect (a denial or a failed command). */
|
|
116
|
+
removeOperation(input: ChannelOperationKeyInput): Promise<boolean>;
|
|
117
|
+
/** Read the cursor; used for ordering evidence, never for dedup. */
|
|
118
|
+
loadCursor(input: ChannelCursorKeyInput): Promise<StoredChannelRecord<ChannelCursorRecord> | null>;
|
|
119
|
+
/** Best-effort ordering evidence written after a successful operation save. */
|
|
120
|
+
advanceCursor(input: ChannelCursorKeyInput, lastEventId?: string): Promise<void>;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface ChannelOperationKeyInput {
|
|
124
|
+
readonly ownership: OwnershipScope;
|
|
125
|
+
readonly connectionId: string;
|
|
126
|
+
/** Derived deterministically from connection + transport event id, so dedup is a keyed CAS create. */
|
|
127
|
+
readonly operationId: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface ChannelCursorKeyInput {
|
|
131
|
+
readonly ownership: OwnershipScope;
|
|
132
|
+
readonly connectionId: string;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface ChannelStateStoreOptions {
|
|
136
|
+
readonly checkpoints: CheckpointStore;
|
|
137
|
+
/** Per-record value bound enforced before save (store ceilings stay above this). */
|
|
138
|
+
readonly maxJournalRecordBytes: number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function scopeTag(ownership: OwnershipScope): string {
|
|
142
|
+
return `${ownership.tenantId ?? "-"}:${ownership.accountId ?? "-"}:${ownership.userId ?? "-"}`;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function keyPart(value: string | undefined): string {
|
|
146
|
+
return value === undefined || value === "" ? "-" : value;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function bindingKey(input: ChannelBindingKeyInput): string {
|
|
150
|
+
return [
|
|
151
|
+
"b1",
|
|
152
|
+
keyPart(input.connectionId),
|
|
153
|
+
scopeTag(input.ownership),
|
|
154
|
+
keyPart(input.externalConversationId),
|
|
155
|
+
keyPart(input.threadId),
|
|
156
|
+
keyPart(input.externalActorId),
|
|
157
|
+
keyPart(input.agentAlias ?? "default"),
|
|
158
|
+
].join(":");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function operationKey(input: ChannelOperationKeyInput): string {
|
|
162
|
+
return ["o1", keyPart(input.connectionId), scopeTag(input.ownership), keyPart(input.operationId)].join(":");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function cursorKey(input: ChannelCursorKeyInput): string {
|
|
166
|
+
return ["c1", keyPart(input.connectionId), scopeTag(input.ownership)].join(":");
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function scopeOf(ownership: OwnershipScope): OwnershipScope {
|
|
170
|
+
return {
|
|
171
|
+
...(ownership.tenantId === undefined ? {} : { tenantId: ownership.tenantId }),
|
|
172
|
+
...(ownership.accountId === undefined ? {} : { accountId: ownership.accountId }),
|
|
173
|
+
...(ownership.userId === undefined ? {} : { userId: ownership.userId }),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Defense in depth: records are stored in the caller's scope, so a foreign read can never match. */
|
|
178
|
+
function storedValue<T>(record: T): T {
|
|
179
|
+
return JSON.parse(JSON.stringify(record)) as T;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
183
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function asBindingRecord(value: unknown): ChannelBindingRecord {
|
|
187
|
+
if (
|
|
188
|
+
!isRecord(value) ||
|
|
189
|
+
typeof value.sessionId !== "string" ||
|
|
190
|
+
!Number.isSafeInteger(value.generation) ||
|
|
191
|
+
typeof value.suspended !== "boolean"
|
|
192
|
+
) {
|
|
193
|
+
throw new TypeError("Channel binding record is corrupt");
|
|
194
|
+
}
|
|
195
|
+
const suspendedRun = value.suspendedRun;
|
|
196
|
+
if (
|
|
197
|
+
suspendedRun !== undefined &&
|
|
198
|
+
(!isRecord(suspendedRun) ||
|
|
199
|
+
typeof suspendedRun.operationId !== "string" ||
|
|
200
|
+
typeof suspendedRun.runId !== "string" ||
|
|
201
|
+
typeof suspendedRun.sessionId !== "string")
|
|
202
|
+
) {
|
|
203
|
+
throw new TypeError("Channel binding record is corrupt");
|
|
204
|
+
}
|
|
205
|
+
return value as unknown as ChannelBindingRecord;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function asOperationRecord(value: unknown): ChannelOperationRecord {
|
|
209
|
+
if (!isRecord(value) || typeof value.operationId !== "string" || typeof value.state !== "string") {
|
|
210
|
+
throw new TypeError("Channel operation record is corrupt");
|
|
211
|
+
}
|
|
212
|
+
return value as unknown as ChannelOperationRecord;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function asCursorRecord(value: unknown): ChannelCursorRecord {
|
|
216
|
+
if (!isRecord(value) || typeof value.admitted !== "number") throw new TypeError("Channel cursor record is corrupt");
|
|
217
|
+
return value as unknown as ChannelCursorRecord;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function createChannelStateStore(options: ChannelStateStoreOptions): ChannelStateStore {
|
|
221
|
+
const { checkpoints } = options;
|
|
222
|
+
const maxBytes = Math.max(1024, options.maxJournalRecordBytes);
|
|
223
|
+
|
|
224
|
+
async function save<T>(
|
|
225
|
+
namespace: ChannelJournalNamespace,
|
|
226
|
+
ownership: OwnershipScope,
|
|
227
|
+
key: string,
|
|
228
|
+
value: T,
|
|
229
|
+
expectedVersion: number,
|
|
230
|
+
fencingToken?: number,
|
|
231
|
+
): Promise<StoredChannelRecord<T> | null> {
|
|
232
|
+
const encoded = storedValue(value);
|
|
233
|
+
if (Buffer.byteLength(JSON.stringify(encoded), "utf8") > maxBytes) {
|
|
234
|
+
throw new RangeError(`Channel journal record exceeds maxJournalRecordBytes (${maxBytes})`);
|
|
235
|
+
}
|
|
236
|
+
try {
|
|
237
|
+
const record = await checkpoints.saveCheckpoint({
|
|
238
|
+
namespace,
|
|
239
|
+
key,
|
|
240
|
+
...scopeOf(ownership),
|
|
241
|
+
category: CATEGORY[namespace],
|
|
242
|
+
expectedVersion,
|
|
243
|
+
version: expectedVersion + 1,
|
|
244
|
+
...(fencingToken === undefined ? {} : { fencingToken }),
|
|
245
|
+
value: encoded,
|
|
246
|
+
});
|
|
247
|
+
return { version: record.version, record: record.value as T };
|
|
248
|
+
} catch (error) {
|
|
249
|
+
// CAS conflicts are a normal outcome (another worker won); everything else is unavailability.
|
|
250
|
+
if (error instanceof Error && error.name === "CheckpointConflictError") return null;
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async function load<T>(
|
|
256
|
+
namespace: ChannelJournalNamespace,
|
|
257
|
+
ownership: OwnershipScope,
|
|
258
|
+
key: string,
|
|
259
|
+
): Promise<StoredChannelRecord<T> | null> {
|
|
260
|
+
const record = await checkpoints.loadCheckpoint({ namespace, key, ...scopeOf(ownership) });
|
|
261
|
+
if (record === null) return null;
|
|
262
|
+
return record.fencingToken === undefined
|
|
263
|
+
? { version: record.version, record: record.value as T }
|
|
264
|
+
: { version: record.version, record: record.value as T, fencingToken: record.fencingToken };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
async list<T>(input: { ownership: OwnershipScope; namespace: ChannelJournalNamespace; limit: number }) {
|
|
269
|
+
const page = await checkpoints.listCheckpoints({
|
|
270
|
+
namespace: input.namespace,
|
|
271
|
+
...scopeOf(input.ownership),
|
|
272
|
+
limit: input.limit,
|
|
273
|
+
order: "desc",
|
|
274
|
+
});
|
|
275
|
+
return page.items.map((item) => ({
|
|
276
|
+
key: item.key,
|
|
277
|
+
version: item.version,
|
|
278
|
+
updatedAt: item.updatedAt,
|
|
279
|
+
record: item.value as T,
|
|
280
|
+
}));
|
|
281
|
+
},
|
|
282
|
+
|
|
283
|
+
async remove(input) {
|
|
284
|
+
return checkpoints.deleteCheckpoint({ namespace: input.namespace, key: input.key, ...scopeOf(input.ownership) });
|
|
285
|
+
},
|
|
286
|
+
|
|
287
|
+
async loadBinding(input) {
|
|
288
|
+
const stored = await load<unknown>(CHANNEL_JOURNAL_NAMESPACES.binding, input.ownership, bindingKey(input));
|
|
289
|
+
return stored === null ? null : { ...stored, record: asBindingRecord(stored.record) };
|
|
290
|
+
},
|
|
291
|
+
|
|
292
|
+
async createBinding(input, record) {
|
|
293
|
+
const stored = await save(CHANNEL_JOURNAL_NAMESPACES.binding, input.ownership, bindingKey(input), record, 0);
|
|
294
|
+
return stored === null ? null : { version: stored.version, record: asBindingRecord(stored.record) };
|
|
295
|
+
},
|
|
296
|
+
|
|
297
|
+
async saveBinding(input, record, expectedVersion, fencingToken) {
|
|
298
|
+
const stored = await save(
|
|
299
|
+
CHANNEL_JOURNAL_NAMESPACES.binding,
|
|
300
|
+
input.ownership,
|
|
301
|
+
bindingKey(input),
|
|
302
|
+
record,
|
|
303
|
+
expectedVersion,
|
|
304
|
+
fencingToken,
|
|
305
|
+
);
|
|
306
|
+
return stored === null ? null : { version: stored.version, record: asBindingRecord(stored.record) };
|
|
307
|
+
},
|
|
308
|
+
|
|
309
|
+
async loadOperation(input) {
|
|
310
|
+
const stored = await load<unknown>(CHANNEL_JOURNAL_NAMESPACES.operation, input.ownership, operationKey(input));
|
|
311
|
+
return stored === null ? null : { ...stored, record: asOperationRecord(stored.record) };
|
|
312
|
+
},
|
|
313
|
+
|
|
314
|
+
async createOperation(input, record) {
|
|
315
|
+
const stored = await save(CHANNEL_JOURNAL_NAMESPACES.operation, input.ownership, operationKey(input), record, 0);
|
|
316
|
+
return stored === null ? null : { version: stored.version, record: asOperationRecord(stored.record) };
|
|
317
|
+
},
|
|
318
|
+
|
|
319
|
+
async saveOperation(input, record, expectedVersion, fencingToken) {
|
|
320
|
+
const stored = await save(
|
|
321
|
+
CHANNEL_JOURNAL_NAMESPACES.operation,
|
|
322
|
+
input.ownership,
|
|
323
|
+
operationKey(input),
|
|
324
|
+
record,
|
|
325
|
+
expectedVersion,
|
|
326
|
+
fencingToken,
|
|
327
|
+
);
|
|
328
|
+
return stored === null ? null : { version: stored.version, record: asOperationRecord(stored.record) };
|
|
329
|
+
},
|
|
330
|
+
|
|
331
|
+
async removeOperation(input) {
|
|
332
|
+
return checkpoints.deleteCheckpoint({
|
|
333
|
+
namespace: CHANNEL_JOURNAL_NAMESPACES.operation,
|
|
334
|
+
key: operationKey(input),
|
|
335
|
+
...scopeOf(input.ownership),
|
|
336
|
+
});
|
|
337
|
+
},
|
|
338
|
+
|
|
339
|
+
async loadCursor(input) {
|
|
340
|
+
const stored = await load<unknown>(CHANNEL_JOURNAL_NAMESPACES.cursor, input.ownership, cursorKey(input));
|
|
341
|
+
return stored === null ? null : { version: stored.version, record: asCursorRecord(stored.record) };
|
|
342
|
+
},
|
|
343
|
+
|
|
344
|
+
async advanceCursor(input, lastEventId) {
|
|
345
|
+
const current = await load<unknown>(CHANNEL_JOURNAL_NAMESPACES.cursor, input.ownership, cursorKey(input));
|
|
346
|
+
const previous = current === null ? null : asCursorRecord(current.record);
|
|
347
|
+
const record: ChannelCursorRecord = {
|
|
348
|
+
admitted: (previous?.admitted ?? 0) + 1,
|
|
349
|
+
...(lastEventId === undefined ? {} : { lastEventId }),
|
|
350
|
+
updatedAt: new Date().toISOString(),
|
|
351
|
+
};
|
|
352
|
+
await save(CHANNEL_JOURNAL_NAMESPACES.cursor, input.ownership, cursorKey(input), record, current?.version ?? 0);
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
}
|