Skip to content

Commit 233611e

Browse files
committed
Merge branch 'feat/durable-auth-profiles-155' into feat/credential-vault-156
2 parents 485faec + 3b84baa commit 233611e

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎apps/headless/Host/AgentBridge.swift‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@ struct ScreenshotArtifactData {
1313

1414
extension BrowserWindowController {
1515
func agentVisit(_ url: URL, timeout: TimeInterval = 20) throws -> JSONValue {
16-
onMain { self.navigate(to: url) }
17-
Thread.sleep(forTimeInterval: 0.05)
18-
return try agentWait(parameters: ["settled": .bool(true), "timeoutMs": .number(timeout * 1_000)])
16+
let deadline = Date().addingTimeInterval(timeout)
17+
guard onMain({ self.beginAgentNavigation(to: url) }) else {
18+
throw HostError(code: .operationFailed, message: "Browser refused to start navigation")
19+
}
20+
while Date() < deadline {
21+
let status = onMain { self.agentNavigationStatus() }
22+
if status.failed {
23+
throw HostError(code: .operationFailed, message: "Browser navigation failed")
24+
}
25+
if !status.pending {
26+
let remainingMs = max(100, deadline.timeIntervalSinceNow * 1_000)
27+
return try agentWait(parameters: [
28+
"settled": .bool(true), "timeoutMs": .number(remainingMs),
29+
])
30+
}
31+
Thread.sleep(forTimeInterval: 0.05)
32+
}
33+
throw HostError(code: .timedOut, message: "Timed out while waiting for navigation")
1934
}
2035

2136
func agentInspect(parameters: [String: JSONValue]) throws -> JSONValue {

‎apps/headless/Tests/macos-e2e.sh‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,14 @@ if [[ "$(frontmost_pid)" == "$BACKGROUND_PID" ]]; then
419419
fail
420420
fi
421421
"$CLI" stop >/dev/null
422+
for _ in {1..100}; do
423+
! kill -0 "$BACKGROUND_PID" >/dev/null 2>&1 && break
424+
sleep 0.05
425+
done
426+
if kill -0 "$BACKGROUND_PID" >/dev/null 2>&1; then
427+
echo "background override host did not stop" >&2
428+
fail
429+
fi
422430

423431
STEP="durable-authentication-profile"
424432
"$CLI" start --background | grep -q '"ready":true'

‎apps/headless/main.swift‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ final class BrowserWindowController: NSWindowController, NSWindowDelegate,
242242
private var lastProgress: CGFloat = 0
243243
private var onStartPage = false
244244
private var pendingRestoredStartupURL: URL?
245+
private var pendingAgentNavigation: WKNavigation?
246+
private var pendingAgentNavigationFailed = false
245247
private var agentControlEnabled = false
246248
var onClose: (() -> Void)?
247249

@@ -478,6 +480,18 @@ final class BrowserWindowController: NSWindowController, NSWindowDelegate,
478480
load(url)
479481
}
480482

483+
func beginAgentNavigation(to url: URL) -> Bool {
484+
pendingRestoredStartupURL = nil
485+
onStartPage = false
486+
pendingAgentNavigationFailed = false
487+
pendingAgentNavigation = webView.load(URLRequest(url: url))
488+
return pendingAgentNavigation != nil
489+
}
490+
491+
func agentNavigationStatus() -> (pending: Bool, failed: Bool) {
492+
(pendingAgentNavigation != nil, pendingAgentNavigationFailed)
493+
}
494+
481495
private func load(_ url: URL) {
482496
onStartPage = false
483497
if url.isFileURL {
@@ -694,6 +708,7 @@ final class BrowserWindowController: NSWindowController, NSWindowDelegate,
694708
}
695709

696710
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
711+
completeAgentNavigation(navigation, failed: false)
697712
if let job = snapJob {
698713
snapJob = nil
699714
runSnapJob(job)
@@ -706,13 +721,21 @@ final class BrowserWindowController: NSWindowController, NSWindowDelegate,
706721
}
707722

708723
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
724+
completeAgentNavigation(navigation, failed: true)
709725
handleLoadError(error)
710726
}
711727

712728
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
729+
completeAgentNavigation(navigation, failed: true)
713730
handleLoadError(error)
714731
}
715732

733+
private func completeAgentNavigation(_ navigation: WKNavigation?, failed: Bool) {
734+
guard let navigation, pendingAgentNavigation === navigation else { return }
735+
pendingAgentNavigation = nil
736+
pendingAgentNavigationFailed = failed
737+
}
738+
716739
private func handleLoadError(_ error: Error) {
717740
let e = error as NSError
718741
// Ignore cancelled loads and "frame load interrupted" (downloads, redirects).

0 commit comments

Comments
 (0)