mtech-ai-local-runner 0.1.41 → 0.1.42

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/README.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 0.1.42: keep the guardian's reports when a late terminate fails
2
+
3
+ The owned guardian now reports to the host on its own pipe (fd3) and takes the host's commands on another (fd5);
4
+ fd4 stays parent liveness, and none of them reaches the executor. Before, one control socket carried both. When
5
+ Codex exited by itself and the guardian confirmed and exited while the daemon was busy, the daemon's late terminate
6
+ write failed on that socket, Node destroyed it, and the unread `root_exit` and `cleaned` reports were lost. 0.1.41
7
+ recovered the cleanup confirmation from the exit code, but the lost root exit outcome still failed the P1 publication
8
+ freeze with `OWNED_PROCESS_EXIT_UNCONFIRMED`. A failed command write now leaves the report pipe intact, so the host reads
9
+ every report to its end. Cleanup is again confirmed only by `cleaned`; the exit-code fallback is removed.
10
+
1
11
  ## 0.1.41: confirm a cleanup the guardian finished before a late terminate
2
12
 
3
13
  When Codex exits by itself, the owned guardian confirms the cleanup and exits. A busy daemon could then write its
@@ -1,15 +1,18 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  // Trusted, packaged JavaScript, deliberately independent of TypeScript loaders.
3
- // Only this helper creates and signals the new executor group. fd3 is private
4
- // control, fd4 parent liveness; neither is passed to the executor.
3
+ // Only this helper creates and signals the new executor group. fd3 carries its
4
+ // private reports to the host, fd4 parent liveness and fd5 the host's commands;
5
+ // none is passed to the executor. Commands have their own pipe: a host write
6
+ // that fails after the guardian exited cannot discard reports not yet read.
5
7
  const GUARDIAN_SOURCE = String.raw `
6
8
  const {spawn}=require('node:child_process');
7
9
  const {Socket}=require('node:net');
8
- const control=new Socket({fd:3,readable:true,writable:true});
9
- control.setEncoding('utf8');
10
+ const reports=new Socket({fd:3,readable:false,writable:true});
10
11
  const liveness=new Socket({fd:4,readable:true,writable:false});
12
+ const commands=new Socket({fd:5,readable:true,writable:false});
13
+ commands.setEncoding('utf8');
11
14
  let child, ownedPid, ownedGone=false, cleaning, ready=Promise.resolve(), rootExited=false, closed=false, input='', configured=false, orphaned=false;
12
- const send=value=>{if(!control.destroyed)control.write(JSON.stringify(value)+'\n');};
15
+ const send=value=>{if(!reports.destroyed)reports.write(JSON.stringify(value)+'\n');};
13
16
  const delay=ms=>new Promise(resolve=>setTimeout(resolve,ms));
14
17
  const alive=()=>{if(!ownedPid||ownedGone)return false;try{process.kill(-ownedPid,0);return true;}catch(e){if(e.code==='ESRCH'){ownedGone=true;return false;}if(e.code==='EPERM')return true;throw Error('cleanup');}};
15
18
  const signal=s=>{if(!ownedPid)throw Error('ownership');if(ownedGone)return 'esrch';try{process.kill(-ownedPid,s);return 'sent';}catch(e){if(e.code==='ESRCH'){ownedGone=true;return 'esrch';}if(e.code==='EPERM'){if(!rootExited)child.kill(s);return 'eperm';}throw Error('cleanup');}};
@@ -39,11 +42,11 @@ function cleanup(immediate=false,trigger='terminate'){
39
42
  })();
40
43
  return cleaning;
41
44
  }
42
- function finish(code){process.exitCode=code;process.stdin.destroy();liveness.destroy();control.end(()=>control.destroy());process.stdout.end();process.stderr.end();}
45
+ function finish(code){process.exitCode=code;process.stdin.destroy();liveness.destroy();commands.destroy();reports.end(()=>reports.destroy());process.stdout.end();process.stderr.end();}
43
46
  function lost(){orphaned=true;if(configured)void cleanup(false,'lost');else finish(1);}
44
47
  liveness.on('data',()=>{});liveness.once('end',lost);liveness.once('error',lost);
45
- control.on('error',lost);control.on('end',lost);
46
- control.on('data',chunk=>{
48
+ reports.on('error',lost);commands.on('error',lost);commands.on('end',lost);
49
+ commands.on('data',chunk=>{
47
50
  input+=chunk.toString('utf8');if(input.length>2000000){lost();return;}
48
51
  let end;while((end=input.indexOf('\n'))>=0){
49
52
  let message;try{message=JSON.parse(input.slice(0,end));}catch{lost();return;}input=input.slice(end+1);
@@ -68,29 +71,31 @@ process.stderr.on('error',()=>{child?.stderr.resume();void cleanup(true,'output_
68
71
  `;
69
72
  export function spawnOwnedGuardian(command, args, options) {
70
73
  const child = spawn(process.execPath, ['--input-type=commonjs', '-e', GUARDIAN_SOURCE], {
71
- cwd: options.cwd, env: {}, shell: false, detached: true, stdio: ['pipe', 'pipe', 'pipe', 'pipe', 'pipe'],
74
+ cwd: options.cwd, env: {}, shell: false, detached: true, stdio: ['pipe', 'pipe', 'pipe', 'pipe', 'pipe', 'pipe'],
72
75
  });
73
- const control = child.stdio[3], liveness = child.stdio[4];
74
- control.setEncoding('utf8');
76
+ // Node types only the first five stdio entries; fd5 is the sixth 'pipe' above.
77
+ const stdio = child.stdio;
78
+ const reports = stdio[3], liveness = stdio[4], commands = stdio[5];
79
+ reports.setEncoding('utf8');
75
80
  liveness.resume();
76
81
  let resolveStarted, rejectStarted;
77
82
  const started = new Promise((resolve, reject) => { resolveStarted = resolve; rejectStarted = reject; });
78
83
  // Hosts can attach observers after spawn returns without unhandled rejection.
79
84
  void started.catch(() => { });
80
85
  let confirmed = false, spawnFailed = false, protocolFailed = false, buffer = '';
81
- let terminateWritten = false, hostTimeout = false, controlFailed = false, detail;
86
+ let terminateWritten = false, hostTimeout = false, commandFailed = false, detail;
82
87
  let outcome;
83
88
  let closePromise;
84
89
  const closed = new Promise(resolve => child.once('close', () => { liveness.destroy(); resolve(); }));
85
- // A guardian that has confirmed and exited closes its control socket; a terminate written late by a
86
- // busy host then fails, possibly before "cleaned" is read. That write error alone is not a protocol
87
- // failure: the guardian exits 0 only after it sent "cleaned" (or after a spawn failure owned nothing).
88
- control.on('error', () => { if (!confirmed && !spawnFailed)
89
- controlFailed = true; });
90
+ // A guardian that has exited no longer reads commands, so a terminate written late by a busy host
91
+ // fails. That failure stays on the command pipe: the host still reads every report to its end.
92
+ commands.on('error', () => { if (!confirmed && !spawnFailed)
93
+ commandFailed = true; });
94
+ reports.on('error', () => { if (!confirmed && !spawnFailed)
95
+ protocolFailed = true; });
90
96
  liveness.on('error', () => { if (!confirmed && !spawnFailed)
91
97
  protocolFailed = true; });
92
- const cleanedByExit = () => controlFailed && !protocolFailed && child.exitCode === 0 && child.signalCode === null;
93
- control.on('data', (chunk) => {
98
+ reports.on('data', (chunk) => {
94
99
  buffer += chunk;
95
100
  if (buffer.length > 8192) {
96
101
  protocolFailed = true;
@@ -132,10 +137,10 @@ export function spawnOwnedGuardian(command, args, options) {
132
137
  });
133
138
  child.once('error', () => { spawnFailed = true; rejectStarted(new Error('Provider process could not start')); });
134
139
  child.once('close', () => { rejectStarted(new Error('Provider process could not start')); });
135
- control.write(JSON.stringify({ type: 'launch', command, args, ...options }) + '\n');
140
+ commands.write(JSON.stringify({ type: 'launch', command, args, ...options }) + '\n');
136
141
  return { child, started, closed, outcome: () => outcome, spawnFailed: () => spawnFailed,
137
- signal(immediate) { if (child.exitCode === null && child.signalCode === null && !confirmed && !spawnFailed && !control.destroyed) {
138
- control.write(JSON.stringify({ type: 'terminate', immediate }) + '\n');
142
+ signal(immediate) { if (child.exitCode === null && child.signalCode === null && !confirmed && !spawnFailed && !commands.destroyed) {
143
+ commands.write(JSON.stringify({ type: 'terminate', immediate }) + '\n');
139
144
  terminateWritten = true;
140
145
  } },
141
146
  terminate(immediate = false) {
@@ -144,7 +149,7 @@ export function spawnOwnedGuardian(command, args, options) {
144
149
  this.signal(immediate);
145
150
  closePromise = new Promise((resolve, reject) => {
146
151
  const timer = setTimeout(() => { protocolFailed = true; hostTimeout = true; liveness.destroy(); reject(new Error('Provider process group cleanup could not be confirmed')); }, 5500);
147
- void closed.then(() => { clearTimeout(timer); if (protocolFailed || !(confirmed || cleanedByExit()))
152
+ void closed.then(() => { clearTimeout(timer); if (protocolFailed || !confirmed)
148
153
  reject(new Error('Provider process group cleanup could not be confirmed'));
149
154
  else
150
155
  resolve(); });
@@ -153,14 +158,14 @@ export function spawnOwnedGuardian(command, args, options) {
153
158
  },
154
159
  /** Fixed cleanup observations after an unconfirmed cleanup; empty once cleanup is confirmed. */
155
160
  cleanupObservations() {
156
- if ((confirmed || cleanedByExit()) && !protocolFailed)
161
+ if (confirmed && !protocolFailed)
157
162
  return [];
158
- if (!protocolFailed && !hostTimeout && !controlFailed)
163
+ if (!protocolFailed && !hostTimeout && !commandFailed)
159
164
  return [];
160
165
  const observed = [terminateWritten ? 'cleanup_guardian_terminate_written' : 'cleanup_guardian_terminate_not_written'];
161
166
  if (hostTimeout)
162
167
  observed.push('cleanup_guardian_host_timeout');
163
- if (controlFailed)
168
+ if (commandFailed)
164
169
  observed.push('cleanup_guardian_control_failed');
165
170
  if (!detail) {
166
171
  if (!hostTimeout && protocolFailed)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mtech-ai-local-runner",
3
- "version": "0.1.41",
3
+ "version": "0.1.42",
4
4
  "private": false,
5
5
  "license": "UNLICENSED",
6
6
  "bin": {