@link-assistant/hive-mind 2.11.7 → 2.11.8

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.
@@ -31,6 +31,9 @@ import { resolveFailedSessionPullRequestState } from './github-pr-state.lib.mjs'
31
31
  // may be an exit code start-command fabricated from the command's own output.
32
32
  import { clearUnverifiedDockerTerminalMarker as clearUnverifiedDockerTerminalMarkerImpl, shouldDeferUnverifiedDockerTerminal as shouldDeferUnverifiedDockerTerminalImpl } from './session-monitor.docker-terminal.lib.mjs';
33
33
  import { isDockerIsolation, sessionStartMs, resolveOomKilledState, resolveStaleExecutingState as resolveStaleExecutingStateImpl } from './session-monitor.stale-executing.lib.mjs';
34
+ // Issue #2134: kill-cause diagnostics + the matching pull-request notice.
35
+ import { buildKillCompletionSections, announceKillOnPullRequest } from './session-monitor.kill-sections.lib.mjs';
36
+ import { runKillRecoveryForCompletion } from './session-kill-resume.lib.mjs';
34
37
 
35
38
  export { formatSessionCompletionMessage, getSessionCompletionExitCode } from './work-session-formatting.lib.mjs';
36
39
  export { DOCKER_TERMINAL_FOOTER_GRACE_MS } from './session-monitor.docker-terminal.lib.mjs';
@@ -550,7 +553,16 @@ async function getIsolationSessionState(sessionName, sessionInfo, options = {})
550
553
 
551
554
  if (statusResult?.exists && statusResult.status) {
552
555
  if (statusResult.oomKilled === true) {
553
- return resolveOomKilledState(sessionName, sessionInfo, statusResult, { verbose, runner, exitFromLog });
556
+ // Issue #2134: `oomKilled` is a *container* flag — the kernel sets it when
557
+ // any process in the cgroup is OOM-killed — so it is verified against the
558
+ // log footer and container liveness before a kill is announced.
559
+ return await resolveOomKilledState(sessionName, sessionInfo, statusResult, {
560
+ verbose,
561
+ runner,
562
+ exitFromLog,
563
+ backendAlive,
564
+ persistSnapshot: () => persistSessionSnapshot(sessionName, sessionInfo),
565
+ });
554
566
  }
555
567
  if (runner.isExecutingSessionStatus(statusResult.status)) {
556
568
  // Issue #1927: an `executing` status is not trusted blindly — verify the
@@ -611,6 +623,27 @@ async function getIsolationSessionState(sessionName, sessionInfo, options = {})
611
623
  if (unverifiedDockerFailure && shouldDeferUnverifiedDockerTerminal(sessionName, sessionInfo, { exitCode, endTime: statusResult.endTime || null, verbose })) {
612
624
  return { running: true, exitCode: null, status: statusResult.status, statusResult, deferred: true };
613
625
  }
626
+ // Issue #2134: even after the grace window, a container that is verifiably
627
+ // still alive cannot have produced a terminal failure — the same liveness
628
+ // ladder used for `oomKilled` applies here, so no kill is announced while
629
+ // the working session keeps running (that is exactly what #2134 reported).
630
+ if (unverifiedDockerFailure) {
631
+ const probe = backendAlive || runner.checkBackendSessionAlive;
632
+ let alive = null;
633
+ if (probe && sessionInfo?.isolationBackend) {
634
+ try {
635
+ alive = await probe(sessionId, sessionInfo.isolationBackend, verbose);
636
+ } catch {
637
+ alive = null;
638
+ }
639
+ }
640
+ if (alive === true) {
641
+ if (verbose) {
642
+ console.log(`[VERBOSE] Session ${sessionName} reported terminal '${statusResult.status}' with exit ${exitCode}, but its docker backend is still alive; keeping the session tracked (issue #2134)`);
643
+ }
644
+ return { running: true, exitCode: null, status: statusResult.status, statusResult, deferred: true };
645
+ }
646
+ }
614
647
  if (!ambiguousDockerTerminal) {
615
648
  clearUnverifiedDockerTerminalMarker(sessionName, sessionInfo);
616
649
  return { running: false, exitCode, status: statusResult.status, statusResult };
@@ -817,6 +850,7 @@ export async function monitorSessions(bot, verbose = false, options = {}) {
817
850
  // Issue #1927: for a killed /solve, offer a command using the last tool
818
851
  // session ID in the log. Do not auto-relaunch work that may reliably OOM.
819
852
  const resumeExtraSections = [];
853
+ let killResumeCommand = null;
820
854
  try {
821
855
  const outcome = classifySessionOutcome({ exitCode: finalExitCode, status: resolvedStatus });
822
856
  const isResumableCommand = (sessionInfo?.command || 'solve') === 'solve';
@@ -831,6 +865,7 @@ export async function monitorSessions(bot, verbose = false, options = {}) {
831
865
  const lastSessionId = readLastSessionIdFromLog(logPath, { verbose });
832
866
  const resumeCommand = buildResumeCommand({ sessionInfo, lastSessionId });
833
867
  const resumeSection = formatResumeSection({ lastSessionId, command: resumeCommand });
868
+ killResumeCommand = resumeCommand || null;
834
869
  if (resumeSection) {
835
870
  resumeExtraSections.push(resumeSection);
836
871
  if (verbose) {
@@ -865,6 +900,42 @@ export async function monitorSessions(bot, verbose = false, options = {}) {
865
900
  }
866
901
  const dockerTaskContainerExtraSections = dockerTaskContainerAction?.extraSection ? [dockerTaskContainerAction.extraSection] : [];
867
902
 
903
+ // Issue #2134: say exactly WHY a session was killed, and warn when a
904
+ // session merely survived a kill event instead of reporting a plain
905
+ // success. The pull request gets the very same report below.
906
+ const killReport = await buildKillCompletionSections({
907
+ sessionName,
908
+ sessionInfo,
909
+ statusResult,
910
+ exitCode: finalExitCode,
911
+ status: resolvedStatus,
912
+ verbose,
913
+ readFile: options.readFile,
914
+ env: options.env || process.env,
915
+ });
916
+
917
+ // Issue #2134: `--on-session-kill=resume` must actually start a new
918
+ // working session, and both surfaces must say so. Done before the
919
+ // message is built so the Telegram report and the pull-request notice
920
+ // below name the very same recovery session.
921
+ let killRecovery = { resumed: false, sessionId: null, attempt: 0, maxAttempts: 0 };
922
+ if (killReport.killed) {
923
+ const recovered = await runKillRecoveryForCompletion({
924
+ sessionName,
925
+ sessionInfo,
926
+ logPath: statusResult?.logPath || sessionInfo?.logPath || null,
927
+ killed: true,
928
+ env: options.env || process.env,
929
+ runner: options.isolationRunner || null,
930
+ trackSession: options.trackSession || trackSession,
931
+ persistSnapshot: () => persistSessionSnapshot(sessionName, sessionInfo),
932
+ locale: sessionInfo?.locale || null,
933
+ verbose,
934
+ });
935
+ killRecovery = recovered.recovery;
936
+ if (recovered.section) killReport.sections.push(recovered.section);
937
+ }
938
+
868
939
  const message = formatSessionCompletionMessage({
869
940
  sessionName,
870
941
  sessionInfo,
@@ -874,9 +945,33 @@ export async function monitorSessions(bot, verbose = false, options = {}) {
874
945
  infoBlock: sessionInfo?.infoBlock || '',
875
946
  pullRequestUrl,
876
947
  pullRequestState,
877
- extraSections: [...limitsExtraSections, ...resumeExtraSections, ...diskExtraSections, ...dockerTaskContainerExtraSections],
948
+ extraSections: [...limitsExtraSections, ...killReport.sections, ...resumeExtraSections, ...diskExtraSections, ...dockerTaskContainerExtraSections],
878
949
  });
879
950
 
951
+ if (killReport.killed || killReport.recovered) {
952
+ const notice = await announceKillOnPullRequest({
953
+ pullRequestUrl,
954
+ sessionName,
955
+ sessionInfo,
956
+ diagnosis: killReport.diagnosis,
957
+ exitCode: finalExitCode,
958
+ observedAt: killReport.observedAt,
959
+ policy: killReport.policy,
960
+ recovered: killReport.recovered,
961
+ resumed: killRecovery.resumed,
962
+ recoverySessionId: killRecovery.resumed ? killRecovery.sessionId : null,
963
+ attempt: killRecovery.resumed ? killRecovery.attempt : null,
964
+ maxAttempts: killRecovery.resumed ? killRecovery.maxAttempts : null,
965
+ resumeCommand: killResumeCommand,
966
+ runCommand: options.runCommand || undefined,
967
+ attachLog: options.attachLog || undefined,
968
+ verbose,
969
+ });
970
+ if (verbose && !notice.posted) {
971
+ console.log(`[VERBOSE] Killed-session notice not posted for ${sessionName}: ${notice.skipped || 'unknown reason'}`);
972
+ }
973
+ }
974
+
880
975
  // Update the original reply message if messageId is available, otherwise send new message
881
976
  let notifyFromChatId = null;
882
977
  let notifyMessageId = null;
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Verified out-of-memory classification for tracked isolation sessions.
3
+ *
4
+ * Issue #2015 made `oomKilled: true` in a `$ --status` record terminal so an
5
+ * OOM-killed session could not be polled forever. Issue #2134 showed the other
6
+ * half of the problem: Docker's `State.OOMKilled` is a *container* flag, not a
7
+ * statement about the container's main process. The kernel sets it when ANY
8
+ * process in the container cgroup is OOM-killed, and it stays `true` afterwards
9
+ * — so a container can be flagged, keep running, and exit 0 (see
10
+ * https://github.com/moby/moby/issues/47618).
11
+ *
12
+ * That is exactly what happened in #2134: the host ran out of memory, the OOM
13
+ * killer terminated a child process, `$ --status` flipped to
14
+ * `executed / exitCode 137`, and Hive Mind announced
15
+ * "Work session killed — out of memory or forced kill (SIGKILL)" while
16
+ * `docker inspect` still reported the container **running**. The session went on
17
+ * for another 3.5 hours and auto-merged its pull request, unmonitored.
18
+ *
19
+ * This module keeps #2015's guarantee (a truly OOM-killed session is terminal)
20
+ * while refusing to declare a kill that is contradicted by stronger evidence,
21
+ * following the same ladder the rest of the monitor already uses:
22
+ *
23
+ * 1. The log FOOTER wins. A written `Exit Code: N` is proof of how the command
24
+ * actually ended — including `Exit Code: 0`, which means the session merely
25
+ * *survived* an OOM event.
26
+ * 2. LIVENESS beats the status record. No footer + the backing container is
27
+ * still alive → the session is still running; keep polling and remember the
28
+ * OOM event so the eventual completion can report the recovery.
29
+ * 3. Otherwise report `oom-killed`, exactly as issue #2015 requires.
30
+ *
31
+ * @see https://github.com/link-assistant/hive-mind/issues/2015
32
+ * @see https://github.com/link-assistant/hive-mind/issues/2134
33
+ */
34
+
35
+ import { classifyExitStatus, normalizeExitCode } from './session-status.lib.mjs';
36
+
37
+ /**
38
+ * Field written on the persisted session snapshot the first time an OOM event is
39
+ * observed for a session that is still alive. It survives a bot restart, so the
40
+ * completion message can still say "recovered from out of memory" hours later.
41
+ */
42
+ export const OOM_EVENT_OBSERVED_FIELD = 'oomEventObservedAt';
43
+
44
+ /**
45
+ * Remember that the kernel OOM killer fired inside this session's container.
46
+ * Idempotent: only the FIRST observation timestamp is kept.
47
+ *
48
+ * @param {Object} sessionInfo - Mutable persisted session info
49
+ * @param {Function} [persistSnapshot] - Callback that mirrors sessionInfo to disk
50
+ * @returns {boolean} True when this call recorded a new observation
51
+ */
52
+ export function markOomEventObserved(sessionInfo, persistSnapshot) {
53
+ if (!sessionInfo || sessionInfo[OOM_EVENT_OBSERVED_FIELD]) return false;
54
+ sessionInfo[OOM_EVENT_OBSERVED_FIELD] = new Date().toISOString();
55
+ if (typeof persistSnapshot === 'function') persistSnapshot();
56
+ return true;
57
+ }
58
+
59
+ /**
60
+ * Whether an OOM event was observed for this session while it was running.
61
+ *
62
+ * @param {Object} sessionInfo
63
+ * @returns {string|null} ISO timestamp of the first observation, or null
64
+ */
65
+ export function getOomEventObservedAt(sessionInfo) {
66
+ return sessionInfo?.[OOM_EVENT_OBSERVED_FIELD] || null;
67
+ }
68
+
69
+ async function probeBackendAlive(sessionName, sessionInfo, { verbose, runner, backendAlive }) {
70
+ if (!sessionInfo?.isolationBackend) return null;
71
+ const probe = backendAlive || runner?.checkBackendSessionAlive;
72
+ if (!probe) return null;
73
+ try {
74
+ return await probe(sessionInfo.sessionId || sessionName, sessionInfo.isolationBackend, verbose);
75
+ } catch (error) {
76
+ if (verbose) {
77
+ console.log(`[VERBOSE] Session ${sessionName} OOM liveness probe failed: ${error?.message || error}`);
78
+ }
79
+ return null;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Resolve the real state of a session whose status record carries
85
+ * `oomKilled: true`.
86
+ *
87
+ * @param {string} sessionName
88
+ * @param {Object} sessionInfo
89
+ * @param {Object} statusResult - Parsed `$ --status` payload
90
+ * @param {Object} deps
91
+ * @param {boolean} [deps.verbose]
92
+ * @param {Object} deps.runner - Isolation runner (for its default probes)
93
+ * @param {Function} [deps.exitFromLog] - Injectable footer reader
94
+ * @param {Function} [deps.backendAlive] - Injectable liveness probe
95
+ * @param {Function} [deps.persistSnapshot] - Persist the session snapshot
96
+ * @returns {Promise<Object>} Monitor state object
97
+ */
98
+ export async function resolveOomKilledState(sessionName, sessionInfo, statusResult, { verbose, runner, exitFromLog, backendAlive, persistSnapshot } = {}) {
99
+ const logPath = statusResult?.logPath || sessionInfo?.logPath || null;
100
+ let footer = null;
101
+ if (logPath) {
102
+ const readFooter = exitFromLog || runner?.readSessionExitFromLog;
103
+ footer = readFooter ? readFooter(logPath, { verbose }) : null;
104
+ }
105
+
106
+ // 1. The authoritative log footer.
107
+ if (footer?.finished) {
108
+ const footerExitCode = normalizeExitCode(footer.exitCode);
109
+ const correctedStatus = classifyExitStatus(footerExitCode) || (footerExitCode === 0 ? 'executed' : 'failed');
110
+ const survivedOom = footerExitCode === 0;
111
+ if (survivedOom) markOomEventObserved(sessionInfo, persistSnapshot);
112
+ if (verbose) {
113
+ console.log(`[VERBOSE] Session ${sessionName} reported oomKilled=true, but its log footer says exit ${footerExitCode} (${correctedStatus}) and wins${survivedOom ? ' — the session SURVIVED the out-of-memory event (issue #2134)' : ''}`);
114
+ }
115
+ return {
116
+ running: false,
117
+ exitCode: footerExitCode,
118
+ status: correctedStatus,
119
+ statusResult: { ...statusResult, status: correctedStatus, exitCode: footerExitCode, endTime: statusResult?.endTime || footer.endTime || null },
120
+ oomEventObserved: true,
121
+ };
122
+ }
123
+
124
+ // 2. Liveness: an alive container cannot have had its command killed.
125
+ const alive = await probeBackendAlive(sessionName, sessionInfo, { verbose, runner, backendAlive });
126
+ if (alive === true) {
127
+ markOomEventObserved(sessionInfo, persistSnapshot);
128
+ if (verbose) {
129
+ console.log(`[VERBOSE] Session ${sessionName} reported oomKilled=true but its ${sessionInfo.isolationBackend} backend is still alive; an OOM event hit the container, not the command — keeping the session tracked (issue #2134)`);
130
+ }
131
+ return { running: true, exitCode: null, status: statusResult?.status || 'executing', statusResult, deferred: true, oomEventObserved: true };
132
+ }
133
+
134
+ // 3. Nothing contradicts the status record: this really is an OOM kill (#2015).
135
+ const statusExitCode = normalizeExitCode(statusResult?.exitCode);
136
+ let exitCode = 137;
137
+ if (statusExitCode !== null && statusExitCode > 0) {
138
+ exitCode = statusExitCode;
139
+ }
140
+ const endTime = statusResult?.endTime || footer?.endTime || statusResult?.currentTime || null;
141
+ const corrected = { ...statusResult, status: 'oom-killed', exitCode, endTime };
142
+
143
+ if (verbose) {
144
+ console.log(`[VERBOSE] Session ${sessionName} status includes oomKilled=true (backend alive: ${alive === null ? 'unknown' : alive}); treating it as terminal oom-killed (exit ${exitCode})`);
145
+ }
146
+
147
+ return { running: false, exitCode, status: 'oom-killed', statusResult: corrected, stale: true, oomEventObserved: true };
148
+ }
@@ -14,7 +14,7 @@
14
14
  * @see https://github.com/link-assistant/hive-mind/issues/2015
15
15
  */
16
16
 
17
- import { classifyExitStatus, normalizeExitCode } from './session-status.lib.mjs';
17
+ import { classifyExitStatus } from './session-status.lib.mjs';
18
18
 
19
19
  /**
20
20
  * Issue #1927: minimum age before a session that `$ --status` still reports as
@@ -122,30 +122,9 @@ export async function resolveStaleExecutingState(sessionName, sessionInfo, statu
122
122
 
123
123
  /**
124
124
  * Issue #2015: `oomKilled` is terminal — the container was killed by the kernel,
125
- * so no further polling can change the outcome.
125
+ * so no further polling can change the outcome. Issue #2134 refined that into a
126
+ * *verified* classification (footer/liveness ladder); the implementation now
127
+ * lives in session-monitor.oom.lib.mjs and is re-exported here so existing
128
+ * importers keep working.
126
129
  */
127
- export function resolveOomKilledState(sessionName, sessionInfo, statusResult, { verbose, runner, exitFromLog }) {
128
- const logPath = statusResult?.logPath || sessionInfo?.logPath || null;
129
- let footer = null;
130
- if (logPath) {
131
- const readFooter = exitFromLog || runner.readSessionExitFromLog;
132
- footer = readFooter ? readFooter(logPath, { verbose }) : null;
133
- }
134
-
135
- const statusExitCode = normalizeExitCode(statusResult?.exitCode);
136
- const footerExitCode = footer?.finished ? normalizeExitCode(footer.exitCode) : null;
137
- let exitCode = 137;
138
- if (statusExitCode !== null && statusExitCode > 0) {
139
- exitCode = statusExitCode;
140
- } else if (footerExitCode !== null && footerExitCode > 0) {
141
- exitCode = footerExitCode;
142
- }
143
- const endTime = statusResult?.endTime || footer?.endTime || statusResult?.currentTime || null;
144
- const corrected = { ...statusResult, status: 'oom-killed', exitCode, endTime };
145
-
146
- if (verbose) {
147
- console.log(`[VERBOSE] Session ${sessionName} status includes oomKilled=true; treating it as terminal oom-killed (exit ${exitCode})`);
148
- }
149
-
150
- return { running: false, exitCode, status: 'oom-killed', statusResult: corrected, stale: true };
151
- }
130
+ export { resolveOomKilledState, markOomEventObserved, getOomEventObservedAt, OOM_EVENT_OBSERVED_FIELD } from './session-monitor.oom.lib.mjs';
@@ -43,6 +43,30 @@ const SESSION_LOG_UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0
43
43
  const DEFAULT_LOG_SCAN_CHUNK_BYTES = 262144;
44
44
  const LOG_SCAN_OVERLAP_BYTES = 1024;
45
45
 
46
+ // Issue #2134: the marker scan runs over a log that can contain *anything* the
47
+ // run printed — including Hive Mind's own source code. `solve` prints its id
48
+ // with `` console.log(`📌 Session ID: ${sessionId}`) ``, so a run that `cat`-ed
49
+ // that source made the literal string `${sessionId}` the "last session id" and
50
+ // the bot offered `--resume "${sessionId}"` to the user. Tool session ids are
51
+ // opaque tokens (UUIDs for claude/codex, slugs elsewhere): a shape check plus a
52
+ // small vocabulary of placeholder words rejects that whole class of false hit
53
+ // without narrowing the ids that are legitimately accepted today.
54
+ const SESSION_ID_SHAPE_RE = /^[A-Za-z0-9][A-Za-z0-9._:-]{6,199}$/;
55
+ const SESSION_ID_PLACEHOLDERS = new Set(['unknown', 'n/a', 'na', 'none', 'null', 'undefined', 'sessionid', 'session_id', 'session-id', 'id']);
56
+
57
+ /**
58
+ * Whether a token captured after a `Session ID:` label can be a real tool
59
+ * session id (issue #2134).
60
+ *
61
+ * @param {*} id - Captured token.
62
+ * @returns {boolean} True when the token has a plausible session-id shape.
63
+ */
64
+ export function isPlausibleSessionId(id) {
65
+ if (!id || typeof id !== 'string') return false;
66
+ if (SESSION_ID_PLACEHOLDERS.has(id.toLowerCase())) return false;
67
+ return SESSION_ID_SHAPE_RE.test(id);
68
+ }
69
+
46
70
  /**
47
71
  * Extract every tool session id printed to a log, in the order they appear.
48
72
  *
@@ -60,8 +84,10 @@ export function extractSessionIds(text) {
60
84
  SESSION_ID_MARKER_RE.lastIndex = 0;
61
85
  while ((match = SESSION_ID_MARKER_RE.exec(text)) !== null) {
62
86
  const id = match[1];
63
- // Skip obvious non-ids that can follow the label in prose/log output.
64
- if (!id || id.toLowerCase() === 'unknown' || id.toLowerCase() === 'n/a') continue;
87
+ // Skip obvious non-ids that can follow the label in prose/log output, and
88
+ // template placeholders such as `${sessionId}` printed by quoted source
89
+ // code (issue #2134).
90
+ if (!isPlausibleSessionId(id)) continue;
65
91
  if (ids[ids.length - 1] !== id) ids.push(id);
66
92
  }
67
93
  return ids;
@@ -154,6 +154,20 @@ export const SOLVE_OPTION_DEFINITIONS = {
154
154
  description: 'DANGEROUS: skip masking known active local tokens in output. This is separate from other sanitization skip flags and should only be used for controlled debugging.',
155
155
  default: false,
156
156
  },
157
+ // Issue #2134: one option decides what happens when a working session is
158
+ // killed (out of memory, disk exhaustion, forced kill). The Telegram bot and
159
+ // solve honour the exact same value, so the report never differs by surface.
160
+ 'on-session-kill': {
161
+ type: 'string',
162
+ description: 'What to do when a working session is killed (out of memory, disk full, forced kill): "report" describes exactly what happened in the pull request and in Telegram, "resume" additionally starts a new working session to recover and says so in both places. Can also be set with HIVE_MIND_ON_SESSION_KILL.',
163
+ choices: ['report', 'resume'],
164
+ default: 'report',
165
+ },
166
+ 'session-kill-resume-attempts': {
167
+ type: 'number',
168
+ description: 'Maximum number of automatic recovery working sessions started for one killed session when --on-session-kill=resume. Can also be set with HIVE_MIND_SESSION_KILL_RESUME_ATTEMPTS.',
169
+ default: 1,
170
+ },
157
171
  'auto-close-pull-request-on-fail': {
158
172
  type: 'boolean',
159
173
  description: 'Automatically close the pull request if execution fails',
@@ -1248,7 +1248,9 @@ let launchAnnouncementShown = false;
1248
1248
 
1249
1249
  function startSessionMonitoringOnce() {
1250
1250
  if (sessionMonitoringTimer) return;
1251
- sessionMonitoringTimer = startSessionMonitoring(bot, VERBOSE);
1251
+ // Issue #2134: the monitor needs the isolation runner to start a recovery
1252
+ // working session when `--on-session-kill=resume` is in effect.
1253
+ sessionMonitoringTimer = startSessionMonitoring(bot, VERBOSE, 30000, { isolationRunner });
1252
1254
  }
1253
1255
 
1254
1256
  // Issue #1927 (requirements #3/#4): a periodic timestamped heartbeat so the "last