fb543f88d5
- Add session.compacted handler in BackgroundManager to prevent premature task completion after compaction (defer first post-compaction idle) - Explicitly block TodoWrite/TodoRead for council members in all sync points (AgentConfig permission + session tools + prompt instructions) - Add council member prefix check to todo-continuation-enforcer skip list to prevent infinite continuation loops on completed council members - Add optional analysis mode (solo/delegation) question to Athena setup: solo = thorough but heavier, delegation = fast via explore/librarian - Allow call_omo_agent in council member allow-list for delegation mode - Update COUNCIL_MEMBER_PROMPT with TodoWrite prohibition and delegation addendum for when delegation mode is selected - Update prepare_council_prompt tool with mode parameter
102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
import { log } from "../../shared"
|
|
import { MIN_IDLE_TIME_MS } from "./constants"
|
|
import type { BackgroundTask } from "./types"
|
|
|
|
function getString(obj: Record<string, unknown>, key: string): string | undefined {
|
|
const value = obj[key]
|
|
return typeof value === "string" ? value : undefined
|
|
}
|
|
|
|
export function handleSessionIdleBackgroundEvent(args: {
|
|
properties: Record<string, unknown>
|
|
findBySession: (sessionID: string) => BackgroundTask | undefined
|
|
idleDeferralTimers: Map<string, ReturnType<typeof setTimeout>>
|
|
recentlyCompactedSessions?: Set<string>
|
|
validateSessionHasOutput: (sessionID: string) => Promise<boolean>
|
|
checkSessionTodos: (sessionID: string) => Promise<boolean>
|
|
tryCompleteTask: (task: BackgroundTask, source: string) => Promise<boolean>
|
|
emitIdleEvent: (sessionID: string) => void
|
|
}): void {
|
|
const {
|
|
properties,
|
|
findBySession,
|
|
idleDeferralTimers,
|
|
recentlyCompactedSessions,
|
|
validateSessionHasOutput,
|
|
checkSessionTodos,
|
|
tryCompleteTask,
|
|
emitIdleEvent,
|
|
} = args
|
|
|
|
const sessionID = getString(properties, "sessionID")
|
|
if (!sessionID) return
|
|
|
|
const task = findBySession(sessionID)
|
|
if (!task || task.status !== "running") return
|
|
|
|
if (recentlyCompactedSessions?.has(sessionID)) {
|
|
recentlyCompactedSessions.delete(sessionID)
|
|
log("[background-agent] Skipping post-compaction session.idle:", { taskId: task.id, sessionID })
|
|
return
|
|
}
|
|
|
|
const startedAt = task.startedAt
|
|
if (!startedAt) return
|
|
|
|
const elapsedMs = Date.now() - startedAt.getTime()
|
|
if (elapsedMs < MIN_IDLE_TIME_MS) {
|
|
const remainingMs = MIN_IDLE_TIME_MS - elapsedMs
|
|
if (!idleDeferralTimers.has(task.id)) {
|
|
log("[background-agent] Deferring early session.idle:", {
|
|
elapsedMs,
|
|
remainingMs,
|
|
taskId: task.id,
|
|
})
|
|
const timer = setTimeout(() => {
|
|
idleDeferralTimers.delete(task.id)
|
|
emitIdleEvent(sessionID)
|
|
}, remainingMs)
|
|
idleDeferralTimers.set(task.id, timer)
|
|
} else {
|
|
log("[background-agent] session.idle already deferred:", { elapsedMs, taskId: task.id })
|
|
}
|
|
return
|
|
}
|
|
|
|
validateSessionHasOutput(sessionID)
|
|
.then(async (hasValidOutput) => {
|
|
if (task.status !== "running") {
|
|
log("[background-agent] Task status changed during validation, skipping:", {
|
|
taskId: task.id,
|
|
status: task.status,
|
|
})
|
|
return
|
|
}
|
|
|
|
if (!hasValidOutput) {
|
|
log("[background-agent] Session.idle but no valid output yet, waiting:", task.id)
|
|
return
|
|
}
|
|
|
|
const hasIncompleteTodos = await checkSessionTodos(sessionID)
|
|
|
|
if (task.status !== "running") {
|
|
log("[background-agent] Task status changed during todo check, skipping:", {
|
|
taskId: task.id,
|
|
status: task.status,
|
|
})
|
|
return
|
|
}
|
|
|
|
if (hasIncompleteTodos) {
|
|
log("[background-agent] Task has incomplete todos, waiting for todo-continuation:", task.id)
|
|
return
|
|
}
|
|
|
|
await tryCompleteTask(task, "session.idle event")
|
|
})
|
|
.catch((err) => {
|
|
log("[background-agent] Error in session.idle handler:", err)
|
|
})
|
|
}
|