Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
4abc374
Save native chat state and restore only its journal tail
brennanb2025 Oct 29, 2026
d2cd8e4
Keep checkpoint recovery durable and skip unchanged captures
brennanb2025 Oct 29, 2026
5fd8c17
Keep journal timer and restart fixtures compatible with checkpoints
brennanb2025 Oct 29, 2026
5de2fa7
Keep checkpoint performance tests focused on operation contracts
brennanb2025 Oct 29, 2026
b272abb
Remove unused benchmark import
brennanb2025 Oct 29, 2026
5c7df65
Restore journal append interception and control Stop fixture time
brennanb2025 Oct 29, 2026
a15c795
Compare saved reducer state without a key assertion
brennanb2025 Oct 29, 2026
50278cb
Merge remote-tracking branch 'origin/main' into brennanb2025/journal-…
brennanb2025 Oct 29, 2026
d07dce8
Supply thread scope in checkpoint test appends
brennanb2025 Oct 29, 2026
7f8a049
Prove restored journal consumer paths
brennanb2025 Oct 29, 2026
7bf75aa
Merge remote-tracking branch 'origin/main' into brennanb2025/journal-…
brennanb2025 Oct 29, 2026
73f4313
Merge remote-tracking branch 'origin/main' into brennanb2025/journal-…
brennanb2025 Oct 29, 2026
12e708a
test: exercise restored consumer backward pagination
brennanb2025 Oct 29, 2026
d2a6c3c
Merge remote-tracking branch 'origin/main' into brennanb2025/journal-…
brennanb2025 Oct 29, 2026
b7d8328
Merge remote-tracking branch 'origin/main' into brennanb2025/journal-…
brennanb2025 Oct 29, 2026
72cc44b
fix: pace journal backfill with a monotonic clock
brennanb2025 Oct 29, 2026
5cc2eeb
Merge remote-tracking branch 'origin/main' into brennanb2025/journal-…
brennanb2025 Oct 29, 2026
67bcaec
Merge origin/main for overlapping journal changes
brennanb2025 Oct 30, 2026
fb6e71e
refactor: keep journal opening cursor checks together
brennanb2025 Oct 30, 2026
3565718
Merge origin/main after shared option menu fix
brennanb2025 Oct 30, 2026
138c98d
refactor: project chat journal state in the append transaction
brennanb2025 Oct 30, 2026
7715c68
fix: keep reducer objects live across journal commits
brennanb2025 Oct 30, 2026
9cf8f71
fix: bound projection repair and check the invariant after notification
brennanb2025 Oct 30, 2026
d301730
fix: keep the background projection pass proportional to chats
brennanb2025 Oct 30, 2026
69d976e
fix: version the keyed projection layout as codec 2
brennanb2025 Oct 30, 2026
1bdd05a
test: follow the append-with-fold contract and deferred publication
brennanb2025 Oct 30, 2026
aef42d8
test: pin projection repair backoff, background pass and N/10N costs
brennanb2025 Oct 30, 2026
b3d3473
refactor: inline draft write-back helpers
brennanb2025 Oct 30, 2026
bb92f52
fix: write a new epoch's saved state in its own transaction
brennanb2025 Oct 30, 2026
4a2a2e1
fix: keep the sampled development check log-only on any failure
brennanb2025 Oct 30, 2026
4491839
fix: typecheck the draft copy without closure narrowing
brennanb2025 Oct 30, 2026
9b2d828
test: catch whole-table sweeps and an undecodable projection
brennanb2025 Oct 30, 2026
db2d504
refactor: drop draft casts the types can carry
brennanb2025 Oct 30, 2026
1fc048e
test: show a parked damaged chat publishes after a clean epoch replac…
brennanb2025 Oct 30, 2026
e1a5338
test: give the seeded queued card its enqueue cursor
brennanb2025 Oct 30, 2026
ff0d9e4
fix: catch lagging saved chat state up in atomic pages
brennanb2025 Oct 30, 2026
0cfb79b
test: pin the draft layer's tracked nodes per streamed revision and a…
brennanb2025 Oct 30, 2026
ab99e00
test: append the between-pages send by item identity
brennanb2025 Oct 30, 2026
94642d1
test: check every catch-up prefix of the recorded Claude and Codex st…
brennanb2025 Oct 30, 2026
a720ff6
test: measure each backfill pass's row reads on its own
brennanb2025 Oct 30, 2026
9c75ad3
fix: bound catch-up reads, release the finished prefix, and keep unre…
brennanb2025 Oct 30, 2026
11a09ca
test: cover takeover, epoch replacement and stop in the middle of a pass
brennanb2025 Oct 30, 2026
7f90ed6
Merge origin/main into brennanb2025/journal-saved-state
brennanb2025 Oct 30, 2026
7eb4bd6
test: fail the takeover test without the handover break, and the repl…
brennanb2025 Oct 30, 2026
7317d9d
test: read saved-view pages with the page budget, and await database …
brennanb2025 Oct 30, 2026
b19a175
Persist native chat outcomes with a rebuildable cache
brennanb2025 Oct 30, 2026
78defc3
test: repair the split suites so they parse, lint clean and find thei…
brennanb2025 Oct 30, 2026
a60dce2
test: read outcome epochs as outcomes where the adapted assertions re…
brennanb2025 Oct 30, 2026
af9290e
test: expect saved queue cards as whole values in the queue-history a…
brennanb2025 Oct 30, 2026
c9b18f4
style: format the files this PR changed
brennanb2025 Oct 30, 2026
c0db485
test: import each module once in two journal suites
brennanb2025 Oct 30, 2026
ce52c52
fix: decode saved outcomes with a frozen format-1 schema, independent…
brennanb2025 Oct 30, 2026
00288ac
fix: key outcomes by an integer generation and keep the cache stamp i…
brennanb2025 Oct 30, 2026
53262df
fix: finalize queue ownership before publishing a new epoch, and reje…
brennanb2025 Oct 30, 2026
d4b39be
fix: open a chat synchronously and unpaced, and classify read failure…
brennanb2025 Oct 30, 2026
ccb6a9a
fix: run all journal maintenance through one paced runner per database
brennanb2025 Oct 30, 2026
cf200d4
test: adapt journal suites to generation-keyed outcomes, the merged c…
brennanb2025 Oct 30, 2026
07c5247
test: pin outcome format 1 with checked-in rows, and cover open consi…
brennanb2025 Oct 30, 2026
9c9f7d0
refactor: check a converting chat's epoch once, in the catch-up step …
brennanb2025 Oct 30, 2026
fecbba9
test: drive the maintenance runner over a real database, each guard k…
brennanb2025 Oct 30, 2026
daa93f1
test: N/10N contracts for outcome appends, opens, conversion, publica…
brennanb2025 Oct 30, 2026
fe25a76
test: shipped builds keep a new or converted outcome chat untouched
brennanb2025 Oct 30, 2026
c86180c
test: an epoch publication over an epoch the caller did not see is re…
brennanb2025 Oct 30, 2026
c8b4019
test: old-build probes fold input rows only, and the interrupted-call…
brennanb2025 Oct 30, 2026
b4660f8
test: import the state layout once in the cache test support
brennanb2025 Oct 30, 2026
70437f2
fix: type the tail verify's live lookups per collection, and two test…
brennanb2025 Oct 30, 2026
63bca05
fix: wake the maintenance runner for a request or a due retry ahead o…
brennanb2025 Oct 30, 2026
dba7e5a
test: pin the draft's tracked nodes per streamed revision and accepta…
brennanb2025 Oct 30, 2026
bc04d49
test: cross-version probes read a new chat's turn end as one outcome …
brennanb2025 Oct 30, 2026
6987dce
fix: store every new write as format-tagged outcome records after an …
brennanb2025 Oct 31, 2026
ae70eed
test: pin the marker, prefix-plus-records reopen, record admission be…
brennanb2025 Oct 31, 2026
bb349d1
fix: refuse a chat whose outcome marker is a newer version as a newer…
brennanb2025 Oct 31, 2026
a2247a1
test: shipped builds refuse an existing chat at its appended marker, …
brennanb2025 Oct 31, 2026
cf85605
test: read the runner's rebuild instances as rebuilds, so the parked-…
brennanb2025 Oct 31, 2026
e3cf586
fix: admit saved outcome records and the cache only through a strict …
brennanb2025 Oct 31, 2026
69dd05a
fix: refuse an input row or legacy card holding a key this build does…
brennanb2025 Oct 31, 2026
db62a02
test: a persisted type holding a key storage does not declare, nested…
brennanb2025 Oct 31, 2026
812427a
test: this build refuses a newer build's body or subject kind as a ne…
brennanb2025 Oct 31, 2026
46c590d
fix: fold each outcome write from the row a reload reads, so an annot…
brennanb2025 Oct 31, 2026
ecc122e
test: an echo declared without a known original is stored as null, th…
brennanb2025 Oct 31, 2026
10ab8a2
test: probe the downgrade refusal on both releases in the downgrade w…
brennanb2025 Oct 31, 2026
36918c4
test: both releases in the downgrade window write a Claude and a Code…
brennanb2025 Oct 31, 2026
6078feb
test: rewind, replacement and rollover of a chat holding old rows, th…
brennanb2025 Oct 31, 2026
cc47d20
test: phone and paired catch-up over a chat holding old rows then rec…
brennanb2025 Oct 31, 2026
7112961
test: ACP launch decisions read an outcome chat from its cache and fr…
brennanb2025 Oct 31, 2026
b4708d7
Open new and mixed chats from each key's newest record and drop the s…
brennanb2025 Oct 31, 2026
95bdf81
Derive an outcome chat's last sequence from its coverage instead of r…
brennanb2025 Oct 31, 2026
ae7a4f1
test: count strict admission per open, catch-up page and write of an …
brennanb2025 Oct 31, 2026
1bf4ca3
Keep a record that updated a prefix key when a later prefix fold no l…
brennanb2025 Oct 31, 2026
4bc26f6
Resume each sweep page at the generation it is deleting
brennanb2025 Oct 31, 2026
945ca74
test: the sweeper rests nine times each page's work
brennanb2025 Oct 31, 2026
3d53835
Start the retired-record sweep from the chat database the attachments…
brennanb2025 Oct 31, 2026
0c6a8e0
test: an existing chat whose first write fails keeps no marker, and t…
brennanb2025 Oct 31, 2026
5553367
test: chats the window's releases and the oldest release with this ch…
brennanb2025 Oct 31, 2026
03394d8
test: the released-chat writer resumes the queue after every Stop it …
brennanb2025 Oct 31, 2026
a16ab2a
test: the released-chat probe checks this build's edit, send and rewi…
brennanb2025 Oct 31, 2026
03d3f71
test: the golden input prefix holds data in every keyed collection
brennanb2025 Oct 31, 2026
69b3a21
Say once, beside the marker version, that the reading of old rows und…
brennanb2025 Oct 31, 2026
f83871e
test: the released-chat probe compares what the release wrote, not fi…
brennanb2025 Oct 31, 2026
d3c7fa6
Merge origin/main into brennanb2025/journal-saved-state
brennanb2025 Oct 31, 2026
4e9b1a9
test: say why the first-write-failure downgrade test needs a longer t…
brennanb2025 Oct 31, 2026
d27c53a
Type the open-union fallback check by schema, not a bare object
brennanb2025 Oct 31, 2026
aaec8b0
Check each outcome-format key set separately so no check reads as a r…
brennanb2025 Oct 31, 2026
a73b65b
Read an open or branded string as a value in the storage key check, n…
brennanb2025 Oct 31, 2026
a293455
Decode each newest outcome record from the value admission parsed, wi…
brennanb2025 Oct 31, 2026
3d67d35
Match each encoded record to its change by position, so a settlement …
brennanb2025 Oct 31, 2026
84bc39e
Encode each changed value as the draft holds it, without reading it t…
brennanb2025 Oct 31, 2026
a65467d
test: rewind, replacement and rollover restate a chat with fixed stat…
brennanb2025 Oct 31, 2026
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Read user_version before persistent pragmas/DDL: even enabling WAL mutates newer databases.

import { ensureJournalSavedStateTables } from './journal-saved-state'
import Database from '../../sqlite/sync-database'
import { hardenSqliteDatabaseFiles } from '../../sqlite/harden-database-files'
import {
Expand Down Expand Up @@ -72,6 +73,7 @@ export function openJournalDatabase(dbPath: string): OpenJournalDatabase {
// Additive tables must exist on every writable open, even when version migration returns early.
ensureQueuedMessagesTable(probe)
ensureCommandReceiptsTable(probe)
ensureJournalSavedStateTables(probe)
ensureAgentSessionAttachmentClaimTables(probe)
hardenSqliteDatabaseFiles(dbPath)
transferred = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import { isAgentSessionContextClear } from '../../../shared/agent-session-contex
export class JournalDerivedTurnScope {
private openTurnItemId: string | null = null

snapshot(): string | null {
return this.openTurnItemId
}

static restore(openTurnItemId: string | null): JournalDerivedTurnScope {
const scope = new JournalDerivedTurnScope()
scope.openTurnItemId = openTurnItemId
return scope
}

scopeFor(body: AgentJournalItemBody): AgentJournalTurnScope {
return this.openTurnItemId === null || readAgentJournalTurn(body)
? AGENT_JOURNAL_THREAD_SCOPE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { clearJournalSavedState } from './journal-saved-state'
import {
journalItemEchoFields,
journalItemEchoRestatement,
Expand Down Expand Up @@ -193,6 +194,7 @@ export function replaceJournalEpoch(input: {
const receipt = input.receipt?.({ epoch, sequence: state.lastSequence })
const claimsCommitted: (() => void)[] = []
input.database.transaction((db) => {
clearJournalSavedState(db, sessionId)
const retired = readJournalSessionEpoch(db, sessionId)
if (retired !== null) {
deleteJournalEpochRows(db, sessionId, retired)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Delete and replace epochs atomically; retaining superseded rows would grow history without bound.

import { clearJournalSavedState } from './journal-saved-state'
import { journalRowSchemaVersion } from '../../../shared/agent-session-journal-types'
import type { AgentSessionJournalIdentity } from '../../../shared/agent-session-journal-types'
import { agentSessionJournalProviderHandle } from '../../../shared/agent-session-provider-handle-encoding'
Expand Down Expand Up @@ -52,6 +53,7 @@ export function publishNewEpoch(input: {
}
}
input.database.transaction((db) => {
clearJournalSavedState(db, sessionId)
const retired = readJournalSessionEpoch(db, sessionId)
if (retired !== null) {
deleteJournalEpochRows(db, sessionId, retired)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { resolve } from 'node:path'
import { JournalHostDatabase } from './journal-host-database'
import { clearJournalSavedState } from './journal-saved-state'
import { replayJournal, type JournalLoad } from './journal-open'
import { serializeJournalRow, type JournalRow } from './journal-row-schema'
import {
Expand Down Expand Up @@ -141,15 +142,31 @@ export function updateTestJournalRowJson(
seq: number,
rowJson: string
): void {
invalidateEditedJournalPrefix(db, sessionId, seq)
db.prepare(
'UPDATE journal_rows SET row_json = ? WHERE session_id = ? AND epoch = ? AND seq = ?'
).run(rowJson, sessionId, liveEpoch(db, sessionId), seq)
}

export function deleteTestJournalRow(db: Database.Database, sessionId: string, seq: number): void {
invalidateEditedJournalPrefix(db, sessionId, seq)
db.prepare('DELETE FROM journal_rows WHERE session_id = ? AND epoch = ? AND seq = ?').run(
sessionId,
liveEpoch(db, sessionId),
seq
)
}

/** Fixture history edits need a fresh replay of their edited prefix, rather than its older checkpoint. */
function invalidateEditedJournalPrefix(
db: Database.Database,
sessionId: string,
seq: number
): void {
const saved = db
.prepare('SELECT through_seq FROM journal_state WHERE session_id = ?')
.get(sessionId)
if (typeof saved?.through_seq === 'number' && seq <= saved.through_seq) {
clearJournalSavedState(db, sessionId)
}
}
36 changes: 30 additions & 6 deletions src/main/native-chat/agent-session-journal/journal-open.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Replay live epochs without rewriting history; damage/newer rows fail load and remain on disk.

import { readJournalSavedState } from './journal-saved-state'
import type Database from '../../sqlite/sync-database'
import {
applyJournalRow,
Expand Down Expand Up @@ -33,23 +34,46 @@ export function replayJournal(db: Database.Database, sessionId: string): Journal
if (epoch === null) {
return null
}
const fold = startJournalRowFold({ sessionId, epoch })
for (const entry of iterateJournalEpochRows(db, sessionId, epoch)) {
return foldJournalFromState(db, createJournalReducerState(sessionId, epoch))
}

/** The saved prefix is authoritative; only the contiguous tail is replayed. */
export function restoreJournal(db: Database.Database, sessionId: string): JournalLoad | null {
const epoch = readJournalSessionEpoch(db, sessionId)
if (epoch === null) {
return null
}
const state =
readJournalSavedState(db, sessionId, epoch) ?? createJournalReducerState(sessionId, epoch)
return foldJournalFromState(db, state)
}

function foldJournalFromState(db: Database.Database, state: JournalReducerState): JournalLoad {
const { sessionId, epoch, lastSequence } = state
const fold = startJournalRowFold({ sessionId, epoch }, state)
for (const entry of iterateJournalEpochRows(
db,
sessionId,
epoch,
lastSequence === 0 ? Number.MIN_SAFE_INTEGER : lastSequence
)) {
if (!fold.add(entry)) {
break
}
}
return fold.finish()
}

export function startJournalRowFold(input: { sessionId: string; epoch: string }): {
export function startJournalRowFold(
input: { sessionId: string; epoch: string },
state = createJournalReducerState(input.sessionId, input.epoch)
): {
add: (entry: { seq: number; rowJson: string; ts?: number }) => boolean
finish: () => JournalLoad
} {
const state = createJournalReducerState(input.sessionId, input.epoch)
let expectedSequence = FIRST_JOURNAL_SEQUENCE
let expectedSequence = state.lastSequence + 1
let damage: JournalDamage | null = null
let anchored = false
let anchored = state.lastSequence > 0
let newer: { sequence: number } | null = null
let empty = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ const EPOCH_ROW_PAGE_SIZE = 128
export function* iterateJournalEpochRows(
db: Database.Database,
sessionId: string,
epoch: string
epoch: string,
afterSequence = Number.MIN_SAFE_INTEGER
): Generator<JournalStoredRow> {
let afterSeq = Number.MIN_SAFE_INTEGER
let afterSeq = afterSequence
for (;;) {
const page = readJournalRowsAfter(db, sessionId, epoch, afterSeq, EPOCH_ROW_PAGE_SIZE)
yield* page
Expand Down
34 changes: 15 additions & 19 deletions src/main/native-chat/agent-session-journal/journal-row-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type JournalRowWriterDeps = {
readOnly: () => boolean
highestFence: () => number
nextSequence: () => number
commit: (rows: readonly JournalRow[], savedAt: number) => void
commit: (rows: readonly JournalRow[], savedAt: number, streamed?: boolean) => void
/** Streamed-text-only revisions may skip per-commit fsync; settlement makes them durable. */
streamedTextRevision?: (row: JournalRow) => boolean
}
Expand All @@ -65,16 +65,14 @@ export class JournalRowWriter {
const row = build(this.deps.nextSequence(), savedAt)
assertJournalFence(row.fence, this.deps.highestFence())
const receipt = typeof prepareReceipt === 'function' ? prepareReceipt([row]) : prepareReceipt
this.deps.database().transaction(
(db) => {
insertJournalRow(db, this.deps.sessionId, row, savedAt)
receipt?.write(db, row)
},
!receipt && this.streamed([row])
)
const streamed = !receipt && this.streamed([row])
this.deps.database().transaction((db) => {
insertJournalRow(db, this.deps.sessionId, row, savedAt)
receipt?.write(db, row)
}, streamed)
// Adopt durable sequence state before fallible work: nonthrowing receipt ledger, then fold.
receipt?.committed()
this.deps.commit([row], savedAt)
this.deps.commit([row], savedAt, streamed)
return row
})
}
Expand Down Expand Up @@ -105,17 +103,15 @@ export class JournalRowWriter {
assertJournalFence(row.fence, this.deps.highestFence())
}
const receipt = typeof prepareReceipt === 'function' ? prepareReceipt(rows) : prepareReceipt
this.deps.database().transaction(
(db) => {
for (const row of rows) {
insertJournalRow(db, this.deps.sessionId, row, ts)
}
receipt?.write(db, receiptRow?.(rows))
},
!receipt && this.streamed(rows)
)
const streamed = !receipt && this.streamed(rows)
this.deps.database().transaction((db) => {
for (const row of rows) {
insertJournalRow(db, this.deps.sessionId, row, ts)
}
receipt?.write(db, receiptRow?.(rows))
}, streamed)
receipt?.committed()
this.deps.commit(rows, ts)
this.deps.commit(rows, ts, streamed)
return rows
}

Expand Down
Loading
Loading