From 3e5d450a1ea79d8a6df2cb42e2d805c1b39b5c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A5=87?= <16399091+binaricat@users.noreply.github.com> Date: Mon, 26 Oct 2026 19:00:02 +0000 Subject: [PATCH] fix(plugins): publish selection targets after runtime mount and wake --- components/Terminal.tsx | 7 ++++- .../terminalCursorRuntimeLifecycle.test.tsx | 30 +++++++++++++++++++ components/terminal/useTerminalEffects.ts | 3 ++ .../terminal-sharing-validation.md | 13 ++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/components/Terminal.tsx b/components/Terminal.tsx index ae17a3519..53cdc90d6 100644 --- a/components/Terminal.tsx +++ b/components/Terminal.tsx @@ -1,3 +1,4 @@ +import { publishTerminalSelectionTarget } from "../infrastructure/plugins/terminalSelectionRegistry"; import { usePluginTerminalSelection } from "../application/state/usePluginTerminalSelection"; import { clearTerminalBroadcastUserInput, markTerminalBroadcastUserInput } from "./terminal/runtime/terminalPacedBroadcast"; import { shouldBroadcastDuringSensitivePrompt } from "../domain/terminalBroadcast"; @@ -1981,6 +1982,7 @@ const TerminalComponent: React.FC = ({ serializeAddonRef.current = null; searchAddonRef.current = null; hasRuntimeRef.current = false; + publishTerminalSelectionTarget(); }; const clearHibernateRuntimeState = useCallback(() => { @@ -4722,7 +4724,10 @@ const TerminalComponent: React.FC = ({ updateStatus: (next) => updateStatusRef.current(next), replayChunkBytes: resolveTerminalHibernateReplayChunkBytes(terminalSettings), additionalKeywordHighlightRules: pluginDecorationRulesRef.current, - }).then((ok) => ok).catch((err) => { + }).then((ok) => { + publishTerminalSelectionTarget(); + return ok; + }).catch((err) => { logger.error("[Terminal] Failed to resume from hibernate", err); return false; }).finally(() => { diff --git a/components/terminal/terminalCursorRuntimeLifecycle.test.tsx b/components/terminal/terminalCursorRuntimeLifecycle.test.tsx index 7ee4ddc8c..e639ba4f6 100644 --- a/components/terminal/terminalCursorRuntimeLifecycle.test.tsx +++ b/components/terminal/terminalCursorRuntimeLifecycle.test.tsx @@ -212,3 +212,33 @@ test("first width-only update after actual runtime creation preserves an applica await act(async () => renderer!.unmount()); }); + +test('the real runtime mount publishes a late terminal ref without another focus or status render', async () => { + const { usePluginTerminalSelection } = await import('../../application/state/usePluginTerminalSelection'); + const element = document.createElement('div'); document.body.append(element); + const term = Object.assign(createTerm(), { element }); + const { context, termRef } = createContext(term, { cursorShape: 'block' }); + const previous = Object.getOwnPropertyDescriptor(window, 'netcatty'); + const targets: Array<{ sessionId: string | null }> = []; + Object.defineProperty(window, 'netcatty', { configurable: true, value: { + async setPluginTerminalTarget(target: { sessionId: string | null }) { targets.push(target); return true; }, + } }); + function SelectionHarness() { + usePluginTerminalSelection('late-mount', () => ({ sessionId: 'cursor-test', term: termRef.current as never, + bootEpoch: 1, visible: true, focused: true, connected: true, sensitive: false, normalize: false, label: 'Synthetic mount' }), 'unchanged-focus-status'); + context.useEffect = useEffect; context.useLayoutEffect = useLayoutEffect; + useTerminalEffects(context); + return null; + } + let renderer: ReturnType | undefined; + try { + await act(async () => { renderer = create(); await Promise.resolve(); await Promise.resolve(); }); + assert.equal(termRef.current, term); + assert.ok(targets.some((target) => target.sessionId === 'cursor-test'), 'runtime assignment must publish after the earlier null ref'); + } finally { + await act(async () => renderer?.unmount()); + assert.equal(targets.at(-1)?.sessionId, null); + element.remove(); + if (previous) Object.defineProperty(window, 'netcatty', previous); else Reflect.deleteProperty(window, 'netcatty'); + } +}); diff --git a/components/terminal/useTerminalEffects.ts b/components/terminal/useTerminalEffects.ts index 94ce4feba..58f91e857 100644 --- a/components/terminal/useTerminalEffects.ts +++ b/components/terminal/useTerminalEffects.ts @@ -1,5 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any, react-hooks/exhaustive-deps */ import { useRef } from 'react'; +import { publishTerminalSelectionTarget } from '../../infrastructure/plugins/terminalSelectionRegistry'; import type { TerminalSettings } from '../../types'; import { publishPluginTerminalRuntimeLifecycleEvent } from '../../application/state/pluginTerminalRuntimeLifecycle'; import { markTerminalCommandCompletionPending } from './runtime/promptLineBreak'; @@ -517,6 +518,7 @@ export function useTerminalEffects(ctx: TerminalEffectsContext) { searchAddonRef.current = null; hasRuntimeRef.current = false; previousCursorPreferenceRef.current = null; + publishTerminalSelectionTarget(); } try { runtime.dispose(); @@ -640,6 +642,7 @@ export function useTerminalEffects(ctx: TerminalEffectsContext) { serializeAddonRef.current = runtime.serializeAddon; searchAddonRef.current = runtime.searchAddon; hasRuntimeRef.current = true; + publishTerminalSelectionTarget(); // Runtime construction already applied the current cursor shape and blink // settings. Publishing records that snapshot so a first width-only update // cannot clear a DEC/vi cursor-style override emitted after boot. diff --git a/docs/plugin-platform/terminal-sharing-validation.md b/docs/plugin-platform/terminal-sharing-validation.md index 1521e602b..1896214e0 100644 --- a/docs/plugin-platform/terminal-sharing-validation.md +++ b/docs/plugin-platform/terminal-sharing-validation.md @@ -16,6 +16,7 @@ main branch, or release was rewritten or published. | `npm run test:plugin-examples` | Five passed, one existing opt-in database skip | | Focused renderer, worker-manager, and #3991 popup input-mode tests | 119 passed | | Final selection reader, host-dialog, and manifest tests | Five passed | +| Late runtime-mount regression, selection/dialog, and popup input-mode checks | 11 passed | | Generated plugin contract, capability tools, Codex schema checks | Passed; tool catalogs unchanged | | `npm run lint` | Zero errors, seven existing warnings | | `npm run build` | Passed; existing chunk-size warning | @@ -40,6 +41,18 @@ Standalone `tsc --noEmit` remains outside the clean repository checks. A compari with the merged baseline reports the same 447 normalized diagnostics, with no new or removed diagnostic. Package builds and the prescribed production build pass. +A real runtime-mount regression failed against the first published head +`3cfc7db3716b42f346e2819c822f546e949e1a88`: the selection hook saw a null ref before +asynchronous xterm creation, without a later focus/status render. Publication now +also runs after runtime assignment, teardown, and hibernate wake completion. +The regression and the associated popup input-mode checks pass. This uses the +existing mount and wake paths and adds no polling or terminal reader fallback. + +That earlier head passed [test CI](https://github.com/binaricat/Netcatty/actions/runs/40380234637) +with 15,316 tests, 15,266 passed, 50 skipped, and zero failures. All four +[platform package jobs](https://github.com/binaricat/Netcatty/actions/runs/40380234655) +also passed. The Draft PR records fresh exact-head results for the lifecycle fix. + ## Packaging and native acceptance Local `npm run pack:dir` completed its production build, then stopped while