@foldrun/core 0.2.0 → 0.4.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/README.md +31 -10
- package/dist/src/approvals.d.ts +104 -1
- package/dist/src/approvals.js +192 -2
- package/dist/src/budget.d.ts +32 -0
- package/dist/src/budget.js +177 -0
- package/dist/src/catalog.js +2 -1
- package/dist/src/confine.js +6 -1
- package/dist/src/deploy.js +4 -2
- package/dist/src/egress.d.ts +19 -2
- package/dist/src/egress.js +2 -1
- package/dist/src/evals.d.ts +4 -0
- package/dist/src/evals.js +20 -11
- package/dist/src/flow-lint.d.ts +4 -0
- package/dist/src/flow-lint.js +108 -0
- package/dist/src/host-env.d.ts +8 -0
- package/dist/src/host-env.js +48 -0
- package/dist/src/json-schema.d.ts +12 -0
- package/dist/src/json-schema.js +202 -0
- package/dist/src/library.js +13 -1
- package/dist/src/notify.d.ts +79 -1
- package/dist/src/notify.js +243 -9
- package/dist/src/paths.d.ts +5 -0
- package/dist/src/paths.js +32 -0
- package/dist/src/platform.d.ts +112 -2
- package/dist/src/platform.js +20 -2
- package/dist/src/provider-health.d.ts +55 -0
- package/dist/src/provider-health.js +232 -0
- package/dist/src/run-container.d.ts +80 -4
- package/dist/src/run-container.js +176 -37
- package/dist/src/runner.d.ts +41 -5
- package/dist/src/runner.js +513 -81
- package/dist/src/script-tools.d.ts +12 -0
- package/dist/src/script-tools.js +10 -2
- package/dist/src/secret-health.d.ts +59 -0
- package/dist/src/secret-health.js +155 -0
- package/dist/src/secrets.d.ts +19 -1
- package/dist/src/secrets.js +44 -12
- package/dist/src/starter.js +4 -0
- package/dist/src/step-exec.d.ts +62 -6
- package/dist/src/step-exec.js +199 -68
- package/dist/src/store.d.ts +300 -3
- package/dist/src/store.js +574 -26
- package/dist/src/tar.js +2 -1
- package/dist/src/test-mode.d.ts +111 -0
- package/dist/src/test-mode.js +285 -0
- package/dist/src/tool-test.js +48 -21
- package/dist/src/translator.js +2 -1
- package/dist/src/trigger-log.d.ts +43 -0
- package/dist/src/trigger-log.js +122 -0
- package/dist/src/triggers.d.ts +20 -0
- package/dist/src/triggers.js +91 -10
- package/dist/src/webhook.d.ts +15 -3
- package/dist/src/webhook.js +11 -11
- package/package.json +6 -5
- package/src/approvals.ts +253 -2
- package/src/budget.ts +178 -0
- package/src/catalog.ts +2 -1
- package/src/confine.ts +6 -1
- package/src/deploy.ts +4 -1
- package/src/egress.ts +20 -4
- package/src/evals.ts +25 -9
- package/src/flow-lint.ts +118 -0
- package/src/host-env.ts +51 -0
- package/src/json-schema.ts +184 -0
- package/src/library.ts +13 -1
- package/src/notify.ts +272 -11
- package/src/paths.ts +29 -0
- package/src/platform.ts +119 -3
- package/src/provider-health.ts +274 -0
- package/src/run-container.ts +215 -35
- package/src/runner.ts +525 -73
- package/src/script-tools.ts +18 -2
- package/src/secret-health.ts +185 -0
- package/src/secrets.ts +44 -10
- package/src/starter.ts +4 -0
- package/src/step-exec.ts +176 -21
- package/src/storage.ts +8 -2
- package/src/store.ts +785 -25
- package/src/tar.ts +2 -1
- package/src/test-mode.ts +311 -0
- package/src/tool-test.ts +49 -20
- package/src/translator.ts +2 -1
- package/src/trigger-log.ts +159 -0
- package/src/triggers.ts +92 -10
- package/src/webhook.ts +17 -11
package/dist/src/tar.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
// 512-byte blocks with an octal size field, and a reader is shorter than the
|
|
10
10
|
// argument about which library to add.
|
|
11
11
|
import zlib from "node:zlib";
|
|
12
|
+
import { trimSlashes } from "./paths.js";
|
|
12
13
|
const BLOCK = 512;
|
|
13
14
|
const str = (b, off, len) => {
|
|
14
15
|
const raw = b.subarray(off, off + len);
|
|
@@ -86,7 +87,7 @@ export function stripRoot(entries) {
|
|
|
86
87
|
* case, since a repo usually holds more than one thing.
|
|
87
88
|
*/
|
|
88
89
|
export function filesFromTarball(gz, subdir = "") {
|
|
89
|
-
const prefix = subdir
|
|
90
|
+
const prefix = trimSlashes(subdir, "both");
|
|
90
91
|
const out = [];
|
|
91
92
|
for (const entry of stripRoot(readTarGz(gz))) {
|
|
92
93
|
let rel = entry.path;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/** Where a test run's email actually goes — Resend's delivered sink. */
|
|
2
|
+
export declare const TEST_SINK_EMAIL = "delivered@resend.dev";
|
|
3
|
+
/** Prefixed to every subject a test run sends, so the sink is readable. */
|
|
4
|
+
export declare const TEST_SUBJECT_PREFIX = "[TEST] ";
|
|
5
|
+
/** What a withheld secret's env var reads as inside the sandbox. */
|
|
6
|
+
export declare const WITHHELD = "TEST_MODE_WITHHELD";
|
|
7
|
+
/** Both are set to "1" in a test run's sandbox: the first is what a script
|
|
8
|
+
* checks, the second is the run-level spelling for verifies and flows. */
|
|
9
|
+
export declare const TEST_MODE_ENV = "FOLDRUN_TEST_MODE";
|
|
10
|
+
export declare const RUN_TEST_ENV = "FOLDRUN_RUN_TEST";
|
|
11
|
+
/** Under runs/<id>/, where diverted state/ and storage/ writes land. */
|
|
12
|
+
export declare const TEST_WRITES_DIR = "test-writes";
|
|
13
|
+
/** The workspace directories a test run may not write for real. */
|
|
14
|
+
export declare const TEST_DIVERTED_DIRS: readonly ["state", "storage"];
|
|
15
|
+
/** What a test run did instead of the real thing — one per intervention,
|
|
16
|
+
* on the run event that reports it, so the run page can list them. */
|
|
17
|
+
export interface TestEffect {
|
|
18
|
+
kind: "refused" | "redirected" | "diverted" | "withheld";
|
|
19
|
+
host?: string;
|
|
20
|
+
method?: string;
|
|
21
|
+
path?: string;
|
|
22
|
+
summary: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Secret names that can make something happen outside. Matched by name
|
|
26
|
+
* because the name is all the runner knows before the value is used: the
|
|
27
|
+
* vault does not say what a key can do, and the provider hosts are only
|
|
28
|
+
* known for http tools, not for the scripts these are withheld from.
|
|
29
|
+
*/
|
|
30
|
+
export declare const SEND_CAPABLE_SECRETS: RegExp[];
|
|
31
|
+
export declare function isSendCapableSecret(name: string): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* The secrets a test-run sandbox is handed, and which were withheld.
|
|
34
|
+
*
|
|
35
|
+
* `outward` — the step grants a script tool whose file says `outward: true`:
|
|
36
|
+
* then every secret is withheld, because a tool that declared itself a
|
|
37
|
+
* sender is not one to hand anything real. `allow` — a script tool said
|
|
38
|
+
* `test_mode: allow` (a reader that happens to use a listed key): the list
|
|
39
|
+
* is not applied. Outward wins over allow, because a step that has both
|
|
40
|
+
* has a sender in it.
|
|
41
|
+
*/
|
|
42
|
+
export declare function withholdSecrets(secrets: Record<string, string>, opts?: {
|
|
43
|
+
outward?: boolean;
|
|
44
|
+
allow?: boolean;
|
|
45
|
+
}): {
|
|
46
|
+
env: Record<string, string>;
|
|
47
|
+
withheld: string[];
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Hosts an outward write may reach on a test run: model providers (the run
|
|
51
|
+
* cannot happen without them, and nothing there is a send), and read-only
|
|
52
|
+
* services whose POSTs are queries. A host is matched exactly, or by a
|
|
53
|
+
* leading `*.` for its subdomains. The list is deliberately short — the
|
|
54
|
+
* default is refusal, and a host earns its place here by having no way to
|
|
55
|
+
* reach a customer.
|
|
56
|
+
*/
|
|
57
|
+
export declare const READ_SAFE_HOSTS: string[];
|
|
58
|
+
export declare function isReadSafeHost(host: string): boolean;
|
|
59
|
+
export type TestPolicy = {
|
|
60
|
+
action: "allow";
|
|
61
|
+
} | {
|
|
62
|
+
action: "refuse";
|
|
63
|
+
} | {
|
|
64
|
+
action: "rewrite";
|
|
65
|
+
rewrite: "resend-email";
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* What the proxy does with one request on a test lease. Pure: method, host
|
|
69
|
+
* and path in, a verdict out. Reads always pass — a test run has to be able
|
|
70
|
+
* to look at the world it is not allowed to change.
|
|
71
|
+
*/
|
|
72
|
+
export declare function testPolicy(method: string, host: string, path: string): TestPolicy;
|
|
73
|
+
/**
|
|
74
|
+
* A Resend `POST /emails` body with its recipients pointed at the sink and
|
|
75
|
+
* the subject marked. Returns the recipients it replaced, for the trace.
|
|
76
|
+
* Null when the body is not a JSON object — then there is nothing safe to
|
|
77
|
+
* rewrite, and the caller refuses instead.
|
|
78
|
+
*/
|
|
79
|
+
export declare function rewriteResendEmail(body: string): {
|
|
80
|
+
body: string;
|
|
81
|
+
to: string[];
|
|
82
|
+
} | null;
|
|
83
|
+
/** The first `max` characters of a body, whitespace collapsed — enough to
|
|
84
|
+
* say what would have gone out, not enough to be the thing itself. The
|
|
85
|
+
* caller redacts; this only cuts. */
|
|
86
|
+
export declare function bodySummary(body: string, max?: number): string;
|
|
87
|
+
/** The recipient field of a body, if it has one — for `would_have.to`. */
|
|
88
|
+
export declare function recipientsOf(body: string): string | string[] | null;
|
|
89
|
+
/** The 403 a refused write is answered with. JSON, so a script that parses
|
|
90
|
+
* the error sees `test_mode` and can say so instead of retrying. */
|
|
91
|
+
export declare function refusalBody(method: string, host: string, path: string, body: string): string;
|
|
92
|
+
/** Is this workspace-relative path one a test run diverts? */
|
|
93
|
+
export declare function isDivertedPath(rel: string): boolean;
|
|
94
|
+
/** The line the trace and the run page show for one diverted write. */
|
|
95
|
+
export declare function divertedSummary(rel: string, was: Buffer | null, next: Buffer): string;
|
|
96
|
+
/**
|
|
97
|
+
* The in-process path has no copy-in and copy-out — a step writes the
|
|
98
|
+
* workspace directly — so a test run there snapshots state/ and storage/
|
|
99
|
+
* before the step and, after it, moves what changed under the run and
|
|
100
|
+
* puts the originals back. Same outcome as the sandbox's divert, reached
|
|
101
|
+
* from the other side. Used by `foldrun run --test`.
|
|
102
|
+
*/
|
|
103
|
+
export declare function snapshotDivertedDirs(wsRoot: string): Map<string, Buffer>;
|
|
104
|
+
/** Undo a step's writes to the diverted directories, keeping each changed
|
|
105
|
+
* or new file under `to` by its workspace-relative path. Deletions are
|
|
106
|
+
* undone too — the original is put back. Returns what was set aside. */
|
|
107
|
+
export declare function restoreDivertedDirs(wsRoot: string, before: Map<string, Buffer>, to: string, note: (rel: string, summary: string) => void): string[];
|
|
108
|
+
/** `[test] ` in front of a headline, once. */
|
|
109
|
+
export declare function testHeadline(summary: string): string;
|
|
110
|
+
/** The env every test-run sandbox gets, beside its secrets. */
|
|
111
|
+
export declare function testModeEnv(): Record<string, string>;
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// Test mode: a run that exercises everything and changes nothing outside.
|
|
2
|
+
//
|
|
3
|
+
// Every desk that sends — email, SMS, orders, posts — grew its own test
|
|
4
|
+
// switch inside its own script: a `test_to` address here, a `mode: test`
|
|
5
|
+
// there. On 2026-09-14 a send step edited its own gate file to get past a
|
|
6
|
+
// script's guard and texted 25 customers. A guard the script can reach is a
|
|
7
|
+
// guard the script can move; this one lives where the script cannot: on the
|
|
8
|
+
// run record, at the egress proxy, and in what the sandbox is handed.
|
|
9
|
+
//
|
|
10
|
+
// The rule has one table (below) so it is testable without a server, and
|
|
11
|
+
// three enforcement points that all read it:
|
|
12
|
+
//
|
|
13
|
+
// - the egress proxy: on a test lease, an outward write to a host that is
|
|
14
|
+
// not read-safe or a model provider is refused with 403 and a JSON body
|
|
15
|
+
// naming what would have been sent; Resend's sink address is the one
|
|
16
|
+
// exception, so a desk still gets a real message id back
|
|
17
|
+
// - the runner: send-capable secrets never enter the sandbox as values on
|
|
18
|
+
// a test run; the env var says TEST_MODE_WITHHELD instead, so a script
|
|
19
|
+
// that forgot to check fails at the provider with an auth error
|
|
20
|
+
// - the write-back: state/ and storage/ go INTO the sandbox as usual and
|
|
21
|
+
// come out into runs/<id>/test-writes/ instead of the real directories
|
|
22
|
+
//
|
|
23
|
+
// Nothing here weakens a live run: every function is a no-op unless the run
|
|
24
|
+
// says `test: true`.
|
|
25
|
+
import fs from "node:fs";
|
|
26
|
+
import path from "node:path";
|
|
27
|
+
/** Where a test run's email actually goes — Resend's delivered sink. */
|
|
28
|
+
export const TEST_SINK_EMAIL = "delivered@resend.dev";
|
|
29
|
+
/** Prefixed to every subject a test run sends, so the sink is readable. */
|
|
30
|
+
export const TEST_SUBJECT_PREFIX = "[TEST] ";
|
|
31
|
+
/** What a withheld secret's env var reads as inside the sandbox. */
|
|
32
|
+
export const WITHHELD = "TEST_MODE_WITHHELD";
|
|
33
|
+
/** Both are set to "1" in a test run's sandbox: the first is what a script
|
|
34
|
+
* checks, the second is the run-level spelling for verifies and flows. */
|
|
35
|
+
export const TEST_MODE_ENV = "FOLDRUN_TEST_MODE";
|
|
36
|
+
export const RUN_TEST_ENV = "FOLDRUN_RUN_TEST";
|
|
37
|
+
/** Under runs/<id>/, where diverted state/ and storage/ writes land. */
|
|
38
|
+
export const TEST_WRITES_DIR = "test-writes";
|
|
39
|
+
/** The workspace directories a test run may not write for real. */
|
|
40
|
+
export const TEST_DIVERTED_DIRS = ["state", "storage"];
|
|
41
|
+
// ---------------------------------------------------------------- secrets
|
|
42
|
+
/**
|
|
43
|
+
* Secret names that can make something happen outside. Matched by name
|
|
44
|
+
* because the name is all the runner knows before the value is used: the
|
|
45
|
+
* vault does not say what a key can do, and the provider hosts are only
|
|
46
|
+
* known for http tools, not for the scripts these are withheld from.
|
|
47
|
+
*/
|
|
48
|
+
export const SEND_CAPABLE_SECRETS = [
|
|
49
|
+
/^RESEND_API_KEY$/,
|
|
50
|
+
/^TWILIO_/,
|
|
51
|
+
/^GETREACH_API_KEY$/,
|
|
52
|
+
/^GITHUB_TOKEN$/,
|
|
53
|
+
/^MONDAY_API_TOKEN$/,
|
|
54
|
+
/^OI_CRM_API_KEY$/,
|
|
55
|
+
/^CLOUDFLARE_/,
|
|
56
|
+
/^LINKEDIN_/,
|
|
57
|
+
/^MEDIUM_COOKIES$/,
|
|
58
|
+
/^TIKTOK_/,
|
|
59
|
+
];
|
|
60
|
+
export function isSendCapableSecret(name) {
|
|
61
|
+
return SEND_CAPABLE_SECRETS.some((re) => re.test(name));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The secrets a test-run sandbox is handed, and which were withheld.
|
|
65
|
+
*
|
|
66
|
+
* `outward` — the step grants a script tool whose file says `outward: true`:
|
|
67
|
+
* then every secret is withheld, because a tool that declared itself a
|
|
68
|
+
* sender is not one to hand anything real. `allow` — a script tool said
|
|
69
|
+
* `test_mode: allow` (a reader that happens to use a listed key): the list
|
|
70
|
+
* is not applied. Outward wins over allow, because a step that has both
|
|
71
|
+
* has a sender in it.
|
|
72
|
+
*/
|
|
73
|
+
export function withholdSecrets(secrets, opts = {}) {
|
|
74
|
+
const env = {};
|
|
75
|
+
const withheld = [];
|
|
76
|
+
for (const [name, value] of Object.entries(secrets)) {
|
|
77
|
+
const hold = opts.outward || (!opts.allow && isSendCapableSecret(name));
|
|
78
|
+
env[name] = hold ? WITHHELD : value;
|
|
79
|
+
if (hold)
|
|
80
|
+
withheld.push(name);
|
|
81
|
+
}
|
|
82
|
+
return { env, withheld };
|
|
83
|
+
}
|
|
84
|
+
// ---------------------------------------------------------------- the proxy
|
|
85
|
+
/** Methods that read. Anything else is a write until the host says otherwise. */
|
|
86
|
+
const READ_METHODS = new Set(["GET", "HEAD", "OPTIONS"]);
|
|
87
|
+
/**
|
|
88
|
+
* Hosts an outward write may reach on a test run: model providers (the run
|
|
89
|
+
* cannot happen without them, and nothing there is a send), and read-only
|
|
90
|
+
* services whose POSTs are queries. A host is matched exactly, or by a
|
|
91
|
+
* leading `*.` for its subdomains. The list is deliberately short — the
|
|
92
|
+
* default is refusal, and a host earns its place here by having no way to
|
|
93
|
+
* reach a customer.
|
|
94
|
+
*/
|
|
95
|
+
export const READ_SAFE_HOSTS = [
|
|
96
|
+
// model providers
|
|
97
|
+
"api.anthropic.com",
|
|
98
|
+
"openrouter.ai",
|
|
99
|
+
"api.openai.com",
|
|
100
|
+
"generativelanguage.googleapis.com",
|
|
101
|
+
"api.groq.com",
|
|
102
|
+
"api.mistral.ai",
|
|
103
|
+
"api.together.xyz",
|
|
104
|
+
"api.deepseek.com",
|
|
105
|
+
"api.x.ai",
|
|
106
|
+
"api.fireworks.ai",
|
|
107
|
+
"api.cerebras.ai",
|
|
108
|
+
"api.perplexity.ai",
|
|
109
|
+
"*.openai.azure.com",
|
|
110
|
+
// queries that are POSTs
|
|
111
|
+
"api.dataforseo.com",
|
|
112
|
+
"*.dataforseo.com",
|
|
113
|
+
"*.svc.cluster.local",
|
|
114
|
+
];
|
|
115
|
+
export function isReadSafeHost(host) {
|
|
116
|
+
const h = host.toLowerCase().replace(/:\d+$/, "");
|
|
117
|
+
return READ_SAFE_HOSTS.some((rule) => rule.startsWith("*.") ? h.endsWith(rule.slice(1)) && h !== rule.slice(2) : h === rule);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* What the proxy does with one request on a test lease. Pure: method, host
|
|
121
|
+
* and path in, a verdict out. Reads always pass — a test run has to be able
|
|
122
|
+
* to look at the world it is not allowed to change.
|
|
123
|
+
*/
|
|
124
|
+
export function testPolicy(method, host, path) {
|
|
125
|
+
const m = method.toUpperCase();
|
|
126
|
+
if (READ_METHODS.has(m))
|
|
127
|
+
return { action: "allow" };
|
|
128
|
+
const h = host.toLowerCase().replace(/:\d+$/, "");
|
|
129
|
+
// Resend's sink: the message really goes out, to an address that keeps
|
|
130
|
+
// it, with the subject marked — so the desk sees a real id and a real
|
|
131
|
+
// 200, and nobody receives anything.
|
|
132
|
+
if (h === "api.resend.com" && /^\/emails(\/|$|\?)/.test(path))
|
|
133
|
+
return { action: "rewrite", rewrite: "resend-email" };
|
|
134
|
+
if (isReadSafeHost(h))
|
|
135
|
+
return { action: "allow" };
|
|
136
|
+
return { action: "refuse" };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A Resend `POST /emails` body with its recipients pointed at the sink and
|
|
140
|
+
* the subject marked. Returns the recipients it replaced, for the trace.
|
|
141
|
+
* Null when the body is not a JSON object — then there is nothing safe to
|
|
142
|
+
* rewrite, and the caller refuses instead.
|
|
143
|
+
*/
|
|
144
|
+
export function rewriteResendEmail(body) {
|
|
145
|
+
let parsed;
|
|
146
|
+
try {
|
|
147
|
+
parsed = JSON.parse(body);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
153
|
+
return null;
|
|
154
|
+
const mail = parsed;
|
|
155
|
+
const to = [];
|
|
156
|
+
for (const field of ["to", "cc", "bcc"]) {
|
|
157
|
+
const v = mail[field];
|
|
158
|
+
if (v === undefined || v === null)
|
|
159
|
+
continue;
|
|
160
|
+
for (const addr of Array.isArray(v) ? v : [v])
|
|
161
|
+
to.push(String(addr));
|
|
162
|
+
// One sink address for the lot: two copies of the same test mail teach
|
|
163
|
+
// nobody anything, and cc/bcc to the sink would be exactly that.
|
|
164
|
+
if (field === "to")
|
|
165
|
+
mail[field] = [TEST_SINK_EMAIL];
|
|
166
|
+
else
|
|
167
|
+
delete mail[field];
|
|
168
|
+
}
|
|
169
|
+
if (!Array.isArray(mail.to) || mail.to.length === 0)
|
|
170
|
+
mail.to = [TEST_SINK_EMAIL];
|
|
171
|
+
const subject = typeof mail.subject === "string" ? mail.subject : "";
|
|
172
|
+
mail.subject = subject.startsWith(TEST_SUBJECT_PREFIX) ? subject : `${TEST_SUBJECT_PREFIX}${subject}`;
|
|
173
|
+
return { body: JSON.stringify(mail), to };
|
|
174
|
+
}
|
|
175
|
+
/** The first `max` characters of a body, whitespace collapsed — enough to
|
|
176
|
+
* say what would have gone out, not enough to be the thing itself. The
|
|
177
|
+
* caller redacts; this only cuts. */
|
|
178
|
+
export function bodySummary(body, max = 200) {
|
|
179
|
+
const flat = body.replace(/\s+/g, " ").trim();
|
|
180
|
+
return flat.length > max ? `${flat.slice(0, max)}…` : flat;
|
|
181
|
+
}
|
|
182
|
+
/** The recipient field of a body, if it has one — for `would_have.to`. */
|
|
183
|
+
export function recipientsOf(body) {
|
|
184
|
+
try {
|
|
185
|
+
const parsed = JSON.parse(body);
|
|
186
|
+
if (parsed && typeof parsed === "object") {
|
|
187
|
+
for (const key of ["to", "To", "recipient", "phone", "email"]) {
|
|
188
|
+
const v = parsed[key];
|
|
189
|
+
if (typeof v === "string" || Array.isArray(v))
|
|
190
|
+
return v;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
// a form body: Twilio's Messages endpoint
|
|
196
|
+
const m = body.match(/(?:^|&)To=([^&]*)/);
|
|
197
|
+
if (m)
|
|
198
|
+
return decodeURIComponent(m[1].replace(/\+/g, " "));
|
|
199
|
+
}
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
/** The 403 a refused write is answered with. JSON, so a script that parses
|
|
203
|
+
* the error sees `test_mode` and can say so instead of retrying. */
|
|
204
|
+
export function refusalBody(method, host, path, body) {
|
|
205
|
+
return JSON.stringify({
|
|
206
|
+
test_mode: true,
|
|
207
|
+
error: { type: "test_mode", message: `test run: ${method} ${host}${path} refused — nothing was sent` },
|
|
208
|
+
would_have: { method, host, path, to: recipientsOf(body), body: bodySummary(body) },
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
// ---------------------------------------------------------------- write-back
|
|
212
|
+
/** Is this workspace-relative path one a test run diverts? */
|
|
213
|
+
export function isDivertedPath(rel) {
|
|
214
|
+
const norm = rel.replaceAll("\\", "/");
|
|
215
|
+
return TEST_DIVERTED_DIRS.some((d) => norm === d || norm.startsWith(`${d}/`));
|
|
216
|
+
}
|
|
217
|
+
/** The line the trace and the run page show for one diverted write. */
|
|
218
|
+
export function divertedSummary(rel, was, next) {
|
|
219
|
+
if (was === null)
|
|
220
|
+
return `would have created ${rel} (${next.length} bytes)`;
|
|
221
|
+
const before = was.toString("utf8").split("\n").length;
|
|
222
|
+
const after = next.toString("utf8").split("\n").length;
|
|
223
|
+
const delta = after - before;
|
|
224
|
+
return `would have written ${rel}${delta > 0 ? `, +${delta} line${delta === 1 ? "" : "s"}` : delta < 0 ? `, ${delta} lines` : ""}`;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The in-process path has no copy-in and copy-out — a step writes the
|
|
228
|
+
* workspace directly — so a test run there snapshots state/ and storage/
|
|
229
|
+
* before the step and, after it, moves what changed under the run and
|
|
230
|
+
* puts the originals back. Same outcome as the sandbox's divert, reached
|
|
231
|
+
* from the other side. Used by `foldrun run --test`.
|
|
232
|
+
*/
|
|
233
|
+
export function snapshotDivertedDirs(wsRoot) {
|
|
234
|
+
const snap = new Map();
|
|
235
|
+
for (const dir of TEST_DIVERTED_DIRS) {
|
|
236
|
+
const root = path.join(wsRoot, dir);
|
|
237
|
+
if (!fs.existsSync(root))
|
|
238
|
+
continue;
|
|
239
|
+
for (const entry of fs.readdirSync(root, { recursive: true, withFileTypes: true })) {
|
|
240
|
+
if (!entry.isFile())
|
|
241
|
+
continue;
|
|
242
|
+
const abs = path.join(entry.parentPath, entry.name);
|
|
243
|
+
snap.set(path.relative(wsRoot, abs).replaceAll("\\", "/"), fs.readFileSync(abs));
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return snap;
|
|
247
|
+
}
|
|
248
|
+
/** Undo a step's writes to the diverted directories, keeping each changed
|
|
249
|
+
* or new file under `to` by its workspace-relative path. Deletions are
|
|
250
|
+
* undone too — the original is put back. Returns what was set aside. */
|
|
251
|
+
export function restoreDivertedDirs(wsRoot, before, to, note) {
|
|
252
|
+
const now = snapshotDivertedDirs(wsRoot);
|
|
253
|
+
const aside = [];
|
|
254
|
+
for (const [rel, next] of now) {
|
|
255
|
+
const was = before.get(rel) ?? null;
|
|
256
|
+
if (was && was.equals(next))
|
|
257
|
+
continue;
|
|
258
|
+
const dest = path.join(to, rel);
|
|
259
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
260
|
+
fs.writeFileSync(dest, next);
|
|
261
|
+
aside.push(rel);
|
|
262
|
+
note(rel, divertedSummary(rel, was, next));
|
|
263
|
+
if (was)
|
|
264
|
+
fs.writeFileSync(path.join(wsRoot, rel), was);
|
|
265
|
+
else
|
|
266
|
+
fs.rmSync(path.join(wsRoot, rel), { force: true });
|
|
267
|
+
}
|
|
268
|
+
for (const [rel, was] of before) {
|
|
269
|
+
if (now.has(rel))
|
|
270
|
+
continue;
|
|
271
|
+
fs.mkdirSync(path.dirname(path.join(wsRoot, rel)), { recursive: true });
|
|
272
|
+
fs.writeFileSync(path.join(wsRoot, rel), was);
|
|
273
|
+
note(rel, `would have deleted ${rel}`);
|
|
274
|
+
aside.push(rel);
|
|
275
|
+
}
|
|
276
|
+
return aside;
|
|
277
|
+
}
|
|
278
|
+
/** `[test] ` in front of a headline, once. */
|
|
279
|
+
export function testHeadline(summary) {
|
|
280
|
+
return summary.startsWith("[test]") ? summary : `[test] ${summary}`;
|
|
281
|
+
}
|
|
282
|
+
/** The env every test-run sandbox gets, beside its secrets. */
|
|
283
|
+
export function testModeEnv() {
|
|
284
|
+
return { [TEST_MODE_ENV]: "1", [RUN_TEST_ENV]: "1" };
|
|
285
|
+
}
|
package/dist/src/tool-test.js
CHANGED
|
@@ -23,6 +23,7 @@ import { libraryDir } from "./library.js";
|
|
|
23
23
|
import { secretsUsedByApi } from "./api-tools.js";
|
|
24
24
|
import { commandFor, resolveRunPath } from "./script-tools.js";
|
|
25
25
|
import { parseRuntime, prepareRuntime } from "./runtime.js";
|
|
26
|
+
import { hostSafeEnv } from "./host-env.js";
|
|
26
27
|
const MAX_DETAIL = 4000;
|
|
27
28
|
const TIMEOUT_MS = 15_000;
|
|
28
29
|
// A tool test runs tenant-authored code directly on the host — real runs are
|
|
@@ -30,25 +31,11 @@ const TIMEOUT_MS = 15_000;
|
|
|
30
31
|
// process.env. That env holds FOLDRUN_SECRET_KEY (the global install key that
|
|
31
32
|
// signs every tenant's webhook tokens and git-push secrets), plus the S3/DB/
|
|
32
33
|
// Stripe/GitHub credentials: handing them to a `console.log(process.env)`
|
|
33
|
-
// script tool is cross-tenant compromise. The child instead starts from
|
|
34
|
-
//
|
|
35
|
-
// and
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"LC_ALL", "LC_CTYPE", "TZ", "TERM", "SHELL", "USER", "LOGNAME",
|
|
39
|
-
"NODE_ENV", // not a secret; some interpreters/tools read it.
|
|
40
|
-
// Windows equivalents, harmless elsewhere.
|
|
41
|
-
"SYSTEMROOT", "COMSPEC", "PATHEXT", "WINDIR",
|
|
42
|
-
];
|
|
43
|
-
function hostSafeBaseEnv() {
|
|
44
|
-
const base = {};
|
|
45
|
-
for (const k of HOST_BASE_ENV_KEYS) {
|
|
46
|
-
const v = process.env[k];
|
|
47
|
-
if (v !== undefined)
|
|
48
|
-
base[k] = v;
|
|
49
|
-
}
|
|
50
|
-
return base;
|
|
51
|
-
}
|
|
34
|
+
// script tool is cross-tenant compromise. The child instead starts from the
|
|
35
|
+
// allowlisted host base (host-env.ts — the same one every in-process script,
|
|
36
|
+
// verify shell and SDK Bash tool starts from) and gets the tool's own
|
|
37
|
+
// declared secrets layered on top by the callers.
|
|
38
|
+
const hostSafeBaseEnv = () => hostSafeEnv();
|
|
52
39
|
const clip = (s) => s.length > MAX_DETAIL ? `${s.slice(0, MAX_DETAIL)}\n… truncated` : s;
|
|
53
40
|
/** `${NAME}` → value, for headers and query. Same rule the runtime uses. */
|
|
54
41
|
const substitute = (v, env) => v.replace(/\$\{([A-Z0-9_]+)\}/g, (_, k) => env[k] ?? "");
|
|
@@ -108,7 +95,7 @@ export async function testTool(tenant, workspace, def, input = {}) {
|
|
|
108
95
|
}
|
|
109
96
|
}
|
|
110
97
|
if (def.kind === "script") {
|
|
111
|
-
|
|
98
|
+
let run = String(def.spec.run ?? "");
|
|
112
99
|
// A run's working directory is the calling AGENT's folder, not the
|
|
113
100
|
// workspace root — and scripts rely on that. `path.resolve(cwd, "..",
|
|
114
101
|
// "..", "state", f)` is the ordinary way to reach workspace state, and
|
|
@@ -126,7 +113,47 @@ export async function testTool(tenant, workspace, def, input = {}) {
|
|
|
126
113
|
// workspace root is wrong but it exists, and the note says so rather
|
|
127
114
|
// than failing a test over a workspace that cannot run anything yet.
|
|
128
115
|
const cwd = caller ? path.join(dir, "agents", caller.name) : dir;
|
|
129
|
-
|
|
116
|
+
// A single-file tool carries its program in a fenced block rather than
|
|
117
|
+
// in a file: `run:` is empty and `code:` holds it. The RUNTIME writes it
|
|
118
|
+
// out at call time (script-tools.ts) and has always worked; the tester
|
|
119
|
+
// only ever looked for a file, so the Test button failed for every one
|
|
120
|
+
// of them while the same tool ran fine in a flow — which is the worst
|
|
121
|
+
// possible way round, since that button is where a developer goes to
|
|
122
|
+
// find out whether their work is broken.
|
|
123
|
+
//
|
|
124
|
+
// Materialised the same way and in the same place a call would, so the
|
|
125
|
+
// test runs the identical program from the identical directory.
|
|
126
|
+
const inlineCode = typeof def.spec.code === "string" ? def.spec.code : "";
|
|
127
|
+
const inline = !run && !!inlineCode;
|
|
128
|
+
if (inline && !caller) {
|
|
129
|
+
// Nowhere to stand: the file would land at the workspace root, which
|
|
130
|
+
// is not where a run puts it and not somewhere a deploy diff should
|
|
131
|
+
// find it. Say what is missing instead.
|
|
132
|
+
return done({
|
|
133
|
+
ok: false, transport: "script", missingSecrets: [],
|
|
134
|
+
summary: "no agent to test it from",
|
|
135
|
+
detail: "This tool carries its code inline and is materialised inside the calling agent's folder. Grant it to an agent (tools:) and test again.",
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
if (inline) {
|
|
139
|
+
const ext = typeof def.spec.codeExt === "string" ? def.spec.codeExt : ".mjs";
|
|
140
|
+
const codeDir = path.join(cwd, ".tool-code");
|
|
141
|
+
try {
|
|
142
|
+
fs.mkdirSync(codeDir, { recursive: true });
|
|
143
|
+
fs.writeFileSync(path.join(codeDir, `${def.name}${ext}`), inlineCode, { mode: 0o755 });
|
|
144
|
+
run = `.tool-code/${def.name}${ext}`;
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
return done({
|
|
148
|
+
ok: false, transport: "script", missingSecrets: [],
|
|
149
|
+
summary: "its inline code could not be written",
|
|
150
|
+
detail: err instanceof Error ? err.message : String(err),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// A materialised file was just written into cwd, so it resolves from
|
|
155
|
+
// there. A declared `run:` keeps resolving exactly as it always has.
|
|
156
|
+
const file = resolveRunPath(inline ? cwd : path.join(dir, "agents", "_probe"), run, libraryDir(tenant, "scripts"));
|
|
130
157
|
if (!fs.existsSync(file)) {
|
|
131
158
|
return done({
|
|
132
159
|
ok: false, transport: "script", missingSecrets: [],
|
package/dist/src/translator.js
CHANGED
|
@@ -29,6 +29,7 @@ import http from "node:http";
|
|
|
29
29
|
import crypto from "node:crypto";
|
|
30
30
|
import { PROTECTED_PARAMS } from "./providers.js";
|
|
31
31
|
import { viaEgress } from "./egress.js";
|
|
32
|
+
import { trimSlashes } from "./paths.js";
|
|
32
33
|
function dropped() {
|
|
33
34
|
const seen = new Set();
|
|
34
35
|
return {
|
|
@@ -806,7 +807,7 @@ function upstreamMessage(text, status) {
|
|
|
806
807
|
export async function startTranslator(spec) {
|
|
807
808
|
const key = `fr-${crypto.randomBytes(16).toString("hex")}`;
|
|
808
809
|
const log = [];
|
|
809
|
-
const base = spec.upstreamBase
|
|
810
|
+
const base = trimSlashes(spec.upstreamBase);
|
|
810
811
|
const label = spec.label ?? base;
|
|
811
812
|
const server = http.createServer(async (req, res) => {
|
|
812
813
|
const started = Date.now();
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** What became of one trigger firing. `started` is here too: without it the
|
|
2
|
+
* log answers "why did nothing run" and not "how often does anything",
|
|
3
|
+
* and the ratio is the whole point of the page. */
|
|
4
|
+
export type TriggerOutcome = "started" | "duplicate" | "throttled" | "debounced" | "quarantined" | "overlap-skipped" | "missed" | "failed";
|
|
5
|
+
export interface TriggerEvent {
|
|
6
|
+
t: string;
|
|
7
|
+
flow: string;
|
|
8
|
+
/** The flow's `trigger:` — schedule, webhook, storage, watch, flow, email. */
|
|
9
|
+
trigger: string;
|
|
10
|
+
outcome: TriggerOutcome;
|
|
11
|
+
/** The sentence a person reads. Always says which key decided, so the fix
|
|
12
|
+
* is one search away in their own flow file. */
|
|
13
|
+
detail?: string;
|
|
14
|
+
runId?: string;
|
|
15
|
+
}
|
|
16
|
+
/** Never throws. A diagnostic that can break a trigger is worse than no
|
|
17
|
+
* diagnostic: the whole point is to be reading it on the bad morning. */
|
|
18
|
+
export declare function recordTriggerEvent(tenant: string, workspace: string, event: TriggerEvent): void;
|
|
19
|
+
export declare function readTriggerEvents(tenant: string, workspace: string, limit?: number): TriggerEvent[];
|
|
20
|
+
export interface FlowTriggerSummary {
|
|
21
|
+
flow: string;
|
|
22
|
+
trigger: string;
|
|
23
|
+
fired: number;
|
|
24
|
+
started: number;
|
|
25
|
+
/** Every outcome that was not a start, by reason, biggest first. */
|
|
26
|
+
dropped: {
|
|
27
|
+
outcome: TriggerOutcome;
|
|
28
|
+
count: number;
|
|
29
|
+
detail: string;
|
|
30
|
+
}[];
|
|
31
|
+
lastFiredAt: string | null;
|
|
32
|
+
lastStartedAt: string | null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* One row per flow: how often it fired, how often that became a run, and
|
|
36
|
+
* what happened to the difference.
|
|
37
|
+
*
|
|
38
|
+
* The gap between `fired` and `started` is the number someone is looking
|
|
39
|
+
* for. A flow with forty fires and two runs is either working exactly as
|
|
40
|
+
* configured or badly misconfigured, and the reasons column is what tells
|
|
41
|
+
* those apart without opening the flow file.
|
|
42
|
+
*/
|
|
43
|
+
export declare function summariseTriggers(tenant: string, workspace: string, sinceMs?: number | null): FlowTriggerSummary[];
|