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
2 changes: 1 addition & 1 deletion FORK.md

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions apps/server/src/j5/sharing/CommandAdmission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,15 @@ it.layer(conversationLayer)("CommandAdmission", (it) => {
]);
const admission = yield* makeCommandAdmission(() => Effect.void, projectOnly);
const launch = (projectId: string) =>
admission.admitLaunch({ projectId } as unknown as OrchestrationV2ThreadLaunchInput).pipe(
Effect.as("admitted"),
Effect.orElseSucceed(() => "refused"),
);
admission
.launchThread(
{ projectId } as unknown as OrchestrationV2ThreadLaunchInput,
Effect.succeed,
)
.pipe(
Effect.as("admitted"),
Effect.orElseSucceed(() => "refused"),
);
assert.strictEqual(yield* launch(PROJECT), "admitted");
assert.strictEqual(yield* launch(OTHER_PROJECT), "refused");
}),
Expand Down
19 changes: 12 additions & 7 deletions apps/server/src/j5/sharing/CommandAdmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ export interface CommandAdmission {
command: OrchestrationV2Command,
dispatch: (command: OrchestrationV2Command) => Effect.Effect<A, E, R>,
) => Effect.Effect<A, E | EnvironmentAuthorizationError | OrchestrationV2DispatchCommandError, R>;
readonly admitLaunch: (
/** A person's launch of a thread, admitted and then handed to upstream's launch (FORK.md case 78). */
readonly launchThread: <A, E, R>(
input: OrchestrationV2ThreadLaunchInput,
) => Effect.Effect<OrchestrationV2ThreadLaunchInput, EnvironmentAuthorizationError>;
launch: (input: OrchestrationV2ThreadLaunchInput) => Effect.Effect<A, E, R>,
) => Effect.Effect<A, E | EnvironmentAuthorizationError, R>;
/**
* A person's save of a scheduled task, with who scheduled it recorded: whoever creates it, and
* whoever changes its prompt (`scheduledRuns`). `ws.ts` wraps upstream's upsert with this
Expand Down Expand Up @@ -294,16 +296,19 @@ export const makeCommandAdmission = Effect.fn("j5.sharing.commandAdmission.makeC
Effect.withSpan("j5.sharing.commandAdmission.dispatchCommand"),
);

const admitLaunch: CommandAdmission["admitLaunch"] = Effect.fn(
"j5.sharing.commandAdmission.admitLaunch",
)(function* (input) {
const launchThread: CommandAdmission["launchThread"] = Effect.fn(
"j5.sharing.commandAdmission.launchThread",
)(function* <A, E, R>(
input: OrchestrationV2ThreadLaunchInput,
launch: (input: OrchestrationV2ThreadLaunchInput) => Effect.Effect<A, E, R>,
) {
// Launching into a thread that exists writes to it; otherwise it starts one in a project.
const scope: CommandScope =
input.reuseExistingThread === true && input.threadId !== undefined
? { kind: "thread", threadId: input.threadId }
: { kind: "project", projectId: input.projectId };
yield* requireLevel(scope, "collaborator");
return yield* conversation.admitLaunch(person, input);
return yield* conversation.launch(person, input, launch);
});

const upsertScheduledTask: CommandAdmission["upsertScheduledTask"] = (input, upsert) => {
Expand Down Expand Up @@ -342,6 +347,6 @@ export const makeCommandAdmission = Effect.fn("j5.sharing.commandAdmission.makeC
});
};

return { dispatchCommand, admitLaunch, upsertScheduledTask };
return { dispatchCommand, launchThread, upsertScheduledTask };
},
);
226 changes: 219 additions & 7 deletions apps/server/src/j5/sharing/SharedConversation.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
type OrchestrationV2Command,
type OrchestrationV2ThreadLaunchInput,
ProviderDriverKind,
ProjectId,
ProviderSessionId,
RunId,
RuntimeRequestId,
ThreadId,
} from "@t3tools/contracts";
Expand Down Expand Up @@ -259,16 +261,21 @@ it.effect(
attachments: [],
},
} as const satisfies OrchestrationV2ThreadLaunchInput;
const admitted = yield* conversation.admitLaunch(owner, launch);
yield* launches.launch({
...launch,
initialMessage: { ...launch.initialMessage, text: admitted.initialMessage!.text },
createdBy: "user",
creationSource: "web",
});
yield* conversation.launch(owner, launch, (admitted) =>
launches.launch({
...launch,
initialMessage: { ...launch.initialMessage, text: admitted.initialMessage!.text },
createdBy: "user",
creationSource: "web",
}),
);
const first = yield* storedText(on, launch.initialMessage.messageId);
assert.include(first, "Who can write: Jackson (owner), Priya (collaborator).");
assert.isTrue(first?.endsWith("\n[Message from Jackson (owner)]\n\nLet us review the plan."));
const recorded = (yield* conversation.threadAuthors(owner, on)).authors.find(
(author) => author.messageId === launch.initialMessage.messageId,
)!;
assert.strictEqual(first, `${recorded.storedPrefix}Let us review the plan.`);

// Her `/compact` goes on as typed, so upstream takes it for the command it is (AC5).
const compact = message("/compact", on);
Expand Down Expand Up @@ -562,3 +569,208 @@ it.effect("a person's approval feedback, written by upstream, is recorded as the
assert.deepStrictEqual(yield* named(priya.personId), [[written.id, "Priya", true]]);
}).pipe(Effect.provide(Layer.provideMerge(ProjectionStore.layer, layerFor(harness))));
});

/** The author record J5 keeps for a message, or none. */
const authorRecord = (messageId: string) =>
SqlClient.SqlClient.use(
(sql) => sql<{ readonly display_name: string | null; readonly stored_prefix: string }>`
SELECT display_name, stored_prefix FROM j5_a2a_message_author
WHERE message_id = ${messageId}
`,
).pipe(Effect.map((rows) => rows[0] ?? null));

it.effect(
"a new message upstream refuses leaves no author record, and its retry is recorded",
() => {
const harness = makeHarness();
const on = ThreadId.make("thread:shared-refused-message");
return Effect.gen(function* () {
const people = yield* PersonAccess.PersonAccess;
const threads = yield* ThreadManagementService;
yield* threads.dispatch({
type: "thread.create",
createdBy: "user",
creationSource: "web",
commandId: CommandId.make("command:refused-message: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",
});

// Upstream refuses to continue a run the thread never had, so nothing is stored.
const typed = "Pick up where you left off.";
const refused = {
...message(typed, on),
manualContinuationOfRunId: RunId.make("run:never-ran"),
} satisfies OrchestrationV2Command;
const refusal = yield* send(priya.personId, refused).pipe(Effect.flip);
assert.strictEqual(refusal._tag, "OrchestratorDispatchError");
assert.isUndefined(yield* storedText(on, refused.messageId));
assert.isNull(yield* authorRecord(refused.messageId));

// She is renamed, then sends it again under the same id: it is recorded as now sent.
yield* people.setName({ personId: priya.personId, displayName: "Priya Shah" });
const retried = { ...message(typed, on), messageId: refused.messageId };
yield* send(priya.personId, retried);
const record = (yield* authorRecord(refused.messageId))!;
assert.strictEqual(record.display_name, "Priya Shah");
assert.include(record.stored_prefix, "[Message from Priya Shah (collaborator)]");
assert.strictEqual(
yield* storedText(on, refused.messageId),
`${record.stored_prefix}${typed}`,
);
}).pipe(Effect.provide(layerFor(harness)));
},
);

it.effect(
"a message's id is held from admission, and its record kept once upstream stored it",
() => {
const harness = makeHarness();
const on = ThreadId.make("thread:shared-held-id");
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:held-id: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" });

// Sam sends under her id while hers is still on its way to upstream: his is refused.
const hers = message("Ship Friday.", on);
const samsRefusal = yield* conversation.dispatch(priya, hers, (admitted) =>
send(sam, { ...message("Ship Monday.", on), messageId: hers.messageId }).pipe(
Effect.flip,
Effect.tap(() => threads.dispatch(admitted)),
),
);
assert.strictEqual(samsRefusal.message, "This message was already sent.");
const record = (yield* authorRecord(hers.messageId))!;
assert.strictEqual(record.display_name, "Priya");
assert.strictEqual(
yield* storedText(on, hers.messageId),
`${record.stored_prefix}Ship Friday.`,
);

// Upstream stores her next message, then the request fails: the message keeps its record.
const late = message("Keep the migration step.", on);
yield* conversation
.dispatch(priya, late, (admitted) =>
threads.dispatch(admitted).pipe(Effect.andThen(Effect.fail("lost after commit"))),
)
.pipe(Effect.flip);
const kept = (yield* authorRecord(late.messageId))!;
assert.strictEqual(kept.display_name, "Priya");
assert.strictEqual(
yield* storedText(on, late.messageId),
`${kept.stored_prefix}Keep the migration step.`,
);

// Her client retries a send still on its way to upstream, and the retry fails first: it
// releases nothing, so the first attempt's record stands once upstream takes it.
const pending = message("Ship it.", on);
yield* conversation.dispatch(priya, pending, (admitted) =>
conversation
.dispatch(priya, pending, () => Effect.fail("retry refused"))
.pipe(Effect.flip, Effect.andThen(threads.dispatch(admitted))),
);
const survived = (yield* authorRecord(pending.messageId))!;
assert.strictEqual(survived.display_name, "Priya");
assert.strictEqual(
yield* storedText(on, pending.messageId),
`${survived.stored_prefix}Ship it.`,
);
}).pipe(Effect.provide(layerFor(harness)));
},
);

it.effect("a launch upstream refuses leaves no author record for its first message", () => {
const harness = makeHarness();
const on = ThreadId.make("thread:shared-refused-launch");
const gone = ProjectId.make("project:gone");
return Effect.gen(function* () {
const people = yield* PersonAccess.PersonAccess;
const conversation = yield* SharedConversation.SharedConversation;
const launches = yield* ThreadLaunch.ThreadLaunchService;
const owner = (yield* people.firstOwner).personId;
yield* people.setName({ personId: owner, displayName: "Jackson" });
const priya = yield* people.createPerson({ displayName: "Priya" });
for (const scoped of [projectId, gone]) {
yield* people.setLevel({
personId: priya.personId,
scope: { kind: "project", projectId: scoped },
level: "collaborator",
});
}
const launch = (into: ProjectId, commandId: string) => {
const input = {
commandId: CommandId.make(commandId),
threadId: on,
projectId: into,
title: "Review the plan",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
workspaceStrategy: { type: "root" },
initialMessage: {
messageId: MessageId.make("message:refused-launch"),
text: "Let us review the plan.",
attachments: [],
},
} as const satisfies OrchestrationV2ThreadLaunchInput;
return conversation.launch(owner, input, (admitted) =>
launches.launch({
...input,
initialMessage: { ...input.initialMessage, text: admitted.initialMessage!.text },
createdBy: "user",
creationSource: "web",
}),
);
};

// Upstream refuses a launch into a project it does not have.
const refusal = yield* launch(gone, "command:refused-launch").pipe(Effect.flip);
assert.strictEqual(refusal._tag, "ThreadLaunchError");
assert.isNull(yield* authorRecord("message:refused-launch"));

yield* people.setName({ personId: owner, displayName: "Jackson Miller" });
yield* launch(projectId, "command:accepted-launch");
const record = (yield* authorRecord("message:refused-launch"))!;
assert.include(record.stored_prefix, "[Message from Jackson Miller (owner)]");
assert.strictEqual(
yield* storedText(on, "message:refused-launch"),
`${record.stored_prefix}Let us review the plan.`,
);
}).pipe(Effect.provide(layerFor(harness)));
});
18 changes: 11 additions & 7 deletions apps/server/src/j5/sharing/SharedConversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ it.effect("a thread started in a project already shared is shared from its first
attachments: [],
},
} as unknown as OrchestrationV2ThreadLaunchInput;
const admitted = yield* conversation.admitLaunch(owner, launch);
const admitted = yield* conversation.launch(owner, launch, Effect.succeed);
const text = admitted.initialMessage!.text;
assert.include(text, "Who can write: Jackson (owner), Priya (collaborator).");
// A new thread has no messages from before.
Expand All @@ -659,7 +659,7 @@ it.effect("a thread started in a project already shared is shared from its first
threadId: "thread:alone",
projectId: ProjectId.make("project:other"),
} as OrchestrationV2ThreadLaunchInput;
assert.strictEqual(yield* conversation.admitLaunch(owner, elsewhere), elsewhere);
assert.strictEqual(yield* conversation.launch(owner, elsewhere, Effect.succeed), elsewhere);
}).pipe(Effect.provide(conversationLayer)),
);

Expand Down Expand Up @@ -1185,11 +1185,15 @@ it.effect(
{ scope: { kind: "project", projectId: PROJECT }, level: "collaborator" },
]);
const conversation = yield* SharedConversation;
yield* conversation.admitLaunch(owner, {
commandId: CommandId.make("command:empty-launch"),
threadId: "thread:empty-launch" as ThreadId,
projectId: PROJECT,
} as unknown as OrchestrationV2ThreadLaunchInput);
yield* conversation.launch(
owner,
{
commandId: CommandId.make("command:empty-launch"),
threadId: "thread:empty-launch" as ThreadId,
projectId: PROJECT,
} as unknown as OrchestrationV2ThreadLaunchInput,
Effect.succeed,
);
yield* conversation.dispatch(
owner,
{
Expand Down
Loading
Loading