Skip to content

Commit 7235943

Browse files
authored
fix(release): align survivor session metadata assertion (#123068)
* fix(release): align survivor session metadata assertion * test(release): cover SQLite survivor metadata
1 parent 1c5fc51 commit 7235943

2 files changed

Lines changed: 39 additions & 34 deletions

File tree

‎scripts/e2e/lib/upgrade-survivor/assertions.mjs‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -835,19 +835,25 @@ function assertSessionMetadataMigrated(stateDir) {
835835
assert(main?.sessionId === LEGACY_SESSION_MAIN_ID, "main legacy session row missing");
836836
assert(direct?.sessionId === LEGACY_SESSION_DIRECT_ID, "direct legacy session row missing");
837837
assert(group?.sessionId === LEGACY_SESSION_GROUP_ID, "channel legacy session row missing");
838-
const migratedSessionIds = [
839-
LEGACY_SESSION_MAIN_ID,
840-
LEGACY_SESSION_DIRECT_ID,
841-
LEGACY_SESSION_GROUP_ID,
838+
const migratedSessions = [
839+
[LEGACY_SESSION_MAIN_ID, main],
840+
[LEGACY_SESSION_DIRECT_ID, direct],
841+
[LEGACY_SESSION_GROUP_ID, group],
842842
];
843+
for (const [sessionId, entry] of migratedSessions) {
844+
assert(
845+
!Object.hasOwn(entry ?? {}, "sessionFile"),
846+
`legacy session row retained retired sessionFile metadata for ${sessionId}`,
847+
);
848+
}
843849
if (source !== "file") {
844850
const dbPath = path.join(stateDir, "agents", "main", "agent", "openclaw-agent.sqlite");
845851
const db = new DatabaseSync(dbPath, { readOnly: true });
846852
try {
847853
const count = db.prepare(
848854
"SELECT COUNT(*) AS count FROM transcript_events WHERE session_id = ?",
849855
);
850-
for (const sessionId of migratedSessionIds) {
856+
for (const [sessionId] of migratedSessions) {
851857
const row = count.get(sessionId);
852858
assert(
853859
Number(row?.count ?? 0) > 0,
@@ -858,20 +864,12 @@ function assertSessionMetadataMigrated(stateDir) {
858864
db.close();
859865
}
860866
} else {
861-
for (const [sessionId, entry] of [
862-
[LEGACY_SESSION_MAIN_ID, main],
863-
[LEGACY_SESSION_DIRECT_ID, direct],
864-
[LEGACY_SESSION_GROUP_ID, group],
865-
]) {
867+
for (const [sessionId] of migratedSessions) {
866868
const expectedPath = path.join(agentSessionsDir, `${sessionId}.jsonl`);
867869
assert(
868870
fs.existsSync(expectedPath),
869871
`legacy session transcript was not moved for ${sessionId}`,
870872
);
871-
assert(
872-
entry?.sessionFile === expectedPath,
873-
`legacy session row still points at the old sessions directory for ${sessionId}`,
874-
);
875873
}
876874
}
877875
assert(

‎test/scripts/upgrade-survivor-assertions.test.ts‎

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,18 @@ function writeMigratedSessionState(stateDir: string): void {
9191
}
9292

9393
function createMigratedSessionFileStore(
94-
stateDir: string,
9594
options: { includePrompt?: boolean } = {},
9695
): Record<string, Record<string, unknown>> {
97-
const agentSessionsDir = join(stateDir, "agents", "main", "sessions");
98-
const main: Record<string, unknown> = {
99-
sessionId: "upgrade-main-session",
100-
sessionFile: join(agentSessionsDir, "upgrade-main-session.jsonl"),
101-
};
96+
const main: Record<string, unknown> = { sessionId: "upgrade-main-session" };
10297
if (options.includePrompt !== false) {
10398
main.skillsSnapshot = {
10499
prompt: "legacy prompt survives as metadata",
105100
};
106101
}
107102
return {
108103
"agent:main:main": main,
109-
"agent:main:+15551234567": {
110-
sessionId: "upgrade-direct-session",
111-
sessionFile: join(agentSessionsDir, "upgrade-direct-session.jsonl"),
112-
},
113-
"agent:main:slack:channel:cupgrade": {
114-
sessionId: "upgrade-group-session",
115-
sessionFile: join(agentSessionsDir, "upgrade-group-session.jsonl"),
116-
},
104+
"agent:main:+15551234567": { sessionId: "upgrade-direct-session" },
105+
"agent:main:slack:channel:cupgrade": { sessionId: "upgrade-group-session" },
117106
};
118107
}
119108

@@ -123,10 +112,7 @@ function writeMigratedSessionFiles(
123112
): void {
124113
const agentSessionsDir = join(stateDir, "agents", "main", "sessions");
125114
mkdirSync(agentSessionsDir, { recursive: true });
126-
writeJson(
127-
join(agentSessionsDir, "sessions.json"),
128-
createMigratedSessionFileStore(stateDir, options),
129-
);
115+
writeJson(join(agentSessionsDir, "sessions.json"), createMigratedSessionFileStore(options));
130116
for (const sessionId of [
131117
"upgrade-main-session",
132118
"upgrade-direct-session",
@@ -162,7 +148,7 @@ function writeLegacyCacheSessionState(
162148
const insert = db.prepare(
163149
"INSERT INTO cache_entries (scope, key, value_json) VALUES (?, ?, ?)",
164150
);
165-
for (const [key, entry] of Object.entries(createMigratedSessionFileStore(stateDir, options))) {
151+
for (const [key, entry] of Object.entries(createMigratedSessionFileStore(options))) {
166152
insert.run("session_entries", key, JSON.stringify(entry));
167153
}
168154
} finally {
@@ -187,7 +173,7 @@ function writeLegacySessionEntriesState(stateDir: string): void {
187173
INSERT INTO session_entries (session_key, session_id, entry_json, updated_at)
188174
VALUES (?, ?, ?, ?)
189175
`);
190-
for (const [key, entry] of Object.entries(createMigratedSessionFileStore(stateDir))) {
176+
for (const [key, entry] of Object.entries(createMigratedSessionFileStore())) {
191177
const sessionId = entry.sessionId;
192178
if (typeof sessionId !== "string") {
193179
throw new TypeError(`missing fixture session id for ${key}`);
@@ -594,6 +580,27 @@ describe("upgrade survivor assertions", () => {
594580
).not.toThrow();
595581
});
596582

583+
it("rejects retired sessionFile metadata in SQLite-backed session rows", () => {
584+
expect(() =>
585+
runSessionStateAssertion((stateDir) => {
586+
writeMigratedSessionState(stateDir);
587+
const db = new DatabaseSync(
588+
join(stateDir, "agents", "main", "agent", "openclaw-agent.sqlite"),
589+
);
590+
try {
591+
db.prepare("UPDATE session_nodes SET entry_json = ? WHERE session_key = ?").run(
592+
JSON.stringify({
593+
sessionFile: join(stateDir, "sessions", "upgrade-main-session.jsonl"),
594+
}),
595+
"agent:main:main",
596+
);
597+
} finally {
598+
db.close();
599+
}
600+
}),
601+
).toThrow(/retained retired sessionFile metadata/);
602+
});
603+
597604
it("rejects ClawHub npm-pack installs outside the managed extensions root", () => {
598605
const root = mkdtempSync(join(tmpdir(), "openclaw-upgrade-survivor-outside-"));
599606
try {

0 commit comments

Comments
 (0)