Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Preserve repeated user prompts and their turns after assistant replies or tool/reasoning activity; still collapse consecutive prompt echoes (issue #9, PR #10).

## 1.6.2 — 2026-10-05

- Import sessions in the installed DSH current format and support handle-based persistence, incremental assistant replies, new turns, and idempotent re-imports (PR #3).
Expand Down
4 changes: 4 additions & 0 deletions lib/codex-reader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export function parseCodexSession(file) {

// ── standalone tool-call / tool-result / reasoning events ─────────────
if (type === 'custom_tool_call' || type === 'function_call') {
lastUserText = undefined
if (pending.length === 0) pendingTime = at
const id = typeof payload.call_id === 'string' ? payload.call_id : `codex-call-${crypto.randomUUID()}`
const input = type === 'function_call' ? (payload.arguments ?? payload.input) : payload.input
Expand All @@ -190,6 +191,7 @@ export function parseCodexSession(file) {
continue
}
if (type === 'custom_tool_call_output' || type === 'function_call_output') {
lastUserText = undefined
if (pending.length === 0) pendingTime = at
const id = typeof payload.call_id === 'string' ? payload.call_id : ''
const raw = payload.output ?? payload.content
Expand All @@ -204,6 +206,7 @@ export function parseCodexSession(file) {
continue
}
if (type === 'reasoning') {
lastUserText = undefined
if (pending.length === 0) pendingTime = at
const mapped = normalizeContentBlock({ type: 'reasoning', summary: payload.summary, text: payload.text })
if (mapped !== undefined) pending.push(mapped)
Expand All @@ -214,6 +217,7 @@ export function parseCodexSession(file) {
if (type !== 'message') continue
flushPending()
const role = payload.role
if (role !== 'user') lastUserText = undefined
if (role !== 'user' && role !== 'assistant') continue
const blocks = []
for (const block of Array.isArray(payload.content) ? payload.content : []) {
Expand Down
54 changes: 54 additions & 0 deletions test/codex-reader.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,60 @@ let n = 0
const write = (file, text) => { writeFileSync(file, text); return file }
const nextFile = (kind) => write(join(mkdtempSync(join(tmpdir(), 'cx-new-')), `${kind}-${++n}.jsonl`), '')

test('reader: identical user prompts separated by an assistant reply keep both turns', () => {
const file = nextFile('repeat-prompt')
write(file, rolloutOf([
msg('T1', '2026-08-17T10:00:01Z', 'user', [{ type: 'input_text', text: 'continue' }]),
msg('T2', '2026-08-17T10:00:02Z', 'assistant', [{ type: 'output_text', text: 'ok' }]),
msg('T3', '2026-08-17T10:00:03Z', 'user', [{ type: 'input_text', text: 'continue' }]),
msg('T4', '2026-08-17T10:00:04Z', 'assistant', [{ type: 'output_text', text: 'done' }]),
]))
const { messages } = parseCodexSession(file)
assert.deepEqual(messages.map(m => [m.role, m.blocks[0].text]), [
['user', 'continue'], ['assistant', 'ok'], ['user', 'continue'], ['assistant', 'done'],
])
const events = buildDshEvents(messages)
assert.equal(events.filter(e => e.type === 'user/message').length, 2)
assert.equal(events.filter(e => e.type === 'turn/start').length, 2)
assert.deepEqual(events.filter(e => e.type === 'assistant/message').map(e => e.data.turn), [1, 2])
})

test('reader: consecutive identical user prompt echoes still collapse', () => {
const file = nextFile('prompt-echo')
write(file, rolloutOf([
msg('T1', '2026-08-17T10:00:01Z', 'user', [{ type: 'input_text', text: 'continue' }]),
msg('T2', '2026-08-17T10:00:02Z', 'user', [{ type: 'input_text', text: 'continue' }]),
msg('T3', '2026-08-17T10:00:03Z', 'assistant', [{ type: 'output_text', text: 'done' }]),
]))
const { messages } = parseCodexSession(file)
assert.deepEqual(messages.map(m => [m.role, m.blocks[0].text]), [
['user', 'continue'], ['assistant', 'done'],
])
assert.equal(buildDshEvents(messages).filter(e => e.type === 'turn/start').length, 1)
})

for (const payload of [
{ type: 'custom_tool_call', call_id: 'c1', name: 'echo', input: 'hi' },
{ type: 'custom_tool_call_output', call_id: 'c1', output: 'hi' },
{ type: 'function_call', call_id: 'c1', name: 'echo', arguments: '{}' },
{ type: 'function_call_output', call_id: 'c1', output: 'hi' },
{ type: 'reasoning', summary: 'thinking' },
{ type: 'reasoning', summary: [] },
{ type: 'message', role: 'assistant', content: [] },
{ type: 'message', role: 'developer', content: [] },
]) {
test(`reader: ${payload.type} ${payload.role ?? JSON.stringify(payload.summary) ?? ''} breaks prompt dedupe`, () => {
const file = nextFile('prompt-boundary')
write(file, rolloutOf([
msg('T1', '2026-08-17T10:00:01Z', 'user', [{ type: 'input_text', text: 'continue' }]),
{ type: 'response_item', timestamp: '2026-08-17T10:00:02Z', payload },
msg('T3', '2026-08-17T10:00:03Z', 'user', [{ type: 'input_text', text: 'continue' }]),
]))
const { messages } = parseCodexSession(file)
assert.deepEqual(messages.filter(m => m.role === 'user').map(m => m.blocks[0].text), ['continue', 'continue'])
})
}

test('new schema: tool call + real output fold into one assistant message with real result', () => {
const events = [
msg('T1', '2026-08-17T10:00:01Z', 'user', [{ type: 'input_text', text: '改一下 main.js' }]),
Expand Down