@osovv/vv-opencode 2.0.1 → 2.1.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 +42 -0
- package/README.md +87 -86
- package/dist/commands/completion.js +0 -1
- package/dist/commands/completion.js.map +1 -1
- package/dist/commands/doctor.js +6 -9
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/init.d.ts +2 -1
- package/dist/commands/init.js +7 -12
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/install.d.ts +4 -0
- package/dist/commands/install.js +40 -37
- package/dist/commands/install.js.map +1 -1
- package/dist/commands/launch.d.ts +2 -1
- package/dist/commands/launch.js +18 -14
- package/dist/commands/launch.js.map +1 -1
- package/dist/commands/patch-provider.d.ts +225 -307
- package/dist/commands/patch-provider.js +92 -296
- package/dist/commands/patch-provider.js.map +1 -1
- package/dist/commands/status.js +7 -10
- package/dist/commands/status.js.map +1 -1
- package/dist/commands/sync.d.ts +4 -0
- package/dist/commands/sync.js +38 -35
- package/dist/commands/sync.js.map +1 -1
- package/dist/index.d.ts +25 -5
- package/dist/index.js +73 -34
- package/dist/index.js.map +1 -1
- package/dist/lib/agent-tool-catalog.d.ts +1 -1
- package/dist/lib/agent-tool-catalog.js +3 -6
- package/dist/lib/agent-tool-catalog.js.map +1 -1
- package/dist/lib/agent-tool-contract.d.ts +22 -13
- package/dist/lib/agent-tool-contract.js +20 -20
- package/dist/lib/agent-tool-contract.js.map +1 -1
- package/dist/lib/analytics/types.d.ts +7 -1
- package/dist/lib/analytics/types.js +2 -2
- package/dist/lib/analytics/types.js.map +1 -1
- package/dist/lib/config-layers.d.ts +28 -6
- package/dist/lib/config-layers.js +268 -73
- package/dist/lib/config-layers.js.map +1 -1
- package/dist/lib/managed-agents.d.ts +24 -2
- package/dist/lib/managed-agents.js +45 -15
- package/dist/lib/managed-agents.js.map +1 -1
- package/dist/lib/model-roles.d.ts +5 -0
- package/dist/lib/model-roles.js +22 -1
- package/dist/lib/model-roles.js.map +1 -1
- package/dist/lib/opencode/agent-registrations.d.ts +4 -4
- package/dist/lib/opencode/agent-registrations.js +386 -197
- package/dist/lib/opencode/agent-registrations.js.map +1 -1
- package/dist/lib/opencode/inspection.d.ts +13 -13
- package/dist/lib/opencode/inspection.js +69 -106
- package/dist/lib/opencode/inspection.js.map +1 -1
- package/dist/lib/opencode/model-overrides.d.ts +2 -2
- package/dist/lib/opencode/model-overrides.js +192 -47
- package/dist/lib/opencode/model-overrides.js.map +1 -1
- package/dist/lib/opencode/paths.d.ts +6 -2
- package/dist/lib/opencode/paths.js +12 -11
- package/dist/lib/opencode/paths.js.map +1 -1
- package/dist/lib/opencode/plugin-registration.d.ts +10 -11
- package/dist/lib/opencode/plugin-registration.js +76 -138
- package/dist/lib/opencode/plugin-registration.js.map +1 -1
- package/dist/lib/opencode/shared-utils.d.ts +47 -3
- package/dist/lib/opencode/shared-utils.js +346 -15
- package/dist/lib/opencode/shared-utils.js.map +1 -1
- package/dist/lib/opencode.d.ts +5 -6
- package/dist/lib/opencode.js +10 -10
- package/dist/lib/opencode.js.map +1 -1
- package/dist/lib/vvoc-config.js +4 -4
- package/dist/lib/vvoc-config.js.map +1 -1
- package/dist/lib/vvoc-paths.d.ts +7 -0
- package/dist/lib/vvoc-paths.js +15 -1
- package/dist/lib/vvoc-paths.js.map +1 -1
- package/dist/lib/vvoc-preset-registry.d.ts +38 -50
- package/dist/lib/vvoc-preset-registry.js +38 -48
- package/dist/lib/vvoc-preset-registry.js.map +1 -1
- package/dist/plugins/analytics/index.d.ts +20 -14
- package/dist/plugins/analytics/index.js +281 -133
- package/dist/plugins/analytics/index.js.map +1 -1
- package/dist/plugins/guardian/index.d.ts +63 -42
- package/dist/plugins/guardian/index.js +217 -759
- package/dist/plugins/guardian/index.js.map +1 -1
- package/dist/plugins/hashline-edit/index.d.ts +145 -31
- package/dist/plugins/hashline-edit/index.js +679 -244
- package/dist/plugins/hashline-edit/index.js.map +1 -1
- package/dist/plugins/hashline-edit/schemas.d.ts +1 -1
- package/dist/plugins/hashline-edit/schemas.js +3 -3
- package/dist/plugins/hashline-edit/schemas.js.map +1 -1
- package/dist/plugins/model-roles/index.d.ts +18 -4
- package/dist/plugins/model-roles/index.js +44 -181
- package/dist/plugins/model-roles/index.js.map +1 -1
- package/dist/plugins/peak-hours/index.d.ts +23 -23
- package/dist/plugins/peak-hours/index.js +211 -180
- package/dist/plugins/peak-hours/index.js.map +1 -1
- package/dist/plugins/secrets-redaction/deep.js +35 -51
- package/dist/plugins/secrets-redaction/deep.js.map +1 -1
- package/dist/plugins/secrets-redaction/index.d.ts +96 -8
- package/dist/plugins/secrets-redaction/index.js +290 -102
- package/dist/plugins/secrets-redaction/index.js.map +1 -1
- package/dist/plugins/secrets-redaction/session.d.ts +9 -0
- package/dist/plugins/secrets-redaction/session.js +21 -4
- package/dist/plugins/secrets-redaction/session.js.map +1 -1
- package/dist/plugins/secrets-redaction/stream.d.ts +92 -0
- package/dist/plugins/secrets-redaction/stream.js +772 -0
- package/dist/plugins/secrets-redaction/stream.js.map +1 -0
- package/dist/plugins/spec-guard/index.d.ts +49 -18
- package/dist/plugins/spec-guard/index.js +231 -129
- package/dist/plugins/spec-guard/index.js.map +1 -1
- package/dist/plugins/system-context-injection/index.d.ts +10 -4
- package/dist/plugins/system-context-injection/index.js +271 -78
- package/dist/plugins/system-context-injection/index.js.map +1 -1
- package/dist/plugins/tool-history-compaction/index.d.ts +26 -4
- package/dist/plugins/tool-history-compaction/index.js +170 -32
- package/dist/plugins/tool-history-compaction/index.js.map +1 -1
- package/dist/plugins/tool-history-compaction/transform.d.ts +47 -16
- package/dist/plugins/tool-history-compaction/transform.js +173 -55
- package/dist/plugins/tool-history-compaction/transform.js.map +1 -1
- package/dist/plugins/web-tools/fetch-service.d.ts +51 -13
- package/dist/plugins/web-tools/fetch-service.js +106 -75
- package/dist/plugins/web-tools/fetch-service.js.map +1 -1
- package/dist/plugins/web-tools/index.d.ts +58 -16
- package/dist/plugins/web-tools/index.js +202 -91
- package/dist/plugins/web-tools/index.js.map +1 -1
- package/dist/plugins/web-tools/media-loader.d.ts +3 -3
- package/dist/plugins/web-tools/media-loader.js +3 -3
- package/dist/plugins/web-tools/media-loader.js.map +1 -1
- package/dist/plugins/web-tools/providers/native-fetch.d.ts +2 -2
- package/dist/plugins/web-tools/providers/native-fetch.js +1 -1
- package/dist/plugins/web-tools/providers/native-fetch.js.map +1 -1
- package/dist/plugins/web-tools/providers/spider.d.ts +2 -2
- package/dist/plugins/web-tools/providers/spider.js +1 -1
- package/dist/plugins/web-tools/providers/spider.js.map +1 -1
- package/dist/plugins/web-tools/providers/zai.d.ts +2 -2
- package/dist/plugins/web-tools/providers/zai.js +1 -1
- package/dist/plugins/web-tools/providers/zai.js.map +1 -1
- package/dist/plugins/web-tools/schemas.d.ts +1 -1
- package/dist/plugins/web-tools/schemas.js +3 -3
- package/dist/plugins/web-tools/schemas.js.map +1 -1
- package/dist/plugins/web-tools/search-service.d.ts +81 -12
- package/dist/plugins/web-tools/search-service.js +104 -57
- package/dist/plugins/web-tools/search-service.js.map +1 -1
- package/dist/plugins/workflow/authorization.d.ts +44 -10
- package/dist/plugins/workflow/authorization.js +113 -73
- package/dist/plugins/workflow/authorization.js.map +1 -1
- package/dist/plugins/workflow/cancellation.d.ts +86 -0
- package/dist/plugins/workflow/cancellation.js +202 -0
- package/dist/plugins/workflow/cancellation.js.map +1 -0
- package/dist/plugins/workflow/checkpoints.d.ts +7 -1
- package/dist/plugins/workflow/checkpoints.js +141 -21
- package/dist/plugins/workflow/checkpoints.js.map +1 -1
- package/dist/plugins/workflow/delegated.d.ts +14 -0
- package/dist/plugins/workflow/delegated.js +6 -5
- package/dist/plugins/workflow/delegated.js.map +1 -1
- package/dist/plugins/workflow/host.d.ts +81 -0
- package/dist/plugins/workflow/host.js +189 -0
- package/dist/plugins/workflow/host.js.map +1 -0
- package/dist/plugins/workflow/index.d.ts +69 -4
- package/dist/plugins/workflow/index.js +1391 -1026
- package/dist/plugins/workflow/index.js.map +1 -1
- package/dist/plugins/workflow/recovery.d.ts +3 -5
- package/dist/plugins/workflow/recovery.js +18 -33
- package/dist/plugins/workflow/recovery.js.map +1 -1
- package/dist/plugins/workflow/repair.d.ts +31 -5
- package/dist/plugins/workflow/repair.js +116 -48
- package/dist/plugins/workflow/repair.js.map +1 -1
- package/dist/plugins/workflow/results.d.ts +31 -31
- package/dist/plugins/workflow/results.js +4 -4
- package/dist/plugins/workflow/results.js.map +1 -1
- package/dist/plugins/workflow/schemas.d.ts +3 -3
- package/dist/plugins/workflow/schemas.js +5 -5
- package/dist/plugins/workflow/schemas.js.map +1 -1
- package/dist/plugins/workflow/system-instruction.md +4 -0
- package/dist/plugins/workflow/tooling.d.ts +6 -0
- package/dist/plugins/workflow/tooling.js +3 -1
- package/dist/plugins/workflow/tooling.js.map +1 -1
- package/dist/runtime/auxiliary.d.ts +13 -0
- package/dist/runtime/auxiliary.js +108 -0
- package/dist/runtime/auxiliary.js.map +1 -0
- package/dist/runtime/client.d.ts +88 -0
- package/dist/runtime/client.js +174 -0
- package/dist/runtime/client.js.map +1 -0
- package/dist/runtime/context-inspection-contract.d.ts +397 -0
- package/dist/runtime/context-inspection-contract.js +622 -0
- package/dist/runtime/context-inspection-contract.js.map +1 -0
- package/dist/runtime/context-inspection.d.ts +52 -0
- package/dist/runtime/context-inspection.js +324 -0
- package/dist/runtime/context-inspection.js.map +1 -0
- package/dist/runtime/context.d.ts +192 -0
- package/dist/runtime/context.js +1246 -0
- package/dist/runtime/context.js.map +1 -0
- package/dist/runtime/coordination.d.ts +40 -0
- package/dist/runtime/coordination.js +87 -0
- package/dist/runtime/coordination.js.map +1 -0
- package/dist/runtime/model-registry.d.ts +34 -0
- package/dist/runtime/model-registry.js +227 -0
- package/dist/runtime/model-registry.js.map +1 -0
- package/dist/runtime/permissions.d.ts +20 -0
- package/dist/runtime/permissions.js +250 -0
- package/dist/runtime/permissions.js.map +1 -0
- package/dist/runtime/snapshot-config.d.ts +80 -0
- package/dist/runtime/snapshot-config.js +268 -0
- package/dist/runtime/snapshot-config.js.map +1 -0
- package/dist/runtime/snapshot-store.d.ts +25 -0
- package/dist/runtime/snapshot-store.js +519 -0
- package/dist/runtime/snapshot-store.js.map +1 -0
- package/dist/runtime/snapshots.d.ts +59 -0
- package/dist/runtime/snapshots.js +721 -0
- package/dist/runtime/snapshots.js.map +1 -0
- package/dist/runtime/types.d.ts +713 -0
- package/dist/runtime/types.js +190 -0
- package/dist/runtime/types.js.map +1 -0
- package/dist/tui/analytics/indicator.d.ts +25 -26
- package/dist/tui/analytics/indicator.js +66 -74
- package/dist/tui/analytics/indicator.js.map +1 -1
- package/dist/tui/branding/footer.d.ts +7 -12
- package/dist/tui/branding/footer.js +16 -24
- package/dist/tui/branding/footer.js.map +1 -1
- package/dist/tui/context/analyze.d.ts +20 -9
- package/dist/tui/context/analyze.js +295 -257
- package/dist/tui/context/analyze.js.map +1 -1
- package/dist/tui/context/collect.d.ts +29 -3
- package/dist/tui/context/collect.js +450 -66
- package/dist/tui/context/collect.js.map +1 -1
- package/dist/tui/context/plugin.d.ts +21 -7
- package/dist/tui/context/plugin.js +65 -70
- package/dist/tui/context/plugin.js.map +1 -1
- package/dist/tui/context/types.d.ts +174 -43
- package/dist/tui/context/types.js +25 -19
- package/dist/tui/context/types.js.map +1 -1
- package/dist/tui/context/view.d.ts +33 -7
- package/dist/tui/context/view.js +136 -81
- package/dist/tui/context/view.js.map +1 -1
- package/dist/tui/peak-hours/banner.d.ts +13 -11
- package/dist/tui/peak-hours/banner.js +48 -80
- package/dist/tui/peak-hours/banner.js.map +1 -1
- package/dist/tui/policy.d.ts +69 -0
- package/dist/tui/policy.js +169 -0
- package/dist/tui/policy.js.map +1 -0
- package/dist/tui.d.ts +3 -8
- package/dist/tui.js +72 -68
- package/dist/tui.js.map +1 -1
- package/package.json +19 -15
- package/schemas/vvoc/v3.json +1 -1
- package/server.js +3 -5
- package/templates/agents/enhancer.md +10 -5
- package/templates/agents/guardian.md +10 -4
- package/templates/agents/investigator.md +4 -2
- package/templates/agents/vv-code-reviewer.md +4 -2
- package/templates/agents/vv-spec-reviewer.md +4 -2
- package/templates/skills/vv-execute/references/tool-contracts.md +1 -1
- package/tui.js +3 -5
- package/dist/plugins/analytics/v2.d.ts +0 -21
- package/dist/plugins/analytics/v2.js +0 -133
- package/dist/plugins/analytics/v2.js.map +0 -1
- package/dist/plugins/guardian/v2.d.ts +0 -13
- package/dist/plugins/guardian/v2.js +0 -204
- package/dist/plugins/guardian/v2.js.map +0 -1
- package/dist/plugins/hashline-edit/v2.d.ts +0 -12
- package/dist/plugins/hashline-edit/v2.js +0 -145
- package/dist/plugins/hashline-edit/v2.js.map +0 -1
- package/dist/plugins/model-roles/v2.d.ts +0 -24
- package/dist/plugins/model-roles/v2.js +0 -274
- package/dist/plugins/model-roles/v2.js.map +0 -1
- package/dist/plugins/peak-hours/v2.d.ts +0 -12
- package/dist/plugins/peak-hours/v2.js +0 -99
- package/dist/plugins/peak-hours/v2.js.map +0 -1
- package/dist/plugins/secrets-redaction/v2.d.ts +0 -19
- package/dist/plugins/secrets-redaction/v2.js +0 -138
- package/dist/plugins/secrets-redaction/v2.js.map +0 -1
- package/dist/plugins/spec-guard/v2.d.ts +0 -12
- package/dist/plugins/spec-guard/v2.js +0 -139
- package/dist/plugins/spec-guard/v2.js.map +0 -1
- package/dist/plugins/system-context-injection/agent-contexts.d.ts +0 -5
- package/dist/plugins/system-context-injection/agent-contexts.js +0 -164
- package/dist/plugins/system-context-injection/agent-contexts.js.map +0 -1
- package/dist/plugins/system-context-injection/v2.d.ts +0 -13
- package/dist/plugins/system-context-injection/v2.js +0 -60
- package/dist/plugins/system-context-injection/v2.js.map +0 -1
- package/dist/plugins/tool-history-compaction/v2.d.ts +0 -11
- package/dist/plugins/tool-history-compaction/v2.js +0 -56
- package/dist/plugins/tool-history-compaction/v2.js.map +0 -1
- package/dist/plugins/v2-runtime/config-watcher.d.ts +0 -26
- package/dist/plugins/v2-runtime/config-watcher.js +0 -111
- package/dist/plugins/v2-runtime/config-watcher.js.map +0 -1
- package/dist/plugins/v2-runtime/index.d.ts +0 -50
- package/dist/plugins/v2-runtime/index.js +0 -114
- package/dist/plugins/v2-runtime/index.js.map +0 -1
- package/dist/plugins/v2-runtime/location-config.d.ts +0 -51
- package/dist/plugins/v2-runtime/location-config.js +0 -91
- package/dist/plugins/v2-runtime/location-config.js.map +0 -1
- package/dist/plugins/v2-runtime/setup.d.ts +0 -48
- package/dist/plugins/v2-runtime/setup.js +0 -174
- package/dist/plugins/v2-runtime/setup.js.map +0 -1
- package/dist/plugins/web-tools/v2.d.ts +0 -11
- package/dist/plugins/web-tools/v2.js +0 -99
- package/dist/plugins/web-tools/v2.js.map +0 -1
- package/dist/plugins/workflow/v2.d.ts +0 -76
- package/dist/plugins/workflow/v2.js +0 -283
- package/dist/plugins/workflow/v2.js.map +0 -1
|
@@ -0,0 +1,721 @@
|
|
|
1
|
+
// FILE: src/runtime/snapshots.ts
|
|
2
|
+
// VERSION: 2.0.0
|
|
3
|
+
// START_MODULE_CONTRACT
|
|
4
|
+
// PURPOSE: Build durable immutable family policy captures from full effective vvoc configuration plus native model overlays, and admit sessions through a staged awaited native model switch that is committed only at the accepted request boundary.
|
|
5
|
+
// SCOPE: Host-verified parent/fork family resolution, content-addressed full-policy capture construction, staged-candidate persistence, family-qualified awaited switchModel admission, commit at acceptance with rollback that preserves a newer choice, idempotent/concurrent deduplication that preserves rejection, force-escalation reconciliation of an identical input, cross-context publication serialization through the app/location coordinator, variant derivation, and the documented SnapshotService consumed by later plugins. No host discovery, no client authentication, no permission flow, no stateless generation.
|
|
6
|
+
// DEPENDS: [node:crypto, src/runtime/auxiliary.ts, src/runtime/coordination.ts, src/runtime/model-registry.ts, src/runtime/snapshot-config.ts, src/runtime/snapshot-store.ts, src/runtime/types.ts]
|
|
7
|
+
// LINKS: [M-NATIVE-RUNTIME, V-M-NATIVE-RUNTIME]
|
|
8
|
+
// ROLE: RUNTIME
|
|
9
|
+
// MAP_MODE: EXPORTS
|
|
10
|
+
// END_MODULE_CONTRACT
|
|
11
|
+
//
|
|
12
|
+
// START_MODULE_MAP
|
|
13
|
+
// MAX_LINEAGE_HOPS - Bound on host-verified parent/fork lineage traversal.
|
|
14
|
+
// MAX_STAGED_CANDIDATES - Bound on concurrently staged per-input candidates for one family.
|
|
15
|
+
// SnapshotCaptureInput - Inputs used to build one family capture.
|
|
16
|
+
// SnapshotServiceDeps - Injectable native boundaries for the snapshot service.
|
|
17
|
+
// defaultSnapshotDigest - Content digest used to address captures.
|
|
18
|
+
// buildFamilyCapture - Build an immutable full-policy family capture.
|
|
19
|
+
// resolveSessionFamily - Resolve a session's family root through host-verified lineage.
|
|
20
|
+
// createSnapshotService - Build the documented snapshot service for one native context.
|
|
21
|
+
// END_MODULE_MAP
|
|
22
|
+
//
|
|
23
|
+
// START_CHANGE_SUMMARY
|
|
24
|
+
// LAST_CHANGE: [C-OPENCODE-V2-NATIVE T-009 - An exact-input force:false then force:true escalation reconciles the required materialization/switch without overwriting a newer explicit choice, an optional caller-supplied admission config is used verbatim, and an app/location coordinator serializes publication so concurrent acceptors across distinct contexts write the family capture once.]
|
|
25
|
+
// END_CHANGE_SUMMARY
|
|
26
|
+
import { createHash } from "node:crypto";
|
|
27
|
+
import { createAuxiliaryService } from "./auxiliary.js";
|
|
28
|
+
import { managedVariantFor, primarySelection, qualifySelection } from "./model-registry.js";
|
|
29
|
+
import { agentBindingsFrom, parseRoleSelections } from "./snapshot-config.js";
|
|
30
|
+
import { familyCaptureIntegrity } from "./snapshot-store.js";
|
|
31
|
+
import { SnapshotAdmissionError, stagedCandidateIdentity, stagedCandidateKey, } from "./types.js";
|
|
32
|
+
/** Bound on host-verified parent/fork lineage traversal. */
|
|
33
|
+
export const MAX_LINEAGE_HOPS = 64;
|
|
34
|
+
/**
|
|
35
|
+
* Bound on concurrently staged per-input admission candidates for one family.
|
|
36
|
+
* Overflow fails the new stage closed rather than dropping an unaccepted input
|
|
37
|
+
* that native preparation may still accept later.
|
|
38
|
+
*/
|
|
39
|
+
export const MAX_STAGED_CANDIDATES = 8;
|
|
40
|
+
// START_BLOCK_CAPTURE_CONSTRUCTION
|
|
41
|
+
/** Content digest used to address captures. */
|
|
42
|
+
export function defaultSnapshotDigest(value) {
|
|
43
|
+
return createHash("sha256").update(value).digest("hex");
|
|
44
|
+
}
|
|
45
|
+
function variantIdFor(snapshotId, modelID, sourceVariant) {
|
|
46
|
+
return sourceVariant === undefined
|
|
47
|
+
? `${snapshotId}.${modelID}`
|
|
48
|
+
: `${snapshotId}.${modelID}.${sourceVariant}`;
|
|
49
|
+
}
|
|
50
|
+
function variantOverlayFor(selection, modelSettings) {
|
|
51
|
+
const match = modelSettings.find((settings) => settings.providerID === selection.providerID &&
|
|
52
|
+
settings.modelID === selection.modelID &&
|
|
53
|
+
(selection.variant === undefined || settings.variant === selection.variant));
|
|
54
|
+
return {
|
|
55
|
+
...(match?.settings === undefined ? {} : { settings: match.settings }),
|
|
56
|
+
...(match?.body === undefined ? {} : { body: match.body }),
|
|
57
|
+
...(match?.headers === undefined ? {} : { headers: match.headers }),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function buildVariants(selections, modelSettings) {
|
|
61
|
+
const byKey = new Map();
|
|
62
|
+
for (const selection of Object.values(selections)) {
|
|
63
|
+
const overlay = variantOverlayFor(selection, modelSettings);
|
|
64
|
+
const managed = managedVariantFor(selection, "");
|
|
65
|
+
const hasOverlay = overlay.settings !== undefined || overlay.body !== undefined || overlay.headers !== undefined;
|
|
66
|
+
if (!hasOverlay && managed === undefined)
|
|
67
|
+
continue;
|
|
68
|
+
const sourceVariant = managed?.sourceVariant ?? selection.variant;
|
|
69
|
+
const body = { ...overlay.body, ...managed?.body };
|
|
70
|
+
const key = `${selection.providerID}\u0000${selection.modelID}\u0000${sourceVariant ?? ""}`;
|
|
71
|
+
byKey.set(key, {
|
|
72
|
+
id: "",
|
|
73
|
+
providerID: selection.providerID,
|
|
74
|
+
modelID: selection.modelID,
|
|
75
|
+
...(sourceVariant === undefined ? {} : { sourceVariant }),
|
|
76
|
+
...(overlay.settings === undefined ? {} : { settings: overlay.settings }),
|
|
77
|
+
...(Object.keys(body).length === 0 ? {} : { body }),
|
|
78
|
+
...(overlay.headers === undefined ? {} : { headers: overlay.headers }),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return [...byKey.values()];
|
|
82
|
+
}
|
|
83
|
+
/** Build an immutable full-policy family capture. */
|
|
84
|
+
export function buildFamilyCapture(input) {
|
|
85
|
+
const digest = input.digest ?? defaultSnapshotDigest;
|
|
86
|
+
const roleModels = parseRoleSelections(input.config.roles);
|
|
87
|
+
const agents = input.config.agentBindings ?? agentBindingsFrom(input.config.agentRoles, roleModels);
|
|
88
|
+
const modelSettings = input.config.modelSettings ?? [];
|
|
89
|
+
const vvoc = input.config.vvoc;
|
|
90
|
+
const modelOverride = input.explicit;
|
|
91
|
+
const draft = {
|
|
92
|
+
schemaVersion: 1,
|
|
93
|
+
snapshotId: "",
|
|
94
|
+
integrity: "",
|
|
95
|
+
familyId: input.familyId,
|
|
96
|
+
capturedAt: input.capturedAt ?? Date.now(),
|
|
97
|
+
location: input.location,
|
|
98
|
+
roles: input.config.roles,
|
|
99
|
+
roleModels,
|
|
100
|
+
agents,
|
|
101
|
+
variants: buildVariants(roleModels, modelSettings),
|
|
102
|
+
modelSettings,
|
|
103
|
+
vvoc,
|
|
104
|
+
...(input.config.rawIntent === undefined ? {} : { rawIntent: input.config.rawIntent }),
|
|
105
|
+
...(input.config.rootDefault === undefined ? {} : { rootSelection: input.config.rootDefault }),
|
|
106
|
+
...(modelOverride === undefined ? {} : { modelOverride }),
|
|
107
|
+
intent: input.intent,
|
|
108
|
+
};
|
|
109
|
+
// Integrity covers roles, native overlays, raw vvoc and source variants, never generated ids.
|
|
110
|
+
const integrity = familyCaptureIntegrity(draft);
|
|
111
|
+
const snapshotId = digest(integrity).slice(0, 16);
|
|
112
|
+
const variants = draft.variants.map((variant) => ({
|
|
113
|
+
...variant,
|
|
114
|
+
id: variantIdFor(snapshotId, variant.modelID, variant.sourceVariant),
|
|
115
|
+
}));
|
|
116
|
+
return {
|
|
117
|
+
...draft,
|
|
118
|
+
snapshotId,
|
|
119
|
+
integrity,
|
|
120
|
+
variants,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
// END_BLOCK_CAPTURE_CONSTRUCTION
|
|
124
|
+
// START_BLOCK_LINEAGE_RESOLUTION
|
|
125
|
+
/** Resolve a session's family root through host-verified lineage. */
|
|
126
|
+
export async function resolveSessionFamily(input) {
|
|
127
|
+
const seen = new Set();
|
|
128
|
+
let current = input.sessionID;
|
|
129
|
+
for (let hop = 0; hop < MAX_LINEAGE_HOPS; hop += 1) {
|
|
130
|
+
if (seen.has(current)) {
|
|
131
|
+
throw new SnapshotAdmissionError("Session lineage contains a cycle; refusing to bind.");
|
|
132
|
+
}
|
|
133
|
+
seen.add(current);
|
|
134
|
+
const view = await input.readSession(current);
|
|
135
|
+
if (view === undefined) {
|
|
136
|
+
throw new SnapshotAdmissionError(`Session ${current} could not be read from the host.`);
|
|
137
|
+
}
|
|
138
|
+
// An empty parentID with a fork.sessionID means the fork's source is the parent.
|
|
139
|
+
const parent = typeof view.parentID === "string" && view.parentID.length > 0
|
|
140
|
+
? view.parentID
|
|
141
|
+
: typeof view.forkSessionID === "string" && view.forkSessionID.length > 0
|
|
142
|
+
? view.forkSessionID
|
|
143
|
+
: undefined;
|
|
144
|
+
if (parent === undefined)
|
|
145
|
+
return current;
|
|
146
|
+
current = parent;
|
|
147
|
+
}
|
|
148
|
+
throw new SnapshotAdmissionError("Session lineage exceeds the supported depth.");
|
|
149
|
+
}
|
|
150
|
+
// END_BLOCK_LINEAGE_RESOLUTION
|
|
151
|
+
// START_BLOCK_ADMISSION
|
|
152
|
+
function sameSelection(left, right) {
|
|
153
|
+
if (left === undefined || right === undefined)
|
|
154
|
+
return left === right;
|
|
155
|
+
return (left.providerID === right.providerID &&
|
|
156
|
+
left.modelID === right.modelID &&
|
|
157
|
+
(left.variant ?? "") === (right.variant ?? ""));
|
|
158
|
+
}
|
|
159
|
+
async function rollbackStaged(input) {
|
|
160
|
+
let current;
|
|
161
|
+
try {
|
|
162
|
+
current = await input.readSessionModel(input.sessionID);
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
return "none";
|
|
166
|
+
}
|
|
167
|
+
// Full identity including variant; a different current means a newer choice arrived.
|
|
168
|
+
if (!sameSelection(current, input.candidate.selection))
|
|
169
|
+
return "preserved-newer-choice";
|
|
170
|
+
if (input.candidate.before === undefined)
|
|
171
|
+
return "none";
|
|
172
|
+
try {
|
|
173
|
+
await input.switchModel({ sessionID: input.sessionID, model: input.candidate.before });
|
|
174
|
+
return "reverted";
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
return "none";
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function toReason(error) {
|
|
181
|
+
return error instanceof Error ? error.message : String(error);
|
|
182
|
+
}
|
|
183
|
+
/** Build the documented snapshot service for one native context. */
|
|
184
|
+
export function createSnapshotService(deps) {
|
|
185
|
+
const now = deps.now ?? Date.now;
|
|
186
|
+
const digest = deps.digest ?? defaultSnapshotDigest;
|
|
187
|
+
const inFlightSessions = new Map();
|
|
188
|
+
const inFlightAccepts = new Map();
|
|
189
|
+
const inFlightOwned = new Map();
|
|
190
|
+
/**
|
|
191
|
+
* Per-family serialization. Candidate read/write, switch and publication are
|
|
192
|
+
* ordered so a concurrent stage cannot be lost or overwritten between an
|
|
193
|
+
* acceptance validation and its publication, and late cleanup cannot delete a
|
|
194
|
+
* newer/committed capture. When a cross-context coordinator is present the lock
|
|
195
|
+
* is shared by every participating service, so two acceptors in distinct native
|
|
196
|
+
* contexts publish the family capture exactly once.
|
|
197
|
+
*/
|
|
198
|
+
const familyLocks = new Map();
|
|
199
|
+
let disposed = false;
|
|
200
|
+
function withFamilyLock(familyId, run) {
|
|
201
|
+
if (deps.coordination !== undefined) {
|
|
202
|
+
return deps.coordination.withFamilyLock(familyId, run);
|
|
203
|
+
}
|
|
204
|
+
const previous = familyLocks.get(familyId) ?? Promise.resolve();
|
|
205
|
+
const next = previous.then(run, run);
|
|
206
|
+
familyLocks.set(familyId, next.then(() => undefined, () => undefined));
|
|
207
|
+
return next;
|
|
208
|
+
}
|
|
209
|
+
const familyOf = async (sessionID) => resolveSessionFamily({ sessionID, readSession: deps.readSession });
|
|
210
|
+
const policy = async (sessionID) => {
|
|
211
|
+
if (disposed)
|
|
212
|
+
return undefined;
|
|
213
|
+
const familyId = await familyOf(sessionID);
|
|
214
|
+
return deps.store.read(familyId);
|
|
215
|
+
};
|
|
216
|
+
async function loadCaptures() {
|
|
217
|
+
const ids = await deps.store.list();
|
|
218
|
+
const captures = [];
|
|
219
|
+
for (const familyId of ids) {
|
|
220
|
+
const capture = await deps.store.read(familyId);
|
|
221
|
+
if (capture !== undefined)
|
|
222
|
+
captures.push(capture);
|
|
223
|
+
}
|
|
224
|
+
return captures;
|
|
225
|
+
}
|
|
226
|
+
async function doStage(request, familyId, materialize) {
|
|
227
|
+
const existing = await deps.store.read(familyId);
|
|
228
|
+
if (existing !== undefined) {
|
|
229
|
+
return { status: "reused", familyId, snapshotId: existing.snapshotId };
|
|
230
|
+
}
|
|
231
|
+
// A durable marker proves the family was bound before; a missing capture must fail closed.
|
|
232
|
+
if (await deps.store.readMarker(familyId)) {
|
|
233
|
+
return {
|
|
234
|
+
status: "rejected",
|
|
235
|
+
familyId,
|
|
236
|
+
error: "A previously bound family capture is missing; refusing to adopt current configuration.",
|
|
237
|
+
rollback: "none",
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
const view = await deps.readSession(request.sessionID);
|
|
241
|
+
if (view === undefined) {
|
|
242
|
+
return {
|
|
243
|
+
status: "rejected",
|
|
244
|
+
familyId,
|
|
245
|
+
error: `Session ${request.sessionID} could not be read for admission.`,
|
|
246
|
+
rollback: "none",
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
if (view.hasActivity === true && request.explicit === undefined) {
|
|
250
|
+
// A session that already did work without a durable capture must not adopt current policy.
|
|
251
|
+
return {
|
|
252
|
+
status: "rejected",
|
|
253
|
+
familyId,
|
|
254
|
+
error: "An active session has no persisted family capture; refusing to adopt current configuration.",
|
|
255
|
+
rollback: "none",
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
let config;
|
|
259
|
+
try {
|
|
260
|
+
// One coherent admission config: when the caller assembled it from a single
|
|
261
|
+
// read, use that exact value so the candidate target and the family capture
|
|
262
|
+
// cannot observe two different vvoc snapshots.
|
|
263
|
+
config = request.admissionConfig ?? (await deps.loadConfig(request.directory));
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
return {
|
|
267
|
+
status: "rejected",
|
|
268
|
+
familyId,
|
|
269
|
+
error: toReason(error),
|
|
270
|
+
rollback: "none",
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
const intent = request.intent ??
|
|
274
|
+
(request.explicit === undefined
|
|
275
|
+
? { mode: "implicit", source: "config" }
|
|
276
|
+
: { mode: "explicit", source: "config", literal: request.explicit });
|
|
277
|
+
let capture;
|
|
278
|
+
try {
|
|
279
|
+
// Resolve the family model before building the capture so `modelOverride`
|
|
280
|
+
// records the exact model this family binds. `explicit` is the user's
|
|
281
|
+
// documented choice; `selectionOverride` is a caller-resolved base (for
|
|
282
|
+
// example the current model when role overriding is disabled).
|
|
283
|
+
const parsedRoles = parseRoleSelections(config.roles);
|
|
284
|
+
const fallback = parsedRoles.default ??
|
|
285
|
+
config.rootDefault ??
|
|
286
|
+
Object.values(parsedRoles)[0] ??
|
|
287
|
+
config.agentBindings?.find((binding) => binding.selection !== undefined)?.selection;
|
|
288
|
+
const baseSelection = request.explicit ?? request.selectionOverride ?? fallback;
|
|
289
|
+
capture = buildFamilyCapture({
|
|
290
|
+
familyId,
|
|
291
|
+
location: request.location,
|
|
292
|
+
config,
|
|
293
|
+
intent,
|
|
294
|
+
...(baseSelection === undefined ? {} : { explicit: baseSelection }),
|
|
295
|
+
capturedAt: now(),
|
|
296
|
+
digest,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
catch (error) {
|
|
300
|
+
return {
|
|
301
|
+
status: "rejected",
|
|
302
|
+
familyId,
|
|
303
|
+
error: toReason(error),
|
|
304
|
+
rollback: "none",
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
// Per-input candidates: repeated identical input is idempotent, distinct
|
|
308
|
+
// inputs coexist up to MAX_STAGED_CANDIDATES, and the family's first
|
|
309
|
+
// accepted input decides the snapshot regardless of stage order.
|
|
310
|
+
const requestIdentity = stagedCandidateIdentity(request);
|
|
311
|
+
const candidates = await deps.store.readCandidates(familyId);
|
|
312
|
+
const sameInput = candidates.find((candidate) => stagedCandidateKey(candidate) === requestIdentity);
|
|
313
|
+
if (sameInput !== undefined && sameInput.revision === capture.integrity) {
|
|
314
|
+
// Repeated identical input is idempotent. When a caller escalates to
|
|
315
|
+
// force:true after an earlier force:false stage, the required materialization
|
|
316
|
+
// and awaited switch are reconciled here instead of returning early with zero
|
|
317
|
+
// switches, while a newer explicit choice is never overwritten.
|
|
318
|
+
if (request.force === true) {
|
|
319
|
+
const newerExplicit = request.explicit !== undefined && !sameSelection(request.explicit, sameInput.selection);
|
|
320
|
+
const current = await deps.readSessionModel(request.sessionID).catch(() => undefined);
|
|
321
|
+
const alreadyApplied = current !== undefined && sameSelection(current, sameInput.selection);
|
|
322
|
+
if (!newerExplicit && !alreadyApplied) {
|
|
323
|
+
try {
|
|
324
|
+
await materialize?.(sameInput.capture, sameInput.selection, requestIdentity);
|
|
325
|
+
await deps.switchModel({ sessionID: request.sessionID, model: sameInput.selection });
|
|
326
|
+
}
|
|
327
|
+
catch (error) {
|
|
328
|
+
return {
|
|
329
|
+
status: "rejected",
|
|
330
|
+
familyId,
|
|
331
|
+
candidateSelection: sameInput.selection,
|
|
332
|
+
error: toReason(error),
|
|
333
|
+
rollback: "none",
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
status: "staged",
|
|
340
|
+
familyId,
|
|
341
|
+
snapshotId: sameInput.capture.snapshotId,
|
|
342
|
+
candidateSelection: sameInput.selection,
|
|
343
|
+
rollback: "none",
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
if (sameInput === undefined && candidates.length >= MAX_STAGED_CANDIDATES) {
|
|
347
|
+
// Bounded set overflow fails the new stage closed rather than dropping an
|
|
348
|
+
// unaccepted input that native preparation may still accept later.
|
|
349
|
+
return {
|
|
350
|
+
status: "rejected",
|
|
351
|
+
familyId,
|
|
352
|
+
error: `The family already holds ${candidates.length} staged candidates; refusing to drop an unaccepted input.`,
|
|
353
|
+
rollback: "none",
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
const baseSelection = request.explicit ?? primarySelection(capture);
|
|
357
|
+
if (baseSelection === undefined) {
|
|
358
|
+
return {
|
|
359
|
+
status: "rejected",
|
|
360
|
+
familyId,
|
|
361
|
+
error: "The effective configuration has no selectable model policy.",
|
|
362
|
+
rollback: "none",
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
// The family's captured variant must be activated, not a sibling family's.
|
|
366
|
+
const selection = qualifySelection(capture, baseSelection);
|
|
367
|
+
try {
|
|
368
|
+
// Materialize the candidate variant before the switch so resolution finds it.
|
|
369
|
+
await materialize?.(capture, selection, requestIdentity);
|
|
370
|
+
}
|
|
371
|
+
catch (error) {
|
|
372
|
+
return {
|
|
373
|
+
status: "rejected",
|
|
374
|
+
familyId,
|
|
375
|
+
candidateSelection: selection,
|
|
376
|
+
error: toReason(error),
|
|
377
|
+
rollback: "none",
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
const candidate = {
|
|
381
|
+
familyId,
|
|
382
|
+
sessionID: request.sessionID,
|
|
383
|
+
...(request.inboxID === undefined ? {} : { inboxID: request.inboxID }),
|
|
384
|
+
...(request.operationID === undefined ? {} : { operationID: request.operationID }),
|
|
385
|
+
...(request.workload === undefined ? {} : { workload: request.workload }),
|
|
386
|
+
stagedAt: now(),
|
|
387
|
+
revision: capture.integrity,
|
|
388
|
+
selection,
|
|
389
|
+
...(request.before === undefined ? {} : { before: request.before }),
|
|
390
|
+
capture,
|
|
391
|
+
};
|
|
392
|
+
try {
|
|
393
|
+
await deps.store.writeCandidate(candidate);
|
|
394
|
+
}
|
|
395
|
+
catch (error) {
|
|
396
|
+
return {
|
|
397
|
+
status: "rejected",
|
|
398
|
+
familyId,
|
|
399
|
+
candidateSelection: selection,
|
|
400
|
+
error: toReason(error),
|
|
401
|
+
rollback: "none",
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
// A caller may bind policy without forcing a model change (role override off).
|
|
405
|
+
if (request.force === false) {
|
|
406
|
+
return {
|
|
407
|
+
status: "staged",
|
|
408
|
+
familyId,
|
|
409
|
+
snapshotId: capture.snapshotId,
|
|
410
|
+
candidateSelection: selection,
|
|
411
|
+
rollback: "none",
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
try {
|
|
415
|
+
await deps.switchModel({ sessionID: request.sessionID, model: selection });
|
|
416
|
+
}
|
|
417
|
+
catch (error) {
|
|
418
|
+
const rollback = await rollbackStaged({
|
|
419
|
+
sessionID: request.sessionID,
|
|
420
|
+
candidate,
|
|
421
|
+
readSessionModel: deps.readSessionModel,
|
|
422
|
+
switchModel: deps.switchModel,
|
|
423
|
+
});
|
|
424
|
+
// Remove only our own candidate; never another owner's.
|
|
425
|
+
await deps.store.removeCandidate(familyId, requestIdentity).catch(() => undefined);
|
|
426
|
+
return {
|
|
427
|
+
status: "rejected",
|
|
428
|
+
familyId,
|
|
429
|
+
candidateSelection: selection,
|
|
430
|
+
rollback,
|
|
431
|
+
error: toReason(error),
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
status: "staged",
|
|
436
|
+
familyId,
|
|
437
|
+
snapshotId: capture.snapshotId,
|
|
438
|
+
candidateSelection: selection,
|
|
439
|
+
rollback: "none",
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Publish the exact winning candidate as the bound family capture. The
|
|
444
|
+
* candidate must still exist with the same owner identity and revision; the
|
|
445
|
+
* bound marker is written first so a crash between the two writes fails closed:
|
|
446
|
+
* a capture is never readable without its marker, and a marker without a
|
|
447
|
+
* capture is refused on the next admission instead of adopting current
|
|
448
|
+
* configuration. Cleanup removes only the winning candidate, never a newer one.
|
|
449
|
+
*/
|
|
450
|
+
async function publishCandidate(familyId, expected) {
|
|
451
|
+
const key = stagedCandidateKey(expected);
|
|
452
|
+
const candidates = await deps.store.readCandidates(familyId);
|
|
453
|
+
const current = candidates.find((candidate) => stagedCandidateKey(candidate) === key);
|
|
454
|
+
if (current === undefined || current.revision !== expected.revision) {
|
|
455
|
+
return {
|
|
456
|
+
status: "rejected",
|
|
457
|
+
familyId,
|
|
458
|
+
error: "The staged candidate changed before publication; refusing to publish.",
|
|
459
|
+
rollback: "none",
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
try {
|
|
463
|
+
await deps.store.writeMarker(familyId);
|
|
464
|
+
await deps.store.write(familyId, current.capture);
|
|
465
|
+
}
|
|
466
|
+
catch (error) {
|
|
467
|
+
// Remove both halves so the next read cannot expose or silently reuse a
|
|
468
|
+
// partially published policy.
|
|
469
|
+
await deps.store.removeMarker(familyId).catch(() => undefined);
|
|
470
|
+
await deps.store.remove(familyId).catch(() => undefined);
|
|
471
|
+
// Publication failure must undo our own switch unless a newer choice replaced it.
|
|
472
|
+
const rollback = await rollbackStaged({
|
|
473
|
+
sessionID: current.sessionID,
|
|
474
|
+
candidate: current,
|
|
475
|
+
readSessionModel: deps.readSessionModel,
|
|
476
|
+
switchModel: deps.switchModel,
|
|
477
|
+
});
|
|
478
|
+
await deps.store.removeCandidate(familyId, key).catch(() => undefined);
|
|
479
|
+
return {
|
|
480
|
+
status: "rejected",
|
|
481
|
+
familyId,
|
|
482
|
+
candidateSelection: current.selection,
|
|
483
|
+
rollback,
|
|
484
|
+
error: toReason(error),
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
// Consume only the winning owner.
|
|
488
|
+
await deps.store.removeCandidate(familyId, key).catch(() => undefined);
|
|
489
|
+
return {
|
|
490
|
+
status: "bound",
|
|
491
|
+
familyId,
|
|
492
|
+
snapshotId: current.capture.snapshotId,
|
|
493
|
+
candidateSelection: current.selection,
|
|
494
|
+
rollback: "none",
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Owned generated-work gateway. Stages with the runtime's operation token and
|
|
499
|
+
* publishes only that exact owned candidate; it never adopts a prompt
|
|
500
|
+
* candidate.
|
|
501
|
+
*/
|
|
502
|
+
async function doAdmitOwned(familyId, request, materialize) {
|
|
503
|
+
const staged = await doStage(request, familyId, materialize);
|
|
504
|
+
if (staged.status === "rejected" || staged.status === "reused")
|
|
505
|
+
return staged;
|
|
506
|
+
const key = stagedCandidateIdentity(request);
|
|
507
|
+
const candidates = await deps.store.readCandidates(familyId);
|
|
508
|
+
const candidate = candidates.find((entry) => stagedCandidateKey(entry) === key);
|
|
509
|
+
if (candidate === undefined || candidate.operationID !== request.operationID) {
|
|
510
|
+
return {
|
|
511
|
+
status: "rejected",
|
|
512
|
+
familyId,
|
|
513
|
+
error: "The owned operation no longer owns the staged candidate.",
|
|
514
|
+
rollback: "none",
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
if (candidate.inboxID !== undefined) {
|
|
518
|
+
return {
|
|
519
|
+
status: "rejected",
|
|
520
|
+
familyId,
|
|
521
|
+
error: "Refusing to publish a prompt candidate through the owned gateway.",
|
|
522
|
+
rollback: "none",
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
return publishCandidate(familyId, candidate);
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* First-accepted publication: among every staged prompt candidate, publish the
|
|
529
|
+
* one whose native accepted input is earliest by durable event order, never the
|
|
530
|
+
* most recently staged or an arbitrary map entry. Covers delayed/missed live
|
|
531
|
+
* notifications by reconciling against the durable session log. An optional
|
|
532
|
+
* `validate` refuses a resolved model that does not belong to the winner
|
|
533
|
+
* without losing the winner's snapshot.
|
|
534
|
+
*/
|
|
535
|
+
async function doAccept(familyId, request) {
|
|
536
|
+
const existing = await deps.store.read(familyId);
|
|
537
|
+
if (existing !== undefined)
|
|
538
|
+
return { status: "reused", familyId, snapshotId: existing.snapshotId };
|
|
539
|
+
const candidates = await deps.store.readCandidates(familyId);
|
|
540
|
+
const promptCandidates = candidates.filter((candidate) => candidate.inboxID !== undefined && candidate.operationID === undefined);
|
|
541
|
+
if (promptCandidates.length === 0) {
|
|
542
|
+
return {
|
|
543
|
+
status: "rejected",
|
|
544
|
+
familyId,
|
|
545
|
+
error: "There is no staged prompt candidate to accept.",
|
|
546
|
+
rollback: "none",
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
const sessions = [...new Set(promptCandidates.map((candidate) => candidate.sessionID))];
|
|
550
|
+
const reconciliation = new Map();
|
|
551
|
+
for (const sessionID of sessions) {
|
|
552
|
+
const resolved = deps.readAcceptedInputs
|
|
553
|
+
? await deps.readAcceptedInputs(sessionID).catch(() => undefined)
|
|
554
|
+
: undefined;
|
|
555
|
+
reconciliation.set(sessionID, resolved);
|
|
556
|
+
}
|
|
557
|
+
// With more than one staged input, only a complete durable replay can prove
|
|
558
|
+
// which was accepted first; an incomplete/live-only view could bind a later
|
|
559
|
+
// policy, so fail closed until the log confirms the ordering.
|
|
560
|
+
if (promptCandidates.length > 1) {
|
|
561
|
+
for (const sessionID of sessions) {
|
|
562
|
+
const resolved = reconciliation.get(sessionID);
|
|
563
|
+
if (resolved === undefined || !resolved.complete) {
|
|
564
|
+
return {
|
|
565
|
+
status: "rejected",
|
|
566
|
+
familyId,
|
|
567
|
+
error: "Cannot establish the first accepted input without a complete session log; refusing to bind a later policy.",
|
|
568
|
+
rollback: "none",
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
const eligible = [];
|
|
574
|
+
for (const candidate of promptCandidates) {
|
|
575
|
+
const resolved = reconciliation.get(candidate.sessionID);
|
|
576
|
+
const match = resolved?.inputs.find((entry) => entry.inboxID === candidate.inboxID &&
|
|
577
|
+
(entry.itemType === "user" || entry.itemType === "synthetic"));
|
|
578
|
+
// A log-derived match is only trusted from a complete replay; a verified
|
|
579
|
+
// live native event may stand alone.
|
|
580
|
+
if (match !== undefined && (match.source === "live" || resolved?.complete === true)) {
|
|
581
|
+
eligible.push({ candidate, order: match });
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
if (eligible.length === 0) {
|
|
585
|
+
return {
|
|
586
|
+
status: "rejected",
|
|
587
|
+
familyId,
|
|
588
|
+
error: "No accepted input matching a staged workload was found.",
|
|
589
|
+
rollback: "none",
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
eligible.sort((left, right) => left.order.created - right.order.created ||
|
|
593
|
+
(left.order.seq ?? 0) - (right.order.seq ?? 0) ||
|
|
594
|
+
left.candidate.stagedAt - right.candidate.stagedAt ||
|
|
595
|
+
stagedCandidateKey(left.candidate).localeCompare(stagedCandidateKey(right.candidate)));
|
|
596
|
+
const winner = eligible[0];
|
|
597
|
+
if (request.validate !== undefined && !request.validate(winner.candidate.selection)) {
|
|
598
|
+
return {
|
|
599
|
+
status: "rejected",
|
|
600
|
+
familyId,
|
|
601
|
+
candidateSelection: winner.candidate.selection,
|
|
602
|
+
error: "The resolved model does not match the first accepted candidate; refusing to publish.",
|
|
603
|
+
rollback: "none",
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
return publishCandidate(familyId, winner.candidate);
|
|
607
|
+
}
|
|
608
|
+
async function stageWithFamily(request, materialize) {
|
|
609
|
+
const familyId = await familyOf(request.sessionID);
|
|
610
|
+
// Dedupe by exact input identity (session + inboxID/operationID/workload):
|
|
611
|
+
// concurrent distinct prompts must not share one staging promise, while a
|
|
612
|
+
// repeated identical input shares its first attempt.
|
|
613
|
+
const sessionKey = `${familyId}\u0000${stagedCandidateIdentity(request)}`;
|
|
614
|
+
const pending = inFlightSessions.get(sessionKey);
|
|
615
|
+
if (pending !== undefined)
|
|
616
|
+
return pending;
|
|
617
|
+
const promise = withFamilyLock(familyId, () => doStage(request, familyId, materialize)).finally(() => {
|
|
618
|
+
inFlightSessions.delete(sessionKey);
|
|
619
|
+
});
|
|
620
|
+
inFlightSessions.set(sessionKey, promise);
|
|
621
|
+
return promise;
|
|
622
|
+
}
|
|
623
|
+
const stage = (request, materialize) => {
|
|
624
|
+
if (disposed) {
|
|
625
|
+
return Promise.resolve({
|
|
626
|
+
status: "rejected",
|
|
627
|
+
familyId: request.sessionID,
|
|
628
|
+
error: "The snapshot service was released.",
|
|
629
|
+
rollback: "none",
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
return stageWithFamily(request, materialize);
|
|
633
|
+
};
|
|
634
|
+
const accept = (request) => {
|
|
635
|
+
if (disposed) {
|
|
636
|
+
return Promise.resolve({
|
|
637
|
+
status: "rejected",
|
|
638
|
+
familyId: request.sessionID,
|
|
639
|
+
error: "The snapshot service was released.",
|
|
640
|
+
rollback: "none",
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
return (async () => {
|
|
644
|
+
const familyId = await familyOf(request.sessionID);
|
|
645
|
+
const key = `${familyId}\u0000${request.sessionID}\u0000${request.inboxID ?? ""}`;
|
|
646
|
+
const pending = inFlightAccepts.get(key);
|
|
647
|
+
if (pending !== undefined)
|
|
648
|
+
return pending;
|
|
649
|
+
const promise = withFamilyLock(familyId, () => doAccept(familyId, request)).finally(() => {
|
|
650
|
+
inFlightAccepts.delete(key);
|
|
651
|
+
});
|
|
652
|
+
inFlightAccepts.set(key, promise);
|
|
653
|
+
return promise;
|
|
654
|
+
})();
|
|
655
|
+
};
|
|
656
|
+
const admitOwned = (request, materialize) => {
|
|
657
|
+
if (disposed) {
|
|
658
|
+
return Promise.resolve({
|
|
659
|
+
status: "rejected",
|
|
660
|
+
familyId: request.sessionID,
|
|
661
|
+
error: "The snapshot service was released.",
|
|
662
|
+
rollback: "none",
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
if (request.operationID.length === 0) {
|
|
666
|
+
return Promise.resolve({
|
|
667
|
+
status: "rejected",
|
|
668
|
+
familyId: request.sessionID,
|
|
669
|
+
error: "An owned admission requires a non-empty operation token.",
|
|
670
|
+
rollback: "none",
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
return (async () => {
|
|
674
|
+
const familyId = await familyOf(request.sessionID);
|
|
675
|
+
const key = `${familyId}\u0000${request.sessionID}\u0000${request.operationID}`;
|
|
676
|
+
const pending = inFlightOwned.get(key);
|
|
677
|
+
if (pending !== undefined)
|
|
678
|
+
return pending;
|
|
679
|
+
const promise = withFamilyLock(familyId, () => doAdmitOwned(familyId, request, materialize)).finally(() => {
|
|
680
|
+
inFlightOwned.delete(key);
|
|
681
|
+
});
|
|
682
|
+
inFlightOwned.set(key, promise);
|
|
683
|
+
return promise;
|
|
684
|
+
})();
|
|
685
|
+
};
|
|
686
|
+
const auxiliary = createAuxiliaryService({
|
|
687
|
+
familyOf,
|
|
688
|
+
policy,
|
|
689
|
+
session: deps.auxiliarySession,
|
|
690
|
+
});
|
|
691
|
+
return {
|
|
692
|
+
familyOf,
|
|
693
|
+
policy,
|
|
694
|
+
configFor: policy,
|
|
695
|
+
captures: async () => (disposed ? [] : loadCaptures()),
|
|
696
|
+
variants: async (familyId) => {
|
|
697
|
+
if (disposed)
|
|
698
|
+
return [];
|
|
699
|
+
const captures = (await loadCaptures()).filter((capture) => familyId === undefined || capture.familyId === familyId);
|
|
700
|
+
return captures.flatMap((capture) => capture.variants.map((variant) => ({
|
|
701
|
+
providerID: variant.providerID,
|
|
702
|
+
modelID: variant.modelID,
|
|
703
|
+
variant,
|
|
704
|
+
})));
|
|
705
|
+
},
|
|
706
|
+
stage,
|
|
707
|
+
accept,
|
|
708
|
+
admitOwned,
|
|
709
|
+
hasStaged: async (familyId) => (await deps.store.readCandidates(familyId)).length > 0,
|
|
710
|
+
auxiliary,
|
|
711
|
+
dispose() {
|
|
712
|
+
disposed = true;
|
|
713
|
+
inFlightSessions.clear();
|
|
714
|
+
inFlightAccepts.clear();
|
|
715
|
+
inFlightOwned.clear();
|
|
716
|
+
familyLocks.clear();
|
|
717
|
+
},
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
// END_BLOCK_ADMISSION
|
|
721
|
+
//# sourceMappingURL=snapshots.js.map
|