Skip to content

Commit d9d13cd

Browse files
committed
fix(gateway): project placement facts on chat startup
chat.startup and chat.history built sessionInfo without the placement projection sessions.list applies, so clients merging that row erased a live worker placement and its move intent — the Runs on Cloud chip disappeared after any turn until a full reload. Reuse the canonical ensureColumn helper for the move table's additive column instead of a hand-rolled PRAGMA read.
1 parent 50024a0 commit d9d13cd

3 files changed

Lines changed: 63 additions & 10 deletions

File tree

‎src/gateway/server-methods/chat-history-handler.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
startOptionalServerMethodModelCatalogSnapshotLoad,
6262
} from "./optional-model-catalog.js";
6363
import { resolveVisibleActiveSessionRunState } from "./session-active-runs.js";
64+
import { readSessionPlacementFields } from "./session-placement-read-projection.js";
6465
import type { GatewayRequestHandlerOptions, GatewayRequestHandlers } from "./types.js";
6566
import { assertValidParams } from "./validation.js";
6667

@@ -479,6 +480,10 @@ async function handleChatHistoryRequest({
479480
});
480481
sessionInfo.hasActiveRun = activeRunState.active;
481482
sessionInfo.activeRunIds = activeRunState.runIds;
483+
// Clients merge this row into the same store sessions.list fills, so it must
484+
// carry the placement facts that projection adds; without them the merge
485+
// erases a live worker placement and its move intent.
486+
Object.assign(sessionInfo, readSessionPlacementFields(context, entry?.sessionId));
482487
if (Object.hasOwn(historyPage, "activeLeafEntryId")) {
483488
sessionInfo.activeLeafEntryId = historyPage.activeLeafEntryId ?? null;
484489
}

‎src/gateway/server.chat.gateway-server-chat-b.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,60 @@ async function prepareMainHistoryHarness(params: {
684684
}
685685

686686
describe("gateway server chat", () => {
687+
test.each(["chat.history", "chat.startup"] as const)(
688+
"%s projects the session's durable worker placement",
689+
async (method) => {
690+
openDirectChatSession();
691+
try {
692+
await writeMainSessionStore();
693+
const placement = {
694+
sessionId: "sess-main",
695+
agentId: "main",
696+
sessionKey: "agent:main:main",
697+
executionMode: "worker-turn",
698+
state: "active",
699+
environmentId: "env-placement",
700+
generation: 7,
701+
activeOwnerEpoch: 12,
702+
workspaceBaseManifestRef: "manifest-base",
703+
remoteWorkspaceDir: "/workspace/main",
704+
workerBundleHash: "ab".repeat(32),
705+
recoveryError: null,
706+
terminalReason: null,
707+
terminalAtMs: null,
708+
turnClaim: null,
709+
createdAtMs: 100,
710+
updatedAtMs: 300,
711+
stateChangedAtMs: 200,
712+
};
713+
const context = createDirectChatContext({
714+
workerSessionPlacementService: {
715+
getMany: () => new Map([[placement.sessionId, placement]]),
716+
getPlacementMoves: () => new Map(),
717+
},
718+
} as unknown as Partial<GatewayRequestContext>);
719+
const responses: Array<{ ok: boolean; payload?: unknown }> = [];
720+
await callDirectChat(method, {
721+
id: method,
722+
params: makeMainSessionParams(),
723+
respond: captureChatResult(responses),
724+
context,
725+
});
726+
727+
expect(responses[0]?.ok).toBe(true);
728+
// Clients merge this row into the same store sessions.list fills, so a
729+
// missing placement here silently erases a live worker placement.
730+
expect(
731+
(responses[0]?.payload as { sessionInfo?: { placement?: { state?: string } } })
732+
?.sessionInfo?.placement,
733+
).toMatchObject({ state: "active", environmentId: "env-placement" });
734+
} finally {
735+
testState.sessionStorePath = undefined;
736+
clearConfigCache();
737+
}
738+
},
739+
);
740+
687741
test.each(["chat.history", "chat.startup"] as const)(
688742
"%s replays the active plan snapshot in inFlightRun",
689743
async (method) => {

‎src/gateway/worker-environments/placement-move-intent.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
getNodeSqliteKysely,
1010
} from "../../infra/kysely-sync.js";
1111
import { generateSecureToken } from "../../infra/secure-random.js";
12-
import { tableExists } from "../../state/openclaw-state-db-schema-helpers.js";
12+
import { ensureColumn, tableExists } from "../../state/openclaw-state-db-schema-helpers.js";
1313
import type {
1414
DB as StateDatabase,
1515
WorkerSessionPlacementMoves,
@@ -26,8 +26,6 @@ const MOVE_SCHEMA_END = "\n) STRICT;";
2626
const MOVE_OPERATION_PREFIX = "move:v1:";
2727
const MOVE_MACHINE_CLASS_MAX_LENGTH = 128;
2828

29-
type MoveColumnInfo = { name?: unknown };
30-
3129
type MoveRow = Selectable<WorkerSessionPlacementMoves>;
3230
type MoveDatabase = Pick<
3331
StateDatabase,
@@ -72,13 +70,9 @@ function ensureWorkerPlacementMoveSchema(db: DatabaseSync): void {
7270
return;
7371
}
7472
db.exec(moveSchemaSql()); // sqlite-allow-raw -- Canonical feature-owned additive DDL only.
75-
const columns = /* sqlite-allow-raw -- Canonical additive schema inspection only. */ db
76-
.prepare("PRAGMA table_info(worker_session_placement_moves)")
77-
.all() as MoveColumnInfo[];
78-
if (!columns.some((column) => column.name === "target_machine_class")) {
79-
// sqlite-allow-raw -- Unreleased additive column migration.
80-
db.exec("ALTER TABLE worker_session_placement_moves ADD COLUMN target_machine_class TEXT");
81-
}
73+
// Databases that created this table before the column shipped upgrade in place;
74+
// the column is bare and nullable, so old readers stay compatible.
75+
ensureColumn(db, "worker_session_placement_moves", "target_machine_class TEXT");
8276
ensuredMoveSchemaHandles.add(db);
8377
}
8478

0 commit comments

Comments
 (0)