Skip to content

Commit ca0a3aa

Browse files
shoootyouyui-soul
andcommitted
fix(session): exclude archived sessions from project list by default
listByProject (the instance-scoped Session.list used by the sidebar) had no time_archived filter, so archived sessions consumed LIMIT-window slots and pushed active sessions out of the sidebar (anomalyco#24850). Add an archived?: boolean to ListInput and filter out archived sessions unless explicitly requested, mirroring the existing guard in listGlobal. Closes anomalyco#24850 Co-authored-by: yui-soul <[email protected]>
1 parent 6ea40cb commit ca0a3aa

2 files changed

Lines changed: 170 additions & 0 deletions

File tree

‎packages/opencode/src/session/session.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ export type ListInput = {
297297
start?: number
298298
search?: string
299299
limit?: number
300+
archived?: boolean
300301
}
301302

302303
export type GlobalListInput = {
@@ -1031,6 +1032,9 @@ function listByProject(
10311032
if (input.search) {
10321033
conditions.push(like(SessionTable.title, `%${input.search}%`))
10331034
}
1035+
if (!input.archived) {
1036+
conditions.push(isNull(SessionTable.time_archived))
1037+
}
10341038

10351039
const limit = input.limit ?? 100
10361040

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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

Comments
 (0)