@operad-io/dev 0.0.0-stage → 0.0.1

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 (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +795 -2
  3. package/dev.schema.json +602 -0
  4. package/flows/chat-worktree.flow.ts +18 -0
  5. package/flows/dev.flow.ts +7 -0
  6. package/package.json +29 -4
  7. package/src/actions.ts +591 -0
  8. package/src/announce.ts +225 -0
  9. package/src/chat.ts +234 -0
  10. package/src/check.ts +80 -0
  11. package/src/commands.ts +417 -0
  12. package/src/editor.ts +55 -0
  13. package/src/flaky.ts +716 -0
  14. package/src/flow-compiler.ts +858 -0
  15. package/src/followups.ts +223 -0
  16. package/src/gate.ts +2897 -0
  17. package/src/git.ts +138 -0
  18. package/src/index.ts +226 -0
  19. package/src/integrate.ts +1201 -0
  20. package/src/integration-queue.ts +393 -0
  21. package/src/lock.ts +36 -0
  22. package/src/mainclone.ts +102 -0
  23. package/src/marks.ts +126 -0
  24. package/src/model.ts +1165 -0
  25. package/src/notcode.ts +266 -0
  26. package/src/place-order.ts +161 -0
  27. package/src/place-wait.ts +64 -0
  28. package/src/places.ts +368 -0
  29. package/src/prepare.ts +106 -0
  30. package/src/progress.ts +115 -0
  31. package/src/project-flows.ts +160 -0
  32. package/src/project-settings.ts +185 -0
  33. package/src/refusals.ts +336 -0
  34. package/src/rerun.ts +378 -0
  35. package/src/reruns.ts +62 -0
  36. package/src/retry.ts +212 -0
  37. package/src/rework.ts +366 -0
  38. package/src/schedule.ts +1052 -0
  39. package/src/service.ts +2160 -0
  40. package/src/settings.ts +1463 -0
  41. package/src/standing.ts +87 -0
  42. package/src/stdin.ts +105 -0
  43. package/src/steps.ts +1775 -0
  44. package/src/stops.ts +103 -0
  45. package/src/throughput.ts +347 -0
  46. package/src/timeouts.ts +60 -0
  47. package/src/views.ts +1167 -0
  48. package/src/waits.ts +105 -0
package/src/git.ts ADDED
@@ -0,0 +1,138 @@
1
+ /**
2
+ * The one way dev runs a git query. Under heavy load git sometimes exits 0 with
3
+ * empty stdout and stderr (8 in 120000 `rev-parse` runs, with Bun and node), so
4
+ * a query whose output is expected is run again, a bounded number of times,
5
+ * before an empty answer is believed.
6
+ */
7
+
8
+ export interface GitResult {
9
+ readonly ok: boolean;
10
+ readonly out: string;
11
+ readonly err: string;
12
+ }
13
+
14
+ export interface GitOptions {
15
+ /** Keep the output exactly as git wrote it; the default trims it. */
16
+ readonly raw?: boolean;
17
+ /** Input for a command that reads stdin. */
18
+ readonly input?: string | Uint8Array;
19
+ /** The command's environment; the default is this process's. */
20
+ readonly env?: NodeJS.ProcessEnv;
21
+ /** How long one run may take before it is killed; the default is `GIT_TIMEOUT_MS`. */
22
+ readonly timeoutMs?: number;
23
+ }
24
+
25
+ /**
26
+ * How long one git run may take. It is well under a test's 30 s limit, so a git
27
+ * that never exits is reported with its command instead of as a silent timeout.
28
+ * This is a diagnosis, not a retry: the run is not tried again.
29
+ */
30
+ export const GIT_TIMEOUT_MS = 20_000;
31
+
32
+ /** A git run that did not end within its limit and was killed. */
33
+ export class GitTimeoutError extends Error {
34
+ constructor(
35
+ readonly cwd: string,
36
+ readonly args: readonly string[],
37
+ readonly ranMs: number,
38
+ ) {
39
+ super(`git ${args.join(" ")} in ${cwd} did not end after ${ranMs} ms and was killed`);
40
+ this.name = "GitTimeoutError";
41
+ }
42
+ }
43
+
44
+ /** How many times a query that printed nothing is run in all. */
45
+ export const GIT_EMPTY_ATTEMPTS = 5;
46
+
47
+ /**
48
+ * True when a successful run of this git command always prints something, so
49
+ * an empty success is a glitch. Every other command (diff, status, rev-list,
50
+ * fetch, push, merge, commit, reset, worktree add/prune/remove, branch,
51
+ * `merge-base --is-ancestor`, `cat-file -e`, ...) may print nothing and is run
52
+ * once: they are the commands marked as allowed to print nothing.
53
+ */
54
+ export function printsOnSuccess(args: readonly string[]): boolean {
55
+ const [command, sub] = args;
56
+ switch (command) {
57
+ case "rev-parse":
58
+ case "symbolic-ref":
59
+ return true;
60
+ case "worktree":
61
+ return sub === "list";
62
+ case "merge-base":
63
+ return !args.includes("--is-ancestor");
64
+ case "remote":
65
+ // `remote` alone may print nothing: a repository need not have one.
66
+ return args[1] === "get-url";
67
+ case "ls-remote":
68
+ case "patch-id":
69
+ case "check-attr":
70
+ return true;
71
+ default:
72
+ return false;
73
+ }
74
+ }
75
+
76
+ function runOnce(cwd: string, args: readonly string[], options: GitOptions): GitResult {
77
+ const started = Date.now();
78
+ const result = Bun.spawnSync({
79
+ cmd: ["git", "-C", cwd, ...args],
80
+ stdin:
81
+ options.input === undefined
82
+ ? "ignore"
83
+ : typeof options.input === "string"
84
+ ? new TextEncoder().encode(options.input)
85
+ : options.input,
86
+ stdout: "pipe",
87
+ stderr: "pipe",
88
+ // Passed on so the child is found through the current PATH.
89
+ env: options.env ?? { ...process.env },
90
+ timeout: options.timeoutMs ?? GIT_TIMEOUT_MS,
91
+ killSignal: "SIGKILL",
92
+ });
93
+ if (result.exitedDueToTimeout === true) {
94
+ throw new GitTimeoutError(cwd, args, Date.now() - started);
95
+ }
96
+ return {
97
+ ok: result.exitCode === 0,
98
+ out: options.raw === true ? result.stdout.toString() : result.stdout.toString().trim(),
99
+ err: result.stderr.toString().trim(),
100
+ };
101
+ }
102
+
103
+ /** True when an empty success is also a real answer, not a glitch. */
104
+ export function emptyIsSuccess(args: readonly string[]): boolean {
105
+ const [command, sub] = args;
106
+ // A missing remote or branch is a normal empty answer; the caller reports it.
107
+ return command === "ls-remote" || (command === "remote" && sub === "get-url");
108
+ }
109
+
110
+ /**
111
+ * Runs git. The output is trimmed unless `raw` is set; output whose leading or
112
+ * trailing spaces mean something (`status --porcelain`) must be read raw. A
113
+ * command that must print (see `printsOnSuccess`) and exits 0 with nothing is
114
+ * run again; if it never prints, the result is a failure that carries git's
115
+ * stderr and says the output was empty, unless emptiness is a real answer.
116
+ */
117
+ export function git(
118
+ cwd: string,
119
+ args: readonly string[],
120
+ rawOrOptions: boolean | GitOptions = false,
121
+ ): GitResult {
122
+ const options: GitOptions =
123
+ typeof rawOrOptions === "boolean" ? { raw: rawOrOptions } : rawOrOptions;
124
+ let result = runOnce(cwd, args, options);
125
+ if (!printsOnSuccess(args)) return result;
126
+ for (
127
+ let attempt = 1;
128
+ attempt < GIT_EMPTY_ATTEMPTS && result.ok && result.out.trim().length === 0;
129
+ attempt += 1
130
+ ) {
131
+ result = runOnce(cwd, args, options);
132
+ }
133
+ if (result.ok && result.out.trim().length === 0 && !emptyIsSuccess(args)) {
134
+ const note = `git ${args[0] ?? ""} printed nothing (output was empty)`;
135
+ return { ok: false, out: "", err: result.err.length > 0 ? `${result.err}; ${note}` : note };
136
+ }
137
+ return result;
138
+ }
package/src/index.ts ADDED
@@ -0,0 +1,226 @@
1
+ /** The dev plugin: repositories, plans, tasks and their commands and views. */
2
+
3
+ import { definePlugin } from "@operad-io/engine";
4
+ import { PLAN_FILTERS, REPO_FILTERS, TASK_FILTERS } from "./model.ts";
5
+ import devFlow from "../flows/dev.flow.ts";
6
+ import chatWorktreeFlow from "../flows/chat-worktree.flow.ts";
7
+ import { actions } from "./actions.ts";
8
+ import { runAnnounce } from "./announce.ts";
9
+ import { checkDev } from "./check.ts";
10
+ import { chatActions, runChatWorktree } from "./chat.ts";
11
+ import { commands } from "./commands.ts";
12
+ import { runRework, runReview, runReviewDue, runVerify, runWorkDue } from "./gate.ts";
13
+ import { runRetry, runRetryDue } from "./retry.ts";
14
+ import { runCleanup, runIntegrate } from "./integrate.ts";
15
+ import { runScheduler, taskWaitAnsweredAutomatically } from "./schedule.ts";
16
+ import { devStanding } from "./standing.ts";
17
+ import { migrateLegacyRepositories, resolveDevKey } from "./service.ts";
18
+ import { runReverify, runWork, runWorktree } from "./steps.ts";
19
+ import { STEP_TIMEOUT_SECS } from "./timeouts.ts";
20
+ import { DEV_PROJECT_SETTINGS } from "./project-settings.ts";
21
+ import { planViews, repoViews, taskViews } from "./views.ts";
22
+
23
+ /** Builds the dev plugin. Tests build it directly when they need a fake host. */
24
+ export function createDevPlugin() {
25
+ return definePlugin({
26
+ name: "dev",
27
+ kind: "domain",
28
+ version: "0.0.0",
29
+ objects: [
30
+ {
31
+ name: "repo",
32
+ prepare: ({ store }) => migrateLegacyRepositories(store),
33
+ filters: REPO_FILTERS,
34
+ },
35
+ {
36
+ name: "plan",
37
+ filters: PLAN_FILTERS,
38
+ index: ["repository"],
39
+ resolve: (ctx, type, id) => resolveDevKey(ctx.store, type, id, ctx.cwd, ctx.scope),
40
+ },
41
+ {
42
+ name: "task",
43
+ filters: TASK_FILTERS,
44
+ index: ["repository"],
45
+ answeredAutomatically: taskWaitAnsweredAutomatically,
46
+ resolve: (ctx, type, id) => resolveDevKey(ctx.store, type, id, ctx.cwd, ctx.scope),
47
+ },
48
+ ],
49
+ steps: [
50
+ {
51
+ name: "worktree",
52
+ calls: ["shell/check"],
53
+ results: ["ready", "conflict", "blocked", "failed", "cancelled", "interrupted"],
54
+ run: runWorktree,
55
+ },
56
+ {
57
+ name: "reverify",
58
+ calls: ["shell/check"],
59
+ results: ["ready", "conflict", "failed", "cancelled", "interrupted"],
60
+ run: runReverify,
61
+ },
62
+ {
63
+ name: "work",
64
+ calls: ["pool/agent"],
65
+ timeoutSecs: STEP_TIMEOUT_SECS,
66
+ results: ["done", "nothing", "failed", "unavailable", "cancelled", "interrupted"],
67
+ run: runWork,
68
+ },
69
+ {
70
+ name: "verify",
71
+ calls: ["shell/check"],
72
+ timeoutSecs: STEP_TIMEOUT_SECS,
73
+ results: ["pass", "fail", "not_code", "cancelled"],
74
+ run: runVerify,
75
+ },
76
+ {
77
+ name: "review",
78
+ calls: ["shell/check"],
79
+ timeoutSecs: STEP_TIMEOUT_SECS,
80
+ results: [
81
+ "pass",
82
+ "fail",
83
+ "failed",
84
+ "unavailable",
85
+ "timed_out",
86
+ "exhausted",
87
+ "out_of_time",
88
+ "cancelled",
89
+ ],
90
+ run: runReview,
91
+ },
92
+ {
93
+ name: "review_due",
94
+ // The wait runs in pieces below this timeout until the recorded retry
95
+ // time; it is cancelled with the run, never by the step clock.
96
+ timeoutSecs: STEP_TIMEOUT_SECS,
97
+ results: ["ready", "interrupted"],
98
+ run: runReviewDue,
99
+ },
100
+ {
101
+ name: "work_due",
102
+ // Like review_due: pieces below this timeout until the recorded time.
103
+ timeoutSecs: STEP_TIMEOUT_SECS,
104
+ results: ["ready", "interrupted"],
105
+ run: runWorkDue,
106
+ },
107
+ {
108
+ name: "retry",
109
+ results: ["again", "hold", "ask", "give_up", "cancelled"],
110
+ run: runRetry,
111
+ },
112
+ {
113
+ // A wait: it holds until what a retry waits for is there or the bound
114
+ // has passed, in pieces below the step timeout.
115
+ name: "retry_due",
116
+ timeoutSecs: STEP_TIMEOUT_SECS,
117
+ results: ["ready", "waiting", "interrupted"],
118
+ run: runRetryDue,
119
+ },
120
+ {
121
+ name: "rework",
122
+ results: ["again", "ask", "cancelled"],
123
+ run: runRework,
124
+ },
125
+ {
126
+ name: "integrate",
127
+ calls: ["shell/check", "shell/command"],
128
+ timeoutSecs: STEP_TIMEOUT_SECS,
129
+ results: ["merged", "conflict", "fail", "failed", "cancelled"],
130
+ run: runIntegrate,
131
+ },
132
+ {
133
+ name: "cleanup",
134
+ calls: ["shell/command"],
135
+ results: ["done", "cancelled"],
136
+ run: runCleanup,
137
+ },
138
+ {
139
+ name: "announce",
140
+ results: ["done", "interrupted", "cancelled"],
141
+ run: runAnnounce,
142
+ },
143
+ {
144
+ // The worktree a chat asked for; git runs in the repository.
145
+ name: "chat_worktree",
146
+ results: ["done", "failed"],
147
+ run: runChatWorktree,
148
+ },
149
+ ],
150
+ settings: {
151
+ type: "object",
152
+ properties: {
153
+ checks: {
154
+ type: "integer",
155
+ minimum: 1,
156
+ default: 2,
157
+ editable: true,
158
+ description:
159
+ "How many checks (a task's verify, and the checks before a merge) may run at the same time on this machine, across all repositories.",
160
+ },
161
+ },
162
+ additionalProperties: false,
163
+ },
164
+ projectSettings: DEV_PROJECT_SETTINGS,
165
+ flows: [devFlow, chatWorktreeFlow],
166
+ // Every stop that needs a person is in history; a user can route the event
167
+ // to a channel by replacing this rule.
168
+ rules: [
169
+ {
170
+ source: "operad",
171
+ type: "dev.waiting",
172
+ start: { flow: "dev" },
173
+ object: "$event.object",
174
+ mode: "observe",
175
+ },
176
+ // A chat of a repository asked for a worktree and a person said yes.
177
+ {
178
+ source: "operad",
179
+ type: "chat.worktree",
180
+ start: { flow: "dev:chat-worktree" },
181
+ object: "$event.object",
182
+ mode: "auto",
183
+ },
184
+ ],
185
+ actions: [...actions, ...chatActions],
186
+ commands,
187
+ onTick: runScheduler,
188
+ standing: devStanding,
189
+ views: {
190
+ repo: repoViews,
191
+ plan: planViews,
192
+ task: taskViews,
193
+ },
194
+ keys: {
195
+ c: "dev:confirm",
196
+ x: "dev:cancel",
197
+ },
198
+ check(ctx) {
199
+ return checkDev(ctx);
200
+ },
201
+ });
202
+ }
203
+
204
+ export default createDevPlugin();
205
+
206
+ export * from "./actions.ts";
207
+ export * from "./announce.ts";
208
+ export * from "./chat.ts";
209
+ export * from "./check.ts";
210
+ export * from "./commands.ts";
211
+ export * from "./gate.ts";
212
+ export * from "./integrate.ts";
213
+ export * from "./model.ts";
214
+ export * from "./flow-compiler.ts";
215
+ export * from "./project-flows.ts";
216
+ export * from "./schedule.ts";
217
+ export * from "./service.ts";
218
+ export * from "./places.ts";
219
+ export * from "./project-settings.ts";
220
+ export * from "./retry.ts";
221
+ export * from "./stops.ts";
222
+ export * from "./settings.ts";
223
+ export * from "./standing.ts";
224
+ export * from "./steps.ts";
225
+ export * from "./timeouts.ts";
226
+ export * from "./views.ts";