@listen-fire/engine 0.12.4 → 0.12.6

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.
@@ -106,6 +106,34 @@ exports.runHostContractCases = [
106
106
  await store.work.complete(claimed);
107
107
  },
108
108
  },
109
+ {
110
+ name: 'work: a key queued if absent is queued once; a row there, due later or leased, is left as it was',
111
+ async run({ store }) {
112
+ const key = uniqueKey('poll_listen');
113
+ strict_1.default.equal(await store.work.enqueueIfAbsent({ kind: 'poll_listen', key, payload: { n: 1 }, dueAt: FUTURE(10) }), true);
114
+ strict_1.default.equal(await store.work.enqueueIfAbsent({ kind: 'poll_listen', key, payload: { n: 2 }, dueAt: PAST(1) }), false);
115
+ strict_1.default.equal(await claimKey(store, key), undefined, 'still due later: the second arm moved nothing');
116
+ await store.work.enqueue({ kind: 'poll_listen', key, payload: { n: 3 }, dueAt: PAST(1) });
117
+ const held = await claimKey(store, key);
118
+ strict_1.default.deepEqual(held?.payload, { n: 3 });
119
+ strict_1.default.ok(held);
120
+ // Another worker arms the same listen while the row is held: nothing changes,
121
+ // so the holder's release decides when it is next due.
122
+ strict_1.default.equal(await store.work.enqueueIfAbsent({ kind: 'poll_listen', key, payload: { n: 4 }, dueAt: PAST(1) }), false);
123
+ await store.work.release({ ...held, dueAt: FUTURE(30) });
124
+ strict_1.default.equal(await claimKey(store, key), undefined, 'due at the release time, not polled again at once');
125
+ const later = await store.work.claimDue({ limit: 1000, leaseMs: 60_000, dueBy: FUTURE(31) });
126
+ const again = later.find((w) => w.key === key);
127
+ strict_1.default.deepEqual(again?.payload, { n: 3 }, 'the payload the row had');
128
+ for (const w of later)
129
+ if (w.key === key)
130
+ await store.work.complete(w);
131
+ strict_1.default.equal(await store.work.enqueueIfAbsent({ kind: 'poll_listen', key, payload: { n: 5 }, dueAt: PAST(1) }), true, 'completed and gone, it is queued again');
132
+ const last = await claimKey(store, key);
133
+ if (last)
134
+ await store.work.complete(last);
135
+ },
136
+ },
109
137
  {
110
138
  name: 'work: only due, unleased rows are claimed; a live lease blocks a second claim',
111
139
  async run({ store }) {
@@ -200,6 +200,12 @@ function memoryRunHostStore(options = {}) {
200
200
  row.teamId = teamId;
201
201
  row.armed += 1;
202
202
  },
203
+ async enqueueIfAbsent({ kind, key, payload, dueAt, teamId }) {
204
+ if (work.has(key))
205
+ return false;
206
+ work.set(key, { id: (0, node_crypto_1.randomUUID)(), kind, key, payload: stored(payload), dueAt: dueAt !== undefined ? Date.parse(dueAt) : now(), teamId, claim: undefined, leasedUntil: 0, armed: 0, armedAtClaim: 0 });
207
+ return true;
208
+ },
203
209
  async claimDue({ limit, leaseMs, dueBy, kinds }) {
204
210
  const by = dueBy !== undefined ? Date.parse(dueBy) : now();
205
211
  const claimed = [];
@@ -245,6 +245,7 @@ class RpcStoreBackend {
245
245
  enqueue: async (input) => {
246
246
  await call('work.enqueue', input, Nothing);
247
247
  },
248
+ enqueueIfAbsent: (input) => call('work.enqueueIfAbsent', input, zod_1.z.boolean()),
248
249
  claimDue: (input) => call('work.claimDue', input, zod_1.z.array(Work)),
249
250
  complete: async (work) => {
250
251
  await call('work.complete', claimOf(work), Nothing);
@@ -214,6 +214,17 @@ export interface WorkQueue {
214
214
  dueAt?: string;
215
215
  teamId?: string;
216
216
  }): Promise<void>;
217
+ /** Insert when no row has the key, in one write; a row there in any state
218
+ * (due, leased, buried) is left as it is. Whether it was queued: arming a
219
+ * listen's standing row from several workers queues it once, and never
220
+ * moves the due time of a row another worker holds. */
221
+ enqueueIfAbsent(input: {
222
+ kind: string;
223
+ key: string;
224
+ payload: unknown;
225
+ dueAt?: string;
226
+ teamId?: string;
227
+ }): Promise<boolean>;
217
228
  /** Lease up to `limit` rows due by `dueBy`, unleased or with a lapsed lease;
218
229
  * only rows of `kinds` when it is given (a drain pool's), else every kind. */
219
230
  claimDue(input: {
@@ -35,6 +35,7 @@ exports.fetchRemoteManifest = fetchRemoteManifest;
35
35
  exports.createRemoteAdapter = createRemoteAdapter;
36
36
  const node_stream_1 = require("node:stream");
37
37
  const engine_contracts_1 = require("@listen-fire/engine-contracts");
38
+ const movement_lang_1 = require("@listen-fire/movement-lang");
38
39
  const client_1 = require("../../protocol/client");
39
40
  const limiter_1 = require("../../../limiter/limiter");
40
41
  const family_1 = require("@listen-fire/module-runtime/secrets/family");
@@ -113,12 +114,12 @@ async function fetchRemoteManifest(config, secret) {
113
114
  cacheScopeId: '<none>',
114
115
  params: {},
115
116
  });
116
- // Checked ahead of the full parse so an older server is refused for what it
117
- // is, not for whichever other field happens to have changed shape.
118
- const version = engine_contracts_1.WireManifestProtocolVersion.safeParse(raw !== null && typeof raw === 'object' ? Reflect.get(raw, 'protocolVersion') : undefined);
119
- if (!version.success) {
120
- throw new Error(`Remote adapter "${config.adapterType}" at ${config.baseUrl} refused: ${version.error.issues[0]?.message ?? 'protocolVersion is not 2'}`);
121
- }
117
+ // Checked ahead of the full parse so a server outside the range this engine
118
+ // speaks is refused for what it is, not for whichever other field happens
119
+ // to have changed shape.
120
+ const standing = (0, engine_contracts_1.protocolStanding)(raw, { host: { name: '@listen-fire/engine', version: movement_lang_1.LANGUAGE_VERSION } });
121
+ if (!standing.ok)
122
+ throw new Error(`Remote adapter "${config.adapterType}" at ${config.baseUrl} refused: ${standing.message}`);
122
123
  const result = (0, client_1.parseWireResult)(engine_contracts_1.METHODS.manifest.result, raw, {
123
124
  method: 'manifest',
124
125
  adapter: config.adapterType,
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.4",
4
+ "version": "0.12.6",
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/module-runtime": "^0.12.4",
21
- "@listen-fire/engine-contracts": "^0.12.4",
22
- "@listen-fire/movement-lang": "^0.12.4"
20
+ "@listen-fire/module-runtime": "^0.12.6",
21
+ "@listen-fire/engine-contracts": "^0.12.6",
22
+ "@listen-fire/movement-lang": "^0.12.6"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/jest": "29.5.14",