@xmoon76/dsh-pi-tui 0.5.4 → 0.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +28 -7
- package/README.md +25 -6
- package/dist/application-runtime-CbBO4bY-.mjs +6860 -0
- package/dist/bootstrap-DLOydLLj.mjs +108410 -0
- package/dist/builtins.mjs +2 -3
- package/dist/client-bootstrap-DwSFzcTW.mjs +872 -0
- package/dist/context-summary-Bz0AGXa1.mjs +12027 -0
- package/dist/{context-summary-DAisTA_7.mjs → context-summary-D9mhxPXL.mjs} +1468 -10
- package/dist/custom-items-CKrOBCox.mjs +655 -0
- package/dist/disposal-BYBbEpVY.mjs +54 -0
- package/dist/dispose-cause-BzcXo4hu.mjs +123 -0
- package/dist/dist-C7QMk0ws.mjs +15431 -0
- package/dist/{entry-control-DvSrndWd.mjs → entry-control-DnduyDiw.mjs} +12 -31
- package/dist/{extensions-CcuMY4n2.mjs → extensions-pcFiH7Qe.mjs} +758 -68
- package/dist/extensions.mjs +1 -1
- package/dist/host-bootstrap-CKdfLkNS.mjs +188 -0
- package/dist/host-foundation-probe-CYHsbAw6.mjs +143 -0
- package/dist/host-prerequisites-9JBTtVNu.mjs +36 -0
- package/dist/host-runtime.d.mts +26 -0
- package/dist/host-runtime.mjs +450 -0
- package/dist/index.d.mts +8 -1
- package/dist/index.mjs +5192 -2118
- package/dist/local-client.mjs +1681 -188
- package/dist/local-discovery-CXnLT4GF.mjs +755 -0
- package/dist/local-discovery-DVFFpfBG.mjs +2 -0
- package/dist/local-host.mjs +927 -78
- package/dist/local-transport-C9aonplC.mjs +513 -0
- package/dist/performance-host.d.mts +2 -2
- package/dist/performance-host.mjs +258 -1
- package/dist/pi-tui-remote-contribution-DRk6Tl3J.mjs +78 -0
- package/dist/pi-tui-terminal-progress-contract-CdBH9j78.mjs +70 -0
- package/dist/pi-tui-terminal-progress-contract-XTQKj-WW.mjs +905 -0
- package/dist/{runtime-vc4mdr-R.mjs → runtime-Bh5OsftD.mjs} +655 -675
- package/dist/runtime-C5ROZqFj.mjs +2 -0
- package/dist/{session-tdJHLnJR.mjs → session-4keoXjk0.mjs} +2341 -217
- package/dist/session-B2zqv4wr.mjs +10869 -0
- package/dist/{startup-JaVZoTIl.mjs → startup-Ct2UUjUL.mjs} +7 -6
- package/dist/startup.d.mts +1 -1
- package/dist/startup.mjs +1 -1
- package/dist/{client-bootstrap-F5DHFI9t.mjs → terminal-progress-source-BhufrZ74.mjs} +3431 -3007
- package/dist/terminal-progress-source-CsBinCJ_.mjs +965 -0
- package/package.json +125 -116
- package/dist/host-bootstrap-DAgKknbY.mjs +0 -119
- package/dist/performance-host-CcDJjjVA.mjs +0 -266
- package/dist/rolldown-runtime-8H4AJuhK.mjs +0 -14
- package/dist/terminal-progress-source-xor3vakJ.mjs +0 -2256
|
@@ -0,0 +1,905 @@
|
|
|
1
|
+
import { accessSync, constants, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { delimiter, join, posix, win32 } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
//#region src/runtime/direct/working-directory-direct.ts
|
|
6
|
+
/**
|
|
7
|
+
* The effective directory of one live Agent's Session: the official
|
|
8
|
+
* `workingDirectory.get(session)` value when the service is mounted, else the
|
|
9
|
+
* immutable header cwd — the INITIAL committed value of that same projection,
|
|
10
|
+
* so a minimal composition without the row keeps the historical read instead
|
|
11
|
+
* of inventing a directory.
|
|
12
|
+
*
|
|
13
|
+
* @param workingDirectory - the official service, or `undefined` when the
|
|
14
|
+
* composition does not mount the row.
|
|
15
|
+
* @param agent - the exact live Agent whose Session is read.
|
|
16
|
+
* @returns the effective absolute directory, or `undefined` when neither the
|
|
17
|
+
* service nor a header cwd can answer (never a Client-side guess).
|
|
18
|
+
*/
|
|
19
|
+
function effectiveAgentCwd(workingDirectory, agent) {
|
|
20
|
+
if (workingDirectory === void 0) return agent.session.header.cwd;
|
|
21
|
+
return workingDirectory.get(agent.session);
|
|
22
|
+
}
|
|
23
|
+
/** The effective directory recorded by one official `workingDirectory`
|
|
24
|
+
* projection SNAPSHOT value: its wire view is `string | null`, where `null`
|
|
25
|
+
* is the legal "no directory recorded" state (the header had none) — an
|
|
26
|
+
* absent/foreign value reads `undefined` (unavailable), never a directory. */
|
|
27
|
+
function projectedWorkingDirectory(value) {
|
|
28
|
+
return typeof value === "string" && value !== "" ? value : void 0;
|
|
29
|
+
}
|
|
30
|
+
/** Escape regex metacharacters for a finder's regex pattern: the completion
|
|
31
|
+
* term is a LITERAL substring (the scoring model's input), so a filename
|
|
32
|
+
* containing `[`, `+`, `.` etc. must match as literal text, never as a regex.
|
|
33
|
+
* Both locality adapters pass the escaped term. */
|
|
34
|
+
function escapeFinderRegex(term) {
|
|
35
|
+
return term.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Normalize one finder's stdout into detached records: NUL records when the
|
|
39
|
+
* finder honoured `--print0` (so legal filenames containing newlines survive),
|
|
40
|
+
* the newline fallback for a fake/older finder (preserving meaningful leading
|
|
41
|
+
* and trailing spaces in a filename), the `./` prefix strip, the directory
|
|
42
|
+
* hint and the `.git` exclusion. PURE — operates solely on detached strings.
|
|
43
|
+
*/
|
|
44
|
+
function normalizeFinderRecords(stdout) {
|
|
45
|
+
const records = stdout.includes("\0") ? stdout.split("\0").filter((record) => record !== "") : (() => {
|
|
46
|
+
const lines = stdout.split(/\r?\n/);
|
|
47
|
+
if (lines.at(-1) === "") lines.pop();
|
|
48
|
+
return lines.filter((line) => line !== "");
|
|
49
|
+
})();
|
|
50
|
+
const out = [];
|
|
51
|
+
for (const line of records) {
|
|
52
|
+
const relative = line.startsWith("./") ? line.slice(2) : line;
|
|
53
|
+
if (relative === ".git" || relative.startsWith(".git/") || relative.includes("/.git/")) continue;
|
|
54
|
+
out.push({
|
|
55
|
+
path: relative.endsWith("/") ? relative.slice(0, -1) : relative,
|
|
56
|
+
directoryHint: relative.endsWith("/")
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The bounded recursive scan of one directory's SUBTREE: the root's direct
|
|
63
|
+
* children are always complete (a root-level `src/` must be found), deeper
|
|
64
|
+
* entries are capped at {@link MAX_FALLBACK_SCAN}. Paths are relative to
|
|
65
|
+
* `baseDir` (`sub/deep.ts`); `.git` is skipped (kimi parity); a symlinked
|
|
66
|
+
* directory is NEVER descended — at the search root and below it alike
|
|
67
|
+
* (cycle safety), so a link to a parent directory or to the workspace's
|
|
68
|
+
* outside cannot widen the scan or consume the traversal budget.
|
|
69
|
+
*
|
|
70
|
+
* ASYNC: the fuzzy fallback runs on the EDITOR path, so a synchronous
|
|
71
|
+
* whole-tree traversal would block the editor's event loop per keystroke. The
|
|
72
|
+
* scan yields to the event loop between directory levels and checks the
|
|
73
|
+
* AbortSignal inside the loop — the editor stays responsive and a cancelled
|
|
74
|
+
* request stops the traversal promptly.
|
|
75
|
+
*/
|
|
76
|
+
async function scanSubtree(baseDir, driver, signal) {
|
|
77
|
+
const candidates = [];
|
|
78
|
+
const firstEntries = await driver.listDirectory(baseDir, "", signal);
|
|
79
|
+
if (signal.aborted || firstEntries === null) return [];
|
|
80
|
+
const stack = [];
|
|
81
|
+
for (const entry of firstEntries) {
|
|
82
|
+
if (entry.name === ".git") continue;
|
|
83
|
+
candidates.push({
|
|
84
|
+
path: entry.name,
|
|
85
|
+
kind: entry.kind
|
|
86
|
+
});
|
|
87
|
+
if (entry.descendable) stack.push(entry.name);
|
|
88
|
+
}
|
|
89
|
+
let scanned = 0;
|
|
90
|
+
while (stack.length > 0 && scanned < 2e3) {
|
|
91
|
+
if (signal.aborted) break;
|
|
92
|
+
const relativeDir = stack.pop() ?? "";
|
|
93
|
+
const entries = await driver.listDirectory(baseDir, relativeDir, signal);
|
|
94
|
+
if (entries === null) continue;
|
|
95
|
+
for (const entry of entries) {
|
|
96
|
+
if (signal.aborted || scanned >= 2e3) break;
|
|
97
|
+
if (entry.name === ".git") continue;
|
|
98
|
+
const path = `${relativeDir}/${entry.name}`;
|
|
99
|
+
scanned += 1;
|
|
100
|
+
candidates.push({
|
|
101
|
+
path,
|
|
102
|
+
kind: entry.kind
|
|
103
|
+
});
|
|
104
|
+
if (entry.kind === "directory" && entry.descendable) stack.push(path);
|
|
105
|
+
}
|
|
106
|
+
if (stack.length > 0) await new Promise((resolve) => setImmediate(resolve));
|
|
107
|
+
}
|
|
108
|
+
return signal.aborted ? [] : candidates;
|
|
109
|
+
}
|
|
110
|
+
/** Map one driver listing into path candidates, dropping `.git` (a `@` or
|
|
111
|
+
* `/image` listing never presents VCS internals). PURE. */
|
|
112
|
+
function listingCandidates(entries) {
|
|
113
|
+
return entries.filter((entry) => entry.name !== ".git").map((entry) => ({
|
|
114
|
+
path: entry.name,
|
|
115
|
+
kind: entry.kind
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Answer one resolved query with discovery facts: a scoped LISTING (empty
|
|
120
|
+
* term) reads the target directory's direct children; a scoped or whole-tree
|
|
121
|
+
* FUZZY query uses the locality finder when available and the bounded subtree
|
|
122
|
+
* scan otherwise. Paths are relative to the QUERY's search base.
|
|
123
|
+
* @param query - the resolved path query (searchBase already absolute).
|
|
124
|
+
* @param driver - the locality's discovery boundary.
|
|
125
|
+
* @param signal - the editor's request abort.
|
|
126
|
+
*/
|
|
127
|
+
async function discoverForQuery(query, driver, signal) {
|
|
128
|
+
if (query.explicitScope && query.searchTerm === "") {
|
|
129
|
+
if (signal.aborted) return [];
|
|
130
|
+
const direct = await driver.listDirectory(query.searchBase, "", signal);
|
|
131
|
+
if (signal.aborted || direct === null) return [];
|
|
132
|
+
return listingCandidates(direct);
|
|
133
|
+
}
|
|
134
|
+
if (!query.winAbsolute && !query.raw.includes("\\")) {
|
|
135
|
+
const found = await driver.find(query.searchBase, query.searchTerm, signal);
|
|
136
|
+
if (signal.aborted) return [];
|
|
137
|
+
if (found !== null) {
|
|
138
|
+
const seen = new Set(found.map((candidate) => candidate.path));
|
|
139
|
+
const lowerTerm = query.searchTerm.toLowerCase();
|
|
140
|
+
const direct = await driver.listDirectory(query.searchBase, "", signal);
|
|
141
|
+
if (signal.aborted) return [];
|
|
142
|
+
const merged = (direct === null ? [] : listingCandidates(direct)).filter((candidate) => lowerTerm === "" || candidate.path.toLowerCase().includes(lowerTerm)).filter((candidate) => !seen.has(candidate.path));
|
|
143
|
+
return [...found, ...merged];
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return scanSubtree(query.searchBase, driver, signal);
|
|
147
|
+
}
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/domain/file-completion/query.ts
|
|
150
|
+
/**
|
|
151
|
+
* The neutral path query resolver (TS8-A, split out of the historical
|
|
152
|
+
* `src/file-completion/query.ts`): PURE — turns a raw token into the search
|
|
153
|
+
* directory, the basename term, the display prefix and whether the token
|
|
154
|
+
* names an EXPLICIT scope. No filesystem access, and no environment
|
|
155
|
+
* discovery: the facts this algebra needs arrive from the LOCALITY owner as
|
|
156
|
+
* an explicit {@link PathQueryEnvironment}, never from `homedir()` /
|
|
157
|
+
* `process.platform` inside the domain layer.
|
|
158
|
+
*
|
|
159
|
+
* A scoped token (`src/fo`, `../foo`, `../../foo`, `~/foo`, `/tmp/foo`,
|
|
160
|
+
* a drive-qualified path, or an UNC path) searches ONLY its resolved
|
|
161
|
+
* directory — never a whole-tree scan filtered by string. An unscoped token
|
|
162
|
+
* (`foo`, `nested`, `config`) is a whole-tree fuzzy query with an EMPTY
|
|
163
|
+
* display base.
|
|
164
|
+
* @module @xmoon76/dsh-pi-tui/domain/file-completion/query
|
|
165
|
+
*/
|
|
166
|
+
/**
|
|
167
|
+
* The HOST filesystem's path algebra, selected from the EXPLICIT
|
|
168
|
+
* `windowsHost` fact. Every host-native path this module computes (a joined
|
|
169
|
+
* scope, an expanded `~`) goes through this api, so the domain never infers
|
|
170
|
+
* the running machine from the ambient `node:path` default export: a caller
|
|
171
|
+
* that injects a foreign host's facts gets that host's algebra.
|
|
172
|
+
*/
|
|
173
|
+
function hostPathApi(windowsHost) {
|
|
174
|
+
return windowsHost ? win32 : posix;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Return the final path component without depending on the host OS dialect.
|
|
178
|
+
* Completion can present a Windows path while the client process is running
|
|
179
|
+
* on POSIX (and vice versa), so `node:path.basename` is not sufficient here.
|
|
180
|
+
*/
|
|
181
|
+
function basenameOfPath(path) {
|
|
182
|
+
const separator = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
|
|
183
|
+
return path.slice(separator + 1);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Expand a leading `~` in one token against the locality's home directory
|
|
187
|
+
* (other tokens unchanged). PURE — both the home directory AND the host
|
|
188
|
+
* platform are explicit facts: the joined result is a path on the HOST
|
|
189
|
+
* filesystem, so it uses that host's algebra.
|
|
190
|
+
* @param token - the raw token.
|
|
191
|
+
* @param homeDir - the locality owner's home directory fact.
|
|
192
|
+
* @param windowsHost - whether the filesystem host is Windows.
|
|
193
|
+
*/
|
|
194
|
+
function expandHomeToken(token, homeDir, windowsHost) {
|
|
195
|
+
if (token === "~") return homeDir;
|
|
196
|
+
if (token.startsWith("~/") || token.startsWith("~\\")) return hostPathApi(windowsHost).join(homeDir, token.slice(2).replace(/\\/g, "/"));
|
|
197
|
+
return token;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Choose the separator immediately before the basename. This preserves a
|
|
201
|
+
* mixed Windows token's actual dialect (forward-slash drive paths stay
|
|
202
|
+
* forward-slashed, while backslash drive paths stay backslashed) instead of
|
|
203
|
+
* normalizing it to the host platform's preferred separator.
|
|
204
|
+
*/
|
|
205
|
+
function separatorOfRaw(raw, windowsDialect) {
|
|
206
|
+
const slash = raw.lastIndexOf("/");
|
|
207
|
+
const backslash = raw.lastIndexOf("\\");
|
|
208
|
+
if (slash === -1 && backslash === -1) return windowsDialect ? "\\" : "/";
|
|
209
|
+
return backslash > slash ? "\\" : "/";
|
|
210
|
+
}
|
|
211
|
+
/** Whether the token is a leading-home form, including `~\\foo`. */
|
|
212
|
+
function isHomeToken$1(raw) {
|
|
213
|
+
return raw === "~" || raw.startsWith("~/") || raw.startsWith("~\\");
|
|
214
|
+
}
|
|
215
|
+
/** Whether one raw token is genuinely Windows-dialect. */
|
|
216
|
+
function isWindowsDialect(raw, winAbsolute) {
|
|
217
|
+
return winAbsolute || raw.includes("\\");
|
|
218
|
+
}
|
|
219
|
+
/** Whether an absolute path uses the Windows drive/UNC grammar. */
|
|
220
|
+
function isWindowsAbsolute(raw) {
|
|
221
|
+
return !raw.startsWith("/") && win32.isAbsolute(raw);
|
|
222
|
+
}
|
|
223
|
+
/** Join a relative scope while keeping POSIX fixtures usable for a
|
|
224
|
+
* Windows-looking token. The result is a path on the HOST filesystem, so the
|
|
225
|
+
* joiner comes from the EXPLICIT host fact (never `process.platform`); the
|
|
226
|
+
* TOKEN's own dialect only decides whether backslashes are the user's
|
|
227
|
+
* separators or ordinary characters.
|
|
228
|
+
*
|
|
229
|
+
* A `windowsDialect` token on a POSIX host has backslashes in the USER'S
|
|
230
|
+
* dialect, not as filesystem separators, so they are normalized; an
|
|
231
|
+
* unmistakable drive/UNC `cwd` selects the win32 joiner even on a POSIX host
|
|
232
|
+
* (that cwd string is itself a Windows path). */
|
|
233
|
+
function joinRelativeScope(cwd, rawPath, windowsDialect, windowsHost) {
|
|
234
|
+
const api = hostPathApi(windowsHost);
|
|
235
|
+
if (!windowsDialect) return api.join(cwd, rawPath);
|
|
236
|
+
const windowsCwd = /^[A-Za-z]:[\\/]/.test(cwd) || cwd.startsWith("\\\\");
|
|
237
|
+
if (windowsHost || windowsCwd) return win32.join(cwd, rawPath);
|
|
238
|
+
return api.join(cwd, rawPath.replace(/\\/g, "/"));
|
|
239
|
+
}
|
|
240
|
+
/** Whether the raw token names a directory that should be LISTED
|
|
241
|
+
* (`searchTerm === ''`). `.`/`..` root forms list their own relative
|
|
242
|
+
* directory; `./`/`../`/`~`/`/` list the scope root. */
|
|
243
|
+
function isBareScopeForm(raw) {
|
|
244
|
+
return raw === "" || raw === "." || raw === ".." || raw === "./" || raw === "../" || raw === "~" || raw === "~/" || raw === "~\\" || raw === "/" || raw === ".\\" || raw === "..\\";
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Resolve ONE raw completion token into the pure path query.
|
|
248
|
+
* @param raw - the token as typed (quotes stripped; leading separator
|
|
249
|
+
* whitespace stripped by the caller).
|
|
250
|
+
* @param cwd - the completion base (the Client cwd for local completion, the
|
|
251
|
+
* Direct workspace cwd for the WORKSPACE compatibility scanner; relative
|
|
252
|
+
* forms resolve against it).
|
|
253
|
+
* @param environment - the locality owner's explicit environment facts.
|
|
254
|
+
*/
|
|
255
|
+
function resolvePathQuery(raw, cwd, environment) {
|
|
256
|
+
const expanded = expandHomeToken(raw, environment.homeDir, environment.windowsHost);
|
|
257
|
+
const posixAbsolute = raw.startsWith("/");
|
|
258
|
+
const winAbsolute = isWindowsAbsolute(raw);
|
|
259
|
+
const windowsDialect = isWindowsDialect(raw, winAbsolute);
|
|
260
|
+
const absolute = isHomeToken$1(raw) || posixAbsolute || winAbsolute;
|
|
261
|
+
const tokenWindows = windowsDialect || environment.windowsHost;
|
|
262
|
+
const pathDirname = tokenWindows ? win32.dirname : posix.dirname;
|
|
263
|
+
const pathBasename = tokenWindows ? win32.basename : posix.basename;
|
|
264
|
+
const separator = separatorOfRaw(raw, windowsDialect);
|
|
265
|
+
if (isBareScopeForm(raw)) return {
|
|
266
|
+
raw,
|
|
267
|
+
searchBase: raw === "." || raw === ".." || raw === "./" || raw === "../" || raw === ".\\" || raw === "..\\" ? joinRelativeScope(cwd, raw, windowsDialect, environment.windowsHost) : absolute ? expanded : joinRelativeScope(cwd, expanded, windowsDialect, environment.windowsHost),
|
|
268
|
+
searchTerm: "",
|
|
269
|
+
displayBase: raw === "" ? "" : raw === "~" ? `~${separator}` : raw.endsWith("/") || raw.endsWith("\\") ? raw : `${raw}${separator}`,
|
|
270
|
+
explicitScope: true,
|
|
271
|
+
winAbsolute
|
|
272
|
+
};
|
|
273
|
+
if (raw.endsWith("/") || raw.endsWith("\\")) return {
|
|
274
|
+
raw,
|
|
275
|
+
searchBase: absolute ? expanded : joinRelativeScope(cwd, expanded, windowsDialect, environment.windowsHost),
|
|
276
|
+
searchTerm: "",
|
|
277
|
+
displayBase: raw,
|
|
278
|
+
explicitScope: true,
|
|
279
|
+
winAbsolute
|
|
280
|
+
};
|
|
281
|
+
const lastSlash = Math.max(raw.lastIndexOf("/"), raw.lastIndexOf("\\"));
|
|
282
|
+
const hasSeparator = lastSlash >= 0;
|
|
283
|
+
const displayBase = hasSeparator ? raw.slice(0, lastSlash + 1) : "";
|
|
284
|
+
const rawDir = hasSeparator ? raw.slice(0, lastSlash) : "";
|
|
285
|
+
const searchTerm = pathBasename(expanded);
|
|
286
|
+
const explicitScope = hasSeparator || absolute;
|
|
287
|
+
const searchBaseForScope = isHomeToken$1(raw) ? expandHomeToken(rawDir, environment.homeDir, environment.windowsHost) : absolute ? pathDirname(expanded) : joinRelativeScope(cwd, rawDir, windowsDialect, environment.windowsHost);
|
|
288
|
+
return {
|
|
289
|
+
raw,
|
|
290
|
+
searchBase: explicitScope ? searchBaseForScope : cwd,
|
|
291
|
+
searchTerm,
|
|
292
|
+
displayBase,
|
|
293
|
+
explicitScope,
|
|
294
|
+
winAbsolute
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Reattach the query's display base onto one discovered candidate path.
|
|
299
|
+
* Discovery returns paths RELATIVE to the query's search base; the candidate
|
|
300
|
+
* the user accepts must read in the USER'S dialect (`../sibling-file.ts`,
|
|
301
|
+
* `~/pics/a.png`, `src/deep.ts`, `/tmp/x`). The SOURCE calls this before its
|
|
302
|
+
* candidates cross the port contract (the port's paths are user-facing).
|
|
303
|
+
* PURE.
|
|
304
|
+
*/
|
|
305
|
+
function reattachDisplayBase(candidate, query) {
|
|
306
|
+
if (query.displayBase === "") return candidate;
|
|
307
|
+
return {
|
|
308
|
+
...candidate,
|
|
309
|
+
path: `${query.displayBase}${candidate.path}`
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
/** Score one candidate against the query term (lowercased). */
|
|
313
|
+
function scorePathCandidate(candidate, lowerQuery) {
|
|
314
|
+
if (lowerQuery === "") {
|
|
315
|
+
const depthPenalty = candidate.path.split(/[\\/]/).length - 1;
|
|
316
|
+
return (candidate.kind === "directory" ? 120 : 100) - depthPenalty;
|
|
317
|
+
}
|
|
318
|
+
const lowerPath = candidate.path.toLowerCase();
|
|
319
|
+
const lowerBase = basenameOfPath(candidate.path).toLowerCase();
|
|
320
|
+
let score = 0;
|
|
321
|
+
if (lowerBase === lowerQuery) score = 100;
|
|
322
|
+
else if (lowerBase.startsWith(lowerQuery)) score = 80;
|
|
323
|
+
else if (lowerBase.includes(lowerQuery)) score = 50;
|
|
324
|
+
else if (lowerPath.includes(lowerQuery)) score = 30;
|
|
325
|
+
if (candidate.kind === "directory" && score > 0) score += 10;
|
|
326
|
+
return score;
|
|
327
|
+
}
|
|
328
|
+
/** Deterministic order: score desc, directories (kind) first, path asc. */
|
|
329
|
+
function compareScoredPaths(left, right) {
|
|
330
|
+
if (left.score !== right.score) return right.score - left.score;
|
|
331
|
+
if (left.candidate.kind !== right.candidate.kind) return left.candidate.kind === "directory" ? -1 : 1;
|
|
332
|
+
return left.candidate.path.localeCompare(right.candidate.path);
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Rank, filter and bound one discovery set (the local scoring model — the
|
|
336
|
+
* SOURCE's own order for its Client-fs / Direct-workspace-compat consumers,
|
|
337
|
+
* NOT the live Session `@` mention path, whose ranking is the official Host
|
|
338
|
+
* authority's). PURE: path candidates in, ranked path candidates out — no UI
|
|
339
|
+
* DTOs, so a semantic adapter can own its ranking end-to-end.
|
|
340
|
+
*/
|
|
341
|
+
function rankPathCandidates(candidates, term) {
|
|
342
|
+
const lowerQuery = term.toLowerCase();
|
|
343
|
+
return candidates.map((candidate) => ({
|
|
344
|
+
candidate,
|
|
345
|
+
score: scorePathCandidate(candidate, lowerQuery)
|
|
346
|
+
})).filter((entry) => entry.score > 0).sort(compareScoredPaths).slice(0, 50).map((entry) => entry.candidate);
|
|
347
|
+
}
|
|
348
|
+
//#endregion
|
|
349
|
+
//#region src/runtime/direct/file-completion/host-discovery.ts
|
|
350
|
+
/**
|
|
351
|
+
* The Direct Host discovery implementation (TS8-A, renamed by TS8-HF1): the
|
|
352
|
+
* DIRECT process's filesystem behind the neutral {@link LocalDiscoveryDriver}
|
|
353
|
+
* contract. It has two legitimate consumers:
|
|
354
|
+
*
|
|
355
|
+
* ```text
|
|
356
|
+
* 1. the sessionless `HostFileScope { kind: 'workspace', cwd }` compatibility
|
|
357
|
+
* path of `DirectHostFilePort` (the cold surface with no official carrier);
|
|
358
|
+
* 2. the explicit-path `@` navigation route of the Session scope
|
|
359
|
+
* (`runtime/direct/host-file-augmentation-direct.ts`), where the user
|
|
360
|
+
* explicitly typed the scope (`@src/`, `@../`, `@/abs`, `@~/`).
|
|
361
|
+
* ```
|
|
362
|
+
*
|
|
363
|
+
* A BARE Session `@` query (`@foo`) never runs through this adapter — that
|
|
364
|
+
* path is the OFFICIAL Host authority's (`ctx.fileReferences.list` through the
|
|
365
|
+
* port), whose candidates arrive already filtered, ranked and bounded.
|
|
366
|
+
*
|
|
367
|
+
* It owns the Direct Host process's own PATH/PATHEXT finder detection, its
|
|
368
|
+
* `node:fs` facts and its fd/fdfind process. The small syscall-level
|
|
369
|
+
* duplication against `client/file-completion/local-discovery.ts` is
|
|
370
|
+
* deliberate: the two adapters read DIFFERENT machines under Remote, and one
|
|
371
|
+
* concrete source owner for both localities is exactly what TS8-A removes.
|
|
372
|
+
* @module @xmoon76/dsh-pi-tui/runtime/direct/file-completion/host-discovery
|
|
373
|
+
*/
|
|
374
|
+
/**
|
|
375
|
+
* The Direct Host process's explicit path-query environment facts. Under
|
|
376
|
+
* Remote these are the HOST machine's facts, never the Client's; today Direct
|
|
377
|
+
* runs in-process, so the numbers coincide — that coincidence is deliberately
|
|
378
|
+
* not encoded in the domain contract.
|
|
379
|
+
*/
|
|
380
|
+
function hostPathQueryEnvironment() {
|
|
381
|
+
return {
|
|
382
|
+
homeDir: homedir(),
|
|
383
|
+
windowsHost: process.platform === "win32"
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Locate an executable file-finder on the Direct Host process's PATH: `fd`
|
|
388
|
+
* preferred, `fdfind` second. See the Client adapter for the probe contract.
|
|
389
|
+
*/
|
|
390
|
+
function resolveFdPath() {
|
|
391
|
+
const pathEntries = process.env.PATH?.split(delimiter) ?? [];
|
|
392
|
+
const suffixes = (process.env.PATHEXT || (process.platform === "win32" ? ".COM;.EXE;.BAT;.CMD" : "")).split(";").map((suffix) => suffix.trim()).filter((suffix) => suffix !== "").flatMap((suffix) => suffix.toLowerCase() === suffix ? [suffix] : [suffix, suffix.toLowerCase()]);
|
|
393
|
+
for (const name of ["fd", "fdfind"]) for (const entry of pathEntries) {
|
|
394
|
+
const dir = entry === "" ? "." : entry;
|
|
395
|
+
for (const suffix of ["", ...suffixes]) {
|
|
396
|
+
const candidate = join(dir, `${name}${suffix}`);
|
|
397
|
+
try {
|
|
398
|
+
if (statSync(candidate).isFile() && accessSync(candidate, constants.X_OK) === void 0) return candidate;
|
|
399
|
+
} catch {}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
return null;
|
|
403
|
+
}
|
|
404
|
+
/** Join a Host path WITHOUT collapsing a `..` segment. The Host's filesystem
|
|
405
|
+
* backend anchors a POSIX path containing a `..` segment with its physical
|
|
406
|
+
* spelling (the kernel resolves an intermediate symlink before the parent step),
|
|
407
|
+
* so a search base that deliberately keeps that traversal may not be lexically
|
|
408
|
+
* re-normalized on the way to a syscall. For every base the neutral resolver
|
|
409
|
+
* already normalized (no `..` left) this is identical to `path.join`. */
|
|
410
|
+
function hostJoin(baseDir, relative) {
|
|
411
|
+
if (relative === "") return baseDir;
|
|
412
|
+
return baseDir.endsWith("/") ? `${baseDir}${relative}` : `${baseDir}/${relative}`;
|
|
413
|
+
}
|
|
414
|
+
/** Whether one Dirent is a directory (symlinks followed; a broken link is a
|
|
415
|
+
* file candidate — the fork's rule). */
|
|
416
|
+
function entryIsDirectory(baseDir, name, entry) {
|
|
417
|
+
if (entry.isDirectory()) return true;
|
|
418
|
+
if (entry.isSymbolicLink()) try {
|
|
419
|
+
return statSync(hostJoin(baseDir, name)).isDirectory();
|
|
420
|
+
} catch {
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
return false;
|
|
424
|
+
}
|
|
425
|
+
/** Run the Direct Host finder for one scoped query. Returns NULL on finder
|
|
426
|
+
* FAILURE (the neutral policy then runs the bounded fallback), `[]` on a
|
|
427
|
+
* genuine no-match or an abort. */
|
|
428
|
+
function runHostFinder(fdPath, baseDir, term, signal) {
|
|
429
|
+
const args = [
|
|
430
|
+
"--base-directory",
|
|
431
|
+
baseDir,
|
|
432
|
+
"--max-results",
|
|
433
|
+
String(100),
|
|
434
|
+
"-i",
|
|
435
|
+
"--full-path",
|
|
436
|
+
"--print0",
|
|
437
|
+
"--type",
|
|
438
|
+
"f",
|
|
439
|
+
"--type",
|
|
440
|
+
"d",
|
|
441
|
+
"--type",
|
|
442
|
+
"l",
|
|
443
|
+
"--hidden",
|
|
444
|
+
"--exclude",
|
|
445
|
+
".git",
|
|
446
|
+
"--exclude",
|
|
447
|
+
".git/*",
|
|
448
|
+
"--exclude",
|
|
449
|
+
".git/**"
|
|
450
|
+
];
|
|
451
|
+
if (term !== "") args.push(escapeFinderRegex(term));
|
|
452
|
+
return new Promise((resolve) => {
|
|
453
|
+
if (signal.aborted) {
|
|
454
|
+
resolve([]);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
let child;
|
|
458
|
+
try {
|
|
459
|
+
child = spawn(fdPath, args, { stdio: [
|
|
460
|
+
"ignore",
|
|
461
|
+
"pipe",
|
|
462
|
+
"ignore"
|
|
463
|
+
] });
|
|
464
|
+
} catch {
|
|
465
|
+
resolve(null);
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
let stdout = "";
|
|
469
|
+
let settled = false;
|
|
470
|
+
const settle = (results) => {
|
|
471
|
+
if (settled) return;
|
|
472
|
+
settled = true;
|
|
473
|
+
signal.removeEventListener("abort", onAbort);
|
|
474
|
+
resolve(results);
|
|
475
|
+
};
|
|
476
|
+
const onAbort = () => {
|
|
477
|
+
if (child.exitCode === null) child.kill("SIGKILL");
|
|
478
|
+
settle([]);
|
|
479
|
+
};
|
|
480
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
481
|
+
child.stdout.setEncoding("utf-8");
|
|
482
|
+
child.stdout.on("data", (chunk) => {
|
|
483
|
+
stdout += chunk;
|
|
484
|
+
});
|
|
485
|
+
child.on("error", () => settle(null));
|
|
486
|
+
child.on("close", (code) => {
|
|
487
|
+
if (signal.aborted) {
|
|
488
|
+
settle([]);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (code !== 0) {
|
|
492
|
+
settle(null);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
const results = [];
|
|
496
|
+
for (const record of normalizeFinderRecords(stdout)) {
|
|
497
|
+
let isDirectory = record.directoryHint;
|
|
498
|
+
if (!isDirectory) try {
|
|
499
|
+
isDirectory = statSync(hostJoin(baseDir, record.path)).isDirectory();
|
|
500
|
+
} catch {
|
|
501
|
+
isDirectory = false;
|
|
502
|
+
}
|
|
503
|
+
results.push({
|
|
504
|
+
path: record.path,
|
|
505
|
+
kind: isDirectory ? "directory" : "file"
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
settle(results);
|
|
509
|
+
});
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* The Direct Host discovery driver: the Direct Host process's OWN filesystem,
|
|
514
|
+
* with a Host PATH finder when present.
|
|
515
|
+
*
|
|
516
|
+
* `fdPath` is a test/API pin: `undefined` probes the Host PATH, `null` FORCES
|
|
517
|
+
* the bounded fallback (no finder), a string pins one finder.
|
|
518
|
+
*/
|
|
519
|
+
var DirectHostDiscoveryDriver = class {
|
|
520
|
+
fdPathValue;
|
|
521
|
+
constructor(fdPath = void 0) {
|
|
522
|
+
this.fdPathValue = fdPath === void 0 ? resolveFdPath() : fdPath;
|
|
523
|
+
}
|
|
524
|
+
/** The pinned/detected Host finder (null = bounded-fallback only). */
|
|
525
|
+
get fdPath() {
|
|
526
|
+
return this.fdPathValue;
|
|
527
|
+
}
|
|
528
|
+
async listDirectory(baseDir, relativeDir, signal) {
|
|
529
|
+
if (signal.aborted) return null;
|
|
530
|
+
let entries;
|
|
531
|
+
try {
|
|
532
|
+
entries = readdirSync(hostJoin(baseDir, relativeDir), { withFileTypes: true });
|
|
533
|
+
} catch {
|
|
534
|
+
return null;
|
|
535
|
+
}
|
|
536
|
+
if (signal.aborted) return null;
|
|
537
|
+
const dir = hostJoin(baseDir, relativeDir);
|
|
538
|
+
return entries.map((entry) => ({
|
|
539
|
+
name: entry.name,
|
|
540
|
+
kind: entryIsDirectory(dir, entry.name, entry) ? "directory" : "file",
|
|
541
|
+
descendable: entry.isDirectory() && !entry.isSymbolicLink()
|
|
542
|
+
}));
|
|
543
|
+
}
|
|
544
|
+
async find(baseDir, term, signal) {
|
|
545
|
+
if (this.fdPathValue === null) return null;
|
|
546
|
+
return runHostFinder(this.fdPathValue, baseDir, term, signal);
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
//#endregion
|
|
550
|
+
//#region src/runtime/direct/host-file-augmentation-direct.ts
|
|
551
|
+
/**
|
|
552
|
+
* The session `@`-reference Host router (TS8-HF1): ONE Host-side authority
|
|
553
|
+
* that answers a completion query with the routing boundary
|
|
554
|
+
*
|
|
555
|
+
* ```text
|
|
556
|
+
* bare workspace fuzzy search -> official ctx.fileReferences.list
|
|
557
|
+
* @foo @readme @src @.env (workspace index, cache, exclusions,
|
|
558
|
+
* scoring, maxResults stay authoritative)
|
|
559
|
+
*
|
|
560
|
+
* explicit path navigation -> dsh-pi-tui Host scoped discovery
|
|
561
|
+
* @src/ @src/uti @./ @../shared/foo (the exact scope the user typed:
|
|
562
|
+
* @../../foo @/tmp/foo @~/Down recursive fuzzy inside it, explicit
|
|
563
|
+
* Windows drive @C:/work/foo and UNC parent/absolute/native paths)
|
|
564
|
+
* ```
|
|
565
|
+
*
|
|
566
|
+
* The decision is the TOKEN's explicit path navigation, never the resolved
|
|
567
|
+
* search base's workspace containment (the superseded v1 rule): a user-typed
|
|
568
|
+
* separator, a root form (`.`/`..`/`~`) or a Windows absolute path is what
|
|
569
|
+
* selects the scoped route, so `@dist/` and `@link/foo` address their
|
|
570
|
+
* explicitly named scope even when the official provider would exclude or not
|
|
571
|
+
* traverse it.
|
|
572
|
+
*
|
|
573
|
+
* POSIX BOUNDARY: on a POSIX Host a backslash is an ordinary FILENAME character,
|
|
574
|
+
* but the reused TS8-A path resolver selects its Windows dialect from the token
|
|
575
|
+
* alone and normalizes the separators (a pinned cross-dialect contract of the
|
|
576
|
+
* Client-local completion — `test/domain-file-completion.test.ts`). The
|
|
577
|
+
* augmentation therefore does NOT CLAIM an ambiguous POSIX token that contains a
|
|
578
|
+
* backslash: such a token is left to the official provider rather than searched
|
|
579
|
+
* in a scope we cannot represent verbatim. This is a deliberate fail-closed
|
|
580
|
+
* narrowing, not a claim that the token is a bare fuzzy query; delete the guard
|
|
581
|
+
* once the shared resolver distinguishes host dialect, token dialect and literal
|
|
582
|
+
* backslashes.
|
|
583
|
+
*
|
|
584
|
+
* PARENT-TRAVERSAL SPELLING: a relative scope is searched with the SAME spelling
|
|
585
|
+
* the Host's local filesystem backend gives the accepted value. On a POSIX Host a
|
|
586
|
+
* path containing a `..` segment is anchored with its PHYSICAL spelling by
|
|
587
|
+
* `dsh-fs-local` (kernel resolves an intermediate symlink before the parent step);
|
|
588
|
+
* the scoped search therefore keeps the raw `<cwd>/<displayBase>` concatenation for
|
|
589
|
+
* those queries instead of the lexically joined directory, so completion and the
|
|
590
|
+
* model's read can never land in two different physical directories. Windows, the
|
|
591
|
+
* absolute forms (already verbatim) and the home shorthand (absolute normalized
|
|
592
|
+
* value) keep the shared resolver's spelling.
|
|
593
|
+
*
|
|
594
|
+
* The official provider is called ONLY on the bare route; an authoritative
|
|
595
|
+
* official `[]` stays empty and never falls back to the scanner. The scoped
|
|
596
|
+
* route never calls the official provider and needs no capability from it.
|
|
597
|
+
*
|
|
598
|
+
* Home shorthand (`@~`, `@~/Down`) is dsh-pi-tui INPUT syntax: discovery runs
|
|
599
|
+
* against the Host HOME, but every returned candidate is materialized as an
|
|
600
|
+
* ABSOLUTE Host path — DSH path resolution performs no shell-style `~`
|
|
601
|
+
* expansion, so a literal `~/...` completion value would be unusable. Ranking
|
|
602
|
+
* always scores the SCOPE-RELATIVE candidate, never the materialized absolute
|
|
603
|
+
* path (the home prefix itself must not satisfy the path-substring tier).
|
|
604
|
+
*
|
|
605
|
+
* This authority receives HOST facts only (the live Agent, the Host discovery
|
|
606
|
+
* driver, the Host path environment); it never reads the Client cwd, home or
|
|
607
|
+
* platform.
|
|
608
|
+
*
|
|
609
|
+
* TODO(upstream-file-reference-explicit-scope): Delete the pi-tui
|
|
610
|
+
* explicit-scope augmentation when the minimum supported DSH fileReferences
|
|
611
|
+
* provider supports equivalent explicit scoped recursive search,
|
|
612
|
+
* parent/absolute Host path navigation, explicit excluded/symlink scope
|
|
613
|
+
* access, and a Host-home-safe completion representation.
|
|
614
|
+
*
|
|
615
|
+
* @module @xmoon76/dsh-pi-tui/runtime/direct/host-file-augmentation-direct
|
|
616
|
+
*/
|
|
617
|
+
/** Whether the token names an EXPLICIT path scope (the frozen classifier,
|
|
618
|
+
* decided from the raw token and the HOST platform — never from filesystem
|
|
619
|
+
* existence, official result count, or workspace containment).
|
|
620
|
+
*
|
|
621
|
+
* A POSIX host has ONE separator: any `/` is an explicit scope, and the root
|
|
622
|
+
* forms are explicit. A token that also contains a backslash is deliberately NOT
|
|
623
|
+
* claimed (see the module comment): the reused resolver cannot represent its
|
|
624
|
+
* literal POSIX scope, so the official provider keeps it.
|
|
625
|
+
*
|
|
626
|
+
* A Windows host treats `\` as a separator as well and accepts any
|
|
627
|
+
* win32-absolute form (drive/UNC) even before its first separator is visible. */
|
|
628
|
+
function isExplicitPathNavigation(raw, environment) {
|
|
629
|
+
if (raw === "") return false;
|
|
630
|
+
if (environment.windowsHost) return raw === "." || raw === ".." || raw === "~" || raw.includes("/") || raw.includes("\\") || win32.isAbsolute(raw);
|
|
631
|
+
if (raw.includes("\\")) return false;
|
|
632
|
+
return raw === "." || raw === ".." || raw === "~" || raw.includes("/");
|
|
633
|
+
}
|
|
634
|
+
/** Whether the token is a leading-home form (`~`, `~/x`, `~\x`). */
|
|
635
|
+
function isHomeToken(raw) {
|
|
636
|
+
return raw === "~" || raw.startsWith("~/") || raw.startsWith("~\\");
|
|
637
|
+
}
|
|
638
|
+
/** Materialize one candidate discovered under a HOME scope as an ABSOLUTE Host
|
|
639
|
+
* path (the `~` shorthand must never reach the prompt as literal text). */
|
|
640
|
+
function materializeHostPath(candidate, searchBase, windowsHost) {
|
|
641
|
+
return {
|
|
642
|
+
path: (windowsHost ? win32 : posix).join(searchBase, candidate.path),
|
|
643
|
+
kind: candidate.kind
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
/** One `..` path SEGMENT — the exact shape the Host filesystem backend keys its
|
|
647
|
+
* physical-spelling rule on (`dsh-fs-local`'s `localDisplayPath`). */
|
|
648
|
+
const PARENT_SEGMENT = /(?:^|[\\/])\.\.(?:[\\/]|$)/u;
|
|
649
|
+
/**
|
|
650
|
+
* The directory one explicit scope searches.
|
|
651
|
+
*
|
|
652
|
+
* The Host's local filesystem backend anchors a POSIX path that contains a `..`
|
|
653
|
+
* SEGMENT with its PHYSICAL spelling (the raw `<anchor>/<path>`, deliberately
|
|
654
|
+
* unnormalized), so the kernel resolves an intermediate symlink BEFORE the parent
|
|
655
|
+
* step; every other spelling goes through a lexical `resolve(anchor, path)`, and
|
|
656
|
+
* Windows always resolves lexically. Two consequences drive this function:
|
|
657
|
+
*
|
|
658
|
+
* 1. The anchor is `<hostCwd>` for a relative scope and the value itself for an
|
|
659
|
+
* absolute one, and the rule tests the WHOLE anchored spelling — so a `..`
|
|
660
|
+
* carried by the SESSION CWD flips it just like one in the typed scope. The
|
|
661
|
+
* consumer applies that rule to the value we hand it (`displayBase + name`), so
|
|
662
|
+
* the search must use the same spelling; otherwise the completion could offer a
|
|
663
|
+
* path the model's read resolves into a DIFFERENT physical directory.
|
|
664
|
+
* 2. A home shorthand emits an ABSOLUTE value through `path.join`, which normalizes
|
|
665
|
+
* lexically. Its scope must be normalized the same way: the bare `~`/`~/` root
|
|
666
|
+
* form is the one form whose search base is the raw `homedir()` string, so a
|
|
667
|
+
* `..`-bearing HOME would otherwise be searched physically while the value we
|
|
668
|
+
* emit names the lexical directory.
|
|
669
|
+
*
|
|
670
|
+
* Absolute forms already spell their traversal verbatim. This is one segment test
|
|
671
|
+
* plus one concatenation (plus one lexical normalize for the home root form) — the
|
|
672
|
+
* consumer's own rule applied to the value we are about to hand it, not a second
|
|
673
|
+
* path parser.
|
|
674
|
+
*/
|
|
675
|
+
function scopeSearchBase(resolved, raw, hostCwd, environment) {
|
|
676
|
+
const api = environment.windowsHost ? win32 : posix;
|
|
677
|
+
if (isHomeToken(raw)) return api.normalize(resolved.searchBase);
|
|
678
|
+
if (environment.windowsHost) return resolved.searchBase;
|
|
679
|
+
if (raw.startsWith("/") || resolved.winAbsolute) return resolved.searchBase;
|
|
680
|
+
const physicalScope = `${hostCwd}/${resolved.displayBase}`;
|
|
681
|
+
return PARENT_SEGMENT.test(physicalScope) ? physicalScope : resolved.searchBase;
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* Answer one session `@` completion query over the Host facts.
|
|
685
|
+
*
|
|
686
|
+
* Cancellation (the caller's `signal`) outranks every route: an aborted
|
|
687
|
+
* request rejects at entry — before the route/capability decision — and again
|
|
688
|
+
* after each await, so a cancelled scope never serves a late result and never
|
|
689
|
+
* reports `unavailable`.
|
|
690
|
+
*
|
|
691
|
+
* @param agent - the live Agent the session scope resolved to.
|
|
692
|
+
* @param query - the official query form (path text following `@`).
|
|
693
|
+
* @param signal - caller cancellation.
|
|
694
|
+
* @param deps - the Host facts this router answers with.
|
|
695
|
+
*/
|
|
696
|
+
async function listPiTuiHostFileReferences(agent, query, signal, deps) {
|
|
697
|
+
signal.throwIfAborted();
|
|
698
|
+
if (!isExplicitPathNavigation(query, deps.environment)) {
|
|
699
|
+
const official = deps.official;
|
|
700
|
+
if (official === void 0) return {
|
|
701
|
+
kind: "unavailable",
|
|
702
|
+
reason: "the official Host file-reference service is not mounted"
|
|
703
|
+
};
|
|
704
|
+
const candidates = await official.list(agent, query, signal);
|
|
705
|
+
signal.throwIfAborted();
|
|
706
|
+
return {
|
|
707
|
+
kind: "ok",
|
|
708
|
+
items: candidates.map((candidate) => ({
|
|
709
|
+
path: candidate.path,
|
|
710
|
+
kind: candidate.kind
|
|
711
|
+
}))
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
const workspaceCwd = effectiveAgentCwd(deps.workingDirectory, agent) ?? process.cwd();
|
|
715
|
+
const resolved = resolvePathQuery(query, workspaceCwd, deps.environment);
|
|
716
|
+
const searchBase = scopeSearchBase(resolved, query, workspaceCwd, deps.environment);
|
|
717
|
+
const discovered = await discoverForQuery(searchBase === resolved.searchBase ? resolved : {
|
|
718
|
+
...resolved,
|
|
719
|
+
searchBase
|
|
720
|
+
}, deps.driver, signal);
|
|
721
|
+
signal.throwIfAborted();
|
|
722
|
+
const homeToken = isHomeToken(query);
|
|
723
|
+
return {
|
|
724
|
+
kind: "ok",
|
|
725
|
+
items: rankPathCandidates(homeToken ? discovered : discovered.map((candidate) => reattachDisplayBase(candidate, resolved)), resolved.searchTerm).map((candidate) => homeToken ? materializeHostPath(candidate, resolved.searchBase, deps.environment.windowsHost) : {
|
|
726
|
+
path: candidate.path,
|
|
727
|
+
kind: candidate.kind
|
|
728
|
+
})
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
//#endregion
|
|
732
|
+
//#region src/domain/terminal-progress/interval.ts
|
|
733
|
+
/**
|
|
734
|
+
* Classify one official `turn/end.reason.kind` (plan §5.2). `completed` is the
|
|
735
|
+
* DSH turn result (not a claim about every business step); `error` and
|
|
736
|
+
* `max-tokens` (a resource ceiling, not success) are `error`; a cancelled /
|
|
737
|
+
* blocked / non-live closer is honestly `idle`. An UNKNOWN upstream kind is
|
|
738
|
+
* NOT guessed — the caller records it and the interval stays `idle`.
|
|
739
|
+
*
|
|
740
|
+
* @param kind - the durable `turn/end` reason kind.
|
|
741
|
+
* @returns the proven outcome, or `undefined` for an unknown kind.
|
|
742
|
+
*/
|
|
743
|
+
function classifyTerminalTurnEnd(kind) {
|
|
744
|
+
switch (kind) {
|
|
745
|
+
case "completed": return "done";
|
|
746
|
+
case "error": return "error";
|
|
747
|
+
case "max-tokens": return "error";
|
|
748
|
+
case "aborted":
|
|
749
|
+
case "blocked":
|
|
750
|
+
case "interrupted":
|
|
751
|
+
case "forked": return "idle";
|
|
752
|
+
default: return;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Create one independent fold instance.
|
|
757
|
+
* @param onUnknownReason - reports an unknown `turn/end.reason.kind` (the
|
|
758
|
+
* Host/Direct owner injects its diagnostics channel here); the interval stays
|
|
759
|
+
* `idle` instead of guessing.
|
|
760
|
+
*/
|
|
761
|
+
function createTerminalProgressInterval(onUnknownReason) {
|
|
762
|
+
let active = false;
|
|
763
|
+
/** The turn opened by the newest live `turn/start` of THIS interval. */
|
|
764
|
+
let openTurn;
|
|
765
|
+
/** The classified outcome of the newest turn whose `turn/end` matched the
|
|
766
|
+
* open turn — reset whenever a newer turn opens. */
|
|
767
|
+
let lastClosed;
|
|
768
|
+
/** The currently committed outcome of the interval (`idle` while running
|
|
769
|
+
* without a proven result). */
|
|
770
|
+
let outcome = "idle";
|
|
771
|
+
const state = () => ({
|
|
772
|
+
active,
|
|
773
|
+
outcome
|
|
774
|
+
});
|
|
775
|
+
return {
|
|
776
|
+
status(running) {
|
|
777
|
+
if (running) {
|
|
778
|
+
if (active) return void 0;
|
|
779
|
+
active = true;
|
|
780
|
+
openTurn = void 0;
|
|
781
|
+
lastClosed = void 0;
|
|
782
|
+
outcome = "idle";
|
|
783
|
+
return state();
|
|
784
|
+
}
|
|
785
|
+
if (!active) return void 0;
|
|
786
|
+
outcome = openTurn === void 0 ? lastClosed ?? "idle" : "idle";
|
|
787
|
+
active = false;
|
|
788
|
+
openTurn = void 0;
|
|
789
|
+
lastClosed = void 0;
|
|
790
|
+
return state();
|
|
791
|
+
},
|
|
792
|
+
turnStart(turn) {
|
|
793
|
+
if (!active) return;
|
|
794
|
+
openTurn = turn;
|
|
795
|
+
lastClosed = void 0;
|
|
796
|
+
},
|
|
797
|
+
turnEnd(turn, reasonKind) {
|
|
798
|
+
if (!active) return;
|
|
799
|
+
if (openTurn !== turn) return;
|
|
800
|
+
openTurn = void 0;
|
|
801
|
+
const classified = classifyTerminalTurnEnd(reasonKind);
|
|
802
|
+
if (classified === void 0) {
|
|
803
|
+
onUnknownReason?.(reasonKind);
|
|
804
|
+
lastClosed = "idle";
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
lastClosed = classified;
|
|
808
|
+
},
|
|
809
|
+
retire() {
|
|
810
|
+
openTurn = void 0;
|
|
811
|
+
lastClosed = void 0;
|
|
812
|
+
if (!active && outcome === "idle") return void 0;
|
|
813
|
+
active = false;
|
|
814
|
+
return state();
|
|
815
|
+
},
|
|
816
|
+
apply(fact) {
|
|
817
|
+
active = fact.active;
|
|
818
|
+
outcome = fact.outcome;
|
|
819
|
+
openTurn = void 0;
|
|
820
|
+
lastClosed = void 0;
|
|
821
|
+
return state();
|
|
822
|
+
},
|
|
823
|
+
reset() {
|
|
824
|
+
active = false;
|
|
825
|
+
openTurn = void 0;
|
|
826
|
+
lastClosed = void 0;
|
|
827
|
+
outcome = "idle";
|
|
828
|
+
return state();
|
|
829
|
+
},
|
|
830
|
+
snapshot() {
|
|
831
|
+
return state();
|
|
832
|
+
}
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
//#endregion
|
|
836
|
+
//#region src/runtime/remote/pi-tui-terminal-progress-contract.ts
|
|
837
|
+
/** The wire namespace (and Host Cordis service key) of the private stream. */
|
|
838
|
+
const PI_TUI_TERMINAL_PROGRESS_NAMESPACE = "piTuiTerminalProgress";
|
|
839
|
+
/** The private method's stable endpoint identity. */
|
|
840
|
+
const PI_TUI_TERMINAL_PROGRESS_WATCH_ENDPOINT = "@xmoon76/dsh-pi-tui#piTuiTerminalProgress/watch";
|
|
841
|
+
/** One strict JSON codec that validates a wire string. */
|
|
842
|
+
function stringCodec(typeSymbol) {
|
|
843
|
+
return {
|
|
844
|
+
mode: "strict",
|
|
845
|
+
typeSymbol,
|
|
846
|
+
create: () => ({ parse: (value) => {
|
|
847
|
+
if (typeof value !== "string") throw new TypeError(`${typeSymbol}: the wire value must be a string`);
|
|
848
|
+
return value;
|
|
849
|
+
} })
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* Validate and detach one `PiTuiTerminalProgressFrame`. This is the ONE
|
|
854
|
+
* structural validator of the frame shape: the descriptor's `result` codec
|
|
855
|
+
* uses it, and the R2 Client source must call it on every downlink item because
|
|
856
|
+
* the framework does not (see the module JSDoc).
|
|
857
|
+
*/
|
|
858
|
+
function parsePiTuiTerminalProgressFrame(value) {
|
|
859
|
+
if (typeof value !== "object" || value === null) throw new TypeError("piTuiTerminalProgress/watch: the frame must be an object");
|
|
860
|
+
const frame = value;
|
|
861
|
+
if (frame.kind !== "snapshot" && frame.kind !== "update") throw new TypeError("piTuiTerminalProgress/watch: the frame kind must be snapshot or update");
|
|
862
|
+
if (typeof frame.sessionId !== "string") throw new TypeError("piTuiTerminalProgress/watch: the frame needs a string sessionId");
|
|
863
|
+
if (typeof frame.hostEpoch !== "string") throw new TypeError("piTuiTerminalProgress/watch: the frame needs a string hostEpoch");
|
|
864
|
+
if (typeof frame.agentEpoch !== "number") throw new TypeError("piTuiTerminalProgress/watch: the frame needs a numeric agentEpoch");
|
|
865
|
+
if (typeof frame.revision !== "number") throw new TypeError("piTuiTerminalProgress/watch: the frame needs a numeric revision");
|
|
866
|
+
if (typeof frame.running !== "boolean") throw new TypeError("piTuiTerminalProgress/watch: the frame needs a boolean running");
|
|
867
|
+
if (frame.outcome !== "idle" && frame.outcome !== "done" && frame.outcome !== "error") throw new TypeError("piTuiTerminalProgress/watch: the frame outcome must be idle, done or error");
|
|
868
|
+
return {
|
|
869
|
+
kind: frame.kind,
|
|
870
|
+
sessionId: frame.sessionId,
|
|
871
|
+
hostEpoch: frame.hostEpoch,
|
|
872
|
+
agentEpoch: frame.agentEpoch,
|
|
873
|
+
revision: frame.revision,
|
|
874
|
+
running: frame.running,
|
|
875
|
+
outcome: frame.outcome
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
const PI_TUI_TERMINAL_PROGRESS_FRAME_CODEC = {
|
|
879
|
+
mode: "strict",
|
|
880
|
+
typeSymbol: "@xmoon76/dsh-pi-tui#piTuiTerminalProgress/watch:frame",
|
|
881
|
+
create: () => ({ parse: parsePiTuiTerminalProgressFrame }),
|
|
882
|
+
decode: parsePiTuiTerminalProgressFrame
|
|
883
|
+
};
|
|
884
|
+
/**
|
|
885
|
+
* The ONE shared invocation descriptor both contributions derive from. The
|
|
886
|
+
* Host registers this exact object; the Client mounts this exact object.
|
|
887
|
+
*/
|
|
888
|
+
const PI_TUI_TERMINAL_PROGRESS_WATCH = {
|
|
889
|
+
id: PI_TUI_TERMINAL_PROGRESS_WATCH_ENDPOINT,
|
|
890
|
+
service: PI_TUI_TERMINAL_PROGRESS_NAMESPACE,
|
|
891
|
+
namespace: PI_TUI_TERMINAL_PROGRESS_NAMESPACE,
|
|
892
|
+
method: "watch",
|
|
893
|
+
mode: "stream",
|
|
894
|
+
invocation: { kind: "direct" },
|
|
895
|
+
parameters: [{
|
|
896
|
+
name: "sessionId",
|
|
897
|
+
wire: "sessionId",
|
|
898
|
+
source: "json",
|
|
899
|
+
codec: stringCodec("@xmoon76/dsh-pi-tui#piTuiTerminalProgress/watch:sessionId")
|
|
900
|
+
}],
|
|
901
|
+
cancellation: { parameter: "signal" },
|
|
902
|
+
result: PI_TUI_TERMINAL_PROGRESS_FRAME_CODEC
|
|
903
|
+
};
|
|
904
|
+
//#endregion
|
|
905
|
+
export { projectedWorkingDirectory as _, listPiTuiHostFileReferences as a, resolveFdPath as c, resolvePathQuery as d, separatorOfRaw as f, effectiveAgentCwd as g, normalizeFinderRecords as h, createTerminalProgressInterval as i, rankPathCandidates as l, escapeFinderRegex as m, PI_TUI_TERMINAL_PROGRESS_WATCH as n, DirectHostDiscoveryDriver as o, discoverForQuery as p, parsePiTuiTerminalProgressFrame as r, hostPathQueryEnvironment as s, PI_TUI_TERMINAL_PROGRESS_NAMESPACE as t, reattachDisplayBase as u };
|