Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
439cf2c
Keep failed sends visible in conversation on unary RPC transport errors
Fei2411 Dec 4, 2026
2977d47
Harden failed-send reconciliation, retry locking, and RPC delivery cl…
Fei2411 Dec 4, 2026
31173b5
Merge remote-tracking branch 'origin/dev' into fix/2299-rework-r2
Fei2411 Dec 4, 2026
e7210f1
Fix group rpcId receipts, legacy replay dedup, queue durability, send…
Fei2411 Dec 4, 2026
92d7ea6
Require durable requestId receipts, retry failed-send lazy chunk, and…
Fei2411 Dec 4, 2026
07d3b4f
Merge remote-tracking branch 'origin/dev' into fix/2299-rework-r2
Fei2411 Dec 4, 2026
5bbf720
Persist group ledger before inbox dequeue, unify ledger durability ev…
Fei2411 Dec 4, 2026
609fc66
Merge remote-tracking branch 'origin/dev' into fix/2299-rework-r5
Fei2411 Dec 5, 2026
e4a33f7
Hand off group inbox-to-ledger ownership atomically across DSH turns …
Fei2411 Dec 5, 2026
b45c381
Harden group inbox handover, pending dispatch recovery, and pre-step …
Fei2411 Dec 5, 2026
4e5832a
Merge origin/dev into fix/2299-rework-r5 and regenerate contracts
Fei2411 Dec 5, 2026
8006526
Merge remote-tracking branch 'origin/dev' into fix/2299-rework-r5
Fei2411 Dec 5, 2026
ec53081
Deduplicate ledger group dispatches on member receipt and persist sto…
Fei2411 Dec 5, 2026
d3de5df
Enforce cancellation and fail-closed receipt verification at group le…
Fei2411 Dec 5, 2026
ff2d62b
Harden parked room ledger wake recheck and add backoff retry for unre…
Fei2411 Dec 5, 2026
912e502
Release room ledger recovery claim and retry after opening round failure
Fei2411 Dec 5, 2026
89b74c3
Require persisted member receipt before marking group ledger dispatched
Fei2411 Dec 5, 2026
75d7ad0
Require persisted native user/message before room supersede or member…
Fei2411 Dec 5, 2026
8138b46
Share ledger dispatch identity and persist stop across provisional ro…
Fei2411 Dec 5, 2026
0736a27
fix(ds-bot): decouple room supersede from member dispatch and bind pe…
Fei2411 Dec 5, 2026
91e14da
fix(ds-bot): hand off orphaned roomInbox posts on native turn exit an…
Fei2411 Dec 5, 2026
db37d00
fix(ds-bot): guarantee ledger prompt text, persist before roomInbox d…
Fei2411 Dec 5, 2026
5f9b1bc
fix(ds-bot): reconstruct failed ledger handovers per session, persist…
Fei2411 Dec 5, 2026
5db17ab
fix(groups): reconstruct rolled-back native turns from persisted even…
Fei2411 Dec 5, 2026
57d3603
fix(groups): handle unstarted/unknown relay crashes with identity ded…
Fei2411 Dec 5, 2026
41bc542
fix(groups): recover unfinished async group_relay rounds per member a…
Fei2411 Dec 5, 2026
9d7c403
fix(groups): bind round ownership to exact anchors/identities and res…
Fei2411 Dec 5, 2026
87d9570
fix(groups): enforce per-message userIdentities over shared relay anchor
Fei2411 Dec 5, 2026
30951cc
fix(groups): preserve persisted session-reference snapshots across ro…
Fei2411 Dec 5, 2026
ba19e94
fix(groups): isolate reference rounds bidirectionally and resolve eve…
Fei2411 Dec 5, 2026
a01e1cb
fix(groups): enforce unified reference-round isolation across drainRo…
Fei2411 Dec 5, 2026
ca440f2
fix(groups): hand over orphaned laterPosts user entries when ledger r…
Fei2411 Dec 5, 2026
8586269
fix(groups): bind reconstructed laterPosts user entries in-place to p…
Fei2411 Dec 5, 2026
1d5d037
fix(groups): preserve bound laterPosts FIFO across consecutive openin…
Fei2411 Dec 5, 2026
9a31eb0
fix(groups): hold FIFO behind unready head and cancel in-flight Bot c…
Fei2411 Dec 5, 2026
8be78b9
fix(groups): hold partial head FIFO, abort opening before call creati…
Fei2411 Dec 5, 2026
85d9ab9
fix(groups): enforce pending-head barrier, drain bot-only suffix, and…
Fei2411 Dec 5, 2026
7c1652c
fix(ds-bot): enforce pending-head FIFO on postToRoom/holdForNextRound…
Fei2411 Dec 5, 2026
d742f7b
fix(ds-bot): guard partial in-flight heads, keep completed room claim…
Fei2411 Dec 5, 2026
d6dc092
fix(ds-bot): tighten room handoff admission, cold FIFO recovery, and …
Fei2411 Dec 6, 2026
2006932
fix(ds-bot): preserve in-flight and cold-rebuild room FIFO barriers a…
Fei2411 Dec 6, 2026
3d49a4c
fix(ds-bot): guard concurrent unpersisted rebuilds and abort mixed st…
Fei2411 Dec 6, 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
Prev Previous commit
Next Next commit
Persist group ledger before inbox dequeue, unify ledger durability ev…
…idence, and sync HostRuntime types
  • Loading branch information
Fei2411 committed Dec 4, 2026
commit 5bbf720c161973ba02760655e21f5540f9502aa2
104 changes: 91 additions & 13 deletions ds-bot/src/host/bot-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,37 @@ export function install(rt) {
noteSessionAdmittedRpc(sessionId, rpcId)
}

const noteRoomLedgerRpcDurable = (roomId, sessionId, rpcId) => {
if (typeof rpcId !== 'string') return
const trimmed = rpcId.trim()
if (trimmed === '') return
const sid = typeof sessionId === 'string' && sessionId !== ''
? sessionId
: (typeof roomId === 'string' && roomId !== '' ? rt.sessionOfRoom?.(rt.roomOf(roomId)) : undefined)
if (typeof sid === 'string' && sid !== '') {
noteAdmittedRpc(sid, trimmed, { durable: true, userMessage: true })
}
rt.publishFeed?.('send-confirmed', {
requestId: trimmed,
...(typeof sid === 'string' && sid !== '' ? { sessionId: sid } : {}),
...(typeof roomId === 'string' && roomId !== '' ? { chatId: roomId } : {}),
})
}

const isUserMessageDurable = (sessionId, rpcId, { liveFrame = false } = {}) => {
if (typeof sessionId !== 'string' || sessionId === '' || typeof rpcId !== 'string' || rpcId === '') return false
const owner = logicalOwnerOf(sessionId)
if (owner?.roomId !== undefined && typeof rt.findRoomLedgerRpc === 'function') {
const ledgerHit = rt.findRoomLedgerRpc(owner.roomId, rpcId)
if (ledgerHit?.found === true) {
return !liveFrame && ledgerHit.persisted === true
}
}
const perSession = sessionRpcDurability.get(sessionId)?.get(rpcId)
if (perSession?.durable === true) {
if (perSession.userMessage === true || !isSupersededPart(sessionId)) return true
}
const ownerKey = ownerKeyOf(logicalOwnerOf(sessionId))
const ownerKey = ownerKeyOf(owner)
const logicalHit = ownerKey === undefined ? undefined : admittedLogicalRpcIds.get(ownerKey)?.get(rpcId)
if (logicalHit?.durable === true) {
const durableSid = logicalHit.durableSessionId ?? logicalHit.sessionId
Expand Down Expand Up @@ -367,22 +391,30 @@ export function install(rt) {
}

const confirmSessionFlushed = async (sessionId, requestId) => {
if (typeof ctx.sessions?.flush !== 'function') return false
if (typeof sessionId !== 'string' || sessionId === '') return false
const owner = logicalOwnerOf(sessionId)
const hasPersistedRoomLedger = () => {
if (owner?.roomId === undefined || typeof requestId !== 'string' || requestId.trim() === '') return false
if (rt.findRoomLedgerRpc?.(owner.roomId, requestId)?.persisted !== true) return false
noteAdmittedRpc(sessionId, requestId, { durable: true, userMessage: true })
return true
}
if (hasPersistedRoomLedger()) return true
if (typeof ctx.sessions?.flush !== 'function') return false
const agent = ctx.get?.('agents')?.get?.(sessionId)
const session = agent?.session ?? ctx.sessions?.get?.(sessionId)
if (session === undefined) return false
const canVerifyAdmission = requestId !== undefined && typeof session.snapshotEvents === 'function'
if (!canVerifyAdmission) {
try {
if ((await ctx.sessions.flush(session)) !== true) return false
if ((await ctx.sessions.flush(session)) !== true) return hasPersistedRoomLedger()
if (requestId !== undefined) {
noteSessionRpcDurability(sessionId, requestId, { durable: true })
noteLogicalAdmittedRpc(sessionId, requestId, { durable: true })
}
return true
} catch {
return false
return hasPersistedRoomLedger()
}
}
const hasUserMessageIn = events => events.some(
Expand All @@ -402,24 +434,29 @@ export function install(rt) {
const supersededWithInboxOnlyEvidence = () => !hasUserMessageNow() && isSupersededPart(sessionId)
try {
for (let pass = 0; pass < 4; pass += 1) {
if (hasPersistedRoomLedger()) return true
let before = session.snapshotEvents?.() ?? []
let hadUserMessageBeforeFlush = hasUserMessageIn(before)
const hadDurableInboxBeforeFlush = !hadUserMessageBeforeFlush
&& collectAdmittedIdsFromEvents(before).has(requestId)
if (!hadUserMessageBeforeFlush && !hadDurableInboxBeforeFlush) {
if (hasPersistedRoomLedger()) return true
if (agent === undefined || !isClaimedInTurn(agent, requestId)) return false
if (!(await waitForClaimedAdmission(sessionId, agent, requestId, hasUserMessageNow))) return false
if (!(await waitForClaimedAdmission(sessionId, agent, requestId, hasUserMessageNow))) {
return hasPersistedRoomLedger()
}
before = session.snapshotEvents?.() ?? []
hadUserMessageBeforeFlush = hasUserMessageIn(before)
if (!hadUserMessageBeforeFlush) return false
if (!hadUserMessageBeforeFlush) return hasPersistedRoomLedger()
}
const beforeLen = before.length
if ((await ctx.sessions.flush(session)) !== true) return false
if ((await ctx.sessions.flush(session)) !== true) return hasPersistedRoomLedger()
if (hadUserMessageBeforeFlush) {
noteSessionRpcDurability(sessionId, requestId, { durable: true, userMessage: true })
noteLogicalAdmittedRpc(sessionId, requestId, { durable: true, userMessage: true })
return true
}
if (hasPersistedRoomLedger()) return true
const after = session.snapshotEvents?.() ?? []
const spliceMutatedDuringFlush = after.length < beforeLen
|| after.slice(beforeLen).some(ev => ev?.type === 'agent/inbox/spliced')
Expand All @@ -430,9 +467,9 @@ export function install(rt) {
return true
}
}
return false
return hasPersistedRoomLedger()
} catch {
return false
return hasPersistedRoomLedger()
}
}

Expand All @@ -449,13 +486,37 @@ export function install(rt) {
if (requestId === undefined) return { admitted: false }
const owner = logicalOwnerOf(sessionId) ?? logicalOwnerOf(ref)
const ownerKey = ownerKeyOf(owner)
const roomLedgerAdmission = (roomId) => {
if (typeof roomId !== 'string' || roomId === '' || typeof rt.findRoomLedgerRpc !== 'function') return undefined
const hit = rt.findRoomLedgerRpc(roomId, requestId)
if (hit?.found !== true) return undefined
const room = rt.roomOf(roomId)
const sid = (room !== undefined ? rt.sessionOfRoom?.(room) : undefined) ?? sessionId
if (hit.persisted === true) {
if (typeof sid === 'string' && sid !== '') {
noteAdmittedRpc(sid, requestId, { durable: true, userMessage: true })
}
return { admitted: true, sessionId: sid, durable: true, userMessage: true }
}
if (typeof sid === 'string' && sid !== '') {
noteAdmittedRpc(sid, requestId, { durable: false, userMessage: false })
}
return { admitted: true, sessionId: sid, durable: false, userMessage: false }
}
const hitResult = (hit, key) => {
const sid = hit.durableSessionId ?? hit.sessionId ?? findLiveAdmittedSession(requestId, key)
// A cached inbox-only durability proof is only valid while its owning part
// has not been superseded by a part switch; once retired, force re-confirmation
// on the replacement part instead of returning a stale `durable: true`.
const durable = hit.durable === true
&& (hit.userMessage === true || (typeof sid === 'string' && !isSupersededPart(sid)))
if (!durable) {
const hitRoomId = (typeof key === 'string' && key.startsWith('room:'))
? key.slice(5)
: (logicalOwnerOf(sid)?.roomId ?? owner?.roomId)
const ledgerHit = roomLedgerAdmission(hitRoomId)
if (ledgerHit?.durable === true) return ledgerHit
}
return {
admitted: true,
sessionId: sid,
Expand All @@ -469,9 +530,9 @@ export function install(rt) {
}
const isImRequest = typeof requestId === 'string' && requestId.startsWith('im:')
if (isImRequest && !targetOnly) {
for (const map of admittedLogicalRpcIds.values()) {
for (const [key, map] of admittedLogicalRpcIds.entries()) {
const hit = map.get(requestId)
if (hit !== undefined) return hitResult(hit, undefined)
if (hit !== undefined) return hitResult(hit, key)
}
}
if (Array.isArray(refs) && refs.length > 0) {
Expand All @@ -484,6 +545,7 @@ export function install(rt) {
}

const candidates = new Map() // sid -> { immutable: boolean }
const candidateRoomIds = new Set()
const addCandidate = (sid, immutable = false) => {
if (typeof sid !== 'string' || sid === '') return
const existing = candidates.get(sid)
Expand All @@ -499,6 +561,7 @@ export function install(rt) {
}
const addRoomCandidates = (room) => {
if (room === undefined) return
candidateRoomIds.add(room.id)
addCandidate(rt.sessionOfRoom?.(room), false)
if (Array.isArray(room?.parts)) {
for (const part of [...room.parts].reverse().slice(0, 4)) {
Expand Down Expand Up @@ -532,6 +595,16 @@ export function install(rt) {
for (const room of Object.values(state.rooms ?? {})) addRoomCandidates(room)
}

// Check canonical persisted room ledger evidence first (works both on the live host
// after an earlier DSH flush failure and after a host restart from disk snapshots).
let pendingLedgerHit
for (const roomId of candidateRoomIds) {
const ledgerHit = roomLedgerAdmission(roomId)
if (ledgerHit === undefined) continue
if (ledgerHit.durable === true) return ledgerHit
pendingLedgerHit ??= ledgerHit
}

const agents = ctx.get?.('agents')
const coldCandidates = []
for (const [sid, meta] of candidates) {
Expand All @@ -557,7 +630,7 @@ export function install(rt) {
}
coldCandidates.push([sid, meta])
}
if (!callerSupplied || coldCandidates.length === 0) return { admitted: false }
if (!callerSupplied || coldCandidates.length === 0) return pendingLedgerHit ?? { admitted: false }
let readFailure
// Persisted evidence on a retired past part proves durability only when it is
// an append-only `user/message`: a turn that started there is part of the
Expand Down Expand Up @@ -617,12 +690,16 @@ export function install(rt) {
}
}
if (readFailure !== undefined) throw readFailure
return { admitted: false }
return pendingLedgerHit ?? { admitted: false }
}

const isDurableProofStillValid = (sid, requestId, userMessage = false) => {
if (userMessage === true) return true
if (typeof sid !== 'string' || sid === '') return false
const owner = logicalOwnerOf(sid)
if (owner?.roomId !== undefined && typeof requestId === 'string' && requestId.trim() !== '') {
if (rt.findRoomLedgerRpc?.(owner.roomId, requestId)?.persisted === true) return true
}
if (!isSupersededPart(sid)) return true
if (requestId === undefined) return false
const session = ctx.get?.('agents')?.get?.(sid)?.session ?? ctx.sessions?.get?.(sid)
Expand Down Expand Up @@ -1132,6 +1209,7 @@ export function install(rt) {
confirmSessionFlushed,
noteAdmittedRpc,
noteSendPrompted,
noteRoomLedgerRpcDurable,
isHandingOff: () => false,
isTaskSettling: () => false,
consumeEmptyTaskWake: () => false,
Expand Down
13 changes: 13 additions & 0 deletions ds-bot/src/host/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2240,7 +2240,14 @@ export function install(rt) {
// never runs again, so the replacement part is where durability must be shown.
// A `user/message` on the retired part is different — a turn that started there
// stays append-only on its transcript and counts as durable history.
const hasPersistedRoomLedger = (sid, reqId) => {
if (typeof sid !== 'string' || sid === '' || typeof reqId !== 'string' || reqId.trim() === '') return false
const roomId = rt.roomOwners?.get?.(sid) ?? rt.pastOwners?.get?.(sid)?.roomId ?? rt.roomOf?.(sid)?.id
return roomId !== undefined && rt.findRoomLedgerRpc?.(roomId, reqId)?.persisted === true
}

const durableEvidenceOnPart = async (sid) => {
if (hasPersistedRoomLedger(sid, confirmedRequestId)) return recordConfirmed(sid, true)
const st = sessionRequestState(sid, confirmedRequestId)
if (!isSupersededPart(sid)) return recordConfirmed(sid, st.hasUserMessage === true)
if (st.live) return st.hasUserMessage === true ? recordConfirmed(sid, true) : false
Expand Down Expand Up @@ -2437,6 +2444,12 @@ export function install(rt) {
if (confirmed?.durable !== true) return false
if (confirmed.userMessage === true) return true
if (typeof confirmed.sessionId !== 'string' || confirmed.sessionId === '') return false
const roomId = rt.roomOwners?.get?.(confirmed.sessionId)
?? rt.pastOwners?.get?.(confirmed.sessionId)?.roomId
?? rt.roomOf?.(confirmed.sessionId)?.id
if (roomId !== undefined && typeof confirmed.requestId === 'string' && confirmed.requestId.trim() !== '') {
if (rt.findRoomLedgerRpc?.(roomId, confirmed.requestId)?.persisted === true) return true
}
if (!isSupersededPart(confirmed.sessionId)) return true
return sessionRequestState(confirmed.sessionId, confirmed.requestId).hasUserMessage === true
}
Expand Down
Loading
Loading