2026-02-08 15:01:42 +09:00
|
|
|
import { log } from "../../shared/logger"
|
|
|
|
|
import { SYSTEM_DIRECTIVE_PREFIX } from "../../shared/system-directive"
|
|
|
|
|
import { isCallerOrchestrator } from "../../shared/session-utils"
|
2026-02-14 19:24:30 +09:00
|
|
|
import type { PluginInput } from "@opencode-ai/plugin"
|
2026-05-11 14:27:03 +09:00
|
|
|
import { existsSync, readFileSync } from "node:fs"
|
|
|
|
|
import { resolve } from "node:path"
|
|
|
|
|
import { getWorkForSession, readBoulderState, readCurrentTopLevelTask, resolveBoulderPlanPath, resolveBoulderPlanPathForWork } from "../../features/boulder-state"
|
2026-02-08 15:01:42 +09:00
|
|
|
import { HOOK_NAME } from "./hook-name"
|
|
|
|
|
import { ORCHESTRATOR_DELEGATION_REQUIRED, SINGLE_TASK_DIRECTIVE } from "./system-reminder-templates"
|
|
|
|
|
import { isSisyphusPath } from "./sisyphus-path"
|
2026-03-18 17:31:27 +09:00
|
|
|
import type { PendingTaskRef, TrackedTopLevelTaskRef } from "./types"
|
2026-02-08 15:01:42 +09:00
|
|
|
import { isWriteOrEditToolName } from "./write-edit-tool-policy"
|
|
|
|
|
|
|
|
|
|
export function createToolExecuteBeforeHandler(input: {
|
2026-02-14 19:24:30 +09:00
|
|
|
ctx: PluginInput
|
2026-02-08 15:01:42 +09:00
|
|
|
pendingFilePaths: Map<string, string>
|
2026-03-18 17:31:27 +09:00
|
|
|
pendingTaskRefs: Map<string, PendingTaskRef>
|
2026-05-11 14:27:03 +09:00
|
|
|
pendingPlanSnapshots?: Map<string, string>
|
2026-05-07 17:47:53 +09:00
|
|
|
isCallerOrchestrator?: (sessionID: string | undefined) => Promise<boolean>
|
2026-02-08 15:01:42 +09:00
|
|
|
}): (
|
|
|
|
|
toolInput: { tool: string; sessionID?: string; callID?: string },
|
|
|
|
|
toolOutput: { args: Record<string, unknown>; message?: string }
|
|
|
|
|
) => Promise<void> {
|
2026-05-11 14:27:03 +09:00
|
|
|
const { ctx, pendingFilePaths, pendingTaskRefs, pendingPlanSnapshots } = input
|
2026-05-07 17:47:53 +09:00
|
|
|
const resolveIsCallerOrchestrator = input.isCallerOrchestrator ?? ((sessionID) => isCallerOrchestrator(sessionID, ctx.client))
|
2026-02-08 15:01:42 +09:00
|
|
|
|
2026-03-18 17:31:27 +09:00
|
|
|
function trackTask(callID: string, task: TrackedTopLevelTaskRef): void {
|
|
|
|
|
pendingTaskRefs.set(callID, { kind: "track", task })
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 15:01:42 +09:00
|
|
|
return async (toolInput, toolOutput): Promise<void> => {
|
2026-05-07 17:47:53 +09:00
|
|
|
if (!(await resolveIsCallerOrchestrator(toolInput.sessionID))) {
|
2026-02-08 15:01:42 +09:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check Write/Edit tools for orchestrator - inject strong warning
|
2026-02-10 21:58:26 +09:00
|
|
|
// Warn-only policy: Atlas guides orchestrators toward delegation but doesn't block, allowing flexibility for urgent fixes
|
2026-02-08 15:01:42 +09:00
|
|
|
if (isWriteOrEditToolName(toolInput.tool)) {
|
|
|
|
|
const filePath = (toolOutput.args.filePath ?? toolOutput.args.path ?? toolOutput.args.file) as string | undefined
|
|
|
|
|
if (filePath && !isSisyphusPath(filePath)) {
|
|
|
|
|
// Store filePath for use in tool.execute.after
|
|
|
|
|
if (toolInput.callID) {
|
|
|
|
|
pendingFilePaths.set(toolInput.callID, filePath)
|
2026-05-11 14:27:03 +09:00
|
|
|
|
|
|
|
|
const sessionID = toolInput.sessionID
|
|
|
|
|
const sessionWork = sessionID
|
|
|
|
|
? getWorkForSession(ctx.directory, sessionID)
|
|
|
|
|
: null
|
|
|
|
|
const state = sessionWork ? null : readBoulderState(ctx.directory)
|
|
|
|
|
const planPath = sessionWork
|
|
|
|
|
? resolveBoulderPlanPathForWork(ctx.directory, sessionWork)
|
|
|
|
|
: state
|
|
|
|
|
? resolveBoulderPlanPath(ctx.directory, state)
|
|
|
|
|
: null
|
|
|
|
|
|
|
|
|
|
if (planPath && resolve(filePath) === resolve(planPath) && pendingPlanSnapshots) {
|
|
|
|
|
try {
|
|
|
|
|
if (existsSync(planPath)) {
|
|
|
|
|
pendingPlanSnapshots.set(toolInput.callID, readFileSync(planPath, "utf-8"))
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
pendingPlanSnapshots.delete(toolInput.callID)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-08 15:01:42 +09:00
|
|
|
}
|
|
|
|
|
const warning = ORCHESTRATOR_DELEGATION_REQUIRED.replace("$FILE_PATH", filePath)
|
|
|
|
|
toolOutput.message = (toolOutput.message || "") + warning
|
|
|
|
|
log(`[${HOOK_NAME}] Injected delegation warning for direct file modification`, {
|
|
|
|
|
sessionID: toolInput.sessionID,
|
|
|
|
|
tool: toolInput.tool,
|
|
|
|
|
filePath,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check task - inject single-task directive
|
|
|
|
|
if (toolInput.tool === "task") {
|
2026-03-17 17:25:46 +09:00
|
|
|
if (toolInput.callID) {
|
|
|
|
|
const requestedSessionId = toolOutput.args.session_id as string | undefined
|
|
|
|
|
if (requestedSessionId) {
|
2026-03-18 17:31:27 +09:00
|
|
|
pendingTaskRefs.set(toolInput.callID, {
|
|
|
|
|
kind: "skip",
|
|
|
|
|
reason: "explicit_resume",
|
|
|
|
|
})
|
2026-03-17 17:25:46 +09:00
|
|
|
} else {
|
|
|
|
|
const boulderState = readBoulderState(ctx.directory)
|
|
|
|
|
const currentTask = boulderState
|
2026-04-25 16:34:34 +08:00
|
|
|
? readCurrentTopLevelTask(resolveBoulderPlanPath(ctx.directory, boulderState))
|
2026-03-17 17:25:46 +09:00
|
|
|
: null
|
|
|
|
|
if (currentTask) {
|
2026-05-11 14:27:03 +09:00
|
|
|
const trackedTask = {
|
2026-03-17 17:25:46 +09:00
|
|
|
key: currentTask.key,
|
|
|
|
|
label: currentTask.label,
|
|
|
|
|
title: currentTask.title,
|
2026-03-18 17:31:27 +09:00
|
|
|
}
|
|
|
|
|
const hasExistingClaim = [...pendingTaskRefs.values()].some((pendingTaskRef) => (
|
2026-05-11 14:27:03 +09:00
|
|
|
pendingTaskRef.kind === "track" && pendingTaskRef.task.key === trackedTask.key
|
2026-03-18 17:31:27 +09:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
if (hasExistingClaim) {
|
|
|
|
|
pendingTaskRefs.set(toolInput.callID, {
|
|
|
|
|
kind: "skip",
|
|
|
|
|
reason: "ambiguous_task_key",
|
2026-05-11 14:27:03 +09:00
|
|
|
task: trackedTask,
|
2026-03-18 17:31:27 +09:00
|
|
|
})
|
|
|
|
|
log(`[${HOOK_NAME}] Skipping task session persistence for ambiguous task key`, {
|
|
|
|
|
sessionID: toolInput.sessionID,
|
|
|
|
|
callID: toolInput.callID,
|
2026-05-11 14:27:03 +09:00
|
|
|
taskKey: trackedTask.key,
|
2026-03-18 17:31:27 +09:00
|
|
|
})
|
|
|
|
|
} else {
|
2026-05-11 14:27:03 +09:00
|
|
|
trackTask(toolInput.callID, trackedTask)
|
2026-03-18 17:31:27 +09:00
|
|
|
}
|
2026-03-17 17:25:46 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 15:01:42 +09:00
|
|
|
const prompt = toolOutput.args.prompt as string | undefined
|
|
|
|
|
if (prompt && !prompt.includes(SYSTEM_DIRECTIVE_PREFIX)) {
|
|
|
|
|
toolOutput.args.prompt = `<system-reminder>${SINGLE_TASK_DIRECTIVE}</system-reminder>\n` + prompt
|
|
|
|
|
log(`[${HOOK_NAME}] Injected single-task directive to task`, {
|
|
|
|
|
sessionID: toolInput.sessionID,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|