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
78 changes: 63 additions & 15 deletions apps/server/src/lazurio/buddyPrompt.orchestrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as EffectWorker from "../orchestration-v2/EffectWorker.ts";
import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
import { isTerminalRunStatus } from "../orchestration-v2/ThreadManagementService.ts";
import { lazurioPageContextBlock } from "./buddyPrompt.ts";
import * as MessageClients from "./messageClients.ts";
import {
addProject,
BUDDY,
Expand All @@ -44,6 +45,9 @@ const page = {
page: { path: "/a1b2c3" },
} as const;

/** The person's web client on a computer, as ws.ts reads its socket. */
const fromWeb = MessageClients.fromClient({}, { surface: "web", clientDeviceType: "desktop" });

/**
* Send a message as the person's web client does, and run the effect worker until the message's
* run settles.
Expand All @@ -70,21 +74,23 @@ const send = Effect.fn("test.buddyPrompt.send")(function* (
Stream.take(1),
Stream.runDrain,
);
yield* orchestrator.dispatch({
type: "message.dispatch",
createdBy: "user",
creationSource: "web",
commandId: CommandId.make(`command:buddy-prompt:${ordinal}`),
threadId,
messageId: MessageId.make(`message:buddy-prompt:${ordinal}`),
text: message.text,
...(message.records === undefined
? {}
: { context: { version: 1 as const, records: [...message.records] } }),
attachments: [],
modelSelection: message.modelSelection ?? buddyModel,
dispatchMode: { type: "start_immediately" },
});
yield* orchestrator
.dispatch({
type: "message.dispatch",
createdBy: "user",
creationSource: "web",
commandId: CommandId.make(`command:buddy-prompt:${ordinal}`),
threadId,
messageId: MessageId.make(`message:buddy-prompt:${ordinal}`),
text: message.text,
...(message.records === undefined
? {}
: { context: { version: 1 as const, records: [...message.records] } }),
attachments: [],
modelSelection: message.modelSelection ?? buddyModel,
dispatchMode: { type: "start_immediately" },
})
.pipe(fromWeb);
yield* Effect.raceFirst(
settled,
Effect.forever(worker.drain().pipe(Effect.andThen(worker.awaitWork))),
Expand Down Expand Up @@ -127,6 +133,48 @@ describe("Buddy's T3 Code", () => {
);
});

describe("T3 Code without Buddy", () => {
it.live("opens an agent's prompt with the client the person wrote it in", () => {
const agentHome = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "message-origin-"));
const agentLog = NodePath.join(agentHome, "requests.jsonl");
return Effect.gen(function* () {
const projectId = ProjectId.make("project:agent");
yield* addProject(projectId, agentHome);
const threadId = ThreadId.make("thread:agent");
yield* (yield* Orchestrator.OrchestratorV2).dispatch({
type: "thread.create",
commandId: CommandId.make("command:agent-thread"),
threadId,
projectId,
title: "Agent",
modelSelection: buddyModel,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
createdBy: "user",
creationSource: "web",
});
yield* send(threadId, 1, { text: "Run the tests." });

const origin = MessageClients.messageOriginLine(
{ id: "message", role: "user", createdBy: "user" },
{ surface: "web", deviceType: "desktop" },
);
assert.include(receivedPrompts(agentLog)[0]?.join("\n\n"), `${origin}\n\nRun the tests.`);
}).pipe(
Effect.provide(layerBuddyServer),
Effect.scoped,
Effect.provideService(HostProcess.Environment, {
...process.env,
HOME: agentHome,
T3CODE_LAZURIO_BUDDY: undefined,
T3_ACP_REQUEST_LOG_PATH: agentLog,
}),
);
});
});

describe("Buddy's T3 Code and Hermes's settings", () => {
it.live("sets Hermes no model, mode or other option a message's selection names", () => {
const selectionHome = NodeFS.mkdtempSync(
Expand Down
44 changes: 44 additions & 0 deletions apps/server/src/lazurio/buddyPrompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ import {
type LazurioPageContext,
lazurioPageContextBlock,
projectComposerContextForProvider,
providerProjection,
withBuddyInstance,
} from "./buddyPrompt.ts";
import * as MessageClients from "./messageClients.ts";

const BUDDY_ENV = { T3CODE_LAZURIO_BUDDY: "1" };
const BUDDY = ProviderInstanceId.make("buddy");
Expand Down Expand Up @@ -212,6 +214,48 @@ describe("the projection a provider turn gets", () => {
assert.strictEqual(text, projectUpstream(plain));
}).pipe(Effect.provideService(HostProcess.Environment, BUDDY_ENV)),
);

const message = { id: "message-1", role: "user", createdBy: "user", creationSource: "web" };
const web = { surface: "web", deviceType: "desktop" } as const;
const markedFromWeb = Effect.gen(function* () {
yield* (yield* MessageClients.MessageClients).record(message.id, web);
return yield* providerProjection;
});

it.effect("opens with where the message came from for another instance", () =>
Effect.gen(function* () {
const project = yield* markedFromWeb;
const codex = ProviderInstanceId.make("codex");
// Without a page record: one is only Buddy's window's, which gets the person's words alone.
const plain = { text: "Run the tests.", records: [] };
const text = yield* project({ ...plain, instanceId: codex, message });
assert.strictEqual(
text,
`${MessageClients.messageOriginLine(message, web)}\n\n${projectUpstream(plain)}`,
);
assert.strictEqual(
yield* project({ ...input, instanceId: codex, message }),
buddyProviderText(input),
);
// A native slash command works only at the start of the prompt.
const command = { instanceId: codex, text: "/compact", records: [], message };
assert.strictEqual(yield* project(command), "/compact");
}).pipe(
Effect.provide(MessageClients.layer),
Effect.provideService(HostProcess.Environment, BUDDY_ENV),
),
);

it.effect("never opens with where the message came from for Buddy's instance", () =>
Effect.gen(function* () {
const project = yield* markedFromWeb;
const text = yield* project({ ...input, instanceId: BUDDY, message });
assert.strictEqual(text, buddyProviderText(input));
}).pipe(
Effect.provide(MessageClients.layer),
Effect.provideService(HostProcess.Environment, BUDDY_ENV),
),
);
});

// What reaches the agent: the repository's mock ACP agent stands in for Hermes and logs every
Expand Down
43 changes: 32 additions & 11 deletions apps/server/src/lazurio/buddyPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";

import type { MessageSenderFields } from "./actingSenderVisibility.ts";
import { isBuddyInstance } from "./buddySurface.ts";
import { MessageClients, messageOriginLine } from "./messageClients.ts";

/** The composer record of the page Buddy is asked from. */
const LAZURIO_PAGE_CONTEXT_KIND = "lazurio-page";
Expand Down Expand Up @@ -130,22 +132,41 @@ export function buddyProviderText(input: {
.join("\n\n");
}

interface ProviderProjectionInput {
readonly instanceId: ProviderInstanceId;
readonly text: string;
readonly records: ReadonlyArray<ComposerContextRecord>;
/** The message the text is, for the line saying where it came from (plan LZD-0025 B9). */
readonly message?: MessageSenderFields;
}

/**
* The text a provider turn gets for a message: Buddy's projection for Buddy's instance and for any
* message with a page record, which only Buddy's window adds (GEN4 on any provider); upstream's
* for every other.
* The text a provider turn gets for a message: Buddy's projection, only the person's words, for
* Buddy's instance and any message with a page record, which only Buddy's window adds (GEN4 on any
* provider); for every other, upstream's after a line saying where the message came from.
*/
export const projectComposerContextForProvider = Effect.fn(
"LazurioBuddyPrompt.projectComposerContextForProvider",
)(function* (input: {
readonly instanceId: ProviderInstanceId;
readonly text: string;
readonly records: ReadonlyArray<ComposerContextRecord>;
}) {
)(function* (input: ProviderProjectionInput) {
const environment = yield* HostProcess.Environment;
return isBuddyInstance(environment, input.instanceId) || input.records.some(isPageRecord)
? buddyProviderText(input)
: projectUpstream(input);
if (isBuddyInstance(environment, input.instanceId) || input.records.some(isPageRecord))
return buddyProviderText(input);
const text = projectUpstream(input);
// A native slash command (`/compact`, `/goal`) works only at the start of the prompt.
if (input.message === undefined || text.trimStart().startsWith("/")) return text;
const clients = yield* MessageClients;
const line = messageOriginLine(input.message, yield* clients.clientOf(input.message.id));
return line === null ? text : `${line}\n\n${text}`;
});

/**
* The projection with the server's marks of where messages came from, taken where a turn service
* is built: its turns run on the effect worker, which does not carry the store.
*/
export const providerProjection = Effect.gen(function* () {
const clients = yield* MessageClients;
return (input: ProviderProjectionInput) =>
projectComposerContextForProvider(input).pipe(Effect.provideService(MessageClients, clients));
});

/** T3's instructions for every ACP instance but Buddy's. */
Expand Down
Loading
Loading