* fix(terminal): stop answering DECSET 2031 subscriptions fish already withdrew fish enables and disables mode 2031 around every prompt (tty_handoff.rs), so a single PTY chunk routinely carries `?2031h ... ?2031l`. All three responders answered the sticky "an h appeared anywhere" flag, so each prompt cycle wrote `?997;1n` into a shell that had already handed the tty to a child — it lands as literal text, or as stdin for whatever is reading. pty-connection.ts's hidden-pane responder already had the right shape (`finalState !== 'subscribed'`); this brings the other three in line: - shared tracker: gate the '2031-subscribe' fact on the chunk-final state - parked-tab byte sidecar: same guard - visible-pane xterm CSI handler: xterm dispatches mid-parse, so there is no chunk-final state to read. Defer the reply to a microtask and re-check the subscription, letting a same-chunk `?2031l` cancel it. Refs #9993 Co-authored-by: Orca <help@stably.ai> * fix(terminal): decide 2031 replies per PTY chunk, not per xterm parse The previous commit deferred the visible-pane reply to a microtask so a same-chunk `?2031l` could cancel it. That cannot work: xterm's WriteBuffer parses every queued `terminal.write()` synchronously in one batch before any microtask runs, so the microtask sees the net state of N PTY chunks, not of the one that carried the subscribe. A TUI that subscribes in chunk N gets no reply when chunk N+1 happens to withdraw, and a fish prompt straddling two writes still gets answered. Move the decision to where chunk boundaries actually exist — pty-connection's dataCallback, which receives one PTY chunk per call. It scans raw bytes with `scanMode2031Sequences`, carrying a tail across chunks so a CSI split mid- sequence still resolves, and replies only when that chunk *ends* subscribed. Ownership stays single: gate-managed PTYs are answered by main's '2031-subscribe' fact, so the chunk scanner returns early for them, and the xterm CSI handler now observes only panes the scanner does not own. The tail is dropped on PTY replacement — a partial prefix belongs to the stream that produced it. Removes the microtask responder and the seed-reply retry path it needed. Mutation-tested: 6 mutations applied, 6 killed. * fix(terminal): carry DECSET 2031 withdrawals as a side-effect fact The previous commit moved 2031 reply decisions to the PTY chunk boundary and gave gate-managed panes a single owner: main's '2031-subscribe' fact. But the fact union is subscribe-only, and that left the withdrawal unobserved. For a gate-managed pane, main drops renderer-bound bytes after model ingestion, the chunk scanner early-returns, and xterm's CSI handler is disabled. So when a TUI emits `?2031l` while hidden, nothing retires the subscription: paneMode2031 stays set, and the next theme flip has maybePushMode2031Flip push `CSI ?997;2n` into the shell that replaced the TUI — #9993 again, through the theme-change door. Before this branch, skipHiddenRendererOutput observed those withheld bytes; consolidating ownership removed that observer without replacing it. No renderer-side observer can close this: the bytes are gone before the renderer sees them. The state protocol has to carry the withdrawal, so add a '2031-unsubscribe' fact alongside the subscribe across the three fact unions (shared, provider, daemon). It fires only on a real chunk-final withdrawal — a chunk with no 2031 bytes scans to null and stays silent. The renderer handler clears both maps and sends nothing: a withdrawal is not a query. Also closes two gaps an adversarial review found by mutation, both previously resting on comments rather than tests: the lifecycle parser-ownership predicate (extracted as isPaneParserOwnedMode2031Observer so it is directly testable) and the scan-before-reconciliation ordering that lets a chunk the snapshot drops as a duplicate still answer its query. Mutation-tested: 12 mutations applied, 12 killed (6 from the prior round re-run, 6 new covering this fix and the two survivors). * fix(daemon): refuse 2031 authority from a daemon that cannot retract it Round-2 review found a wire-compatibility hole in the original #9993 fix. Daemons survive app updates, so a new desktop can drive a daemon that was started by the previous build. Pre-v29 daemons emit '2031-subscribe' but have no '2031-unsubscribe' fact at all. For a gate-managed pane, main drops the renderer-bound bytes before the renderer sees them, so main's transient facts are the ONLY thing that can retire a subscription. Against such a daemon a TUI exiting while its pane is hidden leaves the subscription registered forever, and the next theme flip injects CSI 997 into whatever shell replaced it -- #9993 all over again, reached through the upgrade path. Gate it: bump PROTOCOL_VERSION 28 -> 29, add MODE_2031_UNSUBSCRIBE_FACT_PROTOCOL_VERSION with supportsMode2031UnsubscribeFact(), and drop '2031-subscribe' from any daemon below that floor. Trade-off: a gate-managed pane on a preserved v28 daemon keeps renderer-scanner authority instead of daemon-fact authority. That is exactly the pre-fact behaviour -- correct for visible panes, no worse than today for hidden ones -- and it resolves on the daemon's next restart. Non-2031 transient facts (bell, etc.) are unaffected at every version. Tests: two adapter regression tests (v28 drops subscribe, v29 forwards it), plus a version-pin test asserting the floor sits above every entry in PREVIOUS_DAEMON_PROTOCOL_VERSIONS -- so adding a new preserved version cannot silently re-open the hole. Mutation-verified in both directions: `false &&` (under-block) and `true` (over-block) each fail the new tests. * fix(daemon): gate background delegation, not just the fact stream A pre-v29 daemon can announce a 2031 subscribe but never retract it. Filtering that fact is not enough: while a pane is visible main's own scanner registers the subscription, and scan authority only moves to the daemon when the session is backgrounded. So the gate belongs on setPtyBackgrounded — decline to hand a non-retracting daemon authority at all, and main stays authoritative over the whole stream. Co-authored-by: Orca <help@stably.ai> * fix(daemon): clear a preserved pre-v29 background hint at attach, not just at background Co-authored-by: Orca <help@stably.ai> * fix(terminal): don't answer a 2031 subscribe whose withdrawal straddles a chunk Review found the chunk-final-state fix left one hole open. When the kernel cuts fish's toggle pair mid-withdrawal — chunk 1 ends "...?2031h prompt ESC[?20", chunk 2 is "31l" — chunk 1 genuinely ends subscribed, so it answers, and the reply lands as literal text at the prompt. Chunk 2 then recognizes the withdrawal but cannot recall bytes already written. The same byte stream is safe or corrupting purely by where the kernel split it. The scanner already retains an incomplete private-mode tail; it just didn't tell the caller whether that tail could still resolve to 2031. It now does, and a subscribe is held one chunk while the answer is still in doubt. Only subscribes defer — retiring a subscription writes nothing to the pty, so withdrawals stay eager. Deferral is narrow: a trailing "ESC[?25" (cursor hide) can never become 2031, so a subscribe already seen in that chunk is still answered immediately. This case predates the branch — the old sticky-flag policy replied here too — so it is a residual this fix now closes rather than a regression it introduced. Tests: three cases pinned (split withdrawal, non-2031 partial must not defer, split re-subscribe answers once). Removing the deferral fails only the first. * fix(terminal): preserve mode 2031 reply decisions * fix(build): record daemon protocol v29 compatibility --------- Co-authored-by: Orca <help@stably.ai>
263 lines
9.2 KiB
TypeScript
263 lines
9.2 KiB
TypeScript
// Why: slice 3 of terminal-side-effect-authority.md adds OSC 133;D
|
|
// command-finished and GitHub pr-link scanning to the shared tracker so main
|
|
// emits those facts for local/SSH PTYs. These tests pin the chunk-boundary
|
|
// carry, exit-code best-effort, dedupe, and synthetic-frame isolation rules.
|
|
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
createTerminalTitleTracker,
|
|
type TerminalTitleTrackerCallbacks
|
|
} from './terminal-output-side-effects'
|
|
|
|
const ESC = '\x1b'
|
|
const BEL = '\x07'
|
|
const ST = `${ESC}\\`
|
|
|
|
type RecordedEvent =
|
|
| ['title', string]
|
|
| ['bell']
|
|
| ['finished', number | null]
|
|
| ['pr', string, number]
|
|
| ['2031-subscribe']
|
|
| ['2031-unsubscribe']
|
|
|
|
function createRecordingTracker(overrides: TerminalTitleTrackerCallbacks = {}): {
|
|
events: RecordedEvent[]
|
|
tracker: ReturnType<typeof createTerminalTitleTracker>
|
|
} {
|
|
const events: RecordedEvent[] = []
|
|
const tracker = createTerminalTitleTracker({
|
|
onTitle: (normalized) => events.push(['title', normalized]),
|
|
onBell: () => events.push(['bell']),
|
|
onCommandFinished: (exitCode) => events.push(['finished', exitCode]),
|
|
onPrLink: (link) => events.push(['pr', link.url, link.number]),
|
|
onMode2031Subscribe: () => events.push(['2031-subscribe']),
|
|
onMode2031Unsubscribe: () => events.push(['2031-unsubscribe']),
|
|
...overrides
|
|
})
|
|
return { events, tracker }
|
|
}
|
|
|
|
describe('createTerminalTitleTracker command-finished facts', () => {
|
|
it('emits command-finished with best-effort exit codes', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk(`before${ESC}]133;A${BEL}prompt${ESC}]133;B${BEL}`)
|
|
tracker.handleChunk(`${ESC}]133;C${BEL}running${ESC}]133;D;0${BEL}`)
|
|
tracker.handleChunk(`${ESC}]133;D;130${BEL}`)
|
|
tracker.handleChunk(`${ESC}]133;D;not-a-number${BEL}`)
|
|
tracker.handleChunk(`${ESC}]133;D${BEL}`)
|
|
|
|
expect(events).toEqual([
|
|
['finished', 0],
|
|
['finished', 130],
|
|
['finished', null],
|
|
['finished', null]
|
|
])
|
|
})
|
|
|
|
it('detects OSC 133;D split across chunk boundaries (BEL and ST terminated)', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk(`chunk${ESC}]133`)
|
|
tracker.handleChunk(';D;1')
|
|
expect(events).toEqual([])
|
|
tracker.handleChunk(`30${BEL}rest`)
|
|
tracker.handleChunk(`${ESC}]133;D;7${ST}`)
|
|
|
|
expect(events).toEqual([
|
|
['finished', 130],
|
|
['finished', 7]
|
|
])
|
|
})
|
|
|
|
it('orders chunk facts titles → command-finished → bell', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk(`${ESC}]0;zsh${BEL}${ESC}]133;D;0${BEL}done${BEL}`)
|
|
|
|
expect(events).toEqual([['title', 'zsh'], ['finished', 0], ['bell']])
|
|
})
|
|
})
|
|
|
|
describe('createTerminalTitleTracker pr-link facts', () => {
|
|
it('emits one fact per PR URL including multiple links in one chunk', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk(
|
|
'see https://github.com/acme/orca/pull/42 and https://github.com/acme/orca/pull/43 \r\n'
|
|
)
|
|
|
|
expect(events).toEqual([
|
|
['pr', 'https://github.com/acme/orca/pull/42', 42],
|
|
['pr', 'https://github.com/acme/orca/pull/43', 43]
|
|
])
|
|
})
|
|
|
|
it('waits for a boundary when a URL splits across chunks and dedupes repeats', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk('PR: https://github.com/acme/orca/pull/4')
|
|
expect(events).toEqual([])
|
|
tracker.handleChunk('2\r\n')
|
|
tracker.handleChunk('again https://github.com/acme/orca/pull/42\r\n')
|
|
|
|
expect(events).toEqual([['pr', 'https://github.com/acme/orca/pull/42', 42]])
|
|
})
|
|
|
|
it('skips the 133/URL scans entirely when no consumer is registered', () => {
|
|
// Mirrors headless serve: no pty:sideEffect consumer means no callbacks,
|
|
// so the scanners must not be created (no carry state, no scan cost).
|
|
const titles: string[] = []
|
|
const tracker = createTerminalTitleTracker({
|
|
onTitle: (normalized) => titles.push(normalized)
|
|
})
|
|
|
|
tracker.handleChunk(`${ESC}]133;D;0${BEL}https://github.com/acme/orca/pull/42\r\n`)
|
|
|
|
expect(titles).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('createTerminalTitleTracker 2031-subscribe facts', () => {
|
|
it('emits a fact per chunk containing a DECSET 2031 subscribe, before the bell', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk(`${ESC}[?2031hready${BEL}`)
|
|
|
|
expect(events).toEqual([['2031-subscribe'], ['bell']])
|
|
})
|
|
|
|
it('detects a subscribe split across chunk boundaries', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk(`${ESC}[?20`)
|
|
tracker.handleChunk('31h')
|
|
|
|
expect(events).toEqual([['2031-subscribe']])
|
|
})
|
|
|
|
it('emits an unsubscribe fact so gated views can retire the subscription', () => {
|
|
// Gated views never see these bytes; without the fact their registry goes stale
|
|
// and a later theme flip pushes CSI 997 at a shell that already withdrew (#9993).
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk(`${ESC}[?2031l`)
|
|
|
|
expect(events).toEqual([['2031-unsubscribe']])
|
|
})
|
|
|
|
it('emits nothing for chunks that carry no 2031 bytes at all', () => {
|
|
// finalState is null here, not 'unsubscribed' — ordinary output must stay silent.
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk('plain build output\r\n')
|
|
|
|
expect(events).toEqual([])
|
|
})
|
|
|
|
it('reports only the chunk-final state when a chunk toggles 2031 both ways', () => {
|
|
// fish enables and disables 2031 around every prompt; one chunk, one decision.
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk(`${ESC}[?2031hprompt${ESC}[?2031l`)
|
|
tracker.handleChunk(`${ESC}[?2031lprompt${ESC}[?2031h`)
|
|
|
|
expect(events).toEqual([['2031-unsubscribe'], ['2031-subscribe']])
|
|
})
|
|
|
|
it('skips the 2031 scan entirely when no consumer is registered', () => {
|
|
const { events, tracker } = createRecordingTracker({
|
|
onMode2031Subscribe: undefined,
|
|
onMode2031Unsubscribe: undefined
|
|
})
|
|
|
|
tracker.handleChunk(`${ESC}[?2031h`)
|
|
|
|
expect(events).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('createTerminalTitleTracker synthetic-frame isolation', () => {
|
|
it('never feeds synthetic frames to the 133/PR scanners', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.applySyntheticTitleFrame(
|
|
`${ESC}]0;⠋ Cursor Agent${BEL}${ESC}]133;D;0${BEL}https://github.com/acme/orca/pull/42\r\n`
|
|
)
|
|
|
|
expect(events).toEqual([['title', '⠋ Cursor Agent']])
|
|
})
|
|
|
|
it('keeps a split 133 carry intact across an interleaved synthetic frame', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.handleChunk(`out${ESC}]133;D;`)
|
|
// An 80ms spinner tick lands between the two halves of the real OSC.
|
|
tracker.applySyntheticTitleFrame(`${ESC}]0;⠋ Cursor Agent${BEL}`)
|
|
tracker.handleChunk(`130${BEL}`)
|
|
|
|
expect(events).toEqual([
|
|
['title', '⠋ Cursor Agent'],
|
|
['finished', 130]
|
|
])
|
|
})
|
|
})
|
|
|
|
describe('createTerminalTitleTracker transient-fact scanning suppression', () => {
|
|
it('skips bell/133/pr-link/2031 while suppressed but keeps title processing', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.setTransientFactScanningSuppressed(true)
|
|
tracker.handleChunk(
|
|
`${ESC}]0;zsh${BEL}${ESC}]133;D;0${BEL}ding${BEL}https://github.com/acme/orca/pull/42\r\n${ESC}[?2031h`
|
|
)
|
|
|
|
expect(events).toEqual([['title', 'zsh']])
|
|
})
|
|
|
|
it('resets cross-chunk carry on un-suppress so a pre-gap half-open OSC cannot swallow bells', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
// The OSC terminator (and everything after) is lost in a delivery gap
|
|
// while scanning is suppressed.
|
|
tracker.handleChunk(`${ESC}]0;my long title without terminator`)
|
|
tracker.setTransientFactScanningSuppressed(true)
|
|
tracker.setTransientFactScanningSuppressed(false)
|
|
// Post-gap: a real standalone bell. A stale "inside OSC" state would
|
|
// swallow it.
|
|
tracker.handleChunk(`done${BEL}`)
|
|
|
|
expect(events).toEqual([['bell']])
|
|
})
|
|
|
|
it('a handoff scan seed re-primes a split sequence so its fact fires once, without a phantom bell', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
// The 133 intro was consumed before scan authority moved away; the carry
|
|
// is reset on un-suppress. The transport hands back the emulator's
|
|
// partial escape tail as a seed, so the continuation completes normally:
|
|
// one command-finished, and its terminator BEL is NOT a standalone bell.
|
|
tracker.handleChunk(`out${ESC}]133;D;`)
|
|
tracker.setTransientFactScanningSuppressed(true)
|
|
tracker.setTransientFactScanningSuppressed(false)
|
|
tracker.handleChunk(`${ESC}]133;D;`, { titleScanData: '' })
|
|
tracker.handleChunk(`130${BEL}`)
|
|
|
|
expect(events).toEqual([['finished', 130]])
|
|
})
|
|
|
|
it('a handoff scan seed preserves a provisional 2031 subscribe', () => {
|
|
const { events, tracker } = createRecordingTracker()
|
|
|
|
tracker.setTransientFactScanningSuppressed(true)
|
|
tracker.setTransientFactScanningSuppressed(false)
|
|
tracker.handleChunk(`${ESC}[?`, {
|
|
titleScanData: '',
|
|
mode2031PendingSubscribe: true
|
|
})
|
|
tracker.handleChunk('25h')
|
|
|
|
expect(events).toEqual([['2031-subscribe']])
|
|
})
|
|
})
|