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
117 changes: 112 additions & 5 deletions packages/cli/src/repl/file-recorder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Err, Ok } from "effection";
import { createContext, Err, Ok, scoped, useScope } from "effection";
import type { Operation, Result } from "effection";
import {
FilesInvariantError,
Expand All @@ -9,13 +9,28 @@ import {
parseFileWriteSuccess,
} from "@executablemd/runtime";
import type {
FileCopyInput,
FilesHandler,
FilesOperation,
FilesPhase,
FilePathInput,
FileWriteSuccess,
} from "@executablemd/runtime";
import { registerFileRecorder, assembleFileCall } from "@executablemd/runtime/files-host";
import {
registerFileRecorder,
assembleFileCall,
withHostCopyRoute,
} from "@executablemd/runtime/files-host";
import {
checkCopyPublication,
copyReplayContext,
recordedLocalOperation,
retainedCopyAhead,
ReplLocalRecordError,
retainedSelectionAhead,
failCopy,
} from "./repositories/deno/local.ts";
import type { LocalOperation } from "./repositories/deno/local.ts";
import type { Around } from "@effectionx/context-api";
import type { AuthoredFileOperation } from "@executablemd/core/host";
import type {
Expand All @@ -34,7 +49,85 @@ export interface EntryFileCalls {
read(authored: AuthoredFileOperation, input: FilePathInput): Operation<Result<string>>;
ensure(authored: AuthoredFileOperation, input: FilePathInput): Operation<Result<void>>;
}
function recordingBody(route: FileRecordingRoute) {
interface CopyRouting {
guard(): Operation<void>;
restoreSelection?(): Operation<unknown>;
route(source: string, destination: string): Operation<Operation<void> | undefined>;
}

function copyRecorder(routing: CopyRouting) {
const nextOrdinal = new Map<string, number>();
return function* (
input: FileCopyInput,
perform: () => Operation<Result<void>>,
): Operation<Result<void>> {
const context = yield* copyReplayContext();
const ordinal = nextOrdinal.get(context.coroutineId) ?? 0;
nextOrdinal.set(context.coroutineId, ordinal + 1);
const identity: Pick<LocalOperation, "operation" | "ordinal" | "request"> = {
operation: "file-copy",
ordinal,
request: { cwd: input.cwd, source: input.source, path: input.destination },
};
yield* routing.guard();
if (routing.restoreSelection !== undefined && (yield* retainedSelectionAhead())) {
yield* routing.restoreSelection();
}
if (yield* retainedCopyAhead(ordinal)) {
const result = yield* recordedLocalOperation({
...identity,
*perform() {
throw new ReplLocalRecordError("Cannot perform a retained file-copy occurrence live.");
},
});
return result.ok ? Ok(undefined) : result;
}

let covered = false;
let result: Result<ReplLocalValue>;
try {
result = yield* scoped(() =>
withHostCopyRoute(
function* (source, destination) {
try {
yield* copyReplayContext();
const hold = yield* routing.route(source, destination);
if (hold !== undefined) {
yield* checkCopyPublication();
yield* hold;
covered = true;
}
yield* copyReplayContext();
} catch (error) {
// A delegating layer may recover before the composed call settles.
throw yield* failCopy(error);
}
},
(function* () {
return yield* unitOutcome(perform(), { operation: "copy", phase: "access" });
})(),
),
);
} catch (error) {
throw yield* failCopy(error);
}
yield* copyReplayContext();
if (covered) {
result = yield* recordedLocalOperation({
...identity,
perform: function* () {
return result;
},
});
}
return result.ok ? Ok(undefined) : result;
};
}

function recordingBody(
route: FileRecordingRoute,
copy: ReturnType<typeof copyRecorder> | undefined,
) {
return {
*read(
input: FilePathInput,
Expand Down Expand Up @@ -71,6 +164,11 @@ function recordingBody(route: FileRecordingRoute) {
const result = yield* route("file-write", { ...input }, () => writeOutcome(next(input)));
return result.ok ? Ok(expectWritten(result.value)) : result;
},
...(copy === undefined
? {}
: {
copyFile: ([input], next) => copy(input, () => next(input)),
}),
*deleteFile([input], next) {
const result = yield* route("file-delete", { ...input }, () =>
unitOutcome(next(input), { operation: "delete", phase: "access" }),
Expand All @@ -81,8 +179,17 @@ function recordingBody(route: FileRecordingRoute) {
},
};
}
export function* useEntryFileRecorder(route: FileRecordingRoute): Operation<EntryFileCalls> {
const body = recordingBody(route);
const entryCopyInstalled = createContext<boolean>("executablemd.repl.entry-copy-installed");

export function* useEntryFileRecorder(
route: FileRecordingRoute,
copy: CopyRouting,
): Operation<EntryFileCalls> {
// Repository initialization can precede mount fallback in the same execution.
const scope = yield* useScope();
const alreadyInstalled = scope.hasOwn(entryCopyInstalled);
scope.set(entryCopyInstalled, true);
const body = recordingBody(route, alreadyInstalled ? undefined : copyRecorder(copy));
const handle = yield* registerFileRecorder(body.boundary());
const read = assembleFileCall(handle, "readTextFile");
const ensure = assembleFileCall(handle, "ensureDirectory");
Expand Down
19 changes: 12 additions & 7 deletions packages/cli/src/repl/repositories/admission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export function* admitReplRepositoryHistory(retained: readonly DurableEvent[]):
// checkout it read, and nothing outside this snapshot may authorize one: the
// route has to be a placement a preceding record in the same history made.
const placements: ReplPlacement[] = [];
const lastCopy = new Map<string, number>();
for (const event of retained) {
if (event.type !== "yield") {
continue;
Expand Down Expand Up @@ -162,7 +163,13 @@ export function* admitReplRepositoryHistory(retained: readonly DurableEvent[]):
yield* admitObservation(payload, event.description.name, placements);
continue;
}
yield* admitLocal(payload, event.description.name);
const ordinal = yield* admitLocal(payload, event.description.name);
if (ordinal !== undefined) {
if (ordinal <= (lastCopy.get(event.coroutineId) ?? -1)) {
throw malformed(REPL_REPOSITORY_LOCAL);
}
lastCopy.set(event.coroutineId, ordinal);
}
}
}

Expand Down Expand Up @@ -244,21 +251,19 @@ function* admitObservation(
}
}

function* admitLocal(payload: unknown, name: string): Operation<void> {
function* admitLocal(payload: unknown, name: string): Operation<number | undefined> {
const record = parseReplLocalRecord(payload);
if (record === undefined) {
throw malformed(REPL_REPOSITORY_LOCAL);
}
const fingerprint = yield* replLocalFingerprint({
operation: record.operation,
request: record.request,
});
const fingerprint = yield* replLocalFingerprint(record);
if (record.fingerprint !== fingerprint) {
throw changed(REPL_REPOSITORY_LOCAL);
}
if (name !== replLocalRecordName({ operation: record.operation, fingerprint })) {
if (name !== replLocalRecordName(record)) {
throw misnamed(REPL_REPOSITORY_LOCAL);
}
return record.ordinal;
}

/**
Expand Down
62 changes: 58 additions & 4 deletions packages/cli/src/repl/repositories/deno/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { createMountPolicy } from "../../mount-files.ts";
*/

import type { Operation, Result } from "effection";
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
import { realpath } from "node:fs/promises";
import { Err, until } from "effection";
import { filesFailure, Permissions } from "@executablemd/runtime";
Expand Down Expand Up @@ -316,7 +316,51 @@ function* canonical(directory: string): Operation<string> {
*/
function inside(checkoutRoot: string, target: string): boolean {
const at = relative(checkoutRoot, target);
return at !== "" && !at.startsWith("..") && !isAbsolute(at);
return at !== "" && at !== ".." && !at.startsWith(".." + sep) && !isAbsolute(at);
}

function* copyRoute(
recorder: ReplLocalRecorder,
source: string,
destination: string,
roots: readonly string[],
): Operation<Operation<void> | undefined> {
const placements = recorder.placements();
const read = covering(placements, source);
const write = covering(placements, destination);
const checkout = write === undefined ? yield* recorder.selection() : undefined;
const hold =
write?.commonDirectory ??
(checkout !== undefined && inside(checkout.checkoutRoot, destination)
? checkout.commonDirectory
: undefined);
let owned = false;
if (hold === undefined) {
for (const directory of roots) {
const root = yield* canonical(directory);
if (root === destination || inside(root, destination)) {
owned = true;
break;
}
}
}
if (hold === undefined && !owned) {
if (read !== undefined) {
yield* read.own();
}
return undefined;
}
return (function* () {
if (read !== undefined && read !== write) {
yield* read.own();
}
if (write !== undefined) {
yield* write.own();
}
if (hold !== undefined) {
yield* recorder.hold(hold);
}
})();
}

export function* useReplFileRecorder(
Expand All @@ -327,8 +371,18 @@ export function* useReplFileRecorder(
access?: HostFilesAccess,
): Operation<MountFileEntry> {
const policy = yield* createMountPolicy(mounts, directory);
const files = yield* useEntryFileRecorder((operation, request, perform, authored) =>
routed(recorder, operation, request, perform, workspace, authored, policy, access),
const roots = [
...(workspace === undefined ? [] : [workspace]),
...(access === undefined ? [] : [access.project, access.assets, access.components]),
];
const files = yield* useEntryFileRecorder(
(operation, request, perform, authored) =>
routed(recorder, operation, request, perform, workspace, authored, policy, access),
{
guard: () => policy.guard(),
restoreSelection: () => recorder.selection(),
route: (source, destination) => copyRoute(recorder, source, destination, roots),
},
);
return { files, policy };
}
59 changes: 51 additions & 8 deletions packages/cli/src/repl/repositories/deno/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type {
import { StaleInputError } from "@executablemd/durable-streams";
import { asFilesFatal, FilesInvariantError } from "@executablemd/runtime";
import {
copyOrdinalFromName,
parseReplLocalRecord,
parseReplSelectionRecord,
replLocalFingerprint,
Expand Down Expand Up @@ -77,6 +78,7 @@ export interface LocalOperation {
readonly operation: ReplLocalOperation;
/** What the document asked for. Names the record; never live state. */
readonly request: ReplLocalRequest;
readonly ordinal?: number;
/** The ordinary composed call, run only when no result is saved. */
perform(): Operation<Result<ReplLocalValue>>;
}
Expand Down Expand Up @@ -140,6 +142,47 @@ export function retainedResultAhead(name: string): Operation<boolean> {
return retainedRecordAhead(REPL_REPOSITORY_LOCAL, name);
}

export function* copyReplayContext(): Operation<DurableContext> {
const context = yield* DurableContext.expect();
const failure = context.durability?.failure;
if (failure !== undefined) {
throw failure;
}
if (context.replayIndex.abandonedHistory(context.coroutineId)) {
throw new ReplLocalRecordError("Cannot continue File.Copy after abandoning retained history.");
}
return context;
}

export function* retainedCopyAhead(ordinal: number): Operation<boolean> {
const context = yield* copyReplayContext();
const description = context.replayIndex.peekYield(context.coroutineId)?.description;
const retained =
description?.type === REPL_REPOSITORY_LOCAL ? copyOrdinalFromName(description.name) : undefined;
if (retained !== undefined && retained < ordinal) {
throw yield* failCopy(
new ReplLocalRecordError("A retained file-copy occurrence was passed over."),
);
}
return retained === ordinal;
}

export function* failCopy(error: unknown): Operation<Error> {
const context = yield* DurableContext.expect();
const state = (context.durability ??= {});
return (state.failure ??= classify(error));
}

export function* checkCopyPublication(): Operation<void> {
const context = yield* copyReplayContext();
if (
context.replayIndex.peekYield(context.coroutineId) !== undefined ||
context.replayIndex.hasClose(context.coroutineId)
) {
throw new ReplLocalRecordError("A new file-copy outcome would precede retained history.");
}
}

/**
* This entry's ambient routing, restored from the journal or discovered live.
*
Expand Down Expand Up @@ -189,11 +232,8 @@ export function recordedLocalOperation(
}

function* recording(operation: LocalOperation): Operation<Result<ReplLocalValue>> {
const fingerprint = yield* replLocalFingerprint({
operation: operation.operation,
request: operation.request,
});
const name = replLocalRecordName({ operation: operation.operation, fingerprint });
const fingerprint = yield* replLocalFingerprint(operation);
const name = replLocalRecordName({ ...operation, fingerprint });

// Whatever comes back is journal data on a replay, so it is handed on as the
// unknown it is and parsed below rather than trusted as this code's own.
Expand All @@ -207,9 +247,8 @@ function* recording(operation: LocalOperation): Operation<Result<ReplLocalValue>
throw outcome.error;
}
return replLocalRecordJson({
operation: operation.operation,
...operation,
fingerprint,
request: operation.request,
outcome,
});
},
Expand All @@ -223,7 +262,11 @@ function* recording(operation: LocalOperation): Operation<Result<ReplLocalValue>
"says about work already done is unknown.",
);
}
if (parsed.operation !== operation.operation || parsed.fingerprint !== fingerprint) {
if (
parsed.operation !== operation.operation ||
parsed.fingerprint !== fingerprint ||
parsed.ordinal !== operation.ordinal
) {
throw new ReplLocalRecordError(
`the saved result standing where "${operation.operation}" belongs describes another ` +
"operation, so this history does not say what happened here.",
Expand Down
Loading
Loading