@buildanagent/cli 0.0.0-stage → 0.1.0-alpha.29

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 (78) hide show
  1. package/LICENSE +54 -0
  2. package/LICENSE-APACHE +202 -0
  3. package/LICENSE-MIT +21 -0
  4. package/README.md +143 -2
  5. package/dist/args.d.ts +20 -0
  6. package/dist/args.js +76 -0
  7. package/dist/args.js.map +1 -0
  8. package/dist/ask.d.ts +22 -0
  9. package/dist/ask.js +108 -0
  10. package/dist/ask.js.map +1 -0
  11. package/dist/bin.d.ts +2 -0
  12. package/dist/bin.js +26 -0
  13. package/dist/bin.js.map +1 -0
  14. package/dist/cli.d.ts +9 -0
  15. package/dist/cli.js +111 -0
  16. package/dist/cli.js.map +1 -0
  17. package/dist/command-lines.d.ts +25 -0
  18. package/dist/command-lines.js +166 -0
  19. package/dist/command-lines.js.map +1 -0
  20. package/dist/commands/chat.d.ts +18 -0
  21. package/dist/commands/chat.js +52 -0
  22. package/dist/commands/chat.js.map +1 -0
  23. package/dist/commands/keys.d.ts +3 -0
  24. package/dist/commands/keys.js +47 -0
  25. package/dist/commands/keys.js.map +1 -0
  26. package/dist/commands/rpc.d.ts +3 -0
  27. package/dist/commands/rpc.js +33 -0
  28. package/dist/commands/rpc.js.map +1 -0
  29. package/dist/commands/run.d.ts +3 -0
  30. package/dist/commands/run.js +20 -0
  31. package/dist/commands/run.js.map +1 -0
  32. package/dist/commands/sessions.d.ts +3 -0
  33. package/dist/commands/sessions.js +161 -0
  34. package/dist/commands/sessions.js.map +1 -0
  35. package/dist/commands/unlock.d.ts +3 -0
  36. package/dist/commands/unlock.js +31 -0
  37. package/dist/commands/unlock.js.map +1 -0
  38. package/dist/errors.d.ts +31 -0
  39. package/dist/errors.js +22 -0
  40. package/dist/errors.js.map +1 -0
  41. package/dist/host.d.ts +61 -0
  42. package/dist/host.js +114 -0
  43. package/dist/host.js.map +1 -0
  44. package/dist/index.d.ts +12 -0
  45. package/dist/index.js +11 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/io.d.ts +36 -0
  48. package/dist/io.js +18 -0
  49. package/dist/io.js.map +1 -0
  50. package/dist/live.d.ts +33 -0
  51. package/dist/live.js +100 -0
  52. package/dist/live.js.map +1 -0
  53. package/dist/meter.d.ts +50 -0
  54. package/dist/meter.js +91 -0
  55. package/dist/meter.js.map +1 -0
  56. package/dist/model.d.ts +12 -0
  57. package/dist/model.js +35 -0
  58. package/dist/model.js.map +1 -0
  59. package/dist/options.d.ts +30 -0
  60. package/dist/options.js +2 -0
  61. package/dist/options.js.map +1 -0
  62. package/dist/otlp.d.ts +28 -0
  63. package/dist/otlp.js +110 -0
  64. package/dist/otlp.js.map +1 -0
  65. package/dist/output.d.ts +18 -0
  66. package/dist/output.js +28 -0
  67. package/dist/output.js.map +1 -0
  68. package/dist/render.d.ts +18 -0
  69. package/dist/render.js +65 -0
  70. package/dist/render.js.map +1 -0
  71. package/dist/visible.d.ts +11 -0
  72. package/dist/visible.js +63 -0
  73. package/dist/visible.js.map +1 -0
  74. package/dist/watch.d.ts +63 -0
  75. package/dist/watch.js +219 -0
  76. package/dist/watch.js.map +1 -0
  77. package/llms.txt +31 -0
  78. package/package.json +43 -3
package/LICENSE ADDED
@@ -0,0 +1,54 @@
1
+ buildanagent is open source under five licence terms, one for each kind of
2
+ content in the repository.
3
+
4
+ Code: Apache-2.0 OR MIT, at your option
5
+ Scripts (scripts/), our packages (packages/), the verification harnesses and
6
+ their examples (verify/), the book's code and figure generators (apps/manual/),
7
+ the code of the docs app (apps/docs/) and of the website (apps/site/),
8
+ the CI workflows (.github/), the configuration files, and the code blocks in
9
+ the agent skills (.agents/skills/) are licensed under either of
10
+
11
+ * the Apache License, Version 2.0 (LICENSE-APACHE), or
12
+ * the MIT License (LICENSE-MIT).
13
+
14
+ SPDX-License-Identifier: Apache-2.0 OR MIT
15
+
16
+ Code from the garment engineering textbook: Apache-2.0 only
17
+ Some files in apps/manual/ are copied or adapted from the owner's garment
18
+ engineering textbook (tajistudios/garment-engineering-textbook), with the
19
+ owner's permission. Those files are licensed under the Apache License,
20
+ Version 2.0 (LICENSE-APACHE) only, not under the MIT option above. Each one
21
+ opens with a header comment that names its source and this licence, and
22
+ THIRD-PARTY-NOTICES.md lists each file with its source path and commit.
23
+
24
+ SPDX-License-Identifier: Apache-2.0
25
+
26
+ Documentation: CC BY 4.0
27
+ The documents under docs/, the book's prose and figures (apps/manual/), the
28
+ docs pages (apps/docs/src/content/docs/), the website's pages (apps/site/src/pages/),
29
+ the READMEs, the prose of the agent skills (.agents/skills/) and AGENTS.md are
30
+ licensed under the Creative Commons Attribution 4.0 International licence
31
+ (LICENSE-DOCS).
32
+
33
+ SPDX-License-Identifier: CC-BY-4.0
34
+
35
+ The book's fonts: SIL Open Font License 1.1
36
+ The font files under apps/manual/public/fonts/ are subsets of Instrument
37
+ Serif, Literata and Google Sans Code, and are not ours. They stay under the
38
+ SIL Open Font License, Version 1.1, whose text ships as OFL.txt beside each
39
+ family's files. THIRD-PARTY-NOTICES.md lists each family, its source commit
40
+ and how the subsets are named.
41
+
42
+ SPDX-License-Identifier: OFL-1.1
43
+
44
+ Mirrored third-party docs: their own licences
45
+ The upstream documentation mirrored under docs/reference/*/source/ is not ours.
46
+ Each mirror keeps its upstream licence file, and its _manifest.json records
47
+ the source repository and the tag or commit it was copied from.
48
+ THIRD-PARTY-NOTICES.md lists every mirror and its licence.
49
+
50
+ Copyright (c) 2026 The buildanagent contributors (named agent-speak until
51
+ 2026-11-19).
52
+
53
+ Contributions to this repository are accepted under the same terms: code
54
+ under Apache-2.0 OR MIT, documentation under CC BY 4.0.
package/LICENSE-APACHE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 The buildanagent contributors (named agent-speak until 2026-11-19)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,144 @@
1
- # Temporary Holding Version
1
+ # @buildanagent/cli
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The `buildanagent` command runs the buildanagent harness in your terminal. Use `run --json` to script one turn, `chat` to work with an agent that asks before risky calls, and `sessions` to list, search and replay your stored sessions.
4
+
5
+ The package exports `runCli()`, which the binary calls, and the helpers and types around it. [Run the CLI from your own program](#run-the-cli-from-your-own-program) shows how to call it.
6
+
7
+ ## Install the alpha with `pnpm add @buildanagent/cli@alpha`
8
+
9
+ The CLI needs Node 22.19 or later. npm serves every release under the `alpha` dist-tag until the first stable one, and the package installs the `buildanagent` command, which `pnpm exec` runs from your project:
10
+
11
+ ```sh
12
+ pnpm add @buildanagent/cli@alpha
13
+ pnpm exec buildanagent --help
14
+ ```
15
+
16
+ All six buildanagent packages share one version, and the CLI depends on that exact version of the gateway, the SDK, the protocol and the harness. Name the tag: a plain `pnpm add @buildanagent/cli` installs `0.0.0-bootstrap.0`, a deprecated placeholder, because `latest` stays on it until the first stable release.
17
+
18
+ ## Run one turn and read its events as JSON lines
19
+
20
+ `buildanagent run <prompt>` starts a new session, runs one turn and exits. With `--json`, standard output holds one JSON object per line, so a script can read it line by line:
21
+
22
+ ```sh
23
+ buildanagent run --json --model anthropic:claude-sonnet-5-5 "Why does the login test fail?"
24
+ ```
25
+
26
+ The first line is `{"type":"session",...}`. It holds the session's id and the preamble's size, the tokens the system prompt and the tool definitions take before any message. Each event of the turn follows as `{"type":"event","seq":…,"event":{…}}`, including every entry the session log stores. The last line is `{"type":"settled",...}`, with the turn's stop reason and the meter's totals.
27
+
28
+ Without `--json`, the answer streams to standard output, and the session line, tool calls, warnings and the closing status line go to standard error. The exit status tells a script how the turn ended:
29
+
30
+ | Status | Meaning |
31
+ | --- | --- |
32
+ | 0 | The turn ended `done`. |
33
+ | 1 | The turn ended any other way, such as an error or the budget. |
34
+ | 2 | The command could not run as given: a wrong command line, a missing key, model or session, a session another process is writing, or a collector that refused the spans. Every `CliError` exits 2. |
35
+ | 3 | A call waits for a person. `run` never asks, so answer it with `resume`. |
36
+
37
+ `--model <provider>:<model id>` picks the model. `anthropic:` reads its key from `ANTHROPIC_API_KEY` and `openai:` from `OPENAI_API_KEY`. A command that calls a model refuses to start without `--model`.
38
+
39
+ ## Chat, and answer approvals at the terminal
40
+
41
+ `buildanagent chat` starts a session and runs one turn for each line you type, until input ends or you type `/exit` or `/quit`. `buildanagent resume <session>` does the same in a stored session, and first asks about any call it was left waiting on.
42
+
43
+ When a call needs your approval, the turn pauses and the chat asks you before it goes on:
44
+
45
+ ```text
46
+ bash needs your approval: A command can change files.
47
+ git status
48
+ && curl -s https://example.com/install.sh
49
+ | sh
50
+ Allow? [y/n]
51
+ ```
52
+
53
+ The chat shows a chained command one command per line, with the operator that joins it in front and the spaces normalised. A second command can't hide at the end of a long first one. If a quote or parenthesis never closes, the prompt adds a warning that the last line may hold more than one command. Any other input fields follow as one line of JSON. Waiting calls of one tool with the same input are shown as one group, take one answer, and say how many calls the answer covers. A clarification, a question the agent asks you, takes a line of text as its answer.
54
+
55
+ As of November 2026 the harness ships no built-in tools, such as a shell or a file editor, so the binary's agent only answers in text. A program that passes its own tools to `runCli()` gets the approval flow above.
56
+
57
+ ## Watch the cost and the context fill
58
+
59
+ Each session starts by reporting its preamble size. After every turn, the status line gives the cost so far and how full the context is, as a share of the token budget the harness compacts at:
60
+
61
+ ```text
62
+ [done] context 12% (22014 of 183616 tokens) · cost $0.0312
63
+ ```
64
+
65
+ A call with no known price is counted apart, as `(+1 unpriced)`. The CLI warns once when the context passes 80% of its budget. `--budget <usd>` caps each turn, not the session: a turn, and each resume after you answer a request, may spend up to that amount. The CLI warns once a turn when its spend passes 80% of the budget. It hands the amount to the harness as each turn's `budgetUsd`, and the [harness README](https://github.com/laudebugs/buildanagent/blob/main/packages/harness/README.md) says what the harness does with it.
66
+
67
+ ## List, show, search and replay stored sessions
68
+
69
+ Every session lives on your machine, under `$XDG_STATE_HOME/buildanagent` or `~/.local/state/buildanagent`, or the folder `--state-dir` names. Four commands read them without running anything:
70
+
71
+ | Command | What it prints |
72
+ | --- | --- |
73
+ | `sessions list` | Each session's id, start time, last entry time and entry count. |
74
+ | `sessions show <session>` | Every entry of the log, with its sequence number and type. |
75
+ | `sessions search <text>` | Scans every stored session for each message or tool result that holds the text, ignoring case. Each hit shows the session id, the sequence number and who said it. It exits 1 when none does. |
76
+ | `sessions replay <session>` | The conversation as the log recorded it, with the cost it ran up. |
77
+
78
+ A replay reads the log back and makes no model call, so it costs nothing and shows exactly what happened. With `--json`, `sessions replay` prints one `{"type":"event"}` line per log entry, each holding an `entry_appended` event, then one `{"type":"replayed"}` line with the entry count and the meter. A live `run --json` prints `entry_appended` lines like these too, among its streaming events such as `text_delta`, but a replay has no streaming events to print. The other three commands print one JSON object per result.
79
+
80
+ ## Fix a stuck lock or change the approval keys
81
+
82
+ Only one process writes a session at a time, and that process holds the session's lock. A lock left by a crash on this machine frees itself once its lease ends. `buildanagent session unlock <session> --force` removes any lock at once. Without `--force` it removes nothing and exits 2, because two writers in one log would corrupt it.
83
+
84
+ Each pause for approval is signed with a key from a key ring beside the sessions. `buildanagent keys rotate` adds a key and makes it current, and the older keys still check the requests they signed. `buildanagent keys prune` removes every older key that no waiting request names.
85
+
86
+ ## Nothing leaves your machine except the model call and the collector you name
87
+
88
+ The CLI connects to the provider `--model` names and to the collector `--otlp-endpoint` names, and to nothing else. It runs no update check, sends no usage data and adds no attribution to your commits. `--otlp-endpoint <url>` sends OpenTelemetry spans for each run, model call and tool call to that collector, without prompts, messages or tool arguments.
89
+
90
+ ## Serve the protocol to another program with `rpc`
91
+
92
+ `buildanagent rpc` serves the harness's protocol to one client over standard input and output, one JSON-RPC 2.0 message per line. Use it to drive the harness from another process or language: an editor, a test runner or a script. The command takes the same `--model`, `--state-dir`, `--budget` and `--otlp-endpoint` flags as `run`, and refuses to start without `--model`:
93
+
94
+ ```sh
95
+ buildanagent rpc --model anthropic:claude-sonnet-5-5
96
+ ```
97
+
98
+ `rpc` authenticates nobody: whoever holds its standard input and output acts as the dispatcher's user, and a `Resolution.by` field is passed on as the client sent it. Run it only where you trust every process that can reach those streams.
99
+
100
+ The [protocol README](https://github.com/laudebugs/buildanagent/blob/main/packages/protocol/README.md) lists every method. The client sends `initialize`, then `session/create` or `session/open`, then `turn/start`, and waits for each response before it sends the next command. The server writes each command's response, the session's `event` notifications and one `turn/settled` per turn to standard output:
101
+
102
+ ```text
103
+ → {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"clientInfo":{"name":"my-editor","version":"1.0.0"},"capabilities":[]}}
104
+ → {"jsonrpc":"2.0","id":2,"method":"session/create","params":{"cwd":"/work"}}
105
+ → {"jsonrpc":"2.0","id":3,"method":"turn/start","params":{"session":"<id from 2>","input":[{"type":"text","text":"Why does the login test fail?"}]}}
106
+ ← {"jsonrpc":"2.0","method":"event","params":{"session":"…","seq":4,"event":{…}}}
107
+ ← {"jsonrpc":"2.0","method":"turn/settled","params":{"session":"…","turn":"…","result":{"stopReason":"done",…}}}
108
+ ```
109
+
110
+ Standard output carries protocol lines and nothing else. The transport's own diagnostics, such as a line it could not read, go to standard error. A call that needs approval reaches the client as an `approval/request`, which the client answers with its decision. The paused turn settles as `awaiting_approval`, and the run the answer resumes settles again on its own.
111
+
112
+ Keep standard input open until the turn's `turn/settled` arrives: the command exits 0 when input closes, and it answers every command it has read but doesn't wait for a running turn. It also ends if the server closes the connection.
113
+
114
+ ## Run the CLI from your own program
115
+
116
+ `runCli()` runs one command line and resolves with its exit status. A `CliIo` gives it the terminal: standard output and error, a line reader, the environment and the working folder. `CliOptions` can also pass a model you built, extra tools, and `HostRules` that add deny, ask and allow rules. The tools run in process: the CLI has no client side for a `client_request` tool, so a call to one fails with an error the model reads. `PACKAGE_NAME` is the package's name. This program runs one turn and reads its JSON lines:
117
+
118
+ ```ts
119
+ // Runs one turn with `buildanagent run --json` from a program and reads its events, one JSON object per line.
120
+
121
+ import { type CliIo, runCli } from "@buildanagent/cli";
122
+ import type { Model } from "@buildanagent/gateway";
123
+
124
+ /** Asks `question`, keeps sessions under `stateDir`, and returns the exit status and the parsed JSON lines. */
125
+ export async function askOnce(model: Model, stateDir: string, question: string) {
126
+ let stdout = "";
127
+ const io: CliIo = {
128
+ write: (text) => {
129
+ stdout += text;
130
+ },
131
+ error: (text) => console.error(text),
132
+ readLine: async () => undefined,
133
+ env: {},
134
+ cwd: process.cwd(),
135
+ };
136
+ const status = await runCli(["run", "--json", "--state-dir", stateDir, question], { io, model });
137
+ const lines = stdout.split("\n").filter((line) => line !== "");
138
+ return { status, objects: lines.map((line) => JSON.parse(line) as { type: string }) };
139
+ }
140
+ ```
141
+
142
+ A command the CLI can't run as given resolves with status 2 and a `CliError` message on standard error; `isCliError()` recognises the error class, whose `code` is a `CliErrorCode`. `linesOf()` turns a stream of text or bytes into lines, which is how the binary reads standard input. `HELP` is the text `--help` prints. `rpc` reads the standard streams as bytes from the `CliIo`'s `streams`, a `CliStreams` pair of Node or Web streams. Without them it exits 2 with the code `no_streams`.
143
+
144
+ The [harness README](https://github.com/laudebugs/buildanagent/blob/main/packages/harness/README.md) explains the sessions, approvals and context layers the CLI drives.
package/dist/args.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /** The flags every command shares. A flag a command doesn't use is ignored by it. */
2
+ export interface Flags {
3
+ readonly json: boolean;
4
+ readonly force: boolean;
5
+ readonly help: boolean;
6
+ readonly model?: string;
7
+ readonly stateDir?: string;
8
+ readonly otlpEndpoint?: string;
9
+ readonly budgetUsd?: number;
10
+ }
11
+ /** The command line, read. */
12
+ export interface ParsedArgs {
13
+ readonly positionals: readonly string[];
14
+ readonly flags: Flags;
15
+ }
16
+ /**
17
+ * Reads `argv`, the arguments after the program name. Throws a `CliError` with code `usage` for an unknown flag, or
18
+ * for a missing or empty value, including one where the next word is another flag or `--`.
19
+ */
20
+ export declare function parseArgs(argv: readonly string[]): ParsedArgs;
package/dist/args.js ADDED
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Reads the command line into positional words and flags. Flags may come anywhere, as `--name value` or
3
+ * `--name=value`, and `--` ends them, so a prompt that starts with a dash still reads as a prompt.
4
+ */
5
+ import { CliError } from "./errors.js";
6
+ const BOOLEAN_FLAGS = {
7
+ "--json": "json",
8
+ "--force": "force",
9
+ "--help": "help",
10
+ "-h": "help",
11
+ };
12
+ const VALUE_FLAGS = {
13
+ "--model": "model",
14
+ "--state-dir": "stateDir",
15
+ "--otlp-endpoint": "otlpEndpoint",
16
+ "--budget": "budgetUsd",
17
+ };
18
+ // Decimal digits only: `Number()` would also read `0x10`, `1e3` and ` 2 ` as amounts nobody meant to type.
19
+ const DECIMAL = /^(?:\d+(?:\.\d*)?|\.\d+)$/;
20
+ function budgetOf(text) {
21
+ const value = Number(text);
22
+ if (!DECIMAL.test(text) || !Number.isFinite(value) || value <= 0) {
23
+ throw new CliError("usage", `--budget takes a positive number of US dollars, got ${JSON.stringify(text)}`);
24
+ }
25
+ return value;
26
+ }
27
+ /** Sets the flag `word` names, reading its value from `next` when it takes one. Returns how many words it used. */
28
+ function readFlag(flags, word, next) {
29
+ const [name, inline] = word.startsWith("--") && word.includes("=") ? word.split(/=(.*)/s) : [word, undefined];
30
+ const boolean = BOOLEAN_FLAGS[name];
31
+ if (boolean !== undefined && inline === undefined) {
32
+ flags[boolean] = true;
33
+ return 1;
34
+ }
35
+ const valued = VALUE_FLAGS[name];
36
+ if (valued === undefined)
37
+ throw new CliError("usage", `unknown flag ${JSON.stringify(word)}`);
38
+ const value = inline ?? next;
39
+ // An empty value, as in `--state-dir=`, would otherwise fall back to a default the caller never asked for.
40
+ if (value === undefined || value === "")
41
+ throw new CliError("usage", `${name} needs a value`);
42
+ // A flag or `--` after a value flag means its value was left out, as in `--model --json hi`; taking the next word
43
+ // would swallow it. A value that starts with a dash is still possible written as `--name=value`.
44
+ if (inline === undefined && value.startsWith("-")) {
45
+ throw new CliError("usage", `${name} needs a value, got ${JSON.stringify(value)}; write ${name}=<value> for a value that starts with -`);
46
+ }
47
+ if (valued === "budgetUsd")
48
+ flags.budgetUsd = budgetOf(value);
49
+ else
50
+ flags[valued] = value;
51
+ return inline === undefined ? 2 : 1;
52
+ }
53
+ /**
54
+ * Reads `argv`, the arguments after the program name. Throws a `CliError` with code `usage` for an unknown flag, or
55
+ * for a missing or empty value, including one where the next word is another flag or `--`.
56
+ */
57
+ export function parseArgs(argv) {
58
+ const positionals = [];
59
+ const flags = { json: false, force: false, help: false };
60
+ let at = 0;
61
+ while (at < argv.length) {
62
+ const word = argv[at];
63
+ if (word === "--") {
64
+ positionals.push(...argv.slice(at + 1));
65
+ break;
66
+ }
67
+ if (word.startsWith("-") && word !== "-")
68
+ at += readFlag(flags, word, argv[at + 1]);
69
+ else {
70
+ positionals.push(word);
71
+ at += 1;
72
+ }
73
+ }
74
+ return { positionals,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AK,EAAE,CAAC;IACvE,IAAI,EAAE,GAAG,CAAC,CAAC;IACX,OAAO,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QACxB,MAAM,IAAI,GAAG,IAAI,CAAC,EAAE,CAAW,CAAC;QAChC,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YAClB,WAAW,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;YACxC,MAAM;QACR,CAAC;QACD,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,IAAI,KAAK,GAAG;YAAE,EAAE,IAAI,QAAQ,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;aAC/E,CAAC;YACJ,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACvB,EAAE,IAAI,CAAC,CAAC;QACV,CAAC;IACH,CAAC;IACD,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC;AAChC,CAAC","sourcesContent":["/**\n * Reads the command line into positional words and flags. Flags may come anywhere, as `--name value` or\n * `--name=value`, and `--` ends them, so a prompt that starts with a dash still reads as a prompt.\n */\nimport { CliError } from \"./errors.js\";\n\n/** The flags every command shares. A flag a command doesn't use is ignored by it. */\nexport interface Flags {\n readonly json: boolean;\n readonly force: boolean;\n readonly help: boolean;\n readonly model?: string;\n readonly stateDir?: string;\n readonly otlpEndpoint?: string;\n readonly budgetUsd?: number;\n}\n\n/** The command line, read. */\nexport interface ParsedArgs {\n readonly positionals: readonly string[];\n readonly flags: Flags;\n}\n\nconst BOOLEAN_FLAGS: Readonly<Record<string, \"json\" | \"force\" | \"help\">> = {\n \"--json\": \"json\",\n \"--force\": \"force\",\n \"--help\": \"help\",\n \"-h\": \"help\",\n};\n\nconst VALUE_FLAGS: Readonly<Record<string, \"model\" | \"stateDir\" | \"otlpEndpoint\" | \"budgetUsd\">> = {\n \"--model\": \"model\",\n \"--state-dir\": \"stateDir\",\n \"--otlp-endpoint\": \"otlpEndpoint\",\n \"--budget\": \"budgetUsd\",\n};\n\n// Decimal digits only: `Number()` would also read `0x10`, `1e3` and ` 2 ` as amounts nobody meant to type.\nconst DECIMAL = /^(?:\\d+(?:\\.\\d*)?|\\.\\d+)$/;\n\nfunction budgetOf(text: string): number {\n const value = Number(text);\n if (!DECIMAL.test(text) || !Number.isFinite(value) || value <= 0) {\n throw new CliError(\"usage\", `--budget takes a positive number of US dollars, got ${JSON.stringify(text)}`);\n }\n return value;\n}\n\ntype MutableFlags = { -readonly [K in keyof Flags]: Flags[K] };\n\n/** Sets the flag `word` names, reading its value from `next` when it takes one. Returns how many words it used. */\nfunction readFlag(flags: MutableFlags, word: string, next: string | undefined): number {\n const [name, inline] = word.startsWith(\"--\") && word.includes(\"=\") ? word.split(/=(.*)/s) : [word, undefined];\n const boolean = BOOLEAN_FLAGS[name as string];\n if (boolean !== undefined && inline === undefined) {\n flags[boolean] = true;\n return 1;\n }\n const valued = VALUE_FLAGS[name as string];\n if (valued === undefined) throw new CliError(\"usage\", `unknown flag ${JSON.stringify(word)}`);\n const value = inline ?? next;\n // An empty value, as in `--state-dir=`, would otherwise fall back to a default the caller never asked for.\n if (value === undefined || value === \"\") throw new CliError(\"usage\", `${name} needs a value`);\n // A flag or `--` after a value flag means its value was left out, as in `--model --json hi`; taking the next word\n // would swallow it. A value that starts with a dash is still possible written as `--name=value`.\n if (inline === undefined && value.startsWith(\"-\")) {\n throw new CliError(\n \"usage\",\n `${name} needs a value, got ${JSON.stringify(value)}; write ${name}=<value> for a value that starts with -`,\n );\n }\n if (valued === \"budgetUsd\") flags.budgetUsd = budgetOf(value);\n else flags[valued] = value;\n return inline === undefined ? 2 : 1;\n}\n\n/**\n * Reads `argv`, the arguments after the program name. Throws a `CliError` with code `usage` for an unknown flag, or\n * for a missing or empty value, including one where the next word is another flag or `--`.\n */\nexport function parseArgs(argv: readonly string[]): ParsedArgs {\n const positionals: string[] = [];\n const flags: MutableFlags = { json: false, force: false, help: false };\n let at = 0;\n while (at < argv.length) {\n const word = argv[at] as string;\n if (word === \"--\") {\n positionals.push(...argv.slice(at + 1));\n break;\n }\n if (word.startsWith(\"-\") && word !== \"-\") at += readFlag(flags, word, argv[at + 1]);\n else {\n positionals.push(word);\n at += 1;\n }\n }\n return { positionals, flags };\n}\n"]}
package/dist/ask.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { CliIo } from "./io.js";
2
+ import type { PersonRequest, SessionWatch } from "./watch.js";
3
+ /** Several requests that one answer settles. */
4
+ interface Group {
5
+ readonly requests: PersonRequest[];
6
+ readonly input: unknown;
7
+ }
8
+ /** The approvals grouped by tool and input, in the order they were asked; each clarification stands alone. */
9
+ export declare function groupRequests(requests: readonly PersonRequest[], inputs: ReadonlyMap<string, unknown>): Group[];
10
+ /**
11
+ * The lines that describe a group's input: each chained command on its own line, or the input as JSON. The input's
12
+ * other fields follow the command as compact JSON, because they change what it does: `rm -rf build` with `cwd` set to
13
+ * `/` is not the command it reads as alone. A command whose quotes never close gets a warning line, since its split
14
+ * may hide a command inside the last line.
15
+ */
16
+ export declare function inputLines(input: unknown): string[];
17
+ /**
18
+ * Asks about every request waiting on `watch`, group by group, and sends each answer. Resolves `false` when input
19
+ * ends first, leaving the unanswered requests waiting in the log for `resume`.
20
+ */
21
+ export declare function askPerson(io: CliIo, watch: SessionWatch): Promise<boolean>;
22
+ export {};