Agent: guard reminder promises behind cron scheduling

This commit is contained in:
Vignesh Natarajan
2026-02-16 13:34:09 -08:00
committed by Vignesh
parent 0cff8bc4e6
commit 5a26d1c622
10 changed files with 222 additions and 7 deletions

View File

@@ -23,6 +23,14 @@ import { normalizeToolName } from "./tool-policy.js";
/** Track tool execution start times and args for after_tool_call hook */
const toolStartData = new Map<string, { startTime: number; args: unknown }>();
function isCronAddAction(args: unknown): boolean {
if (!args || typeof args !== "object") {
return false;
}
const action = (args as Record<string, unknown>).action;
return typeof action === "string" && action.trim().toLowerCase() === "add";
}
function buildToolCallSummary(toolName: string, args: unknown, meta?: string): ToolCallSummary {
const mutation = buildToolMutationState(toolName, args, meta);
return {
@@ -188,6 +196,8 @@ export async function handleToolExecutionEnd(
const result = evt.result;
const isToolError = isError || isToolResultError(result);
const sanitizedResult = sanitizeToolResult(result);
const startData = toolStartData.get(toolCallId);
toolStartData.delete(toolCallId);
const callSummary = ctx.state.toolMetaById.get(toolCallId);
const meta = callSummary?.meta;
ctx.state.toolMetas.push({ toolName, meta });
@@ -239,6 +249,11 @@ export async function handleToolExecutionEnd(
}
}
// Track committed reminders only when cron.add completed successfully.
if (!isToolError && toolName === "cron" && isCronAddAction(startData?.args)) {
ctx.state.successfulCronAdds += 1;
}
emitAgentEvent({
runId: ctx.params.runId,
stream: "tool",
@@ -290,8 +305,6 @@ export async function handleToolExecutionEnd(
// Run after_tool_call plugin hook (fire-and-forget)
const hookRunnerAfter = ctx.hookRunner ?? getGlobalHookRunner();
if (hookRunnerAfter?.hasHooks("after_tool_call")) {
const startData = toolStartData.get(toolCallId);
toolStartData.delete(toolCallId);
const durationMs = startData?.startTime != null ? Date.now() - startData.startTime : undefined;
const toolArgs = startData?.args;
const hookEvent: PluginHookAfterToolCallEvent = {
@@ -310,7 +323,5 @@ export async function handleToolExecutionEnd(
.catch((err) => {
ctx.log.warn(`after_tool_call hook failed: tool=${toolName} error=${String(err)}`);
});
} else {
toolStartData.delete(toolCallId);
}
}