diff --git a/Sources/InstanceMCPCore/Browser/BrowserWizardCopy.swift b/Sources/InstanceMCPCore/Browser/BrowserWizardCopy.swift index 4f51cb7..a644f88 100644 --- a/Sources/InstanceMCPCore/Browser/BrowserWizardCopy.swift +++ b/Sources/InstanceMCPCore/Browser/BrowserWizardCopy.swift @@ -79,7 +79,7 @@ extension WizardCopy { /// `PlaywrightInstaller.remove()`: only items that carry the installer's marker, or that an `install.json` /// written by the installer owns. A foreign or unreadable `install.json`, and the profile, stay. public static func removalConfirmation(path: String, home: String = NSHomeDirectory()) -> String { - "This removes what this app installed in \(displayPath(path, home: home)). Version folders that carry this app's marker (Node.js, the Playwright MCP server and the downloaded Chromium) are removed even if install.json is unreadable, invalid or made by hand. The browser profile with its cookies and sign-ins, the output folder and install.json are removed only when a readable install.json written by this app confirms it created them. Anything else stays, and it does not touch Chrome, Safari or ~/.local/oab-computer/. Agents lose the browser tools until you install it again. If you had chosen your installed Google Chrome, that choice is removed too, and Chrome itself is left as it is." + "This removes what this app installed in \(displayPath(path, home: home)). Version folders that carry this app's marker (Node.js, the Playwright MCP server and the downloaded Chromium) are removed even if install.json is unreadable, invalid or made by hand. The browser profile with its cookies and sign-ins, the output folder and install.json are removed only when a readable install.json written by this app confirms it created them. Anything else stays, and it does not touch Chrome, Safari or ~/.local/oab-computer/. Agents lose the browser tools until you install it again. If you had chosen your installed Google Chrome, that choice is removed too when install.json is removed, and Chrome itself is left as it is." } public static let removalDone = "Playwright was removed." diff --git a/Sources/InstanceMCPCore/Browser/InstalledChrome.swift b/Sources/InstanceMCPCore/Browser/InstalledChrome.swift index 467e8b9..8900405 100644 --- a/Sources/InstanceMCPCore/Browser/InstalledChrome.swift +++ b/Sources/InstanceMCPCore/Browser/InstalledChrome.swift @@ -79,7 +79,8 @@ public struct SecurityFrameworkChromeVerifier: ChromeSignatureVerifying { public protocol InstalledChromeChecking: Sendable { /// Cheap: a `Google Chrome.app` exists in an allowed folder. No signature check; used to decide whether to offer. func isInstalled() -> Bool - /// The first allowed `Google Chrome.app`, fully checked. + /// The first allowed `Google Chrome.app` that passes every check, in priority order. If none passes, the + /// problem of the first existing copy. func check() -> Result /// A recorded path, fully checked. A path that is not one of the allowed locations is refused. func check(appPath: String) -> Result @@ -121,8 +122,15 @@ public final class InstalledChromeChecker: InstalledChromeChecking, @unchecked S } public func check() -> Result { - guard let first = candidates().first(where: { Self.kind($0.path) != nil }) else { return .failure(.notFound) } - return check(appPath: first.path) + // The first existing copy in priority order that passes; otherwise the first existing copy's problem. + var firstProblem: InstalledChromeProblem? + for candidate in candidates() where Self.kind(candidate.path) != nil { + switch check(appPath: candidate.path) { + case .success(let chrome): return .success(chrome) + case .failure(let problem): firstProblem = firstProblem ?? problem + } + } + return .failure(firstProblem ?? .notFound) } private func plist(_ app: URL) -> [String: Any]? { diff --git a/Sources/InstanceMCPCore/Browser/PlaywrightBackend.swift b/Sources/InstanceMCPCore/Browser/PlaywrightBackend.swift index 3107e08..98577e4 100644 --- a/Sources/InstanceMCPCore/Browser/PlaywrightBackend.swift +++ b/Sources/InstanceMCPCore/Browser/PlaywrightBackend.swift @@ -91,6 +91,7 @@ public actor PlaywrightBackend: BrowserBackend { switch m.locator.locate() { case .failure(let e): health = nil; toolCache = nil + if InstalledChromeProblem.classes.contains(e.errorClass) { log("browser: chrome_check_failed class=\(e.errorClass)") } await shutdown() return .failure(e) case .success(let spec): diff --git a/Tests/InstanceMCPCoreTests/BrowserWizardWiringTests.swift b/Tests/InstanceMCPCoreTests/BrowserWizardWiringTests.swift index f8552b6..f73fbf4 100644 --- a/Tests/InstanceMCPCoreTests/BrowserWizardWiringTests.swift +++ b/Tests/InstanceMCPCoreTests/BrowserWizardWiringTests.swift @@ -30,6 +30,12 @@ final class BrowserWizardWiringTests: InstallerTestCase { // MARK: BrowserToolsController.remove() + func testRemoveCopySaysTheChoiceGoesOnlyWhenInstallJSONIsRemoved() { + let text = WizardCopy.removalConfirmation(path: "/tmp/oab-playwright", home: "/home/example") + XCTAssertTrue(text.contains("that choice is removed too when install.json is removed"), text) + XCTAssertFalse(text.contains("that choice is removed too, and"), text) + } + func testRemoveReReadsTheStateAfterRemovalAndReportsRemoved() async throws { let fx = try FakeInstallFixture() let installer = fx.installer(root: root) diff --git a/Tests/InstanceMCPCoreTests/FakeInstallFixture.swift b/Tests/InstanceMCPCoreTests/FakeInstallFixture.swift index 661b186..8e540fe 100644 --- a/Tests/InstanceMCPCoreTests/FakeInstallFixture.swift +++ b/Tests/InstanceMCPCoreTests/FakeInstallFixture.swift @@ -179,7 +179,7 @@ final class FakeInstallFixture { lockTries: Int = 50, npmTimeout: TimeInterval = 30, healthTimeout: TimeInterval = 20, now: @escaping @Sendable () -> Date = { Date() }, downloader: (any ArtifactDownloader)? = nil, lockfile: String? = nil, - chromeChecker: any InstalledChromeChecking = InstalledChromeChecker()) -> PlaywrightInstaller { + chromeChecker: any InstalledChromeChecking = InstalledChromeChecker(directories: [])) -> PlaywrightInstaller { let man = m ?? manifest! return PlaywrightInstaller( root: root, manifest: man, archProvider: FixedArch(.arm64), diff --git a/Tests/InstanceMCPCoreTests/InstalledChromeTests.swift b/Tests/InstanceMCPCoreTests/InstalledChromeTests.swift index b991dcb..1ea5bed 100644 --- a/Tests/InstanceMCPCoreTests/InstalledChromeTests.swift +++ b/Tests/InstanceMCPCoreTests/InstalledChromeTests.swift @@ -136,6 +136,38 @@ final class InstalledChromeCheckerTests: XCTestCase { XCTAssertEqual(v.calls.value, 5) } + func testAValidCopyInHomeApplicationsIsUsedWhenTheSystemCopyFails() throws { + struct PathVerifier: ChromeSignatureVerifying { + let valid: String + func verify(app: URL) -> Bool { app.standardizedFileURL.path == valid } + } + let system = try FakeChromeBundle() + let home = try FakeChromeBundle() + let v = PathVerifier(valid: home.app.standardizedFileURL.path) + let both = InstalledChromeChecker(directories: [system.applications, home.applications], verifier: v) + guard case .success(let chrome) = both.check() else { return XCTFail("the ~/Applications copy is valid and must be used") } + XCTAssertEqual(chrome.appPath, home.app.standardizedFileURL.path) + // Both copies fail with different classes: the system copy is first in priority, so its class is reported. + try system.write(version: "100.0.0") + let oldFirst = InstalledChromeChecker(directories: [system.applications, home.applications], + verifier: PathVerifier(valid: system.app.standardizedFileURL.path)) + XCTAssertEqual(oldFirst.check(), .failure(.tooOld(found: 100, minimum: 120))) + } + + func testChromeProblemDuringBrowserUseIsLoggedWithItsClass() async throws { + let tree = try InstallTree() + let chrome = try FakeChromeBundle() + let record = "{\"version\":\"1.0\",\"chromiumExecutable\":\"chromium/chrome\"," + + "\"systemChrome\":{\"appPath\":\"\(chrome.app.path)\",\"versionAtConsent\":\"157.0.8103.79\"}}" + try record.write(to: tree.root.appendingPathComponent("install.json"), atomically: true, encoding: .utf8) + let logs = MutableBox<[String]>([]) + let backend = PlaywrightBackend(managed: ManagedPlaywrightLocator(root: tree.root, chrome: chrome.checker(StubChromeVerifier(false))), + log: { logs.value += [$0] }) + _ = try? await backend.call("browser_navigate", arguments: .object([:])) + XCTAssertTrue(logs.value.contains("browser: chrome_check_failed class=chrome_invalid_signature"), "\(logs.value)") + XCTAssertFalse(logs.value.joined().contains(chrome.app.path), "no path in a log line") + } + func testTheRequirementNamesGoogleLLCAndTheRealVerifierRefusesAnUnsignedBundle() throws { let r = InstalledChromePolicy.requirement XCTAssertTrue(r.contains("anchor apple generic")) diff --git a/Tests/InstanceMCPCoreTests/PlaywrightInstallerTests.swift b/Tests/InstanceMCPCoreTests/PlaywrightInstallerTests.swift index fd9ca50..461110d 100644 --- a/Tests/InstanceMCPCoreTests/PlaywrightInstallerTests.swift +++ b/Tests/InstanceMCPCoreTests/PlaywrightInstallerTests.swift @@ -78,6 +78,13 @@ final class PlaywrightInstallerTests: InstallerTestCase { } } + func testFixtureDefaultCheckerNeverReadsRealApplications() throws { + let fx = try FakeInstallFixture() + let installer = fx.installer(root: root) + XCTAssertFalse(installer.chromeChecker.isInstalled()) + XCTAssertEqual(installer.chromeChecker.check(), .failure(.notFound)) + } + func testFullInstall() async throws { let fx = try FakeInstallFixture() let logs = MutableBox<[String]>([]) diff --git a/poc/reverse-attach-linux/src/probe_tests.rs b/poc/reverse-attach-linux/src/probe_tests.rs index ef369da..449d4b2 100644 --- a/poc/reverse-attach-linux/src/probe_tests.rs +++ b/poc/reverse-attach-linux/src/probe_tests.rs @@ -107,7 +107,18 @@ fn a_tree_that_listens_is_reported_by_kind_and_a_silent_one_as_none() { let dir = root.abs_path().join("pw-1/node/bin"); let mut c = detached(&sleeper_at(&dir, "node")); let real = crate::procnet::Proc; - let x = gather(&t.base(), false, None, &real, ¬hing); + // `spawn` returns right after `exec`, while the dynamic loader may still open and close + // library files. An fd that vanishes during the scan is `unknown` by design (see + // `socket_inodes_in`), so wait for the stub to settle instead of racing its startup. + // A tree that stays `unknown` still fails the assertion below. + let end = std::time::Instant::now() + std::time::Duration::from_secs(5); + let x = loop { + let x = gather(&t.base(), false, None, &real, ¬hing); + if x.listeners != "unknown" || std::time::Instant::now() >= end { + break x; + } + std::thread::sleep(std::time::Duration::from_millis(20)); + }; assert_eq!(x.listeners, "none", "{x:?}"); c.kill().unwrap(); c.wait().unwrap();