@operad-io/dev 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +12 -2
- package/dev.schema.json +631 -0
- package/flows/chat-worktree.flow.ts +18 -0
- package/flows/dev.flow.ts +7 -0
- package/package.json +29 -4
- package/src/actions.ts +591 -0
- package/src/announce.ts +225 -0
- package/src/chat.ts +234 -0
- package/src/check.ts +80 -0
- package/src/commands.ts +417 -0
- package/src/editor.ts +55 -0
- package/src/flaky.ts +716 -0
- package/src/flow-compiler.ts +858 -0
- package/src/followups.ts +223 -0
- package/src/gate.ts +2950 -0
- package/src/git.ts +138 -0
- package/src/index.ts +227 -0
- package/src/integrate.ts +1213 -0
- package/src/integration-queue.ts +393 -0
- package/src/lock.ts +36 -0
- package/src/mainclone.ts +102 -0
- package/src/marks.ts +126 -0
- package/src/model.ts +1171 -0
- package/src/notcode.ts +305 -0
- package/src/place-order.ts +161 -0
- package/src/place-wait.ts +64 -0
- package/src/places.ts +368 -0
- package/src/prepare.ts +106 -0
- package/src/progress.ts +115 -0
- package/src/project-flows.ts +160 -0
- package/src/project-settings.ts +192 -0
- package/src/refusals.ts +336 -0
- package/src/rerun.ts +390 -0
- package/src/reruns.ts +62 -0
- package/src/retry.ts +212 -0
- package/src/rework.ts +366 -0
- package/src/schedule.ts +1052 -0
- package/src/service.ts +2160 -0
- package/src/settings.ts +1495 -0
- package/src/standing.ts +87 -0
- package/src/stdin.ts +105 -0
- package/src/steps.ts +1775 -0
- package/src/stops.ts +103 -0
- package/src/throughput.ts +347 -0
- package/src/timeouts.ts +60 -0
- package/src/views.ts +1167 -0
- package/src/waits.ts +105 -0
package/src/service.ts
ADDED
|
@@ -0,0 +1,2160 @@
|
|
|
1
|
+
/** The work the dev plugin does: repositories, plans and tasks. */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
existsSync,
|
|
5
|
+
appendFileSync,
|
|
6
|
+
mkdirSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
realpathSync,
|
|
9
|
+
statSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from "node:fs";
|
|
12
|
+
import { homedir } from "node:os";
|
|
13
|
+
import { basename, isAbsolute, join, relative, resolve } from "node:path";
|
|
14
|
+
import { objectValueAfter, ViewError } from "@operad-io/engine";
|
|
15
|
+
import { isBusy } from "@operad-io/engine/platform";
|
|
16
|
+
import type { FlowDefinition, ObjectStore, PluginFact, StoredObject } from "@operad-io/engine";
|
|
17
|
+
import { git } from "./git.ts";
|
|
18
|
+
import { withRepositoryLock } from "./lock.ts";
|
|
19
|
+
import {
|
|
20
|
+
appendChange,
|
|
21
|
+
appendRework,
|
|
22
|
+
bareId,
|
|
23
|
+
dependencyKey,
|
|
24
|
+
dependencyLabel,
|
|
25
|
+
DevError,
|
|
26
|
+
DevRefusal,
|
|
27
|
+
editBrief,
|
|
28
|
+
isOpenTask,
|
|
29
|
+
keyRepository,
|
|
30
|
+
listAll,
|
|
31
|
+
nextId,
|
|
32
|
+
parseId,
|
|
33
|
+
planData,
|
|
34
|
+
shiftCostTotals,
|
|
35
|
+
taskCostShare,
|
|
36
|
+
type PlanCostTotals,
|
|
37
|
+
isIntegrating,
|
|
38
|
+
planState,
|
|
39
|
+
PLAN_FINAL_STATES,
|
|
40
|
+
TASK_FINAL_STATES,
|
|
41
|
+
PRIORITIES,
|
|
42
|
+
repositoryData,
|
|
43
|
+
storeKey,
|
|
44
|
+
taskData,
|
|
45
|
+
validateRepositoryName,
|
|
46
|
+
validateSlug,
|
|
47
|
+
validateArea,
|
|
48
|
+
validateTitle,
|
|
49
|
+
type AutoMode,
|
|
50
|
+
type PlanData,
|
|
51
|
+
type PlanState,
|
|
52
|
+
type Priority,
|
|
53
|
+
type RepositoryData,
|
|
54
|
+
type TaskData,
|
|
55
|
+
type TaskPhase,
|
|
56
|
+
} from "./model.ts";
|
|
57
|
+
import {
|
|
58
|
+
planLine,
|
|
59
|
+
needsPerson,
|
|
60
|
+
taskLine,
|
|
61
|
+
type PlanActionName,
|
|
62
|
+
type TaskActionName,
|
|
63
|
+
} from "./refusals.ts";
|
|
64
|
+
import { DEFAULT_DEV_SETTINGS, DEV_SETTINGS_SCHEMA } from "./settings.ts";
|
|
65
|
+
|
|
66
|
+
/** An optional way for the dev plugin to ask the kernel to cancel a run. */
|
|
67
|
+
export interface DevRunControl {
|
|
68
|
+
/** `flow` is the run's own graph, when the task stores one the loaded plugins lack. */
|
|
69
|
+
cancel(runId: string, flow?: FlowDefinition): Promise<void> | void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface ResolveRepositoryOptions {
|
|
73
|
+
readonly name?: string | undefined;
|
|
74
|
+
readonly cwd?: string | undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface AddRepositoryOptions {
|
|
78
|
+
readonly path: string;
|
|
79
|
+
readonly name?: string | undefined;
|
|
80
|
+
readonly cwd?: string | undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const REPOSITORY_TYPE = "repo";
|
|
84
|
+
/** The type name stored repositories had before dev declared `repo`. */
|
|
85
|
+
const LEGACY_REPOSITORY_TYPE = "repository";
|
|
86
|
+
const PLAN_TYPE = "plan";
|
|
87
|
+
const TASK_TYPE = "task";
|
|
88
|
+
|
|
89
|
+
const EXCLUDES = [".operad-worktrees/", ".radian-sandbox/"] as const;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Test-only hook, set by a test to pause between a command's first read and
|
|
93
|
+
* its lock. Production code never sets it; the checks inside the lock must hold
|
|
94
|
+
* without it.
|
|
95
|
+
*/
|
|
96
|
+
export const devTestHooks: {
|
|
97
|
+
beforeLock?: ((operation: string) => Promise<void> | void) | undefined;
|
|
98
|
+
} = {};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The one way a command changes a repository: refuse a removed repository,
|
|
102
|
+
* take its lock, then read it and the objects again before `work` decides.
|
|
103
|
+
*/
|
|
104
|
+
export async function withRepoLock<T>(
|
|
105
|
+
store: ObjectStore,
|
|
106
|
+
repository: string,
|
|
107
|
+
work: (fresh: RepositoryData) => Promise<T> | T,
|
|
108
|
+
): Promise<T> {
|
|
109
|
+
const initial = await repositoryOf(store, repository);
|
|
110
|
+
return withRepositoryLock(initial.path, async () => {
|
|
111
|
+
const fresh = await repositoryOf(store, repository);
|
|
112
|
+
return work(fresh);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Holds the locks of every repository in `names`, taken in name order so two
|
|
118
|
+
* commands that need several never wait for each other.
|
|
119
|
+
*/
|
|
120
|
+
async function withRepoLocks<T>(
|
|
121
|
+
store: ObjectStore,
|
|
122
|
+
names: readonly string[],
|
|
123
|
+
work: () => Promise<T>,
|
|
124
|
+
): Promise<T> {
|
|
125
|
+
const ordered = [...new Set(names)].sort();
|
|
126
|
+
const run = async (index: number): Promise<T> =>
|
|
127
|
+
index >= ordered.length
|
|
128
|
+
? work()
|
|
129
|
+
: withRepoLock(store, ordered[index] ?? "", () => run(index + 1));
|
|
130
|
+
return run(0);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The repositories whose locks a change of `after` takes. A dependency in
|
|
135
|
+
* another repository can close a cycle through any repository, so such a
|
|
136
|
+
* change holds every registered repository's lock.
|
|
137
|
+
*/
|
|
138
|
+
async function afterLockNames(
|
|
139
|
+
store: ObjectStore,
|
|
140
|
+
repository: string,
|
|
141
|
+
after: readonly string[],
|
|
142
|
+
): Promise<readonly string[]> {
|
|
143
|
+
const cross = after.some(
|
|
144
|
+
(entry) => keyRepository(dependencyKey(repository, entry)) !== repository,
|
|
145
|
+
);
|
|
146
|
+
if (!cross) return [repository];
|
|
147
|
+
const names = (await listRepositories(store)).map((item) => item.name);
|
|
148
|
+
return [repository, ...names];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* ---------------------------------------------------------------------------
|
|
152
|
+
* Repositories
|
|
153
|
+
* ------------------------------------------------------------------------- */
|
|
154
|
+
|
|
155
|
+
/** A task that works: running, in review, or integrating (phase waiting, merge already chosen). */
|
|
156
|
+
function isActiveTask(task: TaskData): boolean {
|
|
157
|
+
return task.phase === "running" || task.phase === "review" || isIntegrating(task);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Open plans and open tasks of one repository, as its row shows them. */
|
|
161
|
+
async function openCounts(
|
|
162
|
+
store: ObjectStore,
|
|
163
|
+
name: string,
|
|
164
|
+
): Promise<{
|
|
165
|
+
readonly openPlans: number;
|
|
166
|
+
readonly openTasks: number;
|
|
167
|
+
readonly activeTasks: number;
|
|
168
|
+
}> {
|
|
169
|
+
const plans = (await listAll(store, PLAN_TYPE)).filter(
|
|
170
|
+
(object) => planData(object.data).repository === name,
|
|
171
|
+
);
|
|
172
|
+
const tasks = (await listAll(store, TASK_TYPE)).filter(
|
|
173
|
+
(object) => taskData(object.data).repository === name,
|
|
174
|
+
);
|
|
175
|
+
return {
|
|
176
|
+
openPlans: plans.filter(
|
|
177
|
+
(object) => !PLAN_FINAL_STATES.includes(planState(planData(object.data))),
|
|
178
|
+
).length,
|
|
179
|
+
openTasks: tasks.filter(
|
|
180
|
+
(object) => !TASK_FINAL_STATES.includes(taskData(object.data).phase),
|
|
181
|
+
).length,
|
|
182
|
+
activeTasks: tasks.filter((object) => isActiveTask(taskData(object.data))).length,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Keeps the open counts a repository's row reads, writing the repository only
|
|
188
|
+
* on a change. The row is pure, so the counts live in the stored object.
|
|
189
|
+
*/
|
|
190
|
+
export async function refreshRepositoryCounts(
|
|
191
|
+
store: ObjectStore,
|
|
192
|
+
name: string,
|
|
193
|
+
): Promise<void> {
|
|
194
|
+
const found = await store.get(REPOSITORY_TYPE, name);
|
|
195
|
+
if (found === undefined) return;
|
|
196
|
+
const data = repositoryData(found.data);
|
|
197
|
+
const counts = await openCounts(store, name);
|
|
198
|
+
if (
|
|
199
|
+
data.openPlans === counts.openPlans &&
|
|
200
|
+
data.openTasks === counts.openTasks &&
|
|
201
|
+
data.activeTasks === counts.activeTasks
|
|
202
|
+
) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
await store.put(
|
|
206
|
+
REPOSITORY_TYPE,
|
|
207
|
+
name,
|
|
208
|
+
{ ...data, ...counts },
|
|
209
|
+
"dev:refresh-repo",
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const migrated = new WeakSet<ObjectStore>();
|
|
214
|
+
|
|
215
|
+
/** Copies repositories stored under the old type name to `repo`, once per store. */
|
|
216
|
+
export async function migrateLegacyRepositories(store: ObjectStore): Promise<void> {
|
|
217
|
+
if (migrated.has(store)) return;
|
|
218
|
+
migrated.add(store);
|
|
219
|
+
for (const old of await listAll(store, LEGACY_REPOSITORY_TYPE)) {
|
|
220
|
+
if ((await store.get(REPOSITORY_TYPE, old.id)) !== undefined) continue;
|
|
221
|
+
await store.put(
|
|
222
|
+
REPOSITORY_TYPE,
|
|
223
|
+
old.id,
|
|
224
|
+
{ ...old.data, ...(await openCounts(store, old.id)) },
|
|
225
|
+
"dev:repo-migrate",
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
// A repository stored before the running-task count existed has none: count once.
|
|
229
|
+
for (const object of await listAll(store, REPOSITORY_TYPE)) {
|
|
230
|
+
if (repositoryData(object.data).activeTasks === undefined) {
|
|
231
|
+
await refreshRepositoryCounts(store, object.id);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function repositories(store: ObjectStore): Promise<readonly StoredObject[]> {
|
|
237
|
+
await migrateLegacyRepositories(store);
|
|
238
|
+
return listAll(store, REPOSITORY_TYPE);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export async function findRepository(
|
|
242
|
+
store: ObjectStore,
|
|
243
|
+
name: string,
|
|
244
|
+
options: { readonly includeRemoved?: boolean | undefined } = {},
|
|
245
|
+
): Promise<StoredObject | undefined> {
|
|
246
|
+
await migrateLegacyRepositories(store);
|
|
247
|
+
const object = await store.get(REPOSITORY_TYPE, name);
|
|
248
|
+
if (object === undefined) return undefined;
|
|
249
|
+
if (options.includeRemoved !== true && repositoryData(object.data).removed) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
return object;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export async function listRepositories(
|
|
256
|
+
store: ObjectStore,
|
|
257
|
+
): Promise<readonly RepositoryData[]> {
|
|
258
|
+
const objects = await repositories(store);
|
|
259
|
+
return objects
|
|
260
|
+
.map((object) => repositoryData(object.data))
|
|
261
|
+
.filter((repository) => !repository.removed)
|
|
262
|
+
.sort((left, right) => left.name.localeCompare(right.name));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function inside(path: string, cwd: string): boolean {
|
|
266
|
+
const rel = relative(path, cwd);
|
|
267
|
+
return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function shortPath(path: string): string {
|
|
271
|
+
const home = homedir();
|
|
272
|
+
if (path === home) return "~";
|
|
273
|
+
return path.startsWith(`${home}/`) ? `~${path.slice(home.length)}` : path;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function registeredLines(registered: readonly RepositoryData[]): string[] {
|
|
277
|
+
return registered.map(
|
|
278
|
+
(repository) =>
|
|
279
|
+
` ${repository.name} ${shortPath(repository.path)}${
|
|
280
|
+
existsSync(repository.path) ? "" : " (missing)"
|
|
281
|
+
}`,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function noRepositoryHere(
|
|
286
|
+
registered: readonly RepositoryData[],
|
|
287
|
+
cwd: string,
|
|
288
|
+
): DevError {
|
|
289
|
+
if (registered.length === 0) {
|
|
290
|
+
return new DevError(
|
|
291
|
+
"repo",
|
|
292
|
+
`no registered repository contains ${cwd}, and none is registered; register one with \`operad repo add <path>\``,
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
return new DevError(
|
|
296
|
+
"repo",
|
|
297
|
+
[
|
|
298
|
+
`no registered repository contains ${cwd}; run the command inside one of these, or pass --repo <name>:`,
|
|
299
|
+
...registeredLines(registered),
|
|
300
|
+
].join("\n"),
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function unknownRepository(
|
|
305
|
+
registered: readonly RepositoryData[],
|
|
306
|
+
name: string,
|
|
307
|
+
): DevError {
|
|
308
|
+
if (registered.length === 0) {
|
|
309
|
+
return new DevError(
|
|
310
|
+
"repo",
|
|
311
|
+
`no registered repository "${name}", and none is registered; register one with \`operad repo add <path>\``,
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
return new DevError(
|
|
315
|
+
"repo",
|
|
316
|
+
[`no registered repository "${name}"; registered:`, ...registeredLines(registered)].join(
|
|
317
|
+
"\n",
|
|
318
|
+
),
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function repositoryAt(
|
|
323
|
+
registered: readonly RepositoryData[],
|
|
324
|
+
cwd: string,
|
|
325
|
+
): RepositoryData | undefined {
|
|
326
|
+
const matches = registered.filter((repository) =>
|
|
327
|
+
inside(resolve(repository.path), cwd),
|
|
328
|
+
);
|
|
329
|
+
matches.sort((left, right) => right.path.length - left.path.length);
|
|
330
|
+
return matches[0];
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** The repository the current directory belongs to, or the named one. */
|
|
334
|
+
export async function resolveRepository(
|
|
335
|
+
store: ObjectStore,
|
|
336
|
+
options: ResolveRepositoryOptions = {},
|
|
337
|
+
): Promise<RepositoryData> {
|
|
338
|
+
if (options.name !== undefined) {
|
|
339
|
+
const found = await findRepository(store, options.name);
|
|
340
|
+
if (found === undefined) {
|
|
341
|
+
throw unknownRepository(await listRepositories(store), options.name);
|
|
342
|
+
}
|
|
343
|
+
return repositoryData(found.data);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const cwd = resolve(options.cwd ?? process.cwd());
|
|
347
|
+
const registered = await listRepositories(store);
|
|
348
|
+
const found = repositoryAt(registered, cwd);
|
|
349
|
+
if (found === undefined) throw noRepositoryHere(registered, cwd);
|
|
350
|
+
return found;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* The repository a plan or task id belongs to, and the id without its
|
|
355
|
+
* repository. A qualified id (`<repo>/<id>`) names its repository; a bare id
|
|
356
|
+
* means the repository of the current directory, or, outside every
|
|
357
|
+
* repository, the one repository that has an object with that id.
|
|
358
|
+
*/
|
|
359
|
+
export async function resolveObjectRepository(
|
|
360
|
+
store: ObjectStore,
|
|
361
|
+
kind: "plan" | "task",
|
|
362
|
+
id: string,
|
|
363
|
+
options: ResolveRepositoryOptions = {},
|
|
364
|
+
): Promise<{ readonly repository: RepositoryData; readonly id: string }> {
|
|
365
|
+
const slash = id.indexOf("/");
|
|
366
|
+
if (slash >= 0) {
|
|
367
|
+
const named = id.slice(0, slash);
|
|
368
|
+
if (options.name !== undefined && options.name !== named) {
|
|
369
|
+
throw new DevError(
|
|
370
|
+
"repo",
|
|
371
|
+
`"${id}" is in repository "${named}", but --repo names "${options.name}"`,
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
return {
|
|
375
|
+
repository: await resolveRepository(store, { name: named }),
|
|
376
|
+
id: id.slice(slash + 1),
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
if (options.name !== undefined) {
|
|
380
|
+
return { repository: await resolveRepository(store, options), id };
|
|
381
|
+
}
|
|
382
|
+
const cwd = resolve(options.cwd ?? process.cwd());
|
|
383
|
+
const registered = await listRepositories(store);
|
|
384
|
+
const here = repositoryAt(registered, cwd);
|
|
385
|
+
if (here !== undefined) return { repository: here, id };
|
|
386
|
+
const having: RepositoryData[] = [];
|
|
387
|
+
for (const repository of registered) {
|
|
388
|
+
const object = await store.get(kind, storeKey(repository.name, id));
|
|
389
|
+
if (object !== undefined) having.push(repository);
|
|
390
|
+
}
|
|
391
|
+
const only = having[0];
|
|
392
|
+
if (only === undefined) throw noRepositoryHere(registered, cwd);
|
|
393
|
+
if (having.length > 1) {
|
|
394
|
+
throw new DevError(
|
|
395
|
+
"id",
|
|
396
|
+
`"${id}" is ambiguous, in more than one repository: use ${having
|
|
397
|
+
.map((repository) => storeKey(repository.name, id))
|
|
398
|
+
.join(", ")}`,
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
return { repository: only, id };
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Resolves a bare plan or task id in the repository the current directory
|
|
406
|
+
* belongs to. The kernel calls this for `show` and `dev:<action>`; without a
|
|
407
|
+
* repository the id falls back to the kernel's suffix lookup, which reports an
|
|
408
|
+
* ambiguity when more than one repository has it.
|
|
409
|
+
*
|
|
410
|
+
* A known repository returns the key even when no object is stored there: the
|
|
411
|
+
* lookup stays inside that repository instead of reaching into another one.
|
|
412
|
+
*/
|
|
413
|
+
export async function resolveDevKey(
|
|
414
|
+
store: ObjectStore,
|
|
415
|
+
type: string,
|
|
416
|
+
id: string,
|
|
417
|
+
cwd: string,
|
|
418
|
+
scope?: string,
|
|
419
|
+
): Promise<string | undefined> {
|
|
420
|
+
const kind =
|
|
421
|
+
type === PLAN_TYPE ? "plan" : type === TASK_TYPE ? "task" : undefined;
|
|
422
|
+
if (kind === undefined) return undefined;
|
|
423
|
+
const slash = id.indexOf("/");
|
|
424
|
+
if (parseId(slash >= 0 ? id.slice(slash + 1) : id, kind) === undefined) {
|
|
425
|
+
return undefined;
|
|
426
|
+
}
|
|
427
|
+
try {
|
|
428
|
+
if (slash >= 0) {
|
|
429
|
+
// The kernel already looked the qualified key up; only an unknown
|
|
430
|
+
// repository is worth reporting here.
|
|
431
|
+
await resolveRepository(store, { name: id.slice(0, slash) });
|
|
432
|
+
return undefined;
|
|
433
|
+
}
|
|
434
|
+
const { repository } = await resolveObjectRepository(
|
|
435
|
+
store,
|
|
436
|
+
kind,
|
|
437
|
+
id,
|
|
438
|
+
scope === undefined ? { cwd } : { name: scope },
|
|
439
|
+
);
|
|
440
|
+
return storeKey(repository.name, id);
|
|
441
|
+
} catch (error) {
|
|
442
|
+
if (error instanceof DevError) {
|
|
443
|
+
throw new ViewError(
|
|
444
|
+
error.field === "id" ? "ambiguous" : "not-found",
|
|
445
|
+
error.message,
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
throw error;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function ensureExcludes(repositoryPath: string): void {
|
|
453
|
+
const path = git(repositoryPath, ["rev-parse", "--git-path", "info/exclude"]);
|
|
454
|
+
if (!path.ok) {
|
|
455
|
+
throw new DevError("path", `is not a git work tree: ${path.err}`);
|
|
456
|
+
}
|
|
457
|
+
const excludePath = resolve(repositoryPath, path.out);
|
|
458
|
+
mkdirSync(resolve(excludePath, ".."), { recursive: true });
|
|
459
|
+
const current = existsSync(excludePath) ? readFileSync(excludePath, "utf8") : "";
|
|
460
|
+
const lines = current.length === 0 ? [] : current.replace(/\r\n/g, "\n").split("\n");
|
|
461
|
+
const present = new Set(lines.map((line) => line.trim()));
|
|
462
|
+
const missing = EXCLUDES.filter((entry) => !present.has(entry));
|
|
463
|
+
if (missing.length === 0) return;
|
|
464
|
+
// operad repo add owns this file: the "never write under .git" rule is for
|
|
465
|
+
// our own work as a worker in a repository, not for this command.
|
|
466
|
+
const prefix = current.length === 0 || current.endsWith("\n") ? "" : "\n";
|
|
467
|
+
appendFileSync(excludePath, `${prefix}${missing.join("\n")}\n`, "utf8");
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function writeSettings(repositoryPath: string, name: string): void {
|
|
471
|
+
const directory = join(repositoryPath, ".operad");
|
|
472
|
+
mkdirSync(directory, { recursive: true });
|
|
473
|
+
const settings = join(directory, "dev.json");
|
|
474
|
+
if (existsSync(settings)) return;
|
|
475
|
+
writeFileSync(
|
|
476
|
+
settings,
|
|
477
|
+
`${JSON.stringify(
|
|
478
|
+
{
|
|
479
|
+
$schema: DEV_SETTINGS_SCHEMA,
|
|
480
|
+
name,
|
|
481
|
+
remote: DEFAULT_DEV_SETTINGS.remote,
|
|
482
|
+
mainBranch: DEFAULT_DEV_SETTINGS.mainBranch,
|
|
483
|
+
worktreeRoot: DEFAULT_DEV_SETTINGS.worktreeRoot,
|
|
484
|
+
concurrency: DEFAULT_DEV_SETTINGS.concurrency,
|
|
485
|
+
worker: DEFAULT_DEV_SETTINGS.worker,
|
|
486
|
+
workerBrief: DEFAULT_DEV_SETTINGS.workerBrief,
|
|
487
|
+
},
|
|
488
|
+
null,
|
|
489
|
+
2,
|
|
490
|
+
)}\n`,
|
|
491
|
+
{ flag: "wx" },
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** Registers a git work tree that has a remote. */
|
|
496
|
+
export async function addRepository(
|
|
497
|
+
store: ObjectStore,
|
|
498
|
+
options: AddRepositoryOptions,
|
|
499
|
+
): Promise<RepositoryData> {
|
|
500
|
+
const from = resolve(options.cwd ?? process.cwd(), options.path);
|
|
501
|
+
if (!existsSync(from) || !statSync(from).isDirectory()) {
|
|
502
|
+
throw new DevError("path", `no directory "${from}"`);
|
|
503
|
+
}
|
|
504
|
+
const top = git(from, ["rev-parse", "--show-toplevel"]);
|
|
505
|
+
if (!top.ok || top.out.length === 0) {
|
|
506
|
+
throw new DevError(
|
|
507
|
+
"path",
|
|
508
|
+
`"${from}" is not a git work tree${top.err.length > 0 ? `: ${top.err}` : ""}`,
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
const path = realpathSync(top.out);
|
|
512
|
+
if (path !== realpathSync(from)) {
|
|
513
|
+
throw new DevError(
|
|
514
|
+
"path",
|
|
515
|
+
`"${from}" is not the root of a git work tree`,
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
const bare = git(path, ["rev-parse", "--is-bare-repository"]);
|
|
519
|
+
if (bare.ok && bare.out === "true") {
|
|
520
|
+
throw new DevError("path", `"${path}" is a bare repository`);
|
|
521
|
+
}
|
|
522
|
+
const remotes = git(path, ["remote"]);
|
|
523
|
+
if (!remotes.ok || remotes.out.length === 0) {
|
|
524
|
+
throw new DevError(
|
|
525
|
+
"path",
|
|
526
|
+
`"${path}" has no remote${remotes.err.length > 0 ? `: ${remotes.err}` : ""}`,
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
const name = validateRepositoryName(options.name ?? basename(path));
|
|
531
|
+
const existing = await findRepository(store, name);
|
|
532
|
+
if (existing !== undefined) {
|
|
533
|
+
throw new DevError(
|
|
534
|
+
"name",
|
|
535
|
+
`repository "${name}" is already registered at "${repositoryData(existing.data).path}"`,
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
const samePath = (await listRepositories(store)).find(
|
|
539
|
+
(repository) => resolve(repository.path) === path,
|
|
540
|
+
);
|
|
541
|
+
if (samePath !== undefined) {
|
|
542
|
+
throw new DevError(
|
|
543
|
+
"path",
|
|
544
|
+
`"${path}" is already registered as "${samePath.name}"`,
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
const data: RepositoryData = {
|
|
549
|
+
name,
|
|
550
|
+
path,
|
|
551
|
+
removed: false,
|
|
552
|
+
openPlans: 0,
|
|
553
|
+
openTasks: 0,
|
|
554
|
+
activeTasks: 0,
|
|
555
|
+
};
|
|
556
|
+
await store.put(REPOSITORY_TYPE, name, data, "dev:repo-add");
|
|
557
|
+
writeSettings(path, name);
|
|
558
|
+
ensureExcludes(path);
|
|
559
|
+
return data;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** Refuses to remove a repository that still has open tasks. */
|
|
563
|
+
export async function removeRepository(
|
|
564
|
+
store: ObjectStore,
|
|
565
|
+
name: string,
|
|
566
|
+
): Promise<RepositoryData> {
|
|
567
|
+
const initial = await repositoryOf(store, name);
|
|
568
|
+
await assertNoOpenTasks(store, initial.name);
|
|
569
|
+
await devTestHooks.beforeLock?.("repo-remove");
|
|
570
|
+
return withRepoLock(store, name, async (repository) => {
|
|
571
|
+
await assertNoOpenTasks(store, repository.name);
|
|
572
|
+
const data: RepositoryData = { ...repository, removed: true };
|
|
573
|
+
await store.put(REPOSITORY_TYPE, name, data, "dev:repo-remove");
|
|
574
|
+
return data;
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/** The check that decides whether a repository may be removed. */
|
|
579
|
+
async function assertNoOpenTasks(
|
|
580
|
+
store: ObjectStore,
|
|
581
|
+
name: string,
|
|
582
|
+
): Promise<void> {
|
|
583
|
+
const open = (await listAll(store, TASK_TYPE)).filter((object) => {
|
|
584
|
+
const task = taskData(object.data);
|
|
585
|
+
return task.repository === name && isOpenTask(task);
|
|
586
|
+
});
|
|
587
|
+
if (open.length > 0) {
|
|
588
|
+
throw new DevError(
|
|
589
|
+
"repo",
|
|
590
|
+
`repository "${name}" has ${open.length} open task(s)`,
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function repositoryByName(
|
|
596
|
+
objects: readonly StoredObject[],
|
|
597
|
+
name: string,
|
|
598
|
+
): RepositoryData | undefined {
|
|
599
|
+
const found = objects.find((object) => object.id === name);
|
|
600
|
+
return found === undefined ? undefined : repositoryData(found.data);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
async function repositoryOf(
|
|
604
|
+
store: ObjectStore,
|
|
605
|
+
name: string,
|
|
606
|
+
): Promise<RepositoryData> {
|
|
607
|
+
const found = repositoryByName(await repositories(store), name);
|
|
608
|
+
if (found === undefined) {
|
|
609
|
+
throw new DevError("repository", `no repository "${name}"`);
|
|
610
|
+
}
|
|
611
|
+
if (found.removed) {
|
|
612
|
+
throw new DevError("repository", `repository "${name}" was removed`);
|
|
613
|
+
}
|
|
614
|
+
return found;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/* ---------------------------------------------------------------------------
|
|
618
|
+
* Plans and tasks
|
|
619
|
+
* ------------------------------------------------------------------------- */
|
|
620
|
+
|
|
621
|
+
async function allTasks(store: ObjectStore): Promise<readonly StoredObject[]> {
|
|
622
|
+
return listAll(store, TASK_TYPE);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
async function tasksOfPlan(
|
|
626
|
+
store: ObjectStore,
|
|
627
|
+
planKey: string,
|
|
628
|
+
): Promise<readonly StoredObject[]> {
|
|
629
|
+
const repository = keyRepository(planKey);
|
|
630
|
+
const plan = bareId(planKey);
|
|
631
|
+
return (await allTasks(store)).filter((object) => {
|
|
632
|
+
const task = taskData(object.data);
|
|
633
|
+
return task.repository === repository && task.plan === plan;
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Throws the table's line when a task action cannot run on this task now.
|
|
639
|
+
* Returns true when the result is already true (another action won a race):
|
|
640
|
+
* the caller then succeeds and writes nothing.
|
|
641
|
+
*/
|
|
642
|
+
function refuseTask(
|
|
643
|
+
action: TaskActionName,
|
|
644
|
+
task: TaskData,
|
|
645
|
+
key: string,
|
|
646
|
+
plan?: { readonly id: string; readonly state: PlanState },
|
|
647
|
+
): boolean {
|
|
648
|
+
if (action === "confirm" && task.phase === "proposed" && plan?.state === "confirmed") {
|
|
649
|
+
return false;
|
|
650
|
+
}
|
|
651
|
+
const said = taskLine(action, task, { id: bareId(key), plan });
|
|
652
|
+
if (said?.kind === "refuse") throw new DevRefusal(said.text);
|
|
653
|
+
return said !== undefined;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** As `refuseTask`, for a plan action. */
|
|
657
|
+
function refusePlan(action: PlanActionName, plan: PlanData, key: string): boolean {
|
|
658
|
+
const said = planLine(action, plan, { id: bareId(key) });
|
|
659
|
+
if (said?.kind === "refuse") throw new DevRefusal(said.text);
|
|
660
|
+
return said !== undefined;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
export async function getPlan(
|
|
664
|
+
store: ObjectStore,
|
|
665
|
+
key: string,
|
|
666
|
+
): Promise<StoredObject> {
|
|
667
|
+
const object = await store.get(PLAN_TYPE, key);
|
|
668
|
+
if (object === undefined) {
|
|
669
|
+
throw new DevError("plan", `no plan "${bareId(key)}"`);
|
|
670
|
+
}
|
|
671
|
+
return object;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
export async function getTask(
|
|
675
|
+
store: ObjectStore,
|
|
676
|
+
key: string,
|
|
677
|
+
): Promise<StoredObject> {
|
|
678
|
+
const object = await store.get(TASK_TYPE, key);
|
|
679
|
+
if (object === undefined) {
|
|
680
|
+
throw new DevError("task", `no task "${bareId(key)}"`);
|
|
681
|
+
}
|
|
682
|
+
return object;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
async function writePlan(
|
|
686
|
+
store: ObjectStore,
|
|
687
|
+
id: string,
|
|
688
|
+
data: PlanData,
|
|
689
|
+
cause: string,
|
|
690
|
+
): Promise<StoredObject> {
|
|
691
|
+
const written = await store.put(PLAN_TYPE, id, data, cause);
|
|
692
|
+
// The counts are a cache the scheduler keeps current; a busy store must not
|
|
693
|
+
// turn the plan write that already happened into a reported failure.
|
|
694
|
+
try {
|
|
695
|
+
await refreshRepositoryCounts(store, data.repository);
|
|
696
|
+
} catch (error) {
|
|
697
|
+
if (!isBusy(error)) throw error;
|
|
698
|
+
}
|
|
699
|
+
return written;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
async function writeTask(
|
|
703
|
+
store: ObjectStore,
|
|
704
|
+
id: string,
|
|
705
|
+
data: TaskData,
|
|
706
|
+
cause: string,
|
|
707
|
+
): Promise<StoredObject> {
|
|
708
|
+
return putTaskKeepingPlanCost(store, id, data, cause);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* Writes a task and, when its cost share changed, moves its plan's running
|
|
713
|
+
* cost total by the difference in the same operation. A plan stored before the
|
|
714
|
+
* total existed gets it computed from its tasks now.
|
|
715
|
+
*/
|
|
716
|
+
export async function putTaskKeepingPlanCost(
|
|
717
|
+
store: ObjectStore,
|
|
718
|
+
id: string,
|
|
719
|
+
data: TaskData,
|
|
720
|
+
cause: string,
|
|
721
|
+
): Promise<StoredObject> {
|
|
722
|
+
const before = await store.get(TASK_TYPE, id);
|
|
723
|
+
const written = await store.put(TASK_TYPE, id, data, cause);
|
|
724
|
+
const previous = before === undefined ? undefined : taskData(before.data);
|
|
725
|
+
const a = previous === undefined ? undefined : taskCostShare(previous);
|
|
726
|
+
const b = taskCostShare(data);
|
|
727
|
+
if (a?.micros === b?.micros && a?.quality === b?.quality) return written;
|
|
728
|
+
const plan = await store.get(PLAN_TYPE, storeKey(data.repository, data.plan));
|
|
729
|
+
if (plan === undefined) return written;
|
|
730
|
+
const planned = planData(plan.data);
|
|
731
|
+
const totals =
|
|
732
|
+
planned.costTotals === undefined
|
|
733
|
+
? await totalsOfTasks(store, plan.id)
|
|
734
|
+
: shiftCostTotals(planned.costTotals, previous, data);
|
|
735
|
+
await store.put(PLAN_TYPE, plan.id, { ...planned, costTotals: totals }, "dev:plan-cost");
|
|
736
|
+
return written;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
async function totalsOfTasks(store: ObjectStore, planKey: string): Promise<PlanCostTotals> {
|
|
740
|
+
return sumCostTotals(await tasksOfPlan(store, planKey));
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
function sumCostTotals(tasks: readonly StoredObject[]): PlanCostTotals {
|
|
744
|
+
let totals = shiftCostTotals(undefined, undefined, undefined);
|
|
745
|
+
for (const object of tasks) {
|
|
746
|
+
totals = shiftCostTotals(totals, undefined, taskData(object.data));
|
|
747
|
+
}
|
|
748
|
+
return totals;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
function sameCostTotals(a: PlanCostTotals | undefined, b: PlanCostTotals): boolean {
|
|
752
|
+
return (
|
|
753
|
+
a !== undefined &&
|
|
754
|
+
a.exactMicros === b.exactMicros &&
|
|
755
|
+
a.estimatedMicros === b.estimatedMicros &&
|
|
756
|
+
a.incompleteMicros === b.incompleteMicros &&
|
|
757
|
+
a.exactTasks === b.exactTasks &&
|
|
758
|
+
a.estimatedTasks === b.estimatedTasks &&
|
|
759
|
+
a.incompleteTasks === b.incompleteTasks
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Recomputes the counts a plan row reads, writing the plan only on a change.
|
|
765
|
+
*
|
|
766
|
+
* The counts are a cache the scheduler refreshes on every tick, and callers run
|
|
767
|
+
* this after the write that was the command's work. A store another process
|
|
768
|
+
* keeps busy past the wait must not turn that finished work into a reported
|
|
769
|
+
* failure (a retry would repeat it), so a busy store leaves the counts for the
|
|
770
|
+
* next tick.
|
|
771
|
+
*/
|
|
772
|
+
export async function refreshPlanCounts(
|
|
773
|
+
store: ObjectStore,
|
|
774
|
+
plan: StoredObject,
|
|
775
|
+
): Promise<StoredObject> {
|
|
776
|
+
try {
|
|
777
|
+
return await recountPlan(store, plan);
|
|
778
|
+
} catch (error) {
|
|
779
|
+
if (isBusy(error)) return plan;
|
|
780
|
+
throw error;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/** `refreshPlanCounts` for a plan named by key; its read is tolerated the same way. */
|
|
785
|
+
async function refreshPlanCountsOf(store: ObjectStore, key: string): Promise<void> {
|
|
786
|
+
try {
|
|
787
|
+
await refreshPlanCounts(store, await getPlan(store, key));
|
|
788
|
+
} catch (error) {
|
|
789
|
+
if (!isBusy(error)) throw error;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
async function recountPlan(
|
|
794
|
+
store: ObjectStore,
|
|
795
|
+
plan: StoredObject,
|
|
796
|
+
): Promise<StoredObject> {
|
|
797
|
+
const data = planData(plan.data);
|
|
798
|
+
const tasks = await tasksOfPlan(store, plan.id);
|
|
799
|
+
const taskCount = tasks.length;
|
|
800
|
+
const mergedCount = tasks.filter(
|
|
801
|
+
(object) => taskData(object.data).phase === "merged",
|
|
802
|
+
).length;
|
|
803
|
+
const cancelledCount = tasks.filter(
|
|
804
|
+
(object) => taskData(object.data).phase === "cancelled",
|
|
805
|
+
).length;
|
|
806
|
+
const activeCount = tasks.filter((object) =>
|
|
807
|
+
isActiveTask(taskData(object.data)),
|
|
808
|
+
).length;
|
|
809
|
+
const waitingCount = tasks.filter((object) =>
|
|
810
|
+
needsPerson(taskData(object.data)),
|
|
811
|
+
).length;
|
|
812
|
+
// Recomputed from the tasks each time: the task write and the plan's total
|
|
813
|
+
// are two writes, so a crash or a race between them is healed here.
|
|
814
|
+
const costTotals = sumCostTotals(tasks);
|
|
815
|
+
const flowHeld = tasks
|
|
816
|
+
.map((object) => taskData(object.data))
|
|
817
|
+
.find((task) => task.phase === "ready" && task.flowHeld !== undefined)?.flowHeld;
|
|
818
|
+
if (
|
|
819
|
+
data.flowHeld === flowHeld &&
|
|
820
|
+
sameCostTotals(data.costTotals, costTotals) &&
|
|
821
|
+
data.taskCount === taskCount &&
|
|
822
|
+
data.mergedCount === mergedCount &&
|
|
823
|
+
data.cancelledCount === cancelledCount &&
|
|
824
|
+
(data.activeCount ?? 0) === activeCount &&
|
|
825
|
+
(data.waitingCount ?? 0) === waitingCount
|
|
826
|
+
) {
|
|
827
|
+
return plan;
|
|
828
|
+
}
|
|
829
|
+
const next: PlanData = {
|
|
830
|
+
...data,
|
|
831
|
+
taskCount,
|
|
832
|
+
mergedCount,
|
|
833
|
+
cancelledCount,
|
|
834
|
+
activeCount,
|
|
835
|
+
waitingCount,
|
|
836
|
+
costTotals,
|
|
837
|
+
flowHeld,
|
|
838
|
+
// A new task reopens a plan the scheduler already called finished.
|
|
839
|
+
...(taskCount > 0 && mergedCount + cancelledCount === taskCount
|
|
840
|
+
? {}
|
|
841
|
+
: { finishedReported: false }),
|
|
842
|
+
};
|
|
843
|
+
return writePlan(store, plan.id, next, "dev:refresh-plan");
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
export interface AddPlanOptions {
|
|
847
|
+
readonly repository: RepositoryData;
|
|
848
|
+
readonly slug: string;
|
|
849
|
+
readonly title: string;
|
|
850
|
+
readonly request?: string | undefined;
|
|
851
|
+
readonly concurrency?: number | undefined;
|
|
852
|
+
/** The auto level the plan starts at; `off` when unset. */
|
|
853
|
+
readonly auto?: AutoMode | undefined;
|
|
854
|
+
/** The flow the plan's tasks start with; unset means `default`. */
|
|
855
|
+
readonly flow?: string | undefined;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/** The fixed range of a plan's concurrency. */
|
|
859
|
+
export const MIN_PLAN_CONCURRENCY = 1;
|
|
860
|
+
export const MAX_PLAN_CONCURRENCY = 16;
|
|
861
|
+
|
|
862
|
+
export function validatePlanConcurrency(value: unknown): number {
|
|
863
|
+
if (
|
|
864
|
+
typeof value !== "number" ||
|
|
865
|
+
!Number.isInteger(value) ||
|
|
866
|
+
value < MIN_PLAN_CONCURRENCY ||
|
|
867
|
+
value > MAX_PLAN_CONCURRENCY
|
|
868
|
+
) {
|
|
869
|
+
throw new DevError(
|
|
870
|
+
"concurrency",
|
|
871
|
+
`must be an integer from ${MIN_PLAN_CONCURRENCY} to ${MAX_PLAN_CONCURRENCY}`,
|
|
872
|
+
);
|
|
873
|
+
}
|
|
874
|
+
return value;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
function validateFlowSelection(value: string | undefined): string | undefined {
|
|
878
|
+
if (value === undefined) return undefined;
|
|
879
|
+
const name = value.trim();
|
|
880
|
+
if (name.length === 0) throw new DevError("flow", "must be a non-empty name");
|
|
881
|
+
return name;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
export async function addPlan(
|
|
885
|
+
store: ObjectStore,
|
|
886
|
+
options: AddPlanOptions,
|
|
887
|
+
): Promise<StoredObject> {
|
|
888
|
+
const slug = validateSlug(options.slug);
|
|
889
|
+
const title = validateTitle(options.title);
|
|
890
|
+
const concurrency =
|
|
891
|
+
options.concurrency === undefined
|
|
892
|
+
? 1
|
|
893
|
+
: validatePlanConcurrency(options.concurrency);
|
|
894
|
+
return withRepoLock(store, options.repository.name, async (repository) => {
|
|
895
|
+
const id = await nextId(store, "plan", repository.name, slug);
|
|
896
|
+
const data: PlanData = {
|
|
897
|
+
repository: repository.name,
|
|
898
|
+
title,
|
|
899
|
+
request: options.request ?? "",
|
|
900
|
+
status: "draft",
|
|
901
|
+
auto: options.auto ?? "off",
|
|
902
|
+
priority: "P3",
|
|
903
|
+
concurrency,
|
|
904
|
+
taskCount: 0,
|
|
905
|
+
mergedCount: 0,
|
|
906
|
+
cancelledCount: 0,
|
|
907
|
+
...(options.flow === undefined ? {} : { flow: validateFlowSelection(options.flow) }),
|
|
908
|
+
};
|
|
909
|
+
return writePlan(store, storeKey(repository.name, id), data, "dev:plan-add");
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* Makes a confirmed plan with no tasks yet; the caller holds the repository
|
|
915
|
+
* lock. Used for the plan dev keeps flaky-test tasks in.
|
|
916
|
+
*/
|
|
917
|
+
export async function addConfirmedPlanLocked(
|
|
918
|
+
store: ObjectStore,
|
|
919
|
+
repository: RepositoryData,
|
|
920
|
+
options: {
|
|
921
|
+
readonly slug: string;
|
|
922
|
+
readonly title: string;
|
|
923
|
+
readonly auto: AutoMode;
|
|
924
|
+
readonly priority: Priority;
|
|
925
|
+
},
|
|
926
|
+
): Promise<StoredObject> {
|
|
927
|
+
const slug = validateSlug(options.slug);
|
|
928
|
+
const title = validateTitle(options.title);
|
|
929
|
+
const id = await nextId(store, "plan", repository.name, slug);
|
|
930
|
+
const data: PlanData = {
|
|
931
|
+
repository: repository.name,
|
|
932
|
+
title,
|
|
933
|
+
request: "",
|
|
934
|
+
status: "confirmed",
|
|
935
|
+
auto: options.auto,
|
|
936
|
+
priority: options.priority,
|
|
937
|
+
concurrency: 1,
|
|
938
|
+
taskCount: 0,
|
|
939
|
+
mergedCount: 0,
|
|
940
|
+
cancelledCount: 0,
|
|
941
|
+
};
|
|
942
|
+
return writePlan(store, storeKey(repository.name, id), data, "dev:plan-add");
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
async function registeredNames(store: ObjectStore): Promise<ReadonlySet<string>> {
|
|
946
|
+
return new Set((await listRepositories(store)).map((item) => item.name));
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
/**
|
|
950
|
+
* Checks and normalizes `--after`: each entry is a task id of the task's own
|
|
951
|
+
* repository (any plan) or `<repository>/<task id>`; it is stored as the full
|
|
952
|
+
* key. A task cannot follow itself or close a cycle across plans and
|
|
953
|
+
* repositories.
|
|
954
|
+
*/
|
|
955
|
+
function validateAfter(
|
|
956
|
+
tasks: readonly StoredObject[],
|
|
957
|
+
repository: string,
|
|
958
|
+
taskKey: string,
|
|
959
|
+
after: readonly string[],
|
|
960
|
+
registered: ReadonlySet<string>,
|
|
961
|
+
): readonly string[] {
|
|
962
|
+
const unique: string[] = [];
|
|
963
|
+
for (const entry of after) {
|
|
964
|
+
const key = dependencyKey(repository, entry);
|
|
965
|
+
if (!unique.includes(key)) unique.push(key);
|
|
966
|
+
}
|
|
967
|
+
const known = new Set(tasks.map((object) => object.id));
|
|
968
|
+
for (const key of unique) {
|
|
969
|
+
if (key === taskKey) throw new DevError("after", "a task cannot follow itself");
|
|
970
|
+
if (!known.has(key) || !registered.has(keyRepository(key))) throw new DevError("after", `no task "${dependencyLabel(repository, key)}"`);
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
const follows = new Map<string, readonly string[]>();
|
|
974
|
+
for (const object of tasks) {
|
|
975
|
+
const data = taskData(object.data);
|
|
976
|
+
follows.set(
|
|
977
|
+
object.id,
|
|
978
|
+
data.after.map((entry) => dependencyKey(data.repository, entry)),
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
follows.set(taskKey, unique);
|
|
982
|
+
|
|
983
|
+
const visiting: string[] = [];
|
|
984
|
+
const done = new Set<string>();
|
|
985
|
+
const visit = (key: string): void => {
|
|
986
|
+
if (done.has(key)) return;
|
|
987
|
+
const at = visiting.indexOf(key);
|
|
988
|
+
if (at >= 0) {
|
|
989
|
+
const path = [...visiting.slice(at), key].map((id) =>
|
|
990
|
+
dependencyLabel(repository, id),
|
|
991
|
+
);
|
|
992
|
+
throw new DevError("after", `cycle: ${path.join(" -> ")}`);
|
|
993
|
+
}
|
|
994
|
+
visiting.push(key);
|
|
995
|
+
for (const next of follows.get(key) ?? []) visit(next);
|
|
996
|
+
visiting.pop();
|
|
997
|
+
done.add(key);
|
|
998
|
+
};
|
|
999
|
+
visit(taskKey);
|
|
1000
|
+
return unique;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
export interface AddTaskOptions {
|
|
1004
|
+
readonly plan: string;
|
|
1005
|
+
readonly slug: string;
|
|
1006
|
+
readonly title: string;
|
|
1007
|
+
readonly brief?: string | undefined;
|
|
1008
|
+
readonly after?: readonly string[] | undefined;
|
|
1009
|
+
readonly area?: string | undefined;
|
|
1010
|
+
/** The tasks this follow-up collects; set only by the follow-up collection. */
|
|
1011
|
+
readonly followupOf?: readonly string[] | undefined;
|
|
1012
|
+
/** Set by the code in dev that proposes tasks on its own. */
|
|
1013
|
+
readonly openedBy?: "dev" | undefined;
|
|
1014
|
+
/** The flow this task starts with; unset inherits the plan's flow. */
|
|
1015
|
+
readonly flow?: string | undefined;
|
|
1016
|
+
/** The chat worktree name the task works in; checked by the caller. */
|
|
1017
|
+
readonly worktree?: string | undefined;
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
export async function addTask(
|
|
1021
|
+
store: ObjectStore,
|
|
1022
|
+
options: AddTaskOptions,
|
|
1023
|
+
): Promise<StoredObject> {
|
|
1024
|
+
const slug = validateSlug(options.slug);
|
|
1025
|
+
const title = validateTitle(options.title);
|
|
1026
|
+
const plan = await getPlan(store, options.plan);
|
|
1027
|
+
const planObject = planData(plan.data);
|
|
1028
|
+
if (planObject.status === "cancelled") {
|
|
1029
|
+
throw new DevError("plan", `plan "${bareId(plan.id)}" is cancelled`);
|
|
1030
|
+
}
|
|
1031
|
+
await devTestHooks.beforeLock?.("task-add");
|
|
1032
|
+
const names = await afterLockNames(
|
|
1033
|
+
store,
|
|
1034
|
+
planObject.repository,
|
|
1035
|
+
options.after ?? [],
|
|
1036
|
+
);
|
|
1037
|
+
return withRepoLocks(store, names, async () =>
|
|
1038
|
+
addTaskLocked(store, options, await repositoryOf(store, planObject.repository)),
|
|
1039
|
+
);
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
/** Adds the task; the caller holds the repository's lock. */
|
|
1043
|
+
export async function addTaskLocked(
|
|
1044
|
+
store: ObjectStore,
|
|
1045
|
+
options: AddTaskOptions,
|
|
1046
|
+
repository: RepositoryData,
|
|
1047
|
+
): Promise<StoredObject> {
|
|
1048
|
+
const slug = validateSlug(options.slug);
|
|
1049
|
+
const title = validateTitle(options.title);
|
|
1050
|
+
const currentPlan = await getPlan(store, options.plan);
|
|
1051
|
+
const current = planData(currentPlan.data);
|
|
1052
|
+
if (current.status === "cancelled") {
|
|
1053
|
+
throw new DevError(
|
|
1054
|
+
"plan",
|
|
1055
|
+
`plan "${bareId(currentPlan.id)}" is cancelled`,
|
|
1056
|
+
);
|
|
1057
|
+
}
|
|
1058
|
+
if (current.repository !== repository.name) {
|
|
1059
|
+
throw new DevError(
|
|
1060
|
+
"plan",
|
|
1061
|
+
`plan "${bareId(currentPlan.id)}" is not in repository "${repository.name}"`,
|
|
1062
|
+
);
|
|
1063
|
+
}
|
|
1064
|
+
const id = await nextId(store, "task", repository.name, slug);
|
|
1065
|
+
const tasks = await allTasks(store);
|
|
1066
|
+
const after = validateAfter(
|
|
1067
|
+
tasks,
|
|
1068
|
+
repository.name,
|
|
1069
|
+
storeKey(repository.name, id),
|
|
1070
|
+
options.after ?? [],
|
|
1071
|
+
await registeredNames(store),
|
|
1072
|
+
);
|
|
1073
|
+
const brief = appendChange(options.brief ?? "", "created");
|
|
1074
|
+
const area =
|
|
1075
|
+
options.area === undefined ? undefined : validateArea(options.area);
|
|
1076
|
+
const data: TaskData = {
|
|
1077
|
+
repository: repository.name,
|
|
1078
|
+
plan: bareId(currentPlan.id),
|
|
1079
|
+
title,
|
|
1080
|
+
brief,
|
|
1081
|
+
after,
|
|
1082
|
+
...(area === undefined ? {} : { area }),
|
|
1083
|
+
...(options.followupOf === undefined ? {} : { followupOf: options.followupOf }),
|
|
1084
|
+
...(options.openedBy === undefined ? {} : { openedBy: options.openedBy }),
|
|
1085
|
+
...(options.flow === undefined ? {} : { flow: validateFlowSelection(options.flow) }),
|
|
1086
|
+
...(options.worktree === undefined ? {} : { worktree: options.worktree }),
|
|
1087
|
+
phase: "proposed",
|
|
1088
|
+
attempts: 0,
|
|
1089
|
+
run: null,
|
|
1090
|
+
};
|
|
1091
|
+
const written = await writeTask(
|
|
1092
|
+
store,
|
|
1093
|
+
storeKey(repository.name, id),
|
|
1094
|
+
data,
|
|
1095
|
+
"dev:task-add",
|
|
1096
|
+
);
|
|
1097
|
+
await refreshPlanCounts(store, currentPlan);
|
|
1098
|
+
return written;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
function requireEditable(task: TaskData): void {
|
|
1102
|
+
if (task.phase !== "proposed" && task.phase !== "ready") {
|
|
1103
|
+
throw new DevError(
|
|
1104
|
+
"phase",
|
|
1105
|
+
`task is ${task.phase}; only proposed or ready tasks can change`,
|
|
1106
|
+
);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
/** Replaces the brief body, keeping rework and the changes section. */
|
|
1111
|
+
export async function editTaskBrief(
|
|
1112
|
+
store: ObjectStore,
|
|
1113
|
+
id: string,
|
|
1114
|
+
body: string,
|
|
1115
|
+
): Promise<StoredObject> {
|
|
1116
|
+
const task = await getTask(store, id);
|
|
1117
|
+
const data = taskData(task.data);
|
|
1118
|
+
requireEditable(data);
|
|
1119
|
+
return withRepoLock(store, data.repository, async () => {
|
|
1120
|
+
const current = await getTask(store, id);
|
|
1121
|
+
const latest = taskData(current.data);
|
|
1122
|
+
requireEditable(latest);
|
|
1123
|
+
const next = editBrief(latest.brief, body);
|
|
1124
|
+
if (next === latest.brief) return current;
|
|
1125
|
+
return writeTask(
|
|
1126
|
+
store,
|
|
1127
|
+
id,
|
|
1128
|
+
{ ...latest, brief: next },
|
|
1129
|
+
"dev:task-edit",
|
|
1130
|
+
);
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
export interface UpdateTaskOptions {
|
|
1135
|
+
readonly title?: string | undefined;
|
|
1136
|
+
readonly after?: readonly string[] | undefined;
|
|
1137
|
+
/** A new area; the empty string clears it. */
|
|
1138
|
+
readonly area?: string | undefined;
|
|
1139
|
+
/** The flow before the task's first attempt. */
|
|
1140
|
+
readonly flow?: string | undefined;
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
/**
|
|
1144
|
+
* True while a task waits only because a task it follows was cancelled: its
|
|
1145
|
+
* dependencies are the cause, so changing them releases the wait.
|
|
1146
|
+
*/
|
|
1147
|
+
function waitsForCancelledDependency(
|
|
1148
|
+
data: TaskData,
|
|
1149
|
+
tasks: readonly StoredObject[],
|
|
1150
|
+
): boolean {
|
|
1151
|
+
if (data.phase !== "waiting") return false;
|
|
1152
|
+
return data.after.some((dependency) =>
|
|
1153
|
+
tasks.some(
|
|
1154
|
+
(object) =>
|
|
1155
|
+
object.id === dependencyKey(data.repository, dependency) &&
|
|
1156
|
+
taskData(object.data).phase === "cancelled",
|
|
1157
|
+
),
|
|
1158
|
+
);
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
export async function updateTask(
|
|
1162
|
+
store: ObjectStore,
|
|
1163
|
+
id: string,
|
|
1164
|
+
options: UpdateTaskOptions & { readonly runs?: DevRunControl | undefined },
|
|
1165
|
+
): Promise<StoredObject> {
|
|
1166
|
+
if (
|
|
1167
|
+
options.title === undefined &&
|
|
1168
|
+
options.after === undefined &&
|
|
1169
|
+
options.area === undefined &&
|
|
1170
|
+
options.flow === undefined
|
|
1171
|
+
) {
|
|
1172
|
+
throw new DevError(
|
|
1173
|
+
"update",
|
|
1174
|
+
"nothing to change; pass --title, --after, --area or --flow",
|
|
1175
|
+
);
|
|
1176
|
+
}
|
|
1177
|
+
const title =
|
|
1178
|
+
options.title === undefined ? undefined : validateTitle(options.title);
|
|
1179
|
+
const area =
|
|
1180
|
+
options.area === undefined ? undefined : validateArea(options.area);
|
|
1181
|
+
const flow =
|
|
1182
|
+
options.flow === undefined ? undefined : validateFlowSelection(options.flow);
|
|
1183
|
+
const initial = await getTask(store, id);
|
|
1184
|
+
const initialData = taskData(initial.data);
|
|
1185
|
+
const releasable = (data: TaskData, tasks: readonly StoredObject[]) =>
|
|
1186
|
+
options.after !== undefined && waitsForCancelledDependency(data, tasks);
|
|
1187
|
+
if (!releasable(initialData, await allTasks(store))) requireEditable(initialData);
|
|
1188
|
+
const lockNames = await afterLockNames(
|
|
1189
|
+
store,
|
|
1190
|
+
initialData.repository,
|
|
1191
|
+
options.after ?? [],
|
|
1192
|
+
);
|
|
1193
|
+
const updated = await withRepoLocks(store, lockNames, async () => {
|
|
1194
|
+
const current = await getTask(store, id);
|
|
1195
|
+
const tasks = await allTasks(store);
|
|
1196
|
+
const data = taskData(current.data);
|
|
1197
|
+
const releases = releasable(data, tasks);
|
|
1198
|
+
if (!releases) requireEditable(data);
|
|
1199
|
+
if (flow !== undefined && flow !== data.flow && (data.attempts > 0 || data.flowName !== undefined)) {
|
|
1200
|
+
throw new DevError(
|
|
1201
|
+
"--flow",
|
|
1202
|
+
"the task has already started an attempt on its flow; a task keeps its flow after its first attempt",
|
|
1203
|
+
);
|
|
1204
|
+
}
|
|
1205
|
+
const after =
|
|
1206
|
+
options.after === undefined
|
|
1207
|
+
? data.after
|
|
1208
|
+
: validateAfter(
|
|
1209
|
+
tasks,
|
|
1210
|
+
data.repository,
|
|
1211
|
+
id,
|
|
1212
|
+
options.after,
|
|
1213
|
+
await registeredNames(store),
|
|
1214
|
+
);
|
|
1215
|
+
const lines: string[] = [];
|
|
1216
|
+
if (title !== undefined && title !== data.title) {
|
|
1217
|
+
lines.push(`title: ${title}`);
|
|
1218
|
+
}
|
|
1219
|
+
if (options.after !== undefined) {
|
|
1220
|
+
lines.push(
|
|
1221
|
+
after.length === 0 ? "after: none" : `after: ${after.join(", ")}`,
|
|
1222
|
+
);
|
|
1223
|
+
}
|
|
1224
|
+
const areaChanged = options.area !== undefined && area !== data.area;
|
|
1225
|
+
if (areaChanged) {
|
|
1226
|
+
lines.push(area === undefined ? "area: none" : `area: ${area}`);
|
|
1227
|
+
}
|
|
1228
|
+
if (flow !== undefined && flow !== data.flow) {
|
|
1229
|
+
lines.push(`flow: ${flow}`);
|
|
1230
|
+
}
|
|
1231
|
+
let brief = data.brief;
|
|
1232
|
+
for (const line of lines) brief = appendChange(brief, line);
|
|
1233
|
+
// A new area drops the old hold; the next tick decides it again.
|
|
1234
|
+
const {
|
|
1235
|
+
area: _area,
|
|
1236
|
+
areaHeldBy: _held,
|
|
1237
|
+
areaAhead: _ahead,
|
|
1238
|
+
dependencyHeld: _waits,
|
|
1239
|
+
dependencyHeldFor: _waitsFor,
|
|
1240
|
+
slotHeld: _slot,
|
|
1241
|
+
...kept
|
|
1242
|
+
} = data;
|
|
1243
|
+
const nextArea = options.area === undefined ? data.area : area;
|
|
1244
|
+
const nextHeld = areaChanged ? undefined : data.areaHeldBy;
|
|
1245
|
+
const next: TaskData = {
|
|
1246
|
+
...kept,
|
|
1247
|
+
...(title === undefined ? {} : { title }),
|
|
1248
|
+
after,
|
|
1249
|
+
// The wait on a cancelled dependency ends: the task is ready again and
|
|
1250
|
+
// starts on a later tick like any ready task.
|
|
1251
|
+
...(releases ? { phase: "ready" as const, run: null, claim: null } : {}),
|
|
1252
|
+
brief: releases ? appendChange(brief, "ready") : brief,
|
|
1253
|
+
...(nextArea === undefined ? {} : { area: nextArea }),
|
|
1254
|
+
...(flow === undefined ? {} : { flow }),
|
|
1255
|
+
...(nextHeld === undefined ? {} : { areaHeldBy: nextHeld }),
|
|
1256
|
+
...(nextHeld === undefined || data.areaAhead === undefined
|
|
1257
|
+
? {}
|
|
1258
|
+
: { areaAhead: data.areaAhead }),
|
|
1259
|
+
};
|
|
1260
|
+
const object = await writeTask(store, id, next, "dev:task-update");
|
|
1261
|
+
if (releases) {
|
|
1262
|
+
await refreshPlanCountsOf(store, storeKey(data.repository, data.plan));
|
|
1263
|
+
}
|
|
1264
|
+
return {
|
|
1265
|
+
object,
|
|
1266
|
+
released:
|
|
1267
|
+
releases && data.run !== null
|
|
1268
|
+
? { run: data.run, flow: data.flowGraph }
|
|
1269
|
+
: null,
|
|
1270
|
+
};
|
|
1271
|
+
});
|
|
1272
|
+
if (updated.released !== null) await cancelRuns(options.runs, [updated.released]);
|
|
1273
|
+
return updated.object;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
/** The task after a run changed its phase. Used by later tasks and by tests. */
|
|
1277
|
+
export async function setTaskPhase(
|
|
1278
|
+
store: ObjectStore,
|
|
1279
|
+
id: string,
|
|
1280
|
+
phase: TaskPhase,
|
|
1281
|
+
options: { readonly run?: string | null | undefined } = {},
|
|
1282
|
+
): Promise<StoredObject> {
|
|
1283
|
+
const written = await changeTaskPhase(store, id, undefined, phase, options);
|
|
1284
|
+
if (written === undefined) throw new DevError("task", `no task "${bareId(id)}"`);
|
|
1285
|
+
return written;
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
/**
|
|
1289
|
+
* Moves a task to `to` only while it is still in phase `from`, checked under
|
|
1290
|
+
* the repository lock; undefined when another pass already moved it, so only
|
|
1291
|
+
* the pass that moved it reports the change.
|
|
1292
|
+
*/
|
|
1293
|
+
export async function moveTaskPhase(
|
|
1294
|
+
store: ObjectStore,
|
|
1295
|
+
id: string,
|
|
1296
|
+
from: TaskPhase,
|
|
1297
|
+
to: TaskPhase,
|
|
1298
|
+
options: { readonly run?: string | null | undefined } = {},
|
|
1299
|
+
): Promise<StoredObject | undefined> {
|
|
1300
|
+
return changeTaskPhase(store, id, from, to, options);
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
async function changeTaskPhase(
|
|
1304
|
+
store: ObjectStore,
|
|
1305
|
+
id: string,
|
|
1306
|
+
from: TaskPhase | undefined,
|
|
1307
|
+
phase: TaskPhase,
|
|
1308
|
+
options: { readonly run?: string | null | undefined },
|
|
1309
|
+
): Promise<StoredObject | undefined> {
|
|
1310
|
+
const initial = await getTask(store, id);
|
|
1311
|
+
const data = taskData(initial.data);
|
|
1312
|
+
return withRepoLock(store, data.repository, async () => {
|
|
1313
|
+
const current = await getTask(store, id);
|
|
1314
|
+
const latest = taskData(current.data);
|
|
1315
|
+
if (from !== undefined && latest.phase !== from) return undefined;
|
|
1316
|
+
const run =
|
|
1317
|
+
options.run === undefined
|
|
1318
|
+
? phase === "running" || phase === "review" || phase === "waiting"
|
|
1319
|
+
? latest.run
|
|
1320
|
+
: null
|
|
1321
|
+
: options.run;
|
|
1322
|
+
const brief = appendChange(latest.brief, `phase: ${phase}`);
|
|
1323
|
+
const written = await writeTask(
|
|
1324
|
+
store,
|
|
1325
|
+
id,
|
|
1326
|
+
{ ...latest, phase, run, brief },
|
|
1327
|
+
`dev:task-${phase}`,
|
|
1328
|
+
);
|
|
1329
|
+
await refreshPlanCountsOf(store, storeKey(latest.repository, latest.plan));
|
|
1330
|
+
return written;
|
|
1331
|
+
});
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
/**
|
|
1335
|
+
* Marks a ready task as running before the kernel starts its flow, so the next
|
|
1336
|
+
* tick does not start it a second time. The claim carries a token that the
|
|
1337
|
+
* flow's steps check before writing the task, and the worktree step records
|
|
1338
|
+
* the run id when the child process reaches it. A manual start request stays
|
|
1339
|
+
* on the task so a crash before the run records itself does not lose it.
|
|
1340
|
+
*/
|
|
1341
|
+
export async function claimTaskStart(
|
|
1342
|
+
store: ObjectStore,
|
|
1343
|
+
id: string,
|
|
1344
|
+
options: {
|
|
1345
|
+
readonly checkArea?: boolean | undefined;
|
|
1346
|
+
readonly oldestPlanSlot?: boolean | undefined;
|
|
1347
|
+
/** The flow this start runs; its graph is kept on the task for recovery. */
|
|
1348
|
+
readonly flow?: { readonly name: string; readonly graph?: FlowDefinition | undefined } | undefined;
|
|
1349
|
+
} = {},
|
|
1350
|
+
): Promise<TaskData | undefined> {
|
|
1351
|
+
const initial = await getTask(store, id);
|
|
1352
|
+
const data = taskData(initial.data);
|
|
1353
|
+
return withRepoLock(store, data.repository, async () => {
|
|
1354
|
+
const current = await getTask(store, id);
|
|
1355
|
+
const latest = taskData(current.data);
|
|
1356
|
+
if (latest.phase !== "ready") return undefined;
|
|
1357
|
+
// The area check and the claim are one decision under the repository
|
|
1358
|
+
// lock: whoever holds it reads the stored tasks, so two schedulers (two
|
|
1359
|
+
// ticks, or two processes) can never both start a task of one area.
|
|
1360
|
+
if (options.checkArea !== false && latest.area !== undefined) {
|
|
1361
|
+
const holder = await areaHolder(store, latest.repository, latest.area, id);
|
|
1362
|
+
if (holder !== undefined) return undefined;
|
|
1363
|
+
}
|
|
1364
|
+
const {
|
|
1365
|
+
areaHeldBy: _held,
|
|
1366
|
+
areaAhead: _ahead,
|
|
1367
|
+
dependencyHeld: _waits,
|
|
1368
|
+
dependencyHeldFor: _waitsFor,
|
|
1369
|
+
slotHeld: _slot,
|
|
1370
|
+
oldestPlanSlot: _oldest,
|
|
1371
|
+
...rest
|
|
1372
|
+
} = latest;
|
|
1373
|
+
const next: TaskData = {
|
|
1374
|
+
...rest,
|
|
1375
|
+
...(options.oldestPlanSlot === true ? { oldestPlanSlot: true } : {}),
|
|
1376
|
+
...(options.flow === undefined
|
|
1377
|
+
? {}
|
|
1378
|
+
: {
|
|
1379
|
+
flowName: options.flow.name,
|
|
1380
|
+
...(options.flow.graph === undefined ? {} : { flowGraph: options.flow.graph }),
|
|
1381
|
+
}),
|
|
1382
|
+
phase: "running",
|
|
1383
|
+
run: null,
|
|
1384
|
+
brief: appendChange(latest.brief, "running"),
|
|
1385
|
+
claim: { token: crypto.randomUUID(), phase: "running" },
|
|
1386
|
+
};
|
|
1387
|
+
await writeTask(store, id, next, "dev:task-start");
|
|
1388
|
+
return next;
|
|
1389
|
+
});
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
/** The id of a running or reviewing task that holds the area, if any. */
|
|
1393
|
+
export async function areaHolder(
|
|
1394
|
+
store: ObjectStore,
|
|
1395
|
+
repository: string,
|
|
1396
|
+
area: string,
|
|
1397
|
+
except: string,
|
|
1398
|
+
): Promise<string | undefined> {
|
|
1399
|
+
for (const object of await allTasks(store)) {
|
|
1400
|
+
if (object.id === except) continue;
|
|
1401
|
+
const task = taskData(object.data);
|
|
1402
|
+
if (task.repository !== repository || task.area !== area) continue;
|
|
1403
|
+
if (task.phase === "running" || task.phase === "review") {
|
|
1404
|
+
return bareId(object.id);
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
return undefined;
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* Records which task holds a ready task's area, or clears the record, so a
|
|
1412
|
+
* list can say why the task is waiting. Writes only on a change.
|
|
1413
|
+
*/
|
|
1414
|
+
export async function setAreaHold(
|
|
1415
|
+
store: ObjectStore,
|
|
1416
|
+
id: string,
|
|
1417
|
+
holder: string | undefined,
|
|
1418
|
+
ahead = 0,
|
|
1419
|
+
): Promise<void> {
|
|
1420
|
+
const same = (task: TaskData): boolean =>
|
|
1421
|
+
task.areaHeldBy === holder &&
|
|
1422
|
+
(holder === undefined || (task.areaAhead ?? 0) === ahead);
|
|
1423
|
+
const initial = await getTask(store, id);
|
|
1424
|
+
const data = taskData(initial.data);
|
|
1425
|
+
if (same(data)) return;
|
|
1426
|
+
await withRepoLock(store, data.repository, async () => {
|
|
1427
|
+
const current = await getTask(store, id);
|
|
1428
|
+
const latest = taskData(current.data);
|
|
1429
|
+
if (latest.phase !== "ready" || same(latest)) return;
|
|
1430
|
+
const { areaHeldBy: _held, areaAhead: _ahead, ...rest } = latest;
|
|
1431
|
+
await writeTask(
|
|
1432
|
+
store,
|
|
1433
|
+
id,
|
|
1434
|
+
{
|
|
1435
|
+
...rest,
|
|
1436
|
+
...(holder === undefined ? {} : { areaHeldBy: holder }),
|
|
1437
|
+
...(holder === undefined || ahead === 0 ? {} : { areaAhead: ahead }),
|
|
1438
|
+
},
|
|
1439
|
+
"dev:task-area-hold",
|
|
1440
|
+
);
|
|
1441
|
+
});
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
/**
|
|
1445
|
+
* Records that a ready task waits for a task it follows, or clears the record,
|
|
1446
|
+
* so a row can say the task is blocked. Writes only on a change.
|
|
1447
|
+
*/
|
|
1448
|
+
export async function setDependencyHold(
|
|
1449
|
+
store: ObjectStore,
|
|
1450
|
+
id: string,
|
|
1451
|
+
held: boolean,
|
|
1452
|
+
waitingFor: readonly string[] = [],
|
|
1453
|
+
): Promise<void> {
|
|
1454
|
+
const initial = await getTask(store, id);
|
|
1455
|
+
const data = taskData(initial.data);
|
|
1456
|
+
const same = (left: readonly string[] | undefined) =>
|
|
1457
|
+
(left ?? []).join("\n") === waitingFor.join("\n");
|
|
1458
|
+
if ((data.dependencyHeld === true) === held && (!held || same(data.dependencyHeldFor))) {
|
|
1459
|
+
return;
|
|
1460
|
+
}
|
|
1461
|
+
await withRepoLock(store, data.repository, async () => {
|
|
1462
|
+
const current = await getTask(store, id);
|
|
1463
|
+
const latest = taskData(current.data);
|
|
1464
|
+
if (
|
|
1465
|
+
latest.phase !== "ready" ||
|
|
1466
|
+
((latest.dependencyHeld === true) === held &&
|
|
1467
|
+
(!held || same(latest.dependencyHeldFor)))
|
|
1468
|
+
) {
|
|
1469
|
+
return;
|
|
1470
|
+
}
|
|
1471
|
+
const { dependencyHeld: _waits, dependencyHeldFor: _for, ...rest } = latest;
|
|
1472
|
+
await writeTask(
|
|
1473
|
+
store,
|
|
1474
|
+
id,
|
|
1475
|
+
{
|
|
1476
|
+
...rest,
|
|
1477
|
+
...(held
|
|
1478
|
+
? { dependencyHeld: true, dependencyHeldFor: [...waitingFor] }
|
|
1479
|
+
: {}),
|
|
1480
|
+
},
|
|
1481
|
+
"dev:task-dependency-hold",
|
|
1482
|
+
);
|
|
1483
|
+
});
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
/**
|
|
1487
|
+
* Records why a ready task whose dependencies and area are free does not
|
|
1488
|
+
* start (a limit on running tasks, or a missing plugin), or clears the note.
|
|
1489
|
+
* Writes only on a change.
|
|
1490
|
+
*/
|
|
1491
|
+
export async function setSlotHold(
|
|
1492
|
+
store: ObjectStore,
|
|
1493
|
+
id: string,
|
|
1494
|
+
note: string | undefined,
|
|
1495
|
+
): Promise<void> {
|
|
1496
|
+
const initial = await getTask(store, id);
|
|
1497
|
+
const data = taskData(initial.data);
|
|
1498
|
+
if (data.slotHeld === note) return;
|
|
1499
|
+
await withRepoLock(store, data.repository, async () => {
|
|
1500
|
+
const current = await getTask(store, id);
|
|
1501
|
+
const latest = taskData(current.data);
|
|
1502
|
+
if (latest.phase !== "ready" || latest.slotHeld === note) return;
|
|
1503
|
+
const { slotHeld: _slot, ...rest } = latest;
|
|
1504
|
+
await writeTask(
|
|
1505
|
+
store,
|
|
1506
|
+
id,
|
|
1507
|
+
{ ...rest, ...(note === undefined ? {} : { slotHeld: note }) },
|
|
1508
|
+
"dev:task-slot-hold",
|
|
1509
|
+
);
|
|
1510
|
+
});
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
/**
|
|
1514
|
+
* Records which flow a row names and why a ready task cannot start for its
|
|
1515
|
+
* flow, or clears the record. Writes only on a change.
|
|
1516
|
+
*/
|
|
1517
|
+
export async function setFlowNote(
|
|
1518
|
+
store: ObjectStore,
|
|
1519
|
+
id: string,
|
|
1520
|
+
note: { readonly shown?: string | undefined; readonly held?: string | undefined },
|
|
1521
|
+
): Promise<void> {
|
|
1522
|
+
const initial = await getTask(store, id);
|
|
1523
|
+
const same = (data: TaskData) =>
|
|
1524
|
+
data.flowShown === note.shown && data.flowHeld === note.held;
|
|
1525
|
+
if (same(taskData(initial.data))) return;
|
|
1526
|
+
await withRepoLock(store, taskData(initial.data).repository, async () => {
|
|
1527
|
+
const current = await getTask(store, id);
|
|
1528
|
+
const latest = taskData(current.data);
|
|
1529
|
+
if (same(latest)) return;
|
|
1530
|
+
const { flowShown: _shown, flowHeld: _held, ...rest } = latest;
|
|
1531
|
+
await writeTask(
|
|
1532
|
+
store,
|
|
1533
|
+
id,
|
|
1534
|
+
{
|
|
1535
|
+
...rest,
|
|
1536
|
+
...(note.shown === undefined ? {} : { flowShown: note.shown }),
|
|
1537
|
+
...(note.held === undefined ? {} : { flowHeld: note.held }),
|
|
1538
|
+
},
|
|
1539
|
+
"dev:task-flow-note",
|
|
1540
|
+
);
|
|
1541
|
+
});
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
/** Records when a plan last started a task, under the repository lock. */
|
|
1545
|
+
export async function recordPlanStart(
|
|
1546
|
+
store: ObjectStore,
|
|
1547
|
+
id: string,
|
|
1548
|
+
at: number,
|
|
1549
|
+
): Promise<void> {
|
|
1550
|
+
const initial = await getPlan(store, id);
|
|
1551
|
+
await withRepoLock(store, planData(initial.data).repository, async () => {
|
|
1552
|
+
const latest = await getPlan(store, id);
|
|
1553
|
+
await store.put(
|
|
1554
|
+
"plan",
|
|
1555
|
+
id,
|
|
1556
|
+
{ ...planData(latest.data), lastStartedAt: at },
|
|
1557
|
+
"dev:plan-start",
|
|
1558
|
+
);
|
|
1559
|
+
});
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
/**
|
|
1563
|
+
* Drops the mark of the slot kept for the plan that went longest without a
|
|
1564
|
+
* start, from a task that is no longer running: the slot was free while it
|
|
1565
|
+
* waited, so a task that resumes to running does not hold it again.
|
|
1566
|
+
*/
|
|
1567
|
+
export async function clearOldestPlanSlot(
|
|
1568
|
+
store: ObjectStore,
|
|
1569
|
+
id: string,
|
|
1570
|
+
): Promise<void> {
|
|
1571
|
+
const initial = await getTask(store, id);
|
|
1572
|
+
const data = taskData(initial.data);
|
|
1573
|
+
if (data.oldestPlanSlot !== true) return;
|
|
1574
|
+
await withRepoLock(store, data.repository, async () => {
|
|
1575
|
+
const current = await getTask(store, id);
|
|
1576
|
+
const latest = taskData(current.data);
|
|
1577
|
+
if (latest.oldestPlanSlot !== true) return;
|
|
1578
|
+
if (latest.phase === "running" || latest.phase === "review") return;
|
|
1579
|
+
const { oldestPlanSlot: _slot, ...rest } = latest;
|
|
1580
|
+
await writeTask(store, id, rest, "dev:task-slot-release");
|
|
1581
|
+
});
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
/** Clears a task's dead run so a repair run can start it again. */
|
|
1585
|
+
export async function reopenTask(
|
|
1586
|
+
store: ObjectStore,
|
|
1587
|
+
id: string,
|
|
1588
|
+
): Promise<TaskData | undefined> {
|
|
1589
|
+
const initial = await getTask(store, id);
|
|
1590
|
+
const data = taskData(initial.data);
|
|
1591
|
+
return withRepoLock(store, data.repository, async () => {
|
|
1592
|
+
const current = await getTask(store, id);
|
|
1593
|
+
const latest = taskData(current.data);
|
|
1594
|
+
if (
|
|
1595
|
+
latest.phase !== "waiting" &&
|
|
1596
|
+
latest.phase !== "running" &&
|
|
1597
|
+
latest.phase !== "review"
|
|
1598
|
+
) {
|
|
1599
|
+
return undefined;
|
|
1600
|
+
}
|
|
1601
|
+
const next: TaskData = {
|
|
1602
|
+
...latest,
|
|
1603
|
+
phase: "ready",
|
|
1604
|
+
run: null,
|
|
1605
|
+
claim: null,
|
|
1606
|
+
brief: appendChange(latest.brief, "ready"),
|
|
1607
|
+
};
|
|
1608
|
+
await writeTask(store, id, next, "dev:task-reopen");
|
|
1609
|
+
return next;
|
|
1610
|
+
});
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
/**
|
|
1614
|
+
* Gives back a claim whose start never became a run. A task that is `running`
|
|
1615
|
+
* with no run id has nothing to recover or cancel, so it returns to `ready`
|
|
1616
|
+
* with no claim and the next tick can start it again. A manual start request is
|
|
1617
|
+
* kept: the owner's start survives the crash that lost the run.
|
|
1618
|
+
*/
|
|
1619
|
+
export async function releaseTaskClaim(
|
|
1620
|
+
store: ObjectStore,
|
|
1621
|
+
id: string,
|
|
1622
|
+
): Promise<TaskData | undefined> {
|
|
1623
|
+
const initial = await getTask(store, id);
|
|
1624
|
+
const data = taskData(initial.data);
|
|
1625
|
+
return withRepoLock(store, data.repository, async () => {
|
|
1626
|
+
const current = await getTask(store, id);
|
|
1627
|
+
const latest = taskData(current.data);
|
|
1628
|
+
if (
|
|
1629
|
+
(latest.phase !== "running" && latest.phase !== "review") ||
|
|
1630
|
+
latest.run !== null
|
|
1631
|
+
) {
|
|
1632
|
+
return undefined;
|
|
1633
|
+
}
|
|
1634
|
+
const next: TaskData = {
|
|
1635
|
+
...latest,
|
|
1636
|
+
phase: "ready",
|
|
1637
|
+
claim: null,
|
|
1638
|
+
brief: appendChange(latest.brief, "ready"),
|
|
1639
|
+
};
|
|
1640
|
+
await writeTask(store, id, next, "dev:task-claim-released");
|
|
1641
|
+
return next;
|
|
1642
|
+
});
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
/**
|
|
1646
|
+
* Claims the right to print this attempt's integration result. The mark is
|
|
1647
|
+
* checked and written under the repository lock, so of two passes (in this
|
|
1648
|
+
* process or another one on the same store) only one gets the claim and
|
|
1649
|
+
* prints; the other gets `false`.
|
|
1650
|
+
*/
|
|
1651
|
+
export async function recordMergeReported(
|
|
1652
|
+
store: ObjectStore,
|
|
1653
|
+
id: string,
|
|
1654
|
+
attempt: number,
|
|
1655
|
+
): Promise<boolean> {
|
|
1656
|
+
const initial = await getTask(store, id);
|
|
1657
|
+
const data = taskData(initial.data);
|
|
1658
|
+
return withRepoLock(store, data.repository, async () => {
|
|
1659
|
+
const current = await getTask(store, id);
|
|
1660
|
+
const latest = taskData(current.data);
|
|
1661
|
+
if (latest.mergeReportedAttempt === attempt) return false;
|
|
1662
|
+
await writeTask(
|
|
1663
|
+
store,
|
|
1664
|
+
id,
|
|
1665
|
+
{ ...latest, mergeReportedAttempt: attempt },
|
|
1666
|
+
"dev:task-merge-reported",
|
|
1667
|
+
);
|
|
1668
|
+
return true;
|
|
1669
|
+
});
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
/**
|
|
1673
|
+
* Claims the right to say a plan finished, once. The mark is checked and
|
|
1674
|
+
* written under the repository lock; a second caller gets `false`.
|
|
1675
|
+
*/
|
|
1676
|
+
export async function recordPlanFinishedReported(
|
|
1677
|
+
store: ObjectStore,
|
|
1678
|
+
id: string,
|
|
1679
|
+
): Promise<boolean> {
|
|
1680
|
+
const initial = planData((await getPlan(store, id)).data);
|
|
1681
|
+
return withRepoLock(store, initial.repository, async () => {
|
|
1682
|
+
const current = await getPlan(store, id);
|
|
1683
|
+
const latest = planData(current.data);
|
|
1684
|
+
if (latest.finishedReported === true) return false;
|
|
1685
|
+
await store.put(
|
|
1686
|
+
"plan",
|
|
1687
|
+
id,
|
|
1688
|
+
{ ...latest, finishedReported: true },
|
|
1689
|
+
"dev:plan-finished",
|
|
1690
|
+
);
|
|
1691
|
+
return true;
|
|
1692
|
+
});
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
/** Asks the scheduler to start this ready task on its next tick. */
|
|
1696
|
+
export async function requestTaskStart(
|
|
1697
|
+
store: ObjectStore,
|
|
1698
|
+
id: string,
|
|
1699
|
+
): Promise<StoredObject> {
|
|
1700
|
+
const initial = await getTask(store, id);
|
|
1701
|
+
const data = taskData(initial.data);
|
|
1702
|
+
const at = `task "${bareId(id)}"`;
|
|
1703
|
+
if (data.phase !== "ready") {
|
|
1704
|
+
throw new DevError(
|
|
1705
|
+
"phase",
|
|
1706
|
+
`${at} is ${data.phase}; only a ready task can be started`,
|
|
1707
|
+
);
|
|
1708
|
+
}
|
|
1709
|
+
return withRepoLock(store, data.repository, async () => {
|
|
1710
|
+
const current = await getTask(store, id);
|
|
1711
|
+
const latest = taskData(current.data);
|
|
1712
|
+
if (latest.phase !== "ready") {
|
|
1713
|
+
throw new DevError(
|
|
1714
|
+
"phase",
|
|
1715
|
+
`${at} is ${latest.phase}; only a ready task can be started`,
|
|
1716
|
+
);
|
|
1717
|
+
}
|
|
1718
|
+
if (latest.startRequested === true) return current;
|
|
1719
|
+
return writeTask(
|
|
1720
|
+
store,
|
|
1721
|
+
id,
|
|
1722
|
+
{ ...latest, startRequested: true },
|
|
1723
|
+
"dev:task-start-request",
|
|
1724
|
+
);
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
/** Records that the scheduler answered this attempt's review with `merge`. */
|
|
1729
|
+
export async function recordAutoMerge(
|
|
1730
|
+
store: ObjectStore,
|
|
1731
|
+
id: string,
|
|
1732
|
+
attempt: number,
|
|
1733
|
+
): Promise<StoredObject> {
|
|
1734
|
+
const initial = await getTask(store, id);
|
|
1735
|
+
const data = taskData(initial.data);
|
|
1736
|
+
return withRepoLock(store, data.repository, async () => {
|
|
1737
|
+
const current = await getTask(store, id);
|
|
1738
|
+
const latest = taskData(current.data);
|
|
1739
|
+
return writeTask(
|
|
1740
|
+
store,
|
|
1741
|
+
id,
|
|
1742
|
+
{ ...latest, autoMergeAttempt: attempt },
|
|
1743
|
+
"dev:task-auto-merge",
|
|
1744
|
+
);
|
|
1745
|
+
});
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
/**
|
|
1749
|
+
* Claims the next transition of a waiting task for one person action. The
|
|
1750
|
+
* action passes the version of the task it looked at; under the repository lock
|
|
1751
|
+
* the claim re-reads the task and writes `change` only when the task is still
|
|
1752
|
+
* waiting at that version. A concurrent merge, reject, rework, reverify or
|
|
1753
|
+
* cancel wrote first, and this call reports what happened and changes nothing.
|
|
1754
|
+
* It returns what was written and the data to put back if the answer fails.
|
|
1755
|
+
*/
|
|
1756
|
+
export async function claimWaitingTask(
|
|
1757
|
+
store: ObjectStore,
|
|
1758
|
+
id: string,
|
|
1759
|
+
seen: number,
|
|
1760
|
+
action: TaskActionName,
|
|
1761
|
+
change: (latest: TaskData) => TaskData,
|
|
1762
|
+
): Promise<
|
|
1763
|
+
| { readonly claimed: StoredObject; readonly before: TaskData; readonly settled?: undefined }
|
|
1764
|
+
| { readonly settled: string }
|
|
1765
|
+
> {
|
|
1766
|
+
const initial = await getTask(store, id);
|
|
1767
|
+
const data = taskData(initial.data);
|
|
1768
|
+
return withRepoLock(store, data.repository, async () => {
|
|
1769
|
+
const current = await getTask(store, id);
|
|
1770
|
+
const latest = taskData(current.data);
|
|
1771
|
+
// Where the task is now: the table's line for this action. A result that
|
|
1772
|
+
// is already true (another merge won) succeeds with its line.
|
|
1773
|
+
const said = taskLine(action, latest, { id: bareId(id) });
|
|
1774
|
+
if (said?.kind === "done") return { settled: said.text };
|
|
1775
|
+
refuseTask(action, latest, id);
|
|
1776
|
+
if (current.version !== seen) {
|
|
1777
|
+
throw new DevRefusal(
|
|
1778
|
+
`${bareId(id)} changed since you looked; look again, then dev:${action}`,
|
|
1779
|
+
);
|
|
1780
|
+
}
|
|
1781
|
+
const claimed = await writeTask(
|
|
1782
|
+
store,
|
|
1783
|
+
id,
|
|
1784
|
+
change(latest),
|
|
1785
|
+
`dev:task-${action}`,
|
|
1786
|
+
);
|
|
1787
|
+
return { claimed, before: latest };
|
|
1788
|
+
});
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1791
|
+
/** Puts back a task a failed claim changed, unless something wrote it since. */
|
|
1792
|
+
export async function releaseWaitingClaim(
|
|
1793
|
+
store: ObjectStore,
|
|
1794
|
+
id: string,
|
|
1795
|
+
claimed: StoredObject,
|
|
1796
|
+
before: TaskData,
|
|
1797
|
+
): Promise<void> {
|
|
1798
|
+
const data = taskData(claimed.data);
|
|
1799
|
+
await withRepoLock(store, data.repository, async () => {
|
|
1800
|
+
const current = await getTask(store, id);
|
|
1801
|
+
if (current.version !== claimed.version) return;
|
|
1802
|
+
await writeTask(store, id, before, "dev:task-claim-released");
|
|
1803
|
+
});
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
/** Appends a rework section and records it. */
|
|
1807
|
+
export async function addRework(
|
|
1808
|
+
store: ObjectStore,
|
|
1809
|
+
id: string,
|
|
1810
|
+
source: string,
|
|
1811
|
+
text: string,
|
|
1812
|
+
): Promise<StoredObject> {
|
|
1813
|
+
const initial = await getTask(store, id);
|
|
1814
|
+
const data = taskData(initial.data);
|
|
1815
|
+
return withRepoLock(store, data.repository, async () => {
|
|
1816
|
+
const current = await getTask(store, id);
|
|
1817
|
+
const latest = taskData(current.data);
|
|
1818
|
+
return writeTask(
|
|
1819
|
+
store,
|
|
1820
|
+
id,
|
|
1821
|
+
{ ...latest, brief: appendRework(latest.brief, source, text) },
|
|
1822
|
+
"dev:task-rework",
|
|
1823
|
+
);
|
|
1824
|
+
});
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
export async function confirmPlan(
|
|
1828
|
+
store: ObjectStore,
|
|
1829
|
+
id: string,
|
|
1830
|
+
): Promise<StoredObject> {
|
|
1831
|
+
const initial = await getPlan(store, id);
|
|
1832
|
+
return withRepoLock(store, planData(initial.data).repository, async () => {
|
|
1833
|
+
const plan = await getPlan(store, id);
|
|
1834
|
+
const data = planData(plan.data);
|
|
1835
|
+
if (refusePlan("confirm", data, id)) return plan;
|
|
1836
|
+
const tasks = await tasksOfPlan(store, id);
|
|
1837
|
+
if (tasks.length === 0) {
|
|
1838
|
+
refusePlan("confirm", { ...data, taskCount: 0 }, id);
|
|
1839
|
+
}
|
|
1840
|
+
for (const object of tasks) {
|
|
1841
|
+
const task = taskData(object.data);
|
|
1842
|
+
if (task.phase !== "proposed") continue;
|
|
1843
|
+
await writeTask(
|
|
1844
|
+
store,
|
|
1845
|
+
object.id,
|
|
1846
|
+
{
|
|
1847
|
+
...task,
|
|
1848
|
+
phase: "ready",
|
|
1849
|
+
brief: appendChange(task.brief, "ready"),
|
|
1850
|
+
},
|
|
1851
|
+
"dev:task-ready",
|
|
1852
|
+
);
|
|
1853
|
+
}
|
|
1854
|
+
const next: PlanData = { ...data, status: "confirmed" };
|
|
1855
|
+
const written = await writePlan(store, id, next, "dev:plan-confirm");
|
|
1856
|
+
return refreshPlanCounts(store, written);
|
|
1857
|
+
});
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
export async function confirmTask(
|
|
1861
|
+
store: ObjectStore,
|
|
1862
|
+
id: string,
|
|
1863
|
+
): Promise<StoredObject> {
|
|
1864
|
+
const initial = await getTask(store, id);
|
|
1865
|
+
const initialData = taskData(initial.data);
|
|
1866
|
+
return withRepoLock(store, initialData.repository, async () => {
|
|
1867
|
+
const task = await getTask(store, id);
|
|
1868
|
+
const data = taskData(task.data);
|
|
1869
|
+
const plan = await getPlan(
|
|
1870
|
+
store,
|
|
1871
|
+
storeKey(data.repository, data.plan),
|
|
1872
|
+
);
|
|
1873
|
+
const settled = refuseTask("confirm", data, id, {
|
|
1874
|
+
id: data.plan,
|
|
1875
|
+
state: planState(planData(plan.data)),
|
|
1876
|
+
});
|
|
1877
|
+
if (settled) return task;
|
|
1878
|
+
const written = await writeTask(
|
|
1879
|
+
store,
|
|
1880
|
+
id,
|
|
1881
|
+
{ ...data, phase: "ready", brief: appendChange(data.brief, "ready") },
|
|
1882
|
+
"dev:task-confirm",
|
|
1883
|
+
);
|
|
1884
|
+
await refreshPlanCounts(store, plan);
|
|
1885
|
+
return written;
|
|
1886
|
+
});
|
|
1887
|
+
}
|
|
1888
|
+
|
|
1889
|
+
/** A run to cancel, with the graph its task stored when it started one. */
|
|
1890
|
+
interface RunToCancel {
|
|
1891
|
+
readonly run: string;
|
|
1892
|
+
readonly flow: FlowDefinition | undefined;
|
|
1893
|
+
}
|
|
1894
|
+
|
|
1895
|
+
async function cancelRuns(
|
|
1896
|
+
control: DevRunControl | undefined,
|
|
1897
|
+
runs: readonly RunToCancel[],
|
|
1898
|
+
): Promise<void> {
|
|
1899
|
+
for (const { run, flow } of runs) {
|
|
1900
|
+
if (flow === undefined) await control?.cancel(run);
|
|
1901
|
+
else await control?.cancel(run, flow);
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
async function cancelTasks(
|
|
1906
|
+
store: ObjectStore,
|
|
1907
|
+
tasks: readonly StoredObject[],
|
|
1908
|
+
): Promise<readonly RunToCancel[]> {
|
|
1909
|
+
const runs: RunToCancel[] = [];
|
|
1910
|
+
for (const object of tasks) {
|
|
1911
|
+
const data = taskData(object.data);
|
|
1912
|
+
if (!isOpenTask(data)) continue;
|
|
1913
|
+
if (data.run !== null) runs.push({ run: data.run, flow: data.flowGraph });
|
|
1914
|
+
await writeTask(
|
|
1915
|
+
store,
|
|
1916
|
+
object.id,
|
|
1917
|
+
{
|
|
1918
|
+
...data,
|
|
1919
|
+
phase: "cancelled",
|
|
1920
|
+
run: null,
|
|
1921
|
+
claim: null,
|
|
1922
|
+
brief: appendChange(data.brief, "cancelled"),
|
|
1923
|
+
},
|
|
1924
|
+
"dev:task-cancel",
|
|
1925
|
+
);
|
|
1926
|
+
}
|
|
1927
|
+
return runs;
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
export interface CancelOptions {
|
|
1931
|
+
readonly runs?: DevRunControl | undefined;
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
export async function cancelPlan(
|
|
1935
|
+
store: ObjectStore,
|
|
1936
|
+
id: string,
|
|
1937
|
+
options: CancelOptions = {},
|
|
1938
|
+
): Promise<{ readonly plan: StoredObject; readonly runs: readonly RunToCancel[] }> {
|
|
1939
|
+
const initial = await getPlan(store, id);
|
|
1940
|
+
const initialData = planData(initial.data);
|
|
1941
|
+
const cancelled = await withRepoLock(
|
|
1942
|
+
store,
|
|
1943
|
+
initialData.repository,
|
|
1944
|
+
async () => {
|
|
1945
|
+
const plan = await getPlan(store, id);
|
|
1946
|
+
const data = planData(plan.data);
|
|
1947
|
+
if (refusePlan("cancel", data, id)) return { plan, runs: [] };
|
|
1948
|
+
const tasks = await tasksOfPlan(store, id);
|
|
1949
|
+
const runs = await cancelTasks(store, tasks);
|
|
1950
|
+
const next: PlanData = { ...data, status: "cancelled" };
|
|
1951
|
+
const written = await writePlan(store, id, next, "dev:plan-cancel");
|
|
1952
|
+
await refreshPlanCounts(store, written);
|
|
1953
|
+
return { plan: written, runs };
|
|
1954
|
+
},
|
|
1955
|
+
);
|
|
1956
|
+
await cancelRuns(options.runs, cancelled.runs);
|
|
1957
|
+
return cancelled;
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
export async function cancelTask(
|
|
1961
|
+
store: ObjectStore,
|
|
1962
|
+
id: string,
|
|
1963
|
+
options: CancelOptions = {},
|
|
1964
|
+
): Promise<{ readonly task: StoredObject; readonly runs: readonly RunToCancel[] }> {
|
|
1965
|
+
const initial = await getTask(store, id);
|
|
1966
|
+
const initialData = taskData(initial.data);
|
|
1967
|
+
const cancelled = await withRepoLock(
|
|
1968
|
+
store,
|
|
1969
|
+
initialData.repository,
|
|
1970
|
+
async () => {
|
|
1971
|
+
const task = await getTask(store, id);
|
|
1972
|
+
const data = taskData(task.data);
|
|
1973
|
+
if (refuseTask("cancel", data, id)) return { task, runs: [] };
|
|
1974
|
+
const runs: readonly RunToCancel[] =
|
|
1975
|
+
data.run === null ? [] : [{ run: data.run, flow: data.flowGraph }];
|
|
1976
|
+
const written = await writeTask(
|
|
1977
|
+
store,
|
|
1978
|
+
id,
|
|
1979
|
+
{
|
|
1980
|
+
...data,
|
|
1981
|
+
phase: "cancelled",
|
|
1982
|
+
run: null,
|
|
1983
|
+
claim: null,
|
|
1984
|
+
brief: appendChange(data.brief, "cancelled"),
|
|
1985
|
+
},
|
|
1986
|
+
"dev:task-cancel",
|
|
1987
|
+
);
|
|
1988
|
+
await refreshPlanCountsOf(store, storeKey(data.repository, data.plan));
|
|
1989
|
+
return { task: written, runs };
|
|
1990
|
+
},
|
|
1991
|
+
);
|
|
1992
|
+
await cancelRuns(options.runs, cancelled.runs);
|
|
1993
|
+
return cancelled;
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
/**
|
|
1997
|
+
* Stops a task for a while: its run is cancelled as `cancel` cancels it, but
|
|
1998
|
+
* the task keeps its branch, worktree, commits, attempt count and place. It
|
|
1999
|
+
* holds no concurrency slot and the scheduler does not start it.
|
|
2000
|
+
*/
|
|
2001
|
+
export async function pauseTask(
|
|
2002
|
+
store: ObjectStore,
|
|
2003
|
+
id: string,
|
|
2004
|
+
options: CancelOptions = {},
|
|
2005
|
+
): Promise<{ readonly task: StoredObject; readonly runs: readonly RunToCancel[] }> {
|
|
2006
|
+
const initial = await getTask(store, id);
|
|
2007
|
+
const initialData = taskData(initial.data);
|
|
2008
|
+
const paused = await withRepoLock(store, initialData.repository, async () => {
|
|
2009
|
+
const task = await getTask(store, id);
|
|
2010
|
+
const data = taskData(task.data);
|
|
2011
|
+
if (refuseTask("pause", data, id)) return { task, runs: [] };
|
|
2012
|
+
const runs: readonly RunToCancel[] =
|
|
2013
|
+
data.run === null ? [] : [{ run: data.run, flow: data.flowGraph }];
|
|
2014
|
+
const {
|
|
2015
|
+
areaHeldBy: _area,
|
|
2016
|
+
dependencyHeld: _waits,
|
|
2017
|
+
dependencyHeldFor: _waitsFor,
|
|
2018
|
+
slotHeld: _slot,
|
|
2019
|
+
...rest
|
|
2020
|
+
} = data;
|
|
2021
|
+
const written = await writeTask(
|
|
2022
|
+
store,
|
|
2023
|
+
id,
|
|
2024
|
+
{
|
|
2025
|
+
...rest,
|
|
2026
|
+
phase: "paused",
|
|
2027
|
+
run: null,
|
|
2028
|
+
claim: null,
|
|
2029
|
+
startRequested: false,
|
|
2030
|
+
brief: appendChange(data.brief, "paused"),
|
|
2031
|
+
},
|
|
2032
|
+
"dev:task-pause",
|
|
2033
|
+
);
|
|
2034
|
+
await refreshPlanCountsOf(store, storeKey(data.repository, data.plan));
|
|
2035
|
+
return { task: written, runs };
|
|
2036
|
+
});
|
|
2037
|
+
await cancelRuns(options.runs, paused.runs);
|
|
2038
|
+
return paused;
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
/** Returns a paused task to ready; its next start continues from its branch. */
|
|
2042
|
+
export async function resumeTask(
|
|
2043
|
+
store: ObjectStore,
|
|
2044
|
+
id: string,
|
|
2045
|
+
): Promise<StoredObject> {
|
|
2046
|
+
const initial = await getTask(store, id);
|
|
2047
|
+
const initialData = taskData(initial.data);
|
|
2048
|
+
return withRepoLock(store, initialData.repository, async () => {
|
|
2049
|
+
const task = await getTask(store, id);
|
|
2050
|
+
const data = taskData(task.data);
|
|
2051
|
+
if (refuseTask("resume", data, id)) return task;
|
|
2052
|
+
const written = await writeTask(
|
|
2053
|
+
store,
|
|
2054
|
+
id,
|
|
2055
|
+
{ ...data, phase: "ready", brief: appendChange(data.brief, "ready") },
|
|
2056
|
+
"dev:task-resume",
|
|
2057
|
+
);
|
|
2058
|
+
await refreshPlanCountsOf(store, storeKey(data.repository, data.plan));
|
|
2059
|
+
return written;
|
|
2060
|
+
});
|
|
2061
|
+
}
|
|
2062
|
+
|
|
2063
|
+
export async function setAuto(
|
|
2064
|
+
store: ObjectStore,
|
|
2065
|
+
id: string,
|
|
2066
|
+
auto: AutoMode,
|
|
2067
|
+
): Promise<StoredObject> {
|
|
2068
|
+
const initial = await getPlan(store, id);
|
|
2069
|
+
const data = planData(initial.data);
|
|
2070
|
+
refusePlan("auto", data, id);
|
|
2071
|
+
return withRepoLock(store, data.repository, async () => {
|
|
2072
|
+
const plan = await getPlan(store, id);
|
|
2073
|
+
const fresh = planData(plan.data);
|
|
2074
|
+
refusePlan("auto", fresh, id);
|
|
2075
|
+
return writePlan(
|
|
2076
|
+
store,
|
|
2077
|
+
id,
|
|
2078
|
+
{ ...fresh, auto },
|
|
2079
|
+
`dev:plan-auto-${auto}`,
|
|
2080
|
+
);
|
|
2081
|
+
});
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
export async function setPriority(
|
|
2085
|
+
store: ObjectStore,
|
|
2086
|
+
id: string,
|
|
2087
|
+
priority: Priority,
|
|
2088
|
+
): Promise<StoredObject> {
|
|
2089
|
+
if (!PRIORITIES.includes(priority)) {
|
|
2090
|
+
throw new DevError("priority", `unknown priority "${priority}"`);
|
|
2091
|
+
}
|
|
2092
|
+
const initial = await getPlan(store, id);
|
|
2093
|
+
const data = planData(initial.data);
|
|
2094
|
+
refusePlan("priority", data, id);
|
|
2095
|
+
return withRepoLock(store, data.repository, async () => {
|
|
2096
|
+
const plan = await getPlan(store, id);
|
|
2097
|
+
const fresh = planData(plan.data);
|
|
2098
|
+
refusePlan("priority", fresh, id);
|
|
2099
|
+
return writePlan(
|
|
2100
|
+
store,
|
|
2101
|
+
id,
|
|
2102
|
+
{ ...fresh, priority },
|
|
2103
|
+
`dev:plan-priority-${priority}`,
|
|
2104
|
+
);
|
|
2105
|
+
});
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
export async function setConcurrency(
|
|
2109
|
+
store: ObjectStore,
|
|
2110
|
+
id: string,
|
|
2111
|
+
count: number,
|
|
2112
|
+
): Promise<StoredObject> {
|
|
2113
|
+
const concurrency = validatePlanConcurrency(count);
|
|
2114
|
+
const initial = await getPlan(store, id);
|
|
2115
|
+
const data = planData(initial.data);
|
|
2116
|
+
refusePlan("concurrency", data, id);
|
|
2117
|
+
return withRepoLock(store, data.repository, async () => {
|
|
2118
|
+
const plan = await getPlan(store, id);
|
|
2119
|
+
const fresh = planData(plan.data);
|
|
2120
|
+
refusePlan("concurrency", fresh, id);
|
|
2121
|
+
return writePlan(
|
|
2122
|
+
store,
|
|
2123
|
+
id,
|
|
2124
|
+
{ ...fresh, concurrency },
|
|
2125
|
+
`dev:plan-concurrency-${concurrency}`,
|
|
2126
|
+
);
|
|
2127
|
+
});
|
|
2128
|
+
}
|
|
2129
|
+
|
|
2130
|
+
/* ---------------------------------------------------------------------------
|
|
2131
|
+
* History
|
|
2132
|
+
* ------------------------------------------------------------------------- */
|
|
2133
|
+
|
|
2134
|
+
/** Rebuilds the objects a store's facts describe, in append order. */
|
|
2135
|
+
export function rebuildObjects(
|
|
2136
|
+
facts: readonly PluginFact[],
|
|
2137
|
+
): Map<string, Record<string, unknown>> {
|
|
2138
|
+
const objects = new Map<string, Record<string, unknown>>();
|
|
2139
|
+
for (const fact of facts) {
|
|
2140
|
+
const object = fact.object;
|
|
2141
|
+
if (object === undefined) continue;
|
|
2142
|
+
const key = `${object.type}:${object.id}`;
|
|
2143
|
+
const value = objectValueAfter(fact, objects.get(key) ?? {});
|
|
2144
|
+
if (value !== undefined) objects.set(key, { ...value });
|
|
2145
|
+
}
|
|
2146
|
+
return objects;
|
|
2147
|
+
}
|
|
2148
|
+
|
|
2149
|
+
/** The live object data, keyed the way `rebuildObjects` keys it. */
|
|
2150
|
+
export async function liveObjects(
|
|
2151
|
+
store: ObjectStore,
|
|
2152
|
+
): Promise<Map<string, Record<string, unknown>>> {
|
|
2153
|
+
const objects = new Map<string, Record<string, unknown>>();
|
|
2154
|
+
for (const type of [REPOSITORY_TYPE, PLAN_TYPE, TASK_TYPE]) {
|
|
2155
|
+
for (const object of await listAll(store, type)) {
|
|
2156
|
+
objects.set(`${type}:${object.id}`, { ...object.data });
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
return objects;
|
|
2160
|
+
}
|