Skip to content

Commit 96cacfd

Browse files
authored
fix(gateway): batch worker placement session evidence (#124372)
* perf(gateway): batch worker placement session evidence * test(gateway): tolerate elapsed workspace timeout * fix(gateway): preserve unknown placement evidence * fix(sessions): validate batched placement evidence * fix(gateway): preserve incognito placement evidence * fix(sessions): bound configured target discovery * fix(gateway): canonicalize batched placement evidence
1 parent b07c6b2 commit 96cacfd

13 files changed

Lines changed: 675 additions & 124 deletions

‎src/config/sessions/session-accessor.readonly.test.ts‎

Lines changed: 107 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
hasSessionEntriesByStatusReadOnly,
2020
listSessionEntriesCore,
2121
listSessionEntriesReadOnly,
22-
readSessionIdentityEvidence,
22+
readSessionIdentityEvidenceBatch,
2323
resolveTranscriptSessionKeyBySessionId,
2424
upsertSessionEntryCore,
2525
} from "./session-accessor.js";
@@ -144,54 +144,128 @@ describe("session accessor readonly listing", () => {
144144
expect(countRegisteredAgentDatabases(env)).toBe(0);
145145
});
146146

147-
it("probes session identity by exact key and indexed current session id", async () => {
147+
it("batches exact, moved, absent, and unreadable session identity evidence", async () => {
148148
const stateDir = autoTempDirs.make("openclaw-session-readonly-evidence-");
149149
const env = { OPENCLAW_STATE_DIR: stateDir };
150150
const agentId = "worker-1";
151151
const sessionKey = "agent:worker-1:moved";
152152
const sessionId = "session-1";
153153
await upsertSessionEntryCore({ agentId, env, sessionKey }, { sessionId, updatedAt: 1 });
154154
const storePath = resolveOpenClawAgentSqlitePath({ agentId, env });
155-
closeOpenClawAgentDatabasesForTest();
156-
157-
expect(readSessionIdentityEvidence({ agentId, sessionId, sessionKey, storePath })).toEqual({
158-
status: "current",
159-
sessionKey,
155+
const invalidSessionKey = "agent:worker-1:invalid";
156+
await upsertSessionEntryCore(
157+
{ agentId, env, sessionKey: invalidSessionKey },
158+
{ sessionId: "invalid-session", updatedAt: 1 },
159+
);
160+
openOpenClawAgentDatabase({ agentId, env })
161+
.db.prepare("UPDATE session_nodes SET entry_valid = 0 WHERE session_key = ?")
162+
.run(invalidSessionKey);
163+
const migrationInvalidAgentId = "migration-invalid";
164+
const migrationInvalidSessionKey = "agent:migration-invalid:main";
165+
await upsertSessionEntryCore(
166+
{ agentId: migrationInvalidAgentId, env, sessionKey: migrationInvalidSessionKey },
167+
{ sessionId: "migration-invalid-session", updatedAt: 1 },
168+
);
169+
const invalidDatabase = openOpenClawAgentDatabase({ agentId: migrationInvalidAgentId, env });
170+
invalidDatabase.db.exec("PRAGMA user_version = 999;");
171+
const invalidStorePath = invalidDatabase.path;
172+
const missingAgentId = "missing";
173+
const missingStorePath = resolveOpenClawAgentSqlitePath({ agentId: missingAgentId, env });
174+
const unreadableAgentId = "unreadable";
175+
const unreadableStorePath = resolveOpenClawAgentSqlitePath({
176+
agentId: unreadableAgentId,
177+
env,
160178
});
179+
fs.mkdirSync(unreadableStorePath, { recursive: true });
180+
161181
expect(
162-
readSessionIdentityEvidence({
163-
agentId,
164-
sessionId,
165-
sessionKey: "agent:worker-1:old-key",
166-
storePath,
167-
}),
168-
).toEqual({ status: "current", sessionKey });
169-
expect(
170-
readSessionIdentityEvidence({
171-
agentId,
172-
sessionId: "missing-session",
173-
sessionKey,
174-
storePath,
175-
}),
176-
).toEqual({ status: "absent" });
182+
readSessionIdentityEvidenceBatch([
183+
{ agentId, sessionId, sessionKey, storePath },
184+
{
185+
agentId,
186+
sessionId,
187+
sessionKey: "agent:worker-1:old-key",
188+
storePath,
189+
},
190+
{ agentId, sessionId: "missing-session", sessionKey, storePath },
191+
{
192+
agentId,
193+
sessionId: "invalid-session",
194+
sessionKey: invalidSessionKey,
195+
storePath,
196+
},
197+
{
198+
agentId: missingAgentId,
199+
sessionId: "missing-session",
200+
sessionKey: "agent:missing:main",
201+
storePath: missingStorePath,
202+
},
203+
{
204+
agentId: migrationInvalidAgentId,
205+
sessionId: "migration-invalid-session",
206+
sessionKey: migrationInvalidSessionKey,
207+
storePath: invalidStorePath,
208+
},
209+
{
210+
agentId: unreadableAgentId,
211+
sessionId: "unreadable-session",
212+
sessionKey: "agent:unreadable:main",
213+
storePath: unreadableStorePath,
214+
},
215+
]),
216+
).toEqual([
217+
{ status: "current", sessionKey },
218+
{ status: "current", sessionKey },
219+
{ status: "absent" },
220+
{ status: "unknown", reason: "row-invalid" },
221+
{ status: "absent" },
222+
{ status: "unknown", reason: "read-failed" },
223+
{ status: "unknown", reason: "read-failed" },
224+
]);
177225
});
178226

179-
it("reports migration-invalid session evidence as unknown", async () => {
180-
const stateDir = autoTempDirs.make("openclaw-session-readonly-evidence-invalid-");
227+
it("rejects stale valid projections for unreadable session identity evidence", async () => {
228+
const stateDir = autoTempDirs.make("openclaw-session-readonly-stale-valid-evidence-");
181229
const env = { OPENCLAW_STATE_DIR: stateDir };
182230
const agentId = "worker-1";
183-
const sessionKey = "agent:worker-1:main";
184231
const sessionId = "session-1";
232+
const sessionKey = "agent:worker-1:main";
185233
await upsertSessionEntryCore({ agentId, env, sessionKey }, { sessionId, updatedAt: 1 });
234+
const readableSessionId = "session-2";
235+
const readableSessionKey = "agent:worker-1:readable";
236+
await upsertSessionEntryCore(
237+
{ agentId, env, sessionKey: readableSessionKey },
238+
{ sessionId: readableSessionId, updatedAt: 1 },
239+
);
186240
const database = openOpenClawAgentDatabase({ agentId, env });
187-
database.db.exec("PRAGMA user_version = 999;");
188-
const storePath = database.path;
189-
closeOpenClawAgentDatabasesForTest();
241+
database.db
242+
.prepare("UPDATE session_nodes SET entry_json = ? WHERE session_key = ?")
243+
.run(JSON.stringify({ sessionId: "mismatched-session", updatedAt: 1 }), sessionKey);
244+
database.db
245+
.prepare("UPDATE session_nodes SET entry_valid = 1 WHERE session_key = ?")
246+
.run(sessionKey);
190247

191-
expect(readSessionIdentityEvidence({ agentId, sessionId, sessionKey, storePath })).toEqual({
192-
status: "unknown",
193-
reason: "read-failed",
194-
});
248+
expect(
249+
readSessionIdentityEvidenceBatch([
250+
{ agentId, sessionId, sessionKey, storePath: database.path },
251+
{
252+
agentId,
253+
sessionId,
254+
sessionKey: "agent:worker-1:old-key",
255+
storePath: database.path,
256+
},
257+
{
258+
agentId,
259+
sessionId: readableSessionId,
260+
sessionKey: readableSessionKey,
261+
storePath: database.path,
262+
},
263+
]),
264+
).toEqual([
265+
{ status: "unknown", reason: "row-invalid" },
266+
{ status: "unknown", reason: "row-invalid" },
267+
{ status: "current", sessionKey: readableSessionKey },
268+
]);
195269
});
196270

197271
it("uses the current-session-id index for fallback identity probes", async () => {
@@ -201,7 +275,7 @@ describe("session accessor readonly listing", () => {
201275
const database = openOpenClawAgentDatabase({ agentId, env });
202276
const detail = database.db
203277
.prepare(
204-
"EXPLAIN QUERY PLAN SELECT session_key FROM session_nodes WHERE current_session_id = ? LIMIT 2",
278+
"EXPLAIN QUERY PLAN SELECT session_key FROM session_nodes WHERE current_session_id IN (?)",
205279
)
206280
.all("session-1")
207281
.map((row) => {

0 commit comments

Comments
 (0)