mirror of
https://github.com/stablyai/orca.git
synced 2026-11-18 03:45:08 +00:00
refactor: keep one current queue row with skinny edit holds
Queue cards live in one row per chat/epoch/message, updated in place. A content or state change advances the row revision; edit holds name that revision instead of copying body, fingerprint, state or consumed attempt. Moves read only ordering headers and update only positions, returning placements. Queued admission provenance is a column on the attempt, backfilled from the exact named former card version (missing => not personal), so card versions and source-card columns are removed.
This commit is contained in:
@@ -1,103 +0,0 @@
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import type { SqliteRow } from '../../sqlite/sqlite-statement'
|
||||
import { agentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key'
|
||||
import { agentSessionSendBodyFingerprint } from '../../../shared/agent-session/requests/structured-agent-session-send-mutation'
|
||||
import { structuredAgentSessionCommandTurnIdentity } from '../../../shared/agent-session/view/structured-agent-session-command-turn-identity'
|
||||
import type { QueuedMessageRow, QueuedMessageState } from './queued-messages/queued-message-table'
|
||||
|
||||
type Target = { sessionId: string; epoch: string }
|
||||
|
||||
/** A current card with the version key a replacement must address. */
|
||||
export type JournalDomainCard = {
|
||||
card: QueuedMessageRow
|
||||
version: { sequence: number; index: number }
|
||||
}
|
||||
|
||||
export const SELECT_JOURNAL_DOMAIN_CARD = `SELECT message_id, version_sequence, version_index,
|
||||
state, position, payload_json FROM journal_card_versions`
|
||||
|
||||
export function readJournalDomainCard(sessionId: string, row: SqliteRow): JournalDomainCard {
|
||||
const card: QueuedMessageRow = JSON.parse(String(row.payload_json))
|
||||
const position = row.position === null ? card.position : Number(row.position)
|
||||
return {
|
||||
card: {
|
||||
...card,
|
||||
messageId: String(row.message_id),
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the state column is only ever written from a QueuedMessageRow state.
|
||||
state: String(row.state) as QueuedMessageState,
|
||||
position,
|
||||
fingerprint: agentSessionSendBodyFingerprint(sessionId, card.body)
|
||||
},
|
||||
version: { sequence: Number(row.version_sequence), index: Number(row.version_index) }
|
||||
}
|
||||
}
|
||||
|
||||
export function journalDomainCurrentCard(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
messageId: string
|
||||
): JournalDomainCard | null {
|
||||
const row = db
|
||||
.prepare(
|
||||
`${SELECT_JOURNAL_DOMAIN_CARD} WHERE session_id=? AND epoch=? AND message_id=? AND current=1`
|
||||
)
|
||||
.get(target.sessionId, target.epoch, messageId)
|
||||
return row ? readJournalDomainCard(target.sessionId, row) : null
|
||||
}
|
||||
|
||||
function commandTurnItemId(card: QueuedMessageRow): string | null {
|
||||
return card.state === 'returned' && card.body.command && card.consumedAs !== null
|
||||
? agentJournalItemKey(structuredAgentSessionCommandTurnIdentity(card.consumedAs))
|
||||
: null
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy-replaces the addressed current version with `card` at (sequence, 0). The replaced version
|
||||
* stays only while a submission attempt names it as its source; it is never rewritten.
|
||||
*/
|
||||
export function writeJournalDomainCardVersion(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
prior: JournalDomainCard | null,
|
||||
card: QueuedMessageRow,
|
||||
sequence: number
|
||||
): void {
|
||||
const prefix = [target.sessionId, target.epoch] as const
|
||||
if (prior) {
|
||||
if (prior.card.messageId !== card.messageId || prior.version.sequence >= sequence) {
|
||||
throw new Error('journal card version must follow its addressed prior version')
|
||||
}
|
||||
const key = [card.messageId, prior.version.sequence, prior.version.index] as const
|
||||
const replaced = db
|
||||
.prepare(`UPDATE journal_card_versions SET current=0 WHERE session_id=? AND epoch=?
|
||||
AND message_id=? AND version_sequence=? AND version_index=? AND current=1`)
|
||||
.run(...prefix, ...key)
|
||||
if (replaced.changes !== 1) {
|
||||
throw new Error('journal card version was already replaced')
|
||||
}
|
||||
db.prepare(`DELETE FROM journal_card_versions WHERE session_id=? AND epoch=?
|
||||
AND message_id=? AND version_sequence=? AND version_index=?
|
||||
AND NOT EXISTS(SELECT 1 FROM journal_submission_attempts AS attempt
|
||||
WHERE attempt.session_id=? AND attempt.epoch=? AND attempt.source_card_sequence=?
|
||||
AND attempt.source_card_index=? AND json_extract(attempt.payload_json,'$.queuedMessageId')=?)`).run(
|
||||
...prefix,
|
||||
...key,
|
||||
...prefix,
|
||||
prior.version.sequence,
|
||||
prior.version.index,
|
||||
card.messageId
|
||||
)
|
||||
}
|
||||
const { fingerprint: _derived, ...payload } = card
|
||||
db.prepare(`INSERT INTO journal_card_versions(session_id,epoch,message_id,version_sequence,
|
||||
version_index,current,state,command_turn_item_id,payload_json,position)
|
||||
VALUES(?,?,?,?,0,1,?,?,?,?)`).run(
|
||||
...prefix,
|
||||
card.messageId,
|
||||
sequence,
|
||||
card.state,
|
||||
commandTurnItemId(card),
|
||||
JSON.stringify(payload),
|
||||
card.position
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* One current queue row per card, updated in place. `revision` advances on content or state
|
||||
* changes only, so an edit hold names it instead of copying the card. Admission provenance moves
|
||||
* onto the attempt from the exact card version it named; a missing version reads as not personal.
|
||||
* Runs once, after the queue position schema, inside the caller's migration transaction.
|
||||
*/
|
||||
export const JOURNAL_DOMAIN_CURRENT_QUEUE_SCHEMA = `
|
||||
CREATE TABLE journal_queue_cards (
|
||||
session_id TEXT NOT NULL,
|
||||
epoch TEXT NOT NULL,
|
||||
message_id TEXT NOT NULL,
|
||||
revision INTEGER NOT NULL,
|
||||
state TEXT NOT NULL,
|
||||
position REAL NOT NULL,
|
||||
command_turn_item_id TEXT,
|
||||
payload_json TEXT NOT NULL,
|
||||
PRIMARY KEY(session_id, epoch, message_id)
|
||||
) WITHOUT ROWID;
|
||||
INSERT INTO journal_queue_cards(session_id,epoch,message_id,revision,state,position,command_turn_item_id,payload_json)
|
||||
SELECT session_id, epoch, message_id, 1, state,
|
||||
COALESCE(position, json_extract(payload_json,'$.position'), 0), command_turn_item_id,
|
||||
json_remove(payload_json,'$.messageId','$.position','$.state','$.fingerprint')
|
||||
FROM journal_card_versions WHERE current=1;
|
||||
CREATE INDEX journal_queue_order ON journal_queue_cards(session_id, epoch, position, message_id)
|
||||
WHERE state IN ('waiting','returned');
|
||||
CREATE INDEX journal_queue_last_position ON journal_queue_cards(session_id, epoch, position);
|
||||
CREATE INDEX journal_queue_returned_commands ON journal_queue_cards(session_id, epoch, command_turn_item_id)
|
||||
WHERE state='returned';
|
||||
|
||||
ALTER TABLE journal_submission_attempts ADD COLUMN admitted_personal INTEGER NOT NULL DEFAULT 0
|
||||
CHECK(admitted_personal IN (0,1));
|
||||
UPDATE journal_submission_attempts SET admitted_personal=1
|
||||
WHERE json_type(payload_json,'$.queuedMessageId') IS NOT NULL
|
||||
AND EXISTS(SELECT 1 FROM journal_card_versions AS card
|
||||
WHERE card.session_id=journal_submission_attempts.session_id
|
||||
AND card.epoch=journal_submission_attempts.epoch
|
||||
AND card.message_id=json_extract(journal_submission_attempts.payload_json,'$.queuedMessageId')
|
||||
AND card.version_sequence=journal_submission_attempts.source_card_sequence
|
||||
AND card.version_index=journal_submission_attempts.source_card_index
|
||||
AND COALESCE(json_extract(card.payload_json,'$.unattended'),0)<>1
|
||||
AND json_type(card.payload_json,'$.body.from') IS NULL);
|
||||
DROP INDEX IF EXISTS journal_submission_source_card;
|
||||
ALTER TABLE journal_submission_attempts DROP COLUMN source_card_sequence;
|
||||
ALTER TABLE journal_submission_attempts DROP COLUMN source_card_index;
|
||||
|
||||
CREATE TABLE journal_edit_holds (
|
||||
session_id TEXT NOT NULL,
|
||||
epoch TEXT NOT NULL,
|
||||
owner_id TEXT NOT NULL,
|
||||
caller_key TEXT NOT NULL,
|
||||
edit_id TEXT NOT NULL,
|
||||
message_id TEXT NOT NULL,
|
||||
card_revision INTEGER NOT NULL,
|
||||
expires_at REAL NOT NULL,
|
||||
PRIMARY KEY(session_id, epoch, owner_id, caller_key, edit_id)
|
||||
) WITHOUT ROWID;
|
||||
INSERT INTO journal_edit_holds(session_id,epoch,owner_id,caller_key,edit_id,message_id,card_revision,expires_at)
|
||||
SELECT lease.session_id, lease.epoch, lease.owner_id, lease.caller_key, lease.edit_id,
|
||||
lease.message_id, card.revision, lease.expires_at
|
||||
FROM journal_edit_leases AS lease
|
||||
JOIN journal_card_versions AS former ON former.session_id=lease.session_id AND former.epoch=lease.epoch
|
||||
AND former.message_id=lease.message_id AND former.current=1 AND former.state=lease.state
|
||||
AND json_extract(former.payload_json,'$.consumedAs') IS lease.consumed_as
|
||||
JOIN journal_queue_cards AS card ON card.session_id=lease.session_id AND card.epoch=lease.epoch
|
||||
AND card.message_id=lease.message_id;
|
||||
DROP TABLE journal_edit_leases;
|
||||
ALTER TABLE journal_edit_holds RENAME TO journal_edit_leases;
|
||||
CREATE INDEX journal_edit_leases_message ON journal_edit_leases(session_id, epoch, message_id);
|
||||
CREATE INDEX journal_edit_leases_deadline ON journal_edit_leases(session_id, epoch, expires_at);
|
||||
|
||||
DROP TABLE journal_card_versions;
|
||||
`
|
||||
@@ -2,12 +2,11 @@ import type Database from '../../sqlite/sync-database'
|
||||
import type { SqliteRow } from '../../sqlite/sqlite-statement'
|
||||
import type { AgentSessionQueuedMessageEditHoldResult } from '../../../shared/agent-session/requests/agent-session-queued-message-wire'
|
||||
import { agentSessionSendBodyFingerprint } from '../../../shared/agent-session/requests/structured-agent-session-send-mutation'
|
||||
import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import {
|
||||
leaseMatchesRow,
|
||||
QUEUED_MESSAGE_EDIT_LEASE_MS,
|
||||
type EditLeaseKey
|
||||
} from './queued-messages/queued-message-edit-leases'
|
||||
import type { QueuedMessageRow } from './queued-messages/queued-message-table'
|
||||
|
||||
type Target = { sessionId: string; epoch: string }
|
||||
export type JournalDomainEditLeaseOperation =
|
||||
@@ -23,15 +22,13 @@ export type JournalDomainEditLeaseOutcome = {
|
||||
nextDeadline: number | null
|
||||
}
|
||||
|
||||
const SELECT_LEASE_CARDS = `SELECT
|
||||
lease.owner_id, lease.caller_key, lease.edit_id, lease.message_id,
|
||||
lease.fingerprint, lease.state, lease.consumed_as, lease.expires_at,
|
||||
card.state AS current_state, card.payload_json
|
||||
FROM journal_edit_leases AS lease
|
||||
LEFT JOIN journal_card_versions AS card
|
||||
ON card.session_id=lease.session_id AND card.epoch=lease.epoch
|
||||
AND card.message_id=lease.message_id AND card.current=1
|
||||
WHERE lease.session_id=? AND lease.epoch=?`
|
||||
/** A hold lives while its card is unsettled at the content/state revision it was taken on. */
|
||||
const LIVE_CARD = `card.session_id=lease.session_id AND card.epoch=lease.epoch
|
||||
AND card.message_id=lease.message_id AND card.revision=lease.card_revision
|
||||
AND card.state IN ('waiting','returned')`
|
||||
|
||||
const LIVE_HOLDS = `FROM journal_edit_leases AS lease JOIN journal_queue_cards AS card ON ${LIVE_CARD}
|
||||
WHERE lease.session_id=? AND lease.epoch=? AND lease.expires_at>?`
|
||||
|
||||
function requireWallClock(now: number): void {
|
||||
if (!Number.isFinite(now)) {
|
||||
@@ -39,43 +36,6 @@ function requireWallClock(now: number): void {
|
||||
}
|
||||
}
|
||||
|
||||
function currentCard(target: Target, messageId: string, row: SqliteRow): QueuedMessageRow | null {
|
||||
if (
|
||||
row.payload_json === null ||
|
||||
(row.current_state !== 'waiting' && row.current_state !== 'returned')
|
||||
) {
|
||||
return null
|
||||
}
|
||||
const card: QueuedMessageRow = JSON.parse(String(row.payload_json))
|
||||
return {
|
||||
...card,
|
||||
messageId,
|
||||
state: row.current_state,
|
||||
fingerprint: agentSessionSendBodyFingerprint(target.sessionId, card.body)
|
||||
}
|
||||
}
|
||||
|
||||
function leaseIsCurrent(target: Target, row: SqliteRow, now: number): boolean {
|
||||
if (Number(row.expires_at) <= now) {
|
||||
return false
|
||||
}
|
||||
const messageId = String(row.message_id)
|
||||
const card = currentCard(target, messageId, row)
|
||||
return (
|
||||
card !== null &&
|
||||
leaseMatchesRow(
|
||||
{
|
||||
messageId,
|
||||
fingerprint: String(row.fingerprint),
|
||||
state: row.state === 'waiting' ? 'waiting' : 'returned',
|
||||
consumedAs: row.consumed_as === null ? null : String(row.consumed_as)
|
||||
},
|
||||
card
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/** One joined statement keeps lease validity and the current card in the caller's snapshot. */
|
||||
export function journalDomainMessageEditHeld(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
@@ -83,14 +43,11 @@ export function journalDomainMessageEditHeld(
|
||||
now: number
|
||||
): boolean {
|
||||
requireWallClock(now)
|
||||
for (const row of db
|
||||
.prepare(`${SELECT_LEASE_CARDS} AND lease.message_id=? AND lease.expires_at>?`)
|
||||
.iterate(target.sessionId, target.epoch, messageId, now)) {
|
||||
if (leaseIsCurrent(target, row, now)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return (
|
||||
db
|
||||
.prepare(`SELECT 1 ${LIVE_HOLDS} AND lease.message_id=? LIMIT 1`)
|
||||
.get(target.sessionId, target.epoch, now, messageId) !== undefined
|
||||
)
|
||||
}
|
||||
|
||||
export function journalDomainHeldMessageIds(
|
||||
@@ -99,15 +56,11 @@ export function journalDomainHeldMessageIds(
|
||||
now: number
|
||||
): string[] {
|
||||
requireWallClock(now)
|
||||
const held = new Set<string>()
|
||||
for (const row of db
|
||||
.prepare(`${SELECT_LEASE_CARDS} AND lease.expires_at>? ORDER BY lease.expires_at`)
|
||||
.iterate(target.sessionId, target.epoch, now)) {
|
||||
if (leaseIsCurrent(target, row, now)) {
|
||||
held.add(String(row.message_id))
|
||||
}
|
||||
}
|
||||
return [...held]
|
||||
return db
|
||||
.prepare(`SELECT lease.message_id ${LIVE_HOLDS}
|
||||
GROUP BY lease.message_id ORDER BY MIN(lease.expires_at), lease.message_id`)
|
||||
.all(target.sessionId, target.epoch, now)
|
||||
.map((row) => String(row.message_id))
|
||||
}
|
||||
|
||||
export function journalDomainEditLeaseDeadline(
|
||||
@@ -116,25 +69,42 @@ export function journalDomainEditLeaseDeadline(
|
||||
now: number
|
||||
): number | null {
|
||||
requireWallClock(now)
|
||||
for (const row of db
|
||||
.prepare(`${SELECT_LEASE_CARDS} AND lease.expires_at>? ORDER BY lease.expires_at`)
|
||||
.iterate(target.sessionId, target.epoch, now)) {
|
||||
if (leaseIsCurrent(target, row, now)) {
|
||||
return Number(row.expires_at)
|
||||
}
|
||||
}
|
||||
return null
|
||||
const row = db
|
||||
.prepare(`SELECT MIN(lease.expires_at) AS deadline ${LIVE_HOLDS}`)
|
||||
.get(target.sessionId, target.epoch, now)
|
||||
return row?.deadline === null || row?.deadline === undefined ? null : Number(row.deadline)
|
||||
}
|
||||
|
||||
function deletePair(db: Database.Database, target: Target, row: SqliteRow): boolean {
|
||||
/** Content, state or consumed-attempt changes end every hold on these cards. */
|
||||
export function retireJournalDomainEditHolds(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
messageIds: readonly string[]
|
||||
): boolean {
|
||||
const retire = db.prepare(
|
||||
'DELETE FROM journal_edit_leases WHERE session_id=? AND epoch=? AND message_id=?'
|
||||
)
|
||||
let changed = false
|
||||
for (const messageId of new Set(messageIds)) {
|
||||
changed = retire.run(target.sessionId, target.epoch, messageId).changes > 0 || changed
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
function deletePair(db: Database.Database, target: Target, ownerId: string, key: EditLeaseKey) {
|
||||
return (
|
||||
db
|
||||
.prepare(`DELETE FROM journal_edit_leases
|
||||
WHERE session_id=? AND epoch=? AND owner_id=? AND caller_key=? AND edit_id=?`)
|
||||
.run(target.sessionId, target.epoch, row.owner_id, row.caller_key, row.edit_id).changes > 0
|
||||
.run(target.sessionId, target.epoch, ownerId, key.callerKey, key.editId).changes > 0
|
||||
)
|
||||
}
|
||||
|
||||
function bodyFingerprint(target: Target, row: SqliteRow): string {
|
||||
const card: { body: AgentJournalMessageItem } = JSON.parse(String(row.payload_json))
|
||||
return agentSessionSendBodyFingerprint(target.sessionId, card.body)
|
||||
}
|
||||
|
||||
function heldResult(fingerprint: string, deadline: number, now: number) {
|
||||
return {
|
||||
status: 'held' as const,
|
||||
@@ -152,14 +122,16 @@ function acquireOrRenew(
|
||||
now: number
|
||||
): Pick<JournalDomainEditLeaseOutcome, 'result' | 'changed'> {
|
||||
const existing = db
|
||||
.prepare(
|
||||
`${SELECT_LEASE_CARDS} AND lease.owner_id=? AND lease.caller_key=? AND lease.edit_id=?`
|
||||
)
|
||||
.get(target.sessionId, target.epoch, ownerId, operation.callerKey, operation.editId)
|
||||
const valid = existing !== undefined && leaseIsCurrent(target, existing, now)
|
||||
.prepare(`SELECT lease.message_id, lease.expires_at, card.payload_json,
|
||||
card.message_id IS NOT NULL AND lease.expires_at>? AS live
|
||||
FROM journal_edit_leases AS lease LEFT JOIN journal_queue_cards AS card ON ${LIVE_CARD}
|
||||
WHERE lease.session_id=? AND lease.epoch=? AND lease.owner_id=? AND lease.caller_key=?
|
||||
AND lease.edit_id=?`)
|
||||
.get(now, target.sessionId, target.epoch, ownerId, operation.callerKey, operation.editId)
|
||||
const valid = existing !== undefined && Number(existing.live) === 1
|
||||
let changed = false
|
||||
if (existing && !valid) {
|
||||
changed = deletePair(db, target, existing)
|
||||
changed = deletePair(db, target, ownerId, operation)
|
||||
}
|
||||
if (operation.kind === 'renew') {
|
||||
if (!valid || existing.message_id !== operation.messageId) {
|
||||
@@ -178,45 +150,44 @@ function acquireOrRenew(
|
||||
)
|
||||
changed = true
|
||||
}
|
||||
return { result: heldResult(String(existing.fingerprint), deadline, now), changed }
|
||||
return { result: heldResult(bodyFingerprint(target, existing), deadline, now), changed }
|
||||
}
|
||||
if (valid) {
|
||||
const fingerprint =
|
||||
existing.message_id === operation.messageId ? bodyFingerprint(target, existing) : null
|
||||
return {
|
||||
result:
|
||||
existing.message_id === operation.messageId &&
|
||||
existing.fingerprint === operation.fingerprint
|
||||
? heldResult(String(existing.fingerprint), Number(existing.expires_at), now)
|
||||
fingerprint === operation.fingerprint
|
||||
? heldResult(fingerprint, Number(existing.expires_at), now)
|
||||
: { status: 'changed' },
|
||||
changed
|
||||
}
|
||||
}
|
||||
const row = db
|
||||
.prepare(`SELECT state AS current_state, payload_json FROM journal_card_versions
|
||||
WHERE session_id=? AND epoch=? AND message_id=? AND current=1`)
|
||||
const card = db
|
||||
.prepare(`SELECT revision, payload_json FROM journal_queue_cards
|
||||
WHERE session_id=? AND epoch=? AND message_id=? AND state IN ('waiting','returned')`)
|
||||
.get(target.sessionId, target.epoch, operation.messageId)
|
||||
const card = row ? currentCard(target, operation.messageId, row) : null
|
||||
if (!card) {
|
||||
return { result: { status: 'gone' }, changed }
|
||||
}
|
||||
if (card.fingerprint !== operation.fingerprint) {
|
||||
const fingerprint = bodyFingerprint(target, card)
|
||||
if (fingerprint !== operation.fingerprint) {
|
||||
return { result: { status: 'changed' }, changed }
|
||||
}
|
||||
const deadline = now + QUEUED_MESSAGE_EDIT_LEASE_MS
|
||||
db.prepare(`INSERT INTO journal_edit_leases
|
||||
(session_id,epoch,owner_id,caller_key,edit_id,message_id,fingerprint,state,consumed_as,expires_at)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?)`).run(
|
||||
(session_id,epoch,owner_id,caller_key,edit_id,message_id,card_revision,expires_at)
|
||||
VALUES(?,?,?,?,?,?,?,?)`).run(
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
ownerId,
|
||||
operation.callerKey,
|
||||
operation.editId,
|
||||
card.messageId,
|
||||
card.fingerprint,
|
||||
card.state,
|
||||
card.consumedAs,
|
||||
operation.messageId,
|
||||
card.revision,
|
||||
deadline
|
||||
)
|
||||
return { result: heldResult(card.fingerprint, deadline, now), changed: true }
|
||||
return { result: heldResult(fingerprint, deadline, now), changed: true }
|
||||
}
|
||||
|
||||
/** The caller owns the writer transaction and publishes these facts only after COMMIT. */
|
||||
@@ -254,11 +225,7 @@ export function applyJournalDomainEditLease(
|
||||
result = { status: 'released' }
|
||||
break
|
||||
case 'retire-card':
|
||||
changed =
|
||||
db
|
||||
.prepare(`DELETE FROM journal_edit_leases
|
||||
WHERE session_id=? AND epoch=? AND message_id=?`)
|
||||
.run(target.sessionId, target.epoch, operation.messageId).changes > 0
|
||||
changed = retireJournalDomainEditHolds(db, target, [operation.messageId])
|
||||
break
|
||||
case 'release-owner':
|
||||
changed =
|
||||
@@ -268,11 +235,12 @@ export function applyJournalDomainEditLease(
|
||||
.run(target.sessionId, target.epoch, ownerId).changes > 0
|
||||
break
|
||||
case 'expire':
|
||||
for (const row of db.prepare(SELECT_LEASE_CARDS).all(target.sessionId, target.epoch)) {
|
||||
if (!leaseIsCurrent(target, row, now)) {
|
||||
changed = deletePair(db, target, row) || changed
|
||||
}
|
||||
}
|
||||
changed =
|
||||
db
|
||||
.prepare(`DELETE FROM journal_edit_leases AS lease
|
||||
WHERE session_id=? AND epoch=? AND (expires_at<=?
|
||||
OR NOT EXISTS(SELECT 1 FROM journal_queue_cards AS card WHERE ${LIVE_CARD}))`)
|
||||
.run(target.sessionId, target.epoch, now).changes > 0
|
||||
break
|
||||
}
|
||||
return { result, changed, nextDeadline: journalDomainEditLeaseDeadline(db, target, now) }
|
||||
|
||||
@@ -8,14 +8,18 @@ import type {
|
||||
} from '../../../shared/agent-session/requests/agent-session-queued-message-wire'
|
||||
import { agentSessionSendBodyFingerprint } from '../../../shared/agent-session/requests/structured-agent-session-send-mutation'
|
||||
import { claimAgentSessionAttachmentsInTransaction } from '../agent-session-attachments/agent-session-attachment-claims'
|
||||
import { applyJournalDomainEditLease } from './journal-domain-edit-leases'
|
||||
import {
|
||||
journalDomainCurrentCard,
|
||||
readJournalDomainCard,
|
||||
SELECT_JOURNAL_DOMAIN_CARD,
|
||||
writeJournalDomainCardVersion,
|
||||
type JournalDomainCard
|
||||
} from './journal-domain-card-versions'
|
||||
journalDomainEditLeaseDeadline,
|
||||
retireJournalDomainEditHolds
|
||||
} from './journal-domain-edit-leases'
|
||||
import {
|
||||
holdJournalDomainQueueRow,
|
||||
insertJournalDomainQueueRow,
|
||||
journalDomainQueueRow,
|
||||
placeJournalDomainQueueRow,
|
||||
replaceJournalDomainQueueRow,
|
||||
type JournalDomainQueueRow
|
||||
} from './journal-domain-queue-rows'
|
||||
import { appendJournalSqlEvent } from './journal-sql-event-tail'
|
||||
import type { JournalSqlQueueWrite } from './journal-sql-queued-messages'
|
||||
import { queuedMessageMovePlan } from './queued-messages/queued-message-move'
|
||||
@@ -36,10 +40,14 @@ export type JournalDomainQueueResult =
|
||||
| { kind: 'queue-withdraw'; cards: QueuedMessageRow[] }
|
||||
| { kind: 'queue-move'; move: AgentSessionQueuedMessageMoveResult }
|
||||
|
||||
export type JournalDomainQueuePlacement = { messageId: string; position: number }
|
||||
|
||||
export type JournalDomainQueueCommit = {
|
||||
cursor: AgentJournalCursor
|
||||
result: JournalDomainQueueResult
|
||||
cards: QueuedMessageRow[]
|
||||
/** A move changes only these positions; card content stays as last published. */
|
||||
placements: JournalDomainQueuePlacement[]
|
||||
inputChanged: boolean
|
||||
/** Present only when this command retired edit leases. */
|
||||
editDeadline?: number | null
|
||||
@@ -47,7 +55,12 @@ export type JournalDomainQueueCommit = {
|
||||
referencedUploads?: string[]
|
||||
}
|
||||
|
||||
type Change = { prior: JournalDomainCard | null; card: QueuedMessageRow }
|
||||
type Change = {
|
||||
prior: JournalDomainQueueRow | null
|
||||
card: QueuedMessageRow
|
||||
/** Hold reasons are neither content nor state, so they keep edit holds. */
|
||||
holdOnly?: boolean
|
||||
}
|
||||
|
||||
/** Runs only on the owning writer connection, inside its command transaction or SAVEPOINT. */
|
||||
export function applyJournalDomainQueueCommand(
|
||||
@@ -67,7 +80,7 @@ export function applyJournalDomainQueueCommand(
|
||||
throw new Error('journal queue command targets a retired chat epoch')
|
||||
}
|
||||
const cursor = { epoch: target.epoch, sequence: Number(head.last_sequence) }
|
||||
const current = (messageId: string) => journalDomainCurrentCard(db, target, messageId)
|
||||
const current = (messageId: string) => journalDomainQueueRow(db, target, messageId)
|
||||
const claim = (
|
||||
body: AgentJournalMessageItem,
|
||||
required: boolean,
|
||||
@@ -84,6 +97,7 @@ export function applyJournalDomainQueueCommand(
|
||||
]
|
||||
let result: JournalDomainQueueResult
|
||||
let changes: Change[] = []
|
||||
let placements: JournalDomainQueuePlacement[] = []
|
||||
let retire: string[] = []
|
||||
let referencedUploads: string[] | undefined
|
||||
switch (operation.kind) {
|
||||
@@ -97,8 +111,8 @@ export function applyJournalDomainQueueCommand(
|
||||
}
|
||||
referencedUploads = claim(input.body, input.requireAttachments === true)
|
||||
const last = db
|
||||
.prepare(`SELECT MAX(position) AS position FROM journal_card_versions
|
||||
WHERE session_id=? AND epoch=? AND current=1`)
|
||||
.prepare(`SELECT MAX(position) AS position FROM journal_queue_cards
|
||||
WHERE session_id=? AND epoch=?`)
|
||||
.get(target.sessionId, target.epoch)
|
||||
const options = readAgentSessionExecutionOptions(input.options)
|
||||
const card: QueuedMessageRow = {
|
||||
@@ -147,7 +161,7 @@ export function applyJournalDomainQueueCommand(
|
||||
case 'queue-hold':
|
||||
changes = addressed(operation.messageIds, current).flatMap((prior) =>
|
||||
prior.card.state === 'waiting' && prior.card.holdReason !== operation.reason
|
||||
? [{ prior, card: { ...prior.card, holdReason: operation.reason } }]
|
||||
? [{ prior, card: { ...prior.card, holdReason: operation.reason }, holdOnly: true }]
|
||||
: []
|
||||
)
|
||||
result = { kind: 'queue-hold' }
|
||||
@@ -162,22 +176,21 @@ export function applyJournalDomainQueueCommand(
|
||||
result = { kind: 'queue-withdraw', cards: changes.map(({ card }) => card) }
|
||||
break
|
||||
case 'queue-move': {
|
||||
// Only a move reads the ordered unsettled set; dispatched cards keep their reserved slots.
|
||||
// Only a move reads the ordered unsettled headers; dispatched cards keep their reserved slots.
|
||||
const unsettled = db
|
||||
.prepare(`${SELECT_JOURNAL_DOMAIN_CARD} WHERE session_id=? AND epoch=? AND current=1
|
||||
AND state IN ('waiting','returned') ORDER BY position, message_id`)
|
||||
.prepare(`SELECT message_id, state, position FROM journal_queue_cards
|
||||
WHERE session_id=? AND epoch=? AND state IN ('waiting','returned')
|
||||
ORDER BY position, message_id`)
|
||||
.all(target.sessionId, target.epoch)
|
||||
.map((row) => readJournalDomainCard(target.sessionId, row))
|
||||
const plan = queuedMessageMovePlan(
|
||||
unsettled.map(({ card }) => card),
|
||||
operation.input
|
||||
.map((row) => ({
|
||||
messageId: String(row.message_id),
|
||||
state: row.state === 'returned' ? ('returned' as const) : ('waiting' as const),
|
||||
position: Number(row.position)
|
||||
}))
|
||||
const plan = queuedMessageMovePlan(unsettled, operation.input)
|
||||
placements = plan.changes.flatMap(({ messageId, change }) =>
|
||||
change?.op === 'moved' ? [{ messageId, position: change.position }] : []
|
||||
)
|
||||
changes = plan.changes.flatMap(({ messageId, change }) => {
|
||||
const prior = unsettled.find(({ card }) => card.messageId === messageId)
|
||||
return prior && change?.op === 'moved'
|
||||
? [{ prior, card: { ...prior.card, position: change.position } }]
|
||||
: []
|
||||
})
|
||||
result = { kind: 'queue-move', move: plan.result }
|
||||
break
|
||||
}
|
||||
@@ -186,32 +199,28 @@ export function applyJournalDomainQueueCommand(
|
||||
cursor,
|
||||
result,
|
||||
cards: changes.map(({ card }) => card),
|
||||
inputChanged: changes.length > 0,
|
||||
placements,
|
||||
inputChanged: changes.length > 0 || placements.length > 0,
|
||||
...(referencedUploads?.length ? { referencedUploads } : {})
|
||||
}
|
||||
let leasesChanged = false
|
||||
let editDeadline: number | null = null
|
||||
for (const messageId of retire) {
|
||||
const retired = applyJournalDomainEditLease(
|
||||
db,
|
||||
target,
|
||||
'',
|
||||
{ kind: 'retire-card', messageId },
|
||||
savedAt
|
||||
)
|
||||
leasesChanged ||= retired.changed
|
||||
editDeadline = retired.nextDeadline
|
||||
}
|
||||
if (leasesChanged) {
|
||||
commit.editDeadline = editDeadline
|
||||
}
|
||||
if (changes.length === 0) {
|
||||
if (!commit.inputChanged) {
|
||||
retireHolds(db, target, retire, commit, savedAt)
|
||||
return commit
|
||||
}
|
||||
const sequence = cursor.sequence + 1
|
||||
for (const { prior, card } of changes) {
|
||||
writeJournalDomainCardVersion(db, target, prior, card, sequence)
|
||||
for (const { prior, card, holdOnly } of changes) {
|
||||
if (!prior) {
|
||||
insertJournalDomainQueueRow(db, target, card)
|
||||
} else if (holdOnly) {
|
||||
holdJournalDomainQueueRow(db, target, prior, card.holdReason)
|
||||
} else {
|
||||
replaceJournalDomainQueueRow(db, target, prior, card)
|
||||
}
|
||||
}
|
||||
for (const { messageId, position } of placements) {
|
||||
placeJournalDomainQueueRow(db, target, messageId, position)
|
||||
}
|
||||
retireHolds(db, target, retire, commit, savedAt)
|
||||
db.prepare('UPDATE journal_chat_state SET last_sequence=? WHERE session_id=? AND epoch=?').run(
|
||||
sequence,
|
||||
target.sessionId,
|
||||
@@ -220,17 +229,29 @@ export function applyJournalDomainQueueCommand(
|
||||
appendJournalSqlEvent(db, target, sequence, savedAt, {
|
||||
items: [],
|
||||
submissions: [],
|
||||
cards: commit.cards.map((card) => card.messageId)
|
||||
cards: [...commit.cards, ...placements].map((card) => card.messageId)
|
||||
})
|
||||
commit.cursor = { epoch: target.epoch, sequence }
|
||||
return commit
|
||||
}
|
||||
|
||||
function retireHolds(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
messageIds: readonly string[],
|
||||
commit: JournalDomainQueueCommit,
|
||||
now: number
|
||||
): void {
|
||||
if (retireJournalDomainEditHolds(db, target, messageIds)) {
|
||||
commit.editDeadline = journalDomainEditLeaseDeadline(db, target, now)
|
||||
}
|
||||
}
|
||||
|
||||
/** Each addressed card at most once, in the caller's order. */
|
||||
function addressed(
|
||||
messageIds: readonly string[],
|
||||
current: (messageId: string) => JournalDomainCard | null
|
||||
): JournalDomainCard[] {
|
||||
current: (messageId: string) => JournalDomainQueueRow | null
|
||||
): JournalDomainQueueRow[] {
|
||||
return [...new Set(messageIds)].flatMap((messageId) => {
|
||||
const card = current(messageId)
|
||||
return card ? [card] : []
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import type { SqliteRow } from '../../sqlite/sqlite-statement'
|
||||
import { agentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key'
|
||||
import { agentSessionSendBodyFingerprint } from '../../../shared/agent-session/requests/structured-agent-session-send-mutation'
|
||||
import { structuredAgentSessionCommandTurnIdentity } from '../../../shared/agent-session/view/structured-agent-session-command-turn-identity'
|
||||
import type { QueuedMessageRow, QueuedMessageState } from './queued-messages/queued-message-table'
|
||||
|
||||
type Target = { sessionId: string; epoch: string }
|
||||
|
||||
/** The current card and the content/state revision an edit hold or replacement addresses. */
|
||||
export type JournalDomainQueueRow = { card: QueuedMessageRow; revision: number }
|
||||
|
||||
export const SELECT_JOURNAL_DOMAIN_QUEUE_ROW = `SELECT message_id, revision, state, position,
|
||||
payload_json FROM journal_queue_cards`
|
||||
|
||||
export function readJournalDomainQueueRow(
|
||||
sessionId: string,
|
||||
row: SqliteRow
|
||||
): JournalDomainQueueRow {
|
||||
const card: QueuedMessageRow = JSON.parse(String(row.payload_json))
|
||||
return {
|
||||
card: {
|
||||
...card,
|
||||
messageId: String(row.message_id),
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the state column is only ever written from a QueuedMessageRow state.
|
||||
state: String(row.state) as QueuedMessageState,
|
||||
position: Number(row.position),
|
||||
fingerprint: agentSessionSendBodyFingerprint(sessionId, card.body)
|
||||
},
|
||||
revision: Number(row.revision)
|
||||
}
|
||||
}
|
||||
|
||||
export function journalDomainQueueRow(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
messageId: string
|
||||
): JournalDomainQueueRow | null {
|
||||
const row = db
|
||||
.prepare(`${SELECT_JOURNAL_DOMAIN_QUEUE_ROW} WHERE session_id=? AND epoch=? AND message_id=?`)
|
||||
.get(target.sessionId, target.epoch, messageId)
|
||||
return row ? readJournalDomainQueueRow(target.sessionId, row) : null
|
||||
}
|
||||
|
||||
/** Whether an attempt admitted from this selected current card counts as the person's own input. */
|
||||
export function journalDomainAdmittedPersonal(
|
||||
card: Pick<QueuedMessageRow, 'body' | 'unattended'>
|
||||
): boolean {
|
||||
return card.unattended !== true && card.body.from === undefined
|
||||
}
|
||||
|
||||
function commandTurnItemId(card: QueuedMessageRow): string | null {
|
||||
return card.state === 'returned' && card.body.command && card.consumedAs !== null
|
||||
? agentJournalItemKey(structuredAgentSessionCommandTurnIdentity(card.consumedAs))
|
||||
: null
|
||||
}
|
||||
|
||||
function payload(card: QueuedMessageRow): string {
|
||||
const {
|
||||
messageId: _id,
|
||||
position: _position,
|
||||
state: _state,
|
||||
fingerprint: _derived,
|
||||
...rest
|
||||
} = card
|
||||
return JSON.stringify(rest)
|
||||
}
|
||||
|
||||
export function insertJournalDomainQueueRow(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
card: QueuedMessageRow
|
||||
): void {
|
||||
db.prepare(`INSERT INTO journal_queue_cards(session_id,epoch,message_id,revision,state,position,
|
||||
command_turn_item_id,payload_json) VALUES(?,?,?,1,?,?,?,?)`).run(
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
card.messageId,
|
||||
card.state,
|
||||
card.position,
|
||||
commandTurnItemId(card),
|
||||
payload(card)
|
||||
)
|
||||
}
|
||||
|
||||
/** Rewrites content or state in place; the next revision ends edit holds on the addressed one. */
|
||||
export function replaceJournalDomainQueueRow(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
prior: JournalDomainQueueRow,
|
||||
card: QueuedMessageRow
|
||||
): void {
|
||||
if (prior.card.messageId !== card.messageId || prior.card.position !== card.position) {
|
||||
throw new Error('journal queue replacement must keep its card and placement')
|
||||
}
|
||||
const replaced = db
|
||||
.prepare(`UPDATE journal_queue_cards SET revision=revision+1, state=?, command_turn_item_id=?,
|
||||
payload_json=? WHERE session_id=? AND epoch=? AND message_id=? AND revision=?`)
|
||||
.run(
|
||||
card.state,
|
||||
commandTurnItemId(card),
|
||||
payload(card),
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
card.messageId,
|
||||
prior.revision
|
||||
)
|
||||
if (replaced.changes !== 1) {
|
||||
throw new Error('journal queue card was already replaced')
|
||||
}
|
||||
}
|
||||
|
||||
/** A hold reason is neither content nor state, so edit holds survive it. */
|
||||
export function holdJournalDomainQueueRow(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
prior: JournalDomainQueueRow,
|
||||
holdReason: string | null
|
||||
): void {
|
||||
db.prepare(`UPDATE journal_queue_cards SET payload_json=json_set(payload_json,'$.holdReason',?)
|
||||
WHERE session_id=? AND epoch=? AND message_id=? AND revision=?`).run(
|
||||
holdReason,
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
prior.card.messageId,
|
||||
prior.revision
|
||||
)
|
||||
}
|
||||
|
||||
/** Placement only: no body, revision or edit hold changes. */
|
||||
export function placeJournalDomainQueueRow(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
messageId: string,
|
||||
position: number
|
||||
): void {
|
||||
db.prepare(`UPDATE journal_queue_cards SET position=?
|
||||
WHERE session_id=? AND epoch=? AND message_id=? AND state IN ('waiting','returned')`).run(
|
||||
position,
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
messageId
|
||||
)
|
||||
}
|
||||
@@ -12,7 +12,13 @@ import {
|
||||
} from '../agent-session-attachments/agent-session-attachment-claims'
|
||||
import { agentSessionAttachmentStoreRoot } from '../agent-session-attachments/agent-session-attachment-references'
|
||||
import { migrateJournalDomainSchema } from './journal-domain-schema'
|
||||
import { applyJournalDomainEditLease } from './journal-domain-edit-leases'
|
||||
import { JOURNAL_DOMAIN_CURRENT_QUEUE_SCHEMA } from './journal-domain-current-queue-schema'
|
||||
import {
|
||||
applyJournalDomainEditLease,
|
||||
journalDomainMessageEditHeld
|
||||
} from './journal-domain-edit-leases'
|
||||
import { journalDomainQueueRow } from './journal-domain-queue-rows'
|
||||
import { readJournalSqlAcceptedInputs } from './journal-sql-accepted-inputs'
|
||||
import {
|
||||
applyJournalDomainQueueCommand,
|
||||
type JournalDomainQueueOperation
|
||||
@@ -39,20 +45,14 @@ function fixture() {
|
||||
rmSync(stateDirectory, { recursive: true, force: true })
|
||||
})
|
||||
migrateJournalDomainSchema(db)
|
||||
// Temporary until the central ordered migration list runs this body.
|
||||
db.exec(JOURNAL_DOMAIN_CURRENT_QUEUE_SCHEMA)
|
||||
ensureAgentSessionAttachmentClaimTables(db)
|
||||
db.prepare(`INSERT INTO journal_chat_state(session_id,epoch,last_sequence,highest_fence)
|
||||
VALUES('chat','epoch',10,1)`).run()
|
||||
const seed = (
|
||||
messageId: string,
|
||||
position: number,
|
||||
fields: Partial<QueuedMessageRow> = {},
|
||||
version = 1
|
||||
) => {
|
||||
const card = {
|
||||
messageId,
|
||||
position,
|
||||
const seed = (messageId: string, position: number, fields: Partial<QueuedMessageRow> = {}) => {
|
||||
const { state = 'waiting', ...card } = {
|
||||
body: text(messageId),
|
||||
state: 'waiting',
|
||||
holdReason: null,
|
||||
returnedReason: null,
|
||||
returnedRejection: null,
|
||||
@@ -60,13 +60,12 @@ function fixture() {
|
||||
queuedAt: { epoch: 'epoch', sequence: 1 },
|
||||
...fields
|
||||
}
|
||||
db.prepare(`INSERT INTO journal_card_versions(session_id,epoch,message_id,version_sequence,
|
||||
version_index,current,state,payload_json,position) VALUES('chat','epoch',?,?,0,1,?,?,?)`).run(
|
||||
db.prepare(`INSERT INTO journal_queue_cards(session_id,epoch,message_id,revision,state,position,
|
||||
payload_json) VALUES('chat','epoch',?,1,?,?,?)`).run(
|
||||
messageId,
|
||||
version,
|
||||
card.state,
|
||||
JSON.stringify(card),
|
||||
position
|
||||
state,
|
||||
position,
|
||||
JSON.stringify(card)
|
||||
)
|
||||
}
|
||||
const run = (operation: JournalDomainQueueOperation) => {
|
||||
@@ -109,22 +108,14 @@ function fixture() {
|
||||
.prepare('SELECT sequence, changes_json FROM journal_events ORDER BY sequence')
|
||||
.all()
|
||||
.map((row) => [row.sequence, JSON.parse(String(row.changes_json)).cards])
|
||||
const versions = (messageId: string) =>
|
||||
db
|
||||
.prepare(`SELECT version_sequence, current, state, position, payload_json FROM journal_card_versions
|
||||
WHERE message_id=? ORDER BY version_sequence`)
|
||||
.all(messageId)
|
||||
.map((row) => {
|
||||
const payload = JSON.parse(String(row.payload_json))
|
||||
return [row.version_sequence, row.current, row.state, row.position, payload.body.blocks]
|
||||
})
|
||||
const card = (messageId: string) => journalDomainQueueRow(db, target, messageId)
|
||||
const held = (messageId: string) => journalDomainMessageEditHeld(db, target, messageId, now)
|
||||
const order = () =>
|
||||
db
|
||||
.prepare(`SELECT message_id, position FROM journal_card_versions WHERE current=1
|
||||
ORDER BY position, message_id`)
|
||||
.prepare('SELECT message_id, position FROM journal_queue_cards ORDER BY position, message_id')
|
||||
.all()
|
||||
.map((row) => [row.message_id, row.position])
|
||||
return { db, stateDirectory, seed, run, lease, leases, head, events, versions, order }
|
||||
return { db, stateDirectory, seed, run, lease, leases, head, events, card, held, order }
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
@@ -133,7 +124,7 @@ afterEach(() => {
|
||||
}
|
||||
})
|
||||
|
||||
it('admits a card once and keeps a replaced version only while an attempt names it', () => {
|
||||
it('admits a card once and edits its one current row in place', () => {
|
||||
const queue = fixture()
|
||||
queue.seed('earlier', 4, { state: 'withdrawn' })
|
||||
const first = queue.run({
|
||||
@@ -157,11 +148,6 @@ it('admits a card once and keeps a replaced version only while an attempt names
|
||||
})
|
||||
expect(queue.events()).toEqual([[11, ['card']]])
|
||||
|
||||
queue.db
|
||||
.prepare(`INSERT INTO journal_submission_attempts(session_id,epoch,message_id,attempt_sequence,
|
||||
attempt_index,current,item_id,ordinal,submitted_at,fingerprint,dispatch_state,source_card_sequence,
|
||||
source_card_index,payload_json) VALUES('chat','epoch','send',12,0,1,'item',12,1,'f','pending',11,0,?)`)
|
||||
.run(JSON.stringify({ clientMessageId: 'send', queuedMessageId: 'card' }))
|
||||
const edit = (expected: string, value: string) =>
|
||||
queue.run({
|
||||
kind: 'queue-update',
|
||||
@@ -169,10 +155,17 @@ it('admits a card once and keeps a replaced version only while an attempt names
|
||||
})
|
||||
edit(agentSessionSendBodyFingerprint('chat', text('first')), 'edited once')
|
||||
edit(agentSessionSendBodyFingerprint('chat', text('edited once')), 'edited twice')
|
||||
expect(queue.versions('card')).toEqual([
|
||||
[11, 0, 'waiting', 5, [{ type: 'text', text: 'first' }]],
|
||||
[13, 1, 'waiting', 5, [{ type: 'text', text: 'edited twice' }]]
|
||||
expect(queue.order()).toEqual([
|
||||
['earlier', 4],
|
||||
['card', 5]
|
||||
])
|
||||
expect(queue.card('card')?.card).toMatchObject({
|
||||
position: 5,
|
||||
state: 'waiting',
|
||||
body: text('edited twice'),
|
||||
deliveryMode: 'queue',
|
||||
unattended: false
|
||||
})
|
||||
})
|
||||
|
||||
it('answers a stale or repeated edit without a version and refuses a swept new upload atomically', () => {
|
||||
@@ -231,10 +224,11 @@ it('answers a stale or repeated edit without a version and refuses a swept new u
|
||||
}
|
||||
expect(isAgentSessionAttachmentExpiredError(refusal)).toBe(true)
|
||||
expect(queue.head()).toBe(11)
|
||||
expect(queue.versions('card').filter(([, current]) => current === 1)).toEqual([
|
||||
[11, 1, 'waiting', 1, [{ type: 'text', text: 'look again' }, image(UPLOAD_A)]]
|
||||
expect(queue.card('card')?.card.body.blocks).toEqual([
|
||||
{ type: 'text', text: 'look again' },
|
||||
image(UPLOAD_A)
|
||||
])
|
||||
expect(queue.leases()).toEqual(['card'])
|
||||
expect(queue.held('card')).toBe(true)
|
||||
expect(queue.db.prepare('SELECT upload_id FROM agent_session_attachment_claims').all()).toEqual([
|
||||
{ upload_id: UPLOAD_A }
|
||||
])
|
||||
@@ -246,14 +240,25 @@ it('moves among unsettled cards only, keeping dispatched slots and id ties', ()
|
||||
queue.seed('b', 2, { state: 'dispatched', consumedAs: 'send-b' })
|
||||
queue.seed('c', 3, { state: 'returned', returnedReason: 'refused' })
|
||||
queue.seed('y', 4)
|
||||
queue.seed('x', 4)
|
||||
queue.seed('x', 4, { deliveryMode: 'steer', unattended: true })
|
||||
queue.lease('a')
|
||||
queue.lease('x')
|
||||
const before = queue.card('x')!
|
||||
|
||||
const moved = queue.run({
|
||||
kind: 'queue-move',
|
||||
input: { messageId: 'x', beforeMessageId: 'a' }
|
||||
})
|
||||
expect(moved.result).toEqual({ kind: 'queue-move', move: { messageId: 'x', status: 'moved' } })
|
||||
expect(moved.placements).toEqual([
|
||||
{ messageId: 'x', position: 1 },
|
||||
{ messageId: 'a', position: 3 },
|
||||
{ messageId: 'c', position: 4 }
|
||||
])
|
||||
// Placement keeps the editor's authority, body and delivery facts.
|
||||
expect(queue.card('x')).toEqual({ ...before, card: { ...before.card, position: 1 } })
|
||||
expect(queue.held('x')).toBe(true)
|
||||
expect(queue.held('a')).toBe(true)
|
||||
expect(queue.order()).toEqual([
|
||||
['x', 1],
|
||||
['b', 2],
|
||||
@@ -262,7 +267,6 @@ it('moves among unsettled cards only, keeping dispatched slots and id ties', ()
|
||||
['y', 4]
|
||||
])
|
||||
expect(queue.events()).toEqual([[11, ['x', 'a', 'c']]])
|
||||
expect(queue.leases()).toEqual(['a'])
|
||||
expect(moved.editDeadline).toBeUndefined()
|
||||
|
||||
const stale = queue.run({ kind: 'queue-move', input: { messageId: 'b', beforeMessageId: 'a' } })
|
||||
@@ -282,7 +286,7 @@ it('holds waiting cards and withdraws unsettled ones, ending only invalidated ed
|
||||
expect(held.cards.map((card) => [card.messageId, card.holdReason])).toEqual([
|
||||
['a', 'send_failed']
|
||||
])
|
||||
expect(queue.leases()).toEqual(['a', 'c'])
|
||||
expect([queue.held('a'), queue.held('c')]).toEqual([true, true])
|
||||
const again = queue.run({ kind: 'queue-hold', messageIds: ['a'], reason: 'send_failed' })
|
||||
expect(again).toMatchObject({ cursor: { sequence: 11 }, cards: [], inputChanged: false })
|
||||
|
||||
@@ -297,5 +301,74 @@ it('holds waiting cards and withdraws unsettled ones, ending only invalidated ed
|
||||
[11, ['a']],
|
||||
[12, ['c']]
|
||||
])
|
||||
expect(queue.versions('b')).toEqual([[1, 1, 'dispatched', 2, [{ type: 'text', text: 'b' }]]])
|
||||
expect(queue.card('b')?.card).toMatchObject({ state: 'dispatched', position: 2, body: text('b') })
|
||||
})
|
||||
|
||||
it('migrates current cards in place and snapshots admission provenance from the named card', () => {
|
||||
const db = new Database(':memory:')
|
||||
cleanups.push(() => db.close())
|
||||
migrateJournalDomainSchema(db)
|
||||
const version = (
|
||||
messageId: string,
|
||||
sequence: number,
|
||||
current: number,
|
||||
fields: Partial<QueuedMessageRow> = {}
|
||||
) => {
|
||||
const card = { messageId, position: 2, body: text(messageId), state: 'waiting', ...fields }
|
||||
db.prepare(`INSERT INTO journal_card_versions(session_id,epoch,message_id,version_sequence,
|
||||
version_index,current,state,payload_json,position) VALUES('chat','epoch',?,?,0,?,?,?,?)`).run(
|
||||
messageId,
|
||||
sequence,
|
||||
current,
|
||||
card.state,
|
||||
JSON.stringify(card),
|
||||
card.position
|
||||
)
|
||||
}
|
||||
// Mail admitted from its original card, which a later edit attributed to the user.
|
||||
version('mail', 3, 0, {
|
||||
body: { ...text('mail'), from: { kind: 'agent', senders: [], orchestration: null } }
|
||||
})
|
||||
version('mail', 5, 1, { body: text('edited mail') })
|
||||
version('mine', 4, 1, { deliveryMode: 'steer', unattended: false })
|
||||
version('robot', 6, 1, { unattended: true })
|
||||
const attempt = db.prepare(`INSERT INTO journal_submission_attempts(session_id,epoch,message_id,
|
||||
attempt_sequence,attempt_index,current,item_id,ordinal,submitted_at,accepted_sequence,fingerprint,
|
||||
dispatch_state,source_card_sequence,source_card_index,payload_json)
|
||||
VALUES('chat','epoch',?,?,0,1,?,?,1,?,'f','accepted',?,0,?)`)
|
||||
const admit = (sendId: string, accepted: number, card: string, cardSequence: number | null) =>
|
||||
attempt.run(
|
||||
sendId,
|
||||
accepted,
|
||||
`item-${sendId}`,
|
||||
accepted,
|
||||
accepted,
|
||||
cardSequence,
|
||||
JSON.stringify({ clientMessageId: sendId, queuedMessageId: card })
|
||||
)
|
||||
admit('send-mine', 7, 'mine', 4)
|
||||
admit('send-robot', 8, 'robot', 6)
|
||||
admit('send-mail', 9, 'mail', 3)
|
||||
admit('send-lost', 10, 'mail', 1)
|
||||
db.prepare(`INSERT INTO journal_edit_leases(session_id,epoch,owner_id,caller_key,edit_id,message_id,
|
||||
fingerprint,state,consumed_as,expires_at) VALUES('chat','epoch','handle','phone','edit','mine','f',
|
||||
'waiting',NULL,?)`).run(now + 60_000)
|
||||
|
||||
db.exec('BEGIN IMMEDIATE')
|
||||
db.exec(JOURNAL_DOMAIN_CURRENT_QUEUE_SCHEMA)
|
||||
db.exec('COMMIT')
|
||||
|
||||
const accepted = readJournalSqlAcceptedInputs(db, ['chat', 'epoch'])
|
||||
expect(accepted).toEqual({ latestAcceptedTurnSequence: 10, latestPersonAcceptedSequence: 7 })
|
||||
expect(journalDomainQueueRow(db, target, 'mail')?.card).toMatchObject({
|
||||
messageId: 'mail',
|
||||
position: 2,
|
||||
state: 'waiting',
|
||||
body: text('edited mail')
|
||||
})
|
||||
expect(journalDomainQueueRow(db, target, 'mine')?.card).toMatchObject({
|
||||
deliveryMode: 'steer',
|
||||
unattended: false
|
||||
})
|
||||
expect(journalDomainMessageEditHeld(db, target, 'mine', now)).toBe(true)
|
||||
})
|
||||
|
||||
@@ -5,7 +5,7 @@ export type JournalSqlAcceptedInputs = {
|
||||
latestPersonAcceptedSequence: number
|
||||
}
|
||||
|
||||
/** Admission provenance names its original card version, never a later edited card. */
|
||||
/** Queued admissions read the provenance their attempt snapshotted, never a later edited card. */
|
||||
export function readJournalSqlAcceptedInputs(
|
||||
db: Database.Database,
|
||||
prefix: readonly [string, string]
|
||||
@@ -17,13 +17,9 @@ export function readJournalSqlAcceptedInputs(
|
||||
.get(...prefix)
|
||||
const person = db
|
||||
.prepare(`SELECT attempt.accepted_sequence FROM journal_submission_attempts AS attempt
|
||||
LEFT JOIN journal_card_versions AS card ON card.session_id=attempt.session_id AND card.epoch=attempt.epoch
|
||||
AND card.message_id=json_extract(attempt.payload_json,'$.queuedMessageId')
|
||||
AND card.version_sequence=attempt.source_card_sequence AND card.version_index=attempt.source_card_index
|
||||
WHERE attempt.session_id=? AND attempt.epoch=? AND attempt.dispatch_state='accepted' AND attempt.accepted_sequence IS NOT NULL
|
||||
AND CASE WHEN json_type(attempt.payload_json,'$.queuedMessageId') IS NOT NULL
|
||||
THEN card.message_id IS NOT NULL AND COALESCE(json_extract(card.payload_json,'$.unattended'),0)<>1
|
||||
AND json_type(card.payload_json,'$.body.from') IS NULL
|
||||
THEN attempt.admitted_personal=1
|
||||
ELSE json_extract(attempt.payload_json,'$.source.kind')='user'
|
||||
OR (json_type(attempt.payload_json,'$.source') IS NULL AND json_extract(attempt.payload_json,'$.origin')='client') END
|
||||
ORDER BY attempt.accepted_sequence DESC LIMIT 1`)
|
||||
|
||||
@@ -2,6 +2,7 @@ import { afterEach, expect, it } from 'vitest'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import type { QueuedMessageRow } from './queued-messages/queued-message-table'
|
||||
import { migrateJournalDomainSchema } from './journal-domain-schema'
|
||||
import { JOURNAL_DOMAIN_CURRENT_QUEUE_SCHEMA } from './journal-domain-current-queue-schema'
|
||||
import {
|
||||
applyJournalDomainEditLease,
|
||||
journalDomainEditLeaseDeadline,
|
||||
@@ -21,9 +22,11 @@ function fixture() {
|
||||
const db = new Database(':memory:')
|
||||
databases.push(db)
|
||||
migrateJournalDomainSchema(db)
|
||||
let version = 0
|
||||
// Temporary until the central ordered migration list runs this body.
|
||||
db.exec(JOURNAL_DOMAIN_CURRENT_QUEUE_SCHEMA)
|
||||
// Each seed is a content or state write on the one current row, as the queue writers make.
|
||||
const seed = (fields: Partial<QueuedMessageRow> = {}) => {
|
||||
const card: QueuedMessageRow = {
|
||||
const { messageId, state, position, ...card }: QueuedMessageRow = {
|
||||
messageId: 'card',
|
||||
position: 1,
|
||||
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'hello' }] },
|
||||
@@ -36,20 +39,15 @@ function fixture() {
|
||||
queuedAt: null,
|
||||
...fields
|
||||
}
|
||||
db.prepare(`UPDATE journal_card_versions SET current=0
|
||||
WHERE session_id=? AND epoch=? AND message_id=? AND current=1`).run(
|
||||
db.prepare(`INSERT INTO journal_queue_cards
|
||||
(session_id,epoch,message_id,revision,state,position,payload_json) VALUES(?,?,?,1,?,?,?)
|
||||
ON CONFLICT(session_id,epoch,message_id) DO UPDATE SET revision=revision+1,
|
||||
state=excluded.state, payload_json=excluded.payload_json`).run(
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
card.messageId
|
||||
)
|
||||
db.prepare(`INSERT INTO journal_card_versions
|
||||
(session_id,epoch,message_id,version_sequence,version_index,current,state,payload_json)
|
||||
VALUES(?,?,?,?,0,1,?,?)`).run(
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
card.messageId,
|
||||
++version,
|
||||
card.state,
|
||||
messageId,
|
||||
state,
|
||||
position,
|
||||
JSON.stringify(card)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -154,10 +154,6 @@ it('resets old cursors at the replay count and metadata budgets while preserving
|
||||
for (let index = 0; index < 300; index++) {
|
||||
const messageId = `returned-command-${index}`.padEnd(64, 'x')
|
||||
const card = {
|
||||
messageId,
|
||||
position: index,
|
||||
state: 'returned',
|
||||
fingerprint: 'command',
|
||||
body: { kind: 'message', role: 'user', blocks: [], command: { name: 'compact' } },
|
||||
holdReason: null,
|
||||
returnedReason: 'refused',
|
||||
@@ -166,9 +162,9 @@ it('resets old cursors at the replay count and metadata budgets while preserving
|
||||
queuedAt: null
|
||||
}
|
||||
db.prepare(
|
||||
`INSERT INTO journal_card_versions(session_id,epoch,message_id,version_sequence,version_index,current,state,command_turn_item_id,payload_json)
|
||||
VALUES('chat','epoch',?,1,0,1,'returned','orca:command',?)`
|
||||
).run(messageId, JSON.stringify(card))
|
||||
`INSERT INTO journal_queue_cards(session_id,epoch,message_id,revision,state,position,command_turn_item_id,payload_json)
|
||||
VALUES('chat','epoch',?,1,'returned',?,'orca:command',?)`
|
||||
).run(messageId, index, JSON.stringify(card))
|
||||
}
|
||||
await commit(message('wide withdrawal remains saved'))
|
||||
expect(after(4107)).toMatchObject({ ok: false, reset: 'cursor_compacted' })
|
||||
@@ -176,11 +172,7 @@ it('resets old cursors at the replay count and metadata budgets while preserving
|
||||
body: { blocks: [{ text: 'wide withdrawal remains saved' }] }
|
||||
})
|
||||
expect(
|
||||
db
|
||||
.prepare(
|
||||
"SELECT COUNT(*) AS count FROM journal_card_versions WHERE current=1 AND state='withdrawn'"
|
||||
)
|
||||
.get()
|
||||
db.prepare("SELECT COUNT(*) AS count FROM journal_queue_cards WHERE state='withdrawn'").get()
|
||||
).toEqual({ count: 300 })
|
||||
expect(
|
||||
db.prepare('SELECT COUNT(*) AS count FROM journal_events').get()!.count
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import type { SqliteRow } from '../../sqlite/sqlite-statement'
|
||||
import type {
|
||||
AgentJournalSubmission,
|
||||
AgentJournalAcceptanceReceipt
|
||||
@@ -7,11 +6,11 @@ import type {
|
||||
import type { JournalItemRow } from './durable-rows/journal-row-schema'
|
||||
import type { QueuedMessageRow } from './queued-messages/queued-message-table'
|
||||
import { journalDispatchRowApplies } from './projection/journal-dispatch-settlement'
|
||||
import { readJournalDomainQueueRow } from './journal-domain-queue-rows'
|
||||
import {
|
||||
readJournalDomainCard,
|
||||
writeJournalDomainCardVersion
|
||||
} from './journal-domain-card-versions'
|
||||
import { applyJournalDomainEditLease } from './journal-domain-edit-leases'
|
||||
journalDomainEditLeaseDeadline,
|
||||
retireJournalDomainEditHolds
|
||||
} from './journal-domain-edit-leases'
|
||||
|
||||
export function acceptJournalSqlMessageEcho(
|
||||
db: Database.Database,
|
||||
@@ -61,31 +60,26 @@ export function acceptJournalSqlMessageEcho(
|
||||
return receipt
|
||||
}
|
||||
|
||||
/** Spends returned commands whose reported turn is saved, in place on their current rows. */
|
||||
export function spendJournalSqlCommandCards(
|
||||
db: Database.Database,
|
||||
prefix: readonly [string, string],
|
||||
sequence: number,
|
||||
returned: readonly SqliteRow[],
|
||||
savedAt: number
|
||||
): { cards: QueuedMessageRow[]; editDeadline?: number | null } {
|
||||
const cards: QueuedMessageRow[] = []
|
||||
const target = { sessionId: prefix[0], epoch: prefix[1] }
|
||||
let leasesChanged = false
|
||||
let editDeadline: number | null = null
|
||||
for (const row of returned) {
|
||||
const prior = readJournalDomainCard(target.sessionId, row)
|
||||
const card: QueuedMessageRow = { ...prior.card, state: 'withdrawn' }
|
||||
writeJournalDomainCardVersion(db, target, prior, card, sequence)
|
||||
const retired = applyJournalDomainEditLease(
|
||||
db,
|
||||
target,
|
||||
'',
|
||||
{ kind: 'retire-card', messageId: card.messageId },
|
||||
savedAt
|
||||
)
|
||||
leasesChanged ||= retired.changed
|
||||
editDeadline = retired.nextDeadline
|
||||
cards.push(card)
|
||||
}
|
||||
return { cards, ...(leasesChanged ? { editDeadline } : {}) }
|
||||
const cards = db
|
||||
.prepare(`UPDATE journal_queue_cards AS card SET state='withdrawn', revision=revision+1,
|
||||
command_turn_item_id=NULL
|
||||
WHERE session_id=? AND epoch=? AND state='returned' AND EXISTS(SELECT 1 FROM journal_items AS item
|
||||
WHERE item.session_id=card.session_id AND item.epoch=card.epoch AND item.item_id=card.command_turn_item_id)
|
||||
RETURNING message_id, revision, state, position, payload_json`)
|
||||
.all(...prefix)
|
||||
.map((row) => readJournalDomainQueueRow(target.sessionId, row).card)
|
||||
return retireJournalDomainEditHolds(
|
||||
db,
|
||||
target,
|
||||
cards.map((card) => card.messageId)
|
||||
)
|
||||
? { cards, editDeadline: journalDomainEditLeaseDeadline(db, target, savedAt) }
|
||||
: { cards }
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
import { agentJournalSubmissionKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key'
|
||||
import { agentSessionSendBodyFingerprint } from '../../../shared/agent-session/requests/structured-agent-session-send-mutation'
|
||||
import { migrateJournalDomainSchema } from './journal-domain-schema'
|
||||
import { JOURNAL_DOMAIN_CURRENT_QUEUE_SCHEMA } from './journal-domain-current-queue-schema'
|
||||
import { journalDomainHistoryMetadata } from './journal-domain-history-metadata'
|
||||
import { JournalSqlBatch } from './journal-sql-batch'
|
||||
import {
|
||||
@@ -33,6 +34,8 @@ export function message(
|
||||
export function messageFixture(db = new Database(':memory:')) {
|
||||
db.pragma('user_version=4')
|
||||
migrateJournalDomainSchema(db)
|
||||
// Temporary until the central ordered migration list runs this body.
|
||||
db.exec(JOURNAL_DOMAIN_CURRENT_QUEUE_SCHEMA)
|
||||
db.prepare(
|
||||
`INSERT INTO journal_chat_state(session_id,epoch,last_sequence,highest_fence) VALUES(?,?,10,1)`
|
||||
).run(target.sessionId, target.epoch)
|
||||
|
||||
@@ -96,7 +96,7 @@ it('accepts an optimistic echo once, preserving its local body and moved placeme
|
||||
const { db, commit, item, seedSend, seedItem } = fixture()
|
||||
seedSend()
|
||||
db.exec(`INSERT INTO journal_submission_attempts
|
||||
SELECT session_id,epoch,message_id,0,0,0,item_id,0,0,NULL,fingerprint,'unknown',0,NULL,NULL,payload_json
|
||||
SELECT session_id,epoch,message_id,0,0,0,item_id,0,0,NULL,fingerprint,'unknown',0,payload_json,0
|
||||
FROM journal_submission_attempts WHERE current=1`)
|
||||
const historical = db
|
||||
.prepare('SELECT payload_json FROM journal_submission_attempts WHERE current=0')
|
||||
@@ -110,11 +110,7 @@ it('accepts an optimistic echo once, preserving its local body and moved placeme
|
||||
turnScope: { kind: 'thread' }
|
||||
})
|
||||
const card = {
|
||||
messageId: 'card',
|
||||
position: 1,
|
||||
body: { kind: 'message', role: 'user', blocks: [], command: { name: 'compact' } },
|
||||
fingerprint: 'card',
|
||||
state: 'returned',
|
||||
holdReason: null,
|
||||
returnedReason: 'refused',
|
||||
returnedRejection: null,
|
||||
@@ -122,8 +118,8 @@ it('accepts an optimistic echo once, preserving its local body and moved placeme
|
||||
queuedAt: null
|
||||
}
|
||||
db.prepare(
|
||||
`INSERT INTO journal_card_versions(session_id,epoch,message_id,version_sequence,version_index,current,state,command_turn_item_id,payload_json)
|
||||
VALUES('chat','epoch','card',1,0,1,'returned','orca:reported-command',?)`
|
||||
`INSERT INTO journal_queue_cards(session_id,epoch,message_id,revision,state,position,command_turn_item_id,payload_json)
|
||||
VALUES('chat','epoch','card',1,'returned',1,'orca:reported-command',?)`
|
||||
).run(JSON.stringify(card))
|
||||
const input = message('original', 'unused', 'item')
|
||||
input.identity = { provider: 'codex', threadId: 'thread', turnId: 'turn', ordinal: 0 }
|
||||
@@ -158,9 +154,6 @@ it('accepts an optimistic echo once, preserving its local body and moved placeme
|
||||
db.prepare('SELECT payload_json FROM journal_submission_attempts WHERE current=0').get()!
|
||||
.payload_json
|
||||
).toBe(historical)
|
||||
expect(
|
||||
db.prepare('SELECT payload_json FROM journal_card_versions WHERE current=0').get()
|
||||
).toBeUndefined()
|
||||
expect(
|
||||
db.prepare('SELECT candidate FROM journal_submission_attempts WHERE current=1').get()!.candidate
|
||||
).toBe(0)
|
||||
|
||||
@@ -175,12 +175,7 @@ export function applyJournalSqlMessage(
|
||||
}
|
||||
const rendered = journalRenderItem(itemId, revision, row.body, row, scope)
|
||||
const next = previous ? journalItemRevisionValue(previous, rendered) : rendered
|
||||
const returned = db
|
||||
.prepare(`SELECT card.message_id, card.version_sequence, card.version_index, card.state,
|
||||
card.position, card.payload_json FROM journal_card_versions AS card
|
||||
JOIN journal_items AS item ON item.session_id=card.session_id AND item.epoch=card.epoch AND item.item_id=card.command_turn_item_id
|
||||
WHERE card.session_id=? AND card.epoch=? AND card.current=1 AND card.state='returned'`)
|
||||
.all(...prefix)
|
||||
const spent = spendJournalSqlCommandCards(db, prefix, savedAt)
|
||||
const result: JournalSqlMessageCommit = {
|
||||
cursor: { epoch, sequence },
|
||||
outcome: { kind: 'committed', wrote: true },
|
||||
@@ -235,7 +230,7 @@ export function applyJournalSqlMessage(
|
||||
result.acceptedInputs = readJournalSqlAcceptedInputs(db, prefix)
|
||||
}
|
||||
}
|
||||
Object.assign(result, spendJournalSqlCommandCards(db, prefix, sequence, returned, savedAt))
|
||||
Object.assign(result, spent)
|
||||
result.inputChanged ||= result.cards.length > 0
|
||||
if (isJournalProviderActivity(row.recovered, rawId, row.body)) {
|
||||
db.prepare(`INSERT INTO journal_provider_activity(session_id,epoch,fence,last_activity_at) VALUES(?,?,?,?)
|
||||
|
||||
@@ -5,9 +5,12 @@ import type {
|
||||
import type { JournalQueueCardChange } from './journal-queue-events'
|
||||
import { isUnsettledQueuedMessage, type QueuedMessageRow } from './queued-message-table'
|
||||
|
||||
/** The ordering headers a move reads; bodies never take part in placement. */
|
||||
export type QueuedMessageMoveHeader = Pick<QueuedMessageRow, 'messageId' | 'position' | 'state'>
|
||||
|
||||
/** Permute unsettled positions without changing sender, enqueue/pause/hold facts or edit leases. */
|
||||
export function queuedMessageMovePlan(
|
||||
cards: readonly QueuedMessageRow[],
|
||||
cards: readonly QueuedMessageMoveHeader[],
|
||||
input: { messageId: string } & AgentSessionQueuedMessageMoveAnchor
|
||||
): { changes: JournalQueueCardChange[]; result: AgentSessionQueuedMessageMoveResult } {
|
||||
const unsettled = cards.filter(isUnsettledQueuedMessage)
|
||||
|
||||
Reference in New Issue
Block a user