@trama-dev/runtime 0.3.1 → 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 (47) hide show
  1. package/README.md +4 -3
  2. package/dist/abort.d.ts +12 -0
  3. package/dist/abort.js +31 -0
  4. package/dist/agent-backend.d.ts +0 -6
  5. package/dist/agent-backend.js +1 -21
  6. package/dist/agent.d.ts +16 -1
  7. package/dist/agent.js +108 -35
  8. package/dist/bridge-client.js +44 -14
  9. package/dist/child-process.d.ts +1 -20
  10. package/dist/child-process.js +11 -86
  11. package/dist/client.d.ts +2 -1
  12. package/dist/client.js +5 -15
  13. package/dist/context.d.ts +36 -7
  14. package/dist/context.js +83 -92
  15. package/dist/errors.d.ts +2 -0
  16. package/dist/errors.js +2 -0
  17. package/dist/index.d.ts +12 -1
  18. package/dist/index.js +93 -53
  19. package/dist/ipc-server.d.ts +12 -6
  20. package/dist/ipc-server.js +124 -136
  21. package/dist/journal-view.d.ts +9 -0
  22. package/dist/journal-view.js +139 -0
  23. package/dist/journal.d.ts +70 -0
  24. package/dist/journal.js +454 -0
  25. package/dist/meta.d.ts +18 -0
  26. package/dist/meta.js +29 -0
  27. package/dist/path-guard.d.ts +128 -19
  28. package/dist/path-guard.js +384 -68
  29. package/dist/pi-adapter.d.ts +7 -6
  30. package/dist/pi-adapter.js +15 -26
  31. package/dist/replace-file.d.ts +31 -0
  32. package/dist/replace-file.js +75 -0
  33. package/dist/runner-types.d.ts +2 -0
  34. package/dist/runner.d.ts +37 -37
  35. package/dist/runner.js +565 -239
  36. package/dist/serializable.d.ts +10 -4
  37. package/dist/serializable.js +56 -31
  38. package/dist/{watchdog.d.ts → stopping.d.ts} +15 -0
  39. package/dist/stopping.js +268 -0
  40. package/dist/tools.js +44 -13
  41. package/dist/tree.d.ts +234 -0
  42. package/dist/tree.js +1409 -0
  43. package/dist/types.d.ts +6 -3
  44. package/dist/workspace.d.ts +7 -0
  45. package/dist/workspace.js +93 -104
  46. package/package.json +2 -2
  47. package/dist/watchdog.js +0 -167
@@ -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
+ }
@@ -37,4 +37,6 @@ export interface TramaConfig extends ModelConfig {
37
37
  maxRepairAttempts: number;
38
38
  defaultTimeout: number;
39
39
  defaultMaxIterations: number;
40
+ /** How many runs of a project keep their journal. 0 records nothing. */
41
+ journalRuns: number;
40
42
  }
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,50 +15,23 @@ 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.) */
58
30
  export declare const REPAIR_SYSTEM_PROMPT = "You are a TypeScript repair tool.";
59
31
  /**
60
- * Validate a candidate program.ts for a project without touching the project:
61
- * run it (with repair) in a throwaway copy. Returns the program that worked —
32
+ * Validate a candidate program.ts for a project without touching what the
33
+ * project holds: run it (with repair) in a throwaway copy. The one thing left
34
+ * in the project is the journal of the validation. Returns the program that worked —
62
35
  * the candidate itself, or a repaired version of it, with the failure that was
63
36
  * repaired. Throws if it cannot be made to run.
64
37
  *
@@ -71,10 +44,25 @@ export declare function validateProgram(projectDir: string, backend: AgentBacken
71
44
  timeout: number;
72
45
  maxRepairAttempts: number;
73
46
  maxIterations: number;
47
+ journalRuns?: number;
48
+ signal?: AbortSignal;
74
49
  }): Promise<{
75
50
  source: string;
76
51
  repairedError?: string;
77
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;
78
66
  /**
79
67
  * Make a validated program the project's program.ts and record it as a new
80
68
  * version. A repair made while validating is not a version of its own — it is
@@ -84,13 +72,25 @@ export declare function installValidatedProgram(projectDir: string, validated: {
84
72
  source: string;
85
73
  repairedError?: string;
86
74
  }, reason: "create" | "update", prompt: string): void;
75
+ /** What a run is told when a program asked for it, rather than a person or a host. */
76
+ interface AskedFor {
77
+ /** How many runs deep it is: one more than the run that asked. */
78
+ depth: number;
79
+ /** The call that asked for it: which run's, and which of that run's calls. */
80
+ parent: {
81
+ run: string;
82
+ journal: string;
83
+ seq: number;
84
+ };
85
+ /** Called once this run has a journal of its own. */
86
+ started(run: string, journal: string): void;
87
+ }
87
88
  /**
88
89
  * Run a project, in this process: this is what a runner does.
89
90
  *
90
- * `depth` is how many runs deep this one is — 0 unless a program asked for it
91
- * (see the "/run/program" call), in which case it is one more than that
92
- * program's own. One runner carries the whole tree, so it is also where the
91
+ * `askedFor` is set when a program asked for the run (see the "/run/program"
92
+ * call). One runner carries the whole tree of runs, so it is also where the
93
93
  * tree is kept from growing without bound.
94
94
  */
95
- export declare function runProgram(options: RunOptions, depth?: number): Promise<ProgramResult>;
95
+ export declare function runProgram(options: RunOptions, askedFor?: AskedFor): Promise<ProgramResult>;
96
96
  export {};