@carrieros/cli 0.3.0 → 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 +89 -15
- package/dist/commands/chat.js +6 -6
- package/dist/commands/context.js +40 -0
- package/dist/commands/doctor.js +34 -14
- package/dist/commands/logout.js +2 -0
- package/dist/commands/permissions.js +78 -0
- package/dist/commands/scope.js +9 -6
- package/dist/commands/sessions.js +5 -0
- package/dist/commands/settings.js +83 -10
- package/dist/commands/update.js +177 -51
- package/dist/commands/usage.js +10 -8
- package/dist/commands/version.js +1 -3
- package/dist/commands/whoami.js +12 -9
- package/dist/ide/editor-context.js +115 -12
- package/dist/ide/ide-session.js +288 -18
- package/dist/ide/tool-host.js +6 -6
- package/dist/main.js +10 -1
- package/dist/services/api.js +183 -15
- package/dist/services/auth-origin.js +20 -0
- package/dist/services/auto-update.js +221 -0
- package/dist/services/chat-stream.js +26 -3
- package/dist/services/checkpoints.js +176 -14
- package/dist/services/client-tools.js +123 -27
- 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 +85 -12
- package/dist/services/command-policy.js +34 -4
- package/dist/services/config.js +96 -11
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +98 -25
- package/dist/services/device-auth.js +33 -6
- package/dist/services/execution-platform.js +30 -0
- package/dist/services/file-resolver.js +1 -1
- package/dist/services/folder-key.js +22 -0
- package/dist/services/git-tools.js +181 -0
- package/dist/services/helper-bin.js +56 -35
- package/dist/services/instruction-resolver.js +54 -0
- package/dist/services/job-helper.js +31 -10
- package/dist/services/local-files.js +148 -26
- package/dist/services/local-tools.js +540 -35
- package/dist/services/login-flow.js +44 -13
- package/dist/services/meta.js +27 -0
- package/dist/services/mutation-coordinator.js +186 -20
- package/dist/services/mutation-groups.js +309 -0
- package/dist/services/mutation-recovery.js +159 -0
- package/dist/services/mutations.js +264 -78
- package/dist/services/packaged-update.js +403 -0
- package/dist/services/packaged.js +15 -0
- package/dist/services/permission-mode.js +78 -0
- package/dist/services/process-runner.js +7 -1
- package/dist/services/project-context.js +4 -4
- package/dist/services/result-reconciliation.js +49 -0
- package/dist/services/secure-store.js +21 -1
- package/dist/services/session-resolver.js +7 -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 +457 -13
- package/dist/services/workspace-registry.js +4 -3
- package/dist/services/workspace.js +8 -1
- package/dist/shell/shell-chat.js +320 -34
- package/dist/shell/shell-commands.js +47 -5
- package/dist/shell/shell-prompt.js +21 -11
- package/dist/shell/shell-renderer.js +1 -1
- package/dist/shell/shell.js +107 -20
- package/dist/shell/task-context-view.js +40 -0
- package/dist/ui/agent-cards.js +111 -0
- package/dist/ui/agent-presentation.js +131 -0
- package/dist/ui/approval.js +46 -16
- package/dist/ui/banner.js +31 -87
- package/dist/ui/change-review.js +293 -46
- package/dist/ui/chat-renderer.js +6 -6
- package/dist/ui/colors.js +9 -9
- package/dist/ui/command-details.js +11 -0
- package/dist/ui/command-palette.js +27 -10
- package/dist/ui/command-registry.js +11 -4
- package/dist/ui/command-review.js +77 -28
- package/dist/ui/composer-layout.js +26 -20
- package/dist/ui/conversation.js +77 -25
- package/dist/ui/live.js +1 -1
- package/dist/ui/screen.js +19 -11
- package/dist/ui/select.js +6 -0
- package/dist/ui/task-context-card.js +99 -0
- package/dist/ui/trust.js +7 -12
- package/dist/ui/ui.js +23 -0
- package/package.json +9 -5
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function executionContract(value = process.platform) {
|
|
2
|
+
const windows = value === "win32";
|
|
3
|
+
const isolated = value === "darwin" || value === "linux";
|
|
4
|
+
const supported = windows || isolated;
|
|
5
|
+
return {
|
|
6
|
+
capability: windows ? "NATIVE_SECURE" : isolated ? "ISOLATED_RUNTIME_REQUIRED" : "UNSUPPORTED",
|
|
7
|
+
backend: windows ? "windows-job" : isolated ? "docker-engine-v1" : null,
|
|
8
|
+
guarantees: {
|
|
9
|
+
descendantTracking: windows ? "job-membership" : isolated ? "pid-namespace" : "unavailable",
|
|
10
|
+
descendantTermination: supported, orphanCleanup: supported, timeout: supported,
|
|
11
|
+
cancellation: supported, workingDirectoryContainment: supported,
|
|
12
|
+
environment: windows ? "filtered-host" : isolated ? "isolated-guest" : "unavailable",
|
|
13
|
+
commandRestrictions: supported
|
|
14
|
+
},
|
|
15
|
+
prerequisite: windows ? "Verified Windows Job Object helper; no breakaway. Native OS acceptance must be verified separately."
|
|
16
|
+
: isolated ? "Secure command execution requires the approved Docker Engine runtime because native process lifecycle guarantees are insufficient."
|
|
17
|
+
: `Secure command execution is unsupported on ${value}.`
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function executionPlatform(value = process.platform) {
|
|
21
|
+
if (executionContract(value).capability !== "UNSUPPORTED")
|
|
22
|
+
return value;
|
|
23
|
+
throw Object.assign(new Error(executionContract(value).prerequisite), { code: "UNSUPPORTED_PLATFORM" });
|
|
24
|
+
}
|
|
25
|
+
export function executionAdapter(platform) {
|
|
26
|
+
const contract = executionContract(platform);
|
|
27
|
+
if (contract.capability === "UNSUPPORTED")
|
|
28
|
+
executionPlatform(platform);
|
|
29
|
+
return contract.backend === "windows-job" ? "windows-job" : "isolated-container";
|
|
30
|
+
}
|
|
@@ -34,7 +34,7 @@ function tryExact(repo, rel, kind) {
|
|
|
34
34
|
return null;
|
|
35
35
|
if (kind === "directory" && !isDir)
|
|
36
36
|
return null;
|
|
37
|
-
const relOut = posix(path.relative(repo.root, abs));
|
|
37
|
+
const relOut = posix(path.relative(fs.realpathSync.native(repo.root), abs));
|
|
38
38
|
if (!relOut || relOut.startsWith(".."))
|
|
39
39
|
return null;
|
|
40
40
|
if (isPathIgnored(relOut, repo.root))
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* A folder's canonical identity for trust decisions: its real path (symlinks
|
|
5
|
+
* resolved — macOS /var is /private/var), case-folded on Windows. Trust is about
|
|
6
|
+
* a folder on disk, so two spellings of the same folder must agree and two
|
|
7
|
+
* different folders can never share a key. A path not on disk (yet) keeps its
|
|
8
|
+
* resolved spelling. The ONE comparison every trust check uses.
|
|
9
|
+
*/
|
|
10
|
+
export function folderKey(dir) {
|
|
11
|
+
let resolved = path.resolve(dir);
|
|
12
|
+
try {
|
|
13
|
+
resolved = fs.realpathSync.native(resolved);
|
|
14
|
+
}
|
|
15
|
+
catch { /* not on disk: keep the spelling */ }
|
|
16
|
+
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
17
|
+
}
|
|
18
|
+
/** Whether `dir` is one of `trusted` (entries saved under another spelling still count). */
|
|
19
|
+
export function isTrustedFolder(trusted, dir) {
|
|
20
|
+
const key = folderKey(dir);
|
|
21
|
+
return trusted.some((entry) => entry === key || folderKey(entry) === key);
|
|
22
|
+
}
|
|
@@ -246,3 +246,184 @@ export function gitDiff(root, scope = "working_tree") {
|
|
|
246
246
|
}
|
|
247
247
|
return { scope, files_changed: filesChanged, additions, deletions, diff, truncated };
|
|
248
248
|
}
|
|
249
|
+
// ── History (read-only) ──────────────────────────────────────────────────────
|
|
250
|
+
// log, blame and show answer "when and why did this line change?" — the step a
|
|
251
|
+
// root-cause investigation takes after it has found the function. They are
|
|
252
|
+
// reads with the same hardening as status/diff. Revisions are validated to a
|
|
253
|
+
// closed grammar BEFORE they reach argv, so a model-supplied value can never be
|
|
254
|
+
// read by Git as an option, a ref expression with side effects, or a path; and
|
|
255
|
+
// every path follows a literal `--`. Patches are only ever produced for one
|
|
256
|
+
// path the caller already validated against the read rules (containment,
|
|
257
|
+
// ignore, sensitive names), so history cannot be used to read a credentials
|
|
258
|
+
// file that a current read_file would refuse.
|
|
259
|
+
/** Most commits one git_log returns. */
|
|
260
|
+
export const MAX_LOG_COMMITS = 50;
|
|
261
|
+
/** Most changed paths listed per commit. */
|
|
262
|
+
const MAX_LOG_FILES = 20;
|
|
263
|
+
/** Most lines one git_blame covers. */
|
|
264
|
+
export const MAX_BLAME_LINES = 200;
|
|
265
|
+
/** HEAD, HEAD~n, HEAD^…, or an abbreviated/full object id. Nothing else. */
|
|
266
|
+
const REVISION = /^(?:HEAD(?:~\d{1,4}|\^{1,4})?|[0-9a-fA-F]{4,64})$/;
|
|
267
|
+
export function isSafeRevision(rev) {
|
|
268
|
+
return REVISION.test(rev);
|
|
269
|
+
}
|
|
270
|
+
function requireRevision(rev) {
|
|
271
|
+
if (!isSafeRevision(rev)) {
|
|
272
|
+
throw new GitToolError("INVALID_REVISION", 'A revision must be HEAD, HEAD~n, HEAD^, or a commit hash (4–64 hex characters).');
|
|
273
|
+
}
|
|
274
|
+
return rev;
|
|
275
|
+
}
|
|
276
|
+
const HISTORY_GUARDS = ["--no-color", "--no-ext-diff", "--no-textconv"];
|
|
277
|
+
const FIELD = "\x1f";
|
|
278
|
+
const RECORD = "\x1e";
|
|
279
|
+
function noHistory(reason) {
|
|
280
|
+
return /does not have any commits|unknown revision|bad default revision|ambiguous argument 'HEAD'/i.test(reason ?? "");
|
|
281
|
+
}
|
|
282
|
+
function parseCommits(out, withFiles) {
|
|
283
|
+
const commits = [];
|
|
284
|
+
for (const raw of out.split(RECORD)) {
|
|
285
|
+
const record = raw.replace(/^\r?\n+/, "");
|
|
286
|
+
if (!record.trim())
|
|
287
|
+
continue;
|
|
288
|
+
const [header, ...rest] = record.split(/\r?\n/);
|
|
289
|
+
const [commit, short, author, date, subject] = header.split(FIELD);
|
|
290
|
+
if (!commit)
|
|
291
|
+
continue;
|
|
292
|
+
const entry = { commit, short, author, date, subject: subject ?? "" };
|
|
293
|
+
if (withFiles) {
|
|
294
|
+
const files = rest.map((l) => l.trim()).filter(Boolean);
|
|
295
|
+
entry.files = files.slice(0, MAX_LOG_FILES);
|
|
296
|
+
if (files.length > MAX_LOG_FILES)
|
|
297
|
+
entry.files_truncated = true;
|
|
298
|
+
}
|
|
299
|
+
commits.push(entry);
|
|
300
|
+
}
|
|
301
|
+
return commits;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Recent commits, optionally only those touching one validated path.
|
|
305
|
+
* Metadata and changed-path names only — never patches.
|
|
306
|
+
*/
|
|
307
|
+
export function gitLog(root, opts = {}) {
|
|
308
|
+
const limit = Math.min(MAX_LOG_COMMITS, Math.max(1, Math.floor(opts.limit ?? 10)));
|
|
309
|
+
const args = [
|
|
310
|
+
"log",
|
|
311
|
+
...HISTORY_GUARDS,
|
|
312
|
+
`--max-count=${limit + 1}`,
|
|
313
|
+
`--format=${RECORD}%H${FIELD}%h${FIELD}%an${FIELD}%aI${FIELD}%s`,
|
|
314
|
+
"--name-only"
|
|
315
|
+
];
|
|
316
|
+
if (opts.path)
|
|
317
|
+
args.push("--follow", "--", opts.path);
|
|
318
|
+
const res = run(root, args, overridesFor(root));
|
|
319
|
+
if (!res.ok) {
|
|
320
|
+
if (noHistory(res.reason))
|
|
321
|
+
return { commits: [], truncated: false };
|
|
322
|
+
throw new GitToolError("GIT_FAILED", res.reason ?? "git log failed.");
|
|
323
|
+
}
|
|
324
|
+
const commits = parseCommits(res.out, true);
|
|
325
|
+
return { commits: commits.slice(0, limit), truncated: commits.length > limit };
|
|
326
|
+
}
|
|
327
|
+
/** Line-by-line authorship for a bounded range of one validated path. */
|
|
328
|
+
export function gitBlame(root, relPath, startLine, endLine) {
|
|
329
|
+
const start = Math.max(1, Math.floor(startLine));
|
|
330
|
+
const end = Math.max(start, Math.floor(endLine));
|
|
331
|
+
if (end - start + 1 > MAX_BLAME_LINES) {
|
|
332
|
+
throw new GitToolError("RANGE_TOO_LARGE", `Blame at most ${MAX_BLAME_LINES} lines at a time.`);
|
|
333
|
+
}
|
|
334
|
+
const res = run(root, ["blame", "--porcelain", "--no-textconv", "-L", `${start},${end}`, "--", relPath], overridesFor(root));
|
|
335
|
+
if (!res.ok) {
|
|
336
|
+
if (noHistory(res.reason) || /no such path .* in HEAD/i.test(res.reason ?? "")) {
|
|
337
|
+
throw new GitToolError("NO_HISTORY", `${relPath} has no committed history yet.`);
|
|
338
|
+
}
|
|
339
|
+
throw new GitToolError("GIT_FAILED", res.reason ?? "git blame failed.");
|
|
340
|
+
}
|
|
341
|
+
const meta = new Map();
|
|
342
|
+
const lines = [];
|
|
343
|
+
let current = null;
|
|
344
|
+
for (const raw of res.out.split("\n")) {
|
|
345
|
+
const head = raw.match(/^([0-9a-f]{40}) \d+ (\d+)(?: \d+)?$/);
|
|
346
|
+
if (head) {
|
|
347
|
+
current = { commit: head[1], final: Number(head[2]) };
|
|
348
|
+
if (!meta.has(head[1]))
|
|
349
|
+
meta.set(head[1], { author: "", time: null, summary: "" });
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
if (!current)
|
|
353
|
+
continue;
|
|
354
|
+
const m = meta.get(current.commit);
|
|
355
|
+
if (raw.startsWith("author "))
|
|
356
|
+
m.author = raw.slice(7);
|
|
357
|
+
else if (raw.startsWith("author-time "))
|
|
358
|
+
m.time = Number(raw.slice(12)) || null;
|
|
359
|
+
else if (raw.startsWith("summary "))
|
|
360
|
+
m.summary = raw.slice(8);
|
|
361
|
+
else if (raw.startsWith("\t")) {
|
|
362
|
+
const uncommitted = /^0+$/.test(current.commit);
|
|
363
|
+
lines.push({
|
|
364
|
+
line: current.final,
|
|
365
|
+
commit: uncommitted ? "uncommitted" : current.commit.slice(0, 12),
|
|
366
|
+
author: uncommitted ? "Not committed yet" : m.author,
|
|
367
|
+
date: uncommitted || m.time === null ? null : new Date(m.time * 1000).toISOString(),
|
|
368
|
+
summary: uncommitted ? "" : m.summary,
|
|
369
|
+
text: raw.slice(1).replace(/\r$/, ""),
|
|
370
|
+
uncommitted
|
|
371
|
+
});
|
|
372
|
+
current = null;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return { lines };
|
|
376
|
+
}
|
|
377
|
+
/** One commit: metadata and per-file counts; a bounded patch only for one validated path. */
|
|
378
|
+
export function gitShow(root, revision, relPath) {
|
|
379
|
+
const rev = requireRevision(revision);
|
|
380
|
+
const overrides = overridesFor(root);
|
|
381
|
+
const meta = run(root, ["show", "-s", ...HISTORY_GUARDS, `--format=%H${FIELD}%h${FIELD}%an${FIELD}%aI${FIELD}%s${FIELD}%b`, rev, "--"], overrides);
|
|
382
|
+
if (!meta.ok) {
|
|
383
|
+
if (noHistory(meta.reason) || /bad object|bad revision/i.test(meta.reason ?? "")) {
|
|
384
|
+
throw new GitToolError("UNKNOWN_REVISION", `No commit matches ${rev}.`);
|
|
385
|
+
}
|
|
386
|
+
throw new GitToolError("GIT_FAILED", meta.reason ?? "git show failed.");
|
|
387
|
+
}
|
|
388
|
+
const [commit, short, author, date, subject, ...body] = meta.out.replace(/\n+$/, "").split(FIELD);
|
|
389
|
+
const stat = run(root, ["show", ...HISTORY_GUARDS, "--numstat", "--format=", rev, "--"], overrides);
|
|
390
|
+
if (!stat.ok)
|
|
391
|
+
throw new GitToolError("GIT_FAILED", stat.reason ?? "git show failed.");
|
|
392
|
+
const files = [];
|
|
393
|
+
let filesTruncated = false;
|
|
394
|
+
for (const raw of stat.out.split(/\r?\n/)) {
|
|
395
|
+
if (!raw.trim())
|
|
396
|
+
continue;
|
|
397
|
+
if (files.length >= MAX_FILES) {
|
|
398
|
+
filesTruncated = true;
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
const [add, del, ...name] = raw.split("\t");
|
|
402
|
+
files.push({ path: name.join("\t"), additions: add === "-" ? null : Number(add), deletions: del === "-" ? null : Number(del) });
|
|
403
|
+
}
|
|
404
|
+
const result = {
|
|
405
|
+
commit,
|
|
406
|
+
short,
|
|
407
|
+
author,
|
|
408
|
+
date,
|
|
409
|
+
subject: subject ?? "",
|
|
410
|
+
body: body.join(FIELD).trim().slice(0, 4000),
|
|
411
|
+
files_changed: files,
|
|
412
|
+
files_truncated: filesTruncated,
|
|
413
|
+
truncated: false
|
|
414
|
+
};
|
|
415
|
+
if (relPath) {
|
|
416
|
+
const patch = run(root, ["show", ...HISTORY_GUARDS, "--format=", rev, "--", relPath], overrides);
|
|
417
|
+
if (!patch.ok)
|
|
418
|
+
throw new GitToolError("GIT_FAILED", patch.reason ?? "git show failed.");
|
|
419
|
+
let diff = patch.out;
|
|
420
|
+
if (Buffer.byteLength(diff, "utf8") > MAX_DIFF_BYTES) {
|
|
421
|
+
diff = diff.slice(0, MAX_DIFF_BYTES);
|
|
422
|
+
diff = diff.slice(0, diff.lastIndexOf("\n") + 1) + "… [diff truncated]\n";
|
|
423
|
+
result.truncated = true;
|
|
424
|
+
}
|
|
425
|
+
result.path = relPath;
|
|
426
|
+
result.diff = diff;
|
|
427
|
+
}
|
|
428
|
+
return result;
|
|
429
|
+
}
|
|
@@ -1,53 +1,70 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
-
|
|
5
|
-
* Finding a PREBUILT native helper that ships with the CLI.
|
|
6
|
-
*
|
|
7
|
-
* The Job Object runner and the DPAPI credential helper are tiny C# programs.
|
|
8
|
-
* In a developer checkout they are compiled on first use with the Windows
|
|
9
|
-
* `csc.exe`; in the PACKAGED executable there is no compiler, no TypeScript
|
|
10
|
-
* source and no npm — so the helpers are built ONCE at package time and shipped
|
|
11
|
-
* as `.exe` files next to the CLI, each with a `.sha256` sidecar.
|
|
12
|
-
*
|
|
13
|
-
* This module locates such a prebuilt helper and verifies it before it is used.
|
|
14
|
-
* The integrity check catches corruption and a stale or partial copy; it is NOT
|
|
15
|
-
* a defence against other code running as the same Windows user (which could
|
|
16
|
-
* replace the exe and its sidecar together) — the same honest limitation the
|
|
17
|
-
* runtime-compiled path already documents. See docs/PACKAGING.md.
|
|
18
|
-
*
|
|
19
|
-
* Discovery order, none of which is a source path:
|
|
20
|
-
* 1. COS_HELPER_DIR — an explicit override (packaging, tests).
|
|
21
|
-
* 2. the directory of the running executable (where the packaged .exe ships
|
|
22
|
-
* its helpers) and a `bin/` subfolder of it.
|
|
23
|
-
*/
|
|
4
|
+
import { isPackaged, PACKAGED_DOWNLOAD_URL } from "./packaged.js";
|
|
24
5
|
export function sha256File(file) {
|
|
25
6
|
return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
|
26
7
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
8
|
+
const HEX64 = /^[0-9a-f]{64}$/;
|
|
9
|
+
/**
|
|
10
|
+
* The baked helper hashes, or null when the build carries none or carries
|
|
11
|
+
* something malformed. Null is never "anything goes": in packaged mode it means
|
|
12
|
+
* no helper verifies, so the caller fails closed.
|
|
13
|
+
*/
|
|
14
|
+
export function bakedHelperHashes(raw = typeof __HELPER_HASHES__ === "string" ? __HELPER_HASHES__ : undefined) {
|
|
15
|
+
if (typeof raw !== "string" || !raw)
|
|
16
|
+
return null;
|
|
31
17
|
try {
|
|
32
|
-
const
|
|
33
|
-
|
|
18
|
+
const parsed = JSON.parse(raw);
|
|
19
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
20
|
+
return null;
|
|
21
|
+
const out = {};
|
|
22
|
+
for (const [name, hash] of Object.entries(parsed)) {
|
|
23
|
+
if (!/^[A-Za-z0-9._-]+\.exe$/.test(name) || typeof hash !== "string" || !HEX64.test(hash))
|
|
24
|
+
return null;
|
|
25
|
+
out[name] = hash;
|
|
26
|
+
}
|
|
27
|
+
return Object.keys(out).length ? out : null;
|
|
34
28
|
}
|
|
35
29
|
catch {
|
|
36
|
-
|
|
30
|
+
return null;
|
|
37
31
|
}
|
|
32
|
+
}
|
|
33
|
+
function devCandidateDirs(exeDir, env) {
|
|
34
|
+
const dirs = [];
|
|
35
|
+
if (env.COS_HELPER_DIR)
|
|
36
|
+
dirs.push(env.COS_HELPER_DIR);
|
|
37
|
+
dirs.push(exeDir, path.join(exeDir, "bin"));
|
|
38
38
|
return dirs;
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
* The absolute path to a verified prebuilt `<baseName>.exe`, or null
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
41
|
+
* The absolute path to a verified prebuilt `<baseName>.exe`, or null.
|
|
42
|
+
*
|
|
43
|
+
* Packaged: null means the installation is damaged — callers must fail closed.
|
|
44
|
+
* Development: null means "none shipped" — callers may compile from source.
|
|
45
45
|
*/
|
|
46
|
-
export function findPrebuiltHelper(baseName) {
|
|
47
|
-
|
|
46
|
+
export function findPrebuiltHelper(baseName, opts = {}) {
|
|
47
|
+
const platform = opts.platform ?? process.platform;
|
|
48
|
+
if (platform !== "win32")
|
|
48
49
|
return null;
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
const packaged = opts.packaged ?? isPackaged();
|
|
51
|
+
const exeDir = opts.exeDir ?? path.dirname(process.execPath);
|
|
52
|
+
const file = `${baseName}.exe`;
|
|
53
|
+
if (packaged) {
|
|
54
|
+
const hashes = opts.hashes !== undefined ? opts.hashes : bakedHelperHashes();
|
|
55
|
+
const expected = hashes?.[file];
|
|
56
|
+
if (!expected)
|
|
57
|
+
return null;
|
|
58
|
+
const exe = path.join(exeDir, file);
|
|
59
|
+
try {
|
|
60
|
+
return fs.statSync(exe).isFile() && sha256File(exe) === expected ? exe : null;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (const dir of devCandidateDirs(exeDir, opts.env ?? process.env)) {
|
|
67
|
+
const exe = path.join(dir, file);
|
|
51
68
|
const stamp = `${exe}.sha256`;
|
|
52
69
|
try {
|
|
53
70
|
if (!fs.existsSync(exe) || !fs.existsSync(stamp))
|
|
@@ -62,3 +79,7 @@ export function findPrebuiltHelper(baseName) {
|
|
|
62
79
|
}
|
|
63
80
|
return null;
|
|
64
81
|
}
|
|
82
|
+
/** The message every fail-closed packaged path shows. */
|
|
83
|
+
export function damagedInstallMessage(file) {
|
|
84
|
+
return `This Carrier OS installation is missing or has a damaged component (${file}). Reinstall Carrier OS from ${PACKAGED_DOWNLOAD_URL}.`;
|
|
85
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/** Repository instruction sources, ordered from general to specific. */
|
|
4
|
+
export const INSTRUCTION_FILE_NAMES = ["AGENTS.md", "CLAUDE.md"];
|
|
5
|
+
/**
|
|
6
|
+
* Resolve instruction files that apply to a set of repository-relative
|
|
7
|
+
* directories. Results are ordered root-to-leaf, with AGENTS.md before
|
|
8
|
+
* CLAUDE.md at each scope, so later, narrower guidance can take precedence.
|
|
9
|
+
*
|
|
10
|
+
* This only discovers paths; callers still read the files through the normal
|
|
11
|
+
* containment-checked file reader. In particular, a symlink outside the
|
|
12
|
+
* selected repository is never treated as project guidance.
|
|
13
|
+
*/
|
|
14
|
+
export function instructionPathsForDirectories(repositoryRoot, directories) {
|
|
15
|
+
const root = fs.realpathSync.native(repositoryRoot);
|
|
16
|
+
const scopes = new Set(["."]);
|
|
17
|
+
for (const directory of directories) {
|
|
18
|
+
if (typeof directory !== "string" || !directory.trim() || path.isAbsolute(directory) || /[:\0]/.test(directory)) {
|
|
19
|
+
throw new Error("Instruction scopes must be repository-relative paths.");
|
|
20
|
+
}
|
|
21
|
+
const normalized = path.normalize(directory);
|
|
22
|
+
const parts = normalized.split(path.sep).filter((part) => part && part !== ".");
|
|
23
|
+
if (parts.includes(".."))
|
|
24
|
+
throw new Error("Instruction scopes must stay inside the repository.");
|
|
25
|
+
let current = ".";
|
|
26
|
+
for (const part of parts) {
|
|
27
|
+
current = current === "." ? part : path.join(current, part);
|
|
28
|
+
scopes.add(current);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const orderedScopes = [...scopes].sort((a, b) => {
|
|
32
|
+
const depth = (value) => value === "." ? 0 : value.split(path.sep).length;
|
|
33
|
+
return depth(a) - depth(b) || a.localeCompare(b);
|
|
34
|
+
});
|
|
35
|
+
const found = [];
|
|
36
|
+
for (const scope of orderedScopes) {
|
|
37
|
+
for (const name of INSTRUCTION_FILE_NAMES) {
|
|
38
|
+
const absolute = path.join(root, scope, name);
|
|
39
|
+
try {
|
|
40
|
+
if (!fs.statSync(absolute).isFile())
|
|
41
|
+
continue;
|
|
42
|
+
const actual = fs.realpathSync.native(absolute);
|
|
43
|
+
const relative = path.relative(root, actual);
|
|
44
|
+
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
|
|
45
|
+
continue;
|
|
46
|
+
found.push(path.relative(root, absolute).split(path.sep).join("/"));
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Missing, unreadable and broken-link instruction files are ignored.
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return found;
|
|
54
|
+
}
|
|
@@ -3,7 +3,8 @@ import { execFileSync } from "node:child_process";
|
|
|
3
3
|
import fs from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { ConfigStore } from "./config.js";
|
|
6
|
-
import { findPrebuiltHelper } from "./helper-bin.js";
|
|
6
|
+
import { damagedInstallMessage, findPrebuiltHelper } from "./helper-bin.js";
|
|
7
|
+
import { isPackaged } from "./packaged.js";
|
|
7
8
|
/**
|
|
8
9
|
* The Windows process-containment helper.
|
|
9
10
|
*
|
|
@@ -205,12 +206,34 @@ static class CosJobRunner
|
|
|
205
206
|
Console.Error.WriteLine("cos-job-runner: AssignProcessToJobObject: " + reason);
|
|
206
207
|
return HelperFailure;
|
|
207
208
|
}
|
|
208
|
-
ResumeThread(pi.hThread)
|
|
209
|
+
if (ResumeThread(pi.hThread) == 0xFFFFFFFF)
|
|
210
|
+
{
|
|
211
|
+
int failure = Fail("ResumeThread");
|
|
212
|
+
TerminateJobObject(job, HelperFailure);
|
|
213
|
+
CloseHandle(pi.hThread);
|
|
214
|
+
CloseHandle(pi.hProcess);
|
|
215
|
+
CloseHandle(job);
|
|
216
|
+
return failure;
|
|
217
|
+
}
|
|
209
218
|
CloseHandle(pi.hThread);
|
|
210
219
|
|
|
211
|
-
WaitForSingleObject(pi.hProcess, INFINITE)
|
|
220
|
+
if (WaitForSingleObject(pi.hProcess, INFINITE) != 0)
|
|
221
|
+
{
|
|
222
|
+
int failure = Fail("WaitForSingleObject");
|
|
223
|
+
TerminateJobObject(job, HelperFailure);
|
|
224
|
+
CloseHandle(pi.hProcess);
|
|
225
|
+
CloseHandle(job);
|
|
226
|
+
return failure;
|
|
227
|
+
}
|
|
212
228
|
uint code;
|
|
213
|
-
GetExitCodeProcess(pi.hProcess, out code)
|
|
229
|
+
if (!GetExitCodeProcess(pi.hProcess, out code))
|
|
230
|
+
{
|
|
231
|
+
int failure = Fail("GetExitCodeProcess");
|
|
232
|
+
TerminateJobObject(job, HelperFailure);
|
|
233
|
+
CloseHandle(pi.hProcess);
|
|
234
|
+
CloseHandle(job);
|
|
235
|
+
return failure;
|
|
236
|
+
}
|
|
214
237
|
CloseHandle(pi.hProcess);
|
|
215
238
|
|
|
216
239
|
// Whatever the program left running dies with it.
|
|
@@ -243,7 +266,6 @@ function findCompiler() {
|
|
|
243
266
|
}
|
|
244
267
|
return null;
|
|
245
268
|
}
|
|
246
|
-
let verified = null;
|
|
247
269
|
/**
|
|
248
270
|
* Absolute path to a verified helper, compiling it if needed.
|
|
249
271
|
* Throws JobHelperError when containment cannot be provided — callers must then
|
|
@@ -257,18 +279,18 @@ export function ensureJobHelper() {
|
|
|
257
279
|
// compiler, source or npm involved. Use it when present and intact.
|
|
258
280
|
const prebuilt = findPrebuiltHelper("cos-job-runner");
|
|
259
281
|
if (prebuilt) {
|
|
260
|
-
verified = prebuilt;
|
|
261
282
|
return prebuilt;
|
|
262
283
|
}
|
|
284
|
+
// A packaged build never compiles: no prebuilt helper means a damaged install.
|
|
285
|
+
if (isPackaged()) {
|
|
286
|
+
throw new JobHelperError("HELPER_UNAVAILABLE", `${damagedInstallMessage("cos-job-runner.exe")} Nothing was run.`);
|
|
287
|
+
}
|
|
263
288
|
const tag = sha256(JOB_HELPER_SOURCE).slice(0, 16);
|
|
264
289
|
const dir = helperDir();
|
|
265
290
|
const exe = path.join(dir, `cos-job-runner-${tag}.exe`);
|
|
266
291
|
const stamp = `${exe}.sha256`;
|
|
267
|
-
if (verified === exe && fs.existsSync(exe))
|
|
268
|
-
return exe;
|
|
269
292
|
if (fs.existsSync(exe) && fs.existsSync(stamp)) {
|
|
270
293
|
if (sha256(fs.readFileSync(exe)) === fs.readFileSync(stamp, "utf8").trim()) {
|
|
271
|
-
verified = exe;
|
|
272
294
|
return exe;
|
|
273
295
|
}
|
|
274
296
|
// Corrupt or tampered: rebuild rather than run it.
|
|
@@ -302,6 +324,5 @@ export function ensureJobHelper() {
|
|
|
302
324
|
finally {
|
|
303
325
|
fs.rmSync(srcFile, { force: true });
|
|
304
326
|
}
|
|
305
|
-
verified = exe;
|
|
306
327
|
return exe;
|
|
307
328
|
}
|