@osdlc/wayfinder 0.7.4 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +240 -0
- package/CONTRIBUTING.md +6 -0
- package/README.md +310 -150
- package/SECURITY.md +19 -5
- package/dist/actions.d.ts +23 -0
- package/dist/actions.js +97 -0
- package/dist/app.d.ts +1 -13
- package/dist/app.js +1 -13
- package/dist/args.d.ts +11 -6
- package/dist/args.js +25 -7
- package/dist/cache.d.ts +2 -4
- package/dist/cache.js +5 -10
- package/dist/catalog.d.ts +45 -4
- package/dist/catalog.js +134 -13
- package/dist/clean-command.d.ts +65 -36
- package/dist/clean-command.js +372 -226
- package/dist/cli.d.ts +24 -57
- package/dist/cli.js +139 -568
- package/dist/commands/browse.d.ts +128 -0
- package/dist/commands/browse.js +153 -0
- package/dist/commands/index.d.ts +25 -0
- package/dist/commands/index.js +71 -0
- package/dist/commands/list.d.ts +224 -0
- package/dist/commands/list.js +178 -0
- package/dist/commands/query.d.ts +166 -0
- package/dist/commands/query.js +73 -0
- package/dist/commands/run.d.ts +114 -0
- package/dist/commands/run.js +139 -0
- package/dist/commands/shared.d.ts +152 -0
- package/dist/commands/shared.js +209 -0
- package/dist/config-command.d.ts +27 -43
- package/dist/config-command.js +129 -330
- package/dist/config-keys.d.ts +25 -0
- package/dist/config-keys.js +29 -0
- package/dist/config.d.ts +272 -257
- package/dist/config.js +611 -460
- package/dist/core/copy-text.d.ts +30 -0
- package/dist/core/copy-text.js +50 -0
- package/dist/core/filter.d.ts +55 -0
- package/dist/core/filter.js +59 -0
- package/dist/core/run.d.ts +22 -0
- package/dist/core/run.js +59 -0
- package/dist/core/saved-view.d.ts +59 -0
- package/dist/core/saved-view.js +119 -0
- package/dist/core/session.d.ts +282 -0
- package/dist/core/session.js +602 -0
- package/dist/core/skills.d.ts +53 -0
- package/dist/core/skills.js +54 -0
- package/dist/core/unknowns.d.ts +72 -0
- package/dist/core/unknowns.js +68 -0
- package/dist/devlink.d.ts +3 -0
- package/dist/devlink.js +5 -2
- package/dist/document-schemas.generated.d.ts +22 -0
- package/dist/document-schemas.generated.js +916 -0
- package/dist/documents.d.ts +508 -0
- package/dist/documents.js +181 -0
- package/dist/groups.d.ts +22 -26
- package/dist/groups.js +13 -29
- package/dist/guide.d.ts +26 -13
- package/dist/guide.js +119 -103
- package/dist/index.d.ts +118 -0
- package/dist/index.js +61 -0
- package/dist/keys.d.ts +51 -0
- package/dist/keys.js +273 -0
- package/dist/page.d.ts +18 -8
- package/dist/page.js +28 -17
- package/dist/providers-command.d.ts +81 -20
- package/dist/providers-command.js +33 -75
- package/dist/schema.d.ts +48 -0
- package/dist/schema.js +61 -0
- package/dist/serve/auth.d.ts +46 -0
- package/dist/serve/auth.js +82 -0
- package/dist/serve/catalog-share.d.ts +63 -0
- package/dist/serve/catalog-share.js +155 -0
- package/dist/serve/index.d.ts +100 -0
- package/dist/serve/index.js +259 -0
- package/dist/serve/routes.d.ts +60 -0
- package/dist/serve/routes.js +186 -0
- package/dist/serve/terminal-input.d.ts +50 -0
- package/dist/serve/terminal-input.js +108 -0
- package/dist/skills.d.ts +19 -0
- package/dist/skills.js +54 -0
- package/dist/terminal-text.d.ts +8 -0
- package/dist/terminal-text.js +19 -6
- package/dist/tool-info.d.ts +24 -0
- package/dist/tool-info.js +17 -0
- package/dist/trust-command.d.ts +15 -27
- package/dist/trust-command.js +110 -208
- package/dist/trust-store.d.ts +5 -10
- package/dist/trust-store.js +9 -42
- package/dist/tui/app.d.ts +193 -0
- package/dist/tui/app.js +1400 -0
- package/dist/tui/copy.d.ts +29 -0
- package/dist/tui/copy.js +39 -0
- package/dist/tui/index.d.ts +33 -0
- package/dist/tui/index.js +97 -0
- package/dist/tui/keys-input.d.ts +27 -0
- package/dist/tui/keys-input.js +87 -0
- package/dist/tui/settings.d.ts +94 -0
- package/dist/tui/settings.js +198 -0
- package/dist/tui/style.d.ts +62 -0
- package/dist/tui/style.js +77 -0
- package/dist/version.d.ts +6 -0
- package/dist/version.js +7 -0
- package/dist/vocabulary.d.ts +91 -0
- package/dist/vocabulary.js +155 -0
- package/dist/web/main.js +2232 -582
- package/dist/web/page.html +41 -33
- package/dist/web/styles.css +513 -329
- package/dist/xdg.d.ts +76 -4
- package/dist/xdg.js +77 -5
- package/package.json +27 -8
- package/schema/config.json +133 -0
- package/dist/http.d.ts +0 -22
- package/dist/http.js +0 -149
- package/dist/origin-owner.d.ts +0 -19
- package/dist/origin-owner.js +0 -85
- package/dist/serve.d.ts +0 -98
- package/dist/serve.js +0 -671
- package/dist/tui.d.ts +0 -177
- package/dist/tui.js +0 -840
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The session core: what one run of wayfinder knows about one repository,
|
|
3
|
+
* and the operations every surface shares. See `README.md` beside this file
|
|
4
|
+
* for how the CLI, the terminal UI and the web view use it.
|
|
5
|
+
*/
|
|
6
|
+
import { isAbsolute, relative, resolve, sep } from 'node:path';
|
|
7
|
+
import { commandString } from '@osdlc/command-seam/quote';
|
|
8
|
+
import { closestMatches } from '@osdlc/config-layers/did-you-mean';
|
|
9
|
+
import { homeDir, parseTrustScope, policyExecGate, withheldHint, } from '@osdlc/trust-repo';
|
|
10
|
+
import { BIN, PROJECT_CONFIG_FILENAME } from "../app.js";
|
|
11
|
+
import { cacheDir } from "../cache.js";
|
|
12
|
+
import { settingLayer } from "../config-keys.js";
|
|
13
|
+
import { collectAndCache, fromStore, makeProject, orderedCatalog, projectRootFor, readCachedCatalog, runnerIds, settledBoth, } from "../catalog.js";
|
|
14
|
+
import { ConfigError, collectOptionsFromConfig, readSettings, trustPolicyFromConfig, writeSetting, trustPolicyOptions, } from "../config.js";
|
|
15
|
+
import { readSkills } from "../skills.js";
|
|
16
|
+
import { toolSummary } from "../tool-info.js";
|
|
17
|
+
import { VERSION } from "../version.js";
|
|
18
|
+
import { loadTools, ToolModuleError } from "../tools.js";
|
|
19
|
+
import { appTrustStore } from "../trust-store.js";
|
|
20
|
+
import { EDITOR_VARIABLE_LABEL } from "../vocabulary.js";
|
|
21
|
+
import { taskCopyText } from "./copy-text.js";
|
|
22
|
+
import { filterTasks } from "./filter.js";
|
|
23
|
+
import { readView, viewKey, writeView } from "./saved-view.js";
|
|
24
|
+
import { filterSkills } from "./skills.js";
|
|
25
|
+
import { execOffSource, unknownsOf } from "./unknowns.js";
|
|
26
|
+
/**
|
|
27
|
+
* Why a session could not start, or an operation was refused. `code` is
|
|
28
|
+
* stable, for an error document.
|
|
29
|
+
*/
|
|
30
|
+
export class SessionError extends Error {
|
|
31
|
+
code;
|
|
32
|
+
/** A sentence on what to do, when there is one. */
|
|
33
|
+
hint;
|
|
34
|
+
constructor(code, message, hint) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = 'SessionError';
|
|
37
|
+
this.code = code;
|
|
38
|
+
this.hint = hint;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Load the tool modules this run names and build the project over them.
|
|
43
|
+
* Throws {@link SessionError} `tool-module` when a module cannot be used.
|
|
44
|
+
*/
|
|
45
|
+
async function projectFor(toolModules, cfg, cwd, home, projectAllowed) {
|
|
46
|
+
let loaded;
|
|
47
|
+
try {
|
|
48
|
+
loaded = await loadTools({ flag: toolModules, cwd, config: cfg, home, projectAllowed });
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
if (err instanceof ToolModuleError)
|
|
52
|
+
throw new SessionError('tool-module', err.message);
|
|
53
|
+
throw err;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
return { project: makeProject(loaded.tools.map((t) => t.def)), ...loaded };
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
// Every tool here passed loadTools, so a registry refusal (such as an
|
|
60
|
+
// ecosystem id a built-in already uses) comes from the modules.
|
|
61
|
+
const modules = [...new Set(loaded.tools.map((t) => t.module))].join(', ');
|
|
62
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
63
|
+
throw new SessionError('tool-module', `${message} (from a tool module: ${modules})`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Every scope a tool grant can name for `project`: each runner's id, and
|
|
68
|
+
* each tool manager that wraps commands (`mise exec`), since a runner it
|
|
69
|
+
* wraps needs it trusted too.
|
|
70
|
+
*/
|
|
71
|
+
function toolTrustScopes(project) {
|
|
72
|
+
const wrappers = project.tools.filter((t) => t.roles.toolManager?.exec !== undefined);
|
|
73
|
+
return [...new Set([...runnerIds(project), ...wrappers.map((t) => t.id)])];
|
|
74
|
+
}
|
|
75
|
+
/** `opts` with an `ownerOf` that runs git at most once per root. */
|
|
76
|
+
function onceOwner(opts) {
|
|
77
|
+
const ownerOf = opts.ownerOf;
|
|
78
|
+
if (ownerOf === undefined)
|
|
79
|
+
return opts;
|
|
80
|
+
const seen = new Map();
|
|
81
|
+
return {
|
|
82
|
+
...opts,
|
|
83
|
+
ownerOf: (root) => {
|
|
84
|
+
let owner = seen.get(root);
|
|
85
|
+
if (owner === undefined) {
|
|
86
|
+
owner = ownerOf(root);
|
|
87
|
+
seen.set(root, owner);
|
|
88
|
+
}
|
|
89
|
+
return owner;
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/** A scope a person typed, checked as `way trust --scope` checks it. */
|
|
94
|
+
function parseScope(scope) {
|
|
95
|
+
try {
|
|
96
|
+
return parseTrustScope(scope);
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
throw new SessionError('bad-arguments', err instanceof Error ? err.message : String(err));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** The collect option a flag sets, for each setting a settings screen saves. */
|
|
103
|
+
const FLAG_OF_SETTING = {
|
|
104
|
+
'runners.disabled': 'exclude',
|
|
105
|
+
discovery: 'discovery',
|
|
106
|
+
excludePaths: 'excludePaths',
|
|
107
|
+
};
|
|
108
|
+
/** A config error as a session error; anything else unchanged. */
|
|
109
|
+
function asSessionError(err, prefix = '') {
|
|
110
|
+
if (err instanceof ConfigError)
|
|
111
|
+
return new SessionError('config', prefix + err.message);
|
|
112
|
+
return err;
|
|
113
|
+
}
|
|
114
|
+
/** The fresh state of a runner probed just now, with its measured time when it ran. */
|
|
115
|
+
function probedState(runner, probeMs) {
|
|
116
|
+
return {
|
|
117
|
+
runner,
|
|
118
|
+
state: 'fresh',
|
|
119
|
+
ageMs: 0,
|
|
120
|
+
estimateMs: probeMs ?? null,
|
|
121
|
+
slow: false,
|
|
122
|
+
...(probeMs !== undefined ? { probeMs } : {}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** Fresh states for a catalog probed just now, with each runner's measured time. */
|
|
126
|
+
function probedStates(catalog, times) {
|
|
127
|
+
return catalog.reports.map((r) => probedState(r.runner, times.get(r.runner)));
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Start a session: find the root, load config, decide execution against the
|
|
131
|
+
* trust store, and load tool modules. Throws {@link SessionError} for a start
|
|
132
|
+
* directory that does not exist, a config file that cannot be used, or a tool
|
|
133
|
+
* module that cannot be loaded.
|
|
134
|
+
*/
|
|
135
|
+
export async function createSession(opts) {
|
|
136
|
+
const env = opts.env;
|
|
137
|
+
const home = opts.home ?? homeDir();
|
|
138
|
+
const flags = opts.flags ?? {};
|
|
139
|
+
const start = isAbsolute(opts.dir) ? opts.dir : resolve(opts.cwd, opts.dir);
|
|
140
|
+
const root = await projectRootFor(start);
|
|
141
|
+
if (root === null) {
|
|
142
|
+
throw new SessionError('no-such-directory', `${opts.dir}: no such directory`);
|
|
143
|
+
}
|
|
144
|
+
let settings;
|
|
145
|
+
try {
|
|
146
|
+
settings = await readSettings(root, env, home);
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
throw asSessionError(err);
|
|
150
|
+
}
|
|
151
|
+
const config = settings.loaded;
|
|
152
|
+
// Precedence for allowExec, highest first: --exec / --no-exec, then
|
|
153
|
+
// allowExec in the project file (false only), then the user file, then
|
|
154
|
+
// the built-in default (true). Trust is decided here; collection never
|
|
155
|
+
// reads the trust store.
|
|
156
|
+
const configured = settings.allowExec.value;
|
|
157
|
+
const trustStore = appTrustStore({ env, home });
|
|
158
|
+
const policy = trustPolicyFromConfig(config.effective);
|
|
159
|
+
const policyOptions = onceOwner(trustPolicyOptions(home));
|
|
160
|
+
let gate = await policyExecGate(trustStore, policy, { root, configured, flag: flags.allowExec }, policyOptions);
|
|
161
|
+
// A project module runs code on import, so it needs execution allowed as
|
|
162
|
+
// well as trust.
|
|
163
|
+
const projectAllowed = gate.trusted && gate.allowExec;
|
|
164
|
+
const built = await projectFor(flags.toolModules ?? [], config, opts.cwd, home, projectAllowed);
|
|
165
|
+
const projectToolsPending = !projectAllowed && (config.layers.project?.tools?.length ?? 0) > 0;
|
|
166
|
+
// The config's own allowExec was consumed by the gate.
|
|
167
|
+
const { allowExec: _consumed, ...configOptions } = collectOptionsFromConfig(config.effective);
|
|
168
|
+
// Config first, flags second. --exclude REPLACES runners.disabled.
|
|
169
|
+
const options = {
|
|
170
|
+
...configOptions,
|
|
171
|
+
allowExec: gate.allowExec,
|
|
172
|
+
// Only when trust is what is missing: when a file turned execution off,
|
|
173
|
+
// trusting the repository would change nothing.
|
|
174
|
+
...(gate.reason === 'untrusted' ? { execWithheldHint: withheldHint(BIN) } : {}),
|
|
175
|
+
...(flags.only ? { only: flags.only } : {}),
|
|
176
|
+
...(flags.exclude ? { exclude: flags.exclude } : {}),
|
|
177
|
+
...(flags.excludePaths ? { excludePaths: flags.excludePaths } : {}),
|
|
178
|
+
...(flags.discovery !== undefined ? { discovery: flags.discovery } : {}),
|
|
179
|
+
...(opts.signal !== undefined ? { signal: opts.signal } : {}),
|
|
180
|
+
};
|
|
181
|
+
const pinnedByFlag = [
|
|
182
|
+
...(flags.exclude ? ['exclude'] : []),
|
|
183
|
+
...(flags.excludePaths ? ['excludePaths'] : []),
|
|
184
|
+
...(flags.discovery !== undefined ? ['discovery'] : []),
|
|
185
|
+
];
|
|
186
|
+
// Per-tool trust applies only when the repository's own trust is what
|
|
187
|
+
// withholds execution. The set is read on every probe, so a tool grant in
|
|
188
|
+
// this session applies to the next one.
|
|
189
|
+
const toolTrusted = new Set();
|
|
190
|
+
let scopesByRunner = new Map();
|
|
191
|
+
const toolScopes = gate.reason === 'untrusted' ? toolTrustScopes(built.project) : [];
|
|
192
|
+
const decideTools = async () => {
|
|
193
|
+
toolTrusted.clear();
|
|
194
|
+
if (toolScopes.length === 0)
|
|
195
|
+
return;
|
|
196
|
+
const decided = await policyExecGate(trustStore, policy, { root, configured: true, scopes: toolScopes }, policyOptions);
|
|
197
|
+
for (const [scope, d] of Object.entries(decided.decisions))
|
|
198
|
+
if (d.trusted)
|
|
199
|
+
toolTrusted.add(scope);
|
|
200
|
+
};
|
|
201
|
+
await decideTools();
|
|
202
|
+
if (toolScopes.length > 0) {
|
|
203
|
+
options.toolTrust = {
|
|
204
|
+
trusted: toolTrusted,
|
|
205
|
+
onScopes: (scopes) => {
|
|
206
|
+
scopesByRunner = scopes;
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
const skills = () => readSkills(root, { setting: settings.skills.value, home, env, signal: opts.signal });
|
|
211
|
+
const store = cacheDir(env, home);
|
|
212
|
+
const runnerOrder = () => settings.loaded.effective.runners?.order;
|
|
213
|
+
/** Take `next` as the settings: the collect options follow, except where a flag decided. */
|
|
214
|
+
const adopt = (next) => {
|
|
215
|
+
settings = next;
|
|
216
|
+
const derived = collectOptionsFromConfig(next.loaded.effective);
|
|
217
|
+
const target = options;
|
|
218
|
+
for (const key of ['exclude', 'discovery', 'excludePaths']) {
|
|
219
|
+
if (pinnedByFlag.includes(key))
|
|
220
|
+
continue;
|
|
221
|
+
if (derived[key] === undefined)
|
|
222
|
+
delete target[key];
|
|
223
|
+
else
|
|
224
|
+
target[key] = derived[key];
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
const reread = async (prefix = '') => {
|
|
228
|
+
try {
|
|
229
|
+
adopt(await readSettings(root, env, home));
|
|
230
|
+
}
|
|
231
|
+
catch (err) {
|
|
232
|
+
throw asSessionError(err, prefix);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
const trustable = () => options.execWithheldHint !== undefined;
|
|
236
|
+
/** The options, stopped by this call's `signal` as well as the session's. */
|
|
237
|
+
const withSignal = (signal) => {
|
|
238
|
+
const signals = [options.signal, signal].filter((s) => s !== undefined);
|
|
239
|
+
return signals.length > 0 ? { ...options, signal: AbortSignal.any(signals) } : options;
|
|
240
|
+
};
|
|
241
|
+
/** Probe the runners `only` names (all when absent), timing each and telling `progress`. */
|
|
242
|
+
const probe = async (only, signal, progress) => {
|
|
243
|
+
const times = new Map();
|
|
244
|
+
const catalog = await collectAndCache(project, root, {
|
|
245
|
+
...withSignal(signal),
|
|
246
|
+
...(only !== undefined ? { only } : {}),
|
|
247
|
+
onProbe: ({ runner, durationMs }) => times.set(runner, durationMs),
|
|
248
|
+
...(progress?.onRunners !== undefined
|
|
249
|
+
? { onRunners: (runners) => progress.onRunners?.(runners) }
|
|
250
|
+
: {}),
|
|
251
|
+
...(progress?.onRunner !== undefined
|
|
252
|
+
? {
|
|
253
|
+
onReport: (report) => progress.onRunner?.(report, probedState(report.runner, times.get(report.runner))),
|
|
254
|
+
}
|
|
255
|
+
: {}),
|
|
256
|
+
}, store);
|
|
257
|
+
return { catalog, times };
|
|
258
|
+
};
|
|
259
|
+
const { project, tools } = built;
|
|
260
|
+
/** What decides which rows a probe returns; a saved view is used only under the same key. */
|
|
261
|
+
const currentViewKey = () => viewKey({
|
|
262
|
+
version: VERSION,
|
|
263
|
+
root,
|
|
264
|
+
allowExec: options.allowExec ?? false,
|
|
265
|
+
withheldHint: options.execWithheldHint !== undefined,
|
|
266
|
+
toolTrusted: [...toolTrusted].sort(),
|
|
267
|
+
only: options.only ?? null,
|
|
268
|
+
exclude: options.exclude ?? null,
|
|
269
|
+
discovery: options.discovery ?? null,
|
|
270
|
+
excludePaths: options.excludePaths ?? null,
|
|
271
|
+
skills: settings.skills.value,
|
|
272
|
+
tools: project.tools.map((t) => t.id),
|
|
273
|
+
});
|
|
274
|
+
/** Save `snapshot` as the view a first paint draws, without waiting, and return it. */
|
|
275
|
+
const saved = (snapshot) => {
|
|
276
|
+
void writeView(store, {
|
|
277
|
+
key: currentViewKey(),
|
|
278
|
+
savedAt: Date.now(),
|
|
279
|
+
catalog: snapshot.catalog,
|
|
280
|
+
cache: snapshot.cache.map(({ probeMs: _probeMs, ...rest }) => rest),
|
|
281
|
+
skills: snapshot.skills,
|
|
282
|
+
scopes: Object.fromEntries([...scopesByRunner].map(([r, s]) => [r, [...s]])),
|
|
283
|
+
});
|
|
284
|
+
return snapshot;
|
|
285
|
+
};
|
|
286
|
+
const session = {
|
|
287
|
+
root,
|
|
288
|
+
get config() {
|
|
289
|
+
return settings.loaded;
|
|
290
|
+
},
|
|
291
|
+
warnings: [...config.warnings, ...built.warnings],
|
|
292
|
+
get gate() {
|
|
293
|
+
return gate;
|
|
294
|
+
},
|
|
295
|
+
project,
|
|
296
|
+
tools,
|
|
297
|
+
options,
|
|
298
|
+
projectToolsPending,
|
|
299
|
+
pinnedByFlag,
|
|
300
|
+
store,
|
|
301
|
+
env,
|
|
302
|
+
home,
|
|
303
|
+
runnerOrder,
|
|
304
|
+
withheldReason() {
|
|
305
|
+
if (gate.allowExec)
|
|
306
|
+
return null;
|
|
307
|
+
return gate.reason === 'untrusted' ? 'untrusted' : 'exec-off';
|
|
308
|
+
},
|
|
309
|
+
async catalog(opts = {}) {
|
|
310
|
+
if (opts.cached !== true) {
|
|
311
|
+
// The skills are read beside the probe, so the answer does not wait
|
|
312
|
+
// for both in turn. Both settle before a failure is thrown.
|
|
313
|
+
const [{ catalog, times }, skillRows] = await settledBoth(probe(undefined, opts.signal, opts.progress), skills());
|
|
314
|
+
return saved({
|
|
315
|
+
catalog: orderedCatalog(catalog, runnerOrder()),
|
|
316
|
+
cache: probedStates(catalog, times),
|
|
317
|
+
skills: skillRows,
|
|
318
|
+
origin: 'probe',
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
const view = await session.savedCatalog();
|
|
322
|
+
if (view !== null)
|
|
323
|
+
return view;
|
|
324
|
+
// The call's signal too: a closed tab stops hashing the store.
|
|
325
|
+
const stored = await readCachedCatalog(project, root, withSignal(opts.signal), store);
|
|
326
|
+
if (stored.catalog === null || !fromStore(stored)) {
|
|
327
|
+
const { catalog, times } = await probe(undefined, opts.signal, opts.progress);
|
|
328
|
+
return saved({
|
|
329
|
+
catalog: orderedCatalog(catalog, runnerOrder()),
|
|
330
|
+
cache: probedStates(catalog, times),
|
|
331
|
+
skills: await skills(),
|
|
332
|
+
origin: 'empty-store',
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
// A miss is a runner with nothing stored. Only the missing runners are probed.
|
|
336
|
+
const missing = stored.runners.filter((r) => r.state === 'miss').map((r) => r.runner);
|
|
337
|
+
if (missing.length === 0) {
|
|
338
|
+
return saved({
|
|
339
|
+
catalog: orderedCatalog(stored.catalog, runnerOrder()),
|
|
340
|
+
cache: stored.runners,
|
|
341
|
+
skills: await skills(),
|
|
342
|
+
origin: 'store',
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
// No progress here: it would name only the missing runners, while the
|
|
346
|
+
// stored ones wait for the document.
|
|
347
|
+
const { catalog: probed, times } = await probe(missing, opts.signal);
|
|
348
|
+
const fresh = new Map(probedStates(probed, times).map((s) => [s.runner, s]));
|
|
349
|
+
return saved({
|
|
350
|
+
catalog: orderedCatalog({ ...stored.catalog, reports: [...stored.catalog.reports, ...probed.reports] }, runnerOrder()),
|
|
351
|
+
cache: stored.runners.map((r) => fresh.get(r.runner) ?? r),
|
|
352
|
+
skills: await skills(),
|
|
353
|
+
origin: 'store',
|
|
354
|
+
});
|
|
355
|
+
},
|
|
356
|
+
async storedCatalog() {
|
|
357
|
+
const stored = await readCachedCatalog(project, root, options, store);
|
|
358
|
+
if (stored.catalog === null || !fromStore(stored))
|
|
359
|
+
return null;
|
|
360
|
+
return {
|
|
361
|
+
catalog: orderedCatalog(stored.catalog, runnerOrder()),
|
|
362
|
+
cache: stored.runners,
|
|
363
|
+
skills: await skills(),
|
|
364
|
+
origin: 'store',
|
|
365
|
+
};
|
|
366
|
+
},
|
|
367
|
+
viewKey: currentViewKey,
|
|
368
|
+
async savedCatalog() {
|
|
369
|
+
try {
|
|
370
|
+
const view = await readView(store, root, currentViewKey());
|
|
371
|
+
if (view === null)
|
|
372
|
+
return null;
|
|
373
|
+
// The withheld unknowns name the scopes each runner needs, which a
|
|
374
|
+
// probe would have reported. Only runner ids this session knows.
|
|
375
|
+
if (toolScopes.length > 0) {
|
|
376
|
+
const known = new Set(toolScopes);
|
|
377
|
+
scopesByRunner = new Map(Object.entries(view.scopes)
|
|
378
|
+
.filter(([runner]) => known.has(runner))
|
|
379
|
+
.map(([runner, scopes]) => [runner, scopes.filter((s) => known.has(s))]));
|
|
380
|
+
}
|
|
381
|
+
const since = Math.max(0, Date.now() - view.savedAt);
|
|
382
|
+
return {
|
|
383
|
+
catalog: orderedCatalog(view.catalog, runnerOrder()),
|
|
384
|
+
cache: view.cache.map((r) => r.state === 'miss' ? r : { ...r, ageMs: (r.ageMs ?? 0) + since }),
|
|
385
|
+
skills: view.skills,
|
|
386
|
+
origin: 'saved',
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
catch {
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
async confirmRun(plan, args = [], signal) {
|
|
394
|
+
const { catalog } = await probe([plan.task.runner], signal);
|
|
395
|
+
const shown = plan.task.display;
|
|
396
|
+
const report = catalog.reports.find((r) => r.runner === plan.task.runner);
|
|
397
|
+
const now = report?.tasks.filter((t) => t.id === plan.task.id) ?? [];
|
|
398
|
+
// A runner that failed, was withheld, or lost the task with a complaint
|
|
399
|
+
// (an unparseable file) did not read the task's file cleanly.
|
|
400
|
+
const complaint = report?.diagnostics.find((d) => d.severity !== 'info');
|
|
401
|
+
const failed = report === undefined ||
|
|
402
|
+
report.status === 'error' ||
|
|
403
|
+
report.skippedForExec ||
|
|
404
|
+
(now.length !== 1 && complaint !== undefined);
|
|
405
|
+
if (failed) {
|
|
406
|
+
const why = complaint === undefined ? '' : ` (${complaint.message})`;
|
|
407
|
+
throw new SessionError('task-changed', `${shown}: not run; ${plan.task.runner} could not be read again${why}`, `"${BIN} list" shows what it reports now`);
|
|
408
|
+
}
|
|
409
|
+
if (now.length !== 1) {
|
|
410
|
+
throw new SessionError('task-changed', `${shown}: not found when ${plan.task.runner} was read again; ` +
|
|
411
|
+
'the listing it came from is out of date', `"${BIN} list" shows the tasks now`);
|
|
412
|
+
}
|
|
413
|
+
const task = now[0];
|
|
414
|
+
const same = task.cwd === plan.task.cwd &&
|
|
415
|
+
task.argv.length === plan.task.argv.length &&
|
|
416
|
+
task.argv.every((a, i) => a === plan.task.argv[i]);
|
|
417
|
+
if (!same) {
|
|
418
|
+
const line = taskCopyText(task, process.platform) ?? commandString(task.argv);
|
|
419
|
+
throw new SessionError('task-changed', `${shown}: changed since the listing it came from; it now runs: ${line}`, 'nothing ran; run it again to run the new command');
|
|
420
|
+
}
|
|
421
|
+
return resolveRun(project, root, catalog, task.id, args);
|
|
422
|
+
},
|
|
423
|
+
search(catalog, filter) {
|
|
424
|
+
return filterTasks(catalog.reports, filter);
|
|
425
|
+
},
|
|
426
|
+
searchSkills(list, filter) {
|
|
427
|
+
return filterSkills(list, filter);
|
|
428
|
+
},
|
|
429
|
+
unknowns(snapshot) {
|
|
430
|
+
let withheld = session.withheldReason() ?? 'untrusted';
|
|
431
|
+
// Once any tool is trusted here, a withheld runner is one whose own
|
|
432
|
+
// tools are not.
|
|
433
|
+
if (withheld === 'untrusted' && toolTrusted.size > 0)
|
|
434
|
+
withheld = 'tool-untrusted';
|
|
435
|
+
return unknownsOf({
|
|
436
|
+
reports: snapshot.catalog.reports,
|
|
437
|
+
cache: snapshot.cache,
|
|
438
|
+
withheld,
|
|
439
|
+
...(withheld === 'exec-off' ? { execOff: execOffSource(settings) } : {}),
|
|
440
|
+
missingScopes: (runner) => session.missingScopes(runner),
|
|
441
|
+
});
|
|
442
|
+
},
|
|
443
|
+
missingScopes(runner) {
|
|
444
|
+
if (gate.allowExec || toolScopes.length === 0)
|
|
445
|
+
return undefined;
|
|
446
|
+
const needs = scopesByRunner.get(runner) ?? [runner];
|
|
447
|
+
return needs.filter((s) => !toolTrusted.has(s));
|
|
448
|
+
},
|
|
449
|
+
trustable,
|
|
450
|
+
async grantTrust(scope) {
|
|
451
|
+
if (!trustable()) {
|
|
452
|
+
throw new SessionError('not-trustable', "a trust grant would not change this run's execution gate");
|
|
453
|
+
}
|
|
454
|
+
if (scope !== undefined) {
|
|
455
|
+
const parsed = parseScope(scope);
|
|
456
|
+
const result = await trustStore.trust(root, parsed);
|
|
457
|
+
toolTrusted.add(parsed);
|
|
458
|
+
return { changed: !result.alreadyTrusted };
|
|
459
|
+
}
|
|
460
|
+
const result = await trustStore.trust(root);
|
|
461
|
+
options.allowExec = true;
|
|
462
|
+
// The hint told the user to trust this repository, which they just did.
|
|
463
|
+
delete options.execWithheldHint;
|
|
464
|
+
gate = { ...gate, allowExec: true, trusted: true, reason: 'allowed', missing: [] };
|
|
465
|
+
// Tool modules are code; a grant does not import them.
|
|
466
|
+
const note = projectToolsPending
|
|
467
|
+
? `the tools ${PROJECT_CONFIG_FILENAME} names load the next time ${BIN} starts with execution allowed`
|
|
468
|
+
: undefined;
|
|
469
|
+
return { changed: !result.alreadyTrusted, ...(note !== undefined ? { note } : {}) };
|
|
470
|
+
},
|
|
471
|
+
async revokeTrust(scope) {
|
|
472
|
+
if (scope !== undefined) {
|
|
473
|
+
const removed = await trustStore.untrust(root, parseScope(scope));
|
|
474
|
+
// The user config's trust.tools may still trust it.
|
|
475
|
+
await decideTools();
|
|
476
|
+
return { changed: removed };
|
|
477
|
+
}
|
|
478
|
+
const removed = await trustStore.untrust(root);
|
|
479
|
+
return { changed: removed };
|
|
480
|
+
},
|
|
481
|
+
settings() {
|
|
482
|
+
return {
|
|
483
|
+
root,
|
|
484
|
+
settings,
|
|
485
|
+
allowExec: gate.allowExec,
|
|
486
|
+
reason: gate.reason,
|
|
487
|
+
trusted: gate.trusted,
|
|
488
|
+
trustable: trustable(),
|
|
489
|
+
tools: project.tools.map(toolSummary),
|
|
490
|
+
trustedTools: gate.allowExec ? [] : [...toolTrusted],
|
|
491
|
+
};
|
|
492
|
+
},
|
|
493
|
+
async reloadSettings() {
|
|
494
|
+
await reread();
|
|
495
|
+
return session.settings();
|
|
496
|
+
},
|
|
497
|
+
async saveSetting(key, value) {
|
|
498
|
+
const source = settings.settings.find((s) => s.key === key)?.source ?? 'default';
|
|
499
|
+
const layer = settingLayer(key, source);
|
|
500
|
+
let file;
|
|
501
|
+
try {
|
|
502
|
+
;
|
|
503
|
+
({ file } = await writeSetting({ layer, root, env, home }, key, value));
|
|
504
|
+
}
|
|
505
|
+
catch (err) {
|
|
506
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
507
|
+
throw new SessionError('config', `not saved: ${message}`);
|
|
508
|
+
}
|
|
509
|
+
await reread(`saved to ${file}, but it cannot be read back: `);
|
|
510
|
+
const flag = FLAG_OF_SETTING[key];
|
|
511
|
+
return {
|
|
512
|
+
key,
|
|
513
|
+
file,
|
|
514
|
+
layer,
|
|
515
|
+
pinned: flag !== undefined && pinnedByFlag.includes(flag),
|
|
516
|
+
snapshot: session.settings(),
|
|
517
|
+
};
|
|
518
|
+
},
|
|
519
|
+
resolveRun(catalog, id, args = []) {
|
|
520
|
+
return resolveRun(project, root, catalog, id, args);
|
|
521
|
+
},
|
|
522
|
+
};
|
|
523
|
+
return session;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* The task `id` names: by id, else by the line `list` prints for it (the
|
|
527
|
+
* copied form, with its `cd` and quoting), else by its `display`. A line or
|
|
528
|
+
* a display names a task only when no other task has it.
|
|
529
|
+
*/
|
|
530
|
+
function findTask(catalog, id) {
|
|
531
|
+
const all = catalog.reports.flatMap((r) => r.tasks);
|
|
532
|
+
const only = (tasks) => (tasks.length === 1 ? tasks[0] : undefined);
|
|
533
|
+
const lineOf = (t) => taskCopyText(t, process.platform);
|
|
534
|
+
const byId = all.filter((t) => t.id === id);
|
|
535
|
+
if (byId.length === 1)
|
|
536
|
+
return byId[0];
|
|
537
|
+
const byLine = only(all.filter((t) => lineOf(t) === id));
|
|
538
|
+
if (byLine !== undefined)
|
|
539
|
+
return byLine;
|
|
540
|
+
// Ids are `runner:source:name`, and a name may hold a `:`, so two tasks can
|
|
541
|
+
// share one. Running the first would run a task the caller did not pick.
|
|
542
|
+
if (byId.length > 1) {
|
|
543
|
+
const lines = byId.map(lineOf);
|
|
544
|
+
const usable = lines.every((line) => line !== null && all.filter((t) => lineOf(t) === line).length === 1);
|
|
545
|
+
throw new SessionError('ambiguous-task', `'${id}' is the id of ${byId.length} tasks`, usable
|
|
546
|
+
? `run one by its line from "${BIN} list": ` +
|
|
547
|
+
lines.map((line) => commandString([BIN, 'run', line])).join(' or ')
|
|
548
|
+
: `they have the same line in "${BIN} list", so ${BIN} cannot tell them apart; ` +
|
|
549
|
+
'run the command yourself');
|
|
550
|
+
}
|
|
551
|
+
return only(all.filter((t) => t.display === id));
|
|
552
|
+
}
|
|
553
|
+
/** See {@link Session.resolveRun}. */
|
|
554
|
+
export function resolveRun(project, root, catalog, id, args = []) {
|
|
555
|
+
const task = findTask(catalog, id);
|
|
556
|
+
if (task === undefined) {
|
|
557
|
+
const all = catalog.reports.flatMap((r) => r.tasks);
|
|
558
|
+
// A task that contains `id` first; else one whose name is a near miss of
|
|
559
|
+
// it. Not a fuzzy match: letters in order find unrelated tasks.
|
|
560
|
+
let close = filterTasks(catalog.reports, { query: id, mode: 'exact' })
|
|
561
|
+
.slice(0, 3)
|
|
562
|
+
.map((h) => h.task.id);
|
|
563
|
+
if (close.length === 0) {
|
|
564
|
+
const names = closestMatches(id, [...new Set(all.map((t) => t.path.at(-1) ?? t.id))]);
|
|
565
|
+
close = all
|
|
566
|
+
.filter((t) => names.includes(t.path.at(-1) ?? t.id))
|
|
567
|
+
.map((t) => t.id)
|
|
568
|
+
.slice(0, 3);
|
|
569
|
+
}
|
|
570
|
+
throw new SessionError('task-not-found', `no task '${id}'`, close.length > 0
|
|
571
|
+
? `did you mean ${close.join(', ')}?`
|
|
572
|
+
: `"${BIN} list --json" lists every task id`);
|
|
573
|
+
}
|
|
574
|
+
if (task.parametersUnknown === 'editor-variable') {
|
|
575
|
+
throw new SessionError('not-runnable', `${task.display}: ${EDITOR_VARIABLE_LABEL}`);
|
|
576
|
+
}
|
|
577
|
+
let invocation;
|
|
578
|
+
try {
|
|
579
|
+
invocation =
|
|
580
|
+
args.length === 0
|
|
581
|
+
? { argv: [...task.argv], env: {} }
|
|
582
|
+
: project.runners.invocation(task, [...args]);
|
|
583
|
+
}
|
|
584
|
+
catch (err) {
|
|
585
|
+
throw new SessionError('bad-arguments', err instanceof Error ? err.message : String(err));
|
|
586
|
+
}
|
|
587
|
+
// The binary is always the task's own: invoke may not change it.
|
|
588
|
+
if (invocation.argv[0] !== task.argv[0]) {
|
|
589
|
+
throw new SessionError('bad-arguments', `${task.runner}: invoke changed the binary from ${task.argv[0]} to ${invocation.argv[0]}`);
|
|
590
|
+
}
|
|
591
|
+
// A catalog can come from a file (the saved view, the store), so its cwd
|
|
592
|
+
// is checked here, whatever the catalog's source.
|
|
593
|
+
const cwd = task.cwd === '.' ? root : resolve(root, task.cwd);
|
|
594
|
+
const inside = relative(root, cwd);
|
|
595
|
+
if (isAbsolute(task.cwd) ||
|
|
596
|
+
inside === '..' ||
|
|
597
|
+
inside.startsWith(`..${sep}`) ||
|
|
598
|
+
isAbsolute(inside)) {
|
|
599
|
+
throw new SessionError('not-runnable', `${task.display}: its directory is outside ${root}`);
|
|
600
|
+
}
|
|
601
|
+
return { task, argv: invocation.argv, env: invocation.env, cwd };
|
|
602
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent skills as catalog rows: the Skills group. A skill is not a task. It
|
|
3
|
+
* has no command line; an agent runs it when a person types `/<name>`. So
|
|
4
|
+
* the run action copies {@link SkillRow.invoke} and nothing is spawned.
|
|
5
|
+
*
|
|
6
|
+
* Skill names, descriptions and hints come from files in the repository or
|
|
7
|
+
* the user's config. Filter them like any repository text before printing.
|
|
8
|
+
*
|
|
9
|
+
* Browser-safe: it imports only the matcher, so the page can use it.
|
|
10
|
+
*/
|
|
11
|
+
import type { TaskFilter } from './filter.ts';
|
|
12
|
+
/** Which skills a catalog lists: the `skills` setting. */
|
|
13
|
+
export declare const SKILLS_SETTING_VALUES: readonly ["project", "all", "off"];
|
|
14
|
+
/**
|
|
15
|
+
* `project`: the skills the repository ships (the default). `all`: those and
|
|
16
|
+
* the user's and plugins' skills, read from the home folder. `off`: none.
|
|
17
|
+
*/
|
|
18
|
+
export type SkillsSetting = (typeof SKILLS_SETTING_VALUES)[number];
|
|
19
|
+
/** One agent skill. */
|
|
20
|
+
export interface SkillRow {
|
|
21
|
+
/** Stable: `skill:` and the skill file's path. */
|
|
22
|
+
id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
description: string | null;
|
|
25
|
+
/** What to type after `/<name>`, such as `[pr-number]`. Null when the file sets none. */
|
|
26
|
+
argumentHint: string | null;
|
|
27
|
+
/** The `SKILL.md` file: relative to the root inside it, else absolute. */
|
|
28
|
+
path: string;
|
|
29
|
+
/** The config scope it was found in: `project`, `local`, `user` and so on. */
|
|
30
|
+
scope: string;
|
|
31
|
+
/** `project`, `user`, `local`, `managed`, or `plugin:<id>`. */
|
|
32
|
+
origin: string;
|
|
33
|
+
/** The agents that read this skill file: `claude`, `codex`, `cursor`. */
|
|
34
|
+
harnesses: string[];
|
|
35
|
+
/** What the run action copies: `/<name>`. */
|
|
36
|
+
invoke: string;
|
|
37
|
+
}
|
|
38
|
+
/** One kept skill, with its score and the offsets its name matched at. */
|
|
39
|
+
export interface SkillHit {
|
|
40
|
+
skill: SkillRow;
|
|
41
|
+
/** 0 when there is no query. */
|
|
42
|
+
score: number;
|
|
43
|
+
/** Offsets into `skill.name`, when the name is what matched. */
|
|
44
|
+
labelHits: number[] | null;
|
|
45
|
+
}
|
|
46
|
+
/** The text a surface shows for a skill: `/review [pr-number]`. */
|
|
47
|
+
export declare function skillLine(skill: Pick<SkillRow, 'invoke' | 'argumentHint'>): string;
|
|
48
|
+
/**
|
|
49
|
+
* The skills that pass `filter`, matched like tasks: by name, then the
|
|
50
|
+
* `/<name>` line, the description and the origin. With a query they are
|
|
51
|
+
* ranked best first; without one they keep their order.
|
|
52
|
+
*/
|
|
53
|
+
export declare function filterSkills(skills: readonly SkillRow[], filter?: TaskFilter): SkillHit[];
|