@53able/pi-agent-browser 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.ja-JP.md CHANGED
@@ -164,12 +164,12 @@ https://example.com をブラウザ経由で読み、outputs/browser/example.md
164
164
  すべての操作を無人で任せるべきではありません。適切なタイミングで人間に操作を戻すためのツールが3つあります。
165
165
 
166
166
  - **安全な境界線** — `agent_browser_handoff` は、AI自身では対応できない・すべきでない操作(2FA/OTPの入力、CAPTCHA、本人確認)の直前でエージェントを一時停止させます。スクリーンショットで状況を記録したあと、実際のUIダイアログでブロックし、人間が「対応完了」または「中止」を選ぶまで待ちます。`agent_browser_read` / `agent_browser_snapshot` も簡易なキーワード検知を行い、こうしたチェックポイントらしき内容を出力に警告として付加します(この警告自体は停止ではなく、`agent_browser_handoff` を呼ぶきっかけとして扱ってください)。
167
- - **インタラクティブなログイン** — `agent_browser_login_handoff` は、Google や OAuth など多くのプロバイダで sign-in がブロックされる問題を解決します。これは、agent-browser が使われた時点でオートメーション検出が起きるため、ログイン中に拒否されるというもの。このツールは CDP が接続されていない無人 Chrome プロセスを起動して人間が正常にログインできるようにし、ログイン成功後にその認証済みCookie/storageを取り出して、新しい通常のagent-browserセッションに読み込ませます。Google や OAuth の sign-in フローが期待されるタスクでは、`agent_browser_open` ではなく必ずこちらを使ってください。
167
+ - **インタラクティブなログイン** — `agent_browser_login_handoff` は、Google や OAuth など多くのプロバイダで sign-in がブロックされる問題を解決します。これは、agent-browser が使われた時点でオートメーション検出が起きるため、ログイン中に拒否されるというもの。このツールは CDP が接続されていない無人 Chrome プロセスを起動して人間が正常にログインできるようにし、ログイン成功後に、**そのブラウザはセッションに結合され、閉じられるまで生存し続けます**—リモートデバッグポートが開いたまま表示されます。Google や OAuth の sign-in フローが期待されるタスクでは、`agent_browser_open` ではなく必ずこちらを使ってください。
168
168
  - **Commit前の停止** — `agent_browser_commit` は、決済・削除・投稿・送信といった「やり直しのきかない操作」専用の確認ゲートです。証跡を保存し、クリックの前に必ず人間の明示的な確認を求め、承認されない限りクリックは実行されません。対話的なUIが利用できない場合は、クリックせずにそのまま拒否します。こうした操作の最終確認クリックには `agent_browser_click` ではなく必ずこちらを使ってください。
169
169
 
170
170
  3つのツールすべてが対話的なセッション(TUI、またはUI対応のRPC)を必要とします。無人・headless実行では、黙って進めるのではなく拒否します。
171
171
 
172
- `agent_browser_open`は、各セッションが`headed: true`で起動されたかどうかを記録します。`agent_browser_handoff`が、headedで開かれていないセッションに対して呼ばれた場合、人間への警告に加えて、可視ウィンドウを開き直すための`agent_browser_open`の引数(セッション名、`headed: true`、`restore: true`、最後に開いていたURL)をそのまま提示します — どのコマンドを打ち直せばよいか推測する必要はありません。ログインや2FA、CAPTCHA、本人確認に差し掛かる可能性があるタスクでは、最初から`headed: true`で開くことを推奨します。一部の確認チャレンジは使い切り・期限付きのため、途中で開き直すとログインからやり直しになることがあります。`agent_browser_login_handoff`が作るセッションは常にheadlessです(使い捨てのログイン用ブラウザではなく、新規セッションのため)。見たい場合は`headed: true`で開き直してください。
172
+ `agent_browser_open`は、各セッションが`headed: true`で起動されたかどうかを記録します。`agent_browser_handoff`が、headedで開かれていないセッションに対して呼ばれた場合、人間への警告に加えて、可視ウィンドウを開き直すための`agent_browser_open`の引数(セッション名、`headed: true`、`restore: true`、最後に開いていたURL)をそのまま提示します — どのコマンドを打ち直せばよいか推測する必要はありません。ログインや2FA、CAPTCHA、本人確認に差し掛かる可能性があるタスクでは、最初から`headed: true`で開くことを推奨します。一部の確認チャレンジは使い切り・期限付きのため、途中で開き直すとログインからやり直しになることがあります。`agent_browser_login_handoff`が作るセッションは`headed: true`として記録されます(ログインブラウザが表示・生存し続けるため、開き直す必要はありません)。
173
173
 
174
174
  ## Troubleshooting
175
175
 
package/README.md CHANGED
@@ -164,12 +164,12 @@ Clear all saved states only when intentionally resetting everything:
164
164
  Not every step should run unattended. Three tools hand control back to a human at the right moments:
165
165
 
166
166
  - **Safe boundaries** — `agent_browser_handoff` pauses the agent for steps it cannot or should not perform itself: 2FA/OTP entry, CAPTCHA, identity verification. It captures a screenshot for context, then blocks on a real confirmation dialog until a human resumes or aborts. `agent_browser_read` and `agent_browser_snapshot` also run a lightweight keyword scan and flag likely checkpoints in their output as a nudge to call this tool.
167
- - **Interactive login flows** — `agent_browser_login_handoff` solves the problem that Google, OAuth, and similar providers block sign-in when agent-browser is used, because automation detection happens during the login step itself. This tool spawns an unattended Chrome process (with no CDP attached) so the human can log in normally, then captures the authenticated cookies/storage and loads them into a fresh, ordinary agent-browser session after login succeeds. Use this instead of `agent_browser_open` whenever a task is expected to hit an interactive Google or OAuth sign-in flow.
167
+ - **Interactive login flows** — `agent_browser_login_handoff` solves the problem that Google, OAuth, and similar providers block sign-in when agent-browser is used, because automation detection happens during the login step itself. This tool spawns an unattended Chrome process (with no CDP attached) so the human can log in normally. After login succeeds, the still-running browser is live-attached to the named session and **kept open as the permanent backing browser**—it remains visible on screen with an open remote-debugging port until closed with `agent_browser_close`. Use this instead of `agent_browser_open` whenever a task is expected to hit an interactive Google or OAuth sign-in flow.
168
168
  - **Stop before commit** — `agent_browser_commit` is a dedicated gate for irreversible actions: payment, deletion, publishing, sending. It captures evidence, requires explicit human confirmation before clicking, and refuses outright (without clicking) if no interactive UI is available to ask. Use it instead of `agent_browser_click` for the final confirming step of any such action.
169
169
 
170
170
  All three tools require an interactive session (TUI or RPC with UI support); in headless/unattended runs they refuse rather than proceed silently.
171
171
 
172
- `agent_browser_open` remembers whether each named session was launched with `headed: true`. If `agent_browser_handoff` is called for a session that was not opened headed, it automatically warns the human and includes the exact `agent_browser_open` arguments (session name, `headed: true`, `restore: true`, and the last known URL) needed to reopen a visible window — no more guessing which command to rerun. Prefer `headed: true` up front for any task that might hit a login, 2FA, CAPTCHA, or identity-verification step, since some challenges are single-use and reopening mid-flow may require restarting the login. `agent_browser_login_handoff` always leaves the resulting session headless (it's a fresh session, not the disposable login browser), so reopen with `headed: true` if you need to see it.
172
+ `agent_browser_open` remembers whether each named session was launched with `headed: true`. If `agent_browser_handoff` is called for a session that was not opened headed, it automatically warns the human and includes the exact `agent_browser_open` arguments (session name, `headed: true`, `restore: true`, and the last known URL) needed to reopen a visible window — no more guessing which command to rerun. Prefer `headed: true` up front for any task that might hit a login, 2FA, CAPTCHA, or identity-verification step, since some challenges are single-use and reopening mid-flow may require restarting the login. `agent_browser_login_handoff` records the resulting session as `headed: true` (since the login browser is visible and running); you don't need to reopen it to see it.
173
173
 
174
174
  ## Troubleshooting
175
175
 
package/index.ts CHANGED
@@ -43,7 +43,9 @@ const SESSION_STATE_DIR = ".pi-agent-browser";
43
43
  const SESSION_STATE_FILE = "sessions.json";
44
44
  const DEFAULT_SESSION_KEY = "__default__";
45
45
 
46
- type SessionState = { headed: boolean; lastUrl?: string; updatedAt: string };
46
+ type SessionState = { headed: boolean; lastUrl?: string; loginBrowserPid?: number; loginDebugPort?: number; updatedAt: string };
47
+
48
+ type SessionStatePatch = Partial<Omit<SessionState, "updatedAt">> & { clearLoginBrowserPid?: boolean };
47
49
 
48
50
  function sessionKey(params: JsonObject): string {
49
51
  return typeof params.session === "string" && params.session.trim() ? params.session.trim() : DEFAULT_SESSION_KEY;
@@ -58,7 +60,7 @@ async function readSessionStates(cwd: string): Promise<Record<string, SessionSta
58
60
  }
59
61
  }
60
62
 
61
- async function writeSessionState(cwd: string, session: string, patch: Partial<Omit<SessionState, "updatedAt">>): Promise<void> {
63
+ async function writeSessionState(cwd: string, session: string, patch: SessionStatePatch): Promise<void> {
62
64
  const dir = join(cwd, SESSION_STATE_DIR);
63
65
  await mkdir(dir, { recursive: true });
64
66
  const states = await readSessionStates(cwd);
@@ -66,6 +68,8 @@ async function writeSessionState(cwd: string, session: string, patch: Partial<Om
66
68
  states[session] = {
67
69
  headed: patch.headed ?? previous?.headed ?? false,
68
70
  lastUrl: patch.lastUrl ?? previous?.lastUrl,
71
+ loginBrowserPid: patch.clearLoginBrowserPid ? undefined : (patch.loginBrowserPid ?? previous?.loginBrowserPid),
72
+ loginDebugPort: patch.clearLoginBrowserPid ? undefined : (patch.loginDebugPort ?? previous?.loginDebugPort),
69
73
  updatedAt: new Date().toISOString(),
70
74
  };
71
75
  await writeFile(join(dir, SESSION_STATE_FILE), JSON.stringify(states, null, 2), "utf8");
@@ -266,6 +270,11 @@ async function findFreePort(): Promise<number> {
266
270
  });
267
271
  }
268
272
 
273
+ function killPidBestEffort(pid?: number): void {
274
+ if (!pid) return;
275
+ try { process.kill(pid); } catch { /* already gone / not ours */ }
276
+ }
277
+
269
278
  function abortableDelay(ms: number, signal?: AbortSignal): Promise<void> {
270
279
  return new Promise((resolveDelay) => {
271
280
  if (signal?.aborted) {
@@ -673,12 +682,28 @@ export default function agentBrowserExtension(pi: ExtensionAPI) {
673
682
  parameters: CloseSchema,
674
683
  async execute(_id, rawParams, signal, onUpdate, ctx) {
675
684
  const params = rawParams as JsonObject;
685
+ const states = await readSessionStates(ctx.cwd);
676
686
  const args: string[] = [];
677
687
  pushCommonArgs(args, params);
678
688
  args.push("close");
679
689
  if (params.all === true) args.push("--all");
680
690
  pushExtraArgs(args, params);
681
691
  const result = await runAgentBrowser(args, { signal, timeoutMs: params.timeoutMs as number | undefined, cwd: ctx.cwd, onUpdate });
692
+
693
+ // agent-browser close only detaches an externally-spawned login Chrome; kill the tracked pid(s) ourselves.
694
+ if (params.all === true) {
695
+ for (const [name, st] of Object.entries(states)) {
696
+ if (!st.loginBrowserPid) continue;
697
+ killPidBestEffort(st.loginBrowserPid);
698
+ await writeSessionState(ctx.cwd, name, { clearLoginBrowserPid: true });
699
+ }
700
+ } else {
701
+ const session = sessionKey(params);
702
+ if (states[session]?.loginBrowserPid) {
703
+ killPidBestEffort(states[session]?.loginBrowserPid);
704
+ await writeSessionState(ctx.cwd, session, { clearLoginBrowserPid: true });
705
+ }
706
+ }
682
707
  return buildTextResult(result);
683
708
  },
684
709
  });
@@ -764,12 +789,13 @@ export default function agentBrowserExtension(pi: ExtensionAPI) {
764
789
  pi.registerTool({
765
790
  name: "agent_browser_login_handoff",
766
791
  label: "Agent Browser Login Handoff",
767
- description: "Launch a real, non-CDP-attached Chrome for an interactive login (e.g. Google sign-in) that automation frameworks get blocked from. Once the human confirms login is complete, captures the authenticated cookies/storage and loads them into a fresh, ordinary agent-browser session. Blocks until the human confirms or aborts.",
792
+ description: "Launch a real, non-CDP-attached Chrome for an interactive login (e.g. Google sign-in) that automation frameworks get blocked from. Once the human confirms login is complete, live-attaches that still-running browser to the named agent-browser session and keeps it open as the session's backing browser (no cookie/state export). Blocks until the human confirms or aborts.",
768
793
  promptSnippet: "Use agent_browser_login_handoff (not agent_browser_open) whenever a task requires an interactive Google/OAuth-style login.",
769
794
  promptGuidelines: [
770
795
  "Never attempt an interactive Google/OAuth-style login via agent_browser_open, agent_browser_click, or agent_browser_fill — those attach CDP immediately, and Google blocks sign-in for CDP-attached sessions regardless of profile or cookies.",
771
796
  "Call this tool instead whenever a task is expected to require signing into a Google account or a similarly automation-hostile login flow.",
772
797
  "After this tool reports success, take a fresh agent_browser_snapshot before continuing — do not assume page state.",
798
+ "On success the login browser stays open and IS the session's live backing browser; drive it with the normal agent_browser_* tools by session name, and call agent_browser_close when done to shut it down (it holds an open remote-debugging port until then).",
773
799
  "If this tool reports no interactive UI, a decline, or a failure, stop the task and report back; do not retry via agent_browser_open.",
774
800
  ],
775
801
  parameters: LoginHandoffSchema,
@@ -805,6 +831,10 @@ export default function agentBrowserExtension(pi: ExtensionAPI) {
805
831
 
806
832
  const port = await findFreePort();
807
833
 
834
+ // Supersede an older live login browser for this session so re-running doesn't leak it.
835
+ const prior = (await readSessionStates(ctx.cwd))[session];
836
+ killPidBestEffort(prior?.loginBrowserPid);
837
+
808
838
  let spawnError: Error | undefined;
809
839
  const child = spawn(executablePath, [
810
840
  `--remote-debugging-port=${port}`,
@@ -889,42 +919,27 @@ export default function agentBrowserExtension(pi: ExtensionAPI) {
889
919
  };
890
920
  }
891
921
 
892
- // Do NOT keep driving the live --cdp connection as the ongoing session: `agent-browser
893
- // --cdp <port> ... open` (no url) opens a brand-new blank page/context in the connected
894
- // browser rather than reusing the tab the human actually authenticated in, leaving the
895
- // "attached" session on about:blank with no auth. Instead follow agent-browser's own
896
- // documented pattern for this exact scenario (see its authentication.md "Import Auth
897
- // from Your Browser" / "Two-Factor Authentication" sections): extract the authenticated
898
- // cookies/storage from the login browser via `state save`, close the disposable login
899
- // browser, then load that state into a normal, freshly-launched session via `--state`.
900
- const authStatePath = join(profileDir, "auth-state.json");
901
- const saveArgs = ["--cdp", String(port), "state", "save", authStatePath];
902
- const saveResult = await runAgentBrowser(saveArgs, { signal, timeoutMs: params.timeoutMs as number | undefined, cwd: ctx.cwd, onUpdate });
903
- try { child.kill(); } catch { /* best-effort; the login browser is disposable once we've extracted its auth state */ }
904
-
905
- if (!saveResult.ok) {
906
- return {
907
- content: [{
908
- type: "text",
909
- text: [
910
- "Login browser was confirmed by the human, but extracting its authenticated state via `state save` failed. Do not assume the session is usable.",
911
- `Command: ${saveResult.command} ${saveResult.args.map((a) => JSON.stringify(a)).join(" ")}`,
912
- ].join("\n"),
913
- }],
914
- details: { ...saveResult, artifactPaths: [] },
915
- terminate: true,
916
- };
917
- }
922
+ // Human confirmed login. Bind the STILL-RUNNING login Chrome into the named session
923
+ // via CDP, WITHOUT navigating (snapshot binds + verifies in one shot; open would
924
+ // re-navigate away from the authenticated landing page). Keep the browser ALIVE —
925
+ // it is now the backing browser this session drives from here on.
926
+
927
+ // Defensive: surface multi-tab situations (OAuth popups) without failing.
928
+ const tabArgs = ["--cdp", String(port), "tab", "list"];
929
+ const tabResult = await runAgentBrowser(tabArgs, { signal, timeoutMs: params.timeoutMs as number | undefined, cwd: ctx.cwd, onUpdate });
918
930
 
919
- const loadArgs = ["--session", session, "--restore", "--state", authStatePath, "open", url];
920
- const result = await runAgentBrowser(loadArgs, { signal, timeoutMs: params.timeoutMs as number | undefined, cwd: ctx.cwd, onUpdate });
931
+ const bindArgs = ["--session", session, "--cdp", String(port), "snapshot"];
932
+ const result = await runAgentBrowser(bindArgs, { signal, timeoutMs: params.timeoutMs as number | undefined, cwd: ctx.cwd, onUpdate });
921
933
 
922
934
  if (!result.ok) {
935
+ // Bind failed: the login browser is still alive but unusable by the session.
936
+ // Best-effort kill so we don't leak a debug-port Chrome, and report.
937
+ killPidBestEffort(child.pid);
923
938
  return {
924
939
  content: [{
925
940
  type: "text",
926
941
  text: [
927
- "Login state was captured from the human's browser, but loading it into a fresh agent-browser session failed. Do not assume the session is usable.",
942
+ "Human confirmed login, but attaching the live browser to the agent-browser session failed. Do not assume the session is usable.",
928
943
  `Command: ${result.command} ${result.args.map((a) => JSON.stringify(a)).join(" ")}`,
929
944
  ].join("\n"),
930
945
  }],
@@ -933,16 +948,25 @@ export default function agentBrowserExtension(pi: ExtensionAPI) {
933
948
  };
934
949
  }
935
950
 
936
- await writeSessionState(ctx.cwd, session, { headed: false, lastUrl: url });
951
+ // Persist backing-browser identity + the fact that a VISIBLE window exists (headed:true).
952
+ await writeSessionState(ctx.cwd, session, {
953
+ headed: true,
954
+ lastUrl: url,
955
+ loginBrowserPid: child.pid,
956
+ loginDebugPort: port,
957
+ });
937
958
 
959
+ const multiTab = /\bt2\b/.test(tabResult.stdout) || (tabResult.stdout.match(/\[t\d+\]/g)?.length ?? 0) > 1;
938
960
  return {
939
961
  content: [{
940
962
  type: "text",
941
963
  text: [
942
- "Login handoff complete: the human's authenticated cookies/storage were captured and loaded into a fresh agent-browser session.",
964
+ "Login handoff complete: the browser you signed in with is now live-attached to the agent-browser session and stays open.",
943
965
  `Session: ${session === DEFAULT_SESSION_KEY ? "(default)" : session}`,
944
- "Take a fresh agent_browser_snapshot before continuing.",
945
- ].join("\n"),
966
+ multiTab ? `Note: multiple tabs are open — verify the active tab before acting:\n${tabResult.stdout.trim()}` : undefined,
967
+ "The session drives this real browser window directly. Take a fresh agent_browser_snapshot before continuing.",
968
+ "This login browser has an open remote-debugging port and will stay open until agent_browser_close is called for this session (or the window is closed manually).",
969
+ ].filter(Boolean).join("\n"),
946
970
  }],
947
971
  details: { ...result, artifactPaths: [] },
948
972
  terminate: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@53able/pi-agent-browser",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Typed Pi tools for browser automation, rendered-page context extraction, screenshots, and saved session state via agent-browser.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -44,7 +44,7 @@ Two tools exist so the agent stops and hands control back to a human instead of
44
44
 
45
45
  `agent_browser_login_handoff` solves a specific problem: Google and similar OAuth providers block sign-in when `agent-browser open` is used, because agent-browser attaches a remote debugging protocol (CDP) session the instant Chrome launches, and Google detects this automation marker and rejects the login attempt regardless of profile, cookies, or executable.
46
46
 
47
- This tool spawns a plain, un-instrumented Chrome process (not through `agent-browser open`) with a remote debugging port and a dedicated persistent profile, so no CDP client is attached while the human logs in. Chrome behaves like an ordinary browser to Google during sign-in. After the human confirms login is complete, the tool captures the authenticated cookies and storage from that browser (`agent-browser state save`), closes the disposable login browser, and loads that captured state into a fresh, ordinary agent-browser session (`agent-browser state load`) for all subsequent automated snapshot, read, and click operations. (An earlier version tried to keep driving the login browser live over CDP; that opened a disconnected blank tab instead of reusing the authenticated one, so state capture-and-load is used instead.)
47
+ This tool spawns a plain, un-instrumented Chrome process (not through `agent-browser open`) with a remote debugging port and a dedicated persistent profile, so no CDP client is attached while the human logs in. Chrome behaves like an ordinary browser to Google during sign-in. After the human confirms login is complete, the still-running login browser is **live-attached to the named agent-browser session** (via `agent-browser --session <name> --cdp <port> snapshot`, not `open`, to avoid navigating away from the authenticated page) and **kept open as the permanent backing browser for that session**. All subsequent automated snapshot, read, click, and other operations continue to work against it via `--session <name>` alone, unchanged.
48
48
 
49
49
  **Important:** this is an architecture fix, not detection evasion. The tool does not hide webdriver flags or automation fingerprints — it simply avoids having a CDP session attached during the interactive login moment itself.
50
50
 
@@ -60,7 +60,11 @@ Example call:
60
60
  }
61
61
  ```
62
62
 
63
- After the human confirms login is complete, subsequent `agent_browser_open`, `agent_browser_snapshot`, `agent_browser_click`, and other calls with the same `session` name will reuse the authenticated browser state and operate normally. This new session runs headless by default (it is a fresh agent-browser session, not the disposable login browser, which is closed once its auth state is captured); reopen it with `headed: true` if you need to see it.
63
+ After the human confirms login is complete, subsequent `agent_browser_open`, `agent_browser_snapshot`, `agent_browser_click`, and other calls with the same `session` name will reuse the authenticated browser and operate normally. **The login browser window remains visible and open indefinitely** — this is a real, user-visible Chrome window with an open remote-debugging port on `127.0.0.1` that any local process can access. The session is recorded as `headed: true` (since the authenticated browser is still running).
64
+
65
+ **Security note:** The login browser's `--remote-debugging-port` remains open on localhost for as long as the session is alive. This gives any local process full browser control — the same caveat agent-browser's own docs give about `--remote-debugging-port`. Call `agent_browser_close` when done with a session that went through login handoff, rather than leaving it open indefinitely. `agent_browser_close` will terminate the tracked login Chrome process and clean up its debugging port.
66
+
67
+ Re-running `agent_browser_login_handoff` for the same session name will terminate any previous live login browser for that session before spawning a new one.
64
68
 
65
69
  ## Common examples
66
70