@codelia/runtime 0.1.19 → 0.1.21
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/dist/index.cjs +371 -150
- package/dist/index.js +354 -133
- package/package.json +9 -9
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// src/runtime.ts
|
|
2
|
-
import
|
|
2
|
+
import path17 from "path";
|
|
3
3
|
import { SessionStateStoreImpl as SessionStateStoreImpl2 } from "@codelia/storage";
|
|
4
4
|
|
|
5
5
|
// src/agent-factory.ts
|
|
6
|
-
import { promises as
|
|
6
|
+
import { promises as fs15 } from "fs";
|
|
7
7
|
import {
|
|
8
8
|
Agent,
|
|
9
9
|
ChatAnthropic,
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
ChatOpenRouter,
|
|
12
12
|
DEFAULT_MODEL_REGISTRY as DEFAULT_MODEL_REGISTRY2
|
|
13
13
|
} from "@codelia/core";
|
|
14
|
+
import { parseApprovalMode as parseApprovalMode2 } from "@codelia/shared-types";
|
|
14
15
|
import { ToolOutputCacheStoreImpl } from "@codelia/storage";
|
|
15
16
|
|
|
16
17
|
// src/agents/context.ts
|
|
@@ -1864,7 +1865,7 @@ var resolveCdTarget = (segment, currentWorkingDir, rootDir) => {
|
|
|
1864
1865
|
var parseRawArgsForPrompt = (rawArgs) => parseRawArgsObject(rawArgs);
|
|
1865
1866
|
|
|
1866
1867
|
// src/permissions/service.ts
|
|
1867
|
-
var
|
|
1868
|
+
var SYSTEM_TOOL_ALLOWLIST_MINIMAL = [
|
|
1868
1869
|
"read",
|
|
1869
1870
|
"grep",
|
|
1870
1871
|
"glob_search",
|
|
@@ -1879,6 +1880,11 @@ var SYSTEM_TOOL_ALLOWLIST = [
|
|
|
1879
1880
|
"lane_status",
|
|
1880
1881
|
"done"
|
|
1881
1882
|
];
|
|
1883
|
+
var SYSTEM_TOOL_ALLOWLIST_TRUSTED = [
|
|
1884
|
+
...SYSTEM_TOOL_ALLOWLIST_MINIMAL,
|
|
1885
|
+
"write",
|
|
1886
|
+
"edit"
|
|
1887
|
+
];
|
|
1882
1888
|
var SYSTEM_BASH_ALLOWLIST = [
|
|
1883
1889
|
"pwd",
|
|
1884
1890
|
"ls",
|
|
@@ -1903,10 +1909,14 @@ var SYSTEM_BASH_ALLOWLIST = [
|
|
|
1903
1909
|
"git ls-files",
|
|
1904
1910
|
"git grep"
|
|
1905
1911
|
];
|
|
1906
|
-
var
|
|
1912
|
+
var SYSTEM_BASH_ALLOWLIST_TRUSTED = [...SYSTEM_BASH_ALLOWLIST, "sed", "awk"];
|
|
1913
|
+
var buildSystemPermissions = (approvalMode = "minimal") => ({
|
|
1907
1914
|
allow: [
|
|
1908
|
-
...
|
|
1909
|
-
...
|
|
1915
|
+
...(approvalMode === "trusted" ? SYSTEM_TOOL_ALLOWLIST_TRUSTED : SYSTEM_TOOL_ALLOWLIST_MINIMAL).map((tool) => ({ tool })),
|
|
1916
|
+
...approvalMode === "trusted" ? SYSTEM_BASH_ALLOWLIST_TRUSTED.map((command) => ({
|
|
1917
|
+
tool: "bash",
|
|
1918
|
+
command
|
|
1919
|
+
})) : SYSTEM_BASH_ALLOWLIST.map((command) => ({ tool: "bash", command }))
|
|
1910
1920
|
]
|
|
1911
1921
|
});
|
|
1912
1922
|
var mergePermissions = (configs) => {
|
|
@@ -1922,14 +1932,20 @@ var mergePermissions = (configs) => {
|
|
|
1922
1932
|
deny: deny.length ? deny : void 0
|
|
1923
1933
|
};
|
|
1924
1934
|
};
|
|
1935
|
+
var blockedByDeny = (subject) => ({
|
|
1936
|
+
decision: "deny",
|
|
1937
|
+
reason: `blocked by deny rule (${subject})`
|
|
1938
|
+
});
|
|
1925
1939
|
var PermissionService = class {
|
|
1926
1940
|
allowRules;
|
|
1927
1941
|
denyRules;
|
|
1928
1942
|
bashPathGuard;
|
|
1943
|
+
approvalMode;
|
|
1929
1944
|
constructor(options) {
|
|
1930
1945
|
const merged = mergePermissions([options.system, options.user]);
|
|
1931
1946
|
this.allowRules = merged.allow ?? [];
|
|
1932
1947
|
this.denyRules = merged.deny ?? [];
|
|
1948
|
+
this.approvalMode = options.approvalMode ?? "minimal";
|
|
1933
1949
|
if (options.bashPathGuard) {
|
|
1934
1950
|
this.bashPathGuard = {
|
|
1935
1951
|
rootDir: path4.resolve(options.bashPathGuard.rootDir),
|
|
@@ -1991,14 +2007,41 @@ var PermissionService = class {
|
|
|
1991
2007
|
return { title: "Run tool?", message: toolName };
|
|
1992
2008
|
}
|
|
1993
2009
|
evaluate(toolName, rawArgs) {
|
|
2010
|
+
if (this.approvalMode === "full-access") {
|
|
2011
|
+
return this.evaluateFullAccess(toolName, rawArgs);
|
|
2012
|
+
}
|
|
1994
2013
|
if (toolName === "bash") {
|
|
1995
2014
|
return this.evaluateBash(rawArgs);
|
|
1996
2015
|
}
|
|
1997
2016
|
return this.evaluateTool(toolName, rawArgs);
|
|
1998
2017
|
}
|
|
2018
|
+
evaluateFullAccess(toolName, rawArgs) {
|
|
2019
|
+
if (toolName === "bash") {
|
|
2020
|
+
return this.evaluateFullAccessBash(rawArgs);
|
|
2021
|
+
}
|
|
2022
|
+
if (this.denyRules.some((rule) => matchToolRule(rule, toolName, rawArgs))) {
|
|
2023
|
+
return blockedByDeny(toolName);
|
|
2024
|
+
}
|
|
2025
|
+
return { decision: "allow" };
|
|
2026
|
+
}
|
|
2027
|
+
evaluateFullAccessBash(rawArgs) {
|
|
2028
|
+
const command = extractCommand(rawArgs);
|
|
2029
|
+
if (!command) return { decision: "allow" };
|
|
2030
|
+
const normalized = normalizeCommand(command);
|
|
2031
|
+
if (!normalized) return { decision: "allow" };
|
|
2032
|
+
if (this.denyRules.some((rule) => matchFullCommandRule(rule, normalized))) {
|
|
2033
|
+
return blockedByDeny(normalized);
|
|
2034
|
+
}
|
|
2035
|
+
for (const segment of splitCommandSegments(normalized)) {
|
|
2036
|
+
if (this.denyRules.some((rule) => matchBashRule(rule, segment))) {
|
|
2037
|
+
return blockedByDeny(segment);
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
return { decision: "allow" };
|
|
2041
|
+
}
|
|
1999
2042
|
evaluateTool(toolName, rawArgs) {
|
|
2000
2043
|
if (this.denyRules.some((rule) => matchToolRule(rule, toolName, rawArgs))) {
|
|
2001
|
-
return
|
|
2044
|
+
return blockedByDeny(toolName);
|
|
2002
2045
|
}
|
|
2003
2046
|
if (this.allowRules.some((rule) => matchToolRule(rule, toolName, rawArgs))) {
|
|
2004
2047
|
return { decision: "allow" };
|
|
@@ -2015,10 +2058,7 @@ var PermissionService = class {
|
|
|
2015
2058
|
return { decision: "confirm", reason: "empty command" };
|
|
2016
2059
|
}
|
|
2017
2060
|
if (this.denyRules.some((rule) => matchFullCommandRule(rule, normalized))) {
|
|
2018
|
-
return
|
|
2019
|
-
decision: "deny",
|
|
2020
|
-
reason: `blocked by deny rule (${normalized})`
|
|
2021
|
-
};
|
|
2061
|
+
return blockedByDeny(normalized);
|
|
2022
2062
|
}
|
|
2023
2063
|
const segments = splitCommandSegments(normalized);
|
|
2024
2064
|
if (!segments.length) {
|
|
@@ -2029,10 +2069,7 @@ var PermissionService = class {
|
|
|
2029
2069
|
}
|
|
2030
2070
|
for (const segment of segments) {
|
|
2031
2071
|
if (this.denyRules.some((rule) => matchBashRule(rule, segment))) {
|
|
2032
|
-
return
|
|
2033
|
-
decision: "deny",
|
|
2034
|
-
reason: `blocked by deny rule (${segment})`
|
|
2035
|
-
};
|
|
2072
|
+
return blockedByDeny(segment);
|
|
2036
2073
|
}
|
|
2037
2074
|
}
|
|
2038
2075
|
let currentWorkingDir = this.bashPathGuard?.workingDir;
|
|
@@ -2128,10 +2165,149 @@ ${previewLines.join("\n")}`;
|
|
|
2128
2165
|
}
|
|
2129
2166
|
};
|
|
2130
2167
|
|
|
2131
|
-
// src/
|
|
2132
|
-
import crypto from "crypto";
|
|
2168
|
+
// src/permissions/approval-mode.ts
|
|
2133
2169
|
import { promises as fs4 } from "fs";
|
|
2134
2170
|
import path5 from "path";
|
|
2171
|
+
import { parseApprovalMode } from "@codelia/shared-types";
|
|
2172
|
+
import { ProjectsPolicyStore } from "@codelia/storage";
|
|
2173
|
+
var normalizePath = async (value) => {
|
|
2174
|
+
const absolute = path5.resolve(value);
|
|
2175
|
+
try {
|
|
2176
|
+
return await fs4.realpath(absolute);
|
|
2177
|
+
} catch {
|
|
2178
|
+
return absolute;
|
|
2179
|
+
}
|
|
2180
|
+
};
|
|
2181
|
+
var resolveProjectPolicyKey = async (workingDir, runtimeSandboxRoot) => {
|
|
2182
|
+
const basePath = runtimeSandboxRoot ?? workingDir;
|
|
2183
|
+
return normalizePath(basePath);
|
|
2184
|
+
};
|
|
2185
|
+
var APPROVAL_MODE_HINT = "minimal|trusted|full-access";
|
|
2186
|
+
var parseApprovalModeOrThrow = (raw, source) => {
|
|
2187
|
+
if (!raw || raw.trim().length === 0) {
|
|
2188
|
+
throw new Error(
|
|
2189
|
+
`Invalid ${source}: missing value (expected ${APPROVAL_MODE_HINT})`
|
|
2190
|
+
);
|
|
2191
|
+
}
|
|
2192
|
+
const parsed = parseApprovalMode(raw);
|
|
2193
|
+
if (!parsed) {
|
|
2194
|
+
throw new Error(
|
|
2195
|
+
`Invalid ${source}: '${raw}' (expected ${APPROVAL_MODE_HINT})`
|
|
2196
|
+
);
|
|
2197
|
+
}
|
|
2198
|
+
return parsed;
|
|
2199
|
+
};
|
|
2200
|
+
var resolveCliApprovalModeOrThrow = (args) => {
|
|
2201
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
2202
|
+
const current = args[i];
|
|
2203
|
+
if (!current.startsWith("--approval-mode")) continue;
|
|
2204
|
+
if (current === "--approval-mode") {
|
|
2205
|
+
return parseApprovalModeOrThrow(args[i + 1], "--approval-mode");
|
|
2206
|
+
}
|
|
2207
|
+
if (current.startsWith("--approval-mode=")) {
|
|
2208
|
+
return parseApprovalModeOrThrow(
|
|
2209
|
+
current.slice("--approval-mode=".length),
|
|
2210
|
+
"--approval-mode"
|
|
2211
|
+
);
|
|
2212
|
+
}
|
|
2213
|
+
}
|
|
2214
|
+
return void 0;
|
|
2215
|
+
};
|
|
2216
|
+
var resolveEnvApprovalModeOrThrow = (env) => {
|
|
2217
|
+
if (env.CODELIA_APPROVAL_MODE === void 0) {
|
|
2218
|
+
return void 0;
|
|
2219
|
+
}
|
|
2220
|
+
return parseApprovalModeOrThrow(
|
|
2221
|
+
env.CODELIA_APPROVAL_MODE,
|
|
2222
|
+
"CODELIA_APPROVAL_MODE"
|
|
2223
|
+
);
|
|
2224
|
+
};
|
|
2225
|
+
var resolvePolicyApprovalMode = (policy, projectKey) => {
|
|
2226
|
+
if (!policy) {
|
|
2227
|
+
return void 0;
|
|
2228
|
+
}
|
|
2229
|
+
const projectMode = parseApprovalMode(
|
|
2230
|
+
policy.projects?.[projectKey]?.approval_mode
|
|
2231
|
+
);
|
|
2232
|
+
if (projectMode) {
|
|
2233
|
+
return { approvalMode: projectMode, source: "project" };
|
|
2234
|
+
}
|
|
2235
|
+
const defaultMode = parseApprovalMode(policy.default?.approval_mode);
|
|
2236
|
+
if (defaultMode) {
|
|
2237
|
+
return { approvalMode: defaultMode, source: "default" };
|
|
2238
|
+
}
|
|
2239
|
+
return void 0;
|
|
2240
|
+
};
|
|
2241
|
+
var loadProjectsPolicyOrThrow = async (store) => {
|
|
2242
|
+
try {
|
|
2243
|
+
return await store.load();
|
|
2244
|
+
} catch (error) {
|
|
2245
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
2246
|
+
throw new Error(
|
|
2247
|
+
`Failed to load approval mode policy (${store.getFilePath()}): ${detail}`
|
|
2248
|
+
);
|
|
2249
|
+
}
|
|
2250
|
+
};
|
|
2251
|
+
var persistProjectApprovalModeOrThrow = async (store, projectKey, approvalMode, basePolicy) => {
|
|
2252
|
+
const policy = basePolicy ?? { version: 1 };
|
|
2253
|
+
const nextProjects = {
|
|
2254
|
+
...policy.projects ?? {},
|
|
2255
|
+
[projectKey]: {
|
|
2256
|
+
approval_mode: approvalMode
|
|
2257
|
+
}
|
|
2258
|
+
};
|
|
2259
|
+
await store.save({
|
|
2260
|
+
...policy,
|
|
2261
|
+
version: 1,
|
|
2262
|
+
projects: nextProjects
|
|
2263
|
+
});
|
|
2264
|
+
};
|
|
2265
|
+
var resolveApprovalModeForRuntime = async (options, deps = {}) => {
|
|
2266
|
+
const projectKey = await resolveProjectPolicyKey(
|
|
2267
|
+
options.workingDir,
|
|
2268
|
+
options.runtimeSandboxRoot
|
|
2269
|
+
);
|
|
2270
|
+
const cliMode = resolveCliApprovalModeOrThrow(process.argv.slice(2));
|
|
2271
|
+
if (cliMode) {
|
|
2272
|
+
return { approvalMode: cliMode, source: "cli", projectKey };
|
|
2273
|
+
}
|
|
2274
|
+
const envMode = resolveEnvApprovalModeOrThrow(process.env);
|
|
2275
|
+
if (envMode) {
|
|
2276
|
+
return { approvalMode: envMode, source: "env", projectKey };
|
|
2277
|
+
}
|
|
2278
|
+
const store = deps.store ?? new ProjectsPolicyStore();
|
|
2279
|
+
const loadedPolicy = await loadProjectsPolicyOrThrow(store);
|
|
2280
|
+
const policyMode = resolvePolicyApprovalMode(loadedPolicy, projectKey);
|
|
2281
|
+
if (policyMode) {
|
|
2282
|
+
return {
|
|
2283
|
+
approvalMode: policyMode.approvalMode,
|
|
2284
|
+
source: policyMode.source,
|
|
2285
|
+
projectKey
|
|
2286
|
+
};
|
|
2287
|
+
}
|
|
2288
|
+
if (options.requestStartupSelection) {
|
|
2289
|
+
const selected = await options.requestStartupSelection({ projectKey });
|
|
2290
|
+
if (selected) {
|
|
2291
|
+
await persistProjectApprovalModeOrThrow(
|
|
2292
|
+
store,
|
|
2293
|
+
projectKey,
|
|
2294
|
+
selected,
|
|
2295
|
+
loadedPolicy
|
|
2296
|
+
);
|
|
2297
|
+
return {
|
|
2298
|
+
approvalMode: selected,
|
|
2299
|
+
source: "startup-selection",
|
|
2300
|
+
projectKey
|
|
2301
|
+
};
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
return { approvalMode: "minimal", source: "fallback", projectKey };
|
|
2305
|
+
};
|
|
2306
|
+
|
|
2307
|
+
// src/sandbox/context.ts
|
|
2308
|
+
import crypto from "crypto";
|
|
2309
|
+
import { promises as fs5 } from "fs";
|
|
2310
|
+
import path6 from "path";
|
|
2135
2311
|
var SecurityError = class extends Error {
|
|
2136
2312
|
constructor(message) {
|
|
2137
2313
|
super(message);
|
|
@@ -2149,8 +2325,8 @@ var SandboxContext = class _SandboxContext {
|
|
|
2149
2325
|
}
|
|
2150
2326
|
static async create(rootDir) {
|
|
2151
2327
|
const sessionId = crypto.randomUUID().slice(0, 8);
|
|
2152
|
-
const root = rootDir ?
|
|
2153
|
-
await
|
|
2328
|
+
const root = rootDir ? path6.resolve(rootDir) : process.cwd();
|
|
2329
|
+
await fs5.mkdir(root, { recursive: true });
|
|
2154
2330
|
return new _SandboxContext({
|
|
2155
2331
|
rootDir: root,
|
|
2156
2332
|
workingDir: root,
|
|
@@ -2158,9 +2334,9 @@ var SandboxContext = class _SandboxContext {
|
|
|
2158
2334
|
});
|
|
2159
2335
|
}
|
|
2160
2336
|
resolvePath(targetPath) {
|
|
2161
|
-
const resolved =
|
|
2162
|
-
const relative =
|
|
2163
|
-
if (relative === "" || !relative.startsWith("..") && !
|
|
2337
|
+
const resolved = path6.isAbsolute(targetPath) ? path6.resolve(targetPath) : path6.resolve(this.workingDir, targetPath);
|
|
2338
|
+
const relative = path6.relative(this.rootDir, resolved);
|
|
2339
|
+
if (relative === "" || !relative.startsWith("..") && !path6.isAbsolute(relative)) {
|
|
2164
2340
|
return resolved;
|
|
2165
2341
|
}
|
|
2166
2342
|
throw new SecurityError(
|
|
@@ -2183,9 +2359,9 @@ var getSkillsResolver = async (ctx, key) => ctx.resolve(key);
|
|
|
2183
2359
|
|
|
2184
2360
|
// src/skills/resolver.ts
|
|
2185
2361
|
import crypto2 from "crypto";
|
|
2186
|
-
import { promises as
|
|
2362
|
+
import { promises as fs6 } from "fs";
|
|
2187
2363
|
import os from "os";
|
|
2188
|
-
import
|
|
2364
|
+
import path7 from "path";
|
|
2189
2365
|
var DEFAULT_MARKERS2 = [".codelia", ".git", ".jj"];
|
|
2190
2366
|
var SKILL_NAME_PATTERN2 = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
2191
2367
|
var MAX_SKILL_FILES = 200;
|
|
@@ -2203,7 +2379,7 @@ var parseMarkers2 = (value) => {
|
|
|
2203
2379
|
};
|
|
2204
2380
|
var exists2 = async (targetPath) => {
|
|
2205
2381
|
try {
|
|
2206
|
-
await
|
|
2382
|
+
await fs6.access(targetPath);
|
|
2207
2383
|
return true;
|
|
2208
2384
|
} catch {
|
|
2209
2385
|
return false;
|
|
@@ -2211,47 +2387,47 @@ var exists2 = async (targetPath) => {
|
|
|
2211
2387
|
};
|
|
2212
2388
|
var normalizeMtime2 = (value) => Number.isFinite(value) && value >= 0 ? Math.trunc(value) : 0;
|
|
2213
2389
|
var resolvePathChain2 = (rootDir, targetDir) => {
|
|
2214
|
-
const normalizedRoot =
|
|
2215
|
-
const normalizedTarget =
|
|
2216
|
-
const relative =
|
|
2217
|
-
if (relative.startsWith("..") ||
|
|
2390
|
+
const normalizedRoot = path7.resolve(rootDir);
|
|
2391
|
+
const normalizedTarget = path7.resolve(targetDir);
|
|
2392
|
+
const relative = path7.relative(normalizedRoot, normalizedTarget);
|
|
2393
|
+
if (relative.startsWith("..") || path7.isAbsolute(relative) || relative === "..") {
|
|
2218
2394
|
return [];
|
|
2219
2395
|
}
|
|
2220
|
-
const parts = relative.split(
|
|
2396
|
+
const parts = relative.split(path7.sep).filter((part) => part.length > 0);
|
|
2221
2397
|
const chain = [normalizedRoot];
|
|
2222
2398
|
let current = normalizedRoot;
|
|
2223
2399
|
for (const part of parts) {
|
|
2224
|
-
current =
|
|
2400
|
+
current = path7.join(current, part);
|
|
2225
2401
|
chain.push(current);
|
|
2226
2402
|
}
|
|
2227
2403
|
return chain;
|
|
2228
2404
|
};
|
|
2229
2405
|
var findRootByMarkers2 = async (workingDir, markers) => {
|
|
2230
|
-
let current =
|
|
2406
|
+
let current = path7.resolve(workingDir);
|
|
2231
2407
|
while (true) {
|
|
2232
2408
|
for (const marker of markers) {
|
|
2233
|
-
if (await exists2(
|
|
2409
|
+
if (await exists2(path7.join(current, marker))) {
|
|
2234
2410
|
return current;
|
|
2235
2411
|
}
|
|
2236
2412
|
}
|
|
2237
|
-
const parent =
|
|
2413
|
+
const parent = path7.dirname(current);
|
|
2238
2414
|
if (parent === current) {
|
|
2239
|
-
return
|
|
2415
|
+
return path7.resolve(workingDir);
|
|
2240
2416
|
}
|
|
2241
2417
|
current = parent;
|
|
2242
2418
|
}
|
|
2243
2419
|
};
|
|
2244
2420
|
var tryResolvePath = async (targetPath) => {
|
|
2245
|
-
const resolved =
|
|
2421
|
+
const resolved = path7.resolve(targetPath);
|
|
2246
2422
|
try {
|
|
2247
|
-
return { resolved, canonical: await
|
|
2423
|
+
return { resolved, canonical: await fs6.realpath(resolved) };
|
|
2248
2424
|
} catch {
|
|
2249
2425
|
return { resolved, canonical: resolved };
|
|
2250
2426
|
}
|
|
2251
2427
|
};
|
|
2252
2428
|
var isWithin = (basePath, candidatePath) => {
|
|
2253
|
-
const relative =
|
|
2254
|
-
return relative === "" || !relative.startsWith("..") && !
|
|
2429
|
+
const relative = path7.relative(basePath, candidatePath);
|
|
2430
|
+
return relative === "" || !relative.startsWith("..") && !path7.isAbsolute(relative);
|
|
2255
2431
|
};
|
|
2256
2432
|
var hashPath = (value) => crypto2.createHash("sha256").update(value).digest("hex").slice(0, 24);
|
|
2257
2433
|
var parseFrontmatter = (content) => {
|
|
@@ -2300,12 +2476,12 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2300
2476
|
}
|
|
2301
2477
|
static async create(options) {
|
|
2302
2478
|
const env = options.env ?? process.env;
|
|
2303
|
-
const workingDir =
|
|
2479
|
+
const workingDir = path7.resolve(options.workingDir);
|
|
2304
2480
|
const rootOverride = toTrimmed2(env.CODELIA_AGENTS_ROOT);
|
|
2305
2481
|
const markers = parseMarkers2(env.CODELIA_AGENTS_MARKERS) ?? DEFAULT_MARKERS2;
|
|
2306
|
-
const rootDir = rootOverride ?
|
|
2482
|
+
const rootDir = rootOverride ? path7.resolve(rootOverride) : await findRootByMarkers2(workingDir, markers);
|
|
2307
2483
|
const homeDir = toTrimmed2(env.HOME) ?? os.homedir();
|
|
2308
|
-
const userSkillsDir =
|
|
2484
|
+
const userSkillsDir = path7.join(path7.resolve(homeDir), ".agents", "skills");
|
|
2309
2485
|
const resolver = new _SkillsResolver({
|
|
2310
2486
|
workingDir,
|
|
2311
2487
|
rootDir,
|
|
@@ -2320,7 +2496,7 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2320
2496
|
const walk = async (dirPath) => {
|
|
2321
2497
|
let entries;
|
|
2322
2498
|
try {
|
|
2323
|
-
entries = await
|
|
2499
|
+
entries = await fs6.readdir(dirPath, {
|
|
2324
2500
|
withFileTypes: true,
|
|
2325
2501
|
encoding: "utf8"
|
|
2326
2502
|
});
|
|
@@ -2328,7 +2504,7 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2328
2504
|
return;
|
|
2329
2505
|
}
|
|
2330
2506
|
for (const entry of entries) {
|
|
2331
|
-
const entryPath =
|
|
2507
|
+
const entryPath = path7.join(dirPath, entry.name);
|
|
2332
2508
|
if (entry.isDirectory()) {
|
|
2333
2509
|
await walk(entryPath);
|
|
2334
2510
|
continue;
|
|
@@ -2345,7 +2521,7 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2345
2521
|
const directories = resolvePathChain2(this.rootDir, this.workingDir);
|
|
2346
2522
|
const skillFiles = [];
|
|
2347
2523
|
for (const directoryPath of directories) {
|
|
2348
|
-
const skillsRoot =
|
|
2524
|
+
const skillsRoot = path7.join(directoryPath, ".agents", "skills");
|
|
2349
2525
|
if (!await exists2(skillsRoot)) continue;
|
|
2350
2526
|
const files = await this.collectSkillFilesInTree(skillsRoot);
|
|
2351
2527
|
skillFiles.push(...files);
|
|
@@ -2356,7 +2532,7 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2356
2532
|
const normalized = await tryResolvePath(rawPath);
|
|
2357
2533
|
let stat;
|
|
2358
2534
|
try {
|
|
2359
|
-
stat = await
|
|
2535
|
+
stat = await fs6.stat(normalized.canonical);
|
|
2360
2536
|
} catch (error) {
|
|
2361
2537
|
return {
|
|
2362
2538
|
error: {
|
|
@@ -2375,7 +2551,7 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2375
2551
|
}
|
|
2376
2552
|
let content;
|
|
2377
2553
|
try {
|
|
2378
|
-
content = await
|
|
2554
|
+
content = await fs6.readFile(normalized.canonical, "utf8");
|
|
2379
2555
|
} catch (error) {
|
|
2380
2556
|
return {
|
|
2381
2557
|
error: {
|
|
@@ -2402,8 +2578,8 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2402
2578
|
}
|
|
2403
2579
|
};
|
|
2404
2580
|
}
|
|
2405
|
-
const dir =
|
|
2406
|
-
if (
|
|
2581
|
+
const dir = path7.dirname(normalized.canonical);
|
|
2582
|
+
if (path7.basename(dir) !== name) {
|
|
2407
2583
|
return {
|
|
2408
2584
|
error: {
|
|
2409
2585
|
path: normalized.canonical,
|
|
@@ -2552,7 +2728,7 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2552
2728
|
}
|
|
2553
2729
|
const catalog = await this.getCatalog();
|
|
2554
2730
|
const normalizedQuery = query.toLowerCase();
|
|
2555
|
-
const queryPathCandidate =
|
|
2731
|
+
const queryPathCandidate = path7.resolve(this.workingDir, query);
|
|
2556
2732
|
const queryTokens = new Set(tokenize(normalizedQuery));
|
|
2557
2733
|
const scored = [];
|
|
2558
2734
|
for (const skill of catalog.skills) {
|
|
@@ -2608,9 +2784,9 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2608
2784
|
async listSkillFiles(skillDir) {
|
|
2609
2785
|
let rootReal;
|
|
2610
2786
|
try {
|
|
2611
|
-
rootReal = await
|
|
2787
|
+
rootReal = await fs6.realpath(skillDir);
|
|
2612
2788
|
} catch {
|
|
2613
|
-
rootReal =
|
|
2789
|
+
rootReal = path7.resolve(skillDir);
|
|
2614
2790
|
}
|
|
2615
2791
|
const files = [];
|
|
2616
2792
|
let bytes = 0;
|
|
@@ -2619,7 +2795,7 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2619
2795
|
if (truncated) return;
|
|
2620
2796
|
let entries;
|
|
2621
2797
|
try {
|
|
2622
|
-
entries = await
|
|
2798
|
+
entries = await fs6.readdir(dirPath, {
|
|
2623
2799
|
withFileTypes: true,
|
|
2624
2800
|
encoding: "utf8"
|
|
2625
2801
|
});
|
|
@@ -2628,7 +2804,7 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2628
2804
|
}
|
|
2629
2805
|
for (const entry of entries) {
|
|
2630
2806
|
if (truncated) break;
|
|
2631
|
-
const entryPath =
|
|
2807
|
+
const entryPath = path7.join(dirPath, entry.name);
|
|
2632
2808
|
if (entry.isSymbolicLink()) {
|
|
2633
2809
|
continue;
|
|
2634
2810
|
}
|
|
@@ -2641,16 +2817,16 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2641
2817
|
}
|
|
2642
2818
|
let realPath;
|
|
2643
2819
|
try {
|
|
2644
|
-
realPath = await
|
|
2820
|
+
realPath = await fs6.realpath(entryPath);
|
|
2645
2821
|
} catch {
|
|
2646
|
-
realPath =
|
|
2822
|
+
realPath = path7.resolve(entryPath);
|
|
2647
2823
|
}
|
|
2648
2824
|
if (!isWithin(rootReal, realPath)) {
|
|
2649
2825
|
continue;
|
|
2650
2826
|
}
|
|
2651
2827
|
let stat;
|
|
2652
2828
|
try {
|
|
2653
|
-
stat = await
|
|
2829
|
+
stat = await fs6.stat(realPath);
|
|
2654
2830
|
} catch {
|
|
2655
2831
|
continue;
|
|
2656
2832
|
}
|
|
@@ -2672,7 +2848,7 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2672
2848
|
return { files, truncated };
|
|
2673
2849
|
}
|
|
2674
2850
|
async resolveByPath(pathInput) {
|
|
2675
|
-
const resolved =
|
|
2851
|
+
const resolved = path7.isAbsolute(pathInput) ? path7.resolve(pathInput) : path7.resolve(this.workingDir, pathInput);
|
|
2676
2852
|
const resolvedTry = await tryResolvePath(resolved);
|
|
2677
2853
|
const byCanonical = this.catalog.skills.find(
|
|
2678
2854
|
(entry) => entry.path === resolvedTry.canonical
|
|
@@ -2718,7 +2894,7 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2718
2894
|
}
|
|
2719
2895
|
let stat;
|
|
2720
2896
|
try {
|
|
2721
|
-
stat = await
|
|
2897
|
+
stat = await fs6.stat(selected.path);
|
|
2722
2898
|
} catch (error) {
|
|
2723
2899
|
return { ok: false, message: `skill file stat failed: ${String(error)}` };
|
|
2724
2900
|
}
|
|
@@ -2736,13 +2912,13 @@ var SkillsResolver = class _SkillsResolver {
|
|
|
2736
2912
|
}
|
|
2737
2913
|
let skillBody;
|
|
2738
2914
|
try {
|
|
2739
|
-
skillBody = await
|
|
2915
|
+
skillBody = await fs6.readFile(selected.path, "utf8");
|
|
2740
2916
|
} catch (error) {
|
|
2741
2917
|
return { ok: false, message: `skill file read failed: ${String(error)}` };
|
|
2742
2918
|
}
|
|
2743
2919
|
const list = await this.listSkillFiles(selected.dir);
|
|
2744
2920
|
const filesXml = list.files.map((entry) => `<file>${entry}</file>`).join("\n");
|
|
2745
|
-
const trailingSlashDir = selected.dir.endsWith(
|
|
2921
|
+
const trailingSlashDir = selected.dir.endsWith(path7.sep) ? selected.dir : `${selected.dir}${path7.sep}`;
|
|
2746
2922
|
const content = [
|
|
2747
2923
|
`<skill_context name="${xmlEscape(selected.name)}" path="${xmlEscape(selected.path)}">`,
|
|
2748
2924
|
skillBody.trimEnd(),
|
|
@@ -3044,8 +3220,8 @@ var createDoneTool = () => defineTool3({
|
|
|
3044
3220
|
|
|
3045
3221
|
// src/tools/edit.ts
|
|
3046
3222
|
import crypto3 from "crypto";
|
|
3047
|
-
import { promises as
|
|
3048
|
-
import
|
|
3223
|
+
import { promises as fs7 } from "fs";
|
|
3224
|
+
import path8 from "path";
|
|
3049
3225
|
import { defineTool as defineTool4 } from "@codelia/core";
|
|
3050
3226
|
import { z as z5 } from "zod";
|
|
3051
3227
|
|
|
@@ -3248,12 +3424,12 @@ var createEditTool = (sandboxKey) => defineTool4({
|
|
|
3248
3424
|
let content = "";
|
|
3249
3425
|
let fileExists = false;
|
|
3250
3426
|
try {
|
|
3251
|
-
const stat = await
|
|
3427
|
+
const stat = await fs7.stat(resolved);
|
|
3252
3428
|
if (stat.isDirectory()) {
|
|
3253
3429
|
return `Path is a directory: ${input.file_path}`;
|
|
3254
3430
|
}
|
|
3255
3431
|
fileExists = true;
|
|
3256
|
-
content = await
|
|
3432
|
+
content = await fs7.readFile(resolved, "utf8");
|
|
3257
3433
|
} catch (_error) {
|
|
3258
3434
|
if (input.old_string !== "") {
|
|
3259
3435
|
return `File not found: ${input.file_path}`;
|
|
@@ -3317,8 +3493,8 @@ var createEditTool = (sandboxKey) => defineTool4({
|
|
|
3317
3493
|
};
|
|
3318
3494
|
}
|
|
3319
3495
|
try {
|
|
3320
|
-
await
|
|
3321
|
-
await
|
|
3496
|
+
await fs7.mkdir(path8.dirname(resolved), { recursive: true });
|
|
3497
|
+
await fs7.writeFile(resolved, nextContent, "utf8");
|
|
3322
3498
|
} catch (error) {
|
|
3323
3499
|
return `Error editing file: ${String(error)}`;
|
|
3324
3500
|
}
|
|
@@ -3333,13 +3509,13 @@ var createEditTool = (sandboxKey) => defineTool4({
|
|
|
3333
3509
|
});
|
|
3334
3510
|
|
|
3335
3511
|
// src/tools/glob-search.ts
|
|
3336
|
-
import { promises as
|
|
3512
|
+
import { promises as fs9 } from "fs";
|
|
3337
3513
|
import { defineTool as defineTool5 } from "@codelia/core";
|
|
3338
3514
|
import { z as z6 } from "zod";
|
|
3339
3515
|
|
|
3340
3516
|
// src/utils/glob.ts
|
|
3341
|
-
import { promises as
|
|
3342
|
-
import
|
|
3517
|
+
import { promises as fs8 } from "fs";
|
|
3518
|
+
import path9 from "path";
|
|
3343
3519
|
var globToRegExp2 = (pattern) => {
|
|
3344
3520
|
const normalized = pattern.replaceAll("\\", "/");
|
|
3345
3521
|
const globDirToken = "__GLOBSTAR_DIR__";
|
|
@@ -3352,9 +3528,9 @@ var globToRegExp2 = (pattern) => {
|
|
|
3352
3528
|
return new RegExp(`^${withGlob}$`);
|
|
3353
3529
|
};
|
|
3354
3530
|
var walkFiles = async (startDir, visitor) => {
|
|
3355
|
-
const entries = await
|
|
3531
|
+
const entries = await fs8.readdir(startDir, { withFileTypes: true });
|
|
3356
3532
|
for (const entry of entries) {
|
|
3357
|
-
const fullPath =
|
|
3533
|
+
const fullPath = path9.join(startDir, entry.name);
|
|
3358
3534
|
if (entry.isDirectory()) {
|
|
3359
3535
|
await walkFiles(fullPath, visitor);
|
|
3360
3536
|
} else if (entry.isFile()) {
|
|
@@ -3367,7 +3543,7 @@ var globMatch = async (searchDir, rootDir, pattern) => {
|
|
|
3367
3543
|
const regex = globToRegExp2(pattern.replaceAll("\\", "/"));
|
|
3368
3544
|
const matches = [];
|
|
3369
3545
|
await walkFiles(searchDir, async (filePath) => {
|
|
3370
|
-
const relPath =
|
|
3546
|
+
const relPath = path9.relative(rootDir, filePath).replaceAll("\\", "/");
|
|
3371
3547
|
if (regex.test(relPath)) {
|
|
3372
3548
|
matches.push(relPath);
|
|
3373
3549
|
}
|
|
@@ -3397,7 +3573,7 @@ var createGlobSearchTool = (sandboxKey) => defineTool5({
|
|
|
3397
3573
|
return `Security error: ${String(error)}`;
|
|
3398
3574
|
}
|
|
3399
3575
|
try {
|
|
3400
|
-
const stat = await
|
|
3576
|
+
const stat = await fs9.stat(searchDir);
|
|
3401
3577
|
if (!stat.isDirectory()) {
|
|
3402
3578
|
return `Path is not a directory: ${input.path}`;
|
|
3403
3579
|
}
|
|
@@ -3415,8 +3591,8 @@ ${limited.join("\n")}`;
|
|
|
3415
3591
|
});
|
|
3416
3592
|
|
|
3417
3593
|
// src/tools/grep.ts
|
|
3418
|
-
import { promises as
|
|
3419
|
-
import
|
|
3594
|
+
import { promises as fs10 } from "fs";
|
|
3595
|
+
import path10 from "path";
|
|
3420
3596
|
import { defineTool as defineTool6 } from "@codelia/core";
|
|
3421
3597
|
import { z as z7 } from "zod";
|
|
3422
3598
|
var createGrepTool = (sandboxKey) => defineTool6({
|
|
@@ -3450,11 +3626,11 @@ var createGrepTool = (sandboxKey) => defineTool6({
|
|
|
3450
3626
|
const searchFile = async (filePath) => {
|
|
3451
3627
|
if (results.length >= 50) return false;
|
|
3452
3628
|
try {
|
|
3453
|
-
const content = await
|
|
3629
|
+
const content = await fs10.readFile(filePath, "utf8");
|
|
3454
3630
|
const lines = content.split(/\r?\n/);
|
|
3455
3631
|
for (let i = 0; i < lines.length; i++) {
|
|
3456
3632
|
if (regex.test(lines[i])) {
|
|
3457
|
-
const relPath =
|
|
3633
|
+
const relPath = path10.relative(rootDir, filePath).replaceAll("\\", "/");
|
|
3458
3634
|
results.push(`${relPath}:${i + 1}: ${lines[i].slice(0, 100)}`);
|
|
3459
3635
|
if (results.length >= 50) return false;
|
|
3460
3636
|
}
|
|
@@ -3465,7 +3641,7 @@ var createGrepTool = (sandboxKey) => defineTool6({
|
|
|
3465
3641
|
return true;
|
|
3466
3642
|
};
|
|
3467
3643
|
try {
|
|
3468
|
-
const stats = await
|
|
3644
|
+
const stats = await fs10.stat(searchDir);
|
|
3469
3645
|
if (stats.isFile()) {
|
|
3470
3646
|
await searchFile(searchDir);
|
|
3471
3647
|
} else if (stats.isDirectory()) {
|
|
@@ -3490,9 +3666,9 @@ import { z as z8 } from "zod";
|
|
|
3490
3666
|
|
|
3491
3667
|
// src/lanes/manager.ts
|
|
3492
3668
|
import crypto4 from "crypto";
|
|
3493
|
-
import { promises as
|
|
3669
|
+
import { promises as fs12 } from "fs";
|
|
3494
3670
|
import os2 from "os";
|
|
3495
|
-
import
|
|
3671
|
+
import path12 from "path";
|
|
3496
3672
|
|
|
3497
3673
|
// src/lanes/command.ts
|
|
3498
3674
|
import { spawn as spawn3 } from "child_process";
|
|
@@ -3574,8 +3750,8 @@ ${stdout}`.trim();
|
|
|
3574
3750
|
};
|
|
3575
3751
|
|
|
3576
3752
|
// src/lanes/registry.ts
|
|
3577
|
-
import { promises as
|
|
3578
|
-
import
|
|
3753
|
+
import { promises as fs11 } from "fs";
|
|
3754
|
+
import path11 from "path";
|
|
3579
3755
|
import { resolveStoragePaths as resolveStoragePaths2 } from "@codelia/storage";
|
|
3580
3756
|
var REGISTRY_DIRNAME = "lanes";
|
|
3581
3757
|
var REGISTRY_FILENAME = "registry.json";
|
|
@@ -3583,7 +3759,7 @@ var nowIso = () => (/* @__PURE__ */ new Date()).toISOString();
|
|
|
3583
3759
|
var sortByUpdatedDesc = (lanes) => [...lanes].sort((a, b) => b.updated_at.localeCompare(a.updated_at));
|
|
3584
3760
|
var readJsonFile = async (filePath) => {
|
|
3585
3761
|
try {
|
|
3586
|
-
const raw = await
|
|
3762
|
+
const raw = await fs11.readFile(filePath, "utf8");
|
|
3587
3763
|
const parsed = JSON.parse(raw);
|
|
3588
3764
|
if (!parsed || parsed.version !== 1 || !Array.isArray(parsed.lanes)) {
|
|
3589
3765
|
return null;
|
|
@@ -3597,14 +3773,14 @@ var readJsonFile = async (filePath) => {
|
|
|
3597
3773
|
}
|
|
3598
3774
|
};
|
|
3599
3775
|
var atomicWrite = async (filePath, payload) => {
|
|
3600
|
-
const dir =
|
|
3601
|
-
const base =
|
|
3602
|
-
const tmp =
|
|
3776
|
+
const dir = path11.dirname(filePath);
|
|
3777
|
+
const base = path11.basename(filePath);
|
|
3778
|
+
const tmp = path11.join(
|
|
3603
3779
|
dir,
|
|
3604
3780
|
`${base}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`
|
|
3605
3781
|
);
|
|
3606
|
-
await
|
|
3607
|
-
await
|
|
3782
|
+
await fs11.writeFile(tmp, payload, "utf8");
|
|
3783
|
+
await fs11.rename(tmp, filePath);
|
|
3608
3784
|
};
|
|
3609
3785
|
var LaneRegistryStore = class {
|
|
3610
3786
|
registryPath;
|
|
@@ -3614,10 +3790,10 @@ var LaneRegistryStore = class {
|
|
|
3614
3790
|
return;
|
|
3615
3791
|
}
|
|
3616
3792
|
const root = resolveStoragePaths2().root;
|
|
3617
|
-
this.registryPath =
|
|
3793
|
+
this.registryPath = path11.join(root, REGISTRY_DIRNAME, REGISTRY_FILENAME);
|
|
3618
3794
|
}
|
|
3619
3795
|
async ensureDir() {
|
|
3620
|
-
await
|
|
3796
|
+
await fs11.mkdir(path11.dirname(this.registryPath), { recursive: true });
|
|
3621
3797
|
}
|
|
3622
3798
|
async readAll() {
|
|
3623
3799
|
await this.ensureDir();
|
|
@@ -3717,7 +3893,7 @@ var LaneManager = class {
|
|
|
3717
3893
|
this.now = options.now ?? nowIso2;
|
|
3718
3894
|
this.randomId = options.randomId ?? (() => crypto4.randomUUID());
|
|
3719
3895
|
this.launchCommand = options.launchCommand ?? process.env.CODELIA_LANE_LAUNCH_COMMAND?.trim() ?? "codelia";
|
|
3720
|
-
this.defaultWorktreeRoot = options.defaultWorktreeRoot ??
|
|
3896
|
+
this.defaultWorktreeRoot = options.defaultWorktreeRoot ?? path12.join(os2.homedir(), ".codelia", "worktrees");
|
|
3721
3897
|
}
|
|
3722
3898
|
async cmd(command, args, options) {
|
|
3723
3899
|
debugLog(`lane.cmd ${command} ${args.join(" ")}`);
|
|
@@ -3840,15 +4016,15 @@ var LaneManager = class {
|
|
|
3840
4016
|
"rev-parse",
|
|
3841
4017
|
"--git-common-dir"
|
|
3842
4018
|
]);
|
|
3843
|
-
const commonDir =
|
|
3844
|
-
if (
|
|
3845
|
-
return
|
|
4019
|
+
const commonDir = path12.resolve(worktreePath, stdout.trim());
|
|
4020
|
+
if (path12.basename(commonDir) === ".git") {
|
|
4021
|
+
return path12.dirname(commonDir);
|
|
3846
4022
|
}
|
|
3847
4023
|
return await this.resolveRepoRoot(worktreePath);
|
|
3848
4024
|
}
|
|
3849
4025
|
async isWorktreeDirty(worktreePath) {
|
|
3850
4026
|
try {
|
|
3851
|
-
await
|
|
4027
|
+
await fs12.access(worktreePath);
|
|
3852
4028
|
} catch {
|
|
3853
4029
|
return false;
|
|
3854
4030
|
}
|
|
@@ -3862,7 +4038,7 @@ var LaneManager = class {
|
|
|
3862
4038
|
}
|
|
3863
4039
|
async removeWorktree(worktreePath, force) {
|
|
3864
4040
|
try {
|
|
3865
|
-
await
|
|
4041
|
+
await fs12.access(worktreePath);
|
|
3866
4042
|
} catch {
|
|
3867
4043
|
return;
|
|
3868
4044
|
}
|
|
@@ -3887,7 +4063,7 @@ var LaneManager = class {
|
|
|
3887
4063
|
const slug = toSlug(taskId);
|
|
3888
4064
|
const shortId = laneId.slice(0, 8);
|
|
3889
4065
|
const branchName = `lane/${slug}-${shortId}`;
|
|
3890
|
-
const worktreePath = input.worktree_path ?
|
|
4066
|
+
const worktreePath = input.worktree_path ? path12.resolve(options.workingDir, input.worktree_path) : path12.join(this.defaultWorktreeRoot, `${slug}-${shortId}`);
|
|
3891
4067
|
const muxTarget = `codelia-lane-${shortId}`;
|
|
3892
4068
|
const createdAt = this.now();
|
|
3893
4069
|
const record = {
|
|
@@ -3905,7 +4081,7 @@ var LaneManager = class {
|
|
|
3905
4081
|
};
|
|
3906
4082
|
await this.registry.upsert(record);
|
|
3907
4083
|
try {
|
|
3908
|
-
await
|
|
4084
|
+
await fs12.mkdir(path12.dirname(worktreePath), { recursive: true });
|
|
3909
4085
|
await this.cmd("git", [
|
|
3910
4086
|
"-C",
|
|
3911
4087
|
repoRoot,
|
|
@@ -4211,7 +4387,7 @@ var createLaneGcTool = (sandboxKey) => defineTool7({
|
|
|
4211
4387
|
});
|
|
4212
4388
|
|
|
4213
4389
|
// src/tools/read.ts
|
|
4214
|
-
import { promises as
|
|
4390
|
+
import { promises as fs13 } from "fs";
|
|
4215
4391
|
import { defineTool as defineTool8 } from "@codelia/core";
|
|
4216
4392
|
import { z as z9 } from "zod";
|
|
4217
4393
|
var DEFAULT_READ_LIMIT = 2e3;
|
|
@@ -4234,7 +4410,7 @@ var createReadTool = (sandboxKey) => defineTool8({
|
|
|
4234
4410
|
return `Security error: ${String(error)}`;
|
|
4235
4411
|
}
|
|
4236
4412
|
try {
|
|
4237
|
-
const stat = await
|
|
4413
|
+
const stat = await fs13.stat(resolved);
|
|
4238
4414
|
if (stat.isDirectory()) {
|
|
4239
4415
|
return `Path is a directory: ${input.file_path}`;
|
|
4240
4416
|
}
|
|
@@ -4242,7 +4418,7 @@ var createReadTool = (sandboxKey) => defineTool8({
|
|
|
4242
4418
|
return `File not found: ${input.file_path}`;
|
|
4243
4419
|
}
|
|
4244
4420
|
try {
|
|
4245
|
-
const content = await
|
|
4421
|
+
const content = await fs13.readFile(resolved, "utf8");
|
|
4246
4422
|
const lines = content.split(/\r?\n/);
|
|
4247
4423
|
if (lines.length === 0) {
|
|
4248
4424
|
return "";
|
|
@@ -4635,8 +4811,8 @@ var createToolOutputCacheGrepTool = (store) => defineTool14({
|
|
|
4635
4811
|
});
|
|
4636
4812
|
|
|
4637
4813
|
// src/tools/write.ts
|
|
4638
|
-
import { promises as
|
|
4639
|
-
import
|
|
4814
|
+
import { promises as fs14 } from "fs";
|
|
4815
|
+
import path13 from "path";
|
|
4640
4816
|
import { defineTool as defineTool15 } from "@codelia/core";
|
|
4641
4817
|
import { z as z16 } from "zod";
|
|
4642
4818
|
var createWriteTool = (sandboxKey) => defineTool15({
|
|
@@ -4655,8 +4831,8 @@ var createWriteTool = (sandboxKey) => defineTool15({
|
|
|
4655
4831
|
return `Security error: ${String(error)}`;
|
|
4656
4832
|
}
|
|
4657
4833
|
try {
|
|
4658
|
-
await
|
|
4659
|
-
await
|
|
4834
|
+
await fs14.mkdir(path13.dirname(resolved), { recursive: true });
|
|
4835
|
+
await fs14.writeFile(resolved, input.content, "utf8");
|
|
4660
4836
|
return `Wrote ${input.content.length} bytes to ${input.file_path}`;
|
|
4661
4837
|
} catch (error) {
|
|
4662
4838
|
return `Error writing file: ${String(error)}`;
|
|
@@ -4722,9 +4898,9 @@ var normalizeLanguage = (value) => {
|
|
|
4722
4898
|
};
|
|
4723
4899
|
var languageFromFilePath = (filePath) => {
|
|
4724
4900
|
if (!filePath) return void 0;
|
|
4725
|
-
const
|
|
4726
|
-
if (!
|
|
4727
|
-
const normalizedPath =
|
|
4901
|
+
const path18 = filePath.trim().replace(/^["']|["']$/g, "");
|
|
4902
|
+
if (!path18 || path18 === "/dev/null") return void 0;
|
|
4903
|
+
const normalizedPath = path18.replace(/^a\//, "").replace(/^b\//, "");
|
|
4728
4904
|
const lastSlash = Math.max(
|
|
4729
4905
|
normalizedPath.lastIndexOf("/"),
|
|
4730
4906
|
normalizedPath.lastIndexOf("\\")
|
|
@@ -4884,6 +5060,40 @@ var waitForUiConfirmSupport = async (state, timeoutMs = 5e3) => {
|
|
|
4884
5060
|
}
|
|
4885
5061
|
return !!state.uiCapabilities?.supports_confirm;
|
|
4886
5062
|
};
|
|
5063
|
+
var APPROVAL_MODE_STARTUP_PICK_TITLE = "Choose approval mode for this project";
|
|
5064
|
+
var APPROVAL_MODE_STARTUP_PICK_ITEMS = [
|
|
5065
|
+
{
|
|
5066
|
+
id: "minimal",
|
|
5067
|
+
label: "minimal",
|
|
5068
|
+
detail: "Recommended default. Non-allowed operations require confirmation."
|
|
5069
|
+
},
|
|
5070
|
+
{
|
|
5071
|
+
id: "trusted",
|
|
5072
|
+
label: "trusted",
|
|
5073
|
+
detail: "Adds workspace write-oriented allowlist. Other operations still require confirmation."
|
|
5074
|
+
},
|
|
5075
|
+
{
|
|
5076
|
+
id: "full-access",
|
|
5077
|
+
label: "full-access",
|
|
5078
|
+
detail: "Skips confirmation for non-denied operations."
|
|
5079
|
+
}
|
|
5080
|
+
];
|
|
5081
|
+
var requestApprovalModeStartupSelection = async (state, projectKey) => {
|
|
5082
|
+
if (!state.uiCapabilities?.supports_pick) {
|
|
5083
|
+
return null;
|
|
5084
|
+
}
|
|
5085
|
+
const selection = await requestUiPick(state, {
|
|
5086
|
+
title: APPROVAL_MODE_STARTUP_PICK_TITLE,
|
|
5087
|
+
items: APPROVAL_MODE_STARTUP_PICK_ITEMS,
|
|
5088
|
+
multi: false
|
|
5089
|
+
});
|
|
5090
|
+
const picked = parseApprovalMode2(selection?.ids?.[0]);
|
|
5091
|
+
if (!picked) {
|
|
5092
|
+
log(`approval_mode startup selection skipped project=${projectKey}`);
|
|
5093
|
+
return null;
|
|
5094
|
+
}
|
|
5095
|
+
return picked;
|
|
5096
|
+
};
|
|
4887
5097
|
var MAX_CONFIRM_PREVIEW_LINES = 120;
|
|
4888
5098
|
var splitLines = (value) => value.split("\n").map((line) => line.replace(/\r$/, ""));
|
|
4889
5099
|
var buildBoundedDiffPreview = (diff, maxLines = MAX_CONFIRM_PREVIEW_LINES) => {
|
|
@@ -5096,14 +5306,25 @@ var createAgentFactory = (state, options = {}) => {
|
|
|
5096
5306
|
const message = error instanceof Error ? error.message : String(error);
|
|
5097
5307
|
throw new Error(message);
|
|
5098
5308
|
}
|
|
5309
|
+
const approvalModeResolution = await resolveApprovalModeForRuntime({
|
|
5310
|
+
workingDir: ctx.workingDir,
|
|
5311
|
+
runtimeSandboxRoot: state.runtimeSandboxRoot,
|
|
5312
|
+
requestStartupSelection: async ({ projectKey }) => {
|
|
5313
|
+
return requestApprovalModeStartupSelection(state, projectKey);
|
|
5314
|
+
}
|
|
5315
|
+
});
|
|
5099
5316
|
const permissionService = new PermissionService({
|
|
5100
|
-
|
|
5317
|
+
approvalMode: approvalModeResolution.approvalMode,
|
|
5318
|
+
system: buildSystemPermissions(approvalModeResolution.approvalMode),
|
|
5101
5319
|
user: permissionsConfig,
|
|
5102
5320
|
bashPathGuard: {
|
|
5103
5321
|
rootDir: ctx.rootDir,
|
|
5104
5322
|
workingDir: ctx.workingDir
|
|
5105
5323
|
}
|
|
5106
5324
|
});
|
|
5325
|
+
log(
|
|
5326
|
+
`approval_mode resolved=${approvalModeResolution.approvalMode} source=${approvalModeResolution.source} project=${approvalModeResolution.projectKey}`
|
|
5327
|
+
);
|
|
5107
5328
|
let modelConfig;
|
|
5108
5329
|
try {
|
|
5109
5330
|
modelConfig = await resolveModelConfig(ctx.workingDir);
|
|
@@ -5240,9 +5461,9 @@ var createAgentFactory = (state, options = {}) => {
|
|
|
5240
5461
|
const resolved = sandbox.resolvePath(filePath);
|
|
5241
5462
|
let before = "";
|
|
5242
5463
|
try {
|
|
5243
|
-
const stat = await
|
|
5464
|
+
const stat = await fs15.stat(resolved);
|
|
5244
5465
|
if (!stat.isDirectory()) {
|
|
5245
|
-
before = await
|
|
5466
|
+
before = await fs15.readFile(resolved, "utf8");
|
|
5246
5467
|
}
|
|
5247
5468
|
} catch {
|
|
5248
5469
|
before = "";
|
|
@@ -5898,8 +6119,8 @@ var parseAuthorizationServerMetadata = (value) => {
|
|
|
5898
6119
|
var buildAuthorizationServerMetadataUrl = (issuer) => {
|
|
5899
6120
|
try {
|
|
5900
6121
|
const parsed = new URL(issuer);
|
|
5901
|
-
const
|
|
5902
|
-
const basePath =
|
|
6122
|
+
const path18 = parsed.pathname === "/" ? "" : parsed.pathname;
|
|
6123
|
+
const basePath = path18.startsWith("/") ? path18 : `/${path18}`;
|
|
5903
6124
|
const metadataPath = `/.well-known/oauth-authorization-server${basePath}`;
|
|
5904
6125
|
return new URL(metadataPath, `${parsed.origin}/`).toString();
|
|
5905
6126
|
} catch {
|
|
@@ -6671,8 +6892,8 @@ var createContextHandlers = ({
|
|
|
6671
6892
|
};
|
|
6672
6893
|
|
|
6673
6894
|
// src/rpc/history.ts
|
|
6674
|
-
import { createReadStream, promises as
|
|
6675
|
-
import
|
|
6895
|
+
import { createReadStream, promises as fs16 } from "fs";
|
|
6896
|
+
import path14 from "path";
|
|
6676
6897
|
import { createInterface as createInterface2 } from "readline";
|
|
6677
6898
|
import {
|
|
6678
6899
|
RPC_ERROR_CODE as RPC_ERROR_CODE2
|
|
@@ -6736,7 +6957,7 @@ var collectRunCandidates = async (log2) => {
|
|
|
6736
6957
|
const sessionsDir = paths.sessionsDir;
|
|
6737
6958
|
let yearEntries;
|
|
6738
6959
|
try {
|
|
6739
|
-
yearEntries = await
|
|
6960
|
+
yearEntries = await fs16.readdir(sessionsDir, {
|
|
6740
6961
|
withFileTypes: true,
|
|
6741
6962
|
encoding: "utf8"
|
|
6742
6963
|
});
|
|
@@ -6748,10 +6969,10 @@ var collectRunCandidates = async (log2) => {
|
|
|
6748
6969
|
for (const yearEntry of yearEntries) {
|
|
6749
6970
|
if (!yearEntry.isDirectory()) continue;
|
|
6750
6971
|
if (!/^\d{4}$/.test(yearEntry.name)) continue;
|
|
6751
|
-
const yearPath =
|
|
6972
|
+
const yearPath = path14.join(sessionsDir, yearEntry.name);
|
|
6752
6973
|
let monthEntries;
|
|
6753
6974
|
try {
|
|
6754
|
-
monthEntries = await
|
|
6975
|
+
monthEntries = await fs16.readdir(yearPath, {
|
|
6755
6976
|
withFileTypes: true,
|
|
6756
6977
|
encoding: "utf8"
|
|
6757
6978
|
});
|
|
@@ -6761,10 +6982,10 @@ var collectRunCandidates = async (log2) => {
|
|
|
6761
6982
|
for (const monthEntry of monthEntries) {
|
|
6762
6983
|
if (!monthEntry.isDirectory()) continue;
|
|
6763
6984
|
if (!/^\d{2}$/.test(monthEntry.name)) continue;
|
|
6764
|
-
const monthPath =
|
|
6985
|
+
const monthPath = path14.join(yearPath, monthEntry.name);
|
|
6765
6986
|
let dayEntries;
|
|
6766
6987
|
try {
|
|
6767
|
-
dayEntries = await
|
|
6988
|
+
dayEntries = await fs16.readdir(monthPath, {
|
|
6768
6989
|
withFileTypes: true,
|
|
6769
6990
|
encoding: "utf8"
|
|
6770
6991
|
});
|
|
@@ -6774,10 +6995,10 @@ var collectRunCandidates = async (log2) => {
|
|
|
6774
6995
|
for (const dayEntry of dayEntries) {
|
|
6775
6996
|
if (!dayEntry.isDirectory()) continue;
|
|
6776
6997
|
if (!/^\d{2}$/.test(dayEntry.name)) continue;
|
|
6777
|
-
const dayPath =
|
|
6998
|
+
const dayPath = path14.join(monthPath, dayEntry.name);
|
|
6778
6999
|
let files;
|
|
6779
7000
|
try {
|
|
6780
|
-
files = await
|
|
7001
|
+
files = await fs16.readdir(dayPath, {
|
|
6781
7002
|
withFileTypes: true,
|
|
6782
7003
|
encoding: "utf8"
|
|
6783
7004
|
});
|
|
@@ -6787,9 +7008,9 @@ var collectRunCandidates = async (log2) => {
|
|
|
6787
7008
|
for (const file of files) {
|
|
6788
7009
|
if (!file.isFile()) continue;
|
|
6789
7010
|
if (!file.name.endsWith(".jsonl")) continue;
|
|
6790
|
-
const filePath =
|
|
7011
|
+
const filePath = path14.join(dayPath, file.name);
|
|
6791
7012
|
try {
|
|
6792
|
-
const stat = await
|
|
7013
|
+
const stat = await fs16.stat(filePath);
|
|
6793
7014
|
candidates.push({ path: filePath, mtimeMs: stat.mtimeMs });
|
|
6794
7015
|
} catch {
|
|
6795
7016
|
}
|
|
@@ -6815,7 +7036,7 @@ var collectSessionRuns = async (sessionId, maxRuns, log2) => {
|
|
|
6815
7036
|
const header = parseRunHeader(headerLine);
|
|
6816
7037
|
if (!header) continue;
|
|
6817
7038
|
if (header.session_id !== sessionId) continue;
|
|
6818
|
-
const runId = header.run_id ??
|
|
7039
|
+
const runId = header.run_id ?? path14.basename(candidate.path, ".jsonl");
|
|
6819
7040
|
const startedAt = header.started_at ?? new Date(candidate.mtimeMs).toISOString();
|
|
6820
7041
|
runs.push({
|
|
6821
7042
|
path: candidate.path,
|
|
@@ -8099,7 +8320,7 @@ var createRunHandlers = ({
|
|
|
8099
8320
|
// src/rpc/shell.ts
|
|
8100
8321
|
import { spawn as spawn5 } from "child_process";
|
|
8101
8322
|
import crypto6 from "crypto";
|
|
8102
|
-
import
|
|
8323
|
+
import path15 from "path";
|
|
8103
8324
|
import {
|
|
8104
8325
|
RPC_ERROR_CODE as RPC_ERROR_CODE5
|
|
8105
8326
|
} from "@codelia/protocol";
|
|
@@ -8167,14 +8388,14 @@ var excerptByBytes = (value, maxBytes) => {
|
|
|
8167
8388
|
var needsInlineTruncation = (value) => value.split(/\r?\n/).length > DEFAULT_EXCERPT_LINES * 2 || utf8ByteLength(value) > MAX_INLINE_OUTPUT_BYTES;
|
|
8168
8389
|
var excerptText = (value) => excerptByBytes(excerptByLines(value), MAX_INLINE_OUTPUT_BYTES);
|
|
8169
8390
|
var isWithin2 = (basePath, candidatePath) => {
|
|
8170
|
-
const relative =
|
|
8171
|
-
return relative === "" || !relative.startsWith("..") && !
|
|
8391
|
+
const relative = path15.relative(basePath, candidatePath);
|
|
8392
|
+
return relative === "" || !relative.startsWith("..") && !path15.isAbsolute(relative);
|
|
8172
8393
|
};
|
|
8173
8394
|
var resolveShellCwd = (state, requestedCwd) => {
|
|
8174
8395
|
const workingDir = state.runtimeWorkingDir ?? process.cwd();
|
|
8175
8396
|
const rootDir = state.runtimeSandboxRoot ?? workingDir;
|
|
8176
8397
|
if (!requestedCwd) return workingDir;
|
|
8177
|
-
const resolved =
|
|
8398
|
+
const resolved = path15.resolve(workingDir, requestedCwd);
|
|
8178
8399
|
if (!isWithin2(rootDir, resolved)) {
|
|
8179
8400
|
return null;
|
|
8180
8401
|
}
|
|
@@ -8393,7 +8614,7 @@ var createShellHandlers = ({
|
|
|
8393
8614
|
};
|
|
8394
8615
|
|
|
8395
8616
|
// src/rpc/skills.ts
|
|
8396
|
-
import
|
|
8617
|
+
import path16 from "path";
|
|
8397
8618
|
import {
|
|
8398
8619
|
RPC_ERROR_CODE as RPC_ERROR_CODE6
|
|
8399
8620
|
} from "@codelia/protocol";
|
|
@@ -8402,7 +8623,7 @@ var createSkillsHandlers = ({
|
|
|
8402
8623
|
log: log2
|
|
8403
8624
|
}) => {
|
|
8404
8625
|
const handleSkillsList = async (id, params) => {
|
|
8405
|
-
const requestedCwd = params?.cwd ?
|
|
8626
|
+
const requestedCwd = params?.cwd ? path16.resolve(params.cwd) : void 0;
|
|
8406
8627
|
const workingDir = requestedCwd ?? state.runtimeWorkingDir ?? state.lastUiContext?.cwd ?? process.cwd();
|
|
8407
8628
|
const forceReload = params?.force_reload ?? false;
|
|
8408
8629
|
if (!forceReload) {
|
|
@@ -9028,7 +9249,7 @@ var startRuntime = () => {
|
|
|
9028
9249
|
void (async () => {
|
|
9029
9250
|
const state = new RuntimeState();
|
|
9030
9251
|
state.diagnosticsEnabled = envTruthy2(process.env.CODELIA_DIAGNOSTICS);
|
|
9031
|
-
const workingDir = process.env.CODELIA_SANDBOX_ROOT ?
|
|
9252
|
+
const workingDir = process.env.CODELIA_SANDBOX_ROOT ? path17.resolve(process.env.CODELIA_SANDBOX_ROOT) : process.cwd();
|
|
9032
9253
|
state.runtimeWorkingDir = workingDir;
|
|
9033
9254
|
state.runtimeSandboxRoot = workingDir;
|
|
9034
9255
|
const sessionStateStore = new SessionStateStoreImpl2({
|