refactor(events): unify scoped runtime notifications across clients (#5670)

* refactor(events): unify scoped runtime notifications across clients

* style(tests): sort auto-compaction event imports

* refactor(events): scope connections and remove retry callback plumbing

* docs: remove runtime notification design write-up

* refactor(bus): route operation events through the message bus

* fix(channels): isolate outbound sends by destination

* style(events): group shared notification imports

* refactor(events): converge runtime and hook delivery on MessageBus

* test(events): migrate fixtures to unified message bus
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-06 02:11:19 +08:00
1 parent 3495281282
commit 192e2e90dc
78 files changed
+1620 -1194

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
[
{"event":"context_compaction","chat_id":"chat","compaction_id":"c1","phase":"started"},
{"event":"context_compaction","chat_id":"chat","compaction_id":"c1","phase":"succeeded"},
{"event":"context_compaction","chat_id":"chat","compaction_id":"c1","phase":"failed"},
{"event":"context_compaction","chat_id":"chat","compaction_id":"c1","phase":"cancelled"},
{"event":"recovery_state","chat_id":"chat","status":"resuming","recovery_id":"r1","attempts":0},
{"event":"recovery_state","chat_id":"chat","status":"awaiting_user","recovery_id":"r1","attempts":1,"reason":"tool_state_unknown","can_continue":false},
{"event":"recovery_state","chat_id":"chat","status":"recovered","recovery_id":"r1","attempts":1},
{"event":"recovery_state","chat_id":"chat","status":"failed","recovery_id":"r1","attempts":2}
]
+67
View File
@@ -0,0 +1,67 @@
/** Transport notifications shared by the browser and terminal clients. */
export type RecoveryStatus = "resuming" | "awaiting_user" | "recovered" | "failed"
export interface RecoveryState {
status: RecoveryStatus
recovery_id: string
reason?: string
attempts?: number
can_continue?: boolean
}
export interface ContextCompaction {
id: string
phase: "started" | "succeeded" | "failed" | "cancelled"
}
export type NotificationEvent =
| ({ event: "recovery_state"; chat_id: string; turn_id?: string } & RecoveryState)
| {
event: "context_compaction"
chat_id: string
turn_id?: string
compaction_id: string
phase: ContextCompaction["phase"]
}
function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value)
}
function optional(value: unknown, type: "boolean" | "number" | "string"): boolean {
return value === undefined || typeof value === type
}
export function isCompactionPhase(value: unknown): value is ContextCompaction["phase"] {
return value === "started" || value === "succeeded" || value === "failed" || value === "cancelled"
}
export function isRecoveryState(value: unknown): value is RecoveryState {
return isRecord(value)
&& typeof value.status === "string"
&& ["resuming", "awaiting_user", "recovered", "failed"].includes(value.status)
&& typeof value.recovery_id === "string"
&& optional(value.reason, "string")
&& optional(value.attempts, "number")
&& optional(value.can_continue, "boolean")
}
/** Undefined means another protocol family; null means a malformed notification. */
export function decodeNotification(value: unknown): NotificationEvent | null | undefined {
if (!isRecord(value)) return undefined
if (value.event !== "recovery_state" && value.event !== "context_compaction") return undefined
if (typeof value.chat_id !== "string" || !optional(value.turn_id, "string")) return null
if (value.event === "recovery_state") {
return isRecoveryState(value) ? value as unknown as NotificationEvent : null
}
return typeof value.compaction_id === "string" && value.compaction_id.length > 0
&& isCompactionPhase(value.phase) ? value as unknown as NotificationEvent : null
}
/** A terminal history row must not regress when an older live event arrives. */
export function acceptsCompactionPhase(
current: ContextCompaction["phase"] | undefined,
incoming: ContextCompaction["phase"],
): boolean {
return current === undefined || (current === "started" && incoming !== "started")
}