@agfpd/iapeer 0.4.35 → 0.4.36

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agfpd/iapeer",
3
- "version": "0.4.35",
3
+ "version": "0.4.36",
4
4
  "description": "Run Claude Code and Codex CLI as local background agents that message each other, wake on demand, and share one memory.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "agfpd",
@@ -12,7 +12,7 @@
12
12
  // before any wake or reap. wakeOrSpawn refuses a launchd peer; superviseTick and
13
13
  // sweepZombies skip it. Only daemon-owned (no-plist) peers are managed here.
14
14
 
15
- import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'fs'
15
+ import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'fs'
16
16
  import { homedir } from 'os'
17
17
  import { join } from 'path'
18
18
  import { spawnSync } from 'child_process'
@@ -90,6 +90,10 @@ export interface LifecycleConfig {
90
90
  * A still-WORKING silent-tool worker keeps the pane-log fresh via the TUI render, so it
91
91
  * is no longer reaped mid-task — only a genuinely idle/wedged session crosses this. */
92
92
  ephemeralUnarmedIdleSecs: number
93
+ /** Wake-lock acquisition retries (proper-lockfile). The budget is intentionally MODEST (a full
94
+ * boot can exceed it) — an exhausted budget surfaces as a RETRYABLE FAILED, not a raw ELOCKED, so
95
+ * the sender retries and routeSend logs the outcome. Tunable via IAPEER_WAKE_LOCK_RETRIES. */
96
+ wakeLockRetries: number
93
97
  }
94
98
 
95
99
  export function loadLifecycleConfig(env: NodeJS.ProcessEnv = process.env): LifecycleConfig {
@@ -113,6 +117,10 @@ export function loadLifecycleConfig(env: NodeJS.ProcessEnv = process.env): Lifec
113
117
  crashLoopWindowSecs: num(env.IAPEER_CRASHLOOP_WINDOW_SECS, 300),
114
118
  ephemeralQuietSecs: num(env.IAPEER_EPHEMERAL_QUIET_SECS, 20),
115
119
  ephemeralUnarmedIdleSecs: num(env.IAPEER_EPHEMERAL_UNARMED_IDLE_SECS, 600),
120
+ wakeLockRetries: (() => {
121
+ const n = parseInt(env.IAPEER_WAKE_LOCK_RETRIES ?? '', 10)
122
+ return Number.isFinite(n) && n >= 0 ? n : 30
123
+ })(),
116
124
  }
117
125
  }
118
126
 
@@ -171,6 +179,22 @@ export function removeSessionState(cfg: LifecycleConfig, identity: string): void
171
179
  }
172
180
  }
173
181
 
182
+ /** Atomically CLAIM a dead session for reaping: rename `.session` → `.session.reaping`. Returns true
183
+ * iff THIS caller won the rename. superviseTick runs from BOTH the daemon timer AND the prelude of
184
+ * every wakeOrSpawn (in whatever process called it), so two passes can see the same dead `.session`
185
+ * in the same sub-second window; without a claim BOTH would recordDeath, and TWO deaths for ONE real
186
+ * death can trip the crash-loop guard prematurely (a peer refused to wake for the whole window). The
187
+ * rename is atomic on a single filesystem — the loser gets ENOENT → false and skips recordDeath/eager.
188
+ * A leftover `.reaping` (crash mid-reap) is ignored by readSessionStates (it filters `.session`). */
189
+ function claimDeadSession(cfg: LifecycleConfig, identity: string): boolean {
190
+ try {
191
+ renameSync(sessionStatePath(cfg, identity), `${sessionStatePath(cfg, identity)}.reaping`)
192
+ return true
193
+ } catch {
194
+ return false // ENOENT — another pass already claimed this death (or the file vanished)
195
+ }
196
+ }
197
+
174
198
  // ─────────────────────────────────────────────────────────────────────────────
175
199
  // C1 — durable stopped flag (warm-on-demand stop/start; contract ЖЦ §stop/start,
176
200
  // Демон §stopped). `stop <peer>` on a warm runtime kills the session AND drops this
@@ -702,7 +726,7 @@ export async function withWakeLock<T>(
702
726
  realpath: false,
703
727
  stale: 60_000,
704
728
  update: 5_000,
705
- retries: { retries: 30, factor: 1.3, minTimeout: 100, maxTimeout: 1_000 },
729
+ retries: { retries: cfg.wakeLockRetries, factor: 1.3, minTimeout: 100, maxTimeout: 1_000 },
706
730
  })
707
731
  try {
708
732
  return await fn()
@@ -953,7 +977,7 @@ export async function wakeOrSpawn(args: WakeArgs, deps: WakeDeps = {}): Promise<
953
977
  }
954
978
  }
955
979
 
956
- return withWakeLock(cfg, identity, async () => {
980
+ const runWake = async (): Promise<WakeResult> => {
957
981
  // Re-check the refusal gates INSIDE the lock (audit #3/#11): a `stop` (C1 flag) or a
958
982
  // plist install that completed AFTER the pre-lock check but before the lock was
959
983
  // acquired must still be honored — else a concurrently stopped / launchd-claimed peer
@@ -1074,6 +1098,14 @@ export async function wakeOrSpawn(args: WakeArgs, deps: WakeDeps = {}): Promise<
1074
1098
  try {
1075
1099
  writeSessionState(cfg, { identity, runtime, personality: args.personality, cwd, wokeAt: Date.now() })
1076
1100
  } catch { /* keep the original FAILED reason — enroll is best-effort */ }
1101
+ } else {
1102
+ // В14 — the launch FAILED and left NO live remnant → a genuine boot-crash (broken binary /
1103
+ // doctrine, process died before READY). It writes NO .session, so superviseTick never sees it
1104
+ // and never records the death → countRecentDeaths stays 0 and the crash-loop guard NEVER fires.
1105
+ // Every next message / ephemeral-drain then re-runs the doomed launch (up to bootDeadline each)
1106
+ // forever. Count the fault here so the guard bounds the loop after crashLoopMax in the window.
1107
+ // (No claim needed — there is no .session for a concurrent pass to race on.)
1108
+ recordDeath(cfg, identity, Date.now())
1077
1109
  }
1078
1110
  return { status: 'FAILED', woke: false, runtime, reason: result.reason }
1079
1111
  }
@@ -1102,7 +1134,29 @@ export async function wakeOrSpawn(args: WakeArgs, deps: WakeDeps = {}): Promise<
1102
1134
  addTopic(cfg, identity, args.topic?.trim() ?? '')
1103
1135
  trimDeaths(cfg, identity, cfg.crashLoopWindowSecs, Date.now())
1104
1136
  return { status: 'READY', woke: true, runtime, process_address: identity, taskDelivered: true }
1105
- })
1137
+ }
1138
+
1139
+ try {
1140
+ return await withWakeLock(cfg, identity, runWake)
1141
+ } catch (e) {
1142
+ // В15 — the wake-lock retry budget (~25s) is shorter than a legitimate boot (bootDeadline +
1143
+ // readyGate, up to minutes). A concurrent send to a WAKING peer can exhaust it → proper-lockfile
1144
+ // throws ELOCKED, which otherwise escaped through routeSend as a raw protocol error with NO
1145
+ // delivery.log line (a hole in the very log that reconstructs a suspected loss). Convert it to a
1146
+ // RETRYABLE FAILED: the sender retries, the fast path then finds the now-live session, and routeSend
1147
+ // records the outcome. re-resolving each retry = verify-before-act (no false-OK).
1148
+ const msg = e instanceof Error ? e.message : String(e)
1149
+ if ((e as NodeJS.ErrnoException).code === 'ELOCKED' || /\bELOCKED\b|lock.*already.*held/i.test(msg)) {
1150
+ logWake({ identity, runtime, mode: 'refused', cause: 'wake-lock-held' })
1151
+ return {
1152
+ status: 'FAILED',
1153
+ woke: false,
1154
+ runtime,
1155
+ reason: `"${args.personality}" (${runtime}) wake already in progress (another wake holds the lock) — retry shortly`,
1156
+ }
1157
+ }
1158
+ throw e
1159
+ }
1106
1160
  }
1107
1161
 
1108
1162
  // ─────────────────────────────────────────────────────────────────────────────
@@ -1200,8 +1254,15 @@ function superviseOnePeer(s: SessionState, ctx: SuperviseCtx): SuperviseOutcome
1200
1254
  // A dead session: record a death for crash-loop accounting, then branch on the
1201
1255
  // .new-eager mark. This death was NOT daemon-initiated (the daemon only initiates
1202
1256
  // the idle-reap below) → it died on its own → do NOT write .idle-reaped here.
1257
+ // ATOMIC CLAIM (В16): a concurrent supervise pass (daemon timer + a CLI-process
1258
+ // wakeOrSpawn prelude) must not BOTH recordDeath — two deaths for one real death can
1259
+ // trip the crash-loop guard prematurely. The rename-claim makes recordDeath + eager
1260
+ // happen exactly once; the loser reports a benign concurrently-reaped outcome.
1261
+ if (!claimDeadSession(cfg, s.identity)) {
1262
+ return { identity: s.identity, action: 'reaped-gone', reason: 'concurrently reaped by another supervise pass' }
1263
+ }
1203
1264
  recordDeath(cfg, s.identity, nowMs)
1204
- removeSessionState(cfg, s.identity)
1265
+ rmSync(`${sessionStatePath(cfg, s.identity)}.reaping`, { force: true }) // claim token consumed
1205
1266
  // The .ephemeral-armed mark belongs to THIS (now dead) session — it armed on its
1206
1267
  // outbound reply. Clear it with the session, so a successor session can never be
1207
1268
  // quiet-reaped on a stale mark before answering its own task. No-op otherwise.
@@ -1229,6 +1290,21 @@ function superviseOnePeer(s: SessionState, ctx: SuperviseCtx): SuperviseOutcome
1229
1290
  trace({ identity: s.identity, action: 'reaped-gone', death: gone.death, reason: gone.reason, outcome: 'fresh-next-msg' })
1230
1291
  return { identity: s.identity, action: 'reaped-gone', reason: gone.reason }
1231
1292
  }
1293
+ // В17 — a LIVE session carrying the .stopped flag is an illegitimate state: `stop` takes no wake-lock,
1294
+ // so a stop that raced an in-flight wake killed the pre-pidfile session as a no-op and the wake then
1295
+ // drove a NEW session to READY → alive + .stopped. The live path (routeSend) does not re-check
1296
+ // .stopped, so this peer keeps accepting messages until idle-reap (up to 1h) — the operator halt
1297
+ // silently failed. Reap it now (catch-up for the window the wake-lock cannot cover). H4 is already
1298
+ // cleared above; no legitimate alive+stopped state exists. Park like the stop verb (.idle-reaped is
1299
+ // idempotent with it) — .stopped keeps the peer down until `start`, .idle-reaped makes it resume-clean.
1300
+ if (isStopped(cfg, s.identity)) {
1301
+ kill(sock, s.identity)
1302
+ setIdleReaped(cfg, s.identity)
1303
+ clearEphemeralArmed(cfg, s.identity)
1304
+ removeSessionState(cfg, s.identity)
1305
+ trace({ identity: s.identity, action: 'reaped-idle', reason: 'alive-but-stopped', outcome: 'operator-halt-enforced' })
1306
+ return { identity: s.identity, action: 'reaped-idle', reason: 'live session carried .stopped — operator halt enforced (stop-vs-wake race)' }
1307
+ }
1232
1308
  // Idle accounting via the LAST MEANINGFUL TRANSCRIPT ENTRY's content-timestamp (adapter.lastTurnMtime)
1233
1309
  // — the only RELIABLE idle signal. The two raw mtimes both report FALSE freshness for an idle session:
1234
1310
  // • pane-log mtime — a statusline / footer re-render writes pty bytes at idle (the clock/usage/ctx%