@carrieros/cli-staging 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/dist/commands/attach.js +45 -0
- package/dist/commands/auth.js +60 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +182 -0
- package/dist/commands/context.js +200 -0
- package/dist/commands/doctor.js +119 -0
- package/dist/commands/files.js +94 -0
- package/dist/commands/help.js +37 -0
- package/dist/commands/history.js +112 -0
- package/dist/commands/login.js +62 -0
- package/dist/commands/logout.js +26 -0
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +64 -0
- package/dist/commands/models.js +28 -0
- package/dist/commands/modes.js +30 -0
- package/dist/commands/permissions.js +78 -0
- package/dist/commands/project.js +106 -0
- package/dist/commands/repo.js +146 -0
- package/dist/commands/repos.js +61 -0
- package/dist/commands/review.js +92 -0
- package/dist/commands/scope.js +155 -0
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +282 -0
- package/dist/commands/settings.js +160 -0
- package/dist/commands/update.js +285 -0
- package/dist/commands/upload.js +223 -0
- package/dist/commands/usage.js +229 -0
- package/dist/commands/version.js +26 -0
- package/dist/commands/whoami.js +44 -0
- package/dist/ide/editor-context.js +275 -0
- package/dist/ide/ide-session.js +364 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +132 -0
- package/dist/services/api.js +441 -0
- package/dist/services/auth-origin.js +20 -0
- package/dist/services/auto-update.js +221 -0
- package/dist/services/chat-stream.js +168 -0
- package/dist/services/checkpoints.js +278 -0
- package/dist/services/client-tools.js +356 -0
- package/dist/services/code-intel.js +243 -0
- package/dist/services/code-symbols.js +94 -0
- package/dist/services/coding-workflow.js +554 -0
- package/dist/services/command-coordinator.js +273 -0
- package/dist/services/command-policy.js +434 -0
- package/dist/services/config.js +384 -0
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +416 -0
- package/dist/services/device-auth.js +288 -0
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/execution-platform.js +30 -0
- package/dist/services/file-detect.js +104 -0
- package/dist/services/file-resolver.js +199 -0
- package/dist/services/file-upload.js +126 -0
- package/dist/services/folder-key.js +22 -0
- package/dist/services/git-tools.js +429 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +85 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/instruction-resolver.js +54 -0
- package/dist/services/job-helper.js +328 -0
- package/dist/services/local-files.js +639 -0
- package/dist/services/local-tools.js +937 -0
- package/dist/services/login-flow.js +156 -0
- package/dist/services/meta.js +42 -0
- package/dist/services/mutation-coordinator.js +327 -0
- package/dist/services/mutation-groups.js +309 -0
- package/dist/services/mutation-recovery.js +159 -0
- package/dist/services/mutations.js +679 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +62 -0
- package/dist/services/packaged-update.js +403 -0
- package/dist/services/packaged.js +39 -0
- package/dist/services/permission-mode.js +78 -0
- package/dist/services/process-runner.js +412 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/result-reconciliation.js +49 -0
- package/dist/services/secure-store.js +217 -0
- package/dist/services/session-resolver.js +115 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/spinner.js +40 -0
- package/dist/services/supported-runtime.js +59 -0
- package/dist/services/task-context.js +212 -0
- package/dist/services/task-snapshot.js +38 -0
- package/dist/services/tool-result-outbox.js +195 -0
- package/dist/services/update.js +730 -0
- package/dist/services/workspace-registry.js +310 -0
- package/dist/services/workspace.js +580 -0
- package/dist/shell/shell-chat.js +1622 -0
- package/dist/shell/shell-commands.js +411 -0
- package/dist/shell/shell-prompt.js +110 -0
- package/dist/shell/shell-renderer.js +51 -0
- package/dist/shell/shell-router.js +37 -0
- package/dist/shell/shell.js +414 -0
- package/dist/shell/task-context-view.js +40 -0
- package/dist/staging.js +5 -0
- package/dist/types/index.js +6 -0
- package/dist/ui/agent-cards.js +111 -0
- package/dist/ui/agent-presentation.js +131 -0
- package/dist/ui/approval.js +111 -0
- package/dist/ui/banner.js +56 -0
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +334 -0
- package/dist/ui/chat-renderer.js +463 -0
- package/dist/ui/code-block.js +52 -0
- package/dist/ui/color-picker.js +100 -0
- package/dist/ui/colors.js +55 -0
- package/dist/ui/command-details.js +11 -0
- package/dist/ui/command-filter.js +92 -0
- package/dist/ui/command-item.js +6 -0
- package/dist/ui/command-palette.js +383 -0
- package/dist/ui/command-registry.js +128 -0
- package/dist/ui/command-renderer.js +55 -0
- package/dist/ui/command-review.js +187 -0
- package/dist/ui/composer-layout.js +74 -0
- package/dist/ui/conversation.js +450 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +141 -0
- package/dist/ui/logo.js +58 -0
- package/dist/ui/markdown.js +205 -0
- package/dist/ui/message.js +21 -0
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +297 -0
- package/dist/ui/select.js +284 -0
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/spinner.js +23 -0
- package/dist/ui/sticky-stream.js +64 -0
- package/dist/ui/task-context-card.js +99 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/theme.js +80 -0
- package/dist/ui/trust.js +88 -0
- package/dist/ui/ui.js +101 -0
- package/package.json +33 -4
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Hand a URL to the OS default browser.
|
|
5
|
+
*
|
|
6
|
+
* Detached + ignored stdio so the browser never becomes a child whose output
|
|
7
|
+
* bleeds into the transcript or whose lifetime is tied to the shell.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Only http(s) may reach the OS opener. A `file:` or `javascript:` URL must not
|
|
11
|
+
* launch anything just because it arrived in an API response. Callers check this
|
|
12
|
+
* BEFORE announcing the open, so a refusal never reads as a half-done action.
|
|
13
|
+
*
|
|
14
|
+
* @returns the normalized URL, or an error message explaining the refusal.
|
|
15
|
+
*/
|
|
16
|
+
export function checkUrl(url) {
|
|
17
|
+
let parsed;
|
|
18
|
+
try {
|
|
19
|
+
parsed = new URL(url);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return { ok: false, reason: `Not a valid URL: ${url}` };
|
|
23
|
+
}
|
|
24
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
25
|
+
return { ok: false, reason: `Refusing to open a ${parsed.protocol} URL.` };
|
|
26
|
+
}
|
|
27
|
+
return { ok: true, url: parsed.toString() };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The program and argv that open `url`, without any shell.
|
|
31
|
+
*
|
|
32
|
+
* Windows used to go through `cmd /c start "" <url>`. cmd.exe parses its whole
|
|
33
|
+
* command line, and `&`, `|` and `^` are legal in a URL query — so a link like
|
|
34
|
+
* `https://example.com/?a=1&calc` from a search result ran `calc`. rundll32
|
|
35
|
+
* hands the URL to the shell's protocol handler as data; nothing parses it as a
|
|
36
|
+
* command. It is named by absolute path under SystemRoot so that no PATH entry
|
|
37
|
+
* or folder can substitute a different `rundll32`.
|
|
38
|
+
*
|
|
39
|
+
* Takes an ALREADY-CHECKED url: callers go through checkUrl first, which only
|
|
40
|
+
* lets http(s) through and percent-encodes spaces and quotes.
|
|
41
|
+
*/
|
|
42
|
+
export function browserLaunchCommand(url, platform = process.platform) {
|
|
43
|
+
if (platform === "win32") {
|
|
44
|
+
const systemRoot = process.env.SystemRoot || process.env.windir || "C:\\Windows";
|
|
45
|
+
return {
|
|
46
|
+
program: path.win32.join(systemRoot, "System32", "rundll32.exe"),
|
|
47
|
+
args: ["url.dll,FileProtocolHandler", url]
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (platform === "darwin")
|
|
51
|
+
return { program: "open", args: [url] };
|
|
52
|
+
return { program: "xdg-open", args: [url] };
|
|
53
|
+
}
|
|
54
|
+
/** Launch detached with no shell and no stdio. Exported for the regression test. */
|
|
55
|
+
export function spawnOpener(program, args) {
|
|
56
|
+
const child = spawn(program, args, { detached: true, stdio: "ignore", windowsHide: true, shell: false });
|
|
57
|
+
child.on("error", () => {
|
|
58
|
+
/* No browser / headless box — the caller already printed the URL. */
|
|
59
|
+
});
|
|
60
|
+
child.unref();
|
|
61
|
+
}
|
|
62
|
+
export function openUrl(url) {
|
|
63
|
+
const checked = checkUrl(url);
|
|
64
|
+
if (!checked.ok)
|
|
65
|
+
throw new Error(checked.reason);
|
|
66
|
+
const { program, args } = browserLaunchCommand(checked.url);
|
|
67
|
+
spawnOpener(program, args);
|
|
68
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { c } from "../ui/colors.js";
|
|
2
|
+
import { screenActive, screenPrint } from "../ui/screen.js";
|
|
3
|
+
import { icon } from "../ui/ui.js";
|
|
4
|
+
/** Status glyphs (consistent icon set — never emojis). */
|
|
5
|
+
export const glyph = {
|
|
6
|
+
ok: icon.ok,
|
|
7
|
+
warn: icon.warn,
|
|
8
|
+
err: icon.err
|
|
9
|
+
};
|
|
10
|
+
export const line = (msg = "") => {
|
|
11
|
+
// In the interactive shell the screen owns the terminal: route transcript
|
|
12
|
+
// output through it (so it lands above the pinned composer, not over it).
|
|
13
|
+
if (screenActive()) {
|
|
14
|
+
screenPrint(msg.length ? msg.split("\n") : [""]);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
process.stdout.write(msg + "\n");
|
|
18
|
+
};
|
|
19
|
+
export const success = (msg) => line(`${icon.ok} ${msg}`);
|
|
20
|
+
export const warn = (msg) => line(`${icon.warn} ${msg}`);
|
|
21
|
+
export const info = (msg) => line(`${icon.info} ${msg}`);
|
|
22
|
+
export const errorLine = (msg) => line(`${icon.err} ${msg}`);
|
|
23
|
+
export const heading = (text) => line("\n" + c.title(text));
|
|
24
|
+
/** Left-aligned label + value, dim label. Used across whoami / usage / doctor. */
|
|
25
|
+
export function kv(label, value, width = 14) {
|
|
26
|
+
// A label wider than the column keeps its own separator — without this an
|
|
27
|
+
// over-long label runs straight into its value with no space between them.
|
|
28
|
+
const pad = label.length >= width ? label + " " : label.padEnd(width);
|
|
29
|
+
line(` ${c.label(pad)}${value}`);
|
|
30
|
+
}
|
|
31
|
+
/** The focused-chat header (used by `cos chat`). */
|
|
32
|
+
export function printBanner(_scope, mode, modelLabel) {
|
|
33
|
+
line();
|
|
34
|
+
line(c.brandFixed.bold(" Carrier OS CLI"));
|
|
35
|
+
line(c.dim(" Ready."));
|
|
36
|
+
line();
|
|
37
|
+
kv("Mode", capitalize(mode));
|
|
38
|
+
kv("Model", modelLabel);
|
|
39
|
+
line();
|
|
40
|
+
line(c.dim(" Type your message, or /help for commands. Ctrl+C to exit."));
|
|
41
|
+
line();
|
|
42
|
+
}
|
|
43
|
+
export function capitalize(s) {
|
|
44
|
+
return s ? s.charAt(0).toUpperCase() + s.slice(1) : s;
|
|
45
|
+
}
|
|
46
|
+
/** Compact, dependency-free relative-time formatter for lists. */
|
|
47
|
+
export function timeAgo(iso) {
|
|
48
|
+
const then = new Date(iso).getTime();
|
|
49
|
+
if (Number.isNaN(then))
|
|
50
|
+
return "—";
|
|
51
|
+
const secs = Math.max(0, Math.floor((Date.now() - then) / 1000));
|
|
52
|
+
if (secs < 60)
|
|
53
|
+
return `${secs}s ago`;
|
|
54
|
+
const mins = Math.floor(secs / 60);
|
|
55
|
+
if (mins < 60)
|
|
56
|
+
return `${mins}m ago`;
|
|
57
|
+
const hrs = Math.floor(mins / 60);
|
|
58
|
+
if (hrs < 24)
|
|
59
|
+
return `${hrs}h ago`;
|
|
60
|
+
const days = Math.floor(hrs / 24);
|
|
61
|
+
return `${days}d ago`;
|
|
62
|
+
}
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { cliVersion } from "./meta.js";
|
|
7
|
+
import { RELEASE_MANIFEST_URL } from "./packaged.js";
|
|
8
|
+
import { compareVersions, isVersionLike } from "./update.js";
|
|
9
|
+
/**
|
|
10
|
+
* Self-update for the packaged Windows build (cos.exe installed by the Inno
|
|
11
|
+
* Setup installer). The npm path in update.ts is untouched.
|
|
12
|
+
*
|
|
13
|
+
* The trust chain, in order — each step refuses rather than guesses:
|
|
14
|
+
*
|
|
15
|
+
* 1. The release manifest comes from RELEASE_MANIFEST_URL over HTTPS. Its
|
|
16
|
+
* artifact URL must be on the SAME origin, so a manifest can never point
|
|
17
|
+
* an update at another host.
|
|
18
|
+
* 2. The version must be well-formed and newer than this one (no downgrade).
|
|
19
|
+
* 3. The installer is streamed to `<name>.partial` under the user's updates
|
|
20
|
+
* directory, hashed as it arrives, and capped at the manifest's size. It is
|
|
21
|
+
* renamed to its final name only when size AND sha256 match — so an
|
|
22
|
+
* interrupted or corrupted download never leaves a runnable file.
|
|
23
|
+
* 4. A signed release build additionally requires a valid Authenticode
|
|
24
|
+
* signature whose signer matches the running cos.exe's own signer, and it
|
|
25
|
+
* refuses an installer the manifest marks unsigned.
|
|
26
|
+
* 5. Replacement is done by the installer, never by this process: it is
|
|
27
|
+
* started detached with this PID, waits for cos.exe to exit, then
|
|
28
|
+
* installs over the existing copy. Inno Setup rolls back an install that
|
|
29
|
+
* fails part-way, so a failed update leaves the old version in place.
|
|
30
|
+
*
|
|
31
|
+
* Nothing here deletes or rewrites configuration: config and credentials live
|
|
32
|
+
* in %APPDATA%\CarrierOS, outside the install directory the installer owns.
|
|
33
|
+
*/
|
|
34
|
+
export const MAX_MANIFEST_BYTES = 64 * 1024;
|
|
35
|
+
export const MAX_INSTALLER_BYTES = 512 * 1024 * 1024;
|
|
36
|
+
const MANIFEST_TIMEOUT_MS = 15_000;
|
|
37
|
+
const DOWNLOAD_IDLE_TIMEOUT_MS = 60_000;
|
|
38
|
+
export class UpdateError extends Error {
|
|
39
|
+
code;
|
|
40
|
+
constructor(code, message) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.code = code;
|
|
43
|
+
this.name = "UpdateError";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const LOOPBACK = new Set(["127.0.0.1", "localhost", "[::1]", "::1"]);
|
|
47
|
+
/**
|
|
48
|
+
* The manifest URL: the baked HTTPS constant, or COS_UPDATE_MANIFEST_URL for
|
|
49
|
+
* a staging channel or the acceptance harness. An override must itself be
|
|
50
|
+
* HTTPS, or plain HTTP on loopback only (a local test server).
|
|
51
|
+
*/
|
|
52
|
+
export function manifestUrl(env = process.env) {
|
|
53
|
+
const raw = env.COS_UPDATE_MANIFEST_URL?.trim();
|
|
54
|
+
if (!raw)
|
|
55
|
+
return RELEASE_MANIFEST_URL;
|
|
56
|
+
let u;
|
|
57
|
+
try {
|
|
58
|
+
u = new URL(raw);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
throw new UpdateError("BAD_URL", "COS_UPDATE_MANIFEST_URL is not a valid URL.");
|
|
62
|
+
}
|
|
63
|
+
if (u.protocol === "https:" || (u.protocol === "http:" && LOOPBACK.has(u.hostname)))
|
|
64
|
+
return u.toString();
|
|
65
|
+
throw new UpdateError("BAD_URL", "COS_UPDATE_MANIFEST_URL must be https:// (or http:// on localhost).");
|
|
66
|
+
}
|
|
67
|
+
/** Deterministic artifact names; the manifest must agree with them. */
|
|
68
|
+
export function artifactFileName(key, version, signed) {
|
|
69
|
+
const suffix = signed ? "" : "-UNSIGNED";
|
|
70
|
+
return key === "win-x64-setup"
|
|
71
|
+
? `CarrierOS-Setup-${version}-x64${suffix}.exe`
|
|
72
|
+
: `cos-${version}-win-x64${suffix}.zip`;
|
|
73
|
+
}
|
|
74
|
+
function isObject(v) {
|
|
75
|
+
return !!v && typeof v === "object" && !Array.isArray(v);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Validate a release manifest fetched from `fromUrl`. Throws BAD_MANIFEST with
|
|
79
|
+
* the first problem found; returns a manifest whose every field was checked.
|
|
80
|
+
*/
|
|
81
|
+
export function parseReleaseManifest(raw, fromUrl) {
|
|
82
|
+
const bad = (why) => {
|
|
83
|
+
throw new UpdateError("BAD_MANIFEST", `The release manifest is invalid: ${why}.`);
|
|
84
|
+
};
|
|
85
|
+
if (!isObject(raw))
|
|
86
|
+
return bad("not an object");
|
|
87
|
+
if (raw.schema !== 1)
|
|
88
|
+
bad("unsupported schema");
|
|
89
|
+
if (raw.product !== "carrieros-cli")
|
|
90
|
+
bad("wrong product");
|
|
91
|
+
const version = typeof raw.version === "string" ? raw.version.trim() : "";
|
|
92
|
+
if (!isVersionLike(version))
|
|
93
|
+
bad("malformed version");
|
|
94
|
+
if (!isObject(raw.artifacts))
|
|
95
|
+
return bad("no artifacts");
|
|
96
|
+
const origin = new URL(fromUrl).origin;
|
|
97
|
+
const artifacts = {};
|
|
98
|
+
for (const key of ["win-x64-setup", "win-x64-zip"]) {
|
|
99
|
+
const a = raw.artifacts[key];
|
|
100
|
+
if (a === undefined)
|
|
101
|
+
continue;
|
|
102
|
+
if (!isObject(a))
|
|
103
|
+
bad(`${key} is not an object`);
|
|
104
|
+
const art = a;
|
|
105
|
+
const signed = art.signed === true;
|
|
106
|
+
if (art.file !== artifactFileName(key, version, signed))
|
|
107
|
+
bad(`${key} file name does not match its version`);
|
|
108
|
+
if (typeof art.sha256 !== "string" || !/^[0-9a-f]{64}$/.test(art.sha256))
|
|
109
|
+
bad(`${key} sha256 is malformed`);
|
|
110
|
+
if (typeof art.size !== "number" || !Number.isInteger(art.size) || art.size <= 0 || art.size > MAX_INSTALLER_BYTES) {
|
|
111
|
+
bad(`${key} size is out of range`);
|
|
112
|
+
}
|
|
113
|
+
let url;
|
|
114
|
+
try {
|
|
115
|
+
url = new URL(String(art.url), fromUrl);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return bad(`${key} url is malformed`);
|
|
119
|
+
}
|
|
120
|
+
if (url.origin !== origin)
|
|
121
|
+
bad(`${key} is not hosted on the manifest's own site`);
|
|
122
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && LOOPBACK.has(url.hostname)))
|
|
123
|
+
bad(`${key} is not HTTPS`);
|
|
124
|
+
artifacts[key] = {
|
|
125
|
+
file: art.file,
|
|
126
|
+
url: url.toString(),
|
|
127
|
+
sha256: art.sha256,
|
|
128
|
+
size: art.size,
|
|
129
|
+
signed
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (!artifacts["win-x64-setup"])
|
|
133
|
+
bad("no Windows installer");
|
|
134
|
+
return {
|
|
135
|
+
schema: 1,
|
|
136
|
+
product: "carrieros-cli",
|
|
137
|
+
version,
|
|
138
|
+
released: typeof raw.released === "string" ? raw.released : undefined,
|
|
139
|
+
notes: typeof raw.notes === "string" ? raw.notes.slice(0, 2000) : undefined,
|
|
140
|
+
artifacts
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
async function readCapped(res, cap) {
|
|
144
|
+
const declared = Number(res.headers.get("content-length") || 0);
|
|
145
|
+
if (declared > cap)
|
|
146
|
+
throw new UpdateError("BAD_MANIFEST", "The release manifest is too large.");
|
|
147
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
148
|
+
if (buf.length > cap)
|
|
149
|
+
throw new UpdateError("BAD_MANIFEST", "The release manifest is too large.");
|
|
150
|
+
return buf;
|
|
151
|
+
}
|
|
152
|
+
export async function fetchReleaseManifest(url = manifestUrl(), fetchImpl = fetch) {
|
|
153
|
+
let res;
|
|
154
|
+
try {
|
|
155
|
+
res = await fetchImpl(url, {
|
|
156
|
+
redirect: "error",
|
|
157
|
+
signal: AbortSignal.timeout(MANIFEST_TIMEOUT_MS),
|
|
158
|
+
headers: { accept: "application/json", "cache-control": "no-cache" }
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
throw new UpdateError("UNREACHABLE", `Could not reach the update server (${err.message}).`);
|
|
163
|
+
}
|
|
164
|
+
if (!res.ok)
|
|
165
|
+
throw new UpdateError("UNREACHABLE", `The update server answered HTTP ${res.status}.`);
|
|
166
|
+
const body = await readCapped(res, MAX_MANIFEST_BYTES);
|
|
167
|
+
let json;
|
|
168
|
+
try {
|
|
169
|
+
json = JSON.parse(body.toString("utf8"));
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
throw new UpdateError("BAD_MANIFEST", "The release manifest is not valid JSON.");
|
|
173
|
+
}
|
|
174
|
+
return parseReleaseManifest(json, url);
|
|
175
|
+
}
|
|
176
|
+
export async function checkPackagedUpdate(current = cliVersion(), fetchImpl = fetch, env = process.env) {
|
|
177
|
+
let manifest;
|
|
178
|
+
try {
|
|
179
|
+
manifest = await fetchReleaseManifest(manifestUrl(env), fetchImpl);
|
|
180
|
+
}
|
|
181
|
+
catch (err) {
|
|
182
|
+
return { kind: "unreachable", current, detail: err.message };
|
|
183
|
+
}
|
|
184
|
+
const cmp = compareVersions(current, manifest.version);
|
|
185
|
+
if (cmp === 0)
|
|
186
|
+
return { kind: "current", current, latest: manifest.version };
|
|
187
|
+
if (cmp > 0)
|
|
188
|
+
return { kind: "ahead", current, latest: manifest.version };
|
|
189
|
+
return { kind: "available", current, latest: manifest.version, manifest };
|
|
190
|
+
}
|
|
191
|
+
/** Per-user download directory; never the install directory. */
|
|
192
|
+
export function updatesDir(env = process.env) {
|
|
193
|
+
return env.LOCALAPPDATA
|
|
194
|
+
? path.join(env.LOCALAPPDATA, "CarrierOS", "updates")
|
|
195
|
+
: path.join(os.homedir(), ".carrieros", "updates");
|
|
196
|
+
}
|
|
197
|
+
export function sha256OfFile(file) {
|
|
198
|
+
return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
|
199
|
+
}
|
|
200
|
+
/** Remove leftovers from interrupted or superseded downloads. */
|
|
201
|
+
export function cleanupDownloads(dir, keep) {
|
|
202
|
+
let names;
|
|
203
|
+
try {
|
|
204
|
+
names = fs.readdirSync(dir);
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
for (const name of names) {
|
|
210
|
+
if (name === keep)
|
|
211
|
+
continue;
|
|
212
|
+
if (/^CarrierOS-Setup-.*\.exe(\.partial)?$/.test(name)) {
|
|
213
|
+
try {
|
|
214
|
+
fs.rmSync(path.join(dir, name), { force: true });
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
/* in use or already gone */
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Download `artifact` into `dir` and verify it. Returns the path of a file
|
|
224
|
+
* whose size and sha256 match the manifest; anything else throws and leaves no
|
|
225
|
+
* file behind under the final name.
|
|
226
|
+
*/
|
|
227
|
+
export async function downloadVerified(artifact, dir, opts = {}) {
|
|
228
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
229
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
230
|
+
const final = path.join(dir, artifact.file);
|
|
231
|
+
cleanupDownloads(dir, artifact.file);
|
|
232
|
+
// A complete, verified copy from an earlier attempt is reused.
|
|
233
|
+
try {
|
|
234
|
+
if (fs.statSync(final).size === artifact.size && sha256OfFile(final) === artifact.sha256)
|
|
235
|
+
return final;
|
|
236
|
+
fs.rmSync(final, { force: true });
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
/* not there */
|
|
240
|
+
}
|
|
241
|
+
const partial = `${final}.partial`;
|
|
242
|
+
const idle = new AbortController();
|
|
243
|
+
let idleTimer;
|
|
244
|
+
const touch = () => {
|
|
245
|
+
if (idleTimer)
|
|
246
|
+
clearTimeout(idleTimer);
|
|
247
|
+
idleTimer = setTimeout(() => idle.abort(), DOWNLOAD_IDLE_TIMEOUT_MS);
|
|
248
|
+
};
|
|
249
|
+
const signal = opts.signal ? AbortSignal.any([opts.signal, idle.signal]) : idle.signal;
|
|
250
|
+
const hash = crypto.createHash("sha256");
|
|
251
|
+
let received = 0;
|
|
252
|
+
let fd = null;
|
|
253
|
+
try {
|
|
254
|
+
touch();
|
|
255
|
+
let res;
|
|
256
|
+
try {
|
|
257
|
+
res = await fetchImpl(artifact.url, { redirect: "error", signal });
|
|
258
|
+
}
|
|
259
|
+
catch (err) {
|
|
260
|
+
throw opts.signal?.aborted
|
|
261
|
+
? new UpdateError("CANCELLED", "Update cancelled.")
|
|
262
|
+
: new UpdateError("DOWNLOAD_FAILED", `Download failed (${err.message}).`);
|
|
263
|
+
}
|
|
264
|
+
if (!res.ok || !res.body)
|
|
265
|
+
throw new UpdateError("DOWNLOAD_FAILED", `Download failed (HTTP ${res.status}).`);
|
|
266
|
+
fd = fs.openSync(partial, "w", 0o600);
|
|
267
|
+
const reader = res.body.getReader();
|
|
268
|
+
for (;;) {
|
|
269
|
+
let chunk;
|
|
270
|
+
try {
|
|
271
|
+
chunk = await reader.read();
|
|
272
|
+
}
|
|
273
|
+
catch (err) {
|
|
274
|
+
throw opts.signal?.aborted
|
|
275
|
+
? new UpdateError("CANCELLED", "Update cancelled.")
|
|
276
|
+
: new UpdateError("DOWNLOAD_FAILED", `Download interrupted (${err.message}).`);
|
|
277
|
+
}
|
|
278
|
+
if (chunk.done || !chunk.value)
|
|
279
|
+
break;
|
|
280
|
+
touch();
|
|
281
|
+
received += chunk.value.length;
|
|
282
|
+
if (received > artifact.size) {
|
|
283
|
+
await reader.cancel().catch(() => undefined);
|
|
284
|
+
throw new UpdateError("SIZE_MISMATCH", "The download is larger than the release manifest declares.");
|
|
285
|
+
}
|
|
286
|
+
hash.update(chunk.value);
|
|
287
|
+
fs.writeSync(fd, chunk.value);
|
|
288
|
+
opts.onProgress?.(received, artifact.size);
|
|
289
|
+
}
|
|
290
|
+
fs.fsyncSync(fd);
|
|
291
|
+
fs.closeSync(fd);
|
|
292
|
+
fd = null;
|
|
293
|
+
if (received !== artifact.size) {
|
|
294
|
+
throw new UpdateError("SIZE_MISMATCH", "The download is incomplete.");
|
|
295
|
+
}
|
|
296
|
+
if (hash.digest("hex") !== artifact.sha256) {
|
|
297
|
+
throw new UpdateError("CHECKSUM_MISMATCH", "The download does not match the published checksum.");
|
|
298
|
+
}
|
|
299
|
+
fs.renameSync(partial, final);
|
|
300
|
+
return final;
|
|
301
|
+
}
|
|
302
|
+
finally {
|
|
303
|
+
if (idleTimer)
|
|
304
|
+
clearTimeout(idleTimer);
|
|
305
|
+
if (fd !== null) {
|
|
306
|
+
try {
|
|
307
|
+
fs.closeSync(fd);
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
/* already closed */
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
fs.rmSync(partial, { force: true });
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const POWERSHELL = () => path.join(process.env.SystemRoot || "C:\\Windows", "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
317
|
+
/**
|
|
318
|
+
* Authenticode status of a file, via PowerShell's Get-AuthenticodeSignature.
|
|
319
|
+
* The path travels in an environment variable, never in the command text.
|
|
320
|
+
*/
|
|
321
|
+
export function authenticodeSignature(file) {
|
|
322
|
+
if (process.platform !== "win32")
|
|
323
|
+
return { status: "UnsupportedPlatform", subject: null };
|
|
324
|
+
const script = "$s = Get-AuthenticodeSignature -LiteralPath $env:COS_SIGNED_FILE; " +
|
|
325
|
+
"[Console]::Out.Write(($s.Status.ToString()) + \"`n\" + ($(if ($s.SignerCertificate) { $s.SignerCertificate.Subject } else { '' })))";
|
|
326
|
+
const res = spawnSync(POWERSHELL(), ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", script], {
|
|
327
|
+
env: { ...process.env, COS_SIGNED_FILE: file },
|
|
328
|
+
encoding: "utf8",
|
|
329
|
+
windowsHide: true,
|
|
330
|
+
timeout: 30_000
|
|
331
|
+
});
|
|
332
|
+
if (res.status !== 0)
|
|
333
|
+
return { status: "VerificationFailed", subject: null };
|
|
334
|
+
const [status = "Unknown", subject = ""] = String(res.stdout || "").split("\n");
|
|
335
|
+
return { status: status.trim(), subject: subject.trim() || null };
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* The signature gate. A signed build requires a valid signature from the same
|
|
339
|
+
* publisher as itself; an unsigned build (development, pre-certificate)
|
|
340
|
+
* accepts what the checksum already verified, and says so.
|
|
341
|
+
*/
|
|
342
|
+
export function checkInstallerSignature(installer, artifact, signedBuild, inspect = authenticodeSignature, self = process.execPath) {
|
|
343
|
+
if (!signedBuild)
|
|
344
|
+
return { verified: false };
|
|
345
|
+
if (!artifact.signed) {
|
|
346
|
+
throw new UpdateError("UNSIGNED", "This update is not signed, and a signed installation only accepts signed updates.");
|
|
347
|
+
}
|
|
348
|
+
const got = inspect(installer);
|
|
349
|
+
const mine = inspect(self);
|
|
350
|
+
if (got.status !== "Valid" || !got.subject) {
|
|
351
|
+
throw new UpdateError("SIGNATURE_INVALID", `The update's signature is not valid (${got.status}).`);
|
|
352
|
+
}
|
|
353
|
+
if (!mine.subject || got.subject !== mine.subject) {
|
|
354
|
+
throw new UpdateError("SIGNATURE_INVALID", "The update is signed by a different publisher than this installation.");
|
|
355
|
+
}
|
|
356
|
+
return { verified: true };
|
|
357
|
+
}
|
|
358
|
+
/** Arguments for a silent, in-place upgrade that waits for this process. */
|
|
359
|
+
export function installerArgs(pid, logFile) {
|
|
360
|
+
return ["/VERYSILENT", "/SUPPRESSMSGBOXES", "/NORESTART", "/SP-", `/WAITPID=${pid}`, `/LOG=${logFile}`];
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Start the verified installer detached. It waits for `pid` to exit (the
|
|
364
|
+
* installer's [Code] section) before touching cos.exe, so the caller must exit
|
|
365
|
+
* promptly after this resolves. Resolution confirms process creation, not
|
|
366
|
+
* completion of the installation.
|
|
367
|
+
*/
|
|
368
|
+
export async function launchInstaller(installer, pid = process.pid) {
|
|
369
|
+
const logFile = path.join(path.dirname(installer), `install-${Date.now()}.log`);
|
|
370
|
+
try {
|
|
371
|
+
await new Promise((resolve, reject) => {
|
|
372
|
+
const child = spawn(installer, installerArgs(pid, logFile), {
|
|
373
|
+
detached: true,
|
|
374
|
+
stdio: "ignore",
|
|
375
|
+
windowsHide: true
|
|
376
|
+
});
|
|
377
|
+
child.once("error", reject);
|
|
378
|
+
child.once("spawn", () => {
|
|
379
|
+
child.unref();
|
|
380
|
+
resolve();
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
catch (err) {
|
|
385
|
+
throw new UpdateError("LAUNCH_FAILED", `The installer could not be started (${err.message}).`);
|
|
386
|
+
}
|
|
387
|
+
return logFile;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* The packaged twin of refreshUpdateCache: same cache keys, so the banner's
|
|
391
|
+
* "Update available" notice works unchanged. Asynchronous (fetch), so unlike
|
|
392
|
+
* the npm check it never blocks the shell. Failures are recorded, never shown.
|
|
393
|
+
*/
|
|
394
|
+
export async function refreshPackagedUpdateCache(cache, now = Date.now()) {
|
|
395
|
+
try {
|
|
396
|
+
const manifest = await fetchReleaseManifest();
|
|
397
|
+
cache.set("latestCliVersion", manifest.version);
|
|
398
|
+
cache.set("updateCheckAt", now);
|
|
399
|
+
}
|
|
400
|
+
catch {
|
|
401
|
+
cache.set("updateCheckErrorAt", now);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Am I running as the packaged Windows executable, or from a Node/npm checkout?
|
|
3
|
+
*
|
|
4
|
+
* A Single Executable Application reports this through `node:sea`. Several
|
|
5
|
+
* behaviours differ between the two: a packaged build has no npm to self-update
|
|
6
|
+
* through, no package.json on disk to read a version from, and ships its native
|
|
7
|
+
* helpers prebuilt beside the .exe. Each of those checks this rather than
|
|
8
|
+
* guessing from paths.
|
|
9
|
+
*/
|
|
10
|
+
import { createRequire } from "node:module";
|
|
11
|
+
export function isPackaged() {
|
|
12
|
+
try {
|
|
13
|
+
// `node:sea` is a Node 20+ builtin; resolving it against the executable
|
|
14
|
+
// path works in both the ESM dev build and the bundled CJS executable.
|
|
15
|
+
const require = createRequire(process.execPath);
|
|
16
|
+
const sea = require("node:sea");
|
|
17
|
+
return typeof sea?.isSea === "function" ? sea.isSea() : false;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** Where to point someone at a new packaged build (no npm involved). */
|
|
24
|
+
export const PACKAGED_DOWNLOAD_URL = "https://carrieros.ai/cli";
|
|
25
|
+
/**
|
|
26
|
+
* The release manifest the packaged updater reads. HTTPS on the Carrier OS
|
|
27
|
+
* site: the download page and the update channel are one place, so an
|
|
28
|
+
* installer a person downloads by hand and one `cos update` fetches are the
|
|
29
|
+
* same artifact with the same checksum.
|
|
30
|
+
*/
|
|
31
|
+
export const RELEASE_MANIFEST_URL = "https://carrieros.ai/cli/release.json";
|
|
32
|
+
/**
|
|
33
|
+
* True for a signed release build. A signed build only ever updates to a
|
|
34
|
+
* signed installer — an update must never quietly downgrade the trust a user
|
|
35
|
+
* installed with.
|
|
36
|
+
*/
|
|
37
|
+
export function isSignedRelease() {
|
|
38
|
+
return typeof __RELEASE_SIGNED__ === "boolean" && __RELEASE_SIGNED__;
|
|
39
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export const PERMISSION_MODES = ["manual", "plan", "accept_edits", "auto"];
|
|
2
|
+
const LABELS = { manual: "Manual", plan: "Plan", accept_edits: "Accept Edits", auto: "Auto" };
|
|
3
|
+
const EXPLAIN = {
|
|
4
|
+
manual: "Every file change and every command asks for your approval.",
|
|
5
|
+
plan: "Investigate and plan only. No file changes or commands are available.",
|
|
6
|
+
accept_edits: "File changes can be applied without individual approval. Commands still require approval.",
|
|
7
|
+
auto: "File changes and commands admitted by local policy run without individual prompts. Trust, command policy and runtime checks still apply."
|
|
8
|
+
};
|
|
9
|
+
/** Sessions whose mode is remembered for resume; older entries are dropped. */
|
|
10
|
+
const REMEMBERED = 50;
|
|
11
|
+
export const permissionModeLabel = (m) => LABELS[m];
|
|
12
|
+
export const permissionModeExplanation = (m) => EXPLAIN[m];
|
|
13
|
+
/** Explain the local file-change gates truthfully in the model context. */
|
|
14
|
+
export function localEditPolicyHint(mode, workspaceTrusted, fileChangesEnabled) {
|
|
15
|
+
if (mode === "plan")
|
|
16
|
+
return null;
|
|
17
|
+
if (!workspaceTrusted) {
|
|
18
|
+
return `[LOCAL EDIT STATUS] The user selected ${permissionModeLabel(mode)}, but this VS Code workspace is in Restricted Mode. Do not mention internal tools or session capabilities. Explain simply that VS Code must trust this workspace before Carrier OS can make file changes, and ask the user to trust it using VS Code's Workspace Trust controls, then retry. Do not imply that the selected permission mode overrides workspace trust.`;
|
|
19
|
+
}
|
|
20
|
+
if (!fileChangesEnabled) {
|
|
21
|
+
return `[LOCAL EDIT STATUS] The user selected ${permissionModeLabel(mode)}, but file changes are disabled in Carrier OS settings. Do not mention internal tools or session capabilities. Explain simply that file changes are turned off, and tell the user to open VS Code Settings (Ctrl/Cmd+,), search for "Carrier OS: Enable File Changes", enable it, and retry. Do not imply that the selected permission mode overrides this setting.`;
|
|
22
|
+
}
|
|
23
|
+
if (mode === "auto") {
|
|
24
|
+
return "[LOCAL EDIT POLICY] The user selected Auto. For requested code changes, inspect the relevant files and submit the concrete edit through the available file-change workflow; do not answer with patch instructions alone. The local coordinator applies admitted changes under Auto policy without an individual diff prompt, while workspace trust, freshness, containment, checkpoints, recovery and cancellation checks still apply.";
|
|
25
|
+
}
|
|
26
|
+
if (mode === "accept_edits") {
|
|
27
|
+
return "[LOCAL EDIT POLICY] The user selected Accept Edits. For requested code changes, inspect the relevant files and submit the concrete edit through the available file-change workflow; do not answer with patch instructions alone. File edits apply under this mode without an individual prompt; commands still use their separate approval and execution gates.";
|
|
28
|
+
}
|
|
29
|
+
return "[LOCAL EDIT POLICY] The user selected Manual. For requested code changes, inspect the relevant files and submit the concrete edit through the available file-change workflow; it opens the required diff review. Do not replace the review with a prose patch or prose approval question.";
|
|
30
|
+
}
|
|
31
|
+
/** "accept-edits", "Accept Edits", "accept_edits", "acceptedits" → accept_edits; unknown → undefined. */
|
|
32
|
+
export function parsePermissionMode(value) {
|
|
33
|
+
const v = typeof value === "string" ? value.trim().toLowerCase().replace(/[\s-]+/g, "_") : "";
|
|
34
|
+
if (v === "acceptedits")
|
|
35
|
+
return "accept_edits";
|
|
36
|
+
return PERMISSION_MODES.includes(v) ? v : undefined;
|
|
37
|
+
}
|
|
38
|
+
/** The current mode; anything unreadable is MANUAL (fail closed). */
|
|
39
|
+
export function getPermissionMode(config) {
|
|
40
|
+
return parsePermissionMode(config.get("permissionMode")) ?? "manual";
|
|
41
|
+
}
|
|
42
|
+
/** Change the mode for this session (and remember it for resuming the active conversation). */
|
|
43
|
+
export function setPermissionMode(config, mode) {
|
|
44
|
+
config.set("permissionMode", mode);
|
|
45
|
+
rememberPermissionMode(config);
|
|
46
|
+
}
|
|
47
|
+
/** Record the current mode against the active conversation — called when a turn learns its session id. */
|
|
48
|
+
export function rememberPermissionMode(config) {
|
|
49
|
+
const sessionId = config.get("sessionId");
|
|
50
|
+
if (!sessionId)
|
|
51
|
+
return;
|
|
52
|
+
const saved = { ...(config.get("permissionModes") ?? {}) };
|
|
53
|
+
delete saved[sessionId];
|
|
54
|
+
saved[sessionId] = getPermissionMode(config);
|
|
55
|
+
const keys = Object.keys(saved);
|
|
56
|
+
for (const k of keys.slice(0, Math.max(0, keys.length - REMEMBERED)))
|
|
57
|
+
delete saved[k];
|
|
58
|
+
config.set("permissionModes", saved);
|
|
59
|
+
}
|
|
60
|
+
/** A new task: back to MANUAL. */
|
|
61
|
+
export function resetPermissionMode(config) {
|
|
62
|
+
config.set("permissionMode", "manual");
|
|
63
|
+
}
|
|
64
|
+
/** Resuming a conversation: the mode it was last used with, or MANUAL. */
|
|
65
|
+
export function restorePermissionMode(config, sessionId) {
|
|
66
|
+
const mode = parsePermissionMode((config.get("permissionModes") ?? {})[sessionId]) ?? "manual";
|
|
67
|
+
config.set("permissionMode", mode);
|
|
68
|
+
return mode;
|
|
69
|
+
}
|
|
70
|
+
/** What a mode allows, as the turn's tool policy. */
|
|
71
|
+
export function permissionPolicy(mode) {
|
|
72
|
+
return {
|
|
73
|
+
changeTools: mode !== "plan",
|
|
74
|
+
commands: mode !== "plan",
|
|
75
|
+
approveEditsByPolicy: mode === "accept_edits" || mode === "auto",
|
|
76
|
+
approveCommandByPolicy: () => mode === "auto"
|
|
77
|
+
};
|
|
78
|
+
}
|