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
27 changes: 27 additions & 0 deletions packages/cli/src/backend/MapStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ export interface MapHandle {
export class MapStore extends Context.Service<
MapStore,
{
/**
* Every map, in no particular order. Archived maps are omitted unless
* `includeArchived` is set, so the default listing is the live set.
*/
readonly listMaps: (
project: ProjectHandle,
options?: { readonly includeArchived?: boolean },
) => Effect.Effect<CollectionRead<MapMetadata>, WayfulError>;
readonly createMap: (
project: ProjectHandle,
Expand All @@ -46,13 +51,35 @@ export class MapStore extends Context.Service<
readonly start: string;
readonly goal: string;
readonly goalBody: string;
/** Narrows the map's allowed step types; absent permits every project type. */
readonly allowedStepTypes?: readonly string[];
},
) => Effect.Effect<void, WayfulError>;
readonly openMap: (
project: ProjectHandle,
name: string,
) => Effect.Effect<MapHandle, WayfulError | MapMetadataError>;

/**
* Archives a live map, hiding it from the default listing while keeping
* every step and goal intact. Fails if the map is absent or already
* archived.
*/
readonly archiveMap: (project: ProjectHandle, name: string) => Effect.Effect<void, WayfulError>;
/** Restores an archived map with its steps and goals intact. Fails if it is not archived. */
readonly unarchiveMap: (
project: ProjectHandle,
name: string,
) => Effect.Effect<void, WayfulError>;
/**
* Sets or clears a map's allowed-step-types restriction. `undefined`
* clears it, permitting every project type; a list narrows it.
*/
readonly setAllowedStepTypes: (
map: MapHandle,
allowed: readonly string[] | undefined,
) => Effect.Effect<void, WayfulError>;

readonly listSteps: (map: MapHandle) => Effect.Effect<CollectionRead<StepRecord>, WayfulError>;
readonly createStep: (
map: MapHandle,
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/backend/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ export const RoutedMapStore = Layer.effect(
const storeFor = (backend: ProjectBackend) => (backend === "github" ? github : local);

return MapStore.of({
listMaps: (project) => storeFor(project.backend).listMaps(project),
listMaps: (project, options) => storeFor(project.backend).listMaps(project, options),
createMap: (project, options) => storeFor(project.backend).createMap(project, options),
openMap: (project, name) => storeFor(project.backend).openMap(project, name),
archiveMap: (project, name) => storeFor(project.backend).archiveMap(project, name),
unarchiveMap: (project, name) => storeFor(project.backend).unarchiveMap(project, name),
setAllowedStepTypes: (map, allowed) =>
storeFor(map.project.backend).setAllowedStepTypes(map, allowed),
listSteps: (map) => storeFor(map.project.backend).listSteps(map),
createStep: (map, step) => storeFor(map.project.backend).createStep(map, step),
saveStep: (map, step) => storeFor(map.project.backend).saveStep(map, step),
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/backend/filesystem/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export function decodeMapMetadata(data: unknown, expectedName: string): MapMetad
"start",
"step_id_counter",
"allowed_step_types",
"archived_at",
"created_at",
"updated_at",
]);
Expand All @@ -106,11 +107,14 @@ export function decodeMapMetadata(data: unknown, expectedName: string): MapMetad
}
const createdAt = timestamp(record.created_at, "map created_at");
const updatedAt = timestamp(record.updated_at, "map updated_at");
const archivedAt =
record.archived_at === undefined ? undefined : timestamp(record.archived_at, "map archived_at");
return {
format_version: CURRENT_FORMAT_VERSION,
name: expectedName,
start: record.start as string,
allowed_step_types: allowedStepTypes,
archived: archivedAt !== undefined,
created_at: createdAt,
updated_at: updatedAt,
};
Expand Down
68 changes: 66 additions & 2 deletions packages/cli/src/backend/filesystem/layer/ops/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,42 @@ export function makeMapOps(fs: FileSystem.FileSystem, path: Path.Path) {
return { project, name, metadata };
});

/**
* The raw `map.toml` document behind a map, so a rewrite preserves fields
* this layer does not model (`step_id_counter`). Decoding is left to the
* caller; this only guarantees the file exists and parses as a TOML table.
*/
const readMapDocument = (
project: ProjectHandle,
name: string,
): Effect.Effect<
{ readonly file: string; readonly data: Record<string, unknown> },
WayfulError
> =>
Effect.gen(function* () {
yield* liftSync(() => identifier(name, "map name"));
const file = mapFile(path, mapDir(path, project.root, name));
const exists = yield* fs.exists(file).pipe(Effect.mapError(accessError));
if (!exists) return yield* fail(`map '${name}' does not exist.`);
const text = yield* readTextFile(fs, file, `cannot read ${file}.`);
const data = yield* liftSync(() => parseToml(text, file));
if (!data || typeof data !== "object" || Array.isArray(data))
return yield* fail(`malformed map metadata in ${file}.`);
return { file, data: data as Record<string, unknown> };
});

/** Rewrites a map's metadata document, advancing `updated_at` from the injected clock. */
const writeMapDocument = (
file: string,
data: Record<string, unknown>,
): Effect.Effect<void, WayfulError> =>
Effect.gen(function* () {
const now = yield* nowISO;
yield* writeAtomic(fs, file, stringifyToml({ ...data, updated_at: now }));
});

return {
listMaps: (project: ProjectHandle) =>
listMaps: (project: ProjectHandle, options: { readonly includeArchived?: boolean } = {}) =>
Effect.gen(function* () {
const dir = mapsDir(path, project.root);
const exists = yield* fs.exists(dir).pipe(Effect.mapError(accessError));
Expand All @@ -58,8 +92,9 @@ export function makeMapOps(fs: FileSystem.FileSystem, path: Path.Path) {
(name) => `${name}/map.toml`,
(name) => openMapHandle(project, name),
);
const records = result.records.map((handle) => handle.metadata);
return {
records: result.records.map((handle) => handle.metadata),
records: options.includeArchived ? records : records.filter((map) => !map.archived),
errors: result.errors,
};
}),
Expand All @@ -71,11 +106,13 @@ export function makeMapOps(fs: FileSystem.FileSystem, path: Path.Path) {
start,
goal,
goalBody,
allowedStepTypes,
}: {
readonly name: string;
readonly start: string;
readonly goal: string;
readonly goalBody: string;
readonly allowedStepTypes?: readonly string[];
},
) =>
Effect.gen(function* () {
Expand Down Expand Up @@ -105,6 +142,7 @@ export function makeMapOps(fs: FileSystem.FileSystem, path: Path.Path) {
name,
start: trimmedStart,
step_id_counter: 1,
...(allowedStepTypes !== undefined ? { allowed_step_types: allowedStepTypes } : {}),
created_at: now,
updated_at: now,
}),
Expand All @@ -128,5 +166,31 @@ export function makeMapOps(fs: FileSystem.FileSystem, path: Path.Path) {
}),

openMap: (project: ProjectHandle, name: string) => openMapHandle(project, name),

archiveMap: (project: ProjectHandle, name: string) =>
Effect.gen(function* () {
const { file, data } = yield* readMapDocument(project, name);
if (data.archived_at !== undefined)
return yield* fail(`map '${name}' is already archived.`);
const now = yield* nowISO;
yield* writeMapDocument(file, { ...data, archived_at: now });
}),

unarchiveMap: (project: ProjectHandle, name: string) =>
Effect.gen(function* () {
const { file, data } = yield* readMapDocument(project, name);
if (data.archived_at === undefined) return yield* fail(`map '${name}' is not archived.`);
const { archived_at: _archived, ...rest } = data;
yield* writeMapDocument(file, rest);
}),

setAllowedStepTypes: (map: MapHandle, allowed: readonly string[] | undefined) =>
Effect.gen(function* () {
const { file, data } = yield* readMapDocument(map.project, map.name);
const next = { ...data };
if (allowed === undefined) delete next.allowed_step_types;
else next.allowed_step_types = allowed;
yield* writeMapDocument(file, next);
}),
};
}
37 changes: 17 additions & 20 deletions packages/cli/src/backend/github/github-backend.live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

import {
addSubIssue,
createIssue,
getIssue,
listSubIssues,
updateIssue,
} from "@backend/github/api";
import { addSubIssue, createIssue, getIssue, listSubIssues } from "@backend/github/api";
import { SUB_ISSUE_CAP, subIssueCapError } from "@backend/github/subIssues";
import { MapStore } from "@backend/MapStore";
import {
Expand Down Expand Up @@ -316,29 +310,32 @@ suite(`live GitHub backend suite against ${process.env[LIVE_REPO_ENV] ?? "a scra
}),
);

// Archive and reopen. Closing the map issue is the archive: it disappears
// from `listMaps`/`openMap` but its sub-issues persist, so reopening
// restores the map intact. A fresh run gives fresh, unmemoized reads.
// Archive and reopen through the backend's own operations. Closing the map
// issue is the archive: it disappears from `listMaps` but its sub-issues
// persist, so reopening restores the map intact. A fresh run gives fresh,
// unmemoized reads.
await runLive(
Effect.gen(function* () {
const store = yield* MapStore;
yield* updateIssue(target.ref, target.token, created.mapNumber, {
state: "closed",
state_reason: "completed",
});
yield* store.archiveMap(target.project, mapName);
yield* poll(
`map '${mapName}' to be archived`,
store.listMaps(target.project),
(read) => !read.records.some((map) => map.name === mapName),
);
const absent = yield* Effect.flip(store.openMap(target.project, mapName));
expect(absent.message).toContain("does not exist");
const archived = yield* poll(
`map '${mapName}' to read back archived`,
store.openMap(target.project, mapName),
(map) => map.metadata.archived,
);
expect(archived.metadata.archived).toBe(true);

yield* updateIssue(target.ref, target.token, created.mapNumber, { state: "open" });
const reopened = yield* poll(
`map '${mapName}' to reappear`,
store.openMap(target.project, mapName),
() => true,
`map '${mapName}' to be restored`,
store
.unarchiveMap(target.project, mapName)
.pipe(Effect.andThen(store.openMap(target.project, mapName))),
(map) => !map.metadata.archived,
);
expect(reopened.number).toBe(created.mapNumber);
yield* poll(
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/backend/github/goalStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function metadata(name: string): MapMetadata {
name,
start: "here",
allowed_step_types: undefined,
archived: false,
created_at: ISSUE_TIME,
updated_at: ISSUE_TIME,
};
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/backend/github/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function decodeMapIssue(issue: GithubIssue): MapMetadata {
name,
start: nonEmpty(issue.title, "map start"),
allowed_step_types: decodeAllowedStepTypes(data.allowed_step_types),
archived: issue.state === "closed",
created_at: timestamp(issue.created_at, `map #${issue.number} created_at`),
updated_at: timestamp(issue.updated_at, `map #${issue.number} updated_at`),
};
Expand Down
Loading
Loading