2026-04-16 13:52:12 +09:00
|
|
|
import { recoverToolMetadata } from "../features/tool-metadata-store"
|
2026-02-08 16:25:25 +09:00
|
|
|
import type { CreatedHooks } from "../create-hooks"
|
2026-04-16 13:52:12 +09:00
|
|
|
import { log } from "../shared/logger"
|
2026-04-15 10:46:41 +09:00
|
|
|
import { stripInvisibleAgentCharacters } from "../shared/agent-display-names"
|
2026-03-06 22:37:41 +09:00
|
|
|
import type { PluginContext } from "./types"
|
|
|
|
|
|
|
|
|
|
const VERIFICATION_ATTEMPT_PATTERN = /<ulw_verification_attempt_id>(.*?)<\/ulw_verification_attempt_id>/i
|
2026-02-08 16:25:25 +09:00
|
|
|
|
2026-03-18 17:45:59 +09:00
|
|
|
function getMetadataString(metadata: Record<string, unknown> | undefined, keys: string[]): string | undefined {
|
|
|
|
|
for (const key of keys) {
|
|
|
|
|
const value = metadata?.[key]
|
|
|
|
|
if (typeof value === "string") {
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 01:41:07 +09:00
|
|
|
function getPluginDirectory(ctx: PluginContext): string | null {
|
|
|
|
|
if (typeof ctx === "object" && ctx !== null && "directory" in ctx && typeof ctx.directory === "string") {
|
|
|
|
|
return ctx.directory
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 16:25:25 +09:00
|
|
|
export function createToolExecuteAfterHandler(args: {
|
2026-03-06 22:37:41 +09:00
|
|
|
ctx: PluginContext
|
2026-02-08 16:25:25 +09:00
|
|
|
hooks: CreatedHooks
|
|
|
|
|
}): (
|
|
|
|
|
input: { tool: string; sessionID: string; callID: string },
|
|
|
|
|
output:
|
|
|
|
|
| { title: string; output: string; metadata: Record<string, unknown> }
|
|
|
|
|
| undefined,
|
|
|
|
|
) => Promise<void> {
|
2026-03-06 22:37:41 +09:00
|
|
|
const { ctx, hooks } = args
|
2026-02-08 16:25:25 +09:00
|
|
|
|
2026-04-16 13:52:12 +09:00
|
|
|
// OpenCode injects tool call ids into execute() context and after-hook input via undocumented runtime fields.
|
|
|
|
|
// We must treat their identity as a best-effort correlation key, not a guaranteed public contract.
|
|
|
|
|
|
2026-02-08 16:25:25 +09:00
|
|
|
return async (
|
2026-04-16 13:52:12 +09:00
|
|
|
input: { tool: string; sessionID: string; callID?: string; callId?: string; call_id?: string },
|
2026-02-08 16:25:25 +09:00
|
|
|
output: { title: string; output: string; metadata: Record<string, unknown> } | undefined,
|
|
|
|
|
): Promise<void> => {
|
|
|
|
|
if (!output) return
|
|
|
|
|
|
2026-04-16 13:52:12 +09:00
|
|
|
const hookInput = {
|
|
|
|
|
tool: input.tool,
|
|
|
|
|
sessionID: input.sessionID,
|
|
|
|
|
callID: input.callID ?? input.callId ?? input.call_id ?? "",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nativeSessionId = getMetadataString(output.metadata, ["sessionId", "sessionID", "session_id"])
|
|
|
|
|
const stored = recoverToolMetadata(input.sessionID, input)
|
2026-02-08 16:25:25 +09:00
|
|
|
if (stored) {
|
|
|
|
|
if (stored.title) {
|
|
|
|
|
output.title = stored.title
|
|
|
|
|
}
|
|
|
|
|
if (stored.metadata) {
|
2026-04-16 13:52:12 +09:00
|
|
|
if (nativeSessionId) {
|
|
|
|
|
log("[tool-execute-after] Native output metadata already includes session linkage; skipping stored metadata overwrite", {
|
|
|
|
|
tool: input.tool,
|
|
|
|
|
sessionID: input.sessionID,
|
|
|
|
|
callID: input.callID ?? input.callId ?? input.call_id,
|
|
|
|
|
nativeSessionId,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
output.metadata = { ...output.metadata, ...stored.metadata }
|
|
|
|
|
}
|
2026-02-08 16:25:25 +09:00
|
|
|
}
|
2026-04-16 13:52:12 +09:00
|
|
|
} else if (!nativeSessionId) {
|
|
|
|
|
log("[tool-execute-after] Unable to recover stored metadata and no native session linkage was present", {
|
|
|
|
|
tool: input.tool,
|
|
|
|
|
sessionID: input.sessionID,
|
|
|
|
|
callID: input.callID ?? input.callId ?? input.call_id,
|
|
|
|
|
})
|
2026-02-08 16:25:25 +09:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 22:37:41 +09:00
|
|
|
if (input.tool === "task") {
|
2026-03-12 01:41:07 +09:00
|
|
|
const directory = getPluginDirectory(ctx)
|
2026-03-18 17:45:59 +09:00
|
|
|
const sessionId = getMetadataString(output.metadata, ["sessionId", "sessionID", "session_id"])
|
|
|
|
|
const agent = getMetadataString(output.metadata, ["agent"])
|
|
|
|
|
const prompt = getMetadataString(output.metadata, ["prompt"])
|
2026-03-06 22:37:41 +09:00
|
|
|
const verificationAttemptId = prompt?.match(VERIFICATION_ATTEMPT_PATTERN)?.[1]?.trim()
|
2026-04-16 13:52:12 +09:00
|
|
|
const loopState = directory
|
|
|
|
|
? (await import("../hooks/ralph-loop/storage")).readState(directory)
|
|
|
|
|
: null
|
2026-03-17 16:20:27 +09:00
|
|
|
const isVerificationContext =
|
2026-04-15 10:46:41 +09:00
|
|
|
(agent ? stripInvisibleAgentCharacters(agent) : agent) === "oracle"
|
2026-03-17 16:20:27 +09:00
|
|
|
&& !!sessionId
|
|
|
|
|
&& !!directory
|
2026-03-06 22:37:41 +09:00
|
|
|
&& loopState?.active === true
|
|
|
|
|
&& loopState.ultrawork === true
|
|
|
|
|
&& loopState.verification_pending === true
|
|
|
|
|
&& loopState.session_id === input.sessionID
|
2026-03-17 16:20:27 +09:00
|
|
|
|
|
|
|
|
log("[tool-execute-after] ULW verification tracking check", {
|
|
|
|
|
tool: input.tool,
|
|
|
|
|
agent,
|
|
|
|
|
parentSessionID: input.sessionID,
|
|
|
|
|
oracleSessionID: sessionId,
|
|
|
|
|
hasPromptInMetadata: typeof prompt === "string",
|
|
|
|
|
extractedVerificationAttemptId: verificationAttemptId,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
isVerificationContext
|
|
|
|
|
&& verificationAttemptId
|
2026-03-06 22:37:41 +09:00
|
|
|
&& loopState.verification_attempt_id === verificationAttemptId
|
|
|
|
|
) {
|
2026-04-16 13:52:12 +09:00
|
|
|
;(await import("../hooks/ralph-loop/storage")).writeState(directory, {
|
2026-03-06 22:37:41 +09:00
|
|
|
...loopState,
|
|
|
|
|
verification_session_id: sessionId,
|
|
|
|
|
})
|
2026-03-17 16:20:27 +09:00
|
|
|
log("[tool-execute-after] Stored oracle verification session via attempt match", {
|
|
|
|
|
parentSessionID: input.sessionID,
|
|
|
|
|
oracleSessionID: sessionId,
|
|
|
|
|
verificationAttemptId,
|
|
|
|
|
})
|
|
|
|
|
} else if (isVerificationContext && !verificationAttemptId) {
|
2026-04-16 13:52:12 +09:00
|
|
|
;(await import("../hooks/ralph-loop/storage")).writeState(directory, {
|
2026-03-17 16:20:27 +09:00
|
|
|
...loopState,
|
|
|
|
|
verification_session_id: sessionId,
|
|
|
|
|
})
|
|
|
|
|
log("[tool-execute-after] Fallback: stored oracle verification session without attempt match", {
|
|
|
|
|
parentSessionID: input.sessionID,
|
|
|
|
|
oracleSessionID: sessionId,
|
|
|
|
|
hasPromptInMetadata: typeof prompt === "string",
|
|
|
|
|
expectedAttemptId: loopState.verification_attempt_id,
|
|
|
|
|
extractedAttemptId: verificationAttemptId,
|
|
|
|
|
})
|
2026-03-06 22:37:41 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 01:41:07 +09:00
|
|
|
const runToolExecuteAfterHooks = async (): Promise<void> => {
|
2026-04-16 13:52:12 +09:00
|
|
|
await hooks.toolOutputTruncator?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.claudeCodeHooks?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.preemptiveCompaction?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.contextWindowMonitor?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.commentChecker?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.directoryAgentsInjector?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.directoryReadmeInjector?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.rulesInjector?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.emptyTaskResponseDetector?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.agentUsageReminder?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.categorySkillReminder?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.interactiveBashSession?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.editErrorRecovery?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.delegateTaskRetry?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.atlasHook?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.taskResumeInfo?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.readImageResizer?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.hashlineReadEnhancer?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.webfetchRedirectGuard?.["tool.execute.after"]?.(hookInput, output)
|
|
|
|
|
await hooks.jsonErrorRecovery?.["tool.execute.after"]?.(hookInput, output)
|
2026-03-12 01:41:07 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.tool === "extract" || input.tool === "discard") {
|
|
|
|
|
const originalOutput = {
|
|
|
|
|
title: output.title,
|
|
|
|
|
output: output.output,
|
|
|
|
|
metadata: { ...output.metadata },
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await runToolExecuteAfterHooks()
|
|
|
|
|
} catch (error) {
|
|
|
|
|
output.title = originalOutput.title
|
|
|
|
|
output.output = originalOutput.output
|
|
|
|
|
output.metadata = originalOutput.metadata
|
|
|
|
|
log("[tool-execute-after] Failed to process extract/discard hooks", {
|
|
|
|
|
tool: input.tool,
|
|
|
|
|
sessionID: input.sessionID,
|
2026-04-16 13:52:12 +09:00
|
|
|
callID: input.callID ?? input.callId ?? input.call_id,
|
2026-03-12 01:41:07 +09:00
|
|
|
error,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await runToolExecuteAfterHooks()
|
2026-02-08 16:25:25 +09:00
|
|
|
}
|
|
|
|
|
}
|