|
| 1 | +/** |
| 2 | + * @spec-handoff |
| 3 | + * @interface Session.list(input?: { archived?: boolean }): Effect<Info[]> |
| 4 | + * @behavior |
| 5 | + * - By default (no `archived` flag): EXCLUDE sessions whose `time_archived` is non-null |
| 6 | + * - When `archived: true` is passed: INCLUDE archived sessions in results |
| 7 | + * - The LIMIT window is counted AFTER the archived filter, so archived rows never |
| 8 | + * consume LIMIT slots that belong to active (unarchived) sessions |
| 9 | + * @edge-cases |
| 10 | + * - `ListInput` at session.ts:291-300 currently has no `archived` field — must be added |
| 11 | + * - `listByProject` at session.ts:995-1048 has no archived filter — must mirror the |
| 12 | + * `isNull(SessionTable.time_archived)` guard already present in `listGlobal` (line 604) |
| 13 | + * - Key regression (#24850): when archived sessions have a more recent `time_updated` than |
| 14 | + * active sessions, they appear first in the ORDER BY DESC window and consume LIMIT slots, |
| 15 | + * pushing active sessions past the cutoff — unarchived sessions vanish from the sidebar |
| 16 | + * @see ../../src/session/session.ts (listByProject, ListInput, listGlobal) |
| 17 | + */ |
| 18 | +import { afterEach, describe, expect } from "bun:test" |
| 19 | +import { Effect, Layer } from "effect" |
| 20 | +import { Database } from "@opencode-ai/core/database/database" |
| 21 | +import { SessionProjector } from "@opencode-ai/core/session/projector" |
| 22 | +import { Session as SessionNs } from "@/session/session" |
| 23 | +import { disposeAllInstances } from "../fixture/fixture" |
| 24 | +import { testEffect } from "../lib/effect" |
| 25 | +import { EventV2Bridge } from "@/event-v2-bridge" |
| 26 | +import { Storage } from "@/storage/storage" |
| 27 | +import { RuntimeFlags } from "@/effect/runtime-flags" |
| 28 | +import { BackgroundJob } from "@/background/job" |
| 29 | +import { SessionTable } from "@opencode-ai/core/session/sql" |
| 30 | +import { eq } from "drizzle-orm" |
| 31 | + |
| 32 | +const layer = Layer.mergeAll( |
| 33 | + Database.defaultLayer, |
| 34 | + SessionNs.layer.pipe( |
| 35 | + Layer.provide(EventV2Bridge.defaultLayer), |
| 36 | + Layer.provide(Storage.defaultLayer), |
| 37 | + Layer.provide(Database.defaultLayer), |
| 38 | + Layer.provide(EventV2Bridge.defaultLayer), |
| 39 | + Layer.provide(SessionProjector.defaultLayer), |
| 40 | + Layer.provide(RuntimeFlags.layer({ experimentalWorkspaces: false })), |
| 41 | + Layer.provide(BackgroundJob.defaultLayer), |
| 42 | + ), |
| 43 | +) |
| 44 | + |
| 45 | +const it = testEffect(layer) |
| 46 | + |
| 47 | +const withSession = (input?: Parameters<SessionNs.Interface["create"]>[0]) => |
| 48 | + Effect.acquireRelease(SessionNs.use.create(input), (created) => |
| 49 | + SessionNs.Service.use((session) => session.remove(created.id).pipe(Effect.ignore)), |
| 50 | + ) |
| 51 | + |
| 52 | +afterEach(async () => { |
| 53 | + await disposeAllInstances() |
| 54 | +}) |
| 55 | + |
| 56 | +describe("session.list archived filter (regression #24850)", () => { |
| 57 | + it.instance( |
| 58 | + "excludes archived sessions by default", |
| 59 | + () => |
| 60 | + Effect.gen(function* () { |
| 61 | + const active = yield* withSession({ title: "active-session" }) |
| 62 | + const archived = yield* withSession({ title: "archived-session" }) |
| 63 | + |
| 64 | + yield* SessionNs.Service.use((session) => |
| 65 | + session.setArchived({ sessionID: archived.id, time: Date.now() }), |
| 66 | + ) |
| 67 | + |
| 68 | + const sessions = yield* SessionNs.use.list() |
| 69 | + const ids = sessions.map((s) => s.id) |
| 70 | + |
| 71 | + // Active session must appear in the default (no-archived-flag) list. |
| 72 | + expect(ids).toContain(active.id) |
| 73 | + // Archived session must NOT appear in the default list — this assertion |
| 74 | + // currently FAILS because listByProject has no archived filter. |
| 75 | + expect(ids).not.toContain(archived.id) |
| 76 | + }), |
| 77 | + { git: true }, |
| 78 | + ) |
| 79 | + |
| 80 | + it.instance( |
| 81 | + "includes archived sessions when archived flag is true", |
| 82 | + () => |
| 83 | + Effect.gen(function* () { |
| 84 | + const active = yield* withSession({ title: "active-session" }) |
| 85 | + const archived = yield* withSession({ title: "archived-session" }) |
| 86 | + |
| 87 | + yield* SessionNs.Service.use((session) => |
| 88 | + session.setArchived({ sessionID: archived.id, time: Date.now() }), |
| 89 | + ) |
| 90 | + |
| 91 | + // Pass archived: true — requires `archived?: boolean` in ListInput (currently absent). |
| 92 | + // The @ts-expect-error below suppresses the missing-property error right now. |
| 93 | + // Once the fix adds `archived` to ListInput, remove the @ts-expect-error line: |
| 94 | + // the type will compile cleanly and both runtime assertions below must hold. |
| 95 | + const sessions = yield* SessionNs.Service.use((session) => |
| 96 | + session.list({ archived: true }), |
| 97 | + ) |
| 98 | + const ids = sessions.map((s) => s.id) |
| 99 | + |
| 100 | + expect(ids).toContain(active.id) |
| 101 | + expect(ids).toContain(archived.id) |
| 102 | + }), |
| 103 | + { git: true }, |
| 104 | + ) |
| 105 | + |
| 106 | + it.instance( |
| 107 | + "archived sessions do not consume LIMIT slots intended for active sessions (core #24850 regression)", |
| 108 | + () => |
| 109 | + Effect.gen(function* () { |
| 110 | + const { db } = yield* Database.Service |
| 111 | + |
| 112 | + // Create 3 active sessions first (older timestamps). |
| 113 | + const active1 = yield* withSession({ title: "active-1" }) |
| 114 | + const active2 = yield* withSession({ title: "active-2" }) |
| 115 | + const active3 = yield* withSession({ title: "active-3" }) |
| 116 | + |
| 117 | + // Create 3 archived sessions and give them a time_updated far in the |
| 118 | + // future so they sort ABOVE the active sessions in the DESC window. |
| 119 | + // This is the exact scenario in #24850: a session is archived but its |
| 120 | + // time_updated is more recent, so it crowds out active sessions. |
| 121 | + const archived1 = yield* withSession({ title: "archived-1" }) |
| 122 | + const archived2 = yield* withSession({ title: "archived-2" }) |
| 123 | + const archived3 = yield* withSession({ title: "archived-3" }) |
| 124 | + |
| 125 | + const futureTs = Date.now() + 1_000_000 |
| 126 | + |
| 127 | + // Stamp the archived rows with a future time_updated so they rank first. |
| 128 | + for (const s of [archived1, archived2, archived3]) { |
| 129 | + yield* db |
| 130 | + .update(SessionTable) |
| 131 | + .set({ time_updated: futureTs }) |
| 132 | + .where(eq(SessionTable.id, s.id)) |
| 133 | + .run() |
| 134 | + .pipe(Effect.orDie) |
| 135 | + } |
| 136 | + |
| 137 | + yield* SessionNs.Service.use((session) => |
| 138 | + session.setArchived({ sessionID: archived1.id, time: Date.now() }), |
| 139 | + ) |
| 140 | + yield* SessionNs.Service.use((session) => |
| 141 | + session.setArchived({ sessionID: archived2.id, time: Date.now() }), |
| 142 | + ) |
| 143 | + yield* SessionNs.Service.use((session) => |
| 144 | + session.setArchived({ sessionID: archived3.id, time: Date.now() }), |
| 145 | + ) |
| 146 | + |
| 147 | + // Request exactly 3 results. Without the archived filter in listByProject, |
| 148 | + // the query returns the 3 archived rows (highest time_updated) and the 3 |
| 149 | + // active sessions fall past the LIMIT cutoff — they never appear. |
| 150 | + const sessions = yield* SessionNs.Service.use((session) => session.list({ limit: 3 })) |
| 151 | + const ids = sessions.map((s) => s.id) |
| 152 | + |
| 153 | + // All 3 active sessions must be present within the limit=3 window. |
| 154 | + // Currently FAILS: archived rows eat all 3 slots. |
| 155 | + expect(ids).toContain(active1.id) |
| 156 | + expect(ids).toContain(active2.id) |
| 157 | + expect(ids).toContain(active3.id) |
| 158 | + |
| 159 | + // Archived sessions must be absent from the default (no-flag) list. |
| 160 | + expect(ids).not.toContain(archived1.id) |
| 161 | + expect(ids).not.toContain(archived2.id) |
| 162 | + expect(ids).not.toContain(archived3.id) |
| 163 | + }), |
| 164 | + { git: true }, |
| 165 | + ) |
| 166 | +}) |
0 commit comments