@tyhld/conductor 0.12.159 → 0.12.160

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.
Files changed (2) hide show
  1. package/dist/headless.js +40 -10
  2. package/package.json +1 -1
package/dist/headless.js CHANGED
@@ -1146,6 +1146,11 @@ function openRunLogs(commandId, attempt) {
1146
1146
  // message/parent_tool_use_id/session_id/type/uuid だけ)。時刻つきの流れを出すには
1147
1147
  // 拾った側で時刻を刻んで残すしかない。ここには秘密を隠して上限で切った物だけを書く。
1148
1148
  const eventsLog = createWriteStream(eventsLogPath(commandId, attempt), { flags: 'a' });
1149
+ // ★開く・書くのは裏で後から起きる。失敗(フォルダが先に消えた・空きが無い等)を受け止めないと
1150
+ // 中継ごと落ちる(uncaughtException)。記録が残せないだけなので、知らせて仕事は続ける。
1151
+ for (const ws of [stdoutLog, stderrLog, eventsLog]) {
1152
+ ws.on('error', (err) => log(`WARN 実行ログを書けません: ${err.message}`));
1153
+ }
1149
1154
  return { stdoutLog, stderrLog, eventsLog };
1150
1155
  }
1151
1156
  catch (err) {
@@ -1153,14 +1158,34 @@ function openRunLogs(commandId, attempt) {
1153
1158
  return null;
1154
1159
  }
1155
1160
  }
1156
- /** WriteStream を安全に閉じる。 */
1161
+ /** 実行ログを閉じ終わるのを待つ上限(ms)。書き込みが詰まっても止め方を止めない。 */
1162
+ const RUN_LOG_CLOSE_WAIT_MS = 2000;
1163
+ /**
1164
+ * WriteStream を安全に閉じ、閉じ終わる(開く・書く・閉じるが裏で全部済む)まで待つ。
1165
+ * ★end() だけでは裏の書き込みが残る=止めた後に記録ファイルへ書きに行く(便 edbfbee0)。
1166
+ */
1157
1167
  function closeLog(ws) {
1158
- if (ws) {
1168
+ if (!ws || ws.closed)
1169
+ return Promise.resolve();
1170
+ return new Promise((resolve) => {
1171
+ const t = setTimeout(resolve, RUN_LOG_CLOSE_WAIT_MS);
1172
+ t.unref();
1173
+ ws.once('close', () => {
1174
+ clearTimeout(t);
1175
+ resolve();
1176
+ });
1159
1177
  try {
1160
1178
  ws.end();
1161
1179
  }
1162
- catch { /* ignore */ }
1163
- }
1180
+ catch {
1181
+ clearTimeout(t);
1182
+ resolve();
1183
+ }
1184
+ });
1185
+ }
1186
+ /** 便の3つの実行ログを閉じ、全部閉じ終わるまで待つ。 */
1187
+ async function closeJobLogs(job) {
1188
+ await Promise.all([closeLog(job.stdoutLog), closeLog(job.stderrLog), closeLog(job.eventsLog)]);
1164
1189
  }
1165
1190
  // ─────────────────────────────────────────────────────────────────────────────
1166
1191
  // 子プロセスの起動と停止
@@ -1336,6 +1361,8 @@ resumeNote) {
1336
1361
  job.exitSignal = null;
1337
1362
  job.finishedAt = Date.now();
1338
1363
  job.stderrBuf += `\nspawn error: ${err.message}`;
1364
+ // 起動できなかったときは 'exit' が来ないので、ここで実行ログを閉じる
1365
+ job.logsClosed = closeJobLogs(job);
1339
1366
  }
1340
1367
  });
1341
1368
  // stdin: 指示本文を書いて閉じる
@@ -1421,19 +1448,22 @@ resumeNote) {
1421
1448
  job.exitSignal = signal?.toString() ?? null;
1422
1449
  job.finishedAt = Date.now();
1423
1450
  job.durationMs = Date.now() - job.startedAt;
1424
- closeLog(job.stdoutLog);
1425
- closeLog(job.stderrLog);
1426
- closeLog(job.eventsLog);
1451
+ job.logsClosed = closeJobLogs(job);
1427
1452
  });
1428
1453
  return job;
1429
1454
  }
1430
1455
  /**
1431
1456
  * 子プロセスを停止する。SIGTERM → 猶予 → SIGKILL。
1432
- * プロセスが既に終わっていれば何もしない。
1457
+ * プロセスが既に終わっていれば止めない(実行ログを閉じ終わるのだけ待つ)。
1433
1458
  */
1434
1459
  export async function stopJob(job) {
1435
- if (job.completed)
1436
- return;
1460
+ if (!job.completed)
1461
+ await killJob(job);
1462
+ // ★実行ログを閉じ終わってから返す=止めた後に記録ファイルへの書き込みを残さない(便 edbfbee0)。
1463
+ await job.logsClosed;
1464
+ }
1465
+ /** SIGTERM → 猶予 → SIGKILL で子プロセスを終わらせる。 */
1466
+ async function killJob(job) {
1437
1467
  const proc = job.process;
1438
1468
  proc.kill('SIGTERM');
1439
1469
  // 猶予の間に終了するのを待つ
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tyhld/conductor",
3
- "version": "0.12.159",
3
+ "version": "0.12.160",
4
4
  "description": "采配くん管制の見守りアプリ(conductor-agent)。各PCで常駐し、中央(devlog-tracker)へ定期的に生存報告(heartbeat)を送る常駐CLI。",
5
5
  "type": "module",
6
6
  "bin": {