Skip to content

Commit d67c8e4

Browse files
committed
refactor(heartbeat): keep terminal status internal
1 parent a6f30b4 commit d67c8e4

7 files changed

Lines changed: 38 additions & 20 deletions

‎src/auto-reply/reply/agent-runner-execute.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import {
2727
} from "./pending-final-delivery.js";
2828
import type { FollowupRun } from "./queue.js";
2929
import type { ReplyMediaContext } from "./reply-media-paths.js";
30-
import type { ReplyOperationRunState } from "./reply-operation-run-state.js";
30+
import { recordReplyOperationAgentTurn } from "./reply-operation-agent-turn-state.js";
31+
import { resolveReplyOperationRunState } from "./reply-operation-run-state.js";
3132
import type { ReplyOperation } from "./reply-run-registry.js";
3233
import { resolveReplyToMode } from "./reply-threading.js";
3334
import { createReplyRestartRecoveryClaimController } from "./restart-recovery-claim.js";
@@ -82,7 +83,6 @@ type ExecutePreparedReplyAgentRunInput = Pick<
8283
performSessionReset: (options: SessionResetOptions) => Promise<boolean>;
8384
replyMediaContext: ReplyMediaContext;
8485
replyOperation: ReplyOperation;
85-
replyOperationRunState: ReplyOperationRunState | undefined;
8686
replyRouteThreadId: ReturnType<typeof resolveRoutedDeliveryThreadId>;
8787
replyToChannel: OriginatingChannelType | undefined;
8888
replyToMode: ReturnType<typeof resolveReplyToMode>;
@@ -126,7 +126,6 @@ export async function executePreparedReplyAgentRun(
126126
queueKey,
127127
replyMediaContext,
128128
replyOperation,
129-
replyOperationRunState,
130129
replyRouteThreadId,
131130
replyThreadingOverride,
132131
replyToChannel,
@@ -394,11 +393,10 @@ export async function executePreparedReplyAgentRun(
394393
}),
395394
),
396395
);
397-
if (replyOperationRunState) {
398-
replyOperationRunState.agentTurn = {
399-
status: runOutcome.outcome.kind === "settled" ? runOutcome.outcome.status : "failed",
400-
};
401-
}
396+
recordReplyOperationAgentTurn(
397+
resolveReplyOperationRunState(opts),
398+
runOutcome.outcome.kind === "settled" ? runOutcome.outcome.status : "failed",
399+
);
402400
activeSessionEntry = getActiveSessionEntry();
403401
activeIsNewSession = getActiveIsNewSession();
404402

‎src/auto-reply/reply/agent-runner-run.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { resolveActiveRunQueueAction } from "./queue-policy.js";
4343
import { enqueueFollowupRun, scheduleFollowupDrain } from "./queue.js";
4444
import { REPLY_ADMISSION_TICKET } from "./reply-admission-ticket.js";
4545
import { createReplyMediaContext } from "./reply-media-paths.js";
46+
import { recordReplyOperationAgentTurn } from "./reply-operation-agent-turn-state.js";
4647
import * as replyRunState from "./reply-operation-run-state.js";
4748
import { type ReplyOperation, replyRunRegistry } from "./reply-run-registry.js";
4849
import { bindReplyOperationTyping } from "./reply-run-typing.js";
@@ -533,7 +534,6 @@ export async function runReplyAgent(
533534
queueKey,
534535
replyMediaContext,
535536
replyOperation,
536-
replyOperationRunState,
537537
replyRouteThreadId,
538538
replyThreadingOverride,
539539
replyToChannel,
@@ -567,9 +567,7 @@ export async function runReplyAgent(
567567
typingSignals,
568568
});
569569
} catch (error) {
570-
if (replyOperationRunState) {
571-
replyOperationRunState.agentTurn = { status: "failed" };
572-
}
570+
recordReplyOperationAgentTurn(replyOperationRunState, "failed");
573571
return await handleReplyAgentRunError(error, {
574572
blockReplyPipeline,
575573
cfg,

‎src/auto-reply/reply/agent-runner.runreplyagent.e2e.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
type FollowupRun,
3535
type QueueSettings,
3636
} from "./queue.js";
37+
import { resolveReplyOperationAgentTurn } from "./reply-operation-agent-turn-state.js";
3738
import {
3839
REPLY_OPERATION_RUN_STATE,
3940
type ReplyOperationRunState,
@@ -999,7 +1000,7 @@ describe("runReplyAgent heartbeat followup guard", () => {
9991000
await run();
10001001

10011002
expect(runState.admission).toEqual({ status: "owned" });
1002-
expect(runState.agentTurn).toEqual({ status: "ok" });
1003+
expect(resolveReplyOperationAgentTurn(runState)).toBe("ok");
10031004
});
10041005

10051006
it("records a failed heartbeat turn when a visible reply replaces its synthetic failure", async () => {
@@ -1021,7 +1022,7 @@ describe("runReplyAgent heartbeat followup guard", () => {
10211022
const payloads = Array.isArray(result) ? result : [result];
10221023

10231024
expect(payloads.map((payload) => payload?.text)).toEqual(["Visible terminal failure."]);
1024-
expect(runState.agentTurn).toEqual({ status: "failed" });
1025+
expect(resolveReplyOperationAgentTurn(runState)).toBe("failed");
10251026
});
10261027

10271028
it("runs visible turns with the session id returned by admission", async () => {
@@ -1271,7 +1272,7 @@ describe("runReplyAgent heartbeat followup guard", () => {
12711272
opts: { [REPLY_OPERATION_RUN_STATE]: runState },
12721273
});
12731274
await expect(run()).rejects.toThrow("persist exploded");
1274-
expect(runState.agentTurn).toEqual({ status: "failed" });
1275+
expect(resolveReplyOperationAgentTurn(runState)).toBe("failed");
12751276
expect(vi.mocked(scheduleFollowupDrain)).toHaveBeenCalledTimes(1);
12761277
} finally {
12771278
persistSpy.mockRestore();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { ReplyOperationRunState } from "./reply-operation-run-state.js";
2+
3+
type ReplyOperationAgentTurnStatus = "ok" | "failed";
4+
5+
const agentTurns = new WeakMap<ReplyOperationRunState, ReplyOperationAgentTurnStatus>();
6+
7+
export function recordReplyOperationAgentTurn(
8+
state: ReplyOperationRunState | undefined,
9+
status: ReplyOperationAgentTurnStatus,
10+
): void {
11+
if (state) {
12+
agentTurns.set(state, status);
13+
}
14+
}
15+
16+
export function resolveReplyOperationAgentTurn(
17+
state: ReplyOperationRunState | undefined,
18+
): ReplyOperationAgentTurnStatus | undefined {
19+
return state ? agentTurns.get(state) : undefined;
20+
}

‎src/auto-reply/reply/reply-operation-run-state.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ type ReplyOperationAdmissionSnapshot =
1010

1111
export type ReplyOperationRunState = {
1212
admission?: ReplyOperationAdmissionSnapshot;
13-
agentTurn?: { status: "ok" | "failed" };
1413
};
1514

16-
// Carries this invocation's admission and terminal agent-turn facts through
17-
// reply option spreads so heartbeat cleanup never re-derives them from payloads.
15+
// Carries this invocation's admission decision through reply option spreads so
16+
// heartbeat cleanup never infers it from whichever operation is active later.
1817
export const REPLY_OPERATION_RUN_STATE = Symbol("openclaw.replyOperationRunState");
1918

2019
export type ReplyOptionsWithOperationRunState = {

‎src/infra/heartbeat-runner-execution.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
resolveHeartbeatToolResponseFromReplyResult,
1616
} from "../auto-reply/heartbeat-tool-response.js";
1717
import { stripHeartbeatToken } from "../auto-reply/heartbeat.js";
18+
import { resolveReplyOperationAgentTurn } from "../auto-reply/reply/reply-operation-agent-turn-state.js";
1819
import {
1920
REPLY_OPERATION_RUN_STATE,
2021
type ReplyOperationRunState,
@@ -681,7 +682,7 @@ export async function invokeHeartbeatAgentRun(
681682
resolveHeartbeatTerminalToolFailure(replyResult);
682683
const selectedReplyPayload = resolveHeartbeatReplyPayload(replyResult);
683684
const replyPayload = selectedReplyPayload;
684-
const agentRunFailed = replyOperationRunState.agentTurn?.status === "failed";
685+
const agentRunFailed = resolveReplyOperationAgentTurn(replyOperationRunState) === "failed";
685686
if (
686687
heartbeatScratchProposal !== undefined &&
687688
heartbeatToolResponse &&

‎src/infra/heartbeat-runner.tool-response.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
markReplyPayloadForSourceSuppressionDelivery,
1414
setReplyPayloadMetadata,
1515
} from "../auto-reply/reply-payload.js";
16+
import { recordReplyOperationAgentTurn } from "../auto-reply/reply/reply-operation-agent-turn-state.js";
1617
import { resolveReplyOperationRunState } from "../auto-reply/reply/reply-operation-run-state.js";
1718
import { SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js";
1819
import type { OpenClawConfig } from "../config/config.js";
@@ -181,7 +182,7 @@ describe("runHeartbeatOnce heartbeat response tool", () => {
181182
if (!runState) {
182183
throw new Error("Expected heartbeat reply operation run state");
183184
}
184-
runState.agentTurn = { status };
185+
recordReplyOperationAgentTurn(runState, status);
185186
}
186187

187188
function replyContext(replySpy: ReturnType<typeof vi.fn>): {

0 commit comments

Comments
 (0)