refactor(background-agent): optimize task timing and constants management

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-02-01 19:01:30 +09:00
parent 7f9fcc708f
commit 09e738c989
3 changed files with 15 additions and 12 deletions

View File

@@ -6,8 +6,8 @@ export const MIN_STABILITY_TIME_MS = 10 * 1000
export const DEFAULT_STALE_TIMEOUT_MS = 180_000
export const MIN_RUNTIME_BEFORE_STALE_MS = 30_000
export const MIN_IDLE_TIME_MS = 5000
export const POLLING_INTERVAL_MS = 2000
export const TASK_CLEANUP_DELAY_MS = 5 * 60 * 1000
export const POLLING_INTERVAL_MS = 3000
export const TASK_CLEANUP_DELAY_MS = 10 * 60 * 1000
export const TMUX_CALLBACK_DELAY_MS = 200
export type ProcessCleanupEvent = NodeJS.Signals | "beforeExit" | "exit"

View File

@@ -9,6 +9,15 @@ import { log, getAgentToolRestrictions, promptWithModelSuggestionRetry } from ".
import { ConcurrencyManager } from "./concurrency"
import type { BackgroundTaskConfig, TmuxConfig } from "../../config/schema"
import { isInsideTmux } from "../../shared/tmux"
import {
DEFAULT_STALE_TIMEOUT_MS,
MIN_IDLE_TIME_MS,
MIN_RUNTIME_BEFORE_STALE_MS,
MIN_STABILITY_TIME_MS,
POLLING_INTERVAL_MS,
TASK_CLEANUP_DELAY_MS,
TASK_TTL_MS,
} from "./constants"
import { subagentSessions } from "../claude-code-session-state"
import { getTaskToastManager } from "../task-toast-manager"
@@ -16,11 +25,6 @@ import { findNearestMessageWithFields, MESSAGE_STORAGE } from "../hook-message-i
import { existsSync, readdirSync } from "node:fs"
import { join } from "node:path"
const TASK_TTL_MS = 30 * 60 * 1000
const MIN_STABILITY_TIME_MS = 10 * 1000 // Must run at least 10s before stability detection kicks in
const DEFAULT_STALE_TIMEOUT_MS = 180_000 // 3 minutes
const MIN_RUNTIME_BEFORE_STALE_MS = 30_000 // 30 seconds
type ProcessCleanupEvent = NodeJS.Signals | "beforeExit" | "exit"
type OpencodeClient = PluginInput["client"]
@@ -653,7 +657,6 @@ export class BackgroundManager {
// Edge guard: Require minimum elapsed time (5 seconds) before accepting idle
const elapsedMs = Date.now() - startedAt.getTime()
const MIN_IDLE_TIME_MS = 5000
if (elapsedMs < MIN_IDLE_TIME_MS) {
log("[background-agent] Ignoring early session.idle, elapsed:", { elapsedMs, taskId: task.id })
return
@@ -862,7 +865,7 @@ export class BackgroundManager {
this.pollingInterval = setInterval(() => {
this.pollRunningTasks()
}, 2000)
}, POLLING_INTERVAL_MS)
this.pollingInterval.unref()
}
@@ -1091,7 +1094,7 @@ Use \`background_output(task_id="${task.id}")\` to retrieve this result when rea
this.tasks.delete(taskId)
log("[background-agent] Removed completed task from memory:", taskId)
}
}, 5 * 60 * 1000)
}, TASK_CLEANUP_DELAY_MS)
this.completionTimers.set(taskId, timer)
}

View File

@@ -1,4 +1,4 @@
let POLL_INTERVAL_MS = 500
let POLL_INTERVAL_MS = 1000
let MIN_STABILITY_TIME_MS = 10000
let STABILITY_POLLS_REQUIRED = 3
let WAIT_FOR_SESSION_INTERVAL_MS = 100
@@ -19,7 +19,7 @@ export function getTimingConfig() {
}
export function __resetTimingConfig(): void {
POLL_INTERVAL_MS = 500
POLL_INTERVAL_MS = 1000
MIN_STABILITY_TIME_MS = 10000
STABILITY_POLLS_REQUIRED = 3
WAIT_FOR_SESSION_INTERVAL_MS = 100