From 4910fe981520cb33272d8bf5d2ebe246fc756ea8 Mon Sep 17 00:00:00 2001 From: StuBehan Date: Tue, 24 Nov 2026 23:37:26 +0000 Subject: [PATCH] fix(extensions): hold a restarted service's keys until it sends a document --- .../ExtensionHostServiceTests.swift | 22 +++++++++++++++++++ docs/design/service-extensions.md | 5 +++-- docs/extensions.md | 3 ++- panel/ExtensionHost.swift | 12 +++++++++- panel/ServiceSupervisor.swift | 6 ++--- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Tests/StackNudgePanelCoreTests/ExtensionHostServiceTests.swift b/Tests/StackNudgePanelCoreTests/ExtensionHostServiceTests.swift index 2b91be2..2da2163 100644 --- a/Tests/StackNudgePanelCoreTests/ExtensionHostServiceTests.swift +++ b/Tests/StackNudgePanelCoreTests/ExtensionHostServiceTests.swift @@ -131,6 +131,28 @@ final class ExtensionHostServiceTests: XCTestCase { XCTAssertTrue(first.messages.filter { $0["type"] as? String == "action" }.isEmpty) } + // Running again, the tab still shows the dead process's document until + // the new one sends its own, and a key resolved against it would reach a + // process that never offered that action. + func testAKeyWaitsForTheRestartedProcesssOwnDocument() { + tiers = ["exchange": .service] + let host = host() + host.load(.init(installed: [manifest("exchange")])) + spawner.last.ready() + spawner.last.say(#"{"type":"document","document":{"schema":1,"actions":[{"id":"sync","label":"Sync","key":"s"}]}}"#) + spawner.last.exit(.exited(1)) + if case .restarting(let delay) = host.services["exchange"] { clock.advance(delay) } + let second = spawner.last + second.ready() + XCTAssertEqual(host.services["exchange"], .running) + XCTAssertNotNil(host.pane("exchange").document) + XCTAssertFalse(host.tabKeys("exchange").actions) + XCTAssertFalse(host.handle(key: "s", on: "exchange")) + second.say(#"{"type":"document","document":{"schema":1,"actions":[{"id":"sync","label":"Sync","key":"s"}]}}"#) + XCTAssertTrue(host.handle(key: "s", on: "exchange")) + XCTAssertEqual(second.messages.filter { $0["type"] as? String == "action" }.count, 1) + } + // MARK: - Documents func testADocumentFillsThePaneLikeAFetchDoes() { diff --git a/docs/design/service-extensions.md b/docs/design/service-extensions.md index 278a764..c976337 100644 --- a/docs/design/service-extensions.md +++ b/docs/design/service-extensions.md @@ -132,8 +132,9 @@ user saves the extension's settings, so apply each one as it comes. Events are d `document` exactly as a view's is: the selected row's binding first, then the document's. It's what a view gets as `run --action approve --row d1`, and `row` is left out for a document-level action. Sent only while the extension is -running; a key pressed while it starts or restarts sends nothing, since the -document on screen came from the process before. It grants nothing, so it needs +running and has sent a document of its own: a key pressed while it starts or +restarts, or after a restart before its first document, sends nothing, since +the document on screen came from the process before. It grants nothing, so it needs no capability. Additive: an extension that predates it skips the type. **Extension → app (stdout)** diff --git a/docs/extensions.md b/docs/extensions.md index 3837957..0e673db 100644 --- a/docs/extensions.md +++ b/docs/extensions.md @@ -322,7 +322,8 @@ are ignored rather than queued. A service extension isn't run again. Its running process is sent the same press as a line, `{"type": "action", "id": "open", "row": "h1"}`, and answers with a new `document` if what it shows has changed. A press while it's starting -or restarting sends nothing. See the design note's +or restarting, or before a restarted process has sent its first document, sends +nothing. See the design note's [Protocol](design/service-extensions.md#protocol). #### Key bindings are requested, not granted diff --git a/panel/ExtensionHost.swift b/panel/ExtensionHost.swift index eae6d1b..08f846e 100644 --- a/panel/ExtensionHost.swift +++ b/panel/ExtensionHost.swift @@ -146,6 +146,10 @@ final class ExtensionHost: ObservableObject { private var stopping: [String: ServiceSupervisor] = [:] private var wanted: [String: (manifest: ExtensionManifest, allowed: [ExtensionCapability])] = [:] private var shuttingDown = false + // Services whose tab shows a document from the process running now. One + // started since its last document still shows the dead process's, which + // its keys must not be resolved against. + private var freshDocument: Set = [] private var extensionLog = ExtensionLog() private let writeLog: (String) -> Void private let now: () -> TimeInterval @@ -569,7 +573,11 @@ final class ExtensionHost: ObservableObject { } func tabKeys(_ id: String) -> TabKeys { - Self.tabKeys(mode: manifest(id)?.mode ?? .view, state: services[id]) + var keys = Self.tabKeys(mode: manifest(id)?.mode ?? .view, state: services[id]) + // Running again after a restart, but still showing what the process + // before it sent: its actions wait for the new process's document. + if manifest(id)?.mode == .service, !freshDocument.contains(id) { keys.actions = false } + return keys } // A fresh supervisor, so a fresh crash window: the user has asked, and @@ -679,6 +687,7 @@ final class ExtensionHost: ObservableObject { services[id] = state serviceStateChanged(id, state) case .document(let document): + freshDocument.insert(id) // Exactly the path a view fetch takes, so the tab can't tell them apart. finish(id, .ok(document)) case .malformedDocument(let why): @@ -712,6 +721,7 @@ final class ExtensionHost: ObservableObject { var pane = self.pane(id) switch state { case .starting: + freshDocument.remove(id) if pane.document == nil { pane.status = .loading } case .running: pane.status = pane.document == nil ? .loading : .idle diff --git a/panel/ServiceSupervisor.swift b/panel/ServiceSupervisor.swift index 074bd09..554689d 100644 --- a/panel/ServiceSupervisor.swift +++ b/panel/ServiceSupervisor.swift @@ -292,9 +292,9 @@ final class ServiceSupervisor { } } - // Passes on a key the user pressed on the tab. Dropped unless running: a - // press made while it starts or restarts was made against a document from - // a process that has gone, so it may not mean the same thing to the next. + // Passes on a key the user pressed on the tab. The host offers keys only + // once this process has sent a document of its own (ExtensionHost.tabKeys); + // this drops one still on its way when the process stopped. func perform(action id: String, row: String?) { perform { [weak self] in guard let self, self.state == .running else { return }