@operad-io/dev 0.0.1 → 0.1.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/src/service.ts CHANGED
@@ -10,11 +10,12 @@ import {
10
10
  writeFileSync,
11
11
  } from "node:fs";
12
12
  import { homedir } from "node:os";
13
- import { basename, isAbsolute, join, relative, resolve } from "node:path";
13
+ import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
14
14
  import { objectValueAfter, ViewError } from "@operad-io/engine";
15
15
  import { isBusy } from "@operad-io/engine/platform";
16
16
  import type { FlowDefinition, ObjectStore, PluginFact, StoredObject } from "@operad-io/engine";
17
17
  import { git } from "./git.ts";
18
+ import { withdrawIntegration } from "./integration-queue.ts";
18
19
  import { withRepositoryLock } from "./lock.ts";
19
20
  import {
20
21
  appendChange,
@@ -289,7 +290,7 @@ function noRepositoryHere(
289
290
  if (registered.length === 0) {
290
291
  return new DevError(
291
292
  "repo",
292
- `no registered repository contains ${cwd}, and none is registered; register one with \`operad repo add <path>\``,
293
+ `no registered repository contains ${cwd}, and none is registered; register one with \`operad dev repo add <path>\``,
293
294
  );
294
295
  }
295
296
  return new DevError(
@@ -308,7 +309,7 @@ function unknownRepository(
308
309
  if (registered.length === 0) {
309
310
  return new DevError(
310
311
  "repo",
311
- `no registered repository "${name}", and none is registered; register one with \`operad repo add <path>\``,
312
+ `no registered repository "${name}", and none is registered; register one with \`operad dev repo add <path>\``,
312
313
  );
313
314
  }
314
315
  return new DevError(
@@ -1902,14 +1903,45 @@ async function cancelRuns(
1902
1903
  }
1903
1904
  }
1904
1905
 
1906
+ interface CancelledTasks {
1907
+ readonly runs: readonly RunToCancel[];
1908
+ /** The tasks a batch had already pushed; they stay merged, not cancelled. */
1909
+ readonly merged: readonly string[];
1910
+ }
1911
+
1912
+ /** Where a cancel finds the integration queue: the home the action runs in. */
1913
+ function cancelHome(
1914
+ store: ObjectStore,
1915
+ options: CancelOptions,
1916
+ ): string | undefined {
1917
+ if (options.home !== undefined) return options.home;
1918
+ // Before this option, a cancel called the service directly: the default
1919
+ // store sits at `<home>/store`, so that home still finds the queue.
1920
+ return basename(store.root) === "store" ? dirname(store.root) : undefined;
1921
+ }
1922
+
1905
1923
  async function cancelTasks(
1906
1924
  store: ObjectStore,
1907
1925
  tasks: readonly StoredObject[],
1908
- ): Promise<readonly RunToCancel[]> {
1926
+ home: string | undefined,
1927
+ repositoryPath: string,
1928
+ ): Promise<CancelledTasks> {
1909
1929
  const runs: RunToCancel[] = [];
1930
+ const merged: string[] = [];
1910
1931
  for (const object of tasks) {
1911
1932
  const data = taskData(object.data);
1912
1933
  if (!isOpenTask(data)) continue;
1934
+ if (home !== undefined) {
1935
+ const withdrawal = await withdrawIntegration(
1936
+ home,
1937
+ { name: data.repository, path: repositoryPath },
1938
+ bareId(object.id),
1939
+ );
1940
+ if (withdrawal.kind === "merged") {
1941
+ merged.push(object.id);
1942
+ continue;
1943
+ }
1944
+ }
1913
1945
  if (data.run !== null) runs.push({ run: data.run, flow: data.flowGraph });
1914
1946
  await writeTask(
1915
1947
  store,
@@ -1924,33 +1956,47 @@ async function cancelTasks(
1924
1956
  "dev:task-cancel",
1925
1957
  );
1926
1958
  }
1927
- return runs;
1959
+ return { runs, merged };
1928
1960
  }
1929
1961
 
1930
1962
  export interface CancelOptions {
1931
1963
  readonly runs?: DevRunControl | undefined;
1964
+ /** The operad home, where the integration queue lives. */
1965
+ readonly home?: string | undefined;
1966
+ }
1967
+
1968
+ export interface CancelledTask {
1969
+ readonly task: StoredObject;
1970
+ readonly runs: readonly RunToCancel[];
1971
+ /** Set when a batch pushed the task first: it stays merged and is not cancelled. */
1972
+ readonly alreadyMerged?: string | undefined;
1932
1973
  }
1933
1974
 
1934
1975
  export async function cancelPlan(
1935
1976
  store: ObjectStore,
1936
1977
  id: string,
1937
1978
  options: CancelOptions = {},
1938
- ): Promise<{ readonly plan: StoredObject; readonly runs: readonly RunToCancel[] }> {
1979
+ ): Promise<{
1980
+ readonly plan: StoredObject;
1981
+ readonly runs: readonly RunToCancel[];
1982
+ readonly merged: readonly string[];
1983
+ }> {
1939
1984
  const initial = await getPlan(store, id);
1940
1985
  const initialData = planData(initial.data);
1986
+ const home = cancelHome(store, options);
1941
1987
  const cancelled = await withRepoLock(
1942
1988
  store,
1943
1989
  initialData.repository,
1944
- async () => {
1990
+ async (fresh) => {
1945
1991
  const plan = await getPlan(store, id);
1946
1992
  const data = planData(plan.data);
1947
- if (refusePlan("cancel", data, id)) return { plan, runs: [] };
1993
+ if (refusePlan("cancel", data, id)) return { plan, runs: [], merged: [] };
1948
1994
  const tasks = await tasksOfPlan(store, id);
1949
- const runs = await cancelTasks(store, tasks);
1995
+ const { runs, merged } = await cancelTasks(store, tasks, home, fresh.path);
1950
1996
  const next: PlanData = { ...data, status: "cancelled" };
1951
1997
  const written = await writePlan(store, id, next, "dev:plan-cancel");
1952
1998
  await refreshPlanCounts(store, written);
1953
- return { plan: written, runs };
1999
+ return { plan: written, runs, merged };
1954
2000
  },
1955
2001
  );
1956
2002
  await cancelRuns(options.runs, cancelled.runs);
@@ -1961,16 +2007,29 @@ export async function cancelTask(
1961
2007
  store: ObjectStore,
1962
2008
  id: string,
1963
2009
  options: CancelOptions = {},
1964
- ): Promise<{ readonly task: StoredObject; readonly runs: readonly RunToCancel[] }> {
2010
+ ): Promise<CancelledTask> {
1965
2011
  const initial = await getTask(store, id);
1966
2012
  const initialData = taskData(initial.data);
2013
+ const home = cancelHome(store, options);
1967
2014
  const cancelled = await withRepoLock(
1968
2015
  store,
1969
2016
  initialData.repository,
1970
- async () => {
2017
+ async (fresh) => {
1971
2018
  const task = await getTask(store, id);
1972
2019
  const data = taskData(task.data);
1973
- if (refuseTask("cancel", data, id)) return { task, runs: [] };
2020
+ if (refuseTask("cancel", data, id)) {
2021
+ return { task, runs: [], merged: data.phase === "merged" };
2022
+ }
2023
+ if (home !== undefined) {
2024
+ const withdrawal = await withdrawIntegration(
2025
+ home,
2026
+ { name: data.repository, path: fresh.path },
2027
+ bareId(id),
2028
+ );
2029
+ if (withdrawal.kind === "merged") {
2030
+ return { task, runs: [], merged: true };
2031
+ }
2032
+ }
1974
2033
  const runs: readonly RunToCancel[] =
1975
2034
  data.run === null ? [] : [{ run: data.run, flow: data.flowGraph }];
1976
2035
  const written = await writeTask(
@@ -1986,11 +2045,17 @@ export async function cancelTask(
1986
2045
  "dev:task-cancel",
1987
2046
  );
1988
2047
  await refreshPlanCountsOf(store, storeKey(data.repository, data.plan));
1989
- return { task: written, runs };
2048
+ return { task: written, runs, merged: false };
1990
2049
  },
1991
2050
  );
1992
2051
  await cancelRuns(options.runs, cancelled.runs);
1993
- return cancelled;
2052
+ return {
2053
+ task: cancelled.task,
2054
+ runs: cancelled.runs,
2055
+ ...(cancelled.merged
2056
+ ? { alreadyMerged: `${bareId(id)} was merged; nothing can be cancelled` }
2057
+ : {}),
2058
+ };
1994
2059
  }
1995
2060
 
1996
2061
  /**
package/src/settings.ts CHANGED
@@ -123,6 +123,8 @@ export interface WorkSettings {
123
123
  export interface GateSettings {
124
124
  readonly commands: readonly CheckCommand[];
125
125
  readonly commandTimeoutSecs: number;
126
+ /** How long a gate command may write nothing; unset turns the limit off. */
127
+ readonly stallSecs?: number;
126
128
  /** The most checks of this repository that run at once; unset means no limit of its own. */
127
129
  readonly places?: number;
128
130
  }
@@ -204,8 +206,11 @@ export interface ReviewerSettings {
204
206
  * reviewer's own for that review. Every later review uses the reviewer's.
205
207
  */
206
208
  readonly first?: ReviewerFirst | undefined;
207
- /** The step used when the primary reviewer is unavailable. */
208
- readonly fallback?: ReviewerFallback | undefined;
209
+ /**
210
+ * The step, or the steps in order, used when the primary reviewer is
211
+ * unavailable or does not write the report.
212
+ */
213
+ readonly fallback?: ReviewerFallback | readonly ReviewerFallback[] | undefined;
209
214
  readonly timeoutSecs: number;
210
215
  /** How long a review may print nothing; unset uses the harness default. */
211
216
  readonly stallSecs?: number | undefined;
@@ -610,7 +615,12 @@ function gateSettings(value: unknown, field: string): GateSettings {
610
615
  throw new DevError(field, "must be an object");
611
616
  }
612
617
  for (const key of Object.keys(value)) {
613
- if (key !== "commands" && key !== "commandTimeoutSecs" && key !== "places") {
618
+ if (
619
+ key !== "commands" &&
620
+ key !== "commandTimeoutSecs" &&
621
+ key !== "stallSecs" &&
622
+ key !== "places"
623
+ ) {
614
624
  throw new DevError(`${field}.${key}`, "is not a gate setting");
615
625
  }
616
626
  }
@@ -623,6 +633,9 @@ function gateSettings(value: unknown, field: string): GateSettings {
623
633
  value.commandTimeoutSecs === undefined
624
634
  ? DEFAULT_DEV_SETTINGS.gate.commandTimeoutSecs
625
635
  : positiveSeconds(value.commandTimeoutSecs, `${field}.commandTimeoutSecs`),
636
+ ...(value.stallSecs === undefined
637
+ ? {}
638
+ : { stallSecs: positiveSeconds(value.stallSecs, `${field}.stallSecs`) }),
626
639
  ...(value.places === undefined
627
640
  ? {}
628
641
  : { places: positiveCount(value.places, `${field}.places`) }),
@@ -718,6 +731,25 @@ function fixLevelOf(value: unknown, field: string): FixLevel {
718
731
  return value;
719
732
  }
720
733
 
734
+ /** The fallbacks of a reviewer in the order they are tried. */
735
+ export function reviewerFallbacks(
736
+ fallback: ReviewerFallback | readonly ReviewerFallback[] | undefined,
737
+ ): readonly ReviewerFallback[] {
738
+ if (fallback === undefined) return [];
739
+ return Array.isArray(fallback) ? fallback : [fallback as ReviewerFallback];
740
+ }
741
+
742
+ function reviewerFallbackSetting(
743
+ value: unknown,
744
+ field: string,
745
+ ): ReviewerFallback | readonly ReviewerFallback[] {
746
+ if (!Array.isArray(value)) return reviewerFallback(value, field);
747
+ if (value.length === 0) throw new DevError(field, "must not be an empty list");
748
+ return value.map((entry, index) =>
749
+ reviewerFallback(entry, `${field}[${index}]`),
750
+ );
751
+ }
752
+
721
753
  function reviewerFallback(value: unknown, field: string): ReviewerFallback {
722
754
  if (!isRecord(value)) {
723
755
  throw new DevError(field, "must be an object");
@@ -800,7 +832,7 @@ function reviewerSettings(value: unknown, field: string): ReviewerSettings {
800
832
  const fallback =
801
833
  value.fallback === undefined
802
834
  ? undefined
803
- : reviewerFallback(value.fallback, `${field}.fallback`);
835
+ : reviewerFallbackSetting(value.fallback, `${field}.fallback`);
804
836
  return {
805
837
  use:
806
838
  value.use === undefined