Skip to content

Commit d8f6fa8

Browse files
A.R.claude
andcommitted
fix(review): suppress misleading 'key absent' log on unseal failure + optional-chain ctx.browser()
getSavedCookies was logging both the real unseal error AND a spurious "'cookies' key is absent" message because the existsSync branch fired regardless of whether the null came from a vault error or a missing key. Added an unsealFailed flag to skip the second diagnostic when the catch path already explained the failure. login-runner.js / manual-login-runner.js: ctx.browser().close() → ctx.browser()?.close() so a null browser() return degrades gracefully instead of throwing a TypeError swallowed by .catch(). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f51e7cb commit d8f6fa8

3 files changed

Lines changed: 21 additions & 17 deletions

File tree

packages/mcp-server/src/config.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,22 @@ export async function getSavedCookies(): Promise<PlaywrightCookie[]> {
406406
// so the extension output channel shows why — otherwise the user sees
407407
// "run Login first" for a profile they already logged into.
408408
const profile = activeName();
409+
let unsealFailed = false;
409410
const raw = await _vault.get(profile, "cookies").catch((err: unknown) => {
411+
unsealFailed = true;
410412
const msg = err instanceof Error ? err.message : String(err);
411413
console.error(`[vault] getSavedCookies failed for profile '${profile}': ${msg}`);
412414
return null;
413415
});
414416
if (!raw) {
415-
// Distinguish "no vault.enc" from "vault exists but has no cookies key"
416-
const paths = getProfilePaths(profile);
417-
if (!existsSync(paths.vault)) {
418-
console.error(`[vault] getSavedCookies: no vault.enc for profile '${profile}' — run login first`);
419-
} else {
420-
console.error(`[vault] getSavedCookies: vault.enc exists for profile '${profile}' but 'cookies' key is absent`);
417+
if (!unsealFailed) {
418+
// Distinguish "no vault.enc" from "vault exists but has no cookies key"
419+
const paths = getProfilePaths(profile);
420+
if (!existsSync(paths.vault)) {
421+
console.error(`[vault] getSavedCookies: no vault.enc for profile '${profile}' — run login first`);
422+
} else {
423+
console.error(`[vault] getSavedCookies: vault.enc exists for profile '${profile}' but 'cookies' key is absent`);
424+
}
421425
}
422426
return [];
423427
}

packages/mcp-server/src/login-runner.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async function main() {
8989
const ready = await waitForLoginReady(page);
9090
if (!ready) {
9191
const title = await page.title().catch(() => "");
92-
await ctx.browser().close().catch(() => {});
92+
await ctx.browser()?.close().catch(() => {});
9393
emit({ ok: false, reason: /just a moment/i.test(title) ? "cf_blocked" : "auto_unsupported" });
9494
process.exit(/just a moment/i.test(title) ? 3 : 2);
9595
}
@@ -102,19 +102,19 @@ async function main() {
102102
if (!localOrigin) await minimizePageWindow(page);
103103

104104
if (authFlow.kind === "sso_required") {
105-
await ctx.browser().close().catch(() => {});
105+
await ctx.browser()?.close().catch(() => {});
106106
emit({ ok: false, reason: "sso_required" });
107107
process.exit(2);
108108
}
109109

110110
if (authFlow.kind === "unsupported") {
111-
await ctx.browser().close().catch(() => {});
111+
await ctx.browser()?.close().catch(() => {});
112112
emit({ ok: false, reason: "auto_unsupported", detail: authFlow.detail });
113113
process.exit(2);
114114
}
115115

116116
if (authFlow.kind === "email_rejected") {
117-
await ctx.browser().close().catch(() => {});
117+
await ctx.browser()?.close().catch(() => {});
118118
emit({ ok: false, reason: "email_rejected", detail: authFlow.detail });
119119
process.exit(2);
120120
}
@@ -125,7 +125,7 @@ async function main() {
125125
try {
126126
otp = await awaitOtp();
127127
} catch {
128-
await ctx.browser().close().catch(() => {});
128+
await ctx.browser()?.close().catch(() => {});
129129
emit({ ok: false, reason: "otp_timeout" });
130130
process.exit(2);
131131
}
@@ -148,7 +148,7 @@ async function main() {
148148
recordLoginSuccess(PROFILE, { tier: metadata.tier, loginMode: "auto", lastLogin: new Date().toISOString() });
149149
writeFileSync(paths.reinit, String(Date.now()));
150150

151-
await ctx.browser().close().catch(() => {});
151+
await ctx.browser()?.close().catch(() => {});
152152
emit({ ok: true, tier: metadata.tier, modelCount: Object.keys(metadata.models?.models ?? {}).length });
153153
process.exit(0);
154154
}
@@ -159,7 +159,7 @@ async function main() {
159159
}
160160

161161
if (attempt === MAX_RETRIES) {
162-
await ctx.browser().close().catch(() => {});
162+
await ctx.browser()?.close().catch(() => {});
163163
emit({ ok: false, reason: "otp_rejected" });
164164
process.exit(2);
165165
}

packages/mcp-server/src/manual-login-runner.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async function main() {
7474
if (Date.now() - cfStart >= CF_TIMEOUT_MS) {
7575
const title = await page.title().catch(() => "");
7676
if (/just a moment/i.test(title)) {
77-
await ctx.browser().close().catch(() => {});
77+
await ctx.browser()?.close().catch(() => {});
7878
emit({ ok: false, reason: "cf_blocked" });
7979
process.exit(3);
8080
}
@@ -94,7 +94,7 @@ async function main() {
9494

9595
// Test hook: force a browser close to exercise the cancelled path.
9696
if (process.env.PERPLEXITY_TEST_BROWSER_CLOSE_AFTER_MS) {
97-
setTimeout(() => ctx.browser().close().catch(() => {}), Number(process.env.PERPLEXITY_TEST_BROWSER_CLOSE_AFTER_MS));
97+
setTimeout(() => ctx.browser()?.close().catch(() => {}), Number(process.env.PERPLEXITY_TEST_BROWSER_CLOSE_AFTER_MS));
9898
}
9999

100100
const started = Date.now();
@@ -110,7 +110,7 @@ async function main() {
110110
if (sessionCookie) break;
111111
}
112112
if (!sessionCookie) {
113-
await ctx.browser().close().catch(() => {});
113+
await ctx.browser()?.close().catch(() => {});
114114
emit({ ok: false, reason: "timeout" });
115115
process.exit(2);
116116
}
@@ -130,7 +130,7 @@ async function main() {
130130

131131
writeFileSync(paths.reinit, String(Date.now()));
132132

133-
await ctx.browser().close().catch(() => {});
133+
await ctx.browser()?.close().catch(() => {});
134134
emit({ ok: true, tier: metadata.tier, modelCount: Object.keys(metadata.models?.models ?? {}).length });
135135
process.exit(0);
136136
}

0 commit comments

Comments
 (0)