@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
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flows a project writes in TypeScript: `.operad/flows/<name>.flow.ts`, a
|
|
3
|
+
* `defineFlow` value built from dev's documented steps. dev applies the same
|
|
4
|
+
* two hard rules it applies to a dev.json flow and refuses a file that breaks
|
|
5
|
+
* one, or whose name dev.json also defines.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { pathToFileURL } from "node:url";
|
|
12
|
+
import type { FlowDefinition, StepShape } from "@operad-io/engine";
|
|
13
|
+
import type { DevSettings } from "./settings.ts";
|
|
14
|
+
import { isRecord } from "@operad-io/engine";
|
|
15
|
+
|
|
16
|
+
const FLOW_SUFFIX = ".flow.ts";
|
|
17
|
+
|
|
18
|
+
/** The directory of a repository's own flows, relative to the repository. */
|
|
19
|
+
export const PROJECT_FLOWS_DIR = ".operad/flows";
|
|
20
|
+
|
|
21
|
+
/** The flows a repository's `.operad/flows` gives, and the names dev refused. */
|
|
22
|
+
export interface ProjectFlows {
|
|
23
|
+
/** Each accepted flow by name, ready to declare under its registered name. */
|
|
24
|
+
readonly flows: ReadonlyMap<string, FlowDefinition>;
|
|
25
|
+
/** Why a name is refused; the name starts no task. */
|
|
26
|
+
readonly refused: ReadonlyMap<string, string>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function useOf(step: Record<string, unknown>): string | undefined {
|
|
30
|
+
if (step.kind !== "plugin" || !isRecord(step.params)) return undefined;
|
|
31
|
+
return typeof step.params.use === "string" ? step.params.use : undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function targetsOf(step: Record<string, unknown>): string[] {
|
|
35
|
+
return isRecord(step.next)
|
|
36
|
+
? Object.values(step.next).filter((item): item is string => typeof item === "string")
|
|
37
|
+
: [];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The reason a flow breaks a hard rule, or undefined when it keeps both: the
|
|
42
|
+
* step before a finished end is integrate (or the cleanup that follows it), and
|
|
43
|
+
* there is at least one check, review or human step.
|
|
44
|
+
*/
|
|
45
|
+
export function flowProblem(flow: unknown): string | undefined {
|
|
46
|
+
if (!isRecord(flow) || !Array.isArray(flow.steps) || typeof flow.entry !== "string") {
|
|
47
|
+
return "is not a flow built with defineFlow";
|
|
48
|
+
}
|
|
49
|
+
const steps = flow.steps.filter(isRecord);
|
|
50
|
+
const decides = steps.some((step) => {
|
|
51
|
+
const use = useOf(step);
|
|
52
|
+
return step.kind === "wait_person" || use === "dev/verify" || use === "dev/review";
|
|
53
|
+
});
|
|
54
|
+
if (!decides) return "needs at least one check, review or human step";
|
|
55
|
+
if (!steps.some((step) => useOf(step) === "dev/integrate")) {
|
|
56
|
+
return "has no integrate step: a change reaches main only through integrate";
|
|
57
|
+
}
|
|
58
|
+
const byId = new Map<string, Record<string, unknown>>();
|
|
59
|
+
for (const step of steps) if (typeof step.id === "string") byId.set(step.id, step);
|
|
60
|
+
const doneEnds = new Set<string>(["end"]);
|
|
61
|
+
for (const step of steps) {
|
|
62
|
+
if (step.kind === "end" && isRecord(step.params) && step.params.status === "done") {
|
|
63
|
+
if (typeof step.id === "string") doneEnds.add(step.id);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const before = (target: string): Record<string, unknown>[] =>
|
|
67
|
+
steps.filter((step) => targetsOf(step).includes(target));
|
|
68
|
+
for (const end of doneEnds) {
|
|
69
|
+
for (const from of before(end)) {
|
|
70
|
+
const use = useOf(from);
|
|
71
|
+
if (use === "dev/integrate") continue;
|
|
72
|
+
if (use === "dev/cleanup" && typeof from.id === "string") {
|
|
73
|
+
const behind = before(from.id);
|
|
74
|
+
if (behind.length > 0 && behind.every((step) => useOf(step) === "dev/integrate")) continue;
|
|
75
|
+
}
|
|
76
|
+
return `ends without integrate: step "${String(from.id)}" leads to the end, and only integrate may`;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
interface Loaded {
|
|
83
|
+
readonly stamp: string;
|
|
84
|
+
readonly module: unknown;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const imported = new Map<string, Loaded>();
|
|
88
|
+
let importCount = 0;
|
|
89
|
+
|
|
90
|
+
async function importFlow(path: string): Promise<unknown> {
|
|
91
|
+
const info = statSync(path);
|
|
92
|
+
const stamp = `${info.mtimeMs}:${info.size}`;
|
|
93
|
+
const kept = imported.get(path);
|
|
94
|
+
if (kept?.stamp === stamp) return kept.module;
|
|
95
|
+
importCount += 1;
|
|
96
|
+
const url = pathToFileURL(path);
|
|
97
|
+
url.searchParams.set("operad-project-flow", String(importCount));
|
|
98
|
+
const module = (await import(url.href)) as Record<string, unknown>;
|
|
99
|
+
imported.set(path, { stamp, module: module.default });
|
|
100
|
+
return module.default;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function registeredName(repository: string, name: string, steps: readonly StepShape[]): string {
|
|
104
|
+
const slug = (text: string): string =>
|
|
105
|
+
text.replace(/[^A-Za-z0-9_-]+/g, "_").replace(/^_+|_+$/g, "") || "flow";
|
|
106
|
+
const hash = createHash("sha1").update(JSON.stringify(steps)).digest("hex").slice(0, 12);
|
|
107
|
+
return `dev/${slug(repository)}/${slug(name)}/${hash}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Loads the repository's TypeScript flows; every problem becomes a refusal. */
|
|
111
|
+
export async function loadProjectFlows(
|
|
112
|
+
repositoryPath: string,
|
|
113
|
+
repository: string,
|
|
114
|
+
settings: DevSettings,
|
|
115
|
+
): Promise<ProjectFlows> {
|
|
116
|
+
const flows = new Map<string, FlowDefinition>();
|
|
117
|
+
const refused = new Map<string, string>();
|
|
118
|
+
const directory = join(repositoryPath, PROJECT_FLOWS_DIR);
|
|
119
|
+
if (!existsSync(directory)) return { flows, refused };
|
|
120
|
+
const files = readdirSync(directory)
|
|
121
|
+
.filter((file) => file.endsWith(FLOW_SUFFIX))
|
|
122
|
+
.sort();
|
|
123
|
+
for (const file of files) {
|
|
124
|
+
const name = file.slice(0, -FLOW_SUFFIX.length);
|
|
125
|
+
const where = `${PROJECT_FLOWS_DIR}/${file}`;
|
|
126
|
+
if (settings.flows[name] !== undefined) {
|
|
127
|
+
refused.set(
|
|
128
|
+
name,
|
|
129
|
+
`flow "${name}" is defined in dev.json and in ${where}; keep one`,
|
|
130
|
+
);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
let value: unknown;
|
|
134
|
+
try {
|
|
135
|
+
value = await importFlow(join(directory, file));
|
|
136
|
+
} catch (error) {
|
|
137
|
+
refused.set(
|
|
138
|
+
name,
|
|
139
|
+
`${where}: could not be loaded: ${error instanceof Error ? error.message : String(error)}`,
|
|
140
|
+
);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
const problem = flowProblem(value);
|
|
144
|
+
if (problem !== undefined) {
|
|
145
|
+
refused.set(name, `${where}: ${problem}`);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const flow = value as FlowDefinition;
|
|
149
|
+
// A review step that names no grading reads the entry of this flow's name.
|
|
150
|
+
const steps = flow.steps.map((step): StepShape => {
|
|
151
|
+
if (useOf(step as unknown as Record<string, unknown>) !== "dev/review") return step;
|
|
152
|
+
const outer = step.params as { readonly use: string; readonly params: unknown };
|
|
153
|
+
const inner = isRecord(outer.params) ? outer.params : {};
|
|
154
|
+
if (inner.grading !== undefined) return step;
|
|
155
|
+
return { ...step, params: { ...outer, params: { ...inner, grading: name } } };
|
|
156
|
+
});
|
|
157
|
+
flows.set(name, { ...flow, steps, name: registeredName(repository, name, steps) });
|
|
158
|
+
}
|
|
159
|
+
return { flows, refused };
|
|
160
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a client may read and change in a registered repository's
|
|
3
|
+
* `.operad/dev.json` and `.operad/review.json`. These are the kernel's small
|
|
4
|
+
* schema subset; the full schemas editors read are `dev.schema.json` and the
|
|
5
|
+
* review grading package's `review.schema.json`. A setting is `editable` when a
|
|
6
|
+
* person tunes it from a client, and `confirm` when changing it changes what
|
|
7
|
+
* merges.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ProjectSettingsDeclaration } from "@operad-io/engine";
|
|
11
|
+
import { listRepositories } from "./service.ts";
|
|
12
|
+
|
|
13
|
+
export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
|
|
14
|
+
repositories: async ({ store }) =>
|
|
15
|
+
(await listRepositories(store)).map(({ name, path }) => ({ name, path })),
|
|
16
|
+
files: [
|
|
17
|
+
{
|
|
18
|
+
name: "dev",
|
|
19
|
+
path: "dev.json",
|
|
20
|
+
schema: {
|
|
21
|
+
type: "object",
|
|
22
|
+
$defs: {
|
|
23
|
+
checkCommand: {
|
|
24
|
+
oneOf: [
|
|
25
|
+
{ type: "string", minLength: 1 },
|
|
26
|
+
{
|
|
27
|
+
type: "object",
|
|
28
|
+
properties: {
|
|
29
|
+
run: { type: "string", minLength: 1 },
|
|
30
|
+
failed: {
|
|
31
|
+
type: "object",
|
|
32
|
+
properties: {
|
|
33
|
+
match: { type: "string", minLength: 1 },
|
|
34
|
+
rerun: { type: "string", minLength: 1 },
|
|
35
|
+
max: { type: "integer", minimum: 1 },
|
|
36
|
+
stress: { type: "string", minLength: 1 },
|
|
37
|
+
rule: { type: "string", minLength: 1 },
|
|
38
|
+
},
|
|
39
|
+
required: ["match", "rerun"],
|
|
40
|
+
additionalProperties: false,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
required: ["run"],
|
|
44
|
+
additionalProperties: false,
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
properties: {
|
|
50
|
+
concurrency: {
|
|
51
|
+
type: "integer",
|
|
52
|
+
minimum: 1,
|
|
53
|
+
editable: true,
|
|
54
|
+
description:
|
|
55
|
+
"The most of this repository's tasks that run at the same time, on top of the worker counts. Unset means the workers' own counts decide.",
|
|
56
|
+
},
|
|
57
|
+
autoDefault: {
|
|
58
|
+
type: "string",
|
|
59
|
+
enum: ["off", "run", "merge"],
|
|
60
|
+
default: "off",
|
|
61
|
+
editable: true,
|
|
62
|
+
confirm: true,
|
|
63
|
+
description:
|
|
64
|
+
"The auto level a new plan starts at: off waits for you to start tasks, run starts them by itself, merge also merges a task whose review passed. Plans already made keep their level.",
|
|
65
|
+
},
|
|
66
|
+
gate: {
|
|
67
|
+
type: "object",
|
|
68
|
+
properties: {
|
|
69
|
+
commands: {
|
|
70
|
+
type: "array",
|
|
71
|
+
items: { $ref: "#/$defs/checkCommand" },
|
|
72
|
+
default: [],
|
|
73
|
+
editable: true,
|
|
74
|
+
description:
|
|
75
|
+
"The checks a task must pass before it is reviewed, run in order. Each is a shell line; a check with a rerun of its failed tests is edited in the file.",
|
|
76
|
+
},
|
|
77
|
+
commandTimeoutSecs: {
|
|
78
|
+
type: "number",
|
|
79
|
+
minimum: 1,
|
|
80
|
+
default: 1800,
|
|
81
|
+
editable: true,
|
|
82
|
+
description:
|
|
83
|
+
"The longest one check may run, in seconds, before it counts as failed.",
|
|
84
|
+
},
|
|
85
|
+
stallSecs: {
|
|
86
|
+
type: "number",
|
|
87
|
+
minimum: 1,
|
|
88
|
+
editable: true,
|
|
89
|
+
description:
|
|
90
|
+
"How long a check may write nothing, in seconds, before it is stopped and fails as a stall. Unset turns the limit off.",
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
reviewer: {
|
|
95
|
+
type: "object",
|
|
96
|
+
properties: {
|
|
97
|
+
use: {
|
|
98
|
+
type: "string",
|
|
99
|
+
minLength: 1,
|
|
100
|
+
default: "radian/agent",
|
|
101
|
+
editable: true,
|
|
102
|
+
description:
|
|
103
|
+
"Which agent reviews a task's changes, as plugin/step, such as claude/agent.",
|
|
104
|
+
},
|
|
105
|
+
args: {
|
|
106
|
+
type: "array",
|
|
107
|
+
items: { type: "string" },
|
|
108
|
+
default: [],
|
|
109
|
+
editable: true,
|
|
110
|
+
description:
|
|
111
|
+
"Extra arguments for the reviewing agent; this is where its model is chosen.",
|
|
112
|
+
},
|
|
113
|
+
timeoutSecs: {
|
|
114
|
+
type: "number",
|
|
115
|
+
minimum: 1,
|
|
116
|
+
default: 600,
|
|
117
|
+
editable: true,
|
|
118
|
+
description:
|
|
119
|
+
"The longest one review may take, in seconds, before it counts as timed out.",
|
|
120
|
+
},
|
|
121
|
+
fixLevel: {
|
|
122
|
+
type: "string",
|
|
123
|
+
enum: ["critical", "major", "minor"],
|
|
124
|
+
default: "major",
|
|
125
|
+
editable: true,
|
|
126
|
+
confirm: true,
|
|
127
|
+
description:
|
|
128
|
+
"The lowest severity of a review finding that sends a task back for rework instead of to you. Lower it and fewer tasks reach a merge unexamined; raise it and more do.",
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
name: "review",
|
|
137
|
+
path: "review.json",
|
|
138
|
+
schema: {
|
|
139
|
+
type: "object",
|
|
140
|
+
properties: {
|
|
141
|
+
default: {
|
|
142
|
+
type: "object",
|
|
143
|
+
properties: {
|
|
144
|
+
levels: {
|
|
145
|
+
type: "object",
|
|
146
|
+
properties: {
|
|
147
|
+
critical: {
|
|
148
|
+
type: "string",
|
|
149
|
+
editable: true,
|
|
150
|
+
confirm: true,
|
|
151
|
+
description:
|
|
152
|
+
"What the reviewer should call critical in this repository.",
|
|
153
|
+
},
|
|
154
|
+
major: {
|
|
155
|
+
type: "string",
|
|
156
|
+
editable: true,
|
|
157
|
+
confirm: true,
|
|
158
|
+
description:
|
|
159
|
+
"What the reviewer should call major in this repository.",
|
|
160
|
+
},
|
|
161
|
+
minor: {
|
|
162
|
+
type: "string",
|
|
163
|
+
editable: true,
|
|
164
|
+
confirm: true,
|
|
165
|
+
description:
|
|
166
|
+
"What the reviewer should call minor in this repository.",
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
rules: {
|
|
171
|
+
type: "array",
|
|
172
|
+
items: { type: "string" },
|
|
173
|
+
editable: true,
|
|
174
|
+
confirm: true,
|
|
175
|
+
description:
|
|
176
|
+
"Extra rules the reviewer follows when it grades a finding, one sentence each.",
|
|
177
|
+
},
|
|
178
|
+
fixLevel: {
|
|
179
|
+
type: "string",
|
|
180
|
+
enum: ["critical", "major", "minor"],
|
|
181
|
+
editable: true,
|
|
182
|
+
confirm: true,
|
|
183
|
+
description:
|
|
184
|
+
"The lowest severity that sends a task back for rework. Wins over the same setting in dev.json.",
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
};
|
package/src/refusals.ts
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an action says when it cannot run, or has nothing to do.
|
|
3
|
+
*
|
|
4
|
+
* One table per object kind maps (action, state) to a line. The states are the
|
|
5
|
+
* words a row already shows (a task's `state`, a plan's `state`) plus the
|
|
6
|
+
* waiting reason a person can see next to the row. A new action or a new
|
|
7
|
+
* state does not compile until its line is here, and a test walks the tables.
|
|
8
|
+
*
|
|
9
|
+
* A line is one of:
|
|
10
|
+
* - `ok`: the action works in this state (optionally with a line for a value
|
|
11
|
+
* that is already set);
|
|
12
|
+
* - `done`: the result is already true, so the action succeeds with the line
|
|
13
|
+
* and does nothing;
|
|
14
|
+
* - `refuse`: the action cannot run now; the line says where the object is and
|
|
15
|
+
* when the action becomes possible.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
isIntegrating,
|
|
20
|
+
planState,
|
|
21
|
+
TASK_PHASES,
|
|
22
|
+
type PlanData,
|
|
23
|
+
type PlanState,
|
|
24
|
+
type TaskData,
|
|
25
|
+
} from "./model.ts";
|
|
26
|
+
import {
|
|
27
|
+
flowHasReverify,
|
|
28
|
+
inReviewWait,
|
|
29
|
+
waitChoices,
|
|
30
|
+
waitKindOf,
|
|
31
|
+
type WaitAction,
|
|
32
|
+
} from "./waits.ts";
|
|
33
|
+
|
|
34
|
+
export { inReviewWait } from "./waits.ts";
|
|
35
|
+
|
|
36
|
+
/** What a line may mention besides the object's own id. */
|
|
37
|
+
export interface Facts {
|
|
38
|
+
/** The object's id as a person types it. */
|
|
39
|
+
readonly id: string;
|
|
40
|
+
/** A task's plan, when it could be read. */
|
|
41
|
+
readonly plan?: { readonly id: string; readonly state: PlanState } | undefined;
|
|
42
|
+
/** The `level` or `count` argument of a set-a-value action. */
|
|
43
|
+
readonly value?: string | number | undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type Line =
|
|
47
|
+
| { readonly kind: "ok"; readonly same?: (facts: Facts) => string }
|
|
48
|
+
| { readonly kind: "done"; readonly say: (facts: Facts) => string }
|
|
49
|
+
| { readonly kind: "refuse"; readonly say: (facts: Facts) => string };
|
|
50
|
+
|
|
51
|
+
/** The states of a task: its phases, the integrate mark, and the wait for the review model. */
|
|
52
|
+
export const TASK_STATES = [...TASK_PHASES, "integrating", "waiting-review"] as const;
|
|
53
|
+
export type TaskState = (typeof TASK_STATES)[number];
|
|
54
|
+
|
|
55
|
+
export const TASK_ACTIONS = [
|
|
56
|
+
"confirm",
|
|
57
|
+
"cancel",
|
|
58
|
+
"pause",
|
|
59
|
+
"resume",
|
|
60
|
+
"merge",
|
|
61
|
+
"reject",
|
|
62
|
+
"rework",
|
|
63
|
+
"reverify",
|
|
64
|
+
] as const;
|
|
65
|
+
export type TaskActionName = (typeof TASK_ACTIONS)[number];
|
|
66
|
+
|
|
67
|
+
export const PLAN_ACTIONS = [
|
|
68
|
+
"confirm",
|
|
69
|
+
"cancel",
|
|
70
|
+
"auto",
|
|
71
|
+
"priority",
|
|
72
|
+
"concurrency",
|
|
73
|
+
] as const;
|
|
74
|
+
export type PlanActionName = (typeof PLAN_ACTIONS)[number];
|
|
75
|
+
|
|
76
|
+
const ok: Line = { kind: "ok" };
|
|
77
|
+
const okSame = (same: (facts: Facts) => string): Line => ({ kind: "ok", same });
|
|
78
|
+
const done = (say: (facts: Facts) => string): Line => ({ kind: "done", say });
|
|
79
|
+
const refuse = (say: (facts: Facts) => string): Line => ({ kind: "refuse", say });
|
|
80
|
+
|
|
81
|
+
/** The lines of an action that answers the wait for a person. */
|
|
82
|
+
function personWait(
|
|
83
|
+
action: string,
|
|
84
|
+
participle: string,
|
|
85
|
+
settled: boolean,
|
|
86
|
+
): Record<TaskState, Line> {
|
|
87
|
+
const once = (where: string) =>
|
|
88
|
+
refuse(({ id }) => `${id} is ${where}; dev:${action} works once it waits for you`);
|
|
89
|
+
return {
|
|
90
|
+
proposed: refuse(
|
|
91
|
+
({ id }) => `${id} is proposed; dev:${action} works once it has run and waits for you`,
|
|
92
|
+
),
|
|
93
|
+
ready: refuse(
|
|
94
|
+
({ id }) => `${id} is ready; dev:${action} works once it has run and waits for you`,
|
|
95
|
+
),
|
|
96
|
+
running: once("running"),
|
|
97
|
+
review: once("running (review)"),
|
|
98
|
+
waiting: ok,
|
|
99
|
+
integrating: once("integrating"),
|
|
100
|
+
"waiting-review": refuse(
|
|
101
|
+
({ id }) =>
|
|
102
|
+
`${id} is waiting for the review model to come back; dev:${action} works once it needs you`,
|
|
103
|
+
),
|
|
104
|
+
paused: once("paused"),
|
|
105
|
+
merged: settled
|
|
106
|
+
? done(({ id }) => `${id} is already merged; nothing to do`)
|
|
107
|
+
: refuse(({ id }) => `${id} was merged; nothing can be ${participle}`),
|
|
108
|
+
cancelled: refuse(({ id }) => `${id} was cancelled; nothing can be ${participle}`),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const started = (where: string) =>
|
|
113
|
+
refuse(({ id }) => `${id} is ${where}; nothing to confirm`);
|
|
114
|
+
|
|
115
|
+
/** `resume` works on a paused task only; anywhere else it says where the task is. */
|
|
116
|
+
function resumeLines(): Record<TaskState, Line> {
|
|
117
|
+
const not = (where: string) =>
|
|
118
|
+
refuse(({ id }) => `${id} is ${where}; dev:resume works on a paused task`);
|
|
119
|
+
return {
|
|
120
|
+
proposed: not("proposed"),
|
|
121
|
+
ready: done(({ id }) => `${id} is already ready; nothing to do`),
|
|
122
|
+
running: not("running"),
|
|
123
|
+
review: not("running (review)"),
|
|
124
|
+
waiting: not("waiting"),
|
|
125
|
+
integrating: not("integrating"),
|
|
126
|
+
"waiting-review": not("waiting"),
|
|
127
|
+
paused: ok,
|
|
128
|
+
merged: refuse(({ id }) => `${id} was merged; nothing can be resumed`),
|
|
129
|
+
cancelled: refuse(({ id }) => `${id} was cancelled; nothing can be resumed`),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const TASK_LINES: Record<TaskActionName, Record<TaskState, Line>> = {
|
|
134
|
+
confirm: {
|
|
135
|
+
proposed: refuse(({ id, plan }) => {
|
|
136
|
+
if (plan === undefined) return `${id} is proposed; its plan cannot be found`;
|
|
137
|
+
const at = `${id} is in ${plan.state} plan ${plan.id}`;
|
|
138
|
+
switch (plan.state) {
|
|
139
|
+
case "draft":
|
|
140
|
+
return `${at}; confirm the plan first (dev:confirm ${plan.id})`;
|
|
141
|
+
case "paused":
|
|
142
|
+
return `${at}; dev:confirm works once the plan is confirmed`;
|
|
143
|
+
default:
|
|
144
|
+
return `${at}; nothing can be confirmed`;
|
|
145
|
+
}
|
|
146
|
+
}),
|
|
147
|
+
ready: done(({ id }) => `${id} is already ready; nothing to do`),
|
|
148
|
+
running: started("running"),
|
|
149
|
+
review: started("running (review)"),
|
|
150
|
+
waiting: started("waiting"),
|
|
151
|
+
integrating: started("integrating"),
|
|
152
|
+
"waiting-review": started("waiting"),
|
|
153
|
+
paused: started("paused"),
|
|
154
|
+
merged: refuse(({ id }) => `${id} was merged; nothing to confirm`),
|
|
155
|
+
cancelled: refuse(({ id }) => `${id} was cancelled; nothing can be confirmed`),
|
|
156
|
+
},
|
|
157
|
+
cancel: {
|
|
158
|
+
proposed: ok,
|
|
159
|
+
ready: ok,
|
|
160
|
+
running: ok,
|
|
161
|
+
review: ok,
|
|
162
|
+
waiting: ok,
|
|
163
|
+
integrating: ok,
|
|
164
|
+
"waiting-review": ok,
|
|
165
|
+
paused: ok,
|
|
166
|
+
merged: refuse(({ id }) => `${id} was merged; nothing can be cancelled`),
|
|
167
|
+
cancelled: done(({ id }) => `${id} is already cancelled; nothing to do`),
|
|
168
|
+
},
|
|
169
|
+
pause: {
|
|
170
|
+
proposed: refuse(({ id }) => `${id} is proposed; dev:pause works once it is ready or running`),
|
|
171
|
+
ready: ok,
|
|
172
|
+
running: ok,
|
|
173
|
+
review: ok,
|
|
174
|
+
waiting: refuse(
|
|
175
|
+
({ id }) => `${id} is waiting for you; answer it, or dev:cancel it`,
|
|
176
|
+
),
|
|
177
|
+
integrating: refuse(
|
|
178
|
+
({ id }) => `${id} is integrating; dev:pause works once it is ready or running`,
|
|
179
|
+
),
|
|
180
|
+
"waiting-review": refuse(
|
|
181
|
+
({ id }) => `${id} is waiting for the review model to come back; dev:pause works once it is ready or running`,
|
|
182
|
+
),
|
|
183
|
+
paused: done(({ id }) => `${id} is already paused; nothing to do`),
|
|
184
|
+
merged: refuse(({ id }) => `${id} was merged; nothing can be paused`),
|
|
185
|
+
cancelled: refuse(({ id }) => `${id} was cancelled; nothing can be paused`),
|
|
186
|
+
},
|
|
187
|
+
resume: resumeLines(),
|
|
188
|
+
merge: personWait("merge", "merged", true),
|
|
189
|
+
reject: personWait("reject", "rejected", false),
|
|
190
|
+
rework: personWait("rework", "reworked", false),
|
|
191
|
+
reverify: personWait("reverify", "reverified", false),
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const planClosed = (id: string) => `${id} was cancelled; nothing can be changed`;
|
|
195
|
+
const planFinished = (id: string) => `${id} is finished; nothing can be changed`;
|
|
196
|
+
|
|
197
|
+
/** The lines of an action that sets one value of a plan. */
|
|
198
|
+
function setting(
|
|
199
|
+
what: string,
|
|
200
|
+
finishedRefuses: boolean,
|
|
201
|
+
): Record<PlanState, Line> {
|
|
202
|
+
const same = okSame(({ id, value }) => `${id} already has ${what} ${value}; nothing to do`);
|
|
203
|
+
return {
|
|
204
|
+
draft: same,
|
|
205
|
+
confirmed: same,
|
|
206
|
+
paused: same,
|
|
207
|
+
finished: finishedRefuses ? refuse(({ id }) => planFinished(id)) : same,
|
|
208
|
+
cancelled: refuse(({ id }) => planClosed(id)),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export const PLAN_LINES: Record<PlanActionName, Record<PlanState, Line>> = {
|
|
213
|
+
confirm: {
|
|
214
|
+
draft: ok,
|
|
215
|
+
confirmed: done(({ id }) => `${id} is already confirmed; nothing to do`),
|
|
216
|
+
paused: refuse(({ id }) => `${id} is paused; only a draft can be confirmed`),
|
|
217
|
+
finished: done(({ id }) => `${id} is already confirmed; nothing to do`),
|
|
218
|
+
cancelled: refuse(({ id }) => `${id} was cancelled; nothing can be confirmed`),
|
|
219
|
+
},
|
|
220
|
+
cancel: {
|
|
221
|
+
draft: ok,
|
|
222
|
+
confirmed: ok,
|
|
223
|
+
paused: ok,
|
|
224
|
+
finished: ok,
|
|
225
|
+
cancelled: done(({ id }) => `${id} is already cancelled; nothing to do`),
|
|
226
|
+
},
|
|
227
|
+
auto: setting("auto", false),
|
|
228
|
+
priority: setting("priority", false),
|
|
229
|
+
concurrency: setting("concurrency", true),
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/** The state a task row shows, with the integrate mark and the review-model wait told apart. */
|
|
233
|
+
export function taskStateOf(task: TaskData): TaskState {
|
|
234
|
+
if (isIntegrating(task)) return "integrating";
|
|
235
|
+
if (task.phase === "waiting" && inReviewWait(task)) return "waiting-review";
|
|
236
|
+
return task.phase;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* A task a person has to act on: proposed, or stopped and asking (not a review
|
|
241
|
+
* model's wait, and not an integrating task: the merge is already chosen).
|
|
242
|
+
*/
|
|
243
|
+
export function needsPerson(task: TaskData): boolean {
|
|
244
|
+
if (isIntegrating(task)) return false;
|
|
245
|
+
return task.phase === "proposed" || (task.phase === "waiting" && !inReviewWait(task));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** What a line says for a value that is set again, or `undefined`. */
|
|
249
|
+
export interface Said {
|
|
250
|
+
readonly kind: "done" | "refuse";
|
|
251
|
+
readonly text: string;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function sayLine(line: Line, facts: Facts, sameValue: boolean): Said | undefined {
|
|
255
|
+
if (line.kind === "ok") {
|
|
256
|
+
return sameValue && line.same !== undefined
|
|
257
|
+
? { kind: "done", text: line.same(facts) }
|
|
258
|
+
: undefined;
|
|
259
|
+
}
|
|
260
|
+
return { kind: line.kind, text: line.say(facts) };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function isWaitAction(action: TaskActionName): action is WaitAction {
|
|
264
|
+
return (
|
|
265
|
+
action === "merge" ||
|
|
266
|
+
action === "reject" ||
|
|
267
|
+
action === "rework" ||
|
|
268
|
+
action === "reverify"
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** What a wait action can do, and what it does, in the wait's own words. */
|
|
273
|
+
const WAIT_WORDS: Readonly<
|
|
274
|
+
Record<WaitAction, { readonly cannot: string; readonly can: string }>
|
|
275
|
+
> = {
|
|
276
|
+
merge: { cannot: "cannot be merged", can: "merged" },
|
|
277
|
+
reject: { cannot: "cannot be rejected", can: "rejected" },
|
|
278
|
+
rework: { cannot: "cannot be reworked", can: "reworked" },
|
|
279
|
+
reverify: { cannot: "cannot be verified again", can: "verified again" },
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/** A list as a person says it: `a`, `a or b`, `a, b or c`. */
|
|
283
|
+
function orList(words: readonly string[]): string {
|
|
284
|
+
if (words.length <= 1) return words[0] ?? "";
|
|
285
|
+
return `${words.slice(0, -1).join(", ")} or ${words[words.length - 1]}`;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** The line an action gets when the task's wait does not take it. */
|
|
289
|
+
function waitLine(
|
|
290
|
+
action: WaitAction,
|
|
291
|
+
task: TaskData,
|
|
292
|
+
id: string,
|
|
293
|
+
): Said | undefined {
|
|
294
|
+
const wait = waitKindOf(task);
|
|
295
|
+
if (wait === undefined) return undefined;
|
|
296
|
+
const choices = waitChoices(wait, flowHasReverify(task));
|
|
297
|
+
if (choices.includes(action)) return undefined;
|
|
298
|
+
const can = choices.map((choice) => WAIT_WORDS[choice].can);
|
|
299
|
+
return {
|
|
300
|
+
kind: "refuse",
|
|
301
|
+
text: `${id} ${WAIT_WORDS[action].cannot} now; it can be ${orList(can)}`,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** A task action's line for a task's state; undefined when it works. */
|
|
306
|
+
export function taskLine(
|
|
307
|
+
action: TaskActionName,
|
|
308
|
+
task: TaskData,
|
|
309
|
+
facts: Facts,
|
|
310
|
+
): Said | undefined {
|
|
311
|
+
if (isWaitAction(action)) {
|
|
312
|
+
const refused = waitLine(action, task, facts.id);
|
|
313
|
+
if (refused !== undefined) return refused;
|
|
314
|
+
}
|
|
315
|
+
return sayLine(TASK_LINES[action][taskStateOf(task)], facts, false);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** A draft with no task has nothing to confirm yet. */
|
|
319
|
+
export const EMPTY_DRAFT: Line = refuse(
|
|
320
|
+
({ id }) => `${id} is a draft with no tasks; dev:confirm works once it has a task`,
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
/** A plan action's line for a plan's state; undefined when it works. */
|
|
324
|
+
export function planLine(
|
|
325
|
+
action: PlanActionName,
|
|
326
|
+
plan: PlanData,
|
|
327
|
+
facts: Facts,
|
|
328
|
+
sameValue = false,
|
|
329
|
+
): Said | undefined {
|
|
330
|
+
const state = planState(plan);
|
|
331
|
+
const line =
|
|
332
|
+
action === "confirm" && state === "draft" && plan.taskCount === 0
|
|
333
|
+
? EMPTY_DRAFT
|
|
334
|
+
: PLAN_LINES[action][state];
|
|
335
|
+
return sayLine(line, facts, sameValue);
|
|
336
|
+
}
|