@trama-dev/runtime 0.4.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.
@@ -0,0 +1,75 @@
1
+ // Putting a file in place in one move.
2
+ //
3
+ // The content is written to a temp file beside where it is going, and the
4
+ // temp file is then renamed over the destination. Whoever reads the path sees
5
+ // the old content or the new, never part of either — and between the two
6
+ // steps there is a moment to do something that must not be left half-done
7
+ // (see installValidatedProgram), or to call the whole thing off.
8
+ import { chmodSync, closeSync, fdatasyncSync, mkdirSync, openSync, renameSync, rmSync, statSync, writeFileSync } from "fs";
9
+ import { randomBytes } from "crypto";
10
+ import { dirname, join } from "path";
11
+ import { guardPath, realRootOf } from "./path-guard.js";
12
+ /** Whether a file name is that of a temp file made here — not content, though it sits among it for a moment. */
13
+ export const isStagedName = (name) => /\.trama-tmp-[0-9a-f]{12}$/.test(name);
14
+ /**
15
+ * A name beside a destination for what is to take its place. It is not made
16
+ * of the destination's own: a name near the longest a directory takes leaves
17
+ * no room to add to.
18
+ */
19
+ export const stagedNameFor = (destination) => join(dirname(destination), `.trama-tmp-${randomBytes(6).toString("hex")}`);
20
+ /**
21
+ * Write `content` beside where `relativePath` under `rootDir` leads, ready to
22
+ * take its place.
23
+ *
24
+ * The destination is where the links on the way point, so a link is written
25
+ * through, not replaced. It must be a file's place: the root itself is
26
+ * refused, since "beside" it is outside it. Directories on the way are
27
+ * created. The temp file is created, never opened: a name that is already
28
+ * there — a link somebody planted, say — is not written through. A file that
29
+ * is being replaced hands on its permissions, as it would if it were written
30
+ * into — unless `mode` says what the new file's are. It is a new file all the
31
+ * same, so a second hard link to the old one, or its owner, are not carried
32
+ * over.
33
+ */
34
+ export function stageFile(rootDir, relativePath, content, mode) {
35
+ const destination = guardPath(rootDir, relativePath);
36
+ if (destination === realRootOf(rootDir)) {
37
+ throw new Error(`Not a file path: "${relativePath}" is the directory itself`);
38
+ }
39
+ mkdirSync(dirname(destination), { recursive: true });
40
+ const staged = stagedNameFor(destination);
41
+ const discard = () => { try {
42
+ rmSync(staged, { force: true });
43
+ }
44
+ catch { /* best-effort */ } };
45
+ const fd = openSync(staged, "wx");
46
+ try {
47
+ try {
48
+ writeFileSync(fd, content);
49
+ fdatasyncSync(fd);
50
+ }
51
+ finally {
52
+ closeSync(fd);
53
+ }
54
+ const permissions = mode ?? statSync(destination, { throwIfNoEntry: false })?.mode;
55
+ if (permissions !== undefined)
56
+ chmodSync(staged, permissions & 0o777);
57
+ }
58
+ catch (err) {
59
+ discard();
60
+ throw err;
61
+ }
62
+ return {
63
+ destination,
64
+ discard,
65
+ commit() {
66
+ try {
67
+ renameSync(staged, destination);
68
+ }
69
+ catch (err) {
70
+ discard();
71
+ throw err;
72
+ }
73
+ },
74
+ };
75
+ }
package/dist/runner.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { RunOptions, TramaConfig, ProgramResult } from "./runner-types.js";
2
- import { type AgentBackend } from "./agent-backend.js";
2
+ import type { AgentBackend } from "./agent-backend.js";
3
3
  /**
4
4
  * Everything a project directory needs around program.ts before it can run:
5
5
  * history/ and logs/, an ESM package.json, .gitignore entries, and links to the
@@ -15,43 +15,15 @@ export declare function ensureProjectScaffold(projectDir: string): void;
15
15
  export declare const RUNTIME_TYPES: string;
16
16
  export declare function loadConfig(): TramaConfig;
17
17
  /** The global config, with the model/provider a project was created with taking precedence. */
18
- export declare function loadProjectConfig(projectDir: string): TramaConfig;
18
+ export declare function loadProjectConfig(projectDir: string, meta?: import("./meta.js").ProjectMeta): TramaConfig;
19
19
  /**
20
20
  * What does the LLM work for a project. The only place that picks a backend:
21
21
  * everything else is handed one and does not know which it is.
22
22
  */
23
23
  export declare function createBackend(config: TramaConfig): AgentBackend;
24
24
  export declare function loadState(projectDir: string): Record<string, unknown>;
25
- /**
26
- * Save the current program.ts as the next version: copy it to history/NNNN.ts
27
- * and record why in index.jsonl.
28
- *
29
- * (See installValidatedProgram for how a create or update is recorded.)
30
- */
25
+ /** Save the current program.ts as the next version of the project, and record why. */
31
26
  export declare function copyToHistory(projectDir: string, reason: string, detail?: string): void;
32
- type DirSnapshot = Map<string, {
33
- size: number;
34
- mtimeMs: number;
35
- }>;
36
- /** Record all files in a directory with their size and mtime for change detection. */
37
- export declare function snapshotDir(dir: string, prefix?: string): DirSnapshot;
38
- /**
39
- * Merge what a run did to a staged copy back into the real directory, with a
40
- * three-way check so that nothing changed in the real directory meanwhile —
41
- * by the user, or by another run of the same project — is overwritten.
42
- *
43
- * @param stagedDir - the copy after the run (source of changes)
44
- * @param realDir - the real directory (destination)
45
- * @param stagedBefore - snapshot of the copy before the run (detects what the run changed)
46
- * @param realSeed - snapshot of the real directory when the copy was taken (detects concurrent changes)
47
- *
48
- * For each file the run touched in the copy:
49
- * - Created: copied, unless that path has meanwhile appeared in the real directory.
50
- * - Modified: copied only if the real file still matches realSeed.
51
- * - Deleted: deleted only if the real file still matches realSeed.
52
- * Best-effort: anything that cannot be merged safely is skipped, never thrown.
53
- */
54
- export declare function commitStagedChanges(stagedDir: string, realDir: string, stagedBefore: DirSnapshot, realSeed: DirSnapshot): void;
55
27
  /** Run fn in a temp directory, clean up afterwards regardless of outcome — including this process being stopped. */
56
28
  export declare function withTempDir<T>(prefix: string, fn: (dir: string) => Promise<T>): Promise<T>;
57
29
  /** The instructions a request for a fix is made under. (Exported so the tests' stand-in LLM can tell one from any other call.) */
@@ -73,10 +45,24 @@ export declare function validateProgram(projectDir: string, backend: AgentBacken
73
45
  maxRepairAttempts: number;
74
46
  maxIterations: number;
75
47
  journalRuns?: number;
48
+ signal?: AbortSignal;
76
49
  }): Promise<{
77
50
  source: string;
78
51
  repairedError?: string;
79
52
  }>;
53
+ /**
54
+ * Why a project's program cannot be given a successor — or null if it can.
55
+ *
56
+ * A program is replaced by a new file taking its place, which the system
57
+ * allows whatever the old file's permissions say. So the old file is asked: a
58
+ * program its owner has made read-only is not replaced. A directory in its
59
+ * place is no program, and no file takes the place of one. And the new file
60
+ * has to be made beside it, in a directory that is there or can be made.
61
+ *
62
+ * This is the rule stageProgram enforces. It is also asked before work whose
63
+ * only use is a new program — an update, a repair — is begun.
64
+ */
65
+ export declare function whyNotReplaceable(projectDir: string): string | null;
80
66
  /**
81
67
  * Make a validated program the project's program.ts and record it as a new
82
68
  * version. A repair made while validating is not a version of its own — it is