@trama-dev/runtime 0.1.4 → 0.2.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/runner.d.ts CHANGED
@@ -1,31 +1,62 @@
1
- import type { RunOptions, TramaConfig } from "./types.js";
1
+ import type { RunOptions, TramaConfig, ProgramResult } from "./runner-types.js";
2
2
  import { PiAdapter } from "./pi-adapter.js";
3
- export declare const PI_VERSION: string;
4
3
  /**
5
- * Ensure project has ESM package.json and .gitignore.
6
- * If package.json exists but lacks "type": "module", patches it in.
4
+ * Everything a project directory needs around program.ts before it can run:
5
+ * history/ and logs/, an ESM package.json, .gitignore entries, and links to the
6
+ * @trama-dev packages it can import.
7
+ * Idempotent — called before every run, so a freshly cloned project just works.
7
8
  */
8
9
  export declare function ensureProjectScaffold(projectDir: string): void;
9
10
  /**
10
- * Simplified API reference for LLM system prompts.
11
- * NOT the canonical type definitions (those live in types.ts).
12
- * Kept as a readable string rather than auto-generated, because the LLM
13
- * needs a concise reference, not the full implementation types.
14
- * If types.ts changes, review whether this summary needs updating.
11
+ * The program-facing API, as shown to the LLM when it writes or repairs a
12
+ * program. This is the declaration file generated from types.ts, so the prompt
13
+ * cannot drift from the real API — doc comments included.
15
14
  */
16
- export declare const RUNTIME_TYPES = "// @trama-dev/runtime - Complete API\n\nexport interface Ctx {\n input: { prompt: string; args: Record<string, unknown> };\n state: Record<string, unknown>;\n iteration: number;\n maxIterations: number;\n log(message: string, data?: Record<string, unknown>): Promise<void>;\n ready(data?: Record<string, unknown>): Promise<void>;\n checkpoint(): Promise<void>;\n done(result?: Record<string, unknown>): Promise<void>;\n}\n\nexport interface Agent {\n instruct(prompt: string, options?: { system?: string }): Promise<string>;\n /** @deprecated Use instruct() instead */\n ask(prompt: string, options?: { system?: string }): Promise<string>;\n generate<T>(input: {\n prompt: string;\n schema: Record<string, string>;\n system?: string;\n }): Promise<T>;\n}\n\nexport interface ShellResult {\n exitCode: number;\n stdout: string;\n stderr: string;\n}\n\nexport interface Tools {\n read(path: string): Promise<string>;\n write(path: string, content: string): Promise<void>;\n shell(command: string, options?: { cwd?: string; timeout?: number }): Promise<ShellResult>;\n fetch(url: string, options?: {\n method?: string;\n headers?: Record<string, string>;\n body?: string;\n }): Promise<{ status: number; body: string; headers: Record<string, string> }>;\n}\n\nexport declare const ctx: Ctx;\nexport declare const agent: Agent;\nexport declare const tools: Tools;";
15
+ export declare const RUNTIME_TYPES: string;
17
16
  export declare function loadConfig(): TramaConfig;
17
+ /** The global config, with the model/provider a project was created with taking precedence. */
18
+ export declare function loadProjectConfig(projectDir: string): TramaConfig;
18
19
  export declare function loadState(projectDir: string): Record<string, unknown>;
19
20
  /**
20
- * Copy current program.ts to history/NNNN.ts and append to index.jsonl.
21
+ * Save the current program.ts as the next version: copy it to history/NNNN.ts
22
+ * and record why in index.jsonl.
21
23
  *
22
- * WARNING: Do NOT use this for create-time repair attempts.
23
- * Create-time repairs go to index.jsonl only (reason: "create-repair").
24
+ * Repairs made while validating a create or update are NOT versions of their
25
+ * own — they are noted in index.jsonl only (reason "create-repair" /
26
+ * "update-repair"), and the validated program becomes the create/update version.
24
27
  */
25
28
  export declare function copyToHistory(projectDir: string, reason: string, detail?: string): void;
29
+ type DirSnapshot = Map<string, {
30
+ size: number;
31
+ mtimeMs: number;
32
+ }>;
33
+ /** Record all files in a directory with their size and mtime for change detection. */
34
+ export declare function snapshotDir(dir: string, prefix?: string): DirSnapshot;
26
35
  /**
27
- * Snapshot a directory, run fn, restore from snapshot if fn returns false
28
- * or if fn throws. Always cleans up the snapshot. Returns whatever fn returns.
36
+ * Commit workspace changes from a temp workspace back to the real workspace,
37
+ * using a three-way check to avoid overwriting concurrent updates by other participants.
38
+ *
39
+ * @param tempDir - temp workspace after the run (source of changes)
40
+ * @param realDir - real shared workspace (destination)
41
+ * @param tempBefore - temp workspace snapshot before the run (detects what the run changed)
42
+ * @param realSeed - real workspace snapshot at seed time (detects concurrent changes)
43
+ *
44
+ * For each file the run created or modified in the temp workspace:
45
+ * - New file: copied unless that path has meanwhile appeared in the real workspace.
46
+ * - Modified file: copied only if the real workspace's copy still matches realSeed.
47
+ * If it doesn't, another participant changed or removed it concurrently — skip.
48
+ * Best-effort: anything that cannot be committed safely is skipped, never thrown.
49
+ */
50
+ export declare function commitWorkspaceChanges(tempDir: string, realDir: string, tempBefore: DirSnapshot, realSeed: DirSnapshot): void;
51
+ /**
52
+ * Run fn against a directory that can be put back exactly as it was: if fn
53
+ * returns keep:false or throws, the directory is rolled back. Returns fn's value.
54
+ *
55
+ * The snapshot is a hidden copy next to the directory, on the same file
56
+ * system, so rolling back is two renames — move the directory aside, move the
57
+ * snapshot into its place — and at no point does the content exist nowhere.
58
+ * (If the process is killed outright, the snapshot is left behind beside the
59
+ * directory: it is the pre-change state.)
29
60
  */
30
61
  export declare function withSnapshot<T>(dir: string, fn: () => Promise<{
31
62
  keep: boolean;
@@ -33,10 +64,16 @@ export declare function withSnapshot<T>(dir: string, fn: () => Promise<{
33
64
  }>): Promise<T>;
34
65
  /** Run fn in a temp directory, clean up afterwards regardless of outcome. */
35
66
  export declare function withTempDir<T>(prefix: string, fn: (dir: string) => Promise<T>): Promise<T>;
36
- export declare function resolveSmokeTimeout(timeout?: number): number;
37
- export declare function smokeRunAndRepair(projectDir: string, adapter: PiAdapter, reason: "create" | "update", options?: {
38
- timeout?: number;
39
- maxRepairAttempts?: number;
40
- maxIterations?: number;
67
+ /**
68
+ * Validate a freshly generated program.ts by running it (with repair) in a
69
+ * throwaway copy of the project, so the validation run's file side effects
70
+ * don't persist. On success the validated — possibly repaired — program is
71
+ * copied back. Throws if it cannot be made to run.
72
+ */
73
+ export declare function smokeRunAndRepair(projectDir: string, adapter: PiAdapter, reason: "create" | "update", options: {
74
+ timeout: number;
75
+ maxRepairAttempts: number;
76
+ maxIterations: number;
41
77
  }): Promise<void>;
42
- export declare function runProgram(options: RunOptions): Promise<void>;
78
+ export declare function runProgram(options: RunOptions): Promise<ProgramResult>;
79
+ export {};