@trama-dev/runtime 0.6.1 → 0.7.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 (56) hide show
  1. package/dist/agent-backend.d.ts +51 -0
  2. package/dist/agent-backend.js +4 -0
  3. package/dist/agent-host.d.ts +46 -7
  4. package/dist/agent-host.js +59 -19
  5. package/dist/agent-hosts.js +122 -56
  6. package/dist/agent.d.ts +16 -5
  7. package/dist/agent.js +22 -64
  8. package/dist/bridge-client.d.ts +6 -2
  9. package/dist/bridge-client.js +6 -1
  10. package/dist/client.d.ts +2 -2
  11. package/dist/client.js +126 -2
  12. package/dist/context.js +3 -2
  13. package/dist/history.d.ts +9 -0
  14. package/dist/history.js +98 -0
  15. package/dist/index.d.ts +1 -0
  16. package/dist/index.js +70 -41
  17. package/dist/ipc-server.d.ts +2 -3
  18. package/dist/ipc-server.js +93 -37
  19. package/dist/journal-view.js +19 -9
  20. package/dist/journal.d.ts +10 -4
  21. package/dist/journal.js +28 -6
  22. package/dist/merge-deeds.d.ts +22 -0
  23. package/dist/merge-deeds.js +201 -0
  24. package/dist/merge-doing.d.ts +9 -0
  25. package/dist/merge-doing.js +410 -0
  26. package/dist/merge-settle.d.ts +14 -0
  27. package/dist/merge-settle.js +166 -0
  28. package/dist/merge-shared.d.ts +64 -0
  29. package/dist/merge-shared.js +159 -0
  30. package/dist/merge.d.ts +150 -0
  31. package/dist/merge.js +209 -0
  32. package/dist/model-relay.js +35 -5
  33. package/dist/own-tools.d.ts +60 -0
  34. package/dist/own-tools.js +209 -0
  35. package/dist/path-guard.js +8 -6
  36. package/dist/pi-adapter.d.ts +22 -0
  37. package/dist/pi-adapter.js +147 -46
  38. package/dist/project.d.ts +31 -0
  39. package/dist/project.js +253 -0
  40. package/dist/replace-file.d.ts +13 -2
  41. package/dist/replace-file.js +23 -6
  42. package/dist/runner-backend.d.ts +43 -0
  43. package/dist/runner-backend.js +125 -0
  44. package/dist/runner-types.d.ts +8 -0
  45. package/dist/runner.d.ts +8 -123
  46. package/dist/runner.js +57 -453
  47. package/dist/schema.d.ts +38 -0
  48. package/dist/schema.js +104 -0
  49. package/dist/stopping.js +2 -3
  50. package/dist/tools.js +31 -7
  51. package/dist/tree.d.ts +33 -149
  52. package/dist/tree.js +43 -1077
  53. package/dist/types.d.ts +54 -2
  54. package/dist/workspace.d.ts +20 -0
  55. package/dist/workspace.js +76 -12
  56. package/package.json +2 -2
@@ -8,7 +8,7 @@
8
8
  import { chmodSync, closeSync, fdatasyncSync, mkdirSync, openSync, renameSync, rmSync, statSync, writeFileSync } from "fs";
9
9
  import { randomBytes } from "crypto";
10
10
  import { dirname, join } from "path";
11
- import { guardPath, realRootOf } from "./path-guard.js";
11
+ import { guardPath, makeDirsIn, realRootOf } from "./path-guard.js";
12
12
  /** Whether a file name is that of a temp file made here — not content, though it sits among it for a moment. */
13
13
  export const isStagedName = (name) => /\.trama-tmp-[0-9a-f]{12}$/.test(name);
14
14
  /**
@@ -29,20 +29,29 @@ export const stagedNameFor = (destination) => join(dirname(destination), `.trama
29
29
  * is being replaced hands on its permissions, as it would if it were written
30
30
  * into — unless `mode` says what the new file's are. It is a new file all the
31
31
  * same, so a second hard link to the old one, or its owner, are not carried
32
- * over.
32
+ * over. And while it is being written it is its writer's alone, where it is
33
+ * to have permissions that are handed to it: what takes the place of a file
34
+ * others may not read is not readable by them for the time the writing takes.
35
+ *
36
+ * `makeDirs` makes the directories on the way — with the root itself, unless
37
+ * it is given as what does not (see stageFileInProject).
33
38
  */
34
- export function stageFile(rootDir, relativePath, content, mode) {
39
+ export function stageFile(rootDir, relativePath, content, mode, makeDirs = (dir) => { mkdirSync(dir, { recursive: true }); }) {
35
40
  const destination = guardPath(rootDir, relativePath);
36
41
  if (destination === realRootOf(rootDir)) {
37
42
  throw new Error(`Not a file path: "${relativePath}" is the directory itself`);
38
43
  }
39
- mkdirSync(dirname(destination), { recursive: true });
44
+ makeDirs(dirname(destination));
40
45
  const staged = stagedNameFor(destination);
41
46
  const discard = () => { try {
42
47
  rmSync(staged, { force: true });
43
48
  }
44
49
  catch { /* best-effort */ } };
45
- const fd = openSync(staged, "wx");
50
+ const handedOn = () => mode ?? statSync(destination, { throwIfNoEntry: false })?.mode;
51
+ // (Made closed where it is to be handed permissions, and opened as far as they go once it is whole. One that
52
+ // takes nobody's place and is told nothing is made as any new file is.)
53
+ const first = handedOn();
54
+ const fd = openSync(staged, "wx", first === undefined ? 0o666 : 0o600);
46
55
  try {
47
56
  try {
48
57
  writeFileSync(fd, content);
@@ -51,7 +60,7 @@ export function stageFile(rootDir, relativePath, content, mode) {
51
60
  finally {
52
61
  closeSync(fd);
53
62
  }
54
- const permissions = mode ?? statSync(destination, { throwIfNoEntry: false })?.mode;
63
+ const permissions = handedOn() ?? first;
55
64
  if (permissions !== undefined)
56
65
  chmodSync(staged, permissions & 0o777);
57
66
  }
@@ -73,3 +82,11 @@ export function stageFile(rootDir, relativePath, content, mode) {
73
82
  },
74
83
  };
75
84
  }
85
+ /**
86
+ * stageFile, under a root that is something in itself — a project, which
87
+ * somebody may have moved away since it was named: it must be there, and is
88
+ * not made anew for the sake of what goes into it (see makeDirsIn).
89
+ */
90
+ export function stageFileInProject(projectDir, relativePath, content, mode) {
91
+ return stageFile(projectDir, relativePath, content, mode, (dir) => makeDirsIn(projectDir, dir));
92
+ }
@@ -0,0 +1,43 @@
1
+ import type { AgentBackend } from "./agent-backend.js";
2
+ import { type Meter } from "./budget.js";
3
+ import type { TramaConfig } from "./runner-types.js";
4
+ /**
5
+ * What does the LLM work for a project. The only place that picks a backend:
6
+ * everything else is handed one and does not know which it is.
7
+ *
8
+ * It is also where what is asked of a model is counted, and held to a
9
+ * budget (`meter`): every call passes here, whoever makes it. A call that
10
+ * comes when the budget is spent is refused; one under way is stopped where
11
+ * its agent would ask the model again — what the model was asked before
12
+ * that is used, and what it was answering is let finish. `spent` is told
13
+ * why, the first time either happens.
14
+ */
15
+ export declare function createBackend(config: TramaConfig, meter?: Meter, spent?: (why: string) => void, env?: Record<string, string>): Backend;
16
+ /**
17
+ * Say that a budget is spent, once for the budget — whoever comes up against
18
+ * it first: the run's own backend, or the backend of a project being made
19
+ * for the run's program, of which there may be many.
20
+ */
21
+ export declare const saysOnce: (meter: Meter, say: (why: string) => void) => (why: string) => void;
22
+ /** A backend as the runner holds it: one whose cancelled calls are answered at once, and can be waited for apart. */
23
+ export interface Backend extends AgentBackend {
24
+ /** Why nothing more may be asked of it — the budget it counts in is spent — or nothing, while something may. */
25
+ spent(): string | undefined;
26
+ /** Why a call of it was first refused or stopped for the budget, if one was — whatever the count has come to since. */
27
+ refused(): string | undefined;
28
+ /** Whether what its calls use is held to anything: then what a call it cut off used is to be waited for, recorded or not. */
29
+ counts(): boolean;
30
+ /**
31
+ * End what the backend kept to serve calls with — a process of its own for
32
+ * those that have extensions. Whatever it served is over: a run, a
33
+ * validation. (Asked again after this, it starts anew.)
34
+ */
35
+ close(): Promise<void>;
36
+ /**
37
+ * Resolves when the backend has let go of the calls that were cancelled so
38
+ * far: whoever cancelled one went on at once, and the backend still had to
39
+ * stop what it was doing, and to say what the call had used. They are
40
+ * waited for no longer than anything asked to stop is.
41
+ */
42
+ letGo(): Promise<void>;
43
+ }
@@ -0,0 +1,125 @@
1
+ // The backend as a runner holds it: the one place that picks what does the
2
+ // LLM work, and where every call of a run is counted and held to its budget.
3
+ import { onAbort, rejectOnAbort } from "./abort.js";
4
+ import { createMeter } from "./budget.js";
5
+ import { PiAdapter } from "./pi-adapter.js";
6
+ import { KILL_GRACE_MS } from "./stopping.js";
7
+ import { messageOf } from "./errors.js";
8
+ /**
9
+ * What does the LLM work for a project. The only place that picks a backend:
10
+ * everything else is handed one and does not know which it is.
11
+ *
12
+ * It is also where what is asked of a model is counted, and held to a
13
+ * budget (`meter`): every call passes here, whoever makes it. A call that
14
+ * comes when the budget is spent is refused; one under way is stopped where
15
+ * its agent would ask the model again — what the model was asked before
16
+ * that is used, and what it was answering is let finish. `spent` is told
17
+ * why, the first time either happens.
18
+ */
19
+ export function createBackend(config, meter = createMeter(), spent = () => { }, env) {
20
+ // (`env`: the variables of the run whose calls these are — its agents' too, as they are its program's and its commands'.)
21
+ const backend = new PiAdapter(config, env);
22
+ // The calls that were cancelled.
23
+ const cutOff = new Set();
24
+ // Why a call was first refused, or stopped, for the budget.
25
+ let refused;
26
+ const overBudget = (why) => {
27
+ if (refused === undefined)
28
+ spent(why);
29
+ refused ??= why;
30
+ return new Error(why);
31
+ };
32
+ return {
33
+ spent: () => meter.spent(),
34
+ refused: () => refused,
35
+ counts: () => meter.capped(),
36
+ // The interface promises that a cancelled call rejects, and a run's end
37
+ // waits on that. Every backend is held to it here, rather than trusted with it.
38
+ async ask(prompt, call) {
39
+ let account;
40
+ try {
41
+ account = meter.admit();
42
+ }
43
+ catch (err) {
44
+ throw overBudget(messageOf(err));
45
+ }
46
+ // Stops the call where the budget is reached while it is under way.
47
+ const over = new AbortController();
48
+ let why;
49
+ const signal = call.signal ? AbortSignal.any([call.signal, over.signal]) : over.signal;
50
+ const work = backend.ask(prompt, {
51
+ ...call,
52
+ signal,
53
+ sofar(usage, answered) {
54
+ const reached = account.sofar(usage);
55
+ call.sofar?.(usage, answered);
56
+ // A call that is over keeps what it ended with — its answer, or its own failure: what would be
57
+ // asked beside it is not asked, and that is all. (It is a request refused for the budget all the
58
+ // same, and said as one.)
59
+ if (answered) {
60
+ const spent = why ?? reached;
61
+ if (spent === undefined)
62
+ return true;
63
+ overBudget(spent);
64
+ return false;
65
+ }
66
+ // (Once it is to stop, it is to stop — whatever is said of it after.)
67
+ why ??= reached;
68
+ if (why === undefined)
69
+ return true;
70
+ over.abort();
71
+ return false;
72
+ },
73
+ // (What the call has used is counted from when it is known: nothing is stopped for it here — a reply that
74
+ // has come is not taken back — but whatever is asked next, by this call or another, is asked against it.)
75
+ had(usage) {
76
+ account.sofar(usage);
77
+ call.had?.(usage);
78
+ },
79
+ used(usage) {
80
+ account.sofar(usage);
81
+ call.used?.(usage);
82
+ },
83
+ });
84
+ const off = onAbort(signal, () => { cutOff.add(work); });
85
+ // What the call used is counted as its own until the backend has let go of it — which
86
+ // is when it has said the last of it — however long ago whoever asked was answered.
87
+ const settled = () => {
88
+ off();
89
+ account.close();
90
+ // (Let go of: nobody need wait for it any more.)
91
+ cutOff.delete(work);
92
+ };
93
+ work.then(settled, settled);
94
+ try {
95
+ return await rejectOnAbort(work, signal);
96
+ }
97
+ catch (err) {
98
+ // (Stopped by whoever asked, it was stopped by them, whatever the budget had come to.)
99
+ throw why !== undefined && !call.signal?.aborted ? overBudget(why) : err;
100
+ }
101
+ },
102
+ close: () => backend.close(),
103
+ async letGo() {
104
+ let giveUp;
105
+ await Promise.race([
106
+ Promise.allSettled(cutOff),
107
+ new Promise(resolve => { giveUp = setTimeout(resolve, KILL_GRACE_MS); }),
108
+ ]);
109
+ clearTimeout(giveUp);
110
+ },
111
+ };
112
+ }
113
+ /** The budgets of which it has been said that they are spent. */
114
+ const saidSpent = new WeakSet();
115
+ /**
116
+ * Say that a budget is spent, once for the budget — whoever comes up against
117
+ * it first: the run's own backend, or the backend of a project being made
118
+ * for the run's program, of which there may be many.
119
+ */
120
+ export const saysOnce = (meter, say) => (why) => {
121
+ if (saidSpent.has(meter))
122
+ return;
123
+ saidSpent.add(meter);
124
+ say(why);
125
+ };
@@ -54,6 +54,14 @@ export interface RunOptions {
54
54
  }
55
55
  /** A run as one program asks its runner for it: everything in RunOptions that can cross the bridge. */
56
56
  export type RunRequest = Omit<RunOptions, "signal">;
57
+ /** A project a program asks its runner to make, or to change (see createProject and updateProject). */
58
+ export interface MakeRequest {
59
+ kind: "create" | "update";
60
+ name: string;
61
+ prompt: string;
62
+ model?: string;
63
+ args?: Record<string, unknown>;
64
+ }
57
65
  export interface TramaConfig extends ModelConfig {
58
66
  maxRepairAttempts: number;
59
67
  defaultTimeout: number;
package/dist/runner.d.ts CHANGED
@@ -1,129 +1,22 @@
1
- import type { RunOptions, TramaConfig, ProgramResult } from "./runner-types.js";
2
- import type { AgentBackend, AgentUsage } from "./agent-backend.js";
1
+ import type { MakeRequest, RunOptions, ProgramResult } from "./runner-types.js";
2
+ import type { AgentUsage } from "./agent-backend.js";
3
3
  import { type Budget, type Meter } from "./budget.js";
4
- import { type Tree, type Unmerged } from "./tree.js";
5
- /**
6
- * Everything a project directory needs around program.ts before it can run:
7
- * history/ and logs/, an ESM package.json, .gitignore entries, and links to the
8
- * @trama-dev packages it can import.
9
- * Idempotent — called before every run, so a freshly cloned project just works.
10
- */
11
- export declare function ensureProjectScaffold(projectDir: string): void;
4
+ import { type Unmerged } from "./merge.js";
5
+ import { type Backend } from "./runner-backend.js";
12
6
  /**
13
7
  * The program-facing API, as shown to the LLM when it writes or repairs a
14
8
  * program. This is the declaration file generated from types.ts, so the prompt
15
9
  * cannot drift from the real API — doc comments included.
16
10
  */
17
11
  export declare const RUNTIME_TYPES: string;
18
- export declare function loadConfig(): TramaConfig;
19
- /** The global config, with the model/provider a project was created with taking precedence. */
20
- export declare function loadProjectConfig(projectDir: string, meta?: import("./meta.js").ProjectMeta): TramaConfig;
21
- /**
22
- * What does the LLM work for a project. The only place that picks a backend:
23
- * everything else is handed one and does not know which it is.
24
- *
25
- * It is also where what is asked of a model is counted, and held to a
26
- * budget (`meter`): every call passes here, whoever makes it. A call that
27
- * comes when the budget is spent is refused; one under way is stopped where
28
- * its agent would ask the model again — what the model was asked before
29
- * that is used, and what it was answering is let finish. `spent` is told
30
- * why, the first time either happens.
31
- */
32
- export declare function createBackend(config: TramaConfig, meter?: Meter, spent?: (why: string) => void, env?: Record<string, string>): Backend;
33
- /**
34
- * Say that a budget is spent, once for the budget — whoever comes up against
35
- * it first: the run's own backend, or the backend of a project being made
36
- * for the run's program, of which there may be many.
37
- */
38
- export declare const saysOnce: (meter: Meter, say: (why: string) => void) => (why: string) => void;
39
- /** A backend as the runner holds it: one whose cancelled calls are answered at once, and can be waited for apart. */
40
- export interface Backend extends AgentBackend {
41
- /** Why nothing more may be asked of it — the budget it counts in is spent — or nothing, while something may. */
42
- spent?(): string | undefined;
43
- /** Why a call of it was first refused or stopped for the budget, if one was — whatever the count has come to since. */
44
- refused?(): string | undefined;
45
- /** Whether what its calls use is held to anything: then what a call it cut off used is to be waited for, recorded or not. */
46
- counts?(): boolean;
47
- /**
48
- * End what the backend kept to serve calls with — a process of its own for
49
- * those that have extensions. Whatever it served is over: a run, a
50
- * validation. (Asked again after this, it starts anew.)
51
- */
52
- close?(): Promise<void>;
53
- /**
54
- * Resolves when the backend has let go of the calls that were cancelled so
55
- * far: whoever cancelled one went on at once, and the backend still had to
56
- * stop what it was doing, and to say what the call had used. They are
57
- * waited for no longer than anything asked to stop is.
58
- */
59
- letGo(): Promise<void>;
60
- }
61
- export declare function loadState(projectDir: string): Record<string, unknown>;
62
- /** Save the current program.ts as the next version of the project, and record why. */
63
- export declare function copyToHistory(projectDir: string, reason: string, detail?: string): void;
64
12
  /** Run fn in a temp directory, clean up afterwards regardless of outcome — including this process being stopped. */
65
13
  export declare function withTempDir<T>(prefix: string, fn: (dir: string) => Promise<T>): Promise<T>;
66
14
  /**
67
- * Have what is beside a project be beside a copy of it: for each name in the
68
- * directory the project is in, a link of that name, to it, in the directory
69
- * the copy is in. So ".." leads from the copy to what it leads to from the
70
- * project — a file beside it that its program reads, a module it imports
71
- * from there, a project beside it that it runs.
72
- *
73
- * What is come to that way is the real thing, not a copy: what is done to it
74
- * is done. (So is what it comes to in turn — and from a directory beside the
75
- * project, ".." is the directory the project is in: by its name from there,
76
- * the project itself, and not the copy.) One step up is all there is: what
77
- * the directory the project is in is itself beside is not beside the copy.
78
- * And it is as much as can be found out: a directory that may not be listed
79
- * has nothing told of it, and a name the copy's own directory will not have
80
- * beside the copy is left out — the project's own, which is the copy's
81
- * there, before any.
82
- *
83
- * Returns the names put there, and the directory they are names in. A name
84
- * is not the thing: one that is made beside the copy, or taken away there,
85
- * is made or taken away in a directory that goes with the copy, and not
86
- * beside the project (see strandedBeside).
15
+ * How a run that failed is said to whoever asked for it: as a program's
16
+ * failure — which the reasons that begin so say for themselves, and is not
17
+ * said of them twice.
87
18
  */
88
- interface Beside {
89
- among: string;
90
- names: string[];
91
- }
92
- /**
93
- * Make, in `privateDir`, a copy of a project — to try a program in, or for an
94
- * agent to look at and work in: what the project is and holds (see copyTree),
95
- * with `program` as its program.ts where one is given. Returns where the
96
- * copy is, what the project was when it was copied, and the names put beside
97
- * the copy for what is beside the project.
98
- *
99
- * The copy allows what the project allows, to everybody: so it is made in a
100
- * directory that is this runner's alone, and is nobody else's to come to. It
101
- * has the project's name there, and what is beside the project beside it (see
102
- * standAmong) — nothing else is to be in that directory. Where what is
103
- * copied is itself a copy of a project — as where a candidate is validated
104
- * in the project as an agent prepared it — `among` names the project: what
105
- * is beside the copy is what is beside that, and nothing an agent made
106
- * beside its own.
107
- *
108
- * The project's journals are not carried over: they are the record of its
109
- * runs, not part of what a run works on, and a copy is where a run is tried.
110
- *
111
- * In everything else the copy is as the project is, so that what a program
112
- * can do in the one it can do in the other: what a run did in a copy is
113
- * merged back into the project, and must not have been possible only there.
114
- * The one exception is the program: the one to be tried, in the place of
115
- * the project's. It allows what the project's allows — and its owner to
116
- * replace it — and is put there whatever the directory it goes in allows:
117
- * a copy is where programs are tried and replaced, and whether the project's
118
- * own may be is said of the project, where one is to take its place (see
119
- * whyNotReplaceable). A project with a directory where its program should be
120
- * has no place for one, and is not copied to try one in.
121
- */
122
- export declare function copyProjectWith(projectDir: string, privateDir: string, program?: string, among?: string): {
123
- copy: string;
124
- held: Tree;
125
- beside: Beside;
126
- };
19
+ export declare const failureOf: (reason: string | undefined) => string;
127
20
  /** A line as it is shown for a run: under the run's name, if it has one. */
128
21
  export declare const tagged: (label: string | undefined, line: string) => string;
129
22
  /** The instructions a request for a fix is made under. (Exported so the tests' stand-in LLM can tell one from any other call.) */
@@ -232,14 +125,6 @@ export declare function installValidatedProgram(projectDir: string, validated: {
232
125
  source: string;
233
126
  repairedError?: string;
234
127
  }, reason: "create" | "update", prompt: string): void;
235
- /** A project a program asks its runner to make, or to change (see createProject and updateProject). */
236
- export interface MakeRequest {
237
- kind: "create" | "update";
238
- name: string;
239
- prompt: string;
240
- model?: string;
241
- args?: Record<string, unknown>;
242
- }
243
128
  /**
244
129
  * What a project is made or changed under where a run's program asked for
245
130
  * it: that run's — stopped with it, counted in its budget, shown under its