()
| 222 | } |
| 223 | |
| 224 | async function main() { |
| 225 | let input; |
| 226 | try { |
| 227 | const chunks = []; |
| 228 | for await (const chunk of process.stdin) chunks.push(chunk); |
| 229 | input = JSON.parse(Buffer.concat(chunks).toString()); |
| 230 | } catch { |
| 231 | log("skip", { stage: "stdin_parse", reason: "invalid input" }); |
| 232 | noop(); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | const source = input.source || "unknown"; |
| 237 | const newSessionId = input.session_id || "unknown"; |
| 238 | log("start", { source, newSessionId, activeWindowMs: ACTIVE_WINDOW_MS, idleTtlMs: IDLE_TTL_MS }); |
| 239 | |
| 240 | try { |
| 241 | await detectRecallCompressorProfile(cfg, { log, logError }); |
| 242 | } catch (err) { |
| 243 | logError("compress_profile_detect_uncaught", err); |
| 244 | } |
| 245 | |
| 246 | if (source === "resume") { |
| 247 | await injectResumeArchive(newSessionId); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | // Other non-startup sources are hard no-ops. We don't sweep there, because |
| 252 | // reconnect-like sources may fire often and sweep should stay tied to a new |
| 253 | // session boundary. |
| 254 | if (source !== "startup" && source !== "clear") { |
| 255 | log("skip", { stage: "source_check", reason: `source=${source} (only startup|clear act)` }); |
| 256 | noop(); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | const health = await fetchJSON("/health"); |
| 261 | if (!health) { |
| 262 | logError("health_check", "server unreachable; skipping commit + sweep"); |
| 263 | noop(); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | const now = Date.now(); |
| 268 | const states = await listStates(); |
| 269 | |
| 270 | // ------------------------------------------------------------------------- |
| 271 | // Active-window heuristic (DESIGN.md §3) |
| 272 | // ------------------------------------------------------------------------- |
| 273 | const otherStates = states.filter( |
| 274 | (s) => s?.codexSessionId && s.codexSessionId !== newSessionId, |
| 275 | ); |
| 276 | |
| 277 | const recentlyActive = otherStates.filter( |
| 278 | (s) => typeof s.lastUpdatedAt === "number" |
| 279 | && (now - s.lastUpdatedAt) <= ACTIVE_WINDOW_MS, |
| 280 | ); |
| 281 |
no test coverage detected