@shumkov/orchestra 0.5.0 → 0.7.0

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.
@@ -120,12 +120,18 @@ class Process extends EventEmitter {
120
120
  async interrupt() {
121
121
  throw new UnsupportedOperationError('interrupt', this.backend);
122
122
  }
123
+ async steerTurn(_text, _opts) {
124
+ throw new UnsupportedOperationError('steerTurn', this.backend);
125
+ }
123
126
  async setModel(_model) {
124
127
  throw new UnsupportedOperationError('setModel', this.backend);
125
128
  }
126
129
  async applyFlagSettings(_settings) {
127
130
  throw new UnsupportedOperationError('applyFlagSettings', this.backend);
128
131
  }
132
+ async selectModelSettings(_settings) {
133
+ throw new UnsupportedOperationError('selectModelSettings', this.backend);
134
+ }
129
135
  async setPermissionMode(_mode) {
130
136
  throw new UnsupportedOperationError('setPermissionMode', this.backend);
131
137
  }
@@ -136,6 +142,16 @@ class Process extends EventEmitter {
136
142
  throw new UnsupportedOperationError('getContextUsage', this.backend);
137
143
  }
138
144
 
145
+ /**
146
+ * Fence this exact process after an external durability failure. Backends
147
+ * without a durable mutation ledger cannot provide this containment.
148
+ *
149
+ * @returns {boolean} true when the process entered a durable work fence
150
+ */
151
+ blockDurability(_error) {
152
+ return false;
153
+ }
154
+
139
155
  // ─── OPTIONAL sync HOT-PATH methods — never throw (R1-F1) ──────────
140
156
 
141
157
  /**
@@ -5,6 +5,7 @@ const fs = require('node:fs');
5
5
  const { canonicalizeToolInput } = require('../canonical-json');
6
6
 
7
7
  const TASK_NOTIFICATION_ID_RE = /<tool-use-id>\s*([^<]+?)\s*<\/tool-use-id>/g;
8
+ const MAX_WORKFLOW_TRANSCRIPT_BYTES = 16 * 1024 * 1024;
8
9
 
9
10
  function normalizeVisibleText(value) {
10
11
  return typeof value === 'string'
@@ -94,13 +95,62 @@ function result(reason, extra = {}) {
94
95
  };
95
96
  }
96
97
 
97
- async function readSnapshot(filePath, byteSize) {
98
+ async function readSnapshot(filePath, byteSize, {
99
+ prefixByteSize = null,
100
+ prefixHash = null,
101
+ fileDevice = null,
102
+ fileInode = null,
103
+ } = {}) {
98
104
  if (typeof filePath !== 'string' || !filePath || !Number.isSafeInteger(byteSize) || byteSize <= 0) {
99
105
  return { ok: false, reason: 'snapshot-missing' };
100
106
  }
107
+ if (byteSize > MAX_WORKFLOW_TRANSCRIPT_BYTES) {
108
+ return { ok: false, reason: 'snapshot-too-large' };
109
+ }
110
+ const validatesPrefix = prefixByteSize !== null || prefixHash !== null;
111
+ if (
112
+ validatesPrefix
113
+ && (
114
+ !Number.isSafeInteger(prefixByteSize)
115
+ || prefixByteSize <= 0
116
+ || prefixByteSize > byteSize
117
+ || typeof prefixHash !== 'string'
118
+ || !/^[0-9a-f]{64}$/.test(prefixHash)
119
+ )
120
+ ) {
121
+ return { ok: false, reason: 'snapshot-prefix-invalid' };
122
+ }
123
+ const validatesIdentity = fileDevice !== null || fileInode !== null;
124
+ if (
125
+ validatesIdentity
126
+ && (
127
+ typeof fileDevice !== 'string'
128
+ || !/^\d+$/.test(fileDevice)
129
+ || typeof fileInode !== 'string'
130
+ || !/^\d+$/.test(fileInode)
131
+ )
132
+ ) {
133
+ return { ok: false, reason: 'snapshot-identity-invalid' };
134
+ }
101
135
  let handle;
102
136
  try {
103
137
  handle = await fs.promises.open(filePath, 'r');
138
+ const before = await handle.stat({ bigint: true });
139
+ if (
140
+ !before.isFile()
141
+ || (
142
+ validatesIdentity
143
+ && (
144
+ before.dev.toString() !== fileDevice
145
+ || before.ino.toString() !== fileInode
146
+ )
147
+ )
148
+ ) {
149
+ return { ok: false, reason: 'snapshot-replaced' };
150
+ }
151
+ if (before.size < BigInt(byteSize)) {
152
+ return { ok: false, reason: 'snapshot-truncated' };
153
+ }
104
154
  const buffer = Buffer.allocUnsafe(byteSize);
105
155
  let offset = 0;
106
156
  while (offset < byteSize) {
@@ -109,8 +159,33 @@ async function readSnapshot(filePath, byteSize) {
109
159
  offset += bytesRead;
110
160
  }
111
161
  if (offset !== byteSize) return { ok: false, reason: 'snapshot-truncated' };
162
+ const after = await handle.stat({ bigint: true });
163
+ if (
164
+ !after.isFile()
165
+ || after.dev !== before.dev
166
+ || after.ino !== before.ino
167
+ || after.size < BigInt(byteSize)
168
+ ) {
169
+ return { ok: false, reason: 'snapshot-changed-during-read' };
170
+ }
171
+ if (
172
+ after.size === before.size
173
+ && (after.mtimeNs !== before.mtimeNs || after.ctimeNs !== before.ctimeNs)
174
+ ) {
175
+ return { ok: false, reason: 'snapshot-changed-during-read' };
176
+ }
177
+ if (
178
+ validatesPrefix
179
+ && crypto.createHash('sha256').update(buffer.subarray(0, prefixByteSize)).digest('hex') !== prefixHash
180
+ ) {
181
+ return { ok: false, reason: 'snapshot-prefix-changed' };
182
+ }
112
183
  if (buffer[byteSize - 1] !== 0x0a) return { ok: false, reason: 'partial-line' };
113
- return { ok: true, text: buffer.toString('utf8') };
184
+ return {
185
+ ok: true,
186
+ text: buffer.toString('utf8'),
187
+ hash: crypto.createHash('sha256').update(buffer).digest('hex'),
188
+ };
114
189
  } catch {
115
190
  return { ok: false, reason: 'snapshot-unreadable' };
116
191
  } finally {
@@ -235,12 +310,21 @@ async function correlateWorkflowCompletionSnapshot({
235
310
  byteSize,
236
311
  sessionId,
237
312
  finalText,
313
+ prefixByteSize,
314
+ prefixHash,
315
+ fileDevice,
316
+ fileInode,
238
317
  } = {}) {
239
318
  const normalizedFinal = normalizeVisibleText(finalText);
240
319
  if (!normalizedFinal) return result('final-empty');
241
320
  if (typeof sessionId !== 'string' || !sessionId) return result('session-missing');
242
321
 
243
- const snapshot = await readSnapshot(transcriptPath, byteSize);
322
+ const snapshot = await readSnapshot(transcriptPath, byteSize, {
323
+ prefixByteSize,
324
+ prefixHash,
325
+ fileDevice,
326
+ fileInode,
327
+ });
244
328
  if (!snapshot.ok) return result(snapshot.reason);
245
329
 
246
330
  const rows = [];
@@ -264,10 +348,13 @@ async function correlateWorkflowCompletionSnapshot({
264
348
  && (row.type === 'assistant' || row.type === 'user')
265
349
  ));
266
350
  if (!mainline.length) return result('session-mainline-missing');
351
+ const ancestryRows = rows.filter(row => (
352
+ row.sessionId === sessionId && row.isSidechain === false
353
+ ));
267
354
 
268
355
  const byUuid = new Map();
269
356
  const children = new Map();
270
- for (const row of mainline) {
357
+ for (const row of ancestryRows) {
271
358
  if (
272
359
  typeof row.uuid !== 'string'
273
360
  || !row.uuid
@@ -292,7 +379,12 @@ async function correlateWorkflowCompletionSnapshot({
292
379
  }
293
380
  }
294
381
  if (terminalIndex < 0) return result('terminal-missing');
295
- if (terminalIndex !== mainline.length - 1) return result('terminal-not-last-mainline');
382
+ if (terminalIndex !== mainline.length - 1) {
383
+ return result('terminal-not-last-mainline', {
384
+ snapshotByteSize: byteSize,
385
+ snapshotHash: snapshot.hash,
386
+ });
387
+ }
296
388
 
297
389
  const terminal = mainline[terminalIndex];
298
390
  if (
@@ -403,6 +495,8 @@ async function correlateWorkflowCompletionSnapshot({
403
495
 
404
496
  return result('eligible', {
405
497
  eligible: true,
498
+ snapshotByteSize: byteSize,
499
+ snapshotHash: snapshot.hash,
406
500
  finalHash: hashText(normalizedFinal),
407
501
  deliveredFinal: receipts.deliveredFinal,
408
502
  visibleHashes: receipts.visibleHashes,
@@ -411,6 +505,7 @@ async function correlateWorkflowCompletionSnapshot({
411
505
  }
412
506
 
413
507
  module.exports = {
508
+ MAX_WORKFLOW_TRANSCRIPT_BYTES,
414
509
  correlateWorkflowCompletionSnapshot,
415
510
  hashDeliveryArguments,
416
511
  normalizeVisibleText,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shumkov/orchestra",
3
- "version": "0.5.0",
4
- "description": "Drive interactive Claude Code CLI sessions: spawn, inject messages via the MCP channels bridge, observe turns, recover. The transport-agnostic session engine extracted from polygram, shared by polygram (Telegram) and water (WhatsApp).",
3
+ "version": "0.7.0",
4
+ "description": "Transport-agnostic session engine for Claude Code and the native Codex app-server: spawn, steer, supervise, observe turns, and recover.",
5
5
  "type": "commonjs",
6
6
  "main": "index.js",
7
7
  "files": [
@@ -11,7 +11,7 @@
11
11
  "README.md"
12
12
  ],
13
13
  "scripts": {
14
- "test": "node --test --test-force-exit tests/*.test.js"
14
+ "test": "node --test --test-force-exit --test-concurrency=1 tests/*.test.js"
15
15
  },
16
16
  "publishConfig": {
17
17
  "access": "public"
@@ -27,6 +27,8 @@
27
27
  "claude",
28
28
  "claude-code",
29
29
  "claude-cli",
30
+ "codex",
31
+ "openai",
30
32
  "channels",
31
33
  "mcp",
32
34
  "session-pool",