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:
parent
7f9fcc708f
commit
09e738c989
@ -6,8 +6,8 @@ export const MIN_STABILITY_TIME_MS = 10 * 1000
|
|||||||
export const DEFAULT_STALE_TIMEOUT_MS = 180_000
|
export const DEFAULT_STALE_TIMEOUT_MS = 180_000
|
||||||
export const MIN_RUNTIME_BEFORE_STALE_MS = 30_000
|
export const MIN_RUNTIME_BEFORE_STALE_MS = 30_000
|
||||||
export const MIN_IDLE_TIME_MS = 5000
|
export const MIN_IDLE_TIME_MS = 5000
|
||||||
export const POLLING_INTERVAL_MS = 2000
|
export const POLLING_INTERVAL_MS = 3000
|
||||||
export const TASK_CLEANUP_DELAY_MS = 5 * 60 * 1000
|
export const TASK_CLEANUP_DELAY_MS = 10 * 60 * 1000
|
||||||
export const TMUX_CALLBACK_DELAY_MS = 200
|
export const TMUX_CALLBACK_DELAY_MS = 200
|
||||||
|
|
||||||
export type ProcessCleanupEvent = NodeJS.Signals | "beforeExit" | "exit"
|
export type ProcessCleanupEvent = NodeJS.Signals | "beforeExit" | "exit"
|
||||||
|
|||||||
@ -9,6 +9,15 @@ import { log, getAgentToolRestrictions, promptWithModelSuggestionRetry } from ".
|
|||||||
import { ConcurrencyManager } from "./concurrency"
|
import { ConcurrencyManager } from "./concurrency"
|
||||||
import type { BackgroundTaskConfig, TmuxConfig } from "../../config/schema"
|
import type { BackgroundTaskConfig, TmuxConfig } from "../../config/schema"
|
||||||
import { isInsideTmux } from "../../shared/tmux"
|
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 { subagentSessions } from "../claude-code-session-state"
|
||||||
import { getTaskToastManager } from "../task-toast-manager"
|
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 { existsSync, readdirSync } from "node:fs"
|
||||||
import { join } from "node:path"
|
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 ProcessCleanupEvent = NodeJS.Signals | "beforeExit" | "exit"
|
||||||
|
|
||||||
type OpencodeClient = PluginInput["client"]
|
type OpencodeClient = PluginInput["client"]
|
||||||
@ -653,7 +657,6 @@ export class BackgroundManager {
|
|||||||
|
|
||||||
// Edge guard: Require minimum elapsed time (5 seconds) before accepting idle
|
// Edge guard: Require minimum elapsed time (5 seconds) before accepting idle
|
||||||
const elapsedMs = Date.now() - startedAt.getTime()
|
const elapsedMs = Date.now() - startedAt.getTime()
|
||||||
const MIN_IDLE_TIME_MS = 5000
|
|
||||||
if (elapsedMs < MIN_IDLE_TIME_MS) {
|
if (elapsedMs < MIN_IDLE_TIME_MS) {
|
||||||
log("[background-agent] Ignoring early session.idle, elapsed:", { elapsedMs, taskId: task.id })
|
log("[background-agent] Ignoring early session.idle, elapsed:", { elapsedMs, taskId: task.id })
|
||||||
return
|
return
|
||||||
@ -862,7 +865,7 @@ export class BackgroundManager {
|
|||||||
|
|
||||||
this.pollingInterval = setInterval(() => {
|
this.pollingInterval = setInterval(() => {
|
||||||
this.pollRunningTasks()
|
this.pollRunningTasks()
|
||||||
}, 2000)
|
}, POLLING_INTERVAL_MS)
|
||||||
this.pollingInterval.unref()
|
this.pollingInterval.unref()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1091,7 +1094,7 @@ Use \`background_output(task_id="${task.id}")\` to retrieve this result when rea
|
|||||||
this.tasks.delete(taskId)
|
this.tasks.delete(taskId)
|
||||||
log("[background-agent] Removed completed task from memory:", taskId)
|
log("[background-agent] Removed completed task from memory:", taskId)
|
||||||
}
|
}
|
||||||
}, 5 * 60 * 1000)
|
}, TASK_CLEANUP_DELAY_MS)
|
||||||
this.completionTimers.set(taskId, timer)
|
this.completionTimers.set(taskId, timer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
let POLL_INTERVAL_MS = 500
|
let POLL_INTERVAL_MS = 1000
|
||||||
let MIN_STABILITY_TIME_MS = 10000
|
let MIN_STABILITY_TIME_MS = 10000
|
||||||
let STABILITY_POLLS_REQUIRED = 3
|
let STABILITY_POLLS_REQUIRED = 3
|
||||||
let WAIT_FOR_SESSION_INTERVAL_MS = 100
|
let WAIT_FOR_SESSION_INTERVAL_MS = 100
|
||||||
@ -19,7 +19,7 @@ export function getTimingConfig() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function __resetTimingConfig(): void {
|
export function __resetTimingConfig(): void {
|
||||||
POLL_INTERVAL_MS = 500
|
POLL_INTERVAL_MS = 1000
|
||||||
MIN_STABILITY_TIME_MS = 10000
|
MIN_STABILITY_TIME_MS = 10000
|
||||||
STABILITY_POLLS_REQUIRED = 3
|
STABILITY_POLLS_REQUIRED = 3
|
||||||
WAIT_FOR_SESSION_INTERVAL_MS = 100
|
WAIT_FOR_SESSION_INTERVAL_MS = 100
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user