@aitoroses/telar 0.0.0 → 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.
Files changed (94) hide show
  1. package/README.md +457 -3
  2. package/dist/compile.d.ts +48 -0
  3. package/dist/compile.js +1240 -0
  4. package/dist/contract.d.ts +109 -0
  5. package/dist/contract.js +491 -0
  6. package/dist/evaluator.d.ts +23 -0
  7. package/dist/evaluator.js +40 -0
  8. package/dist/evidence.d.ts +23 -0
  9. package/dist/evidence.js +74 -0
  10. package/dist/guide.d.ts +8 -0
  11. package/dist/guide.js +16 -0
  12. package/dist/install.d.ts +104 -0
  13. package/dist/install.js +402 -0
  14. package/dist/iso.d.ts +10 -0
  15. package/dist/iso.js +73 -0
  16. package/dist/node/build.d.ts +2 -0
  17. package/dist/node/build.js +56 -0
  18. package/dist/node/telar-module.d.ts +2 -0
  19. package/dist/node/telar-module.js +28 -0
  20. package/dist/observer.d.ts +38 -0
  21. package/dist/observer.js +53 -0
  22. package/dist/pieces.d.ts +230 -0
  23. package/dist/pieces.js +178 -0
  24. package/dist/powers.d.ts +46 -0
  25. package/dist/powers.js +126 -0
  26. package/dist/protocol.d.ts +220 -0
  27. package/dist/protocol.js +243 -0
  28. package/dist/query.d.ts +67 -0
  29. package/dist/query.js +102 -0
  30. package/dist/read.d.ts +37 -0
  31. package/dist/read.js +27 -0
  32. package/dist/runner.d.ts +113 -0
  33. package/dist/runner.js +134 -0
  34. package/dist/scenario/builder.d.ts +25 -0
  35. package/dist/scenario/builder.js +603 -0
  36. package/dist/scenario/templates.d.ts +35 -0
  37. package/dist/scenario/templates.js +64 -0
  38. package/dist/scenario/types.d.ts +432 -0
  39. package/dist/scenario/types.js +10 -0
  40. package/dist/scenario/validate.d.ts +7 -0
  41. package/dist/scenario/validate.js +19 -0
  42. package/dist/scenario.d.ts +4 -0
  43. package/dist/scenario.js +4 -0
  44. package/dist/schema.d.ts +221 -0
  45. package/dist/schema.js +227 -0
  46. package/dist/seating.d.ts +47 -0
  47. package/dist/seating.js +26 -0
  48. package/dist/send-hook.d.ts +46 -0
  49. package/dist/send-hook.js +123 -0
  50. package/dist/show.d.ts +34 -0
  51. package/dist/show.js +1 -0
  52. package/dist/source.d.ts +85 -0
  53. package/dist/source.js +104 -0
  54. package/dist/spec.d.ts +21 -0
  55. package/dist/spec.js +18 -0
  56. package/dist/standard-schema.d.ts +21 -0
  57. package/dist/standard-schema.js +1 -0
  58. package/dist/std/accuracy.d.ts +12 -0
  59. package/dist/std/accuracy.js +122 -0
  60. package/dist/std/ack.d.ts +14 -0
  61. package/dist/std/ack.js +43 -0
  62. package/dist/std/actions.d.ts +105 -0
  63. package/dist/std/actions.js +153 -0
  64. package/dist/std/brief.d.ts +36 -0
  65. package/dist/std/brief.js +78 -0
  66. package/dist/std/claude-code.d.ts +9 -0
  67. package/dist/std/claude-code.js +113 -0
  68. package/dist/std/compare.d.ts +29 -0
  69. package/dist/std/compare.js +36 -0
  70. package/dist/std/git-workspace.d.ts +44 -0
  71. package/dist/std/git-workspace.js +184 -0
  72. package/dist/std/judgment.d.ts +85 -0
  73. package/dist/std/judgment.js +462 -0
  74. package/dist/std/observe-command.d.ts +23 -0
  75. package/dist/std/observe-command.js +151 -0
  76. package/dist/std/observe-github-checks.d.ts +24 -0
  77. package/dist/std/observe-github-checks.js +289 -0
  78. package/dist/std/observe-http-status.d.ts +27 -0
  79. package/dist/std/observe-http-status.js +193 -0
  80. package/dist/std/plan.d.ts +7 -0
  81. package/dist/std/plan.js +28 -0
  82. package/dist/std/queries.d.ts +31 -0
  83. package/dist/std/queries.js +318 -0
  84. package/dist/std/reading.d.ts +17 -0
  85. package/dist/std/reading.js +88 -0
  86. package/dist/std/schedule.d.ts +19 -0
  87. package/dist/std/schedule.js +122 -0
  88. package/dist/std.d.ts +15 -0
  89. package/dist/std.js +15 -0
  90. package/dist/types.d.ts +370 -0
  91. package/dist/types.js +1 -0
  92. package/dist/values.d.ts +81 -0
  93. package/dist/values.js +129 -0
  94. package/package.json +62 -5
@@ -0,0 +1,184 @@
1
+ import { parseIsoUtc } from "../iso.js";
2
+ export class GitWorkspaceError extends Error {
3
+ name = "GitWorkspaceError";
4
+ code;
5
+ constructor(code, message) {
6
+ super(message);
7
+ this.code = code;
8
+ }
9
+ }
10
+ const SUBCOMMANDS = [
11
+ "init",
12
+ "config",
13
+ "fetch",
14
+ "hash-object",
15
+ "update-ref",
16
+ "cat-file",
17
+ "worktree",
18
+ "rev-parse",
19
+ "check-ref-format",
20
+ ];
21
+ /** The process grants `gitWorkspace` needs, one `git` subcommand each. */
22
+ export const GIT_WORKSPACE_GRANT = SUBCOMMANDS.map((sub) => ({
23
+ executable: "git",
24
+ argsPrefix: [sub],
25
+ }));
26
+ export const GIT_WORKSPACE_LOCK_REF = "refs/telar-workspace/lock";
27
+ export const GIT_WORKSPACE_LOCK_STALE_MS = 10 * 60 * 1000;
28
+ const REMOTE_REFSPEC = "+refs/heads/*:refs/remotes/origin/*";
29
+ const isSegment = (value) => /^[A-Za-z0-9._-]+$/.test(value) && value !== "." && value !== ".." && !value.startsWith("-");
30
+ function segment(what, value) {
31
+ if (!isSegment(value)) {
32
+ throw new GitWorkspaceError("INVALID_SEGMENT", `${what} ${JSON.stringify(value)} must match [A-Za-z0-9._-]+, not start with "-", and not be "." or ".."`);
33
+ }
34
+ return value;
35
+ }
36
+ function noOption(what, value) {
37
+ if (value.length === 0 || value.startsWith("-")) {
38
+ throw new GitWorkspaceError("INVALID_REF", `${what} ${JSON.stringify(value)} must be non-empty and not start with "-"`);
39
+ }
40
+ return value;
41
+ }
42
+ function parseLock(text) {
43
+ try {
44
+ const v = JSON.parse(text);
45
+ if (typeof v !== "object" || v === null)
46
+ return undefined;
47
+ const seat = Reflect.get(v, "seat");
48
+ const at = Reflect.get(v, "at");
49
+ return typeof seat === "string" && typeof at === "string" ? { seat, at } : undefined;
50
+ }
51
+ catch {
52
+ return undefined;
53
+ }
54
+ }
55
+ function isStale(lock, now) {
56
+ const held = lock === undefined ? undefined : parseIsoUtc(lock.at);
57
+ const at = parseIsoUtc(now);
58
+ return held === undefined || at === undefined || at - held > GIT_WORKSPACE_LOCK_STALE_MS;
59
+ }
60
+ function parseWorktrees(porcelain) {
61
+ return porcelain
62
+ .split("\n\n")
63
+ .map((block) => block.split("\n"))
64
+ .flatMap((lines) => {
65
+ const path = lines.find((l) => l.startsWith("worktree "))?.slice("worktree ".length);
66
+ const branch = lines.find((l) => l.startsWith("branch "))?.slice("branch ".length);
67
+ return path === undefined ? [] : [{ path, branch }];
68
+ });
69
+ }
70
+ /** Gives each seat its own worktree of one or more repos, under `roots`, through `powers.process` and `git` only. */
71
+ export function gitWorkspace(powers, roots) {
72
+ let lastAt = "";
73
+ const run = (cwd, sub, args, stdin) => {
74
+ const out = powers.process({
75
+ executable: "git",
76
+ args: [sub, ...args],
77
+ cwd,
78
+ ...(stdin !== undefined ? { stdin } : {}),
79
+ });
80
+ lastAt = out.startedAt;
81
+ return out;
82
+ };
83
+ const git = (cwd, sub, args, stdin) => {
84
+ const out = run(cwd, sub, args, stdin);
85
+ if (out.exitCode !== 0) {
86
+ throw new GitWorkspaceError("GIT_COMMAND_FAILED", `git ${sub} failed (exit ${String(out.exitCode)}): ${out.stderr.trim()}`);
87
+ }
88
+ return out.stdout.trim();
89
+ };
90
+ const isBranch = (value) => {
91
+ const out = run(roots.repos, "check-ref-format", ["--branch", value]);
92
+ return out.exitCode === 0 && out.stdout.trim() === value;
93
+ };
94
+ const branchName = (what, value) => {
95
+ const candidate = noOption(what, value);
96
+ if (!isBranch(candidate)) {
97
+ throw new GitWorkspaceError("INVALID_REF", `${what} ${JSON.stringify(value)} is not a branch name`);
98
+ }
99
+ return candidate;
100
+ };
101
+ const repoDir = (id) => `${roots.repos}/${id}.git`;
102
+ const worktreeDir = (seat, id) => `${roots.workspaces}/${seat}/${id}`;
103
+ const ensureRepo = (id, url) => {
104
+ git(roots.repos, "init", ["--quiet", "--bare", "--", `${id}.git`]);
105
+ git(repoDir(id), "config", ["--", "remote.origin.url", url]);
106
+ git(repoDir(id), "config", ["--", "remote.origin.fetch", REMOTE_REFSPEC]);
107
+ };
108
+ const acquire = (dir, seat) => {
109
+ let failure = "";
110
+ for (let attempt = 0; attempt < 2; attempt += 1) {
111
+ const blob = git(dir, "hash-object", ["-w", "--stdin"], JSON.stringify({ seat, at: lastAt }));
112
+ const created = run(dir, "update-ref", [GIT_WORKSPACE_LOCK_REF, blob, ""]);
113
+ if (created.exitCode === 0)
114
+ return blob;
115
+ failure = created.stderr.trim();
116
+ const held = run(dir, "rev-parse", ["--verify", "--quiet", GIT_WORKSPACE_LOCK_REF]);
117
+ if (held.exitCode !== 0)
118
+ continue;
119
+ const holder = held.stdout.trim();
120
+ const lock = parseLock(git(dir, "cat-file", ["blob", holder]));
121
+ if (!isStale(lock, lastAt)) {
122
+ throw new GitWorkspaceError("WORKSPACE_BUSY", `${dir} is fetching for seat ${lock?.seat ?? "?"} since ${lock?.at ?? "?"}; call again on the next pass`);
123
+ }
124
+ run(dir, "update-ref", ["-d", GIT_WORKSPACE_LOCK_REF, holder]);
125
+ }
126
+ throw new GitWorkspaceError("GIT_COMMAND_FAILED", `git update-ref failed: ${failure}`);
127
+ };
128
+ const fetch = (id, seat) => {
129
+ const dir = repoDir(id);
130
+ const blob = acquire(dir, seat);
131
+ try {
132
+ git(dir, "fetch", ["--quiet", "--prune", "origin"]);
133
+ }
134
+ finally {
135
+ run(dir, "update-ref", ["-d", GIT_WORKSPACE_LOCK_REF, blob]);
136
+ }
137
+ };
138
+ const worktreeOf = (id, seat, branch) => {
139
+ const suffix = `/${seat}/${id}`;
140
+ return parseWorktrees(git(repoDir(id), "worktree", ["list", "--porcelain"])).find((w) => w.path.endsWith(suffix) && (branch === undefined || w.branch === `refs/heads/${branch}`));
141
+ };
142
+ const ensureWorktree = (id, seat, branch, base) => {
143
+ const dir = repoDir(id);
144
+ git(dir, "worktree", ["prune"]);
145
+ if (worktreeOf(id, seat, branch) !== undefined)
146
+ return;
147
+ const path = worktreeDir(seat, id);
148
+ const exists = run(dir, "rev-parse", ["--verify", "--quiet", `refs/heads/${branch}`]).exitCode === 0;
149
+ git(dir, "worktree", exists
150
+ ? ["add", "--quiet", "--", path, branch]
151
+ : ["add", "--quiet", "-b", branch, "--", path, `origin/${base}`]);
152
+ };
153
+ const isRepo = (id) => run(roots.repos, "rev-parse", ["--resolve-git-dir", `${id}.git`]).exitCode === 0;
154
+ return {
155
+ prepare({ seat, branch, repos }) {
156
+ const s = segment("seat", seat);
157
+ const parsed = repos.map((r) => ({
158
+ id: segment("repo id", r.id),
159
+ url: noOption("url", r.url),
160
+ base: noOption("base", r.base),
161
+ }));
162
+ const b = branchName("branch", branch);
163
+ const checked = parsed.map((r) => ({ ...r, base: branchName("base", r.base) }));
164
+ const prepared = checked.map((r) => {
165
+ ensureRepo(r.id, r.url);
166
+ fetch(r.id, s);
167
+ ensureWorktree(r.id, s, b, r.base);
168
+ return { id: r.id, dir: worktreeDir(s, r.id), branch: b };
169
+ });
170
+ return { dir: `${roots.workspaces}/${s}`, repos: prepared };
171
+ },
172
+ remove({ seat, repos }) {
173
+ const s = segment("seat", seat);
174
+ for (const id of repos.map((r) => segment("repo id", r))) {
175
+ if (!isRepo(id))
176
+ continue;
177
+ git(repoDir(id), "worktree", ["prune"]);
178
+ const entry = worktreeOf(id, s);
179
+ if (entry !== undefined)
180
+ git(repoDir(id), "worktree", ["remove", "--force", "--", entry.path]);
181
+ }
182
+ },
183
+ };
184
+ }
@@ -0,0 +1,85 @@
1
+ import { type AnyCheck, type AnyRole, type AnyTypeDef, type Check, type CheckDecl, type Effect, type FieldSchemaMap, type JsonObject, type NonEmpty, type Protocol, type ProtocolDecl, type Role, type SeatLabelsDecl, type TypeDef, type TypeSchema } from "../spec.js";
2
+ import type { EvaluatorId, StdEvaluatorId } from "../evaluator.js";
3
+ import { type compareNumberArgs } from "./compare.js";
4
+ type Rec<V> = Readonly<Record<string, V>>;
5
+ export declare const LADDER_EVALUATOR_ID: "telar.std/ladder/1";
6
+ export interface ChoiceKindDef<O extends string = string> {
7
+ readonly kind: "choice";
8
+ readonly options: NonEmpty<O>;
9
+ readonly evidenceRequired: boolean;
10
+ readonly typedRef?: AnyTypeDef | undefined;
11
+ readonly annotated: boolean;
12
+ }
13
+ export interface CustomKindDef<O extends string = string> {
14
+ readonly kind: "custom";
15
+ readonly fields: FieldSchemaMap;
16
+ readonly outcomes: NonEmpty<O>;
17
+ readonly record: readonly string[];
18
+ }
19
+ export type JudgmentKindDef<O extends string = string> = ChoiceKindDef<O> | CustomKindDef<O>;
20
+ export interface JudgmentRung {
21
+ readonly role: AnyRole;
22
+ readonly label: string;
23
+ }
24
+ export interface LadderRung extends JudgmentRung {
25
+ readonly threshold: number;
26
+ }
27
+ export interface JudgmentPolicyDef {
28
+ readonly evaluator: EvaluatorId | StdEvaluatorId;
29
+ readonly params: JsonObject;
30
+ readonly paramsSchema: AnyTypeDef;
31
+ }
32
+ export interface LadderPolicy {
33
+ readonly rungs: NonEmpty<JudgmentRung>;
34
+ readonly policy: JudgmentPolicyDef;
35
+ }
36
+ export interface JudgmentInput<Q extends string = string, O extends string = string> {
37
+ readonly question: Q;
38
+ readonly questionVersion?: string | undefined;
39
+ readonly input: AnyTypeDef;
40
+ readonly rationale?: TypeDef<string> | undefined;
41
+ readonly kind: JudgmentKindDef<O>;
42
+ readonly rungs: NonEmpty<JudgmentRung>;
43
+ readonly policy: JudgmentPolicyDef;
44
+ readonly outcomeEffects?: Partial<Readonly<Record<O, readonly Effect[]>>> | undefined;
45
+ }
46
+ export interface JudgmentProtocolBundle<Q extends string = string, O extends string = string> {
47
+ readonly question: Q;
48
+ readonly outcomes: NonEmpty<O>;
49
+ readonly protocol: Protocol<Q, string, `settled-${O}`>;
50
+ readonly policyCheck: AnyCheck;
51
+ readonly rungLabels: Rec<NonEmpty<string>>;
52
+ }
53
+ export interface JudgmentFragment {
54
+ readonly checks: Rec<CheckDecl>;
55
+ readonly protocols: Rec<ProtocolDecl>;
56
+ readonly roles: Rec<{
57
+ readonly seatLabels: SeatLabelsDecl;
58
+ }>;
59
+ readonly schemas: Rec<TypeSchema>;
60
+ }
61
+ export declare function choiceKind<const O extends string>(opts: {
62
+ readonly options: NonEmpty<O>;
63
+ readonly evidenceRequired?: boolean | undefined;
64
+ readonly typedRef?: AnyTypeDef | undefined;
65
+ readonly annotated?: boolean | undefined;
66
+ }): ChoiceKindDef<O>;
67
+ export declare function customKind<const O extends string>(opts: {
68
+ readonly fields: FieldSchemaMap;
69
+ readonly outcomes: NonEmpty<O>;
70
+ readonly record: readonly string[];
71
+ }): CustomKindDef<O>;
72
+ export declare function ladder(rungs: readonly LadderRung[], last: JudgmentRung): LadderPolicy;
73
+ export declare function withRungLabels<const Name extends string>(base: Role<Name>, labels: NonEmpty<string>): Role<Name, {
74
+ readonly rung: NonEmpty<string>;
75
+ }>;
76
+ export declare function judgmentProtocol<const Q extends string, const O extends string>(def: JudgmentInput<Q, O>): JudgmentProtocolBundle<Q, O>;
77
+ export declare function compileJudgmentFragment<const Q extends string, const O extends string>(def: JudgmentInput<Q, O>): JudgmentFragment;
78
+ export declare function judgmentOverrideProtocol<const Q extends string, const O extends string>(def: {
79
+ readonly question: Q;
80
+ readonly outcomes: NonEmpty<O>;
81
+ readonly rungs: NonEmpty<JudgmentRung>;
82
+ readonly rationale?: TypeDef<string> | undefined;
83
+ readonly compareNumberCheck?: Check<string, typeof compareNumberArgs> | undefined;
84
+ }): Protocol<`${Q}-override`, string, "overridden"> | undefined;
85
+ export {};