fix(cloud): multi-line held typing is dropped not joined; skip continue after any typing into the pane; tell the person when held typing was not sent

This commit is contained in:
Neil
2026-11-09 08:59:41 -08:00
parent 4f24497f71
commit 96db2acee8
9 changed files with 254 additions and 114 deletions
@@ -24,11 +24,8 @@ import { CloudWorkspaceNotice } from './CloudWorkspaceNotice'
import { CloudWorkspaceOfflineView } from './offline/CloudWorkspaceOfflineView'
import { useCloudOfflineData } from './offline/use-cloud-offline-data'
import { useCloudWorkspaceView } from './use-cloud-workspace-view'
import { useWakeInput } from './use-wake-input'
import {
clearWakeInput,
subscribeWakeInputDelivered
} from '@/components/terminal-pane/input/wake-input-buffer'
import { useWakeInput, useWakeInputNotices } from './use-wake-input'
import { clearWakeInput } from '@/components/terminal-pane/input/wake-input-buffer'
import {
runRestartCloudWorkspace,
runUnarchiveCloudWorkspace,
@@ -229,21 +226,7 @@ function CloudSurface({ workspaceId }: { workspaceId: string }): React.JSX.Eleme
const now = useNow(1_000, view.wake !== undefined)
// Typing is accepted while a wake is in flight, so the terminal stays visible and typeable.
const wakeInput = useWakeInput(workspaceId)
useEffect(
() =>
subscribeWakeInputDelivered((delivery) => {
if (delivery.workspaceId === workspaceId && delivery.submitted) {
// Enter is never replayed into the woken terminal: the person runs it after a look.
toast(
t(
'orcaCloudWorkspace.typedWhileWaking',
'What you typed is on the prompt. Press Enter to run it.'
)
)
}
}),
[workspaceId, t]
)
useWakeInputNotices(workspaceId)
const overlay = useWakeOverlay(
view.wake,
Math.max(now, view.wake?.at ?? 0),
@@ -1,9 +1,13 @@
import { useSyncExternalStore } from 'react'
import { useEffect, useSyncExternalStore } from 'react'
import { toast } from 'sonner'
import { useTranslate } from '@/i18n/use-translate'
import {
getWakeInputCount,
getWakeInputVersion,
isWorkspaceWaking,
subscribeWakeInput
subscribeWakeInput,
subscribeWakeInputDelivered,
subscribeWakeInputDropped
} from '@/components/terminal-pane/input/wake-input-buffer'
/** Whether typing into this workspace is being held for its woken terminal, and how much. */
@@ -20,3 +24,30 @@ function readWakeInput(
): { waking: boolean; pending: number } {
return { waking: isWorkspaceWaking(workspaceId), pending: getWakeInputCount(workspaceId) }
}
/** Toasts for typing held during a wake: it is on the prompt (press Enter), or it was not sent. */
export function useWakeInputNotices(workspaceId: string): void {
const t = useTranslate()
useEffect(() => {
const offDelivered = subscribeWakeInputDelivered((delivery) => {
if (delivery.workspaceId === workspaceId && delivery.submitted) {
// Enter is never replayed into the woken terminal: the person runs it after a look.
toast(
t(
'orcaCloudWorkspace.typedWhileWaking',
'What you typed is on the prompt. Press Enter to run it.'
)
)
}
})
const offDropped = subscribeWakeInputDropped((dropped) => {
if (dropped.workspaceId === workspaceId) {
toast(t('orcaCloudWorkspace.typingNotSent', 'Your typing wasn’t sent.'))
}
})
return () => {
offDelivered()
offDropped()
}
}, [workspaceId, t])
}
@@ -10,7 +10,8 @@ import {
endWorkspaceWake,
getWakeInputCount,
isWorkspaceWaking,
paneReceivedWakeTyping,
paneHadUserInputSince,
subscribeWakeInputDropped,
pumpWakeInputForTests,
rebindWakeInputTarget,
resetWakeInputForTests,
@@ -56,7 +57,7 @@ describe('wake input buffer', () => {
pumpWakeInputForTests(T + 300)
expect(p.delivered).toEqual(['echo hi'])
expect(deliveries).toEqual([true])
expect(paneReceivedWakeTyping('leaf-1', T)).toBe(true)
expect(paneHadUserInputSince('leaf-1', T)).toBe(true)
})
it('keeps order for keys typed after the wake ends but before delivery', () => {
@@ -174,12 +175,16 @@ describe('applyWakeInput', () => {
const apply = (data: string, text = '') => applyWakeInput({ text, submitted: false }, data)
it('keeps plain text and Tab; notes Enter without keeping it', () => {
expect(apply('git st\tatus\r')).toEqual({ text: 'git st\tatus', submitted: true })
expect(apply('git st\tatus\r')).toEqual({
text: 'git st\tatus',
submitted: true,
multiline: false
})
})
it('Backspace edits the held text and Ctrl-C clears it', () => {
expect(apply('lss\x7f -la').text).toBe('ls -la')
expect(apply('\x03', 'rm -rf /')).toEqual({ text: '', submitted: false })
expect(apply('\x03', 'rm -rf /')).toEqual({ text: '', submitted: false, multiline: false })
})
it('drops escape sequences (arrows, OSC, alt keys) and other controls', () => {
@@ -190,3 +195,35 @@ describe('applyWakeInput', () => {
expect(apply('\x1bbz').text).toBe('z')
})
})
describe('nothing joined, nothing lost silently', () => {
it('drops a multi-line paste instead of joining its lines, and says so', () => {
const p = pane(false)
const dropped: string[] = []
subscribeWakeInputDropped((d) => void dropped.push(d.workspaceId))
beginWorkspaceWake('ws', T)
captureWakeInput('ws', 'leaf-1', 'cd a\rgit pull', p.deliver, p.isReady)
pumpWakeInputForTests(T)
p.state.ready = true
pumpWakeInputForTests(T + 1)
expect(p.delivered).toEqual([])
expect(dropped).toEqual(['ws'])
})
it('says so when held typing is dropped by a failed wake, the max hold or the grace', () => {
const dropped: string[] = []
subscribeWakeInputDropped((d) => void dropped.push(d.workspaceId))
const p = pane(false)
beginWorkspaceWake('a', T)
captureWakeInput('a', 'leaf-1', 'x', p.deliver, p.isReady)
endWorkspaceWake('a', 'failed')
beginWorkspaceWake('b', T)
captureWakeInput('b', 'leaf-1', 'x', p.deliver, p.isReady)
pumpWakeInputForTests(T + WAKE_INPUT_MAX_HOLD_MS + 1)
beginWorkspaceWake('c', T)
captureWakeInput('c', 'leaf-1', 'x', p.deliver, p.isReady)
endWorkspaceWake('c', 'ready')
pumpWakeInputForTests(Date.now() + WAKE_INPUT_SETTLED_GRACE_MS + 1)
expect(dropped.sort()).toEqual(['a', 'b', 'c'])
})
})
@@ -7,7 +7,9 @@
// and other control keys are dropped. Held typing is dropped when the wake fails, a while after it
// settles without a terminal to take it, and when the workspace wakes again.
export const WAKE_INPUT_CAP = 4096
import { applyWakeInput } from './wake-input-text'
export { WAKE_INPUT_CAP, applyWakeInput } from './wake-input-text'
const FLUSH_POLL_MS = 100
/** A wake nobody settles stops holding typing after this, and the typing is dropped. */
export const WAKE_INPUT_MAX_HOLD_MS = 90_000
@@ -18,6 +20,8 @@ type PaneBuffer = {
text: string
/** Enter was pressed: the person meant to submit; shown as "press Enter to run" after insertion. */
submitted: boolean
/** Text followed an Enter (a multi-line paste): joining the lines would change the command. */
multiline: boolean
deliver: (data: string) => void
isReady: () => boolean
/** The pane's transport was seen not ready during the wake, so "ready" means re-attached. */
@@ -32,6 +36,8 @@ type WorkspaceWake = {
}
export type WakeInputDelivery = { workspaceId: string; paneId: string; submitted: boolean }
/** Held typing that was not sent (wake failed, never re-attached, multi-line). */
export type WakeInputDropped = { workspaceId: string }
const wakes = new Map<string, WorkspaceWake>()
if (import.meta.env?.DEV && typeof window !== 'undefined') {
@@ -40,8 +46,10 @@ if (import.meta.env?.DEV && typeof window !== 'undefined') {
}
const listeners = new Set<() => void>()
const deliveryListeners = new Set<(delivery: WakeInputDelivery) => void>()
/** Panes whose typing was delivered after a wake (the continue logic leaves those alone). */
const typedPanes = new Map<string, number>()
const droppedListeners = new Set<(dropped: WakeInputDropped) => void>()
/** Last time a person's input reached each pane (layout leaf); the continue logic leaves those alone. */
const lastUserInputByPane = new Map<string, number>()
const USER_INPUT_MEMORY_MS = 30 * 60_000
let timer: ReturnType<typeof setInterval> | null = null
let version = 0
@@ -52,73 +60,6 @@ function changed(): void {
}
}
/**
* Applies typed `data` to held `text`: printable characters append, Backspace removes one, Ctrl-C
* clears, Enter/Tab are noted (Enter) or kept (Tab), everything else (escape sequences, other
* controls) is dropped.
*/
export function applyWakeInput(
held: { text: string; submitted: boolean },
data: string
): { text: string; submitted: boolean } {
let text = held.text
let submitted = held.submitted
for (let i = 0; i < data.length; i++) {
const ch = data[i]!
const code = ch.charCodeAt(0)
if (code === 0x1b) {
const next = data[i + 1]
if (next === '[') {
i += 2
while (i < data.length && !/[\x40-\x7e]/.test(data[i]!)) {
i++
}
} else if (next === ']') {
// OSC: up to BEL or ST (ESC \).
i += 2
while (
i < data.length &&
data[i] !== '\x07' &&
!(data[i] === '\x1b' && data[i + 1] === '\\')
) {
i++
}
if (data[i] === '\x1b') {
i++
}
} else if (next === 'O') {
i += 2
} else if (next !== undefined) {
i += 1
}
continue
}
if (code === 0x03) {
text = ''
submitted = false
continue
}
if (code === 0x7f || code === 0x08) {
text = Array.from(text).slice(0, -1).join('')
continue
}
if (code === 0x0d || code === 0x0a) {
// Never replayed: the person submits after reviewing what landed.
submitted = text.length > 0
continue
}
if (code === 0x09) {
text += ch
continue
}
if (code < 0x20 || (code >= 0x80 && code < 0xa0)) {
continue
}
text += ch
}
return { text: text.slice(0, WAKE_INPUT_CAP), submitted }
}
function ensureTimer(): void {
if (timer) {
return
@@ -134,9 +75,32 @@ function stopTimerIfIdle(): void {
}
}
function drop(workspaceId: string): void {
if (wakes.delete(workspaceId)) {
changed()
function hasHeldText(wake: WorkspaceWake): boolean {
for (const pane of wake.panes.values()) {
if (pane.text) {
return true
}
}
return false
}
/** Drops a workspace's held typing; tells the window when keys were actually lost. */
function discard(workspaceId: string, wake: WorkspaceWake, notify: boolean): void {
const lost = notify && hasHeldText(wake)
wakes.delete(workspaceId)
pruneUserInput()
if (lost) {
for (const listener of droppedListeners) {
listener({ workspaceId })
}
}
}
function pruneUserInput(now = Date.now()): void {
for (const [paneId, at] of lastUserInputByPane) {
if (now - at > USER_INPUT_MEMORY_MS) {
lastUserInputByPane.delete(paneId)
}
}
}
@@ -146,17 +110,17 @@ function pump(now = Date.now()): void {
for (const [workspaceId, wake] of wakes) {
if (wake.outcome === 'pending' && now - wake.startedAt > WAKE_INPUT_MAX_HOLD_MS) {
// Nobody settled this wake: whatever it attaches to now is unknown, so nothing is sent.
wakes.delete(workspaceId)
discard(workspaceId, wake, true)
didChange = true
continue
}
if (wake.outcome === 'failed') {
wakes.delete(workspaceId)
discard(workspaceId, wake, true)
didChange = true
continue
}
if (wake.settledAt !== null && now - wake.settledAt > WAKE_INPUT_SETTLED_GRACE_MS) {
wakes.delete(workspaceId)
discard(workspaceId, wake, true)
didChange = true
continue
}
@@ -183,12 +147,19 @@ function pump(now = Date.now()): void {
if (!pane.sawUnready && wake.outcome !== 'ready') {
continue
}
const { text, submitted } = pane
const { text, submitted, multiline } = pane
wake.panes.delete(paneId)
didChange = true
if (multiline) {
// Several lines held: inserting them joined would change the command; send nothing.
for (const listener of droppedListeners) {
listener({ workspaceId })
}
continue
}
try {
pane.deliver(text)
typedPanes.set(paneId, now)
lastUserInputByPane.set(paneId, now)
for (const listener of deliveryListeners) {
listener({ workspaceId, paneId, submitted })
}
@@ -197,7 +168,7 @@ function pump(now = Date.now()): void {
}
}
if (wake.outcome !== 'pending' && wake.panes.size === 0) {
wakes.delete(workspaceId)
discard(workspaceId, wake, false)
didChange = true
}
}
@@ -265,6 +236,7 @@ export function captureWakeInput(
const pane: PaneBuffer = existing ?? {
text: '',
submitted: false,
multiline: false,
deliver,
isReady,
sawUnready: false
@@ -274,6 +246,7 @@ export function captureWakeInput(
const next = applyWakeInput(pane, data)
pane.text = next.text
pane.submitted = next.submitted
pane.multiline = next.multiline
wake.panes.set(paneId, pane)
ensureTimer()
changed()
@@ -305,7 +278,8 @@ export function clearWakeInput(workspaceId: string): void {
}
wake.panes.clear()
if (wake.outcome !== 'pending') {
drop(workspaceId)
discard(workspaceId, wake, false)
changed()
return
}
changed()
@@ -324,9 +298,14 @@ export function getWakeInputCount(workspaceId: string | null | undefined): numbe
return count
}
/** True when typing went to this pane after its last wake (since `since`). */
export function paneReceivedWakeTyping(paneId: string, since: number): boolean {
const at = typedPanes.get(paneId)
/** Any person's input reached this pane (layout leaf). Called for every admitted keystroke. */
export function noteUserInputForPane(paneId: string, at = Date.now()): void {
lastUserInputByPane.set(paneId, at)
}
/** True when a person's input reached this pane since `since` (wake typing included). */
export function paneHadUserInputSince(paneId: string, since: number): boolean {
const at = lastUserInputByPane.get(paneId)
return at !== undefined && at >= since
}
@@ -342,6 +321,13 @@ export function subscribeWakeInputDelivered(
return () => deliveryListeners.delete(listener)
}
export function subscribeWakeInputDropped(
listener: (dropped: WakeInputDropped) => void
): () => void {
droppedListeners.add(listener)
return () => droppedListeners.delete(listener)
}
export function getWakeInputVersion(): number {
return version
}
@@ -349,7 +335,7 @@ export function getWakeInputVersion(): number {
/** Tests: drops every wake and stops the pump. */
export function resetWakeInputForTests(): void {
wakes.clear()
typedPanes.clear()
lastUserInputByPane.clear()
if (timer) {
clearInterval(timer)
timer = null
@@ -0,0 +1,78 @@
// What typing held for a waking terminal keeps: plain text and Tab; Backspace edits, Ctrl-C clears,
// Enter is noted (never replayed), text after an Enter marks a second line; escape sequences and
// other control keys are dropped.
export const WAKE_INPUT_CAP = 4096
/**
* Applies typed `data` to held `text`: printable characters append, Backspace removes one, Ctrl-C
* clears, Enter/Tab are noted (Enter) or kept (Tab), everything else (escape sequences, other
* controls) is dropped.
*/
export function applyWakeInput(
held: { text: string; submitted: boolean; multiline?: boolean },
data: string
): { text: string; submitted: boolean; multiline: boolean } {
let text = held.text
let submitted = held.submitted
let multiline = held.multiline ?? false
for (let i = 0; i < data.length; i++) {
const ch = data[i]!
const code = ch.charCodeAt(0)
if (code === 0x1b) {
const next = data[i + 1]
if (next === '[') {
i += 2
while (i < data.length && !/[\x40-\x7e]/.test(data[i]!)) {
i++
}
} else if (next === ']') {
// OSC: up to BEL or ST (ESC \).
i += 2
while (
i < data.length &&
data[i] !== '\x07' &&
!(data[i] === '\x1b' && data[i + 1] === '\\')
) {
i++
}
if (data[i] === '\x1b') {
i++
}
} else if (next === 'O') {
i += 2
} else if (next !== undefined) {
i += 1
}
continue
}
if (code === 0x03) {
text = ''
submitted = false
multiline = false
continue
}
if (code === 0x7f || code === 0x08) {
text = Array.from(text).slice(0, -1).join('')
continue
}
if (code === 0x0d || code === 0x0a) {
// Never replayed: the person submits after reviewing what landed.
submitted = text.length > 0
continue
}
if (code === 0x09) {
text += ch
continue
}
if (code < 0x20 || (code >= 0x80 && code < 0xa0)) {
continue
}
if (submitted) {
// Text after an Enter: a second line. It is never joined onto the first.
multiline = true
}
text += ch
}
return { text: text.slice(0, WAKE_INPUT_CAP), submitted, multiline }
}
@@ -21,6 +21,7 @@ import { recordAdmittedTerminalUserInput } from '@/lib/telemetry'
import {
captureWakeInput,
hasWakeInput,
noteUserInputForPane,
rebindWakeInputTarget
} from '../../input/wake-input-buffer'
import { findColdRestoreResumeSource } from '../startup/cold-restore-resume-startup'
@@ -184,6 +185,9 @@ export function installPtyInputForward(session: ConnectPanePtySession): void {
session.onDataDisposable = subscribeToTerminalInputData(
session.pane.terminal,
(data, wasUserInput) => {
if (wasUserInput) {
noteUserInputForPane(wakeInputKey)
}
// Typing into a workspace whose server is waking is accepted now and delivered, in order,
// once this pane is attached to the woken terminal.
if (
+2 -1
View File
@@ -26203,7 +26203,8 @@
"idleSleptMinutes": "{{name}} slept after {{count}} minutes idle",
"idleSleptNoTime": "{{name}} went to sleep while idle",
"idleSleptDetail": "Files and chats are saved. It uses no sandbox hours until you open it.",
"typedWhileWaking": "What you typed is on the prompt. Press Enter to run it."
"typedWhileWaking": "What you typed is on the prompt. Press Enter to run it.",
"typingNotSent": "Your typing wasn’t sent."
},
"cloudAgentSetup": {
"loading": "Checking what agents can use…",
@@ -254,3 +254,23 @@ describe('safety', () => {
expect(submit).not.toHaveBeenCalled()
})
})
describe('person in charge', () => {
it('leaves the relaunched agent alone once a person typed into it after the restart', async () => {
const buffer = await import('@/components/terminal-pane/input/wake-input-buffer')
state.agentStatusByPaneKey = { 'tab-a:1': working('tab-a:1', 'codex') }
armInterruptedTurnsForHostRestart('ws', NOW)
buffer.noteUserInputForPane('1', NOW + 3)
state.agentStatusByPaneKey = {
'tab-a:1': working('tab-a:1', 'codex', {
state: 'done',
updatedAt: NOW + 1,
stateStartedAt: NOW + 1
})
}
const { submit, send, deps: d } = deps()
expect(await continueInterruptedTurns('ws', d)).toBe(0)
expect(submit).not.toHaveBeenCalled()
expect(send).not.toHaveBeenCalled()
})
})
@@ -19,7 +19,7 @@ import {
isInterruptedTurnContinued,
markInterruptedTurnContinued
} from '@/components/terminal-pane/pty-connection/startup/cold-restore-resume-startup'
import { paneReceivedWakeTyping } from '@/components/terminal-pane/input/wake-input-buffer'
import { paneHadUserInputSince } from '@/components/terminal-pane/input/wake-input-buffer'
import { inspectRuntimeTerminalProcess } from '@/runtime/hosted-terminals/inspection/runtime-terminal-process-inspection'
import {
isAgentForegroundWrapperProcess,
@@ -159,9 +159,9 @@ async function continueOne(turn: ArmedTurn, deps: ContinueDeps): Promise<boolean
while (deps.now() < deadline) {
if (
continuedOnItsOwn(turn.paneKey, turn.armedAt) ||
// A cold-restore resume already carried `continue`, or the person is driving the pane.
// A cold-restore resume already carried `continue`, or a person typed into the pane since.
isInterruptedTurnContinued(turn.turnKey) ||
paneReceivedWakeTyping(leafOf(turn.paneKey), turn.armedAt)
paneHadUserInputSince(leafOf(turn.paneKey), turn.armedAt)
) {
return false
}