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
4 changes: 2 additions & 2 deletions FORK.md

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions apps/server/src/j5/sharing/CommandAdmission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ const admitted = Effect.fn("test.admitted")(function* (
sent: OrchestrationV2Command,
) {
const admission = yield* makeCommandAdmission(() => Effect.void, session as never);
return yield* admission.admit(sent).pipe(
Effect.as("admitted"),
Effect.catch((error) => Effect.succeed(`refused: ${error.message}`)),
);
return yield* admission
.dispatchCommand(sent, () => Effect.void)
.pipe(
Effect.as("admitted"),
Effect.catch((error) => Effect.succeed(`refused: ${error.message}`)),
);
});

const deviceOf = Effect.fn("test.deviceOf")(function* (
Expand Down
91 changes: 43 additions & 48 deletions apps/server/src/j5/sharing/CommandAdmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ const OWNERS_ONLY =
"Only an Owner of this server archives or deletes a thread, for now. Ask an Owner to do it.";

export interface CommandAdmission {
readonly admit: (
command: OrchestrationV2Command,
) => Effect.Effect<
OrchestrationV2Command,
EnvironmentAuthorizationError | OrchestrationV2DispatchCommandError
>;
/**
* A person's command, admitted and then handed to upstream's `dispatch`. `ws.ts` wraps upstream's
* dispatch with this (FORK.md case 78).
Expand Down Expand Up @@ -228,43 +222,38 @@ export const makeCommandAdmission = (
if (!levelReaches(held, level)) return yield* refused(NOT_SHARED);
}).pipe(Effect.catchTags({ PersonAccessStorageError: refuseUnreadable }));

const checked: CommandAdmission["admit"] = Effect.fn("j5.sharing.commandAdmission.checked")(
function* (command) {
if (OWNER_ONLY_COMMANDS.has(command.type)) {
const owner =
reach.kind === "service" ||
(reach.kind === "person" && levelReaches(reach.serverLevel, "owner"));
if (!owner) return yield* refused(OWNERS_ONLY);
yield* crewSeatArchiveGuard(command).pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2DispatchCommandError({
commandId: command.commandId,
commandType: command.type,
message: cause.message,
detail: cause.message,
cause,
}),
),
);
return command;
}
if (isWide(reach)) return command;
const scopeOf = NARROWED_COMMAND_SCOPES[command.type] as
| ((command: OrchestrationV2Command) => CommandScope)
| undefined;
if (scopeOf === undefined) return yield* refused(NOT_SHARED);
yield* requireLevel(scopeOf(command), "collaborator");
const checked = Effect.fn("j5.sharing.commandAdmission.checked")(function* (
command: OrchestrationV2Command,
) {
if (OWNER_ONLY_COMMANDS.has(command.type)) {
const owner =
reach.kind === "service" ||
(reach.kind === "person" && levelReaches(reach.serverLevel, "owner"));
if (!owner) return yield* refused(OWNERS_ONLY);
yield* crewSeatArchiveGuard(command).pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2DispatchCommandError({
commandId: command.commandId,
commandType: command.type,
message: cause.message,
detail: cause.message,
cause,
}),
),
);
return command;
},
);
}
if (isWide(reach)) return command;
const scopeOf = NARROWED_COMMAND_SCOPES[command.type] as
| ((command: OrchestrationV2Command) => CommandScope)
| undefined;
if (scopeOf === undefined) return yield* refused(NOT_SHARED);
yield* requireLevel(scopeOf(command), "collaborator");
return command;
});

const person = reachPersonId(reach);
const admit: CommandAdmission["admit"] = (command) =>
checked(command).pipe(
Effect.flatMap((admitted) => conversation.admit(person, admitted)),
Effect.withSpan("j5.sharing.commandAdmission.admit"),
);

// A person's edit of a queued agent message is recorded in the ledger under their name
// (`queuedEdits.ts`). A failure to claim it names no one and does not stop the edit.
Expand All @@ -287,13 +276,19 @@ export const makeCommandAdmission = (
),
);
const dispatchCommand: CommandAdmission["dispatchCommand"] = (command, dispatch) =>
Effect.gen(function* () {
const admitted = yield* admit(command);
const claimed = yield* claimEdit(admitted);
return yield* dispatch(admitted).pipe(
Effect.tapError(() => (claimed ? releaseEdit(admitted) : Effect.void)),
);
});
checked(command).pipe(
Effect.flatMap((admitted) =>
conversation.dispatch(person, admitted, (stamped) =>
Effect.gen(function* () {
const claimed = yield* claimEdit(stamped);
return yield* dispatch(stamped).pipe(
Effect.tapError(() => (claimed ? releaseEdit(stamped) : Effect.void)),
);
}),
),
),
Effect.withSpan("j5.sharing.commandAdmission.dispatchCommand"),
);

const admitLaunch: CommandAdmission["admitLaunch"] = Effect.fn(
"j5.sharing.commandAdmission.admitLaunch",
Expand Down Expand Up @@ -343,5 +338,5 @@ export const makeCommandAdmission = (
});
};

return { admit, dispatchCommand, admitLaunch, upsertScheduledTask };
return { dispatchCommand, admitLaunch, upsertScheduledTask };
});
190 changes: 187 additions & 3 deletions apps/server/src/j5/sharing/SharedConversation.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ const send = Effect.fn("test.send")(function* (
command: OrchestrationV2Command,
) {
const conversation = yield* SharedConversation.SharedConversation;
const admitted = yield* conversation.admit(personId, command);
yield* (yield* ThreadManagementService).dispatch(admitted);
return admitted;
const threads = yield* ThreadManagementService;
return yield* conversation.dispatch(personId, command, (admitted) =>
threads.dispatch(admitted).pipe(Effect.as(admitted)),
);
});

const runs = (on: ThreadId) =>
Expand Down Expand Up @@ -272,3 +273,186 @@ it.effect(
}).pipe(Effect.provide(layerFor(harness)));
},
);

it.effect("an edit upstream refuses leaves its message's record as it was", () => {
const harness = makeHarness();
const on = ThreadId.make("thread:shared-refused-edit");
return Effect.gen(function* () {
const people = yield* PersonAccess.PersonAccess;
const conversation = yield* SharedConversation.SharedConversation;
const threads = yield* ThreadManagementService;
yield* threads.dispatch({
type: "thread.create",
createdBy: "user",
creationSource: "web",
commandId: CommandId.make("command:refused-edit:create"),
threadId: on,
projectId,
title: "Shared",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
});
const owner = (yield* people.firstOwner).personId;
yield* people.setName({ personId: owner, displayName: "Jackson" });
const priya = yield* people.createPerson({ displayName: "Priya" });
yield* people.setLevel({
personId: priya.personId,
scope: { kind: "thread", threadId: on },
level: "collaborator",
});
// The first run stays active with no provider, so both of hers queue behind it.
yield* send(owner, message("Let us review the plan.", on));
const refusedMessage = message("Keep the migration step.", on);
const acceptedMessage = message("Ship Friday.", on);
yield* send(priya.personId, refusedMessage);
yield* send(priya.personId, acceptedMessage);
const recordOf = (messageId: string) =>
conversation
.threadAuthors(owner, on)
.pipe(
Effect.map((authors) => authors.authors.find((entry) => entry.messageId === messageId)!),
);
const runOf = (messageId: string) =>
threads
.getThreadProjection(on)
.pipe(
Effect.map(
(projection) => projection.runs.find((run) => run.userMessageId === messageId)!.id,
),
);
const before = yield* recordOf(refusedMessage.messageId);
const beforeText = yield* storedText(on, refusedMessage.messageId);

// Its run leaves the queue before the owner's edit lands, so upstream refuses the edit.
yield* threads.dispatch({
type: "queued-run.cancel",
commandId: CommandId.make("command:refused-edit:cancel"),
threadId: on,
runId: yield* runOf(refusedMessage.messageId),
});
const refusal = yield* send(owner, {
type: "queued-run.edit",
commandId: CommandId.make("command:refused-edit:edit"),
threadId: on,
runId: yield* runOf(refusedMessage.messageId),
text: "Drop the migration step.",
}).pipe(Effect.flip);
assert.strictEqual(refusal._tag, "OrchestratorCommandRejectedError");
assert.strictEqual(yield* storedText(on, refusedMessage.messageId), beforeText);
const after = yield* recordOf(refusedMessage.messageId);
assert.isNull(after.editedBy);
assert.strictEqual(after.storedPrefix, before.storedPrefix);
assert.deepStrictEqual(after, before);

// An edit upstream takes is recorded.
yield* send(owner, {
type: "queued-run.edit",
commandId: CommandId.make("command:accepted-edit:edit"),
threadId: on,
runId: yield* runOf(acceptedMessage.messageId),
text: "Ship Monday.",
});
const accepted = yield* recordOf(acceptedMessage.messageId);
assert.strictEqual(accepted.editedBy, "Jackson");
assert.strictEqual(
yield* storedText(on, acceptedMessage.messageId),
`${accepted.storedPrefix}Ship Monday.`,
);
}).pipe(Effect.provide(layerFor(harness)));
});

it.effect("an edit's record is written only while the stored text is the edit's own", () => {
const harness = makeHarness();
const on = ThreadId.make("thread:shared-edit-race");
return Effect.gen(function* () {
const people = yield* PersonAccess.PersonAccess;
const conversation = yield* SharedConversation.SharedConversation;
const threads = yield* ThreadManagementService;
yield* threads.dispatch({
type: "thread.create",
createdBy: "user",
creationSource: "web",
commandId: CommandId.make("command:edit-race:create"),
threadId: on,
projectId,
title: "Shared",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
});
const owner = (yield* people.firstOwner).personId;
yield* people.setName({ personId: owner, displayName: "Jackson" });
const scope = { kind: "thread" as const, threadId: on };
const priya = (yield* people.createPerson({ displayName: "Priya" })).personId;
const sam = (yield* people.createPerson({ displayName: "Sam" })).personId;
yield* people.setLevel({ personId: priya, scope, level: "collaborator" });
yield* people.setLevel({ personId: sam, scope, level: "collaborator" });
yield* send(owner, message("Let us review the plan.", on));
const replayed = message("Ship Friday.", on);
const raced = message("Keep the migration step.", on);
yield* send(priya, replayed);
yield* send(priya, raced);
const runOf = (messageId: string) =>
threads
.getThreadProjection(on)
.pipe(
Effect.map(
(projection) => projection.runs.find((run) => run.userMessageId === messageId)!.id,
),
);
const edit = (commandId: string, messageId: string, text: string) =>
runOf(messageId).pipe(
Effect.map(
(runId) =>
({
type: "queued-run.edit",
commandId: CommandId.make(commandId),
threadId: on,
runId,
text,
}) as const satisfies OrchestrationV2Command,
),
);
const recordOf = (messageId: string) =>
conversation
.threadAuthors(owner, on)
.pipe(
Effect.map((authors) => authors.authors.find((entry) => entry.messageId === messageId)!),
);

// Jackson edits, Sam edits after him, and Jackson's client retries his command: upstream
// replays its receipt and leaves Sam's text, so the record stays Sam's.
const jacksons = yield* edit("command:edit-race:jackson", replayed.messageId, "Ship Monday.");
yield* send(owner, jacksons);
yield* send(sam, yield* edit("command:edit-race:sam", replayed.messageId, "Ship Tuesday."));
const samsRecord = yield* recordOf(replayed.messageId);
assert.strictEqual(samsRecord.editedBy, "Sam");
yield* send(owner, jacksons);
assert.deepStrictEqual(yield* recordOf(replayed.messageId), samsRecord);
assert.strictEqual(
yield* storedText(on, replayed.messageId),
`${samsRecord.storedPrefix}Ship Tuesday.`,
);

// Sam's edit is taken and recorded after Jackson's commits and before Jackson's is recorded:
// the record stays Sam's, which the stored text carries.
const samsEdit = yield* edit("command:edit-race:sam-2", raced.messageId, "Drop it.");
yield* conversation.dispatch(
owner,
yield* edit("command:edit-race:jackson-2", raced.messageId, "Keep it."),
(admitted) =>
threads.dispatch(admitted).pipe(Effect.andThen(send(sam, samsEdit)), Effect.as(admitted)),
);
const raceRecord = yield* recordOf(raced.messageId);
assert.strictEqual(raceRecord.editedBy, "Sam");
assert.strictEqual(
yield* storedText(on, raced.messageId),
`${raceRecord.storedPrefix}Drop it.`,
);
}).pipe(Effect.provide(layerFor(harness)));
});
Loading
Loading