@cruxy/cli 0.22.0 → 0.22.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/dist/approval/classify.js +7 -3
- package/dist/approval/policy.d.ts +6 -0
- package/dist/approval/policy.js +15 -3
- package/dist/approval/types.d.ts +8 -1
- package/dist/checkpoint/index.d.ts +1 -0
- package/dist/checkpoint/index.js +1 -0
- package/dist/checkpoint/set.d.ts +44 -0
- package/dist/checkpoint/set.js +142 -0
- package/dist/checkpoint/types.d.ts +47 -0
- package/dist/errors/constructors.d.ts +37 -0
- package/dist/errors/constructors.js +109 -0
- package/dist/errors/types.d.ts +26 -0
- package/dist/errors/types.js +38 -0
- package/dist/sandbox/docker-runtime.js +4 -1
- package/dist/sandbox/policy.d.ts +12 -3
- package/dist/sandbox/policy.js +17 -3
- package/dist/sandbox/types.d.ts +10 -1
- package/dist/tools/file/paths.d.ts +10 -17
- package/dist/tools/file/paths.js +11 -58
- package/dist/workspace/index.d.ts +5 -0
- package/dist/workspace/index.js +3 -0
- package/dist/workspace/resolve.d.ts +54 -0
- package/dist/workspace/resolve.js +96 -0
- package/dist/workspace/select.d.ts +41 -0
- package/dist/workspace/select.js +44 -0
- package/dist/workspace/types.d.ts +30 -0
- package/dist/workspace/types.js +15 -0
- package/dist/workspace/workspace.d.ts +56 -0
- package/dist/workspace/workspace.js +180 -0
- package/package.json +1 -1
package/dist/sandbox/policy.js
CHANGED
|
@@ -7,8 +7,12 @@ import { globalDir } from "../config/paths.js";
|
|
|
7
7
|
* {@link IsolationPolicy}. This is where the security posture is decided, and
|
|
8
8
|
* every default here is deny/minimal:
|
|
9
9
|
*
|
|
10
|
-
* - the
|
|
11
|
-
* path, so paths stay coherent with the host and the C.32
|
|
10
|
+
* - the DEFAULT read-write mount is the command's own project workdir (at its
|
|
11
|
+
* identical absolute path, so paths stay coherent with the host and the C.32
|
|
12
|
+
* checkpoint);
|
|
13
|
+
* - other declared roots in a multi-repo session (C.26, R5) are mounted READ-ONLY
|
|
14
|
+
* — readable for legit cross-repo builds, never writable unless an explicit
|
|
15
|
+
* per-command escalation names that root in `writableRoots`;
|
|
12
16
|
* - extra mounts come solely from `sandbox.mounts` (explicit by construction),
|
|
13
17
|
* and a mount of the docker socket, the cruxy home, or the user's home root
|
|
14
18
|
* is rejected — those are the escape hatches we refuse to open;
|
|
@@ -16,12 +20,21 @@ import { globalDir } from "../config/paths.js";
|
|
|
16
20
|
* writable and never left root-owned;
|
|
17
21
|
* - network defaults to `none`; any widening can only come from explicit config.
|
|
18
22
|
*/
|
|
19
|
-
export function buildPolicy(cfg, cwd) {
|
|
23
|
+
export function buildPolicy(cfg, cwd, opts = {}) {
|
|
20
24
|
const workdir = {
|
|
21
25
|
source: cwd,
|
|
22
26
|
target: cwd,
|
|
23
27
|
readonly: false,
|
|
24
28
|
};
|
|
29
|
+
const writable = new Set((opts.writableRoots ?? []).map((r) => resolvePath(r)));
|
|
30
|
+
const siblingRoots = (opts.siblingRoots ?? [])
|
|
31
|
+
.map((r) => resolvePath(r))
|
|
32
|
+
.filter((r) => r !== resolvePath(cwd)) // the workdir is never a sibling
|
|
33
|
+
.map((source) => ({
|
|
34
|
+
source,
|
|
35
|
+
target: source, // identical path, like the workdir, for path coherence
|
|
36
|
+
readonly: !writable.has(source), // RO unless explicitly escalated (R5)
|
|
37
|
+
}));
|
|
25
38
|
return {
|
|
26
39
|
image: cfg.image,
|
|
27
40
|
network: cfg.network,
|
|
@@ -30,6 +43,7 @@ export function buildPolicy(cfg, cwd) {
|
|
|
30
43
|
pids: cfg.pids,
|
|
31
44
|
cpus: cfg.cpus,
|
|
32
45
|
workdir,
|
|
46
|
+
siblingRoots,
|
|
33
47
|
mounts: cfg.mounts.map((spec) => parseMount(spec, cwd)),
|
|
34
48
|
tmpfs: "/tmp",
|
|
35
49
|
};
|
package/dist/sandbox/types.d.ts
CHANGED
|
@@ -43,8 +43,17 @@ export interface IsolationPolicy {
|
|
|
43
43
|
readonly pids: number;
|
|
44
44
|
/** CPU cap (`--cpus`, fractional allowed). */
|
|
45
45
|
readonly cpus: number;
|
|
46
|
-
/** The
|
|
46
|
+
/** The command's OWN workspace root, mounted read-write at its absolute path. */
|
|
47
47
|
readonly workdir: BindMount;
|
|
48
|
+
/**
|
|
49
|
+
* The OTHER declared workspace roots in a multi-repo session (C.26, R5). Each
|
|
50
|
+
* is mounted **read-only** so a legit build can *read* a sibling (e.g. generated
|
|
51
|
+
* client types) — but never write it. A sibling flips to read-write ONLY when an
|
|
52
|
+
* explicit per-command cross-root-write escalation was approved for that exact
|
|
53
|
+
* root; ambient cross-root write authority is never the default. Empty in a
|
|
54
|
+
* single-root session (identical to the pre-C.26 posture).
|
|
55
|
+
*/
|
|
56
|
+
readonly siblingRoots?: readonly BindMount[];
|
|
48
57
|
/** Extra explicit mounts beyond the workdir (from `sandbox.mounts`). */
|
|
49
58
|
readonly mounts: readonly BindMount[];
|
|
50
59
|
/** Writable in-memory tmp mount point; the rest of the root fs is read-only. */
|
|
@@ -1,26 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PathEscapeError } from "../../workspace/index.js";
|
|
2
2
|
import type { ToolContext } from "../types.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* Path confinement for file tools. The confinement kernel now lives in
|
|
5
|
+
* `src/workspace` so multi-root (C.26) and single-root callers share ONE
|
|
6
|
+
* implementation. This module keeps the single-root `resolveInRoot` entry point
|
|
7
|
+
* (and re-exports {@link PathEscapeError}) so existing call sites are unchanged:
|
|
8
|
+
* they confine to `ctx.cwd`, which is the workspace's primary root.
|
|
8
9
|
*/
|
|
9
|
-
export
|
|
10
|
-
constructor(message: string);
|
|
11
|
-
}
|
|
10
|
+
export { PathEscapeError };
|
|
12
11
|
/**
|
|
13
12
|
* Resolve a tool-supplied path against the project root (`ctx.cwd`) and prove it
|
|
14
|
-
* stays inside — the single
|
|
13
|
+
* stays inside — the single-root funnel. Delegates to {@link confineToRoot}; see
|
|
14
|
+
* there for the 2-layer (lexical + symlink) confinement logic.
|
|
15
15
|
*
|
|
16
|
-
*
|
|
17
|
-
* (rejects `../` and absolute-outside before touching the FS); (2) a symlink
|
|
18
|
-
* check that the real target — or, for a new path, its nearest existing parent —
|
|
19
|
-
* resolves inside the *real* root. The root is realpath'd too, so this is correct
|
|
20
|
-
* even when the root itself sits under a symlink (e.g. macOS `/var → /private/var`).
|
|
21
|
-
*
|
|
22
|
-
* @returns the resolved absolute path (lexical, not realpath'd — so callers
|
|
23
|
-
* operate on the intended location).
|
|
16
|
+
* @returns the resolved absolute path (lexical, not realpath'd).
|
|
24
17
|
* @throws {PathEscapeError} if the path escapes the root.
|
|
25
18
|
*/
|
|
26
19
|
export declare function resolveInRoot(ctx: ToolContext, p: string): Promise<string>;
|
package/dist/tools/file/paths.js
CHANGED
|
@@ -1,67 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { CruxyError, ErrorCode } from "../../errors/index.js";
|
|
1
|
+
import { confineToRoot, PathEscapeError } from "../../workspace/index.js";
|
|
4
2
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
3
|
+
* Path confinement for file tools. The confinement kernel now lives in
|
|
4
|
+
* `src/workspace` so multi-root (C.26) and single-root callers share ONE
|
|
5
|
+
* implementation. This module keeps the single-root `resolveInRoot` entry point
|
|
6
|
+
* (and re-exports {@link PathEscapeError}) so existing call sites are unchanged:
|
|
7
|
+
* they confine to `ctx.cwd`, which is the workspace's primary root.
|
|
9
8
|
*/
|
|
10
|
-
export
|
|
11
|
-
constructor(message) {
|
|
12
|
-
super({ code: ErrorCode.PathEscape, title: message });
|
|
13
|
-
this.name = "PathEscapeError";
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
/** Is `target` the root itself or a descendant of it? */
|
|
17
|
-
function isInside(root, target) {
|
|
18
|
-
return target === root || target.startsWith(root + path.sep);
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* realpath `p`, or — if it doesn't exist yet — the realpath of its nearest
|
|
22
|
-
* existing ancestor directory. Lets us validate a not-yet-created path by the
|
|
23
|
-
* directory it would be created in (catching outward symlinked parents).
|
|
24
|
-
*/
|
|
25
|
-
async function realpathOfNearestExisting(p) {
|
|
26
|
-
let cur = p;
|
|
27
|
-
for (;;) {
|
|
28
|
-
try {
|
|
29
|
-
return await fs.realpath(cur);
|
|
30
|
-
}
|
|
31
|
-
catch (err) {
|
|
32
|
-
if (err.code !== "ENOENT")
|
|
33
|
-
throw err;
|
|
34
|
-
const parent = path.dirname(cur);
|
|
35
|
-
if (parent === cur)
|
|
36
|
-
return cur; // reached the filesystem root
|
|
37
|
-
cur = parent;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
9
|
+
export { PathEscapeError };
|
|
41
10
|
/**
|
|
42
11
|
* Resolve a tool-supplied path against the project root (`ctx.cwd`) and prove it
|
|
43
|
-
* stays inside — the single
|
|
44
|
-
*
|
|
45
|
-
* Two layers: (1) a lexical check that the resolved absolute path is within root
|
|
46
|
-
* (rejects `../` and absolute-outside before touching the FS); (2) a symlink
|
|
47
|
-
* check that the real target — or, for a new path, its nearest existing parent —
|
|
48
|
-
* resolves inside the *real* root. The root is realpath'd too, so this is correct
|
|
49
|
-
* even when the root itself sits under a symlink (e.g. macOS `/var → /private/var`).
|
|
12
|
+
* stays inside — the single-root funnel. Delegates to {@link confineToRoot}; see
|
|
13
|
+
* there for the 2-layer (lexical + symlink) confinement logic.
|
|
50
14
|
*
|
|
51
|
-
* @returns the resolved absolute path (lexical, not realpath'd
|
|
52
|
-
* operate on the intended location).
|
|
15
|
+
* @returns the resolved absolute path (lexical, not realpath'd).
|
|
53
16
|
* @throws {PathEscapeError} if the path escapes the root.
|
|
54
17
|
*/
|
|
55
18
|
export async function resolveInRoot(ctx, p) {
|
|
56
|
-
|
|
57
|
-
const resolved = path.resolve(root, p);
|
|
58
|
-
if (!isInside(root, resolved)) {
|
|
59
|
-
throw new PathEscapeError(`path "${p}" resolves outside the project root`);
|
|
60
|
-
}
|
|
61
|
-
const realRoot = await fs.realpath(root);
|
|
62
|
-
const realTarget = await realpathOfNearestExisting(resolved);
|
|
63
|
-
if (!isInside(realRoot, realTarget)) {
|
|
64
|
-
throw new PathEscapeError(`path "${p}" resolves outside the project root (via a symlink)`);
|
|
65
|
-
}
|
|
66
|
-
return resolved;
|
|
19
|
+
return confineToRoot(ctx.cwd, p);
|
|
67
20
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { DeclaredRoot, RootSpec } from "./types.js";
|
|
2
|
+
export { Workspace, buildWorkspace, singleRootWorkspace } from "./workspace.js";
|
|
3
|
+
export { PathEscapeError, confineToRoot, isInside, resolveInWorkspace, } from "./resolve.js";
|
|
4
|
+
export { selectRoot } from "./select.js";
|
|
5
|
+
export type { RootRef, SelectedRoot } from "./select.js";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { CruxyError } from "../errors/index.js";
|
|
2
|
+
import type { Workspace } from "./workspace.js";
|
|
3
|
+
/**
|
|
4
|
+
* The single path-confinement funnel for the whole CLI (C.26). Two things live
|
|
5
|
+
* here so there is exactly ONE confinement implementation, not several:
|
|
6
|
+
* • {@link confineToRoot} — the pure 2-layer check (lexical + symlink) against
|
|
7
|
+
* ONE root. It never sees any other root, which is the structural argument
|
|
8
|
+
* that a validated target can't cross into a sibling root.
|
|
9
|
+
* • {@link resolveInWorkspace} — select the one named root, then confine to it.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Thrown when a tool argument resolves to a path outside the root it is acting in
|
|
13
|
+
* — via `../` traversal, an absolute path, an outward symlink, OR a path that
|
|
14
|
+
* lands in a *different* declared root. A cross-root path is deliberately the
|
|
15
|
+
* SAME error as any other escape (R2): a distinct code would wrongly imply
|
|
16
|
+
* "less bad". A {@link CruxyError} (code CRUXY_E_PATH_ESCAPE) so it carries a code
|
|
17
|
+
* if it reaches the boundary; tools still catch it and surface `{ ok:false }`.
|
|
18
|
+
*/
|
|
19
|
+
export declare class PathEscapeError extends CruxyError {
|
|
20
|
+
constructor(message: string);
|
|
21
|
+
}
|
|
22
|
+
/** Is `target` the root itself or a descendant of it? */
|
|
23
|
+
export declare function isInside(root: string, target: string): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Resolve `p` against a SINGLE root and prove it stays inside — the pure kernel
|
|
26
|
+
* every file tool ultimately funnels through.
|
|
27
|
+
*
|
|
28
|
+
* Two layers: (1) a lexical check that the resolved absolute path is within root
|
|
29
|
+
* (rejects `../` and absolute-outside before touching the FS); (2) a symlink
|
|
30
|
+
* check that the real target — or, for a new path, its nearest existing parent —
|
|
31
|
+
* resolves inside the *real* root. The root is realpath'd too, so this is correct
|
|
32
|
+
* even when the root itself sits under a symlink (macOS `/var → /private/var`).
|
|
33
|
+
*
|
|
34
|
+
* Crucially, this function is a pure function of `(one root, the path)`. The rest
|
|
35
|
+
* of the declared root set is NOT in scope here, so there is no code path by
|
|
36
|
+
* which a target validated against this root can be accepted into another root.
|
|
37
|
+
*
|
|
38
|
+
* @returns the resolved absolute path (lexical, not realpath'd — so callers
|
|
39
|
+
* operate on the intended location).
|
|
40
|
+
* @throws {PathEscapeError} if the path escapes the root.
|
|
41
|
+
*/
|
|
42
|
+
export declare function confineToRoot(rootAbsPath: string, p: string): Promise<string>;
|
|
43
|
+
/**
|
|
44
|
+
* Resolve a tool-supplied path against ONE named root of the workspace and prove
|
|
45
|
+
* it stays inside that root. Selection happens first (by exact name — see
|
|
46
|
+
* {@link Workspace.rootByName}, which fail-loud refuses an unknown name), and
|
|
47
|
+
* confinement runs against only that one root's path. A `../otherRoot/x` that
|
|
48
|
+
* would land in a sibling declared root is refused exactly as any escape is (R2),
|
|
49
|
+
* because {@link confineToRoot} is only ever handed this one root.
|
|
50
|
+
*
|
|
51
|
+
* @throws {CruxyError} CRUXY_E_ROOT_UNKNOWN if `rootName` is not declared.
|
|
52
|
+
* @throws {PathEscapeError} if `p` escapes the selected root.
|
|
53
|
+
*/
|
|
54
|
+
export declare function resolveInWorkspace(ws: Workspace, rootName: string, p: string): Promise<string>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { promises as fs } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { CruxyError, ErrorCode } from "../errors/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* The single path-confinement funnel for the whole CLI (C.26). Two things live
|
|
6
|
+
* here so there is exactly ONE confinement implementation, not several:
|
|
7
|
+
* • {@link confineToRoot} — the pure 2-layer check (lexical + symlink) against
|
|
8
|
+
* ONE root. It never sees any other root, which is the structural argument
|
|
9
|
+
* that a validated target can't cross into a sibling root.
|
|
10
|
+
* • {@link resolveInWorkspace} — select the one named root, then confine to it.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Thrown when a tool argument resolves to a path outside the root it is acting in
|
|
14
|
+
* — via `../` traversal, an absolute path, an outward symlink, OR a path that
|
|
15
|
+
* lands in a *different* declared root. A cross-root path is deliberately the
|
|
16
|
+
* SAME error as any other escape (R2): a distinct code would wrongly imply
|
|
17
|
+
* "less bad". A {@link CruxyError} (code CRUXY_E_PATH_ESCAPE) so it carries a code
|
|
18
|
+
* if it reaches the boundary; tools still catch it and surface `{ ok:false }`.
|
|
19
|
+
*/
|
|
20
|
+
export class PathEscapeError extends CruxyError {
|
|
21
|
+
constructor(message) {
|
|
22
|
+
super({ code: ErrorCode.PathEscape, title: message });
|
|
23
|
+
this.name = "PathEscapeError";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Is `target` the root itself or a descendant of it? */
|
|
27
|
+
export function isInside(root, target) {
|
|
28
|
+
return target === root || target.startsWith(root + path.sep);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* realpath `p`, or — if it doesn't exist yet — the realpath of its nearest
|
|
32
|
+
* existing ancestor directory. Lets us validate a not-yet-created path by the
|
|
33
|
+
* directory it would be created in (catching outward symlinked parents).
|
|
34
|
+
*/
|
|
35
|
+
async function realpathOfNearestExisting(p) {
|
|
36
|
+
let cur = p;
|
|
37
|
+
for (;;) {
|
|
38
|
+
try {
|
|
39
|
+
return await fs.realpath(cur);
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
if (err.code !== "ENOENT")
|
|
43
|
+
throw err;
|
|
44
|
+
const parent = path.dirname(cur);
|
|
45
|
+
if (parent === cur)
|
|
46
|
+
return cur; // reached the filesystem root
|
|
47
|
+
cur = parent;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Resolve `p` against a SINGLE root and prove it stays inside — the pure kernel
|
|
53
|
+
* every file tool ultimately funnels through.
|
|
54
|
+
*
|
|
55
|
+
* Two layers: (1) a lexical check that the resolved absolute path is within root
|
|
56
|
+
* (rejects `../` and absolute-outside before touching the FS); (2) a symlink
|
|
57
|
+
* check that the real target — or, for a new path, its nearest existing parent —
|
|
58
|
+
* resolves inside the *real* root. The root is realpath'd too, so this is correct
|
|
59
|
+
* even when the root itself sits under a symlink (macOS `/var → /private/var`).
|
|
60
|
+
*
|
|
61
|
+
* Crucially, this function is a pure function of `(one root, the path)`. The rest
|
|
62
|
+
* of the declared root set is NOT in scope here, so there is no code path by
|
|
63
|
+
* which a target validated against this root can be accepted into another root.
|
|
64
|
+
*
|
|
65
|
+
* @returns the resolved absolute path (lexical, not realpath'd — so callers
|
|
66
|
+
* operate on the intended location).
|
|
67
|
+
* @throws {PathEscapeError} if the path escapes the root.
|
|
68
|
+
*/
|
|
69
|
+
export async function confineToRoot(rootAbsPath, p) {
|
|
70
|
+
const root = path.resolve(rootAbsPath);
|
|
71
|
+
const resolved = path.resolve(root, p);
|
|
72
|
+
if (!isInside(root, resolved)) {
|
|
73
|
+
throw new PathEscapeError(`path "${p}" resolves outside the project root`);
|
|
74
|
+
}
|
|
75
|
+
const realRoot = await fs.realpath(root);
|
|
76
|
+
const realTarget = await realpathOfNearestExisting(resolved);
|
|
77
|
+
if (!isInside(realRoot, realTarget)) {
|
|
78
|
+
throw new PathEscapeError(`path "${p}" resolves outside the project root (via a symlink)`);
|
|
79
|
+
}
|
|
80
|
+
return resolved;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Resolve a tool-supplied path against ONE named root of the workspace and prove
|
|
84
|
+
* it stays inside that root. Selection happens first (by exact name — see
|
|
85
|
+
* {@link Workspace.rootByName}, which fail-loud refuses an unknown name), and
|
|
86
|
+
* confinement runs against only that one root's path. A `../otherRoot/x` that
|
|
87
|
+
* would land in a sibling declared root is refused exactly as any escape is (R2),
|
|
88
|
+
* because {@link confineToRoot} is only ever handed this one root.
|
|
89
|
+
*
|
|
90
|
+
* @throws {CruxyError} CRUXY_E_ROOT_UNKNOWN if `rootName` is not declared.
|
|
91
|
+
* @throws {PathEscapeError} if `p` escapes the selected root.
|
|
92
|
+
*/
|
|
93
|
+
export async function resolveInWorkspace(ws, rootName, p) {
|
|
94
|
+
const root = ws.rootByName(rootName); // fail-loud on unknown name (R1)
|
|
95
|
+
return confineToRoot(root.absPath, p);
|
|
96
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { DeclaredRoot } from "./types.js";
|
|
2
|
+
import type { Workspace } from "./workspace.js";
|
|
3
|
+
/**
|
|
4
|
+
* How a tool call addresses a workspace root (C.26, R1). Either an explicit `root`
|
|
5
|
+
* name plus a root-relative `path`, or just a `path` that may carry a leading
|
|
6
|
+
* root-name segment. Bare relative paths fall back to the primary root; absolute
|
|
7
|
+
* paths select the unique root that contains them.
|
|
8
|
+
*/
|
|
9
|
+
export interface RootRef {
|
|
10
|
+
/** Explicit root name (`root: "service"`). Wins over any name in `path`. */
|
|
11
|
+
readonly root?: string;
|
|
12
|
+
/** The path argument (root-relative, name-prefixed, or absolute). */
|
|
13
|
+
readonly path: string;
|
|
14
|
+
}
|
|
15
|
+
/** The outcome of resolving a {@link RootRef}: the chosen root + a path within it. */
|
|
16
|
+
export interface SelectedRoot {
|
|
17
|
+
readonly root: DeclaredRoot;
|
|
18
|
+
/** The path to hand to `confineToRoot` (root-relative or absolute-inside). */
|
|
19
|
+
readonly relPath: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolve a {@link RootRef} to exactly one declared root and a path within it —
|
|
23
|
+
* the selection half of confinement, run BEFORE any resolution so a call always
|
|
24
|
+
* commits to a single root first (§1.1). Precedence:
|
|
25
|
+
*
|
|
26
|
+
* 1. explicit `root` name → that root, exactly (fail-loud on unknown name);
|
|
27
|
+
* 2. absolute `path` → the unique declared root containing it (fail-loud on
|
|
28
|
+
* unknown / — defensively — ambiguous);
|
|
29
|
+
* 3. `path` whose first segment exactly matches a declared root name → that
|
|
30
|
+
* root, with the segment stripped (only when the name is unambiguous);
|
|
31
|
+
* 4. bare relative `path` → the primary root.
|
|
32
|
+
*
|
|
33
|
+
* `requireExplicit` (set by mutating tools per ⚖︎#3) refuses the case-4 default
|
|
34
|
+
* in a multi-root session: a write must NAME its root rather than silently land
|
|
35
|
+
* in the primary. Read/enumeration tools leave it false.
|
|
36
|
+
*
|
|
37
|
+
* @throws CRUXY_E_ROOT_UNKNOWN / CRUXY_E_ROOT_AMBIGUOUS per the rules above.
|
|
38
|
+
*/
|
|
39
|
+
export declare function selectRoot(ws: Workspace, ref: RootRef, opts?: {
|
|
40
|
+
requireExplicit?: boolean;
|
|
41
|
+
}): SelectedRoot;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { rootAmbiguous } from "../errors/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Resolve a {@link RootRef} to exactly one declared root and a path within it —
|
|
5
|
+
* the selection half of confinement, run BEFORE any resolution so a call always
|
|
6
|
+
* commits to a single root first (§1.1). Precedence:
|
|
7
|
+
*
|
|
8
|
+
* 1. explicit `root` name → that root, exactly (fail-loud on unknown name);
|
|
9
|
+
* 2. absolute `path` → the unique declared root containing it (fail-loud on
|
|
10
|
+
* unknown / — defensively — ambiguous);
|
|
11
|
+
* 3. `path` whose first segment exactly matches a declared root name → that
|
|
12
|
+
* root, with the segment stripped (only when the name is unambiguous);
|
|
13
|
+
* 4. bare relative `path` → the primary root.
|
|
14
|
+
*
|
|
15
|
+
* `requireExplicit` (set by mutating tools per ⚖︎#3) refuses the case-4 default
|
|
16
|
+
* in a multi-root session: a write must NAME its root rather than silently land
|
|
17
|
+
* in the primary. Read/enumeration tools leave it false.
|
|
18
|
+
*
|
|
19
|
+
* @throws CRUXY_E_ROOT_UNKNOWN / CRUXY_E_ROOT_AMBIGUOUS per the rules above.
|
|
20
|
+
*/
|
|
21
|
+
export function selectRoot(ws, ref, opts = {}) {
|
|
22
|
+
// 1. Explicit root name wins.
|
|
23
|
+
if (ref.root !== undefined) {
|
|
24
|
+
return { root: ws.rootByName(ref.root), relPath: ref.path };
|
|
25
|
+
}
|
|
26
|
+
// 2. Absolute path → the unique containing root.
|
|
27
|
+
if (path.isAbsolute(ref.path)) {
|
|
28
|
+
return { root: ws.rootContaining(ref.path), relPath: ref.path };
|
|
29
|
+
}
|
|
30
|
+
// 3. `name/rest` where `name` is a declared root (only meaningful multi-root).
|
|
31
|
+
const firstSeg = ref.path.split(/[/\\]/, 1)[0];
|
|
32
|
+
if (firstSeg && ref.path !== firstSeg) {
|
|
33
|
+
const named = ws.tryRootByName(firstSeg);
|
|
34
|
+
if (named) {
|
|
35
|
+
const rest = ref.path.slice(firstSeg.length).replace(/^[/\\]+/, "");
|
|
36
|
+
return { root: named, relPath: rest };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// 4. Bare relative path → primary root, unless a mutation must be explicit.
|
|
40
|
+
if (opts.requireExplicit && ws.isMultiRoot) {
|
|
41
|
+
throw rootAmbiguous(ref.path, ws.roots().map((r) => r.name));
|
|
42
|
+
}
|
|
43
|
+
return { root: ws.primary(), relPath: ref.path };
|
|
44
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-repo workspace model (C.26). A session may operate across N repo/package
|
|
3
|
+
* roots. The {@link Workspace} is the single value that carries the declared root
|
|
4
|
+
* set; every path-taking tool resolves *through* it rather than through a bare
|
|
5
|
+
* `cwd` string.
|
|
6
|
+
*
|
|
7
|
+
* Two invariants the type system helps enforce:
|
|
8
|
+
* • The root set is **immutable for the session** — a {@link Workspace} exposes
|
|
9
|
+
* no mutator, so nothing the model can call adds a root. The set only grows by
|
|
10
|
+
* the CLI constructing a *new* Workspace from an explicit human act (argv, or
|
|
11
|
+
* an interactive add-root that prompts + trusts first).
|
|
12
|
+
* • Roots are addressed by a stable **name** (R1), matched **exactly** — never
|
|
13
|
+
* by prefix or nearest-match. An unknown name is a fail-loud refusal.
|
|
14
|
+
*/
|
|
15
|
+
/** One declared workspace root: a stable name + its resolved absolute path. */
|
|
16
|
+
export interface DeclaredRoot {
|
|
17
|
+
/** Stable, user-facing identifier (assigned at declaration, unique per session). */
|
|
18
|
+
readonly name: string;
|
|
19
|
+
/** Absolute, lexically-resolved path (realpath is applied at confinement time). */
|
|
20
|
+
readonly absPath: string;
|
|
21
|
+
/** Exactly one root is primary — the default for bare relative paths + git info. */
|
|
22
|
+
readonly primary: boolean;
|
|
23
|
+
}
|
|
24
|
+
/** A root as declared on the CLI (or interactively), before resolution/validation. */
|
|
25
|
+
export interface RootSpec {
|
|
26
|
+
/** Explicit name (`--root name=path`); defaults to a deduped basename of `path`. */
|
|
27
|
+
readonly name?: string;
|
|
28
|
+
/** Path as supplied (resolved against `process.cwd()` by `buildWorkspace`). */
|
|
29
|
+
readonly path: string;
|
|
30
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-repo workspace model (C.26). A session may operate across N repo/package
|
|
3
|
+
* roots. The {@link Workspace} is the single value that carries the declared root
|
|
4
|
+
* set; every path-taking tool resolves *through* it rather than through a bare
|
|
5
|
+
* `cwd` string.
|
|
6
|
+
*
|
|
7
|
+
* Two invariants the type system helps enforce:
|
|
8
|
+
* • The root set is **immutable for the session** — a {@link Workspace} exposes
|
|
9
|
+
* no mutator, so nothing the model can call adds a root. The set only grows by
|
|
10
|
+
* the CLI constructing a *new* Workspace from an explicit human act (argv, or
|
|
11
|
+
* an interactive add-root that prompts + trusts first).
|
|
12
|
+
* • Roots are addressed by a stable **name** (R1), matched **exactly** — never
|
|
13
|
+
* by prefix or nearest-match. An unknown name is a fail-loud refusal.
|
|
14
|
+
*/
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { DeclaredRoot, RootSpec } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The declared workspace root set for a session (C.26). Immutable by design: it
|
|
4
|
+
* exposes readers only, no mutator. The root set grows solely by the CLI building
|
|
5
|
+
* a *new* Workspace from an explicit human act — so no tool, and nothing the model
|
|
6
|
+
* emits, can add a root. Roots are addressed by exact name (R1).
|
|
7
|
+
*/
|
|
8
|
+
export declare class Workspace {
|
|
9
|
+
private readonly rootsByName;
|
|
10
|
+
private readonly ordered;
|
|
11
|
+
private readonly primaryRoot;
|
|
12
|
+
constructor(roots: readonly DeclaredRoot[]);
|
|
13
|
+
/** All declared roots, in declaration order. */
|
|
14
|
+
roots(): readonly DeclaredRoot[];
|
|
15
|
+
/** The primary root — the default for bare relative paths and git/instructions. */
|
|
16
|
+
primary(): DeclaredRoot;
|
|
17
|
+
/** True when more than one root is declared (i.e. a genuine multi-repo session). */
|
|
18
|
+
get isMultiRoot(): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Look up a root by EXACT name (R1). Never fuzzy-, prefix-, or nearest-matched
|
|
21
|
+
* — a silent near-match would be a cross-root misfire.
|
|
22
|
+
* @throws {CruxyError} CRUXY_E_ROOT_UNKNOWN if no root has that exact name.
|
|
23
|
+
*/
|
|
24
|
+
rootByName(name: string): DeclaredRoot;
|
|
25
|
+
/** Like {@link rootByName} but returns undefined instead of throwing. */
|
|
26
|
+
tryRootByName(name: string): DeclaredRoot | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* The single declared root that contains `absPath`. Roots never overlap (that's
|
|
29
|
+
* refused at declaration), so at most one can match.
|
|
30
|
+
* @throws CRUXY_E_ROOT_UNKNOWN if the path is inside no declared root.
|
|
31
|
+
* @throws CRUXY_E_ROOT_AMBIGUOUS if — defensively — it matches more than one.
|
|
32
|
+
*/
|
|
33
|
+
rootContaining(absPath: string): DeclaredRoot;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Build a {@link Workspace} from declared specs (R1, ⚖︎#5). Each path is resolved
|
|
37
|
+
* against `process.cwd()`, must exist and be a directory, and the set must not
|
|
38
|
+
* overlap (a root nested in / equal to another is refused — declare the monorepo
|
|
39
|
+
* root OR its packages, never both). The first spec is primary unless one is
|
|
40
|
+
* marked. Names are explicit-or-basename, validated, and deduped.
|
|
41
|
+
*
|
|
42
|
+
* This is the ONLY constructor of a Workspace from user input; it is called by the
|
|
43
|
+
* CLI from argv and by the interactive add-root path — never from a tool.
|
|
44
|
+
*
|
|
45
|
+
* @throws CRUXY_E_ROOT_OVERLAP on a nested/overlapping/duplicate root.
|
|
46
|
+
* @throws CRUXY_E_USAGE on a missing path, non-directory, or bad/duplicate name.
|
|
47
|
+
*/
|
|
48
|
+
export declare function buildWorkspace(specs: readonly RootSpec[], opts?: {
|
|
49
|
+
cwd?: string;
|
|
50
|
+
}): Promise<Workspace>;
|
|
51
|
+
/**
|
|
52
|
+
* Build a trivial single-root workspace from one absolute path — the back-compat
|
|
53
|
+
* bridge for the many call sites that still pass a single `cwd`. The one root is
|
|
54
|
+
* primary and named by its basename.
|
|
55
|
+
*/
|
|
56
|
+
export declare function singleRootWorkspace(absPath: string): Workspace;
|