@volter/supercode-orchestrator 0.5.106 → 0.5.107

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/board/doors.mjs CHANGED
@@ -64,6 +64,11 @@ export async function machineCall(machine, method, params = {}, { timeoutMs = 60
64
64
  }
65
65
  }
66
66
 
67
+ export async function machineRecordStream(params = {}) {
68
+ const door = await opened(null);
69
+ return door.channel.open('machine-record-changes', params);
70
+ }
71
+
67
72
  /** Let go of every held channel (the owner stopping). */
68
73
  export async function closeDoors() {
69
74
  for (const [key, opening] of [...held]) {
@@ -8,7 +8,9 @@
8
8
  // sessions under them not read) or was never read is `unknown` (its runs are kept, as an unread machine's always were), unless its record says it was unlinked from the
9
9
  // team: then it is offline. This machine's own record not being given is no reading at all.
10
10
  import { noteStep } from '@volter/supercode-harness-sdk/slow-log';
11
- import { machineCall } from './doors.mjs';
11
+ import { machineCall, machineRecordStream } from './doors.mjs';
12
+ import { RecordView } from './record-view.mjs';
13
+ const recordView = new RecordView(machineRecordStream);
12
14
  const DEADLINE_MS = 10_000;
13
15
  const within = (promise, ms) => Promise.race([promise, new Promise((_, reject) => setTimeout(() => reject(Object.assign(new Error(`no answer within ${ms} ms`), { code: 'timeout' })), ms).unref?.())]);
14
16
  // One machine's reading, from its held record: its panes, what a board launch needs of it, and the session under each
@@ -32,7 +34,15 @@ export async function readMachineFleet(machines, local) {
32
34
  const names = [...new Set([local, ...machines])];
33
35
  const started = performance.now();
34
36
  let held = null, heldError = null;
35
- try { held = (await within(machineCall(null, 'harness.v1.machine.records', {}), DEADLINE_MS))?.records ?? {}; }
37
+ try {
38
+ try { held = await within(recordView.read(DEADLINE_MS), DEADLINE_MS); }
39
+ catch (error) {
40
+ if (!String(error.message).includes('unknown stream machine-record-changes')) throw error;
41
+ // Supported older daemons have only the snapshot door. A refusal or a
42
+ // failed current stream never falls back to data of uncertain freshness.
43
+ held = (await within(machineCall(null, 'harness.v1.machine.records', {}), DEADLINE_MS))?.records ?? {};
44
+ }
45
+ }
36
46
  catch (error) { heldError = String(error?.message ?? error).slice(0, 300); }
37
47
  const rows = names.map((name) => heldError ? { machine: name, error: `this machine's daemon did not give the records it holds: ${heldError}`, code: 'unavailable' } : readOne(name, held[name]));
38
48
  noteStep(`fleet reading (${names.length} machine${names.length === 1 ? '' : 's'}, held records)`, performance.now() - started);
@@ -0,0 +1,93 @@
1
+ // Resident local consumer of the daemon's committed record rows. Disconnects
2
+ // preserve the last applied cursor, but held data is stale until caught up.
3
+ export class RecordView {
4
+ constructor(open) {
5
+ this.open = open; this.records = {}; this.positions = new Map();
6
+ this.cursor = null; this.stream = null; this.waiters = new Set();
7
+ this.ready = false; this.heardAt = 0;
8
+ }
9
+ async read(timeoutMs) {
10
+ const deadline = Date.now() + timeoutMs;
11
+ if (this.stream && Date.now() - this.heardAt > 45_000) this.stream.close('its record stream went quiet');
12
+ if (!this.stream && !this.opening) {
13
+ this.opening = this.open({ after: this.cursor }).then((stream) => {
14
+ this.stream = stream;
15
+ this.ready = false;
16
+ this.heardAt = Date.now();
17
+ stream.on('data', (frame) => {
18
+ try { this.apply(frame); }
19
+ catch (error) { this.fail(error); stream.close(error.message); }
20
+ });
21
+ stream.on('close', (reason) => {
22
+ if (this.stream !== stream) return;
23
+ this.stream = null; this.ready = false;
24
+ this.fail(new Error(reason ?? 'the local record stream closed'));
25
+ });
26
+ }).finally(() => { this.opening = null; });
27
+ }
28
+ if (this.opening) await this.opening;
29
+ if (this.ready) return this.records;
30
+ if (!this.stream) throw this.failure ?? new Error('the local record stream ended before its snapshot');
31
+ return new Promise((resolve, reject) => {
32
+ const waiter = { resolve, reject };
33
+ waiter.timer = setTimeout(() => {
34
+ this.waiters.delete(waiter);
35
+ reject(new Error('the local record stream did not finish its snapshot or cursor catch-up within its deadline'));
36
+ }, Math.max(0, deadline - Date.now()));
37
+ this.waiters.add(waiter);
38
+ });
39
+ }
40
+ fail(error) {
41
+ this.failure = error;
42
+ for (const waiter of this.waiters) { clearTimeout(waiter.timer); waiter.reject(error); }
43
+ this.waiters.clear();
44
+ }
45
+ own(key) { return key.role === 'producer' ? key.machine === this.self : key.role === 'holder' && key.machine !== this.self; }
46
+ apply(frame) {
47
+ if (frame.type === 'beat') { this.heardAt = Date.now(); return; }
48
+ if (frame.type === 'snapshot') {
49
+ this.self = frame.machine; this.records = frame.records; this.positions.clear();
50
+ for (const [machine, held] of Object.entries(this.records)) this.positions.set(machine, new Map((held.record?.sessions ?? []).map((row, at) => [row.address, at])));
51
+ this.cursor = frame.cursor; this.ready = true;
52
+ } else if (frame.type === 'changes') {
53
+ if (!this.cursor || frame.cursor?.id !== this.cursor.id) throw new Error('record row changes name a different database; a new snapshot is required');
54
+ const moved = new Set();
55
+ for (const change of frame.changes ?? []) {
56
+ if (change.sequence <= this.cursor.sequence || !this.own(change.key)) continue;
57
+ const { machine, section, key, position } = change.key;
58
+ if (change.topic === 'record-head') {
59
+ if (change.operation === 'delete') { delete this.records[machine]; this.positions.delete(machine); continue; }
60
+ const { record: summary, meta } = change.value;
61
+ const previous = this.records[machine]?.record;
62
+ const record = summary == null ? null : { sessions: [], mail: {}, panes: null, capabilities: null, unread: {}, ...previous, ...summary };
63
+ if (record) { if (!summary.sessions_rows) record.sessions = null; delete record.sessions_rows; }
64
+ this.records[machine] = { record, stream: machine === this.self ? 'self' : meta?.stream ?? 'closed', since: meta?.since ?? null, why: meta?.why ?? null, received_at: meta?.received_at ?? null };
65
+ } else if (change.topic === 'record-row') {
66
+ const held = this.records[machine] ??= { stream: 'closed', record: null };
67
+ const record = held.record ??= { sessions: [], mail: {}, panes: null, capabilities: null, unread: {} };
68
+ const deleted = change.operation === 'delete';
69
+ if (section === 'sessions') {
70
+ const rows = new Map((record.sessions ?? []).map((row) => [row.address, row]));
71
+ const positions = this.positions.get(machine) ?? new Map();
72
+ this.positions.set(machine, positions);
73
+ if (deleted) { rows.delete(key); positions.delete(key); }
74
+ else { rows.set(key, change.value); positions.set(key, position); }
75
+ record.sessions = [...rows.values()]; moved.add(machine);
76
+ } else if (section === 'mail') {
77
+ if (deleted) delete record.mail[key]; else record.mail[key] = change.value;
78
+ } else record[section] = deleted ? null : change.value;
79
+ }
80
+ }
81
+ for (const machine of moved) {
82
+ const positions = this.positions.get(machine);
83
+ this.records[machine]?.record?.sessions?.sort((a, b) => (positions.get(a.address) ?? 0) - (positions.get(b.address) ?? 0) || a.address.localeCompare(b.address));
84
+ }
85
+ this.cursor = frame.cursor; this.ready = !frame.more;
86
+ } else throw new Error(`unrecognized record-view frame ${frame.type}`);
87
+ this.heardAt = Date.now();
88
+ if (this.ready) {
89
+ for (const waiter of this.waiters) { clearTimeout(waiter.timer); waiter.resolve(this.records); }
90
+ this.waiters.clear();
91
+ }
92
+ }
93
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/supercode-orchestrator",
3
- "version": "0.5.106",
3
+ "version": "0.5.107",
4
4
  "type": "module",
5
5
  "description": "The orchestrator runtime over the Volter Harness ontology: one typed operational model whose folder is its serialization, read and written through the harness orchestration doors (docs/ORCHESTRATOR-IR.md)",
6
6
  "exports": {