@arnilo/prism-hosts 0.0.0-stage → 0.13.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 +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 +6167 -0
- package/host-builder/data/errors.json +546 -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 +252 -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 +317 -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 +64 -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,262 @@
|
|
|
1
|
+
// Plan 079 Task 6: opaque, one-use channel controls. Core owns decision validation and CAS;
|
|
2
|
+
// this record only binds a short-lived transport token to that core decision.
|
|
3
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
4
|
+
import { OwnershipScope } from "@arnilo/prism";
|
|
5
|
+
import type { CheckpointStore } from "./checkpoint-contract.js";
|
|
6
|
+
import type { AgentIdentity } from "./identity.js";
|
|
7
|
+
import { CHANNEL_JOURNAL_NAMESPACES } from "./state.js";
|
|
8
|
+
import type { ChannelApprovalOutcome } from "./types.js";
|
|
9
|
+
|
|
10
|
+
const TOKEN_BYTES = 32;
|
|
11
|
+
|
|
12
|
+
interface ChannelApprovalRecord {
|
|
13
|
+
readonly connectionId: string;
|
|
14
|
+
readonly externalConversationId: string;
|
|
15
|
+
readonly externalActorId: string;
|
|
16
|
+
readonly threadId?: string;
|
|
17
|
+
readonly principalKind: string;
|
|
18
|
+
readonly principalId: string;
|
|
19
|
+
readonly grantRevision: string;
|
|
20
|
+
readonly agentAlias?: string;
|
|
21
|
+
readonly definitionRevision?: string;
|
|
22
|
+
readonly sessionId: string;
|
|
23
|
+
readonly runId: string;
|
|
24
|
+
readonly approvalId: string;
|
|
25
|
+
readonly expectedVersion?: number;
|
|
26
|
+
readonly outcome: ChannelApprovalOutcome;
|
|
27
|
+
readonly state: "pending" | "consumed";
|
|
28
|
+
readonly createdAt: string;
|
|
29
|
+
readonly expiresAt: string;
|
|
30
|
+
readonly consumedAt?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface ApprovalTarget {
|
|
34
|
+
readonly ownership: OwnershipScope;
|
|
35
|
+
readonly identity: AgentIdentity;
|
|
36
|
+
readonly connectionId: string;
|
|
37
|
+
readonly externalConversationId: string;
|
|
38
|
+
readonly externalActorId: string;
|
|
39
|
+
readonly threadId?: string;
|
|
40
|
+
readonly grantRevision: string;
|
|
41
|
+
readonly agentAlias?: string;
|
|
42
|
+
readonly definitionRevision?: string;
|
|
43
|
+
readonly sessionId: string;
|
|
44
|
+
readonly runId: string;
|
|
45
|
+
readonly approvalId: string;
|
|
46
|
+
readonly expectedVersion?: number;
|
|
47
|
+
readonly outcome: ChannelApprovalOutcome;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface StoredApproval {
|
|
51
|
+
readonly version: number;
|
|
52
|
+
readonly record: ChannelApprovalRecord;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ChannelApprovalStore {
|
|
56
|
+
create(
|
|
57
|
+
input: ApprovalTarget & { readonly ttlMs: number; readonly now?: string },
|
|
58
|
+
): Promise<{ readonly token: string; readonly record: ChannelApprovalRecord }>;
|
|
59
|
+
load(input: {
|
|
60
|
+
readonly ownership: OwnershipScope;
|
|
61
|
+
readonly connectionId: string;
|
|
62
|
+
readonly token: string;
|
|
63
|
+
}): Promise<StoredApproval | null>;
|
|
64
|
+
consume(input: {
|
|
65
|
+
readonly ownership: OwnershipScope;
|
|
66
|
+
readonly connectionId: string;
|
|
67
|
+
readonly token: string;
|
|
68
|
+
readonly expectedVersion: number;
|
|
69
|
+
readonly now?: string;
|
|
70
|
+
}): Promise<"consumed" | "invalid" | "expired" | "already_consumed">;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function scopeOf(ownership: OwnershipScope): OwnershipScope {
|
|
74
|
+
return {
|
|
75
|
+
...(ownership.tenantId === undefined ? {} : { tenantId: ownership.tenantId }),
|
|
76
|
+
...(ownership.accountId === undefined ? {} : { accountId: ownership.accountId }),
|
|
77
|
+
...(ownership.userId === undefined ? {} : { userId: ownership.userId }),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function scopeTag(ownership: OwnershipScope): string {
|
|
82
|
+
return `${ownership.tenantId ?? "-"}:${ownership.accountId ?? "-"}:${ownership.userId ?? "-"}`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function hash(token: string): string {
|
|
86
|
+
return createHash("sha256").update(token).digest("hex");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function controlKey(ownership: OwnershipScope, connectionId: string, tokenHash: string, prefix = "a1"): string {
|
|
90
|
+
return [prefix, connectionId || "-", scopeTag(ownership), tokenHash.slice(0, 40)].join(":");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function asRecord(value: unknown): ChannelApprovalRecord | null {
|
|
94
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
|
|
95
|
+
const record = value as Partial<ChannelApprovalRecord>;
|
|
96
|
+
if (
|
|
97
|
+
typeof record.connectionId !== "string" ||
|
|
98
|
+
typeof record.externalConversationId !== "string" ||
|
|
99
|
+
typeof record.externalActorId !== "string" ||
|
|
100
|
+
typeof record.principalKind !== "string" ||
|
|
101
|
+
typeof record.principalId !== "string" ||
|
|
102
|
+
typeof record.grantRevision !== "string" ||
|
|
103
|
+
(record.agentAlias !== undefined && typeof record.agentAlias !== "string") ||
|
|
104
|
+
(record.definitionRevision !== undefined && typeof record.definitionRevision !== "string") ||
|
|
105
|
+
typeof record.sessionId !== "string" ||
|
|
106
|
+
typeof record.runId !== "string" ||
|
|
107
|
+
typeof record.approvalId !== "string" ||
|
|
108
|
+
(record.expectedVersion !== undefined && (!Number.isSafeInteger(record.expectedVersion) || record.expectedVersion < 1)) ||
|
|
109
|
+
(record.outcome !== "allow_once" && record.outcome !== "reject_once" && record.outcome !== "allow_for_run") ||
|
|
110
|
+
(record.state !== "pending" && record.state !== "consumed") ||
|
|
111
|
+
typeof record.expiresAt !== "string" ||
|
|
112
|
+
!Number.isFinite(Date.parse(record.expiresAt))
|
|
113
|
+
) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
return record as ChannelApprovalRecord;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function createChannelApprovalStore(checkpoints: CheckpointStore): ChannelApprovalStore {
|
|
120
|
+
return createControlStore(checkpoints, "a1", asRecord, (input: ApprovalTarget & { ttlMs: number; now?: string }, now) => {
|
|
121
|
+
return {
|
|
122
|
+
connectionId: input.connectionId,
|
|
123
|
+
externalConversationId: input.externalConversationId,
|
|
124
|
+
externalActorId: input.externalActorId,
|
|
125
|
+
...(input.threadId === undefined ? {} : { threadId: input.threadId }),
|
|
126
|
+
principalKind: input.identity.principal.kind,
|
|
127
|
+
principalId: input.identity.principal.id,
|
|
128
|
+
grantRevision: input.grantRevision,
|
|
129
|
+
agentAlias: input.agentAlias ?? "default",
|
|
130
|
+
definitionRevision: input.definitionRevision ?? "1",
|
|
131
|
+
sessionId: input.sessionId,
|
|
132
|
+
runId: input.runId,
|
|
133
|
+
approvalId: input.approvalId,
|
|
134
|
+
expectedVersion: input.expectedVersion ?? 1,
|
|
135
|
+
outcome: input.outcome,
|
|
136
|
+
state: "pending",
|
|
137
|
+
createdAt: now,
|
|
138
|
+
expiresAt: new Date(Date.parse(now) + input.ttlMs).toISOString(),
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface ChannelUiRequestRecord
|
|
144
|
+
extends Omit<ChannelApprovalRecord, "sessionId" | "runId" | "approvalId" | "outcome" | "expectedVersion"> {
|
|
145
|
+
readonly requestId: string;
|
|
146
|
+
readonly valueKind: "confirm" | "select" | "input";
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
type UiTarget = Omit<ApprovalTarget, "sessionId" | "runId" | "approvalId" | "outcome" | "expectedVersion"> & {
|
|
150
|
+
readonly requestId: string;
|
|
151
|
+
readonly valueKind: ChannelUiRequestRecord["valueKind"];
|
|
152
|
+
readonly ttlMs: number;
|
|
153
|
+
readonly now?: string;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
function asUiRecord(value: unknown): ChannelUiRequestRecord | null {
|
|
157
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
|
|
158
|
+
const r = value as Partial<ChannelUiRequestRecord>;
|
|
159
|
+
if (
|
|
160
|
+
typeof r.connectionId !== "string" ||
|
|
161
|
+
typeof r.externalConversationId !== "string" ||
|
|
162
|
+
typeof r.externalActorId !== "string" ||
|
|
163
|
+
(r.threadId !== undefined && typeof r.threadId !== "string") ||
|
|
164
|
+
typeof r.principalKind !== "string" ||
|
|
165
|
+
typeof r.principalId !== "string" ||
|
|
166
|
+
typeof r.grantRevision !== "string" ||
|
|
167
|
+
typeof r.requestId !== "string" ||
|
|
168
|
+
!["confirm", "select", "input"].includes(r.valueKind ?? "") ||
|
|
169
|
+
(r.state !== "pending" && r.state !== "consumed") ||
|
|
170
|
+
typeof r.expiresAt !== "string" ||
|
|
171
|
+
!Number.isFinite(Date.parse(r.expiresAt))
|
|
172
|
+
)
|
|
173
|
+
return null;
|
|
174
|
+
return r as ChannelUiRequestRecord;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function createChannelUiRequestStore(checkpoints: CheckpointStore) {
|
|
178
|
+
return createControlStore(
|
|
179
|
+
checkpoints,
|
|
180
|
+
"u1",
|
|
181
|
+
asUiRecord,
|
|
182
|
+
(input: UiTarget, now): ChannelUiRequestRecord => ({
|
|
183
|
+
connectionId: input.connectionId,
|
|
184
|
+
externalConversationId: input.externalConversationId,
|
|
185
|
+
externalActorId: input.externalActorId,
|
|
186
|
+
...(input.threadId === undefined ? {} : { threadId: input.threadId }),
|
|
187
|
+
principalKind: input.identity.principal.kind,
|
|
188
|
+
principalId: input.identity.principal.id,
|
|
189
|
+
grantRevision: input.grantRevision,
|
|
190
|
+
requestId: input.requestId,
|
|
191
|
+
valueKind: input.valueKind,
|
|
192
|
+
state: "pending",
|
|
193
|
+
createdAt: now,
|
|
194
|
+
expiresAt: new Date(Date.parse(now) + input.ttlMs).toISOString(),
|
|
195
|
+
}),
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function createControlStore<
|
|
200
|
+
T extends ChannelApprovalRecord | ChannelUiRequestRecord,
|
|
201
|
+
I extends { ownership: OwnershipScope; connectionId: string; ttlMs: number; now?: string },
|
|
202
|
+
>(checkpoints: CheckpointStore, prefix: string, parse: (value: unknown) => T | null, build: (input: I, now: string) => T) {
|
|
203
|
+
async function load(input: { readonly ownership: OwnershipScope; readonly connectionId: string; readonly token: string }) {
|
|
204
|
+
const stored = await checkpoints.loadCheckpoint({
|
|
205
|
+
namespace: CHANNEL_JOURNAL_NAMESPACES.control,
|
|
206
|
+
key: controlKey(input.ownership, input.connectionId, hash(input.token), prefix),
|
|
207
|
+
...scopeOf(input.ownership),
|
|
208
|
+
});
|
|
209
|
+
if (stored === null) return null;
|
|
210
|
+
const record = parse(stored.value);
|
|
211
|
+
return record === null || record.connectionId !== input.connectionId ? null : { version: stored.version, record };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
async create(input: I) {
|
|
216
|
+
const now = input.now ?? new Date().toISOString();
|
|
217
|
+
const token = randomBytes(TOKEN_BYTES).toString("base64url");
|
|
218
|
+
const record = build(input, now);
|
|
219
|
+
await checkpoints.saveCheckpoint({
|
|
220
|
+
namespace: CHANNEL_JOURNAL_NAMESPACES.control,
|
|
221
|
+
key: controlKey(input.ownership, input.connectionId, hash(token), prefix),
|
|
222
|
+
...scopeOf(input.ownership),
|
|
223
|
+
category: "channel-control",
|
|
224
|
+
expectedVersion: 0,
|
|
225
|
+
version: 1,
|
|
226
|
+
value: record,
|
|
227
|
+
});
|
|
228
|
+
return { token, record };
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
load,
|
|
232
|
+
|
|
233
|
+
async consume(input: {
|
|
234
|
+
ownership: OwnershipScope;
|
|
235
|
+
connectionId: string;
|
|
236
|
+
token: string;
|
|
237
|
+
expectedVersion: number;
|
|
238
|
+
now?: string;
|
|
239
|
+
}): Promise<"consumed" | "invalid" | "expired" | "already_consumed"> {
|
|
240
|
+
const found = await load(input);
|
|
241
|
+
if (found === null || found.version !== input.expectedVersion) return "invalid";
|
|
242
|
+
if (found.record.state === "consumed") return "already_consumed";
|
|
243
|
+
const now = input.now ?? new Date().toISOString();
|
|
244
|
+
if (Date.parse(found.record.expiresAt) <= Date.parse(now)) return "expired";
|
|
245
|
+
try {
|
|
246
|
+
await checkpoints.saveCheckpoint({
|
|
247
|
+
namespace: CHANNEL_JOURNAL_NAMESPACES.control,
|
|
248
|
+
key: controlKey(input.ownership, input.connectionId, hash(input.token), prefix),
|
|
249
|
+
...scopeOf(input.ownership),
|
|
250
|
+
category: "channel-control",
|
|
251
|
+
expectedVersion: found.version,
|
|
252
|
+
version: found.version + 1,
|
|
253
|
+
value: { ...found.record, state: "consumed", consumedAt: now },
|
|
254
|
+
});
|
|
255
|
+
return "consumed";
|
|
256
|
+
} catch (error) {
|
|
257
|
+
if (error instanceof Error && error.name === "CheckpointConflictError") return "already_consumed";
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local structural view of the `checkpoints`/`leases` service contracts (plan 168 Task 4).
|
|
3
|
+
*
|
|
4
|
+
* Channels is a host library: it must not gain a dependency on `@arnilo/prism-extensions/workflows` (whose root
|
|
5
|
+
* loads the workflow engine) just to name two store types. These declarations mirror the contract in
|
|
6
|
+
* `@arnilo/prism-extensions/workflows/src/contracts.ts` member for member, so any conforming store — the workflows
|
|
7
|
+
* memory defaults, or the sqlite/postgres `checkpoints`/`leases` services — is assignable here.
|
|
8
|
+
*
|
|
9
|
+
* Only the generic persistence types that stayed in the kernel (`OwnershipScope`, `PersistencePage`,
|
|
10
|
+
* `PersistenceQuery`) are imported.
|
|
11
|
+
*/
|
|
12
|
+
import type { OwnershipScope, PersistencePage, PersistenceQuery } from "@arnilo/prism";
|
|
13
|
+
|
|
14
|
+
/** Generic versioned checkpoint key. Namespaces prevent consumer collisions. */
|
|
15
|
+
export interface CheckpointKey extends OwnershipScope {
|
|
16
|
+
readonly namespace: string;
|
|
17
|
+
readonly key: string;
|
|
18
|
+
readonly signal?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Input for an optimistic checkpoint write. Versions must strictly increase. */
|
|
22
|
+
export interface CheckpointSaveInput extends CheckpointKey {
|
|
23
|
+
readonly version: number;
|
|
24
|
+
/** Exact current version required before update; use 0 for create-only. */
|
|
25
|
+
readonly expectedVersion?: number;
|
|
26
|
+
/** Monotonic lease fence. Lower or absent worker fences cannot replace a fenced record. */
|
|
27
|
+
readonly fencingToken?: number;
|
|
28
|
+
readonly value: unknown;
|
|
29
|
+
readonly category?: string;
|
|
30
|
+
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Durable generic checkpoint record. */
|
|
34
|
+
export interface CheckpointRecord extends OwnershipScope {
|
|
35
|
+
readonly namespace: string;
|
|
36
|
+
readonly key: string;
|
|
37
|
+
readonly version: number;
|
|
38
|
+
readonly fencingToken?: number;
|
|
39
|
+
readonly value: unknown;
|
|
40
|
+
readonly category?: string;
|
|
41
|
+
readonly createdAt: string;
|
|
42
|
+
readonly updatedAt: string;
|
|
43
|
+
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Bounded checkpoint query. */
|
|
47
|
+
export interface CheckpointQuery extends PersistenceQuery, OwnershipScope {
|
|
48
|
+
readonly namespace?: string;
|
|
49
|
+
readonly keyPrefix?: string;
|
|
50
|
+
readonly category?: string | readonly string[];
|
|
51
|
+
readonly signal?: AbortSignal;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Generic versioned checkpoint capability for persistence adapters. */
|
|
55
|
+
export interface CheckpointStore {
|
|
56
|
+
saveCheckpoint(input: CheckpointSaveInput): Promise<CheckpointRecord>;
|
|
57
|
+
loadCheckpoint(input: CheckpointKey): Promise<CheckpointRecord | null>;
|
|
58
|
+
listCheckpoints(query?: CheckpointQuery): Promise<PersistencePage<CheckpointRecord>>;
|
|
59
|
+
deleteCheckpoint(input: CheckpointKey): Promise<boolean>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Generic lease key. Ownership fields are part of the trust boundary. */
|
|
63
|
+
export interface LeaseKey extends OwnershipScope {
|
|
64
|
+
readonly namespace: string;
|
|
65
|
+
readonly key: string;
|
|
66
|
+
readonly signal?: AbortSignal;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface LeaseAcquireInput extends LeaseKey {
|
|
70
|
+
readonly ownerId: string;
|
|
71
|
+
readonly ttlMs: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface LeaseClaimInput extends LeaseKey {
|
|
75
|
+
readonly ownerId: string;
|
|
76
|
+
readonly token: string;
|
|
77
|
+
readonly ttlMs?: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface LeaseRecord extends OwnershipScope {
|
|
81
|
+
readonly namespace: string;
|
|
82
|
+
readonly key: string;
|
|
83
|
+
readonly ownerId: string;
|
|
84
|
+
readonly token: string;
|
|
85
|
+
readonly fencingToken: number;
|
|
86
|
+
readonly acquiredAt: string;
|
|
87
|
+
readonly expiresAt: string;
|
|
88
|
+
readonly updatedAt: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Atomic distributed lease capability. Expired rows retain fencing counters. */
|
|
92
|
+
export interface LeaseStore {
|
|
93
|
+
tryAcquireLease(input: LeaseAcquireInput): Promise<LeaseRecord | null>;
|
|
94
|
+
renewLease(input: LeaseClaimInput & { readonly ttlMs: number }): Promise<LeaseRecord | null>;
|
|
95
|
+
releaseLease(input: LeaseClaimInput): Promise<boolean>;
|
|
96
|
+
getLease(input: LeaseKey): Promise<LeaseRecord | null>;
|
|
97
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Plan 079 Task 3: reply journal. A reply is persisted before `deliver` is called, so a crash
|
|
2
|
+
// between the run and the send never reruns model/tool work and never loses the answer. A
|
|
3
|
+
// `deliver` throw is ambiguous (`delivery_unknown`), a returned failure is `delivery_failed`;
|
|
4
|
+
// neither state is resent automatically — only an authorized `reconcile` call resends.
|
|
5
|
+
import type { OwnershipScope } from "@arnilo/prism";
|
|
6
|
+
import type { CheckpointStore } from "./checkpoint-contract.js";
|
|
7
|
+
import { CHANNEL_JOURNAL_NAMESPACES, type StoredChannelRecord } from "./state.js";
|
|
8
|
+
import type { ChannelPruneResult, ChannelReplyRecord } from "./types.js";
|
|
9
|
+
|
|
10
|
+
export interface ChannelReplyKeyInput {
|
|
11
|
+
readonly ownership: OwnershipScope;
|
|
12
|
+
readonly connectionId: string;
|
|
13
|
+
readonly operationId: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ChannelDeliveryJournal {
|
|
17
|
+
load(input: ChannelReplyKeyInput): Promise<StoredChannelRecord<ChannelReplyRecord> | null>;
|
|
18
|
+
/** `null` when a reply for this operation already exists; never overwrites a staged reply. */
|
|
19
|
+
stage(input: ChannelReplyKeyInput, record: ChannelReplyRecord): Promise<StoredChannelRecord<ChannelReplyRecord> | null>;
|
|
20
|
+
/** `null` on CAS conflict; the caller keeps the previous durable state. */
|
|
21
|
+
mark(
|
|
22
|
+
input: ChannelReplyKeyInput,
|
|
23
|
+
record: ChannelReplyRecord,
|
|
24
|
+
expectedVersion: number,
|
|
25
|
+
fencingToken?: number,
|
|
26
|
+
): Promise<StoredChannelRecord<ChannelReplyRecord> | null>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ChannelDeliveryJournalOptions {
|
|
30
|
+
readonly checkpoints: CheckpointStore;
|
|
31
|
+
readonly maxJournalRecordBytes: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Reply states that are settled and therefore eligible for retention-based deletion. */
|
|
35
|
+
export function isSettledReply(record: ChannelReplyRecord): boolean {
|
|
36
|
+
return record.state === "delivered" || record.state === "delivery_failed";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function emptyPruneResult(): ChannelPruneResult {
|
|
40
|
+
return { scanned: 0, deleted: 0, retained: 0 };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function scopeTag(ownership: OwnershipScope): string {
|
|
44
|
+
return `${ownership.tenantId ?? "-"}:${ownership.accountId ?? "-"}:${ownership.userId ?? "-"}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function replyKey(input: ChannelReplyKeyInput): string {
|
|
48
|
+
return ["r1", input.connectionId || "-", scopeTag(input.ownership), input.operationId].join(":");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function scopeOf(ownership: OwnershipScope): OwnershipScope {
|
|
52
|
+
return {
|
|
53
|
+
...(ownership.tenantId === undefined ? {} : { tenantId: ownership.tenantId }),
|
|
54
|
+
...(ownership.accountId === undefined ? {} : { accountId: ownership.accountId }),
|
|
55
|
+
...(ownership.userId === undefined ? {} : { userId: ownership.userId }),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function asReplyRecord(value: unknown): ChannelReplyRecord {
|
|
60
|
+
if (typeof value !== "object" || value === null || typeof (value as ChannelReplyRecord).text !== "string") {
|
|
61
|
+
throw new TypeError("Channel reply record is corrupt");
|
|
62
|
+
}
|
|
63
|
+
return value as ChannelReplyRecord;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function createChannelDeliveryJournal(options: ChannelDeliveryJournalOptions): ChannelDeliveryJournal {
|
|
67
|
+
const { checkpoints } = options;
|
|
68
|
+
const maxBytes = Math.max(1024, options.maxJournalRecordBytes);
|
|
69
|
+
|
|
70
|
+
async function save(
|
|
71
|
+
input: ChannelReplyKeyInput,
|
|
72
|
+
record: ChannelReplyRecord,
|
|
73
|
+
expectedVersion: number,
|
|
74
|
+
fencingToken?: number,
|
|
75
|
+
): Promise<StoredChannelRecord<ChannelReplyRecord> | null> {
|
|
76
|
+
const value = JSON.parse(JSON.stringify(record)) as ChannelReplyRecord;
|
|
77
|
+
if (Buffer.byteLength(JSON.stringify(value), "utf8") > maxBytes) {
|
|
78
|
+
throw new RangeError(`Channel reply record exceeds maxJournalRecordBytes (${maxBytes})`);
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const saved = await checkpoints.saveCheckpoint({
|
|
82
|
+
namespace: CHANNEL_JOURNAL_NAMESPACES.reply,
|
|
83
|
+
key: replyKey(input),
|
|
84
|
+
...scopeOf(input.ownership),
|
|
85
|
+
category: "channel-reply",
|
|
86
|
+
expectedVersion,
|
|
87
|
+
version: expectedVersion + 1,
|
|
88
|
+
...(fencingToken === undefined ? {} : { fencingToken }),
|
|
89
|
+
value,
|
|
90
|
+
});
|
|
91
|
+
return { version: saved.version, record: asReplyRecord(saved.value) };
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (error instanceof Error && error.name === "CheckpointConflictError") return null;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
async load(input) {
|
|
100
|
+
const stored = await checkpoints.loadCheckpoint({
|
|
101
|
+
namespace: CHANNEL_JOURNAL_NAMESPACES.reply,
|
|
102
|
+
key: replyKey(input),
|
|
103
|
+
...scopeOf(input.ownership),
|
|
104
|
+
});
|
|
105
|
+
return stored === null ? null : { version: stored.version, record: asReplyRecord(stored.value) };
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
async stage(input, record) {
|
|
109
|
+
return save(input, record, 0);
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
async mark(input, record, expectedVersion, fencingToken) {
|
|
113
|
+
return save(input, record, expectedVersion, fencingToken);
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|