@omercnet/paseo-omp 5.4.0 → 5.5.0-next.427.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,203 @@
|
|
|
1
|
+
import { tmpdir } from "node:os";
|
|
2
|
+
import { delimiter, dirname, parse } from "node:path";
|
|
3
|
+
import type { RpcInput } from "@getpaseo/plugin";
|
|
4
|
+
import {
|
|
5
|
+
type getOmpUpdateStatus,
|
|
6
|
+
type installOmpUpdate,
|
|
7
|
+
type OmpUpdateStatus,
|
|
8
|
+
OmpUpdateStatusSchema,
|
|
9
|
+
} from "../shared/omp-update";
|
|
10
|
+
import { SerialMutationQueue } from "./mutation-queue";
|
|
11
|
+
import { resolveOmpExecutable } from "./omp-settings";
|
|
12
|
+
import { currentOmpEnvironment } from "./paths";
|
|
13
|
+
import {
|
|
14
|
+
type BoundedRun,
|
|
15
|
+
buildStatefulCommandEnv,
|
|
16
|
+
defaultSpawn,
|
|
17
|
+
runBounded,
|
|
18
|
+
} from "./provider-diagnostics";
|
|
19
|
+
|
|
20
|
+
const CHECK_TIMEOUT_MS = 20_000;
|
|
21
|
+
const INSTALL_TIMEOUT_MS = 5 * 60_000;
|
|
22
|
+
const KILL_GRACE_MS = 1_000;
|
|
23
|
+
const MAX_OUTPUT_BYTES = 64 * 1024;
|
|
24
|
+
const CHECK_CACHE_TTL_MS = 10 * 60_000;
|
|
25
|
+
// Network and install-root settings an npm, bun or binary self-update needs. Credentials and
|
|
26
|
+
// tokens stay excluded.
|
|
27
|
+
const UPDATE_ENV = [
|
|
28
|
+
"HTTPS_PROXY",
|
|
29
|
+
"HTTP_PROXY",
|
|
30
|
+
"NO_PROXY",
|
|
31
|
+
"https_proxy",
|
|
32
|
+
"http_proxy",
|
|
33
|
+
"no_proxy",
|
|
34
|
+
"BUN_INSTALL",
|
|
35
|
+
"NODE_EXTRA_CA_CERTS",
|
|
36
|
+
"SSL_CERT_FILE",
|
|
37
|
+
"SSL_CERT_DIR",
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
export interface OmpUpdateDependencies {
|
|
41
|
+
resolveExecutable(): Promise<string | null>;
|
|
42
|
+
run(executable: string, args: readonly string[], timeoutMs: number): Promise<BoundedRun>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface OmpInstallResult {
|
|
46
|
+
ok: boolean;
|
|
47
|
+
message: string;
|
|
48
|
+
status: OmpUpdateStatus;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* `omp update` finds the install to replace by looking up `omp` on PATH, not by its own path, so
|
|
53
|
+
* the directory of the executable we run goes first: the check and the update then act on the
|
|
54
|
+
* install the Diagnostics card shows.
|
|
55
|
+
*/
|
|
56
|
+
export function updateEnvironment(executable: string): NodeJS.ProcessEnv {
|
|
57
|
+
const source = currentOmpEnvironment();
|
|
58
|
+
const env = buildStatefulCommandEnv(source);
|
|
59
|
+
for (const name of UPDATE_ENV) {
|
|
60
|
+
const value = source[name];
|
|
61
|
+
if (value !== undefined) env[name] = value;
|
|
62
|
+
}
|
|
63
|
+
env.PATH = [dirname(executable), env.PATH].filter(Boolean).join(delimiter);
|
|
64
|
+
env.NO_COLOR = "1";
|
|
65
|
+
return env;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function runOmp(executable: string, args: readonly string[], timeoutMs: number) {
|
|
69
|
+
return runBounded(
|
|
70
|
+
defaultSpawn,
|
|
71
|
+
executable,
|
|
72
|
+
args,
|
|
73
|
+
updateEnvironment(executable),
|
|
74
|
+
timeoutMs,
|
|
75
|
+
KILL_GRACE_MS,
|
|
76
|
+
MAX_OUTPUT_BYTES,
|
|
77
|
+
tmpdir(),
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** An executable under another name cannot be found by omp's PATH lookup, so it is refused. */
|
|
82
|
+
async function resolveUpdateTarget(): Promise<string | null> {
|
|
83
|
+
const executable = await resolveOmpExecutable();
|
|
84
|
+
return executable && parse(executable).name.toLowerCase() === "omp" ? executable : null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function succeeded(run: BoundedRun): boolean {
|
|
88
|
+
return (
|
|
89
|
+
run.outcome === "exited" &&
|
|
90
|
+
run.exitCode === 0 &&
|
|
91
|
+
run.signal === null &&
|
|
92
|
+
!run.truncated &&
|
|
93
|
+
!run.cleanupFailed
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Reads the two lines `omp update --check` documents: "New version available" or "Already up to date". */
|
|
98
|
+
export function parseOmpUpdateCheck(stdout: string): OmpUpdateStatus {
|
|
99
|
+
const available = /^New version available: (\S+)\s*$/m.exec(stdout);
|
|
100
|
+
if (available) {
|
|
101
|
+
const parsed = OmpUpdateStatusSchema.safeParse({ state: "available", version: available[1] });
|
|
102
|
+
return parsed.success ? parsed.data : { state: "unknown" };
|
|
103
|
+
}
|
|
104
|
+
return /Already up to date/.test(stdout) ? { state: "up-to-date" } : { state: "unknown" };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function installMessage(run: BoundedRun, status: OmpUpdateStatus): string {
|
|
108
|
+
if (run.cleanupFailed) return "OMP update process cleanup could not be confirmed.";
|
|
109
|
+
if (run.outcome === "timeout") return "OMP update timed out. The install may be incomplete.";
|
|
110
|
+
if (!succeeded(run)) return "OMP could not update itself. Update it the way it was installed.";
|
|
111
|
+
if (status.state === "available") {
|
|
112
|
+
return "OMP is still on the old version. If a package manager installed it, update it there.";
|
|
113
|
+
}
|
|
114
|
+
if (status.state === "unknown") return "OMP update finished, but the new version is unconfirmed.";
|
|
115
|
+
return "OMP updated. Running agents keep the old version until they restart.";
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Owns the update check cache. `omp update --check` goes to the network, so a recent answer is
|
|
120
|
+
* reused and concurrent callers share one run. A failed check is never cached. An install
|
|
121
|
+
* invalidates every check that started before it ended, so a slow check cannot write a stale
|
|
122
|
+
* answer over the post-install one.
|
|
123
|
+
*/
|
|
124
|
+
export function createOmpUpdateService(
|
|
125
|
+
dependencies: OmpUpdateDependencies,
|
|
126
|
+
now: () => number = Date.now,
|
|
127
|
+
) {
|
|
128
|
+
let cached: { status: OmpUpdateStatus; expiresAt: number } | null = null;
|
|
129
|
+
let inFlight: Promise<OmpUpdateStatus> | null = null;
|
|
130
|
+
let generation = 0;
|
|
131
|
+
const installs = new SerialMutationQueue();
|
|
132
|
+
|
|
133
|
+
async function checkNow(): Promise<OmpUpdateStatus> {
|
|
134
|
+
const executable = await dependencies.resolveExecutable();
|
|
135
|
+
if (!executable) return { state: "unknown" };
|
|
136
|
+
const run = await dependencies.run(executable, ["update", "--check"], CHECK_TIMEOUT_MS);
|
|
137
|
+
return succeeded(run) ? parseOmpUpdateCheck(run.stdout) : { state: "unknown" };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function check(force: boolean): Promise<OmpUpdateStatus> {
|
|
141
|
+
if (!force && cached && cached.expiresAt > now()) return Promise.resolve(cached.status);
|
|
142
|
+
if (inFlight) return inFlight;
|
|
143
|
+
const started = generation;
|
|
144
|
+
const request = checkNow()
|
|
145
|
+
.then((status) => {
|
|
146
|
+
if (started === generation && status.state !== "unknown") {
|
|
147
|
+
cached = { status, expiresAt: now() + CHECK_CACHE_TTL_MS };
|
|
148
|
+
}
|
|
149
|
+
return status;
|
|
150
|
+
})
|
|
151
|
+
.finally(() => {
|
|
152
|
+
if (inFlight === request) inFlight = null;
|
|
153
|
+
});
|
|
154
|
+
inFlight = request;
|
|
155
|
+
return request;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function invalidate(): void {
|
|
159
|
+
generation += 1;
|
|
160
|
+
cached = null;
|
|
161
|
+
inFlight = null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function install(): Promise<OmpInstallResult> {
|
|
165
|
+
const executable = await dependencies.resolveExecutable();
|
|
166
|
+
if (!executable) {
|
|
167
|
+
return {
|
|
168
|
+
ok: false,
|
|
169
|
+
message: "OMP can only be updated when its executable is named omp.",
|
|
170
|
+
status: { state: "unknown" },
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
invalidate();
|
|
174
|
+
const run = await dependencies.run(executable, ["update"], INSTALL_TIMEOUT_MS);
|
|
175
|
+
invalidate();
|
|
176
|
+
const status = await check(true);
|
|
177
|
+
return {
|
|
178
|
+
ok: succeeded(run) && status.state === "up-to-date",
|
|
179
|
+
message: installMessage(run, status),
|
|
180
|
+
status,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
status: (input: RpcInput<typeof getOmpUpdateStatus>) => check(input.force === true),
|
|
186
|
+
install: () => installs.run(install),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Binary-global, so the handlers are not store-scoped: a selected profile changes nothing here.
|
|
191
|
+
const service = createOmpUpdateService({ resolveExecutable: resolveUpdateTarget, run: runOmp });
|
|
192
|
+
|
|
193
|
+
export function resolveGetOmpUpdateStatus(
|
|
194
|
+
input: RpcInput<typeof getOmpUpdateStatus>,
|
|
195
|
+
): Promise<OmpUpdateStatus> {
|
|
196
|
+
return service.status(input);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function resolveInstallOmpUpdate(
|
|
200
|
+
_input: RpcInput<typeof installOmpUpdate>,
|
|
201
|
+
): Promise<OmpInstallResult> {
|
|
202
|
+
return service.install();
|
|
203
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** Release Please keeps this bundle-safe constant aligned with package.json. */
|
|
2
|
-
export const PASEO_OMP_PACKAGE_VERSION = "5.
|
|
2
|
+
export const PASEO_OMP_PACKAGE_VERSION = "5.5.0-next.427.1"; // x-release-please-version
|
|
@@ -925,7 +925,7 @@ export function createOmpConnection(
|
|
|
925
925
|
requestFailure(
|
|
926
926
|
input.requestId,
|
|
927
927
|
new OmpPublicError(
|
|
928
|
-
`OMP session limit reached (${sessionLimit.maxSessions} open sessions). Archive idle agents or raise the session limit in OMP → Plugin.`,
|
|
928
|
+
`OMP session limit reached (${sessionLimit.maxSessions} open sessions). Archive idle agents or raise the session limit in OMP → Plugin provider.`,
|
|
929
929
|
),
|
|
930
930
|
);
|
|
931
931
|
return;
|
|
@@ -23,7 +23,7 @@ import type { getOmpImage } from "../../shared/provider-image";
|
|
|
23
23
|
import type { OmpImageMimeType } from "./image";
|
|
24
24
|
|
|
25
25
|
/** Matches the projector's 8 MiB encoded image bound. */
|
|
26
|
-
const MAX_CACHED_IMAGE_BYTES = 6 * 1024 * 1024;
|
|
26
|
+
export const MAX_CACHED_IMAGE_BYTES = 6 * 1024 * 1024;
|
|
27
27
|
const MAX_CACHE_BYTES = 256 * 1024 * 1024;
|
|
28
28
|
const MAX_CACHE_FILES = 1_024;
|
|
29
29
|
const PRIVATE_DIRECTORY_MODE = 0o700;
|
|
@@ -200,7 +200,7 @@ function inheritedEnvironmentNames(inheritEnv: readonly string[] | undefined): R
|
|
|
200
200
|
return names;
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
function buildOmpEnvironment(
|
|
203
|
+
export function buildOmpEnvironment(
|
|
204
204
|
sessionEnv: Readonly<Record<string, string>> | undefined,
|
|
205
205
|
inheritEnv: readonly string[] | undefined,
|
|
206
206
|
sourceEnv: NodeJS.ProcessEnv,
|
|
@@ -59,6 +59,8 @@ export interface OmpSessionListOptions {
|
|
|
59
59
|
sessionId?: string;
|
|
60
60
|
sessionDir?: string;
|
|
61
61
|
signal?: AbortSignal;
|
|
62
|
+
/** Called when a scan bound ended the listing early, so the result may miss sessions. */
|
|
63
|
+
onTruncated?: () => void;
|
|
62
64
|
}
|
|
63
65
|
|
|
64
66
|
interface ScanBudget {
|
|
@@ -510,6 +512,7 @@ export async function listOmpSessionDescriptors(
|
|
|
510
512
|
options.signal,
|
|
511
513
|
);
|
|
512
514
|
options.signal?.throwIfAborted();
|
|
515
|
+
if (budgetExceeded(budget)) options.onTruncated?.();
|
|
513
516
|
return matches;
|
|
514
517
|
}
|
|
515
518
|
|
|
@@ -118,6 +118,8 @@ export type SpawnFn = (
|
|
|
118
118
|
args: readonly string[],
|
|
119
119
|
env: NodeJS.ProcessEnv,
|
|
120
120
|
cwd?: string,
|
|
121
|
+
/** Written to the child's stdin, which is closed afterwards. Without it stdin closes at once. */
|
|
122
|
+
input?: string,
|
|
121
123
|
) => ProbeChildProcess;
|
|
122
124
|
|
|
123
125
|
export type SignalProcess = (pid: number, signal: NodeJS.Signals | 0) => void;
|
|
@@ -234,13 +236,18 @@ export function defaultSpawn(
|
|
|
234
236
|
args: readonly string[],
|
|
235
237
|
env: NodeJS.ProcessEnv,
|
|
236
238
|
cwd?: string,
|
|
239
|
+
input?: string,
|
|
237
240
|
): ProbeChildProcess {
|
|
238
241
|
const child = spawn(command, args, {
|
|
239
242
|
cwd,
|
|
240
|
-
stdio: ["
|
|
243
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
241
244
|
env,
|
|
242
245
|
detached: process.platform !== "win32",
|
|
243
246
|
});
|
|
247
|
+
// A child that exits without reading closes the pipe; that is not an error here. Without
|
|
248
|
+
// input, stdin closes at once, so a child that reads it sees end of input.
|
|
249
|
+
child.stdin.on("error", () => {});
|
|
250
|
+
child.stdin.end(input);
|
|
244
251
|
return {
|
|
245
252
|
pid: child.pid,
|
|
246
253
|
stdout: child.stdout,
|
|
@@ -356,6 +363,8 @@ export interface BoundedRun {
|
|
|
356
363
|
spawnErrorCode: string | null;
|
|
357
364
|
/** True when tree termination/verification failed or the leader missed its final close bound. */
|
|
358
365
|
cleanupFailed: boolean;
|
|
366
|
+
/** Only present when the caller asked for it with `stderrLimit`. Never forwarded across RPC. */
|
|
367
|
+
stderr?: string;
|
|
359
368
|
}
|
|
360
369
|
|
|
361
370
|
/**
|
|
@@ -375,11 +384,13 @@ export function runBounded(
|
|
|
375
384
|
killGraceMs: number,
|
|
376
385
|
maxBytes: number,
|
|
377
386
|
cwd?: string,
|
|
387
|
+
stderrLimit = 0,
|
|
388
|
+
input?: string,
|
|
378
389
|
): Promise<BoundedRun> {
|
|
379
390
|
const { promise, resolve } = Promise.withResolvers<BoundedRun>();
|
|
380
391
|
let child: ProbeChildProcess;
|
|
381
392
|
try {
|
|
382
|
-
child = spawnFn(command, args, env, cwd);
|
|
393
|
+
child = spawnFn(command, args, env, cwd, input);
|
|
383
394
|
} catch (error) {
|
|
384
395
|
resolve({
|
|
385
396
|
outcome: "spawn-error",
|
|
@@ -397,6 +408,8 @@ export function runBounded(
|
|
|
397
408
|
const stdoutChunks: Buffer[] = [];
|
|
398
409
|
let stdoutBytes = 0;
|
|
399
410
|
let truncated = false;
|
|
411
|
+
const stderrChunks: Buffer[] = [];
|
|
412
|
+
let stderrBytes = 0;
|
|
400
413
|
let timedOut = false;
|
|
401
414
|
let leaderClosed = false;
|
|
402
415
|
let leaderExitCode: number | null = null;
|
|
@@ -405,6 +418,7 @@ export function runBounded(
|
|
|
405
418
|
let treeCleanupSucceeded = false;
|
|
406
419
|
let finalLeaderTimer: ReturnType<typeof setTimeout> | undefined;
|
|
407
420
|
const getStdout = () => Buffer.concat(stdoutChunks, stdoutBytes).toString("utf8");
|
|
421
|
+
const getStderr = () => Buffer.concat(stderrChunks, stderrBytes).toString("utf8");
|
|
408
422
|
|
|
409
423
|
const cleanupListeners = () => {
|
|
410
424
|
child.stdout.removeAllListeners();
|
|
@@ -428,6 +442,7 @@ export function runBounded(
|
|
|
428
442
|
signal: leaderSignal,
|
|
429
443
|
spawnErrorCode: null,
|
|
430
444
|
cleanupFailed,
|
|
445
|
+
...(stderrLimit > 0 ? { stderr: getStderr() } : {}),
|
|
431
446
|
});
|
|
432
447
|
};
|
|
433
448
|
const settleAfterCleanup = () => {
|
|
@@ -482,8 +497,14 @@ export function runBounded(
|
|
|
482
497
|
stdoutChunks.push(accepted);
|
|
483
498
|
stdoutBytes += accepted.length;
|
|
484
499
|
});
|
|
485
|
-
child.stderr.on("data", () => {
|
|
486
|
-
//
|
|
500
|
+
child.stderr.on("data", (chunk: Buffer) => {
|
|
501
|
+
// Discarded unless the caller opted in with `stderrLimit`; then kept, bounded, for that
|
|
502
|
+
// caller to parse. It is never forwarded across the RPC boundary as-is.
|
|
503
|
+
const remaining = stderrLimit - stderrBytes;
|
|
504
|
+
if (remaining <= 0) return;
|
|
505
|
+
const accepted = chunk.subarray(0, remaining);
|
|
506
|
+
stderrChunks.push(accepted);
|
|
507
|
+
stderrBytes += accepted.length;
|
|
487
508
|
});
|
|
488
509
|
child.onClose((code, signal) => {
|
|
489
510
|
leaderClosed = true;
|
|
@@ -501,6 +522,7 @@ export function runBounded(
|
|
|
501
522
|
signal,
|
|
502
523
|
spawnErrorCode: null,
|
|
503
524
|
cleanupFailed: false,
|
|
525
|
+
...(stderrLimit > 0 ? { stderr: getStderr() } : {}),
|
|
504
526
|
});
|
|
505
527
|
});
|
|
506
528
|
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { closeSync, constants, fstatSync, lstatSync, openSync, readSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
/** The bytes of a regular file of at most `maxBytes`; a symlink, FIFO, directory or larger file reads as nothing. */
|
|
4
|
+
export function readRegularFile(path: string, maxBytes: number): Buffer | undefined {
|
|
5
|
+
// O_NONBLOCK keeps a FIFO planted under the file's name from hanging the worker.
|
|
6
|
+
const flags = constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0);
|
|
7
|
+
let descriptor: number;
|
|
8
|
+
try {
|
|
9
|
+
if (lstatSync(path).isSymbolicLink()) return undefined;
|
|
10
|
+
descriptor = openSync(path, flags);
|
|
11
|
+
} catch {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
const stat = fstatSync(descriptor);
|
|
16
|
+
if (!stat.isFile() || stat.size > maxBytes) return undefined;
|
|
17
|
+
// A file another user planted in a shared directory is not ours to read.
|
|
18
|
+
if (process.getuid !== undefined && stat.uid !== process.getuid()) return undefined;
|
|
19
|
+
const bytes = Buffer.alloc(stat.size);
|
|
20
|
+
let offset = 0;
|
|
21
|
+
while (offset < bytes.length) {
|
|
22
|
+
const read = readSync(descriptor, bytes, offset, bytes.length - offset, offset);
|
|
23
|
+
if (read === 0) return undefined;
|
|
24
|
+
offset += read;
|
|
25
|
+
}
|
|
26
|
+
return bytes;
|
|
27
|
+
} finally {
|
|
28
|
+
closeSync(descriptor);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { type FileHandle, open } from "node:fs/promises";
|
|
3
|
+
import { isAbsolute } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
MAX_OMP_RULE_NOTICE_RULES,
|
|
6
|
+
OMP_RULE_MATCH_MAX_LENGTH,
|
|
7
|
+
OMP_RULE_NAME_MAX_LENGTH,
|
|
8
|
+
OMP_RULE_NOTICE_KIND,
|
|
9
|
+
type OmpRuleNotice,
|
|
10
|
+
} from "../shared/rule-notices";
|
|
11
|
+
import type { OmpTurnRow } from "./turn-rows";
|
|
12
|
+
|
|
13
|
+
/** Turn rows are read from the end of the session file; a turn rarely writes more. */
|
|
14
|
+
const TAIL_BYTES = 1024 * 1024;
|
|
15
|
+
|
|
16
|
+
function bounded(value: string, max: number): string {
|
|
17
|
+
return value.length > max ? `${value.slice(0, max - 1)}…` : value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function stringArray(value: unknown): string[] {
|
|
21
|
+
return Array.isArray(value)
|
|
22
|
+
? value.filter((entry): entry is string => typeof entry === "string" && entry.length > 0)
|
|
23
|
+
: [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function readTail(file: string): Promise<string | undefined> {
|
|
27
|
+
let handle: FileHandle;
|
|
28
|
+
try {
|
|
29
|
+
handle = await open(
|
|
30
|
+
file,
|
|
31
|
+
constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0),
|
|
32
|
+
);
|
|
33
|
+
} catch {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const stat = await handle.stat();
|
|
38
|
+
if (!stat.isFile()) return;
|
|
39
|
+
const start = Math.max(0, stat.size - TAIL_BYTES);
|
|
40
|
+
const buffer = Buffer.allocUnsafe(stat.size - start);
|
|
41
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, start);
|
|
42
|
+
const text = buffer.toString("utf8", 0, bytesRead);
|
|
43
|
+
return start === 0 ? text : text.slice(text.indexOf("\n") + 1);
|
|
44
|
+
} finally {
|
|
45
|
+
await handle.close().catch(() => undefined);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The OMP rules injected between `since` and `until` (epoch ms), from OMP's persisted
|
|
51
|
+
* `ttsr_injection` entries; `id` is the first injection entry's.
|
|
52
|
+
*/
|
|
53
|
+
export async function readOmpRuleNotice(
|
|
54
|
+
sessionFile: string,
|
|
55
|
+
since: number,
|
|
56
|
+
until: number,
|
|
57
|
+
): Promise<{ id: string; notice: OmpRuleNotice } | undefined> {
|
|
58
|
+
if (!isAbsolute(sessionFile) || !sessionFile.endsWith(".jsonl")) return;
|
|
59
|
+
const tail = await readTail(sessionFile);
|
|
60
|
+
if (!tail) return;
|
|
61
|
+
let id: string | undefined;
|
|
62
|
+
const names: string[] = [];
|
|
63
|
+
const matched = new Map<string, string>();
|
|
64
|
+
for (const line of tail.split("\n")) {
|
|
65
|
+
if (!line.includes("ttsr")) continue;
|
|
66
|
+
let entry: Record<string, unknown>;
|
|
67
|
+
try {
|
|
68
|
+
entry = JSON.parse(line);
|
|
69
|
+
} catch {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const at = typeof entry.timestamp === "string" ? Date.parse(entry.timestamp) : Number.NaN;
|
|
73
|
+
if (!(at >= since && at <= until)) continue;
|
|
74
|
+
if (entry.type === "ttsr_injection") {
|
|
75
|
+
id ??= typeof entry.id === "string" ? entry.id : undefined;
|
|
76
|
+
for (const name of stringArray(entry.injectedRules)) {
|
|
77
|
+
if (!names.includes(name)) names.push(name);
|
|
78
|
+
}
|
|
79
|
+
} else if (entry.type === "custom_message" && entry.customType === "ttsr-injection") {
|
|
80
|
+
const details = entry.details as { matched?: unknown } | undefined;
|
|
81
|
+
if (details?.matched && typeof details.matched === "object") {
|
|
82
|
+
for (const [name, text] of Object.entries(details.matched)) {
|
|
83
|
+
if (typeof text === "string") matched.set(name, text);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (!id || names.length === 0) return;
|
|
89
|
+
return {
|
|
90
|
+
id,
|
|
91
|
+
notice: {
|
|
92
|
+
rules: names.slice(0, MAX_OMP_RULE_NOTICE_RULES).map((name) => {
|
|
93
|
+
const text = matched.get(name);
|
|
94
|
+
return {
|
|
95
|
+
name: bounded(name, OMP_RULE_NAME_MAX_LENGTH),
|
|
96
|
+
matched: text === undefined ? null : bounded(text, OMP_RULE_MATCH_MAX_LENGTH),
|
|
97
|
+
};
|
|
98
|
+
}),
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The OMP rule notice of one turn as a timeline row; Paseo's bundled `omp` provider drops rule events. */
|
|
104
|
+
export async function ompRuleNoticeRow(
|
|
105
|
+
sessionFile: string,
|
|
106
|
+
since: number,
|
|
107
|
+
until: number,
|
|
108
|
+
): Promise<OmpTurnRow | undefined> {
|
|
109
|
+
const found = await readOmpRuleNotice(sessionFile, since, until);
|
|
110
|
+
if (!found) return;
|
|
111
|
+
return { id: `rule-notice:${found.id}`, kind: OMP_RULE_NOTICE_KIND, data: found.notice };
|
|
112
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { PluginServerContext } from "@getpaseo/plugin/server";
|
|
2
|
+
import type { JsonValue } from "@getpaseo/protocol/agent-types";
|
|
3
|
+
|
|
4
|
+
export interface OmpTurnRow {
|
|
5
|
+
id: string;
|
|
6
|
+
kind: string;
|
|
7
|
+
data: JsonValue;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Reads one row from an OMP session file for the turn between `since` and `until` (epoch ms). */
|
|
11
|
+
export type OmpTurnRowReader = (
|
|
12
|
+
sessionFile: string,
|
|
13
|
+
since: number,
|
|
14
|
+
until: number,
|
|
15
|
+
) => Promise<OmpTurnRow | undefined>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Paseo's bundled `omp` provider drops parts of what OMP persists to its session file. After each
|
|
19
|
+
* turn of such an agent, every reader may append one timeline row built from that file.
|
|
20
|
+
*/
|
|
21
|
+
export function registerOmpTurnRows(
|
|
22
|
+
server: Pick<PluginServerContext, "addHook">,
|
|
23
|
+
readers: readonly OmpTurnRowReader[],
|
|
24
|
+
): () => void {
|
|
25
|
+
// One Paseo turn can report several native turn starts; keep the earliest. Hooks run
|
|
26
|
+
// concurrently, so state is read and cleared before the first await.
|
|
27
|
+
const turnStarts = new Map<string, number>();
|
|
28
|
+
const turnKey = (agentId: string, turnId: string | null) => `${agentId}\0${turnId ?? ""}`;
|
|
29
|
+
const removeBefore = server.addHook("agent.turn.before", async ({ agent, turnId }) => {
|
|
30
|
+
const key = turnKey(agent.id, turnId);
|
|
31
|
+
if (agent.provider === "omp" && !turnStarts.has(key)) turnStarts.set(key, Date.now());
|
|
32
|
+
});
|
|
33
|
+
const removeAfter = server.addHook("agent.turn.after", async ({ agent, turnId }, { paseo }) => {
|
|
34
|
+
const key = turnKey(agent.id, turnId);
|
|
35
|
+
const since = turnStarts.get(key);
|
|
36
|
+
const until = Date.now();
|
|
37
|
+
turnStarts.delete(key);
|
|
38
|
+
if (agent.provider !== "omp" || since === undefined) return;
|
|
39
|
+
const ref = paseo.agents.ref(agent.id);
|
|
40
|
+
const sessionFile = (await ref.refresh())?.agent.persistence?.nativeHandle;
|
|
41
|
+
if (!sessionFile) return;
|
|
42
|
+
for (const read of readers) {
|
|
43
|
+
const row = await read(sessionFile, since, until);
|
|
44
|
+
if (!row) continue;
|
|
45
|
+
await ref.timeline.append({
|
|
46
|
+
type: "plugin",
|
|
47
|
+
id: row.id,
|
|
48
|
+
kind: row.kind,
|
|
49
|
+
version: 1,
|
|
50
|
+
data: row.data,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return () => {
|
|
55
|
+
removeBefore();
|
|
56
|
+
removeAfter();
|
|
57
|
+
turnStarts.clear();
|
|
58
|
+
};
|
|
59
|
+
}
|