@carrieros/cli-staging 0.0.0-stage → 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.
- 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 +25 -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 +77 -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 +100 -0
- package/dist/commands/update.js +229 -0
- package/dist/commands/upload.js +223 -0
- package/dist/commands/usage.js +227 -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 +360 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +126 -0
- package/dist/services/api.js +439 -0
- package/dist/services/auth-origin.js +20 -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 +383 -0
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +371 -0
- package/dist/services/device-auth.js +286 -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 +15 -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 +113 -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 +311 -0
- package/dist/services/workspace-registry.js +310 -0
- package/dist/services/workspace.js +580 -0
- package/dist/shell/shell-chat.js +1619 -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 +389 -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,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Line-based unified diffs, for showing a person exactly what a proposed change
|
|
3
|
+
* does before they approve it.
|
|
4
|
+
*
|
|
5
|
+
* Myers' algorithm on the lines that actually differ: the common prefix and
|
|
6
|
+
* suffix are trimmed first, which is what keeps the typical case — a few lines
|
|
7
|
+
* changed in a long file — cheap. When what is left is still huge (a whole-file
|
|
8
|
+
* rewrite), it is shown as a replacement rather than spending seconds finding a
|
|
9
|
+
* minimal edit script nobody will read line by line.
|
|
10
|
+
*
|
|
11
|
+
* Deterministic: the same before/after always yields the same diff, so the
|
|
12
|
+
* text a user approved can be reproduced exactly for the audit and for tests.
|
|
13
|
+
*/
|
|
14
|
+
const CONTEXT = 3;
|
|
15
|
+
/** Beyond this many differing lines per side, show a replacement, not an edit script. */
|
|
16
|
+
const MAX_MYERS_LINES = 4000;
|
|
17
|
+
/** Split into lines without their terminators; a trailing newline adds no empty line. */
|
|
18
|
+
export function toLines(text) {
|
|
19
|
+
if (text === "")
|
|
20
|
+
return [];
|
|
21
|
+
const lines = text.split(/\r?\n/);
|
|
22
|
+
if (lines[lines.length - 1] === "")
|
|
23
|
+
lines.pop();
|
|
24
|
+
return lines;
|
|
25
|
+
}
|
|
26
|
+
/** Shortest edit script for a and b, as a list of ops in order. */
|
|
27
|
+
function myers(a, b) {
|
|
28
|
+
const n = a.length;
|
|
29
|
+
const m = b.length;
|
|
30
|
+
const max = n + m;
|
|
31
|
+
const offset = max;
|
|
32
|
+
const v = new Int32Array(2 * max + 2);
|
|
33
|
+
const trace = [];
|
|
34
|
+
let found = -1;
|
|
35
|
+
for (let d = 0; d <= max; d += 1) {
|
|
36
|
+
trace.push(v.slice());
|
|
37
|
+
for (let k = -d; k <= d; k += 2) {
|
|
38
|
+
let x;
|
|
39
|
+
if (k === -d || (k !== d && v[offset + k - 1] < v[offset + k + 1])) {
|
|
40
|
+
x = v[offset + k + 1]; // down: insertion
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
x = v[offset + k - 1] + 1; // right: deletion
|
|
44
|
+
}
|
|
45
|
+
let y = x - k;
|
|
46
|
+
while (x < n && y < m && a[x] === b[y]) {
|
|
47
|
+
x += 1;
|
|
48
|
+
y += 1;
|
|
49
|
+
}
|
|
50
|
+
v[offset + k] = x;
|
|
51
|
+
if (x >= n && y >= m) {
|
|
52
|
+
found = d;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (found >= 0)
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
// Walk the trace backwards to recover the path.
|
|
60
|
+
const out = [];
|
|
61
|
+
let x = n;
|
|
62
|
+
let y = m;
|
|
63
|
+
for (let d = found; d > 0; d -= 1) {
|
|
64
|
+
const vd = trace[d];
|
|
65
|
+
const k = x - y;
|
|
66
|
+
const down = k === -d || (k !== d && vd[offset + k - 1] < vd[offset + k + 1]);
|
|
67
|
+
const prevK = down ? k + 1 : k - 1;
|
|
68
|
+
const prevX = vd[offset + prevK];
|
|
69
|
+
const prevY = prevX - prevK;
|
|
70
|
+
while (x > prevX && y > prevY) {
|
|
71
|
+
out.push({ op: " ", text: a[x - 1] });
|
|
72
|
+
x -= 1;
|
|
73
|
+
y -= 1;
|
|
74
|
+
}
|
|
75
|
+
if (down)
|
|
76
|
+
out.push({ op: "+", text: b[y - 1] });
|
|
77
|
+
else
|
|
78
|
+
out.push({ op: "-", text: a[x - 1] });
|
|
79
|
+
x = prevX;
|
|
80
|
+
y = prevY;
|
|
81
|
+
}
|
|
82
|
+
while (x > 0 && y > 0) {
|
|
83
|
+
out.push({ op: " ", text: a[x - 1] });
|
|
84
|
+
x -= 1;
|
|
85
|
+
y -= 1;
|
|
86
|
+
}
|
|
87
|
+
return out.reverse();
|
|
88
|
+
}
|
|
89
|
+
/** Full op list for before → after. */
|
|
90
|
+
function editScript(a, b) {
|
|
91
|
+
let start = 0;
|
|
92
|
+
while (start < a.length && start < b.length && a[start] === b[start])
|
|
93
|
+
start += 1;
|
|
94
|
+
let endA = a.length;
|
|
95
|
+
let endB = b.length;
|
|
96
|
+
while (endA > start && endB > start && a[endA - 1] === b[endB - 1]) {
|
|
97
|
+
endA -= 1;
|
|
98
|
+
endB -= 1;
|
|
99
|
+
}
|
|
100
|
+
const head = a.slice(0, start).map((text) => ({ op: " ", text }));
|
|
101
|
+
const tail = a.slice(endA).map((text) => ({ op: " ", text }));
|
|
102
|
+
const midA = a.slice(start, endA);
|
|
103
|
+
const midB = b.slice(start, endB);
|
|
104
|
+
let middle;
|
|
105
|
+
if (midA.length > MAX_MYERS_LINES || midB.length > MAX_MYERS_LINES) {
|
|
106
|
+
middle = [
|
|
107
|
+
...midA.map((text) => ({ op: "-", text })),
|
|
108
|
+
...midB.map((text) => ({ op: "+", text }))
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
middle = myers(midA, midB);
|
|
113
|
+
}
|
|
114
|
+
return [...head, ...middle, ...tail];
|
|
115
|
+
}
|
|
116
|
+
/** Group an op list into unified-diff hunks with surrounding context. */
|
|
117
|
+
export function diffLines(before, after) {
|
|
118
|
+
const ops = editScript(toLines(before), toLines(after));
|
|
119
|
+
const added = ops.filter((o) => o.op === "+").length;
|
|
120
|
+
const removed = ops.filter((o) => o.op === "-").length;
|
|
121
|
+
const hunks = [];
|
|
122
|
+
let oldLine = 1;
|
|
123
|
+
let newLine = 1;
|
|
124
|
+
let i = 0;
|
|
125
|
+
while (i < ops.length) {
|
|
126
|
+
if (ops[i].op === " ") {
|
|
127
|
+
oldLine += 1;
|
|
128
|
+
newLine += 1;
|
|
129
|
+
i += 1;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
// A change starts here: back up for context, then run forward until the
|
|
133
|
+
// changes are separated by more than 2 × CONTEXT unchanged lines.
|
|
134
|
+
const ctxStart = Math.max(0, i - CONTEXT);
|
|
135
|
+
const lead = i - ctxStart;
|
|
136
|
+
const hunk = { oldStart: oldLine - lead, newStart: newLine - lead, oldLines: 0, newLines: 0, lines: [] };
|
|
137
|
+
for (let j = ctxStart; j < i; j += 1) {
|
|
138
|
+
hunk.lines.push(ops[j]);
|
|
139
|
+
hunk.oldLines += 1;
|
|
140
|
+
hunk.newLines += 1;
|
|
141
|
+
}
|
|
142
|
+
let j = i;
|
|
143
|
+
let quiet = 0;
|
|
144
|
+
while (j < ops.length) {
|
|
145
|
+
const op = ops[j];
|
|
146
|
+
if (op.op === " ") {
|
|
147
|
+
quiet += 1;
|
|
148
|
+
if (quiet > CONTEXT * 2)
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
quiet = 0;
|
|
153
|
+
}
|
|
154
|
+
hunk.lines.push(op);
|
|
155
|
+
if (op.op !== "+")
|
|
156
|
+
hunk.oldLines += 1;
|
|
157
|
+
if (op.op !== "-")
|
|
158
|
+
hunk.newLines += 1;
|
|
159
|
+
if (op.op !== "+")
|
|
160
|
+
oldLine += 1;
|
|
161
|
+
if (op.op !== "-")
|
|
162
|
+
newLine += 1;
|
|
163
|
+
j += 1;
|
|
164
|
+
}
|
|
165
|
+
// Trim trailing context beyond CONTEXT.
|
|
166
|
+
let trailing = 0;
|
|
167
|
+
for (let t = hunk.lines.length - 1; t >= 0 && hunk.lines[t].op === " "; t -= 1)
|
|
168
|
+
trailing += 1;
|
|
169
|
+
const excess = Math.max(0, trailing - CONTEXT);
|
|
170
|
+
if (excess > 0) {
|
|
171
|
+
hunk.lines.splice(hunk.lines.length - excess, excess);
|
|
172
|
+
hunk.oldLines -= excess;
|
|
173
|
+
hunk.newLines -= excess;
|
|
174
|
+
oldLine -= excess;
|
|
175
|
+
newLine -= excess;
|
|
176
|
+
j -= excess;
|
|
177
|
+
}
|
|
178
|
+
hunks.push(hunk);
|
|
179
|
+
i = j;
|
|
180
|
+
}
|
|
181
|
+
return { hunks, added, removed };
|
|
182
|
+
}
|
|
183
|
+
/** Standard unified diff text (`--- a/…`, `+++ b/…`, `@@ … @@`). */
|
|
184
|
+
export function formatUnified(path, result, opts = {}) {
|
|
185
|
+
const lines = [];
|
|
186
|
+
lines.push(opts.oldExists === false ? "--- /dev/null" : `--- a/${path}`);
|
|
187
|
+
lines.push(opts.newExists === false ? "+++ /dev/null" : `+++ b/${path}`);
|
|
188
|
+
for (const h of result.hunks) {
|
|
189
|
+
const oldRange = h.oldLines === 0 ? `${Math.max(0, h.oldStart - 1)},0` : `${h.oldStart},${h.oldLines}`;
|
|
190
|
+
const newRange = h.newLines === 0 ? `${Math.max(0, h.newStart - 1)},0` : `${h.newStart},${h.newLines}`;
|
|
191
|
+
lines.push(`@@ -${oldRange} +${newRange} @@`);
|
|
192
|
+
for (const l of h.lines)
|
|
193
|
+
lines.push(`${l.op}${l.text}`);
|
|
194
|
+
}
|
|
195
|
+
return lines.join("\n");
|
|
196
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
/**
|
|
3
|
+
* The environment a local command gets: built from an allowlist, never
|
|
4
|
+
* inherited wholesale.
|
|
5
|
+
*
|
|
6
|
+
* A developer's shell environment is where credentials live — cloud keys,
|
|
7
|
+
* registry tokens, CI secrets, other tools' session tokens. A project command
|
|
8
|
+
* is project code the user approved to RUN; it was never approved to read those.
|
|
9
|
+
*
|
|
10
|
+
* Values are never logged or returned — only the NAMES that were dropped, so a
|
|
11
|
+
* prompt or an audit row can say what a command did not get.
|
|
12
|
+
*/
|
|
13
|
+
/** Exact names (compared case-insensitively — Windows env names are). */
|
|
14
|
+
const ALLOWED = new Set([
|
|
15
|
+
"PATH",
|
|
16
|
+
"PATHEXT",
|
|
17
|
+
"SystemRoot",
|
|
18
|
+
"SystemDrive",
|
|
19
|
+
"windir",
|
|
20
|
+
"ComSpec",
|
|
21
|
+
"TEMP",
|
|
22
|
+
"TMP",
|
|
23
|
+
"USERPROFILE",
|
|
24
|
+
"APPDATA",
|
|
25
|
+
"LOCALAPPDATA",
|
|
26
|
+
"HOMEDRIVE",
|
|
27
|
+
"HOMEPATH",
|
|
28
|
+
"OS",
|
|
29
|
+
"NUMBER_OF_PROCESSORS",
|
|
30
|
+
"ProgramFiles",
|
|
31
|
+
"ProgramFiles(x86)",
|
|
32
|
+
"ProgramW6432",
|
|
33
|
+
"CommonProgramFiles",
|
|
34
|
+
"CommonProgramFiles(x86)",
|
|
35
|
+
"CommonProgramW6432",
|
|
36
|
+
"ProgramData",
|
|
37
|
+
"USERNAME",
|
|
38
|
+
"COMPUTERNAME",
|
|
39
|
+
"LANG",
|
|
40
|
+
"TZ"
|
|
41
|
+
].map((n) => n.toUpperCase()));
|
|
42
|
+
/** Prefixes allowed when nothing else rules the name out. */
|
|
43
|
+
const ALLOWED_PREFIXES = ["PROCESSOR_", "NPM_CONFIG_"];
|
|
44
|
+
/** Any of these in a name means it is dropped, even if otherwise allowed. */
|
|
45
|
+
const SENSITIVE_NAME = /(TOKEN|SECRET|PASS|KEY|CRED|AUTH|SESSION|COOKIE|CERT|PRIVATE)/i;
|
|
46
|
+
/** Whole families that are dropped regardless. */
|
|
47
|
+
const DENIED_PREFIX = /^(AWS_|AZURE_|GOOGLE_|GCP_|GCLOUD_|GH_|GITHUB_|GITLAB_|OPENAI_|ANTHROPIC_|CLAUDE_CODE_|HF_|HUGGING|STRIPE_|SLACK_|TWILIO_|SENDGRID_|VAULT_|DOCKER_|KUBE)/i;
|
|
48
|
+
/** Database and connection URLs, by name. */
|
|
49
|
+
const DATABASE_NAME = /(DATABASE|_DB_|^DB_|_URI$|_URL$|DSN|CONNECTION|REDIS|MONGO|POSTGRES|PG[A-Z]*$|MYSQL)/i;
|
|
50
|
+
/** A value that embeds credentials in a URL (`scheme://user:pass@host`). */
|
|
51
|
+
const URL_WITH_USERINFO = /^[a-z][a-z0-9+.-]*:\/\/[^/\s:@]+:[^/\s@]+@/i;
|
|
52
|
+
/** Always set, overriding anything inherited. */
|
|
53
|
+
export const FORCED_ENV = {
|
|
54
|
+
CI: "1",
|
|
55
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
56
|
+
GIT_OPTIONAL_LOCKS: "0",
|
|
57
|
+
NO_COLOR: "1",
|
|
58
|
+
npm_config_update_notifier: "false",
|
|
59
|
+
npm_config_fund: "false"
|
|
60
|
+
};
|
|
61
|
+
function allowedName(name) {
|
|
62
|
+
const upper = name.toUpperCase();
|
|
63
|
+
if (SENSITIVE_NAME.test(upper) || DENIED_PREFIX.test(upper))
|
|
64
|
+
return false;
|
|
65
|
+
if (upper.startsWith("NPM_CONFIG_")) {
|
|
66
|
+
// npm config can carry registry auth (`_auth`, `_authToken`, `//host/:_password`).
|
|
67
|
+
return !/(_AUTH|TOKEN|PASSWORD|USERNAME|EMAIL|CERT|KEY|CAFILE)/.test(upper);
|
|
68
|
+
}
|
|
69
|
+
if (DATABASE_NAME.test(upper))
|
|
70
|
+
return false;
|
|
71
|
+
return ALLOWED.has(upper) || ALLOWED_PREFIXES.some((p) => upper.startsWith(p));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* PATH without relative, empty or project-local entries: a `.` or a folder
|
|
75
|
+
* inside the repository would let the project substitute its own `git.exe`.
|
|
76
|
+
*/
|
|
77
|
+
export function sanitizePath(value, projectRoot) {
|
|
78
|
+
const root = path.resolve(projectRoot).toLowerCase();
|
|
79
|
+
return value
|
|
80
|
+
.split(path.delimiter)
|
|
81
|
+
.map((p) => p.trim().replace(/^"(.*)"$/, "$1"))
|
|
82
|
+
.filter((p) => p && path.isAbsolute(p))
|
|
83
|
+
.filter((p) => {
|
|
84
|
+
const abs = path.resolve(p).toLowerCase();
|
|
85
|
+
return abs !== root && !abs.startsWith(root + path.sep);
|
|
86
|
+
})
|
|
87
|
+
.join(path.delimiter);
|
|
88
|
+
}
|
|
89
|
+
export function buildChildEnv(source, projectRoot) {
|
|
90
|
+
const env = {};
|
|
91
|
+
const dropped = [];
|
|
92
|
+
for (const [name, value] of Object.entries(source)) {
|
|
93
|
+
if (value === undefined)
|
|
94
|
+
continue;
|
|
95
|
+
if (!allowedName(name) || URL_WITH_USERINFO.test(value)) {
|
|
96
|
+
dropped.push(name);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
env[name] = name.toUpperCase() === "PATH" ? sanitizePath(value, projectRoot) : value;
|
|
100
|
+
}
|
|
101
|
+
// Forced values replace any inherited spelling of the same name.
|
|
102
|
+
for (const name of Object.keys(env)) {
|
|
103
|
+
if (Object.keys(FORCED_ENV).some((f) => f.toUpperCase() === name.toUpperCase()))
|
|
104
|
+
delete env[name];
|
|
105
|
+
}
|
|
106
|
+
Object.assign(env, FORCED_ENV);
|
|
107
|
+
return { env, dropped: dropped.sort() };
|
|
108
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isInside } from "./local-files.js";
|
|
4
|
+
import { repositoryForPath } from "./workspace-registry.js";
|
|
5
|
+
/** Split input into tokens, honoring double quotes so paths with spaces work. */
|
|
6
|
+
function tokenize(input) {
|
|
7
|
+
const tokens = [];
|
|
8
|
+
const re = /"([^"]*)"|(\S+)/g;
|
|
9
|
+
let m;
|
|
10
|
+
while ((m = re.exec(input)) !== null) {
|
|
11
|
+
if (m[1] !== undefined)
|
|
12
|
+
tokens.push({ value: m[1], quoted: true });
|
|
13
|
+
else
|
|
14
|
+
tokens.push({ value: m[2], quoted: false });
|
|
15
|
+
}
|
|
16
|
+
return tokens;
|
|
17
|
+
}
|
|
18
|
+
/** A token worth checking as a path: has a separator or a file extension. */
|
|
19
|
+
function looksLikePath(s) {
|
|
20
|
+
return /[\\/]/.test(s) || /\.[a-z0-9]{1,8}$/i.test(s);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Resolve a token to a file this workspace may attach.
|
|
24
|
+
*
|
|
25
|
+
* Attaching UPLOADS the file, so auto-detection has to obey the same boundary
|
|
26
|
+
* as every other local read. It used to accept any existing path at all — so
|
|
27
|
+
* `"C:\Users\me\.ssh\id_rsa" explain this`, typed from inside a project, would
|
|
28
|
+
* have sent that key to the server with nothing but the folder-trust prompt for
|
|
29
|
+
* the directory the shell started in standing in the way.
|
|
30
|
+
*
|
|
31
|
+
* Two checks, as in local-files.ts: the resolved path must be inside the
|
|
32
|
+
* workspace root, and so must its realpath — which is what stops a symlink or
|
|
33
|
+
* junction inside the project from pointing the upload somewhere else.
|
|
34
|
+
*/
|
|
35
|
+
function resolveToken(token) {
|
|
36
|
+
try {
|
|
37
|
+
const abs = path.resolve(token);
|
|
38
|
+
if (!(fs.existsSync(abs) && fs.statSync(abs).isFile()))
|
|
39
|
+
return { kind: "none" };
|
|
40
|
+
// A workspace holds several repositories, so "inside the workspace" means
|
|
41
|
+
// inside ONE OF THEM — never the folder that contains them. A file in no
|
|
42
|
+
// registered repository is still outside, exactly as before.
|
|
43
|
+
const owner = repositoryForPath(abs);
|
|
44
|
+
if (!owner)
|
|
45
|
+
return { kind: "outside", abs };
|
|
46
|
+
const real = fs.realpathSync.native(abs);
|
|
47
|
+
// The realpath must belong to the SAME repository: a junction inside repo A
|
|
48
|
+
// pointing at repo B is not a file of A, and following it would attach one
|
|
49
|
+
// repository's file under another's name.
|
|
50
|
+
const realOwner = repositoryForPath(real);
|
|
51
|
+
if (!realOwner || realOwner.id !== owner.id || !isInside(owner.root, real))
|
|
52
|
+
return { kind: "outside", abs };
|
|
53
|
+
return { kind: "file", abs, repository: owner.name };
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return { kind: "none" }; // invalid path — not a file
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Detect local file path(s) at the START of the message (a leading run) or in
|
|
61
|
+
* any QUOTED token, and return the message with those tokens stripped. Only the
|
|
62
|
+
* leading run + quoted tokens are considered, so a filename mentioned mid-
|
|
63
|
+
* sentence (e.g. "tell me about report.pdf") is NOT uploaded.
|
|
64
|
+
*
|
|
65
|
+
* "D:\a\report.pdf summarize" → { files: [D:\a\report.pdf], prompt: "summarize" }
|
|
66
|
+
* "report.pdf explain this" → { files: [report.pdf], prompt: "explain this" }
|
|
67
|
+
* "\"./my notes.txt\" summarize"→ { files: [./my notes.txt], prompt: "summarize" }
|
|
68
|
+
* "just a normal question" → { files: [], prompt: "just a normal question" }
|
|
69
|
+
*/
|
|
70
|
+
export function detectLocalFiles(input) {
|
|
71
|
+
const tokens = tokenize(input);
|
|
72
|
+
const files = [];
|
|
73
|
+
const rejected = [];
|
|
74
|
+
const consumed = new Set();
|
|
75
|
+
let inLeadingRun = true;
|
|
76
|
+
tokens.forEach((t, i) => {
|
|
77
|
+
const consider = t.quoted || inLeadingRun;
|
|
78
|
+
let matched = false;
|
|
79
|
+
if (consider && looksLikePath(t.value)) {
|
|
80
|
+
const r = resolveToken(t.value);
|
|
81
|
+
if (r.kind === "file") {
|
|
82
|
+
files.push(r.abs);
|
|
83
|
+
consumed.add(i);
|
|
84
|
+
matched = true;
|
|
85
|
+
}
|
|
86
|
+
else if (r.kind === "outside") {
|
|
87
|
+
// Not attached, and left in the prompt as the text the user typed — the
|
|
88
|
+
// file's CONTENT is what must not leave the machine, not their words.
|
|
89
|
+
rejected.push(r.abs);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// The leading run ends at the first non-quoted token that isn't a file path.
|
|
93
|
+
if (inLeadingRun && !matched && !t.quoted)
|
|
94
|
+
inLeadingRun = false;
|
|
95
|
+
});
|
|
96
|
+
if (files.length === 0)
|
|
97
|
+
return { files: [], prompt: input.trim(), rejected };
|
|
98
|
+
const prompt = tokens
|
|
99
|
+
.filter((_, i) => !consumed.has(i))
|
|
100
|
+
.map((t) => t.value)
|
|
101
|
+
.join(" ")
|
|
102
|
+
.trim();
|
|
103
|
+
return { files, prompt, rejected };
|
|
104
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { LocalFileError, isInside, isPathIgnored, resolveInside, walkRepository } from "./local-files.js";
|
|
4
|
+
import { isSensitiveName } from "./local-tools.js";
|
|
5
|
+
import { activeRepository, isWildcardSelector, listRepositories, repositoryForPath, repositoryTrust, resolveRepository } from "./workspace-registry.js";
|
|
6
|
+
/** Cap on how many repositories/entries a fuzzy search will examine. */
|
|
7
|
+
const MAX_CANDIDATES = 10;
|
|
8
|
+
const posix = (p) => p.split(path.sep).join("/");
|
|
9
|
+
function trustedRepositories() {
|
|
10
|
+
return listRepositories().filter((r) => repositoryTrust(r) === "trusted");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A resolved entry IF `rel` exists inside `repo` and passes containment. Returns
|
|
14
|
+
* null when it does not exist or is excluded; throws nothing — a bad path is
|
|
15
|
+
* simply "no match here".
|
|
16
|
+
*/
|
|
17
|
+
function tryExact(repo, rel, kind) {
|
|
18
|
+
let abs;
|
|
19
|
+
try {
|
|
20
|
+
abs = resolveInside(rel, repo.root);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return null; // outside the repo, or a link that leaves it
|
|
24
|
+
}
|
|
25
|
+
let st;
|
|
26
|
+
try {
|
|
27
|
+
st = fs.statSync(abs);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const isDir = st.isDirectory();
|
|
33
|
+
if (kind === "file" && isDir)
|
|
34
|
+
return null;
|
|
35
|
+
if (kind === "directory" && !isDir)
|
|
36
|
+
return null;
|
|
37
|
+
const relOut = posix(path.relative(fs.realpathSync.native(repo.root), abs));
|
|
38
|
+
if (!relOut || relOut.startsWith(".."))
|
|
39
|
+
return null;
|
|
40
|
+
if (isPathIgnored(relOut, repo.root))
|
|
41
|
+
return null;
|
|
42
|
+
if (!isDir && isSensitiveName(path.basename(abs)))
|
|
43
|
+
return null;
|
|
44
|
+
return {
|
|
45
|
+
repositoryId: repo.id,
|
|
46
|
+
repositoryName: repo.name,
|
|
47
|
+
relativePath: relOut,
|
|
48
|
+
absolutePath: abs,
|
|
49
|
+
isDirectory: isDir,
|
|
50
|
+
matchType: "exact_repo_relative",
|
|
51
|
+
confidence: "high"
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** The registered repo a reference is qualified with, plus the remaining path. */
|
|
55
|
+
function splitRepoQualified(reference) {
|
|
56
|
+
const norm = reference.replace(/\\/g, "/").replace(/^\.\//, "");
|
|
57
|
+
const slash = norm.indexOf("/");
|
|
58
|
+
if (slash <= 0)
|
|
59
|
+
return null;
|
|
60
|
+
const head = norm.slice(0, slash);
|
|
61
|
+
const rest = norm.slice(slash + 1);
|
|
62
|
+
if (!rest)
|
|
63
|
+
return null;
|
|
64
|
+
const wanted = head.toLowerCase();
|
|
65
|
+
const repo = listRepositories().find((r) => r.id === wanted || r.name.toLowerCase() === wanted);
|
|
66
|
+
return repo ? { repo, rest } : null;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Fuzzy match by basename (a bare name like `user.js` or `hooks`) or by path
|
|
70
|
+
* suffix (`routes/user.js`) across the trusted repositories. Uses the contained
|
|
71
|
+
* walker, so ignore rules and symlink-skipping already apply. Bounded.
|
|
72
|
+
*/
|
|
73
|
+
function searchByName(reference, kind, repos) {
|
|
74
|
+
const norm = reference.replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+$/, "");
|
|
75
|
+
const wantBasename = norm.split("/").pop() ?? norm;
|
|
76
|
+
const hasSlash = norm.includes("/");
|
|
77
|
+
const out = [];
|
|
78
|
+
for (const repo of repos) {
|
|
79
|
+
const walk = walkRepository({ root: repo.root, includeDirectories: true });
|
|
80
|
+
for (const e of walk.entries) {
|
|
81
|
+
if (out.length >= MAX_CANDIDATES)
|
|
82
|
+
break;
|
|
83
|
+
const isDir = e.type === "dir";
|
|
84
|
+
if (kind === "file" && isDir)
|
|
85
|
+
continue;
|
|
86
|
+
if (kind === "directory" && !isDir)
|
|
87
|
+
continue;
|
|
88
|
+
const base = e.path.split("/").pop() ?? e.path;
|
|
89
|
+
// A path-like reference matches by suffix; a bare name matches basename.
|
|
90
|
+
const matches = hasSlash
|
|
91
|
+
? e.path === norm || e.path.endsWith("/" + norm)
|
|
92
|
+
: base === wantBasename;
|
|
93
|
+
if (!matches)
|
|
94
|
+
continue;
|
|
95
|
+
if (!isDir && isSensitiveName(base))
|
|
96
|
+
continue;
|
|
97
|
+
out.push({
|
|
98
|
+
repositoryId: repo.id,
|
|
99
|
+
repositoryName: repo.name,
|
|
100
|
+
relativePath: e.path,
|
|
101
|
+
absolutePath: path.join(repo.root, e.path),
|
|
102
|
+
isDirectory: isDir,
|
|
103
|
+
matchType: isDir ? "directory" : hasSlash ? "exact_filename" : "basename",
|
|
104
|
+
confidence: "medium"
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (out.length >= MAX_CANDIDATES)
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Resolve one reference. See the module comment for the guarantees. The order
|
|
114
|
+
* is deterministic; a tie among repositories is returned as candidates.
|
|
115
|
+
*/
|
|
116
|
+
export function resolveWorkspaceFile(input) {
|
|
117
|
+
const reference = typeof input.reference === "string" ? input.reference.trim() : "";
|
|
118
|
+
const kind = input.kind ?? "either";
|
|
119
|
+
if (!reference)
|
|
120
|
+
return { status: "error", error: { code: "INVALID_REFERENCE", message: "A file reference is required." } };
|
|
121
|
+
if (isWildcardSelector(reference)) {
|
|
122
|
+
return { status: "error", error: { code: "INVALID_REFERENCE", message: 'A file reference cannot be "*".' } };
|
|
123
|
+
}
|
|
124
|
+
const trusted = trustedRepositories();
|
|
125
|
+
if (trusted.length === 0) {
|
|
126
|
+
return { status: "error", error: { code: "NO_TRUSTED_REPOSITORY", message: "No trusted repository to resolve against. Trust one first." } };
|
|
127
|
+
}
|
|
128
|
+
let active = null;
|
|
129
|
+
try {
|
|
130
|
+
active = input.activeRepository ? resolveRepository(input.activeRepository) : activeRepository();
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
active = activeRepository();
|
|
134
|
+
}
|
|
135
|
+
const isTrusted = (r) => repositoryTrust(r) === "trusted";
|
|
136
|
+
// 1. Repository-qualified: "api.carrieros.ai/src/routes/user.js".
|
|
137
|
+
const qualified = splitRepoQualified(reference);
|
|
138
|
+
if (qualified) {
|
|
139
|
+
if (!isTrusted(qualified.repo)) {
|
|
140
|
+
return { status: "error", error: { code: "REPOSITORY_NOT_TRUSTED", message: `${qualified.repo.name} is not a trusted repository.` } };
|
|
141
|
+
}
|
|
142
|
+
const hit = tryExact(qualified.repo, qualified.rest, kind);
|
|
143
|
+
if (hit)
|
|
144
|
+
return { status: "resolved", match: { ...hit, matchType: "repo_qualified", confidence: "high" } };
|
|
145
|
+
// A named repo whose path is absent is a miss, not a fall-through to fuzzy —
|
|
146
|
+
// the user was specific.
|
|
147
|
+
return { status: "not_found", error: { code: "NOT_FOUND", message: `${qualified.rest} is not in ${qualified.repo.name}.` } };
|
|
148
|
+
}
|
|
149
|
+
// 2. Absolute path that lands inside a registered, trusted repository.
|
|
150
|
+
if (path.isAbsolute(reference) || /^[a-zA-Z]:[\\/]/.test(reference) || reference.startsWith("\\\\")) {
|
|
151
|
+
const repo = repositoryForPath(reference);
|
|
152
|
+
if (!repo) {
|
|
153
|
+
return { status: "error", error: { code: "OUTSIDE_WORKSPACE", message: "That path is outside every registered repository." } };
|
|
154
|
+
}
|
|
155
|
+
if (!isTrusted(repo)) {
|
|
156
|
+
return { status: "error", error: { code: "REPOSITORY_NOT_TRUSTED", message: `${repo.name} is not a trusted repository.` } };
|
|
157
|
+
}
|
|
158
|
+
const rel = path.relative(repo.root, path.resolve(reference));
|
|
159
|
+
const hit = tryExact(repo, rel, kind);
|
|
160
|
+
if (hit)
|
|
161
|
+
return { status: "resolved", match: { ...hit, matchType: "absolute_workspace_path" } };
|
|
162
|
+
return { status: "not_found", error: { code: "NOT_FOUND", message: "No such file in that repository." } };
|
|
163
|
+
}
|
|
164
|
+
// A traversal reference is refused outright — never allowed to climb out.
|
|
165
|
+
if (reference.replace(/\\/g, "/").split("/").includes("..")) {
|
|
166
|
+
return { status: "error", error: { code: "OUTSIDE_WORKSPACE", message: "A reference may not contain '..'." } };
|
|
167
|
+
}
|
|
168
|
+
// 3. Exact path in the active repository.
|
|
169
|
+
if (active && isTrusted(active)) {
|
|
170
|
+
const hit = tryExact(active, reference, kind);
|
|
171
|
+
if (hit)
|
|
172
|
+
return { status: "resolved", match: hit };
|
|
173
|
+
}
|
|
174
|
+
// 4. Exact path in another trusted repository.
|
|
175
|
+
const others = trusted.filter((r) => r.id !== active?.id);
|
|
176
|
+
const exactElsewhere = [];
|
|
177
|
+
for (const repo of others) {
|
|
178
|
+
const hit = tryExact(repo, reference, kind);
|
|
179
|
+
if (hit)
|
|
180
|
+
exactElsewhere.push({ ...hit, matchType: "exact_path_other_repo" });
|
|
181
|
+
}
|
|
182
|
+
if (exactElsewhere.length === 1)
|
|
183
|
+
return { status: "resolved", match: exactElsewhere[0] };
|
|
184
|
+
if (exactElsewhere.length > 1)
|
|
185
|
+
return { status: "ambiguous", candidates: exactElsewhere };
|
|
186
|
+
// 5. Filename / basename / directory match across trusted repos (active first).
|
|
187
|
+
const ordered = active && isTrusted(active) ? [active, ...others] : trusted;
|
|
188
|
+
const found = searchByName(reference, kind, ordered);
|
|
189
|
+
if (found.length === 1)
|
|
190
|
+
return { status: "resolved", match: found[0] };
|
|
191
|
+
if (found.length > 1)
|
|
192
|
+
return { status: "ambiguous", candidates: found };
|
|
193
|
+
return {
|
|
194
|
+
status: "not_found",
|
|
195
|
+
error: { code: "NOT_FOUND", message: `Could not find "${reference}" in any trusted repository.` }
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/** Exported for the tool executor to keep error mapping in one place. */
|
|
199
|
+
export { LocalFileError, isInside };
|