Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
65d2964
fix(native-chat): give a failed Claude background task a typed row in…
brennanb2025 Sep 13, 2026
f6da6eb
Merge remote-tracking branch 'origin/main' into brennanb2025/claude-t…
brennanb2025 Sep 13, 2026
0dbd2fe
fix(native-chat): keep tool attribution across a background-task row
brennanb2025 Sep 14, 2026
b8293eb
fix(native-chat): harden background task rows
brennanb2025 Sep 14, 2026
32a79b1
fix(native-chat): settle background rows on provider end
brennanb2025 Sep 14, 2026
8d73da2
fix(native-chat): scope malformed task fallback text
brennanb2025 Sep 14, 2026
32c0b15
test(native-chat): assert only eligibility at the disposition layer
brennanb2025 Sep 14, 2026
7acb0bc
fix(native-chat): gate background-task admission and scope rows per run
brennanb2025 Sep 14, 2026
398971e
fix(native-chat): read the aggregate roster by membership, not a phan…
brennanb2025 Sep 14, 2026
e39a25a
fix(native-chat): mirror reference task admission and drop the synthe…
brennanb2025 Sep 14, 2026
d3ad77d
fix(native-chat): preserve background task ownership across restarts
brennanb2025 Sep 14, 2026
147f345
Merge origin/main into brennanb2025/claude-task-frame-rows
brennanb2025 Sep 14, 2026
51cea54
chore: restore pnpm-lock.yaml to origin/main
brennanb2025 Sep 14, 2026
af72858
fix(claude): harden background task lifecycle
brennanb2025 Sep 15, 2026
a9665f5
fix(claude): bound task generation history
brennanb2025 Sep 15, 2026
11ab98c
fix(claude): preserve task identity after history eviction
brennanb2025 Sep 15, 2026
1e08430
fix(claude): resolve background task identity after rebind
brennanb2025 Sep 15, 2026
29e3bcb
fix(claude): isolate queued task runs
brennanb2025 Sep 15, 2026
7a9dd44
Merge remote-tracking branch 'origin/main' into brennanb2025/pr20519-…
brennanb2025 Sep 15, 2026
9f5c738
refactor(claude): give the background-task ledgers one bounded owner
brennanb2025 Sep 15, 2026
571cbdf
Merge remote-tracking branch 'origin/main' into brennanb2025/claude-t…
brennanb2025 Sep 16, 2026
5cc5141
Merge remote-tracking branch 'origin/main' into brennanb2025/pr20519-…
brennanb2025 Sep 17, 2026
11d534a
fix(native-chat): report a background task failure the transcript nev…
brennanb2025 Sep 17, 2026
f93c2cb
test(native-chat): pin that an orphan task row reopens the provider's…
brennanb2025 Sep 17, 2026
b598e36
fix(native-chat): stop an orphan task row drawing its sentence twice
brennanb2025 Sep 17, 2026
1412144
fix(native-chat): keep Claude task outcomes owned through capacity an…
brennanb2025 Sep 17, 2026
ff78489
Merge remote-tracking branch 'origin/main' into brennanb2025/claude-t…
brennanb2025 Sep 17, 2026
11f05cd
fix(native-chat): keep settled overflow notifications from reopening …
brennanb2025 Sep 17, 2026
e84c650
fix(native-chat): retain Claude task rows through journal pressure
brennanb2025 Sep 17, 2026
d8c7281
Merge remote-tracking branch 'origin/main' into brennanb2025/claude-t…
brennanb2025 Sep 17, 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
66 changes: 66 additions & 0 deletions src/main/claude/claude-background-task-frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
AgentSessionBackgroundTask,
AgentSessionBackgroundTaskRunState
} from '../../shared/agent-session-wire'
import { backgroundTaskFallbackText } from '../../shared/native-chat-background-task-row'

const MAX_TASK_ID_LENGTH = 512
const MAX_TASK_TEXT_LENGTH = 512
Expand All @@ -29,6 +30,10 @@ export function taskId(message: Record<string, unknown>): string | null {
return typeof value === 'string' && isBoundedClaudeTaskId(value) ? value : null
}

export function taskAliasId(value: unknown): string | undefined {
return typeof value === 'string' && isBoundedClaudeTaskId(value) ? value : undefined
}

function boundedTaskText(value: unknown): string | undefined {
if (typeof value !== 'string') {
return undefined
Expand All @@ -41,6 +46,66 @@ export function taskDescription(value: unknown): string | undefined {
return boundedTaskText(value)
}

/** Every other provider string a durable task row carries — its summary, its
* error, its output path — takes the description bound: the row is replayed on
* every reconnect, and each reader clips it again anyway. */
export function taskText(value: unknown): string | undefined {
return boundedTaskText(value)
}

/**
* The sentence a task frame wrote about itself.
*
* Only for a frame the row owner could not claim — malformed or capacity-refused.
* It reaches the generic fallback, which has no key for `summary` and would
* otherwise print the bare opcode. Passed to that fallback as Claude's own
* display text rather than taught to its shared key list, because that list is
* read for every provider and already resolves `summary` by hand for two Codex
* methods; widening it globally to reach one malformed Claude frame would
* re-rank the row text of every unmodelled frame on both providers.
*/
export function taskFrameSentence(frame: Record<string, unknown>): string | undefined {
const patch = record(frame.patch)
const sentence =
taskText(frame.summary) ??
taskText(frame.error) ??
taskText(patch?.summary) ??
taskText(patch?.error)
if (sentence) {
return sentence
}

// A terminal task update often carries only its status in the nested patch.
// Reuse the durable row's frozen sentence so capacity fallbacks never expose
// the provider opcode when no human-facing text was supplied.
if (
frame.subtype !== 'task_started' &&
frame.subtype !== 'task_updated' &&
frame.subtype !== 'task_progress' &&
frame.subtype !== 'task_notification'
) {
return undefined
}
const status = patch?.status ?? frame.status
const state = terminalClaudeTaskRunState(status)
if (state === null) {
return undefined
}
const kind = classifyClaudeBackgroundTaskKind(patch?.task_type ?? frame.task_type)
return backgroundTaskFallbackText({
type: 'background-task',
taskId: taskId(frame) ?? '',
kind,
label:
taskDescription(patch?.description) ??
taskDescription(frame.description) ??
(patch ? taskName(patch) : undefined) ??
taskName(frame) ??
'',
state
})
}

/** The provider-reported identity for a task. Subagent frames have carried the
* type under both `agent_type` and `subagent_type` across SDK versions. */
export function taskName(frame: Record<string, unknown>): string | undefined {
Expand All @@ -54,6 +119,7 @@ export function taskName(frame: Record<string, unknown>): string | undefined {
export function classifyClaudeBackgroundTaskKind(taskType: unknown): ClaudeBackgroundTaskKind {
switch (taskType) {
case 'local_agent':
case 'local_subagent':
return 'agent'
case 'local_workflow':
return 'workflow'
Expand Down
184 changes: 184 additions & 0 deletions src/main/claude/claude-background-task-memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import { isSettledBackgroundTaskState } from '../../shared/native-chat-background-task-row'
import type { ClaudeBackgroundTaskRow } from './claude-background-task-row-lifecycle'

const MAX_GENERATION_ENTRIES = 512
const MAX_FOREIGN_TASK_ROWS = 512
const MAX_TERMINAL_TASK_IDS = 512
const MAX_FALLBACK_TASK_IDS = 512

/** Bounded run identity ledger. Once old ids fall out, a monotonic sequence
* keeps a reused id from colliding with a durable row already in the journal. */
class ClaudeBackgroundTaskGenerationLedger {
private readonly entries = new Map<string, number>()
private nextUniqueGeneration = 1
private evicted = false

next(id: string): number {
const previous = this.entries.get(id)
const generation =
previous === undefined ? (this.evicted ? this.nextUniqueGeneration++ : 1) : previous + 1
this.entries.delete(id)
this.entries.set(id, generation)
this.nextUniqueGeneration = Math.max(this.nextUniqueGeneration, generation + 1)
while (this.entries.size > MAX_GENERATION_ENTRIES) {
const oldest = this.entries.keys().next()
if (oldest.done || oldest.value === id) {
break
}
this.entries.delete(oldest.value)
this.evicted = true
}
return generation
}

get size(): number {
return this.entries.size
}

clear(): void {
this.entries.clear()
this.nextUniqueGeneration = 1
this.evicted = false
}
}

function rememberBoundedClaudeTaskSet(ids: Set<string>, id: string, maxSize: number): void {
ids.delete(id)
ids.add(id)
while (ids.size > maxSize) {
const oldest = ids.values().next()
if (oldest.done || oldest.value === id) {
break
}
ids.delete(oldest.value)
}
}

function rememberBoundedClaudeTaskMap<T>(
entries: Map<string, T>,
id: string,
value: T,
maxSize: number
): void {
entries.delete(id)
entries.set(id, value)
while (entries.size > maxSize) {
const oldest = entries.keys().next()
if (oldest.done || oldest.value === id) {
break
}
entries.delete(oldest.value)
}
}

export function ensureClaudeBackgroundTaskRowSlot(
rows: Map<string, ClaudeBackgroundTaskRow>,
maxSize: number
): boolean {
if (rows.size < maxSize) {
return true
}
for (const [id, row] of rows) {
if (isSettledBackgroundTaskState(row.block.state)) {
rows.delete(id)
return true
}
}
return false
}

function rememberClaudeBackgroundTaskTerminal(
terminalIds: Set<string>,
terminalToolUseIds: Map<string, string | undefined>,
rows: Map<string, ClaudeBackgroundTaskRow>,
id: string,
toolUseId: string | undefined,
maxSize: number
): void {
terminalIds.delete(id)
terminalIds.add(id)
terminalToolUseIds.set(id, toolUseId ?? rows.get(id)?.toolUseId ?? terminalToolUseIds.get(id))
while (terminalIds.size > maxSize) {
const oldest = terminalIds.values().next()
if (oldest.done || oldest.value === id) {
break
}
terminalToolUseIds.delete(oldest.value)
terminalIds.delete(oldest.value)
}
}

/** Who renders a task this owner deliberately declined.
*
* `sidechain` is a Task spawned inside a subagent's own run: its spawning tool
* never reached the top-level transcript, so no top-level row may claim it.
* `terminal` is a capacity-refused task whose settled typed row was already
* written without taking a live slot; its redeliveries must not print again. */
export type ForeignOwner = 'roster' | 'ambient' | 'foreground' | 'sidechain' | 'terminal'

/** How much each ledger is holding. Named and readonly so a caller can prove
* eviction still bounds them without reaching into the collections. */
export type ClaudeBackgroundTaskLedgerSizes = {
readonly generations: number
readonly foreign: number
readonly fallbackTaskIds: number
readonly terminalTaskIds: number
}

/** Every bounded ledger a session keeps beside its rows, with the caps that
* bound them. One owner, so a sweep clears them together and no cap is
* applied at only some of the call sites that write to a ledger. */
export class ClaudeBackgroundTaskLedgers {
/** Runs seen per task id, so a reused id opens a new row instead of
* overwriting the finished one. Survives the row being evicted. */
readonly generations = new ClaudeBackgroundTaskGenerationLedger()
readonly foreign = new Map<string, ForeignOwner>()
/** Tasks that were declined because every typed row slot was live. Their
* later frames must remain visible through the generic fallback. */
readonly fallbackTaskIds = new Set<string>()
readonly terminalTaskIds = new Set<string>()
/** The parent alias for the terminal run, when one was reported. Keeping it
* lets an evicted row distinguish a late duplicate start from a genuine
* restart under a fresh tool invocation. */
readonly terminalToolUseIds = new Map<string, string | undefined>()

rememberForeign(id: string, owner: ForeignOwner): void {
rememberBoundedClaudeTaskMap(this.foreign, id, owner, MAX_FOREIGN_TASK_ROWS)
}

rememberFallback(id: string): void {
rememberBoundedClaudeTaskSet(this.fallbackTaskIds, id, MAX_FALLBACK_TASK_IDS)
}

rememberTerminal(
rows: Map<string, ClaudeBackgroundTaskRow>,
id: string,
toolUseId: string | undefined
): void {
rememberClaudeBackgroundTaskTerminal(
this.terminalTaskIds,
this.terminalToolUseIds,
rows,
id,
toolUseId,
MAX_TERMINAL_TASK_IDS
)
}

get sizes(): ClaudeBackgroundTaskLedgerSizes {
return {
generations: this.generations.size,
foreign: this.foreign.size,
fallbackTaskIds: this.fallbackTaskIds.size,
terminalTaskIds: this.terminalTaskIds.size
}
}

clear(): void {
this.generations.clear()
this.foreign.clear()
this.fallbackTaskIds.clear()
this.terminalTaskIds.clear()
this.terminalToolUseIds.clear()
}
}
41 changes: 41 additions & 0 deletions src/main/claude/claude-background-task-roster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { isSettledBackgroundTaskState } from '../../shared/native-chat-background-task-row'
import {
classifyClaudeBackgroundTaskKind,
record,
taskDescription,
taskId,
taskName
} from './claude-background-task-frames'
import type {
ClaudeBackgroundTaskChange,
ClaudeBackgroundTaskRow
} from './claude-background-task-row-lifecycle'

/** A roster carries membership and identity, not a task's terminal outcome. */
export function observeClaudeBackgroundTaskRoster(
tasks: unknown[],
rows: Map<string, ClaudeBackgroundTaskRow>,
revise: (id: string, change: ClaudeBackgroundTaskChange) => void
): void {
for (const entry of tasks) {
const task = record(entry)
const id = task === null ? null : taskId(task)
const row = id === null ? undefined : rows.get(id)
if (
task === null ||
id === null ||
task.ambient === true ||
!row ||
isSettledBackgroundTaskState(row.block.state)
) {
continue
}
// Presence must not revive a settled row; the task's own frames alone
// settle or restart it, while the roster enriches its identity fields.
revise(id, {
label: taskDescription(task.description) ?? taskName(task),
kind:
task.task_type === undefined ? undefined : classifyClaudeBackgroundTaskKind(task.task_type)
})
}
}
Loading
Loading