Files
apes-sidebar/src/client/ops-stream.ts
T
unsc9 31dc3b4572 perf(sidebar): give every consumer one stable session scope
A tab body keys memos and effects off `scope` (the diff pane resolves its
media/html routes from it, the editor restores its preview scroll, the side
chat rebuilds its prose-rewrite cache), but the tab cell built
`{ sessionId, cwd }` fresh on every render. Every such dependency therefore
looked changed whenever the cell re-rendered — which the memo comparator
allows exactly when a COMPARED field moves: `expanded` on a directory toggle,
plus `revealed`, `localeRevision`, `tabsVersion`. The diff preview then
re-ran its whole-document rewrite and its content tab re-issued a whole-file
`fs.read`, flashing its loading row.

`useSessionScope` is now the one place a scope for a component tree is built;
the tab cell, the diff tab, the native tab adapter, and the shell's badge /
rightActions / `+` menu / lifecycle-callback seams all use it. Those last
ones are documented external plug-in surfaces, so a plug-in memoizing on
`scope` stops churning on every unrelated re-render too.

Stabilizing the object removes the only path the diff pane's `content` tab
had to re-read after the file changed on disk, so the header's refresh button
now drives it explicitly (the pane owns the gesture; the loader no longer
leaks its internal counter).

Seven comments asserted that the scope object's identity churns on every
render; that premise is gone, so the ones that still apply say what they
actually guard.
2026-10-25 20:41:54 +08:00

333 lines
15 KiB
TypeScript

/**
* The session's live FILE-OPERATION stream — the signal the editor's
* auto-refresh rides (#855).
*
* The model's file tools leave their calls in the session event log — as a
* plain `tool/call` + `tool/result` pair, or as a PTC pair
* (`tool/ptc-dispatch-start` / `tool/ptc-dispatch`) when the call ran inside
* `<code>` — and the plugin's own `changes.ops` route ships those rows as a
* `seq > afterSeq` delta (see `src/index.ts`). This module folds those deltas
* into "which file did the model just write or edit", which is the whole
* question an open preview has to answer.
*
* Design constraints it exists to honour:
*
* - **One poller per session, not one per tab.** The native right sidebar
* keeps every visited tab body mounted (`keepMounted`), so an N-editor
* session would otherwise run N identical `changes.ops` polls. Members
* (editor tabs) elect ONE leader per session; that leader owns the single
* `usePolling` loop and every member reads the same published state. Any
* member leaving re-elects, and the loop stops when the last one goes.
* - **The plugin's own `changes.ops` delta stream, on ONE poller per session.**
* No `fs.watch`, no `fsRead` fingerprint polling, and no per-tab interval —
* a per-tab poll would multiply the same request by the number of mounted
* editors. Requests ride `use-polling.ts` (the sidebar's one polling loop)
* on the changes tab's own 2.5s cadence, and stop entirely while no editor
* of the session is on screen. A primed stream asks the host to WAIT for the
* next tool event (25s ceiling) instead of re-reading an unchanged window,
* so an idle session costs one parked request rather than a full replay. The
* plugin's OWN saves go through `/sidebar/file` and leave no session event,
* so a reload can never re-trigger itself.
* - **Settled successes only.** A `write`/`edit` call fires once its result
* landed without an error, so a half-written file never reaches a preview
* and a failed call never triggers a pointless reload.
* - **Nothing historical fires.** The first pull of a fresh stream is the
* BASELINE: every op the session already settled is recorded as seen and
* published to nobody. Only an op that settles while someone is watching
* can refresh anything.
* - **Session-scoped.** Streams are keyed by session id; a touch published
* for one session is invisible to every other session's editors.
*
* The stream record outlives its members (only the poller stops): a tab that
* becomes visible again re-joins, the leader resumes from the persisted `seq`
* cursor, and the missed delta is folded then — so a file the model rewrote
* while the tab was parked is still seen as touched. What it does NOT do is
* live for the page: the record map is bounded and least-recently-joined
* first (see {@link STREAM_CAP}). A session evicted after that many newer ones
* re-joins as a FRESH stream, which re-baselines exactly like a page reload
* (see {@link pullStream}) — the only state a returner can lose, and only
* after the bound was actually reached.
*/
import { useCallback, useEffect, useState, useSyncExternalStore } from 'react'
import type { SidebarSessionEvent } from '../context-types.ts'
import { api, type SessionScope } from './api.ts'
import { extractFileOps, type FileOp } from './changes/ops.ts'
import { resolveSidebarPath } from './paths.ts'
import { usePolling } from './use-polling.ts'
/** The delta cadence: the changes tab's session-lens poll of the same route. */
export const FILE_OPS_INTERVAL_MS = 2_500
/**
* Events kept for cross-tick `tool/call` → `tool/result` pairing: a call may
* be delivered in one delta and settle in the next. Only the tail matters
* (a call that old has long since settled), and the bound is what keeps a
* long session's fold cheap.
*/
const EVENT_WINDOW = 400
/**
* Events kept once the last member goes away. The cursor, the baseline and
* the published touches all survive, so a returning tab resumes the delta it
* missed — but a session's tool traffic (write payloads, read results) must
* not be pinned for the page's lifetime just because a tab was visited.
*/
const IDLE_EVENT_WINDOW = 32
/** Touched paths remembered per session (oldest insertion evicted past this). */
const TOUCH_WINDOW = 256
/**
* How many session records the store holds. The record already shrinks to what
* a re-join needs once its last member goes ({@link IDLE_EVENT_WINDOW} events
* plus the published touches), but the MAP itself was "alive for the page":
* every session an editor tab had ever watched kept its cursor, baseline and
* touch table forever. A session still inside this window resumes from its
* cursor and folds the delta it missed; the oldest record that nobody is
* watching is what a new mint evicts.
*/
const STREAM_CAP = 32
/** The op kinds that changed a file's bytes. */
export type FileOpTouchKind = 'write' | 'edit'
/** The newest settled write/edit of one path. */
export interface FileOpTouch {
readonly kind: FileOpTouchKind
/** The published revision this touch belongs to (compare with a baseline). */
readonly revision: number
/** The originating tool call's event time (epoch ms). */
readonly time: number
}
/** What a member reads: the published state of its session's stream. */
export interface FileOpStreamState {
/** Monotonic per-session counter, bumped once per published delta. */
readonly revision: number
/** Comparison key ({@link fileOpKey}) → that path's newest settled touch. */
readonly touched: ReadonlyMap<string, FileOpTouch>
}
/** The state every session starts from (a stable identity: the store contract). */
const EMPTY_STATE: FileOpStreamState = { revision: 0, touched: new Map() }
/**
* The comparison key of one path, so a model-spelled path and an editor tab's
* absolute path meet: relative paths are resolved against the session cwd
* ({@link resolveSidebarPath}), `.`/`..` segments are folded lexically, both
* separators unify to '/', and Windows-style paths (drive letters, UNC
* shares) fold case — the same file may be spelled either way there.
* @param cwd - the session working directory (undefined = nothing to join onto).
* @param path - an absolute or session-relative path.
* @returns the comparison key (never a path to hand back to the host).
*/
export function fileOpKey(cwd: string | undefined, path: string): string {
const unified = resolveSidebarPath(cwd, path).replace(/\\/g, '/')
const windows = /^[A-Za-z]:\//.test(unified) || unified.startsWith('//')
const segments: string[] = []
for (const segment of unified.split('/')) {
if (segment === '' || segment === '.') continue
if (segment === '..') {
segments.pop()
continue
}
segments.push(segment)
}
const key = `${unified.startsWith('/') ? '/' : ''}${segments.join('/')}`
return windows ? key.toLowerCase() : key
}
/** One polling member: an editor tab, and the way to tell it it leads now. */
interface Member {
/** Become (true) or stop being (false) this session's poller. */
lead(leading: boolean): void
}
/** One session's stream: the shared cursor, the published state, the members. */
interface Stream {
state: FileOpStreamState
listeners: Set<() => void>
members: Set<Member>
leader: Member | undefined
/** The delivered cursor (`-1` includes an eventual first event at seq 0). */
seq: number
/** Whether the baseline pull completed (only then may a delta publish). */
primed: boolean
events: SidebarSessionEvent[]
/** callIds already accounted for: a settle must fire exactly once. */
published: Set<string>
/** The leader's cwd, used to resolve the model's own relative spellings. */
cwd: string | undefined
}
/** Every session's stream, bounded by {@link STREAM_CAP} (see `streamOf`). */
const streams = new Map<string, Stream>()
/** The stream record of one session, created on first use. */
function streamOf(sessionId: string): Stream {
const existing = streams.get(sessionId)
if (existing !== undefined) return existing
// Bound the store before minting: a session nobody is watching is what goes.
// A record WITH members is never evicted — it owns the leader election and
// the members' published state — so the cap is soft while many sessions are
// genuinely open.
if (streams.size >= STREAM_CAP) {
for (const [key, candidate] of streams) {
if (candidate.members.size > 0) continue
streams.delete(key)
if (streams.size < STREAM_CAP) break
}
}
const stream: Stream = {
state: EMPTY_STATE,
listeners: new Set(),
members: new Set(),
leader: undefined,
seq: -1,
primed: false,
events: [],
published: new Set(),
cwd: undefined,
}
streams.set(sessionId, stream)
return stream
}
/** Move one session's record to the back of the eviction order (LRU). */
function touchStream(sessionId: string, stream: Stream): void {
if (streams.get(sessionId) !== stream) return
streams.delete(sessionId)
streams.set(sessionId, stream)
}
/** Publish one delta's settled write/edit ops to every member of the stream. */
function publish(stream: Stream, ops: readonly FileOp[]): void {
const revision = stream.state.revision + 1
const touched = new Map(stream.state.touched)
// `extractFileOps` orders newest first; walking it backwards leaves the
// NEWEST op of this delta as the one recorded for a path touched twice.
for (const op of [...ops].reverse()) {
stream.published.add(op.callId)
touched.set(fileOpKey(stream.cwd, op.path), { kind: op.kind as FileOpTouchKind, revision, time: op.time })
}
// Bound the memory of a long session. Map iteration is insertion order, and
// re-touching a path keeps its original slot, so the evicted entry is not
// strictly the oldest revision — a miss here only costs a stale preview
// until the next write of that path, never wrong content.
for (const key of touched.keys()) {
if (touched.size <= TOUCH_WINDOW) break
touched.delete(key)
}
stream.state = { revision, touched }
for (const listener of stream.listeners) listener()
}
/**
* Fold one delta (plus the retained window) into the stream.
* @param baseline - true for a fresh stream's FIRST fold: everything already
* settled predates the editors that are joining, so it marks state without
* publishing anything.
*/
function fold(stream: Stream, events: readonly SidebarSessionEvent[], baseline: boolean): void {
const merged = [...stream.events, ...events]
stream.events = merged.length > EVENT_WINDOW ? merged.slice(merged.length - EVENT_WINDOW) : merged
const ops = extractFileOps(stream.events)
if (baseline) {
for (const op of ops) stream.published.add(op.callId)
} else {
const settled = ops.filter(op => (op.kind === 'write' || op.kind === 'edit')
&& !op.running && !op.isError && !stream.published.has(op.callId))
if (settled.length > 0) publish(stream, settled)
}
// Ops that scrolled out of the window cannot settle any more: drop their
// marks so the set can never grow past what the window can hold.
const present = new Set(ops.map(op => op.callId))
for (const callId of stream.published) {
if (!present.has(callId)) stream.published.delete(callId)
}
}
/** One leader's delta pull: advance the cursor, then fold (or baseline) it. */
async function pullStream(stream: Stream, scope: SessionScope, signal: AbortSignal): Promise<void> {
const { events, lastSeq } = await api.changesOps(scope, stream.primed ? stream.seq : undefined, signal, stream.primed)
// A torn-down run's answer must never publish state: the transport may
// deliver an aborted response anyway.
if (signal.aborted === true) return
if (scope.cwd !== undefined && scope.cwd !== '') stream.cwd = scope.cwd
if (lastSeq > stream.seq) stream.seq = lastSeq
const baseline = !stream.primed
stream.primed = true
if (events.length > 0) fold(stream, events, baseline)
}
/**
* Join one session's file-operation stream while `enabled`, and read its
* published state. Every mounted editor of a session calls this; exactly one
* of them (the elected leader) runs the poll.
* @param scope - the session whose log is watched (sessionId + cwd).
* @param enabled - whether this tab is on screen and holds a real file (a
* parked tab must not keep polling, and a folder/path-less window has no file
* to watch).
* @returns the session's published touches, stable between deltas.
*/
export function useFileOpStream(scope: SessionScope, enabled: boolean): FileOpStreamState {
const { sessionId } = scope
const [leading, setLeading] = useState(false)
useEffect(() => {
if (!enabled || sessionId === '') {
setLeading(false)
return
}
const stream = streamOf(sessionId)
// The session that just joined is the most recently used, so an ACTIVE
// session is never the record a later mint evicts.
touchStream(sessionId, stream)
const member: Member = { lead: (value: boolean) => { setLeading(value) } }
stream.members.add(member)
if (stream.leader === undefined) {
stream.leader = member
setLeading(true)
}
return () => {
stream.members.delete(member)
if (stream.members.size === 0 && stream.events.length > IDLE_EVENT_WINDOW) {
// Nobody is watching: keep only what a settle landing right now would
// still need to pair with. The cursor and the published touches stay,
// so the missed delta is folded (and published) when a tab returns.
stream.events = stream.events.slice(stream.events.length - IDLE_EVENT_WINDOW)
}
if (stream.leader !== member) return
stream.leader = undefined
setLeading(false)
// Hand the loop to whoever is left: only a member's own hook can run it.
const next = stream.members.values().next().value as Member | undefined
if (next !== undefined) {
stream.leader = next
next.lead(true)
}
}
}, [enabled, sessionId])
// Granular scope fields: only these two gate the request target (callers
// may assemble a scope object themselves).
const cwd = scope.cwd
const pull = useCallback(async (signal: AbortSignal): Promise<void> => {
await pullStream(streamOf(sessionId), { sessionId, cwd }, signal)
}, [sessionId, cwd])
// Self-scheduling: at most ONE delta request in flight per session, and the
// first one runs immediately so the baseline is established before any
// delta can be mistaken for a fresh write.
usePolling(leading, pull, { intervalMs: FILE_OPS_INTERVAL_MS, mode: 'self-scheduling', immediate: true })
return useSyncExternalStore(
useCallback((listener: () => void) => {
const stream = streamOf(sessionId)
stream.listeners.add(listener)
return () => { stream.listeners.delete(listener) }
}, [sessionId]),
useCallback(() => streams.get(sessionId)?.state ?? EMPTY_STATE, [sessionId]),
)
}