mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
fix: refuse chat access on newer journal schemas
This commit is contained in:
@@ -5,6 +5,7 @@ import { afterEach, expect, it } from 'vitest'
|
||||
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
|
||||
import { openJournalDatabase } from './journal-database'
|
||||
import { journalDatabasePath } from './journal-host-database'
|
||||
import { JOURNAL_DOMAIN_SCHEMA_VERSION } from './journal-domain-migrations'
|
||||
import { JournalSqlDatabase } from './journal-sql-database'
|
||||
import { publishJournalSessionEpoch } from '../durable-rows/journal-row-table'
|
||||
import { publishJournalOutcomeEpoch } from '../outcomes/journal-outcome-epoch.test-fixture'
|
||||
@@ -168,6 +169,53 @@ it('keeps saved history readable after startup rollback and retries before the n
|
||||
})
|
||||
})
|
||||
|
||||
it('refuses newer-schema chat access while retaining the catalog and saved chat rows', async () => {
|
||||
const record = storedTestAgentSessionRecord(agentSessionRecordFixture())
|
||||
const sessionId = record.sessionId
|
||||
const { root, database, errors } = await fixture([sessionId])
|
||||
await database.openSession(identity(sessionId))
|
||||
await withTestAgentSessionDatabase(root, (db) => {
|
||||
db.prepare('INSERT INTO agent_session_records(session_id,record_json) VALUES(?,?)').run(
|
||||
sessionId,
|
||||
JSON.stringify(record)
|
||||
)
|
||||
})
|
||||
await database.close()
|
||||
const saved = await withTestAgentSessionDatabase(root, (db) => {
|
||||
db.pragma(`user_version = ${JOURNAL_DOMAIN_SCHEMA_VERSION + 1}`)
|
||||
return {
|
||||
version: db.pragma('user_version', { simple: true }),
|
||||
chats: db.prepare('SELECT * FROM journal_chat_state ORDER BY session_id').all(),
|
||||
items: db.prepare('SELECT * FROM journal_items ORDER BY session_id,item_id').all(),
|
||||
records: db.prepare('SELECT * FROM agent_session_records ORDER BY session_id').all()
|
||||
}
|
||||
})
|
||||
const newer = await open(root, errors)
|
||||
const refusal = {
|
||||
refusal: {
|
||||
code: 'agent_session_journal_unreadable',
|
||||
details: { reason: 'journalWrittenByNewerOrca' }
|
||||
}
|
||||
}
|
||||
await expect(newer.openSession(identity(sessionId))).rejects.toMatchObject(refusal)
|
||||
expect(await newer.ensureSessionEpoch(sessionId)).toBeNull()
|
||||
await expect(newer.readers.domainHistory(history(sessionId))).rejects.toMatchObject(refusal)
|
||||
await expect(
|
||||
newer.readers.point({ sessionId, operation: { kind: 'saved-content' } })
|
||||
).rejects.toMatchObject(refusal)
|
||||
expect((await newer.loadRecords()).records.get(sessionId)).toMatchObject({ sessionId })
|
||||
await newer.close()
|
||||
expect(
|
||||
await withTestAgentSessionDatabase(root, (db) => ({
|
||||
version: db.pragma('user_version', { simple: true }),
|
||||
chats: db.prepare('SELECT * FROM journal_chat_state ORDER BY session_id').all(),
|
||||
items: db.prepare('SELECT * FROM journal_items ORDER BY session_id,item_id').all(),
|
||||
records: db.prepare('SELECT * FROM agent_session_records ORDER BY session_id').all()
|
||||
}))
|
||||
).toEqual(saved)
|
||||
expect(errors).toEqual([])
|
||||
})
|
||||
|
||||
it('opens an unconverted chat while background work waits for live activity, then converts the rest', async () => {
|
||||
const { database, errors } = await fixture(['a', 'b'])
|
||||
database.setSessionActive('live-chat', true)
|
||||
|
||||
@@ -14,6 +14,7 @@ import type { JournalWriterCommit } from '../journal-writer-changes'
|
||||
import type { JournalWriterEffect, JournalWriterEffectResult } from '../journal-writer-effects'
|
||||
import type { CommandReceiptScope } from '../writing/command-receipt-schema'
|
||||
import { AgentSessionJournalError } from '../writing/journal-write-guards'
|
||||
import { assertJournalChatReadable } from '../replay/journal-open-failure'
|
||||
import { journalDatabasePath } from './journal-host-database'
|
||||
import { journalDomainDatabasePath } from './journal-domain-database'
|
||||
import type { JournalSqlStoredRecords } from '../journal-sql-database-read'
|
||||
@@ -49,7 +50,8 @@ export class JournalSqlDatabase {
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
() => assertJournalChatReadable(this.readOnly)
|
||||
)
|
||||
this.conversions = new JournalSqlConversions(writer, this.readers, this.activity.waitForIdle)
|
||||
this.maintenance = new JournalSqlBackgroundMaintenance(
|
||||
@@ -193,7 +195,9 @@ export class JournalSqlDatabase {
|
||||
}
|
||||
|
||||
ensureSessionEpoch(sessionId: string): Promise<string | null> {
|
||||
return this.serialize(sessionId, () => this.conversions.ensure(sessionId))
|
||||
return this.readOnly
|
||||
? Promise.resolve(null)
|
||||
: this.serialize(sessionId, () => this.conversions.ensure(sessionId))
|
||||
}
|
||||
|
||||
openSession(
|
||||
@@ -202,6 +206,7 @@ export class JournalSqlDatabase {
|
||||
recover?: { fence: number }
|
||||
) {
|
||||
return this.serialize(identity.sessionId, async () => {
|
||||
assertJournalChatReadable(this.readOnly)
|
||||
let epoch = await this.conversions.ensure(identity.sessionId)
|
||||
if (epoch === null) {
|
||||
const founded = await this.writer.maintain({
|
||||
@@ -229,18 +234,16 @@ export class JournalSqlDatabase {
|
||||
this.conversions.rememberReady(identity.sessionId, epoch)
|
||||
}
|
||||
const target = { sessionId: identity.sessionId, epoch, ownerToken: randomUUID() }
|
||||
if (!this.readOnly) {
|
||||
try {
|
||||
const commit = await this.writer.write({
|
||||
target,
|
||||
operation: { kind: 'open', recover },
|
||||
savedAt
|
||||
})
|
||||
return { target, commit }
|
||||
} catch (error) {
|
||||
if (!this.writer.recordsPending) {
|
||||
throw error
|
||||
}
|
||||
try {
|
||||
const commit = await this.writer.write({
|
||||
target,
|
||||
operation: { kind: 'open', recover },
|
||||
savedAt
|
||||
})
|
||||
return { target, commit }
|
||||
} catch (error) {
|
||||
if (!this.writer.recordsPending) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
const head = await this.readers.point({
|
||||
|
||||
@@ -32,7 +32,8 @@ export class JournalSqlReaderPool {
|
||||
currentWorkerEntryLayout(__dirname),
|
||||
'journal-sql-reader-entry.js'
|
||||
),
|
||||
private readonly cacheOutline?: (cache: JournalOutlineCache) => void
|
||||
private readonly cacheOutline?: (cache: JournalOutlineCache) => void,
|
||||
private readonly assertReadable?: () => void
|
||||
) {}
|
||||
|
||||
async prepareFork(
|
||||
@@ -113,7 +114,7 @@ export class JournalSqlReaderPool {
|
||||
}
|
||||
|
||||
async database(operation: JournalSqlDatabaseRead): Promise<JournalSqlDatabaseReadResult> {
|
||||
const response = await this.slot().request({ kind: 'database', operation })
|
||||
const response = await this.slot(false, operation).request({ kind: 'database', operation })
|
||||
if (response.type !== 'database') {
|
||||
throw new Error('journal reader returned no database result')
|
||||
}
|
||||
@@ -133,10 +134,13 @@ export class JournalSqlReaderPool {
|
||||
return response.result
|
||||
}
|
||||
|
||||
private slot(forFork = false): ReaderSlot {
|
||||
private slot(forFork = false, databaseRead?: JournalSqlDatabaseRead): ReaderSlot {
|
||||
if (this.closed) {
|
||||
throw new Error('journal SQL reader pool is closed')
|
||||
}
|
||||
if (databaseRead?.kind !== 'records') {
|
||||
this.assertReadable?.()
|
||||
}
|
||||
for (let index = this.slots.length - 1; index >= 0; index--) {
|
||||
if (this.slots[index]!.failed) {
|
||||
const [failed] = this.slots.splice(index, 1)
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
} from '../../../../shared/agent-session/failures/agent-session-wire-refusals'
|
||||
import { isSqliteCorruption } from '../../../sqlite/sqlite-read-failure'
|
||||
import type { JournalLoad } from './journal-open'
|
||||
import { AgentSessionJournalError } from '../writing/journal-write-guards'
|
||||
import { AgentSessionJournalError, assertJournalWritable } from '../writing/journal-write-guards'
|
||||
import type { StructuredAgentSessionLogger } from '../../agent-session-wire/host/structured-agent-session-logger'
|
||||
|
||||
type JournalRefusalReason = AgentSessionRefusalReason<'agent_session_journal_unreadable'>
|
||||
@@ -73,6 +73,14 @@ const JOURNAL_OPEN_MESSAGE: Record<JournalRefusalReason, string> = {
|
||||
|
||||
export const JOURNAL_NEWER_SCHEMA_MESSAGE = JOURNAL_OPEN_MESSAGE.journalWrittenByNewerOrca
|
||||
|
||||
export function assertJournalChatReadable(readOnly: boolean): void {
|
||||
try {
|
||||
assertJournalWritable(readOnly, 'database')
|
||||
} catch (error) {
|
||||
throw journalOpenRefusalError(error)
|
||||
}
|
||||
}
|
||||
|
||||
export function isJournalWrittenByNewerOrca(error: unknown): boolean {
|
||||
return error instanceof AgentSessionJournalError && error.code === 'journal_read_only'
|
||||
}
|
||||
|
||||
@@ -389,6 +389,13 @@ describe('a chat tab over records a newer Orca wrote', () => {
|
||||
ok: true,
|
||||
result: { ok: true, readable: false }
|
||||
})
|
||||
expect(await host.revealSession(HOST_TEST_SESSION)).toMatchObject({
|
||||
readable: false,
|
||||
openRefusal: {
|
||||
code: 'agent_session_journal_unreadable',
|
||||
details: { reason: 'journalWrittenByNewerOrca' }
|
||||
}
|
||||
})
|
||||
|
||||
expect((await snapshot()).activeTabId).toBe(`agent-session:${HOST_TEST_SESSION}`)
|
||||
expect(warn).toHaveBeenCalledWith(
|
||||
|
||||
Reference in New Issue
Block a user