pi-daddy 0.22.0 → 0.23.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 +30 -0
- package/contracts/daily-view/v1/README.md +114 -0
- package/contracts/daily-view/v1/fixtures/view.json +525 -0
- package/contracts/daily-view/v1/fixtures/view.txt +75 -0
- package/contracts/daily-view/v1/fixtures/work.jsonl +17 -0
- package/contracts/daily-view/v1/p03/README.md +26 -0
- package/contracts/daily-view/v1/p03/fixtures/retained-executions.json +241 -0
- package/contracts/daily-view/v1/p03/projection.schema.json +194 -0
- package/contracts/daily-view/v1/p03/provenance.json +21 -0
- package/contracts/dashboard-host/v1/README.md +176 -0
- package/contracts/debrief/v1/README.md +146 -0
- package/contracts/debrief/v1/intervention/README.md +32 -0
- package/contracts/debrief/v1/intervention/fixtures/artifact-3ee11136beb864f518252e5024dae9197421fbf97b6e981d3243c782b5c44bd0.txt +1 -0
- package/contracts/debrief/v1/intervention/fixtures/artifact-58905d872dd3c54a9705f67b05fa82edbad7af6476ae8993faf1a7b9fadfb905.txt +1 -0
- package/contracts/debrief/v1/intervention/fixtures/assessment.json +29 -0
- package/contracts/debrief/v1/intervention/fixtures/blind-view.json +20 -0
- package/contracts/debrief/v1/intervention/fixtures/choice.json +7 -0
- package/contracts/debrief/v1/intervention/fixtures/manifest.json +57 -0
- package/contracts/debrief/v1/intervention/fixtures/reveal.json +39 -0
- package/contracts/debrief/v1/provenance.json +89 -0
- package/contracts/debrief/v1/work-capture/README.md +38 -0
- package/contracts/debrief/v1/work-capture/fixtures/batch.json +17 -0
- package/contracts/debrief/v1/work-capture/fixtures/cases.json +184 -0
- package/contracts/debrief/v1/work-capture/fixtures/review.json +184 -0
- package/contracts/debrief/v1/work-capture/review-request.schema.json +41 -0
- package/contracts/debrief/v1/work-capture/work-case.schema.json +190 -0
- package/contracts/debrief/v2/README.md +71 -0
- package/contracts/debrief/v2/input-pin.json +66 -0
- package/contracts/debrief/v2/intervention-upstream.md +40 -0
- package/contracts/debrief/v2/work-signals-upstream.md +22 -0
- package/contracts/dispatch-control/v1/README.md +109 -0
- package/contracts/dispatch-control/v1/request.schema.json +19 -0
- package/contracts/effect-profile/v1/README.md +124 -0
- package/contracts/execution-retention/v1/README.md +109 -0
- package/contracts/execution-retention/v2/README.md +148 -0
- package/contracts/execution-retention/v2/fixtures/interrupted-native-bytes.json +91 -0
- package/contracts/execution-retention/v2/fixtures/missing-native-session.json +91 -0
- package/contracts/execution-retention/v2/fixtures/native-file-unknown-branch.json +91 -0
- package/contracts/execution-retention/v2/fixtures/native-id-without-bytes.json +91 -0
- package/contracts/execution-retention/v2/fixtures/native-live-branch.json +90 -0
- package/contracts/execution-retention/v2/fixtures/replaced-native-session.json +92 -0
- package/contracts/execution-retention/v2/fixtures/result-82c620cdfeb8ca6b858cf653d7f5e2eedca5315d6244216d60e8b36f03223d6a.bin +1 -0
- package/contracts/execution-retention/v2/fixtures/session-2ec1f134bc739984434f7fca689e1c3d1c39fce2dcadae709e9b6faedfcc4ac7.bin +3 -0
- package/contracts/execution-retention/v2/fixtures/session-9bdb4bd16a901da31580acf056bda525225259e15ec409e0410480109011772f.bin +3 -0
- package/contracts/execution-retention/v2/manifest.schema.json +858 -0
- package/contracts/experiment/v1/README.md +164 -0
- package/contracts/factory-order/v1/README.md +151 -0
- package/contracts/factory-order/v1/adoption-pin.json +8 -0
- package/contracts/intent-control/v1/README.md +135 -0
- package/contracts/intent-control/v1/request.schema.json +1096 -0
- package/contracts/intent-control/v2/README.md +34 -0
- package/contracts/intent-control/v2/request.schema.json +1070 -0
- package/contracts/ledger/v4/README.md +241 -0
- package/contracts/ledger/v4/fixtures/layout-options.json +428 -0
- package/contracts/ledger/v4/fixtures/work-acceptance.json +57 -0
- package/contracts/ledger/v4/fixtures/work-occurrence.json +44 -0
- package/contracts/ledger/v4/fixtures/work-revision.json +26 -0
- package/contracts/ledger/v4/fixtures/work-snapshot.json +73 -0
- package/contracts/ledger/v4/ledger-event.schema.json +162 -0
- package/contracts/ordinary-control/v1/README.md +59 -0
- package/contracts/producer-ipc/v1/README.md +137 -0
- package/dist/check-runner.d.ts +5 -0
- package/dist/check-runner.d.ts.map +1 -1
- package/dist/check-runner.js +16 -2
- package/dist/check-runner.js.map +1 -1
- package/dist/control-journal.d.ts +20 -0
- package/dist/control-journal.d.ts.map +1 -0
- package/dist/control-journal.js +73 -0
- package/dist/control-journal.js.map +1 -0
- package/dist/daily-view-input.d.ts +44 -0
- package/dist/daily-view-input.d.ts.map +1 -0
- package/dist/daily-view-input.js +68 -0
- package/dist/daily-view-input.js.map +1 -0
- package/dist/daily-view-render.d.ts +4 -0
- package/dist/daily-view-render.d.ts.map +1 -0
- package/dist/daily-view-render.js +41 -0
- package/dist/daily-view-render.js.map +1 -0
- package/dist/daily-view.d.ts +74 -0
- package/dist/daily-view.d.ts.map +1 -0
- package/dist/daily-view.js +170 -0
- package/dist/daily-view.js.map +1 -0
- package/dist/dashboard-cli.d.ts +26 -1
- package/dist/dashboard-cli.d.ts.map +1 -1
- package/dist/dashboard-cli.js +121 -3
- package/dist/dashboard-cli.js.map +1 -1
- package/dist/dashboard-harness.d.ts +19 -0
- package/dist/dashboard-harness.d.ts.map +1 -0
- package/dist/dashboard-harness.js +75 -0
- package/dist/dashboard-harness.js.map +1 -0
- package/dist/dashboard-herdr.d.ts.map +1 -1
- package/dist/dashboard-herdr.js +7 -2
- package/dist/dashboard-herdr.js.map +1 -1
- package/dist/dashboard-host-contract.d.ts +111 -0
- package/dist/dashboard-host-contract.d.ts.map +1 -0
- package/dist/dashboard-host-contract.js +14 -0
- package/dist/dashboard-host-contract.js.map +1 -0
- package/dist/dashboard-host-transport.d.ts +15 -0
- package/dist/dashboard-host-transport.d.ts.map +1 -0
- package/dist/dashboard-host-transport.js +122 -0
- package/dist/dashboard-host-transport.js.map +1 -0
- package/dist/dashboard-host.d.ts +149 -0
- package/dist/dashboard-host.d.ts.map +1 -0
- package/dist/dashboard-host.js +329 -0
- package/dist/dashboard-host.js.map +1 -0
- package/dist/dashboard-observation.d.ts +28 -0
- package/dist/dashboard-observation.d.ts.map +1 -0
- package/dist/dashboard-observation.js +86 -0
- package/dist/dashboard-observation.js.map +1 -0
- package/dist/debrief-contract.d.ts +65 -0
- package/dist/debrief-contract.d.ts.map +1 -0
- package/dist/debrief-contract.js +151 -0
- package/dist/debrief-contract.js.map +1 -0
- package/dist/debrief-fixture.d.ts +16 -0
- package/dist/debrief-fixture.d.ts.map +1 -0
- package/dist/debrief-fixture.js +41 -0
- package/dist/debrief-fixture.js.map +1 -0
- package/dist/debrief-host.d.ts +59 -0
- package/dist/debrief-host.d.ts.map +1 -0
- package/dist/debrief-host.js +49 -0
- package/dist/debrief-host.js.map +1 -0
- package/dist/debrief-render.d.ts +5 -0
- package/dist/debrief-render.d.ts.map +1 -0
- package/dist/debrief-render.js +65 -0
- package/dist/debrief-render.js.map +1 -0
- package/dist/debrief.d.ts +90 -0
- package/dist/debrief.d.ts.map +1 -0
- package/dist/debrief.js +263 -0
- package/dist/debrief.js.map +1 -0
- package/dist/delegate-types.d.ts +2 -0
- package/dist/delegate-types.d.ts.map +1 -1
- package/dist/delegate-types.js.map +1 -1
- package/dist/delegate.d.ts.map +1 -1
- package/dist/delegate.js +1 -0
- package/dist/delegate.js.map +1 -1
- package/dist/dispatch-control.d.ts +45 -0
- package/dist/dispatch-control.d.ts.map +1 -0
- package/dist/dispatch-control.js +76 -0
- package/dist/dispatch-control.js.map +1 -0
- package/dist/effect-profile-runtime.d.ts +40 -0
- package/dist/effect-profile-runtime.d.ts.map +1 -0
- package/dist/effect-profile-runtime.js +51 -0
- package/dist/effect-profile-runtime.js.map +1 -0
- package/dist/effect-profile.d.ts +40 -0
- package/dist/effect-profile.d.ts.map +1 -0
- package/dist/effect-profile.js +152 -0
- package/dist/effect-profile.js.map +1 -0
- package/dist/execution-retention.d.ts +90 -0
- package/dist/execution-retention.d.ts.map +1 -0
- package/dist/execution-retention.js +247 -0
- package/dist/execution-retention.js.map +1 -0
- package/dist/experiment-contract.d.ts +56 -0
- package/dist/experiment-contract.d.ts.map +1 -0
- package/dist/experiment-contract.js +80 -0
- package/dist/experiment-contract.js.map +1 -0
- package/dist/experiment-state.d.ts +22 -0
- package/dist/experiment-state.d.ts.map +1 -0
- package/dist/experiment-state.js +79 -0
- package/dist/experiment-state.js.map +1 -0
- package/dist/experiment-store.d.ts +28 -0
- package/dist/experiment-store.d.ts.map +1 -0
- package/dist/experiment-store.js +130 -0
- package/dist/experiment-store.js.map +1 -0
- package/dist/experiment.d.ts +65 -0
- package/dist/experiment.d.ts.map +1 -0
- package/dist/experiment.js +357 -0
- package/dist/experiment.js.map +1 -0
- package/dist/factory-contract.d.ts +79 -0
- package/dist/factory-contract.d.ts.map +1 -0
- package/dist/factory-contract.js +89 -0
- package/dist/factory-contract.js.map +1 -0
- package/dist/factory-migration.d.ts +20 -0
- package/dist/factory-migration.d.ts.map +1 -0
- package/dist/factory-migration.js +55 -0
- package/dist/factory-migration.js.map +1 -0
- package/dist/factory-order.d.ts +848 -0
- package/dist/factory-order.d.ts.map +1 -0
- package/dist/factory-order.js +83 -0
- package/dist/factory-order.js.map +1 -0
- package/dist/factory-registry.d.ts +121 -0
- package/dist/factory-registry.d.ts.map +1 -0
- package/dist/factory-registry.js +133 -0
- package/dist/factory-registry.js.map +1 -0
- package/dist/file-lock.d.ts +8 -2
- package/dist/file-lock.d.ts.map +1 -1
- package/dist/file-lock.js +78 -19
- package/dist/file-lock.js.map +1 -1
- package/dist/herdr-poll.d.ts +8 -0
- package/dist/herdr-poll.d.ts.map +1 -1
- package/dist/herdr-poll.js +23 -0
- package/dist/herdr-poll.js.map +1 -1
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -1
- package/dist/intent-application.d.ts +17 -0
- package/dist/intent-application.d.ts.map +1 -0
- package/dist/intent-application.js +113 -0
- package/dist/intent-application.js.map +1 -0
- package/dist/intent-control.d.ts +201 -0
- package/dist/intent-control.d.ts.map +1 -0
- package/dist/intent-control.js +110 -0
- package/dist/intent-control.js.map +1 -0
- package/dist/intent-scheduling.d.ts +9 -0
- package/dist/intent-scheduling.d.ts.map +1 -0
- package/dist/intent-scheduling.js +15 -0
- package/dist/intent-scheduling.js.map +1 -0
- package/dist/ledger-append.d.ts +15 -0
- package/dist/ledger-append.d.ts.map +1 -0
- package/dist/ledger-append.js +94 -0
- package/dist/ledger-append.js.map +1 -0
- package/dist/ledger.d.ts +11 -0
- package/dist/ledger.d.ts.map +1 -1
- package/dist/ledger.js +3 -7
- package/dist/ledger.js.map +1 -1
- package/dist/native-session-target.d.ts +9 -0
- package/dist/native-session-target.d.ts.map +1 -0
- package/dist/native-session-target.js +29 -0
- package/dist/native-session-target.js.map +1 -0
- package/dist/native-session.d.ts +59 -0
- package/dist/native-session.d.ts.map +1 -0
- package/dist/native-session.js +154 -0
- package/dist/native-session.js.map +1 -0
- package/dist/order-schedule.d.ts +37 -0
- package/dist/order-schedule.d.ts.map +1 -0
- package/dist/order-schedule.js +81 -0
- package/dist/order-schedule.js.map +1 -0
- package/dist/ordinary-children.d.ts +52 -0
- package/dist/ordinary-children.d.ts.map +1 -0
- package/dist/ordinary-children.js +93 -0
- package/dist/ordinary-children.js.map +1 -0
- package/dist/producer-ipc-contract.d.ts +29 -0
- package/dist/producer-ipc-contract.d.ts.map +1 -0
- package/dist/producer-ipc-contract.js +29 -0
- package/dist/producer-ipc-contract.js.map +1 -0
- package/dist/producer-ipc.d.ts +55 -0
- package/dist/producer-ipc.d.ts.map +1 -0
- package/dist/producer-ipc.js +143 -0
- package/dist/producer-ipc.js.map +1 -0
- package/dist/resource-budget.d.ts +334 -0
- package/dist/resource-budget.d.ts.map +1 -0
- package/dist/resource-budget.js +415 -0
- package/dist/resource-budget.js.map +1 -0
- package/dist/retention-contract.d.ts +73 -0
- package/dist/retention-contract.d.ts.map +1 -0
- package/dist/retention-contract.js +104 -0
- package/dist/retention-contract.js.map +1 -0
- package/dist/retention-json.d.ts +3 -0
- package/dist/retention-json.d.ts.map +1 -0
- package/dist/retention-json.js +32 -0
- package/dist/retention-json.js.map +1 -0
- package/dist/run-child.d.ts +4 -0
- package/dist/run-child.d.ts.map +1 -1
- package/dist/run-child.js +16 -2
- package/dist/run-child.js.map +1 -1
- package/dist/run-herdr.d.ts +6 -0
- package/dist/run-herdr.d.ts.map +1 -1
- package/dist/run-herdr.js +19 -6
- package/dist/run-herdr.js.map +1 -1
- package/dist/vendor/adoption.d.ts +129 -0
- package/dist/vendor/adoption.d.ts.map +1 -0
- package/dist/vendor/adoption.js +102 -0
- package/dist/vendor/adoption.js.map +1 -0
- package/dist/work-ledger-destination.d.ts +20 -0
- package/dist/work-ledger-destination.d.ts.map +1 -0
- package/dist/work-ledger-destination.js +131 -0
- package/dist/work-ledger-destination.js.map +1 -0
- package/dist/work-ledger-json.d.ts +25 -0
- package/dist/work-ledger-json.d.ts.map +1 -0
- package/dist/work-ledger-json.js +267 -0
- package/dist/work-ledger-json.js.map +1 -0
- package/dist/work-ledger-occurrences.d.ts +227 -0
- package/dist/work-ledger-occurrences.d.ts.map +1 -0
- package/dist/work-ledger-occurrences.js +115 -0
- package/dist/work-ledger-occurrences.js.map +1 -0
- package/dist/work-ledger-projection.d.ts +295 -0
- package/dist/work-ledger-projection.d.ts.map +1 -0
- package/dist/work-ledger-projection.js +237 -0
- package/dist/work-ledger-projection.js.map +1 -0
- package/dist/work-ledger-snapshot.d.ts +29 -0
- package/dist/work-ledger-snapshot.d.ts.map +1 -0
- package/dist/work-ledger-snapshot.js +267 -0
- package/dist/work-ledger-snapshot.js.map +1 -0
- package/dist/work-ledger-types.d.ts +261 -0
- package/dist/work-ledger-types.d.ts.map +1 -0
- package/dist/work-ledger-types.js +13 -0
- package/dist/work-ledger-types.js.map +1 -0
- package/dist/work-ledger-validation.d.ts +14 -0
- package/dist/work-ledger-validation.d.ts.map +1 -0
- package/dist/work-ledger-validation.js +226 -0
- package/dist/work-ledger-validation.js.map +1 -0
- package/dist/work-ledger.d.ts +43 -0
- package/dist/work-ledger.d.ts.map +1 -0
- package/dist/work-ledger.js +225 -0
- package/dist/work-ledger.js.map +1 -0
- package/extensions/delegate-chain.ts +1 -0
- package/extensions/delegation-native.ts +12 -0
- package/extensions/delegation.ts +4 -3
- package/extensions/execute-child.ts +34 -4
- package/extensions/grants.ts +2 -0
- package/extensions/ordinary-runtime.ts +14 -0
- package/extensions/run-delegation.ts +7 -7
- package/extensions/session.ts +3 -1
- package/package.json +78 -4
- package/src/check-runner.ts +20 -2
- package/src/control-journal.ts +26 -0
- package/src/daily-view-input.ts +59 -0
- package/src/daily-view-render.ts +44 -0
- package/src/daily-view.ts +152 -0
- package/src/dashboard-cli.ts +103 -4
- package/src/dashboard-harness.ts +28 -0
- package/src/dashboard-herdr.ts +7 -2
- package/src/dashboard-host-contract.ts +42 -0
- package/src/dashboard-host-transport.ts +38 -0
- package/src/dashboard-host.ts +168 -0
- package/src/dashboard-observation.ts +44 -0
- package/src/debrief-contract.ts +127 -0
- package/src/debrief-fixture.ts +29 -0
- package/src/debrief-host.ts +53 -0
- package/src/debrief-render.ts +49 -0
- package/src/debrief.ts +198 -0
- package/src/delegate-types.ts +2 -0
- package/src/delegate.ts +1 -0
- package/src/dispatch-control.ts +81 -0
- package/src/effect-profile-runtime.ts +46 -0
- package/src/effect-profile.ts +129 -0
- package/src/execution-retention.ts +230 -0
- package/src/experiment-contract.ts +66 -0
- package/src/experiment-state.ts +44 -0
- package/src/experiment-store.ts +90 -0
- package/src/experiment.ts +231 -0
- package/src/factory-contract.ts +47 -0
- package/src/factory-migration.ts +29 -0
- package/src/factory-order.ts +56 -0
- package/src/factory-registry.ts +60 -0
- package/src/file-lock.ts +47 -11
- package/src/herdr-poll.ts +18 -0
- package/src/index.ts +32 -0
- package/src/intent-application.ts +90 -0
- package/src/intent-control.ts +107 -0
- package/src/intent-scheduling.ts +14 -0
- package/src/ledger-append.ts +72 -0
- package/src/ledger.ts +3 -9
- package/src/native-session-target.ts +23 -0
- package/src/native-session.ts +139 -0
- package/src/order-schedule.ts +42 -0
- package/src/ordinary-children.ts +59 -0
- package/src/producer-ipc-contract.ts +26 -0
- package/src/producer-ipc.ts +107 -0
- package/src/resource-budget.ts +349 -0
- package/src/retention-contract.ts +84 -0
- package/src/retention-json.ts +20 -0
- package/src/run-child.ts +13 -2
- package/src/run-herdr.ts +11 -7
- package/src/vendor/adoption.ts +109 -0
- package/src/work-ledger-destination.ts +113 -0
- package/src/work-ledger-json.ts +228 -0
- package/src/work-ledger-occurrences.ts +99 -0
- package/src/work-ledger-projection.ts +262 -0
- package/src/work-ledger-snapshot.ts +246 -0
- package/src/work-ledger-types.ts +229 -0
- package/src/work-ledger-validation.ts +174 -0
- package/src/work-ledger.ts +186 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { indexWorkLedgerText, workEventConflictSources, workRevisionConflictSources } from "./work-ledger-projection.ts";
|
|
2
|
+
import { canonicalWorkJson, copyWorkJson, freezeWork } from "./work-ledger-json.ts";
|
|
3
|
+
import { validateWorkSelection } from "./work-ledger-validation.ts";
|
|
4
|
+
import {
|
|
5
|
+
WorkInputError, type EventRef, type Identity, type ObligationBinding, type RevisionRef,
|
|
6
|
+
type WorkConflict, type WorkDiagnostic, type WorkFrozen, type WorkProblem, type WorkReference,
|
|
7
|
+
type WorkResolutionCode, type WorkRevision, type WorkSnapshot,
|
|
8
|
+
} from "./work-ledger-types.ts";
|
|
9
|
+
|
|
10
|
+
type Ref = WorkFrozen<RevisionRef>;
|
|
11
|
+
type Revision = WorkFrozen<WorkRevision>;
|
|
12
|
+
type Problem = WorkFrozen<WorkProblem>;
|
|
13
|
+
/** Internal shape of WorkProjectionContext.selectedSnapshot, not an authority context. */
|
|
14
|
+
interface Selection { snapshot: Identity; event: EventRef }
|
|
15
|
+
/** Internal structural result only. Never a substitute for WorkProjection/acceptance/runtime. */
|
|
16
|
+
interface SnapshotResolution {
|
|
17
|
+
readonly scopeState: "unselected" | "valid" | "unresolved" | "invalid";
|
|
18
|
+
readonly selectedSnapshot: WorkFrozen<Identity> | null;
|
|
19
|
+
readonly snapshot: WorkFrozen<WorkSnapshot> | null;
|
|
20
|
+
readonly scope: Revision | null;
|
|
21
|
+
readonly revisions: readonly Revision[];
|
|
22
|
+
readonly obligations: ReadonlyArray<{
|
|
23
|
+
readonly binding: WorkFrozen<ObligationBinding>;
|
|
24
|
+
readonly revision: Revision;
|
|
25
|
+
readonly artifact: Revision | null;
|
|
26
|
+
readonly problems: readonly Problem[];
|
|
27
|
+
}>;
|
|
28
|
+
readonly problems: readonly Problem[];
|
|
29
|
+
readonly conflicts: ReadonlyArray<WorkFrozen<WorkConflict>>;
|
|
30
|
+
readonly errors: ReadonlyArray<WorkFrozen<WorkDiagnostic>>;
|
|
31
|
+
}
|
|
32
|
+
const key = canonicalWorkJson;
|
|
33
|
+
const entity = (ref: Ref): string => `${ref.kind}:${ref.id}`;
|
|
34
|
+
const reference = (ref: Ref): WorkFrozen<WorkReference> => ({ type: "revision", ref });
|
|
35
|
+
function unique<T>(values: readonly T[]): T[] {
|
|
36
|
+
const rows = new Map(values.map(value => [key(value), value]));
|
|
37
|
+
return [...rows].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([, value]) => value);
|
|
38
|
+
}
|
|
39
|
+
function diagnosticKey(row: Problem | WorkFrozen<WorkConflict>): string {
|
|
40
|
+
// Internally built output collections may exceed the wire array bound (e.g. 300 conflicting
|
|
41
|
+
// deliveries). Only their validated scalar/reference constituents use the wire canonicalizer.
|
|
42
|
+
const affected = key(row.affectedObligations);
|
|
43
|
+
return "code" in row
|
|
44
|
+
? `{"affectedObligations":${affected},"code":${JSON.stringify(row.code)},"reference":${key(row.reference)}}`
|
|
45
|
+
: `{"affectedObligations":${affected},"digests":${JSON.stringify(row.digests)},"id":${JSON.stringify(row.id)},"kind":${JSON.stringify(row.kind)}}`;
|
|
46
|
+
}
|
|
47
|
+
function sortedDiagnostics<T extends Problem | WorkFrozen<WorkConflict>>(rows: T[]): T[] {
|
|
48
|
+
return rows.map(value => ({ value, key: diagnosticKey(value) }))
|
|
49
|
+
.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0).map(row => row.value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** @internal Real builder/text/index/selector path. No unchecked parsed-object projection entry. */
|
|
53
|
+
export function resolveWorkSnapshotText(text: string, requested: WorkFrozen<Selection> | null = null): SnapshotResolution {
|
|
54
|
+
const index = indexWorkLedgerText(text);
|
|
55
|
+
const errors: WorkFrozen<WorkDiagnostic>[] = [...index.ingestion.errors];
|
|
56
|
+
let selection: WorkFrozen<Selection> | null = null;
|
|
57
|
+
try {
|
|
58
|
+
const copied = copyWorkJson(requested);
|
|
59
|
+
validateWorkSelection(copied);
|
|
60
|
+
selection = copied as unknown as WorkFrozen<Selection> | null;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (!(error instanceof WorkInputError)) throw error;
|
|
63
|
+
errors.push({ line: null, code: "WORK_CONTEXT_INVALID" });
|
|
64
|
+
}
|
|
65
|
+
const problems = new Map<string, Problem>();
|
|
66
|
+
const conflicts = new Map<string, { digests: string[]; affected: Ref[] }>();
|
|
67
|
+
for (const ref of index.conflictingEvents) {
|
|
68
|
+
const row = conflicts.get(ref.eventId) ?? { digests: [], affected: [] };
|
|
69
|
+
row.digests.push(ref.digest); conflicts.set(ref.eventId, row);
|
|
70
|
+
}
|
|
71
|
+
function problem(code: WorkResolutionCode, ref: WorkFrozen<WorkReference> | null, affected: readonly Ref[] = []) {
|
|
72
|
+
const id = `${code}:${key(ref)}`;
|
|
73
|
+
const old = problems.get(id);
|
|
74
|
+
problems.set(id, { code, reference: ref, affectedObligations: unique([...(old?.affectedObligations ?? []), ...affected]) });
|
|
75
|
+
}
|
|
76
|
+
function conflict(id: string, affected: readonly Ref[]) {
|
|
77
|
+
const row = conflicts.get(id);
|
|
78
|
+
if (!row) return;
|
|
79
|
+
row.affected = unique([...row.affected, ...affected]);
|
|
80
|
+
for (const digest of row.digests) problem("EVENT_CONFLICT", { type: "event", ref: { eventId: id, digest } }, affected);
|
|
81
|
+
}
|
|
82
|
+
function reports() {
|
|
83
|
+
return {
|
|
84
|
+
problems: sortedDiagnostics([...problems.values()]),
|
|
85
|
+
conflicts: sortedDiagnostics([...conflicts].map(([id, row]) => ({
|
|
86
|
+
kind: "event" as const, id, digests: [...row.digests].sort(), affectedObligations: unique(row.affected),
|
|
87
|
+
}))), errors,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function empty(scopeState: SnapshotResolution["scopeState"]): SnapshotResolution {
|
|
91
|
+
return freezeWork({ scopeState, selectedSnapshot: selection?.snapshot ?? null,
|
|
92
|
+
snapshot: null, scope: null, revisions: [], obligations: [], ...reports() });
|
|
93
|
+
}
|
|
94
|
+
if (errors.length) { problem("INPUT_INCOMPLETE", null); return empty("unresolved"); }
|
|
95
|
+
if (!selection) { problem("NO_SELECTION", null); return empty("unselected"); }
|
|
96
|
+
|
|
97
|
+
// Exact event AND nested snapshot identity: never elect a head from candidate order or time.
|
|
98
|
+
const nominated = index.candidates.find(event => event.eventId === selection!.event.eventId && event.digest === selection!.event.digest);
|
|
99
|
+
const exactSnapshot = nominated?.event === "work_snapshot" &&
|
|
100
|
+
nominated.payload.snapshot.snapshotId === selection.snapshot.id && nominated.payload.snapshot.digest === selection.snapshot.digest
|
|
101
|
+
? nominated.payload.snapshot : null;
|
|
102
|
+
// A nominated inventory is diagnostic evidence only until ALL conflicting source IDs have been
|
|
103
|
+
// checked. Nested identity, not delivery ID/time, connects an alias to the selected closure.
|
|
104
|
+
const affected = exactSnapshot?.revisions.filter(ref => ref.kind === "obligation") ?? [];
|
|
105
|
+
const snapshotSources = workEventConflictSources(index, exactSnapshot ? nominated! : selection.event);
|
|
106
|
+
for (const source of snapshotSources) conflict(source.eventId, affected);
|
|
107
|
+
// An empty selected inventory is still quarantined: affected.length is not a validity test.
|
|
108
|
+
if (snapshotSources.length) return empty("unresolved");
|
|
109
|
+
if (!nominated) { problem("REFERENCE_MISSING", { type: "event", ref: selection.event }); return empty("unresolved"); }
|
|
110
|
+
if (!exactSnapshot) {
|
|
111
|
+
problem("SCOPE_INVALID", { type: "snapshot", ref: selection.snapshot }); return empty("invalid");
|
|
112
|
+
}
|
|
113
|
+
const snapshot = exactSnapshot;
|
|
114
|
+
const selected = new Map<string, Ref>();
|
|
115
|
+
let invalid = false, missing = false;
|
|
116
|
+
function reject(code: WorkResolutionCode, ref: Ref, affected: readonly Ref[] = []) {
|
|
117
|
+
invalid = true; problem(code, reference(ref), affected);
|
|
118
|
+
}
|
|
119
|
+
for (const ref of [snapshot.scope, ...snapshot.revisions]) {
|
|
120
|
+
if (selected.has(entity(ref))) reject("REVISION_INVALID", ref);
|
|
121
|
+
selected.set(entity(ref), ref); // Diagnostic lookup only if invalid; no rows will be exposed.
|
|
122
|
+
}
|
|
123
|
+
const obligationRefs = snapshot.revisions.filter(ref => ref.kind === "obligation");
|
|
124
|
+
const allAffected = unique(obligationRefs);
|
|
125
|
+
const records = new Map<string, Revision>();
|
|
126
|
+
const quarantineSources = new Map<string, readonly WorkFrozen<EventRef>[]>();
|
|
127
|
+
for (const event of index.candidates) if (event.event === "work_revision") {
|
|
128
|
+
const r = event.payload.revision;
|
|
129
|
+
const ref = { kind: r.kind, id: r.id, revision: r.revision, digest: r.digest };
|
|
130
|
+
if (!records.has(key(ref))) {
|
|
131
|
+
const sources = workRevisionConflictSources(index, ref);
|
|
132
|
+
if (sources.length) quarantineSources.set(key(ref), sources);
|
|
133
|
+
}
|
|
134
|
+
records.set(key(ref), r);
|
|
135
|
+
}
|
|
136
|
+
const isSelected = (ref: Ref) => key(selected.get(entity(ref)) ?? null) === key(ref);
|
|
137
|
+
function lookup(ref: Ref, affected: readonly Ref[], localArtifact = false): Revision | null {
|
|
138
|
+
// A scope revision's ID is its scopeId by the validated wire contract. This contradiction
|
|
139
|
+
// is already known from the reference; absent bytes must not downgrade it to a mere gap.
|
|
140
|
+
if (ref.kind === "scope" && ref.id !== snapshot.scope.id) reject("SCOPE_INVALID", ref, affected);
|
|
141
|
+
const sources = quarantineSources.get(key(ref));
|
|
142
|
+
if (sources) {
|
|
143
|
+
for (const source of sources) conflict(source.eventId, affected);
|
|
144
|
+
if (!localArtifact) missing = true;
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
const r = records.get(key(ref));
|
|
148
|
+
if (!r) {
|
|
149
|
+
problem("REFERENCE_MISSING", reference(ref), affected);
|
|
150
|
+
if (!localArtifact) missing = true;
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
if (r.scopeId !== snapshot.scope.id) reject("REVISION_INVALID", ref, affected);
|
|
154
|
+
return r;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function walk(root: Ref, affected: readonly Ref[], localArtifact = false) {
|
|
158
|
+
// Iterative traversal: the wire depth bound does not impose a limit on revision history length.
|
|
159
|
+
type Frame = { ref: Ref; current: boolean; exit?: boolean; local?: boolean };
|
|
160
|
+
const stack: Frame[] = [{ ref: root, current: true, local: localArtifact }];
|
|
161
|
+
const done = new Set<string>(), active = new Set<string>();
|
|
162
|
+
while (stack.length) {
|
|
163
|
+
const frame = stack.pop()!, id = `${frame.current}:${key(frame.ref)}`;
|
|
164
|
+
if (frame.exit) { active.delete(id); done.add(id); continue; }
|
|
165
|
+
if (active.has(id)) { reject("CYCLE", frame.ref, affected); continue; }
|
|
166
|
+
if (done.has(id)) continue;
|
|
167
|
+
if (frame.current && selected.has(entity(frame.ref)) && !isSelected(frame.ref)) reject("REVISION_INVALID", frame.ref, affected);
|
|
168
|
+
if (frame.current && frame.ref.kind === "scope" && key(frame.ref) !== key(snapshot.scope)) reject("SCOPE_INVALID", frame.ref, affected);
|
|
169
|
+
const r = lookup(frame.ref, affected, frame.local);
|
|
170
|
+
if (!r) continue;
|
|
171
|
+
active.add(id); stack.push({ ...frame, exit: true });
|
|
172
|
+
if (r.predecessor) {
|
|
173
|
+
const prior = lookup(r.predecessor, affected);
|
|
174
|
+
if (prior && (prior.kind !== r.kind || prior.id !== r.id || prior.scopeId !== r.scopeId || prior.revision + 1 !== r.revision)) {
|
|
175
|
+
reject("REVISION_INVALID", frame.ref, affected);
|
|
176
|
+
}
|
|
177
|
+
stack.push({ ref: r.predecessor, current: false });
|
|
178
|
+
}
|
|
179
|
+
if (r.parent) {
|
|
180
|
+
if (entity(r.parent) === entity(frame.ref)) reject("CYCLE", frame.ref, affected);
|
|
181
|
+
stack.push({ ref: r.parent, current: frame.current });
|
|
182
|
+
}
|
|
183
|
+
if (r.policy) {
|
|
184
|
+
if (frame.current && !isSelected(r.policy)) reject("REVISION_INVALID", r.policy, affected);
|
|
185
|
+
stack.push({ ref: r.policy, current: frame.current });
|
|
186
|
+
}
|
|
187
|
+
for (const dep of r.dependencies) {
|
|
188
|
+
if (entity(dep) === entity(frame.ref)) reject("CYCLE", frame.ref, affected);
|
|
189
|
+
if (frame.current && !isSelected(dep)) reject("DEPENDENCY_INVALID", dep, affected);
|
|
190
|
+
stack.push({ ref: dep, current: frame.current });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// Structural roots must be checked even when no obligation names them. Then walk each obligation
|
|
195
|
+
// independently to accumulate exact affected-obligation reachability, including shared ancestors.
|
|
196
|
+
walk(snapshot.scope, allAffected);
|
|
197
|
+
for (const ref of snapshot.revisions) {
|
|
198
|
+
const affected = ref.kind === "obligation" ? [ref] : snapshot.bindings.filter(binding =>
|
|
199
|
+
[binding.intent, binding.policy, binding.artifact].some(value => value && key(value) === key(ref)),
|
|
200
|
+
).map(binding => binding.obligation).filter(isSelected);
|
|
201
|
+
walk(ref, affected, ref.kind === "artifact");
|
|
202
|
+
}
|
|
203
|
+
function ancestor(obligation: Ref, intent: Ref): boolean | null {
|
|
204
|
+
const seen = new Set<string>();
|
|
205
|
+
let cursor: Ref | null = obligation;
|
|
206
|
+
while (cursor) {
|
|
207
|
+
const id = key(cursor);
|
|
208
|
+
if (seen.has(id) || quarantineSources.has(id)) return null;
|
|
209
|
+
seen.add(id);
|
|
210
|
+
const r = records.get(id);
|
|
211
|
+
if (!r) return null;
|
|
212
|
+
cursor = r.parent;
|
|
213
|
+
if (cursor && key(cursor) === key(intent)) return true;
|
|
214
|
+
}
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
for (const ref of obligationRefs) {
|
|
218
|
+
if (snapshot.bindings.filter(binding => key(binding.obligation) === key(ref)).length !== 1) reject("SCOPE_INVALID", ref, [ref]);
|
|
219
|
+
}
|
|
220
|
+
for (const binding of snapshot.bindings) {
|
|
221
|
+
const affected = isSelected(binding.obligation) ? [binding.obligation] : [];
|
|
222
|
+
if (!isSelected(binding.obligation)) reject("SCOPE_INVALID", binding.obligation);
|
|
223
|
+
if (!isSelected(binding.intent)) reject("SCOPE_INVALID", binding.intent, affected);
|
|
224
|
+
if (!isSelected(binding.policy)) reject("REVISION_INVALID", binding.policy, affected);
|
|
225
|
+
const r = lookup(binding.obligation, affected);
|
|
226
|
+
if (r && key(r.policy) !== key(binding.policy)) reject("REVISION_INVALID", binding.policy, affected);
|
|
227
|
+
if (r && ancestor(binding.obligation, binding.intent) === false) reject("SCOPE_INVALID", binding.intent, affected);
|
|
228
|
+
if (binding.artifact && !isSelected(binding.artifact)) reject("ARTIFACT_UNSELECTED", binding.artifact, affected);
|
|
229
|
+
}
|
|
230
|
+
if (invalid) return empty("invalid");
|
|
231
|
+
if (missing) return empty("unresolved");
|
|
232
|
+
const diagnostics = reports();
|
|
233
|
+
return freezeWork({
|
|
234
|
+
scopeState: "valid", selectedSnapshot: selection.snapshot, snapshot,
|
|
235
|
+
scope: records.get(key(snapshot.scope))!,
|
|
236
|
+
revisions: unique(snapshot.revisions.flatMap(ref => {
|
|
237
|
+
const r = quarantineSources.has(key(ref)) ? undefined : records.get(key(ref));
|
|
238
|
+
return r ? [r] : [];
|
|
239
|
+
})),
|
|
240
|
+
obligations: snapshot.bindings.map(binding => ({
|
|
241
|
+
binding, revision: records.get(key(binding.obligation))!,
|
|
242
|
+
artifact: binding.artifact && !quarantineSources.has(key(binding.artifact)) ? records.get(key(binding.artifact)) ?? null : null,
|
|
243
|
+
problems: diagnostics.problems.filter(p => p.affectedObligations.some(ref => key(ref) === key(binding.obligation))),
|
|
244
|
+
})), ...diagnostics,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/** Opt-in work-v4 wire identities; none of these declarations grants authority. */
|
|
2
|
+
export type WorkFrozen<T> = T extends ReadonlyArray<infer E>
|
|
3
|
+
? ReadonlyArray<WorkFrozen<E>>
|
|
4
|
+
: T extends object ? { readonly [K in keyof T]: WorkFrozen<T[K]> } : T;
|
|
5
|
+
export type WorkInputCode =
|
|
6
|
+
| "WORK_JSON_INVALID" | "WORK_DUPLICATE_MEMBER" | "WORK_LIMIT_EXCEEDED"
|
|
7
|
+
| "WORK_VERSION_UNSUPPORTED" | "WORK_SCHEMA_INVALID"
|
|
8
|
+
| "WORK_DIGEST_MISMATCH" | "WORK_CONTEXT_INVALID";
|
|
9
|
+
|
|
10
|
+
export class WorkInputError extends TypeError {
|
|
11
|
+
readonly code: WorkInputCode;
|
|
12
|
+
constructor(code: WorkInputCode) {
|
|
13
|
+
super(code);
|
|
14
|
+
this.name = "WorkInputError";
|
|
15
|
+
this.code = code;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type WorkWriteCode = "WORK_DESTINATION_INVALID" | "WORK_DESTINATION_ALIAS" | "WORK_LEDGER_WRITE_FAILED";
|
|
20
|
+
export class WorkLedgerWriteError extends Error {
|
|
21
|
+
readonly code: WorkWriteCode;
|
|
22
|
+
constructor(code: WorkWriteCode) { super(code); this.name = "WorkLedgerWriteError"; this.code = code; }
|
|
23
|
+
}
|
|
24
|
+
export type WorkInspectionCode = "WORK_INSPECTION_PATH_INVALID" | "WORK_INSPECTION_NOT_REGULAR" | "WORK_INSPECTION_READ_FAILED";
|
|
25
|
+
export interface WorkInspectionDiagnostic { readonly line: null; readonly code: WorkInputCode | WorkInspectionCode }
|
|
26
|
+
export interface WorkLedgerInspection {
|
|
27
|
+
readonly version: 4;
|
|
28
|
+
readonly status: "read" | "missing" | "error";
|
|
29
|
+
readonly exists: boolean | null;
|
|
30
|
+
readonly ingestion: WorkIngestion | null;
|
|
31
|
+
readonly projection: WorkProjection | null;
|
|
32
|
+
readonly errors: ReadonlyArray<WorkInspectionDiagnostic>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type RevisionKind = "scope" | "goal" | "node" | "obligation" | "artifact" | "policy";
|
|
36
|
+
export type WorkEventKind = "work_revision" | "work_snapshot" | "work_occurrence" | "work_acceptance";
|
|
37
|
+
export interface RevisionRef { kind: RevisionKind; id: string; revision: number; digest: string }
|
|
38
|
+
export interface EventRef { eventId: string; digest: string }
|
|
39
|
+
export interface Identity { id: string; digest: string }
|
|
40
|
+
export interface WorkRevision {
|
|
41
|
+
kind: RevisionKind;
|
|
42
|
+
id: string;
|
|
43
|
+
revision: number;
|
|
44
|
+
scopeId: string;
|
|
45
|
+
predecessor: RevisionRef | null;
|
|
46
|
+
contentDigest: string;
|
|
47
|
+
parent: RevisionRef | null;
|
|
48
|
+
dependencies: RevisionRef[];
|
|
49
|
+
ownerId: string;
|
|
50
|
+
permittedEffects: string[];
|
|
51
|
+
policy: RevisionRef | null;
|
|
52
|
+
digest: string;
|
|
53
|
+
}
|
|
54
|
+
export interface ObligationBinding {
|
|
55
|
+
intent: RevisionRef;
|
|
56
|
+
obligation: RevisionRef;
|
|
57
|
+
artifact: RevisionRef | null;
|
|
58
|
+
policy: RevisionRef;
|
|
59
|
+
}
|
|
60
|
+
export interface WorkSnapshot {
|
|
61
|
+
snapshotId: string;
|
|
62
|
+
scope: RevisionRef;
|
|
63
|
+
revisions: RevisionRef[];
|
|
64
|
+
bindings: ObligationBinding[];
|
|
65
|
+
digest: string;
|
|
66
|
+
}
|
|
67
|
+
export interface WorkOccurrencePayload {
|
|
68
|
+
scope: RevisionRef;
|
|
69
|
+
obligation: RevisionRef;
|
|
70
|
+
executionId: string;
|
|
71
|
+
parentExecutionId: string | null;
|
|
72
|
+
childId: string | null;
|
|
73
|
+
variantId: string | null;
|
|
74
|
+
artifact: RevisionRef | null;
|
|
75
|
+
provenance: "declared" | "observed";
|
|
76
|
+
state: "unknown" | "starting" | "running" | "completed" | "failed";
|
|
77
|
+
labels: {
|
|
78
|
+
sessionId: string | null;
|
|
79
|
+
branchLeafId: string | null;
|
|
80
|
+
toolCallId: string | null;
|
|
81
|
+
taskId: string | null;
|
|
82
|
+
workspaceId: string | null;
|
|
83
|
+
definitionDigest: string | null;
|
|
84
|
+
configurationDigest: string | null;
|
|
85
|
+
modelId: string | null;
|
|
86
|
+
effortId: string | null;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export interface EvidenceRef { id: string; digest: string; event: EventRef | null }
|
|
90
|
+
export interface AcceptanceBinding {
|
|
91
|
+
snapshot: Identity;
|
|
92
|
+
scope: RevisionRef;
|
|
93
|
+
intent: RevisionRef;
|
|
94
|
+
obligation: RevisionRef;
|
|
95
|
+
artifact: RevisionRef;
|
|
96
|
+
artifactDigest: string;
|
|
97
|
+
policy: RevisionRef;
|
|
98
|
+
evidence: EvidenceRef[];
|
|
99
|
+
}
|
|
100
|
+
export interface WorkAcceptancePayload { authorityId: string; binding: AcceptanceBinding }
|
|
101
|
+
interface WorkEvent<K extends WorkEventKind, P> {
|
|
102
|
+
ledgerVersion: 4;
|
|
103
|
+
event: K;
|
|
104
|
+
eventId: string;
|
|
105
|
+
ts: string;
|
|
106
|
+
payload: P;
|
|
107
|
+
digest: string;
|
|
108
|
+
}
|
|
109
|
+
export type WorkRevisionEvent = WorkEvent<"work_revision", { revision: WorkRevision }>;
|
|
110
|
+
export type WorkSnapshotEvent = WorkEvent<"work_snapshot", { snapshot: WorkSnapshot }>;
|
|
111
|
+
export type WorkOccurrenceEvent = WorkEvent<"work_occurrence", WorkOccurrencePayload>;
|
|
112
|
+
export type WorkAcceptanceEvent = WorkEvent<"work_acceptance", WorkAcceptancePayload>;
|
|
113
|
+
export type WorkLedgerEvent = WorkRevisionEvent | WorkSnapshotEvent | WorkOccurrenceEvent | WorkAcceptanceEvent;
|
|
114
|
+
export interface WorkDiagnostic { readonly line: number | null; readonly code: WorkInputCode }
|
|
115
|
+
export interface WorkIngestion {
|
|
116
|
+
readonly events: ReadonlyArray<WorkLedgerEvent>;
|
|
117
|
+
readonly errors: ReadonlyArray<WorkDiagnostic>;
|
|
118
|
+
readonly complete: boolean;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type WorkResolutionCode =
|
|
122
|
+
| "INPUT_INCOMPLETE" | "NO_SELECTION" | "REFERENCE_MISSING"
|
|
123
|
+
| "SCOPE_INVALID" | "REVISION_INVALID" | "CYCLE" | "DEPENDENCY_INVALID"
|
|
124
|
+
| "EVENT_CONFLICT" | "ARTIFACT_UNSELECTED" | "ARTIFACT_DIGEST_MISMATCH"
|
|
125
|
+
| "NO_CLAIM" | "SUPERSEDED_BINDING" | "AUTHORITY_MISSING"
|
|
126
|
+
| "RECEIPT_MISSING" | "RECEIPT_MISMATCH" | "RECEIPT_CONFLICT" | "DECISION_CONFLICT"
|
|
127
|
+
| "TRUSTED_REJECTION" | "AVAILABILITY_MISSING" | "BYTES_UNAVAILABLE"
|
|
128
|
+
| "AVAILABILITY_CONFLICT" | "OCCURRENCE_CONFLICT"
|
|
129
|
+
| "PARENT_MISSING" | "PARENT_CYCLE" | "OBSERVATION_CONFLICT" | "BRANCH_UNKNOWN";
|
|
130
|
+
export type WorkReference =
|
|
131
|
+
| { readonly type: "revision"; readonly ref: RevisionRef }
|
|
132
|
+
| { readonly type: "event"; readonly ref: EventRef }
|
|
133
|
+
| { readonly type: "snapshot" | "authority" | "artifact-bytes" | "evidence-bytes"; readonly ref: Identity }
|
|
134
|
+
| { readonly type: "execution"; readonly id: string };
|
|
135
|
+
export interface WorkProblem {
|
|
136
|
+
readonly code: WorkResolutionCode;
|
|
137
|
+
readonly reference: WorkReference | null;
|
|
138
|
+
readonly affectedObligations: ReadonlyArray<RevisionRef>;
|
|
139
|
+
}
|
|
140
|
+
export interface WorkConflict {
|
|
141
|
+
readonly kind: "event" | "receipt";
|
|
142
|
+
readonly id: string;
|
|
143
|
+
readonly digests: ReadonlyArray<string>;
|
|
144
|
+
readonly affectedObligations: ReadonlyArray<RevisionRef>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Explicit trusted-host declarations, never constructed from wire evidence by production code. */
|
|
148
|
+
export interface WorkProjectionContext {
|
|
149
|
+
selectedSnapshot: { snapshot: Identity; event: EventRef } | null;
|
|
150
|
+
authority: {
|
|
151
|
+
snapshot: Identity;
|
|
152
|
+
decisions: Array<{
|
|
153
|
+
receiptId: string;
|
|
154
|
+
authorityId: string;
|
|
155
|
+
claim: EventRef;
|
|
156
|
+
binding: AcceptanceBinding;
|
|
157
|
+
decision: "accept" | "reject";
|
|
158
|
+
}>;
|
|
159
|
+
availability: Array<{ kind: "artifact" | "evidence"; id: string; digest: string; available: boolean }>;
|
|
160
|
+
} | null;
|
|
161
|
+
}
|
|
162
|
+
export type WorkAvailability = "available" | "unavailable" | "unknown" | "conflicted";
|
|
163
|
+
export interface WorkCoverageItem {
|
|
164
|
+
readonly kind: "artifact" | "evidence";
|
|
165
|
+
readonly identity: Identity;
|
|
166
|
+
readonly state: WorkAvailability;
|
|
167
|
+
}
|
|
168
|
+
export interface WorkCoverage {
|
|
169
|
+
readonly state: WorkAvailability | "unselected";
|
|
170
|
+
readonly items: ReadonlyArray<WorkCoverageItem>;
|
|
171
|
+
}
|
|
172
|
+
export type WorkAcceptanceState = "accepted-under-supplied-authority" | "unaccepted" | "unresolved";
|
|
173
|
+
export interface WorkClaimResult {
|
|
174
|
+
readonly claim: EventRef;
|
|
175
|
+
readonly obligation: RevisionRef;
|
|
176
|
+
readonly applicability: WorkAcceptanceState | "superseded";
|
|
177
|
+
readonly matchedReceiptIds: ReadonlyArray<string>;
|
|
178
|
+
readonly problems: ReadonlyArray<WorkProblem>;
|
|
179
|
+
}
|
|
180
|
+
export interface WorkObligationResult {
|
|
181
|
+
readonly binding: ObligationBinding;
|
|
182
|
+
readonly acceptance: WorkAcceptanceState;
|
|
183
|
+
readonly artifactCoverage: WorkCoverage;
|
|
184
|
+
readonly evidenceCoverage: WorkCoverage;
|
|
185
|
+
readonly claims: ReadonlyArray<EventRef>;
|
|
186
|
+
readonly problems: ReadonlyArray<WorkProblem>;
|
|
187
|
+
}
|
|
188
|
+
export interface WorkOccurrenceView { readonly event: EventRef; readonly payload: WorkOccurrencePayload }
|
|
189
|
+
export interface WorkAttemptBinding {
|
|
190
|
+
readonly scope: RevisionRef;
|
|
191
|
+
readonly obligation: RevisionRef;
|
|
192
|
+
readonly variantIds: ReadonlyArray<string>;
|
|
193
|
+
readonly artifacts: ReadonlyArray<RevisionRef>;
|
|
194
|
+
}
|
|
195
|
+
export interface WorkAttempt {
|
|
196
|
+
readonly executionId: string;
|
|
197
|
+
readonly bindings: ReadonlyArray<WorkAttemptBinding>;
|
|
198
|
+
readonly parentExecutionId: string | null;
|
|
199
|
+
readonly childId: string | null;
|
|
200
|
+
readonly declaredLabels: ReadonlyArray<WorkOccurrencePayload["labels"]>;
|
|
201
|
+
readonly observedLabels: ReadonlyArray<WorkOccurrencePayload["labels"]>;
|
|
202
|
+
readonly effectiveLabels: WorkOccurrencePayload["labels"];
|
|
203
|
+
readonly state: WorkOccurrencePayload["state"] | "conflicted";
|
|
204
|
+
readonly resolution: "resolved" | "unresolved";
|
|
205
|
+
readonly problems: ReadonlyArray<WorkProblem>;
|
|
206
|
+
}
|
|
207
|
+
export interface WorkRuntimeCounts {
|
|
208
|
+
readonly attempts: number;
|
|
209
|
+
readonly variants: number;
|
|
210
|
+
readonly observedCompletedAttempts: number;
|
|
211
|
+
}
|
|
212
|
+
export interface WorkRuntime {
|
|
213
|
+
readonly counts: WorkRuntimeCounts;
|
|
214
|
+
readonly occurrences: ReadonlyArray<WorkOccurrenceView>;
|
|
215
|
+
readonly attempts: ReadonlyArray<WorkAttempt>;
|
|
216
|
+
}
|
|
217
|
+
export interface WorkProjection {
|
|
218
|
+
readonly selectedSnapshot: Identity | null;
|
|
219
|
+
readonly authoritySnapshot: Identity | null;
|
|
220
|
+
readonly scopeState: "unselected" | "valid" | "unresolved" | "invalid";
|
|
221
|
+
readonly progress: Readonly<{ accepted: number; total: number }> | null;
|
|
222
|
+
readonly obligations: ReadonlyArray<WorkObligationResult>;
|
|
223
|
+
readonly claims: ReadonlyArray<WorkClaimResult>;
|
|
224
|
+
readonly supersededClaims: number;
|
|
225
|
+
readonly conflicts: ReadonlyArray<WorkConflict>;
|
|
226
|
+
readonly problems: ReadonlyArray<WorkProblem>;
|
|
227
|
+
readonly errors: ReadonlyArray<WorkDiagnostic>;
|
|
228
|
+
readonly runtime: WorkRuntime | null;
|
|
229
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { isExecutionId } from "./execution-id.ts";
|
|
2
|
+
import { canonicalWorkJson, workDigest, type WorkJson } from "./work-ledger-json.ts";
|
|
3
|
+
import { WorkInputError, type WorkLedgerEvent } from "./work-ledger-types.ts";
|
|
4
|
+
|
|
5
|
+
type ObjectValue = { [key: string]: WorkJson };
|
|
6
|
+
const kinds = ["scope", "goal", "node", "obligation", "artifact", "policy"];
|
|
7
|
+
const fail = (): never => { throw new WorkInputError("WORK_SCHEMA_INVALID"); };
|
|
8
|
+
function require(value: boolean): asserts value { if (!value) fail(); }
|
|
9
|
+
export function workObject(value: WorkJson): ObjectValue {
|
|
10
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return fail();
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
function closed(value: WorkJson, keys: string[]): ObjectValue {
|
|
14
|
+
const object = workObject(value);
|
|
15
|
+
require(Object.keys(object).length === keys.length && keys.every(key => Object.hasOwn(object, key)));
|
|
16
|
+
return object;
|
|
17
|
+
}
|
|
18
|
+
function id(value: WorkJson): void { require(typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9._:/@+\-]{0,127}$/.test(value)); }
|
|
19
|
+
function digest(value: WorkJson): void { require(typeof value === "string" && /^[0-9a-f]{64}$/.test(value)); }
|
|
20
|
+
function integer(value: WorkJson): void { require(typeof value === "number" && Number.isSafeInteger(value) && value >= 1); }
|
|
21
|
+
function member(value: WorkJson, values: string[]): void { require(typeof value === "string" && values.includes(value)); }
|
|
22
|
+
function nullable(value: WorkJson, validate: (value: WorkJson) => void): void { if (value !== null) validate(value); }
|
|
23
|
+
function reference(value: WorkJson, allowed = kinds): void {
|
|
24
|
+
const ref = closed(value, ["kind", "id", "revision", "digest"]);
|
|
25
|
+
member(ref.kind, allowed); id(ref.id); integer(ref.revision); digest(ref.digest);
|
|
26
|
+
}
|
|
27
|
+
function identity(value: WorkJson): void {
|
|
28
|
+
const ref = closed(value, ["id", "digest"]); id(ref.id); digest(ref.digest);
|
|
29
|
+
}
|
|
30
|
+
function eventRef(value: WorkJson): void {
|
|
31
|
+
const ref = closed(value, ["eventId", "digest"]); id(ref.eventId); digest(ref.digest);
|
|
32
|
+
}
|
|
33
|
+
/** Internal validation for the exact out-of-band selectedSnapshot shape; no authority is constructed. */
|
|
34
|
+
export function validateWorkSelection(value: WorkJson): void {
|
|
35
|
+
if (value === null) return;
|
|
36
|
+
const selected = closed(value, ["snapshot", "event"]);
|
|
37
|
+
identity(selected.snapshot); eventRef(selected.event);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function timestamp(value: WorkJson): void {
|
|
41
|
+
require(typeof value === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(value));
|
|
42
|
+
require(!value.startsWith("0000") && Number.isFinite(Date.parse(value)) && new Date(value).toISOString() === value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Builder sorts detached copies; reader refuses noncanonical sets instead of repairing them. */
|
|
46
|
+
function set(value: WorkJson, validate: (item: WorkJson) => void, building: boolean,
|
|
47
|
+
key: (item: WorkJson) => string = canonicalWorkJson): void {
|
|
48
|
+
require(Array.isArray(value));
|
|
49
|
+
value.forEach(validate);
|
|
50
|
+
const keyed = value.map(item => ({ item, key: key(item) }));
|
|
51
|
+
const sorted = [...keyed].sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
52
|
+
for (let i = 1; i < sorted.length; i++) require(sorted[i - 1].key !== sorted[i].key);
|
|
53
|
+
if (building) value.splice(0, value.length, ...sorted.map(entry => entry.item));
|
|
54
|
+
else require(keyed.every((entry, i) => entry.key === sorted[i].key));
|
|
55
|
+
}
|
|
56
|
+
function ownKeys(keys: string[], building: boolean): string[] { return building ? keys : [...keys, "digest"]; }
|
|
57
|
+
|
|
58
|
+
function revision(value: WorkJson, building: boolean): void {
|
|
59
|
+
const r = closed(value, ownKeys(["kind", "id", "revision", "scopeId", "predecessor", "contentDigest",
|
|
60
|
+
"parent", "dependencies", "ownerId", "permittedEffects", "policy"], building));
|
|
61
|
+
member(r.kind, kinds); id(r.id); integer(r.revision); id(r.scopeId); id(r.ownerId); digest(r.contentDigest);
|
|
62
|
+
nullable(r.predecessor, v => reference(v));
|
|
63
|
+
nullable(r.parent, v => reference(v));
|
|
64
|
+
nullable(r.policy, v => reference(v, ["policy"]));
|
|
65
|
+
set(r.dependencies, v => reference(v, ["obligation"]), building);
|
|
66
|
+
set(r.permittedEffects, id, building, v => v as string);
|
|
67
|
+
if (r.revision === 1) require(r.predecessor === null);
|
|
68
|
+
else {
|
|
69
|
+
require(r.predecessor !== null);
|
|
70
|
+
const predecessor = workObject(r.predecessor);
|
|
71
|
+
require(predecessor.kind === r.kind && predecessor.id === r.id &&
|
|
72
|
+
(predecessor.revision as number) === (r.revision as number) - 1);
|
|
73
|
+
}
|
|
74
|
+
const noDependencies = (r.dependencies as WorkJson[]).length === 0;
|
|
75
|
+
if (r.kind === "scope") require(r.scopeId === r.id && r.parent === null && noDependencies && r.policy === null);
|
|
76
|
+
if (r.kind === "artifact" || r.kind === "policy") {
|
|
77
|
+
require(r.parent === null && noDependencies && r.policy === null && (r.permittedEffects as WorkJson[]).length === 0);
|
|
78
|
+
}
|
|
79
|
+
if (r.kind === "goal" || r.kind === "node" || r.kind === "obligation") {
|
|
80
|
+
reference(r.parent, r.kind === "goal" ? ["scope", "goal"] : ["goal", "node"]);
|
|
81
|
+
if (r.kind === "obligation") reference(r.policy, ["policy"]);
|
|
82
|
+
else require(noDependencies && r.policy === null);
|
|
83
|
+
}
|
|
84
|
+
if (!building) digest(r.digest);
|
|
85
|
+
}
|
|
86
|
+
function obligationBinding(value: WorkJson): void {
|
|
87
|
+
const b = closed(value, ["intent", "obligation", "artifact", "policy"]);
|
|
88
|
+
reference(b.intent, ["goal", "node"]); reference(b.obligation, ["obligation"]);
|
|
89
|
+
nullable(b.artifact, v => reference(v, ["artifact"])); reference(b.policy, ["policy"]);
|
|
90
|
+
}
|
|
91
|
+
function snapshot(value: WorkJson, building: boolean): void {
|
|
92
|
+
const s = closed(value, ownKeys(["snapshotId", "scope", "revisions", "bindings"], building));
|
|
93
|
+
id(s.snapshotId); reference(s.scope, ["scope"]);
|
|
94
|
+
set(s.revisions, v => reference(v, kinds.filter(k => k !== "scope")), building);
|
|
95
|
+
set(s.bindings, obligationBinding, building, v => canonicalWorkJson(workObject(v).obligation));
|
|
96
|
+
if (!building) digest(s.digest);
|
|
97
|
+
// Inventory/ancestor/predecessor resolution requires the complete event snapshot, not a guessed head.
|
|
98
|
+
}
|
|
99
|
+
function occurrence(value: WorkJson): void {
|
|
100
|
+
const p = closed(value, ["scope", "obligation", "executionId", "parentExecutionId", "childId", "variantId",
|
|
101
|
+
"artifact", "provenance", "state", "labels"]);
|
|
102
|
+
reference(p.scope, ["scope"]); reference(p.obligation, ["obligation"]);
|
|
103
|
+
require(isExecutionId(p.executionId));
|
|
104
|
+
require(p.parentExecutionId === null || isExecutionId(p.parentExecutionId));
|
|
105
|
+
require(p.executionId !== p.parentExecutionId);
|
|
106
|
+
nullable(p.childId, id); nullable(p.variantId, id); nullable(p.artifact, v => reference(v, ["artifact"]));
|
|
107
|
+
member(p.provenance, ["declared", "observed"]);
|
|
108
|
+
member(p.state, ["unknown", "starting", "running", "completed", "failed"]);
|
|
109
|
+
const labels = closed(p.labels, ["sessionId", "branchLeafId", "toolCallId", "taskId", "workspaceId",
|
|
110
|
+
"definitionDigest", "configurationDigest", "modelId", "effortId"]);
|
|
111
|
+
for (const [key, value] of Object.entries(labels)) nullable(value, key.endsWith("Digest") ? digest : id);
|
|
112
|
+
}
|
|
113
|
+
function evidence(value: WorkJson): void {
|
|
114
|
+
const e = closed(value, ["id", "digest", "event"]);
|
|
115
|
+
id(e.id); digest(e.digest); nullable(e.event, eventRef);
|
|
116
|
+
}
|
|
117
|
+
function acceptance(value: WorkJson, building: boolean): void {
|
|
118
|
+
const p = closed(value, ["authorityId", "binding"]); id(p.authorityId);
|
|
119
|
+
acceptanceBinding(p.binding, building);
|
|
120
|
+
}
|
|
121
|
+
function acceptanceBinding(value: WorkJson, building: boolean): void {
|
|
122
|
+
const b = closed(value, ["snapshot", "scope", "intent", "obligation", "artifact", "artifactDigest", "policy", "evidence"]);
|
|
123
|
+
identity(b.snapshot); reference(b.scope, ["scope"]); reference(b.intent, ["goal", "node"]);
|
|
124
|
+
reference(b.obligation, ["obligation"]); reference(b.artifact, ["artifact"]);
|
|
125
|
+
digest(b.artifactDigest); reference(b.policy, ["policy"]);
|
|
126
|
+
set(b.evidence, evidence, building); require((b.evidence as WorkJson[]).length > 0);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Validate a detached host context. Receipt/availability order and duplicate deliveries have no priority. */
|
|
130
|
+
export function validateWorkContext(value: WorkJson): void {
|
|
131
|
+
const c = closed(value, ["selectedSnapshot", "authority"]);
|
|
132
|
+
validateWorkSelection(c.selectedSnapshot);
|
|
133
|
+
if (c.authority === null) return;
|
|
134
|
+
const a = closed(c.authority, ["snapshot", "decisions", "availability"]);
|
|
135
|
+
identity(a.snapshot);
|
|
136
|
+
require(Array.isArray(a.decisions)); require(Array.isArray(a.availability));
|
|
137
|
+
for (const value of a.decisions) {
|
|
138
|
+
const d = closed(value, ["receiptId", "authorityId", "claim", "binding", "decision"]);
|
|
139
|
+
id(d.receiptId); id(d.authorityId); eventRef(d.claim); acceptanceBinding(d.binding, false);
|
|
140
|
+
member(d.decision, ["accept", "reject"]);
|
|
141
|
+
}
|
|
142
|
+
for (const value of a.availability) {
|
|
143
|
+
const row = closed(value, ["kind", "id", "digest", "available"]);
|
|
144
|
+
member(row.kind, ["artifact", "evidence"]); id(row.id); digest(row.digest);
|
|
145
|
+
require(typeof row.available === "boolean");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function ownDigest(object: ObjectValue, building: boolean): void {
|
|
150
|
+
const { digest: supplied, ...body } = object;
|
|
151
|
+
const computed = workDigest(body);
|
|
152
|
+
if (building) object.digest = computed;
|
|
153
|
+
else if (supplied !== computed) throw new WorkInputError("WORK_DIGEST_MISMATCH");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** All closed shapes/domain checks finish before any own digest is computed or verified. */
|
|
157
|
+
export function validateWorkEvent(value: WorkJson, building = false): WorkLedgerEvent {
|
|
158
|
+
const e = workObject(value);
|
|
159
|
+
if (e.ledgerVersion !== 4) throw new WorkInputError("WORK_VERSION_UNSUPPORTED");
|
|
160
|
+
closed(e, ownKeys(["ledgerVersion", "event", "eventId", "ts", "payload"], building));
|
|
161
|
+
member(e.event, ["work_revision", "work_snapshot", "work_occurrence", "work_acceptance"]);
|
|
162
|
+
id(e.eventId); timestamp(e.ts);
|
|
163
|
+
let nested: ObjectValue | undefined;
|
|
164
|
+
if (e.event === "work_revision") {
|
|
165
|
+
nested = workObject(closed(e.payload, ["revision"]).revision); revision(nested, building);
|
|
166
|
+
} else if (e.event === "work_snapshot") {
|
|
167
|
+
nested = workObject(closed(e.payload, ["snapshot"]).snapshot); snapshot(nested, building);
|
|
168
|
+
} else if (e.event === "work_occurrence") occurrence(e.payload);
|
|
169
|
+
else acceptance(e.payload, building);
|
|
170
|
+
if (!building) digest(e.digest);
|
|
171
|
+
if (nested) ownDigest(nested, building);
|
|
172
|
+
ownDigest(e, building);
|
|
173
|
+
return e as unknown as WorkLedgerEvent;
|
|
174
|
+
}
|