Multiplayer agent harness for work.
Bound transcript reads and reduce repeated request work (#1645)
Joshua France65856b286d87
diff --git a/src/api/app-helpers.ts b/src/api/app-helpers.tsindex 412447ee5..5decc942e 100644--- a/src/api/app-helpers.ts+++ b/src/api/app-helpers.ts@@ -284,10 +284,14 @@ export function createAppHelpers(deps: AppDeps, app: App) {async function sessionsForViewer(principalId: string): Promise<Session[]> {const sessions = await deps.sessions.listByParticipant(principalId);- const allowed = await Promise.all(- sessions.map((session) => managedProjectMembership(session.scopeId, principalId)),+ const allowed = new Map(+ await Promise.all(+ [...new Set(sessions.map((session) => session.scopeId))].map(+ async (scope) => [scope, await managedProjectMembership(scope, principalId)] as const,+ ),+ ),);- return sessions.filter((_session, index) => allowed[index] !== false);+ return sessions.filter((session) => allowed.get(session.scopeId) !== false);}async function sessionForViewer(sessionId: string, principalId: string): Promise<Session | null> {diff --git a/src/api/app-sessions.ts b/src/api/app-sessions.tsindex 0abe9a362..c36d870e6 100644--- a/src/api/app-sessions.ts+++ b/src/api/app-sessions.ts@@ -212,7 +212,7 @@ export function createSessionMethods(if (!session) return null;let limit = tailWindowLimit(window);const [initialRead, pinRecords] = await Promise.all([- transcripts.forRender(sessionId, { limit, beforeSeq: window?.beforeSeq }),+ transcripts.forRender(sessionId, { limit, beforeSeq: window?.beforeSeq, sinceSeq: window?.sinceSeq }),deps.sessions.listPins(sessionId),]);let read = initialRead;@@ -257,7 +257,11 @@ export function createSessionMethods(if (!session) return null;let limit = tailWindowLimit(window);const [initialRead, pinRecords] = await Promise.all([- transcripts.forViewer(sessionId, principalId, { limit, beforeSeq: window?.beforeSeq }),+ transcripts.forViewer(sessionId, principalId, {+ limit,+ beforeSeq: window?.beforeSeq,+ sinceSeq: window?.sinceSeq,+ }),deps.sessions.listPins(sessionId),]);let read = initialRead;diff --git a/src/api/routes/admin/common.ts b/src/api/routes/admin/common.tsindex 041359254..4afb5add4 100644--- a/src/api/routes/admin/common.ts+++ b/src/api/routes/admin/common.ts@@ -37,15 +37,17 @@ export async function discoverScopes(extraScopeIds: Iterable<string> = [],): Promise<Map<string, string>> {const labels = new Map<string, string>([[orgScope(deps), "org-wide"]]);- for (const s of (await deps.sessions?.distinctScopes()) ?? []) {+ const [scopes, participants, grants] = await Promise.all([+ deps.sessions?.distinctScopes() ?? [],+ deps.sessions?.distinctParticipants() ?? [],+ deps.admin?.listGrants() ?? [],+ ]);+ for (const s of scopes) {if (!labels.has(s.scopeId) || (s.channelName && !labels.get(s.scopeId))) {labels.set(s.scopeId, s.channelName ? `#${s.channelName}` : "");}}- const people = [- ...((await deps.sessions?.distinctParticipants()) ?? []),- ...((await deps.admin?.listGrants()) ?? []).map((g) => g.principalId),- ];+ const people = [...participants, ...grants.map((g) => g.principalId)];for (const principalId of people) {const personal = makeScopeId("personal", principalId);if (!labels.has(personal)) labels.set(personal, "");@@ -54,8 +56,9 @@ export async function discoverScopes(if (!labels.has(id)) labels.set(id, "");}if ([...labels.values()].some((l) => !l)) {- const membersById = new Map((await app.directoryMembers()).map((m) => [m.principalId, m.displayName]));- const channelsById = new Map((await app.directoryChannels()).map((c) => [c.channelId, c.name]));+ const [members, channels] = await Promise.all([app.directoryMembers(), app.directoryChannels()]);+ const membersById = new Map(members.map((m) => [m.principalId, m.displayName]));+ const channelsById = new Map(channels.map((c) => [c.channelId, c.name]));for (const [id, label] of labels) {if (label) continue;const { kind, ref } = parseScopeId(id);diff --git a/src/api/routes/admin/scope-config.ts b/src/api/routes/admin/scope-config.tsindex c7000b16f..c45b8baf9 100644--- a/src/api/routes/admin/scope-config.ts+++ b/src/api/routes/admin/scope-config.ts@@ -151,10 +151,13 @@ export async function listAdminScopes(ctx: ApiCtx): Promise<void> {const actor = await authorizeAdmin(ctx, scope);if (!actor) return;audit(deps, { principalId: actor.id, action: "scopes.read", resource: "scopes", scopeLabel: scope });- const crons = await app.listCrons();- const deployments = await app.listDeployments();- const skills = await app.listSkills();- const environmentRows = await app.listEnvironments();+ const [crons, deployments, skills, environmentRows, rollups] = await Promise.all([+ app.listCrons(),+ app.listDeployments(),+ app.listSkills(),+ app.listEnvironments(),+ deps.sessions?.scopeSessionRollups(scope, true) ?? [],+ ]);const environments = environmentRows.map(({ environment, attachments }) => ({id: environment.id,name: environment.name,@@ -174,16 +177,17 @@ export async function listAdminScopes(ctx: ApiCtx): Promise<void> {...environments.map((environment) => environment.id),...environments.flatMap((environment) => environment.attachedScopes),];- const labels = await discoverScopes(app, deps, owners);+ const previewIds = rollups.flatMap((r) => (r.previewSessionId ? [r.previewSessionId] : []));+ const [labels, previews] = await Promise.all([+ discoverScopes(app, deps, owners),+ deps.sessions?.lastUserMessages(previewIds) ?? new Map<string, string>(),+ ]);const countBy = (ids: string[]): Map<string, number> => {const m = new Map<string, number>();for (const id of ids) m.set(id, (m.get(id) ?? 0) + 1);return m;};- const rollups = (await deps.sessions?.scopeSessionRollups(scope, true)) ?? [];const rollupBy = new Map(rollups.map((r) => [r.scopeId, r]));- const previewIds = rollups.flatMap((r) => (r.previewSessionId ? [r.previewSessionId] : []));- const previews = (await deps.sessions?.lastUserMessages(previewIds)) ?? new Map<string, string>();const cronN = countBy(crons.map((c) => c.ownerScopeId));const deployN = countBy(deployments.map((d) => d.ownerScopeId));const skillN = countBy(skills.map((s) => s.scopeId));diff --git a/src/harness/tape-projection.ts b/src/harness/tape-projection.tsindex 5df46a5ce..1f0953ef4 100644--- a/src/harness/tape-projection.ts+++ b/src/harness/tape-projection.ts@@ -398,7 +398,7 @@ type TranscriptStore = Pick<SessionStore,"getEntries" | "visibleEntries" | "getTape" | "latestEntrySeq" | "participantWindowsOf"> &- Partial<Pick<SessionStore, "getTranscriptEntries">>;+ Partial<Pick<SessionStore, "getTranscriptEntries" | "canReadTranscriptSuffix">>;interface TranscriptRead {entries: SessionEntry[];@@ -407,11 +407,7 @@ interface TranscriptRead {export interface TranscriptSource {forRender(sessionId: string, opts?: GetEntriesOptions): Promise<TranscriptRead>;- forViewer(- sessionId: string,- principalId: string,- opts?: { limit?: number; beforeSeq?: number },- ): Promise<TranscriptRead>;+ forViewer(sessionId: string, principalId: string, opts?: GetEntriesOptions): Promise<TranscriptRead>;}interface ProjectedRead {@@ -421,7 +417,12 @@ interface ProjectedRead {}export function createTranscriptSource(sessions: TranscriptStore): TranscriptSource {- const projected = async (sessionId: string, limit?: number, beforeSeq?: number): Promise<ProjectedRead | null> => {+ const projected = async (+ sessionId: string,+ limit?: number,+ beforeSeq?: number,+ sinceSeq?: number,+ ): Promise<ProjectedRead | null> => {try {const latest = Math.min(await sessions.latestEntrySeq(sessionId),@@ -429,8 +430,12 @@ export function createTranscriptSource(sessions: TranscriptStore): TranscriptSou);if (latest < 0) return { entries: [], anchored: false, base: -1 };if (sessions.getTranscriptEntries) {- const entries = await sessions.getTranscriptEntries(sessionId, { limit, beforeSeq });- const first = limit === undefined ? 0 : Math.max(0, latest - limit + 1);+ const first = Math.min(latest + 1, Math.max(sinceSeq ?? 0, limit === undefined ? 0 : latest - limit + 1));+ const [entries, completePrefix] = await Promise.all([+ sessions.getTranscriptEntries(sessionId, { limit, beforeSeq, sinceSeq }),+ sinceSeq === undefined || first === 0 || sessions.canReadTranscriptSuffix?.(sessionId, first),+ ]);+ if (sinceSeq !== undefined && !completePrefix) return projected(sessionId, limit, beforeSeq);if (entries.length === latest - first + 1 && entries.every((entry, i) => entry.seq === first + i)) {return { entries, anchored: first > 0, base: first - 1 };}@@ -463,16 +468,19 @@ export function createTranscriptSource(sessions: TranscriptStore): TranscriptSoureturn {async forRender(sessionId, opts?): Promise<TranscriptRead> {- const read = await projected(sessionId, opts?.limit, opts?.beforeSeq);+ const read = await projected(sessionId, opts?.limit, opts?.beforeSeq, opts?.sinceSeq);if (read === null) {- const rows = await sessions.getEntries(sessionId, opts);- return { entries: rows, earlier: rows[0]?.seq ?? 0 };+ const rows = await sessions.getEntries(sessionId, { ...opts, sinceSeq: undefined });+ const entries = rows.filter((entry) => entry.seq >= (opts?.sinceSeq ?? 0));+ const earlier = rows.slice(0, rows.length - entries.length).filter((entry) => entry.type !== "soul");+ return { entries, earlier: (rows[0]?.seq ?? 0) + earlier.length };}const since = opts?.sinceSeq !== undefined ? read.entries.filter((e) => e.seq >= opts.sinceSeq!) : read.entries;const limit = opts?.beforeSeq !== undefined && !read.anchored ? undefined : opts?.limit;const entries = limit !== undefined ? since.slice(-limit) : since;const below = read.anchored ? read.base + 1 : 0;- return { entries, earlier: read.entries.length - entries.length + below };+ const earlier = read.entries.slice(0, read.entries.length - entries.length).filter((e) => e.type !== "soul");+ return { entries, earlier: earlier.length + below };},async forViewer(sessionId, principalId, opts?): Promise<TranscriptRead> {const fallback = async (): Promise<TranscriptRead> => ({@@ -494,7 +502,7 @@ export function createTranscriptSource(sessions: TranscriptStore): TranscriptSoureturn fallback();}const limit = window.validToSeq === null ? opts?.limit : undefined;- let read = await projected(sessionId, limit, opts?.beforeSeq);+ let read = await projected(sessionId, limit, opts?.beforeSeq, opts?.sinceSeq);if (read === null) return fallback();let filtered = read.entries.filter((e) => entryWithinTenure(e, window));if (opts?.beforeSeq !== undefined && !read.anchored) return { entries: filtered, earlier: 0 };@@ -503,8 +511,10 @@ export function createTranscriptSource(sessions: TranscriptStore): TranscriptSouif (read === null) return fallback();filtered = read.entries.filter((e) => entryWithinTenure(e, window));}- if (limit === undefined) return { entries: filtered, earlier: 0 };- const below = read.anchored ? Math.max(0, read.base + 1 - window.validFromSeq) : 0;+ const below = read.anchored+ ? Math.max(0, Math.min(read.base + 1, window.validToSeq ?? Infinity) - window.validFromSeq)+ : 0;+ if (limit === undefined) return { entries: filtered, earlier: below };return { entries: filtered.slice(-limit), earlier: Math.max(0, filtered.length - limit) + below };},};diff --git a/src/sessions/memory-session-store.ts b/src/sessions/memory-session-store.tsindex fc1f3d6a6..9473381c3 100644--- a/src/sessions/memory-session-store.ts+++ b/src/sessions/memory-session-store.ts@@ -316,6 +316,15 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStorereturn opts?.limit === undefined ? filtered : filtered.slice(-opts.limit);},+ async canReadTranscriptSuffix(sessionId, beforeSeq) {+ const prefix = new Map<number, string>();+ for (const row of tape.get(sessionId) ?? []) {+ const entry = transcriptEntryFromTape(row);+ if (entry && entry.seq >= 0 && entry.seq < beforeSeq) prefix.set(entry.seq, entry.type);+ }+ return prefix.size === beforeSeq && [...prefix.values()].every((type) => type !== "soul");+ },+async getContextWindow(sessionId) {return contextWindowFromEntries(entries.get(sessionId) ?? []);},diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.tsindex 102362416..b89aee20e 100644--- a/src/sessions/postgres-session-store.ts+++ b/src/sessions/postgres-session-store.ts@@ -1055,6 +1055,20 @@ export function createPostgresSessionStore(connectionString: string, opts: Storereturn rows.map(rowToEntry).reverse();},+ async canReadTranscriptSuffix(sessionId, beforeSeq) {+ const rows = await q(+ `SELECT NOT EXISTS (+ SELECT 1 FROM session_entries WHERE session_id = $1 AND seq < $2 AND type = 'soul'+ ) AND (+ SELECT COUNT(DISTINCT entry_seq) FROM session_tape+ WHERE session_id = $1 AND entry_seq >= 0 AND entry_seq < $2+ AND kind = 'annotation' AND safe_json(payload)->>'event' = 'transcript_entry'+ ) = $2 AS complete`,+ [sessionId, beforeSeq],+ );+ return rows[0]?.complete === true;+ },+async appendTape(lease, rec: NewTapeRecord): Promise<TapeRecord> {return withLease(lease, "tape append without a valid session lease", (client) =>insertTapeRow(client, lease.sessionId, rec),@@ -1694,19 +1708,24 @@ export function createPostgresSessionStore(connectionString: string, opts: Storeasync scopeSessionRollups(scope, orgWide): Promise<ScopeSessionRollup[]> {const rows = await q(- `SELECT scope_id,- COUNT(*) FILTER (WHERE NOT background) AS sessions,- COUNT(*) FILTER (WHERE background) AS background_sessions,- MAX(last_activity) AS last_activity,- COALESCE(MAX(last_activity) FILTER (WHERE NOT background), 0) AS last_conversation_activity,- (array_agg(id ORDER BY last_activity DESC, id DESC) FILTER (WHERE NOT background AND turns > 0))[1]- AS preview_session_id- FROM (SELECT s.scope_id, s.id, COALESCE(s.turns, 0) AS turns,- ${lastActivityExpr("s")} AS last_activity,- ${isBackground("s")} AS background- FROM sessions s- WHERE ($1::boolean OR s.scope_id = $2)) t- GROUP BY scope_id`,+ `SELECT r.scope_id, r.sessions, r.background_sessions, r.last_activity, r.last_conversation_activity,+ (+ SELECT s.id FROM sessions s+ WHERE s.scope_id = r.scope_id AND ${lastActivityExpr("s")} <= r.preview_activity+ AND NOT (${isBackground("s")}) AND s.turns > 0+ ORDER BY ${lastActivityExpr("s")} DESC, s.id DESC LIMIT 1+ ) AS preview_session_id+ FROM (SELECT scope_id,+ COUNT(*) FILTER (WHERE NOT background) AS sessions,+ COUNT(*) FILTER (WHERE background) AS background_sessions,+ MAX(last_activity) AS last_activity,+ COALESCE(MAX(last_activity) FILTER (WHERE NOT background), 0) AS last_conversation_activity,+ MAX(last_activity) FILTER (WHERE NOT background AND turns > 0) AS preview_activity+ FROM (SELECT s.scope_id, s.turns, ${lastActivityExpr("s")} AS last_activity,+ ${isBackground("s")} AS background+ FROM sessions s+ WHERE ($1::boolean OR s.scope_id = $2)) t+ GROUP BY scope_id) r`,[orgWide, scope],);return rows.map((r) => ({diff --git a/src/sessions/session-store.ts b/src/sessions/session-store.tsindex ec743fcbc..1152745df 100644--- a/src/sessions/session-store.ts+++ b/src/sessions/session-store.ts@@ -728,6 +728,7 @@ export interface SessionStore {append(lease: Lease, entry: NewEntry): Promise<SessionEntry>;getEntries(sessionId: string, opts?: GetEntriesOptions): Promise<SessionEntry[]>;getTranscriptEntries(sessionId: string, opts?: GetEntriesOptions): Promise<SessionEntry[]>;+ canReadTranscriptSuffix(sessionId: string, beforeSeq: number): Promise<boolean>;getContextWindow(sessionId: string): Promise<ContextWindow>;getEntry(sessionId: string, seq: number): Promise<SessionEntry | undefined>;latestEntrySeq(sessionId: string): Promise<number>;diff --git a/test/admin-scopes-directory.test.ts b/test/admin-scopes-directory.test.tsindex b604750af..ddf3ce44d 100644--- a/test/admin-scopes-directory.test.ts+++ b/test/admin-scopes-directory.test.ts@@ -27,6 +27,37 @@ function start() {const ALICE_ADMIN = { "x-admin-actor": "admin-alice@default-org" };const json = async (r: Response): Promise<any> => r.json();+test("the scope directory starts independent resource reads together", async () => {+ const s = start();+ const gate = Promise.withResolvers<void>();+ const started = new Set<string>();+ const ready = Promise.withResolvers<void>();+ const hold =+ <T>(name: string, read: () => Promise<T>) =>+ async () => {+ started.add(name);+ ready.resolve();+ await gate.promise;+ return read();+ };+ const app = s.built.app;+ app.listCrons = hold("crons", app.listCrons.bind(app));+ app.listDeployments = hold("deployments", app.listDeployments.bind(app));+ app.listSkills = hold("skills", app.listSkills.bind(app));+ app.listEnvironments = hold("environments", app.listEnvironments.bind(app));+ const rollups = s.built.sessions.scopeSessionRollups.bind(s.built.sessions);+ s.built.sessions.scopeSessionRollups = hold("rollups", () => rollups("org:default-org", true));+ const request = fetch(`${s.base}/v1/admin/scopes`, { headers: ALICE_ADMIN });+ try {+ await ready.promise;+ assert.deepEqual([...started].sort(), ["crons", "deployments", "environments", "rollups", "skills"]);+ } finally {+ gate.resolve();+ await request;+ await s.close();+ }+});+test("the admin scope directory lists every known scope with labels and counts (powers the picker)", async () => {const s = start();try {diff --git a/test/postgres-store.test.ts b/test/postgres-store.test.tsindex c84ef7089..634eea983 100644--- a/test/postgres-store.test.ts+++ b/test/postgres-store.test.ts@@ -2781,6 +2781,9 @@ test(});const before = await s.getEntries(session.id);assert.deepEqual(await s.getTranscriptEntries(session.id), before);+ assert.equal(await s.canReadTranscriptSuffix(session.id, 0), true);+ assert.equal(await s.canReadTranscriptSuffix(session.id, 4), true);+ assert.equal(await s.canReadTranscriptSuffix(session.id, 5), false);assert.deepEqual(await s.getTranscriptEntries(session.id, { limit: 2 }), before.slice(-2));assert.deepEqual(await s.getTranscriptEntries(session.id, { sinceSeq: 1, limit: 2 }), before.slice(-2));for (const beforeSeq of [0, 1, 3, 4, 99]) {@@ -2803,6 +2806,10 @@ test(assert.equal(await s.clearSecurityTaint(session.id), true);assert.equal((await s.getTape(session.id)).length, 8);assert.equal(await s.tapeCoverage(session.id), -1);+ assert.equal(await s.canReadTranscriptSuffix(session.id, 4), true);+ await s.append(lease, { type: "soul", payload: { text: "legacy instructions" }, scopeLabel: scope });+ assert.equal(await s.canReadTranscriptSuffix(session.id, 4), true);+ assert.equal(await s.canReadTranscriptSuffix(session.id, 5), false);await s.releaseLease(lease);},);@@ -2857,6 +2864,7 @@ test("pg transcript backfill is bounded, idempotent, and independent of model coconst dry = await migrateTranscriptPage(client, session.id, { afterSeq: -1, limit: 200, apply: false });assert.deepEqual(dry, { busy: false, scanned: 200, changed: 200, afterSeq: 199 });assert.deepEqual(await s.getTranscriptEntries(session.id), []);+ assert.equal(await s.canReadTranscriptSuffix(session.id, 620), false);for (let afterSeq = -1; afterSeq < 619;) {const page = await migrateTranscriptPage(client, session.id, { afterSeq, limit: 200, apply: true });assert.ok(!page.busy);diff --git a/test/projects.test.ts b/test/projects.test.tsindex be99e0403..cc185392c 100644--- a/test/projects.test.ts+++ b/test/projects.test.ts@@ -44,6 +44,44 @@ test("ProjectStore atomically maintains a managed-group roster", async () => {assert.equal(await projects.name(projectGroupRef(project.id)), "Launch Cohort");});+test("session lists check each project once per request and observe membership changes", async () => {+ const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "project-list-membership-")) }));+ try {+ const project = await built.projects.create({ name: "Shared history", ownerId: "owner" });+ await built.projects.addMember(project.id, "owner", "member");+ for (let i = 0; i < 50; i++) {+ const session = await built.sessions.getOrCreateByThread(+ `web:member:history-${i}`,+ "group",+ projectScopeId(project.id),+ );+ await built.sessions.addParticipant(session.id, "member", undefined, { includeHistory: true });+ await built.sessions.updateTitle(session.id, `History ${i}`);+ }+ const get = built.projects.get.bind(built.projects);+ const membership = built.projects.membership.bind(built.projects);+ let gets = 0;+ let memberships = 0;+ built.projects.get = async (id) => {+ gets++;+ return get(id);+ };+ built.projects.membership = async (ref, principal) => {+ memberships++;+ return membership(ref, principal);+ };+ assert.equal((await built.app.listSessions("member")).length, 50);+ assert.equal(gets, 1);+ assert.equal(memberships, 1);+ await built.projects.removeMember(project.id, "owner", "member");+ assert.deepEqual(await built.app.listSessions("member"), []);+ assert.equal(gets, 2);+ assert.equal(memberships, 2);+ } finally {+ await built.runtime.stop();+ }+});+test("ProjectStore rename is owner-only and cleans the name", async () => {let at = 100;const projects = createProjectStore(undefined, { id: () => "pr", now: () => at++ });diff --git a/test/session-view-bounds.test.ts b/test/session-view-bounds.test.tsindex dc0937e6c..ee001f3dd 100644--- a/test/session-view-bounds.test.ts+++ b/test/session-view-bounds.test.ts@@ -272,3 +272,127 @@ test("earlier pages retain tape fallback and viewer tenure boundaries", async ()await built.runtime.stop();}});++test("incremental transcript reads bound canonical storage and preserve windows and pins", async () => {+ const built = freshApp();+ try {+ const { session, narration } = await coarseForeignSession(built.sessions, 100);+ await built.app.pinConversationItem(session.threadRef, "U1", { entrySeq: narration.seq });+ const canonical = built.sessions.getTranscriptEntries.bind(built.sessions);+ const calls: Array<{ sinceSeq?: number; beforeSeq?: number } | undefined> = [];+ let loadedRows = 0;+ built.sessions.getTranscriptEntries = async (id, opts) => {+ calls.push(opts);+ const rows = await canonical(id, opts);+ loadedRows += rows.length;+ return rows;+ };+ for (const read of [+ () => built.app.getSession(session.id, { sinceSeq: 490 }),+ () => built.app.getSessionForViewer(session.id, "U1", { sinceSeq: 490 }),+ ]) {+ calls.length = 0;+ loadedRows = 0;+ const page = (await read())!;+ assert.equal(page.entries.length, 10);+ assert.equal(page.earlierEntries, 490);+ assert.equal(loadedRows, 10);+ assert.ok(calls.every((opts) => opts?.sinceSeq === 490));+ }+ await built.sessions.addParticipant(session.id, "late");+ const { lease } = await built.sessions.acquireLease(session.id);+ await built.sessions.append(lease!, {+ type: "soul",+ payload: { text: "legacy instructions" },+ scopeLabel: session.scopeId,+ });+ for (let i = 0; i < 10; i++) {+ await built.sessions.append(lease!, {+ type: "user",+ payload: { text: `new question ${i}` },+ scopeLabel: session.scopeId,+ });+ await built.sessions.append(lease!, {+ type: "assistant",+ payload: { text: `new reply ${i}` },+ scopeLabel: session.scopeId,+ });+ }+ await built.sessions.removeParticipant(session.id, "U1");+ await built.sessions.append(lease!, {+ type: "user",+ payload: { text: "after departure" },+ scopeLabel: session.scopeId,+ });+ await built.sessions.releaseLease(lease!);+ for (const viewer of [undefined, "U1", "late", "stranger"]) {+ const read = (window?: { sinceSeq?: number; beforeSeq?: number }) =>+ viewer === undefined+ ? built.app.getSession(session.id, window)+ : built.app.getSessionForViewer(session.id, viewer, window);+ const full = await read();+ for (const window of [+ { sinceSeq: 490 },+ { sinceSeq: 500, beforeSeq: 515 },+ { sinceSeq: 515, beforeSeq: 500 },+ { sinceSeq: 900 },+ ]) {+ calls.length = 0;+ const page = await read(window);+ if (!full) {+ assert.equal(page, null);+ continue;+ }+ const expected = windowedTranscript(full.entries as SessionEntry[], window);+ assert.deepEqual(page!.entries, expected.entries);+ assert.equal(page!.earlierEntries ?? 0, expected.earlier);+ assert.deepEqual(page!.pins, full.pins);+ assert.ok(calls.length > 0);+ assert.ok(calls.some((opts) => opts?.sinceSeq === window.sinceSeq));+ }+ }+ withoutCanonicalTranscript(built.sessions);+ for (const viewer of [undefined, "U1", "late"]) {+ const read = (window?: { sinceSeq?: number; beforeSeq?: number }) =>+ viewer === undefined+ ? built.app.getSession(session.id, window)+ : built.app.getSessionForViewer(session.id, viewer, window);+ const full = (await read())!;+ for (const window of [+ { sinceSeq: 490, beforeSeq: 516 },+ { sinceSeq: 515, beforeSeq: 520 },+ ]) {+ const expected = windowedTranscript(full.entries as SessionEntry[], window);+ const page = (await read(window))!;+ assert.deepEqual(page.entries, expected.entries);+ assert.equal(page.earlierEntries ?? 0, expected.earlier);+ }+ }+ } finally {+ await built.runtime.stop();+ }+});++test("incremental reads retain coarse tape fallback when a canonical prefix is missing", async () => {+ const built = freshApp();+ try {+ const { session } = await coarseForeignSession(built.sessions, 100);+ const canonical = built.sessions.getTranscriptEntries.bind(built.sessions);+ built.sessions.getTranscriptEntries = async (id, opts) =>+ (await canonical(id, opts)).filter((entry) => entry.seq >= 450);+ built.sessions.canReadTranscriptSuffix = async () => false;+ const full = (await built.app.getSession(session.id))!;+ assert.equal(full.entries.length, 200);+ const window = { sinceSeq: 490 };+ const expected = windowedTranscript(full.entries as SessionEntry[], window);+ for (const page of [+ await built.app.getSession(session.id, window),+ await built.app.getSessionForViewer(session.id, "U1", window),+ ]) {+ assert.deepEqual(page!.entries, expected.entries);+ assert.equal(page!.earlierEntries ?? 0, expected.earlier);+ }+ } finally {+ await built.runtime.stop();+ }+});