dsh-extra-paths 0.2.1 → 0.3.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.
Files changed (52) hide show
  1. package/README.md +189 -28
  2. package/README.zh.md +163 -21
  3. package/lib/argv.js +96 -24
  4. package/lib/argv.js.map +1 -1
  5. package/lib/client.js +1169 -467
  6. package/lib/config.js +140 -44
  7. package/lib/config.js.map +1 -1
  8. package/lib/context.js +48 -12
  9. package/lib/context.js.map +1 -1
  10. package/lib/denial.js +132 -0
  11. package/lib/denial.js.map +1 -0
  12. package/lib/index.js +40 -5
  13. package/lib/index.js.map +1 -1
  14. package/lib/name-rule.js +217 -0
  15. package/lib/name-rule.js.map +1 -0
  16. package/lib/paths.js +155 -27
  17. package/lib/paths.js.map +1 -1
  18. package/lib/pattern.js +128 -62
  19. package/lib/pattern.js.map +1 -1
  20. package/lib/platform.js +325 -0
  21. package/lib/platform.js.map +1 -0
  22. package/lib/preview.js +47 -20
  23. package/lib/preview.js.map +1 -1
  24. package/lib/resolve.js +345 -64
  25. package/lib/resolve.js.map +1 -1
  26. package/lib/schema.js +19 -6
  27. package/lib/schema.js.map +1 -1
  28. package/lib/shell-dialect.js +92 -0
  29. package/lib/shell-dialect.js.map +1 -0
  30. package/lib/tool-guard.js +214 -0
  31. package/lib/tool-guard.js.map +1 -0
  32. package/lib/types/argv.d.ts +50 -12
  33. package/lib/types/config.d.ts +29 -13
  34. package/lib/types/context.d.ts +6 -1
  35. package/lib/types/denial.d.ts +88 -0
  36. package/lib/types/index.d.ts +8 -4
  37. package/lib/types/name-rule.d.ts +112 -0
  38. package/lib/types/paths.d.ts +55 -10
  39. package/lib/types/pattern.d.ts +43 -5
  40. package/lib/types/platform.d.ts +148 -0
  41. package/lib/types/preview.d.ts +39 -3
  42. package/lib/types/resolve.d.ts +129 -23
  43. package/lib/types/schema.d.ts +24 -8
  44. package/lib/types/shell-dialect.d.ts +60 -0
  45. package/lib/types/tool-guard.d.ts +103 -0
  46. package/lib/types/wrap-fs.d.ts +86 -4
  47. package/lib/types/wrap-sandbox.d.ts +26 -6
  48. package/lib/wrap-fs.js +323 -47
  49. package/lib/wrap-fs.js.map +1 -1
  50. package/lib/wrap-sandbox.js +149 -12
  51. package/lib/wrap-sandbox.js.map +1 -1
  52. package/package.json +1 -1
@@ -0,0 +1,88 @@
1
+ /**
2
+ * The shared vocabulary of a refusal: which configured rule covers a target, and
3
+ * what the user sees when that rule fires.
4
+ *
5
+ * Why a plain `Error` subclass instead of the host's `FsError`: the plugin must
6
+ * not import an `@deepseek-ai/*` VALUE (every peer is optional and a runtime
7
+ * import would make the plugin fail to load on a host generation that renamed the
8
+ * error class). The tool layer renders a thrown ordinary `Error` as
9
+ * `Error: <message>`, so a self-explanatory message plus a stable
10
+ * {@link ACCESS_DENIED_CODE} is all a consumer needs.
11
+ *
12
+ * `matchDenied` / `matchReadOnly` are the ONE containment test both enforcement
13
+ * surfaces share: they filter by surface and access, then delegate to
14
+ * `isUnder`, whose identity fallback (dev+ino) is what makes a DENY as hard to
15
+ * dodge as a grant (a casing/8.3/WSL alias must not slip past it).
16
+ * @module dsh-extra-paths/denial
17
+ */
18
+ import type { Surface } from './config.js';
19
+ import { type PlatformFacts } from './paths.js';
20
+ import type { ResolvedRule } from './resolve.js';
21
+ /** The stable error code a tool result or a consumer can match on. */
22
+ export declare const ACCESS_DENIED_CODE = "EXTRA_PATHS_ACCESS_DENIED";
23
+ /**
24
+ * A configured `deny`/`read` rule refused one operation.
25
+ *
26
+ * Deliberately an ordinary Error: the message is the user-visible explanation,
27
+ * and the `code` lets a caller distinguish "the plugin said no" from a backend
28
+ * failure without depending on the host's error hierarchy.
29
+ */
30
+ export declare class AccessDeniedError extends Error {
31
+ /** Stable machine-readable reason (see {@link ACCESS_DENIED_CODE}). */
32
+ readonly code: string;
33
+ /**
34
+ * @param message - the user-visible explanation.
35
+ */
36
+ constructor(message: string);
37
+ }
38
+ /**
39
+ * Whether a `deny` rule covers this target on this surface.
40
+ * @param rules - resolved rules (normally a report's `denied` list).
41
+ * @param surface - the enforcement surface asking.
42
+ * @param targetKey - a canonical path.
43
+ * @param facts - host facts (defaults to the discovered ones).
44
+ * @returns the matching deny rule, or undefined (no opinion).
45
+ */
46
+ export declare function matchDenied(rules: readonly ResolvedRule[], surface: Surface, targetKey: string, facts?: PlatformFacts): ResolvedRule | undefined;
47
+ /**
48
+ * Whether a `read` rule covers this target on this surface: reads still pass,
49
+ * writes must be refused (a downgrade, not a denial).
50
+ * @param rules - resolved rules (normally a report's `readOnly` list).
51
+ * @param surface - the enforcement surface asking.
52
+ * @param targetKey - a canonical path.
53
+ * @param facts - host facts (defaults to the discovered ones).
54
+ * @returns the matching read-only rule, or undefined (no opinion).
55
+ */
56
+ export declare function matchReadOnly(rules: readonly ResolvedRule[], surface: Surface, targetKey: string, facts?: PlatformFacts): ResolvedRule | undefined;
57
+ /**
58
+ * The error for a content read refused by a `deny` rule.
59
+ * @param displayPath - the path as the caller spelled it (for the message).
60
+ * @param rule - the rule that refused it.
61
+ * @returns the error to throw.
62
+ */
63
+ export declare function readDenied(displayPath: string, rule: ResolvedRule): AccessDeniedError;
64
+ /**
65
+ * The error for a write refused by a `deny` rule.
66
+ * @param displayPath - the path as the caller spelled it.
67
+ * @param rule - the rule that refused it.
68
+ * @returns the error to throw.
69
+ */
70
+ export declare function writeDenied(displayPath: string, rule: ResolvedRule): AccessDeniedError;
71
+ /**
72
+ * The error for a write refused by a `read` rule: the path is readable but the
73
+ * configuration downgraded it, even inside the writable workspace.
74
+ * @param displayPath - the path as the caller spelled it.
75
+ * @param rule - the rule that downgraded it.
76
+ * @returns the error to throw.
77
+ */
78
+ export declare function writeReadOnly(displayPath: string, rule: ResolvedRule): AccessDeniedError;
79
+ /**
80
+ * The refusal reason a tool-level guard returns (a string, not a throw: the tool
81
+ * layer renders it as `Error: <reason>`). Names the tool, the argument path and
82
+ * the configured rule so the model does not retry with a different spelling.
83
+ * @param toolName - the tool call about to run.
84
+ * @param displayPath - the argument path that matched the rule.
85
+ * @param rule - the rule that refused it.
86
+ * @returns the one-line reason.
87
+ */
88
+ export declare function toolAccessDenied(toolName: string, displayPath: string, rule: ResolvedRule): string;
@@ -26,11 +26,13 @@ export declare const SETTINGS_NAMESPACE = "extra-paths";
26
26
  * are `volatile()` because a settings form only describes volatile fields.
27
27
  */
28
28
  export declare const Config: import("@deepseek-ai/schemastery").default<Schemastery.ObjectS<NoInfer<{
29
- defaultScope: import("@deepseek-ai/schemastery").default<import("./config.js").Scope, import("./config.js").Scope, "defined">;
29
+ defaultAccess: import("@deepseek-ai/schemastery").default<import("./config.js").Access>;
30
+ defaultScope: import("@deepseek-ai/schemastery").default<import("./config.js").Scope>;
30
31
  defaultSurfaces: import("@deepseek-ai/schemastery").default<import("./config.js").Surface[], import("./config.js").Surface[], "defined">;
31
32
  paths: import("@deepseek-ai/schemastery").default<(string | import("./schema.js").ExtraPathsSectionEntry)[], (string | import("./schema.js").ExtraPathsSectionEntry)[], "defined">;
32
33
  }>>, Schemastery.ObjectT<NoInfer<{
33
- defaultScope: import("@deepseek-ai/schemastery").default<import("./config.js").Scope, import("./config.js").Scope, "defined">;
34
+ defaultAccess: import("@deepseek-ai/schemastery").default<import("./config.js").Access>;
35
+ defaultScope: import("@deepseek-ai/schemastery").default<import("./config.js").Scope>;
34
36
  defaultSurfaces: import("@deepseek-ai/schemastery").default<import("./config.js").Surface[], import("./config.js").Surface[], "defined">;
35
37
  paths: import("@deepseek-ai/schemastery").default<(string | import("./schema.js").ExtraPathsSectionEntry)[], (string | import("./schema.js").ExtraPathsSectionEntry)[], "defined">;
36
38
  }>>, "plain">;
@@ -74,11 +76,13 @@ export declare function apply(ctx: PluginContext, config?: RawConfig): void;
74
76
  declare const plugin: typeof apply & {
75
77
  inject: readonly ["sandboxPolicy"];
76
78
  Config: import("@deepseek-ai/schemastery").default<Schemastery.ObjectS<NoInfer<{
77
- defaultScope: import("@deepseek-ai/schemastery").default<import("./config.js").Scope, import("./config.js").Scope, "defined">;
79
+ defaultAccess: import("@deepseek-ai/schemastery").default<import("./config.js").Access>;
80
+ defaultScope: import("@deepseek-ai/schemastery").default<import("./config.js").Scope>;
78
81
  defaultSurfaces: import("@deepseek-ai/schemastery").default<import("./config.js").Surface[], import("./config.js").Surface[], "defined">;
79
82
  paths: import("@deepseek-ai/schemastery").default<(string | import("./schema.js").ExtraPathsSectionEntry)[], (string | import("./schema.js").ExtraPathsSectionEntry)[], "defined">;
80
83
  }>>, Schemastery.ObjectT<NoInfer<{
81
- defaultScope: import("@deepseek-ai/schemastery").default<import("./config.js").Scope, import("./config.js").Scope, "defined">;
84
+ defaultAccess: import("@deepseek-ai/schemastery").default<import("./config.js").Access>;
85
+ defaultScope: import("@deepseek-ai/schemastery").default<import("./config.js").Scope>;
82
86
  defaultSurfaces: import("@deepseek-ai/schemastery").default<import("./config.js").Surface[], import("./config.js").Surface[], "defined">;
83
87
  paths: import("@deepseek-ai/schemastery").default<(string | import("./schema.js").ExtraPathsSectionEntry)[], (string | import("./schema.js").ExtraPathsSectionEntry)[], "defined">;
84
88
  }>>, "plain">;
@@ -0,0 +1,112 @@
1
+ /**
2
+ * NAME RULES: a configured path that is a single component (`.git`, `.env`,
3
+ * `node_modules`) means "that component at any depth", not one literal path.
4
+ *
5
+ * Why: protecting the same handful of names in every repository should not need
6
+ * a full path (or a walk-everything pattern) per checkout. A bare name is the
7
+ * cheapest possible spelling of "this structure anywhere in my projects".
8
+ *
9
+ * Two different jobs, deliberately separate:
10
+ * - {@link findNameComponent} is the LEXICAL, filesystem-free test the fs
11
+ * wrapper runs per call: it walks the target's components under the scope
12
+ * roots and returns the matched component path (or undefined). It costs
13
+ * nothing and applies to files created a moment ago.
14
+ * - {@link expandNameMatches} is the bounded filesystem WALK. It exists ONLY for
15
+ * the settings preview's "N matches right now" display: the shell surface
16
+ * deliberately does not enumerate bare names (that was costly, stale and
17
+ * partial in the real host), so no enforcement path walks. The preview caches
18
+ * the walk and reports when a cap makes the count partial; fs stays exact
19
+ * either way.
20
+ *
21
+ * Scope (chosen by the user): the session workspace plus every configured
22
+ * writable root. A name is compared case-insensitively on a Windows host.
23
+ * @module dsh-extra-paths/name-rule
24
+ */
25
+ import type { Access, Surface } from './config.js';
26
+ import { type PlatformFacts } from './paths.js';
27
+ /** A bare name after classification (the configured spelling is kept for messages). */
28
+ export interface NameRule {
29
+ /** The configured spelling, e.g. `.git`. */
30
+ readonly raw: string;
31
+ /** The component name to match. */
32
+ readonly name: string;
33
+ readonly access: Access;
34
+ readonly surfaces: readonly Surface[];
35
+ }
36
+ /** Caps that keep one name-rule walk bounded (same spirit as the pattern limits). */
37
+ export declare const NAME_RULE_LIMITS: {
38
+ /** Directory entries examined in one expansion pass. */
39
+ readonly maxDirs: 8192;
40
+ /** Directory levels walked below each scope root. */
41
+ readonly maxDepth: 12;
42
+ /** Paths one name rule may match (each match becomes a sandbox rule). */
43
+ readonly maxMatches: 256;
44
+ /** How long one expansion is reused (milliseconds). */
45
+ readonly cacheMs: 30000;
46
+ };
47
+ /** One bounded walk's result. `complete: false` means the list is not exhaustive. */
48
+ export interface NameExpansion {
49
+ /**
50
+ * Concrete matched paths (canonical, sorted, deduplicated). Matches FOUND in a
51
+ * partially walked scope root stay here: denying more concrete paths is always
52
+ * safe, and dropping them would remove shell protection from the very tree the
53
+ * user configured. The warning is what tells the user coverage is partial.
54
+ */
55
+ readonly matches: readonly string[];
56
+ /** A runaway cap stopped the walk; the list is not exhaustive. */
57
+ readonly capped: boolean;
58
+ /** Which cap stopped it. */
59
+ readonly cap?: 'directories' | 'matches';
60
+ /** The depth bound or an unreadable entry cut the walk short. */
61
+ readonly truncated: boolean;
62
+ /**
63
+ * Scope roots whose walk could not be finished (cap, depth, unreadable entry,
64
+ * skipped symlink match). Completeness is tracked PER ROOT so one unreadable or
65
+ * very deep writable root cannot silently discard the matches found in every
66
+ * other root — precisely the failure that made a `.git` rule protect the fs
67
+ * tools but not the shell because `~/.dsh` contains a deep runtime tree.
68
+ */
69
+ readonly incompleteRoots: readonly string[];
70
+ /** Symlink name matches were skipped (a sandbox cannot reliably mask a link). */
71
+ readonly skippedSymlinks: number;
72
+ /** Directory entries examined. */
73
+ readonly scanned: number;
74
+ /** True when every scope root was fully walked (no cap, no skip). */
75
+ readonly complete: boolean;
76
+ }
77
+ /**
78
+ * Build the scope roots of every name rule: the session workspace plus every
79
+ * configured writable root, deduplicated and with nested roots dropped (a root
80
+ * inside another cannot add a match the outer walk would miss).
81
+ * @param workspaceRoot - the session workspace (canonical).
82
+ * @param writableRoots - canonical roots of the configured write entries.
83
+ * @returns scope roots in walk order.
84
+ */
85
+ export declare function scopeRootsOf(workspaceRoot: string, writableRoots: readonly string[], facts: PlatformFacts): readonly string[];
86
+ /**
87
+ * The LEXICAL match: find the first component named `name` on the path from one
88
+ * of the scope roots down to the target. No filesystem access, so a name rule
89
+ * applies to a directory the moment it appears.
90
+ * @param targetKey - a canonical target path.
91
+ * @param name - the component name to match.
92
+ * @param scopeRoots - {@link scopeRootsOf} output.
93
+ * @param facts - host facts.
94
+ * @returns the matched component path (the rule's effective root), or undefined.
95
+ */
96
+ export declare function findNameComponent(targetKey: string, name: string, scopeRoots: readonly string[], facts: PlatformFacts): string | undefined;
97
+ /**
98
+ * The bounded filesystem walk for one name rule: enumerate concrete matches so
99
+ * the shell sandbox can deny them.
100
+ *
101
+ * Matching stops at a match (the rule covers its whole subtree, so descending is
102
+ * wasted work), and the walk never descends into `node_modules`/`.git` (their
103
+ * own names are still matched at their level). Symlinked matches are SKIPPED and
104
+ * counted: a sandbox cannot reliably mask a link destination, so the caller must
105
+ * report the incompleteness instead of pretending the path is covered.
106
+ * @param name - the component name to match.
107
+ * @param scopeRoots - {@link scopeRootsOf} output.
108
+ * @param facts - host facts.
109
+ * @param limits - caps (tests pass a small budget to pin the degraded path).
110
+ * @returns the concrete matches plus the completeness verdict.
111
+ */
112
+ export declare function expandNameMatches(name: string, scopeRoots: readonly string[], facts: PlatformFacts, limits?: typeof NAME_RULE_LIMITS): NameExpansion;
@@ -3,30 +3,75 @@
3
3
  * way the filesystem service canonicalises a write target — realpath of the
4
4
  * deepest EXISTING ancestor plus the remaining suffix — so a not-yet-created
5
5
  * directory still compares equal to the path the enforcement layer computes.
6
+ *
7
+ * The FILE-SYSTEM side lives here; the pure spelling/dialect vocabulary lives
8
+ * in `platform.ts` and is re-used by both halves. Host facts (platform, WSL,
9
+ * home) are always injected, so the Windows and WSL rules are unit-testable on
10
+ * a POSIX machine and the client can share the pure part.
6
11
  * @module dsh-extra-paths/paths
7
12
  */
13
+ import { posix } from 'node:path';
14
+ import { type ExpandOutcome, type PathDialect, type PlatformFacts } from './platform.js';
15
+ export type { ExpandOutcome, PlatformFacts } from './platform.js';
16
+ export { expandPath } from './platform.js';
8
17
  /**
9
- * Resolve one configured path spelling to an absolute path.
10
- * @param raw - the configured spelling: absolute, `~`-anchored, or relative.
18
+ * Whether this Linux host is a WSL guest: WSL exports `WSL_DISTRO_NAME` /
19
+ * `WSL_INTEROP` into the environment, and older releases only expose the
20
+ * Microsoft kernel string.
21
+ * @returns true inside WSL.
22
+ */
23
+ export declare function isWslHost(): boolean;
24
+ /**
25
+ * Discover where Windows drives are mounted inside WSL. `/mnt` is the default,
26
+ * but `/etc/wsl.conf` can remap the automount root, so the REAL mount table is
27
+ * consulted when it is readable; failure falls back to the documented default.
28
+ * @returns the mount root without a trailing slash.
29
+ */
30
+ export declare function detectWslMountRoot(): string;
31
+ /**
32
+ * The host facts the path layer needs, discovered from the running process.
33
+ * @returns a frozen snapshot (cached: platform, WSL flag and home cannot move).
34
+ */
35
+ export declare function discoverPlatformFacts(): PlatformFacts;
36
+ /** Test-only: forget the discovered facts so a different host can be simulated. */
37
+ export declare function resetPlatformFactsCache(): void;
38
+ /** The `node:path` implementation matching one host. */
39
+ export declare function pathApiOf(facts: PlatformFacts): typeof posix;
40
+ /** The dialect of host-native paths. */
41
+ export declare function hostPathDialect(facts?: PlatformFacts): PathDialect;
42
+ /**
43
+ * Resolve one configured spelling to a canonical host-native path.
44
+ * @param raw - the configured spelling: absolute, `~`-anchored, relative, or a
45
+ * path from the other platform (translated when the host can map it).
11
46
  * @param workspaceRoot - the session workspace a relative spelling is resolved against.
12
- * @param home - home directory seam (tests).
13
- * @returns the absolute path, not yet canonicalised.
47
+ * @param facts - host facts (defaults to the discovered ones).
48
+ * @returns the expansion outcome, including the `foreign` reason.
14
49
  */
15
- export declare function expandPath(raw: string, workspaceRoot: string, home?: string): string;
50
+ export declare function expandConfiguredPath(raw: string, workspaceRoot: string, facts?: PlatformFacts): ExpandOutcome;
16
51
  /**
17
52
  * Canonical spelling of a configured root. Symlinked ancestors are resolved
18
53
  * (so `/tmp` and `/private/tmp` cannot silently fail to match), while a suffix
19
54
  * that does not exist yet is appended verbatim.
20
- * @param path - an absolute path.
55
+ * @param path - an absolute path in the host's dialect.
56
+ * @param facts - host facts (defaults to the discovered ones).
21
57
  * @returns the canonical spelling.
22
58
  */
23
- export declare function canonicalizeRoot(path: string): string;
59
+ export declare function canonicalizeRoot(path: string, facts?: PlatformFacts): string;
60
+ /**
61
+ * Whether a canonical path names a volume root on THIS host (translated Windows
62
+ * drive roots included: `/mnt/c` inside WSL).
63
+ * @param path - a canonical host-native path.
64
+ * @param facts - host facts (defaults to the discovered ones).
65
+ * @returns true for a filesystem/volume root.
66
+ */
67
+ export declare function isHostVolumeRootPath(path: string, facts?: PlatformFacts): boolean;
24
68
  /**
25
69
  * Whether a canonical target is a root or lies beneath it. Both sides must
26
- * already be canonical; containment is then purely lexical, which is why the
27
- * caller canonicalises before comparing.
70
+ * already be canonical; containment is then purely lexical, with a filesystem
71
+ * identity fallback for alias-prone hosts (see {@link sharesIdentity}).
28
72
  * @param targetKey - canonical write target.
29
73
  * @param root - canonical configured root.
74
+ * @param facts - host facts (defaults to the discovered ones).
30
75
  * @returns true for the root itself and every descendant.
31
76
  */
32
- export declare function isUnder(targetKey: string, root: string): boolean;
77
+ export declare function isUnder(targetKey: string, root: string, facts?: PlatformFacts): boolean;
@@ -13,16 +13,24 @@
13
13
  * Matching only ever yields directories that EXIST, which is what makes "the path
14
14
  * contains `.worktrees`" a structural condition rather than a spelling.
15
15
  *
16
+ * A Windows spelling is parsed in its OWN dialect first (`C:\a\**`, `\\server\…`):
17
+ * separators are normalised for the grammar, and the literal anchor is translated
18
+ * through `expandConfiguredPath`, so the same configuration also works on the
19
+ * other platform. A spelling whose anchor cannot be expressed on this host is
20
+ * `foreign`: inactive with one warning, never a save failure.
21
+ *
16
22
  * Safety envelope, enforced here and validated again at save time:
17
23
  * - the literal prefix before the first wildcard is the anchor; it must be
18
- * absolute after `~` expansion, must not be a filesystem root, and must not
19
- * CONTAIN the home directory — a pattern can never walk `/`, `/Users` or `~`;
24
+ * absolute in its own dialect after `~` expansion, must not resolve to a
25
+ * volume root (including a translated WSL drive root), and must not CONTAIN
26
+ * the home directory — a pattern can never walk `/`, `/Users` or `~`;
20
27
  * - the walk never descends into `node_modules` or `.git`;
21
28
  * - every cap (depth, directories scanned, matches) makes the WHOLE pattern yield
22
29
  * nothing (fail closed) instead of a partial set: a half-expanded grant would
23
30
  * be an unpredictable permission.
24
31
  * @module dsh-extra-paths/pattern
25
32
  */
33
+ import { type PlatformFacts } from './platform.js';
26
34
  /** Caps that keep one pattern's expansion bounded. */
27
35
  export declare const PATTERN_LIMITS: {
28
36
  /** Directory entries examined in one expansion. */
@@ -41,12 +49,18 @@ export declare const PATTERN_LIMITS: {
41
49
  export interface RootPattern {
42
50
  /** The configured spelling this came from (for messages and the preview). */
43
51
  readonly source: string;
44
- /** The literal directory the walk starts from (absolute). */
52
+ /** The literal directory the walk starts from (absolute, host-native). */
45
53
  readonly anchor: string;
46
54
  /** Segments after the anchor, each `*`, `**`, or a literal name. */
47
55
  readonly segments: readonly string[];
48
56
  /** Trailing `..` segments applied to each match. */
49
57
  readonly up: number;
58
+ /**
59
+ * The host facts the anchor was translated with. The walk must use the SAME
60
+ * dialect as the parser: re-deriving it from `process.platform` would make a
61
+ * pattern parsed for one host walk with another host's `node:path`.
62
+ */
63
+ readonly platform: PlatformFacts;
50
64
  }
51
65
  /** One expansion. */
52
66
  export interface PatternExpansion {
@@ -65,16 +79,40 @@ export interface PatternExpansion {
65
79
  /** Directories examined, for diagnostics. */
66
80
  readonly scanned: number;
67
81
  }
82
+ /** Why a pattern spelling could not be turned into a walkable pattern. */
83
+ export type PatternParseOutcome = {
84
+ readonly ok: true;
85
+ readonly pattern: RootPattern;
86
+ }
87
+ /**
88
+ * Not walkable here. `foreign` is set when the ANCHOR is a spelling of the
89
+ * other platform: the entry must stay inactive with one informative warning
90
+ * (a shared configuration has to remain usable on every host), while every
91
+ * other rejection is a configuration mistake.
92
+ */
93
+ | {
94
+ readonly ok: false;
95
+ readonly foreign?: string;
96
+ };
68
97
  /** Whether a spelling is a pattern rather than a literal path. */
69
98
  export declare function isPattern(raw: string): boolean;
99
+ /**
100
+ * Parse a pattern spelling and keep the reason when it cannot be walked.
101
+ * @param raw - the configured spelling (POSIX or Windows).
102
+ * @param workspaceRoot - the base a relative anchor resolves against.
103
+ * @param facts - host facts (defaults to the discovered ones); the anchor is
104
+ * translated into the host's dialect with these.
105
+ * @returns the pattern, or a rejection that says whether it is merely foreign.
106
+ */
107
+ export declare function parsePatternOutcome(raw: string, workspaceRoot: string, facts?: PlatformFacts): PatternParseOutcome;
70
108
  /**
71
109
  * Parse a pattern spelling.
72
110
  * @param raw - the configured spelling.
73
111
  * @param workspaceRoot - the base a relative anchor resolves against.
74
- * @param home - the home directory to expand `~` with.
112
+ * @param facts - host facts (defaults to the discovered ones).
75
113
  * @returns the parsed pattern, or undefined when it cannot be walked safely.
76
114
  */
77
- export declare function parsePattern(raw: string, workspaceRoot: string, home?: string): RootPattern | undefined;
115
+ export declare function parsePattern(raw: string, workspaceRoot: string, facts?: PlatformFacts): RootPattern | undefined;
78
116
  /**
79
117
  * Expand a pattern against the filesystem.
80
118
  * @param pattern - the parsed pattern.
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Path-dialect vocabulary, spelling classification and the Windows ⇄ WSL
3
+ * translation. Everything here is a PURE string function: no `node:fs`, no
4
+ * `node:path`, no `process` — the caller injects the host facts, which keeps the
5
+ * module importable from the browser client (it is bundled by tsdown) and makes
6
+ * the Windows logic unit-testable on a POSIX CI machine.
7
+ *
8
+ * Why this module exists:
9
+ * - A configuration file is meant to be the SAME on every machine (dotfiles /
10
+ * a repository). A path spelled `C:\Users\me\work` must be understood inside
11
+ * WSL as `/mnt/c/Users/me/work`, and `/mnt/c/Users/me/work` must be understood
12
+ * on a native Windows host. The host half does the translation — the browser
13
+ * client must never infer a platform (under WSL the browser is Windows Chrome
14
+ * while the host is Linux, and a remote web client is a different machine).
15
+ * - A spelling that cannot be expressed on the current host becomes `foreign`
16
+ * and the entry is inactive with a warning; it must NOT fail the save, or one
17
+ * shared configuration could not be used on two platforms.
18
+ *
19
+ * @module dsh-extra-paths/platform
20
+ */
21
+ /** A path spelling family. */
22
+ export type PathDialect = 'posix' | 'windows';
23
+ /** Facts about the host the plugin process runs on. */
24
+ export interface PlatformFacts {
25
+ /** `process.platform` at call time: `darwin` / `linux` / `win32` / … */
26
+ readonly platform: string;
27
+ /** True when the host is a Linux process inside WSL. */
28
+ readonly wsl: boolean;
29
+ /** Where Windows drives are mounted inside WSL (`/mnt` unless remapped). */
30
+ readonly wslMountRoot: string;
31
+ /** The host's home directory, already in the host's native spelling. */
32
+ readonly home: string;
33
+ }
34
+ /** The default WSL drive mount root. */
35
+ export declare const DEFAULT_WSL_MOUNT_ROOT = "/mnt";
36
+ /** How one configured spelling is classified (platform-independent). */
37
+ export type SpellingKind = 'home' | 'posix-absolute' | 'windows-absolute' | 'windows-home' | 'windows-drive-relative' | 'windows-current-drive' | 'relative';
38
+ /** Strip a Windows extended-length prefix, which only adds noise to comparison. */
39
+ export declare function stripWindowsPrefix(spelling: string): string;
40
+ /** The logical host dialect. */
41
+ export declare function hostDialect(facts: PlatformFacts): PathDialect;
42
+ /**
43
+ * Classify one spelling without consulting the host.
44
+ * @param spelling - the raw configured spelling (trimmed by the caller).
45
+ * @returns what kind of path it is.
46
+ */
47
+ export declare function classifySpelling(spelling: string): SpellingKind;
48
+ /**
49
+ * Whether a spelling is a BARE NAME (`.git`, `.env`, `node_modules`): one path
50
+ * component, no separator, no wildcard, not the `.`/`..` relative specials and
51
+ * not a `~` spelling. A bare name is a NAME RULE — it matches that component at
52
+ * any depth under the rule's scope roots rather than naming one literal path.
53
+ *
54
+ * The `./name` spelling is the escape hatch for a literal single-component
55
+ * relative path (it contains a separator, so it stays a normal relative path).
56
+ * @param spelling - the raw configured spelling.
57
+ * @returns true when the spelling is a name rule.
58
+ */
59
+ export declare function isBareNameSpelling(spelling: string): boolean;
60
+ /**
61
+ * Whether a spelling is absolute in its own dialect (a `~` spelling counts:
62
+ * the caller expands home first).
63
+ * @param spelling - the raw configured spelling.
64
+ * @returns true for home, POSIX-absolute and Windows-absolute spellings.
65
+ */
66
+ export declare function isAbsoluteSpelling(spelling: string): boolean;
67
+ /**
68
+ * Normalize a spelling for the pattern grammar: Windows spellings use `/` as the
69
+ * separator for parsing, and the extended-length prefix is dropped first.
70
+ * POSIX spellings are returned verbatim: on POSIX a backslash is a legal
71
+ * FILENAME character and must never be turned into a separator.
72
+ * @param spelling - the raw configured spelling.
73
+ * @returns a spelling whose separators are `/` when it is a Windows path.
74
+ */
75
+ export declare function normalizePatternSpelling(spelling: string): string;
76
+ /** Collapse `.`, `..` and duplicate separators in an absolute POSIX spelling. */
77
+ export declare function collapsePosix(path: string): string;
78
+ /**
79
+ * Collapse an absolute Windows spelling (drive or UNC) into its native form.
80
+ * The result uses `\` separators; case is preserved (Windows comparison folds
81
+ * case later, but the host's `realpath`/ACL layers still want the real spelling).
82
+ * @param path - an absolute Windows spelling using either separator.
83
+ * @returns the collapsed spelling, e.g. `C:\Users\me\x` or `\\server\share\x`.
84
+ */
85
+ export declare function collapseWindows(path: string): string;
86
+ /**
87
+ * A translation outcome: the host-native spelling, or the reason this platform
88
+ * cannot express the configured spelling.
89
+ */
90
+ export type ExpandOutcome = {
91
+ readonly ok: true;
92
+ readonly path: string;
93
+ readonly dialect: PathDialect;
94
+ readonly translated: boolean;
95
+ } | {
96
+ readonly ok: false;
97
+ readonly foreign: string;
98
+ };
99
+ /**
100
+ * Translate a Windows absolute spelling into its WSL spelling.
101
+ * @param spelling - a drive-rooted or UNC spelling (any separator).
102
+ * @param mountRoot - the WSL mount root, normally `/mnt`.
103
+ * @returns the POSIX spelling, or undefined for a share UNC path (no mapping).
104
+ */
105
+ export declare function windowsToWsl(spelling: string, mountRoot?: string): string | undefined;
106
+ /**
107
+ * Translate a POSIX spelling that names a Windows drive into its Windows form.
108
+ * Two conventions are recognized: WSL's `/mnt/<letter>/…` and the MSYS/git-bash
109
+ * `/c/…` form. The caller verifies the result by resolving it (existence is the
110
+ * only proof that the drive really exists on this host).
111
+ * @param spelling - an absolute POSIX spelling.
112
+ * @param mountRoot - the WSL drive mount root, normally `/mnt`.
113
+ * @returns the Windows spelling, or undefined when the spelling is not a drive.
114
+ */
115
+ export declare function posixToWindows(spelling: string, mountRoot?: string): string | undefined;
116
+ /**
117
+ * Whether a spelling names a filesystem/volume root, which would disable the
118
+ * sandbox outright. Covers POSIX `/`, a drive root (`C:`, `C:\`, `C:/`) and a
119
+ * UNC SHARE root (`\\server\share`), which is as broad as a drive for a share.
120
+ * @param spelling - a path spelling (either dialect).
121
+ * @returns true when the spelling is a volume root.
122
+ */
123
+ export declare function isVolumeRootSpelling(spelling: string): boolean;
124
+ /**
125
+ * Whether a canonical, host-native path names a volume root, including the WSL
126
+ * spelling of a Windows drive root (`/mnt/c`). The literal-root rejection runs
127
+ * on the CANONICAL path, so translation (`C:\` ⇒ `/mnt/c`) must be caught here.
128
+ * @param path - a host-native absolute path.
129
+ * @param facts - the host facts.
130
+ * @returns true for a volume root.
131
+ */
132
+ export declare function isHostVolumeRoot(path: string, facts: PlatformFacts): boolean;
133
+ /**
134
+ * Resolve one configured spelling to a HOST-NATIVE absolute path.
135
+ *
136
+ * Translation happens only when the spelling's dialect differs from the host's:
137
+ * a Windows spelling inside WSL becomes `/mnt/<drive>/…`, a `/mnt/<drive>/…`
138
+ * spelling on a native Windows host becomes `<Drive>:\…`. A spelling with no
139
+ * mapping on this host is `foreign`: the caller marks the entry inactive with a
140
+ * warning instead of failing the whole configuration.
141
+ * @param spelling - the configured spelling (already trimmed).
142
+ * @param workspaceRoot - the session workspace a relative spelling resolves against.
143
+ * @param facts - host platform facts.
144
+ * @returns the host-native absolute path, or the reason it is foreign.
145
+ */
146
+ export declare function expandPath(spelling: string, workspaceRoot: string, facts: PlatformFacts): ExpandOutcome;
147
+ /** Marker used in warnings for entries that only apply on another platform. */
148
+ export declare function foreignPathMessage(spelling: string, reason: string): string;
@@ -1,26 +1,61 @@
1
1
  /**
2
- * The settings page's effective-roots preview: one authenticated read-only
3
- * route that resolves the entries the page asks about for a workspace the page
2
+ * The settings page's effective-rules preview: one authenticated read-only route
3
+ * that resolves the entries the page asks about for a workspace the page
4
4
  * supplies. It never probes arbitrary paths — every entry still goes through
5
5
  * `normalizeConfig` + `resolveRoots`, the same pair the enforcement layers use —
6
6
  * and it computes only path spellings, never file contents.
7
+ *
8
+ * The payload also carries the HOST's platform facts and shell capability: the
9
+ * browser cannot know either (under WSL it runs as Windows Chrome while the host
10
+ * is Linux, and a remote client is a different machine entirely), so the page
11
+ * only ever displays the host's answers.
7
12
  * @module dsh-extra-paths/preview
8
13
  */
9
14
  import { type ExtraPathsConfig, type RawConfig } from './config.js';
15
+ import { type PathDialect, type PlatformFacts } from './platform.js';
10
16
  import { type EntryResolution } from './resolve.js';
17
+ import { type ShellCapability } from './shell-dialect.js';
11
18
  /** The single route this plugin owns. */
12
19
  export declare const PREVIEW_PATH = "/extra-paths/resolved";
13
20
  /** The most entries one preview request may carry (a settings page sends its rows). */
14
21
  export declare const MAX_PREVIEW_ENTRIES = 64;
22
+ /** The host's path dialect as the page needs it (facts it must not infer itself). */
23
+ export interface PreviewPlatform {
24
+ /** `process.platform` of the host half. */
25
+ readonly platform: string;
26
+ /** True when the host runs inside WSL. */
27
+ readonly wsl: boolean;
28
+ /** Where Windows drives are mounted inside WSL (`/mnt` unless remapped). */
29
+ readonly wslMountRoot: string;
30
+ /** The host's native path dialect. */
31
+ readonly dialect: PathDialect;
32
+ }
15
33
  /** The preview payload. */
16
34
  export interface PreviewResponse {
17
35
  readonly workspaceCwd: string;
18
36
  /** The host's home directory, so the page can resolve `~` locally too. */
19
37
  readonly home: string;
38
+ readonly platform: PreviewPlatform;
39
+ /** What the shell surface can enforce on this host. */
40
+ readonly capabilities: ShellCapability;
20
41
  readonly entries: readonly EntryResolution[];
42
+ /** Writable roots (the original name of {@link writable}). */
21
43
  readonly effective: readonly string[];
44
+ /** The same writable roots under the 0.3.0 name. */
45
+ readonly writable: readonly string[];
46
+ /** Roots that are readable but NOT writable. */
47
+ readonly readOnly: readonly string[];
48
+ /** Roots that are neither readable nor writable. */
49
+ readonly denied: readonly string[];
22
50
  readonly warnings: readonly string[];
23
51
  }
52
+ /** Optional host inputs, injected by tests (the route uses the discovered ones). */
53
+ export interface PreviewOptions {
54
+ /** Host facts; defaults to {@link discoverPlatformFacts}. */
55
+ readonly facts?: PlatformFacts;
56
+ /** Shell capability override; defaults to {@link shellCapabilityOf}(platform). */
57
+ readonly capability?: ShellCapability;
58
+ }
24
59
  /**
25
60
  * Parse the optional `paths` query parameter: the page's UNSAVED rows, so the
26
61
  * preview can answer for what the user is typing instead of only for what is
@@ -36,11 +71,12 @@ export declare function parseStagedPaths(raw: string | null | undefined): RawCon
36
71
  * @param workspaceRoot - the workspace the page is asking about.
37
72
  * @param staged - the page's unsaved rows, when it sent any: they REPLACE the
38
73
  * stored entries so the preview answers for the draft, not for the document.
74
+ * @param options - optional host inputs (tests); defaults to the live host.
39
75
  * @returns the preview payload.
40
76
  * @throws {Error} when no usable workspace was supplied or the staged rows are
41
77
  * invalid (the caller answers 400).
42
78
  */
43
- export declare function buildPreview(config: ExtraPathsConfig, workspaceRoot: string | undefined, staged?: RawConfig | undefined): PreviewResponse;
79
+ export declare function buildPreview(config: ExtraPathsConfig, workspaceRoot: string | undefined, staged?: RawConfig | undefined, options?: PreviewOptions): PreviewResponse;
44
80
  /** The route-registration context surface (webServer + connection). */
45
81
  export interface PreviewRouteContext {
46
82
  webServer: {