@evomap/evolver-cli 2.0.41 → 2.0.44
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/dist/autoRecall.d.ts +24 -5
- package/dist/autoRecall.js +233 -12
- package/dist/autoexec.d.ts +69 -10
- package/dist/autoexec.js +847 -137
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +71 -26
- package/dist/cliContracts.js +29 -7
- package/dist/cliEntry.d.ts +3 -0
- package/dist/cliEntry.js +63 -0
- package/dist/commandCatalog.d.ts +1 -0
- package/dist/commandCatalog.js +1 -0
- package/dist/contextDelivery.d.ts +12 -0
- package/dist/contextDelivery.js +139 -0
- package/dist/cycleConsumer.js +9 -1
- package/dist/dispatch.js +6 -0
- package/dist/feedbackOutbox.d.ts +96 -0
- package/dist/feedbackOutbox.js +510 -0
- package/dist/feedbackOutboxCli.d.ts +7 -0
- package/dist/feedbackOutboxCli.js +208 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +286 -69
- package/dist/lifecycle.d.ts +18 -0
- package/dist/lifecycle.js +130 -3
- package/dist/lifecycleBootstrapTransaction.js +1 -6
- package/dist/liveStatus.d.ts +27 -0
- package/dist/liveStatus.js +197 -0
- package/dist/outboxLock.d.ts +7 -0
- package/dist/outboxLock.js +141 -0
- package/dist/promptRecallHook.d.ts +7 -22
- package/dist/promptRecallHook.js +20 -489
- package/dist/promptRecallRuntime.d.ts +24 -0
- package/dist/promptRecallRuntime.js +678 -0
- package/dist/promptRecallShared.d.ts +18 -0
- package/dist/promptRecallShared.js +119 -0
- package/dist/recall.js +27 -6
- package/dist/runtimeTelemetry.d.ts +9 -0
- package/dist/runtimeTelemetry.js +16 -0
- package/dist/sessionIngest.d.ts +25 -2
- package/dist/sessionIngest.js +196 -32
- package/dist/setupHooks.js +63 -13
- package/dist/setupHooksLifecycle.js +39 -17
- package/dist/skill2gep.d.ts +11 -0
- package/dist/skill2gep.js +23 -3
- package/dist/solo/mode.js +4 -0
- package/dist/taskCommitments.d.ts +4 -2
- package/dist/taskCommitments.js +208 -114
- package/dist/taskOutcomeOutbox.d.ts +83 -0
- package/dist/taskOutcomeOutbox.js +553 -0
- package/dist/workflowRuntime.js +12 -0
- package/package.json +11 -7
package/dist/taskCommitments.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
2
|
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
3
|
import { dirname, join } from 'node:path';
|
|
4
4
|
import { util } from '@evomap/evolver-core';
|
|
@@ -137,6 +137,11 @@ function commitmentExpired(claim, nowMs) {
|
|
|
137
137
|
function retryDelayMs(attempts) {
|
|
138
138
|
return Math.min(COMPLETION_RETRY_BASE_MS * 2 ** Math.max(0, attempts - 1), COMPLETION_RETRY_CAP_MS);
|
|
139
139
|
}
|
|
140
|
+
/** A dead owner cannot prove remote non-delivery, regardless of the retry count. */
|
|
141
|
+
function completionDeliveryUnknown(entry) {
|
|
142
|
+
return entry.dispatchState !== undefined
|
|
143
|
+
|| (entry.attempts === 0 && entry.lastError === 'completion_in_progress');
|
|
144
|
+
}
|
|
140
145
|
/** Queue ids the receiver writes carry this prefix, so the loop can tell hub work apart. */
|
|
141
146
|
export const HUB_QUEUE_PREFIX = 'hub-';
|
|
142
147
|
/** A task id becomes a filename, so it may only be what a filename may safely be. */
|
|
@@ -218,52 +223,101 @@ function clearUnqueued(path, queueTaskId) {
|
|
|
218
223
|
* Write the debt down, unless it is no longer owed. A report that failed while an
|
|
219
224
|
* overlapping one succeeded must not come back as a pending debt: the Hub already has the
|
|
220
225
|
* result, and re-sending it is how a settled task starts failing as `not_claimed`.
|
|
221
|
-
*
|
|
226
|
+
* A recorded refusal may retry, retire an obsolete dispatch, or exhaust its budget.
|
|
227
|
+
* A persistence failure is distinct from an obsolete response and is reported explicitly.
|
|
222
228
|
*/
|
|
223
|
-
function oweCompletion(ledgerPath,
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
//
|
|
228
|
-
//
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
229
|
+
function oweCompletion(ledgerPath, entry, error, nowMs) {
|
|
230
|
+
return withLedgerLock(ledgerPath, () => {
|
|
231
|
+
const ledger = readCompletionLedger(ledgerPath);
|
|
232
|
+
const current = ledger.pending[entry.taskId];
|
|
233
|
+
// This response owns only its exact write-ahead entry. A replaced debt is not
|
|
234
|
+
// its acknowledgement to record, even if it shares the same task id.
|
|
235
|
+
if (entry.generation === undefined || !sameDebt(current, entry) || current?.dispatchState !== 'in_progress')
|
|
236
|
+
return 'obsolete';
|
|
237
|
+
const owned = { ...(ledger.owned ?? {}) };
|
|
238
|
+
const pending = { ...ledger.pending };
|
|
239
|
+
const owner = owned[`${HUB_QUEUE_PREFIX}${entry.taskId}`];
|
|
240
|
+
// An actual returned refusal settles this old dispatch's ambiguity. If the
|
|
241
|
+
// claim moved on, retire only its matching marker, never the replacement claim.
|
|
242
|
+
if (owner?.generation !== entry.generation) {
|
|
243
|
+
delete pending[entry.taskId];
|
|
244
|
+
return replaceCompletionLedger(ledgerPath, { ...ledger, pending }) ? 'obsolete' : 'unrecorded';
|
|
245
|
+
}
|
|
246
|
+
const attempts = entry.attempts + 1;
|
|
247
|
+
if (attempts >= COMPLETION_MAX_ATTEMPTS) {
|
|
248
|
+
delete pending[entry.taskId];
|
|
249
|
+
delete owned[`${HUB_QUEUE_PREFIX}${entry.taskId}`];
|
|
250
|
+
return replaceCompletionLedger(ledgerPath, { ...ledger, pending, owned }) ? 'abandoned' : 'unrecorded';
|
|
251
|
+
}
|
|
252
|
+
const next = { ...entry, attempts, nextAttemptMs: nowMs + retryDelayMs(attempts), lastError: error };
|
|
253
|
+
delete next.dispatchState;
|
|
254
|
+
pending[entry.taskId] = next;
|
|
255
|
+
return replaceCompletionLedger(ledgerPath, { ...ledger, pending }) ? 'retryable' : 'unrecorded';
|
|
256
|
+
}, 'unrecorded');
|
|
234
257
|
}
|
|
235
258
|
/**
|
|
236
259
|
* What a wiring with no Hub client can still do: write the debt down. A task claimed before
|
|
237
260
|
* a restart finishes in a process that may have no identity left, and dropping the report
|
|
238
261
|
* there is the one outcome the Hub cannot recover from — it waits out the whole commitment.
|
|
239
262
|
*/
|
|
240
|
-
/** A
|
|
263
|
+
/** A returned refusal can retry; a throw does not prove whether the Hub accepted the POST. */
|
|
241
264
|
async function hubComplete(client, taskId, assetId) {
|
|
242
265
|
try {
|
|
243
266
|
const result = await client.completeTask(taskId, assetId);
|
|
244
267
|
return result.ok ? { ok: true } : { ok: false, error: result.error ?? `hub ${result.status ?? 0}` };
|
|
245
268
|
}
|
|
246
269
|
catch (error) {
|
|
247
|
-
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
270
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error), deliveryUnknown: true };
|
|
248
271
|
}
|
|
249
272
|
}
|
|
250
273
|
const NO_RETRIES = async () => ({ retried: 0, completed: 0, abandoned: 0, pending: 0, unrecorded: 0, abandonedTasks: [] });
|
|
251
274
|
const NO_QUEUE_RETRIES = async () => ({ retried: 0, queued: 0, pending: 0, dropped: 0 });
|
|
275
|
+
/** Fix a filing's result under the same lock as the ownership check, before any send. */
|
|
276
|
+
function reserveCompletion(path, taskId, resultAssetId, generation, nowMs, error, attempts) {
|
|
277
|
+
return withLedgerLock(path, () => {
|
|
278
|
+
const ledger = readCompletionLedger(path);
|
|
279
|
+
const owned = ledger.owned?.[`${HUB_QUEUE_PREFIX}${taskId}`];
|
|
280
|
+
if (!owned)
|
|
281
|
+
return { outcome: { taskId, completed: false, error: 'not_claimed' } };
|
|
282
|
+
if (generation === undefined || owned.generation !== generation) {
|
|
283
|
+
return { outcome: { taskId, completed: false, error: 'stale_generation' } };
|
|
284
|
+
}
|
|
285
|
+
const prior = ledger.pending[taskId];
|
|
286
|
+
if (prior && (prior.generation === undefined || completionDeliveryUnknown(prior) || prior.generation === generation)) {
|
|
287
|
+
return { outcome: { taskId, completed: false, pending: true,
|
|
288
|
+
...(prior.generation === undefined ? { error: 'generation_unbound' }
|
|
289
|
+
: prior.generation === generation && prior.resultAssetId !== resultAssetId ? { error: 'completion_result_conflict' }
|
|
290
|
+
: completionDeliveryUnknown(prior) ? { error: 'completion_in_progress' }
|
|
291
|
+
: prior.lastError ? { error: prior.lastError } : {}) } };
|
|
292
|
+
}
|
|
293
|
+
const entry = { taskId, resultAssetId, generation, attempts,
|
|
294
|
+
nextAttemptMs: nowMs + retryDelayMs(Math.max(attempts, 1)), lastError: error,
|
|
295
|
+
...(error === 'completion_in_progress' ? { dispatchState: 'in_progress' } : {}) };
|
|
296
|
+
return replaceCompletionLedger(path, { ...ledger, pending: { ...ledger.pending, [taskId]: entry } })
|
|
297
|
+
? { entry } : { outcome: { taskId, completed: false, error, unrecorded: true } };
|
|
298
|
+
}, { outcome: { taskId, completed: false, error, unrecorded: true } });
|
|
299
|
+
}
|
|
300
|
+
/** A live request owns its send through settlement; another beat cannot overlap it. */
|
|
301
|
+
function acquireCompletionDispatch(path, taskId) {
|
|
302
|
+
const lock = `${path}.complete-${createHash('sha256').update(taskId).digest('hex')}.lock`;
|
|
303
|
+
try {
|
|
304
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
305
|
+
return { path: lock, owner: util.acquireLock(lock, { maxTries: 1, waitMs: 0 }) };
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
252
311
|
export function unsentCommitments(ledgerPath, now) {
|
|
253
312
|
return {
|
|
254
313
|
claimFor: (queueTaskId) => readCompletionLedger(ledgerPath).owned?.[queueTaskId] ?? null,
|
|
255
314
|
complete: async (taskId, resultAssetId, generation) => {
|
|
256
315
|
// The same gate as the live wiring: a debt is only worth recording for a claim this
|
|
257
316
|
// node actually holds, under the filing that holds it.
|
|
258
|
-
const owned = readCompletionLedger(ledgerPath).owned?.[`${HUB_QUEUE_PREFIX}${taskId}`];
|
|
259
|
-
if (!owned)
|
|
260
|
-
return { taskId, completed: false, error: 'not_claimed' };
|
|
261
|
-
if (owned.generation !== generation)
|
|
262
|
-
return { taskId, completed: false, error: 'stale_generation' };
|
|
263
317
|
// Nothing can send this now, so writing it down is the whole job — and claiming a
|
|
264
318
|
// retry the ledger never recorded would promise a delivery no later run can make.
|
|
265
|
-
const
|
|
266
|
-
return { taskId, completed: false, error: 'task_receiver_disabled',
|
|
319
|
+
const reserved = reserveCompletion(ledgerPath, taskId, resultAssetId, generation, now(), 'task_receiver_disabled', 1);
|
|
320
|
+
return 'outcome' in reserved ? reserved.outcome : { taskId, completed: false, error: 'task_receiver_disabled', pending: true };
|
|
267
321
|
},
|
|
268
322
|
retryCompletions: NO_RETRIES,
|
|
269
323
|
retryQueueWrites: NO_QUEUE_RETRIES,
|
|
@@ -273,12 +327,18 @@ export function liveCommitments(client, deps) {
|
|
|
273
327
|
const { ledgerPath, now, queueDir } = deps;
|
|
274
328
|
// Clearing a debt is itself a ledger write, and a write that did not land leaves the
|
|
275
329
|
// record disagreeing with what the caller was told. `unrecorded` says so out loud.
|
|
276
|
-
// Clearing a debt is itself a ledger write, and a write that did not land leaves the
|
|
277
|
-
// record disagreeing with what the caller was told. `unrecorded` says so out loud.
|
|
278
330
|
// The claim only goes with it when it is the claim this debt was made under: a later
|
|
279
331
|
// filing shares the task id and is still held.
|
|
280
|
-
const settle = (taskId, outcome, generation) => {
|
|
332
|
+
const settle = (taskId, outcome, generation, onlyKnownDelivery = false) => {
|
|
333
|
+
let protectedDebt;
|
|
281
334
|
const persisted = updateCompletionLedger(ledgerPath, (ledger) => {
|
|
335
|
+
// Expiry or changed ownership is not a remote acknowledgement. Recheck the
|
|
336
|
+
// current debt under this write lock before releasing either it or its claim.
|
|
337
|
+
const debt = ledger.pending[taskId];
|
|
338
|
+
if (onlyKnownDelivery && debt && (debt.generation === undefined || completionDeliveryUnknown(debt))) {
|
|
339
|
+
protectedDebt = debt;
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
282
342
|
const queueTaskId = `${HUB_QUEUE_PREFIX}${taskId}`;
|
|
283
343
|
const owned = { ...(ledger.owned ?? {}) };
|
|
284
344
|
// The claim goes only when this debt can prove the claim is its own. A debt written
|
|
@@ -288,13 +348,15 @@ export function liveCommitments(client, deps) {
|
|
|
288
348
|
if (generation !== undefined && owned[queueTaskId]?.generation === generation)
|
|
289
349
|
delete owned[queueTaskId];
|
|
290
350
|
const pending = { ...ledger.pending };
|
|
291
|
-
//
|
|
292
|
-
//
|
|
293
|
-
|
|
294
|
-
if (debt && (debt.generation === undefined || debt.generation === generation))
|
|
351
|
+
// Only a bound debt belongs to this settlement. Legacy unbound evidence stays,
|
|
352
|
+
// as does any entry that names another filing.
|
|
353
|
+
if (debt?.generation !== undefined && debt.generation === generation)
|
|
295
354
|
delete pending[taskId];
|
|
296
355
|
return { ...ledger, pending, owned };
|
|
297
356
|
});
|
|
357
|
+
if (protectedDebt)
|
|
358
|
+
return { taskId, completed: false, pending: true,
|
|
359
|
+
error: protectedDebt.generation === undefined ? 'generation_unbound' : 'completion_in_progress' };
|
|
298
360
|
return persisted ? outcome : { ...outcome, unrecorded: true };
|
|
299
361
|
};
|
|
300
362
|
// A debt is only this attempt's to settle while the ledger still holds the entry the
|
|
@@ -303,61 +365,67 @@ export function liveCommitments(client, deps) {
|
|
|
303
365
|
const current = readCompletionLedger(ledgerPath).pending[entry.taskId];
|
|
304
366
|
return sameDebt(current, entry);
|
|
305
367
|
};
|
|
306
|
-
// A task the register has let go of has been settled — by this pass or an overlapping
|
|
307
|
-
// one — and its debt is paid. `held` is read before the Hub call, so a claim that was
|
|
308
|
-
// never registered (an old ledger) does not read as "settled" here.
|
|
309
|
-
// Settled, or moved on: either way this filing no longer holds the claim, and a debt
|
|
310
|
-
// recorded for it now would block the filing that does from recording its own.
|
|
311
|
-
const settledSince = (taskId, held, generation) => () => {
|
|
312
|
-
if (!held)
|
|
313
|
-
return false;
|
|
314
|
-
const owned = readCompletionLedger(ledgerPath).owned?.[`${HUB_QUEUE_PREFIX}${taskId}`];
|
|
315
|
-
return owned === undefined || owned.generation !== generation;
|
|
316
|
-
};
|
|
317
|
-
// Two reports for the same task can fail out of order. `before` is what the ledger held
|
|
318
|
-
// when this attempt started; anything else on record now is a later word than this one.
|
|
319
|
-
const supersededSince = (taskId, before) => () => {
|
|
320
|
-
const current = readCompletionLedger(ledgerPath).pending[taskId];
|
|
321
|
-
if (!current)
|
|
322
|
-
return false;
|
|
323
|
-
return !sameDebt(current, before);
|
|
324
|
-
};
|
|
325
|
-
const owe = (taskId, resultAssetId, error, settled, generation) => {
|
|
326
|
-
const recorded = oweCompletion(ledgerPath, taskId, resultAssetId, error, now(), settled, generation);
|
|
327
|
-
// Only a recorded debt is a promised retry; an unwritable ledger has to say so.
|
|
328
|
-
return { taskId, completed: false, error, ...(recorded ? { pending: true } : { unrecorded: true }) };
|
|
329
|
-
};
|
|
330
368
|
const report = async (taskId, resultAssetId, generation) => {
|
|
331
369
|
const opening = readCompletionLedger(ledgerPath);
|
|
332
370
|
const owned = opening.owned?.[`${HUB_QUEUE_PREFIX}${taskId}`];
|
|
333
|
-
const wasHeld = Boolean(owned);
|
|
334
371
|
const before = opening.pending[taskId];
|
|
335
372
|
// Only a claim this node registered may be completed, and only by the filing that
|
|
336
373
|
// holds it. No record, or a record from a later filing, and the result is not ours to
|
|
337
374
|
// hand over — the Hub cannot take a completion back once it has one.
|
|
338
375
|
if (!owned)
|
|
339
376
|
return { taskId, completed: false, error: 'not_claimed' };
|
|
340
|
-
if (owned.generation !== generation)
|
|
377
|
+
if (generation === undefined || owned.generation !== generation)
|
|
341
378
|
return { taskId, completed: false, error: 'stale_generation' };
|
|
379
|
+
if (before && (before.generation === undefined || completionDeliveryUnknown(before))) {
|
|
380
|
+
return { taskId, completed: false, pending: true,
|
|
381
|
+
error: before.generation === undefined ? 'generation_unbound'
|
|
382
|
+
: before.generation === generation && before.resultAssetId !== resultAssetId ? 'completion_result_conflict' : 'completion_in_progress' };
|
|
383
|
+
}
|
|
342
384
|
// The Hub holds a task for its claimant only until the commitment runs out. Past that
|
|
343
385
|
// it may have re-dispatched the work, and a late report would overwrite whoever now
|
|
344
386
|
// owns it — so stop, and name the orphaned asset loudly enough to be recoverable.
|
|
345
387
|
if (commitmentExpired(owned, now())) {
|
|
346
388
|
// This filing's commitment is over, so this filing's claim goes with it — scoped, so
|
|
347
389
|
// a re-claim that happened in the meantime keeps the claim it made.
|
|
348
|
-
return settle(taskId, { taskId, completed: false, error: 'commitment_expired' }, generation);
|
|
390
|
+
return settle(taskId, { taskId, completed: false, error: 'commitment_expired' }, generation, true);
|
|
391
|
+
}
|
|
392
|
+
// A filing owes one result. Changing its asset cannot replace an ambiguous
|
|
393
|
+
// completion or bypass backoff; only retryCompletions may send its saved debt.
|
|
394
|
+
if (before && (before.generation === undefined || before.generation === generation)) {
|
|
395
|
+
return { taskId, completed: false, pending: true,
|
|
396
|
+
...(before.generation === undefined ? { error: 'generation_unbound' }
|
|
397
|
+
: before.resultAssetId !== resultAssetId ? { error: 'completion_result_conflict' }
|
|
398
|
+
: before.lastError ? { error: before.lastError } : {}) };
|
|
399
|
+
}
|
|
400
|
+
const dispatch = acquireCompletionDispatch(ledgerPath, taskId);
|
|
401
|
+
if (!dispatch) {
|
|
402
|
+
const current = readCompletionLedger(ledgerPath).pending[taskId];
|
|
403
|
+
return { taskId, completed: false, ...(current ? { pending: true } : { unrecorded: true }),
|
|
404
|
+
error: current?.generation === generation && current.resultAssetId !== resultAssetId ? 'completion_result_conflict' : 'completion_busy' };
|
|
405
|
+
}
|
|
406
|
+
try {
|
|
407
|
+
const reserved = reserveCompletion(ledgerPath, taskId, resultAssetId, generation, now(), 'completion_in_progress', 0);
|
|
408
|
+
if ('outcome' in reserved)
|
|
409
|
+
return reserved.outcome;
|
|
410
|
+
const result = await hubComplete(client, taskId, resultAssetId);
|
|
411
|
+
// Keep the write-ahead marker on network/timeouts or unclassified adapter throws.
|
|
412
|
+
// Neither a released local lock nor an expired commitment proves non-delivery.
|
|
413
|
+
if (!result.ok && result.deliveryUnknown)
|
|
414
|
+
return { taskId, completed: false, pending: true, error: result.error };
|
|
415
|
+
// The Hub has this asset. Whatever this filing owed is paid, including a debt another
|
|
416
|
+
// attempt of the SAME filing recorded while we waited — leaving it would re-send a
|
|
417
|
+
// completed task. A later filing's debt is another claim's and stays.
|
|
418
|
+
if (result.ok)
|
|
419
|
+
return settle(taskId, { taskId, completed: true }, generation);
|
|
420
|
+
// The work is done and the Hub does not know it. Owe the report and retry it on
|
|
421
|
+
// later beats — otherwise a transient 429 silently costs us the whole delivery.
|
|
422
|
+
const recorded = oweCompletion(ledgerPath, reserved.entry, result.error, now());
|
|
423
|
+
return { taskId, completed: false, error: result.error,
|
|
424
|
+
...(recorded === 'retryable' ? { pending: true } : recorded === 'unrecorded' ? { unrecorded: true } : {}) };
|
|
425
|
+
}
|
|
426
|
+
finally {
|
|
427
|
+
util.releaseLock(dispatch.path, dispatch.owner);
|
|
349
428
|
}
|
|
350
|
-
const result = await hubComplete(client, taskId, resultAssetId);
|
|
351
|
-
const stale = settledSince(taskId, wasHeld, generation);
|
|
352
|
-
const superseded = supersededSince(taskId, before);
|
|
353
|
-
// The Hub has this asset. Whatever this filing owed is paid, including a debt another
|
|
354
|
-
// attempt of the SAME filing recorded while we waited — leaving it would re-send a
|
|
355
|
-
// completed task. A later filing's debt is another claim's and stays.
|
|
356
|
-
if (result.ok)
|
|
357
|
-
return settle(taskId, { taskId, completed: true }, generation);
|
|
358
|
-
// The work is done and the Hub does not know it. Owe the report and retry it on
|
|
359
|
-
// later beats — otherwise a transient 429 silently costs us the whole delivery.
|
|
360
|
-
return owe(taskId, resultAssetId, result.error, () => stale() || superseded(), generation);
|
|
361
429
|
};
|
|
362
430
|
return {
|
|
363
431
|
claimFor: (queueTaskId) => readCompletionLedger(ledgerPath).owned?.[queueTaskId] ?? null,
|
|
@@ -421,65 +489,91 @@ export function liveCommitments(client, deps) {
|
|
|
421
489
|
// Oldest due first, not insertion order: a fixed slice of an unsorted map lets the
|
|
422
490
|
// same five debts hold every slot forever while the ones behind them expire unsent.
|
|
423
491
|
const due = Object.values(ledger.pending)
|
|
424
|
-
.filter((entry) => entry.nextAttemptMs <= now())
|
|
492
|
+
.filter((entry) => entry.generation !== undefined && !completionDeliveryUnknown(entry) && entry.nextAttemptMs <= now())
|
|
425
493
|
.sort((left, right) => left.nextAttemptMs - right.nextAttemptMs)
|
|
426
494
|
.slice(0, COMPLETION_RETRIES_PER_TICK);
|
|
427
495
|
const out = { retried: 0, completed: 0, abandoned: 0, pending: Object.keys(ledger.pending).length, unrecorded: 0, abandonedTasks: [] };
|
|
428
496
|
for (const entry of due) {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
497
|
+
// Only known-unsent or response-settled, generation-bound debt is retryable.
|
|
498
|
+
// Unknown delivery and legacy evidence remain pending for authoritative reconciliation.
|
|
499
|
+
const dispatch = acquireCompletionDispatch(ledgerPath, entry.taskId);
|
|
500
|
+
if (!dispatch)
|
|
501
|
+
continue;
|
|
502
|
+
try {
|
|
432
503
|
if (!stillOurs(entry))
|
|
433
504
|
continue;
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
505
|
+
out.retried += 1;
|
|
506
|
+
const owned = readCompletionLedger(ledgerPath).owned?.[`${HUB_QUEUE_PREFIX}${entry.taskId}`];
|
|
507
|
+
if (commitmentExpired(owned, now())) {
|
|
508
|
+
if (!stillOurs(entry))
|
|
509
|
+
continue;
|
|
510
|
+
// A debt the ledger still holds has not been let go of, whatever this tick says.
|
|
511
|
+
const expired = settle(entry.taskId, { taskId: entry.taskId, completed: false }, entry.generation, true);
|
|
512
|
+
if (expired.pending) {
|
|
513
|
+
out.retried -= 1;
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
if (expired.unrecorded) {
|
|
517
|
+
out.unrecorded += 1;
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
out.abandoned += 1;
|
|
521
|
+
out.abandonedTasks.push({ taskId: entry.taskId, resultAssetId: entry.resultAssetId, lastError: 'commitment_expired' });
|
|
437
522
|
continue;
|
|
438
523
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
const attempts = entry.attempts + 1;
|
|
465
|
-
if (attempts >= COMPLETION_MAX_ATTEMPTS) {
|
|
466
|
-
if (settle(entry.taskId, { taskId: entry.taskId, completed: false }, entry.generation).unrecorded) {
|
|
524
|
+
// The same gate the first report passed: a debt with no claim behind it, or one
|
|
525
|
+
// whose claim moved on to a later filing, is not ours to send. Drop it, and leave
|
|
526
|
+
// any claim that is not this debt's alone — it is still held.
|
|
527
|
+
if (!owned || owned.generation !== entry.generation) {
|
|
528
|
+
if (stillOurs(entry)) {
|
|
529
|
+
const stale = settle(entry.taskId, { taskId: entry.taskId, completed: false }, entry.generation, true);
|
|
530
|
+
if (stale.pending)
|
|
531
|
+
out.retried -= 1;
|
|
532
|
+
}
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
// Reserve this exact known-unsent or response-settled retry under the lock. A snapshot
|
|
536
|
+
// selected before another process crashed must not revive its now-unknown send.
|
|
537
|
+
let marked;
|
|
538
|
+
const reserved = updateCompletionLedger(ledgerPath, (ledger) => {
|
|
539
|
+
const current = ledger.pending[entry.taskId];
|
|
540
|
+
const owner = ledger.owned?.[`${HUB_QUEUE_PREFIX}${entry.taskId}`];
|
|
541
|
+
if (!sameDebt(current, entry) || !current || completionDeliveryUnknown(current)
|
|
542
|
+
|| current.nextAttemptMs !== entry.nextAttemptMs || current.lastError !== entry.lastError
|
|
543
|
+
|| owner?.generation !== entry.generation || commitmentExpired(owner, now()))
|
|
544
|
+
return null;
|
|
545
|
+
marked = { ...current, dispatchState: 'in_progress' };
|
|
546
|
+
return { ...ledger, pending: { ...ledger.pending, [entry.taskId]: marked } };
|
|
547
|
+
});
|
|
548
|
+
if (!reserved) {
|
|
467
549
|
out.unrecorded += 1;
|
|
468
550
|
continue;
|
|
469
551
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
552
|
+
if (!marked)
|
|
553
|
+
continue;
|
|
554
|
+
const result = await hubComplete(client, entry.taskId, entry.resultAssetId);
|
|
555
|
+
// An unclassified throw leaves the actual dispatch unknown; do not spend a
|
|
556
|
+
// response-settled retry attempt or erase the marker. Other debts still drain.
|
|
557
|
+
if (!result.ok && result.deliveryUnknown)
|
|
558
|
+
continue;
|
|
559
|
+
if (result.ok) {
|
|
560
|
+
// The Hub has the result; the record must go even if a newer debt replaced it.
|
|
561
|
+
if (settle(entry.taskId, { taskId: entry.taskId, completed: true }, entry.generation).unrecorded)
|
|
562
|
+
out.unrecorded += 1;
|
|
563
|
+
out.completed += 1;
|
|
564
|
+
continue;
|
|
565
|
+
}
|
|
566
|
+
const recorded = oweCompletion(ledgerPath, marked, result.error, now());
|
|
567
|
+
if (recorded === 'unrecorded')
|
|
568
|
+
out.unrecorded += 1;
|
|
569
|
+
if (recorded === 'abandoned') {
|
|
570
|
+
out.abandoned += 1;
|
|
571
|
+
out.abandonedTasks.push({ taskId: entry.taskId, resultAssetId: entry.resultAssetId, lastError: result.error });
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
finally {
|
|
575
|
+
util.releaseLock(dispatch.path, dispatch.owner);
|
|
473
576
|
}
|
|
474
|
-
const written = updateCompletionLedger(ledgerPath, (ledger) => {
|
|
475
|
-
// Re-checked under the lock, not merely before it: the debt may have been
|
|
476
|
-
// settled or replaced between the check above and this write.
|
|
477
|
-
if (!sameDebt(ledger.pending[entry.taskId], entry))
|
|
478
|
-
return null;
|
|
479
|
-
return { ...ledger, pending: { ...ledger.pending, [entry.taskId]: { ...entry, attempts, nextAttemptMs: now() + retryDelayMs(attempts), lastError: result.error } } };
|
|
480
|
-
});
|
|
481
|
-
if (!written)
|
|
482
|
-
out.unrecorded += 1;
|
|
483
577
|
}
|
|
484
578
|
out.pending = Object.keys(readCompletionLedger(ledgerPath).pending).length;
|
|
485
579
|
return out;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type exec } from '@evomap/evolver-core';
|
|
2
|
+
import type { OutcomeReceipt, OutcomeReport } from '@evomap/evolver-adapter-public';
|
|
3
|
+
declare const SCHEMA_VERSION = "autoexec-task-outcome-outbox.v1";
|
|
4
|
+
export type TaskOutcomeLocalStatus = 'pending' | 'acknowledged';
|
|
5
|
+
export type TaskOutcomeNetworkStatus = 'pending' | 'ambiguous' | 'acknowledged' | 'terminal';
|
|
6
|
+
export type TaskOutcomeUseEvidence = Pick<exec.AppliedUseOutcome, 'schemaVersion' | 'assetId' | 'geneId' | 'cycleId' | 'source' | 'outcome' | 'producedValue' | 'applicationBinding' | 'verification' | 'hubOriginDestinationId'>;
|
|
7
|
+
export interface TaskOutcomeRecord {
|
|
8
|
+
schemaVersion: typeof SCHEMA_VERSION;
|
|
9
|
+
/** Derived from the queue claim root, so a reused task id gets a new operation. */
|
|
10
|
+
operationId: string;
|
|
11
|
+
claimRootDigest: string;
|
|
12
|
+
taskId: string;
|
|
13
|
+
generation?: string;
|
|
14
|
+
/** Nonsecret fingerprint of the effective Hub destination and principal. */
|
|
15
|
+
destinationId: string;
|
|
16
|
+
verdictStatus: exec.AutoExecVerdict['status'];
|
|
17
|
+
/** A committed bridge observation; required before task-wide Hub learning. */
|
|
18
|
+
agentInvoked: true;
|
|
19
|
+
producedValue?: boolean;
|
|
20
|
+
appliedUse?: TaskOutcomeUseEvidence;
|
|
21
|
+
report: OutcomeReport;
|
|
22
|
+
payloadDigest: string;
|
|
23
|
+
localStatus: TaskOutcomeLocalStatus;
|
|
24
|
+
networkStatus: TaskOutcomeNetworkStatus;
|
|
25
|
+
revision: number;
|
|
26
|
+
preparedAt: string;
|
|
27
|
+
localAcknowledgedAt?: string;
|
|
28
|
+
dispatchStartedAt?: string;
|
|
29
|
+
settledAt?: string;
|
|
30
|
+
terminalReason?: string;
|
|
31
|
+
ambiguousReason?: string;
|
|
32
|
+
reconciliationEvidence?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface PrepareTaskOutcomeInput {
|
|
35
|
+
claimRoot: string;
|
|
36
|
+
destinationId: string;
|
|
37
|
+
task: Pick<exec.AutoExecTask, 'id' | 'signals' | 'generation'>;
|
|
38
|
+
verdict: Pick<exec.AutoExecVerdict, 'taskId' | 'status' | 'generation' | 'outcome' | 'producedValue' | 'appliedUse' | 'agentInvoked' | 'failureKind' | 'execution_terminal'>;
|
|
39
|
+
}
|
|
40
|
+
/** The caller supplies the actual Hub adapter's recordOutcome method. */
|
|
41
|
+
export type TaskOutcomeSender = (report: Readonly<OutcomeReport>) => Promise<OutcomeReceipt>;
|
|
42
|
+
export type TaskOutcomeReconciliation = {
|
|
43
|
+
status: 'acknowledged';
|
|
44
|
+
evidence: string;
|
|
45
|
+
} | {
|
|
46
|
+
status: 'terminal';
|
|
47
|
+
evidence: string;
|
|
48
|
+
reason: string;
|
|
49
|
+
};
|
|
50
|
+
export declare class TaskOutcomeOutboxError extends Error {
|
|
51
|
+
constructor(message: string);
|
|
52
|
+
}
|
|
53
|
+
export declare function taskOutcomeIdentity(claimRoot: string): {
|
|
54
|
+
operationId: string;
|
|
55
|
+
claimRootDigest: string;
|
|
56
|
+
};
|
|
57
|
+
export declare class TaskOutcomeOutbox {
|
|
58
|
+
readonly dir: string;
|
|
59
|
+
private readonly directory;
|
|
60
|
+
private readonly reportedUnreadable;
|
|
61
|
+
constructor(dir: string);
|
|
62
|
+
private path;
|
|
63
|
+
get(operationId: string): Readonly<TaskOutcomeRecord> | null;
|
|
64
|
+
private save;
|
|
65
|
+
/** Called after the queue claim receipt is durable and before the claim is archived. */
|
|
66
|
+
prepare(input: PrepareTaskOutcomeInput): Readonly<TaskOutcomeRecord> | null;
|
|
67
|
+
private rows;
|
|
68
|
+
localPending(): Readonly<TaskOutcomeRecord>[];
|
|
69
|
+
/** Acknowledge only after the queue's local receipt is committed. */
|
|
70
|
+
acknowledgeLocal(operationId: string): Readonly<TaskOutcomeRecord>;
|
|
71
|
+
pending(destinationId?: string): Readonly<TaskOutcomeRecord>[];
|
|
72
|
+
/** An explicit false receipt is terminal; an indeterminate send remains ambiguous. */
|
|
73
|
+
deliver(operationId: string, destinationId: string, sender: TaskOutcomeSender): Promise<Readonly<TaskOutcomeRecord>>;
|
|
74
|
+
/** A later receipt or operator evidence can settle ambiguity without resending. */
|
|
75
|
+
reconcile(operationId: string, resolution: TaskOutcomeReconciliation): Readonly<TaskOutcomeRecord>;
|
|
76
|
+
/** Skip rows bound to another destination; never retry started or settled sends. */
|
|
77
|
+
flush(destinationId: string, sender: TaskOutcomeSender): Promise<{
|
|
78
|
+
delivered: number;
|
|
79
|
+
terminal: number;
|
|
80
|
+
ambiguous: number;
|
|
81
|
+
}>;
|
|
82
|
+
}
|
|
83
|
+
export {};
|