Skip to content

Commit 7ddc5ac

Browse files
committed
fix(discord): hide internal code mode waits
1 parent 9a0c9f8 commit 7ddc5ac

14 files changed

Lines changed: 335 additions & 5 deletions

‎packages/agent-core/src/agent-loop.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,65 @@ describe("agentLoop tool termination", () => {
730730
};
731731
}
732732

733+
it("marks lifecycle events from the concrete hidden tool instance", async () => {
734+
let turn = 0;
735+
const streamFn: StreamFn = () => {
736+
turn += 1;
737+
const stream = createAssistantMessageEventStream();
738+
queueMicrotask(() => {
739+
const message =
740+
turn === 1
741+
? makeAssistantMessage([
742+
{ type: "toolCall", id: "call-wait", name: "wait", arguments: {} },
743+
])
744+
: makeAssistantMessage([{ type: "text", text: "done" }]);
745+
stream.push({
746+
type: "done",
747+
reason: message.stopReason === "toolUse" ? "toolUse" : "stop",
748+
message,
749+
});
750+
stream.end();
751+
});
752+
return stream;
753+
};
754+
const hiddenTool: AgentTool = {
755+
...makeTool("wait", []),
756+
hideFromChannelProgress: true,
757+
execute: async (_toolCallId, _args, _signal, onUpdate) => {
758+
onUpdate?.({
759+
content: [{ type: "text", text: "still waiting" }],
760+
details: { status: "waiting" },
761+
});
762+
return {
763+
content: [{ type: "text", text: "resumed" }],
764+
details: { status: "completed" },
765+
};
766+
},
767+
};
768+
769+
const events = await collectEvents(
770+
agentLoop(
771+
[{ role: "user", content: "resume", timestamp: 1 }],
772+
{ systemPrompt: "", messages: [], tools: [hiddenTool] },
773+
{ ...config, toolExecution: "sequential" },
774+
undefined,
775+
streamFn,
776+
),
777+
);
778+
const lifecycleEvents = events.filter((event) => event.type.startsWith("tool_execution_"));
779+
780+
expect(lifecycleEvents.map((event) => event.type)).toEqual([
781+
"tool_execution_start",
782+
"tool_execution_update",
783+
"tool_execution_end",
784+
]);
785+
expect(
786+
lifecycleEvents.every(
787+
(event) => "hideFromChannelProgress" in event && event.hideFromChannelProgress === true,
788+
),
789+
).toBe(true);
790+
});
791+
733792
it("continues after a side-effect tool result when afterToolCall records it without terminate", async () => {
734793
const executed: string[] = [];
735794
let turn = 0;

‎packages/agent-core/src/agent-loop.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,19 @@ type ResolvedToolCallOutcome =
605605
| { kind: "resolved"; tool?: AgentTool }
606606
| { kind: "error"; error: unknown };
607607

608+
function hidesToolCallFromChannelProgress(
609+
context: AgentContext,
610+
toolCall: AgentToolCall,
611+
resolvedToolCalls: Map<AgentToolCall, ResolvedToolCallOutcome>,
612+
): boolean {
613+
const resolution = resolvedToolCalls.get(toolCall);
614+
const tool =
615+
resolution?.kind === "resolved"
616+
? resolution.tool
617+
: context.tools?.find((candidate) => candidate.name === toolCall.name);
618+
return tool?.hideFromChannelProgress === true;
619+
}
620+
608621
async function executeToolCallsSequential(
609622
currentContext: AgentContext,
610623
assistantMessage: AssistantMessage,
@@ -618,11 +631,17 @@ async function executeToolCallsSequential(
618631
const messages: ToolResultMessage[] = [];
619632

620633
for (const toolCall of toolCalls) {
634+
const hideFromChannelProgress = hidesToolCallFromChannelProgress(
635+
currentContext,
636+
toolCall,
637+
resolvedToolCalls,
638+
);
621639
await emit({
622640
type: "tool_execution_start",
623641
toolCallId: toolCall.id,
624642
toolName: toolCall.name,
625643
args: toolCall.arguments,
644+
...(hideFromChannelProgress ? { hideFromChannelProgress: true } : {}),
626645
});
627646

628647
const preparation = await prepareToolCall(
@@ -640,6 +659,7 @@ async function executeToolCallsSequential(
640659
result: preparation.result,
641660
isError: preparation.isError,
642661
executionStarted: false,
662+
...(hideFromChannelProgress ? { hideFromChannelProgress: true } : {}),
643663
};
644664
} else {
645665
const executed = await executePreparedToolCall(preparation, signal, emit);
@@ -682,11 +702,17 @@ async function executeToolCallsParallel(
682702
const finalizedCalls: FinalizedToolCallEntry[] = [];
683703

684704
for (const toolCall of toolCalls) {
705+
const hideFromChannelProgress = hidesToolCallFromChannelProgress(
706+
currentContext,
707+
toolCall,
708+
resolvedToolCalls,
709+
);
685710
await emit({
686711
type: "tool_execution_start",
687712
toolCallId: toolCall.id,
688713
toolName: toolCall.name,
689714
args: toolCall.arguments,
715+
...(hideFromChannelProgress ? { hideFromChannelProgress: true } : {}),
690716
});
691717

692718
const preparation = await prepareToolCall(
@@ -703,6 +729,7 @@ async function executeToolCallsParallel(
703729
result: preparation.result,
704730
isError: preparation.isError,
705731
executionStarted: false,
732+
...(hideFromChannelProgress ? { hideFromChannelProgress: true } : {}),
706733
} satisfies FinalizedToolCallOutcome;
707734
await emitToolExecutionEnd(finalized, emit);
708735
finalizedCalls.push(finalized);
@@ -769,6 +796,7 @@ type FinalizedToolCallOutcome = {
769796
result: AgentToolResult<unknown>;
770797
isError: boolean;
771798
executionStarted: boolean;
799+
hideFromChannelProgress?: boolean;
772800
};
773801

774802
type FinalizedToolCallEntry = FinalizedToolCallOutcome | (() => Promise<FinalizedToolCallOutcome>);
@@ -947,6 +975,9 @@ async function executePreparedToolCall(
947975
toolName: prepared.toolCall.name,
948976
args: prepared.toolCall.arguments,
949977
partialResult,
978+
...(prepared.tool.hideFromChannelProgress === true
979+
? { hideFromChannelProgress: true }
980+
: {}),
950981
}),
951982
),
952983
);
@@ -1006,6 +1037,7 @@ async function finalizeExecutedToolCall(
10061037
result,
10071038
isError,
10081039
executionStarted: true,
1040+
...(prepared.tool.hideFromChannelProgress === true ? { hideFromChannelProgress: true } : {}),
10091041
};
10101042
}
10111043

@@ -1027,6 +1059,7 @@ async function emitToolExecutionEnd(
10271059
result: finalized.result,
10281060
isError: finalized.isError,
10291061
executionStarted: finalized.executionStarted,
1062+
...(finalized.hideFromChannelProgress === true ? { hideFromChannelProgress: true } : {}),
10301063
});
10311064
}
10321065

‎packages/agent-core/src/types.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ export interface AgentTool<
462462
> extends Tool<TParameters> {
463463
/** Human-readable label for UI display. */
464464
label: string;
465+
/** Preserve lifecycle telemetry without rendering transient channel progress. */
466+
hideFromChannelProgress?: boolean;
465467
/**
466468
* Optional compatibility shim for raw tool-call arguments before schema validation.
467469
* Must return an object that matches `TParameters`.
@@ -514,13 +516,20 @@ export type AgentEvent =
514516
| { type: "message_update"; message: AgentMessage; assistantMessageEvent: AssistantMessageEvent }
515517
| { type: "message_end"; message: AgentMessage }
516518
// Tool execution lifecycle
517-
| { type: "tool_execution_start"; toolCallId: string; toolName: string; args: unknown }
519+
| {
520+
type: "tool_execution_start";
521+
toolCallId: string;
522+
toolName: string;
523+
args: unknown;
524+
hideFromChannelProgress?: boolean;
525+
}
518526
| {
519527
type: "tool_execution_update";
520528
toolCallId: string;
521529
toolName: string;
522530
args: unknown;
523531
partialResult: unknown;
532+
hideFromChannelProgress?: boolean;
524533
}
525534
| {
526535
type: "tool_execution_end";
@@ -530,4 +539,5 @@ export type AgentEvent =
530539
isError: boolean;
531540
/** False when resolution, argument preparation, validation, or policy blocked execution. */
532541
executionStarted?: boolean;
542+
hideFromChannelProgress?: boolean;
533543
};

‎src/agents/code-mode.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ describe("Code Mode", () => {
269269
expect(compacted.catalogToolCount).toBe(2);
270270
});
271271

272+
it("marks only the internal wait control as hidden from channel progress", () => {
273+
const { tools } = createCodeModeHarness();
274+
275+
expect(tools[0].hideFromChannelProgress).toBeUndefined();
276+
expect(tools[1].hideFromChannelProgress).toBe(true);
277+
});
278+
272279
it("tells models to return the final code value", () => {
273280
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
274281
const compacted = applyCodeModeCatalog({

‎src/agents/code-mode.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ export function createCodeModeTools(ctx: CodeModeToolContext): AnyAgentTool[] {
11721172
const waitTool = markCodeModeControlTool({
11731173
name: CODE_MODE_WAIT_TOOL_NAME,
11741174
label: "wait",
1175+
hideFromChannelProgress: true,
11751176
description: "Resume a suspended OpenClaw code mode run returned by exec.",
11761177
parameters: Type.Object({
11771178
runId: Type.String({ description: "Code mode run id returned by exec." }),

‎src/agents/embedded-agent-runner/run/attempt.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3756,6 +3756,9 @@ export async function runEmbeddedAttempt(
37563756
toolCallId: toolParams.toolCallId,
37573757
args: toolParams.input,
37583758
replaySafe: replaySafeTools.has(toolParams.tool as never),
3759+
hideFromChannelProgress:
3760+
"hideFromChannelProgress" in toolParams.tool &&
3761+
toolParams.tool.hideFromChannelProgress === true,
37593762
execute: async () =>
37603763
await toolParams.tool.execute(
37613764
toolParams.toolCallId,

‎src/agents/embedded-agent-subscribe.handlers.tools.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,68 @@ describe("handleToolExecutionStart read path checks", () => {
393393
expect(ctx.state.toolMetaById.has("tool-callback-rejects")).toBe(true);
394394
expect(warn).toHaveBeenCalledWith(expect.stringContaining("tool agent event callback failed"));
395395
});
396+
397+
it("preserves hidden tool telemetry while marking its channel progress private", async () => {
398+
const { ctx, onAgentEvent } = createTestContext();
399+
400+
await handleToolExecutionStart(ctx, {
401+
type: "tool_execution_start",
402+
toolName: "wait",
403+
toolCallId: "tool-code-wait",
404+
args: { runId: "cm_1" },
405+
hideFromChannelProgress: true,
406+
});
407+
handleToolExecutionUpdate(ctx, {
408+
type: "tool_execution_update",
409+
toolName: "wait",
410+
toolCallId: "tool-code-wait",
411+
args: { runId: "cm_1" },
412+
partialResult: { status: "waiting" },
413+
hideFromChannelProgress: true,
414+
});
415+
await handleToolExecutionEnd(ctx, {
416+
type: "tool_execution_end",
417+
toolName: "wait",
418+
toolCallId: "tool-code-wait",
419+
isError: false,
420+
result: { details: { status: "completed" } },
421+
hideFromChannelProgress: true,
422+
});
423+
424+
const lifecycleEvents = onAgentEvent.mock.calls
425+
.map((call) => call[0] as CapturedAgentEvent)
426+
.filter((event) => event.data?.name === "wait");
427+
expect(lifecycleEvents).not.toHaveLength(0);
428+
expect(lifecycleEvents.every((event) => event.data?.hideFromChannelProgress === true)).toBe(
429+
true,
430+
);
431+
});
432+
433+
it("keeps an unmarked catalog tool named wait visible", async () => {
434+
const { ctx, onAgentEvent } = createTestContext();
435+
436+
await handleToolExecutionStart(ctx, {
437+
type: "tool_execution_start",
438+
toolName: "wait",
439+
toolCallId: "tool-catalog-wait",
440+
args: {},
441+
});
442+
await handleToolExecutionEnd(ctx, {
443+
type: "tool_execution_end",
444+
toolName: "wait",
445+
toolCallId: "tool-catalog-wait",
446+
isError: false,
447+
result: { details: { status: "completed" } },
448+
});
449+
450+
const lifecycleEvents = onAgentEvent.mock.calls
451+
.map((call) => call[0] as CapturedAgentEvent)
452+
.filter((event) => event.data?.name === "wait");
453+
expect(lifecycleEvents).not.toHaveLength(0);
454+
expect(lifecycleEvents.every((event) => event.data?.hideFromChannelProgress !== true)).toBe(
455+
true,
456+
);
457+
});
396458
});
397459

398460
describe("handleToolExecutionEnd cron mutation tracking", () => {

0 commit comments

Comments
 (0)