|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import { |
| 3 | + EventId, |
| 4 | + ProjectId, |
| 5 | + ProviderInstanceId, |
| 6 | + ThreadId, |
| 7 | + type OrchestrationV2AppThread, |
| 8 | +} from "@t3tools/contracts"; |
| 9 | +import * as DateTime from "effect/DateTime"; |
| 10 | +import * as Effect from "effect/Effect"; |
| 11 | +import * as Layer from "effect/Layer"; |
| 12 | +import * as Tracer from "effect/Tracer"; |
| 13 | +import * as Schema from "effect/Schema"; |
| 14 | +import * as SqlClient from "effect/sql/SqlClient"; |
| 15 | +import * as Persistence from "../persistence/Layers/Sqlite.ts"; |
| 16 | +import * as ProjectionStore from "./ProjectionStore.ts"; |
| 17 | + |
| 18 | +const encodeSessionError = Schema.encodeSync( |
| 19 | + Schema.fromJsonString(Schema.Struct({ lastError: Schema.String })), |
| 20 | +); |
| 21 | + |
| 22 | +const layer = ProjectionStore.layer.pipe(Layer.provideMerge(Persistence.layerMemory)); |
| 23 | + |
| 24 | +it.effect( |
| 25 | + "starts shell error lookups from thread bindings while preserving latest matching sessions", |
| 26 | + () => |
| 27 | + Effect.gen(function* () { |
| 28 | + const store = yield* ProjectionStore.ProjectionStoreV2; |
| 29 | + const sql = yield* SqlClient.SqlClient; |
| 30 | + const now = yield* DateTime.now; |
| 31 | + const provider = ProviderInstanceId.make("codex"); |
| 32 | + const threadIds = ["first", "shared", "unbound"].map((name) => |
| 33 | + ThreadId.make(`thread:session-lookup:${name}`), |
| 34 | + ); |
| 35 | + for (const id of threadIds) { |
| 36 | + const payload: OrchestrationV2AppThread = { |
| 37 | + createdBy: "user", |
| 38 | + creationSource: "web", |
| 39 | + id, |
| 40 | + projectId: ProjectId.make("project:session-lookup"), |
| 41 | + title: "Session lookup", |
| 42 | + providerInstanceId: provider, |
| 43 | + modelSelection: { instanceId: provider, model: "gpt-5.4" }, |
| 44 | + runtimeMode: "full-access", |
| 45 | + interactionMode: "default", |
| 46 | + branch: null, |
| 47 | + worktreePath: null, |
| 48 | + activeProviderThreadId: null, |
| 49 | + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: id }, |
| 50 | + forkedFrom: null, |
| 51 | + createdAt: now, |
| 52 | + updatedAt: now, |
| 53 | + archivedAt: null, |
| 54 | + settledOverride: null, |
| 55 | + settledAt: null, |
| 56 | + lastVisitedAt: null, |
| 57 | + deletedAt: null, |
| 58 | + }; |
| 59 | + yield* store.apply({ |
| 60 | + id: EventId.make(`event:session-lookup:${id}`), |
| 61 | + type: "thread.created", |
| 62 | + threadId: id, |
| 63 | + occurredAt: now, |
| 64 | + payload, |
| 65 | + }); |
| 66 | + } |
| 67 | + const sessions = [ |
| 68 | + { id: "old", provider: "codex", updated: "2026-01-01", error: "old error" }, |
| 69 | + { id: "tie-a", provider: "codex", updated: "2026-01-02", error: "tie a" }, |
| 70 | + { id: "tie-z", provider: "codex", updated: "2026-01-02", error: "latest matching" }, |
| 71 | + { |
| 72 | + id: "wrong-provider", |
| 73 | + provider: "claude", |
| 74 | + updated: "2026-01-03", |
| 75 | + error: "wrong provider", |
| 76 | + }, |
| 77 | + { id: "unbound-newest", provider: "codex", updated: "2026-01-04", error: "unbound" }, |
| 78 | + ]; |
| 79 | + for (const session of sessions) { |
| 80 | + yield* sql`INSERT INTO orchestration_v2_projection_provider_sessions |
| 81 | + (provider_session_id, provider, provider_instance_id, status, updated_at, payload_json) |
| 82 | + VALUES (${session.id}, ${session.provider}, ${session.provider}, 'error', ${session.updated}, ${encodeSessionError({ lastError: session.error })})`; |
| 83 | + if (session.id !== "unbound-newest") { |
| 84 | + yield* sql`INSERT INTO orchestration_v2_projection_provider_session_bindings VALUES (${session.id}, ${threadIds[0]})`; |
| 85 | + } |
| 86 | + } |
| 87 | + yield* sql`INSERT INTO orchestration_v2_projection_provider_session_bindings VALUES ('tie-z', ${threadIds[1]})`; |
| 88 | + let shellQuery: string | undefined; |
| 89 | + const tracer = Tracer.make({ |
| 90 | + span(options) { |
| 91 | + const span = new Tracer.NativeSpan(options); |
| 92 | + const end = span.end.bind(span); |
| 93 | + span.end = (time, exit) => { |
| 94 | + end(time, exit); |
| 95 | + const query = span.attributes.get("db.query.text"); |
| 96 | + if (typeof query === "string" && query.includes("AS last_error")) shellQuery = query; |
| 97 | + }; |
| 98 | + return span; |
| 99 | + }, |
| 100 | + }); |
| 101 | + const shell = yield* store.getShellSnapshot().pipe(Effect.withTracer(tracer)); |
| 102 | + assert.deepEqual( |
| 103 | + shell.threads.map((thread) => thread.lastError), |
| 104 | + ["latest matching", "latest matching", null], |
| 105 | + ); |
| 106 | + assert.isDefined(shellQuery); |
| 107 | + const plan = yield* sql.unsafe<{ detail: string }>(`EXPLAIN QUERY PLAN ${shellQuery}`); |
| 108 | + assert.isTrue( |
| 109 | + plan.some((row) => |
| 110 | + row.detail.includes( |
| 111 | + "SEARCH binding USING INDEX orchestration_v2_projection_provider_session_bindings_thread_idx", |
| 112 | + ), |
| 113 | + ), |
| 114 | + ); |
| 115 | + assert.isFalse( |
| 116 | + plan.some((row) => |
| 117 | + row.detail.includes( |
| 118 | + "SEARCH session USING INDEX orchestration_v2_projection_provider_sessions_instance_status_idx", |
| 119 | + ), |
| 120 | + ), |
| 121 | + ); |
| 122 | + }).pipe(Effect.provide(layer)), |
| 123 | +); |
0 commit comments