fix: guard session_ids with optional chaining to prevent crash
boulderState?.session_ids.includes() only guards boulderState, not session_ids. If boulder.json is corrupted or missing the field, session_ids is undefined and .includes() crashes silently, losing subagent results. Changes: - readBoulderState: validate parsed JSON is object, default session_ids to [] - atlas hook line 427: boulderState?.session_ids?.includes - atlas hook line 655: boulderState?.session_ids?.includes - prometheus-md-only line 93: boulderState?.session_ids?.includes - appendSessionId: guard with ?. and initialize to [] if missing Fixes #1672
This commit is contained in:
parent
95a4e971a0
commit
d60697bb13
@ -22,7 +22,14 @@ export function readBoulderState(directory: string): BoulderState | null {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const content = readFileSync(filePath, "utf-8")
|
const content = readFileSync(filePath, "utf-8")
|
||||||
return JSON.parse(content) as BoulderState
|
const parsed = JSON.parse(content)
|
||||||
|
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
if (!Array.isArray(parsed.session_ids)) {
|
||||||
|
parsed.session_ids = []
|
||||||
|
}
|
||||||
|
return parsed as BoulderState
|
||||||
} catch {
|
} catch {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@ -48,7 +55,10 @@ export function appendSessionId(directory: string, sessionId: string): BoulderSt
|
|||||||
const state = readBoulderState(directory)
|
const state = readBoulderState(directory)
|
||||||
if (!state) return null
|
if (!state) return null
|
||||||
|
|
||||||
if (!state.session_ids.includes(sessionId)) {
|
if (!state.session_ids?.includes(sessionId)) {
|
||||||
|
if (!Array.isArray(state.session_ids)) {
|
||||||
|
state.session_ids = []
|
||||||
|
}
|
||||||
state.session_ids.push(sessionId)
|
state.session_ids.push(sessionId)
|
||||||
if (writeBoulderState(directory, state)) {
|
if (writeBoulderState(directory, state)) {
|
||||||
return state
|
return state
|
||||||
|
|||||||
@ -41,7 +41,7 @@ export function createAtlasEventHandler(input: {
|
|||||||
|
|
||||||
// Read boulder state FIRST to check if this session is part of an active boulder
|
// Read boulder state FIRST to check if this session is part of an active boulder
|
||||||
const boulderState = readBoulderState(ctx.directory)
|
const boulderState = readBoulderState(ctx.directory)
|
||||||
const isBoulderSession = boulderState?.session_ids.includes(sessionID) ?? false
|
const isBoulderSession = boulderState?.session_ids?.includes(sessionID) ?? false
|
||||||
|
|
||||||
const isBackgroundTaskSession = subagentSessions.has(sessionID)
|
const isBackgroundTaskSession = subagentSessions.has(sessionID)
|
||||||
|
|
||||||
|
|||||||
@ -65,7 +65,7 @@ export function createToolExecuteAfterHandler(input: {
|
|||||||
if (boulderState) {
|
if (boulderState) {
|
||||||
const progress = getPlanProgress(boulderState.active_plan)
|
const progress = getPlanProgress(boulderState.active_plan)
|
||||||
|
|
||||||
if (toolInput.sessionID && !boulderState.session_ids.includes(toolInput.sessionID)) {
|
if (toolInput.sessionID && !boulderState.session_ids?.includes(toolInput.sessionID)) {
|
||||||
appendSessionId(ctx.directory, toolInput.sessionID)
|
appendSessionId(ctx.directory, toolInput.sessionID)
|
||||||
log(`[${HOOK_NAME}] Appended session to boulder`, {
|
log(`[${HOOK_NAME}] Appended session to boulder`, {
|
||||||
sessionID: toolInput.sessionID,
|
sessionID: toolInput.sessionID,
|
||||||
|
|||||||
@ -43,7 +43,7 @@ export function getAgentFromSession(sessionID: string, directory: string): strin
|
|||||||
|
|
||||||
// Check boulder state (persisted across restarts) - fixes #927
|
// Check boulder state (persisted across restarts) - fixes #927
|
||||||
const boulderState = readBoulderState(directory)
|
const boulderState = readBoulderState(directory)
|
||||||
if (boulderState?.session_ids.includes(sessionID) && boulderState.agent) {
|
if (boulderState?.session_ids?.includes(sessionID) && boulderState.agent) {
|
||||||
return boulderState.agent
|
return boulderState.agent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user