Skip to content

Commit a77fbde

Browse files
clawsweeperyanmo42jesse-merhiroboclaw-bot
authored
fix(ui): recover queued follow-ups from settled runs (#126180)
* fix(ui): recover queued follow-ups from settled runs Co-authored-by: jesse-merhi <[email protected]> * fix(ui): preserve terminal client run ownership Co-authored-by: jesse-merhi <[email protected]> * fix(sessions): persist recovered terminal client runs Co-authored-by: jesse-merhi <[email protected]> * test(gateway): publish configured reply runtime Co-authored-by: jesse-merhi <[email protected]> * fix(protocol): refresh Swift session row Co-authored-by: jesse-merhi <[email protected]> --------- Co-authored-by: Ian Moog <[email protected]> Co-authored-by: jesse-merhi <[email protected]> Co-authored-by: Roboclaw <[email protected]>
1 parent b26ee4f commit a77fbde

47 files changed

Lines changed: 405 additions & 23 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6434,6 +6434,7 @@ public struct SessionRow: Codable, Sendable {
64346434
public let lastinteractionat: Double?
64356435
public let status: AnyCodable?
64366436
public let lastrunerror: String?
6437+
public let lastrunid: String?
64376438
public let restartrecoverystatus: String?
64386439
public let activeleafentryid: AnyCodable?
64396440
public let spawnedby: String?
@@ -6504,6 +6505,7 @@ public struct SessionRow: Codable, Sendable {
65046505
lastinteractionat: Double? = nil,
65056506
status: AnyCodable? = nil,
65066507
lastrunerror: String? = nil,
6508+
lastrunid: String? = nil,
65076509
restartrecoverystatus: String? = nil,
65086510
activeleafentryid: AnyCodable? = nil,
65096511
spawnedby: String? = nil,
@@ -6573,6 +6575,7 @@ public struct SessionRow: Codable, Sendable {
65736575
self.lastinteractionat = lastinteractionat
65746576
self.status = status
65756577
self.lastrunerror = lastrunerror
6578+
self.lastrunid = lastrunid
65766579
self.restartrecoverystatus = restartrecoverystatus
65776580
self.activeleafentryid = activeleafentryid
65786581
self.spawnedby = spawnedby
@@ -6644,6 +6647,7 @@ public struct SessionRow: Codable, Sendable {
66446647
case lastinteractionat = "lastInteractionAt"
66456648
case status
66466649
case lastrunerror = "lastRunError"
6650+
case lastrunid = "lastRunId"
66476651
case restartrecoverystatus = "restartRecoveryStatus"
66486652
case activeleafentryid = "activeLeafEntryId"
66496653
case spawnedby = "spawnedBy"

‎packages/gateway-protocol/src/schema/sessions-row.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe("SessionRowSchema", () => {
88
const row = {
99
key: "agent:main:main",
1010
kind: "global",
11+
lastRunId: "run-settled",
1112
activeLeafEntryId: "leaf-rendered",
1213
createdActor: {
1314
type: "human",
@@ -37,6 +38,7 @@ describe("SessionRowSchema", () => {
3738
const roundTripped = structuredClone(row);
3839

3940
expect(SessionRowSchema.properties.activeLeafEntryId).toBeDefined();
41+
expect(SessionRowSchema.properties.lastRunId).toBeDefined();
4042
expect(Value.Check(SessionRowSchema, roundTripped)).toBe(true);
4143
expect(Value.Check(SessionRowSchema, { ...roundTripped, activeLeafEntryId: null })).toBe(true);
4244
expect(
@@ -50,6 +52,7 @@ describe("SessionRowSchema", () => {
5052
).toBe(false);
5153
expect(roundTripped).toMatchObject({
5254
activeLeafEntryId: "leaf-rendered",
55+
lastRunId: "run-settled",
5356
createdActor: { avatarUrl: "/api/users/profile-ada/avatar?v=7" },
5457
participantCount: 2,
5558
archivedBy: { type: "human", id: "profile-bob", label: "Bob" },
@@ -63,6 +66,7 @@ describe("SessionRowSchema", () => {
6366
expect(Value.Check(SessionRowSchema, { ...roundTripped, permissionMode: "unrestricted" })).toBe(
6467
false,
6568
);
69+
expect(Value.Check(SessionRowSchema, { ...roundTripped, lastRunId: "" })).toBe(false);
6670
});
6771

6872
it("keeps sessions.assignOwner target actors closed and non-empty", () => {

‎packages/gateway-protocol/src/schema/sessions-row.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ export const SessionRowSchema = Type.Object(
8888
]),
8989
),
9090
lastRunError: Type.Optional(Type.String()),
91+
/** Exact run that produced the latest terminal lifecycle projection. */
92+
lastRunId: Type.Optional(NonEmptyString),
9193
restartRecoveryStatus: Type.Optional(Type.Literal("tombstoned")),
9294
activeLeafEntryId: Type.Optional(Type.Union([NonEmptyString, Type.Null()])),
9395
spawnedBy: Type.Optional(Type.String()),

‎src/agents/command/session.provider-owned-reset.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import type { OpenClawConfig } from "../../config/config.js";
3-
import type { SessionEntry } from "../../config/sessions/types.js";
3+
import type { InternalSessionEntry as SessionEntry } from "../../config/sessions/types.js";
44

55
const hoisted = vi.hoisted(() => ({
66
store: {} as Record<string, SessionEntry>,
@@ -79,6 +79,7 @@ describe("command resolveSession provider-owned daily reset", () => {
7979
pendingTranscriptRepair: [
8080
{ id: "predecessor-repair", text: "old reply", createdAt: startedAt },
8181
],
82+
lastRunId: "settled-old-run",
8283
},
8384
};
8485

@@ -91,6 +92,7 @@ describe("command resolveSession provider-owned daily reset", () => {
9192
expect(result.isNewSession).toBe(true);
9293
expect(result.sessionId).not.toBe("old-session-id");
9394
expect(result.sessionEntry?.pendingTranscriptRepair).toBeUndefined();
95+
expect(result.sessionEntry?.lastRunId).toBeUndefined();
9496
});
9597

9698
it("keeps a model-locked session across the daily boundary", () => {

‎src/agents/command/session.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export function clearRotatedSessionMetadata(entry: InternalSessionEntry): Intern
8080
sessionFile: undefined,
8181
status: undefined,
8282
lifecycleRunId: undefined,
83+
lastRunId: undefined,
8384
startedAt: undefined,
8485
endedAt: undefined,
8586
runtimeMs: undefined,

‎src/agents/main-session-recovery/main-session-recovery-state.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ describe("main session recovery state", () => {
176176
it("marks without charging and replaces an older lifecycle owner for the same run", () => {
177177
const entry = interruptedEntry({
178178
lifecycleRunId: "dead-run",
179+
lastRunId: "settled-run",
179180
restartRecoveryRuns: [
180181
{ runId: "older-run", lifecycleGeneration: "generation-old" },
181182
{ runId: "shared-run", lifecycleGeneration: "generation-1" },
@@ -208,6 +209,7 @@ describe("main session recovery state", () => {
208209
{ runId: "shared-run", lifecycleGeneration: "generation-2" },
209210
]);
210211
expect(entry.lifecycleRunId).toBeUndefined();
212+
expect(entry.lastRunId).toBeUndefined();
211213
});
212214

213215
it("rejects foreground work after the automatic recovery budget is exhausted", () => {
@@ -346,6 +348,7 @@ describe("main session recovery state", () => {
346348

347349
it("moves a reservation into the lifecycle fence during Gateway admission", () => {
348350
const entry = interruptedEntry({
351+
lastRunId: "settled-run",
349352
pendingFinalDelivery: { kind: "replayable", text: " captured reply ", createdAt: 1 },
350353
restartRecoveryDeliveryRunId: "recovery-1",
351354
restartRecoveryDeliverySourceRunId: "source-1",
@@ -391,6 +394,7 @@ describe("main session recovery state", () => {
391394
});
392395
expect(entry.mainRestartRecovery?.reservation).toBeUndefined();
393396
expect(entry.lifecycleRunId).toBe("recovery-1");
397+
expect(entry.lastRunId).toBeUndefined();
394398

395399
expect(
396400
transitionMainSessionRecovery(entry, {
@@ -407,6 +411,7 @@ describe("main session recovery state", () => {
407411
expect(entry.restartRecoveryDeliveryRunId).toBeUndefined();
408412
expect(entry.restartRecoveryDeliverySourceRunId).toBe("source-1");
409413
expect(entry.lifecycleRunId).toBeUndefined();
414+
expect(entry.lastRunId).toBeUndefined();
410415
});
411416

412417
it("rejects a reservation created by an older lifecycle generation", () => {

‎src/agents/main-session-recovery/main-session-recovery-state.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import type {
2121
MainSessionRecoveryTransitionResult,
2222
MainSessionRecoveryView,
2323
} from "./main-session-recovery-types.js";
24-
import { MAX_RECOVERY_RETRIES } from "./main-session-restart-recovery-shared.js";
24+
import {
25+
MAX_RECOVERY_RETRIES,
26+
resolveRestartRecoveryTerminalClientRunId,
27+
} from "./main-session-restart-recovery-shared.js";
2528

2629
export type {
2730
MainSessionRecoveryCommand,
@@ -334,6 +337,7 @@ export function transitionMainSessionRecovery(
334337
}
335338
entry.status = "running";
336339
entry.lifecycleRunId = undefined;
340+
entry.lastRunId = undefined;
337341
entry.abortedLastRun = true;
338342
if (command.resetRuntime) {
339343
entry.startedAt = undefined;
@@ -517,6 +521,7 @@ export function transitionMainSessionRecovery(
517521
});
518522
entry.abortedLastRun = false;
519523
entry.lifecycleRunId = command.runId;
524+
entry.lastRunId = undefined;
520525
recordLifecycleFence(entry, {
521526
runId: command.runId,
522527
lifecycleGeneration: command.lifecycleGeneration,
@@ -548,6 +553,7 @@ export function transitionMainSessionRecovery(
548553
}
549554
entry.status = "running";
550555
entry.lifecycleRunId = undefined;
556+
entry.lastRunId = undefined;
551557
entry.abortedLastRun = true;
552558
entry.startedAt = undefined;
553559
entry.endedAt = undefined;
@@ -699,6 +705,7 @@ export function transitionMainSessionRecovery(
699705
entry.abortedLastRun = false;
700706
entry.status = "failed";
701707
entry.lifecycleRunId = undefined;
708+
entry.lastRunId = resolveRestartRecoveryTerminalClientRunId(entry);
702709
entry.endedAt = command.now;
703710
entry.runtimeMs = Math.max(0, command.now - (entry.startedAt ?? command.now));
704711
entry.updatedAt = command.now;

‎src/agents/main-session-recovery/main-session-restart-recovery-checkpoint.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { isRestartAbortTailArtifact } from "./main-session-restart-recovery-resu
2323
import {
2424
buildRestartRecoveryExpectedState,
2525
mainSessionRecoveryLog,
26+
resolveRestartRecoveryTerminalClientRunId,
2627
} from "./main-session-restart-recovery-shared.js";
2728

2829
export function hasOnlyAnnounceRecoveryRuns(entry: SessionEntry): boolean {
@@ -70,6 +71,7 @@ export async function reconcileInterruptedCompletionReport(params: {
7071
...buildMainSessionRecoveryClearPatch(entry),
7172
status: "killed",
7273
lifecycleRunId: undefined,
74+
lastRunId: resolveRestartRecoveryTerminalClientRunId(entry),
7375
abortedLastRun: false,
7476
endedAt,
7577
lastRunError: undefined,
@@ -323,6 +325,7 @@ export async function markSessionCompletedAfterRecoveryCheckpoint(params: {
323325
}),
324326
abortedLastRun: false,
325327
lifecycleRunId: undefined,
328+
lastRunId: resolveRestartRecoveryTerminalClientRunId(params.entry),
326329
endedAt,
327330
pendingFinalDelivery: undefined,
328331
restartRecoveryForceSafeTools: undefined,

‎src/agents/main-session-recovery/main-session-restart-recovery-failure.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { resolveRestartRecoveryDeliveryContext } from "./main-session-restart-di
1515
import {
1616
buildRestartRecoveryExpectedState,
1717
mainSessionRecoveryLog,
18+
resolveRestartRecoveryTerminalClientRunId,
1819
} from "./main-session-restart-recovery-shared.js";
1920

2021
const TOMBSTONED_SESSION_NOTICE =
@@ -151,6 +152,7 @@ export async function tombstoneMainRestartRecoveryWithNotice(params: {
151152
abortedLastRun: false,
152153
endedAt: now,
153154
lifecycleRunId: undefined,
155+
lastRunId: resolveRestartRecoveryTerminalClientRunId(entry),
154156
mainRestartRecovery: {
155157
...recoveryState,
156158
revision: recoveryState.revision + 1,

‎src/agents/main-session-recovery/main-session-restart-recovery-shared.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from "node:path";
22
import { asFiniteNumber } from "@openclaw/normalization-core/number-coercion";
3+
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
34
import { resolveStateDir } from "../../config/paths.js";
45
import {
56
listConfiguredSessionStoreAgentIds,
@@ -54,6 +55,14 @@ export function buildRestartRecoveryExpectedState(
5455
};
5556
}
5657

58+
export function resolveRestartRecoveryTerminalClientRunId(
59+
entry: Pick<SessionEntry, "restartRecoveryDeliverySourceRunId" | "restartRecoverySourceIngress">,
60+
): string | undefined {
61+
return entry.restartRecoverySourceIngress === "control-ui"
62+
? normalizeOptionalString(entry.restartRecoveryDeliverySourceRunId)
63+
: undefined;
64+
}
65+
5766
export function normalizeStringSet(values: Iterable<string> | undefined): Set<string> {
5867
const normalized = new Set<string>();
5968
for (const value of values ?? []) {

0 commit comments

Comments
 (0)