@foldrun/core 0.3.0 → 0.5.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.
Files changed (175) hide show
  1. package/README.md +27 -10
  2. package/dist/index.d.ts +3 -0
  3. package/dist/index.js +3 -0
  4. package/dist/src/account.d.ts +45 -0
  5. package/dist/src/account.js +130 -0
  6. package/dist/src/api-tools.d.ts +8 -0
  7. package/dist/src/api-tools.js +22 -4
  8. package/dist/src/approvals.d.ts +104 -1
  9. package/dist/src/approvals.js +192 -2
  10. package/dist/src/browser-pod.d.ts +129 -0
  11. package/dist/src/browser-pod.js +284 -0
  12. package/dist/src/budget.d.ts +32 -0
  13. package/dist/src/budget.js +177 -0
  14. package/dist/src/catalog.js +3 -1
  15. package/dist/src/clock.d.ts +49 -0
  16. package/dist/src/clock.js +121 -0
  17. package/dist/src/completions.js +17 -8
  18. package/dist/src/confine.d.ts +22 -0
  19. package/dist/src/confine.js +224 -4
  20. package/dist/src/deploy.d.ts +33 -0
  21. package/dist/src/deploy.js +145 -6
  22. package/dist/src/egress.d.ts +26 -2
  23. package/dist/src/egress.js +8 -1
  24. package/dist/src/eol.d.ts +2 -0
  25. package/dist/src/eol.js +7 -0
  26. package/dist/src/evals.d.ts +9 -0
  27. package/dist/src/evals.js +26 -13
  28. package/dist/src/flow-lint.d.ts +12 -0
  29. package/dist/src/flow-lint.js +331 -1
  30. package/dist/src/flow-patterns.d.ts +141 -0
  31. package/dist/src/flow-patterns.js +568 -0
  32. package/dist/src/gitrepo.d.ts +18 -0
  33. package/dist/src/gitrepo.js +49 -0
  34. package/dist/src/host-env.d.ts +8 -0
  35. package/dist/src/host-env.js +48 -0
  36. package/dist/src/json-schema.d.ts +12 -0
  37. package/dist/src/json-schema.js +202 -0
  38. package/dist/src/kinds.js +1 -1
  39. package/dist/src/language.d.ts +17 -0
  40. package/dist/src/language.js +55 -0
  41. package/dist/src/layout.d.ts +69 -0
  42. package/dist/src/layout.js +200 -0
  43. package/dist/src/library.d.ts +9 -0
  44. package/dist/src/library.js +43 -4
  45. package/dist/src/limits.d.ts +84 -0
  46. package/dist/src/limits.js +287 -0
  47. package/dist/src/locale.d.ts +47 -0
  48. package/dist/src/locale.js +177 -0
  49. package/dist/src/mail.d.ts +88 -0
  50. package/dist/src/mail.js +245 -0
  51. package/dist/src/model-probe.d.ts +3 -0
  52. package/dist/src/model-probe.js +1 -0
  53. package/dist/src/notify.d.ts +132 -8
  54. package/dist/src/notify.js +360 -58
  55. package/dist/src/openapi.js +5 -3
  56. package/dist/src/operator.d.ts +201 -0
  57. package/dist/src/operator.js +251 -0
  58. package/dist/src/paths.d.ts +33 -0
  59. package/dist/src/paths.js +87 -0
  60. package/dist/src/platform.d.ts +144 -4
  61. package/dist/src/platform.js +27 -3
  62. package/dist/src/provider-health.d.ts +55 -0
  63. package/dist/src/provider-health.js +233 -0
  64. package/dist/src/providers.d.ts +481 -0
  65. package/dist/src/providers.js +1105 -6
  66. package/dist/src/refusal.d.ts +83 -0
  67. package/dist/src/refusal.js +281 -0
  68. package/dist/src/run-container.d.ts +206 -17
  69. package/dist/src/run-container.js +547 -80
  70. package/dist/src/runner.d.ts +179 -10
  71. package/dist/src/runner.js +1423 -169
  72. package/dist/src/runtime.d.ts +52 -0
  73. package/dist/src/runtime.js +493 -58
  74. package/dist/src/script-tools.d.ts +15 -2
  75. package/dist/src/script-tools.js +44 -4
  76. package/dist/src/secret-files.d.ts +6 -5
  77. package/dist/src/secret-files.js +24 -11
  78. package/dist/src/secret-health.d.ts +83 -0
  79. package/dist/src/secret-health.js +177 -0
  80. package/dist/src/secrets.d.ts +38 -1
  81. package/dist/src/secrets.js +197 -96
  82. package/dist/src/starter.d.ts +9 -0
  83. package/dist/src/starter.js +95 -11
  84. package/dist/src/step-exec.d.ts +106 -1
  85. package/dist/src/step-exec.js +558 -85
  86. package/dist/src/step-removal.d.ts +15 -0
  87. package/dist/src/step-removal.js +49 -0
  88. package/dist/src/storage.d.ts +23 -4
  89. package/dist/src/storage.js +125 -23
  90. package/dist/src/store.d.ts +492 -38
  91. package/dist/src/store.js +882 -69
  92. package/dist/src/subagents.d.ts +75 -0
  93. package/dist/src/subagents.js +149 -0
  94. package/dist/src/tar.js +2 -1
  95. package/dist/src/test-mode.d.ts +111 -0
  96. package/dist/src/test-mode.js +285 -0
  97. package/dist/src/tool-names.d.ts +13 -1
  98. package/dist/src/tool-names.js +38 -11
  99. package/dist/src/tool-programs.d.ts +26 -0
  100. package/dist/src/tool-programs.js +172 -0
  101. package/dist/src/tool-test.js +152 -33
  102. package/dist/src/translator.d.ts +8 -2
  103. package/dist/src/translator.js +71 -24
  104. package/dist/src/trigger-log.d.ts +43 -0
  105. package/dist/src/trigger-log.js +122 -0
  106. package/dist/src/triggers.d.ts +23 -0
  107. package/dist/src/triggers.js +101 -12
  108. package/dist/src/untrusted-fetch.d.ts +11 -0
  109. package/dist/src/untrusted-fetch.js +77 -0
  110. package/dist/src/web-actions.d.ts +91 -0
  111. package/dist/src/web-actions.js +199 -0
  112. package/dist/src/webhook.d.ts +19 -3
  113. package/dist/src/webhook.js +50 -11
  114. package/index.ts +3 -0
  115. package/package.json +5 -5
  116. package/src/account.ts +149 -0
  117. package/src/api-tools.ts +32 -4
  118. package/src/approvals.ts +253 -2
  119. package/src/browser-pod.ts +313 -0
  120. package/src/budget.ts +178 -0
  121. package/src/catalog.ts +3 -1
  122. package/src/clock.ts +149 -0
  123. package/src/completions.ts +17 -8
  124. package/src/confine.ts +224 -4
  125. package/src/deploy.ts +137 -6
  126. package/src/egress.ts +32 -4
  127. package/src/eol.ts +7 -0
  128. package/src/evals.ts +36 -11
  129. package/src/flow-lint.ts +371 -1
  130. package/src/flow-patterns.ts +597 -0
  131. package/src/gitrepo.ts +48 -0
  132. package/src/host-env.ts +51 -0
  133. package/src/json-schema.ts +184 -0
  134. package/src/kinds.ts +1 -1
  135. package/src/language.ts +62 -0
  136. package/src/layout.ts +228 -0
  137. package/src/library.ts +50 -4
  138. package/src/limits.ts +290 -0
  139. package/src/locale.ts +217 -0
  140. package/src/mail.ts +316 -0
  141. package/src/model-probe.ts +4 -0
  142. package/src/notify.ts +391 -44
  143. package/src/openapi.ts +5 -3
  144. package/src/operator.ts +300 -0
  145. package/src/paths.ts +83 -0
  146. package/src/platform.ts +158 -5
  147. package/src/provider-health.ts +275 -0
  148. package/src/providers.ts +1368 -6
  149. package/src/refusal.ts +320 -0
  150. package/src/run-container.ts +611 -76
  151. package/src/runner.ts +1463 -165
  152. package/src/runtime.ts +487 -55
  153. package/src/script-tools.ts +55 -6
  154. package/src/secret-files.ts +22 -10
  155. package/src/secret-health.ts +230 -0
  156. package/src/secrets.ts +191 -95
  157. package/src/starter.ts +96 -11
  158. package/src/step-exec.ts +548 -41
  159. package/src/step-removal.ts +59 -0
  160. package/src/storage.ts +149 -24
  161. package/src/store.ts +1227 -74
  162. package/src/subagents.ts +189 -0
  163. package/src/tar.ts +2 -1
  164. package/src/test-mode.ts +311 -0
  165. package/src/tool-names.ts +40 -11
  166. package/src/tool-programs.ts +179 -0
  167. package/src/tool-test.ts +159 -32
  168. package/src/translator.ts +74 -23
  169. package/src/trigger-log.ts +160 -0
  170. package/src/triggers.ts +102 -12
  171. package/src/untrusted-fetch.ts +88 -0
  172. package/src/web-actions.ts +237 -0
  173. package/src/webhook.ts +58 -11
  174. package/templates/hello/agents/curator/agent.md +1 -1
  175. package/templates/hello/agents/notetaker/agent.md +1 -1
@@ -0,0 +1,237 @@
1
+ // The web, as actions. One question answered in one place: which provider
2
+ // can do which action, and what does foldrun do when none is named?
3
+ //
4
+ // An action is a call with a fixed input and a fixed output — search takes a
5
+ // query and returns URLs, fetch takes a URL and returns the page — whoever
6
+ // answers it. A provider is the list of actions it has an adapter for. foldrun
7
+ // is a provider like any other, and the one used when `web: {<action>: …}` is
8
+ // unset. A provider named for an action it cannot do is an error in `check`,
9
+ // never a quiet fall back to ours: a fallback would bill and behave
10
+ // differently from what the file says.
11
+ //
12
+ // Browse has a second level: inside a page, the steps and modes of
13
+ // the browse action. A browser reached over CDP is driven by our own code, so it
14
+ // takes every step and mode unless it is listed as lacking one; a vendor
15
+ // that runs the steps itself would list the few it has.
16
+ //
17
+ // The vendor facts (endpoint, key, docs page, the date the adapter was last
18
+ // matched to those docs) stay on SEARCH_APIS, FETCH_APIS and BROWSER_APIS in
19
+ // providers.ts; this file reads them rather than restating them.
20
+
21
+ import { SEARCH_APIS, FETCH_APIS, BROWSER_APIS, ACTION_APIS, WEB_ACTIONS, webConfig, type WebAction } from "./providers.ts";
22
+
23
+ export { WEB_ACTIONS, type WebAction };
24
+
25
+ /** What the browse action can return — its `mode`. Kept equal to the tool's own
26
+ * MODES by the platform's gallery test. */
27
+ export const BROWSE_MODES = [
28
+ "text", "markdown", "html", "links", "meta", "table", "network", "screenshot", "pdf", "capture",
29
+ "aria", "map", "crawl", "console", "vitals", "a11y", "diff", "feed", "webmcp", "react",
30
+ ] as const;
31
+
32
+ /** What the browse action can do in a page — its `actions` steps. Kept equal to
33
+ * the tool's own ACTIONS by the platform's gallery test. */
34
+ export const BROWSE_STEPS = [
35
+ "click", "dblclick", "rightclick", "hover", "check", "uncheck", "drag", "fill", "type", "press", "select",
36
+ "wait", "goto", "screenshot", "pdf", "scroll", "paste", "upload", "download", "extract", "frame", "tab",
37
+ "back", "forward", "reload", "focus", "clear", "highlight", "keyboard", "keydown", "keyup", "mouse", "wheel",
38
+ "tap", "swipe", "clipboard", "eval", "get", "expect", "if", "mock", "unmock", "offline", "cookie",
39
+ "localstorage", "sessionstorage", "viewport", "dialog", "solve", "pushstate", "insert", "webmcp", "login",
40
+ ] as const;
41
+
42
+ export interface ActionSupport {
43
+ /** live: callable today. planned: agreed and not built — naming it is an
44
+ * error that says so, rather than a tool that does nothing. */
45
+ status: "live" | "planned";
46
+ /** How: which tool serves it, and for foldrun's own, with what. */
47
+ how: string;
48
+ /** Browse only. cdp: our code drives the browser, so every step and mode
49
+ * works unless listed in `lacks`. api: the vendor runs the steps, and
50
+ * `steps`/`modes` list what it has. */
51
+ via?: "cdp" | "api";
52
+ steps?: readonly string[];
53
+ modes?: readonly string[];
54
+ lacks?: readonly string[];
55
+ /** The vendor page the adapter was built from, and the day it was last
56
+ * matched to it (YYYY-MM-DD). Absent on foldrun's own. */
57
+ docs?: string;
58
+ checked?: string;
59
+ }
60
+
61
+ export interface WebProvider {
62
+ name: string;
63
+ title: string;
64
+ actions: Partial<Record<WebAction, ActionSupport>>;
65
+ }
66
+
67
+ /** Ours: every action, and none of them with a second model in it. */
68
+ export const FOLDRUN: WebProvider = {
69
+ name: "foldrun",
70
+ title: "foldrun",
71
+ actions: {
72
+ search: { status: "live", how: "the account's own SearXNG" },
73
+ fetch: { status: "live", how: "one plain HTTP request in the sandbox" },
74
+ browse: { status: "live", how: "the account's browser pod", via: "cdp" },
75
+ crawl: { status: "live", how: "plain HTTP, same site, robots.txt honoured" },
76
+ map: { status: "live", how: "the site's sitemaps, else its links" },
77
+ extract: { status: "live", how: "selectors, or the page's own JSON-LD and OpenGraph — no model" },
78
+ answer: { status: "live", how: "search, read the top pages, rank the passages that answer — no model" },
79
+ monitor: { status: "live", how: "a page's lines or a query's results, against the last call, in state/" },
80
+ },
81
+ };
82
+
83
+ type Facts = { name: string; title: string; aliases?: string[]; docs?: string; checked?: string; lacks?: readonly string[] };
84
+
85
+ /** Every provider and what it can do, foldrun first. Built from the vendor
86
+ * lists so a vendor added there is here without a second edit. */
87
+ export function webProviders(): WebProvider[] {
88
+ const byName = new Map<string, WebProvider>();
89
+ const add = (f: Facts, action: WebAction, support: ActionSupport) => {
90
+ const p = byName.get(f.name) ?? { name: f.name, title: f.title, actions: {} };
91
+ p.actions[action] = { ...support, ...(f.docs ? { docs: f.docs } : {}), ...(f.checked ? { checked: f.checked } : {}) };
92
+ byName.set(f.name, p);
93
+ };
94
+ for (const a of SEARCH_APIS as readonly Facts[]) add(a, "search", { status: "live", how: a.title });
95
+ for (const a of FETCH_APIS as readonly Facts[]) add(a, "fetch", { status: "live", how: a.title });
96
+ for (const a of BROWSER_APIS as readonly Facts[]) {
97
+ add(a, "browse", { status: "live", how: a.title, via: "cdp", ...(a.lacks?.length ? { lacks: a.lacks } : {}) });
98
+ }
99
+ for (const [action, apis] of Object.entries(ACTION_APIS) as [WebAction, readonly Facts[]][]) {
100
+ for (const a of apis) add(a, action, { status: "live", how: a.title });
101
+ }
102
+ return [FOLDRUN, ...byName.values()];
103
+ }
104
+
105
+ /** The names a provider answers to, for lookups. */
106
+ function aliasesOf(name: string): string[] {
107
+ const all = [...SEARCH_APIS, ...FETCH_APIS, ...BROWSER_APIS, ...Object.values(ACTION_APIS).flat()] as readonly Facts[];
108
+ return all.filter((a) => a.name === name).flatMap((a) => a.aliases ?? []);
109
+ }
110
+
111
+ export function findWebProvider(name: string): WebProvider | undefined {
112
+ const key = name.trim().toLowerCase();
113
+ if (key === "" || key === "ours") return FOLDRUN;
114
+ return webProviders().find((p) => p.name === key || aliasesOf(p.name).includes(key));
115
+ }
116
+
117
+ /** The live providers for an action, foldrun first — for "these do" lines. */
118
+ export function providersFor(action: WebAction): string[] {
119
+ return webProviders().filter((p) => p.actions[action]?.status === "live").map((p) => p.name);
120
+ }
121
+
122
+ export type ActionChoice =
123
+ | { provider: WebProvider; support: ActionSupport; error?: undefined }
124
+ | { provider?: undefined; support?: undefined; error: string };
125
+
126
+ /**
127
+ * Who does `action`. Unset is foldrun. A named provider must have the action
128
+ * live; anything else is an error naming the providers that do — the rule is
129
+ * "fix the file", never "we used ours instead".
130
+ */
131
+ export function resolveWebAction(action: WebAction, name?: string | null): ActionChoice {
132
+ const field = `web.${action}`;
133
+ const p = name == null ? FOLDRUN : findWebProvider(name);
134
+ if (!p) {
135
+ return { error: `${field}: ${name} — no provider by that name. These ${action}: ${providersFor(action).join(", ")}.` };
136
+ }
137
+ const s = p.actions[action];
138
+ if (!s) {
139
+ const live = providersFor(action);
140
+ return {
141
+ error: `${field}: ${p.name} does not ${action} here.` +
142
+ (live.length ? ` These do: ${live.join(", ")}.` : " No provider has it wired yet.") +
143
+ (FOLDRUN.actions[action]?.status === "live" ? " Unset uses foldrun." : ""),
144
+ };
145
+ }
146
+ if (s.status === "planned") {
147
+ const others = providersFor(action).filter((n) => n !== p.name);
148
+ return {
149
+ error: `${field}: ${p.title}'s own ${action} is not built yet` +
150
+ (others.length ? ` — name a provider that has it: ${others.join(", ")}.` : " and no provider has it wired either."),
151
+ };
152
+ }
153
+ return { provider: p, support: s };
154
+ }
155
+
156
+ /** Can this browser run this step, or return this mode? */
157
+ export function browseSupports(support: ActionSupport, stepOrMode: string): boolean {
158
+ if (support.via === "cdp") return !(support.lacks ?? []).includes(stepOrMode);
159
+ return (support.steps ?? []).includes(stepOrMode) || (support.modes ?? []).includes(stepOrMode);
160
+ }
161
+
162
+ /** The browsers that can run a step or mode — for the error when one cannot. */
163
+ export function browsersFor(stepOrMode: string): string[] {
164
+ return webProviders()
165
+ .filter((p) => p.actions.browse?.status === "live" && browseSupports(p.actions.browse, stepOrMode))
166
+ .map((p) => p.name);
167
+ }
168
+
169
+ /** Vendor actions whose adapter has not been matched to the vendor's docs
170
+ * within `days` — each as "provider action (checked …)". Empty is healthy.
171
+ * The test that calls this is the rule "integrations follow the latest
172
+ * docs" in a form CI can fail on. */
173
+ export function staleIntegrations(today: string, days = 90): string[] {
174
+ const out: string[] = [];
175
+ const limit = Date.parse(today) - days * 86_400_000;
176
+ for (const p of webProviders()) {
177
+ if (p === FOLDRUN) continue;
178
+ for (const [action, s] of Object.entries(p.actions)) {
179
+ if (!s || s.status !== "live") continue;
180
+ if (!s.docs || !s.checked) out.push(`${p.name} ${action} (no docs page or check date recorded)`);
181
+ else if (Date.parse(s.checked) < limit) out.push(`${p.name} ${action} (checked ${s.checked})`);
182
+ }
183
+ }
184
+ return out;
185
+ }
186
+
187
+ /** The actions after search, fetch and browse. Those three are resolved by
188
+ * providers.ts, which also knows the model providers that search. */
189
+ export const NEWER_ACTIONS = ["crawl", "map", "extract", "answer", "monitor"] as const;
190
+ type NewerAction = (typeof NEWER_ACTIONS)[number];
191
+
192
+ export type ActionApiChoice =
193
+ | { provider: null; error?: undefined }
194
+ | { provider: string; secret: string; host: string; error?: undefined }
195
+ | { provider: null; error: string };
196
+
197
+ /**
198
+ * Who does one of the newer actions, and with which key: unset (or
199
+ * "foldrun") is ours and needs none; a name, or `{name, key: ${VAULT_NAME}}`
200
+ * for a key stored under another name, is that provider — if it has the
201
+ * action. The runner turns a provider into FOLDRUN_WEB_<ACTION>_VIA, the
202
+ * secret the step holds, and the one host that secret may reach.
203
+ */
204
+ export function resolveActionApi(action: NewerAction, raw: unknown): ActionApiChoice {
205
+ const field = `web.${action}`;
206
+ if (raw === undefined || raw === null || raw === "") return { provider: null };
207
+ let name: string;
208
+ let secret: string | undefined;
209
+ if (typeof raw === "string") name = raw;
210
+ else if (typeof raw === "object" && !Array.isArray(raw) && typeof (raw as { name?: unknown }).name === "string") {
211
+ name = (raw as { name: string }).name;
212
+ const key = (raw as { key?: unknown }).key;
213
+ if (key !== undefined) {
214
+ const m = typeof key === "string" ? /^\$\{([A-Z][A-Z0-9_]*)\}$/.exec(key.trim()) : null;
215
+ if (!m) return { provider: null, error: `${field}.key must be a \${NAME} reference to a secret in the vault, not the key itself.` };
216
+ secret = m[1];
217
+ }
218
+ } else {
219
+ return { provider: null, error: `${field}: takes a provider name, or a block with name: (and key: for your own vault name).` };
220
+ }
221
+ const choice = resolveWebAction(action, name);
222
+ if (choice.error !== undefined) return { provider: null, error: choice.error };
223
+ if (choice.provider.name === "foldrun") return { provider: null };
224
+ const api = ACTION_APIS[action].find((a) => a.name === choice.provider.name)!;
225
+ return { provider: api.name, secret: secret ?? api.secret, host: api.host };
226
+ }
227
+
228
+ /** Every newer action in the agent's web: block that names a provider unable
229
+ * to do it — the sentences resolveActionApi gives. Empty when all are unset
230
+ * or answerable. */
231
+ export function actionProblems(front: Record<string, unknown>): string[] {
232
+ const { raw } = webConfig(front);
233
+ return NEWER_ACTIONS.flatMap((a) => {
234
+ const e = resolveActionApi(a, raw[a]).error;
235
+ return e ? [e] : [];
236
+ });
237
+ }
package/src/webhook.ts CHANGED
@@ -4,22 +4,23 @@
4
4
 
5
5
  import fs from "node:fs";
6
6
  import path from "node:path";
7
- import { dataRoot } from "./paths.ts";
8
7
  import { workspaceDir } from "./store.ts";
8
+ import { installKeyMaterial } from "./secrets.ts";
9
9
  import crypto from "node:crypto";
10
10
 
11
- const keyFile = () => path.join(dataRoot(), ".secret-key");
12
-
13
11
  /**
14
- * The install's secret key. Every derived token — flow hooks, git secrets —
15
- * hangs off this one value, so rotating it invalidates all of them together.
12
+ * The install's secret key. Every derived token — flow hooks, git secrets,
13
+ * approval links — hangs off this one value, so rotating it invalidates all
14
+ * of them together.
15
+ *
16
+ * The same material the vault encrypts with, created on first use. This used
17
+ * to fall back to the constant "foldrun-dev-install" until the vault lazily
18
+ * wrote `.secret-key`, which meant two things nobody wanted: every hook and
19
+ * approval token on a fresh install was computable from the source, and all
20
+ * of them silently changed the first time a secret was stored.
16
21
  */
17
22
  export function installKey(): string {
18
- if (process.env.FOLDRUN_SECRET_KEY) return process.env.FOLDRUN_SECRET_KEY;
19
- if (fs.existsSync(keyFile())) return fs.readFileSync(keyFile(), "utf8");
20
- // secrets.ts creates this on first use; fall back to a fixed dev value so
21
- // hook URLs stay stable before any secret has been set.
22
- return "foldrun-dev-install";
23
+ return installKeyMaterial();
23
24
  }
24
25
 
25
26
  // Per-hook rotation without storing tokens: what is stored is a generation
@@ -133,12 +134,26 @@ export function publicUrl(): string | null {
133
134
  export interface HookDelivery {
134
135
  t: string;
135
136
  flow: string;
136
- outcome: "accepted" | "invalid-token" | "invalid-signature" | "not-webhook" | "no-flow" | "error";
137
+ /** `dropped` is a delivery that arrived, was authentic, and did not
138
+ * become a run because the flow's own gates said one was enough —
139
+ * a duplicate, a throttled burst, a quarantined flow. Distinct from
140
+ * `accepted` so "why did nothing run" has an answer in the log, and
141
+ * distinct from the refusals so nobody hunts for a bad token. */
142
+ outcome: "accepted" | "invalid-token" | "invalid-signature" | "not-webhook" | "no-flow" | "error" | "dropped";
137
143
  runId?: string;
138
144
  bytes?: number;
139
145
  detail?: string;
146
+ /** A run of refusals, collapsed: how many, and when the first came.
147
+ * `t` is the latest. Absent means one. */
148
+ count?: number;
149
+ first?: string;
140
150
  }
141
151
 
152
+ /** What anyone can produce without the token or the signing secret. A run
153
+ * of these collapses into one line, so a stranger's flood costs the log a
154
+ * single entry and can never push an owner's real deliveries out of it. */
155
+ const UNAUTHENTICATED: HookDelivery["outcome"][] = ["invalid-token", "invalid-signature"];
156
+
142
157
  function deliveriesFile(tenant: string, workspace: string) {
143
158
  return path.join(workspaceDir(tenant, workspace), "hook-deliveries.jsonl");
144
159
  }
@@ -148,6 +163,7 @@ const KEEP_DELIVERIES = 500;
148
163
  export function recordDelivery(tenant: string, workspace: string, delivery: HookDelivery) {
149
164
  const file = deliveriesFile(tenant, workspace);
150
165
  if (!fs.existsSync(path.dirname(file))) return; // no workspace, nothing to log against
166
+ if (UNAUTHENTICATED.includes(delivery.outcome) && collapseInto(file, delivery)) return;
151
167
  fs.appendFileSync(file, JSON.stringify(delivery) + "\n");
152
168
  // Bounded: compact to the newest KEEP when it doubles. Amortised cheap,
153
169
  // and a log that grows forever is a disk-full incident with a delay.
@@ -163,6 +179,37 @@ export function recordDelivery(tenant: string, workspace: string, delivery: Hook
163
179
  }
164
180
  }
165
181
 
182
+ /** Fold a refusal into the log's last line when that line is a refusal
183
+ * too. The flow name is not part of the match: it comes from the URL, so
184
+ * a flood that varies it would otherwise never collapse. True when folded. */
185
+ function collapseInto(file: string, delivery: HookDelivery): boolean {
186
+ let text: string;
187
+ try {
188
+ text = fs.readFileSync(file, "utf8");
189
+ } catch {
190
+ return false;
191
+ }
192
+ const end = text.trimEnd();
193
+ const cut = end.lastIndexOf("\n") + 1;
194
+ let last: HookDelivery;
195
+ try {
196
+ last = JSON.parse(end.slice(cut)) as HookDelivery;
197
+ } catch {
198
+ return false;
199
+ }
200
+ if (!UNAUTHENTICATED.includes(last.outcome)) return false;
201
+ const merged: HookDelivery = {
202
+ ...delivery,
203
+ count: (last.count ?? 1) + 1,
204
+ first: last.first ?? last.t,
205
+ ...(last.detail === "more than one flow name" || last.flow !== delivery.flow ? { detail: "more than one flow name" } : {}),
206
+ };
207
+ const tmp = `${file}.tmp`;
208
+ fs.writeFileSync(tmp, end.slice(0, cut) + JSON.stringify(merged) + "\n");
209
+ fs.renameSync(tmp, file);
210
+ return true;
211
+ }
212
+
166
213
  export function readDeliveries(tenant: string, workspace: string, limit = 20): HookDelivery[] {
167
214
  const file = deliveriesFile(tenant, workspace);
168
215
  if (!fs.existsSync(file)) return [];
@@ -24,7 +24,7 @@ Do exactly this:
24
24
  - A fact a later run or a knowledge file contradicts: correct it, and say
25
25
  in the file what changed and which run showed it.
26
26
  - A note that was only ever true for one day (a count, a cursor, "today"):
27
- move it to `../../state/` if it is still useful, else delete it.
27
+ move it to `workspace/state/` if it is still useful, else delete it.
28
28
  - Anything you are not sure about: leave it, and list it in your report.
29
29
  3. Never write a fact you did not find in a file or a run. Never touch
30
30
  `knowledge/` — that is what people gave the agents, and it is theirs.
@@ -18,7 +18,7 @@ Do exactly this, in order:
18
18
  again. Save the final text to `outputs/note.md`.
19
19
  3. Write one thing you learned to `../../memory/` as a new file — one fact,
20
20
  plain markdown, a sentence or two. The platform records who wrote it.
21
- 4. Update `../../state/publishing.md`: increase the note count by one and set
21
+ 4. Update `workspace/state/publishing.md`: increase the note count by one and set
22
22
  the last topic to what you wrote about. Keep it as markdown.
23
23
 
24
24
  Then stop. Do not create anything else.