@dsh-enhanced/assistant-work-items 0.0.0-stage → 0.1.66

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.
Files changed (152) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +685 -2
  3. package/cordis.patch.yml +11 -0
  4. package/lib/acceptance-binding.d.ts +86 -0
  5. package/lib/acceptance-binding.d.ts.map +1 -0
  6. package/lib/acceptance-binding.js +250 -0
  7. package/lib/acceptance-binding.js.map +1 -0
  8. package/lib/admission.d.ts +95 -0
  9. package/lib/admission.d.ts.map +1 -0
  10. package/lib/admission.js +642 -0
  11. package/lib/admission.js.map +1 -0
  12. package/lib/authority.d.ts +92 -0
  13. package/lib/authority.d.ts.map +1 -0
  14. package/lib/authority.js +205 -0
  15. package/lib/authority.js.map +1 -0
  16. package/lib/config.d.ts +365 -0
  17. package/lib/config.d.ts.map +1 -0
  18. package/lib/config.js +672 -0
  19. package/lib/config.js.map +1 -0
  20. package/lib/continuation-contract.d.ts +126 -0
  21. package/lib/continuation-contract.d.ts.map +1 -0
  22. package/lib/continuation-contract.js +109 -0
  23. package/lib/continuation-contract.js.map +1 -0
  24. package/lib/continuation-record.d.ts +32 -0
  25. package/lib/continuation-record.d.ts.map +1 -0
  26. package/lib/continuation-record.js +57 -0
  27. package/lib/continuation-record.js.map +1 -0
  28. package/lib/continuation.d.ts +89 -0
  29. package/lib/continuation.d.ts.map +1 -0
  30. package/lib/continuation.js +607 -0
  31. package/lib/continuation.js.map +1 -0
  32. package/lib/diagnosis-contract.d.ts +95 -0
  33. package/lib/diagnosis-contract.d.ts.map +1 -0
  34. package/lib/diagnosis-contract.js +167 -0
  35. package/lib/diagnosis-contract.js.map +1 -0
  36. package/lib/diagnosis.d.ts +70 -0
  37. package/lib/diagnosis.d.ts.map +1 -0
  38. package/lib/diagnosis.js +466 -0
  39. package/lib/diagnosis.js.map +1 -0
  40. package/lib/dispatch.d.ts +100 -0
  41. package/lib/dispatch.d.ts.map +1 -0
  42. package/lib/dispatch.js +317 -0
  43. package/lib/dispatch.js.map +1 -0
  44. package/lib/engineering-context.d.ts +86 -0
  45. package/lib/engineering-context.d.ts.map +1 -0
  46. package/lib/engineering-context.js +263 -0
  47. package/lib/engineering-context.js.map +1 -0
  48. package/lib/evaluation.d.ts +125 -0
  49. package/lib/evaluation.d.ts.map +1 -0
  50. package/lib/evaluation.js +295 -0
  51. package/lib/evaluation.js.map +1 -0
  52. package/lib/event-follow.d.ts +93 -0
  53. package/lib/event-follow.d.ts.map +1 -0
  54. package/lib/event-follow.js +150 -0
  55. package/lib/event-follow.js.map +1 -0
  56. package/lib/feedback-targets.d.ts +91 -0
  57. package/lib/feedback-targets.d.ts.map +1 -0
  58. package/lib/feedback-targets.js +362 -0
  59. package/lib/feedback-targets.js.map +1 -0
  60. package/lib/index.d.ts +27 -0
  61. package/lib/index.d.ts.map +1 -0
  62. package/lib/index.js +25 -0
  63. package/lib/index.js.map +1 -0
  64. package/lib/intake-admission.d.ts +49 -0
  65. package/lib/intake-admission.d.ts.map +1 -0
  66. package/lib/intake-admission.js +100 -0
  67. package/lib/intake-admission.js.map +1 -0
  68. package/lib/intake-evaluation-runtime.d.ts +74 -0
  69. package/lib/intake-evaluation-runtime.d.ts.map +1 -0
  70. package/lib/intake-evaluation-runtime.js +343 -0
  71. package/lib/intake-evaluation-runtime.js.map +1 -0
  72. package/lib/intake-evaluation.d.ts +180 -0
  73. package/lib/intake-evaluation.d.ts.map +1 -0
  74. package/lib/intake-evaluation.js +440 -0
  75. package/lib/intake-evaluation.js.map +1 -0
  76. package/lib/intake.d.ts +118 -0
  77. package/lib/intake.d.ts.map +1 -0
  78. package/lib/intake.js +137 -0
  79. package/lib/intake.js.map +1 -0
  80. package/lib/ledger.d.ts +86 -0
  81. package/lib/ledger.d.ts.map +1 -0
  82. package/lib/ledger.js +145 -0
  83. package/lib/ledger.js.map +1 -0
  84. package/lib/mandate.d.ts +45 -0
  85. package/lib/mandate.d.ts.map +1 -0
  86. package/lib/mandate.js +168 -0
  87. package/lib/mandate.js.map +1 -0
  88. package/lib/outcome.d.ts +137 -0
  89. package/lib/outcome.d.ts.map +1 -0
  90. package/lib/outcome.js +303 -0
  91. package/lib/outcome.js.map +1 -0
  92. package/lib/owner-access.d.ts +35 -0
  93. package/lib/owner-access.d.ts.map +1 -0
  94. package/lib/owner-access.js +152 -0
  95. package/lib/owner-access.js.map +1 -0
  96. package/lib/owner-continuation.d.ts +27 -0
  97. package/lib/owner-continuation.d.ts.map +1 -0
  98. package/lib/owner-continuation.js +131 -0
  99. package/lib/owner-continuation.js.map +1 -0
  100. package/lib/owner-recall.d.ts +85 -0
  101. package/lib/owner-recall.d.ts.map +1 -0
  102. package/lib/owner-recall.js +134 -0
  103. package/lib/owner-recall.js.map +1 -0
  104. package/lib/owner-replies.d.ts +44 -0
  105. package/lib/owner-replies.d.ts.map +1 -0
  106. package/lib/owner-replies.js +232 -0
  107. package/lib/owner-replies.js.map +1 -0
  108. package/lib/peers.d.ts +622 -0
  109. package/lib/peers.d.ts.map +1 -0
  110. package/lib/peers.js +10 -0
  111. package/lib/peers.js.map +1 -0
  112. package/lib/policy-notice.d.ts +31 -0
  113. package/lib/policy-notice.d.ts.map +1 -0
  114. package/lib/policy-notice.js +137 -0
  115. package/lib/policy-notice.js.map +1 -0
  116. package/lib/reply-contract.d.ts +67 -0
  117. package/lib/reply-contract.d.ts.map +1 -0
  118. package/lib/reply-contract.js +58 -0
  119. package/lib/reply-contract.js.map +1 -0
  120. package/lib/service.d.ts +625 -0
  121. package/lib/service.d.ts.map +1 -0
  122. package/lib/service.js +2388 -0
  123. package/lib/service.js.map +1 -0
  124. package/lib/settlement.d.ts +74 -0
  125. package/lib/settlement.d.ts.map +1 -0
  126. package/lib/settlement.js +233 -0
  127. package/lib/settlement.js.map +1 -0
  128. package/lib/signals.d.ts +176 -0
  129. package/lib/signals.d.ts.map +1 -0
  130. package/lib/signals.js +497 -0
  131. package/lib/signals.js.map +1 -0
  132. package/lib/store.d.ts +789 -0
  133. package/lib/store.d.ts.map +1 -0
  134. package/lib/store.js +3266 -0
  135. package/lib/store.js.map +1 -0
  136. package/lib/tools.d.ts +7 -0
  137. package/lib/tools.d.ts.map +1 -0
  138. package/lib/tools.js +443 -0
  139. package/lib/tools.js.map +1 -0
  140. package/lib/types.d.ts +831 -0
  141. package/lib/types.d.ts.map +1 -0
  142. package/lib/types.js +29 -0
  143. package/lib/types.js.map +1 -0
  144. package/lib/version.d.ts +2 -0
  145. package/lib/version.d.ts.map +1 -0
  146. package/lib/version.js +2 -0
  147. package/lib/version.js.map +1 -0
  148. package/lib/workspace.d.ts +43 -0
  149. package/lib/workspace.d.ts.map +1 -0
  150. package/lib/workspace.js +137 -0
  151. package/lib/workspace.js.map +1 -0
  152. package/package.json +101 -3
@@ -0,0 +1,11 @@
1
+ - insert:
2
+ - id: dsh-enhanced-assistant-work-items
3
+ name: '@dsh-enhanced/assistant-work-items'
4
+ inject:
5
+ - assistantPolicy
6
+ config:
7
+ enabled: false
8
+ mode: observe
9
+ databasePath: !!js dshHomePath('assistant-work-items/state.sqlite')
10
+ workRoot: !!js dshHomePath('assistant-work-items/work')
11
+ intakeSourceId: work-items/v1
@@ -0,0 +1,86 @@
1
+ /**
2
+ * WP21 slice 4: independent acceptance binding for execute decisions.
3
+ *
4
+ * A decision may name an already-prepared TaskAcceptanceContract by handle.
5
+ * Admission re-reads that contract from `assistant-verifier` itself, checks it
6
+ * against the item's owner scope, and freezes a summary into the mandate. The
7
+ * item's completion is then read back only from the verifier's own receipt:
8
+ * neither the decision caller nor the worker can supply a contract body or a
9
+ * verdict, and a worker `ready` report stays a self-report.
10
+ */
11
+ import type { PeerOwnerRouteReceipt, PeerVerifierService } from './peers.ts';
12
+ import type { WorkItemAcceptanceAuthor, WorkItemAcceptanceBinding, WorkItemAcceptanceBlockReason, WorkItemAcceptanceGateExpectation, WorkItemAcceptanceHandle, WorkItemAcceptanceSettleResult, WorkItemAcceptanceVerdict, WorkItemDecisionRefusal, WorkItemRecord } from './types.ts';
13
+ export declare const ACCEPTANCE_BINDING_PROTOCOL = "work-items/acceptance-binding/v1";
14
+ /** The only identity whose receipt may settle a bound item. */
15
+ export declare const ACCEPTANCE_VERIFIER = "assistant-verifier";
16
+ /** The executing side; it may never author or verify its own acceptance. */
17
+ export declare const ACCEPTANCE_EXECUTOR = "assistant-work-items";
18
+ /** Same shape the acceptance wire contract uses for ids. */
19
+ export declare const ACCEPTANCE_CONTRACT_ID: RegExp;
20
+ /**
21
+ * The Host producer that froze a contract, derived from its task kind. The
22
+ * verifier accepts each kind only from its one registered producer, so the
23
+ * kind is a faithful author identity; an unknown kind has no author.
24
+ */
25
+ export declare function acceptanceAuthorFor(taskKind: string): WorkItemAcceptanceAuthor | undefined;
26
+ /** Author, verifier and executor must be three distinct identities. */
27
+ export declare function acceptanceIsIndependent(identities: Readonly<{
28
+ author: string;
29
+ verifier: string;
30
+ executor: string;
31
+ }>): boolean;
32
+ /**
33
+ * Resolve a decision's acceptance handle into a frozen binding.
34
+ *
35
+ * Pure apart from the read-only verifier call; every refusal is a value and
36
+ * happens before any durable write. The contract must be the exact one the
37
+ * handle names, for exactly this owner scope, unexpired, and not yet
38
+ * verified: a receipt that predates the work cannot complete it.
39
+ */
40
+ export declare function resolveAcceptanceBinding(verifier: PeerVerifierService | undefined, handle: WorkItemAcceptanceHandle, owner: PeerOwnerRouteReceipt, now: number,
41
+ /**
42
+ * Late binding of a worker run's own contract (WP21 slice 6): the contract
43
+ * must name exactly `taskRef`. Because that ref pins the one run of this
44
+ * generation, `pending`, `verifying` and `done` are also accepted. Nothing
45
+ * may predate `notBefore` (this generation's dispatch): not the start of any
46
+ * receipt, a `pending` retry's included, nor the issue of a `verifying` or
47
+ * `done` contract. `needs-attention` never binds.
48
+ */
49
+ late?: Readonly<{
50
+ taskRef: string;
51
+ notBefore: number;
52
+ }>): WorkItemAcceptanceBinding | WorkItemDecisionRefusal;
53
+ /**
54
+ * The acceptance task ref Automations freezes for the worker run of one
55
+ * occurrence: task id `task-<occurrenceId>`, ref `run-<taskId>`. Coupled to
56
+ * Automations' id derivation on purpose, so a contract for any other run of
57
+ * the same automation can never bind this item.
58
+ */
59
+ export declare function workerRunTaskRef(occurrenceId: string): string;
60
+ /** Structural check of a stored binding; anything else makes the mandate malformed. */
61
+ export declare function acceptanceBindingIsUsable(value: unknown): value is WorkItemAcceptanceBinding;
62
+ /** Durable verdict sink; the store keeps one verdict per item. */
63
+ export interface AcceptanceVerdictSink {
64
+ acceptanceVerdict(itemId: string): WorkItemAcceptanceVerdict | undefined;
65
+ recordAcceptanceVerdict(verdict: Omit<WorkItemAcceptanceVerdict, 'recordedAt'>, gate?: WorkItemAcceptanceGateExpectation): Readonly<{
66
+ verdict: WorkItemAcceptanceVerdict;
67
+ created: boolean;
68
+ blockedReason?: WorkItemAcceptanceBlockReason;
69
+ }>;
70
+ }
71
+ /**
72
+ * Read one bound item's verdict back from the independent verifier.
73
+ *
74
+ * Takes no verdict input. The verifier must still hold the exact frozen
75
+ * contract, and its receipt must validate against that contract; the
76
+ * objective status is the one `validateTaskVerificationReceipt` derives from
77
+ * criterion results. Anything short of a validated receipt writes nothing.
78
+ */
79
+ export declare function settleAcceptance(sink: AcceptanceVerdictSink, verifier: PeerVerifierService | undefined, item: WorkItemRecord, binding: WorkItemAcceptanceBinding,
80
+ /**
81
+ * `acceptance.verdictGate`: the executed generation a not-achieved or
82
+ * unknown verdict moves to `blocked`, in the verdict's own transaction.
83
+ * The caller handles `needs-attention` (no verdict is written for it).
84
+ */
85
+ gate?: WorkItemAcceptanceGateExpectation): WorkItemAcceptanceSettleResult;
86
+ //# sourceMappingURL=acceptance-binding.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"acceptance-binding.d.ts","sourceRoot":"","sources":["../src/acceptance-binding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAQH,OAAO,KAAK,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,YA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AiB,CAAC,MAAM,EAAE,MAAM,GAAG,yBAAyB,GAAG,SAAS,CAAA;IACxE,uBAAuB,CAAC,OAAO,EAAE,IAAI,CAAC,yBAAyB,EAAE,YAAY,CAAC,EAAE,IAAI,CAAC,EAAE,iCAAiC,GAAG,QAAQ,CAAC;QAAE,OAAO,EAAE,yBAAyB,CAAC;QAAC,OAAO,EAAE,OAAO,CAAC;QAAC,aAAa,CAAC,EAAE,6BAA6B,CAAA;KAAE,CAAC,CAAA;CAC7O;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,qBAAqB,EAC3B,QAAQ,EAAE,mBAAmB,GAAG,SAAS,EACzC,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,yBAAyB;AAClC;;;;GAIG;AACH,IAAI,CAAC,EAAE,iCAAiC,GACvC,8BAA8B,CA6DhC"}
@@ -0,0 +1,250 @@
1
+ /**
2
+ * WP21 slice 4: independent acceptance binding for execute decisions.
3
+ *
4
+ * A decision may name an already-prepared TaskAcceptanceContract by handle.
5
+ * Admission re-reads that contract from `assistant-verifier` itself, checks it
6
+ * against the item's owner scope, and freezes a summary into the mandate. The
7
+ * item's completion is then read back only from the verifier's own receipt:
8
+ * neither the decision caller nor the worker can supply a contract body or a
9
+ * verdict, and a worker `ready` report stays a self-report.
10
+ */
11
+ import { acceptanceDigest, validateTaskAcceptanceContract, validateTaskVerificationReceipt, } from '@dsh-enhanced/task-acceptance-contract';
12
+ import { WorkItemError } from "./types.js";
13
+ export const ACCEPTANCE_BINDING_PROTOCOL = 'work-items/acceptance-binding/v1';
14
+ /** The only identity whose receipt may settle a bound item. */
15
+ export const ACCEPTANCE_VERIFIER = 'assistant-verifier';
16
+ /** The executing side; it may never author or verify its own acceptance. */
17
+ export const ACCEPTANCE_EXECUTOR = 'assistant-work-items';
18
+ /** Same shape the acceptance wire contract uses for ids. */
19
+ export const ACCEPTANCE_CONTRACT_ID = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,255}$/u;
20
+ const DIGEST = /^[0-9a-f]{64}$/u;
21
+ const AUTHORS = ['assistant-automations', 'assistant-delivery', 'assistant-goals'];
22
+ const MAX_CRITERIA = 1_000;
23
+ /**
24
+ * The Host producer that froze a contract, derived from its task kind. The
25
+ * verifier accepts each kind only from its one registered producer, so the
26
+ * kind is a faithful author identity; an unknown kind has no author.
27
+ */
28
+ export function acceptanceAuthorFor(taskKind) {
29
+ switch (taskKind) {
30
+ case 'automation-run': return 'assistant-automations';
31
+ case 'foreground-turn': return 'assistant-delivery';
32
+ case 'goal-step':
33
+ case 'goal-outcome': return 'assistant-goals';
34
+ default: return undefined;
35
+ }
36
+ }
37
+ /** Author, verifier and executor must be three distinct identities. */
38
+ export function acceptanceIsIndependent(identities) {
39
+ const { author, verifier, executor } = identities;
40
+ return author !== verifier && author !== executor && verifier !== executor;
41
+ }
42
+ function inspect(verifier, contractId) {
43
+ if (verifier === undefined || typeof verifier.inspectAcceptedTask !== 'function')
44
+ return { kind: 'unavailable', detail: 'assistantVerifier is not connected' };
45
+ try {
46
+ return { kind: 'read', value: verifier.inspectAcceptedTask(contractId) };
47
+ }
48
+ catch (error) {
49
+ return { kind: 'unavailable', detail: error instanceof Error ? error.message : String(error) };
50
+ }
51
+ }
52
+ function validContract(value) {
53
+ try {
54
+ return validateTaskAcceptanceContract(value);
55
+ }
56
+ catch {
57
+ return undefined;
58
+ }
59
+ }
60
+ /**
61
+ * Resolve a decision's acceptance handle into a frozen binding.
62
+ *
63
+ * Pure apart from the read-only verifier call; every refusal is a value and
64
+ * happens before any durable write. The contract must be the exact one the
65
+ * handle names, for exactly this owner scope, unexpired, and not yet
66
+ * verified: a receipt that predates the work cannot complete it.
67
+ */
68
+ export function resolveAcceptanceBinding(verifier, handle, owner, now,
69
+ /**
70
+ * Late binding of a worker run's own contract (WP21 slice 6): the contract
71
+ * must name exactly `taskRef`. Because that ref pins the one run of this
72
+ * generation, `pending`, `verifying` and `done` are also accepted. Nothing
73
+ * may predate `notBefore` (this generation's dispatch): not the start of any
74
+ * receipt, a `pending` retry's included, nor the issue of a `verifying` or
75
+ * `done` contract. `needs-attention` never binds.
76
+ */
77
+ late) {
78
+ const read = inspect(verifier, handle.contractId);
79
+ if (read.kind === 'unavailable' || read.value === null || read.value === undefined)
80
+ return 'acceptance-unavailable';
81
+ const contract = validContract(read.value.contract);
82
+ if (contract === undefined || contract.id !== handle.contractId || contract.digest !== handle.contractDigest
83
+ || contract.scope.workspace !== owner.workspace || contract.scope.preset !== owner.agentPreset
84
+ || contract.owner.principalRecordId !== owner.principalRecordId || contract.owner.principalVersion !== owner.principalVersion
85
+ || (late !== undefined && (contract.task.kind !== 'automation-run' || contract.task.ref !== late.taskRef))) {
86
+ return 'acceptance-mismatch';
87
+ }
88
+ if (contract.expiresAt <= now)
89
+ return 'acceptance-expired';
90
+ const state = read.value.state;
91
+ if (late !== undefined && (state === 'pending' || state === 'verifying' || state === 'done')) {
92
+ // The run may have finished between two wakes. Its ref already pins this
93
+ // generation's run; additionally the contract, and any receipt, must not
94
+ // predate this generation's dispatch. A `pending` contract may carry the
95
+ // inconclusive receipt of a scheduled retry; it is checked the same way.
96
+ if (state !== 'pending' && contract.issuedAt < late.notBefore)
97
+ return 'acceptance-already-settled';
98
+ if (state === 'done' || read.value.receipt !== null) {
99
+ let receipt;
100
+ try {
101
+ receipt = validateTaskVerificationReceipt(contract, read.value.receipt);
102
+ }
103
+ catch {
104
+ return 'acceptance-already-settled';
105
+ }
106
+ if (receipt.startedAt < late.notBefore)
107
+ return 'acceptance-already-settled';
108
+ }
109
+ }
110
+ else {
111
+ if (state === 'done' || state === 'needs-attention' || read.value.receipt !== null)
112
+ return 'acceptance-already-settled';
113
+ // Only a contract nobody has started verifying may bind new work; a pending
114
+ // or verifying one could settle on evidence gathered before this item ran.
115
+ if (state !== 'awaiting-execution')
116
+ return 'acceptance-not-awaiting';
117
+ }
118
+ const author = acceptanceAuthorFor(contract.task.kind);
119
+ if (author === undefined || !acceptanceIsIndependent({ author, verifier: ACCEPTANCE_VERIFIER, executor: ACCEPTANCE_EXECUTOR })) {
120
+ return 'acceptance-not-independent';
121
+ }
122
+ return Object.freeze({
123
+ protocol: ACCEPTANCE_BINDING_PROTOCOL,
124
+ verifier: ACCEPTANCE_VERIFIER,
125
+ author,
126
+ contractId: contract.id,
127
+ contractDigest: contract.digest,
128
+ taskKind: contract.task.kind,
129
+ taskDigest: acceptanceDigest(contract.task),
130
+ objectiveDigest: acceptanceDigest(contract.objective),
131
+ profileDigest: contract.profile.digest,
132
+ criteria: contract.criteria.length,
133
+ expiresAt: contract.expiresAt,
134
+ });
135
+ }
136
+ /**
137
+ * The acceptance task ref Automations freezes for the worker run of one
138
+ * occurrence: task id `task-<occurrenceId>`, ref `run-<taskId>`. Coupled to
139
+ * Automations' id derivation on purpose, so a contract for any other run of
140
+ * the same automation can never bind this item.
141
+ */
142
+ export function workerRunTaskRef(occurrenceId) {
143
+ return `run-task-${occurrenceId}`;
144
+ }
145
+ /** Structural check of a stored binding; anything else makes the mandate malformed. */
146
+ export function acceptanceBindingIsUsable(value) {
147
+ if (value === null || typeof value !== 'object' || Array.isArray(value))
148
+ return false;
149
+ const binding = value;
150
+ const keys = ['protocol', 'verifier', 'author', 'contractId', 'contractDigest', 'taskKind', 'taskDigest', 'objectiveDigest', 'profileDigest', 'criteria', 'expiresAt'];
151
+ if (Object.keys(binding).length !== keys.length || !keys.every(key => Object.hasOwn(binding, key)))
152
+ return false;
153
+ const author = binding['author'];
154
+ const criteria = binding['criteria'];
155
+ const expiresAt = binding['expiresAt'];
156
+ return binding['protocol'] === ACCEPTANCE_BINDING_PROTOCOL
157
+ && binding['verifier'] === ACCEPTANCE_VERIFIER
158
+ && typeof author === 'string' && AUTHORS.includes(author)
159
+ && typeof binding['taskKind'] === 'string' && acceptanceAuthorFor(binding['taskKind']) === author
160
+ && acceptanceIsIndependent({ author, verifier: ACCEPTANCE_VERIFIER, executor: ACCEPTANCE_EXECUTOR })
161
+ && typeof binding['contractId'] === 'string' && ACCEPTANCE_CONTRACT_ID.test(binding['contractId'])
162
+ && ['contractDigest', 'taskDigest', 'objectiveDigest', 'profileDigest'].every(key => typeof binding[key] === 'string' && DIGEST.test(binding[key]))
163
+ && typeof criteria === 'number' && Number.isSafeInteger(criteria) && criteria >= 0 && criteria <= MAX_CRITERIA
164
+ && typeof expiresAt === 'number' && Number.isSafeInteger(expiresAt) && expiresAt > 0;
165
+ }
166
+ /**
167
+ * Read one bound item's verdict back from the independent verifier.
168
+ *
169
+ * Takes no verdict input. The verifier must still hold the exact frozen
170
+ * contract, and its receipt must validate against that contract; the
171
+ * objective status is the one `validateTaskVerificationReceipt` derives from
172
+ * criterion results. Anything short of a validated receipt writes nothing.
173
+ */
174
+ export function settleAcceptance(sink, verifier, item, binding,
175
+ /**
176
+ * `acceptance.verdictGate`: the executed generation a not-achieved or
177
+ * unknown verdict moves to `blocked`, in the verdict's own transaction.
178
+ * The caller handles `needs-attention` (no verdict is written for it).
179
+ */
180
+ gate) {
181
+ const recorded = sink.acceptanceVerdict(item.id);
182
+ if (recorded !== undefined) {
183
+ // First receipt wins, but only for the contract this binding froze.
184
+ if (recorded.contractId !== binding.contractId || recorded.contractDigest !== binding.contractDigest) {
185
+ return Object.freeze({ outcome: 'drift', detail: 'recorded verdict is for another contract' });
186
+ }
187
+ if (gate === undefined || recorded.objectiveStatus === 'achieved')
188
+ return Object.freeze({ outcome: 'already-verified', verdict: recorded });
189
+ // The gate was enabled after this verdict: replaying the same receipt is idempotent and applies it.
190
+ const { recordedAt: _recordedAt, ...replay } = recorded;
191
+ const result = sink.recordAcceptanceVerdict(replay, gate);
192
+ return Object.freeze({ outcome: 'already-verified', verdict: result.verdict,
193
+ ...(result.blockedReason === undefined ? {} : { blockedReason: result.blockedReason }) });
194
+ }
195
+ const read = inspect(verifier, binding.contractId);
196
+ if (read.kind === 'unavailable')
197
+ return Object.freeze({ outcome: 'unavailable', detail: read.detail });
198
+ if (read.value === null || read.value === undefined)
199
+ return Object.freeze({ outcome: 'drift', detail: 'verifier no longer holds the bound contract' });
200
+ const contract = validContract(read.value.contract);
201
+ if (contract === undefined || contract.id !== binding.contractId || contract.digest !== binding.contractDigest) {
202
+ return Object.freeze({ outcome: 'drift', detail: 'verifier contract differs from the frozen binding' });
203
+ }
204
+ const state = read.value.state;
205
+ if (state === 'needs-attention')
206
+ return Object.freeze({ outcome: 'needs-attention', reason: read.value.reason });
207
+ if (state !== 'done')
208
+ return Object.freeze({ outcome: 'pending', state });
209
+ let receipt;
210
+ try {
211
+ receipt = validateTaskVerificationReceipt(contract, read.value.receipt);
212
+ }
213
+ catch (error) {
214
+ return Object.freeze({ outcome: 'drift', detail: `verifier receipt is invalid: ${error instanceof Error ? error.message : String(error)}` });
215
+ }
216
+ // Defensive: validateTaskVerificationReceipt already binds the receipt to
217
+ // this contract's id and digest, so this cannot fire for a valid receipt.
218
+ if (receipt.contractId !== binding.contractId || receipt.contractDigest !== binding.contractDigest) {
219
+ return Object.freeze({ outcome: 'drift', detail: 'verifier receipt is for another contract' });
220
+ }
221
+ // Evidence gathered before this item existed cannot complete it.
222
+ if (receipt.startedAt < item.createdAt) {
223
+ return Object.freeze({ outcome: 'drift', detail: 'verifier receipt predates the work item' });
224
+ }
225
+ let result;
226
+ try {
227
+ result = sink.recordAcceptanceVerdict({
228
+ itemId: item.id,
229
+ verifier: ACCEPTANCE_VERIFIER,
230
+ contractId: binding.contractId,
231
+ contractDigest: binding.contractDigest,
232
+ receiptId: receipt.id,
233
+ receiptDigest: receipt.digest,
234
+ objectiveStatus: receipt.objectiveStatus,
235
+ completedAt: receipt.completedAt,
236
+ attempt: item.attempt,
237
+ occurrenceId: item.occurrenceId,
238
+ }, gate);
239
+ }
240
+ catch (error) {
241
+ // A different receipt already won (first-receipt-wins); report, never overwrite.
242
+ const existing = error instanceof WorkItemError && error.code === 'identity-mismatch' ? sink.acceptanceVerdict(item.id) : undefined;
243
+ if (existing === undefined)
244
+ throw error;
245
+ return Object.freeze({ outcome: 'identity-mismatch', verdict: existing, receiptDigest: receipt.digest });
246
+ }
247
+ return Object.freeze({ outcome: result.created ? 'verified' : 'already-verified', verdict: result.verdict,
248
+ ...(result.blockedReason === undefined ? {} : { blockedReason: result.blockedReason }) });
249
+ }
250
+ //# sourceMappingURL=acceptance-binding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"acceptance-binding.js","sourceRoot":"","sources":["../src/acceptance-binding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EACL,gBAAgB,EAChB,8BAA8B,EAC9B,+BAA+B,GAEhC,MAAM,wCAAwC,CAAA;AAa/C,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE1C,MAAM,CAAC,MAAM,2BAA2B,GAAG,kCAAkC,CAAA;AAC7E,+DAA+D;AAC/D,MAAM,CAAC,MAAM,mBAAmB,GAAG,oBAAoB,CAAA;AACvD,4EAA4E;AAC5E,MAAM,CAAC,MAAM,mBAAmB,GAAG,sBAAsB,CAAA;AAEzD,4DAA4D;AAC5D,MAAM,CAAC,MAAM,sBAAsB,GAAG,uCAAuC,CAAA;AAC7E,MAAM,MAAM,GAAG,iBAAiB,CAAA;AAChC,MAAM,OAAO,GAAwC,CAAC,uBAAuB,EAAE,oBAAoB,EAAE,iBAAiB,CAAC,CAAA;AACvH,MAAM,YAAY,GAAG,KAAK,CAAA;AAE1B;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,QAAgB;IAClD,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,gBAAgB,CAAC,CAAC,OAAO,uBAAuB,CAAA;QACrD,KAAK,iBAAiB,CAAC,CAAC,OAAO,oBAAoB,CAAA;QACnD,KAAK,WAAW,CAAC;QACjB,KAAK,cAAc,CAAC,CAAC,OAAO,iBAAiB,CAAA;QAC7C,OAAO,CAAC,CAAC,OAAO,SAAS,CAAA;IAC3B,CAAC;AACH,CAAC;AAED,uEAAuE;AACvE,MAAM,UAAU,uBAAu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AE,aAAa,EAAE,MAAM,CAAC,aAAa,EAAE,CAAC,EAAE,CAAC,CAAA;AAC7F,CAAC"}
@@ -0,0 +1,95 @@
1
+ import type { NormalizedConfig } from './config.ts';
2
+ import type { WorkItemLedger } from './ledger.ts';
3
+ import type { PeerDeliveryService, PeerPolicyService, PeerVerifierService } from './peers.ts';
4
+ import type { WorkItemStore } from './store.ts';
5
+ import type { WorkItemAdmitInput, WorkItemAdmitResult, WorkItemDecisionAdmitResult, WorkItemRecord, WorkItemWithdrawResult } from './types.ts';
6
+ import type { WorkItemDiagnosisPlan } from './diagnosis-contract.js';
7
+ import { type WorkItemContinuationAdmission } from './continuation-record.js';
8
+ import type { WorkItemWorkspace } from './workspace.ts';
9
+ /** Stable per-source dedup identity for one external work signal. */
10
+ export declare function dedupKeyFor(sourceId: string, kind: string, externalId: string): string;
11
+ /** Dedup key of an execute decision: source and decision external id only, never the kind. */
12
+ export declare function decisionDedupKeyFor(sourceId: string, externalId: string): string;
13
+ export declare function dedupBucketFor(createdAt: number, windowMs: number): number;
14
+ export interface AdmissionDependencies {
15
+ readonly config: NormalizedConfig;
16
+ readonly store: WorkItemStore;
17
+ readonly ledger: WorkItemLedger;
18
+ readonly workspace: WorkItemWorkspace;
19
+ readonly policy: PeerPolicyService | undefined;
20
+ readonly delivery: PeerDeliveryService | undefined | (() => PeerDeliveryService | undefined);
21
+ /** Independent verifier, read only for decisions that carry an acceptance handle. */
22
+ readonly verifier?: PeerVerifierService | undefined | (() => PeerVerifierService | undefined);
23
+ readonly continuationFor?: (input: WorkItemAdmitInput) => WorkItemContinuationAdmission | undefined;
24
+ readonly diagnosisPlanFor?: (input: WorkItemAdmitInput) => ((item: WorkItemRecord) => Readonly<WorkItemDiagnosisPlan>) | undefined;
25
+ /**
26
+ * Look up the registered thread source for one ref kind.
27
+ *
28
+ * Synchronous and read-only; the intake stores the registration's identity so
29
+ * a later pass can detect a generation change instead of believing a stale
30
+ * closure. A ref whose source is not registered at admission is rejected.
31
+ */
32
+ readonly threadSource?: (kind: string) => {
33
+ readonly source: string;
34
+ readonly generation: string;
35
+ readonly validate?: (ref: string) => boolean;
36
+ readonly canonicalize?: (ref: string) => string | undefined;
37
+ } | undefined;
38
+ readonly now?: () => number;
39
+ readonly newId?: () => string;
40
+ }
41
+ /** What an admission would have done, recorded so observe mode is legible. */
42
+ export interface AdmissionGateReport {
43
+ readonly wouldReject: readonly string[];
44
+ }
45
+ /**
46
+ * The only write path for external work signals.
47
+ *
48
+ * Gate order is fixed and cheap-first, and every rejection happens before any
49
+ * durable write: an invalid owner route, a duplicate, an unlisted kind, a
50
+ * cooldown, the hourly rate or the open-item cap all leave the ledger
51
+ * untouched. In `observe` mode the same pipeline still records the item as
52
+ * `dropped`/`observe-only` with `observed = 1`, and never calls automations or
53
+ * delivery.
54
+ */
55
+ export declare class WorkItemAdmission {
56
+ #private;
57
+ constructor(dependencies: AdmissionDependencies);
58
+ /**
59
+ * Validate and (maybe) admit one signal.
60
+ *
61
+ * Returns `created: false` with the existing item when the same signal was
62
+ * already admitted inside the dedup window.
63
+ */
64
+ admit(input: WorkItemAdmitInput): WorkItemAdmitResult;
65
+ /**
66
+ * Admit one execute decision under a frozen mandate.
67
+ *
68
+ * Order: shape → active-only → owner route → decision replay (any state, any
69
+ * dedup window; a different mandate under the same id is `identity-mismatch`)
70
+ * → inferred gate (auto mode, named Policy rule, remaining budget ≥ 1) →
71
+ * ordinary kind/cooldown/rate/capacity gates → Policy `admit`, whose allowing
72
+ * rule must equal the named rule. A decision carrying an acceptance handle
73
+ * is also bound to the verifier's exact contract for this owner scope
74
+ * (after the inferred gate, before the ordinary gates). Every refusal
75
+ * happens before any durable write; the mandate is persisted inside the
76
+ * item's authority basis.
77
+ */
78
+ admitDecision(value: unknown): WorkItemDecisionAdmitResult;
79
+ /**
80
+ * Retire the open item a withdrawn signal already produced, if any.
81
+ *
82
+ * Withdrawal shares `admit()`'s normalization and dedup identity, so a
83
+ * `cancelled` revision of an execution resolves to the same item as its
84
+ * `failed` revision. Every open row of that key is retired, not just the
85
+ * newest: the dedup window may have rolled while an older item stayed open.
86
+ * It deliberately skips the owner-route re-anchor, Policy
87
+ * authorization, cooldown, kind/rate/capacity gates and ledger recovery:
88
+ * those bound *new* work, while withdrawal only reduces pending work. It
89
+ * cannot recall a report already handed to `assistant-delivery`; it retires
90
+ * open state and a still-pending wake-up, and the worker's next tool call
91
+ * observes the dropped item.
92
+ */
93
+ withdraw(input: WorkItemAdmitInput): WorkItemWithdrawResult;
94
+ }
95
+ //# sourceMappingURL=admission.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"admission.d.ts","sourceRoot":"","sources":["../src/admission.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAGnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AACjD,OAAO,KAAK,EAAE,mBAAmB,EAAyB,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAEpH,OAAO,KAAK,EAAuB,aAAa,EAAE,MAAM,YAAY,CAAA;AAEpE,OAAO,KAAK,EACV,kBAAkB,EAClB,mBAAmB,EAEnB,2BAA2B,EAM3B,cAAc,EAGd,sBAAsB,EACvB,MAAM,YAAY,CAAA;AAEnB,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AACpE,OAAO,EAAyB,KAAK,6BAA6B,EAAE,MAAM,0BAA0B,CAAA;AACpG,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AA4GvD,qEAAqE;AACrE,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAEtF;AAED,8FAA8F;AAC9F,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAIhF;AAED,wBAAgB,cA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