@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,299 @@
|
|
|
1
|
+
import { type ChannelReplyKeyInput, isSettledReply } from "./delivery.js";
|
|
2
|
+
import type { RuntimeCore } from "./runtime-core.js";
|
|
3
|
+
import { MAX_ERROR_BYTES, type RuntimeContext } from "./runtime-types.js";
|
|
4
|
+
import {
|
|
5
|
+
CHANNEL_JOURNAL_NAMESPACES,
|
|
6
|
+
type ChannelBindingRecord,
|
|
7
|
+
type ChannelOperationKeyInput,
|
|
8
|
+
type StoredChannelRecord,
|
|
9
|
+
UNRESOLVED_OPERATION_STATES,
|
|
10
|
+
} from "./state.js";
|
|
11
|
+
import type {
|
|
12
|
+
ChannelDiagnostics,
|
|
13
|
+
ChannelOperationRecord,
|
|
14
|
+
ChannelOperationState,
|
|
15
|
+
ChannelPruneResult,
|
|
16
|
+
ChannelReconcileInput,
|
|
17
|
+
ChannelReconcileResult,
|
|
18
|
+
ChannelReply,
|
|
19
|
+
ChannelReplyRecord,
|
|
20
|
+
ChannelSendResult,
|
|
21
|
+
ChannelUnresolvedOperation,
|
|
22
|
+
ChannelDrainResult,
|
|
23
|
+
} from "./types.js";
|
|
24
|
+
import { type AgentIdentity, ownershipFromIdentity } from "./identity.js";
|
|
25
|
+
export function createRuntimeReconcile(ctx: RuntimeContext, core: RuntimeCore) {
|
|
26
|
+
const { counters, inflight, journal, limits, options, replies, routes } = ctx;
|
|
27
|
+
const { boundText, identityActive, nowIso, pendingCount, releaseLease, settleOperation, track } = core;
|
|
28
|
+
async function drain(options_: { readonly deadlineMs?: number } = {}): Promise<ChannelDrainResult> {
|
|
29
|
+
const deadline = options_.deadlineMs === undefined ? undefined : Date.now() + Math.max(0, options_.deadlineMs);
|
|
30
|
+
while (inflight.size > 0) {
|
|
31
|
+
const waits = [...inflight];
|
|
32
|
+
if (deadline === undefined) {
|
|
33
|
+
await Promise.all(waits);
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const remaining = deadline - Date.now();
|
|
37
|
+
if (remaining <= 0) break;
|
|
38
|
+
await Promise.race([Promise.all(waits), delay(remaining)]);
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
settled: inflight.size === 0,
|
|
42
|
+
pending: inflight.size,
|
|
43
|
+
completed: counters.completed,
|
|
44
|
+
failed: counters.failed,
|
|
45
|
+
cancelled: counters.cancelled,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function delay(ms: number): Promise<void> {
|
|
50
|
+
return new Promise((resolve) => {
|
|
51
|
+
setTimeout(resolve, ms).unref();
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function stop(): Promise<void> {
|
|
56
|
+
ctx.stopped = true;
|
|
57
|
+
for (const route of routes.values()) {
|
|
58
|
+
for (const queued of route.queue) {
|
|
59
|
+
if (!queued.cancelled) {
|
|
60
|
+
queued.cancelled = true;
|
|
61
|
+
counters.cancelled += 1;
|
|
62
|
+
track(settleOperation(queued, "cancelled", { errorCode: "cancelled_at_shutdown" }));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
route.queue.length = 0;
|
|
66
|
+
const active = route.active;
|
|
67
|
+
if (active?.controller !== undefined) active.controller.abort(new Error("Channel runtime stopped"));
|
|
68
|
+
else if (active !== undefined && !active.cancelled) {
|
|
69
|
+
// Waiting for a concurrency slot: no controller exists yet, so mark it so it exits.
|
|
70
|
+
active.cancelled = true;
|
|
71
|
+
counters.cancelled += 1;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
await drain({ deadlineMs: limits.stopDeadlineMs });
|
|
75
|
+
for (const route of routes.values()) {
|
|
76
|
+
if (route.ownershipHint !== undefined) await releaseLease(route, route.ownershipHint);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function reconcile(input: ChannelReconcileInput): Promise<ChannelReconcileResult> {
|
|
81
|
+
if (journal === undefined || replies === undefined) return { status: "unavailable" };
|
|
82
|
+
if (!identityActive(input.identity)) return { status: "denied" };
|
|
83
|
+
if (
|
|
84
|
+
typeof input.operationId !== "string" ||
|
|
85
|
+
input.operationId.length === 0 ||
|
|
86
|
+
typeof input.connectionId !== "string" ||
|
|
87
|
+
!Number.isSafeInteger(input.expectedVersion)
|
|
88
|
+
) {
|
|
89
|
+
return { status: "not_found" };
|
|
90
|
+
}
|
|
91
|
+
const ownership = ownershipFromIdentity(input.identity);
|
|
92
|
+
const keyInput: ChannelOperationKeyInput = {
|
|
93
|
+
ownership,
|
|
94
|
+
connectionId: input.connectionId,
|
|
95
|
+
operationId: input.operationId,
|
|
96
|
+
};
|
|
97
|
+
let stored: StoredChannelRecord<ChannelOperationRecord> | null;
|
|
98
|
+
try {
|
|
99
|
+
stored = await journal.loadOperation(keyInput);
|
|
100
|
+
} catch {
|
|
101
|
+
counters.storageFailures += 1;
|
|
102
|
+
return { status: "unavailable" };
|
|
103
|
+
}
|
|
104
|
+
if (stored === null) return { status: "not_found" };
|
|
105
|
+
if (stored.version !== input.expectedVersion) return { status: "conflict", state: stored.record.state };
|
|
106
|
+
|
|
107
|
+
// Unproven model/tool work: never replayed. Resolution is an explicit, recorded dead-letter.
|
|
108
|
+
if (stored.record.state === "executing") {
|
|
109
|
+
if (input.acknowledgeDuplicateRisk !== true) return { status: "conflict", state: stored.record.state, detail: "duplicate_risk" };
|
|
110
|
+
const saved = await journal
|
|
111
|
+
.saveOperation(keyInput, { ...stored.record, state: "execution_unknown", updatedAt: nowIso() }, stored.version, stored.fencingToken)
|
|
112
|
+
.catch(() => null);
|
|
113
|
+
if (saved === null) return { status: "conflict", state: stored.record.state };
|
|
114
|
+
return { status: "resolved", outcome: "execution_unknown", state: "execution_unknown" };
|
|
115
|
+
}
|
|
116
|
+
if (stored.record.state === "accepted") {
|
|
117
|
+
const saved = await journal
|
|
118
|
+
.saveOperation(keyInput, { ...stored.record, state: "abandoned", updatedAt: nowIso() }, stored.version, stored.fencingToken)
|
|
119
|
+
.catch(() => null);
|
|
120
|
+
if (saved === null) return { status: "conflict", state: stored.record.state };
|
|
121
|
+
return { status: "resolved", outcome: "abandoned", state: "abandoned" };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Settled work may still have an undelivered or ambiguous reply: resending needs the ack.
|
|
125
|
+
const replyKey: ChannelReplyKeyInput = {
|
|
126
|
+
ownership,
|
|
127
|
+
connectionId: input.connectionId,
|
|
128
|
+
operationId: input.operationId,
|
|
129
|
+
};
|
|
130
|
+
const staged = await replies.load(replyKey).catch(() => null);
|
|
131
|
+
if (staged === null) return { status: "resolved", outcome: "none", state: stored.record.state };
|
|
132
|
+
const resendable =
|
|
133
|
+
staged.record.state === "pending" || staged.record.state === "delivery_unknown" || staged.record.state === "delivery_failed";
|
|
134
|
+
if (!resendable) return { status: "resolved", outcome: "none", state: stored.record.state };
|
|
135
|
+
// A known failure is safe to retry; `pending`/`delivery_unknown` may already have landed.
|
|
136
|
+
if (staged.record.state !== "delivery_failed" && input.acknowledgeDuplicateRisk !== true) {
|
|
137
|
+
return { status: "conflict", state: stored.record.state, detail: "duplicate_risk" };
|
|
138
|
+
}
|
|
139
|
+
const reply: ChannelReply = {
|
|
140
|
+
connectionId: staged.record.connectionId,
|
|
141
|
+
externalConversationId: staged.record.externalConversationId,
|
|
142
|
+
text: staged.record.text,
|
|
143
|
+
kind: staged.record.kind,
|
|
144
|
+
...(staged.record.controls === undefined ? {} : { controls: staged.record.controls }),
|
|
145
|
+
...(staged.record.threadId === undefined ? {} : { threadId: staged.record.threadId }),
|
|
146
|
+
};
|
|
147
|
+
let result: ChannelSendResult | undefined;
|
|
148
|
+
let ambiguous = false;
|
|
149
|
+
try {
|
|
150
|
+
result = await options.deliver(reply);
|
|
151
|
+
} catch {
|
|
152
|
+
ambiguous = true;
|
|
153
|
+
}
|
|
154
|
+
const state = ambiguous ? "delivery_unknown" : result?.delivered ? "delivered" : "delivery_failed";
|
|
155
|
+
if (result?.delivered) counters.deliveries += 1;
|
|
156
|
+
else if (!ambiguous) counters.deliveryFailures += 1;
|
|
157
|
+
const updated: ChannelReplyRecord = {
|
|
158
|
+
...staged.record,
|
|
159
|
+
state,
|
|
160
|
+
attempts: staged.record.attempts + 1,
|
|
161
|
+
updatedAt: nowIso(),
|
|
162
|
+
...(result?.messageId === undefined ? {} : { messageId: result.messageId }),
|
|
163
|
+
...(result?.reason === undefined ? {} : { reason: boundText(result.reason, MAX_ERROR_BYTES) }),
|
|
164
|
+
};
|
|
165
|
+
const saved = await replies.mark(replyKey, updated, staged.version).catch(() => null);
|
|
166
|
+
if (saved === null) {
|
|
167
|
+
counters.storageFailures += 1;
|
|
168
|
+
return { status: "conflict", state: stored.record.state };
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
status: "resolved",
|
|
172
|
+
outcome: result?.delivered ? "reply_delivered" : ambiguous ? "reply_unknown" : "reply_failed",
|
|
173
|
+
state: stored.record.state,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function listUnresolved(input: {
|
|
178
|
+
readonly identity: AgentIdentity;
|
|
179
|
+
readonly limit?: number;
|
|
180
|
+
}): Promise<readonly ChannelUnresolvedOperation[]> {
|
|
181
|
+
if (journal === undefined) return [];
|
|
182
|
+
if (!identityActive(input.identity)) return [];
|
|
183
|
+
const limit = Math.min(Math.max(1, input.limit ?? limits.maxJournalPage), limits.maxJournalPage);
|
|
184
|
+
const items = await journal
|
|
185
|
+
.list<ChannelOperationRecord>({
|
|
186
|
+
ownership: ownershipFromIdentity(input.identity),
|
|
187
|
+
namespace: CHANNEL_JOURNAL_NAMESPACES.operation,
|
|
188
|
+
limit,
|
|
189
|
+
})
|
|
190
|
+
.catch(() => null);
|
|
191
|
+
if (items === null) {
|
|
192
|
+
counters.storageFailures += 1;
|
|
193
|
+
return [];
|
|
194
|
+
}
|
|
195
|
+
const unresolved: ChannelUnresolvedOperation[] = [];
|
|
196
|
+
for (const item of items) {
|
|
197
|
+
if (UNRESOLVED_OPERATION_STATES.includes(item.record.state)) {
|
|
198
|
+
unresolved.push({
|
|
199
|
+
operationId: item.record.operationId,
|
|
200
|
+
connectionId: item.record.connectionId,
|
|
201
|
+
state: item.record.state,
|
|
202
|
+
version: item.version,
|
|
203
|
+
updatedAt: item.updatedAt,
|
|
204
|
+
});
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const reply = await replies
|
|
208
|
+
?.load({
|
|
209
|
+
ownership: ownershipFromIdentity(input.identity),
|
|
210
|
+
connectionId: item.record.connectionId,
|
|
211
|
+
operationId: item.record.operationId,
|
|
212
|
+
})
|
|
213
|
+
.catch(() => null);
|
|
214
|
+
const replyState = reply?.record.state;
|
|
215
|
+
const needsReplyWork = replyState === "pending" || replyState === "delivery_unknown" || replyState === "delivery_failed";
|
|
216
|
+
if (!needsReplyWork && !UNRESOLVED_OPERATION_STATES.includes(item.record.state)) continue;
|
|
217
|
+
unresolved.push({
|
|
218
|
+
operationId: item.record.operationId,
|
|
219
|
+
connectionId: item.record.connectionId,
|
|
220
|
+
state: item.record.state,
|
|
221
|
+
...(replyState === undefined ? {} : { replyState }),
|
|
222
|
+
version: item.version,
|
|
223
|
+
updatedAt: item.updatedAt,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return unresolved;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Retention sweep. Unresolved operations and unsettled replies are never deleted; pages are bounded. */
|
|
230
|
+
async function prune(input: { readonly identity: AgentIdentity; readonly now?: string }): Promise<ChannelPruneResult> {
|
|
231
|
+
const result: { scanned: number; deleted: number; retained: number } = { scanned: 0, deleted: 0, retained: 0 };
|
|
232
|
+
if (journal === undefined) return result;
|
|
233
|
+
if (!identityActive(input.identity)) return result;
|
|
234
|
+
const store = journal;
|
|
235
|
+
const ownership = ownershipFromIdentity(input.identity);
|
|
236
|
+
const cutoff = Date.parse(input.now ?? nowIso()) - limits.retentionDays * 24 * 60 * 60_000;
|
|
237
|
+
const page = limits.maxJournalPage;
|
|
238
|
+
|
|
239
|
+
async function sweep(
|
|
240
|
+
namespace: (typeof CHANNEL_JOURNAL_NAMESPACES)[keyof typeof CHANNEL_JOURNAL_NAMESPACES],
|
|
241
|
+
keep: (record: unknown) => boolean,
|
|
242
|
+
) {
|
|
243
|
+
const items = await store.list<unknown>({ ownership, namespace, limit: page }).catch(() => null);
|
|
244
|
+
if (items === null) {
|
|
245
|
+
counters.storageFailures += 1;
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
for (const item of items) {
|
|
249
|
+
result.scanned += 1;
|
|
250
|
+
// The record's own stamp is authoritative (it records when the state transition happened);
|
|
251
|
+
// the store's `updatedAt` is the fallback for records that carry none.
|
|
252
|
+
const own = (item.record as { updatedAt?: unknown }).updatedAt;
|
|
253
|
+
const stamp = typeof own === "string" && Number.isFinite(Date.parse(own)) ? own : item.updatedAt;
|
|
254
|
+
if (Date.parse(stamp) > cutoff || keep(item.record)) {
|
|
255
|
+
result.retained += 1;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
const deleted = await store.remove({ ownership, namespace, key: item.key }).catch(() => false);
|
|
259
|
+
if (deleted === true) result.deleted += 1;
|
|
260
|
+
else {
|
|
261
|
+
counters.storageFailures += 1;
|
|
262
|
+
result.retained += 1;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
await sweep(CHANNEL_JOURNAL_NAMESPACES.operation, (record) => {
|
|
268
|
+
const state = (record as Partial<ChannelOperationRecord>).state;
|
|
269
|
+
return typeof state !== "string" || UNRESOLVED_OPERATION_STATES.includes(state as ChannelOperationState);
|
|
270
|
+
});
|
|
271
|
+
await sweep(CHANNEL_JOURNAL_NAMESPACES.reply, (record) => !isSettledReply(record as ChannelReplyRecord));
|
|
272
|
+
await sweep(CHANNEL_JOURNAL_NAMESPACES.binding, (record) => (record as Partial<ChannelBindingRecord>).suspended === true);
|
|
273
|
+
await sweep(CHANNEL_JOURNAL_NAMESPACES.cursor, () => false);
|
|
274
|
+
await sweep(CHANNEL_JOURNAL_NAMESPACES.control, () => false);
|
|
275
|
+
return result;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function diagnostics(): ChannelDiagnostics {
|
|
279
|
+
return {
|
|
280
|
+
admitted: counters.admitted,
|
|
281
|
+
denied: counters.denied,
|
|
282
|
+
unsupported: counters.unsupported,
|
|
283
|
+
duplicates: counters.duplicates,
|
|
284
|
+
queued: pendingCount(),
|
|
285
|
+
active: ctx.activeRuns,
|
|
286
|
+
completed: counters.completed,
|
|
287
|
+
failed: counters.failed,
|
|
288
|
+
cancelled: counters.cancelled,
|
|
289
|
+
suspended: counters.suspended,
|
|
290
|
+
deliveries: counters.deliveries,
|
|
291
|
+
deliveryFailures: counters.deliveryFailures,
|
|
292
|
+
truncated: counters.truncated,
|
|
293
|
+
storageFailures: counters.storageFailures,
|
|
294
|
+
leaseLosses: counters.leaseLosses,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return { drain, diagnostics, listUnresolved, prune, reconcile, stop };
|
|
299
|
+
}
|