{"version":3,"file":"session-repo.d.ts","sourceRoot":"","sources":["../../../../../src/harness/session/testing/conformance/session-repo.ts"],"names":[],"mappings":"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","sourcesContent":["import { deepStrictEqual, rejects, strictEqual } from \"node:assert/strict\";\nimport type { AssistantMessage, StopReason } from \"@earendil-works/pi-ai\";\nimport { BACKGROUND_CONTEXT } from \"../../../context.ts\";\nimport { insertEntry, insertUsage } from \"../../commit.ts\";\nimport type {\n\tJsonValue,\n\tLaneConfiguration,\n\tLaneState,\n\tSession,\n\tSessionMetadata,\n\tSessionRepo,\n\tUsageRow,\n} from \"../../types.ts\";\nimport {\n\tappendList,\n\tbranchTip,\n\tentryLabel,\n\tlaneConfig,\n\tlaneState,\n\tlist,\n\toperationMeta,\n\toperationPreparation,\n\toperationResult,\n\toperationState,\n\toperationToolArgs,\n\tpendingEntry,\n\tsessionName,\n\tsetValue,\n\tvalue,\n} from \"../../values.ts\";\nimport type { ConformanceCase } from \"../types.ts\";\n\nconst ROOT_ID = \"00000000-0000-7000-8000-000000000001\";\nconst CHILD_ID = \"00000000-0000-7000-8000-000000000002\";\nconst SIBLING_ID = \"00000000-0000-7000-8000-000000000003\";\nconst USAGE_ID = \"00000000-0000-7000-8000-000000000004\";\nconst OPERATION_ID = \"00000000-0000-7000-8000-000000000005\";\nconst PENDING_ID = \"00000000-0000-7000-8000-000000000006\";\nconst UNKNOWN_ID = \"00000000-0000-7000-8000-000000000007\";\nconst idleLaneState = {\n\tcurrentOperationId: null,\n\tlastOperationId: null,\n\tinbox: [],\n} satisfies LaneState;\nconst applicationValue = value<JsonValue>(\"test.application.value\");\nconst applicationList = list<JsonValue>(\"test.application.list\");\nconst configuration = {\n\tmodel: { provider: \"provider\", modelId: \"model\" },\n\tthinkingLevel: \"off\",\n\tactiveToolNames: [\"read\"],\n} satisfies LaneConfiguration;\n\nasync function getBranchTip(session: Session, name = \"main\"): Promise<string | null | undefined> {\n\treturn (await session.branch(name, BACKGROUND_CONTEXT))?.getTipId(BACKGROUND_CONTEXT);\n}\n\nfunction assistantMessage(stopReason: StopReason): AssistantMessage {\n\treturn {\n\t\trole: \"assistant\",\n\t\tcontent:\n\t\t\tstopReason === \"toolUse\"\n\t\t\t\t? [{ type: \"toolCall\", id: \"call\", name: \"read\", arguments: {} }]\n\t\t\t\t: [{ type: \"text\", text: stopReason }],\n\t\tapi: \"anthropic-messages\",\n\t\tprovider: \"anthropic\",\n\t\tmodel: \"claude-sonnet-4-5\",\n\t\tusage: {\n\t\t\tinput: 0,\n\t\t\toutput: 0,\n\t\t\tcacheRead: 0,\n\t\t\tcacheWrite: 0,\n\t\t\ttotalTokens: 0,\n\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t},\n\t\tstopReason,\n\t\t...(stopReason === \"deferred\"\n\t\t\t? {\n\t\t\t\t\tdeferred: {\n\t\t\t\t\t\tprovider: \"anthropic\",\n\t\t\t\t\t\tmodelId: \"claude-sonnet-4-5\",\n\t\t\t\t\t\tapi: \"anthropic-messages\",\n\t\t\t\t\t\tid: \"job\",\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t: {}),\n\t\ttimestamp: 1,\n\t};\n}\n\nfunction usageRow(): Omit<UsageRow, \"seq\"> {\n\treturn {\n\t\tid: USAGE_ID,\n\t\tadjustment: true,\n\t\tusage: {\n\t\t\tinput: 1,\n\t\t\toutput: 2,\n\t\t\tcacheRead: 0,\n\t\t\tcacheWrite: 0,\n\t\t\ttotalTokens: 3,\n\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t},\n\t};\n}\n\ninterface RepoCaseContext<TRepo> {\n\trepo: TRepo;\n\tclose?: () => void | Promise<void>;\n}\n\nfunction prepareRepoCaseFactory<TRepo>(\n\tfactory: () => Promise<TRepo>,\n\tonClose?: () => void | Promise<void>,\n): () => Promise<RepoCaseContext<TRepo>> {\n\treturn async () => ({\n\t\trepo: await factory(),\n\t\t...(onClose === undefined ? {} : { close: onClose }),\n\t});\n}\n\nfunction createCase<TRepo>(\n\tfactory: () => Promise<RepoCaseContext<TRepo>>,\n\tgroup: string,\n\tname: string,\n\ttest: (context: { repo: TRepo }) => Promise<void>,\n): ConformanceCase {\n\treturn {\n\t\tgroup,\n\t\tname,\n\t\tasync run() {\n\t\t\tconst context = await factory();\n\t\t\ttry {\n\t\t\t\tawait test(context);\n\t\t\t} finally {\n\t\t\t\tawait context.close?.();\n\t\t\t}\n\t\t},\n\t};\n}\n\n/** Creates lifecycle cases for repositories that support creation, discovery, open, and deletion. */\nexport function createSessionRepoLifecycleConformance<TMetadata extends SessionMetadata>(\n\t// Require only the methods this group exercises so partial backends can run it independently.\n\tbackendFactory: () => Promise<Pick<SessionRepo<TMetadata>, \"create\" | \"open\" | \"list\" | \"delete\">>,\n\tonClose?: () => void | Promise<void>,\n): readonly ConformanceCase[] {\n\tconst factory = prepareRepoCaseFactory(backendFactory, onClose);\n\treturn [\n\t\tcreateCase(\n\t\t\tfactory,\n\t\t\t\"lifecycle\",\n\t\t\t\"creates a session with no implicit branch and rejects duplicate ids\",\n\t\t\tasync ({ repo }) => {\n\t\t\t\tconst session = await repo.create({ id: \"session\" }, BACKGROUND_CONTEXT);\n\n\t\t\t\tstrictEqual(session.metadata.id, \"session\");\n\t\t\t\tstrictEqual(Number.isSafeInteger(session.metadata.createdAt), true);\n\t\t\t\tstrictEqual(session.metadata.storageVersion, 1);\n\t\t\t\tstrictEqual(await session.branch(\"main\", BACKGROUND_CONTEXT), undefined);\n\t\t\t\tstrictEqual(await session.getValue(laneState(\"main\"), BACKGROUND_CONTEXT), undefined);\n\t\t\t\tstrictEqual(await session.getValue(laneConfig(\"main\"), BACKGROUND_CONTEXT), undefined);\n\t\t\t\tawait rejects(repo.create({ id: \"session\" }, BACKGROUND_CONTEXT));\n\t\t\t\tawait session.close(BACKGROUND_CONTEXT);\n\t\t\t},\n\t\t),\n\t\tcreateCase(\n\t\t\tfactory,\n\t\t\t\"lifecycle\",\n\t\t\t\"close drains an acquired scope and rejects a queued mutation callback\",\n\t\t\tasync ({ repo }) => {\n\t\t\t\tconst session = await repo.create({ id: \"session\" }, BACKGROUND_CONTEXT);\n\t\t\t\tconst active = await session.beginMutation(BACKGROUND_CONTEXT);\n\t\t\t\tlet queuedStarted = false;\n\t\t\t\tconst queued = session.mutate(() => {\n\t\t\t\t\tqueuedStarted = true;\n\t\t\t\t}, BACKGROUND_CONTEXT);\n\t\t\t\tvoid queued.catch(() => {});\n\t\t\t\tconst closing = session.close(BACKGROUND_CONTEXT);\n\n\t\t\t\tawait active.end(BACKGROUND_CONTEXT);\n\t\t\t\tawait rejects(queued);\n\t\t\t\tawait closing;\n\t\t\t\tstrictEqual(queuedStarted, false);\n\t\t\t},\n\t\t),\n\t\tcreateCase(\n\t\t\tfactory,\n\t\t\t\"lifecycle\",\n\t\t\t\"lists metadata and preserves state across close and reopen\",\n\t\t\tasync ({ repo }) => {\n\t\t\t\tconst first = await repo.create({ id: \"first\" }, BACKGROUND_CONTEXT);\n\t\t\t\tawait first.setName(\"preserved\", BACKGROUND_CONTEXT);\n\t\t\t\tconst second = await repo.create({ id: \"second\", parentSessionId: \"parent\" }, BACKGROUND_CONTEXT);\n\t\t\t\tawait second.close(BACKGROUND_CONTEXT);\n\n\t\t\t\tconst listed = await repo.list(undefined, BACKGROUND_CONTEXT);\n\t\t\t\tdeepStrictEqual(\n\t\t\t\t\tlisted\n\t\t\t\t\t\t.map(({ id, parentSessionId }) => ({ id, parentSessionId }))\n\t\t\t\t\t\t.sort((left, right) => left.id.localeCompare(right.id)),\n\t\t\t\t\t[\n\t\t\t\t\t\t{ id: \"first\", parentSessionId: undefined },\n\t\t\t\t\t\t{ id: \"second\", parentSessionId: \"parent\" },\n\t\t\t\t\t],\n\t\t\t\t);\n\t\t\t\tawait first.close(BACKGROUND_CONTEXT);\n\t\t\t\tawait rejects(first.getName(BACKGROUND_CONTEXT));\n\t\t\t\tconst reopened = await repo.open(first.metadata, BACKGROUND_CONTEXT);\n\t\t\t\tstrictEqual(reopened === first, false);\n\t\t\t\tstrictEqual(await reopened.getName(BACKGROUND_CONTEXT), \"preserved\");\n\t\t\t\tawait reopened.close(BACKGROUND_CONTEXT);\n\t\t\t},\n\t\t),\n\t\tcreateCase(factory, \"lifecycle\", \"deletes closed sessions without affecting other sessions\", async ({ repo }) => {\n\t\t\tconst removed = await repo.create({ id: \"removed\" }, BACKGROUND_CONTEXT);\n\t\t\tconst retained = await repo.create({ id: \"retained\" }, BACKGROUND_CONTEXT);\n\t\t\tawait Promise.all([removed.close(BACKGROUND_CONTEXT), retained.close(BACKGROUND_CONTEXT)]);\n\n\t\t\tawait repo.delete(removed.metadata, BACKGROUND_CONTEXT);\n\t\t\tdeepStrictEqual(\n\t\t\t\t(await repo.list(undefined, BACKGROUND_CONTEXT)).map(({ id }) => id),\n\t\t\t\t[\"retained\"],\n\t\t\t);\n\t\t\tawait rejects(repo.open(removed.metadata, BACKGROUND_CONTEXT));\n\t\t\tawait rejects(repo.delete(removed.metadata, BACKGROUND_CONTEXT));\n\t\t}),\n\t];\n}\n\n/** Creates exclusive-open cases for repositories that own active session handles. */\nexport function createSessionRepoOwnershipConformance<TMetadata extends SessionMetadata>(\n\tbackendFactory: () => Promise<Pick<SessionRepo<TMetadata>, \"create\" | \"open\">>,\n\tonClose?: () => void | Promise<void>,\n): readonly ConformanceCase[] {\n\tconst factory = prepareRepoCaseFactory(backendFactory, onClose);\n\treturn [\n\t\tcreateCase(factory, \"ownership\", \"rejects opening an already-open session\", async ({ repo }) => {\n\t\t\tconst session = await repo.create({ id: \"session\" }, BACKGROUND_CONTEXT);\n\t\t\tawait rejects(repo.open(session.metadata, BACKGROUND_CONTEXT));\n\t\t\tawait session.close(BACKGROUND_CONTEXT);\n\n\t\t\tconst reopened = await repo.open(session.metadata, BACKGROUND_CONTEXT);\n\t\t\tawait rejects(repo.open(session.metadata, BACKGROUND_CONTEXT));\n\t\t\tawait reopened.close(BACKGROUND_CONTEXT);\n\t\t}),\n\t];\n}\n\n/** Creates message cases for repositories that support session creation. */\nexport function createSessionRepoMessageConformance<TMetadata extends SessionMetadata>(\n\tbackendFactory: () => Promise<Pick<SessionRepo<TMetadata>, \"create\">>,\n\tonClose?: () => void | Promise<void>,\n): readonly ConformanceCase[] {\n\tconst factory = prepareRepoCaseFactory(backendFactory, onClose);\n\treturn [\n\t\tcreateCase(\n\t\t\tfactory,\n\t\t\t\"messages\",\n\t\t\t\"rejects pending assistant messages without changing the tree\",\n\t\t\tasync ({ repo }) => {\n\t\t\t\tconst session = await repo.create({ id: \"session\" }, BACKGROUND_CONTEXT);\n\t\t\t\tconst branch = await session.createBranch(\"main\", null, BACKGROUND_CONTEXT);\n\n\t\t\t\tawait rejects(branch.appendMessage(assistantMessage(\"pending\"), BACKGROUND_CONTEXT));\n\n\t\t\t\tstrictEqual(await getBranchTip(session), null);\n\t\t\t\tdeepStrictEqual(await session.findEntries(undefined, BACKGROUND_CONTEXT), []);\n\t\t\t\tawait session.close(BACKGROUND_CONTEXT);\n\t\t\t},\n\t\t),\n\t\tcreateCase(factory, \"messages\", \"preserves every settled assistant stop reason\", async ({ repo }) => {\n\t\t\tconst session = await repo.create({ id: \"session\" }, BACKGROUND_CONTEXT);\n\t\t\tconst messagesByStopReason = {\n\t\t\t\tstop: assistantMessage(\"stop\"),\n\t\t\t\tlength: assistantMessage(\"length\"),\n\t\t\t\ttoolUse: assistantMessage(\"toolUse\"),\n\t\t\t\terror: assistantMessage(\"error\"),\n\t\t\t\taborted: assistantMessage(\"aborted\"),\n\t\t\t\tdeferred: assistantMessage(\"deferred\"),\n\t\t\t} satisfies Record<Exclude<StopReason, \"pending\">, AssistantMessage>;\n\t\t\tconst branch = await session.createBranch(\"main\", null, BACKGROUND_CONTEXT);\n\t\t\tconst messages = Object.values(messagesByStopReason);\n\t\t\tconst ids: string[] = [];\n\n\t\t\tfor (const message of messages) ids.push(await branch.appendMessage(message, BACKGROUND_CONTEXT));\n\n\t\t\tconst entries = await session.findEntries({ order: \"asc\", type: \"message\" }, BACKGROUND_CONTEXT);\n\t\t\tdeepStrictEqual(\n\t\t\t\tentries.map((entry) => entry.id),\n\t\t\t\tids,\n\t\t\t);\n\t\t\tfor (const [index, entry] of entries.entries()) {\n\t\t\t\tif (entry.type !== \"message\") throw new Error(\"Expected message entry\");\n\t\t\t\tdeepStrictEqual(entry.message, messages[index]);\n\t\t\t}\n\t\t\tstrictEqual(await getBranchTip(session), ids.at(-1));\n\t\t\tawait session.close(BACKGROUND_CONTEXT);\n\t\t}),\n\t];\n}\n\n/** Creates fork-content cases that do not require concurrent repository coordination. */\nexport function createSessionRepoForkBehaviorConformance<TMetadata extends SessionMetadata>(\n\tbackendFactory: () => Promise<Pick<SessionRepo<TMetadata>, \"create\" | \"list\" | \"fork\">>,\n\tonClose?: () => void | Promise<void>,\n): readonly ConformanceCase[] {\n\tconst factory = prepareRepoCaseFactory(backendFactory, onClose);\n\treturn [\n\t\tcreateCase(factory, \"forks\", \"tree-forks a fresh session before first attachment\", async ({ repo }) => {\n\t\t\tconst source = await repo.create({ id: \"source\" }, BACKGROUND_CONTEXT);\n\t\t\tconst fork = await repo.fork(source.metadata, { id: \"fork\", scope: \"tree\" }, BACKGROUND_CONTEXT);\n\n\t\t\tstrictEqual(fork.metadata.id, \"fork\");\n\t\t\tstrictEqual(fork.metadata.parentSessionId, \"source\");\n\t\t\tstrictEqual(await fork.branch(\"main\", BACKGROUND_CONTEXT), undefined);\n\t\t\tstrictEqual(await fork.getValue(laneConfig(\"main\"), BACKGROUND_CONTEXT), undefined);\n\t\t\tstrictEqual(await fork.getValue(laneState(\"main\"), BACKGROUND_CONTEXT), undefined);\n\t\t\tdeepStrictEqual(await fork.findEntries(undefined, BACKGROUND_CONTEXT), []);\n\t\t\tdeepStrictEqual(await fork.getStats(BACKGROUND_CONTEXT), {\n\t\t\t\tmessageCount: 0,\n\t\t\t\tusage: {\n\t\t\t\t\tinput: 0,\n\t\t\t\t\toutput: 0,\n\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\ttotalTokens: 0,\n\t\t\t\t\tcost: {\n\t\t\t\t\t\tinput: 0,\n\t\t\t\t\t\toutput: 0,\n\t\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\ttotal: 0,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t});\n\t\t\tawait Promise.all([source.close(BACKGROUND_CONTEXT), fork.close(BACKGROUND_CONTEXT)]);\n\t\t}),\n\t\tcreateCase(factory, \"forks\", \"rejects a data-only branch and releases its destination id\", async ({ repo }) => {\n\t\t\tconst source = await repo.create({ id: \"source\" }, BACKGROUND_CONTEXT);\n\t\t\tawait source.createBranch(\"data\", null, BACKGROUND_CONTEXT);\n\n\t\t\tawait rejects(\n\t\t\t\trepo.fork(source.metadata, { id: \"destination\", scope: \"branch\", branch: \"data\" }, BACKGROUND_CONTEXT),\n\t\t\t);\n\t\t\tdeepStrictEqual(\n\t\t\t\t(await repo.list(undefined, BACKGROUND_CONTEXT)).map(({ id }) => id),\n\t\t\t\t[\"source\"],\n\t\t\t);\n\n\t\t\tconst destination = await repo.create({ id: \"destination\" }, BACKGROUND_CONTEXT);\n\t\t\tawait Promise.all([source.close(BACKGROUND_CONTEXT), destination.close(BACKGROUND_CONTEXT)]);\n\t\t}),\n\t\tcreateCase(\n\t\t\tfactory,\n\t\t\t\"forks\",\n\t\t\t\"forks one named configured branch with scoped values and a zero ledger\",\n\t\t\tasync ({ repo }) => {\n\t\t\t\tconst source = await repo.create({ id: \"source\" }, BACKGROUND_CONTEXT);\n\t\t\t\tawait source.mutate(\n\t\t\t\t\t(mutator) =>\n\t\t\t\t\t\tmutator.commit(\n\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\tinsertEntry({\n\t\t\t\t\t\t\t\t\tid: ROOT_ID,\n\t\t\t\t\t\t\t\t\tparentId: null,\n\t\t\t\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\t\t\t\tcustomType: \"root\",\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tinsertEntry({\n\t\t\t\t\t\t\t\t\tid: CHILD_ID,\n\t\t\t\t\t\t\t\t\tparentId: ROOT_ID,\n\t\t\t\t\t\t\t\t\ttype: \"message\",\n\t\t\t\t\t\t\t\t\tmessage: { role: \"user\", content: \"child\", timestamp: 1 },\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tinsertEntry({\n\t\t\t\t\t\t\t\t\tid: SIBLING_ID,\n\t\t\t\t\t\t\t\t\tparentId: ROOT_ID,\n\t\t\t\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\t\t\t\tcustomType: \"sibling\",\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tsetValue(branchTip(\"main\"), SIBLING_ID),\n\t\t\t\t\t\t\t\tsetValue(branchTip(\"review\"), CHILD_ID),\n\t\t\t\t\t\t\t\tsetValue(laneConfig(\"review\"), configuration),\n\t\t\t\t\t\t\t\tsetValue(laneState(\"review\"), {\n\t\t\t\t\t\t\t\t\tcurrentOperationId: OPERATION_ID,\n\t\t\t\t\t\t\t\t\tlastOperationId: \"previous\",\n\t\t\t\t\t\t\t\t\tinbox: [{ entryId: PENDING_ID, kind: \"write\" }],\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tsetValue(operationResult(\"previous\"), {\n\t\t\t\t\t\t\t\t\toperationId: \"previous\",\n\t\t\t\t\t\t\t\t\tkind: \"navigation\",\n\t\t\t\t\t\t\t\t\tstatus: \"completed\",\n\t\t\t\t\t\t\t\t\tfromTipId: ROOT_ID,\n\t\t\t\t\t\t\t\t\ttipId: CHILD_ID,\n\t\t\t\t\t\t\t\t\tstartedAt: 1,\n\t\t\t\t\t\t\t\t\tendedAt: 2,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tsetValue(sessionName, \"source name\"),\n\t\t\t\t\t\t\t\tsetValue(applicationValue, { copied: false }),\n\t\t\t\t\t\t\t\tappendList(applicationList, { copied: false }),\n\t\t\t\t\t\t\t\tsetValue(entryLabel(ROOT_ID), \"root label\"),\n\t\t\t\t\t\t\t\tsetValue(entryLabel(SIBLING_ID), \"sibling label\"),\n\t\t\t\t\t\t\t\tsetValue(pendingEntry(PENDING_ID), {\n\t\t\t\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\t\t\t\tcustomType: \"pending\",\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tsetValue(operationMeta(OPERATION_ID), {\n\t\t\t\t\t\t\t\t\toperationId: OPERATION_ID,\n\t\t\t\t\t\t\t\t\tlane: \"review\",\n\t\t\t\t\t\t\t\t\tsourceTipId: CHILD_ID,\n\t\t\t\t\t\t\t\t\tstartedAt: 1,\n\t\t\t\t\t\t\t\t\tintent: { kind: \"compaction\" },\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tsetValue(operationState(OPERATION_ID), {\n\t\t\t\t\t\t\t\t\tat: \"summary.deciding\",\n\t\t\t\t\t\t\t\t\tcontrol: { status: \"running\" },\n\t\t\t\t\t\t\t\t\tsettings: {\n\t\t\t\t\t\t\t\t\t\tcompaction: { enabled: true, reserveTokens: 1, keepRecentTokens: 1 },\n\t\t\t\t\t\t\t\t\t\tsteeringMode: \"all\",\n\t\t\t\t\t\t\t\t\t\tfollowUpMode: \"all\",\n\t\t\t\t\t\t\t\t\t\ttoolExecution: \"sequential\",\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\tlatestAssistantEntryId: null,\n\t\t\t\t\t\t\t\t\ttask: { taskId: OPERATION_ID, reason: \"manual\", boundary: { kind: \"finish\" } },\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tsetValue(operationToolArgs(OPERATION_ID, ROOT_ID, 0), {\n\t\t\t\t\t\t\t\t\targument: true,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tsetValue(operationPreparation(OPERATION_ID, OPERATION_ID), {\n\t\t\t\t\t\t\t\t\tkind: \"compaction\",\n\t\t\t\t\t\t\t\t\tmessagesToSummarize: [],\n\t\t\t\t\t\t\t\t\tturnPrefixMessages: [],\n\t\t\t\t\t\t\t\t\tretainedTail: [],\n\t\t\t\t\t\t\t\t\tisSplitTurn: false,\n\t\t\t\t\t\t\t\t\ttokensBefore: 0,\n\t\t\t\t\t\t\t\t\tfileOps: { read: [], written: [], edited: [] },\n\t\t\t\t\t\t\t\t\tsettings: {\n\t\t\t\t\t\t\t\t\t\tenabled: true,\n\t\t\t\t\t\t\t\t\t\treserveTokens: 1,\n\t\t\t\t\t\t\t\t\t\tkeepRecentTokens: 1,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tinsertUsage(usageRow()),\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t\t\t),\n\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t);\n\n\t\t\t\tconst fork = await repo.fork(\n\t\t\t\t\tsource.metadata,\n\t\t\t\t\t{ id: \"fork\", scope: \"branch\", branch: \"review\", entryId: CHILD_ID, position: \"at\" },\n\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t);\n\n\t\t\t\tdeepStrictEqual(\n\t\t\t\t\t(await fork.findEntries({ order: \"asc\" }, BACKGROUND_CONTEXT)).map(({ id }) => id),\n\t\t\t\t\t[ROOT_ID, CHILD_ID],\n\t\t\t\t);\n\t\t\t\tstrictEqual(await fork.branch(\"main\", BACKGROUND_CONTEXT), undefined);\n\t\t\t\tstrictEqual(await getBranchTip(fork, \"review\"), CHILD_ID);\n\t\t\t\tdeepStrictEqual((await fork.getValue(laneConfig(\"review\"), BACKGROUND_CONTEXT))?.value, configuration);\n\t\t\t\tdeepStrictEqual((await fork.getValue(laneState(\"review\"), BACKGROUND_CONTEXT))?.value, idleLaneState);\n\t\t\t\tstrictEqual(await fork.getValue(laneConfig(\"main\"), BACKGROUND_CONTEXT), undefined);\n\t\t\t\tstrictEqual(await fork.getValue(laneState(\"main\"), BACKGROUND_CONTEXT), undefined);\n\t\t\t\tstrictEqual(await fork.getName(BACKGROUND_CONTEXT), \"source name\");\n\t\t\t\tstrictEqual(await fork.getValue(applicationValue, BACKGROUND_CONTEXT), undefined);\n\t\t\t\tdeepStrictEqual(await fork.readList(applicationList, undefined, BACKGROUND_CONTEXT), []);\n\t\t\t\tstrictEqual(await fork.getLabel(ROOT_ID, BACKGROUND_CONTEXT), \"root label\");\n\t\t\t\tstrictEqual(await fork.getLabel(SIBLING_ID, BACKGROUND_CONTEXT), undefined);\n\t\t\t\tstrictEqual(await fork.getValue(operationResult(\"previous\"), BACKGROUND_CONTEXT), undefined);\n\t\t\t\tstrictEqual(await fork.getValue(pendingEntry(PENDING_ID), BACKGROUND_CONTEXT), undefined);\n\t\t\t\tstrictEqual(await fork.getValue(operationMeta(OPERATION_ID), BACKGROUND_CONTEXT), undefined);\n\t\t\t\tstrictEqual(await fork.getValue(operationState(OPERATION_ID), BACKGROUND_CONTEXT), undefined);\n\t\t\t\tstrictEqual(\n\t\t\t\t\tawait fork.getValue(operationToolArgs(OPERATION_ID, ROOT_ID, 0), BACKGROUND_CONTEXT),\n\t\t\t\t\tundefined,\n\t\t\t\t);\n\t\t\t\tstrictEqual(\n\t\t\t\t\tawait fork.getValue(operationPreparation(OPERATION_ID, OPERATION_ID), BACKGROUND_CONTEXT),\n\t\t\t\t\tundefined,\n\t\t\t\t);\n\t\t\t\tconst stats = await fork.getStats(BACKGROUND_CONTEXT);\n\t\t\t\tstrictEqual(stats.messageCount, 1);\n\t\t\t\tdeepStrictEqual(stats.usage, {\n\t\t\t\t\tinput: 0,\n\t\t\t\t\toutput: 0,\n\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\ttotalTokens: 0,\n\t\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t\t});\n\t\t\t\tawait Promise.all([source.close(BACKGROUND_CONTEXT), fork.close(BACKGROUND_CONTEXT)]);\n\t\t\t},\n\t\t),\n\t\tcreateCase(factory, \"forks\", \"enforces branch ancestry for at and before placement\", async ({ repo }) => {\n\t\t\tconst source = await repo.create({ id: \"source\" }, BACKGROUND_CONTEXT);\n\t\t\tawait source.mutate(\n\t\t\t\t(mutator) =>\n\t\t\t\t\tmutator.commit(\n\t\t\t\t\t\t[\n\t\t\t\t\t\t\tinsertEntry({ id: ROOT_ID, parentId: null, type: \"custom\", customType: \"root\" }),\n\t\t\t\t\t\t\tinsertEntry({ id: CHILD_ID, parentId: ROOT_ID, type: \"custom\", customType: \"child\" }),\n\t\t\t\t\t\t\tinsertEntry({ id: SIBLING_ID, parentId: ROOT_ID, type: \"custom\", customType: \"sibling\" }),\n\t\t\t\t\t\t\tsetValue(branchTip(\"main\"), CHILD_ID),\n\t\t\t\t\t\t\tsetValue(laneConfig(\"main\"), configuration),\n\t\t\t\t\t\t\tsetValue(laneState(\"main\"), idleLaneState),\n\t\t\t\t\t\t\tsetValue(branchTip(\"empty\"), null),\n\t\t\t\t\t\t\tsetValue(laneConfig(\"empty\"), configuration),\n\t\t\t\t\t\t\tsetValue(laneState(\"empty\"), idleLaneState),\n\t\t\t\t\t\t],\n\t\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t\t),\n\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t);\n\n\t\t\tconst before = await repo.fork(\n\t\t\t\tsource.metadata,\n\t\t\t\t{ id: \"before\", scope: \"branch\", branch: \"main\", entryId: CHILD_ID, position: \"before\" },\n\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t);\n\t\t\tstrictEqual(await getBranchTip(before), ROOT_ID);\n\t\t\tdeepStrictEqual(\n\t\t\t\t(await before.findEntries({ order: \"asc\" }, BACKGROUND_CONTEXT)).map(({ id }) => id),\n\t\t\t\t[ROOT_ID],\n\t\t\t);\n\n\t\t\tconst mid = await repo.fork(\n\t\t\t\tsource.metadata,\n\t\t\t\t{ id: \"mid\", scope: \"branch\", branch: \"main\", entryId: ROOT_ID, position: \"at\" },\n\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t);\n\t\t\tstrictEqual(await getBranchTip(mid), ROOT_ID);\n\t\t\tdeepStrictEqual(\n\t\t\t\t(await mid.findEntries({ order: \"asc\" }, BACKGROUND_CONTEXT)).map(({ id }) => id),\n\t\t\t\t[ROOT_ID],\n\t\t\t);\n\n\t\t\tconst beforeRoot = await repo.fork(\n\t\t\t\tsource.metadata,\n\t\t\t\t{ id: \"before-root\", scope: \"branch\", branch: \"main\", entryId: ROOT_ID, position: \"before\" },\n\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t);\n\t\t\tstrictEqual(await getBranchTip(beforeRoot), null);\n\t\t\tdeepStrictEqual(await beforeRoot.findEntries({ order: \"asc\" }, BACKGROUND_CONTEXT), []);\n\n\t\t\tconst empty = await repo.fork(\n\t\t\t\tsource.metadata,\n\t\t\t\t{ id: \"empty\", scope: \"branch\", branch: \"empty\" },\n\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t);\n\t\t\tstrictEqual(await getBranchTip(empty, \"empty\"), null);\n\t\t\tdeepStrictEqual(await empty.findEntries({ order: \"asc\" }, BACKGROUND_CONTEXT), []);\n\n\t\t\tfor (const [id, branch, entryId] of [\n\t\t\t\t[\"off-branch\", \"main\", SIBLING_ID],\n\t\t\t\t[\"unknown\", \"main\", UNKNOWN_ID],\n\t\t\t\t[\"null-tip\", \"empty\", ROOT_ID],\n\t\t\t] as const) {\n\t\t\t\tawait rejects(repo.fork(source.metadata, { id, scope: \"branch\", branch, entryId }, BACKGROUND_CONTEXT));\n\t\t\t}\n\t\t\tdeepStrictEqual((await repo.list(undefined, BACKGROUND_CONTEXT)).map(({ id }) => id).sort(), [\n\t\t\t\t\"before\",\n\t\t\t\t\"before-root\",\n\t\t\t\t\"empty\",\n\t\t\t\t\"mid\",\n\t\t\t\t\"source\",\n\t\t\t]);\n\t\t\tawait Promise.all([\n\t\t\t\tsource.close(BACKGROUND_CONTEXT),\n\t\t\t\tbefore.close(BACKGROUND_CONTEXT),\n\t\t\t\tbeforeRoot.close(BACKGROUND_CONTEXT),\n\t\t\t\tempty.close(BACKGROUND_CONTEXT),\n\t\t\t\tmid.close(BACKGROUND_CONTEXT),\n\t\t\t]);\n\t\t}),\n\t\tcreateCase(factory, \"forks\", \"forks a closed source session\", async ({ repo }) => {\n\t\t\tconst source = await repo.create({ id: \"source\" }, BACKGROUND_CONTEXT);\n\t\t\tawait source.mutate(\n\t\t\t\t(mutator) =>\n\t\t\t\t\tmutator.commit(\n\t\t\t\t\t\t[\n\t\t\t\t\t\t\tinsertEntry({\n\t\t\t\t\t\t\t\tid: ROOT_ID,\n\t\t\t\t\t\t\t\tparentId: null,\n\t\t\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\t\t\tcustomType: \"root\",\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tsetValue(branchTip(\"main\"), ROOT_ID),\n\t\t\t\t\t\t\tsetValue(laneConfig(\"main\"), configuration),\n\t\t\t\t\t\t\tsetValue(laneState(\"main\"), idleLaneState),\n\t\t\t\t\t\t\tsetValue(applicationValue, \"excluded\"),\n\t\t\t\t\t\t\tappendList(applicationList, \"excluded\"),\n\t\t\t\t\t\t],\n\t\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t\t),\n\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t);\n\t\t\tawait source.close(BACKGROUND_CONTEXT);\n\n\t\t\tconst fork = await repo.fork(\n\t\t\t\tsource.metadata,\n\t\t\t\t{ id: \"fork\", scope: \"branch\", branch: \"main\" },\n\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t);\n\t\t\tstrictEqual(await getBranchTip(fork), ROOT_ID);\n\t\t\tdeepStrictEqual((await fork.getValue(laneConfig(\"main\"), BACKGROUND_CONTEXT))?.value, configuration);\n\t\t\tdeepStrictEqual((await fork.getValue(laneState(\"main\"), BACKGROUND_CONTEXT))?.value, idleLaneState);\n\t\t\tstrictEqual(await fork.getValue(applicationValue, BACKGROUND_CONTEXT), undefined);\n\t\t\tdeepStrictEqual(await fork.readList(applicationList, undefined, BACKGROUND_CONTEXT), []);\n\t\t\tawait fork.close(BACKGROUND_CONTEXT);\n\t\t}),\n\n\t\tcreateCase(factory, \"forks\", \"forks the whole configured tree with fresh lane state\", async ({ repo }) => {\n\t\t\tconst source = await repo.create({ id: \"source\" }, BACKGROUND_CONTEXT);\n\t\t\tawait source.mutate(\n\t\t\t\t(mutator) =>\n\t\t\t\t\tmutator.commit(\n\t\t\t\t\t\t[\n\t\t\t\t\t\t\tinsertEntry({\n\t\t\t\t\t\t\t\tid: ROOT_ID,\n\t\t\t\t\t\t\t\tparentId: null,\n\t\t\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\t\t\tcustomType: \"root\",\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tinsertEntry({\n\t\t\t\t\t\t\t\tid: CHILD_ID,\n\t\t\t\t\t\t\t\tparentId: ROOT_ID,\n\t\t\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\t\t\tcustomType: \"child\",\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tinsertEntry({\n\t\t\t\t\t\t\t\tid: SIBLING_ID,\n\t\t\t\t\t\t\t\tparentId: ROOT_ID,\n\t\t\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\t\t\tcustomType: \"sibling\",\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tsetValue(branchTip(\"main\"), CHILD_ID),\n\t\t\t\t\t\t\tsetValue(laneConfig(\"main\"), configuration),\n\t\t\t\t\t\t\tsetValue(laneState(\"main\"), idleLaneState),\n\t\t\t\t\t\t\tsetValue(branchTip(\"review\"), SIBLING_ID),\n\t\t\t\t\t\t\tsetValue(laneConfig(\"review\"), configuration),\n\t\t\t\t\t\t\tsetValue(laneState(\"review\"), idleLaneState),\n\t\t\t\t\t\t\tsetValue(branchTip(\"notes\"), ROOT_ID),\n\t\t\t\t\t\t\tsetValue(applicationValue, { copied: true }),\n\t\t\t\t\t\t],\n\t\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t\t),\n\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t);\n\n\t\t\tconst fork = await repo.fork(source.metadata, { id: \"fork\", scope: \"tree\" }, BACKGROUND_CONTEXT);\n\n\t\t\tdeepStrictEqual(\n\t\t\t\t(await fork.findEntries({ order: \"asc\" }, BACKGROUND_CONTEXT)).map(({ id }) => id),\n\t\t\t\t[ROOT_ID, CHILD_ID, SIBLING_ID],\n\t\t\t);\n\t\t\tstrictEqual(await getBranchTip(fork), CHILD_ID);\n\t\t\tstrictEqual(await getBranchTip(fork, \"review\"), SIBLING_ID);\n\t\t\tstrictEqual(await getBranchTip(fork, \"notes\"), ROOT_ID);\n\t\t\tstrictEqual(await fork.getValue(laneConfig(\"notes\"), BACKGROUND_CONTEXT), undefined);\n\t\t\tstrictEqual(await fork.getValue(laneState(\"notes\"), BACKGROUND_CONTEXT), undefined);\n\t\t\tdeepStrictEqual((await fork.getValue(laneConfig(\"review\"), BACKGROUND_CONTEXT))?.value, configuration);\n\t\t\tdeepStrictEqual((await fork.getValue(laneState(\"review\"), BACKGROUND_CONTEXT))?.value, idleLaneState);\n\t\t\tdeepStrictEqual((await fork.getValue(applicationValue, BACKGROUND_CONTEXT))?.value, { copied: true });\n\t\t\tawait Promise.all([source.close(BACKGROUND_CONTEXT), fork.close(BACKGROUND_CONTEXT)]);\n\t\t}),\n\t\tcreateCase(factory, \"forks\", \"rejects only surviving unknown reserved scalar state\", async ({ repo }) => {\n\t\t\tconst source = await repo.create({ id: \"source\" }, BACKGROUND_CONTEXT);\n\t\t\tawait source.createBranch(\"main\", null, BACKGROUND_CONTEXT);\n\t\t\tawait source.mutate(\n\t\t\t\t(mutator) =>\n\t\t\t\t\tmutator.commit(\n\t\t\t\t\t\t[setValue(laneConfig(\"main\"), configuration), setValue(laneState(\"main\"), idleLaneState)],\n\t\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t\t),\n\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t);\n\n\t\t\tfor (const namespace of [\"pi\", \"pi.unknown\"] as const) {\n\t\t\t\tconst address = value<JsonValue>(namespace);\n\t\t\t\tawait source.setValue(address, true, BACKGROUND_CONTEXT);\n\t\t\t\tawait rejects(repo.fork(source.metadata, { id: \"tree\", scope: \"tree\" }, BACKGROUND_CONTEXT));\n\t\t\t\tawait rejects(\n\t\t\t\t\trepo.fork(source.metadata, { id: \"branch\", scope: \"branch\", branch: \"main\" }, BACKGROUND_CONTEXT),\n\t\t\t\t);\n\t\t\t\tawait source.deleteValue(address, BACKGROUND_CONTEXT);\n\t\t\t}\n\t\t\tawait source.close(BACKGROUND_CONTEXT);\n\n\t\t\tconst tree = await repo.fork(source.metadata, { id: \"tree\", scope: \"tree\" }, BACKGROUND_CONTEXT);\n\t\t\tconst branch = await repo.fork(\n\t\t\t\tsource.metadata,\n\t\t\t\t{ id: \"branch\", scope: \"branch\", branch: \"main\" },\n\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t);\n\t\t\tawait Promise.all([tree.close(BACKGROUND_CONTEXT), branch.close(BACKGROUND_CONTEXT)]);\n\t\t}),\n\t];\n}\n\n/** Creates fork cases that require destination reservation across create and fork. */\nexport function createSessionRepoForkDestinationReservationConformance<TMetadata extends SessionMetadata>(\n\tbackendFactory: () => Promise<Pick<SessionRepo<TMetadata>, \"create\" | \"fork\">>,\n\tonClose?: () => void | Promise<void>,\n): readonly ConformanceCase[] {\n\tconst factory = prepareRepoCaseFactory(backendFactory, onClose);\n\treturn [\n\t\tcreateCase(\n\t\t\tfactory,\n\t\t\t\"fork coordination\",\n\t\t\t\"publishes create when it reserves a shared destination id first\",\n\t\t\tasync ({ repo }) => {\n\t\t\t\tconst source = await repo.create({ id: \"source\" }, BACKGROUND_CONTEXT);\n\t\t\t\tconst results = await Promise.allSettled([\n\t\t\t\t\trepo.create({ id: \"destination\" }, BACKGROUND_CONTEXT),\n\t\t\t\t\trepo.fork(source.metadata, { id: \"destination\", scope: \"tree\" }, BACKGROUND_CONTEXT),\n\t\t\t\t]);\n\t\t\t\tstrictEqual(results[0].status, \"fulfilled\");\n\t\t\t\tstrictEqual(results[1].status, \"rejected\");\n\t\t\t\tif (results[0].status === \"fulfilled\") await results[0].value.close(BACKGROUND_CONTEXT);\n\t\t\t\tawait source.close(BACKGROUND_CONTEXT);\n\t\t\t},\n\t\t),\n\t\tcreateCase(\n\t\t\tfactory,\n\t\t\t\"fork coordination\",\n\t\t\t\"publishes fork when it reserves a shared destination id first\",\n\t\t\tasync ({ repo }) => {\n\t\t\t\tconst source = await repo.create({ id: \"source\" }, BACKGROUND_CONTEXT);\n\t\t\t\tconst results = await Promise.allSettled([\n\t\t\t\t\trepo.fork(source.metadata, { id: \"destination\", scope: \"tree\" }, BACKGROUND_CONTEXT),\n\t\t\t\t\trepo.create({ id: \"destination\" }, BACKGROUND_CONTEXT),\n\t\t\t\t]);\n\t\t\t\tstrictEqual(results[0].status, \"fulfilled\");\n\t\t\t\tstrictEqual(results[1].status, \"rejected\");\n\t\t\t\tif (results[0].status === \"fulfilled\") await results[0].value.close(BACKGROUND_CONTEXT);\n\t\t\t\tawait source.close(BACKGROUND_CONTEXT);\n\t\t\t},\n\t\t),\n\t];\n}\n\n/** Creates fork cases that require a snapshot boundary on an active source storage queue. */\nexport function createSessionRepoForkSourceSnapshotConformance<TMetadata extends SessionMetadata>(\n\tbackendFactory: () => Promise<Pick<SessionRepo<TMetadata>, \"create\" | \"fork\">>,\n\tonClose?: () => void | Promise<void>,\n): readonly ConformanceCase[] {\n\tconst factory = prepareRepoCaseFactory(backendFactory, onClose);\n\treturn [\n\t\tcreateCase(\n\t\t\tfactory,\n\t\t\t\"fork coordination\",\n\t\t\t\"captures one coherent boundary between source commits\",\n\t\t\tasync ({ repo }) => {\n\t\t\t\tconst source = await repo.create({ id: \"source\" }, BACKGROUND_CONTEXT);\n\t\t\t\tconst firstMutation = await source.beginMutation(BACKGROUND_CONTEXT);\n\t\t\t\tconst firstCommit = firstMutation.commit(\n\t\t\t\t\t[\n\t\t\t\t\t\tinsertEntry({\n\t\t\t\t\t\t\tid: ROOT_ID,\n\t\t\t\t\t\t\tparentId: null,\n\t\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\t\tcustomType: \"first\",\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tsetValue(branchTip(\"main\"), ROOT_ID),\n\t\t\t\t\t\tsetValue(laneConfig(\"main\"), configuration),\n\t\t\t\t\t\tsetValue(laneState(\"main\"), idleLaneState),\n\t\t\t\t\t\tsetValue(sessionName, \"first name\"),\n\t\t\t\t\t\tsetValue(entryLabel(ROOT_ID), \"first label\"),\n\t\t\t\t\t],\n\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t);\n\t\t\t\tconst fork = repo.fork(\n\t\t\t\t\tsource.metadata,\n\t\t\t\t\t{ id: \"fork\", scope: \"branch\", branch: \"main\" },\n\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t);\n\t\t\t\tconst secondCommit = source.mutate(\n\t\t\t\t\t(mutator) =>\n\t\t\t\t\t\tmutator.commit(\n\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\tinsertEntry({\n\t\t\t\t\t\t\t\t\tid: CHILD_ID,\n\t\t\t\t\t\t\t\t\tparentId: ROOT_ID,\n\t\t\t\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\t\t\t\tcustomType: \"second\",\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\tsetValue(branchTip(\"main\"), CHILD_ID),\n\t\t\t\t\t\t\t\tsetValue(sessionName, \"second name\"),\n\t\t\t\t\t\t\t\tsetValue(entryLabel(ROOT_ID), \"second label\"),\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t\t\t),\n\t\t\t\t\tBACKGROUND_CONTEXT,\n\t\t\t\t);\n\n\t\t\t\tconst [, forked] = await Promise.all([firstCommit, fork]);\n\t\t\t\tawait firstMutation.end(BACKGROUND_CONTEXT);\n\t\t\t\tawait secondCommit;\n\t\t\t\tstrictEqual(await getBranchTip(forked), ROOT_ID);\n\t\t\t\tdeepStrictEqual(\n\t\t\t\t\t(await forked.findEntries({ order: \"asc\" }, BACKGROUND_CONTEXT)).map(({ id }) => id),\n\t\t\t\t\t[ROOT_ID],\n\t\t\t\t);\n\t\t\t\tstrictEqual(await forked.getName(BACKGROUND_CONTEXT), \"first name\");\n\t\t\t\tstrictEqual(await forked.getLabel(ROOT_ID, BACKGROUND_CONTEXT), \"first label\");\n\t\t\t\tawait Promise.all([source.close(BACKGROUND_CONTEXT), forked.close(BACKGROUND_CONTEXT)]);\n\t\t\t},\n\t\t),\n\t];\n}\n\n/** Creates every fork coordination case. */\nexport function createSessionRepoForkCoordinationConformance<TMetadata extends SessionMetadata>(\n\tbackendFactory: () => Promise<Pick<SessionRepo<TMetadata>, \"create\" | \"fork\">>,\n\tonClose?: () => void | Promise<void>,\n): readonly ConformanceCase[] {\n\treturn [\n\t\t...createSessionRepoForkDestinationReservationConformance(backendFactory, onClose),\n\t\t...createSessionRepoForkSourceSnapshotConformance(backendFactory, onClose),\n\t];\n}\n\n/** Creates every fork conformance case. */\nexport function createSessionRepoForkConformance<TMetadata extends SessionMetadata>(\n\tbackendFactory: () => Promise<Pick<SessionRepo<TMetadata>, \"create\" | \"list\" | \"fork\">>,\n\tonClose?: () => void | Promise<void>,\n): readonly ConformanceCase[] {\n\treturn [\n\t\t...createSessionRepoForkBehaviorConformance(backendFactory, onClose),\n\t\t...createSessionRepoForkCoordinationConformance(backendFactory, onClose),\n\t];\n}\n\n/** Creates every SessionRepo conformance case. */\nexport function createSessionRepoConformance<TMetadata extends SessionMetadata>(\n\tfactory: () => Promise<SessionRepo<TMetadata>>,\n\tonClose?: () => void | Promise<void>,\n): readonly ConformanceCase[] {\n\treturn [\n\t\t...createSessionRepoLifecycleConformance(factory, onClose),\n\t\t...createSessionRepoOwnershipConformance(factory, onClose),\n\t\t...createSessionRepoMessageConformance(factory, onClose),\n\t\t...createSessionRepoForkConformance(factory, onClose),\n\t];\n}\n"]}