mirror of
https://mirror.skon.top/github.com/code-yeongyu/oh-my-opencode
synced 2026-04-30 18:50:29 +08:00
fix(background-agent): add post-compaction continuation + fix stale/idle race
Extract sendPostCompactionContinuation to dedicated file — council members now resume after compaction instead of silently failing. Refresh lastUpdate before async validation in both idle handler and polling path to prevent stale timeout from racing with completion detection. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import type { PluginInput } from "@opencode-ai/plugin"
|
||||
import type { BackgroundTask } from "./types"
|
||||
import {
|
||||
log,
|
||||
getAgentToolRestrictions,
|
||||
createInternalAgentTextPart,
|
||||
} from "../../shared"
|
||||
import { setSessionTools } from "../../shared/session-tools-store"
|
||||
|
||||
type OpencodeClient = PluginInput["client"]
|
||||
|
||||
const CONTINUATION_PROMPT =
|
||||
"Your session was compacted (context summarized). Continue your analysis from where you left off. Report your findings when done."
|
||||
|
||||
export function sendPostCompactionContinuation(
|
||||
client: OpencodeClient,
|
||||
task: BackgroundTask,
|
||||
sessionID: string,
|
||||
): void {
|
||||
if (task.status !== "running") return
|
||||
|
||||
const resumeModel = task.model
|
||||
? { providerID: task.model.providerID, modelID: task.model.modelID }
|
||||
: undefined
|
||||
const resumeVariant = task.model?.variant
|
||||
|
||||
client.session.promptAsync({
|
||||
path: { id: sessionID },
|
||||
body: {
|
||||
agent: task.agent,
|
||||
...(resumeModel ? { model: resumeModel } : {}),
|
||||
...(resumeVariant ? { variant: resumeVariant } : {}),
|
||||
tools: (() => {
|
||||
const tools = {
|
||||
task: false,
|
||||
call_omo_agent: true,
|
||||
question: false,
|
||||
...getAgentToolRestrictions(task.agent),
|
||||
}
|
||||
setSessionTools(sessionID, tools)
|
||||
return tools
|
||||
})(),
|
||||
parts: [createInternalAgentTextPart(CONTINUATION_PROMPT)],
|
||||
},
|
||||
}).catch((error) => {
|
||||
log("[background-agent] Post-compaction continuation error:", {
|
||||
taskId: task.id,
|
||||
error: String(error),
|
||||
})
|
||||
})
|
||||
|
||||
if (task.progress) {
|
||||
task.progress.lastUpdate = new Date()
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ export function handleSessionIdleBackgroundEvent(args: {
|
||||
findBySession: (sessionID: string) => BackgroundTask | undefined
|
||||
idleDeferralTimers: Map<string, ReturnType<typeof setTimeout>>
|
||||
recentlyCompactedSessions?: Set<string>
|
||||
onPostCompactionIdle?: (task: BackgroundTask, sessionID: string) => void
|
||||
validateSessionHasOutput: (sessionID: string) => Promise<boolean>
|
||||
checkSessionTodos: (sessionID: string) => Promise<boolean>
|
||||
tryCompleteTask: (task: BackgroundTask, source: string) => Promise<boolean>
|
||||
@@ -22,6 +23,7 @@ export function handleSessionIdleBackgroundEvent(args: {
|
||||
findBySession,
|
||||
idleDeferralTimers,
|
||||
recentlyCompactedSessions,
|
||||
onPostCompactionIdle,
|
||||
validateSessionHasOutput,
|
||||
checkSessionTodos,
|
||||
tryCompleteTask,
|
||||
@@ -37,6 +39,7 @@ export function handleSessionIdleBackgroundEvent(args: {
|
||||
if (recentlyCompactedSessions?.has(sessionID)) {
|
||||
recentlyCompactedSessions.delete(sessionID)
|
||||
log("[background-agent] Skipping post-compaction session.idle:", { taskId: task.id, sessionID })
|
||||
onPostCompactionIdle?.(task, sessionID)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -63,6 +66,13 @@ export function handleSessionIdleBackgroundEvent(args: {
|
||||
return
|
||||
}
|
||||
|
||||
// Refresh lastUpdate to prevent stale timeout from racing with this async validation.
|
||||
// Without this, checkAndInterruptStaleTasks can kill the task synchronously
|
||||
// while validateSessionHasOutput is still awaiting an API response.
|
||||
if (task.progress) {
|
||||
task.progress.lastUpdate = new Date()
|
||||
}
|
||||
|
||||
validateSessionHasOutput(sessionID)
|
||||
.then(async (hasValidOutput) => {
|
||||
if (task.status !== "running") {
|
||||
|
||||
Reference in New Issue
Block a user