@carrieros/cli 0.2.0 → 0.4.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 (124) hide show
  1. package/README.md +170 -64
  2. package/dist/commands/attach.js +45 -0
  3. package/dist/commands/btw.js +78 -0
  4. package/dist/commands/changes.js +167 -0
  5. package/dist/commands/chat.js +81 -15
  6. package/dist/commands/context.js +200 -0
  7. package/dist/commands/doctor.js +63 -3
  8. package/dist/commands/files.js +84 -23
  9. package/dist/commands/help.js +1 -0
  10. package/dist/commands/history.js +88 -23
  11. package/dist/commands/login.js +31 -7
  12. package/dist/commands/logout.js +1 -0
  13. package/dist/commands/memory.js +162 -0
  14. package/dist/commands/mode-model.js +39 -3
  15. package/dist/commands/modes.js +4 -1
  16. package/dist/commands/permissions.js +77 -0
  17. package/dist/commands/project.js +106 -0
  18. package/dist/commands/repo.js +146 -0
  19. package/dist/commands/repos.js +61 -0
  20. package/dist/commands/review.js +92 -0
  21. package/dist/commands/scope.js +9 -6
  22. package/dist/commands/search.js +92 -0
  23. package/dist/commands/sessions.js +224 -65
  24. package/dist/commands/settings.js +94 -0
  25. package/dist/commands/update.js +229 -0
  26. package/dist/commands/upload.js +204 -31
  27. package/dist/commands/usage.js +213 -17
  28. package/dist/commands/whoami.js +12 -9
  29. package/dist/ide/editor-context.js +275 -0
  30. package/dist/ide/ide-session.js +355 -0
  31. package/dist/ide/tool-host.js +109 -0
  32. package/dist/main.js +26 -1
  33. package/dist/services/api.js +315 -15
  34. package/dist/services/auth-origin.js +20 -0
  35. package/dist/services/chat-stream.js +168 -0
  36. package/dist/services/checkpoints.js +278 -0
  37. package/dist/services/client-tools.js +352 -0
  38. package/dist/services/code-intel.js +243 -0
  39. package/dist/services/code-symbols.js +94 -0
  40. package/dist/services/coding-workflow.js +554 -0
  41. package/dist/services/command-coordinator.js +273 -0
  42. package/dist/services/command-policy.js +434 -0
  43. package/dist/services/config.js +311 -24
  44. package/dist/services/container-runner.js +254 -0
  45. package/dist/services/container-supervisor.js +38 -0
  46. package/dist/services/context.js +296 -23
  47. package/dist/services/device-auth.js +72 -35
  48. package/dist/services/diff.js +196 -0
  49. package/dist/services/env-policy.js +108 -0
  50. package/dist/services/execution-platform.js +30 -0
  51. package/dist/services/file-detect.js +104 -0
  52. package/dist/services/file-resolver.js +199 -0
  53. package/dist/services/file-upload.js +126 -0
  54. package/dist/services/folder-key.js +22 -0
  55. package/dist/services/git-tools.js +429 -0
  56. package/dist/services/glob-match.js +164 -0
  57. package/dist/services/helper-bin.js +85 -0
  58. package/dist/services/ignore-rules.js +189 -0
  59. package/dist/services/instruction-resolver.js +54 -0
  60. package/dist/services/job-helper.js +328 -0
  61. package/dist/services/local-files.js +639 -0
  62. package/dist/services/local-tools.js +937 -0
  63. package/dist/services/login-flow.js +55 -17
  64. package/dist/services/meta.js +3 -1
  65. package/dist/services/mutation-coordinator.js +327 -0
  66. package/dist/services/mutation-groups.js +309 -0
  67. package/dist/services/mutation-recovery.js +159 -0
  68. package/dist/services/mutations.js +679 -0
  69. package/dist/services/open-url.js +68 -0
  70. package/dist/services/output.js +5 -2
  71. package/dist/services/packaged-update.js +403 -0
  72. package/dist/services/packaged.js +39 -0
  73. package/dist/services/permission-mode.js +60 -0
  74. package/dist/services/process-runner.js +412 -0
  75. package/dist/services/project-context.js +298 -0
  76. package/dist/services/redaction.js +201 -0
  77. package/dist/services/result-reconciliation.js +49 -0
  78. package/dist/services/secure-store.js +217 -0
  79. package/dist/services/session-resolver.js +113 -0
  80. package/dist/services/sia.js +51 -0
  81. package/dist/services/supported-runtime.js +59 -0
  82. package/dist/services/task-context.js +212 -0
  83. package/dist/services/task-snapshot.js +38 -0
  84. package/dist/services/tool-result-outbox.js +195 -0
  85. package/dist/services/update.js +311 -0
  86. package/dist/services/workspace-registry.js +310 -0
  87. package/dist/services/workspace.js +580 -0
  88. package/dist/shell/shell-chat.js +1297 -59
  89. package/dist/shell/shell-commands.js +243 -12
  90. package/dist/shell/shell-prompt.js +94 -11
  91. package/dist/shell/shell-renderer.js +8 -20
  92. package/dist/shell/shell-router.js +19 -3
  93. package/dist/shell/shell.js +252 -22
  94. package/dist/shell/task-context-view.js +40 -0
  95. package/dist/types/index.js +3 -1
  96. package/dist/ui/agent-cards.js +111 -0
  97. package/dist/ui/agent-presentation.js +131 -0
  98. package/dist/ui/approval.js +111 -0
  99. package/dist/ui/banner.js +31 -48
  100. package/dist/ui/btw-overlay.js +230 -0
  101. package/dist/ui/change-review.js +334 -0
  102. package/dist/ui/chat-renderer.js +207 -14
  103. package/dist/ui/colors.js +12 -8
  104. package/dist/ui/command-details.js +11 -0
  105. package/dist/ui/command-filter.js +26 -5
  106. package/dist/ui/command-palette.js +44 -14
  107. package/dist/ui/command-registry.js +92 -10
  108. package/dist/ui/command-review.js +187 -0
  109. package/dist/ui/composer-layout.js +48 -1
  110. package/dist/ui/conversation.js +450 -0
  111. package/dist/ui/interrupt.js +35 -0
  112. package/dist/ui/live.js +4 -2
  113. package/dist/ui/markdown.js +63 -9
  114. package/dist/ui/message.js +14 -31
  115. package/dist/ui/screen-prompt.js +181 -0
  116. package/dist/ui/screen.js +19 -11
  117. package/dist/ui/select.js +191 -29
  118. package/dist/ui/sia-qa.js +213 -0
  119. package/dist/ui/task-context-card.js +99 -0
  120. package/dist/ui/text-prompt.js +74 -0
  121. package/dist/ui/trust.js +88 -0
  122. package/dist/ui/ui.js +27 -1
  123. package/package.json +18 -4
  124. package/dist/ui/activity.js +0 -77
@@ -0,0 +1,273 @@
1
+ import { createHash } from "node:crypto";
2
+ import { APPROVAL_TIMEOUT_MS } from "./mutations.js";
3
+ import { PolicyError, evaluateCommand } from "./command-policy.js";
4
+ import { JobHelperError } from "./job-helper.js";
5
+ import { RunnerError, runProcess } from "./process-runner.js";
6
+ import { Redactor, projectEnvSecrets } from "./redaction.js";
7
+ import { neutralizingOverrides } from "./workspace.js";
8
+ import { containerRuntime, verifyContainerRuntime } from "./container-runner.js";
9
+ import { executionAdapter, executionPlatform, executionContract } from "./execution-platform.js";
10
+ import { RepositoryError, activeRepository, isWildcardSelector, listRepositories, resolveOperableRepository } from "./workspace-registry.js";
11
+ /**
12
+ * One turn's proposed local commands: check → ask → run → answer.
13
+ *
14
+ * Order is the whole point. The policy decides first, so a refused command
15
+ * never reaches the prompt. The person at the keyboard decides second, on
16
+ * exactly what will run — resolved script text included. Only then does the
17
+ * contained runner start anything. Commands run one at a time.
18
+ *
19
+ * Like the change reviewer, this is not an agent: it never proposes, retries or
20
+ * re-runs anything, and a "no" ends command proposals for the rest of the turn.
21
+ */
22
+ /** Largest serialized result posted — under the server's 64 KB cap. */
23
+ const MAX_RESULT_BYTES = 62 * 1024;
24
+ /** Keep the posted result under the cap by trimming the middle of the output. */
25
+ export function fitCommandResult(result) {
26
+ let out = result;
27
+ let size = Buffer.byteLength(JSON.stringify(out), "utf8");
28
+ while (size > MAX_RESULT_BYTES && typeof out.output === "string" && out.output.length > 0) {
29
+ const text = out.output;
30
+ const keep = Math.floor(text.length * 0.8);
31
+ const head = text.slice(0, Math.floor(keep * 0.25));
32
+ const tail = text.slice(text.length - Math.floor(keep * 0.75));
33
+ out = { ...out, output: `${head}\n… [output trimmed to fit] …\n${tail}`, truncated: true };
34
+ size = Buffer.byteLength(JSON.stringify(out), "utf8");
35
+ }
36
+ return out;
37
+ }
38
+ /**
39
+ * The repository a command runs in.
40
+ *
41
+ * Omitted means the active repository. A name may be any registered, TRUSTED
42
+ * repository — the user reads that name on the approval card before anything
43
+ * starts, and the active repository does not move. A wildcard, an unknown name
44
+ * or an untrusted one is an error the model sees.
45
+ *
46
+ * The returned root is what the policy, the cwd and the runner all use, so the
47
+ * package.json a script is read from is the SELECTED repository's, never the
48
+ * active one's.
49
+ */
50
+ function commandRepository(selector, fallbackRoot) {
51
+ if (selector === undefined || selector === null || selector === "") {
52
+ return repositoryForRoot(fallbackRoot) ?? activeRepository();
53
+ }
54
+ if (isWildcardSelector(selector)) {
55
+ throw new RunnerError("REPOSITORY_WILDCARD_NOT_ALLOWED", 'A command must name one repository; "*" is not accepted.');
56
+ }
57
+ return resolveOperableRepository(selector);
58
+ }
59
+ /** The registered repository a root belongs to, for the result and the audit. */
60
+ function repositoryForRoot(root) {
61
+ const norm = (p) => (process.platform === "win32" ? p.toLowerCase() : p);
62
+ return listRepositories().find((r) => norm(r.root) === norm(root));
63
+ }
64
+ function errorCodeOf(err) {
65
+ if (err instanceof PolicyError || err instanceof RunnerError || err instanceof JobHelperError || err instanceof RepositoryError) {
66
+ return { code: err.code, message: err.message };
67
+ }
68
+ return { code: "START_FAILED", message: err?.message ?? "The command could not be run." };
69
+ }
70
+ export function createCommandCoordinator(opts) {
71
+ const { ui } = opts;
72
+ let chain = Promise.resolve();
73
+ let declined = false;
74
+ // One answer per call. Transport owns retrying it; a later error must never
75
+ // send a second, contradictory result for a command that already ran.
76
+ const answered = new Set();
77
+ const submitOnce = (result) => {
78
+ if (answered.has(result.tool_call_id))
79
+ return Promise.resolve();
80
+ answered.add(result.tool_call_id);
81
+ return opts.submit(result);
82
+ };
83
+ const seen = new Set();
84
+ const fail = (call, started, code, message) => submitOnce({ tool_call_id: call.tool_call_id, ok: false, error: { code, message }, duration_ms: Date.now() - started });
85
+ async function process(call) {
86
+ const boundCall = structuredClone(call);
87
+ const started = Date.now();
88
+ const args = (call.arguments ?? {});
89
+ const reason = typeof args.reason === "string" ? args.reason.slice(0, 300) : "";
90
+ if (declined) {
91
+ await fail(boundCall, started, "COMMAND_REJECTED", "The user already declined a command in this turn. Nothing ran; ask how to proceed.");
92
+ return;
93
+ }
94
+ if (opts.signal.aborted) {
95
+ await fail(boundCall, started, "COMMAND_CANCELLED", "The turn was stopped. Nothing ran.");
96
+ return;
97
+ }
98
+ // 1. Which repository, then policy — both before anyone is asked. A command
99
+ // runs in ONE repository: the active one, or one explicitly named as long as
100
+ // it IS the active one. Naming another is refused, never redirected.
101
+ let root = opts.root;
102
+ let repository = repositoryForRoot(opts.root);
103
+ try {
104
+ repository = commandRepository(args.repository, opts.root);
105
+ root = repository.root;
106
+ }
107
+ catch (err) {
108
+ const e = errorCodeOf(err);
109
+ ui.finished({ decision: null, errorCode: e.code, message: e.message });
110
+ await fail(boundCall, started, e.code, e.message);
111
+ return;
112
+ }
113
+ let decision;
114
+ let policyFingerprint;
115
+ try {
116
+ const { reason: _r, repository: _repo, ...proposal } = args;
117
+ decision = evaluateCommand(proposal, root);
118
+ policyFingerprint = JSON.stringify(decision);
119
+ }
120
+ catch (err) {
121
+ const e = errorCodeOf(err);
122
+ ui.finished({ decision: null, errorCode: e.code, message: e.message });
123
+ await fail(boundCall, started, e.code, e.message);
124
+ return;
125
+ }
126
+ // Git reads file content through repository-configured programs; neutralize
127
+ // them for this call exactly as repository detection does.
128
+ if (decision.program === "git") {
129
+ const overrides = neutralizingOverrides(root);
130
+ if (!overrides) {
131
+ const message = "The repository's Git configuration could not be read safely, so the command was not run.";
132
+ ui.finished({ decision, errorCode: "COMMAND_DENIED", message });
133
+ await fail(boundCall, started, "COMMAND_DENIED", message);
134
+ return;
135
+ }
136
+ const sub = decision.effectiveArgs.indexOf(decision.args[0]);
137
+ decision = { ...decision, effectiveArgs: [...decision.effectiveArgs.slice(0, sub), ...overrides, ...decision.effectiveArgs.slice(sub)] };
138
+ }
139
+ // Capability is checked before asking for approval, and again by runProcess.
140
+ try {
141
+ if (executionAdapter(executionPlatform()) === "isolated-container") {
142
+ await verifyContainerRuntime(containerRuntime());
143
+ }
144
+ }
145
+ catch (err) {
146
+ const e = errorCodeOf(err);
147
+ ui.finished({ decision, errorCode: e.code, message: e.message });
148
+ await fail(boundCall, started, e.code, e.message);
149
+ return;
150
+ }
151
+ // 2. Approval, then 3. run — both while holding the terminal.
152
+ await opts.withTerminal(async () => {
153
+ if (opts.signal.aborted) {
154
+ await fail(boundCall, started, "COMMAND_CANCELLED", "The turn was stopped before command review. Nothing ran.");
155
+ return;
156
+ }
157
+ if (executionAdapter(executionPlatform()) === "isolated-container") {
158
+ decision = { ...decision, notes: [
159
+ ...decision.notes.filter(note => !note.includes("Windows account")),
160
+ "Runs in an isolated Linux container with only this repository mounted. Network access is disabled; host tools and credentials are not inherited."
161
+ ] };
162
+ }
163
+ const scope = opts.recoveryScope?.();
164
+ const fingerprint = () => createHash("sha256").update(JSON.stringify({ call, decision, root, repository: repository?.id, workflow: opts.approvalContext?.() })).digest("hex");
165
+ const reviewed = fingerprint();
166
+ const deadline = Date.now() + Math.min(APPROVAL_TIMEOUT_MS, call.timeout_ms ?? APPROVAL_TIMEOUT_MS);
167
+ const answer = await ui.review(decision, reason, repository?.name, boundCall.tool_call_id);
168
+ if (opts.signal.aborted) {
169
+ await fail(boundCall, started, "COMMAND_CANCELLED", "The turn was stopped during command review. Nothing ran.");
170
+ return;
171
+ }
172
+ if (answer !== "approve") {
173
+ declined = true;
174
+ const code = answer === "timeout" ? "APPROVAL_TIMEOUT" : answer === "cancel" ? "COMMAND_CANCELLED" : "COMMAND_REJECTED";
175
+ const message = answer === "timeout"
176
+ ? "No one approved the command in time. Nothing ran."
177
+ : answer === "cancel"
178
+ ? "The user stopped the turn. Nothing ran."
179
+ : "The user rejected the command. Nothing ran.";
180
+ ui.finished({ decision, errorCode: code, message });
181
+ await fail(boundCall, started, code, message);
182
+ if (answer === "cancel")
183
+ ui.cancelTurn();
184
+ return;
185
+ }
186
+ const { reason: _reason, repository: _selector, ...currentProposal } = args;
187
+ let unchanged = false;
188
+ try {
189
+ unchanged = Date.now() < deadline && scope === opts.recoveryScope?.() && fingerprint() === reviewed &&
190
+ commandRepository(args.repository, opts.root).id === repository?.id &&
191
+ JSON.stringify(evaluateCommand(currentProposal, root)) === policyFingerprint;
192
+ }
193
+ catch {
194
+ unchanged = false;
195
+ }
196
+ if (!unchanged) {
197
+ declined = true;
198
+ ui.finished({ decision, errorCode: "APPROVAL_INVALIDATED", message: "Command, script, scope or deadline changed during review. Request fresh approval." });
199
+ await fail(boundCall, started, "APPROVAL_INVALIDATED", "Command, script, scope or deadline changed during review. Nothing ran.");
200
+ return;
201
+ }
202
+ let result;
203
+ try {
204
+ const redactor = new Redactor([...projectEnvSecrets(root), ...(opts.cliSecrets ?? [])]);
205
+ result = await runProcess({
206
+ program: decision.program,
207
+ args: decision.effectiveArgs,
208
+ cwd: decision.cwdAbsolute,
209
+ root,
210
+ timeoutMs: decision.timeoutMs,
211
+ operationId: boundCall.tool_call_id,
212
+ signal: opts.signal,
213
+ redactor,
214
+ onStart: () => ui.started(decision),
215
+ onOutput: (stream, text) => ui.output(stream, text)
216
+ });
217
+ }
218
+ catch (err) {
219
+ const e = errorCodeOf(err);
220
+ ui.finished({ decision, errorCode: e.code, message: e.message });
221
+ await fail(boundCall, started, e.code, e.message);
222
+ return;
223
+ }
224
+ ui.finished({ decision, result });
225
+ const payload = fitCommandResult({
226
+ // Reported from the RESOLVED handle, so the audit records which
227
+ // repository was selected rather than inferring one from a path.
228
+ repository: repository?.name,
229
+ repository_id: repository?.id,
230
+ program: decision.program,
231
+ args: decision.args,
232
+ cwd: decision.cwdRelative,
233
+ risk_tier: decision.tier,
234
+ network_label: decision.network,
235
+ exit_code: result.exit_code,
236
+ signal: result.signal,
237
+ timed_out: result.timed_out,
238
+ cancelled: result.cancelled,
239
+ tree_killed: result.tree_killed,
240
+ started_at: result.started_at,
241
+ ended_at: result.ended_at,
242
+ duration_ms: result.duration_ms,
243
+ execution_backend: executionContract().backend,
244
+ stdout_bytes: result.stdout_bytes,
245
+ stderr_bytes: result.stderr_bytes,
246
+ truncated: result.truncated,
247
+ redaction_count: result.redaction_count,
248
+ dropped_environment_names: result.dropped_environment_names,
249
+ ...(decision.script ? { script: { name: decision.script.name, text: decision.script.text.slice(0, 500) } } : {}),
250
+ output: result.output
251
+ });
252
+ await submitOnce({ tool_call_id: boundCall.tool_call_id, ok: true, result: payload, duration_ms: Date.now() - started });
253
+ });
254
+ }
255
+ return {
256
+ handle(call) {
257
+ // A repeated id is the same call: it is reviewed and run at most once.
258
+ if (seen.has(call.tool_call_id))
259
+ return Promise.resolve();
260
+ seen.add(call.tool_call_id);
261
+ const identity = structuredClone(call);
262
+ const next = chain.then(() => process(call)).catch(async (err) => {
263
+ // Already answered (e.g. its delivery failed): surface that, never a second result.
264
+ if (answered.has(identity.tool_call_id))
265
+ throw err;
266
+ // A bug here must still answer the call, never leave the turn waiting.
267
+ await fail(identity, Date.now(), "START_FAILED", err?.message ?? "The command could not be processed.").catch(() => undefined);
268
+ });
269
+ chain = next.catch(() => undefined);
270
+ return next;
271
+ }
272
+ };
273
+ }