@omercnet/paseo-omp 5.4.0 → 5.5.0-next.429.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/OVERVIEW.md +7 -1
- package/README.md +2 -1
- package/client/ast-edit.tsx +53 -0
- package/client/composer-pill-settings.tsx +0 -5
- package/client/config-sidebar.tsx +15 -0
- package/client/generated-image.tsx +79 -0
- package/client/lsp-diagnostics.tsx +69 -0
- package/client/omp-config-surface.tsx +438 -108
- package/client/omp-config-views.ts +32 -5
- package/client/omp-extensions-section.tsx +145 -0
- package/client/omp-local-models-section.tsx +215 -0
- package/client/omp-plugin-manager.tsx +198 -3
- package/client/omp-routing-editor.tsx +123 -2
- package/client/omp-rule-test.tsx +140 -0
- package/client/omp-rules-section.tsx +353 -0
- package/client/omp-security-section.tsx +99 -0
- package/client/omp-security-state.ts +14 -0
- package/client/omp-skills-section.tsx +195 -0
- package/client/omp-tool-config-section.tsx +204 -0
- package/client/omp-tool-policy-state.ts +101 -0
- package/client/omp-tool-policy.tsx +435 -0
- package/client/omp-update-row.tsx +87 -0
- package/client/provider-diagnostics-state.ts +38 -1
- package/client/provider-image.tsx +1 -1
- package/client/rule-notice.tsx +33 -0
- package/client/sidebar-compat.ts +0 -109
- package/client/web.ts +34 -0
- package/index.client.tsx +79 -46
- package/index.server.ts +77 -3
- package/npm-shrinkwrap.json +2 -2
- package/package.json +4 -4
- package/server/generated-images.ts +57 -0
- package/server/omp-agents.ts +170 -0
- package/server/omp-extensions.ts +235 -0
- package/server/omp-list-setting.ts +98 -0
- package/server/omp-local-models.ts +202 -0
- package/server/omp-models-file.ts +178 -0
- package/server/omp-plugins.ts +182 -0
- package/server/omp-prompt-files.ts +41 -0
- package/server/omp-rule-firings.ts +211 -0
- package/server/omp-rule-switch.ts +38 -0
- package/server/omp-rule-test.ts +82 -0
- package/server/omp-rules.ts +452 -0
- package/server/omp-security.ts +169 -0
- package/server/omp-settings.ts +92 -16
- package/server/omp-skills.ts +179 -0
- package/server/omp-tool-config.ts +144 -0
- package/server/omp-update.ts +203 -0
- package/server/package-version.ts +1 -1
- package/server/provider/connection.ts +1 -1
- package/server/provider/image-cache.ts +1 -1
- package/server/provider/omp-rpc-environment.ts +1 -1
- package/server/provider/session-descriptors.ts +3 -0
- package/server/provider-diagnostics.ts +26 -4
- package/server/regular-file.ts +30 -0
- package/server/rule-notices.ts +112 -0
- package/server/turn-rows.ts +59 -0
- package/server/turn-screenshots.ts +140 -0
- package/shared/ast-edit.ts +124 -0
- package/shared/composer-pill-settings.ts +0 -2
- package/shared/generated-image.ts +68 -0
- package/shared/lsp-diagnostics.ts +141 -0
- package/shared/omp-agents.ts +26 -0
- package/shared/omp-extensions.ts +45 -0
- package/shared/omp-local-models.ts +44 -0
- package/shared/omp-models-file.ts +30 -0
- package/shared/omp-plugins.ts +90 -0
- package/shared/omp-prompt-files.ts +24 -0
- package/shared/omp-rules.ts +134 -0
- package/shared/omp-security.ts +50 -0
- package/shared/omp-settings.ts +211 -2
- package/shared/omp-skills.ts +80 -0
- package/shared/omp-tool-config.ts +283 -0
- package/shared/omp-update.ts +27 -0
- package/shared/provider-image.ts +10 -3
- package/shared/rule-notices.ts +20 -0
- package/client/hub-sidebar.tsx +0 -99
- package/client/sessions-popover.tsx +0 -78
- package/server/sessions.ts +0 -58
- package/shared/sessions.ts +0 -24
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { type FileHandle, lstat, mkdtemp, open, readdir, rm, stat } from "node:fs/promises";
|
|
3
|
+
import { homedir, tmpdir } from "node:os";
|
|
4
|
+
import { basename, dirname, extname, isAbsolute, join, resolve, sep } from "node:path";
|
|
5
|
+
import type { RpcInput } from "@getpaseo/plugin";
|
|
6
|
+
import { parse as parseYaml } from "yaml";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import type { listOmpRules, OmpRule, OmpRulesList } from "../shared/omp-rules";
|
|
9
|
+
import { resolveOmpExecutable } from "./omp-settings";
|
|
10
|
+
import { currentOmpEnvironment, ompAgentDir } from "./paths";
|
|
11
|
+
import { OmpPublicError } from "./provider/security";
|
|
12
|
+
import {
|
|
13
|
+
type BoundedRun,
|
|
14
|
+
buildStatefulCommandEnv,
|
|
15
|
+
defaultSpawn,
|
|
16
|
+
runBounded,
|
|
17
|
+
} from "./provider-diagnostics";
|
|
18
|
+
|
|
19
|
+
const COMMAND_TIMEOUT_MS = 30_000;
|
|
20
|
+
const KILL_GRACE_MS = 1_000;
|
|
21
|
+
const MAX_OUTPUT_BYTES = 2 * 1024 * 1024;
|
|
22
|
+
const MAX_RULES = 500;
|
|
23
|
+
const MAX_RULE_FILES = 200;
|
|
24
|
+
const MAX_FILE_BYTES = 256 * 1024;
|
|
25
|
+
const MAX_TEXT_CHARS = 2_000;
|
|
26
|
+
const MAX_LIST_ENTRIES = 50;
|
|
27
|
+
|
|
28
|
+
export class OmpRulesError extends OmpPublicError {}
|
|
29
|
+
|
|
30
|
+
export interface OmpRulesDependencies {
|
|
31
|
+
resolveExecutable(): Promise<string | null>;
|
|
32
|
+
run(
|
|
33
|
+
executable: string,
|
|
34
|
+
args: readonly string[],
|
|
35
|
+
cwd: string,
|
|
36
|
+
input?: string,
|
|
37
|
+
): Promise<BoundedRun>;
|
|
38
|
+
makeScratchDir(prefix: string): Promise<string>;
|
|
39
|
+
removeDir(path: string): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function clip(text: string): string {
|
|
43
|
+
return text.slice(0, MAX_TEXT_CHARS);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function clipList(list: readonly string[]): string[] {
|
|
47
|
+
return list.slice(0, MAX_LIST_ENTRIES).map(clip);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const TextList = z
|
|
51
|
+
.array(z.string())
|
|
52
|
+
.nullish()
|
|
53
|
+
.transform((list) => clipList(list ?? []));
|
|
54
|
+
const OptionalText = z
|
|
55
|
+
.string()
|
|
56
|
+
.nullish()
|
|
57
|
+
.transform((text) => (text ? clip(text) : null));
|
|
58
|
+
|
|
59
|
+
/** One entry of `omp ttsr list --json`. */
|
|
60
|
+
const WatchRuleSchema = z.object({
|
|
61
|
+
name: z.string().min(1).max(256),
|
|
62
|
+
path: z.string().min(1).max(4_096),
|
|
63
|
+
provider: z.string().min(1).max(64),
|
|
64
|
+
condition: TextList,
|
|
65
|
+
astCondition: TextList,
|
|
66
|
+
question: OptionalText,
|
|
67
|
+
scope: TextList,
|
|
68
|
+
globs: TextList,
|
|
69
|
+
agents: TextList,
|
|
70
|
+
description: OptionalText,
|
|
71
|
+
});
|
|
72
|
+
type WatchRule = z.infer<typeof WatchRuleSchema>;
|
|
73
|
+
|
|
74
|
+
/** An entry that does not fit is left out, so one odd rule never hides the rest. */
|
|
75
|
+
export function parseWatchRules(raw: unknown): WatchRule[] {
|
|
76
|
+
if (!Array.isArray(raw))
|
|
77
|
+
throw new OmpRulesError("omp ttsr list gave output the plugin can't read");
|
|
78
|
+
return raw.slice(0, MAX_RULES).flatMap((entry) => {
|
|
79
|
+
const parsed = WatchRuleSchema.safeParse(entry);
|
|
80
|
+
return parsed.success ? [parsed.data] : [];
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const DisabledRulesSchema = z.object({ value: z.array(z.string()) });
|
|
85
|
+
const BuiltinRulesSchema = z.object({ value: z.boolean() });
|
|
86
|
+
|
|
87
|
+
function isWithin(parent: string, child: string): boolean {
|
|
88
|
+
return child.startsWith(parent.endsWith(sep) ? parent : parent + sep);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function watchSource(rule: WatchRule, cwd: string | undefined): OmpRule["source"] {
|
|
92
|
+
if (rule.provider === "builtin-defaults") return "builtin";
|
|
93
|
+
if (rule.provider !== "native") return "imported";
|
|
94
|
+
return cwd && isWithin(cwd, rule.path) ? "project" : "user";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function toWatchRule(rule: WatchRule, cwd: string | undefined): OmpRule {
|
|
98
|
+
return {
|
|
99
|
+
name: rule.name,
|
|
100
|
+
kind: "watch",
|
|
101
|
+
source: watchSource(rule, cwd),
|
|
102
|
+
provider: rule.provider,
|
|
103
|
+
description: rule.description,
|
|
104
|
+
condition: rule.condition,
|
|
105
|
+
astCondition: rule.astCondition,
|
|
106
|
+
question: rule.question,
|
|
107
|
+
scope: rule.scope,
|
|
108
|
+
globs: rule.globs,
|
|
109
|
+
agents: rule.agents,
|
|
110
|
+
path: rule.path,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function runJson(
|
|
115
|
+
dependencies: Pick<OmpRulesDependencies, "run">,
|
|
116
|
+
executable: string,
|
|
117
|
+
args: readonly string[],
|
|
118
|
+
cwd: string,
|
|
119
|
+
input?: string,
|
|
120
|
+
): Promise<unknown> {
|
|
121
|
+
const label = `omp ${args.join(" ")}`;
|
|
122
|
+
const run = await dependencies.run(executable, args, cwd, input);
|
|
123
|
+
if (run.outcome === "timeout") throw new OmpRulesError(`${label} timed out`);
|
|
124
|
+
if (run.outcome !== "exited" || run.exitCode !== 0 || run.truncated) {
|
|
125
|
+
throw new OmpRulesError(`${label} failed`);
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
return JSON.parse(run.stdout);
|
|
129
|
+
} catch {
|
|
130
|
+
throw new OmpRulesError(`${label} gave output the plugin can't read`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function readSetting<T>(
|
|
135
|
+
dependencies: Pick<OmpRulesDependencies, "run">,
|
|
136
|
+
executable: string,
|
|
137
|
+
path: string,
|
|
138
|
+
schema: z.ZodType<{ value: T }>,
|
|
139
|
+
cwd: string,
|
|
140
|
+
): Promise<T> {
|
|
141
|
+
const parsed = schema.safeParse(
|
|
142
|
+
await runJson(dependencies, executable, ["config", "get", path, "--json"], cwd),
|
|
143
|
+
);
|
|
144
|
+
if (!parsed.success) throw new OmpRulesError(`OMP's ${path} setting could not be read`);
|
|
145
|
+
return parsed.data.value;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Reads a small regular file without following a symlink or blocking on a pipe, or returns null. */
|
|
149
|
+
export async function readRegularFile(path: string): Promise<string | null> {
|
|
150
|
+
let handle: FileHandle | undefined;
|
|
151
|
+
try {
|
|
152
|
+
// O_NOFOLLOW does not exist on Windows, so reject a symlink before opening it too.
|
|
153
|
+
if (!(await lstat(path)).isFile()) return null;
|
|
154
|
+
handle = await open(
|
|
155
|
+
path,
|
|
156
|
+
constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0),
|
|
157
|
+
);
|
|
158
|
+
const info = await handle.stat();
|
|
159
|
+
if (!info.isFile()) return null;
|
|
160
|
+
const buffer = Buffer.alloc(MAX_FILE_BYTES + 1);
|
|
161
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
162
|
+
if (bytesRead > MAX_FILE_BYTES) return null;
|
|
163
|
+
return buffer.toString("utf8", 0, bytesRead);
|
|
164
|
+
} catch {
|
|
165
|
+
return null;
|
|
166
|
+
} finally {
|
|
167
|
+
await handle?.close();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function isNonEmptyDirectory(path: string): Promise<boolean> {
|
|
172
|
+
try {
|
|
173
|
+
if (!(await lstat(path)).isDirectory()) return false;
|
|
174
|
+
return (await readdir(path)).length > 0;
|
|
175
|
+
} catch {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async function isRealDirectory(path: string): Promise<boolean> {
|
|
181
|
+
return lstat(path).then(
|
|
182
|
+
(info) => info.isDirectory(),
|
|
183
|
+
() => false,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function ruleFilesIn(directory: string): Promise<string[]> {
|
|
188
|
+
try {
|
|
189
|
+
const entries = await readdir(directory, { withFileTypes: true });
|
|
190
|
+
return entries
|
|
191
|
+
.filter((entry) => entry.isFile() && [".md", ".mdc"].includes(extname(entry.name)))
|
|
192
|
+
.map((entry) => entry.name)
|
|
193
|
+
.sort()
|
|
194
|
+
.slice(0, MAX_RULE_FILES)
|
|
195
|
+
.map((name) => join(directory, name));
|
|
196
|
+
} catch {
|
|
197
|
+
return [];
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** OMP never treats home or a temporary root as a project. */
|
|
202
|
+
const DISCOVERY_CEILINGS = new Set([
|
|
203
|
+
homedir(),
|
|
204
|
+
tmpdir(),
|
|
205
|
+
...(process.platform === "win32" ? [] : ["/tmp", "/var/tmp"]),
|
|
206
|
+
]);
|
|
207
|
+
|
|
208
|
+
function ancestors(cwd: string): string[] {
|
|
209
|
+
const dirs: string[] = [];
|
|
210
|
+
for (let current = resolve(cwd); ; current = dirname(current)) {
|
|
211
|
+
dirs.push(current);
|
|
212
|
+
if (dirname(current) === current) return dirs;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The `.omp/` OMP takes the project `RULES.md` from: the nearest non-empty one up to the repo root. */
|
|
217
|
+
async function nearestProjectConfigDir(cwd: string): Promise<string | null> {
|
|
218
|
+
const dirs = ancestors(cwd);
|
|
219
|
+
let repoRoot: string | undefined;
|
|
220
|
+
for (const dir of dirs) {
|
|
221
|
+
const isRepoRoot = await lstat(join(dir, ".git")).then(
|
|
222
|
+
() => true,
|
|
223
|
+
() => false,
|
|
224
|
+
);
|
|
225
|
+
if (isRepoRoot) {
|
|
226
|
+
repoRoot = dir;
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
for (const dir of dirs) {
|
|
231
|
+
if (DISCOVERY_CEILINGS.has(dir)) return null;
|
|
232
|
+
if (await isNonEmptyDirectory(join(dir, ".omp"))) return join(dir, ".omp");
|
|
233
|
+
if (dir === repoRoot) return null;
|
|
234
|
+
}
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
interface RuleFile {
|
|
239
|
+
name: string;
|
|
240
|
+
path: string;
|
|
241
|
+
source: "project" | "user";
|
|
242
|
+
/** `RULES.md` always applies, whatever its frontmatter says. */
|
|
243
|
+
sticky: boolean;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** OMP's own rule files, in its first-wins order. A global listing never reads project files. */
|
|
247
|
+
async function ruleFiles(cwd: string | undefined): Promise<RuleFile[]> {
|
|
248
|
+
const agentDir = ompAgentDir(currentOmpEnvironment());
|
|
249
|
+
const files: RuleFile[] = [];
|
|
250
|
+
const add = (paths: string[], source: RuleFile["source"]) => {
|
|
251
|
+
for (const path of paths) {
|
|
252
|
+
const base = path.slice(path.lastIndexOf(sep) + 1);
|
|
253
|
+
files.push({ name: base.replace(/\.(md|mdc)$/u, ""), path, source, sticky: false });
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
// A cloned project could point `.omp` or `.omp/rules` anywhere, so neither may be a symlink.
|
|
257
|
+
const projectRules = cwd ? join(cwd, ".omp", "rules") : undefined;
|
|
258
|
+
if (
|
|
259
|
+
cwd &&
|
|
260
|
+
projectRules &&
|
|
261
|
+
resolve(cwd) !== homedir() &&
|
|
262
|
+
(await isRealDirectory(join(cwd, ".omp"))) &&
|
|
263
|
+
(await isRealDirectory(projectRules))
|
|
264
|
+
)
|
|
265
|
+
add(await ruleFilesIn(projectRules), "project");
|
|
266
|
+
add(await ruleFilesIn(join(agentDir, "rules")), "user");
|
|
267
|
+
files.push({ name: "RULES", path: join(agentDir, "RULES.md"), source: "user", sticky: true });
|
|
268
|
+
const projectConfig = cwd ? await nearestProjectConfigDir(cwd) : null;
|
|
269
|
+
if (projectConfig) {
|
|
270
|
+
files.push({
|
|
271
|
+
name: "RULES@project",
|
|
272
|
+
path: join(projectConfig, "RULES.md"),
|
|
273
|
+
source: "project",
|
|
274
|
+
sticky: true,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return files;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const FrontmatterSchema = z.record(z.string(), z.unknown());
|
|
281
|
+
type Frontmatter = z.infer<typeof FrontmatterSchema>;
|
|
282
|
+
|
|
283
|
+
function frontmatter(text: string): Frontmatter {
|
|
284
|
+
const normalized = text.replace(/\r\n/gu, "\n");
|
|
285
|
+
if (!normalized.startsWith("---")) return {};
|
|
286
|
+
const end = normalized.indexOf("\n---", 3);
|
|
287
|
+
if (end === -1) return {};
|
|
288
|
+
try {
|
|
289
|
+
const parsed = FrontmatterSchema.safeParse(parseYaml(normalized.slice(3, end)));
|
|
290
|
+
return parsed.success ? parsed.data : {};
|
|
291
|
+
} catch {
|
|
292
|
+
return {};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function stringList(value: unknown, separator?: string): string[] {
|
|
297
|
+
if (typeof value === "string") {
|
|
298
|
+
const items = separator ? value.split(separator) : [value];
|
|
299
|
+
return clipList(items.map((item) => item.trim()).filter(Boolean));
|
|
300
|
+
}
|
|
301
|
+
if (!Array.isArray(value)) return [];
|
|
302
|
+
return clipList(value.filter((item): item is string => typeof item === "string"));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function classify(file: RuleFile, meta: Frontmatter): OmpRule | null {
|
|
306
|
+
const description = typeof meta.description === "string" ? clip(meta.description) : null;
|
|
307
|
+
let kind: OmpRule["kind"];
|
|
308
|
+
if (file.sticky || meta.alwaysApply === true) kind = "always";
|
|
309
|
+
else if (description) kind = "rulebook";
|
|
310
|
+
else return null;
|
|
311
|
+
return {
|
|
312
|
+
name: file.name,
|
|
313
|
+
kind,
|
|
314
|
+
source: file.source,
|
|
315
|
+
provider: "native",
|
|
316
|
+
description,
|
|
317
|
+
condition: [],
|
|
318
|
+
astCondition: [],
|
|
319
|
+
question: null,
|
|
320
|
+
scope: [],
|
|
321
|
+
globs: stringList(meta.globs),
|
|
322
|
+
agents: stringList(meta.agents, ","),
|
|
323
|
+
path: file.path,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Always-apply and rulebook rules: OMP's `ttsr list` reports only watch rules. */
|
|
328
|
+
export async function readPromptRules(
|
|
329
|
+
cwd: string | undefined,
|
|
330
|
+
hidden: ReadonlySet<string>,
|
|
331
|
+
): Promise<OmpRule[]> {
|
|
332
|
+
const seen = new Set<string>();
|
|
333
|
+
const rules: OmpRule[] = [];
|
|
334
|
+
for (const file of await ruleFiles(cwd)) {
|
|
335
|
+
if (seen.has(file.name)) continue;
|
|
336
|
+
const text = await readRegularFile(file.path);
|
|
337
|
+
// OMP drops an empty RULES.md and an `enabled: false` file before the first-wins check.
|
|
338
|
+
if (text === null || (file.sticky && text.trim() === "")) continue;
|
|
339
|
+
const meta = frontmatter(text);
|
|
340
|
+
if (meta.enabled === false) continue;
|
|
341
|
+
seen.add(file.name);
|
|
342
|
+
const rule = classify(file, meta);
|
|
343
|
+
if (rule && !hidden.has(rule.name)) rules.push(rule);
|
|
344
|
+
}
|
|
345
|
+
return rules;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
async function listRulesIn(
|
|
349
|
+
runIn: string,
|
|
350
|
+
cwd: string | undefined,
|
|
351
|
+
dependencies: OmpRulesDependencies,
|
|
352
|
+
): Promise<OmpRulesList> {
|
|
353
|
+
const executable = await dependencies.resolveExecutable();
|
|
354
|
+
if (!executable) throw new OmpRulesError("The omp command was not found on this host");
|
|
355
|
+
// Every command finishes before the caller may remove its working directory.
|
|
356
|
+
const [watchResult, disabledResult, builtinResult] = await Promise.allSettled([
|
|
357
|
+
runJson(dependencies, executable, ["ttsr", "list", "--json"], runIn).then(parseWatchRules),
|
|
358
|
+
readSetting(dependencies, executable, "ttsr.disabledRules", DisabledRulesSchema, runIn),
|
|
359
|
+
readSetting(dependencies, executable, "ttsr.builtinRules", BuiltinRulesSchema, runIn),
|
|
360
|
+
]);
|
|
361
|
+
for (const result of [watchResult, disabledResult, builtinResult]) {
|
|
362
|
+
if (result.status === "rejected") throw result.reason;
|
|
363
|
+
}
|
|
364
|
+
const watch = (watchResult as PromiseFulfilledResult<WatchRule[]>).value;
|
|
365
|
+
const disabled = (disabledResult as PromiseFulfilledResult<string[]>).value;
|
|
366
|
+
const builtinRules = (builtinResult as PromiseFulfilledResult<boolean>).value;
|
|
367
|
+
// OMP trims each name and matches the whole list; only the displayed names are clipped.
|
|
368
|
+
const allDisabled = [...new Set(disabled.map((name) => name.trim()).filter(Boolean))];
|
|
369
|
+
const disabledNames = clipList(allDisabled);
|
|
370
|
+
const hidden = new Set([...watch.map((rule) => rule.name), ...allDisabled]);
|
|
371
|
+
return {
|
|
372
|
+
status: "ready",
|
|
373
|
+
rules: [
|
|
374
|
+
...watch.map((rule) => toWatchRule(rule, cwd)),
|
|
375
|
+
...(await readPromptRules(cwd, hidden)),
|
|
376
|
+
],
|
|
377
|
+
disabled: disabledNames,
|
|
378
|
+
builtinRules,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Runs `operation` where OMP finds only this scope: the workspace, or an empty directory so no
|
|
384
|
+
* project's files leak into the global (user-level) view.
|
|
385
|
+
*/
|
|
386
|
+
export async function inOmpScope<T>(
|
|
387
|
+
cwd: string | undefined,
|
|
388
|
+
dependencies: Pick<OmpRulesDependencies, "makeScratchDir" | "removeDir">,
|
|
389
|
+
operation: (runIn: string) => Promise<T>,
|
|
390
|
+
): Promise<T> {
|
|
391
|
+
if (cwd !== undefined) {
|
|
392
|
+
const isDirectory =
|
|
393
|
+
isAbsolute(cwd) &&
|
|
394
|
+
!cwd.includes("\0") &&
|
|
395
|
+
(await stat(cwd).then(
|
|
396
|
+
(info) => info.isDirectory(),
|
|
397
|
+
() => false,
|
|
398
|
+
));
|
|
399
|
+
if (!isDirectory) throw new OmpRulesError("The workspace path is invalid");
|
|
400
|
+
return operation(cwd);
|
|
401
|
+
}
|
|
402
|
+
const prefix = join(tmpdir(), "paseo-omp-rules-");
|
|
403
|
+
const scratch = await dependencies.makeScratchDir(prefix);
|
|
404
|
+
try {
|
|
405
|
+
return await operation(scratch);
|
|
406
|
+
} finally {
|
|
407
|
+
// Only ever delete the directory this call created directly under the system temp directory.
|
|
408
|
+
const ours =
|
|
409
|
+
scratch === resolve(scratch) &&
|
|
410
|
+
dirname(scratch) === tmpdir() &&
|
|
411
|
+
basename(scratch).startsWith("paseo-omp-rules-") &&
|
|
412
|
+
basename(scratch) !== "paseo-omp-rules-";
|
|
413
|
+
if (ours) await dependencies.removeDir(scratch).catch(() => undefined);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export async function listOmpRulesWithDependencies(
|
|
418
|
+
input: RpcInput<typeof listOmpRules>,
|
|
419
|
+
dependencies: OmpRulesDependencies,
|
|
420
|
+
): Promise<OmpRulesList> {
|
|
421
|
+
try {
|
|
422
|
+
return await inOmpScope(input.cwd, dependencies, (runIn) =>
|
|
423
|
+
listRulesIn(runIn, input.cwd, dependencies),
|
|
424
|
+
);
|
|
425
|
+
} catch (error) {
|
|
426
|
+
if (error instanceof OmpRulesError) return { status: "error", message: error.message };
|
|
427
|
+
throw error;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export const ompCommandDependencies: OmpRulesDependencies = {
|
|
432
|
+
resolveExecutable: resolveOmpExecutable,
|
|
433
|
+
run: (executable, args, cwd, input) =>
|
|
434
|
+
runBounded(
|
|
435
|
+
defaultSpawn,
|
|
436
|
+
executable,
|
|
437
|
+
args,
|
|
438
|
+
buildStatefulCommandEnv(currentOmpEnvironment()),
|
|
439
|
+
COMMAND_TIMEOUT_MS,
|
|
440
|
+
KILL_GRACE_MS,
|
|
441
|
+
MAX_OUTPUT_BYTES,
|
|
442
|
+
cwd,
|
|
443
|
+
0,
|
|
444
|
+
input,
|
|
445
|
+
),
|
|
446
|
+
makeScratchDir: (prefix) => mkdtemp(prefix),
|
|
447
|
+
removeDir: (path) => rm(path, { recursive: true, force: true }),
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
export function resolveListOmpRules(input: RpcInput<typeof listOmpRules>): Promise<OmpRulesList> {
|
|
451
|
+
return listOmpRulesWithDependencies(input, ompCommandDependencies);
|
|
452
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { RpcInput } from "@getpaseo/plugin";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import {
|
|
4
|
+
type listOmpSecurity,
|
|
5
|
+
type OmpSecurityList,
|
|
6
|
+
type OmpSecurityScan,
|
|
7
|
+
type OmpSecuritySeverity,
|
|
8
|
+
OmpSecuritySeveritySchema,
|
|
9
|
+
} from "../shared/omp-security";
|
|
10
|
+
import {
|
|
11
|
+
type OmpListDependencies,
|
|
12
|
+
ompListDependencies,
|
|
13
|
+
requireOmpExecutable,
|
|
14
|
+
} from "./omp-list-setting";
|
|
15
|
+
import { inOmpScope, readSetting } from "./omp-rules";
|
|
16
|
+
import { OmpPublicError } from "./provider/security";
|
|
17
|
+
|
|
18
|
+
const MAX_SCANS = 20;
|
|
19
|
+
const MAX_PARALLEL_READS = 4;
|
|
20
|
+
const MAX_FIELD_CHARS = 256;
|
|
21
|
+
const DASH = "\u2014";
|
|
22
|
+
const SCAN_LINE = new RegExp(
|
|
23
|
+
`^- \`(secscan_[A-Za-z0-9]+)\` ${DASH} (\\w+); (\\d+) finding\\(s\\); (.*); (\\S+)$`,
|
|
24
|
+
"u",
|
|
25
|
+
);
|
|
26
|
+
const FINDING_LINE = new RegExp(`^- \`[^\`]+\` \\*\\*(\\w+)\\*\\* ${DASH} `, "u");
|
|
27
|
+
const EnabledSchema = z.object({ value: z.boolean() });
|
|
28
|
+
|
|
29
|
+
export class OmpSecurityError extends OmpPublicError {}
|
|
30
|
+
|
|
31
|
+
interface ListedScan {
|
|
32
|
+
id: string;
|
|
33
|
+
phase: string;
|
|
34
|
+
findingCount: number;
|
|
35
|
+
producer: string;
|
|
36
|
+
createdAt: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface ParsedScans {
|
|
40
|
+
scans: ListedScan[];
|
|
41
|
+
/** Bullet lines that did not parse, or repeated an id: an odd producer name can break a line. */
|
|
42
|
+
skipped: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The scan lines of `security://scans`. A heading or a blank line is ignored; a bullet that does
|
|
47
|
+
* not parse, or whose id was already listed, is counted as skipped so the tab can say so.
|
|
48
|
+
*/
|
|
49
|
+
export function parseScanLines(text: string): ParsedScans {
|
|
50
|
+
const scans: ListedScan[] = [];
|
|
51
|
+
const seen = new Set<string>();
|
|
52
|
+
let skipped = 0;
|
|
53
|
+
for (const line of text.split("\n")) {
|
|
54
|
+
if (!line.startsWith("- `")) continue;
|
|
55
|
+
const match = SCAN_LINE.exec(line);
|
|
56
|
+
if (!match || seen.has(match[1])) {
|
|
57
|
+
skipped += 1;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const [, id, phase, count, producer, createdAt] = match;
|
|
61
|
+
seen.add(id);
|
|
62
|
+
scans.push({
|
|
63
|
+
id: id.slice(0, MAX_FIELD_CHARS),
|
|
64
|
+
phase: phase.slice(0, MAX_FIELD_CHARS),
|
|
65
|
+
findingCount: Number(count),
|
|
66
|
+
producer: producer.slice(0, MAX_FIELD_CHARS),
|
|
67
|
+
createdAt: createdAt.slice(0, MAX_FIELD_CHARS),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return { scans, skipped };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Counts the severity at the start of each finding line; a title can never be mistaken for one. */
|
|
74
|
+
export function countSeverities(text: string): Record<OmpSecuritySeverity, number> {
|
|
75
|
+
const counts: Record<OmpSecuritySeverity, number> = {
|
|
76
|
+
critical: 0,
|
|
77
|
+
high: 0,
|
|
78
|
+
medium: 0,
|
|
79
|
+
low: 0,
|
|
80
|
+
informational: 0,
|
|
81
|
+
};
|
|
82
|
+
for (const line of text.split("\n")) {
|
|
83
|
+
const level = OmpSecuritySeveritySchema.safeParse(FINDING_LINE.exec(line)?.[1]);
|
|
84
|
+
if (level.success) counts[level.data] += 1;
|
|
85
|
+
}
|
|
86
|
+
return counts;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function readResource(
|
|
90
|
+
dependencies: OmpListDependencies,
|
|
91
|
+
executable: string,
|
|
92
|
+
url: string,
|
|
93
|
+
cwd: string,
|
|
94
|
+
): Promise<string> {
|
|
95
|
+
const run = await dependencies.run(executable, ["read", url], cwd);
|
|
96
|
+
if (run.outcome === "timeout") throw new OmpSecurityError(`omp read ${url} timed out`);
|
|
97
|
+
if (run.outcome !== "exited" || run.exitCode !== 0 || run.truncated) {
|
|
98
|
+
throw new OmpSecurityError(`omp read ${url} failed`);
|
|
99
|
+
}
|
|
100
|
+
return run.stdout;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** A scan whose findings cannot be read still lists, with its counts marked incomplete. */
|
|
104
|
+
async function withSeverities(
|
|
105
|
+
dependencies: OmpListDependencies,
|
|
106
|
+
executable: string,
|
|
107
|
+
scan: ListedScan,
|
|
108
|
+
cwd: string,
|
|
109
|
+
): Promise<OmpSecurityScan> {
|
|
110
|
+
const url = `security://scans/${scan.id}/findings`;
|
|
111
|
+
const text =
|
|
112
|
+
scan.findingCount === 0
|
|
113
|
+
? ""
|
|
114
|
+
: await readResource(dependencies, executable, url, cwd).catch((error: unknown) => {
|
|
115
|
+
if (error instanceof OmpSecurityError) return null;
|
|
116
|
+
throw error;
|
|
117
|
+
});
|
|
118
|
+
const severities = countSeverities(text ?? "");
|
|
119
|
+
const counted = Object.values(severities).reduce((sum, count) => sum + count, 0);
|
|
120
|
+
return {
|
|
121
|
+
...scan,
|
|
122
|
+
severities,
|
|
123
|
+
severitiesComplete: text !== null && counted === scan.findingCount,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Scans live in OMP's private, project-keyed store. The plugin reads them through OMP itself
|
|
129
|
+
* (`omp read security://...`), the documented read-only surface, so the store layout, profiles and
|
|
130
|
+
* XDG roots stay OMP's business. Only phase and counts leave the server, never finding details.
|
|
131
|
+
*/
|
|
132
|
+
export async function listOmpSecurityWithDependencies(
|
|
133
|
+
input: RpcInput<typeof listOmpSecurity>,
|
|
134
|
+
dependencies: OmpListDependencies,
|
|
135
|
+
): Promise<OmpSecurityList> {
|
|
136
|
+
try {
|
|
137
|
+
const executable = await requireOmpExecutable(dependencies);
|
|
138
|
+
return await inOmpScope(input.cwd, dependencies, async (runIn) => {
|
|
139
|
+
const enabled = await readSetting(
|
|
140
|
+
dependencies,
|
|
141
|
+
executable,
|
|
142
|
+
"security.enabled",
|
|
143
|
+
EnabledSchema,
|
|
144
|
+
runIn,
|
|
145
|
+
);
|
|
146
|
+
if (!enabled) return { status: "off" as const };
|
|
147
|
+
const text = await readResource(dependencies, executable, "security://scans", runIn);
|
|
148
|
+
const { scans: listed, skipped } = parseScanLines(text);
|
|
149
|
+
const shown = listed.slice(0, MAX_SCANS);
|
|
150
|
+
const scans: OmpSecurityScan[] = [];
|
|
151
|
+
for (let start = 0; start < shown.length; start += MAX_PARALLEL_READS) {
|
|
152
|
+
const batch = shown.slice(start, start + MAX_PARALLEL_READS);
|
|
153
|
+
scans.push(
|
|
154
|
+
...(await Promise.all(
|
|
155
|
+
batch.map((scan) => withSeverities(dependencies, executable, scan, runIn)),
|
|
156
|
+
)),
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
return { status: "ready" as const, scans, truncated: listed.length > shown.length, skipped };
|
|
160
|
+
});
|
|
161
|
+
} catch (error) {
|
|
162
|
+
if (error instanceof OmpPublicError) return { status: "error", message: error.message };
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function resolveListOmpSecurity(input: RpcInput<typeof listOmpSecurity>) {
|
|
168
|
+
return listOmpSecurityWithDependencies(input, ompListDependencies);
|
|
169
|
+
}
|