@foldrun/core 17.3.0 → 17.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/dist/src/approvals.js +3 -3
- package/dist/src/blob-cache.d.ts +58 -0
- package/dist/src/blob-cache.js +309 -0
- package/dist/src/blob-pins.d.ts +44 -0
- package/dist/src/blob-pins.js +61 -0
- package/dist/src/blob-store.d.ts +47 -0
- package/dist/src/blob-store.js +357 -0
- package/dist/src/blob-sweeper.d.ts +70 -0
- package/dist/src/blob-sweeper.js +254 -0
- package/dist/src/blob-types.d.ts +145 -0
- package/dist/src/blob-types.js +115 -0
- package/dist/src/evals.js +1 -1
- package/dist/src/history.d.ts +86 -9
- package/dist/src/history.js +132 -16
- package/dist/src/journal-store.d.ts +58 -0
- package/dist/src/journal-store.js +117 -0
- package/dist/src/manifest.d.ts +92 -0
- package/dist/src/manifest.js +239 -0
- package/dist/src/ops-log.js +26 -3
- package/dist/src/platform.d.ts +7 -0
- package/dist/src/platform.js +1 -0
- package/dist/src/publish.d.ts +134 -0
- package/dist/src/publish.js +434 -0
- package/dist/src/records.d.ts +72 -0
- package/dist/src/records.js +24 -0
- package/dist/src/revision-store.d.ts +161 -0
- package/dist/src/revision-store.js +378 -0
- package/dist/src/run-container.d.ts +20 -0
- package/dist/src/run-container.js +61 -0
- package/dist/src/runner.js +1 -1
- package/dist/src/s3-blob-store.d.ts +29 -0
- package/dist/src/s3-blob-store.js +458 -0
- package/dist/src/state-retention.d.ts +15 -0
- package/dist/src/state-retention.js +65 -0
- package/dist/src/storage.d.ts +29 -1
- package/dist/src/storage.js +331 -81
- package/dist/src/store.d.ts +41 -0
- package/dist/src/store.js +188 -5
- package/dist/src/trigger-log.js +17 -0
- package/dist/src/workspace-revisions.d.ts +259 -0
- package/dist/src/workspace-revisions.js +1274 -0
- package/package.json +1 -1
- package/src/approvals.ts +3 -3
- package/src/blob-cache.ts +354 -0
- package/src/blob-pins.ts +99 -0
- package/src/blob-store.ts +385 -0
- package/src/blob-sweeper.ts +308 -0
- package/src/blob-types.ts +216 -0
- package/src/evals.ts +1 -1
- package/src/history.ts +182 -16
- package/src/journal-store.ts +152 -0
- package/src/manifest.ts +274 -0
- package/src/ops-log.ts +25 -3
- package/src/platform.ts +8 -0
- package/src/publish.ts +520 -0
- package/src/records.ts +105 -0
- package/src/revision-store.ts +513 -0
- package/src/run-container.ts +62 -0
- package/src/runner.ts +1 -1
- package/src/s3-blob-store.ts +489 -0
- package/src/state-retention.ts +83 -0
- package/src/storage.ts +335 -81
- package/src/store.ts +178 -5
- package/src/trigger-log.ts +15 -0
- package/src/workspace-revisions.ts +1324 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** What is stored: the whole entry (new content only, never the old text). */
|
|
2
|
+
export interface JournalRecord {
|
|
3
|
+
v: 1;
|
|
4
|
+
tenant: string;
|
|
5
|
+
workspace: string;
|
|
6
|
+
id: string;
|
|
7
|
+
files: {
|
|
8
|
+
path: string;
|
|
9
|
+
before: null;
|
|
10
|
+
after: string | null;
|
|
11
|
+
base?: string | null;
|
|
12
|
+
handed?: string | null;
|
|
13
|
+
}[];
|
|
14
|
+
by: string;
|
|
15
|
+
message: string;
|
|
16
|
+
at: string;
|
|
17
|
+
restoredFrom?: string;
|
|
18
|
+
/** A run's copy-back: the revisions (workspace head, state head) its stage-in
|
|
19
|
+
* started from. Each file's base is read from these, not from the folder. */
|
|
20
|
+
baseRev?: {
|
|
21
|
+
workspace: string | null;
|
|
22
|
+
state: string | null;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface JournalStore {
|
|
26
|
+
/** Which store this is, for logs ("files", "database"). */
|
|
27
|
+
readonly kind: string;
|
|
28
|
+
/** Write the entry down; throws if it cannot. Returns its handle. Putting an
|
|
29
|
+
* id that is already there answers the existing handle. */
|
|
30
|
+
put(rec: JournalRecord): string;
|
|
31
|
+
/** Forget one entry. Safe to repeat. */
|
|
32
|
+
remove(tenant: string, workspace: string, handle: string): void;
|
|
33
|
+
/** Every workspace that has entries. */
|
|
34
|
+
workspaces(): {
|
|
35
|
+
tenant: string;
|
|
36
|
+
workspace: string;
|
|
37
|
+
}[];
|
|
38
|
+
/** One workspace's entries, oldest first, with how long each has been there. */
|
|
39
|
+
entries(tenant: string, workspace: string): {
|
|
40
|
+
handle: string;
|
|
41
|
+
ageMs: number;
|
|
42
|
+
}[];
|
|
43
|
+
/** Read one entry; throws for one that is torn or foreign. */
|
|
44
|
+
read(tenant: string, workspace: string, handle: string): JournalRecord;
|
|
45
|
+
/** Keep an unreadable entry out of every later replay, without destroying it. */
|
|
46
|
+
setAside(tenant: string, workspace: string, handle: string): void;
|
|
47
|
+
/** Housekeeping before a replay: debris of a write that died half way (files only). */
|
|
48
|
+
sweep(tenant: string, workspace: string, olderThanMs: number): void;
|
|
49
|
+
/** Remove everything of one workspace, or of the whole account. Safe to repeat. */
|
|
50
|
+
drop(tenant: string, workspace?: string): void;
|
|
51
|
+
/** A workspace was renamed: its entries not held by a live queue (a dead
|
|
52
|
+
* process's) now belong to the new name. Handles of entries that were
|
|
53
|
+
* moved are NOT promised to stay valid: a caller holding some forgets them
|
|
54
|
+
* first and puts them again under the new name. */
|
|
55
|
+
move(tenant: string, from: string, to: string): void;
|
|
56
|
+
}
|
|
57
|
+
/** The files: <root>/<tenant>/<workspace>/<time>-<seq>-<id>.json, tmp + rename. */
|
|
58
|
+
export declare function fsJournalStore(root: () => string): JournalStore;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Where the revision journal's entries are kept (FOL-126 phase 4, item 3).
|
|
2
|
+
//
|
|
3
|
+
// The journal (workspace-revisions.ts) writes every change down BEFORE it is
|
|
4
|
+
// queued for a commit, and removes it once committed. Until now "written down"
|
|
5
|
+
// meant a file under <data>/.revision-journal/ — on the shared volume, which
|
|
6
|
+
// the storage refactor retires. So the place is a seam:
|
|
7
|
+
//
|
|
8
|
+
// fsJournalStore the files, as before: the default, and the only one a
|
|
9
|
+
// laptop or a Compose install without a database ever has.
|
|
10
|
+
// A hosted install keeps it as a LEGACY source, replayed at
|
|
11
|
+
// boot and drained, so switching loses nothing.
|
|
12
|
+
// (platform) pgJournalStore (src/journal-pg.ts): a table, behind the
|
|
13
|
+
// same calls, registered where there is a database.
|
|
14
|
+
//
|
|
15
|
+
// The calls are synchronous (the writers that journal are), and an entry's
|
|
16
|
+
// `handle` is whatever the store needs to find it again.
|
|
17
|
+
import fs from "node:fs";
|
|
18
|
+
import path from "node:path";
|
|
19
|
+
/** The files: <root>/<tenant>/<workspace>/<time>-<seq>-<id>.json, tmp + rename. */
|
|
20
|
+
export function fsJournalStore(root) {
|
|
21
|
+
let seq = 0;
|
|
22
|
+
const dirOf = (tenant, workspace) => path.join(root(), tenant, workspace);
|
|
23
|
+
return {
|
|
24
|
+
kind: "files",
|
|
25
|
+
put(rec) {
|
|
26
|
+
const dir = dirOf(rec.tenant, rec.workspace);
|
|
27
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
28
|
+
const name = `${Date.now().toString().padStart(14, "0")}-${String(seq++ % 1_000_000).padStart(6, "0")}-${rec.id}.json`;
|
|
29
|
+
const file = path.join(dir, name);
|
|
30
|
+
fs.writeFileSync(`${file}.tmp`, JSON.stringify(rec));
|
|
31
|
+
fs.renameSync(`${file}.tmp`, file);
|
|
32
|
+
return file;
|
|
33
|
+
},
|
|
34
|
+
remove(tenant, workspace, handle) {
|
|
35
|
+
try {
|
|
36
|
+
fs.rmSync(handle, { force: true });
|
|
37
|
+
fs.rmdirSync(dirOf(tenant, workspace)); // only when empty
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// not empty, or already gone
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
workspaces() {
|
|
44
|
+
const out = [];
|
|
45
|
+
let tenants = [];
|
|
46
|
+
try {
|
|
47
|
+
tenants = fs.readdirSync(root());
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
for (const tenant of tenants) {
|
|
53
|
+
try {
|
|
54
|
+
for (const workspace of fs.readdirSync(path.join(root(), tenant)))
|
|
55
|
+
out.push({ tenant, workspace });
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// not a directory
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
},
|
|
63
|
+
entries(tenant, workspace) {
|
|
64
|
+
const dir = dirOf(tenant, workspace);
|
|
65
|
+
let names = [];
|
|
66
|
+
try {
|
|
67
|
+
names = fs.readdirSync(dir).filter((n) => n.endsWith(".json")).sort();
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
const out = [];
|
|
73
|
+
for (const n of names) {
|
|
74
|
+
const file = path.join(dir, n);
|
|
75
|
+
try {
|
|
76
|
+
out.push({ handle: file, ageMs: Date.now() - fs.statSync(file).mtimeMs });
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// gone meanwhile
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
},
|
|
84
|
+
read(_t, _w, handle) {
|
|
85
|
+
return JSON.parse(fs.readFileSync(handle, "utf8"));
|
|
86
|
+
},
|
|
87
|
+
setAside(_t, _w, handle) {
|
|
88
|
+
fs.renameSync(handle, `${handle}.unreadable`);
|
|
89
|
+
},
|
|
90
|
+
sweep(tenant, workspace, olderThanMs) {
|
|
91
|
+
const dir = dirOf(tenant, workspace);
|
|
92
|
+
try {
|
|
93
|
+
for (const n of fs.readdirSync(dir)) {
|
|
94
|
+
// a write that died before its rename leaves a .tmp: nothing to replay
|
|
95
|
+
if (n.endsWith(".json.tmp") && olderThanMs > 0 && Date.now() - fs.statSync(path.join(dir, n)).mtimeMs > olderThanMs)
|
|
96
|
+
fs.rmSync(path.join(dir, n), { force: true });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
// nothing there
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
drop(tenant, workspace) {
|
|
104
|
+
fs.rmSync(workspace === undefined ? path.join(root(), tenant) : dirOf(tenant, workspace), { recursive: true, force: true });
|
|
105
|
+
},
|
|
106
|
+
move(tenant, from, to) {
|
|
107
|
+
const src = dirOf(tenant, from);
|
|
108
|
+
if (!fs.existsSync(src))
|
|
109
|
+
return;
|
|
110
|
+
const dest = dirOf(tenant, to);
|
|
111
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
112
|
+
for (const n of fs.readdirSync(src))
|
|
113
|
+
fs.renameSync(path.join(src, n), path.join(dest, n));
|
|
114
|
+
fs.rmSync(src, { recursive: true, force: true });
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export interface ManifestEntry {
|
|
2
|
+
/** sha256 of the bytes, lower-case hex. The blob's name. */
|
|
3
|
+
sha: string;
|
|
4
|
+
size: number;
|
|
5
|
+
/** 0o644, or 0o755 when the file is executable (store.ts marks scripts so).
|
|
6
|
+
* Stored as the number, so JSON carries 420 or 493. Nothing else is kept:
|
|
7
|
+
* owner, times and extended attributes are not part of a workspace. */
|
|
8
|
+
mode: number;
|
|
9
|
+
}
|
|
10
|
+
export interface Manifest {
|
|
11
|
+
v: 1;
|
|
12
|
+
/** Forward-slashed paths, relative to the workspace root. */
|
|
13
|
+
files: Record<string, ManifestEntry>;
|
|
14
|
+
}
|
|
15
|
+
export declare const MODE_FILE = 420;
|
|
16
|
+
export declare const MODE_EXEC = 493;
|
|
17
|
+
/** A manifest id: the sha256 of its canonical JSON. */
|
|
18
|
+
export type ManifestId = string;
|
|
19
|
+
export declare const AUTHORED_DIRS: readonly string[];
|
|
20
|
+
export declare const STATE_DIRS: readonly string[];
|
|
21
|
+
export declare const AUTHORED_ROOT_FILES: readonly string[];
|
|
22
|
+
export type Side = "authored" | "state";
|
|
23
|
+
/** Which manifest a path belongs in, or null when it belongs in neither. */
|
|
24
|
+
export declare function sideOf(rel: string): Side | null;
|
|
25
|
+
/**
|
|
26
|
+
* A manifest path: relative, forward-slashed, no empty / `.` / `..` segment,
|
|
27
|
+
* no control characters, at most 400 characters (the same bounds as a storage
|
|
28
|
+
* path), and on one of the two sides. Throws; returns the path unchanged when
|
|
29
|
+
* it is fine. Backslashes are REFUSED rather than converted: a manifest must
|
|
30
|
+
* round-trip byte for byte, and `a\b` on one machine is a different name from
|
|
31
|
+
* `a/b` on another.
|
|
32
|
+
*/
|
|
33
|
+
export declare const MAX_PATH_BYTES = 400;
|
|
34
|
+
/** Files per manifest (authored and state each). A manifest is read and
|
|
35
|
+
* rewritten whole on every publish; this bounds that cost until manifests
|
|
36
|
+
* are sharded. 50k files is ~5 MB of canonical JSON. */
|
|
37
|
+
export declare const MAX_MANIFEST_FILES = 50000;
|
|
38
|
+
export declare function assertManifestPath(rel: string): string;
|
|
39
|
+
/** Two paths that differ only by case cannot both be extracted on a
|
|
40
|
+
* case-insensitive disk (macOS and Windows laptops, Docker Desktop volumes).
|
|
41
|
+
* Display casing is kept; a collision inside one manifest is refused. */
|
|
42
|
+
export declare function assertNoCaseCollisions(m: Manifest): void;
|
|
43
|
+
export declare const sha256Hex: (b: string | Uint8Array) => string;
|
|
44
|
+
/** What "the same entry" means: bytes AND mode. A chmod +x is a change even
|
|
45
|
+
* though the sha is the same. Changes record this as their `base`. */
|
|
46
|
+
export declare const entryId: (e: Pick<ManifestEntry, "sha" | "mode">) => string;
|
|
47
|
+
export declare function assertEntry(p: string, e: ManifestEntry): void;
|
|
48
|
+
/** Everything parseManifest would refuse, checked on a manifest we are about
|
|
49
|
+
* to WRITE — a revision must never point at a manifest it cannot read back. */
|
|
50
|
+
export declare function assertManifest(m: Manifest): void;
|
|
51
|
+
export declare function emptyManifest(): Manifest;
|
|
52
|
+
/** Bytes → the mode a manifest records: executable stays executable. */
|
|
53
|
+
export declare function modeFor(executable: boolean): number;
|
|
54
|
+
/** Sorted keys at every level, no whitespace: one tree, one byte string, one
|
|
55
|
+
* id — whichever machine wrote it. */
|
|
56
|
+
export declare function canonicalManifest(m: Manifest): string;
|
|
57
|
+
export declare function manifestId(m: Manifest): ManifestId;
|
|
58
|
+
/** The id of a manifest with no files. A revision on one head names this for
|
|
59
|
+
* the side that head does not own (the workspace head carries no state, the
|
|
60
|
+
* state head carries no authored files); it is never stored, loadManifest
|
|
61
|
+
* answers it. */
|
|
62
|
+
export declare const EMPTY_MANIFEST_ID: ManifestId;
|
|
63
|
+
/** Parse and validate a manifest read from a store. A manifest that does not
|
|
64
|
+
* say so cleanly is a broken revision, not an empty one. */
|
|
65
|
+
export declare function parseManifest(text: string): Manifest;
|
|
66
|
+
export declare const blobObjectKey: (tenant: string, sha: string) => string;
|
|
67
|
+
export declare const manifestObjectKey: (tenant: string, id: ManifestId) => string;
|
|
68
|
+
/** One path's change, as a writer saw it. `base` is the entryId (sha + mode)
|
|
69
|
+
* the writer STARTED from (null: the path did not exist) — the fact that makes "disjoint paths
|
|
70
|
+
* replay, the same path conflicts" decidable without a merge. */
|
|
71
|
+
export type PathChange = {
|
|
72
|
+
op: "put";
|
|
73
|
+
base: string | null;
|
|
74
|
+
sha: string;
|
|
75
|
+
size: number;
|
|
76
|
+
mode: number;
|
|
77
|
+
} | {
|
|
78
|
+
op: "delete";
|
|
79
|
+
base: string;
|
|
80
|
+
};
|
|
81
|
+
export type Changes = Record<string, PathChange>;
|
|
82
|
+
/** Apply changes to a manifest, ignoring `base` (the caller decided). */
|
|
83
|
+
export declare function applyChanges(m: Manifest, changes: Changes): Manifest;
|
|
84
|
+
/** The changes that turn `from` into `to`, with `from`'s shas as the bases. */
|
|
85
|
+
export declare function diffManifests(from: Manifest, to: Manifest): Changes;
|
|
86
|
+
/** The copy a conflicting writer's version is kept as. Deterministic in the
|
|
87
|
+
* operation id — a retried publish names the same file, so a retry cannot
|
|
88
|
+
* leave two copies — and next to the original, like today's
|
|
89
|
+
* `.conflict-<stamp>` copy in applyContainerChanges. 48 bits of the op id's
|
|
90
|
+
* hash; `attempt` > 0 is the caller stepping aside when that name is already
|
|
91
|
+
* taken by something else (publish never overwrites an occupied name). */
|
|
92
|
+
export declare function conflictPath(rel: string, opId: string, attempt?: number): string;
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// The manifest: what a workspace revision is made of (FOL-126, phase 1).
|
|
2
|
+
//
|
|
3
|
+
// A revision does not hold files. It points at two manifests — one for what
|
|
4
|
+
// people author, one for state — and a manifest maps each path to an
|
|
5
|
+
// immutable blob:
|
|
6
|
+
//
|
|
7
|
+
// { v: 1, files: { "agents/writer/agent.md": { sha, size, mode } } }
|
|
8
|
+
//
|
|
9
|
+
// Content-addressed twice over: a blob is named by the hash of its bytes, and
|
|
10
|
+
// a manifest by the hash of its canonical JSON. Equal content is the same
|
|
11
|
+
// object, so a re-publish of an unchanged tree uploads nothing and a restore
|
|
12
|
+
// is just a new pointer at old objects.
|
|
13
|
+
//
|
|
14
|
+
// What a manifest cannot say (by decision, phase 1):
|
|
15
|
+
// symlinks not representable. A scanner that meets one must REFUSE it
|
|
16
|
+
// (lstat, never follow), not store its target's bytes: following
|
|
17
|
+
// a link out of the workspace is how a file outside it gets
|
|
18
|
+
// published. (The laptop's agents/<a>/workspace link is a CLI
|
|
19
|
+
// thing; the hosted copy-back already skips symlinks.)
|
|
20
|
+
// empty folders not representable; a folder exists while it holds a file.
|
|
21
|
+
// rename there is no rename entry: a move is a delete plus a put, so a
|
|
22
|
+
// concurrent edit to the old path survives beside the new one.
|
|
23
|
+
// owner/times not kept.
|
|
24
|
+
// case two names that differ only by case cannot share a manifest;
|
|
25
|
+
// paths must be NFC. Both are enforced on write and on read.
|
|
26
|
+
// size at most MAX_MANIFEST_FILES per manifest; a manifest is read
|
|
27
|
+
// and rewritten whole on every publish, so very large trees
|
|
28
|
+
// want a sharded manifest (open, phase 3/4).
|
|
29
|
+
//
|
|
30
|
+
// Nothing here touches a disk or a network. It is the vocabulary the
|
|
31
|
+
// BlobStore, the RevisionStore and the publish protocol share.
|
|
32
|
+
import crypto from "node:crypto";
|
|
33
|
+
export const MODE_FILE = 0o644;
|
|
34
|
+
export const MODE_EXEC = 0o755;
|
|
35
|
+
// ---------- which top-level names are what ----------
|
|
36
|
+
//
|
|
37
|
+
// The two manifests split a workspace by lifetime, not by file type:
|
|
38
|
+
//
|
|
39
|
+
// authored what a person (or a deploy, or the assistant) wrote on purpose.
|
|
40
|
+
// Reviewed, diffed, restored. Everything in WORKSPACE_DIRS except
|
|
41
|
+
// state/, plus the two root files.
|
|
42
|
+
// state what runs accumulate: state/. Checkpointed per step, restorable
|
|
43
|
+
// separately and only on request (restoring source must not
|
|
44
|
+
// rewind a week of an agent's memory-of-work).
|
|
45
|
+
//
|
|
46
|
+
// Everything else a workspace directory holds is somebody else's record and is
|
|
47
|
+
// refused here on purpose, because it already has an owner:
|
|
48
|
+
//
|
|
49
|
+
// storage/ its own file index (uploads; FileRecord, not a manifest)
|
|
50
|
+
// runs/ run records and outputs (Postgres + S3 in phase 2/5)
|
|
51
|
+
// secrets the vault (phase 6), outside every manifest and every restore
|
|
52
|
+
// once/, trigger-log.jsonl, hook-deliveries.jsonl, history* records
|
|
53
|
+
// .git, node_modules, .venv never workspace content
|
|
54
|
+
//
|
|
55
|
+
// A path that is in none of these lists is refused rather than guessed at: a
|
|
56
|
+
// new top-level folder is a decision (add it here and say which side it is
|
|
57
|
+
// on), not an accident that silently starts being versioned.
|
|
58
|
+
// FROZEN for manifest v1. These are literals, not derived from WORKSPACE_DIRS:
|
|
59
|
+
// a manifest already written must keep meaning what it meant, so adding a
|
|
60
|
+
// folder to store.ts later must not silently change which old paths are valid.
|
|
61
|
+
// tests/manifest.test.ts compares this list with WORKSPACE_DIRS and fails when
|
|
62
|
+
// they differ — the failure is the prompt to decide which side the new folder
|
|
63
|
+
// is on, and whether that needs manifest v2.
|
|
64
|
+
export const AUTHORED_DIRS = ["agents", "flows", "evals", "skills", "memory", "knowledge", "tools", "scripts", "hooks"];
|
|
65
|
+
export const STATE_DIRS = ["state"];
|
|
66
|
+
export const AUTHORED_ROOT_FILES = ["AGENTS.md", "project.md"];
|
|
67
|
+
/** A conflict copy of a root file (conflictPath) is a root file too. */
|
|
68
|
+
const ROOT_CONFLICT = /^(AGENTS|project)\.conflict-[0-9a-f]{12}(-\d+)?\.md$/;
|
|
69
|
+
/** Which manifest a path belongs in, or null when it belongs in neither. */
|
|
70
|
+
export function sideOf(rel) {
|
|
71
|
+
if (AUTHORED_ROOT_FILES.includes(rel) || ROOT_CONFLICT.test(rel))
|
|
72
|
+
return "authored";
|
|
73
|
+
const top = rel.split("/")[0];
|
|
74
|
+
if (rel.includes("/") && AUTHORED_DIRS.includes(top))
|
|
75
|
+
return "authored";
|
|
76
|
+
if (rel.includes("/") && STATE_DIRS.includes(top))
|
|
77
|
+
return "state";
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* A manifest path: relative, forward-slashed, no empty / `.` / `..` segment,
|
|
82
|
+
* no control characters, at most 400 characters (the same bounds as a storage
|
|
83
|
+
* path), and on one of the two sides. Throws; returns the path unchanged when
|
|
84
|
+
* it is fine. Backslashes are REFUSED rather than converted: a manifest must
|
|
85
|
+
* round-trip byte for byte, and `a\b` on one machine is a different name from
|
|
86
|
+
* `a/b` on another.
|
|
87
|
+
*/
|
|
88
|
+
export const MAX_PATH_BYTES = 400;
|
|
89
|
+
/** Files per manifest (authored and state each). A manifest is read and
|
|
90
|
+
* rewritten whole on every publish; this bounds that cost until manifests
|
|
91
|
+
* are sharded. 50k files is ~5 MB of canonical JSON. */
|
|
92
|
+
export const MAX_MANIFEST_FILES = 50_000;
|
|
93
|
+
export function assertManifestPath(rel) {
|
|
94
|
+
if (typeof rel !== "string" || !rel || Buffer.byteLength(rel) > MAX_PATH_BYTES)
|
|
95
|
+
throw new Error(`illegal manifest path: ${String(rel).slice(0, 80)}`);
|
|
96
|
+
if (rel.includes("\\") || rel.startsWith("/") || /^[a-zA-Z]:/.test(rel))
|
|
97
|
+
throw new Error(`illegal manifest path: ${rel}`);
|
|
98
|
+
if (rel.split("/").some((s) => s === "" || s === "." || s === ".."))
|
|
99
|
+
throw new Error(`illegal manifest path: ${rel}`);
|
|
100
|
+
if (/[\u0000-\u001f\u007f]/.test(rel))
|
|
101
|
+
throw new Error(`illegal manifest path: ${rel}`);
|
|
102
|
+
// NFC only: "é" typed as one character and as e + a combining accent are one
|
|
103
|
+
// name on a Mac and two on Linux. A manifest holds the composed form, so it
|
|
104
|
+
// extracts the same everywhere. (Case is handled per manifest, below.)
|
|
105
|
+
if (rel !== rel.normalize("NFC"))
|
|
106
|
+
throw new Error(`illegal manifest path (not NFC-normalised): ${rel}`);
|
|
107
|
+
if (sideOf(rel) === null)
|
|
108
|
+
throw new Error(`${rel} is not workspace content (authored: ${[...AUTHORED_DIRS, ...AUTHORED_ROOT_FILES].join(", ")}; state: state/)`);
|
|
109
|
+
return rel;
|
|
110
|
+
}
|
|
111
|
+
/** Two paths that differ only by case cannot both be extracted on a
|
|
112
|
+
* case-insensitive disk (macOS and Windows laptops, Docker Desktop volumes).
|
|
113
|
+
* Display casing is kept; a collision inside one manifest is refused. */
|
|
114
|
+
export function assertNoCaseCollisions(m) {
|
|
115
|
+
const seen = new Map();
|
|
116
|
+
for (const p of Object.keys(m.files)) {
|
|
117
|
+
const k = p.toLowerCase();
|
|
118
|
+
const was = seen.get(k);
|
|
119
|
+
if (was !== undefined && was !== p)
|
|
120
|
+
throw new Error(`${p} and ${was} differ only by case`);
|
|
121
|
+
seen.set(k, p);
|
|
122
|
+
}
|
|
123
|
+
if (seen.size > MAX_MANIFEST_FILES)
|
|
124
|
+
throw new Error(`a manifest holds at most ${MAX_MANIFEST_FILES} files`);
|
|
125
|
+
}
|
|
126
|
+
export const sha256Hex = (b) => crypto.createHash("sha256").update(b).digest("hex");
|
|
127
|
+
/** What "the same entry" means: bytes AND mode. A chmod +x is a change even
|
|
128
|
+
* though the sha is the same. Changes record this as their `base`. */
|
|
129
|
+
export const entryId = (e) => `${e.sha}:${e.mode.toString(8)}`;
|
|
130
|
+
export function assertEntry(p, e) {
|
|
131
|
+
if (!/^[0-9a-f]{64}$/.test(e?.sha) || !Number.isSafeInteger(e.size) || e.size < 0 || (e.mode !== MODE_FILE && e.mode !== MODE_EXEC)) {
|
|
132
|
+
throw new Error(`manifest entry for ${p} is malformed`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** Everything parseManifest would refuse, checked on a manifest we are about
|
|
136
|
+
* to WRITE — a revision must never point at a manifest it cannot read back. */
|
|
137
|
+
export function assertManifest(m) {
|
|
138
|
+
for (const [p, e] of Object.entries(m.files)) {
|
|
139
|
+
assertManifestPath(p);
|
|
140
|
+
assertEntry(p, e);
|
|
141
|
+
}
|
|
142
|
+
assertNoCaseCollisions(m);
|
|
143
|
+
}
|
|
144
|
+
export function emptyManifest() {
|
|
145
|
+
return { v: 1, files: {} };
|
|
146
|
+
}
|
|
147
|
+
/** Bytes → the mode a manifest records: executable stays executable. */
|
|
148
|
+
export function modeFor(executable) {
|
|
149
|
+
return executable ? MODE_EXEC : MODE_FILE;
|
|
150
|
+
}
|
|
151
|
+
/** Sorted keys at every level, no whitespace: one tree, one byte string, one
|
|
152
|
+
* id — whichever machine wrote it. */
|
|
153
|
+
export function canonicalManifest(m) {
|
|
154
|
+
const files = {};
|
|
155
|
+
for (const p of Object.keys(m.files).sort()) {
|
|
156
|
+
const e = m.files[p];
|
|
157
|
+
files[p] = { sha: e.sha, size: e.size, mode: e.mode };
|
|
158
|
+
}
|
|
159
|
+
return JSON.stringify({ v: 1, files });
|
|
160
|
+
}
|
|
161
|
+
export function manifestId(m) {
|
|
162
|
+
return sha256Hex(canonicalManifest(m));
|
|
163
|
+
}
|
|
164
|
+
/** The id of a manifest with no files. A revision on one head names this for
|
|
165
|
+
* the side that head does not own (the workspace head carries no state, the
|
|
166
|
+
* state head carries no authored files); it is never stored, loadManifest
|
|
167
|
+
* answers it. */
|
|
168
|
+
export const EMPTY_MANIFEST_ID = sha256Hex(JSON.stringify({ v: 1, files: {} }));
|
|
169
|
+
/** Parse and validate a manifest read from a store. A manifest that does not
|
|
170
|
+
* say so cleanly is a broken revision, not an empty one. */
|
|
171
|
+
export function parseManifest(text) {
|
|
172
|
+
const raw = JSON.parse(text);
|
|
173
|
+
if (raw?.v !== 1 || typeof raw.files !== "object" || raw.files === null || Array.isArray(raw.files)) {
|
|
174
|
+
throw new Error("not a manifest (v1)");
|
|
175
|
+
}
|
|
176
|
+
assertManifest(raw);
|
|
177
|
+
return raw;
|
|
178
|
+
}
|
|
179
|
+
// ---------- object keys ----------
|
|
180
|
+
//
|
|
181
|
+
// Tenant-first, so a bucket policy can be written per tenant, and nothing in a
|
|
182
|
+
// key depends on the workspace's name: renaming a workspace moves no object.
|
|
183
|
+
const SEG = /^[A-Za-z0-9._-]{1,128}$/;
|
|
184
|
+
function seg(v, what) {
|
|
185
|
+
if (!SEG.test(v) || v === "." || v === "..")
|
|
186
|
+
throw new Error(`illegal ${what} in an object key`);
|
|
187
|
+
return v;
|
|
188
|
+
}
|
|
189
|
+
export const blobObjectKey = (tenant, sha) => {
|
|
190
|
+
if (!/^[0-9a-f]{64}$/.test(sha))
|
|
191
|
+
throw new Error("expected a sha256");
|
|
192
|
+
return `t/${seg(tenant, "tenant")}/blobs/${sha}`;
|
|
193
|
+
};
|
|
194
|
+
export const manifestObjectKey = (tenant, id) => {
|
|
195
|
+
if (!/^[0-9a-f]{64}$/.test(id))
|
|
196
|
+
throw new Error("expected a manifest id");
|
|
197
|
+
return `t/${seg(tenant, "tenant")}/manifests/${id}.json`;
|
|
198
|
+
};
|
|
199
|
+
/** Apply changes to a manifest, ignoring `base` (the caller decided). */
|
|
200
|
+
export function applyChanges(m, changes) {
|
|
201
|
+
const files = { ...m.files };
|
|
202
|
+
for (const [p, c] of Object.entries(changes)) {
|
|
203
|
+
if (c.op === "delete")
|
|
204
|
+
delete files[p];
|
|
205
|
+
else
|
|
206
|
+
files[p] = { sha: c.sha, size: c.size, mode: c.mode };
|
|
207
|
+
}
|
|
208
|
+
return { v: 1, files };
|
|
209
|
+
}
|
|
210
|
+
/** The changes that turn `from` into `to`, with `from`'s shas as the bases. */
|
|
211
|
+
export function diffManifests(from, to) {
|
|
212
|
+
const out = {};
|
|
213
|
+
for (const [p, e] of Object.entries(to.files)) {
|
|
214
|
+
const was = from.files[p];
|
|
215
|
+
if (!was)
|
|
216
|
+
out[p] = { op: "put", base: null, ...e };
|
|
217
|
+
else if (entryId(was) !== entryId(e))
|
|
218
|
+
out[p] = { op: "put", base: entryId(was), ...e };
|
|
219
|
+
}
|
|
220
|
+
for (const [p, e] of Object.entries(from.files))
|
|
221
|
+
if (!to.files[p])
|
|
222
|
+
out[p] = { op: "delete", base: entryId(e) };
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
/** The copy a conflicting writer's version is kept as. Deterministic in the
|
|
226
|
+
* operation id — a retried publish names the same file, so a retry cannot
|
|
227
|
+
* leave two copies — and next to the original, like today's
|
|
228
|
+
* `.conflict-<stamp>` copy in applyContainerChanges. 48 bits of the op id's
|
|
229
|
+
* hash; `attempt` > 0 is the caller stepping aside when that name is already
|
|
230
|
+
* taken by something else (publish never overwrites an occupied name). */
|
|
231
|
+
export function conflictPath(rel, opId, attempt = 0) {
|
|
232
|
+
const slash = rel.lastIndexOf("/");
|
|
233
|
+
const dir = slash >= 0 ? rel.slice(0, slash + 1) : "";
|
|
234
|
+
const name = slash >= 0 ? rel.slice(slash + 1) : rel;
|
|
235
|
+
const dot = name.lastIndexOf(".");
|
|
236
|
+
const stem = dot > 0 ? name.slice(0, dot) : name;
|
|
237
|
+
const ext = dot > 0 ? name.slice(dot) : "";
|
|
238
|
+
return `${dir}${stem}.conflict-${sha256Hex(opId).slice(0, 12)}${attempt ? `-${attempt}` : ""}${ext}`;
|
|
239
|
+
}
|
package/dist/src/ops-log.js
CHANGED
|
@@ -41,6 +41,8 @@
|
|
|
41
41
|
// infra's tests refuse it in any manifest.
|
|
42
42
|
import fs from "node:fs";
|
|
43
43
|
import os from "node:os";
|
|
44
|
+
import path from "node:path";
|
|
45
|
+
import { fileURLToPath } from "node:url";
|
|
44
46
|
/** Why something failed, in one word an operator can count. Small on
|
|
45
47
|
* purpose: a class is a bucket on a dashboard, not a diagnosis. */
|
|
46
48
|
export const ERROR_CLASSES = [
|
|
@@ -218,6 +220,27 @@ export function errorClass(err) {
|
|
|
218
220
|
return "platform_error";
|
|
219
221
|
}
|
|
220
222
|
}
|
|
223
|
+
/** The checkout this file runs from, found from where it is, not from what
|
|
224
|
+
* the folder is called: a copy under another name (the pre-push hook's
|
|
225
|
+
* `.prepush-foldrun-core-<sha>`, a worktree, a CI workspace) is still ours. */
|
|
226
|
+
const OWN_ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
|
|
227
|
+
function ownTree(p) {
|
|
228
|
+
if (!p.startsWith(OWN_ROOT + path.sep))
|
|
229
|
+
return false;
|
|
230
|
+
return /^(src|server|app|lib|dist|tests|scripts|instrumentation)\//.test(p.slice(OWN_ROOT.length + 1));
|
|
231
|
+
}
|
|
232
|
+
/** A frame's file as a path: V8 writes an ES module as a file: URL, with a
|
|
233
|
+
* space or a non-ASCII letter percent-encoded. */
|
|
234
|
+
function framePath(raw) {
|
|
235
|
+
if (!raw.startsWith("file://"))
|
|
236
|
+
return raw;
|
|
237
|
+
try {
|
|
238
|
+
return fileURLToPath(raw);
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
return raw.slice(7);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
221
244
|
/** Is this source path the platform's own code (core, platform, web, their
|
|
222
245
|
* dependencies), not something an account brought — and a file that is
|
|
223
246
|
* really there? A stack is a settable string; a frame naming a file that
|
|
@@ -228,7 +251,7 @@ function ourPath(p) {
|
|
|
228
251
|
return true;
|
|
229
252
|
// A checkout (the image runs core and platform from source), the web's
|
|
230
253
|
// build, or the published package (`node_modules/@foldrun/core/dist`).
|
|
231
|
-
if (!/\/foldrun-(core|platform|web)\/(src|server|app|lib|dist|\.next|node_modules|tests|scripts|instrumentation)|\/node_modules\/@foldrun\//.test(p))
|
|
254
|
+
if (!ownTree(p) && !/\/foldrun-(core|platform|web)\/(src|server|app|lib|dist|\.next|node_modules|tests|scripts|instrumentation)|\/node_modules\/@foldrun\//.test(p))
|
|
232
255
|
return false;
|
|
233
256
|
let known = seenPaths.get(p);
|
|
234
257
|
if (known === undefined) {
|
|
@@ -256,7 +279,7 @@ export function stackFrames(stack, max = 5, skip = 0) {
|
|
|
256
279
|
const m = /^\s+at (?:.*? \()?(.+?):(\d+):\d+\)?$/.exec(raw);
|
|
257
280
|
if (!m)
|
|
258
281
|
continue;
|
|
259
|
-
const where = m[1]
|
|
282
|
+
const where = framePath(m[1]);
|
|
260
283
|
if (!ourPath(where))
|
|
261
284
|
continue;
|
|
262
285
|
if (seen++ < skip)
|
|
@@ -326,7 +349,7 @@ function callsite() {
|
|
|
326
349
|
const m = /^\s+at (?:.*? \()?(.+?):(\d+):\d+\)?$/.exec(raw);
|
|
327
350
|
if (!m || /\/ops-log\.(ts|js)$/.test(m[1]))
|
|
328
351
|
continue;
|
|
329
|
-
const where = m[1]
|
|
352
|
+
const where = framePath(m[1]);
|
|
330
353
|
if (!ourPath(where))
|
|
331
354
|
return undefined;
|
|
332
355
|
const frame = `${where.split("/").pop()}:${m[2]}`;
|
package/dist/src/platform.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { SupplyAnswer } from "./model-supply.ts";
|
|
|
2
2
|
import type { FlowInfo, FlowStep, RunRecord } from "./store.ts";
|
|
3
3
|
import type { RunInContainerArgs, ContainerStepOutcome } from "./run-container.ts";
|
|
4
4
|
import type { EgressHooks } from "./egress.ts";
|
|
5
|
+
import type { RecordsPort } from "./records.ts";
|
|
5
6
|
export type IsolatedStepRunner = (args: RunInContainerArgs) => Promise<ContainerStepOutcome>;
|
|
6
7
|
/** Who this install is for. `hosted` means someone is paying the operator
|
|
7
8
|
* for it — a wallet, a plan, a bill. `self-hosted` is everything else: a
|
|
@@ -248,6 +249,12 @@ export interface PlatformHooks {
|
|
|
248
249
|
* preferences and sends what it always sent.
|
|
249
250
|
*/
|
|
250
251
|
mailPreference(tenant: string, email: string, category: string, workspace: string | null): Promise<boolean>;
|
|
252
|
+
/**
|
|
253
|
+
* Where runs, the run index, the trigger log and the storage file index
|
|
254
|
+
* live (records.ts). Absent: files under the workspace, which is what a
|
|
255
|
+
* laptop and a self-hosted install with no database have.
|
|
256
|
+
*/
|
|
257
|
+
records?: RecordsPort;
|
|
251
258
|
}
|
|
252
259
|
/** What handing a webhook over came to: the delivery's id (the
|
|
253
260
|
* X-Foldrun-Delivery header) and its first attempt, when one was made. */
|
package/dist/src/platform.js
CHANGED