mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
The host stops the old agent (about 0.6 s for Claude, up to 3 s with slow MCP servers) before it writes the divider, so the old transcript lingered. Linear: STA-14361
114 lines
4.0 KiB
TypeScript
114 lines
4.0 KiB
TypeScript
import type { AgentJournalCursor } from './journal/agent-session-journal-identity'
|
|
import type {
|
|
AgentJournalItemBody,
|
|
AgentJournalRenderItem,
|
|
AgentJournalSubmission
|
|
} from './journal/agent-session-journal-types'
|
|
import { isAgentSessionProviderContextBoundary } from './agent-session-provider-context'
|
|
import type { NativeChatMessage } from '../native-chat/native-chat-types'
|
|
|
|
export function isAgentSessionContextClear(body: AgentJournalItemBody | undefined): boolean {
|
|
return body?.kind === 'status' && isAgentSessionProviderContextBoundary(body.contextClear)
|
|
}
|
|
|
|
export function isNativeChatContextClear(message: NativeChatMessage): boolean {
|
|
return (
|
|
message.role === 'system' &&
|
|
message.blocks.some(
|
|
(block) => block.type === 'text' && isAgentSessionProviderContextBoundary(block.contextClear)
|
|
)
|
|
)
|
|
}
|
|
|
|
export function agentSessionContextSequenceFor(
|
|
sequence: number,
|
|
boundaries: readonly number[]
|
|
): number {
|
|
let left = 0
|
|
let right = boundaries.length
|
|
while (left < right) {
|
|
const middle = Math.floor((left + right) / 2)
|
|
if (boundaries[middle] < sequence) {
|
|
left = middle + 1
|
|
} else {
|
|
right = middle
|
|
}
|
|
}
|
|
return left > 0 ? boundaries[left - 1] : 0
|
|
}
|
|
|
|
export function latestAgentSessionContextClearSequence(
|
|
items: Iterable<Pick<AgentJournalRenderItem, 'sequence' | 'body'>>
|
|
): number {
|
|
let sequence = 0
|
|
for (const item of items) {
|
|
if (item.sequence > sequence && isAgentSessionContextClear(item.body)) {
|
|
sequence = item.sequence
|
|
}
|
|
}
|
|
return sequence
|
|
}
|
|
|
|
/** A /clear sent while `sentTail` was the newest row drawn hides the rows up to it until a divider past it lands.
|
|
* Once none of those rows is loaded (a tail snapshot past the divider), nothing is left to hide. */
|
|
export function agentSessionContextClearOutstanding(
|
|
items: readonly Pick<AgentJournalRenderItem, 'sequence'>[],
|
|
latestClear: number,
|
|
sentTail: number | null
|
|
): sentTail is number {
|
|
return (
|
|
sentTail !== null && latestClear <= sentTail && (items[0]?.sequence ?? Infinity) <= sentTail
|
|
)
|
|
}
|
|
|
|
/** Show rows from the /clear divider onward; retain earlier journal rows for workers and archives. */
|
|
export function agentSessionTranscriptRows<
|
|
T extends Pick<AgentJournalRenderItem, 'sequence' | 'body'>
|
|
>(items: readonly T[], floor = latestAgentSessionContextClearSequence(items)): readonly T[] {
|
|
return floor > 0 ? items.filter((item) => item.sequence >= floor) : items
|
|
}
|
|
|
|
/** The host's latest `/clear` sequence, when it speaks for this journal epoch; 0 otherwise. */
|
|
export function agentSessionContextFloorSequence(
|
|
floor: AgentJournalCursor | undefined,
|
|
epoch: string | null
|
|
): number {
|
|
return floor && floor.epoch === epoch ? floor.sequence : 0
|
|
}
|
|
|
|
/** A loaded `/clear` divider is where a transcript's history starts, so nothing older is paged. */
|
|
export function agentSessionHasOlderTranscript(
|
|
items: readonly Pick<AgentJournalRenderItem, 'body'>[],
|
|
hasOlder: boolean
|
|
): boolean {
|
|
return hasOlder && !items.some((item) => isAgentSessionContextClear(item.body))
|
|
}
|
|
|
|
export function agentSessionCurrentContextRows(
|
|
items: readonly AgentJournalRenderItem[],
|
|
submissions: readonly AgentJournalSubmission[] = [],
|
|
minimumSequence = 0
|
|
) {
|
|
const sequence = Math.max(minimumSequence, latestAgentSessionContextClearSequence(items))
|
|
return sequence > 0
|
|
? {
|
|
items: items.filter((item) => item.sequence > sequence),
|
|
submissions: submissions.filter(
|
|
(submission) => (submission.acceptedSequence ?? 0) > sequence
|
|
),
|
|
sequence
|
|
}
|
|
: { items, submissions, sequence }
|
|
}
|
|
|
|
/** A body read that answers only for rows after the `/clear` at `sequence`, matching `agentSessionCurrentContextRows`. */
|
|
export function agentSessionCurrentContextBody(
|
|
itemOf: (itemId: string) => Pick<AgentJournalRenderItem, 'sequence' | 'body'> | null,
|
|
sequence: number
|
|
): (itemId: string) => AgentJournalRenderItem['body'] | undefined {
|
|
return (itemId) => {
|
|
const item = itemOf(itemId)
|
|
return item && (sequence === 0 || item.sequence > sequence) ? item.body : undefined
|
|
}
|
|
}
|