@foldrun/core 1.4.2 → 2.0.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/index.d.ts +0 -3
- package/dist/index.js +0 -3
- package/dist/src/deploy.d.ts +0 -26
- package/dist/src/deploy.js +7 -52
- package/dist/src/history.d.ts +30 -10
- package/dist/src/history.js +232 -99
- package/dist/src/library.d.ts +0 -13
- package/dist/src/library.js +0 -50
- package/dist/src/platform.d.ts +1 -3
- package/dist/src/platform.js +0 -1
- package/dist/src/run-container.d.ts +29 -0
- package/dist/src/run-container.js +63 -2
- package/dist/src/runner.js +10 -2
- package/dist/src/secrets.js +1 -12
- package/dist/src/starter.js +2 -2
- package/dist/src/store.d.ts +15 -10
- package/dist/src/store.js +45 -21
- package/index.ts +0 -3
- package/package.json +1 -3
- package/src/deploy.ts +2 -70
- package/src/history.ts +230 -108
- package/src/library.ts +0 -44
- package/src/platform.ts +1 -4
- package/src/run-container.ts +68 -2
- package/src/runner.ts +10 -1
- package/src/secrets.ts +1 -11
- package/src/starter.ts +2 -2
- package/src/store.ts +55 -27
- package/dist/src/git-hook.d.ts +0 -1
- package/dist/src/git-hook.js +0 -70
- package/dist/src/git.d.ts +0 -37
- package/dist/src/git.js +0 -101
- package/dist/src/gitrepo.d.ts +0 -113
- package/dist/src/gitrepo.js +0 -404
- package/dist/src/tar.d.ts +0 -30
- package/dist/src/tar.js +0 -112
- package/src/git-hook.ts +0 -73
- package/src/git.ts +0 -117
- package/src/gitrepo.ts +0 -439
- package/src/tar.ts +0 -125
package/src/deploy.ts
CHANGED
|
@@ -25,7 +25,7 @@ import {
|
|
|
25
25
|
saveWorkspace,
|
|
26
26
|
keptOnDeploy,
|
|
27
27
|
workspaceDir,
|
|
28
|
-
|
|
28
|
+
listRunSummaries,
|
|
29
29
|
parseFlow,
|
|
30
30
|
parseToolDef,
|
|
31
31
|
WORKSPACE_DIRS,
|
|
@@ -44,7 +44,6 @@ import { networkProblems } from "./network.ts";
|
|
|
44
44
|
import { actionProblems } from "./web-actions.ts";
|
|
45
45
|
import { languageProblem } from "./language.ts";
|
|
46
46
|
import { localeProblems } from "./locale.ts";
|
|
47
|
-
import { repoDir } from "./gitrepo.ts";
|
|
48
47
|
import { entriesNoFollow } from "./paths.ts";
|
|
49
48
|
import { filterIgnored, findingFingerprint, ruleForWhere, scanFiles, type ScanFinding, type ScanOptions } from "./scan.ts";
|
|
50
49
|
|
|
@@ -455,7 +454,7 @@ export function deployIssues(files: DeployFile[]): DeployIssue[] {
|
|
|
455
454
|
*/
|
|
456
455
|
export function runsInFlight(tenant: string, workspace: string): string[] {
|
|
457
456
|
if (!fs.existsSync(workspaceDir(tenant, workspace))) return [];
|
|
458
|
-
return
|
|
457
|
+
return listRunSummaries(tenant, workspace)
|
|
459
458
|
.filter((r) => r.status === "queued" || r.status === "running" || r.status === "awaiting-approval")
|
|
460
459
|
.map((r) => r.id);
|
|
461
460
|
}
|
|
@@ -539,73 +538,6 @@ export interface DeployOptions {
|
|
|
539
538
|
* webhook or a CLI that needs to report *why*, and every reason is already in
|
|
540
539
|
* the plan.
|
|
541
540
|
*/
|
|
542
|
-
/** What a push recorded about itself, where the Repository page reads it. */
|
|
543
|
-
export interface PushNote {
|
|
544
|
-
commit: string;
|
|
545
|
-
at: string;
|
|
546
|
-
applied: boolean;
|
|
547
|
-
issues?: unknown[];
|
|
548
|
-
blockedBy?: string[];
|
|
549
|
-
/** Set when a blocked push was applied later, by the retry. */
|
|
550
|
-
appliedAt?: string;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
export const pushNotePath = (tenant: string, workspace: string) =>
|
|
554
|
-
path.join(repoDir(tenant, workspace), "foldrun-last-push.json");
|
|
555
|
-
|
|
556
|
-
export function readPushNote(tenant: string, workspace: string): PushNote | null {
|
|
557
|
-
try {
|
|
558
|
-
return JSON.parse(fs.readFileSync(pushNotePath(tenant, workspace), "utf8")) as PushNote;
|
|
559
|
-
} catch {
|
|
560
|
-
return null;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
export function writePushNote(tenant: string, workspace: string, note: PushNote) {
|
|
565
|
-
try {
|
|
566
|
-
fs.writeFileSync(pushNotePath(tenant, workspace), JSON.stringify(note));
|
|
567
|
-
} catch {
|
|
568
|
-
// The note is a record, not the deploy. Losing it must not fail a push.
|
|
569
|
-
}
|
|
570
|
-
// A push waiting on a run is the scheduler's to apply; tell it there is one.
|
|
571
|
-
notifyWorkspaceChanged(tenant, workspace, "push");
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
/**
|
|
575
|
-
* A push whose deploy was refused only because a run was in flight is not a
|
|
576
|
-
* failure — it is a deploy that has not happened yet. Git already said the
|
|
577
|
-
* push succeeded (the deploy happens after the client is gone), so nothing
|
|
578
|
-
* would ever tell anyone the workspace on disk is behind `main`. This applies
|
|
579
|
-
* it once the runs are done.
|
|
580
|
-
*
|
|
581
|
-
* Only `blockedBy`: a push refused for `issues` is a broken workspace and
|
|
582
|
-
* wants a person, not a retry.
|
|
583
|
-
*/
|
|
584
|
-
export function applyPendingPush(
|
|
585
|
-
tenant: string,
|
|
586
|
-
workspace: string,
|
|
587
|
-
filesAt: (tenant: string, scope: string, ref: string) => DeployFile[],
|
|
588
|
-
): { commit: string } | null {
|
|
589
|
-
const note = readPushNote(tenant, workspace);
|
|
590
|
-
if (!note || note.applied) return null;
|
|
591
|
-
if (!note.blockedBy?.length || (note.issues?.length ?? 0) > 0) return null;
|
|
592
|
-
if (runsInFlight(tenant, workspace).length > 0) return null;
|
|
593
|
-
// Only the commit the note names, and only while it is still what main
|
|
594
|
-
// points at — a newer push supersedes it and wrote its own note.
|
|
595
|
-
const result = deployWorkspace(tenant, workspace, filesAt(tenant, workspace, note.commit), {
|
|
596
|
-
commit: note.commit,
|
|
597
|
-
force: false,
|
|
598
|
-
});
|
|
599
|
-
writePushNote(tenant, workspace, {
|
|
600
|
-
...note,
|
|
601
|
-
applied: result.applied,
|
|
602
|
-
issues: result.issues ?? [],
|
|
603
|
-
blockedBy: result.blockedBy ?? [],
|
|
604
|
-
...(result.applied ? { appliedAt: new Date().toISOString() } : {}),
|
|
605
|
-
});
|
|
606
|
-
return result.applied ? { commit: note.commit } : null;
|
|
607
|
-
}
|
|
608
|
-
|
|
609
541
|
export function deployWorkspace(
|
|
610
542
|
tenant: string,
|
|
611
543
|
workspace: string,
|
package/src/history.ts
CHANGED
|
@@ -15,28 +15,29 @@
|
|
|
15
15
|
// git repository — and when one is connected later, its commits slot into
|
|
16
16
|
// the same series.
|
|
17
17
|
//
|
|
18
|
-
// No git
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
18
|
+
// No git. This is a journal of content plus a snapshot of the whole tree at
|
|
19
|
+
// every revision — the platform's own deploy history, the way Vercel and
|
|
20
|
+
// Cloudflare keep theirs: rollback and restore point back to a snapshot and
|
|
21
|
+
// never need a repository. A customer's code history belongs in their own
|
|
22
|
+
// GitHub or GitLab, connected later, and is a separate thing. (Until 17 Oct
|
|
23
|
+
// 2026 this preferred a bare git repository per workspace whenever a git
|
|
24
|
+
// binary existed; that git is gone, and history started fresh from a
|
|
25
|
+
// baseline snapshot.)
|
|
22
26
|
//
|
|
23
|
-
// Storage
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
+
// Storage, under <data>/<tenant>/.history/<scope>/:
|
|
28
|
+
// <id>.json one revision: what changed, from what, to what (inline,
|
|
29
|
+
// clipped at 256 KB — what the history page diffs)
|
|
30
|
+
// index.jsonl one line per revision, newest last, so listing opens none
|
|
31
|
+
// trees/<id>.json the whole tree right after it: { path: sha256 }
|
|
32
|
+
// blobs/<aa>/<sha256> each version of a file, once — content-addressed,
|
|
33
|
+
// because a snapshot per save is mostly files that did not
|
|
34
|
+
// change, and storing them again would be the whole tree
|
|
35
|
+
// per save.
|
|
27
36
|
|
|
28
37
|
import fs from "node:fs";
|
|
29
38
|
import path from "node:path";
|
|
30
39
|
import crypto from "node:crypto";
|
|
31
40
|
import { dataRoot } from "./paths.ts";
|
|
32
|
-
import {
|
|
33
|
-
commitChanges,
|
|
34
|
-
gitAvailable,
|
|
35
|
-
headSha,
|
|
36
|
-
listCommits,
|
|
37
|
-
readCommit,
|
|
38
|
-
repoExists,
|
|
39
|
-
} from "./gitrepo.ts";
|
|
40
41
|
|
|
41
42
|
/**
|
|
42
43
|
* The current tree of a scope, so the first commit can be a full import.
|
|
@@ -54,30 +55,6 @@ export function registerTreeReader(fn: TreeReader) {
|
|
|
54
55
|
readTree = (tenant, scope) => fn(tenant, scope) ?? prev(tenant, scope);
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
/** Something to do after a commit lands on main — the mirror push. Registered
|
|
58
|
-
* by mirror.ts; this module cannot import it (secrets, settings). */
|
|
59
|
-
type AfterCommit = (tenant: string, scope: string, sha: string) => void;
|
|
60
|
-
const afterCommit: AfterCommit[] = [];
|
|
61
|
-
export function registerAfterCommit(fn: AfterCommit) {
|
|
62
|
-
afterCommit.push(fn);
|
|
63
|
-
}
|
|
64
|
-
export function notifyCommit(tenant: string, scope: string, sha: string) {
|
|
65
|
-
for (const fn of afterCommit) {
|
|
66
|
-
try {
|
|
67
|
-
fn(tenant, scope, sha);
|
|
68
|
-
} catch {
|
|
69
|
-
// a mirror that fails must not fail the save that triggered it
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** Journal or git? Git whenever the binary exists; the journal is the
|
|
75
|
-
* fallback for an install without one. An install that started on the
|
|
76
|
-
* journal keeps it readable — both are consulted on read. */
|
|
77
|
-
const useGit = () => process.env.FOLDRUN_HISTORY !== "journal" && gitAvailable();
|
|
78
|
-
|
|
79
|
-
// Not store's assertSafeName: store imports this module, and a cycle between
|
|
80
|
-
// the two is avoidable for one regex.
|
|
81
58
|
function assertSafeName(v: string, what: string) {
|
|
82
59
|
if (!/^[A-Za-z0-9][A-Za-z0-9._@-]{0,120}$/.test(v)) throw new Error(`illegal ${what}: ${v}`);
|
|
83
60
|
}
|
|
@@ -142,37 +119,6 @@ export function recordRevision(
|
|
|
142
119
|
const changed = files.filter((f) => f.before !== f.after);
|
|
143
120
|
if (changed.length === 0) return null;
|
|
144
121
|
|
|
145
|
-
if (useGit()) {
|
|
146
|
-
// First commit ever for this scope: import the whole tree as it stands
|
|
147
|
-
// now (the writer has already written), so the repository is complete
|
|
148
|
-
// from its first commit rather than starting with one file.
|
|
149
|
-
const first = !headSha(tenant, scope);
|
|
150
|
-
// Generated bundle files (knowledge/index.md, memory/log.md) are rewritten
|
|
151
|
-
// by the same save that changed a concept, outside this call. Commit them
|
|
152
|
-
// as they now stand, or a clone's index disagrees with its own files —
|
|
153
|
-
// the exact drift the template tests exist to catch.
|
|
154
|
-
const generated = first
|
|
155
|
-
? []
|
|
156
|
-
: (readTree(tenant, scope) ?? []).filter((f) => /(^|\/)(knowledge|memory)\/(.*\/)?(index|log)\.md$/.test(f.path));
|
|
157
|
-
const toCommit = first ? (readTree(tenant, scope) ?? changed) : [...changed, ...generated];
|
|
158
|
-
const sha = commitChanges(tenant, scope, toCommit, {
|
|
159
|
-
by: meta.by,
|
|
160
|
-
// Named after what the caller changed, not the generated files that
|
|
161
|
-
// rode along — "edited knowledge/style.md", not "changed 3 files".
|
|
162
|
-
message: first ? `initial import${meta.message ? ` — ${meta.message}` : ""}` : (meta.message ?? describe(changed)),
|
|
163
|
-
});
|
|
164
|
-
if (!sha) return null;
|
|
165
|
-
notifyCommit(tenant, scope, sha);
|
|
166
|
-
return {
|
|
167
|
-
id: meta.commit ?? sha,
|
|
168
|
-
at: new Date().toISOString(),
|
|
169
|
-
by: meta.by ?? "system",
|
|
170
|
-
message: meta.message ?? describe(changed),
|
|
171
|
-
commit: sha,
|
|
172
|
-
files: changed,
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
122
|
const dir = historyDir(tenant, scope);
|
|
177
123
|
fs.mkdirSync(dir, { recursive: true });
|
|
178
124
|
const rev: Revision = {
|
|
@@ -197,10 +143,169 @@ export function recordRevision(
|
|
|
197
143
|
commit: rev.commit,
|
|
198
144
|
paths: rev.files.map((f) => f.path),
|
|
199
145
|
};
|
|
146
|
+
// The tree as it stands now (the writer has already written), so this
|
|
147
|
+
// revision can be restored to exactly — including files a save rewrote
|
|
148
|
+
// outside this call, such as a bundle's generated index.
|
|
149
|
+
snapshot(tenant, scope, rev.id);
|
|
200
150
|
fs.appendFileSync(path.join(dir, "index.jsonl"), `${JSON.stringify(line)}\n`);
|
|
201
151
|
return rev;
|
|
202
152
|
}
|
|
203
153
|
|
|
154
|
+
// ----------------------------------------------------------------- snapshots
|
|
155
|
+
|
|
156
|
+
const sha256 = (s: string) => crypto.createHash("sha256").update(s).digest("hex");
|
|
157
|
+
const treePath = (tenant: string, scope: string, id: string) => path.join(historyDir(tenant, scope), "trees", `${id}.json`);
|
|
158
|
+
const blobPath = (tenant: string, scope: string, sha: string) => path.join(historyDir(tenant, scope), "blobs", sha.slice(0, 2), sha);
|
|
159
|
+
|
|
160
|
+
/** Write the scope's whole tree as revision `id`'s snapshot. False when there
|
|
161
|
+
* is no tree to read (a workspace being deleted).
|
|
162
|
+
*
|
|
163
|
+
* What a snapshot is: the tree as it stood just after this revision was
|
|
164
|
+
* written — point in time, which is what a restore wants. A revision's
|
|
165
|
+
* diff names only its own change; its snapshot is everything on disk at
|
|
166
|
+
* that moment, including another writer's change that landed in between,
|
|
167
|
+
* part of one if it was mid-write (saves are not transactional across
|
|
168
|
+
* files, and never were). Reviewed with Codex, 17 Oct 2026. */
|
|
169
|
+
function snapshot(tenant: string, scope: string, id: string): boolean {
|
|
170
|
+
const tree = readTree(tenant, scope);
|
|
171
|
+
if (!tree) return false;
|
|
172
|
+
const files: Record<string, string> = {};
|
|
173
|
+
for (const f of tree) {
|
|
174
|
+
if (f.after === null) continue;
|
|
175
|
+
const sha = sha256(f.after);
|
|
176
|
+
const blob = blobPath(tenant, scope, sha);
|
|
177
|
+
if (!fs.existsSync(blob)) {
|
|
178
|
+
fs.mkdirSync(path.dirname(blob), { recursive: true });
|
|
179
|
+
const tmp = `${blob}.${process.pid}.tmp`;
|
|
180
|
+
fs.writeFileSync(tmp, f.after);
|
|
181
|
+
fs.renameSync(tmp, blob);
|
|
182
|
+
}
|
|
183
|
+
files[f.path] = sha;
|
|
184
|
+
}
|
|
185
|
+
const file = treePath(tenant, scope, id);
|
|
186
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
187
|
+
const tmp = `${file}.tmp`;
|
|
188
|
+
fs.writeFileSync(tmp, JSON.stringify({ v: 1, files }));
|
|
189
|
+
fs.renameSync(tmp, file);
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Whether a revision can be restored to: it has a snapshot. */
|
|
194
|
+
export function hasSnapshot(tenant: string, scope: string, id: string): boolean {
|
|
195
|
+
assertSafeName(id, "revision id");
|
|
196
|
+
return fs.existsSync(treePath(tenant, scope, id));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The whole tree at revision `id`, or null when it has no snapshot. */
|
|
200
|
+
export function snapshotAt(tenant: string, scope: string, id: string): { path: string; content: string }[] | null {
|
|
201
|
+
assertSafeName(id, "revision id");
|
|
202
|
+
let manifest: { v: number; files: Record<string, string> };
|
|
203
|
+
try {
|
|
204
|
+
manifest = JSON.parse(fs.readFileSync(treePath(tenant, scope, id), "utf8"));
|
|
205
|
+
} catch {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
return Object.entries(manifest.files ?? {})
|
|
209
|
+
.map(([p, sha]) => ({ path: p, content: fs.readFileSync(blobPath(tenant, scope, sha), "utf8") }))
|
|
210
|
+
.sort((a, b) => a.path.localeCompare(b.path));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Every revision of the scope that has a snapshot — one listing, rather
|
|
214
|
+
* than a stat per revision. */
|
|
215
|
+
function snapshotIds(tenant: string, scope: string): Set<string> {
|
|
216
|
+
try {
|
|
217
|
+
return new Set(fs.readdirSync(path.join(historyDir(tenant, scope), "trees")).filter((f) => f.endsWith(".json")).map((f) => f.slice(0, -5)));
|
|
218
|
+
} catch {
|
|
219
|
+
return new Set();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The newest revision that can be restored to — what "now" is in history. */
|
|
224
|
+
export function headRevision(tenant: string, scope: string): RevisionSummary | null {
|
|
225
|
+
const have = snapshotIds(tenant, scope);
|
|
226
|
+
return journalRevisions(tenant, scope, { limit: Infinity }).find((r) => have.has(r.id)) ?? null;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* A scope with no snapshot yet gets one: its tree as it stands, recorded as
|
|
231
|
+
* a revision of its own — where its history starts. Every workspace that
|
|
232
|
+
* existed when history moved off git starts here, and so does one created
|
|
233
|
+
* by a path that never recorded a change. Returns the head's id, or null
|
|
234
|
+
* when there is nothing to snapshot.
|
|
235
|
+
*/
|
|
236
|
+
export function ensureBaseline(tenant: string, scope: string, by = "foldrun"): string | null {
|
|
237
|
+
const head = headRevision(tenant, scope);
|
|
238
|
+
if (head) return head.id;
|
|
239
|
+
const tree = readTree(tenant, scope);
|
|
240
|
+
if (!tree || tree.length === 0) return null;
|
|
241
|
+
const dir = historyDir(tenant, scope);
|
|
242
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
243
|
+
const id = newRevisionId();
|
|
244
|
+
// One baseline even when the web tier and the worker both find none at
|
|
245
|
+
// once: whoever creates the marker first writes it, the other reads it.
|
|
246
|
+
const marker = path.join(dir, ".baseline");
|
|
247
|
+
try {
|
|
248
|
+
fs.writeFileSync(marker, id, { flag: "wx" });
|
|
249
|
+
} catch {
|
|
250
|
+
const held = headRevision(tenant, scope)?.id ?? null;
|
|
251
|
+
// A marker with no baseline behind it, minutes old, is a process that
|
|
252
|
+
// died writing one: clear it, and the next call writes the baseline.
|
|
253
|
+
try {
|
|
254
|
+
if (!held && Date.now() - fs.statSync(marker).mtimeMs > 5 * 60_000) fs.rmSync(marker, { force: true });
|
|
255
|
+
} catch {
|
|
256
|
+
// gone already
|
|
257
|
+
}
|
|
258
|
+
return held;
|
|
259
|
+
}
|
|
260
|
+
const rev: Revision = {
|
|
261
|
+
id,
|
|
262
|
+
at: new Date().toISOString(),
|
|
263
|
+
by,
|
|
264
|
+
message: "history starts here",
|
|
265
|
+
commit: null,
|
|
266
|
+
files: [],
|
|
267
|
+
};
|
|
268
|
+
fs.writeFileSync(path.join(dir, `${rev.id}.json`), JSON.stringify(rev));
|
|
269
|
+
if (!snapshot(tenant, scope, rev.id)) return null;
|
|
270
|
+
const line: RevisionSummary = { id: rev.id, at: rev.at, by: rev.by, message: rev.message, commit: null, paths: [] };
|
|
271
|
+
fs.appendFileSync(path.join(dir, "index.jsonl"), `${JSON.stringify(line)}\n`);
|
|
272
|
+
return rev.id;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* The revision a restore means: an id (or the commit a deploy recorded), a
|
|
277
|
+
* date (`2026-10-17`, `2026-10-17T09:00Z`) or a span back from now (`90m`,
|
|
278
|
+
* `12h`, `3d`, `2w`) — the newest revision with a snapshot at or before that
|
|
279
|
+
* moment. Null when history holds nothing that old. Throws (status 400) on
|
|
280
|
+
* something that is none of those.
|
|
281
|
+
*/
|
|
282
|
+
export function resolveRevision(
|
|
283
|
+
tenant: string,
|
|
284
|
+
scope: string,
|
|
285
|
+
to: string,
|
|
286
|
+
now = Date.now(),
|
|
287
|
+
): { id: string; at: string; by: string; message: string } | null {
|
|
288
|
+
const t = to.trim();
|
|
289
|
+
if (!t || t.startsWith("-")) throw Object.assign(new Error(`not a revision or a time: ${to}`), { status: 400 });
|
|
290
|
+
const have = snapshotIds(tenant, scope);
|
|
291
|
+
const all = journalRevisions(tenant, scope, { limit: Infinity }).filter((r) => have.has(r.id));
|
|
292
|
+
const rel = /^(\d+)\s*(m|h|d|w)$/.exec(t);
|
|
293
|
+
const moment = rel
|
|
294
|
+
? now - Number(rel[1]) * { m: 60_000, h: 3_600_000, d: 86_400_000, w: 604_800_000 }[rel[2] as "m" | "h" | "d" | "w"]
|
|
295
|
+
: /^\d{4}-\d{2}-\d{2}/.test(t)
|
|
296
|
+
? Date.parse(t)
|
|
297
|
+
: null;
|
|
298
|
+
let hit: RevisionSummary | undefined;
|
|
299
|
+
if (moment !== null) {
|
|
300
|
+
if (Number.isNaN(moment)) throw Object.assign(new Error(`not a date: ${to}`), { status: 400 });
|
|
301
|
+
hit = all.find((r) => Date.parse(r.at) <= moment);
|
|
302
|
+
} else {
|
|
303
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,120}$/.test(t)) throw Object.assign(new Error(`not a revision or a time: ${to}`), { status: 400 });
|
|
304
|
+
hit = all.find((r) => r.id === t || r.commit === t || (t.length >= 7 && r.commit?.startsWith(t)));
|
|
305
|
+
}
|
|
306
|
+
return hit ? { id: hit.id, at: hit.at, by: hit.by, message: hit.message } : null;
|
|
307
|
+
}
|
|
308
|
+
|
|
204
309
|
const clip = (s: string | null) =>
|
|
205
310
|
s === null ? null : s.length > MAX_INLINE ? `[${s.length} bytes — too large to keep inline]` : s;
|
|
206
311
|
|
|
@@ -225,12 +330,6 @@ export function listRevisions(
|
|
|
225
330
|
scope: string,
|
|
226
331
|
opts: { forPath?: string; limit?: number } = {},
|
|
227
332
|
): RevisionSummary[] {
|
|
228
|
-
if (useGit() && repoExists(tenant, scope)) {
|
|
229
|
-
const fromGit = listCommits(tenant, scope, opts);
|
|
230
|
-
// Anything the journal holds from before the repository existed comes
|
|
231
|
-
// after git's rows: older, and still worth showing.
|
|
232
|
-
return [...fromGit, ...journalRevisions(tenant, scope, opts)].slice(0, opts.limit ?? 200);
|
|
233
|
-
}
|
|
234
333
|
return journalRevisions(tenant, scope, opts);
|
|
235
334
|
}
|
|
236
335
|
|
|
@@ -239,45 +338,68 @@ function journalRevisions(
|
|
|
239
338
|
scope: string,
|
|
240
339
|
opts: { forPath?: string; limit?: number } = {},
|
|
241
340
|
): RevisionSummary[] {
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
341
|
+
const dir = historyDir(tenant, scope);
|
|
342
|
+
const file = path.join(dir, "index.jsonl");
|
|
343
|
+
if (!fs.existsSync(dir)) return [];
|
|
344
|
+
let all = fs.existsSync(file)
|
|
345
|
+
? fs
|
|
346
|
+
.readFileSync(file, "utf8")
|
|
347
|
+
.split("\n")
|
|
348
|
+
.filter(Boolean)
|
|
349
|
+
.flatMap((l) => {
|
|
350
|
+
try {
|
|
351
|
+
return [JSON.parse(l) as RevisionSummary];
|
|
352
|
+
} catch {
|
|
353
|
+
return []; // a torn line — the rebuild below brings it back
|
|
354
|
+
}
|
|
355
|
+
})
|
|
356
|
+
.reverse()
|
|
357
|
+
: [];
|
|
358
|
+
// The revision files are the record; index.jsonl is a cache of them. An
|
|
359
|
+
// append from two processes on NFS is not atomic across clients, and a
|
|
360
|
+
// process can die between a revision's file and its line — so when the
|
|
361
|
+
// two disagree, the index is rebuilt from the files.
|
|
362
|
+
const recorded = fs.readdirSync(dir).filter((f) => f.endsWith(".json")).length;
|
|
363
|
+
if (recorded !== new Set(all.map((r) => r.id)).size) all = rebuildIndex(dir);
|
|
364
|
+
// A line appended just after a rebuild wrote it too: once each.
|
|
365
|
+
const seen = new Set<string>();
|
|
366
|
+
all = all.filter((r) => !seen.has(r.id) && seen.add(r.id));
|
|
256
367
|
const narrowed = opts.forPath ? all.filter((r) => r.paths.includes(opts.forPath!)) : all;
|
|
257
368
|
return narrowed.slice(0, opts.limit ?? 200);
|
|
258
369
|
}
|
|
259
370
|
|
|
371
|
+
/** index.jsonl again, from the revision files themselves, newest first. */
|
|
372
|
+
function rebuildIndex(dir: string): RevisionSummary[] {
|
|
373
|
+
const rows: (RevisionSummary & { written: number })[] = [];
|
|
374
|
+
for (const f of fs.readdirSync(dir)) {
|
|
375
|
+
if (!f.endsWith(".json")) continue;
|
|
376
|
+
try {
|
|
377
|
+
const file = path.join(dir, f);
|
|
378
|
+
const rev = JSON.parse(fs.readFileSync(file, "utf8")) as Revision;
|
|
379
|
+
rows.push({ id: rev.id, at: rev.at, by: rev.by, message: rev.message, commit: rev.commit, paths: rev.files.map((x) => x.path), written: fs.statSync(file).mtimeMs });
|
|
380
|
+
} catch {
|
|
381
|
+
// a revision half-written by a process that died: not one
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
// Two saves in one millisecond share an `at`; the order they were written
|
|
385
|
+
// in breaks the tie (an id's suffix is random, so it cannot).
|
|
386
|
+
rows.sort((a, b) => a.at.localeCompare(b.at) || a.written - b.written || a.id.localeCompare(b.id));
|
|
387
|
+
try {
|
|
388
|
+
const tmp = path.join(dir, `index.jsonl.${process.pid}.tmp`);
|
|
389
|
+
fs.writeFileSync(tmp, rows.map(({ written: _w, ...r }) => JSON.stringify(r) + "\n").join(""));
|
|
390
|
+
fs.renameSync(tmp, path.join(dir, "index.jsonl"));
|
|
391
|
+
} catch {
|
|
392
|
+
// a read-only volume still gets the answer
|
|
393
|
+
}
|
|
394
|
+
return rows.map(({ written: _w, ...r }) => r).reverse();
|
|
395
|
+
}
|
|
396
|
+
|
|
260
397
|
export function readRevision(tenant: string, scope: string, id: string, forPath?: string): Revision | null {
|
|
261
398
|
assertSafeName(id, "revision id");
|
|
262
|
-
if (useGit() && repoExists(tenant, scope) && /^[0-9a-f]{7,40}$/.test(id)) {
|
|
263
|
-
const fromGit = readCommit(tenant, scope, id, forPath);
|
|
264
|
-
if (fromGit) return fromGit;
|
|
265
|
-
}
|
|
266
399
|
const file = path.join(historyDir(tenant, scope), `${id}.json`);
|
|
267
400
|
if (!fs.existsSync(file)) return null;
|
|
268
|
-
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Make sure a scope's repository has its initial import, so a clone made
|
|
273
|
-
* right now is complete. A no-op once any commit exists.
|
|
274
|
-
*/
|
|
275
|
-
export function ensureImported(tenant: string, scope: string): string | null {
|
|
276
|
-
if (!useGit()) return null;
|
|
277
|
-
if (headSha(tenant, scope)) return headSha(tenant, scope);
|
|
278
|
-
const tree = readTree(tenant, scope);
|
|
279
|
-
if (!tree || tree.length === 0) return null;
|
|
280
|
-
return commitChanges(tenant, scope, tree, { by: "foldrun", message: "initial import" });
|
|
401
|
+
const rev = JSON.parse(fs.readFileSync(file, "utf8")) as Revision;
|
|
402
|
+
return forPath ? { ...rev, files: rev.files.filter((f) => f.path === forPath) } : rev;
|
|
281
403
|
}
|
|
282
404
|
|
|
283
405
|
/** The most recent revision id for a scope — what a change is attributed
|
package/src/library.ts
CHANGED
|
@@ -435,47 +435,3 @@ registerTreeReader((tenant, scope) => {
|
|
|
435
435
|
});
|
|
436
436
|
|
|
437
437
|
|
|
438
|
-
/**
|
|
439
|
-
* Make the shelf match a tree — what a push to the library repository does.
|
|
440
|
-
* Written with plain fs and recorded as ONE revision at the end: going
|
|
441
|
-
* through writeLibraryFile per file would commit each one on top of the
|
|
442
|
-
* pushed commit, and the history would show a push as fifty edits.
|
|
443
|
-
*/
|
|
444
|
-
export function syncLibraryFromTree(tenant: string, files: { path: string; content: string }[], meta: { by?: string; commit?: string | null } = {}) {
|
|
445
|
-
const wanted = new Map<string, string>();
|
|
446
|
-
for (const f of files) {
|
|
447
|
-
const [kind, ...rest] = f.path.split("/");
|
|
448
|
-
if (!LIBRARY_KINDS.includes(kind as LibraryKind) || rest.length === 0) continue;
|
|
449
|
-
wanted.set(f.path, f.content);
|
|
450
|
-
}
|
|
451
|
-
const changes: { path: string; before: string | null; after: string | null }[] = [];
|
|
452
|
-
for (const kind of LIBRARY_KINDS) {
|
|
453
|
-
const dir = libraryDir(tenant, kind);
|
|
454
|
-
if (!fs.existsSync(dir)) continue;
|
|
455
|
-
for (const entry of fs.readdirSync(dir, { recursive: true })) {
|
|
456
|
-
const rel = `${kind}/${String(entry).split(path.sep).join("/")}`;
|
|
457
|
-
const full = path.join(dir, String(entry));
|
|
458
|
-
try {
|
|
459
|
-
if (!fs.statSync(full).isFile() || !TEXT_EXT.test(rel)) continue;
|
|
460
|
-
} catch {
|
|
461
|
-
continue;
|
|
462
|
-
}
|
|
463
|
-
if (!wanted.has(rel)) {
|
|
464
|
-
changes.push({ path: rel, before: fs.readFileSync(full, "utf8"), after: null });
|
|
465
|
-
fs.rmSync(full, { force: true });
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
for (const [rel, content] of wanted) {
|
|
470
|
-
const [kind, ...rest] = rel.split("/");
|
|
471
|
-
const full = path.join(libraryDir(tenant, kind as LibraryKind), assertLibraryPath(kind as LibraryKind, rest.join("/")));
|
|
472
|
-
const before = fs.existsSync(full) ? fs.readFileSync(full, "utf8") : null;
|
|
473
|
-
if (before === content) continue;
|
|
474
|
-
fs.mkdirSync(path.dirname(full), { recursive: true });
|
|
475
|
-
fs.writeFileSync(full, content);
|
|
476
|
-
if (kind === "scripts" || /scripts\//.test(rel) || (kind === "tools" && !rel.endsWith(".md"))) fs.chmodSync(full, 0o755);
|
|
477
|
-
changes.push({ path: rel, before, after: content });
|
|
478
|
-
}
|
|
479
|
-
recordRevision(tenant, "@library", changes, { by: meta.by ?? "deploy", message: meta.commit ? `deployed ${meta.commit.slice(0, 7)}` : "synced from push", commit: meta.commit ?? null });
|
|
480
|
-
return changes.length;
|
|
481
|
-
}
|
package/src/platform.ts
CHANGED
|
@@ -109,8 +109,6 @@ export interface PlatformHooks {
|
|
|
109
109
|
killRunSandboxes(runId: string): void;
|
|
110
110
|
/** Publish storage/public/ as share links after a run. Default: no links. */
|
|
111
111
|
syncPublicShares(tenant: string, workspace: string): Promise<{ added: string[] }>;
|
|
112
|
-
/** The workspace a preview was branched from, for inherited secrets. Default: none. */
|
|
113
|
-
previewSourceOf(tenant: string, workspace: string): string | null;
|
|
114
112
|
/** This account's own data key, or null to use the install key. */
|
|
115
113
|
tenantKey(tenant: string): Buffer | null;
|
|
116
114
|
/** The egress proxy — a per-step lease that keeps credentials out of the
|
|
@@ -120,7 +118,7 @@ export interface PlatformHooks {
|
|
|
120
118
|
* workspace deleted. What a scheduler that keeps flows in memory rather
|
|
121
119
|
* than re-reading every workspace each tick needs to hear. Default:
|
|
122
120
|
* nothing — locally the scheduler reads the disk. Never throws. */
|
|
123
|
-
workspaceChanged(tenant: string, workspace: string, why: "write" | "deploy" | "
|
|
121
|
+
workspaceChanged(tenant: string, workspace: string, why: "write" | "deploy" | "delete"): void;
|
|
124
122
|
/** Can a step in this isolation mode be re-attached to after the driver
|
|
125
123
|
* that started it is gone? True means an orphaned step with a `sandbox`
|
|
126
124
|
* on its record is resumed rather than destroyed and run again. Default:
|
|
@@ -204,7 +202,6 @@ const local: PlatformHooks = {
|
|
|
204
202
|
isolation: {},
|
|
205
203
|
killRunSandboxes() {},
|
|
206
204
|
syncPublicShares: async () => ({ added: [] }),
|
|
207
|
-
previewSourceOf: () => null,
|
|
208
205
|
tenantKey: () => null,
|
|
209
206
|
egress: { lease: async () => null },
|
|
210
207
|
workspaceChanged() {},
|