@basein/runner 0.1.0

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.
Files changed (100) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +276 -0
  3. package/dist/auth/client.d.ts +85 -0
  4. package/dist/auth/client.js +284 -0
  5. package/dist/bin/bir-hooks.d.ts +48 -0
  6. package/dist/bin/bir-hooks.js +201 -0
  7. package/dist/bin/bir-proxy.d.ts +45 -0
  8. package/dist/bin/bir-proxy.js +207 -0
  9. package/dist/bin/bir-scenario.d.ts +24 -0
  10. package/dist/bin/bir-scenario.js +177 -0
  11. package/dist/bin/bir.d.ts +21 -0
  12. package/dist/bin/bir.js +876 -0
  13. package/dist/config/adapters/claude-code.d.ts +76 -0
  14. package/dist/config/adapters/claude-code.js +181 -0
  15. package/dist/config/adapters/generic.d.ts +17 -0
  16. package/dist/config/adapters/generic.js +36 -0
  17. package/dist/config/generate.d.ts +127 -0
  18. package/dist/config/generate.js +114 -0
  19. package/dist/config/resolve.d.ts +68 -0
  20. package/dist/config/resolve.js +132 -0
  21. package/dist/control/client.d.ts +56 -0
  22. package/dist/control/client.js +86 -0
  23. package/dist/control/correlation.d.ts +86 -0
  24. package/dist/control/correlation.js +0 -0
  25. package/dist/control/discovery.d.ts +50 -0
  26. package/dist/control/discovery.js +123 -0
  27. package/dist/control/ordering.d.ts +38 -0
  28. package/dist/control/ordering.js +44 -0
  29. package/dist/control/paths.d.ts +32 -0
  30. package/dist/control/paths.js +56 -0
  31. package/dist/control/server.d.ts +272 -0
  32. package/dist/control/server.js +1131 -0
  33. package/dist/control/transcript.d.ts +75 -0
  34. package/dist/control/transcript.js +241 -0
  35. package/dist/index.d.ts +37 -0
  36. package/dist/index.js +32 -0
  37. package/dist/jsonrpc/framing.d.ts +49 -0
  38. package/dist/jsonrpc/framing.js +143 -0
  39. package/dist/jsonrpc/types.d.ts +52 -0
  40. package/dist/jsonrpc/types.js +46 -0
  41. package/dist/proxy/intercept.d.ts +55 -0
  42. package/dist/proxy/intercept.js +147 -0
  43. package/dist/proxy/relay.d.ts +97 -0
  44. package/dist/proxy/relay.js +166 -0
  45. package/dist/proxy/session.d.ts +116 -0
  46. package/dist/proxy/session.js +319 -0
  47. package/dist/record/housekeeping.d.ts +34 -0
  48. package/dist/record/housekeeping.js +39 -0
  49. package/dist/record/queue.d.ts +48 -0
  50. package/dist/record/queue.js +96 -0
  51. package/dist/record/recorder.d.ts +111 -0
  52. package/dist/record/recorder.js +39 -0
  53. package/dist/record/redact.d.ts +37 -0
  54. package/dist/record/redact.js +119 -0
  55. package/dist/record/remote-recorder.d.ts +110 -0
  56. package/dist/record/remote-recorder.js +301 -0
  57. package/dist/record/truncate.d.ts +36 -0
  58. package/dist/record/truncate.js +85 -0
  59. package/dist/replay/bundle.d.ts +36 -0
  60. package/dist/replay/bundle.js +89 -0
  61. package/dist/replay/controller.d.ts +300 -0
  62. package/dist/replay/controller.js +807 -0
  63. package/dist/replay/coverage.d.ts +41 -0
  64. package/dist/replay/coverage.js +56 -0
  65. package/dist/replay/derive.d.ts +58 -0
  66. package/dist/replay/derive.js +166 -0
  67. package/dist/replay/executor.d.ts +78 -0
  68. package/dist/replay/executor.js +233 -0
  69. package/dist/replay/logic.d.ts +31 -0
  70. package/dist/replay/logic.js +50 -0
  71. package/dist/replay/plan.d.ts +181 -0
  72. package/dist/replay/plan.js +397 -0
  73. package/dist/replay/pricing.d.ts +41 -0
  74. package/dist/replay/pricing.js +76 -0
  75. package/dist/replay/source-run.d.ts +50 -0
  76. package/dist/replay/source-run.js +98 -0
  77. package/dist/replay/tool-error.d.ts +22 -0
  78. package/dist/replay/tool-error.js +60 -0
  79. package/dist/replay/types.d.ts +116 -0
  80. package/dist/replay/types.js +35 -0
  81. package/dist/upstream/client.d.ts +78 -0
  82. package/dist/upstream/client.js +114 -0
  83. package/dist/upstream/http-client.d.ts +78 -0
  84. package/dist/upstream/http-client.js +261 -0
  85. package/dist/upstream/lazy-client.d.ts +31 -0
  86. package/dist/upstream/lazy-client.js +53 -0
  87. package/dist/upstream/stdio-client.d.ts +57 -0
  88. package/dist/upstream/stdio-client.js +203 -0
  89. package/dist/util/log.d.ts +27 -0
  90. package/dist/util/log.js +51 -0
  91. package/dist/util/version.d.ts +2 -0
  92. package/dist/util/version.js +40 -0
  93. package/docs/BaseInstRunner.md +621 -0
  94. package/docs/calculatedReplay.md +1185 -0
  95. package/docs/calculatedReplayGuide.md +448 -0
  96. package/docs/installRun.md +413 -0
  97. package/docs/mcpmark.md +752 -0
  98. package/docs/quickstart.md +201 -0
  99. package/docs/t-bench.md +394 -0
  100. package/package.json +56 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,276 @@
1
+ # BaseInstRunnerMCP
2
+
3
+ A recording MCP **proxy**. It sits between any MCP client (Claude Code, Cursor,
4
+ Codex, an Agent SDK session) and every MCP server that client uses, executes each
5
+ call on the client's behalf, and records the run as a reusable scenario in the
6
+ BaseIn service.
7
+
8
+ The design this implements is [docs/BaseInstRunner.md](docs/BaseInstRunner.md).
9
+ This README is the operator's half: how to run it, what it records, and what it
10
+ cannot. For v2 — running a calculated scenario when a prompt means the same thing
11
+ as one you have run before — see [docs/calculatedReplay.md](docs/calculatedReplay.md)
12
+ and its runbook [docs/calculatedReplayGuide.md](docs/calculatedReplayGuide.md).
13
+
14
+ ```
15
+ model ──mcp__chrome-devtools__*──▶ bir-proxy ──stdio/http──▶ chrome-devtools-mcp
16
+ hooks ──built-ins, prompt, stop──▶ bir-hooks (control server) ──▶ BaseIn /recordings/*
17
+ ▲
18
+ bir-proxy ────┘ step reports
19
+ ```
20
+
21
+ ---
22
+
23
+ ## Install
24
+
25
+ On a runner machine, one command does machine, account and project scope:
26
+
27
+ ```powershell
28
+ .\scripts\install-runner.ps1 C:\work\repo -AuthUrl https://your-basein-service # Windows
29
+ ```
30
+
31
+ ```bash
32
+ BIR_AUTH_URL=https://your-basein-service ./scripts/install-runner.sh ~/work/repo # macOS/Linux
33
+ ```
34
+
35
+ Step by step, for one machine: [docs/quickstart.md](docs/quickstart.md).
36
+ For a fleet — packaging, releases, offline installs, updates:
37
+ [docs/installRun.md](docs/installRun.md).
38
+
39
+ By hand, or when developing BaseInstRunner itself:
40
+
41
+ ```bash
42
+ npm i -g @basein/runner # or: npm install && npm run build, in a checkout
43
+ cd the-project-you-record-from
44
+ bir install --global # --local from a checkout; plain `bir install` writes a pinned npx entry
45
+ ```
46
+
47
+ `bir install` rewrites your MCP entries in place, keeping every key (so
48
+ `mcp__<server>__<tool>` names never change), and wires Claude Code's hooks. It
49
+ stashes the original text of every file first — `bir uninstall` puts it back
50
+ byte-for-byte.
51
+
52
+ ```bash
53
+ export BIR_AUTH_URL=https://your-basein-service # required to record anything
54
+ bir login # once
55
+ bir-hooks # leave running; Ctrl-C to stop
56
+ claude # your session, now recorded
57
+ ```
58
+
59
+ Check it:
60
+
61
+ ```bash
62
+ bir status # what is wrapped, and which config scope won
63
+ bir doctor # is it working *right now* — exits non-zero if not
64
+ ```
65
+
66
+ ### Any other MCP client
67
+
68
+ Nothing here knows where Cursor or Codex keep their settings, and it does not
69
+ guess. Two paths:
70
+
71
+ ```bash
72
+ bir install --config /path/to/that/client/mcp.json
73
+ bir wrap --server-name my-server -- node my-server.js # prints an entry to paste
74
+ ```
75
+
76
+ ---
77
+
78
+ ## The two tiers
79
+
80
+ Only Claude Code has hooks, so run ownership is negotiated at proxy startup.
81
+
82
+ | Tier | When | Records |
83
+ |---|---|---|
84
+ | **1 — Bound** | a control server (`bir-hooks`) is discoverable for this cwd | built-ins **and** MCP, one ordered step stream, prompt and final answer |
85
+ | **2 — Standalone** | no control server within 5 s | MCP calls only — no prompt, no final answer, no built-in steps |
86
+
87
+ Tier 2 is not a bug; it is the honest ceiling of what an MCP proxy can observe. A
88
+ run records which tier produced it in `metadata.tier`, so nothing downstream
89
+ mistakes a partial trace for a complete one.
90
+
91
+ Steps taken while the tier is still being decided are **buffered**, not dropped —
92
+ the host may spawn MCP servers before `SessionStart` fires, and the ordering
93
+ between the two is not guaranteed.
94
+
95
+ ---
96
+
97
+ ## What it will not do
98
+
99
+ - **Non-MCP tools without the hook binary.** `Bash`, `Read`, `Edit` and friends
100
+ are not MCP traffic. An MCP server sees only MCP traffic addressed to it. That
101
+ is a structural fact, not a limitation of this implementation.
102
+ - **`claude-in-chrome`.** It is `scope: "dynamic"`, present in no config file, so
103
+ there is no entry to rewrite.
104
+ - **Replay, unless you turn it on.** Recording is the default and the whole of
105
+ what runs out of the box. Calculated replay — running a matched prompt's
106
+ scenario instead of rediscovering it — ships, but needs `bir install --replay`
107
+ *and* `BIR_REPLAY=1`, for the reason in the next section.
108
+ - **Change what the model does.** Tool descriptions are never edited. The
109
+ model-visible changes are schema relaxation, below, and — only with
110
+ `--replay` — one first-party tool.
111
+
112
+ ---
113
+
114
+ ## Calculated replay
115
+
116
+ When the service recognises that a prompt means the same thing as one you have
117
+ run before, it hands back the *calculated scenario* derived from that earlier
118
+ run: an intent, a parameter schema, and one bit of logic per step that recomputes
119
+ that step's arguments. `bir` can run it instead of letting the model rediscover
120
+ the plan token by token.
121
+
122
+ The design is [docs/calculatedReplay.md](docs/calculatedReplay.md); the runbook —
123
+ record, calculate, check, enable, read the audit lines — is
124
+ [docs/calculatedReplayGuide.md](docs/calculatedReplayGuide.md).
125
+
126
+ ```bash
127
+ bir install --replay # adds the `bir` MCP server; raises the prompt-hook timeout
128
+ BIR_REPLAY=1 bir-hooks # and this is the switch
129
+ ```
130
+
131
+ Two switches, not one, because of what replay is: **a scenario is a pre-approved
132
+ list of tool calls with computed arguments.** A steered step is answered
133
+ `permissionDecision: "allow"`, so a pinned `Bash` or `Edit` runs without the
134
+ prompt you would have seen. A *directly executed* step never reaches the
135
+ permission system at all — it is dispatched to a `bir-proxy` and called on a
136
+ connection that is already open. Set `BIR_REPLAY_ALLOW_SERVERS` to the servers
137
+ you are content to have called unattended, and read
138
+ [the guide's §5.1](docs/calculatedReplayGuide.md) before enabling it anywhere
139
+ that matters.
140
+
141
+ What it buys: on a scenario whose every step is a wrapped MCP tool, the whole
142
+ sequence runs through connections the proxies already hold — **zero model tokens**
143
+ — and the model's only job is one tool call that reads the results. Mixed
144
+ scenarios steer the live session instead, pinning each step's arguments. Either
145
+ way the turn reports what it cost, so the savings ledger measures something real.
146
+
147
+ ---
148
+
149
+ ## Two things to know before you turn it on
150
+
151
+ **1. Correlation auto-approves wrapped MCP calls.** Merging the hook's view of a
152
+ call with the proxy's requires carrying an id through the tool's arguments, and
153
+ the mechanism for that is answering `PreToolUse` with
154
+ `permissionDecision: "allow"`. That skips a permission prompt you might otherwise
155
+ have seen for those tools. To keep the prompt:
156
+
157
+ ```bash
158
+ BIR_CORRELATION_DECISION=ask bir-hooks
159
+ ```
160
+
161
+ Hosts that ignore `updatedInput` on a non-allow decision then fall back to
162
+ fingerprint matching, which is lossy under identical concurrent calls.
163
+
164
+ **2. Schema relaxation is visible to the model.** Carrying that id means adding
165
+ `additionalProperties: true` plus one declared key to each wrapped tool's schema,
166
+ on every call — not only recorded ones. It is applied **only** in Tier 1 with
167
+ correlation active, and **only** to wrapped servers. `--no-correlation` turns it
168
+ off entirely and joins on `(server, tool, hash(args))` inside a 30 s window
169
+ instead.
170
+
171
+ ---
172
+
173
+ ## Configuration
174
+
175
+ | Variable | Effect |
176
+ |---|---|
177
+ | `BIR_AUTH_URL` | BaseIn auth-service. **Unset means nothing is recorded** — everything else still works. |
178
+ | `BIR_AUTH_DISABLE=1` | Skip authentication (local dev / CI). |
179
+ | `BIR_CONTROL_PORT` | Preferred control-server port (default 53411). |
180
+ | `BIR_CONTROL_URL` / `BIR_CONTROL_TOKEN` | Point a proxy at a control server directly, bypassing discovery. Injected into generated config; the only reliable channel for an SDK session. |
181
+ | `BIR_CORRELATION_DECISION` | `allow` (default) or `ask` — see above. |
182
+ | `BIR_NO_CORRELATION=1` | Never inject a call id. |
183
+ | `BIR_HOME` | Where state lives (default `~/.baseinstrunner`). |
184
+ | `BIR_CLAUDE_JSON` | Override the `~/.claude.json` location. |
185
+ | `BIR_VERBOSE=1` | Per-message detail lines on stderr. |
186
+
187
+ Calculated replay adds these, all read by `bir-hooks` only — the proxies are told
188
+ what to do when they register, so there is one switch and no second copy of it:
189
+
190
+ | Variable | Effect |
191
+ |---|---|
192
+ | `BIR_REPLAY=1` | Enable replay. Nothing below matters until it is set. |
193
+ | `BIR_REPLAY_ALLOW_SERVERS` | Server keys eligible for **direct** execution. Unset means every wrapped server; setting it is the recommendation. |
194
+ | `BIR_MIN_STEER_SIMILARITY` | Minimum match similarity to replay (default `0.92`, above the service's own `0.9` detection threshold). |
195
+ | `ANTHROPIC_API_KEY` | Enables parameter derivation. Without it, replay uses the scenario's recorded sample values — free, and often still correct. |
196
+ | `BIR_DERIVE_MODEL` | Derivation model (default `claude-haiku-4-5-20251001`). |
197
+ | `BIR_MATCH_BUDGET_MS` | How long the prompt hook waits for a match (default `2500`). |
198
+ | `BIR_DERIVE_BUDGET_MS` | How long the first `PreToolUse` waits for parameters (default `8000`). |
199
+ | `BIR_REPLAY_BUDGET_MS` / `BIR_STEP_TIMEOUT_MS` | Whole-plan and per-step ceilings for direct execution (default `120000` / `60000`). |
200
+
201
+ ### The audit log
202
+
203
+ Every decision is one `key=value` line on **stderr** — never stdout, which is the
204
+ host's JSON-RPC stream. There is no file sink to configure; persistence is the
205
+ shell's job:
206
+
207
+ ```bash
208
+ bir-hooks 2>&1 | tee -a ~/.baseinstrunner/audit.log
209
+ grep run.lossy ~/.baseinstrunner/audit.log
210
+ ```
211
+
212
+ ---
213
+
214
+ ## Failure behaviour
215
+
216
+ The governing rule: **a host session never fails because of BaseInstRunner.**
217
+
218
+ | Failure | Behaviour |
219
+ |---|---|
220
+ | Control server unreachable at startup | buffer ≤ 5 s, then Tier 2; logged once |
221
+ | Control server dies mid-session | the proxy switches to Tier 2 for the rest; run flagged lossy |
222
+ | Recorder HTTP fails | send dropped and logged; never retried inline |
223
+ | Upstream fails to start | `initialize` still answers; requests return an error — one broken server, not a broken session |
224
+ | Upstream dies mid-call | `isError: true` with the transport message, so the model can react |
225
+ | Unknown JSON-RPC method | forwarded; any rejection is relayed back |
226
+ | Step queue overflow | oldest dropped, run flagged lossy |
227
+ | Host sends unparseable input | `-32700` and the relay keeps going |
228
+
229
+ ---
230
+
231
+ ## Development
232
+
233
+ ```bash
234
+ npm run build # src → dist
235
+ npm test # 233 tests: transcript identity, passthrough, correlation, config, replay
236
+ npm run typecheck
237
+ npm run test:smoke # end-to-end replay with real processes (needs build + pretest first)
238
+ ```
239
+
240
+ The core test is **transcript identity**: the same scripted conversation is run
241
+ against a fake upstream directly and through the proxy, and every host-visible
242
+ message must match. If a change breaks transparency, that test says so first.
243
+
244
+ `test/smoke-replay.mjs` is the other one worth running by hand. Everything else
245
+ runs in-process; this starts a stand-in BaseIn service, a real `bir-hooks`, a real
246
+ `bir-proxy` wrapping a real MCP server and a real `bir-scenario`, then drives a
247
+ two-step scenario through actual stdio. It is what catches the seams between
248
+ processes — and it is how the "a proxy between polls is not a missing proxy" bug
249
+ was found.
250
+
251
+ `test/fake-upstream.ts` is a scriptable MCP server — arbitrary capabilities,
252
+ prompts, resources, `list_changed` notifications, reverse-direction
253
+ `elicitation/create`, delays, failures, and dying on command. Everything else
254
+ builds on it.
255
+
256
+ ---
257
+
258
+ ## Known gaps
259
+
260
+ - **`metadata.lossy` is the value known at run start.** A run that turns lossy
261
+ later says so on the audit log (`run.lossy`) and in the step gap, but the
262
+ metadata is not updated: the service exposes no metadata patch, and this is
263
+ exactly open question 1 in the design. Fixing it properly needs a server-side
264
+ decision, not a client-side workaround.
265
+ - **Subagent steps are attributed, not partitioned.** `SubagentStart` /
266
+ `SubagentStop` give us `agent_id`, which is prefixed onto a step's `context`.
267
+ A real agent column is a schema change, and out of scope for v1 (D6).
268
+ - **`--lazy` defers to the first host message, not to `tools/list`.** Deferring
269
+ further would mean answering `initialize` before the upstream exists — that is,
270
+ fabricating its capabilities, which is the thing Phase 3 exists to prevent. For
271
+ a client that connects at session start (Claude Code does) the flag saves
272
+ nothing; for one that connects on demand it saves the whole process. See
273
+ `src/upstream/lazy-client.ts`.
274
+ - **Coexistence with RRepeat is untested.** Both would record the same session.
275
+ Runs carry `metadata.recorder`, so server-side dedupe is possible, but nothing
276
+ here detects the other (design §13, open question 2).
@@ -0,0 +1,85 @@
1
+ /**
2
+ * auth client — ported from RRepeat's `src/auth/client.ts` (Phase 4.1).
3
+ *
4
+ * Authenticates the CLI user against the BaseIn auth-service and returns a JWT
5
+ * session. Every recording call carries that access token; the recording's owner
6
+ * is the JWT subject on the server side, which is what makes tenancy work
7
+ * without BaseInstRunner knowing anything about it.
8
+ *
9
+ * Flow:
10
+ * 1. Load the cached session from ~/.baseinstrunner/credentials.json.
11
+ * 2. If the access token is still valid, reuse it.
12
+ * 3. Else if a refresh token is present, POST /auth/refresh (silent).
13
+ * 4. Else prompt for email + password and POST /auth/login.
14
+ * 5. Persist the result (mode 0600) and return it.
15
+ *
16
+ * WHY IT NEVER `process.exit`s: `bir-proxy` runs inside the host's process tree.
17
+ * Failing to authenticate must degrade to not-recording (§10), not kill a server
18
+ * the host is waiting on. Callers get `undefined` and decide.
19
+ *
20
+ * Config:
21
+ * BIR_AUTH_URL base URL of the BaseIn auth-service (required).
22
+ * BIR_AUTH_DISABLE "1" to skip auth entirely (local dev / CI).
23
+ */
24
+ export interface AuthUser {
25
+ id: string;
26
+ email: string;
27
+ name?: string;
28
+ }
29
+ export interface AuthSession {
30
+ accessToken: string;
31
+ refreshToken: string;
32
+ /** Epoch milliseconds at which the access token expires. */
33
+ accessExpiresAt: number;
34
+ user: AuthUser;
35
+ }
36
+ export interface AuthenticateOptions {
37
+ /** Base URL of the auth service. Defaults to BIR_AUTH_URL. */
38
+ authUrl?: string;
39
+ /** Never prompt: fail (return undefined) if no valid/refreshable session exists. */
40
+ nonInteractive?: boolean;
41
+ }
42
+ /**
43
+ * Where the cached session lives.
44
+ *
45
+ * A **function**, not a constant: `configDir()` reads `BIR_HOME`, and freezing
46
+ * this at import time meant the directory we created and the file we wrote could
47
+ * disagree the moment anything set that variable after load — which surfaced as
48
+ * an ENOENT deep inside a token refresh.
49
+ */
50
+ export declare function credentialsPath(): string;
51
+ /** Load the cached session, or undefined if missing/unreadable/malformed. */
52
+ export declare function loadCredentials(): AuthSession | undefined;
53
+ /** Persist the session with mode 0600. */
54
+ export declare function saveCredentials(session: AuthSession): void;
55
+ /** Remove the cached session (`bir logout`). No-op if absent. */
56
+ export declare function clearCredentials(): void;
57
+ export declare function resolveAuthUrl(opts?: AuthenticateOptions): string;
58
+ /**
59
+ * Follow a redirect on the base URL *once*, so a bearer token is never sent to
60
+ * a URL that will bounce it.
61
+ *
62
+ * WHY THIS EXISTS. A service behind a TLS-terminating proxy answers plain HTTP
63
+ * with `308 → https://…`. Node's `fetch` follows that, and because `http:` and
64
+ * `https:` are different origins the spec requires it to **strip the
65
+ * `Authorization` header**. The request arrives unauthenticated.
66
+ *
67
+ * The failure is maddening because sign-in works: login sends credentials in the
68
+ * BODY, which a 308 preserves. So `auth.ok` is printed, and then every
69
+ * subsequent call fails `401 {"error":"missing_token"}` — the server saying it
70
+ * saw no token at all, which reads like a bug in the client rather than a
71
+ * redirect. Observed against a Caddy-fronted deployment; the whole session
72
+ * recorded nothing.
73
+ *
74
+ * A same-host upgrade to https is adopted silently — it is what the operator
75
+ * meant. Anything else (a different host, a downgrade to http) is reported and
76
+ * NOT followed: quietly re-sending someone's bearer token to a host they did not
77
+ * configure is worse than failing.
78
+ */
79
+ export declare function normalizeAuthUrl(baseUrl: string, fetchImpl?: typeof fetch): Promise<string>;
80
+ /**
81
+ * Authenticate and return a valid session, or `undefined` when none can be
82
+ * obtained. Callers degrade to not-recording rather than failing the session.
83
+ */
84
+ export declare function authenticate(opts?: AuthenticateOptions): Promise<AuthSession | undefined>;
85
+ //# sourceMappingURL=client.d.ts.map