Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/settled-completions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@shivaedev/work-fleet": patch
---

### Fixed

- `@shivaedev/work-fleet`: Done and released records whose Board completion is already applied no longer wake on every scheduling write. Fleet stores the Board revision its completion last saw (migration `38_board_settled_revision`) and reconciles such a record again only when its brief or one of its published decisions changes.
- `@shivaedev/work-fleet`: Closing finished decisions for one work item reads only that item's published decisions instead of scanning every Board document.
1 change: 1 addition & 0 deletions packages/work-fleet/src/board/GatewayService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export interface BoardGatewayService {
text?: string,
) => Effect.Effect<string, BoardFailure>;
readonly sourceCreatedAt?: (sourcePath: string) => Effect.Effect<number | undefined, BoardFailure>;
readonly workRevisions?: () => Effect.Effect<(workId: string) => string | undefined, BoardFailure>;
}
46 changes: 27 additions & 19 deletions packages/work-fleet/src/board/closeFinishedDecisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { managedRequestReceipt } from "@shivaedev/work-board/server/attention/ma
import type { Snapshot as BoardSnapshot } from "@shivaedev/work-board/server/search/snapshot.ts";
import { closePublishedDecision, publishedDecision } from "#board/closePublishedDecision.ts";
import type { ClosedDecision } from "#board/schema.ts";
import { snapshotProblems } from "#board/workPolicy.ts";
import { type PublishedDecisionDocument, type WorkIndex, workIndex } from "#board/workIndex.ts";
import type { BoardFailure } from "#policy.ts";

// Each closure syncs its file and directory, so a sweep in sequence waits on every sync in turn.
Expand All @@ -19,16 +19,17 @@ export interface CloseFinishedOptions {
export function closeFinishedDecisions(realRoot: string, snapshot: Effect.Effect<BoardSnapshot, BoardFailure>) {
return Effect.fn("FleetBoard.closeFinishedDecisions")(function* (options: CloseFinishedOptions = {}) {
const current = yield* snapshot;
const questions = new Map(
current.documents.flatMap(({ parsed }) =>
parsed.fields.question && parsed.fields.id ? [[parsed.fields.id, parsed.fields.question.source] as const] : [],
),
);
const answered = new Set(
current.documents.flatMap(({ parsed }) => (parsed.fields.response?.type === "answer" ? [questions.get(parsed.fields.response.question)] : [])),
);
const candidates = current.documents.flatMap((document) => {
const candidate = finishedDecision(current, document, options, answered);
const index = workIndex(current);
const published =
options.workId === undefined
? current.documents.flatMap((document) => {
const decision = publishedDecision(document);
return decision === undefined ? [] : [{ decision, document }];
})
: index.decisions(options.workId);
const answered = options.workId === undefined ? answeredQuestions(current) : new Set<string | undefined>();
const candidates = published.flatMap(({ decision, document }) => {
const candidate = finishedDecision(current, index, decision, document, options, answered);
return candidate === undefined ? [] : [{ decision: { ...candidate, sourcePath: document.file } satisfies ClosedDecision, document }];
});
if (options.dryRun) {
Expand All @@ -44,19 +45,26 @@ export function closeFinishedDecisions(realRoot: string, snapshot: Effect.Effect
});
}

function answeredQuestions(snapshot: BoardSnapshot) {
const questions = new Map(
snapshot.documents.flatMap(({ parsed }) =>
parsed.fields.question && parsed.fields.id ? [[parsed.fields.id, parsed.fields.question.source] as const] : [],
),
);
return new Set(
snapshot.documents.flatMap(({ parsed }) => (parsed.fields.response?.type === "answer" ? [questions.get(parsed.fields.response.question)] : [])),
);
}

function finishedDecision(
snapshot: BoardSnapshot,
index: WorkIndex,
decision: PublishedDecisionDocument["decision"],
document: BoardDocument,
options: CloseFinishedOptions,
answered: ReadonlySet<string | undefined>,
) {
const decision = publishedDecision(document);
if (
decision === undefined
|| decision.request.state !== "open"
|| snapshotProblems(snapshot).has(document.file)
|| (options.workId !== undefined && decision.workId !== options.workId)
) {
if (decision.request.state !== "open" || index.problems.has(document.file)) {
return;
}
const receipt = managedRequestReceipt(document, decision.request, snapshot.model);
Expand All @@ -73,7 +81,7 @@ function finishedDecision(
const matches = snapshot.model.ids.get(decision.workId);
const work = matches?.length === 1 ? matches[0] : undefined;
const status = work?.parsed.fields.status;
if (work === undefined || snapshotProblems(snapshot).has(work.file) || !["done", "deferred", "dropped"].includes(status ?? "")) {
if (work === undefined || index.problems.has(work.file) || !["done", "deferred", "dropped"].includes(status ?? "")) {
return;
}
return { reason: `Closed because Board work ${decision.workId} is ${status}.`, workId: decision.workId };
Expand Down
23 changes: 21 additions & 2 deletions packages/work-fleet/src/board/markdownGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { workMetadata } from "#board/readPass.ts";
import { readWork } from "#board/readWork.ts";
import { recordDecisionResponse } from "#board/recordDecisionResponse.ts";
import { upgradeDecisionClosure } from "#board/upgradeDecisionClosure.ts";
import { workIndex } from "#board/workIndex.ts";
import { metadataProblem } from "#board/workPolicy.ts";
import { workRevision } from "#board/workRevision.ts";
import { boardSnapshot } from "#board/workspace.ts";
Expand Down Expand Up @@ -68,9 +69,26 @@ export const MarkdownBoard = defineService({
Effect.mapError((error) => new BoardFailure({ message: error.message })),
);
}
return { decisionSnapshot, documents, fs, get, path, publish, realRoot, root, snapshot, sourceCreatedAt, sources, workspace };
function workRevisions() {
return sources.pipe(Effect.map((current) => workIndex(current).revision));
}
return { decisionSnapshot, documents, fs, get, path, publish, realRoot, root, snapshot, sourceCreatedAt, sources, workRevisions, workspace };
}),
methods: ({ root, realRoot, decisionSnapshot, documents, get, publish, path, snapshot, sources, sourceCreatedAt, workspace, fs }) => ({
methods: ({
root,
realRoot,
decisionSnapshot,
documents,
get,
publish,
path,
snapshot,
sources,
sourceCreatedAt,
workRevisions,
workspace,
fs,
}) => ({
...BoardGateway.of({
acknowledgeDecision: (input) =>
acknowledgeDecision(
Expand Down Expand Up @@ -123,6 +141,7 @@ export const MarkdownBoard = defineService({
report: publishReport(root, realRoot, documents),
respond: recordDecisionResponse(root, realRoot, snapshot),
sourceCreatedAt,
workRevisions,
}),
}),
requires: [BoardRoot, FileSystem.FileSystem, Path.Path, Workspace, BoardIndex],
Expand Down
66 changes: 66 additions & 0 deletions packages/work-fleet/src/board/workIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { MetadataDocument as BoardDocument } from "@shivaedev/work-board/integration/metadata.ts";
import { responses } from "@shivaedev/work-board/server/integration/responses.ts";
import { documentSource } from "@shivaedev/work-board/server/metadata/model.ts";
import type { Snapshot as BoardSnapshot } from "@shivaedev/work-board/server/search/snapshot.ts";
import { publishedDecision } from "#board/closePublishedDecision.ts";
import { snapshotProblems } from "#board/workPolicy.ts";
import type { BoardFailure } from "#policy.ts";

const { revisionOf } = responses;

export interface PublishedDecisionDocument {
readonly decision: NonNullable<ReturnType<typeof publishedDecision>>;
readonly document: BoardDocument;
}

export interface WorkIndex {
readonly decisions: (workId: string) => readonly PublishedDecisionDocument[];
readonly problems: ReadonlyMap<string, BoardFailure>;
readonly revision: (workId: string) => string | undefined;
}

// Snapshots never change, and the scheduler and decision closure consult them per record, so each snapshot is indexed once.
const indexes = new WeakMap<BoardSnapshot, WorkIndex>();

export function workIndex(snapshot: BoardSnapshot): WorkIndex {
const known = indexes.get(snapshot);
if (known !== undefined) {
return known;
}
const decisions = new Map<string, PublishedDecisionDocument[]>();
for (const document of snapshot.documents) {
const decision = publishedDecision(document);
if (decision !== undefined) {
decisions.set(decision.workId, [...(decisions.get(decision.workId) ?? []), { decision, document }]);
}
}
const revisions = new Map<string, string | undefined>();
const index: WorkIndex = {
decisions: (workId) => decisions.get(workId) ?? [],
problems: snapshotProblems(snapshot),
revision: (workId) => {
if (!revisions.has(workId)) {
revisions.set(workId, workRevision(snapshot, workId, decisions.get(workId) ?? []));
}
return revisions.get(workId);
},
};
indexes.set(snapshot, index);
return index;
}

function workRevision(snapshot: BoardSnapshot, workId: string, decisions: readonly PublishedDecisionDocument[]) {
const matches = snapshot.model.ids.get(workId);
const brief = matches?.length === 1 ? matches[0] : undefined;
if (brief === undefined && decisions.length === 0) {
return;
}
return revisionOf(
JSON.stringify([
brief === undefined ? null : [brief.file, documentSource(brief)],
...decisions
.map(({ document }) => [document.file, documentSource(document)])
.sort(([left], [right]) => String(left).localeCompare(String(right))),
]),
);
}
44 changes: 37 additions & 7 deletions packages/work-fleet/src/engine/completeBoardWork.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Effect } from "effect";
import { responses } from "@shivaedev/work-board/server/integration/responses.ts";
import { briefChanged } from "#engine/boardBrief.ts";
import { refuseDeliveredId } from "#engine/deliveredId.ts";
import type { EngineState } from "#engine/makeState.ts";
import type { FleetRecord } from "#model.ts";
import type { Versioned } from "#storage/model.ts";

const { revisionOf } = responses;

export function boardCompletionAction(record: FleetRecord) {
if (record.step === "done" && record.report !== undefined) {
return "Done; report recorded.";
Expand All @@ -22,18 +25,23 @@ export function boardCompletionAction(record: FleetRecord) {
return number === undefined ? undefined : `Done; PR #${number} merged.`;
}

export function completionSettlement(action: string, revision: string | undefined) {
return revisionOf(JSON.stringify([action, revision ?? null]));
}

export function completeBoardWork(state: EngineState) {
const reported = state.completionWarnings;
return Effect.fn("Fleet.completeBoardWork")(function* (stored: Versioned<FleetRecord>) {
const action = boardCompletionAction(stored.value);
const complete = state.board.complete;
const sourcePath = stored.value.boardSourcePath;
if (
const applicable =
action !== undefined
&& sourcePath !== undefined
&& complete !== undefined
&& stored.value.attempts.some((attempt) => attempt.role !== "reviewer" && attempt.receipt !== undefined)
) {
&& stored.value.attempts.some((attempt) => attempt.role !== "reviewer" && attempt.receipt !== undefined);
let failed = false;
if (applicable) {
let source = sourcePath;
yield* state.board.get(stored.value.workId).pipe(
Effect.flatMap((work) => {
Expand All @@ -44,6 +52,7 @@ export function completeBoardWork(state: EngineState) {
}),
Effect.tap(() => Effect.sync(() => reported.delete(stored.value.workId))),
Effect.catch((error) => {
failed = true;
const warning = JSON.stringify([source, error.message]);
if (reported.get(stored.value.workId) === warning) {
return Effect.void;
Expand All @@ -54,10 +63,31 @@ export function completeBoardWork(state: EngineState) {
);
}
if (state.board.closeFinishedDecisions !== undefined) {
yield* state.board
.closeFinishedDecisions({ workId: stored.value.workId })
.pipe(Effect.catchTag("BoardFailure", (error) => Effect.logWarning(error.message)));
yield* state.board.closeFinishedDecisions({ workId: stored.value.workId }).pipe(
Effect.catchTag("BoardFailure", (error) => {
failed = true;
return Effect.logWarning(error.message);
}),
);
}
return stored;
return applicable && !failed ? yield* settleCompletion(state, stored, action) : stored;
});
}

// Marking the Board revision that completion and decision closure last saw lets the scheduler skip this record until that revision moves.
const settleCompletion = Effect.fn("Fleet.settleCompletion")(function* (state: EngineState, stored: Versioned<FleetRecord>, action: string) {
if (state.board.workRevisions === undefined) {
return stored;
}
const revision = yield* state.board.workRevisions().pipe(Effect.option);
if (revision._tag === "None") {
return stored;
}
const settled = completionSettlement(action, revision.value(stored.value.workId));
if (stored.value.boardSettledRevision === settled) {
return stored;
}
return yield* state
.save(stored, { ...stored.value, boardSettledRevision: settled })
.pipe(Effect.catchTag("StorageConflict", () => Effect.succeed(stored)));
});
8 changes: 7 additions & 1 deletion packages/work-fleet/src/engine/runScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ export const startScheduler = Effect.fn("Fleet.startScheduler")(function* <R = n
}),
key: ({ value }) => value.workId,
keys,
read: fleet.list().pipe(Effect.map((rows) => activeRecords(rows).filter(({ value }) => !openSweep(value)))),
read: fleet
.list()
.pipe(
Effect.flatMap((rows) =>
fleet.settledCompletions(rows).pipe(Effect.map((settled) => activeRecords(rows, settled).filter(({ value }) => !openSweep(value)))),
),
),
});
const admission = yield* reconcile({
act: () =>
Expand Down
4 changes: 2 additions & 2 deletions packages/work-fleet/src/engine/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ export const dispatchRecords = Effect.fn("Fleet.dispatchRecords")(function* (rec
return dispatched;
});

export function activeRecords(records: readonly Versioned<FleetRecord>[]) {
export function activeRecords(records: readonly Versioned<FleetRecord>[], settled: ReadonlySet<string> = new Set()) {
return records
.filter(
({ value }) =>
value.pendingContinuation !== undefined
|| !["done", "released", "queued"].includes(value.step)
|| needsReleasedDelivery(value)
|| needsReleasedSettlement(value)
|| boardCompletionAction(value) !== undefined
|| (boardCompletionAction(value) !== undefined && !settled.has(value.workId))
|| pendingAcknowledgements(value).length > 0
|| pendingSessionCleanup(value).length > 0,
)
Expand Down
25 changes: 25 additions & 0 deletions packages/work-fleet/src/engine/settledCompletions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Effect } from "effect";
import { inBoardReadPass } from "#board/readPass.ts";
import { boardCompletionAction, completionSettlement } from "#engine/completeBoardWork.ts";
import type { FleetRecord } from "#model.ts";
import { BoardGateway } from "#policy.ts";
import type { Versioned } from "#storage/model.ts";

export const settledCompletions = Effect.fn("Fleet.settledCompletions")(function* (records: readonly Versioned<FleetRecord>[]) {
const revisions = (yield* BoardGateway).workRevisions;
const settled = new Set<string>();
if (revisions === undefined) {
return settled;
}
const revision = yield* inBoardReadPass(revisions()).pipe(Effect.orElseSucceed(() => undefined));
if (revision === undefined) {
return settled;
}
for (const { value } of records) {
const action = boardCompletionAction(value);
if (action !== undefined && value.boardSettledRevision === completionSettlement(action, revision(value.workId))) {
settled.add(value.workId);
}
}
return settled;
});
2 changes: 2 additions & 0 deletions packages/work-fleet/src/fleet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { releaseClosedCommand } from "#engine/releaseClosedCommand.ts";
import { FleetRepository } from "#engine/repository.ts";
import { resolveCommand } from "#engine/resolveCommand.ts";
import { cleanupSessions } from "#engine/sessionCleanup.ts";
import { settledCompletions } from "#engine/settledCompletions.ts";
import { stopCommand } from "#engine/stopCommand.ts";
import { BoardGateway, FleetIntegrations, FleetPolicy } from "#policy.ts";
import type { Preparation } from "#preparation/schema.ts";
Expand Down Expand Up @@ -66,6 +67,7 @@ export const Fleet = defineService({
),
resolve: (workId: string, decisionId: string, responseId: string) =>
resolveCommand(state)(workId, decisionId, responseId).pipe(Effect.flatMap(cleanupSessions(state))),
settledCompletions,
stop: stopCommand(state),
}),
requires: [FleetRepository, BoardGateway, FleetPolicy, FleetIntegrations, SessionService],
Expand Down
Loading
Loading