@operad-io/dev 0.2.0 → 0.3.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.
- package/dev.schema.json +12 -3
- package/package.json +7 -7
- package/skills/git/SKILL.md +70 -0
- package/skills/setup/SKILL.md +26 -9
- package/src/actions.ts +14 -4
- package/src/announce.ts +7 -2
- package/src/chat.ts +83 -9
- package/src/check.ts +28 -10
- package/src/clone-chat.ts +32 -0
- package/src/flow-compiler.ts +1 -1
- package/src/gate.ts +70 -23
- package/src/git-state.ts +155 -0
- package/src/git.ts +45 -4
- package/src/index.ts +6 -0
- package/src/integrate.ts +86 -26
- package/src/mainclone.ts +58 -2
- package/src/model.ts +13 -0
- package/src/project-settings.ts +30 -4
- package/src/register-requests.ts +38 -4
- package/src/schedule.ts +25 -7
- package/src/service.ts +128 -36
- package/src/settings.ts +107 -8
- package/src/setup-git.ts +366 -0
- package/src/steps.ts +486 -66
- package/src/views.ts +183 -10
package/src/service.ts
CHANGED
|
@@ -12,10 +12,12 @@ import {
|
|
|
12
12
|
import { homedir } from "node:os";
|
|
13
13
|
import { basename, isAbsolute, join, relative, resolve } from "node:path";
|
|
14
14
|
import { ViewError } from "@operad-io/engine";
|
|
15
|
-
import { isBusy } from "@operad-io/engine/platform";
|
|
15
|
+
import { isBusy, type GitFinderHost } from "@operad-io/engine/platform";
|
|
16
16
|
import type { FlowDefinition, ObjectStore, StoredObject } from "@operad-io/engine";
|
|
17
17
|
import { git } from "./git.ts";
|
|
18
|
+
import { assertCanCreateTask, GitStateError, gitStateOf, type GitState } from "./git-state.ts";
|
|
18
19
|
import { withdrawIntegration } from "./integration-queue.ts";
|
|
20
|
+
import { switchBackToStart } from "./mainclone.ts";
|
|
19
21
|
import { withRepositoryLock } from "./lock.ts";
|
|
20
22
|
import {
|
|
21
23
|
appendChange,
|
|
@@ -78,6 +80,8 @@ export interface AddRepositoryOptions {
|
|
|
78
80
|
readonly path: string;
|
|
79
81
|
readonly name?: string | undefined;
|
|
80
82
|
readonly cwd?: string | undefined;
|
|
83
|
+
/** The machine the git program is looked up on; a test injects one without git. */
|
|
84
|
+
readonly finder?: GitFinderHost | undefined;
|
|
81
85
|
}
|
|
82
86
|
|
|
83
87
|
const REPOSITORY_TYPE = "repo";
|
|
@@ -449,7 +453,7 @@ export async function resolveDevKey(
|
|
|
449
453
|
}
|
|
450
454
|
}
|
|
451
455
|
|
|
452
|
-
function ensureExcludes(repositoryPath: string): void {
|
|
456
|
+
export function ensureExcludes(repositoryPath: string): void {
|
|
453
457
|
const path = git(repositoryPath, ["rev-parse", "--git-path", "info/exclude"]);
|
|
454
458
|
if (!path.ok) {
|
|
455
459
|
throw new DevError("path", `is not a git work tree: ${path.err}`);
|
|
@@ -467,7 +471,12 @@ function ensureExcludes(repositoryPath: string): void {
|
|
|
467
471
|
appendFileSync(excludePath, `${prefix}${missing.join("\n")}\n`, "utf8");
|
|
468
472
|
}
|
|
469
473
|
|
|
470
|
-
function writeSettings(
|
|
474
|
+
export function writeSettings(
|
|
475
|
+
repositoryPath: string,
|
|
476
|
+
name: string,
|
|
477
|
+
hasRemote: boolean,
|
|
478
|
+
inPlace: boolean,
|
|
479
|
+
): void {
|
|
471
480
|
const directory = join(repositoryPath, ".operad");
|
|
472
481
|
mkdirSync(directory, { recursive: true });
|
|
473
482
|
const settings = join(directory, "dev.json");
|
|
@@ -478,7 +487,8 @@ function writeSettings(repositoryPath: string, name: string): void {
|
|
|
478
487
|
{
|
|
479
488
|
$schema: DEV_SETTINGS_SCHEMA,
|
|
480
489
|
name,
|
|
481
|
-
remote: DEFAULT_DEV_SETTINGS.remote,
|
|
490
|
+
...(hasRemote ? { remote: DEFAULT_DEV_SETTINGS.remote } : {}),
|
|
491
|
+
...(inPlace ? { workIn: "branch" } : {}),
|
|
482
492
|
mainBranch: DEFAULT_DEV_SETTINGS.mainBranch,
|
|
483
493
|
worktreeRoot: DEFAULT_DEV_SETTINGS.worktreeRoot,
|
|
484
494
|
concurrency: DEFAULT_DEV_SETTINGS.concurrency,
|
|
@@ -513,7 +523,10 @@ function repositoryHasRemote(
|
|
|
513
523
|
};
|
|
514
524
|
}
|
|
515
525
|
|
|
516
|
-
/**
|
|
526
|
+
/**
|
|
527
|
+
* Registers a folder. It may have no remote, no git repository, or no git
|
|
528
|
+
* program on the machine; a subfolder of a repository is refused, naming the root.
|
|
529
|
+
*/
|
|
517
530
|
export async function addRepository(
|
|
518
531
|
store: ObjectStore,
|
|
519
532
|
options: AddRepositoryOptions,
|
|
@@ -522,31 +535,32 @@ export async function addRepository(
|
|
|
522
535
|
if (!existsSync(from) || !statSync(from).isDirectory()) {
|
|
523
536
|
throw new DevError("path", `no directory "${from}"`);
|
|
524
537
|
}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
538
|
+
// A folder needs git only to create a task (ADR 0039 D7): without the git
|
|
539
|
+
// program, or in no repository, it is registered as it is.
|
|
540
|
+
let state: GitState;
|
|
541
|
+
try {
|
|
542
|
+
state = gitStateOf(from, options.finder === undefined ? {} : { finder: options.finder });
|
|
543
|
+
} catch (error) {
|
|
544
|
+
if (error instanceof GitStateError) {
|
|
545
|
+
throw new DevError("path", `"${from}" is not a git work tree: ${error.reason}`);
|
|
546
|
+
}
|
|
547
|
+
throw error;
|
|
531
548
|
}
|
|
532
|
-
|
|
533
|
-
if (path !== realpathSync(from)) {
|
|
549
|
+
if (state.state === "subfolder") {
|
|
534
550
|
throw new DevError(
|
|
535
551
|
"path",
|
|
536
|
-
`"${from}" is
|
|
552
|
+
`"${from}" is inside the git repository at "${state.root}"; add "${state.root}" instead`,
|
|
537
553
|
);
|
|
538
554
|
}
|
|
539
|
-
const
|
|
540
|
-
|
|
555
|
+
const path = realpathSync(from);
|
|
556
|
+
const hasRepository = state.state === "no commit" || state.state === "ready";
|
|
557
|
+
const bare =
|
|
558
|
+
state.state === "no program" ? undefined : git(path, ["rev-parse", "--is-bare-repository"]);
|
|
559
|
+
if (bare !== undefined && bare.ok && bare.out === "true") {
|
|
541
560
|
throw new DevError("path", `"${path}" is a bare repository`);
|
|
542
561
|
}
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
throw new DevError(
|
|
546
|
-
"path",
|
|
547
|
-
`"${path}" has no remote${remote.err.length > 0 ? `: ${remote.err}` : ""}`,
|
|
548
|
-
);
|
|
549
|
-
}
|
|
562
|
+
// A repository with no remote is allowed: its merges stay local.
|
|
563
|
+
const hasRemote = hasRepository && repositoryHasRemote(path).present;
|
|
550
564
|
|
|
551
565
|
const name = validateRepositoryName(options.name ?? basename(path));
|
|
552
566
|
const existing = await findRepository(store, name, { includeRemoved: true });
|
|
@@ -582,8 +596,8 @@ export async function addRepository(
|
|
|
582
596
|
}
|
|
583
597
|
: { ...previous, name, path, removed: false };
|
|
584
598
|
await store.put(REPOSITORY_TYPE, name, data, "dev:repo-add");
|
|
585
|
-
writeSettings(path, name);
|
|
586
|
-
ensureExcludes(path);
|
|
599
|
+
writeSettings(path, name, hasRemote, !hasRepository);
|
|
600
|
+
if (hasRepository) ensureExcludes(path);
|
|
587
601
|
return data;
|
|
588
602
|
}
|
|
589
603
|
|
|
@@ -922,6 +936,7 @@ export async function addPlan(
|
|
|
922
936
|
? 1
|
|
923
937
|
: validatePlanConcurrency(options.concurrency);
|
|
924
938
|
return withRepoLock(store, options.repository.name, async (repository) => {
|
|
939
|
+
assertCanCreateTask(repository.path);
|
|
925
940
|
const id = await nextId(store, "plan", repository.name, slug);
|
|
926
941
|
const data: PlanData = {
|
|
927
942
|
repository: repository.name,
|
|
@@ -1091,6 +1106,7 @@ export async function addTaskLocked(
|
|
|
1091
1106
|
`plan "${idOfKey(currentPlan.id)}" is not in repository "${repository.name}"`,
|
|
1092
1107
|
);
|
|
1093
1108
|
}
|
|
1109
|
+
assertCanCreateTask(repository.path);
|
|
1094
1110
|
const id = await nextId(store, "task", repository.name, slug);
|
|
1095
1111
|
const tasks = await allTasks(store);
|
|
1096
1112
|
const after = validateAfter(
|
|
@@ -1907,10 +1923,71 @@ async function cancelRuns(
|
|
|
1907
1923
|
}
|
|
1908
1924
|
}
|
|
1909
1925
|
|
|
1926
|
+
/** A task whose attempt put the main clone on the task's own branch (ADR 0039 D3). */
|
|
1927
|
+
function holdsClone(task: TaskData): boolean {
|
|
1928
|
+
return task.attempt?.inPlace === true && isOpenTask(task);
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
/**
|
|
1932
|
+
* The id of an open task that holds a repository's main clone on its own
|
|
1933
|
+
* branch, or undefined when none does (ADR 0039 D3).
|
|
1934
|
+
*/
|
|
1935
|
+
export async function cloneHolder(
|
|
1936
|
+
store: ObjectStore,
|
|
1937
|
+
repositoryName: string,
|
|
1938
|
+
): Promise<string | undefined> {
|
|
1939
|
+
for (const object of await allTasks(store)) {
|
|
1940
|
+
const task = taskData(object.data);
|
|
1941
|
+
if (task.repository === repositoryName && holdsClone(task)) return idOfKey(object.id);
|
|
1942
|
+
}
|
|
1943
|
+
return undefined;
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
/**
|
|
1947
|
+
* Switches the main clone back to the branch an ended branch-mode task started
|
|
1948
|
+
* on and keeps the task's branch (ADR 0039 D5). A clone that is not clean, or
|
|
1949
|
+
* that has moved, stays as it is and the returned note says so.
|
|
1950
|
+
*/
|
|
1951
|
+
function releaseClone(
|
|
1952
|
+
repositoryPath: string,
|
|
1953
|
+
key: string,
|
|
1954
|
+
task: TaskData,
|
|
1955
|
+
): string | undefined {
|
|
1956
|
+
const attempt = task.attempt;
|
|
1957
|
+
if (attempt?.inPlace !== true) return undefined;
|
|
1958
|
+
const start = attempt.startBranch;
|
|
1959
|
+
if (typeof start !== "string" || start.length === 0) return undefined;
|
|
1960
|
+
const branch =
|
|
1961
|
+
typeof attempt.branch === "string" && attempt.branch.length > 0
|
|
1962
|
+
? attempt.branch
|
|
1963
|
+
: `work/${idOfKey(key)}`;
|
|
1964
|
+
const back = switchBackToStart(repositoryPath, start, branch);
|
|
1965
|
+
return back.ok ? undefined : `${idOfKey(key)} kept its branch "${branch}": ${back.note}`;
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
/**
|
|
1969
|
+
* A branch-mode task's run works in the main clone itself, so it is stopped
|
|
1970
|
+
* before the clone is switched. Returns the runs it stopped.
|
|
1971
|
+
*/
|
|
1972
|
+
async function stopCloneRuns(
|
|
1973
|
+
control: DevRunControl | undefined,
|
|
1974
|
+
tasks: readonly StoredObject[],
|
|
1975
|
+
): Promise<ReadonlySet<string>> {
|
|
1976
|
+
const runs: RunToCancel[] = [];
|
|
1977
|
+
for (const object of tasks) {
|
|
1978
|
+
const data = taskData(object.data);
|
|
1979
|
+
if (holdsClone(data) && data.run !== null) runs.push({ run: data.run, flow: data.flowGraph });
|
|
1980
|
+
}
|
|
1981
|
+
await cancelRuns(control, runs);
|
|
1982
|
+
return new Set(runs.map((entry) => entry.run));
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1910
1985
|
interface CancelledTasks {
|
|
1911
1986
|
readonly runs: readonly RunToCancel[];
|
|
1912
1987
|
/** The tasks a batch had already pushed; they stay merged, not cancelled. */
|
|
1913
1988
|
readonly merged: readonly string[];
|
|
1989
|
+
/** What the end of a branch-mode task left in the main clone when it could not switch back. */
|
|
1990
|
+
readonly notes: readonly string[];
|
|
1914
1991
|
}
|
|
1915
1992
|
|
|
1916
1993
|
async function cancelTasks(
|
|
@@ -1918,9 +1995,11 @@ async function cancelTasks(
|
|
|
1918
1995
|
tasks: readonly StoredObject[],
|
|
1919
1996
|
home: string | undefined,
|
|
1920
1997
|
repositoryPath: string,
|
|
1998
|
+
stopped: ReadonlySet<string> = new Set(),
|
|
1921
1999
|
): Promise<CancelledTasks> {
|
|
1922
2000
|
const runs: RunToCancel[] = [];
|
|
1923
2001
|
const merged: string[] = [];
|
|
2002
|
+
const notes: string[] = [];
|
|
1924
2003
|
for (const object of tasks) {
|
|
1925
2004
|
const data = taskData(object.data);
|
|
1926
2005
|
if (!isOpenTask(data)) continue;
|
|
@@ -1935,7 +2014,11 @@ async function cancelTasks(
|
|
|
1935
2014
|
continue;
|
|
1936
2015
|
}
|
|
1937
2016
|
}
|
|
1938
|
-
if (data.run !== null
|
|
2017
|
+
if (data.run !== null && !stopped.has(data.run)) {
|
|
2018
|
+
runs.push({ run: data.run, flow: data.flowGraph });
|
|
2019
|
+
}
|
|
2020
|
+
const note = releaseClone(repositoryPath, object.id, data);
|
|
2021
|
+
if (note !== undefined) notes.push(note);
|
|
1939
2022
|
await writeTask(
|
|
1940
2023
|
store,
|
|
1941
2024
|
object.id,
|
|
@@ -1944,12 +2027,12 @@ async function cancelTasks(
|
|
|
1944
2027
|
phase: "cancelled",
|
|
1945
2028
|
run: null,
|
|
1946
2029
|
claim: null,
|
|
1947
|
-
brief: appendChange(data.brief, "cancelled"),
|
|
2030
|
+
brief: appendChange(data.brief, "cancelled", note),
|
|
1948
2031
|
},
|
|
1949
2032
|
"dev:task-cancel",
|
|
1950
2033
|
);
|
|
1951
2034
|
}
|
|
1952
|
-
return { runs, merged };
|
|
2035
|
+
return { runs, merged, notes };
|
|
1953
2036
|
}
|
|
1954
2037
|
|
|
1955
2038
|
export interface CancelOptions {
|
|
@@ -1966,6 +2049,8 @@ export interface CancelledTask {
|
|
|
1966
2049
|
readonly runs: readonly RunToCancel[];
|
|
1967
2050
|
/** Set when a batch pushed the task first: it stays merged and is not cancelled. */
|
|
1968
2051
|
readonly alreadyMerged?: string | undefined;
|
|
2052
|
+
/** Set when a branch-mode task's end could not switch the main clone back. */
|
|
2053
|
+
readonly note?: string | undefined;
|
|
1969
2054
|
}
|
|
1970
2055
|
|
|
1971
2056
|
export async function cancelPlan(
|
|
@@ -1976,23 +2061,25 @@ export async function cancelPlan(
|
|
|
1976
2061
|
readonly plan: StoredObject;
|
|
1977
2062
|
readonly runs: readonly RunToCancel[];
|
|
1978
2063
|
readonly merged: readonly string[];
|
|
2064
|
+
readonly notes: readonly string[];
|
|
1979
2065
|
}> {
|
|
1980
2066
|
const initial = await getPlan(store, id);
|
|
1981
2067
|
const initialData = planData(initial.data);
|
|
1982
2068
|
const { home } = options;
|
|
2069
|
+
const stopped = await stopCloneRuns(options.runs, await tasksOfPlan(store, id));
|
|
1983
2070
|
const cancelled = await withRepoLock(
|
|
1984
2071
|
store,
|
|
1985
2072
|
initialData.repository,
|
|
1986
2073
|
async (fresh) => {
|
|
1987
2074
|
const plan = await getPlan(store, id);
|
|
1988
2075
|
const data = planData(plan.data);
|
|
1989
|
-
if (refusePlan("cancel", data, id)) return { plan, runs: [], merged: [] };
|
|
2076
|
+
if (refusePlan("cancel", data, id)) return { plan, runs: [], merged: [], notes: [] };
|
|
1990
2077
|
const tasks = await tasksOfPlan(store, id);
|
|
1991
|
-
const { runs, merged } = await cancelTasks(store, tasks, home, fresh.path);
|
|
2078
|
+
const { runs, merged, notes } = await cancelTasks(store, tasks, home, fresh.path, stopped);
|
|
1992
2079
|
const next: PlanData = { ...data, status: "cancelled" };
|
|
1993
2080
|
const written = await writePlan(store, id, next, "dev:plan-cancel");
|
|
1994
2081
|
await refreshPlanCounts(store, written);
|
|
1995
|
-
return { plan: written, runs, merged };
|
|
2082
|
+
return { plan: written, runs, merged, notes };
|
|
1996
2083
|
},
|
|
1997
2084
|
);
|
|
1998
2085
|
await cancelRuns(options.runs, cancelled.runs);
|
|
@@ -2007,6 +2094,7 @@ export async function cancelTask(
|
|
|
2007
2094
|
const initial = await getTask(store, id);
|
|
2008
2095
|
const initialData = taskData(initial.data);
|
|
2009
2096
|
const { home } = options;
|
|
2097
|
+
const stopped = await stopCloneRuns(options.runs, [initial]);
|
|
2010
2098
|
const cancelled = await withRepoLock(
|
|
2011
2099
|
store,
|
|
2012
2100
|
initialData.repository,
|
|
@@ -2014,7 +2102,7 @@ export async function cancelTask(
|
|
|
2014
2102
|
const task = await getTask(store, id);
|
|
2015
2103
|
const data = taskData(task.data);
|
|
2016
2104
|
if (refuseTask("cancel", data, id)) {
|
|
2017
|
-
return { task, runs: [], merged: data.phase === "merged" };
|
|
2105
|
+
return { task, runs: [], merged: data.phase === "merged", note: undefined };
|
|
2018
2106
|
}
|
|
2019
2107
|
if (home !== undefined) {
|
|
2020
2108
|
const withdrawal = await withdrawIntegration(
|
|
@@ -2023,11 +2111,14 @@ export async function cancelTask(
|
|
|
2023
2111
|
idOfKey(id),
|
|
2024
2112
|
);
|
|
2025
2113
|
if (withdrawal.kind === "merged") {
|
|
2026
|
-
return { task, runs: [], merged: true };
|
|
2114
|
+
return { task, runs: [], merged: true, note: undefined };
|
|
2027
2115
|
}
|
|
2028
2116
|
}
|
|
2029
2117
|
const runs: readonly RunToCancel[] =
|
|
2030
|
-
data.run === null
|
|
2118
|
+
data.run === null || stopped.has(data.run)
|
|
2119
|
+
? []
|
|
2120
|
+
: [{ run: data.run, flow: data.flowGraph }];
|
|
2121
|
+
const note = releaseClone(fresh.path, id, data);
|
|
2031
2122
|
const written = await writeTask(
|
|
2032
2123
|
store,
|
|
2033
2124
|
id,
|
|
@@ -2036,12 +2127,12 @@ export async function cancelTask(
|
|
|
2036
2127
|
phase: "cancelled",
|
|
2037
2128
|
run: null,
|
|
2038
2129
|
claim: null,
|
|
2039
|
-
brief: appendChange(data.brief, "cancelled"),
|
|
2130
|
+
brief: appendChange(data.brief, "cancelled", note),
|
|
2040
2131
|
},
|
|
2041
2132
|
"dev:task-cancel",
|
|
2042
2133
|
);
|
|
2043
2134
|
await refreshPlanCountsOf(store, storeKey(data.repository, data.plan));
|
|
2044
|
-
return { task: written, runs, merged: false };
|
|
2135
|
+
return { task: written, runs, merged: false, note };
|
|
2045
2136
|
},
|
|
2046
2137
|
);
|
|
2047
2138
|
await cancelRuns(options.runs, cancelled.runs);
|
|
@@ -2051,6 +2142,7 @@ export async function cancelTask(
|
|
|
2051
2142
|
...(cancelled.merged
|
|
2052
2143
|
? { alreadyMerged: `${idOfKey(id)} was merged; nothing can be cancelled` }
|
|
2053
2144
|
: {}),
|
|
2145
|
+
...(cancelled.note === undefined ? {} : { note: cancelled.note }),
|
|
2054
2146
|
};
|
|
2055
2147
|
}
|
|
2056
2148
|
|
package/src/settings.ts
CHANGED
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
import { defaultOperadHome, isRecord } from "@operad-io/engine";
|
|
32
32
|
import { writeJsonAtomicSync } from "@operad-io/engine/platform";
|
|
33
33
|
import { DevError, type AutoMode } from "./model.ts";
|
|
34
|
+
import { git } from "./git.ts";
|
|
34
35
|
import { repositoryPathKey } from "./repository-key.ts";
|
|
35
36
|
import {
|
|
36
37
|
type DevFlowSpec,
|
|
@@ -289,10 +290,18 @@ export interface ReviewerSettings {
|
|
|
289
290
|
readonly maxDiffBytes: number;
|
|
290
291
|
}
|
|
291
292
|
|
|
293
|
+
/** Where a repository's tasks work; see ADR 0039 D1. */
|
|
294
|
+
export type WorkIn = "branch" | "worktree";
|
|
295
|
+
|
|
292
296
|
/** The whole `.operad/dev.json` document, with every default filled. */
|
|
293
297
|
export interface DevSettings {
|
|
294
298
|
readonly name?: string | undefined;
|
|
295
|
-
|
|
299
|
+
/**
|
|
300
|
+
* The git remote task branches are fetched from and pushed to. Undefined
|
|
301
|
+
* means the repository has none: the local main branch is the base, nothing
|
|
302
|
+
* is fetched or pushed, and a merge only moves the local main branch.
|
|
303
|
+
*/
|
|
304
|
+
readonly remote?: string | undefined;
|
|
296
305
|
readonly mainBranch: string;
|
|
297
306
|
readonly worktreeRoot: string;
|
|
298
307
|
/**
|
|
@@ -300,6 +309,11 @@ export interface DevSettings {
|
|
|
300
309
|
* it, the default pool's counts alone decide (see `runningTaskCap`).
|
|
301
310
|
*/
|
|
302
311
|
readonly concurrency: number;
|
|
312
|
+
/**
|
|
313
|
+
* Where a task's changes are made: a branch in the main clone (`branch`, one
|
|
314
|
+
* task at a time) or the task's own worktree (`worktree`, the default).
|
|
315
|
+
*/
|
|
316
|
+
readonly workIn: WorkIn;
|
|
303
317
|
/** Whether the file wrote `concurrency` itself. */
|
|
304
318
|
readonly concurrencySet: boolean;
|
|
305
319
|
/**
|
|
@@ -350,8 +364,8 @@ export const DEFAULT_AFTER_MERGE: readonly string[] = [
|
|
|
350
364
|
"if [ -e \"$OPERAD_WORKTREE\" ]; then git worktree remove --force \"$OPERAD_WORKTREE\"; fi",
|
|
351
365
|
"if [ -e \"$OPERAD_WORKTREE-gate\" ]; then git worktree remove --force \"$OPERAD_WORKTREE-gate\"; fi",
|
|
352
366
|
"git worktree prune",
|
|
353
|
-
"if git show-ref --verify --quiet \"refs/heads/work/$OPERAD_TASK\"; then git branch -D \"work/$OPERAD_TASK\"; fi",
|
|
354
|
-
"if git ls-remote --exit-code --heads \"$OPERAD_REMOTE\" \"work/$OPERAD_TASK\" >/dev/null; then git push \"$OPERAD_REMOTE\" --delete \"work/$OPERAD_TASK\"; else [ $? -eq 2 ]; fi",
|
|
367
|
+
"if git show-ref --verify --quiet \"refs/heads/work/$OPERAD_TASK\" && [ \"$(git symbolic-ref --short -q HEAD)\" != \"work/$OPERAD_TASK\" ]; then git branch -D \"work/$OPERAD_TASK\"; fi",
|
|
368
|
+
"if [ -z \"$OPERAD_REMOTE\" ]; then :; elif git ls-remote --exit-code --heads \"$OPERAD_REMOTE\" \"work/$OPERAD_TASK\" >/dev/null; then git push \"$OPERAD_REMOTE\" --delete \"work/$OPERAD_TASK\"; else [ $? -eq 2 ]; fi",
|
|
355
369
|
];
|
|
356
370
|
|
|
357
371
|
/**
|
|
@@ -370,6 +384,7 @@ export const DEFAULT_DEV_SETTINGS: DevSettings = {
|
|
|
370
384
|
mainBranch: "main",
|
|
371
385
|
worktreeRoot: ".operad-worktrees",
|
|
372
386
|
concurrency: 1,
|
|
387
|
+
workIn: "worktree",
|
|
373
388
|
concurrencySet: false,
|
|
374
389
|
worker: {
|
|
375
390
|
use: DEFAULT_WORKER_USE,
|
|
@@ -431,6 +446,7 @@ const KNOWN_FIELDS = new Set([
|
|
|
431
446
|
"worktrees",
|
|
432
447
|
"sandbox",
|
|
433
448
|
"concurrency",
|
|
449
|
+
"workIn",
|
|
434
450
|
"worker",
|
|
435
451
|
"workers",
|
|
436
452
|
"pools",
|
|
@@ -520,6 +536,13 @@ export function assertInsideRepository(
|
|
|
520
536
|
}
|
|
521
537
|
}
|
|
522
538
|
|
|
539
|
+
function workInOf(value: unknown, field: string): WorkIn {
|
|
540
|
+
if (value !== "branch" && value !== "worktree") {
|
|
541
|
+
throw new DevError(field, 'must be "branch" or "worktree"');
|
|
542
|
+
}
|
|
543
|
+
return value;
|
|
544
|
+
}
|
|
545
|
+
|
|
523
546
|
function autoMode(value: unknown, field: string): AutoMode {
|
|
524
547
|
if (value !== "off" && value !== "run" && value !== "merge") {
|
|
525
548
|
throw new DevError(field, 'must be "off", "run" or "merge"');
|
|
@@ -1122,6 +1145,7 @@ export function runningTaskCap(settings: DevSettings, pool: string = DEFAULT_POO
|
|
|
1122
1145
|
* cap added up, and the file's `concurrency` when written caps them together.
|
|
1123
1146
|
*/
|
|
1124
1147
|
export function repositoryTaskCap(settings: DevSettings): number {
|
|
1148
|
+
if (settings.workIn === "branch") return 1;
|
|
1125
1149
|
const total = Object.keys(settings.workers.pools).reduce(
|
|
1126
1150
|
(sum, name) => sum + poolCap(settings, name),
|
|
1127
1151
|
0,
|
|
@@ -1315,6 +1339,35 @@ export function useNewStageNames(value: unknown): void {
|
|
|
1315
1339
|
}
|
|
1316
1340
|
}
|
|
1317
1341
|
|
|
1342
|
+
/**
|
|
1343
|
+
* A repository that works in a branch of the main clone runs one task at a
|
|
1344
|
+
* time (ADR 0039 D2): refuse more, and say which setting to change.
|
|
1345
|
+
*/
|
|
1346
|
+
function refuseBranchWithMany(
|
|
1347
|
+
concurrency: number,
|
|
1348
|
+
concurrencyWritten: boolean,
|
|
1349
|
+
workers: ParsedWorkers,
|
|
1350
|
+
): void {
|
|
1351
|
+
if (concurrencyWritten && concurrency > 1) {
|
|
1352
|
+
throw new DevError(
|
|
1353
|
+
"workIn",
|
|
1354
|
+
`"branch" runs one task at a time, but concurrency is ${String(concurrency)}: set concurrency to 1, or set workIn to "worktree"`,
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1357
|
+
for (const [pool, names] of Object.entries(workers.pools)) {
|
|
1358
|
+
let total = 0;
|
|
1359
|
+
for (const name of names) {
|
|
1360
|
+
total += workers.workers.find((item) => item.name === name)?.count ?? 0;
|
|
1361
|
+
}
|
|
1362
|
+
if (total > 1) {
|
|
1363
|
+
throw new DevError(
|
|
1364
|
+
"workIn",
|
|
1365
|
+
`"branch" runs one task at a time, but the workers of pool "${pool}" add up to ${String(total)}: lower their counts to 1 in all, or set workIn to "worktree"`,
|
|
1366
|
+
);
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1318
1371
|
/**
|
|
1319
1372
|
* Validates one parsed dev.json value and fills the defaults.
|
|
1320
1373
|
*/
|
|
@@ -1336,6 +1389,13 @@ export function validateDevSettings(
|
|
|
1336
1389
|
? defaults.concurrency
|
|
1337
1390
|
: positiveInteger(value.concurrency, "concurrency");
|
|
1338
1391
|
const { worker, workers } = workerSettingsOf(value, concurrency, defaults);
|
|
1392
|
+
const workIn =
|
|
1393
|
+
value.workIn === undefined
|
|
1394
|
+
? DEFAULT_DEV_SETTINGS.workIn
|
|
1395
|
+
: workInOf(value.workIn, "workIn");
|
|
1396
|
+
if (workIn === "branch") {
|
|
1397
|
+
refuseBranchWithMany(concurrency, value.concurrency !== undefined, workers);
|
|
1398
|
+
}
|
|
1339
1399
|
return {
|
|
1340
1400
|
...(value.name === undefined
|
|
1341
1401
|
? {}
|
|
@@ -1350,6 +1410,7 @@ export function validateDevSettings(
|
|
|
1350
1410
|
: nonEmptyString(value.mainBranch, "mainBranch"),
|
|
1351
1411
|
worktreeRoot: worktreeRoot.root,
|
|
1352
1412
|
concurrency,
|
|
1413
|
+
workIn,
|
|
1353
1414
|
concurrencySet: value.concurrency !== undefined,
|
|
1354
1415
|
worker,
|
|
1355
1416
|
workers,
|
|
@@ -1548,7 +1609,27 @@ function settingsFromText(
|
|
|
1548
1609
|
): ReadSettings {
|
|
1549
1610
|
const parsed = parseSettingsText(path, text);
|
|
1550
1611
|
checkSettingsPaths(repositoryPath, parsed);
|
|
1551
|
-
|
|
1612
|
+
const raw: unknown = JSON.parse(text);
|
|
1613
|
+
const named = isRecord(raw) && raw.remote !== undefined;
|
|
1614
|
+
return {
|
|
1615
|
+
settings: named ? parsed.settings : withoutAbsentRemote(repositoryPath, parsed.settings),
|
|
1616
|
+
text,
|
|
1617
|
+
};
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
/**
|
|
1621
|
+
* The settings of a file that names no remote: the default remote when the
|
|
1622
|
+
* repository has it, else none. A repository with no remote is a normal one.
|
|
1623
|
+
* The exit status of `git config --get` is the answer; it is not read from
|
|
1624
|
+
* output that can be empty under load.
|
|
1625
|
+
*/
|
|
1626
|
+
function withoutAbsentRemote(repositoryPath: string, settings: DevSettings): DevSettings {
|
|
1627
|
+
const name = settings.remote;
|
|
1628
|
+
if (name === undefined) return settings;
|
|
1629
|
+
if (git(repositoryPath, ["config", "--local", "--get", `remote.${name}.url`]).ok) {
|
|
1630
|
+
return settings;
|
|
1631
|
+
}
|
|
1632
|
+
return { ...settings, remote: undefined };
|
|
1552
1633
|
}
|
|
1553
1634
|
|
|
1554
1635
|
/** Reads the file as it is now; any problem with it is thrown. */
|
|
@@ -1560,7 +1641,7 @@ function readSettingsNow(repositoryPath: string): ReadSettings {
|
|
|
1560
1641
|
resolve(repositoryPath, DEFAULT_DEV_SETTINGS.worktreeRoot),
|
|
1561
1642
|
"worktreeRoot",
|
|
1562
1643
|
);
|
|
1563
|
-
return { settings: defaultDevSettings(), text: "" };
|
|
1644
|
+
return { settings: withoutAbsentRemote(repositoryPath, defaultDevSettings()), text: "" };
|
|
1564
1645
|
}
|
|
1565
1646
|
let text: string;
|
|
1566
1647
|
try {
|
|
@@ -1637,7 +1718,7 @@ function readDefaults(repositoryPath: string): ReadSettings {
|
|
|
1637
1718
|
resolve(repositoryPath, DEFAULT_DEV_SETTINGS.worktreeRoot),
|
|
1638
1719
|
"worktreeRoot",
|
|
1639
1720
|
);
|
|
1640
|
-
return { settings: defaultDevSettings(), text: "" };
|
|
1721
|
+
return { settings: withoutAbsentRemote(repositoryPath, defaultDevSettings()), text: "" };
|
|
1641
1722
|
}
|
|
1642
1723
|
|
|
1643
1724
|
/**
|
|
@@ -1647,17 +1728,35 @@ function readDefaults(repositoryPath: string): ReadSettings {
|
|
|
1647
1728
|
* repository that never had valid settings gets the error.
|
|
1648
1729
|
*/
|
|
1649
1730
|
export function readDevSettingsNow(repositoryPath: string): DevSettings {
|
|
1731
|
+
return readDevSettingsText(repositoryPath).settings;
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
/**
|
|
1735
|
+
* Like `readDevSettingsNow`, with the file's text as well (empty when the file
|
|
1736
|
+
* does not exist). A step records the text when an attempt starts (ADR 0039 D6).
|
|
1737
|
+
*/
|
|
1738
|
+
export function readDevSettingsText(repositoryPath: string): {
|
|
1739
|
+
readonly settings: DevSettings;
|
|
1740
|
+
readonly text: string;
|
|
1741
|
+
} {
|
|
1650
1742
|
try {
|
|
1651
1743
|
const read = readSettingsNow(repositoryPath);
|
|
1652
1744
|
keep(repositoryPath, read);
|
|
1653
|
-
return read
|
|
1745
|
+
return read;
|
|
1654
1746
|
} catch (error) {
|
|
1655
1747
|
const kept = recall(repositoryPath);
|
|
1656
|
-
if (kept !== undefined) return kept
|
|
1748
|
+
if (kept !== undefined) return kept;
|
|
1657
1749
|
throw error;
|
|
1658
1750
|
}
|
|
1659
1751
|
}
|
|
1660
1752
|
|
|
1753
|
+
/** The settings a recorded text gives, read the way the file is read. */
|
|
1754
|
+
export function devSettingsFromText(repositoryPath: string, text: string): DevSettings {
|
|
1755
|
+
return text === ""
|
|
1756
|
+
? readDefaults(repositoryPath).settings
|
|
1757
|
+
: settingsFromText(repositoryPath, join(repositoryPath, DEV_SETTINGS_FILE), text).settings;
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1661
1760
|
/** Reads a repository's settings; an action's schema uses the synchronous form. */
|
|
1662
1761
|
export async function readDevSettings(
|
|
1663
1762
|
repositoryPath: string,
|