Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/InstanceMCPCore/Browser/BrowserWizardCopy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
14 changes: 11 additions & 3 deletions Sources/InstanceMCPCore/Browser/InstalledChrome.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<InstalledChrome, InstalledChromeProblem>
/// A recorded path, fully checked. A path that is not one of the allowed locations is refused.
func check(appPath: String) -> Result<InstalledChrome, InstalledChromeProblem>
Expand Down Expand Up @@ -121,8 +122,15 @@ public final class InstalledChromeChecker: InstalledChromeChecking, @unchecked S
}

public func check() -> Result<InstalledChrome, InstalledChromeProblem> {
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]? {
Expand Down
1 change: 1 addition & 0 deletions Sources/InstanceMCPCore/Browser/PlaywrightBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions Tests/InstanceMCPCoreTests/BrowserWizardWiringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Tests/InstanceMCPCoreTests/FakeInstallFixture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
32 changes: 32 additions & 0 deletions Tests/InstanceMCPCoreTests/InstalledChromeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
7 changes: 7 additions & 0 deletions Tests/InstanceMCPCoreTests/PlaywrightInstallerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]>([])
Expand Down
13 changes: 12 additions & 1 deletion poc/reverse-attach-linux/src/probe_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &nothing);
// `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, &nothing);
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();
Expand Down
Loading