Repository navigation
fix(acp-registry): native sessions past 256 on one agent page stay listed #48338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
48c502b
bfa9f28
957736d
2f43858
66077c0
966670d
86f861f
aa74c40
3166fcd
7156f64
2d8e0b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; | ||
| import { assert, it } from "@effect/vitest"; | ||
| import type { AcpRegistryListSessionsResult } from "@t3tools/contracts"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as TestClock from "effect/testing/TestClock"; | ||
|
|
||
| import { makeAcpRegistrySessionPages } from "./sessionPages.ts"; | ||
|
|
||
| const cwd = "/workspace/demo"; | ||
| const range = (first: number, last: number) => | ||
| Array.from({ length: last - first + 1 }, (_, index) => `native-${first + index}`); | ||
|
|
||
| const agentPage = ( | ||
| sessionIds: ReadonlyArray<string>, | ||
| nextCursor: string | null, | ||
| ): AcpRegistryListSessionsResult => ({ | ||
| sessions: sessionIds.map((sessionId) => ({ | ||
| sessionId, | ||
| cwd, | ||
| additionalDirectories: [], | ||
| title: null, | ||
| updatedAt: null, | ||
| importedThreadId: null, | ||
| })), | ||
| ...(nextCursor !== null && nextCursor.length > 2_048 | ||
| ? { nextCursor: null, longNextCursor: nextCursor } | ||
| : { nextCursor }), | ||
| canLoad: true, | ||
| canResume: false, | ||
| canDelete: true, | ||
| }); | ||
|
|
||
| const makeAgent = (pages: Record<string, AcpRegistryListSessionsResult>) => { | ||
| const requested: Array<string | undefined> = []; | ||
| const fetch = (cursor: string | undefined) => { | ||
| requested.push(cursor); | ||
| const page = pages[cursor ?? ""]; | ||
| return page === undefined ? Effect.die(`unexpected cursor ${cursor}`) : Effect.succeed(page); | ||
| }; | ||
| return { requested, fetch }; | ||
| }; | ||
|
|
||
| const listAll = Effect.fn(function* ( | ||
| pages: Effect.Success<typeof makeAcpRegistrySessionPages>, | ||
| fetch: ReturnType<typeof makeAgent>["fetch"], | ||
| ) { | ||
| const listed: Array<ReadonlyArray<string>> = []; | ||
| let cursor: string | undefined; | ||
| do { | ||
| const page = yield* pages.list({ cwd, ...(cursor === undefined ? {} : { cursor }) }, fetch); | ||
| listed.push(page.sessions.map(({ sessionId }) => sessionId)); | ||
| cursor = page.nextCursor ?? page.longNextCursor ?? undefined; | ||
| } while (cursor !== undefined && listed.length < 10); | ||
| assert.isUndefined(cursor); | ||
| return listed; | ||
| }); | ||
|
|
||
| const longCursor = "c".repeat(2_048); | ||
| const longOlder = "o".repeat(4_096); | ||
|
|
||
| it.effect("passes agent pages and their cursors through exactly, even ones shaped like ours", () => | ||
| Effect.gen(function* () { | ||
| const pages = yield* makeAcpRegistrySessionPages; | ||
| const agent = makeAgent({ | ||
| "": agentPage(range(1, 257), "t3-sessions-native-page-2"), | ||
| "t3-sessions-native-page-2": agentPage(["native-258"], null), | ||
| }); | ||
| assert.deepStrictEqual(yield* listAll(pages, agent.fetch), [ | ||
| range(1, 256), | ||
| ["native-257"], | ||
| ["native-258"], | ||
| ]); | ||
| assert.deepStrictEqual(agent.requested, [undefined, "t3-sessions-native-page-2"]); | ||
| }).pipe(Effect.provide(NodeCrypto.layer)), | ||
| ); | ||
|
|
||
| it.effect.each([ | ||
| { size: 257, expected: [["native-0"], range(1, 256), ["native-257"], ["native-258"]] }, | ||
| { | ||
| size: 513, | ||
| expected: [["native-0"], range(1, 256), range(257, 512), ["native-513"], ["native-514"]], | ||
| }, | ||
| ])( | ||
| "splits an agent page of $size sessions reached through a maximum-length cursor", | ||
| ({ size, expected }) => | ||
| Effect.gen(function* () { | ||
| const pages = yield* makeAcpRegistrySessionPages; | ||
| const agent = makeAgent({ | ||
| "": agentPage(["native-0"], longCursor), | ||
| [longCursor]: agentPage(range(1, size), longOlder), | ||
| [longOlder]: agentPage([`native-${size + 1}`], null), | ||
| }); | ||
| assert.deepStrictEqual(yield* listAll(pages, agent.fetch), expected); | ||
| assert.deepStrictEqual(agent.requested, [undefined, longCursor, longOlder]); | ||
| }).pipe(Effect.provide(NodeCrypto.layer)), | ||
| ); | ||
|
|
||
| it.effect("serves the rest of a large page from the response the user already saw", () => | ||
| Effect.gen(function* () { | ||
| const pages = yield* makeAcpRegistrySessionPages; | ||
| const pagesByCursor: Record<string, AcpRegistryListSessionsResult> = { | ||
| "": agentPage(range(1, 257), null), | ||
| }; | ||
| const agent = makeAgent(pagesByCursor); | ||
| const first = yield* pages.list({ cwd }, agent.fetch); | ||
| assert.deepStrictEqual( | ||
| first.sessions.map(({ sessionId }) => sessionId), | ||
| range(1, 256), | ||
| ); | ||
| pagesByCursor[""] = agentPage(["native-new", ...range(2, 257)], null); | ||
| yield* pages.forgetSession("native-unrelated"); | ||
| const rest = yield* pages.list({ cwd, cursor: first.nextCursor ?? "" }, agent.fetch); | ||
| assert.deepStrictEqual( | ||
| rest.sessions.map(({ sessionId }) => sessionId), | ||
| ["native-257"], | ||
| ); | ||
| assert.isNull(rest.nextCursor); | ||
| assert.deepStrictEqual(agent.requested, [undefined]); | ||
| }).pipe(Effect.provide(NodeCrypto.layer)), | ||
| ); | ||
|
|
||
| it.effect("drops a deleted session from the rest of a large page", () => | ||
| Effect.gen(function* () { | ||
| const pages = yield* makeAcpRegistrySessionPages; | ||
| const agent = makeAgent({ "": agentPage(range(1, 258), null) }); | ||
| const first = yield* pages.list({ cwd }, agent.fetch); | ||
| yield* pages.forgetSession("native-257"); | ||
| const rest = yield* pages.list({ cwd, cursor: first.nextCursor ?? "" }, agent.fetch); | ||
| assert.deepStrictEqual( | ||
| rest.sessions.map(({ sessionId }) => sessionId), | ||
| ["native-258"], | ||
| ); | ||
| }).pipe(Effect.provide(NodeCrypto.layer)), | ||
| ); | ||
|
|
||
| it.effect("asks to list again when the rest of a large page expired or names another project", () => | ||
| Effect.gen(function* () { | ||
| const pages = yield* makeAcpRegistrySessionPages; | ||
| const agent = makeAgent({ "": agentPage(range(1, 257), null) }); | ||
| const first = yield* pages.list({ cwd }, agent.fetch); | ||
| const cursor = first.nextCursor ?? ""; | ||
| const otherProject = yield* pages | ||
| .list({ cwd: "/workspace/other", cursor }, agent.fetch) | ||
| .pipe(Effect.flip); | ||
| assert.strictEqual(otherProject.message, "The ACP session list expired. List sessions again."); | ||
| yield* TestClock.adjust("11 minutes"); | ||
| const expired = yield* pages.list({ cwd, cursor }, agent.fetch).pipe(Effect.flip); | ||
| assert.strictEqual(expired.message, "The ACP session list expired. List sessions again."); | ||
| assert.deepStrictEqual(agent.requested, [undefined]); | ||
| }).pipe(Effect.provide(NodeCrypto.layer)), | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { AcpRegistryOperationError, type AcpRegistryListSessionsResult } from "@t3tools/contracts"; | ||
| import * as Clock from "effect/Clock"; | ||
| import * as SecureRandom from "effect/crypto/SecureRandom"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Ref from "effect/Ref"; | ||
|
|
||
| const MAX_LISTED_SESSIONS = 256; | ||
| const MAX_CONTINUATIONS = 16; | ||
| const CONTINUATION_TTL_MS = 10 * 60_000; | ||
| const CONTINUATION_PREFIX = "t3-sessions-"; | ||
|
|
||
| interface Continuation { | ||
| readonly cwd: string; | ||
| readonly listed: AcpRegistryListSessionsResult; | ||
| readonly expiresAt: number; | ||
| } | ||
|
|
||
| /** | ||
| * Agents may return more sessions in one page than the list result carries. The rest is | ||
| * kept from that same response behind a short-lived cursor, so a list that changes between | ||
| * pages can neither repeat nor hide a session. Agent cursors pass through untouched. | ||
| */ | ||
| export const makeAcpRegistrySessionPages = Effect.gen(function* () { | ||
| const random = yield* SecureRandom.SecureRandom; | ||
| const continuations = yield* Ref.make<ReadonlyMap<string, Continuation>>(new Map()); | ||
|
|
||
| const list = Effect.fn("AcpRegistrySessionPages.list")(function* <E, R>( | ||
| input: { readonly cwd: string; readonly cursor?: string }, | ||
| fetch: (cursor: string | undefined) => Effect.Effect<AcpRegistryListSessionsResult, E, R>, | ||
| ) { | ||
| const now = yield* Clock.currentTimeMillis; | ||
| // Only membership marks a continuation: agent cursors are opaque and may look like ours. | ||
| const stored = | ||
| input.cursor === undefined ? undefined : (yield* Ref.get(continuations)).get(input.cursor); | ||
| if (stored !== undefined && (stored.cwd !== input.cwd || stored.expiresAt <= now)) { | ||
| return yield* new AcpRegistryOperationError({ | ||
| reason: "probe_failed", | ||
| message: "The ACP session list expired. List sessions again.", | ||
| }); | ||
| } | ||
| const listed = stored?.listed ?? (yield* fetch(input.cursor)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject local cursors whose continuation is no longer available. If another oversized listing prunes an expired token, this fallback sends that token to the agent. Eviction after 16 newer continuations has the same result. The agent never issued the token, so it can reject the request or return its first page again. The supplied scenario agent takes the latter path. Retain enough local-cursor provenance to return the list-again error for unavailable continuations. Keep genuine agent cursors unchanged.
|
||
| if (listed.sessions.length <= MAX_LISTED_SESSIONS) return listed; | ||
|
|
||
| const token = `${CONTINUATION_PREFIX}${yield* random.uuidV4()}`; | ||
| const continuation: Continuation = { | ||
| cwd: input.cwd, | ||
| listed: { ...listed, sessions: listed.sessions.slice(MAX_LISTED_SESSIONS) }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium A session deleted while 🤖 Copy this AI Prompt to have your agent fix this:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleted sessions can reappear through Load more after a concurrent list. A list request that is waiting on the agent can store overflow containing a session deleted while that request was pending. The overflow is built from the agent response at Nothing in the driver serializes these calls. A session deleted during a pending list must not be returned by Load more. Keep deletions that finish during the fetch from reaching stored overflow. Either record those deletions and drop them before the overflow is written, or serialize the overflow write with
|
||
| expiresAt: now + CONTINUATION_TTL_MS, | ||
| }; | ||
| yield* Ref.update(continuations, (current) => { | ||
| const live = [...current].filter(([, entry]) => entry.expiresAt > now); | ||
| return new Map([...live.slice(-(MAX_CONTINUATIONS - 1)), [token, continuation]]); | ||
| }); | ||
| const { longNextCursor: _agentCursor, ...head } = listed; | ||
| return { ...head, sessions: listed.sessions.slice(0, MAX_LISTED_SESSIONS), nextCursor: token }; | ||
| }); | ||
|
|
||
| const forgetSession = (sessionId: string) => | ||
| Ref.update( | ||
| continuations, | ||
| (current) => | ||
| new Map( | ||
| [...current].map(([token, entry]) => [ | ||
| token, | ||
| { | ||
| ...entry, | ||
| listed: { | ||
| ...entry.listed, | ||
| sessions: entry.listed.sessions.filter( | ||
| (session) => session.sessionId !== sessionId, | ||
| ), | ||
| }, | ||
| }, | ||
| ]), | ||
| ), | ||
| ); | ||
|
|
||
| return { list, forgetSession }; | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overflow sessions from before sign-out stay listable for up to 10 minutes.
A session-list cursor stored before sign-out keeps working after sign-out. Load more for that cursor returns the earlier listing from the same driver, without contacting the agent. Its session titles and IDs can therefore appear for the previous account. The entry expires after 10 minutes, and the instance keeps at most 16 entries.
Clear the stored overflow when the account is signed out or the auth state is invalidated.
Suggested fix