@foldrun/core 19.0.0 → 19.1.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.
@@ -14,3 +14,10 @@ export declare function customerKey(tenant: string, customer: string): string;
14
14
  * Returns the body unchanged when it is not JSON or not one of those.
15
15
  */
16
16
  export declare function withProviderUser(body: Buffer, userId: string, shape: "anthropic" | "responses" | "chat"): Buffer;
17
+ /**
18
+ * How many runs of this end customer are open — queued, running or parked
19
+ * at a gate — across every workspace of the account. Read from the run
20
+ * records themselves, so a wiped cache never hands out more room than the
21
+ * customer has. What the platform's open-runs limit counts under its lock.
22
+ */
23
+ export declare function openRunsOfCustomer(tenant: string, customer: string): number;
@@ -14,6 +14,8 @@
14
14
  // partners' `user-1` are never one identity there, and the partner's own id
15
15
  // never leaves foldrun.
16
16
  import crypto from "node:crypto";
17
+ import { listRunSummaries, listWorkspaces } from "./store.js";
18
+ import { records } from "./records.js";
17
19
  /** A customer id as a partner writes it: printable, no spaces, 1–128. */
18
20
  export const CUSTOMER_ID = /^[A-Za-z0-9._:@-]{1,128}$/;
19
21
  /** Null when the id is fine; otherwise why not. */
@@ -61,3 +63,27 @@ export function withProviderUser(body, userId, shape) {
61
63
  }
62
64
  return Buffer.from(JSON.stringify(parsed), "utf8");
63
65
  }
66
+ const OPEN = new Set(["queued", "running", "awaiting-approval"]);
67
+ /**
68
+ * How many runs of this end customer are open — queued, running or parked
69
+ * at a gate — across every workspace of the account. Read from the run
70
+ * records themselves, so a wiped cache never hands out more room than the
71
+ * customer has. What the platform's open-runs limit counts under its lock.
72
+ */
73
+ export function openRunsOfCustomer(tenant, customer) {
74
+ const workspaces = listWorkspaces(tenant).map((w) => w.name);
75
+ const port = records();
76
+ // The database's count covers every workspace whose records live there;
77
+ // a workspace still on files (its import pending — records() answers
78
+ // null for it) is counted from its own summaries and added (Codex review).
79
+ const counted = port?.openRunsOfCustomer ? port.openRunsOfCustomer(tenant, customer) : null;
80
+ let n = counted ?? 0;
81
+ for (const ws of workspaces) {
82
+ if (counted !== null && records(tenant, ws))
83
+ continue;
84
+ for (const s of listRunSummaries(tenant, ws))
85
+ if (s.customer === customer && OPEN.has(s.status))
86
+ n++;
87
+ }
88
+ return n;
89
+ }
@@ -44,6 +44,10 @@ export interface RecordsPort {
44
44
  * whole, newest first, at most `limit`. Optional: without it
45
45
  * conversation.ts filters the run summaries itself. */
46
46
  runsInContext?(tenant: string, workspace: string, key: string, before: string, test: boolean, limit: number): RunRecord[];
47
+ /** FOL-168: how many runs of this end customer are open (queued, running
48
+ * or parked at a gate) across every workspace of the account. Optional:
49
+ * without it customer.ts counts the run summaries itself. */
50
+ openRunsOfCustomer?(tenant: string, customer: string): number;
47
51
  /** When the row last changed, in epoch MICROseconds, or null when there is
48
52
  * none — what a poller compares instead of a file's mtime (two writes in one
49
53
  * millisecond still differ), and what runUpdatedMs divides by 1000 for the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foldrun/core",
3
- "version": "19.0.0",
3
+ "version": "19.1.0",
4
4
  "description": "Agents are just folders. The format, the runner, the checks.",
5
5
  "license": "FSL-1.1-ALv2",
6
6
  "type": "module",
package/src/customer.ts CHANGED
@@ -15,6 +15,8 @@
15
15
  // never leaves foldrun.
16
16
 
17
17
  import crypto from "node:crypto";
18
+ import { listRunSummaries, listWorkspaces } from "./store.ts";
19
+ import { records } from "./records.ts";
18
20
 
19
21
  /** A customer id as a partner writes it: printable, no spaces, 1–128. */
20
22
  export const CUSTOMER_ID = /^[A-Za-z0-9._:@-]{1,128}$/;
@@ -61,3 +63,26 @@ export function withProviderUser(body: Buffer, userId: string, shape: "anthropic
61
63
  }
62
64
  return Buffer.from(JSON.stringify(parsed), "utf8");
63
65
  }
66
+
67
+ const OPEN = new Set(["queued", "running", "awaiting-approval"]);
68
+
69
+ /**
70
+ * How many runs of this end customer are open — queued, running or parked
71
+ * at a gate — across every workspace of the account. Read from the run
72
+ * records themselves, so a wiped cache never hands out more room than the
73
+ * customer has. What the platform's open-runs limit counts under its lock.
74
+ */
75
+ export function openRunsOfCustomer(tenant: string, customer: string): number {
76
+ const workspaces = listWorkspaces(tenant).map((w) => w.name);
77
+ const port = records();
78
+ // The database's count covers every workspace whose records live there;
79
+ // a workspace still on files (its import pending — records() answers
80
+ // null for it) is counted from its own summaries and added (Codex review).
81
+ const counted = port?.openRunsOfCustomer ? port.openRunsOfCustomer(tenant, customer) : null;
82
+ let n = counted ?? 0;
83
+ for (const ws of workspaces) {
84
+ if (counted !== null && records(tenant, ws)) continue;
85
+ for (const s of listRunSummaries(tenant, ws)) if (s.customer === customer && OPEN.has(s.status)) n++;
86
+ }
87
+ return n;
88
+ }
package/src/records.ts CHANGED
@@ -64,6 +64,10 @@ export interface RecordsPort {
64
64
  * whole, newest first, at most `limit`. Optional: without it
65
65
  * conversation.ts filters the run summaries itself. */
66
66
  runsInContext?(tenant: string, workspace: string, key: string, before: string, test: boolean, limit: number): RunRecord[];
67
+ /** FOL-168: how many runs of this end customer are open (queued, running
68
+ * or parked at a gate) across every workspace of the account. Optional:
69
+ * without it customer.ts counts the run summaries itself. */
70
+ openRunsOfCustomer?(tenant: string, customer: string): number;
67
71
  /** When the row last changed, in epoch MICROseconds, or null when there is
68
72
  * none — what a poller compares instead of a file's mtime (two writes in one
69
73
  * millisecond still differ), and what runUpdatedMs divides by 1000 for the