@evomap/evolver-cli 2.0.46 → 2.0.47

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 (53) hide show
  1. package/dist/autoexec.d.ts +1 -2
  2. package/dist/autoexec.js +2 -7
  3. package/dist/cliEntry.js +8 -0
  4. package/dist/commandCatalog.d.ts +4 -0
  5. package/dist/commandCatalog.js +4 -0
  6. package/dist/dispatch.js +8 -0
  7. package/dist/experience.d.ts +2 -0
  8. package/dist/experience.js +229 -0
  9. package/dist/feedbackDestinationIdentity.d.ts +1 -0
  10. package/dist/feedbackDestinationIdentity.js +8 -0
  11. package/dist/kernelTriggerFeedback.d.ts +13 -0
  12. package/dist/kernelTriggerFeedback.js +65 -0
  13. package/dist/lifecycle.js +4 -1
  14. package/dist/lifecycleBootstrapTransaction.js +13 -7
  15. package/dist/localMemoryGraph.d.ts +38 -0
  16. package/dist/localMemoryGraph.js +192 -7
  17. package/dist/localTriggerFeedback.d.ts +30 -0
  18. package/dist/localTriggerFeedback.js +126 -0
  19. package/dist/mcpStdio.d.ts +1 -0
  20. package/dist/mcpStdio.js +9 -0
  21. package/dist/plugin.d.ts +8 -0
  22. package/dist/plugin.js +132 -0
  23. package/dist/pluginBundle.d.ts +3 -0
  24. package/dist/pluginBundle.js +94 -0
  25. package/dist/pluginHosts.d.ts +36 -0
  26. package/dist/pluginHosts.js +120 -0
  27. package/dist/pluginInspection.d.ts +22 -0
  28. package/dist/pluginInspection.js +237 -0
  29. package/dist/pluginInstall.d.ts +19 -0
  30. package/dist/pluginInstall.js +84 -0
  31. package/dist/pluginLock.d.ts +4 -0
  32. package/dist/pluginLock.js +52 -0
  33. package/dist/pluginRuntime.d.ts +31 -0
  34. package/dist/pluginRuntime.js +191 -0
  35. package/dist/recipe.d.ts +1 -1
  36. package/dist/recipe.js +14 -5
  37. package/dist/setupHooks.js +5 -3
  38. package/dist/setupHooksLifecycle.d.ts +2 -0
  39. package/dist/setupHooksLifecycle.js +52 -52
  40. package/dist/sourceQualification.js +6 -2
  41. package/dist/triggerFeedbackCli.d.ts +15 -0
  42. package/dist/triggerFeedbackCli.js +146 -0
  43. package/dist/triggerFeedbackDelivery.d.ts +9 -0
  44. package/dist/triggerFeedbackDelivery.js +29 -0
  45. package/dist/triggerFeedbackHub.d.ts +7 -0
  46. package/dist/triggerFeedbackHub.js +62 -0
  47. package/dist/triggerFeedbackRecovery.d.ts +15 -0
  48. package/dist/triggerFeedbackRecovery.js +124 -0
  49. package/dist/triggerFeedbackSync.d.ts +23 -0
  50. package/dist/triggerFeedbackSync.js +98 -0
  51. package/dist/triggerFeedbackSyncState.d.ts +42 -0
  52. package/dist/triggerFeedbackSyncState.js +112 -0
  53. package/package.json +7 -7
@@ -0,0 +1,146 @@
1
+ import { join } from 'node:path';
2
+ import { algo } from '@evomap/evolver-core';
3
+ import { LocalMemoryGraph, resolveLocalMemoryUserIdentity } from './localMemoryGraph.js';
4
+ import { kernelTriggerFeedbackBinding, readKernelTriggerFeedback } from './kernelTriggerFeedback.js';
5
+ import { createBoundTriggerFeedbackHub } from './triggerFeedbackHub.js';
6
+ import { synchronizeTriggerFeedback } from './triggerFeedbackSync.js';
7
+ import { recoverTriggerFeedback } from './triggerFeedbackRecovery.js';
8
+ const USAGE = 'evolver trigger-feedback (sync|get) --run-id <id> --json [--hub|--local-only] [--minimum-revision <n> --operation-id <id> --trigger-id <id>]\nevolver trigger-feedback recover --json [--local-only]\n';
9
+ export async function runTriggerFeedbackCommand(argv, deps = {}) {
10
+ const stdout = deps.stdout ?? ((text) => { process.stdout.write(text); });
11
+ const stderr = deps.stderr ?? ((text) => { process.stderr.write(text); });
12
+ if (argv.length === 1 && (argv[0] === '--help' || argv[0] === '-h')) {
13
+ stdout(USAGE);
14
+ return 0;
15
+ }
16
+ const command = argv[0];
17
+ let runId;
18
+ let json = false;
19
+ let syncHub = false;
20
+ let localOnly = false;
21
+ let receiptOnly = false;
22
+ let minimumRevision;
23
+ let operationId;
24
+ let triggerId;
25
+ for (let i = 1; i < argv.length; i++) {
26
+ const flag = argv[i];
27
+ if (flag === '--json' && !json)
28
+ json = true;
29
+ else if (flag === '--hub' && !syncHub)
30
+ syncHub = true;
31
+ else if (flag === '--local-only' && !localOnly)
32
+ localOnly = true;
33
+ else if (flag === '--receipt-only' && !receiptOnly)
34
+ receiptOnly = true;
35
+ else if (flag === '--run-id' && runId === undefined && argv[i + 1] && !argv[i + 1].startsWith('--'))
36
+ runId = argv[++i];
37
+ else if (flag === '--minimum-revision' && minimumRevision === undefined && argv[i + 1])
38
+ minimumRevision = Number(argv[++i]);
39
+ else if (flag === '--operation-id' && operationId === undefined && argv[i + 1])
40
+ operationId = argv[++i];
41
+ else if (flag === '--trigger-id' && triggerId === undefined && argv[i + 1])
42
+ triggerId = argv[++i];
43
+ else {
44
+ stderr(USAGE);
45
+ return 2;
46
+ }
47
+ }
48
+ const guarded = minimumRevision !== undefined || operationId !== undefined || triggerId !== undefined;
49
+ if (!json || (receiptOnly && command !== 'get') || (syncHub && (command !== 'sync' || localOnly))
50
+ || (command === 'recover' ? runId !== undefined || guarded : (command !== 'sync' && command !== 'get') || !runId?.trim() || Buffer.byteLength(runId) > 256)) {
51
+ stderr(USAGE);
52
+ return 2;
53
+ }
54
+ if (guarded && (!Number.isSafeInteger(minimumRevision) || minimumRevision < 1 || !operationId?.trim() || !triggerId?.trim()
55
+ || [operationId, triggerId].some((value) => value.startsWith('--') || Buffer.byteLength(value) > 256))) {
56
+ stderr(USAGE);
57
+ return 2;
58
+ }
59
+ const requireReceipt = (snapshot) => {
60
+ if (guarded && (!snapshot.commit || snapshot.triggerId !== triggerId || snapshot.commit.revision < minimumRevision
61
+ || (snapshot.commit.revision === minimumRevision && snapshot.commit.operationId !== operationId)))
62
+ throw new Error('trigger_feedback_source_mismatch');
63
+ };
64
+ let graph;
65
+ try {
66
+ if (command === 'recover') {
67
+ const report = await recoverTriggerFeedback(deps, localOnly);
68
+ stdout(JSON.stringify(report) + '\n');
69
+ return report.ok ? 0 : 1;
70
+ }
71
+ const env = { ...(deps.env ?? process.env) };
72
+ const binding = kernelTriggerFeedbackBinding(env);
73
+ const id = runId;
74
+ const dir = join(binding.agentDir, 'evolver', 'evolution');
75
+ graph = deps.createGraph?.(dir) ?? new LocalMemoryGraph({ dir, ...resolveLocalMemoryUserIdentity(dir) });
76
+ let snapshot = await (deps.readKernel ?? readKernelTriggerFeedback)(binding, id);
77
+ requireReceipt(snapshot);
78
+ let transport;
79
+ let remote;
80
+ if (syncHub) {
81
+ try {
82
+ transport = (deps.createRemote ?? createBoundTriggerFeedbackHub)(binding, env);
83
+ }
84
+ catch (error) {
85
+ remote = { status: 'unverified', reason: safeReason(error, 'trigger_feedback_destination_unbound') };
86
+ }
87
+ snapshot = await (deps.readKernel ?? readKernelTriggerFeedback)(binding, id);
88
+ requireReceipt(snapshot);
89
+ }
90
+ let local = command === 'sync' ? graph.applyKernelTriggerFeedback(snapshot, transport?.destinationId) : graph.readTriggerFeedback(id);
91
+ const captured = graph.readTriggerFeedback(id);
92
+ if (captured)
93
+ snapshot = algo.updateTriggerFeedbackState(captured, snapshot).snapshot;
94
+ const delivery = graph.readTriggerFeedbackDelivery(id);
95
+ const remoteRequired = syncHub || delivery !== null;
96
+ if (command === 'sync' && remoteRequired && !localOnly && !remote) {
97
+ try {
98
+ transport ??= (deps.createRemote ?? createBoundTriggerFeedbackHub)(binding, env);
99
+ const synchronized = await synchronizeTriggerFeedback({ snapshot, graph, remote: transport,
100
+ readKernel: () => (deps.readKernel ?? readKernelTriggerFeedback)(binding, id), requireReceipt,
101
+ local: local, force: syncHub });
102
+ snapshot = synchronized.snapshot;
103
+ local = synchronized.local;
104
+ remote = synchronized.remote;
105
+ }
106
+ catch (error) {
107
+ remote = { status: 'unverified', reason: safeReason(error, 'trigger_feedback_hub_unverified') };
108
+ }
109
+ }
110
+ else if (remoteRequired && localOnly) {
111
+ remote = { status: 'unverified', reason: 'trigger_feedback_publish_suspended' };
112
+ }
113
+ else if (command === 'get' && delivery) {
114
+ try {
115
+ transport ??= (deps.createRemote ?? createBoundTriggerFeedbackHub)(binding, env);
116
+ remote = transport.isCurrent() && transport.destinationId === delivery.destinationId && delivery.status === 'confirmed'
117
+ && delivery.acknowledgedRevision === (snapshot.commit?.revision ?? 0)
118
+ ? { status: 'confirmed', from: 'durable-receipt' } : { status: 'unverified', reason: delivery.reason ?? 'trigger_feedback_hub_unverified' };
119
+ }
120
+ catch (error) {
121
+ remote = { status: 'unverified', reason: safeReason(error, 'trigger_feedback_destination_unbound') };
122
+ }
123
+ }
124
+ const ok = command === 'get' || localOnly || remote === undefined || remote.status === 'confirmed';
125
+ const localCommitted = command === 'sync' || Boolean(local && 'snapshot' in local && JSON.stringify(local.snapshot) === JSON.stringify(snapshot));
126
+ const outputLocal = receiptOnly && local && 'snapshot' in local ? { committed: local.snapshot.commit } : local;
127
+ stdout(JSON.stringify({ ok, schemaVersion: 1, runId: id, command, canonical: snapshot.commit, local: outputLocal, localCommitted,
128
+ sourceAvailable: graph.triggerFeedbackSourceAvailable(id), remoteRequired, remote,
129
+ delivery: graph.readTriggerFeedbackDelivery(id), effectiveScore: algo.triggerEffectiveScore(snapshot) }) + '\n');
130
+ return ok ? 0 : 1;
131
+ }
132
+ catch (error) {
133
+ if (graph && runId) {
134
+ try {
135
+ graph.markTriggerFeedbackSourceUnavailable(runId);
136
+ }
137
+ catch { /* Preserve the original failure and existing records. */ }
138
+ }
139
+ const reason = error instanceof algo.TriggerFeedbackConflictError ? error.code : safeReason(error, 'trigger_feedback_local_unverified');
140
+ stdout(JSON.stringify({ ok: false, command, runId, reason }) + '\n');
141
+ return 1;
142
+ }
143
+ }
144
+ function safeReason(error, fallback) {
145
+ return error instanceof Error && /^trigger_feedback_[a-z_]+$/.test(error.message) ? error.message : fallback;
146
+ }
@@ -0,0 +1,9 @@
1
+ import { hub, type algo } from '@evomap/evolver-core';
2
+ export type TriggerFeedbackDelivery = {
3
+ status: 'confirmed';
4
+ receipt: hub.TriggerFeedbackReceipt;
5
+ } | {
6
+ status: 'unsupported' | 'rejected' | 'unverified';
7
+ reason: string;
8
+ };
9
+ export declare function deliverTriggerFeedback(snapshot: algo.TriggerFeedbackSnapshot, remote: hub.TriggerFeedbackCapability): Promise<TriggerFeedbackDelivery>;
@@ -0,0 +1,29 @@
1
+ import { hub } from '@evomap/evolver-core';
2
+ import { HubClientError, AuthError } from '@evomap/evolver-adapter-public';
3
+ export async function deliverTriggerFeedback(snapshot, remote) {
4
+ const report = hub.triggerFeedbackReport(snapshot);
5
+ try {
6
+ const receipt = await remote.commit(report);
7
+ if (!hub.triggerFeedbackReceiptMatches(receipt, report))
8
+ throw new Error('trigger_feedback_receipt_unconfirmed');
9
+ return { status: 'confirmed', receipt };
10
+ }
11
+ catch (error) {
12
+ if (error instanceof Error && error.message === 'trigger_feedback_egress_blocked')
13
+ return { status: 'rejected', reason: error.message };
14
+ if (error instanceof HubClientError && (error.status === 404 || error.status === 405))
15
+ return { status: 'unsupported', reason: 'trigger_feedback_hub_unsupported' };
16
+ if (error instanceof AuthError || (error instanceof HubClientError && error.status >= 400 && error.status < 500)) {
17
+ return { status: 'rejected', reason: 'trigger_feedback_hub_rejected' };
18
+ }
19
+ try {
20
+ const receipt = await remote.get(snapshot.runId);
21
+ if (hub.triggerFeedbackReceiptMatches(receipt, report))
22
+ return { status: 'confirmed', receipt };
23
+ }
24
+ catch {
25
+ // An unavailable read is not evidence that the mutation did not commit.
26
+ }
27
+ return { status: 'unverified', reason: 'trigger_feedback_hub_unverified' };
28
+ }
29
+ }
@@ -0,0 +1,7 @@
1
+ import { hub } from '@evomap/evolver-core';
2
+ import type { KernelTriggerFeedbackBinding } from './kernelTriggerFeedback.js';
3
+ export interface BoundTriggerFeedbackCapability extends hub.TriggerFeedbackCapability {
4
+ readonly destinationId: string;
5
+ isCurrent(): boolean;
6
+ }
7
+ export declare function createBoundTriggerFeedbackHub(binding: KernelTriggerFeedbackBinding, env: Readonly<Record<string, string | undefined>>, signal?: AbortSignal): BoundTriggerFeedbackCapability;
@@ -0,0 +1,62 @@
1
+ import { join } from 'node:path';
2
+ import { hub } from '@evomap/evolver-core';
3
+ import { awaitWithAbort, connectPublicHub, globalFetchLike, resolveHubUrl } from '@evomap/evolver-adapter-public';
4
+ import { createRecipeHubFromEnv, resolveRecipeHubResumeIdentityFingerprint } from './recipe.js';
5
+ import { feedbackDestinationIdentity } from './feedbackDestinationIdentity.js';
6
+ export function createBoundTriggerFeedbackHub(binding, env, signal) {
7
+ if (![env['A2A_HUB_URL'], env['EVOMAP_HUB_URL']].some((value) => value?.trim()))
8
+ throw new Error('trigger_feedback_destination_unbound');
9
+ const evolverHome = join(binding.agentDir, 'evolver');
10
+ const evomapHome = join(binding.agentDir, 'evomap');
11
+ const scoped = {
12
+ ...env, EVOLVER_ENV_FILE: '', EVOLVER_HOME: evolverHome, EVOMAP_HOME: evomapHome, EVOMAP_DIR: evomapHome,
13
+ EVOLVER_PROXY_STORE: join(evolverHome, 'proxy', 'mailbox.db'),
14
+ };
15
+ const identify = () => {
16
+ const identity = resolveRecipeHubResumeIdentityFingerprint(scoped, true);
17
+ const storedEnv = { ...scoped };
18
+ for (const key of ['A2A_NODE_ID', 'EVOMAP_NODE_ID', 'A2A_NODE_SECRET', 'EVOMAP_NODE_SECRET', 'A2A_NODE_SECRET_VERSION', 'EVOMAP_NODE_SECRET_VERSION'])
19
+ delete storedEnv[key];
20
+ let storedIdentity = null;
21
+ try {
22
+ storedIdentity = resolveRecipeHubResumeIdentityFingerprint(storedEnv, true);
23
+ }
24
+ catch { /* An explicit environment-only identity has no stored counterpart. */ }
25
+ return feedbackDestinationIdentity('public', resolveHubUrl(scoped), identity, storedIdentity ?? 'environment-only');
26
+ };
27
+ const destinationId = identify();
28
+ const connected = createRecipeHubFromEnv(scoped, (options) => connectPublicHub({ ...options,
29
+ fetchFn: async (url, init) => {
30
+ signal?.throwIfAborted();
31
+ const requestSignal = signal ? AbortSignal.any([signal, ...(init.signal ? [init.signal] : [])]) : init.signal;
32
+ return await awaitWithAbort(globalFetchLike(url, { ...init, ...(requestSignal ? { signal: requestSignal } : {}) }), signal);
33
+ },
34
+ }));
35
+ if (!('getNodeSecretVersion' in connected.auth))
36
+ throw new Error('trigger_feedback_node_auth_required');
37
+ const capability = connected.triggerFeedback;
38
+ const isCurrent = () => { try {
39
+ return identify() === destinationId;
40
+ }
41
+ catch {
42
+ return false;
43
+ } };
44
+ const assertCurrent = () => { signal?.throwIfAborted(); if (!isCurrent())
45
+ throw new Error('trigger_feedback_destination_changed'); };
46
+ const assertSafePayload = async (payload, path) => {
47
+ assertCurrent();
48
+ const content = JSON.stringify(payload);
49
+ const credentials = await awaitWithAbort(connected.auth.authenticate({ method: 'POST', path, body: content, ...(signal ? { signal } : {}) }), signal);
50
+ assertCurrent();
51
+ const secret = credentials.bodyFields?.['node_secret'];
52
+ const known = { ...scoped, ...(typeof secret === 'string' ? { EVOMAP_FEEDBACK_AUTH_SECRET: secret } : {}) };
53
+ const folded = Object.fromEntries(Object.entries(known).map(([key, value]) => [key, value?.toLowerCase()]));
54
+ if (hub.fullLeakCheck(content, known).found || hub.fullLeakCheck(content.toLowerCase(), folded).found)
55
+ throw new Error('trigger_feedback_egress_blocked');
56
+ };
57
+ return {
58
+ destinationId, isCurrent,
59
+ async commit(report) { const normalized = hub.normalizeTriggerFeedbackReport(report); await assertSafePayload({ ...normalized }, '/a2a/trigger-feedback'); assertCurrent(); return await awaitWithAbort(capability.commit(normalized), signal); },
60
+ async get(runId) { await assertSafePayload({ run_id: runId }, '/a2a/trigger-feedback/get'); assertCurrent(); return await awaitWithAbort(capability.get(runId), signal); },
61
+ };
62
+ }
@@ -0,0 +1,15 @@
1
+ import type { TriggerFeedbackCommandDeps } from './triggerFeedbackCli.js';
2
+ export interface TriggerFeedbackRecoveryReport {
3
+ ok: boolean;
4
+ schemaVersion: 1;
5
+ command: 'recover';
6
+ localCommitted: boolean;
7
+ scanned: number;
8
+ localChanged: number;
9
+ hubConfirmed: number;
10
+ pending: number;
11
+ blocked: number;
12
+ partial: boolean;
13
+ reasons: Record<string, number>;
14
+ }
15
+ export declare function recoverTriggerFeedback(deps: TriggerFeedbackCommandDeps, localOnly: boolean): Promise<TriggerFeedbackRecoveryReport>;
@@ -0,0 +1,124 @@
1
+ import { join } from 'node:path';
2
+ import { awaitWithAbort } from '@evomap/evolver-adapter-public';
3
+ import { LocalMemoryGraph, resolveLocalMemoryUserIdentity } from './localMemoryGraph.js';
4
+ import { kernelTriggerFeedbackBinding, listKernelTriggerFeedback, readKernelTriggerFeedback } from './kernelTriggerFeedback.js';
5
+ import { createBoundTriggerFeedbackHub } from './triggerFeedbackHub.js';
6
+ import { synchronizeTriggerFeedback } from './triggerFeedbackSync.js';
7
+ export async function recoverTriggerFeedback(deps, localOnly) {
8
+ const now = deps.now ?? Date.now;
9
+ const deadline = now() + 20_000;
10
+ const controller = new AbortController();
11
+ const expired = new Error('trigger_feedback_deadline_exceeded');
12
+ const timer = setTimeout(() => controller.abort(expired), 20_000);
13
+ const check = () => {
14
+ if (now() >= deadline && !controller.signal.aborted)
15
+ controller.abort(expired);
16
+ controller.signal.throwIfAborted();
17
+ };
18
+ const budget = { signal: controller.signal, check,
19
+ async run(work) {
20
+ check();
21
+ const value = await awaitWithAbort(work(), controller.signal);
22
+ check();
23
+ return value;
24
+ },
25
+ };
26
+ try {
27
+ return await recoverWithinBudget(deps, localOnly, budget);
28
+ }
29
+ finally {
30
+ clearTimeout(timer);
31
+ }
32
+ }
33
+ async function recoverWithinBudget(deps, localOnly, budget) {
34
+ const env = { ...(deps.env ?? process.env) };
35
+ const binding = kernelTriggerFeedbackBinding(env);
36
+ const dir = join(binding.agentDir, 'evolver', 'evolution');
37
+ const graph = deps.createGraph?.(dir) ?? new LocalMemoryGraph({ dir, ...resolveLocalMemoryUserIdentity(dir) });
38
+ const report = { ok: true, schemaVersion: 1, command: 'recover', localCommitted: false,
39
+ scanned: 0, localChanged: 0, hubConfirmed: 0, pending: 0, blocked: 0, partial: false, reasons: {} };
40
+ const failure = (error) => {
41
+ const reason = error instanceof Error && /^trigger_feedback_[a-z_]+$/.test(error.message) ? error.message : 'trigger_feedback_recovery_unverified';
42
+ report.reasons[reason] = (report.reasons[reason] ?? 0) + 1;
43
+ report.ok = false;
44
+ report.partial ||= budget.signal.aborted;
45
+ };
46
+ let after = graph.readTriggerFeedbackRecoveryCursor();
47
+ try {
48
+ const seen = new Set();
49
+ for (let pageIndex = 0; pageIndex < 8; pageIndex++) {
50
+ const page = await budget.run(() => (deps.listKernel ?? listKernelTriggerFeedback)(binding, after, budget.signal));
51
+ if (page.snapshots.length > 100 || (page.nextAfter !== null && (page.snapshots.length === 0 || page.nextAfter === after || seen.has(page.nextAfter))))
52
+ throw new Error('trigger_feedback_kernel_invalid_output');
53
+ if (page.snapshots.length > 0) {
54
+ const local = graph.applyKernelTriggerFeedbackBatch(page.snapshots);
55
+ report.scanned += page.snapshots.length;
56
+ report.localChanged += local.filter((value) => value.changed).length;
57
+ report.localCommitted = true;
58
+ budget.check();
59
+ graph.saveTriggerFeedbackRecoveryCursor(page.nextAfter);
60
+ }
61
+ else if (after !== null) {
62
+ graph.saveTriggerFeedbackRecoveryCursor(null);
63
+ }
64
+ after = page.nextAfter;
65
+ report.partial = after !== null;
66
+ if (after === null)
67
+ break;
68
+ seen.add(after);
69
+ }
70
+ budget.check();
71
+ }
72
+ catch (error) {
73
+ if (!budget.signal.aborted)
74
+ graph.markTriggerFeedbackSourceUnavailable();
75
+ failure(error);
76
+ return report;
77
+ }
78
+ if (localOnly)
79
+ return report;
80
+ let cursor = graph.readTriggerFeedbackRecoveryCursor('delivery');
81
+ let debt = graph.readTriggerFeedbackDeliveries(cursor, 25);
82
+ if (debt.length === 0 && cursor !== null) {
83
+ cursor = null;
84
+ debt = graph.readTriggerFeedbackDeliveries(null, 25);
85
+ }
86
+ if (debt.length === 0)
87
+ return report;
88
+ let remote;
89
+ try {
90
+ budget.check();
91
+ remote = (deps.createRemote ?? createBoundTriggerFeedbackHub)(binding, env, budget.signal);
92
+ }
93
+ catch (error) {
94
+ failure(error);
95
+ report.blocked += debt.length;
96
+ return report;
97
+ }
98
+ for (const row of debt) {
99
+ const readKernel = () => budget.run(() => (deps.readKernel ?? readKernelTriggerFeedback)(binding, row.snapshot.runId, budget.signal));
100
+ try {
101
+ // Cancelling the waiter retains the persisted claim; late work stays signal-fenced, not graph-closed.
102
+ const result = await budget.run(() => synchronizeTriggerFeedback({ snapshot: row.snapshot, graph, remote, readKernel, signal: budget.signal }));
103
+ if (result.remote.status === 'confirmed')
104
+ report.hubConfirmed++;
105
+ else {
106
+ if (graph.readTriggerFeedbackDelivery(row.snapshot.runId)?.status === 'blocked')
107
+ report.blocked++;
108
+ else
109
+ report.pending++;
110
+ failure(new Error(result.remote.reason ?? 'trigger_feedback_hub_unverified'));
111
+ }
112
+ }
113
+ catch (error) {
114
+ if (!budget.signal.aborted)
115
+ graph.markTriggerFeedbackSourceUnavailable(row.snapshot.runId);
116
+ failure(error);
117
+ report.pending++;
118
+ }
119
+ if (budget.signal.aborted)
120
+ break;
121
+ graph.saveTriggerFeedbackRecoveryCursor(row.snapshot.runId, 'delivery');
122
+ }
123
+ return report;
124
+ }
@@ -0,0 +1,23 @@
1
+ import { algo, hub } from '@evomap/evolver-core';
2
+ import { LocalMemoryGraph } from './localMemoryGraph.js';
3
+ import type { BoundTriggerFeedbackCapability } from './triggerFeedbackHub.js';
4
+ export interface TriggerFeedbackSyncResult {
5
+ snapshot: algo.TriggerFeedbackSnapshot;
6
+ local: ReturnType<LocalMemoryGraph['applyKernelTriggerFeedback']>;
7
+ remote: {
8
+ status: 'confirmed' | 'unsupported' | 'rejected' | 'unverified';
9
+ reason?: string;
10
+ receipt?: hub.TriggerFeedbackReceipt;
11
+ from?: 'durable-receipt';
12
+ };
13
+ }
14
+ export declare function synchronizeTriggerFeedback(options: {
15
+ snapshot: algo.TriggerFeedbackSnapshot;
16
+ graph: LocalMemoryGraph;
17
+ remote: BoundTriggerFeedbackCapability;
18
+ readKernel: () => Promise<algo.TriggerFeedbackSnapshot>;
19
+ local?: ReturnType<LocalMemoryGraph['applyKernelTriggerFeedback']>;
20
+ requireReceipt?: (snapshot: algo.TriggerFeedbackSnapshot) => void;
21
+ force?: boolean;
22
+ signal?: AbortSignal;
23
+ }): Promise<TriggerFeedbackSyncResult>;
@@ -0,0 +1,98 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { algo, hub } from '@evomap/evolver-core';
3
+ import { HubClientError } from '@evomap/evolver-adapter-public';
4
+ import { LocalMemoryGraph } from './localMemoryGraph.js';
5
+ import { deliverTriggerFeedback } from './triggerFeedbackDelivery.js';
6
+ export async function synchronizeTriggerFeedback(options) {
7
+ const { graph, remote } = options;
8
+ let snapshot = options.snapshot;
9
+ let local = options.local ?? { committed: snapshot.commit, changed: false, resetSuppressed: false };
10
+ const unavailable = (reason) => ({ snapshot, local, remote: { status: 'unverified', reason } });
11
+ const assertCurrent = () => { options.signal?.throwIfAborted(); if (!remote.isCurrent())
12
+ throw new Error('trigger_feedback_destination_changed'); };
13
+ assertCurrent();
14
+ const claimed = graph.claimTriggerFeedbackDelivery(snapshot.runId, remote.destinationId, randomUUID());
15
+ if (!claimed.claimed) {
16
+ if (claimed.state.status === 'confirmed' && claimed.state.acknowledgedRevision === (snapshot.commit?.revision ?? 0)) {
17
+ return { snapshot, local, remote: { status: 'confirmed', from: 'durable-receipt' } };
18
+ }
19
+ return unavailable(claimed.state.reason ?? 'trigger_feedback_busy');
20
+ }
21
+ const id = claimed.state.claim.id;
22
+ const observe = async () => {
23
+ assertCurrent();
24
+ const current = await options.readKernel();
25
+ assertCurrent();
26
+ if (current.runId !== snapshot.runId || current.triggerId !== snapshot.triggerId)
27
+ throw new Error('trigger_feedback_source_mismatch');
28
+ options.requireReceipt?.(current);
29
+ local = graph.applyKernelTriggerFeedback(current);
30
+ snapshot = algo.updateTriggerFeedbackState(graph.readTriggerFeedback(current.runId) ?? undefined, current).snapshot;
31
+ return snapshot;
32
+ };
33
+ const settle = (status, reason) => {
34
+ graph.settleTriggerFeedbackDelivery(snapshot.runId, id, { status, reason });
35
+ return unavailable(reason);
36
+ };
37
+ try {
38
+ if (claimed.reconcile) {
39
+ let receipt = null;
40
+ let absent = false;
41
+ try {
42
+ assertCurrent();
43
+ receipt = await remote.get(snapshot.runId);
44
+ }
45
+ catch (error) {
46
+ const body = error instanceof HubClientError && error.body && typeof error.body === 'object' ? error.body : {};
47
+ absent = error instanceof HubClientError && error.status === 404 && body['error'] === 'trigger_feedback_not_found';
48
+ if (!absent)
49
+ return settle('ambiguous', 'trigger_feedback_hub_unverified');
50
+ }
51
+ await observe();
52
+ const latestReport = hub.triggerFeedbackReport(snapshot);
53
+ if (receipt && hub.triggerFeedbackReceiptMatches(receipt, latestReport)) {
54
+ if ((snapshot.commit?.revision ?? 0) !== claimed.state.claim.revision)
55
+ return settle('ambiguous', 'trigger_feedback_source_changed');
56
+ graph.settleTriggerFeedbackDelivery(snapshot.runId, id, { status: 'confirmed', revision: snapshot.commit?.revision ?? 0, operationId: snapshot.commit?.operationId ?? null });
57
+ return { snapshot, local, remote: { status: 'confirmed', receipt } };
58
+ }
59
+ if (!absent && (!receipt || (receipt.committed?.revision ?? 0) >= (snapshot.commit?.revision ?? 0)))
60
+ return settle('blocked', 'trigger_feedback_remote_ahead');
61
+ if (!options.force && claimed.state.retryAt > Date.now())
62
+ return settle('ambiguous', 'trigger_feedback_retry_deferred');
63
+ }
64
+ const guarded = {
65
+ async commit(value) {
66
+ await observe();
67
+ if (JSON.stringify(hub.triggerFeedbackReport(snapshot)) !== JSON.stringify(value))
68
+ throw new Error('trigger_feedback_source_changed');
69
+ assertCurrent();
70
+ graph.markTriggerFeedbackDispatched(snapshot.runId, id);
71
+ return remote.commit(value);
72
+ },
73
+ async get(runId) { assertCurrent(); return remote.get(runId); },
74
+ };
75
+ const delivered = await deliverTriggerFeedback(snapshot, guarded);
76
+ await observe();
77
+ if (delivered.status === 'confirmed') {
78
+ const receipt = delivered.receipt;
79
+ graph.settleTriggerFeedbackDelivery(snapshot.runId, id, { status: 'confirmed', revision: receipt.committed?.revision ?? 0, operationId: receipt.committed?.operationId ?? null });
80
+ if (!hub.triggerFeedbackReceiptMatches(receipt, hub.triggerFeedbackReport(snapshot)))
81
+ return unavailable('trigger_feedback_pending_newer_revision');
82
+ return { snapshot, local, remote: delivered };
83
+ }
84
+ graph.settleTriggerFeedbackDelivery(snapshot.runId, id, {
85
+ status: delivered.status === 'rejected' ? 'blocked' : 'ambiguous', reason: delivered.reason,
86
+ });
87
+ return { snapshot, local, remote: delivered };
88
+ }
89
+ catch (error) {
90
+ const reason = error instanceof Error && /^trigger_feedback_[a-z_]+$/.test(error.message) ? error.message : 'trigger_feedback_hub_unverified';
91
+ if (remote.isCurrent()) {
92
+ if (reason.startsWith('trigger_feedback_kernel_'))
93
+ graph.markTriggerFeedbackSourceUnavailable(snapshot.runId);
94
+ graph.settleTriggerFeedbackDelivery(snapshot.runId, id, { status: 'ambiguous', reason });
95
+ }
96
+ return unavailable(reason);
97
+ }
98
+ }
@@ -0,0 +1,42 @@
1
+ import type { algo } from '@evomap/evolver-core';
2
+ export type TriggerFeedbackDeliveryStatus = 'pending' | 'ambiguous' | 'confirmed' | 'blocked';
3
+ export interface TriggerFeedbackDeliveryState {
4
+ version: 1;
5
+ destinationId: string;
6
+ consentEpoch: string;
7
+ desiredRevision: number;
8
+ desiredOperationId: string | null;
9
+ acknowledgedRevision: number | null;
10
+ acknowledgedOperationId: string | null;
11
+ status: TriggerFeedbackDeliveryStatus;
12
+ attempted: boolean;
13
+ retryAt: number;
14
+ reason?: string;
15
+ claim?: {
16
+ id: string;
17
+ revision: number;
18
+ operationId: string | null;
19
+ expiresAt: number;
20
+ };
21
+ retired: Array<{
22
+ destinationId: string;
23
+ revision: number;
24
+ status: TriggerFeedbackDeliveryStatus;
25
+ acknowledgedRevision: number | null;
26
+ }>;
27
+ }
28
+ export declare function parseTriggerFeedbackDelivery(raw: unknown): TriggerFeedbackDeliveryState | undefined;
29
+ export declare function advanceTriggerFeedbackDelivery(prior: TriggerFeedbackDeliveryState | undefined, snapshot: algo.TriggerFeedbackSnapshot, epoch: string): TriggerFeedbackDeliveryState | undefined;
30
+ export declare function authorizeTriggerFeedbackDelivery(prior: TriggerFeedbackDeliveryState | undefined, snapshot: algo.TriggerFeedbackSnapshot, epoch: string, destinationId: string): TriggerFeedbackDeliveryState;
31
+ export declare function claimTriggerFeedbackDelivery(prior: TriggerFeedbackDeliveryState, destinationId: string, id: string, now: number): {
32
+ state: TriggerFeedbackDeliveryState;
33
+ claimed: boolean;
34
+ reconcile: boolean;
35
+ };
36
+ export declare function markTriggerFeedbackDispatched(prior: TriggerFeedbackDeliveryState, id: string, now?: number): TriggerFeedbackDeliveryState;
37
+ export declare function settleTriggerFeedbackDelivery(prior: TriggerFeedbackDeliveryState, id: string, result: {
38
+ status: TriggerFeedbackDeliveryStatus;
39
+ revision?: number;
40
+ operationId?: string | null;
41
+ reason?: string;
42
+ }, now: number): TriggerFeedbackDeliveryState;