|
1 | 1 | import { assert, it } from "@effect/vitest"; |
2 | | -import { vi } from "vitest"; |
| 2 | +import { vi } from "vite-plus/test"; |
3 | 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
4 | 4 | import { ThreadId } from "@t3tools/contracts"; |
5 | 5 | import * as Effect from "effect/Effect"; |
@@ -1122,53 +1122,81 @@ layer("ThreadColdStorage", (it) => { |
1122 | 1122 | }), |
1123 | 1123 | ); |
1124 | 1124 |
|
1125 | | - it.effect("ignores traversal attachment entries while restoring", () => |
1126 | | - Effect.gen(function* () { |
1127 | | - const sql = yield* SqlClient.SqlClient; |
1128 | | - const storage = yield* ThreadColdStorage.ThreadColdStorage; |
1129 | | - const config = yield* ServerConfig.ServerConfig; |
1130 | | - const fs = yield* FileSystem.FileSystem; |
1131 | | - const path = yield* Path.Path; |
1132 | | - const threadId = ThreadId.make("thread-traversal"); |
1133 | | - const attachmentName = "thread-traversal-00000000-0000-4000-8000-000000000001.png"; |
1134 | | - |
1135 | | - yield* insertArchivedThread(threadId, "Traversal thread"); |
1136 | | - yield* sql.unsafe( |
1137 | | - `INSERT INTO projection_thread_messages ( |
| 1125 | + it.effect( |
| 1126 | + "retains cold data when an unsafe attachment entry blocks restore, then retries after repair", |
| 1127 | + () => |
| 1128 | + Effect.gen(function* () { |
| 1129 | + const sql = yield* SqlClient.SqlClient; |
| 1130 | + const storage = yield* ThreadColdStorage.ThreadColdStorage; |
| 1131 | + const config = yield* ServerConfig.ServerConfig; |
| 1132 | + const fs = yield* FileSystem.FileSystem; |
| 1133 | + const path = yield* Path.Path; |
| 1134 | + const threadId = ThreadId.make("thread-traversal"); |
| 1135 | + const attachmentName = "thread-traversal-00000000-0000-4000-8000-000000000001.png"; |
| 1136 | + |
| 1137 | + yield* insertArchivedThread(threadId, "Traversal thread"); |
| 1138 | + yield* sql.unsafe( |
| 1139 | + `INSERT INTO projection_thread_messages ( |
1138 | 1140 | message_id, thread_id, turn_id, role, text, attachments_json, |
1139 | 1141 | is_streaming, created_at, updated_at |
1140 | 1142 | ) VALUES (?, ?, NULL, 'user', 'validate attachment name', ?, 0, ?, ?)`, |
1141 | | - [ |
1142 | | - "message-traversal", |
1143 | | - threadId, |
1144 | | - encodeUnknownJsonString([ |
1145 | | - { |
1146 | | - type: "image", |
1147 | | - id: attachmentName.slice(0, -4), |
1148 | | - name: "image.png", |
1149 | | - mimeType: "image/png", |
1150 | | - }, |
1151 | | - ]), |
1152 | | - "2026-07-01T00:00:00.000Z", |
1153 | | - "2026-07-01T00:00:00.000Z", |
1154 | | - ], |
1155 | | - ); |
1156 | | - yield* fs.writeFileString(path.join(config.attachmentsDir, attachmentName), "image bytes"); |
1157 | | - yield* storage.archiveThread(threadId); |
1158 | | - const escapedPath = path.join(config.attachmentsDir, "..", "thread-traversal-escape"); |
1159 | | - yield* sql` |
| 1143 | + [ |
| 1144 | + "message-traversal", |
| 1145 | + threadId, |
| 1146 | + encodeUnknownJsonString([ |
| 1147 | + { |
| 1148 | + type: "image", |
| 1149 | + id: attachmentName.slice(0, -4), |
| 1150 | + name: "image.png", |
| 1151 | + mimeType: "image/png", |
| 1152 | + }, |
| 1153 | + ]), |
| 1154 | + "2026-07-01T00:00:00.000Z", |
| 1155 | + "2026-07-01T00:00:00.000Z", |
| 1156 | + ], |
| 1157 | + ); |
| 1158 | + yield* fs.writeFileString(path.join(config.attachmentsDir, attachmentName), "image bytes"); |
| 1159 | + yield* storage.archiveThread(threadId); |
| 1160 | + const escapedPath = path.join(config.attachmentsDir, "..", "thread-traversal-escape"); |
| 1161 | + yield* sql` |
1160 | 1162 | UPDATE cold_archive.archive_thread_chunks |
1161 | 1163 | SET kind = 'attachment:../thread-traversal-escape' |
1162 | 1164 | WHERE thread_id = ${threadId} AND kind LIKE 'attachment:%' |
1163 | 1165 | `; |
1164 | 1166 |
|
1165 | | - assert.isTrue(yield* storage.restoreTree(threadId)); |
1166 | | - const manifest = yield* sql<{ readonly status: string }>` |
| 1167 | + const failure = yield* Effect.flip(storage.restoreTree(threadId)); |
| 1168 | + assert.strictEqual(failure.operation, "restore"); |
| 1169 | + const manifest = yield* sql<{ readonly status: string }>` |
1167 | 1170 | SELECT status FROM thread_archive_manifests WHERE thread_id = ${threadId} |
1168 | 1171 | `; |
1169 | | - assert.deepStrictEqual(manifest, [{ status: "restored" }]); |
1170 | | - assert.isFalse(yield* fs.exists(escapedPath)); |
1171 | | - }), |
| 1172 | + assert.deepStrictEqual(manifest, [{ status: "cold" }]); |
| 1173 | + assert.deepStrictEqual( |
| 1174 | + yield* sql`SELECT message_id FROM projection_thread_messages WHERE thread_id = ${threadId}`, |
| 1175 | + [], |
| 1176 | + ); |
| 1177 | + assert.isAbove( |
| 1178 | + (yield* sql<{ |
| 1179 | + count: number; |
| 1180 | + }>`SELECT COUNT(*) AS count FROM cold_archive.archive_thread_chunks WHERE thread_id = ${threadId}`)[0] |
| 1181 | + ?.count ?? 0, |
| 1182 | + 0, |
| 1183 | + ); |
| 1184 | + assert.isFalse(yield* fs.exists(escapedPath)); |
| 1185 | + yield* sql` |
| 1186 | + UPDATE cold_archive.archive_thread_chunks |
| 1187 | + SET kind = ${`attachment:${attachmentName}`} |
| 1188 | + WHERE thread_id = ${threadId} AND kind LIKE 'attachment:%' |
| 1189 | + `; |
| 1190 | + assert.isTrue(yield* storage.restoreTree(threadId)); |
| 1191 | + assert.strictEqual( |
| 1192 | + yield* fs.readFileString(path.join(config.attachmentsDir, attachmentName)), |
| 1193 | + "image bytes", |
| 1194 | + ); |
| 1195 | + assert.deepStrictEqual( |
| 1196 | + yield* sql`SELECT text FROM projection_thread_messages WHERE thread_id = ${threadId}`, |
| 1197 | + [{ text: "validate attachment name" }], |
| 1198 | + ); |
| 1199 | + }), |
1172 | 1200 | ); |
1173 | 1201 |
|
1174 | 1202 | it.effect("keeps cold SQL data authoritative when attachment restore fails", () => |
@@ -1468,6 +1496,41 @@ layer("ThreadColdStorage", (it) => { |
1468 | 1496 | UPDATE projection_threads SET archived_at = NULL WHERE thread_id = ${threadId} |
1469 | 1497 | `; |
1470 | 1498 |
|
| 1499 | + yield* sql` |
| 1500 | + INSERT INTO cold_archive.archive_threads (thread_id, root_thread_id, archive_version, archived_at, original_bytes, compressed_bytes, created_at) |
| 1501 | + VALUES (${threadId}, ${threadId}, 1, '2026-07-02T00:00:00.000Z', 1, 1, CURRENT_TIMESTAMP) |
| 1502 | + `; |
| 1503 | + yield* sql` |
| 1504 | + INSERT INTO cold_archive.archive_thread_chunks (thread_id, chunk_index, kind, row_count, data) |
| 1505 | + VALUES (${threadId}, 0, 'table:projection_thread_messages', 0, ${new Uint8Array([1])}) |
| 1506 | + `; |
| 1507 | + const unsafe = sql.unsafe.bind(sql); |
| 1508 | + const spy = vi |
| 1509 | + .spyOn(sql, "unsafe") |
| 1510 | + .mockImplementation((query, params) => |
| 1511 | + unsafe( |
| 1512 | + query.includes("DELETE FROM thread_archive_manifests") |
| 1513 | + ? "SELECT * FROM missing_aborted_archive_cleanup_fixture" |
| 1514 | + : query, |
| 1515 | + params, |
| 1516 | + ), |
| 1517 | + ); |
| 1518 | + yield* Effect.flip(storage.archiveThread(threadId)).pipe( |
| 1519 | + Effect.ensuring(Effect.sync(() => spy.mockRestore())), |
| 1520 | + ); |
| 1521 | + assert.deepStrictEqual( |
| 1522 | + yield* sql`SELECT status FROM thread_archive_manifests WHERE thread_id = ${threadId}`, |
| 1523 | + [{ status: "pending" }], |
| 1524 | + ); |
| 1525 | + assert.deepStrictEqual( |
| 1526 | + yield* sql`SELECT thread_id FROM cold_archive.archive_threads WHERE thread_id = ${threadId}`, |
| 1527 | + [], |
| 1528 | + ); |
| 1529 | + assert.deepStrictEqual( |
| 1530 | + yield* sql`SELECT chunk_index FROM cold_archive.archive_thread_chunks WHERE thread_id = ${threadId}`, |
| 1531 | + [], |
| 1532 | + ); |
| 1533 | + assert.include(yield* storage.listPendingArchiveThreadIds, threadId); |
1471 | 1534 | yield* storage.archiveThread(threadId); |
1472 | 1535 | const messages = yield* sql<{ readonly text: string }>` |
1473 | 1536 | SELECT text FROM projection_thread_messages WHERE thread_id = ${threadId} |
|
0 commit comments