dsh-taskbridge 1.7.1 → 1.8.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 +12 -0
- package/LICENSE +21 -21
- package/README.md +149 -290
- package/THIRD_PARTY_NOTICES.txt +23 -23
- package/antigravity/agents/dsh-taskbridge.md +31 -0
- package/antigravity/skills/dsh-taskbridge/SKILL.md +51 -0
- package/codex/dsh-taskbridge.toml +170 -631
- package/cordis.patch.yml +6 -6
- package/docs/INTRODUCTION.md +119 -295
- package/docs/INTRODUCTION.zh.md +134 -279
- package/docs/antigravity-integration.md +42 -0
- package/docs/auth-contract.md +138 -138
- package/docs/claude-code.md +6 -11
- package/docs/codex-integration.md +2 -2
- package/docs/contracts-stage-b.md +443 -443
- package/docs/plugin-market.md +30 -30
- package/docs/redaction-contract.md +170 -170
- package/docs/releases/1.7.0.zh.md +11 -11
- package/docs/releases/1.8.0.zh.md +16 -0
- package/docs/runtime-compatibility.md +1 -1
- package/docs/taskbridge-upgrade.md +28 -28
- package/docs/taskdispatch-upgrade.md +25 -25
- package/docs/workspace-contract.md +538 -538
- package/docs/workspace-grouping.md +126 -126
- package/icon.svg +1 -1
- package/lib/client.d.ts +733 -699
- package/lib/client.js +7 -7
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +149 -144
- package/src/antigravity/installer.js +1046 -0
- package/src/antigravity/seam.js +142 -0
- package/src/auth/fence.js +361 -361
- package/src/auth/grants.js +789 -789
- package/src/client/api.js +143 -0
- package/src/client/index.jsx +113 -3
- package/src/client/sidebar.jsx +862 -862
- package/src/client/styles.js +624 -624
- package/src/client/task-cleanup.js +304 -304
- package/src/client/task-records-table.jsx +217 -217
- package/src/client/task-view.js +307 -307
- package/src/client/workspace-groups.js +482 -482
- package/src/client/workspaces-browser.jsx +770 -770
- package/src/codex/agent-ownership.js +270 -270
- package/src/codex/installer.js +3 -0
- package/src/codex/toml.js +784 -784
- package/src/config/redact.js +245 -245
- package/src/config/store.js +186 -186
- package/src/credentials/seam.js +202 -202
- package/src/http/respond.js +6 -5
- package/src/http/user-plane.js +72 -0
- package/src/index.js +25 -0
- package/src/jobs/diagnostics.js +15 -15
- package/src/jobs/plan-store.js +1020 -1020
- package/src/jobs/review-note.js +63 -63
- package/src/jobs/task-descriptions.js +82 -82
- package/src/jobs/task-display.js +73 -73
- package/src/jobs/task-tree.js +786 -786
- package/src/jobs/title.js +134 -134
- package/src/mcp/continuation.js +317 -317
- package/src/mcp/server.js +4 -1
- package/src/model/connectivity.js +371 -371
- package/src/model/discovery.js +582 -582
- package/src/model/native-catalog.js +68 -68
- package/src/model/openai-adapter.js +671 -671
- package/src/model/probe.js +264 -264
- package/src/model/providers.js +756 -756
- package/src/model/routing.js +321 -321
- package/src/runtime/permissions.js +1619 -1619
- package/src/runtime/question-call.js +162 -162
- package/src/runtime/questions.js +909 -909
- package/src/runtime/worker-prompt.js +60 -55
- package/src/workspace/audit.js +775 -775
- package/src/workspace/preflight.js +424 -424
- package/src/workspace/worktrees.js +679 -679
package/src/mcp/continuation.js
CHANGED
|
@@ -1,317 +1,317 @@
|
|
|
1
|
-
// The two predicates the post-decision continuation needs — and nothing else.
|
|
2
|
-
//
|
|
3
|
-
// `delegate_permission_decide({ waitAfterDecision: true })` answers in the SAME shape a
|
|
4
|
-
// `delegate_wait` answers in, because the commander's next decision is the same decision
|
|
5
|
-
// either way. That means the continuation must be THE wait loop (`./wait.js`): the same
|
|
6
|
-
// 25-second internal observation, the same default deadline, the same heartbeat handling,
|
|
7
|
-
// the same `interrupted` classification and the same bounded deadline semantics. This
|
|
8
|
-
// module therefore contains no loop, no timeout and no reason vocabulary of its own; it
|
|
9
|
-
// supplies only the two facts the shared loop cannot know:
|
|
10
|
-
//
|
|
11
|
-
// 1. **Rollover.** After a decision the engine may still publish the settled request in
|
|
12
|
-
// `task.permission.pending` until the paused native call actually resumes (the
|
|
13
|
-
// bridge's `onSettled` fires when the call continues, not when the decision is made).
|
|
14
|
-
// A loop that answered that would present the commander's own decision back as a new
|
|
15
|
-
// question. {@link decidedRequestMatcher} is the predicate the shared loop uses to
|
|
16
|
-
// keep waiting, and it matches on the FULL identity — `requestId` AND `fingerprint` —
|
|
17
|
-
// so a new request id, or the same id with a changed operation, is never inherited.
|
|
18
|
-
//
|
|
19
|
-
// 2. **Receipt evidence.** {@link decisionReceipt} reads the harness's answer to the
|
|
20
|
-
// decision and states exactly what can be proven about it, including the case where
|
|
21
|
-
// the transport failed and nobody can know whether it was applied.
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* The pending operation a wait-shaped answer carries, if any.
|
|
25
|
-
* @param {object | null | undefined} observation - an observation.
|
|
26
|
-
* @returns {object | null} the pending operation.
|
|
27
|
-
*/
|
|
28
|
-
export function pendingOperationOf(observation) {
|
|
29
|
-
const block = observation?.permission;
|
|
30
|
-
if (block === null || typeof block !== 'object') return null;
|
|
31
|
-
const operation = block.pending !== undefined ? block.pending : block;
|
|
32
|
-
if (operation === null || typeof operation !== 'object' || Array.isArray(operation)) return null;
|
|
33
|
-
return operation;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Build the predicate that decides whether an observation describes the request this call
|
|
38
|
-
* already decided.
|
|
39
|
-
*
|
|
40
|
-
* The match requires BOTH the request id and the fingerprint. That is the whole rule:
|
|
41
|
-
*
|
|
42
|
-
* * same id, same fingerprint → the settled request still in its slot → keep waiting;
|
|
43
|
-
* * a different request id → a NEW question → answered immediately;
|
|
44
|
-
* * same id, different fingerprint → the operation changed → a NEW question, because a
|
|
45
|
-
* decision is bound to one exact operation and inheriting it would grant an operation
|
|
46
|
-
* the commander never reviewed.
|
|
47
|
-
*
|
|
48
|
-
* Cost asymmetry: wrongly treating a settled request as new costs one host turn; wrongly
|
|
49
|
-
* treating a new request as settled would leave a worker paused with nothing reported. So
|
|
50
|
-
* every field is required to match, and nothing is assumed.
|
|
51
|
-
* @param {{ requestId?: string | null, fingerprint?: string | null }} receipt - the decision.
|
|
52
|
-
* @returns {(observation: object) => boolean} predicate for the shared wait loop.
|
|
53
|
-
*/
|
|
54
|
-
export function decidedRequestMatcher(receipt) {
|
|
55
|
-
const requestId = typeof receipt?.requestId === 'string' && receipt.requestId !== '' ? receipt.requestId : null;
|
|
56
|
-
const fingerprint = typeof receipt?.fingerprint === 'string' && receipt.fingerprint !== ''
|
|
57
|
-
? receipt.fingerprint
|
|
58
|
-
: null;
|
|
59
|
-
if (requestId === null || fingerprint === null) {
|
|
60
|
-
// Without BOTH halves of the identity nothing can be proven to be the decided request,
|
|
61
|
-
// so nothing is skipped: a request already settled is refused by the engine as
|
|
62
|
-
// `PERMISSION_REQUEST_NOT_PENDING`, which is a refusal the host can act on.
|
|
63
|
-
return () => false;
|
|
64
|
-
}
|
|
65
|
-
return (observation) => {
|
|
66
|
-
const operation = pendingOperationOf(observation);
|
|
67
|
-
if (operation === null) return false;
|
|
68
|
-
return operation.requestId === requestId && operation.fingerprint === fingerprint;
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* The ONE settled state that means "this request was decided".
|
|
74
|
-
*
|
|
75
|
-
* `expired`, `cancelled` and `unavailable` are settled records too, but none of them is a
|
|
76
|
-
* decision this call applied — an `expired` request carrying a stale `allowed-once` outcome
|
|
77
|
-
* is exactly the shape that must never read as confirmed.
|
|
78
|
-
*/
|
|
79
|
-
const CONFIRMED_STATE = 'decided';
|
|
80
|
-
|
|
81
|
-
/** The closed native outcomes a settled allow/reject record can carry. */
|
|
82
|
-
const SETTLED_OUTCOMES = Object.freeze(['allowed-once', 'rejected', 'cancelled', 'unavailable']);
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* The verification checks an `allowed-once` must report as TRUE to confirm the allow.
|
|
86
|
-
*
|
|
87
|
-
* These are the four claims the harness makes when it grants one call: the caller quoted the
|
|
88
|
-
* fingerprint, the arguments were complete, the operation was re-read from the Session log,
|
|
89
|
-
* and the turn was still open. A record that reports any of them false is not evidence for an
|
|
90
|
-
* allow, whatever its `outcome` says. A `reject` needs none of them.
|
|
91
|
-
*/
|
|
92
|
-
const VERIFIED_REQUIRED = Object.freeze(['fingerprintQuoted', 'argumentsComplete', 'callReread', 'turnOpen']);
|
|
93
|
-
|
|
94
|
-
const asString = (value) => (typeof value === 'string' && value !== '' ? value : null);
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* What can honestly be said about one decision, from the harness's own answer.
|
|
98
|
-
*
|
|
99
|
-
* The ONLY thing that makes a decision `applied` is a permission answer whose own fields
|
|
100
|
-
* confirm it, and the check is deliberately strict because a false `applied: true` is the
|
|
101
|
-
* worst outcome this tool can produce: it tells a commander an authorization is in force
|
|
102
|
-
* when it is not (or one operation when another was settled). The real permission route
|
|
103
|
-
* answers:
|
|
104
|
-
*
|
|
105
|
-
* ```js
|
|
106
|
-
* { ok: true,
|
|
107
|
-
* task: { taskId, ... },
|
|
108
|
-
* permission: { taskId, requestId, fingerprint, state: 'decided',
|
|
109
|
-
* outcome: 'allowed-once' | 'rejected', verified?, grant?, decision? } }
|
|
110
|
-
* ```
|
|
111
|
-
*
|
|
112
|
-
* `decision` is sometimes `null` in the real bridge, so it is NOT required; the settled
|
|
113
|
-
* `outcome` is. `verified`/`grant` exist only for an `allowed-once`, so a `reject` is never
|
|
114
|
-
* required to have them.
|
|
115
|
-
*
|
|
116
|
-
* Two outcomes, and the difference between them is the point:
|
|
117
|
-
*
|
|
118
|
-
* * **applied** — the answer is `ok: true`, names the SAME task (both task ids, when both
|
|
119
|
-
* appear, must agree), the SAME request and the SAME operation fingerprint the decision
|
|
120
|
-
* was bound to, is in state `decided`, and its `outcome` matches the decision
|
|
121
|
-
* (`allowed-once` for allow-once, `rejected` for reject). An allow additionally needs
|
|
122
|
-
* every {@link VERIFIED_REQUIRED} check to be true. Only then is the decision in force.
|
|
123
|
-
* * **unknown** — the call failed before an answer existed (transport, timeout, malformed
|
|
124
|
-
* body), or the answer is `ok: false`, or it lacks the evidence above, or its identity
|
|
125
|
-
* CONTRADICTS the request that was decided. Nobody can say whether the decision applied,
|
|
126
|
-
* so it is never claimed, never continued and never resent automatically.
|
|
127
|
-
* * **refused** — the harness's own refusal code: the decision was definitely not applied.
|
|
128
|
-
*
|
|
129
|
-
* A QUOTED fingerprint must be matched by evidence. The caller's value is never silently
|
|
130
|
-
* replaced by a conflicting backend one, and an answer that omits it cannot confirm that
|
|
131
|
-
* operation. A `reject` that quoted no fingerprint is not required to produce one.
|
|
132
|
-
* @param {object} options - `taskId`, `requestId`, `decision`, `fingerprint` (the quoted one,
|
|
133
|
-
* or null), `answer` (the harness answer, or null), `error` (the thrown failure, or null).
|
|
134
|
-
* @returns {{ applied: boolean | null, confirmation: 'applied' | 'unknown' | 'refused',
|
|
135
|
-
* taskId: string | null, requestId: string | null, decision: 'allow-once' | 'reject' | null,
|
|
136
|
-
* fingerprint: string | null, outcome?: string, verified?: object, grant?: object,
|
|
137
|
-
* decidedAt?: number, reason?: string, error?: object, evidence?: object }} the receipt fields.
|
|
138
|
-
*/
|
|
139
|
-
export function decisionReceipt(options) {
|
|
140
|
-
const error = options.error ?? null;
|
|
141
|
-
const answer = options.answer ?? null;
|
|
142
|
-
const settled = answer?.permission ?? null;
|
|
143
|
-
const answerTask = answer?.task ?? null;
|
|
144
|
-
const requestedTaskId = asString(options.taskId);
|
|
145
|
-
const requestedRequestId = asString(options.requestId);
|
|
146
|
-
const quotedFingerprint = asString(options.fingerprint);
|
|
147
|
-
const decision = options.decision === 'allow-once' || options.decision === 'reject'
|
|
148
|
-
? options.decision
|
|
149
|
-
: null;
|
|
150
|
-
|
|
151
|
-
/** The identity the receipt may safely publish: only values the caller itself supplied. */
|
|
152
|
-
const identity = {
|
|
153
|
-
taskId: requestedTaskId,
|
|
154
|
-
requestId: requestedRequestId,
|
|
155
|
-
decision,
|
|
156
|
-
fingerprint: quotedFingerprint,
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
/** Build an UNCONFIRMED receipt that keeps the caller's own identity and says why. */
|
|
160
|
-
const unconfirmed = (reason, extra = {}) => ({
|
|
161
|
-
...identity,
|
|
162
|
-
// Never `false`: "not applied" would be a fact nobody established.
|
|
163
|
-
applied: null,
|
|
164
|
-
confirmation: 'unknown',
|
|
165
|
-
reason,
|
|
166
|
-
...extra,
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
if (error !== null) {
|
|
170
|
-
const code = typeof error.code === 'string' ? error.code : 'MCP_CALL_FAILED';
|
|
171
|
-
// A refusal the harness produced is a fact about the decision; every other failure
|
|
172
|
-
// happened before an answer existed, so the outcome is genuinely unknown.
|
|
173
|
-
if (options.refusedByHarness === true) {
|
|
174
|
-
return {
|
|
175
|
-
...identity,
|
|
176
|
-
applied: false,
|
|
177
|
-
confirmation: 'refused',
|
|
178
|
-
error: { code, message: 'the harness refused this decision' },
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
return unconfirmed(
|
|
182
|
-
'the decision call did not produce a harness answer, so whether it was applied is unknown; '
|
|
183
|
-
+ 'read the task\'s real state with delegate_result before deciding whether to send it again',
|
|
184
|
-
{ error: { code, message: 'the decision could not be confirmed' } },
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// ---- What the answer itself proves -------------------------------------------------
|
|
189
|
-
if (answer === null || typeof answer !== 'object' || answer.ok !== true) {
|
|
190
|
-
return unconfirmed('the harness answer was not ok, so the decision cannot be confirmed as applied');
|
|
191
|
-
}
|
|
192
|
-
if (settled === null || typeof settled !== 'object' || Array.isArray(settled)) {
|
|
193
|
-
return unconfirmed('the harness answer carried no settled permission record to confirm the decision');
|
|
194
|
-
}
|
|
195
|
-
const settledTaskId = asString(settled.taskId);
|
|
196
|
-
const outerTaskId = asString(answerTask?.taskId);
|
|
197
|
-
const answerRequestId = asString(settled.requestId);
|
|
198
|
-
const answerFingerprint = asString(settled.fingerprint);
|
|
199
|
-
const answerState = asString(settled.state);
|
|
200
|
-
const answerOutcome = asString(settled.outcome);
|
|
201
|
-
if (answerRequestId === null || answerState === null || answerOutcome === null) {
|
|
202
|
-
return unconfirmed('the harness answer did not carry the identity and outcome needed to confirm the decision', {
|
|
203
|
-
evidence: { taskId: settledTaskId ?? outerTaskId, requestId: answerRequestId, state: answerState, outcome: answerOutcome },
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
// Identity must MATCH what was decided. A different task, request or operation is a
|
|
207
|
-
// CONTRADICTION: the answer describes something this call did not decide.
|
|
208
|
-
//
|
|
209
|
-
// BOTH task ids are checked when both appear, and they must ALSO agree with each other. A
|
|
210
|
-
// matching outer `task.taskId` cannot vouch for a settled record that names another task:
|
|
211
|
-
// the settled record is the thing being confirmed, so an internal disagreement means the
|
|
212
|
-
// answer is not one coherent settlement of this task.
|
|
213
|
-
if (settledTaskId !== null && outerTaskId !== null && settledTaskId !== outerTaskId) {
|
|
214
|
-
return unconfirmed('the harness answer contradicted itself: its task and its settled record name different tasks', {
|
|
215
|
-
evidence: { answeredTaskId: settledTaskId, surroundingTaskId: outerTaskId },
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
const answerTaskId = settledTaskId ?? outerTaskId;
|
|
219
|
-
if (answerTaskId === null) {
|
|
220
|
-
return unconfirmed('the harness answer did not name the task it settled', {
|
|
221
|
-
evidence: { taskId: null },
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
if (requestedTaskId !== null && answerTaskId !== requestedTaskId) {
|
|
225
|
-
return unconfirmed('the harness confirmed a DIFFERENT task than the one this decision named', {
|
|
226
|
-
evidence: { answeredTaskId: answerTaskId },
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
if (requestedRequestId !== null && answerRequestId !== requestedRequestId) {
|
|
230
|
-
return unconfirmed('the harness confirmed a DIFFERENT request than the one this decision named', {
|
|
231
|
-
evidence: { answeredRequestId: answerRequestId },
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
// The operation is part of the identity. A QUOTED fingerprint must be matched by evidence:
|
|
235
|
-
// an answer that omits it cannot confirm that THIS operation was settled, and a conflicting
|
|
236
|
-
// backend value is never allowed to replace the quoted one.
|
|
237
|
-
if (quotedFingerprint !== null) {
|
|
238
|
-
if (answerFingerprint === null) {
|
|
239
|
-
return unconfirmed('the harness settled without reporting the operation fingerprint this decision quoted', {
|
|
240
|
-
evidence: { answeredFingerprint: null },
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
if (answerFingerprint !== quotedFingerprint) {
|
|
244
|
-
return unconfirmed('the harness settled a DIFFERENT operation than the fingerprint this decision quoted', {
|
|
245
|
-
evidence: { answeredFingerprint: answerFingerprint },
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
// Only `decided` is a settled DECISION. `expired`, `cancelled` and `unavailable` are also
|
|
250
|
-
// settled records, but none of them means "this decision was applied".
|
|
251
|
-
if (answerState !== CONFIRMED_STATE) {
|
|
252
|
-
return unconfirmed(`the harness reported state ${JSON.stringify(answerState)}, which is not a decided request`, {
|
|
253
|
-
evidence: { state: answerState },
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
if (!SETTLED_OUTCOMES.includes(answerOutcome)) {
|
|
257
|
-
return unconfirmed(`the harness reported outcome ${JSON.stringify(answerOutcome)}, which is not a settled decision`, {
|
|
258
|
-
evidence: { outcome: answerOutcome },
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
// The outcome must be the one this decision asked for. `allowed-once` is the only grant,
|
|
262
|
-
// and a reject must read `rejected`; anything else is not this decision's confirmation.
|
|
263
|
-
const expectedOutcome = decision === 'allow-once' ? 'allowed-once' : decision === 'reject' ? 'rejected' : null;
|
|
264
|
-
if (expectedOutcome === null) {
|
|
265
|
-
return unconfirmed('the decision argument was not a recognized decision');
|
|
266
|
-
}
|
|
267
|
-
if (answerOutcome !== expectedOutcome) {
|
|
268
|
-
return unconfirmed(
|
|
269
|
-
`the harness settled ${JSON.stringify(answerOutcome)}, not the ${JSON.stringify(expectedOutcome)} this decision asked for`,
|
|
270
|
-
{ evidence: { outcome: answerOutcome } },
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
// An allow must be backed by the checks it claims, and every required one must be TRUE: a
|
|
274
|
-
// record whose own verification says the fingerprint was not quoted, the arguments were not
|
|
275
|
-
// complete, the call was not re-read or the turn was not open is not evidence for an allow.
|
|
276
|
-
// A reject needs none of them.
|
|
277
|
-
if (decision === 'allow-once') {
|
|
278
|
-
const verified = settled.verified;
|
|
279
|
-
if (verified === null || typeof verified !== 'object' || Array.isArray(verified)) {
|
|
280
|
-
return unconfirmed('an allowed-once was reported without the verification evidence an allow requires');
|
|
281
|
-
}
|
|
282
|
-
const failed = VERIFIED_REQUIRED.filter((key) => verified[key] !== true);
|
|
283
|
-
if (failed.length > 0) {
|
|
284
|
-
return unconfirmed(
|
|
285
|
-
`an allowed-once reported verification that does not hold (${failed.join(', ')}), so it cannot confirm the allow`,
|
|
286
|
-
{ evidence: { unverified: failed } },
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
return {
|
|
291
|
-
taskId: answerTaskId,
|
|
292
|
-
requestId: answerRequestId,
|
|
293
|
-
decision,
|
|
294
|
-
// The confirmed operation. It equals the quoted fingerprint here — a mismatch returned
|
|
295
|
-
// above — so publishing the harness's own value is a restatement, not a substitution.
|
|
296
|
-
fingerprint: answerFingerprint ?? quotedFingerprint,
|
|
297
|
-
applied: true,
|
|
298
|
-
confirmation: 'applied',
|
|
299
|
-
outcome: answerOutcome,
|
|
300
|
-
...(settled.verified !== undefined ? { verified: settled.verified } : {}),
|
|
301
|
-
...(settled.decidedAt !== undefined ? { decidedAt: settled.decidedAt } : {}),
|
|
302
|
-
...(settled.grant !== undefined ? { grant: settled.grant } : {}),
|
|
303
|
-
// Whether the rejection reason reached the worker, stated as the SHORT fact only. The
|
|
304
|
-
// note text is the worker's own message and is stored in history; repeating it here
|
|
305
|
-
// would multiply the receipt for no gain, so only delivery and its reason travel.
|
|
306
|
-
...(settled.rejectionFeedback !== undefined && settled.rejectionFeedback !== null
|
|
307
|
-
? {
|
|
308
|
-
rejectionFeedback: {
|
|
309
|
-
delivered: settled.rejectionFeedback.delivered === true,
|
|
310
|
-
reason: typeof settled.rejectionFeedback.reason === 'string'
|
|
311
|
-
? settled.rejectionFeedback.reason
|
|
312
|
-
: null,
|
|
313
|
-
},
|
|
314
|
-
}
|
|
315
|
-
: {}),
|
|
316
|
-
};
|
|
317
|
-
}
|
|
1
|
+
// The two predicates the post-decision continuation needs — and nothing else.
|
|
2
|
+
//
|
|
3
|
+
// `delegate_permission_decide({ waitAfterDecision: true })` answers in the SAME shape a
|
|
4
|
+
// `delegate_wait` answers in, because the commander's next decision is the same decision
|
|
5
|
+
// either way. That means the continuation must be THE wait loop (`./wait.js`): the same
|
|
6
|
+
// 25-second internal observation, the same default deadline, the same heartbeat handling,
|
|
7
|
+
// the same `interrupted` classification and the same bounded deadline semantics. This
|
|
8
|
+
// module therefore contains no loop, no timeout and no reason vocabulary of its own; it
|
|
9
|
+
// supplies only the two facts the shared loop cannot know:
|
|
10
|
+
//
|
|
11
|
+
// 1. **Rollover.** After a decision the engine may still publish the settled request in
|
|
12
|
+
// `task.permission.pending` until the paused native call actually resumes (the
|
|
13
|
+
// bridge's `onSettled` fires when the call continues, not when the decision is made).
|
|
14
|
+
// A loop that answered that would present the commander's own decision back as a new
|
|
15
|
+
// question. {@link decidedRequestMatcher} is the predicate the shared loop uses to
|
|
16
|
+
// keep waiting, and it matches on the FULL identity — `requestId` AND `fingerprint` —
|
|
17
|
+
// so a new request id, or the same id with a changed operation, is never inherited.
|
|
18
|
+
//
|
|
19
|
+
// 2. **Receipt evidence.** {@link decisionReceipt} reads the harness's answer to the
|
|
20
|
+
// decision and states exactly what can be proven about it, including the case where
|
|
21
|
+
// the transport failed and nobody can know whether it was applied.
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The pending operation a wait-shaped answer carries, if any.
|
|
25
|
+
* @param {object | null | undefined} observation - an observation.
|
|
26
|
+
* @returns {object | null} the pending operation.
|
|
27
|
+
*/
|
|
28
|
+
export function pendingOperationOf(observation) {
|
|
29
|
+
const block = observation?.permission;
|
|
30
|
+
if (block === null || typeof block !== 'object') return null;
|
|
31
|
+
const operation = block.pending !== undefined ? block.pending : block;
|
|
32
|
+
if (operation === null || typeof operation !== 'object' || Array.isArray(operation)) return null;
|
|
33
|
+
return operation;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Build the predicate that decides whether an observation describes the request this call
|
|
38
|
+
* already decided.
|
|
39
|
+
*
|
|
40
|
+
* The match requires BOTH the request id and the fingerprint. That is the whole rule:
|
|
41
|
+
*
|
|
42
|
+
* * same id, same fingerprint → the settled request still in its slot → keep waiting;
|
|
43
|
+
* * a different request id → a NEW question → answered immediately;
|
|
44
|
+
* * same id, different fingerprint → the operation changed → a NEW question, because a
|
|
45
|
+
* decision is bound to one exact operation and inheriting it would grant an operation
|
|
46
|
+
* the commander never reviewed.
|
|
47
|
+
*
|
|
48
|
+
* Cost asymmetry: wrongly treating a settled request as new costs one host turn; wrongly
|
|
49
|
+
* treating a new request as settled would leave a worker paused with nothing reported. So
|
|
50
|
+
* every field is required to match, and nothing is assumed.
|
|
51
|
+
* @param {{ requestId?: string | null, fingerprint?: string | null }} receipt - the decision.
|
|
52
|
+
* @returns {(observation: object) => boolean} predicate for the shared wait loop.
|
|
53
|
+
*/
|
|
54
|
+
export function decidedRequestMatcher(receipt) {
|
|
55
|
+
const requestId = typeof receipt?.requestId === 'string' && receipt.requestId !== '' ? receipt.requestId : null;
|
|
56
|
+
const fingerprint = typeof receipt?.fingerprint === 'string' && receipt.fingerprint !== ''
|
|
57
|
+
? receipt.fingerprint
|
|
58
|
+
: null;
|
|
59
|
+
if (requestId === null || fingerprint === null) {
|
|
60
|
+
// Without BOTH halves of the identity nothing can be proven to be the decided request,
|
|
61
|
+
// so nothing is skipped: a request already settled is refused by the engine as
|
|
62
|
+
// `PERMISSION_REQUEST_NOT_PENDING`, which is a refusal the host can act on.
|
|
63
|
+
return () => false;
|
|
64
|
+
}
|
|
65
|
+
return (observation) => {
|
|
66
|
+
const operation = pendingOperationOf(observation);
|
|
67
|
+
if (operation === null) return false;
|
|
68
|
+
return operation.requestId === requestId && operation.fingerprint === fingerprint;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The ONE settled state that means "this request was decided".
|
|
74
|
+
*
|
|
75
|
+
* `expired`, `cancelled` and `unavailable` are settled records too, but none of them is a
|
|
76
|
+
* decision this call applied — an `expired` request carrying a stale `allowed-once` outcome
|
|
77
|
+
* is exactly the shape that must never read as confirmed.
|
|
78
|
+
*/
|
|
79
|
+
const CONFIRMED_STATE = 'decided';
|
|
80
|
+
|
|
81
|
+
/** The closed native outcomes a settled allow/reject record can carry. */
|
|
82
|
+
const SETTLED_OUTCOMES = Object.freeze(['allowed-once', 'rejected', 'cancelled', 'unavailable']);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The verification checks an `allowed-once` must report as TRUE to confirm the allow.
|
|
86
|
+
*
|
|
87
|
+
* These are the four claims the harness makes when it grants one call: the caller quoted the
|
|
88
|
+
* fingerprint, the arguments were complete, the operation was re-read from the Session log,
|
|
89
|
+
* and the turn was still open. A record that reports any of them false is not evidence for an
|
|
90
|
+
* allow, whatever its `outcome` says. A `reject` needs none of them.
|
|
91
|
+
*/
|
|
92
|
+
const VERIFIED_REQUIRED = Object.freeze(['fingerprintQuoted', 'argumentsComplete', 'callReread', 'turnOpen']);
|
|
93
|
+
|
|
94
|
+
const asString = (value) => (typeof value === 'string' && value !== '' ? value : null);
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* What can honestly be said about one decision, from the harness's own answer.
|
|
98
|
+
*
|
|
99
|
+
* The ONLY thing that makes a decision `applied` is a permission answer whose own fields
|
|
100
|
+
* confirm it, and the check is deliberately strict because a false `applied: true` is the
|
|
101
|
+
* worst outcome this tool can produce: it tells a commander an authorization is in force
|
|
102
|
+
* when it is not (or one operation when another was settled). The real permission route
|
|
103
|
+
* answers:
|
|
104
|
+
*
|
|
105
|
+
* ```js
|
|
106
|
+
* { ok: true,
|
|
107
|
+
* task: { taskId, ... },
|
|
108
|
+
* permission: { taskId, requestId, fingerprint, state: 'decided',
|
|
109
|
+
* outcome: 'allowed-once' | 'rejected', verified?, grant?, decision? } }
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* `decision` is sometimes `null` in the real bridge, so it is NOT required; the settled
|
|
113
|
+
* `outcome` is. `verified`/`grant` exist only for an `allowed-once`, so a `reject` is never
|
|
114
|
+
* required to have them.
|
|
115
|
+
*
|
|
116
|
+
* Two outcomes, and the difference between them is the point:
|
|
117
|
+
*
|
|
118
|
+
* * **applied** — the answer is `ok: true`, names the SAME task (both task ids, when both
|
|
119
|
+
* appear, must agree), the SAME request and the SAME operation fingerprint the decision
|
|
120
|
+
* was bound to, is in state `decided`, and its `outcome` matches the decision
|
|
121
|
+
* (`allowed-once` for allow-once, `rejected` for reject). An allow additionally needs
|
|
122
|
+
* every {@link VERIFIED_REQUIRED} check to be true. Only then is the decision in force.
|
|
123
|
+
* * **unknown** — the call failed before an answer existed (transport, timeout, malformed
|
|
124
|
+
* body), or the answer is `ok: false`, or it lacks the evidence above, or its identity
|
|
125
|
+
* CONTRADICTS the request that was decided. Nobody can say whether the decision applied,
|
|
126
|
+
* so it is never claimed, never continued and never resent automatically.
|
|
127
|
+
* * **refused** — the harness's own refusal code: the decision was definitely not applied.
|
|
128
|
+
*
|
|
129
|
+
* A QUOTED fingerprint must be matched by evidence. The caller's value is never silently
|
|
130
|
+
* replaced by a conflicting backend one, and an answer that omits it cannot confirm that
|
|
131
|
+
* operation. A `reject` that quoted no fingerprint is not required to produce one.
|
|
132
|
+
* @param {object} options - `taskId`, `requestId`, `decision`, `fingerprint` (the quoted one,
|
|
133
|
+
* or null), `answer` (the harness answer, or null), `error` (the thrown failure, or null).
|
|
134
|
+
* @returns {{ applied: boolean | null, confirmation: 'applied' | 'unknown' | 'refused',
|
|
135
|
+
* taskId: string | null, requestId: string | null, decision: 'allow-once' | 'reject' | null,
|
|
136
|
+
* fingerprint: string | null, outcome?: string, verified?: object, grant?: object,
|
|
137
|
+
* decidedAt?: number, reason?: string, error?: object, evidence?: object }} the receipt fields.
|
|
138
|
+
*/
|
|
139
|
+
export function decisionReceipt(options) {
|
|
140
|
+
const error = options.error ?? null;
|
|
141
|
+
const answer = options.answer ?? null;
|
|
142
|
+
const settled = answer?.permission ?? null;
|
|
143
|
+
const answerTask = answer?.task ?? null;
|
|
144
|
+
const requestedTaskId = asString(options.taskId);
|
|
145
|
+
const requestedRequestId = asString(options.requestId);
|
|
146
|
+
const quotedFingerprint = asString(options.fingerprint);
|
|
147
|
+
const decision = options.decision === 'allow-once' || options.decision === 'reject'
|
|
148
|
+
? options.decision
|
|
149
|
+
: null;
|
|
150
|
+
|
|
151
|
+
/** The identity the receipt may safely publish: only values the caller itself supplied. */
|
|
152
|
+
const identity = {
|
|
153
|
+
taskId: requestedTaskId,
|
|
154
|
+
requestId: requestedRequestId,
|
|
155
|
+
decision,
|
|
156
|
+
fingerprint: quotedFingerprint,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/** Build an UNCONFIRMED receipt that keeps the caller's own identity and says why. */
|
|
160
|
+
const unconfirmed = (reason, extra = {}) => ({
|
|
161
|
+
...identity,
|
|
162
|
+
// Never `false`: "not applied" would be a fact nobody established.
|
|
163
|
+
applied: null,
|
|
164
|
+
confirmation: 'unknown',
|
|
165
|
+
reason,
|
|
166
|
+
...extra,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
if (error !== null) {
|
|
170
|
+
const code = typeof error.code === 'string' ? error.code : 'MCP_CALL_FAILED';
|
|
171
|
+
// A refusal the harness produced is a fact about the decision; every other failure
|
|
172
|
+
// happened before an answer existed, so the outcome is genuinely unknown.
|
|
173
|
+
if (options.refusedByHarness === true) {
|
|
174
|
+
return {
|
|
175
|
+
...identity,
|
|
176
|
+
applied: false,
|
|
177
|
+
confirmation: 'refused',
|
|
178
|
+
error: { code, message: 'the harness refused this decision' },
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
return unconfirmed(
|
|
182
|
+
'the decision call did not produce a harness answer, so whether it was applied is unknown; '
|
|
183
|
+
+ 'read the task\'s real state with delegate_result before deciding whether to send it again',
|
|
184
|
+
{ error: { code, message: 'the decision could not be confirmed' } },
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ---- What the answer itself proves -------------------------------------------------
|
|
189
|
+
if (answer === null || typeof answer !== 'object' || answer.ok !== true) {
|
|
190
|
+
return unconfirmed('the harness answer was not ok, so the decision cannot be confirmed as applied');
|
|
191
|
+
}
|
|
192
|
+
if (settled === null || typeof settled !== 'object' || Array.isArray(settled)) {
|
|
193
|
+
return unconfirmed('the harness answer carried no settled permission record to confirm the decision');
|
|
194
|
+
}
|
|
195
|
+
const settledTaskId = asString(settled.taskId);
|
|
196
|
+
const outerTaskId = asString(answerTask?.taskId);
|
|
197
|
+
const answerRequestId = asString(settled.requestId);
|
|
198
|
+
const answerFingerprint = asString(settled.fingerprint);
|
|
199
|
+
const answerState = asString(settled.state);
|
|
200
|
+
const answerOutcome = asString(settled.outcome);
|
|
201
|
+
if (answerRequestId === null || answerState === null || answerOutcome === null) {
|
|
202
|
+
return unconfirmed('the harness answer did not carry the identity and outcome needed to confirm the decision', {
|
|
203
|
+
evidence: { taskId: settledTaskId ?? outerTaskId, requestId: answerRequestId, state: answerState, outcome: answerOutcome },
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
// Identity must MATCH what was decided. A different task, request or operation is a
|
|
207
|
+
// CONTRADICTION: the answer describes something this call did not decide.
|
|
208
|
+
//
|
|
209
|
+
// BOTH task ids are checked when both appear, and they must ALSO agree with each other. A
|
|
210
|
+
// matching outer `task.taskId` cannot vouch for a settled record that names another task:
|
|
211
|
+
// the settled record is the thing being confirmed, so an internal disagreement means the
|
|
212
|
+
// answer is not one coherent settlement of this task.
|
|
213
|
+
if (settledTaskId !== null && outerTaskId !== null && settledTaskId !== outerTaskId) {
|
|
214
|
+
return unconfirmed('the harness answer contradicted itself: its task and its settled record name different tasks', {
|
|
215
|
+
evidence: { answeredTaskId: settledTaskId, surroundingTaskId: outerTaskId },
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
const answerTaskId = settledTaskId ?? outerTaskId;
|
|
219
|
+
if (answerTaskId === null) {
|
|
220
|
+
return unconfirmed('the harness answer did not name the task it settled', {
|
|
221
|
+
evidence: { taskId: null },
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
if (requestedTaskId !== null && answerTaskId !== requestedTaskId) {
|
|
225
|
+
return unconfirmed('the harness confirmed a DIFFERENT task than the one this decision named', {
|
|
226
|
+
evidence: { answeredTaskId: answerTaskId },
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
if (requestedRequestId !== null && answerRequestId !== requestedRequestId) {
|
|
230
|
+
return unconfirmed('the harness confirmed a DIFFERENT request than the one this decision named', {
|
|
231
|
+
evidence: { answeredRequestId: answerRequestId },
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
// The operation is part of the identity. A QUOTED fingerprint must be matched by evidence:
|
|
235
|
+
// an answer that omits it cannot confirm that THIS operation was settled, and a conflicting
|
|
236
|
+
// backend value is never allowed to replace the quoted one.
|
|
237
|
+
if (quotedFingerprint !== null) {
|
|
238
|
+
if (answerFingerprint === null) {
|
|
239
|
+
return unconfirmed('the harness settled without reporting the operation fingerprint this decision quoted', {
|
|
240
|
+
evidence: { answeredFingerprint: null },
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
if (answerFingerprint !== quotedFingerprint) {
|
|
244
|
+
return unconfirmed('the harness settled a DIFFERENT operation than the fingerprint this decision quoted', {
|
|
245
|
+
evidence: { answeredFingerprint: answerFingerprint },
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// Only `decided` is a settled DECISION. `expired`, `cancelled` and `unavailable` are also
|
|
250
|
+
// settled records, but none of them means "this decision was applied".
|
|
251
|
+
if (answerState !== CONFIRMED_STATE) {
|
|
252
|
+
return unconfirmed(`the harness reported state ${JSON.stringify(answerState)}, which is not a decided request`, {
|
|
253
|
+
evidence: { state: answerState },
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
if (!SETTLED_OUTCOMES.includes(answerOutcome)) {
|
|
257
|
+
return unconfirmed(`the harness reported outcome ${JSON.stringify(answerOutcome)}, which is not a settled decision`, {
|
|
258
|
+
evidence: { outcome: answerOutcome },
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
// The outcome must be the one this decision asked for. `allowed-once` is the only grant,
|
|
262
|
+
// and a reject must read `rejected`; anything else is not this decision's confirmation.
|
|
263
|
+
const expectedOutcome = decision === 'allow-once' ? 'allowed-once' : decision === 'reject' ? 'rejected' : null;
|
|
264
|
+
if (expectedOutcome === null) {
|
|
265
|
+
return unconfirmed('the decision argument was not a recognized decision');
|
|
266
|
+
}
|
|
267
|
+
if (answerOutcome !== expectedOutcome) {
|
|
268
|
+
return unconfirmed(
|
|
269
|
+
`the harness settled ${JSON.stringify(answerOutcome)}, not the ${JSON.stringify(expectedOutcome)} this decision asked for`,
|
|
270
|
+
{ evidence: { outcome: answerOutcome } },
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
// An allow must be backed by the checks it claims, and every required one must be TRUE: a
|
|
274
|
+
// record whose own verification says the fingerprint was not quoted, the arguments were not
|
|
275
|
+
// complete, the call was not re-read or the turn was not open is not evidence for an allow.
|
|
276
|
+
// A reject needs none of them.
|
|
277
|
+
if (decision === 'allow-once') {
|
|
278
|
+
const verified = settled.verified;
|
|
279
|
+
if (verified === null || typeof verified !== 'object' || Array.isArray(verified)) {
|
|
280
|
+
return unconfirmed('an allowed-once was reported without the verification evidence an allow requires');
|
|
281
|
+
}
|
|
282
|
+
const failed = VERIFIED_REQUIRED.filter((key) => verified[key] !== true);
|
|
283
|
+
if (failed.length > 0) {
|
|
284
|
+
return unconfirmed(
|
|
285
|
+
`an allowed-once reported verification that does not hold (${failed.join(', ')}), so it cannot confirm the allow`,
|
|
286
|
+
{ evidence: { unverified: failed } },
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
taskId: answerTaskId,
|
|
292
|
+
requestId: answerRequestId,
|
|
293
|
+
decision,
|
|
294
|
+
// The confirmed operation. It equals the quoted fingerprint here — a mismatch returned
|
|
295
|
+
// above — so publishing the harness's own value is a restatement, not a substitution.
|
|
296
|
+
fingerprint: answerFingerprint ?? quotedFingerprint,
|
|
297
|
+
applied: true,
|
|
298
|
+
confirmation: 'applied',
|
|
299
|
+
outcome: answerOutcome,
|
|
300
|
+
...(settled.verified !== undefined ? { verified: settled.verified } : {}),
|
|
301
|
+
...(settled.decidedAt !== undefined ? { decidedAt: settled.decidedAt } : {}),
|
|
302
|
+
...(settled.grant !== undefined ? { grant: settled.grant } : {}),
|
|
303
|
+
// Whether the rejection reason reached the worker, stated as the SHORT fact only. The
|
|
304
|
+
// note text is the worker's own message and is stored in history; repeating it here
|
|
305
|
+
// would multiply the receipt for no gain, so only delivery and its reason travel.
|
|
306
|
+
...(settled.rejectionFeedback !== undefined && settled.rejectionFeedback !== null
|
|
307
|
+
? {
|
|
308
|
+
rejectionFeedback: {
|
|
309
|
+
delivered: settled.rejectionFeedback.delivered === true,
|
|
310
|
+
reason: typeof settled.rejectionFeedback.reason === 'string'
|
|
311
|
+
? settled.rejectionFeedback.reason
|
|
312
|
+
: null,
|
|
313
|
+
},
|
|
314
|
+
}
|
|
315
|
+
: {}),
|
|
316
|
+
};
|
|
317
|
+
}
|