@gobing-ai/ts-ai-runner 0.8.7 → 0.8.9

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.
@@ -21,11 +21,22 @@ const startsWithExit = script[0]?.op === 'exit';
21
21
  let currentTurnId = null;
22
22
  let ignoreCancel = false;
23
23
  let flushing = false;
24
+ let exiting = false;
24
25
  /** Pending sleep timers mapped to their resolvers so a cancel can wake them (not just clear them). */
25
26
  const sleepers = new Map();
26
27
  function send(line) {
27
28
  process.stdout.write(`${JSON.stringify(line)}\n`);
28
29
  }
30
+ function exitAfterOutput(code) {
31
+ if (exiting)
32
+ return;
33
+ exiting = true;
34
+ for (const timer of sleepers.keys())
35
+ clearTimeout(timer);
36
+ sleepers.clear();
37
+ // A synchronous exit can drop queued frames when the parent has not drained the pipe.
38
+ process.stdout.end(() => process.exit(code));
39
+ }
29
40
  // Parent may drop stdout (kill/close) while frames are in flight — exit instead of crashing on EPIPE.
30
41
  process.stdout.on('error', () => process.exit(0));
31
42
  // Handshake: explicit ready action first (nativeSessionId control), else the default.
@@ -75,12 +86,16 @@ async function replay() {
75
86
  await sleep(action.ms ?? 0);
76
87
  else if (action.op === 'ignore-cancel')
77
88
  ignoreCancel = true;
78
- else if (action.op === 'exit')
79
- process.exit(action.code ?? 0);
89
+ else if (action.op === 'exit') {
90
+ exitAfterOutput(action.code ?? 0);
91
+ return;
92
+ }
80
93
  }
81
94
  }
82
95
  const rl = createInterface({ input: process.stdin });
83
96
  rl.on('line', (raw) => {
97
+ if (exiting)
98
+ return;
84
99
  let frame;
85
100
  try {
86
101
  frame = JSON.parse(raw);
@@ -106,7 +121,7 @@ rl.on('line', (raw) => {
106
121
  }
107
122
  });
108
123
  rl.on('close', () => {
109
- process.exit(0);
124
+ exitAfterOutput(0);
110
125
  });
111
126
  // Scripts that lead with `exit` run at startup (direct child drives and handshake tests);
112
127
  // turn-dependent actions replay with a null turnId in that mode.
@@ -1,4 +1,4 @@
1
- import { TurnProtocolError } from '../turn-session.js';
1
+ import { TURN_HANDSHAKE_TIMEOUT_MS, TurnProtocolError } from '../turn-session.js';
2
2
  /** Path of the child script: source tree in dev/checkouts, compiled sibling in built packages. */
3
3
  function fakeTurnAgentPath() {
4
4
  // No node:path/node:fs (runtime-boundaries rule): import.meta.dir is already absolute, so a
@@ -17,7 +17,7 @@ export function createFakeTurnSessionDriver(actions, overrides = {}) {
17
17
  args: [fakeTurnAgentPath(), scriptJson],
18
18
  }),
19
19
  handshake: async (pull) => {
20
- const raw = await pull(2_000);
20
+ const raw = await pull(TURN_HANDSHAKE_TIMEOUT_MS);
21
21
  if (raw === null)
22
22
  return null; // EOF or timeout before ready
23
23
  const frame = JSON.parse(raw);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gobing-ai/ts-ai-runner",
3
- "version": "0.8.7",
3
+ "version": "0.8.9",
4
4
  "description": "@gobing-ai/ts-ai-runner — Coding-agent shims, detection, doctor checks, and prompt execution.",
5
5
  "keywords": [
6
6
  "typescript",
@@ -51,12 +51,12 @@
51
51
  "release": "echo 'Manual publish is disabled. Releases go through GitHub Actions via Trusted Publishing — push a tag: git tag @gobing-ai/ts-ai-runner-v<version> && git push --tags' && exit 1"
52
52
  },
53
53
  "dependencies": {
54
- "@gobing-ai/ts-infra": "^0.8.7",
55
- "@gobing-ai/ts-runtime": "^0.8.7"
54
+ "@gobing-ai/ts-infra": "^0.8.9",
55
+ "@gobing-ai/ts-runtime": "^0.8.9"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@types/bun": "1.4.3",
59
- "@gobing-ai/ts-db": "^0.8.7"
59
+ "@gobing-ai/ts-db": "^0.8.9"
60
60
  },
61
61
  "publishConfig": {
62
62
  "access": "public"
@@ -35,6 +35,7 @@ const startsWithExit = script[0]?.op === 'exit';
35
35
  let currentTurnId: string | null = null;
36
36
  let ignoreCancel = false;
37
37
  let flushing = false;
38
+ let exiting = false;
38
39
  /** Pending sleep timers mapped to their resolvers so a cancel can wake them (not just clear them). */
39
40
  const sleepers = new Map<NodeJS.Timeout, () => void>();
40
41
 
@@ -42,6 +43,15 @@ function send(line: Record<string, unknown>): void {
42
43
  process.stdout.write(`${JSON.stringify(line)}\n`);
43
44
  }
44
45
 
46
+ function exitAfterOutput(code: number): void {
47
+ if (exiting) return;
48
+ exiting = true;
49
+ for (const timer of sleepers.keys()) clearTimeout(timer);
50
+ sleepers.clear();
51
+ // A synchronous exit can drop queued frames when the parent has not drained the pipe.
52
+ process.stdout.end(() => process.exit(code));
53
+ }
54
+
45
55
  // Parent may drop stdout (kill/close) while frames are in flight — exit instead of crashing on EPIPE.
46
56
  process.stdout.on('error', () => process.exit(0));
47
57
 
@@ -88,12 +98,16 @@ async function replay(): Promise<void> {
88
98
  send({ turn: currentTurnId, type: 'output', data: 'x'.repeat(1024 * 1024 + 64) });
89
99
  else if (action.op === 'sleep') await sleep(action.ms ?? 0);
90
100
  else if (action.op === 'ignore-cancel') ignoreCancel = true;
91
- else if (action.op === 'exit') process.exit(action.code ?? 0);
101
+ else if (action.op === 'exit') {
102
+ exitAfterOutput(action.code ?? 0);
103
+ return;
104
+ }
92
105
  }
93
106
  }
94
107
 
95
108
  const rl = createInterface({ input: process.stdin });
96
109
  rl.on('line', (raw) => {
110
+ if (exiting) return;
97
111
  let frame: Record<string, unknown>;
98
112
  try {
99
113
  frame = JSON.parse(raw) as Record<string, unknown>;
@@ -116,7 +130,7 @@ rl.on('line', (raw) => {
116
130
  }
117
131
  });
118
132
  rl.on('close', () => {
119
- process.exit(0);
133
+ exitAfterOutput(0);
120
134
  });
121
135
  // Scripts that lead with `exit` run at startup (direct child drives and handshake tests);
122
136
  // turn-dependent actions replay with a null turnId in that mode.
@@ -5,7 +5,7 @@
5
5
  * Exported from the `./testing` subpath only; never imported by library code.
6
6
  */
7
7
  import type { TurnBinary, TurnHandshake, TurnSessionDriver, TurnSessionPolicy, TurnWireEvent } from '../turn-session';
8
- import { TurnProtocolError } from '../turn-session';
8
+ import { TURN_HANDSHAKE_TIMEOUT_MS, TurnProtocolError } from '../turn-session';
9
9
 
10
10
  /** One scripted child action (see `fake-turn-agent.ts` for the wire semantics). */
11
11
  export interface FakeTurnAction {
@@ -54,7 +54,7 @@ export function createFakeTurnSessionDriver(
54
54
  args: [fakeTurnAgentPath(), scriptJson],
55
55
  }),
56
56
  handshake: async (pull: (timeoutMs: number) => Promise<string | null>): Promise<TurnHandshake> => {
57
- const raw = await pull(2_000);
57
+ const raw = await pull(TURN_HANDSHAKE_TIMEOUT_MS);
58
58
  if (raw === null) return null; // EOF or timeout before ready
59
59
  const frame = JSON.parse(raw) as FakeFrame;
60
60
  if (frame.ctl !== 'ready') return null;