@listen-fire/engine 0.12.10 → 0.12.11

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.
@@ -87,3 +87,46 @@ export interface DeliveryContractCase {
87
87
  run(backend: DeliveryContractBackend): Promise<void>;
88
88
  }
89
89
  export declare const deliveryContractCases: DeliveryContractCase[];
90
+ export interface RerunContractBackend {
91
+ startRun(input: {
92
+ id: string;
93
+ file: string;
94
+ movement: string;
95
+ startedAt: string;
96
+ input: string;
97
+ }): Promise<void>;
98
+ runGet(input: {
99
+ runId: string;
100
+ }): Promise<RunRow | null>;
101
+ runSettle(input: {
102
+ runId: string;
103
+ status: string;
104
+ outcome: unknown;
105
+ at: string;
106
+ }): Promise<void>;
107
+ runRerun(input: {
108
+ runId: string;
109
+ input: string;
110
+ at: string;
111
+ fence: string;
112
+ }): Promise<number | null>;
113
+ markFailed(input: {
114
+ runId: string;
115
+ message: string;
116
+ }): Promise<void>;
117
+ claimLease(input: {
118
+ runId: string;
119
+ ttlMs: number;
120
+ }): Promise<{
121
+ token: string;
122
+ } | null>;
123
+ releaseLease(input: {
124
+ runId: string;
125
+ token: string;
126
+ }): Promise<void>;
127
+ }
128
+ export interface RerunContractCase {
129
+ name: string;
130
+ run(backend: RerunContractBackend): Promise<void>;
131
+ }
132
+ export declare const rerunContractCases: RerunContractCase[];
@@ -13,7 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  return (mod && mod.__esModule) ? mod : { "default": mod };
14
14
  };
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.deliveryContractCases = exports.cancelContractCases = exports.callbackContractCases = exports.runHostContractCases = void 0;
16
+ exports.rerunContractCases = exports.deliveryContractCases = exports.cancelContractCases = exports.callbackContractCases = exports.runHostContractCases = void 0;
17
17
  const strict_1 = __importDefault(require("node:assert/strict"));
18
18
  const node_crypto_1 = require("node:crypto");
19
19
  const errors_1 = require("../movement_engine/errors");
@@ -1118,3 +1118,86 @@ exports.deliveryContractCases = [
1118
1118
  },
1119
1119
  },
1120
1120
  ];
1121
+ const WROTE = { adapter: 'crm', recordType: 'Company', recordId: 'c-1', action: 'create', committed: true, values: {} };
1122
+ /** A run started then settled `status` with `outcome`, a millisecond apart. */
1123
+ async function settledRun(backend, status, outcome) {
1124
+ const id = (0, node_crypto_1.randomUUID)();
1125
+ await backend.startRun({ id, file: 'f', movement: 'm', startedAt: PAST(10), input: '{"attempt":1}' });
1126
+ await backend.runSettle({ runId: id, status, outcome, at: PAST(9) });
1127
+ return id;
1128
+ }
1129
+ async function leased(backend, runId) {
1130
+ const lease = await backend.claimLease({ runId, ttlMs: 60_000 });
1131
+ strict_1.default.ok(lease, 'the run is not held');
1132
+ return lease.token;
1133
+ }
1134
+ exports.rerunContractCases = [
1135
+ {
1136
+ name: "reruns: a failed run's rerun is its next attempt, the failed one kept beside it",
1137
+ async run(backend) {
1138
+ const runId = await settledRun(backend, 'failed', { error: 'boom', writes: 1, writeRecords: [WROTE] });
1139
+ const before = await backend.runGet({ runId });
1140
+ strict_1.default.equal(before?.attempt ?? 1, 1);
1141
+ strict_1.default.deepEqual(before?.attempts ?? [], []);
1142
+ strict_1.default.deepEqual(JSON.parse(before?.outcome ?? 'null'), { error: 'boom', writes: 1, writeRecords: [WROTE] }, 'a failed outcome keeps its writes');
1143
+ const fence = await leased(backend, runId);
1144
+ const at = PAST(1);
1145
+ strict_1.default.equal(await backend.runRerun({ runId, input: '{"attempt":2}', at, fence }), 2);
1146
+ const row = await backend.runGet({ runId });
1147
+ strict_1.default.ok(row);
1148
+ strict_1.default.deepEqual([row.status, row.attempt, row.input, row.startedAt, row.finishedAt ?? null], ['running', 2, '{"attempt":2}', at, null]);
1149
+ // The new attempt has written nothing yet (a store may answer `{ writes: 0 }` for none).
1150
+ const fresh = JSON.parse(row.outcome ?? '{}');
1151
+ strict_1.default.deepEqual([fresh.error, fresh.writes ?? 0, fresh.writeRecords], [undefined, 0, undefined]);
1152
+ strict_1.default.equal(row.attempts?.length, 1);
1153
+ const [first] = row.attempts;
1154
+ strict_1.default.deepEqual([first.attempt, first.status, first.startedAt, first.finishedAt], [1, 'failed', before.startedAt, before.finishedAt]);
1155
+ strict_1.default.deepEqual(first.outcome, { error: 'boom', writes: 1, writeRecords: [WROTE] });
1156
+ await backend.runSettle({ runId, status: 'failed', outcome: { error: 'boom again', writes: 0, writeRecords: [] }, at: PAST(0.5) });
1157
+ strict_1.default.equal(await backend.runRerun({ runId, input: '{"attempt":3}', at: PAST(0.2), fence }), 3);
1158
+ const third = await backend.runGet({ runId });
1159
+ strict_1.default.deepEqual(third?.attempts?.map((a) => [a.attempt, a.status]), [[1, 'failed'], [2, 'failed']]);
1160
+ strict_1.default.equal(third?.attempt, 3);
1161
+ await backend.releaseLease({ runId, token: fence });
1162
+ },
1163
+ },
1164
+ {
1165
+ name: 'reruns: a run whose latest attempt did not fail is not rerun, and nothing is written',
1166
+ async run(backend) {
1167
+ const completed = await settledRun(backend, 'completed', { writes: 0, writeRecords: [] });
1168
+ const running = (0, node_crypto_1.randomUUID)();
1169
+ await backend.startRun({ id: running, file: 'f', movement: 'm', startedAt: PAST(1), input: '{}' });
1170
+ const parked = await settledRun(backend, 'parked', { writes: 0 });
1171
+ for (const runId of [completed, running, parked]) {
1172
+ const before = await backend.runGet({ runId });
1173
+ const fence = await leased(backend, runId);
1174
+ strict_1.default.equal(await backend.runRerun({ runId, input: '{"attempt":2}', at: PAST(0), fence }), null, before?.status);
1175
+ const after = await backend.runGet({ runId });
1176
+ strict_1.default.deepEqual([after?.status, after?.attempt ?? 1, after?.input, after?.attempts ?? []], [before?.status, 1, before?.input, []]);
1177
+ await backend.releaseLease({ runId, token: fence });
1178
+ }
1179
+ },
1180
+ },
1181
+ {
1182
+ name: 'reruns: a rerun is fenced by the run lease',
1183
+ async run(backend) {
1184
+ const runId = await settledRun(backend, 'failed', { error: 'boom' });
1185
+ await strict_1.default.rejects(backend.runRerun({ runId, input: '{}', at: PAST(0), fence: (0, node_crypto_1.randomUUID)() }), errors_1.RunLeaseLost);
1186
+ const fence = await leased(backend, runId);
1187
+ await strict_1.default.rejects(backend.runRerun({ runId, input: '{}', at: PAST(0), fence: (0, node_crypto_1.randomUUID)() }), errors_1.RunLeaseLost, 'another token');
1188
+ const row = await backend.runGet({ runId });
1189
+ strict_1.default.deepEqual([row?.status, row?.attempt ?? 1], ['failed', 1]);
1190
+ await backend.releaseLease({ runId, token: fence });
1191
+ },
1192
+ },
1193
+ {
1194
+ name: "reruns: a run failed by markFailed keeps the writes it settled with",
1195
+ async run(backend) {
1196
+ const runId = await settledRun(backend, 'parked', { writes: 1, writeRecords: [WROTE] });
1197
+ await backend.markFailed({ runId, message: 'cancelled by the operator' });
1198
+ const row = await backend.runGet({ runId });
1199
+ strict_1.default.equal(row?.status, 'failed');
1200
+ strict_1.default.deepEqual(JSON.parse(row?.outcome ?? 'null'), { error: 'cancelled by the operator', writes: 1, writeRecords: [WROTE] });
1201
+ },
1202
+ },
1203
+ ];
@@ -50,6 +50,21 @@ export interface RunRow {
50
50
  input: string;
51
51
  outcome: string | null;
52
52
  deliveryId: string | null;
53
+ /** When it last settled completed or failed; a store that keeps none answers none. */
54
+ finishedAt?: string | null;
55
+ /** The latest attempt's number; a store that keeps no attempts answers none (1). */
56
+ attempt?: number;
57
+ /** The run's earlier attempts, oldest first. */
58
+ attempts?: RunAttempt[];
59
+ }
60
+ /** One earlier attempt of a run, as `runs.rerun` kept it. */
61
+ export interface RunAttempt {
62
+ attempt: number;
63
+ status: string;
64
+ startedAt: string;
65
+ finishedAt: string | null;
66
+ /** The JSON object it settled with: `{ error, writes?, writeRecords? }` for a failed one. */
67
+ outcome: unknown;
53
68
  }
54
69
  export declare class RpcStoreBackend {
55
70
  readonly store: RunHostStore;
@@ -77,6 +92,21 @@ export declare class RpcStoreBackend {
77
92
  runGet(input: {
78
93
  runId: string;
79
94
  }): Promise<RunRow | null>;
95
+ /** Where the run stands (`runs.settle`): an ending revokes its callbacks. */
96
+ runSettle(input: {
97
+ runId: string;
98
+ status: string;
99
+ outcome: unknown;
100
+ at: string;
101
+ }): Promise<void>;
102
+ /** The run's next attempt, under its lease (`runs.rerun`): its number, or
103
+ * null when the run's latest attempt did not fail. */
104
+ runRerun(input: {
105
+ runId: string;
106
+ input: string;
107
+ at: string;
108
+ fence: string;
109
+ }): Promise<number | null>;
80
110
  /** The door's one write: the receipt, the cursor (absent: left as it is)
81
111
  * and the work rows; `kept: false`, and nothing written, for a duplicate. */
82
112
  keepDelivery(input: {
@@ -73,6 +73,11 @@ const Run = zod_1.z.object({
73
73
  input: zod_1.z.string(),
74
74
  outcome: zod_1.z.string().nullable(),
75
75
  deliveryId: zod_1.z.string().nullable(),
76
+ finishedAt: zod_1.z.string().nullable().optional(),
77
+ attempt: zod_1.z.number().optional(),
78
+ attempts: zod_1.z
79
+ .array(zod_1.z.object({ attempt: zod_1.z.number(), status: zod_1.z.string(), startedAt: zod_1.z.string(), finishedAt: zod_1.z.string().nullable(), outcome: zod_1.z.unknown() }))
80
+ .optional(),
76
81
  });
77
82
  /** The work claim the server reads: no more than it needs, whatever the caller spreads in. */
78
83
  function claimOf(work) {
@@ -119,6 +124,16 @@ class RpcStoreBackend {
119
124
  runGet(input) {
120
125
  return this.call('runs.get', input, Run.nullable());
121
126
  }
127
+ /** Where the run stands (`runs.settle`): an ending revokes its callbacks. */
128
+ async runSettle(input) {
129
+ await this.call('runs.settle', input, Nothing);
130
+ }
131
+ /** The run's next attempt, under its lease (`runs.rerun`): its number, or
132
+ * null when the run's latest attempt did not fail. */
133
+ async runRerun(input) {
134
+ const answer = await this.call('runs.rerun', input, zod_1.z.object({ attempt: zod_1.z.number() }).nullable());
135
+ return answer?.attempt ?? null;
136
+ }
122
137
  /** The door's one write: the receipt, the cursor (absent: left as it is)
123
138
  * and the work rows; `kept: false`, and nothing written, for a duplicate. */
124
139
  keepDelivery(input) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@listen-fire/engine",
3
3
  "license": "Apache-2.0",
4
- "version": "0.12.10",
4
+ "version": "0.12.11",
5
5
  "description": "The movement engine for this release's language and its host contract: the Rust engine's shim, the EngineHost families, the run host, the embed kit, and the adapter and model types they reach. Every shell (the hosted product, the `lf` command line, an embedder) runs the same engine. A run pinned to a release this one does not read is refused by name. Embedders reach it only through public/* (the exports map); the internals resolve only under the `movement-engine-internals` condition, which only test runners set",
6
6
  "engines": {
7
7
  "node": ">=22.23.4 <23"
@@ -17,9 +17,9 @@
17
17
  "dependencies": {
18
18
  "safe-stable-stringify": "2.5.0",
19
19
  "zod": "4.1.12",
20
+ "@listen-fire/engine-contracts": "^0.12.11",
20
21
  "@listen-fire/module-runtime": "^0.12.8",
21
- "@listen-fire/engine-contracts": "^0.12.10",
22
- "@listen-fire/movement-lang": "^0.12.10"
22
+ "@listen-fire/movement-lang": "^0.12.11"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/jest": "29.5.14",