Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 51 additions & 41 deletions src/utils/sessionStorage.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { afterEach, expect, test } from 'bun:test'
import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
import { type UUID } from 'node:crypto'
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
Expand Down Expand Up @@ -27,7 +28,7 @@ function id(n: number): UUID {
return `00000000-0000-4000-8000-${String(n).padStart(12, '0')}` as UUID
}

function base(uuid: string, parentUuid: string | null) {
function base(uuid: UUID, parentUuid: UUID | null) {
return {
uuid,
parentUuid,
Expand All @@ -40,7 +41,11 @@ function base(uuid: string, parentUuid: string | null) {
}
}

function user(uuid: string, parentUuid: string | null, content: string) {
function user(
uuid: UUID,
parentUuid: UUID | null,
content: string | ToolResultBlockParam[],
) {
return {
...base(uuid, parentUuid),
type: 'user',
Expand All @@ -52,7 +57,7 @@ function user(uuid: string, parentUuid: string | null, content: string) {
}
}

function assistant(uuid: string, parentUuid: string | null, text: string) {
function assistant(uuid: UUID, parentUuid: UUID | null, text: string) {
return {
...base(uuid, parentUuid),
type: 'assistant',
Expand All @@ -74,12 +79,12 @@ function assistant(uuid: string, parentUuid: string | null, text: string) {
}

function compactBoundary(
uuid: string,
parentUuid: string | null,
uuid: UUID,
parentUuid: UUID | null,
preservedSegment: {
headUuid: string
anchorUuid: string
tailUuid: string
headUuid: UUID
anchorUuid: UUID
tailUuid: UUID
},
) {
return {
Expand All @@ -98,9 +103,9 @@ function compactBoundary(
}

function snipBoundary(
uuid: string,
parentUuid: string | null,
removedUuids: string[],
uuid: UUID,
parentUuid: UUID | null,
removedUuids: UUID[],
) {
return {
...base(uuid, parentUuid),
Expand All @@ -121,6 +126,23 @@ async function writeJsonl(entries: unknown[]): Promise<string> {
return filePath
}

function getToolResultContent(content: unknown): string | undefined {
if (!Array.isArray(content)) return undefined

const [block] = content
if (
typeof block !== 'object' ||
block === null ||
!('type' in block) ||
block.type !== 'tool_result' ||
!('content' in block)
) {
return undefined
}

return typeof block.content === 'string' ? block.content : undefined
}

function readGoalStateEntries(text: string): Array<{ goal: GoalState | null }> {
return text
.split('\n')
Expand Down Expand Up @@ -300,18 +322,14 @@ test('loadTranscriptFile fails closed when preserved-segment anchor is missing',
})

test('stripPersistedToolUseResultsFromJSONLBuffer drops raw toolUseResult while preserving persisted preview content', () => {
const persisted = user(id(31), null, 'placeholder')
persisted.message = {
role: 'user',
content: [
{
type: 'tool_result',
tool_use_id: 'tool-31',
is_error: false,
content: '<persisted-output>\nPreview text\n</persisted-output>',
},
],
}
const persisted = user(id(31), null, [
{
type: 'tool_result',
tool_use_id: 'tool-31',
is_error: false,
content: '<persisted-output>\nPreview text\n</persisted-output>',
},
])
;(persisted as typeof persisted & { toolUseResult?: unknown }).toolUseResult = {
stdout: 'x'.repeat(200_000),
stderr: '',
Expand All @@ -324,24 +342,18 @@ test('stripPersistedToolUseResultsFromJSONLBuffer drops raw toolUseResult while
>

expect(parsed?.toolUseResult).toBeUndefined()
expect(
(parsed?.message.content as Array<{ content: string }>)[0]?.content,
).toContain('Preview text')
expect(getToolResultContent(parsed?.message.content)).toContain('Preview text')
})

test('loadTranscriptFile omits raw toolUseResult for persisted-output transcript entries', async () => {
const persisted = user(id(41), null, 'placeholder')
persisted.message = {
role: 'user',
content: [
{
type: 'tool_result',
tool_use_id: 'tool-41',
is_error: false,
content: '<persisted-output>\nPreview text\n</persisted-output>',
},
],
}
const persisted = user(id(41), null, [
{
type: 'tool_result',
tool_use_id: 'tool-41',
is_error: false,
content: '<persisted-output>\nPreview text\n</persisted-output>',
},
])
;(persisted as typeof persisted & { toolUseResult?: unknown }).toolUseResult = {
stdout: 'y'.repeat(200_000),
stderr: '',
Expand All @@ -355,9 +367,7 @@ test('loadTranscriptFile omits raw toolUseResult for persisted-output transcript

expect(loaded).toBeDefined()
expect(loaded?.toolUseResult).toBeUndefined()
expect(
(loaded?.message.content as Array<{ content: string }>)[0]?.content,
).toContain('Preview text')
expect(getToolResultContent(loaded?.message.content)).toContain('Preview text')
})

test('loadTranscriptFile restores last goal-state metadata entry', async () => {
Expand Down