Skip to content
Open
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
97 changes: 91 additions & 6 deletions apps/server/src/memory/Memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,38 @@ it.effect("accepting adds the edited line to the chosen file and merges at once"
{ projectId: PROJECT_ID, proposalId: team.id, choice: "team", text: "Tests need Node 24." },
"session-1",
);
const mineAll = yield* memory.proposeFromAgent(agentProposal("Keep diffs small."));
const acceptedMineAll = yield* memory.decide(
{
projectId: PROJECT_ID,
proposalId: mineAll.id,
choice: "me-all",
text: "Keep diffs small.",
},
"session-1",
);
expect(acceptedMineAll.decision).toMatchObject({
choice: "me-all",
text: "Keep diffs small.",
file: "memory/people/alice.md",
merged: true,
});
const teamAll = yield* memory.proposeFromAgent(agentProposal("Write plain commit titles."));
const acceptedTeamAll = yield* memory.decide(
{
projectId: PROJECT_ID,
proposalId: teamAll.id,
choice: "team-all",
text: "Write plain commit titles.",
},
"session-1",
);
expect(acceptedTeamAll.decision).toMatchObject({
choice: "team-all",
text: "Write plain commit titles.",
file: "memory/team.md",
merged: true,
});
const rejected = yield* memory.proposeFromAgent(agentProposal("Always use drafts."));
const decision = yield* memory.decide(
{ projectId: PROJECT_ID, proposalId: rejected.id, choice: "reject", text: "Ignored." },
Expand All @@ -493,8 +525,40 @@ it.effect("accepting adds the edited line to the chosen file and merges at once"
text: "- Run the web tests before pushing.\n- Tests need Node 24.\n",
},
],
[
{
file: "memory/people/alice.md",
text: "- Keep replies short.\n- Keep diffs small.\n",
},
],
[
{
file: "memory/team.md",
text: "- Prefer small pull requests.\n- Write plain commit titles.\n",
},
],
]);
expect(fixture.proposals[2]).toMatchObject({
title: "Remember for me across all projects: Keep diffs small.",
branchStem: "memory/personal-alice",
});
expect(fixture.proposals[3]).toMatchObject({
title: "Remember for the team across all projects: Write plain commit titles.",
branchStem: "memory/team",
});
expect(fixture.merged).toHaveLength(4);

// After a GitHub CLI account switch, me-all goes to the signed-in person's file.
const switched = makeMemory({ catalog: catalog(), liveLogin: "Bob" });
const bob = yield* switched.build;
const bobProposal = yield* bob.proposeFromAgent(agentProposal("Skip emojis."));
yield* bob.decide(
{ projectId: PROJECT_ID, proposalId: bobProposal.id, choice: "me-all", text: "Skip emojis." },
"session-1",
);
expect(switched.proposals.map((proposal) => proposal.files)).toEqual([
[{ file: "memory/people/bob.md", text: "- Bob's own rule.\n- Skip emojis.\n" }],
]);
expect(fixture.merged).toHaveLength(2);
}).pipe(Effect.scoped),
);

Expand Down Expand Up @@ -683,14 +747,16 @@ it.effect("accepting a voice agent memory puts it in place of the one it replace
replaces: "Keep updates to one sentence per agent.",
decision: null,
});
const decide = (choice: "me" | "team", proposalId = proposal.id) =>
const decide = (choice: "me" | "me-all" | "team" | "team-all", proposalId = proposal.id) =>
memory.decide(
{ projectId: PROJECT_ID, proposalId, choice, text: "Give two sentences per agent." },
"session-1",
);
expect((yield* Effect.flip(decide("team"))).detail).toBe(
"A voice agent memory is yours alone: accept it for yourself or reject it.",
);
for (const disallowed of ["team", "team-all", "me-all"] as const) {
expect((yield* Effect.flip(decide(disallowed))).detail).toBe(
"A voice agent memory is yours alone: accept it for yourself or reject it.",
);
}
expect((yield* decide("me")).decision).toMatchObject({
choice: "me",
text: "Give two sentences per agent.",
Expand Down Expand Up @@ -942,7 +1008,10 @@ it.effect("undo reverts an accepted addition, change or removal in the file it e
const files: Record<string, string> = { ...FILES };
const fixture = makeMemory({ catalog: catalog(), files });
const memory = yield* fixture.build;
const accept = (proposal: MemoryProposal, choice: "me" | "team" | "accept" | "reject") =>
const accept = (
proposal: MemoryProposal,
choice: "me" | "me-all" | "team" | "team-all" | "accept" | "reject",
) =>
memory.decide(
{ projectId: PROJECT_ID, proposalId: proposal.id, choice, text: proposal.text },
"session-1",
Expand All @@ -968,6 +1037,22 @@ it.effect("undo reverts an accepted addition, change or removal in the file it e
expect(fixture.store.saved[0]?.decidedBySession).toBe("session-1");
expect((yield* Effect.flip(undo(added))).detail).toBe("This memory was already undone.");

const addedMineAll = yield* memory.proposeFromAgent(agentProposal("Keep diffs small."));
yield* accept(addedMineAll, "me-all");
expect(files["memory/people/alice.md"]).toBe("- Keep replies short.\n- Keep diffs small.\n");
yield* undo(addedMineAll);
expect(files["memory/people/alice.md"]).toBe("- Keep replies short.\n");

const addedTeamAll = yield* memory.proposeFromAgent(
agentProposal("Write plain commit titles."),
);
yield* accept(addedTeamAll, "team-all");
expect(files["memory/team.md"]).toBe(
"- Prefer small pull requests.\n- Write plain commit titles.\n",
);
yield* undo(addedTeamAll);
expect(files["memory/team.md"]).toBe("- Prefer small pull requests.\n");

const changed = yield* memory.proposeFromAgent(
editProposal({ text: "Keep replies to one line.", replaces: "Keep replies short." }),
);
Expand Down
44 changes: 31 additions & 13 deletions apps/server/src/memory/Memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,28 @@ const make = Effect.gen(function* () {
return onWire(decided);
}, writeLock.withPermits(1));

/** Accept for me or for the team: the line joins the end of that file. */
/** Accept for me or for the team (this project or all projects): the line joins the end of that file. */
const appendedLine = Effect.fn("Memory.appendedLine")(function* (
input: MemoryDecideInput,
projectId: ProjectId,
choice: "me" | "me-all" | "team" | "team-all",
line: string,
) {
const { view, project } = yield* readWithProject(input.projectId);
const scope: MemoryScope = input.choice === "me" ? "personal-project" : "project";
const { view, project } = yield* readWithProject(projectId);
const { scope, titlePrefix } = ((): {
readonly scope: MemoryScope;
readonly titlePrefix: string;
} => {
switch (choice) {
case "me":
return { scope: "personal-project", titlePrefix: "Remember for me" };
case "me-all":
return { scope: "personal", titlePrefix: "Remember for me across all projects" };
case "team":
return { scope: "project", titlePrefix: "Remember for the team" };
case "team-all":
return { scope: "team", titlePrefix: "Remember for the team across all projects" };
}
})();
const target = yield* writableDocument(view, project, scope);
if (view.revision === null || target === undefined) {
return yield* new TeamCatalogError({ detail: NO_CATALOG });
Expand All @@ -651,22 +666,20 @@ const make = Effect.gen(function* () {
baseRevision: view.revision,
file: target.file,
text: appendMemoryLine(target.text, line),
title: `${input.choice === "me" ? "Remember for me" : "Remember for the team"}: ${line}`,
title: `${titlePrefix}: ${line}`,
body: [`Adds to \`${target.file}\`:`, `> ${line}`],
branchStem: `memory/${scope}-${target.file
.slice(target.file.lastIndexOf("/") + 1)
.replace(/\.md$/, "")}`,
branchStem: scope === "team" ? "memory/team" : `memory/${scope}-${fileName(target.file)}`,
};
});

/** A voice agent memory goes to the live login's own file, in place of the one it replaces. */
const supersededVoiceAgentMemory = Effect.fn("Memory.supersededVoiceAgentMemory")(function* (
input: MemoryDecideInput,
choice: "me" | "me-all" | "team" | "team-all",
proposal: MemoryProposal,
line: string,
today: string,
) {
if (input.choice === "team") {
if (choice !== "me") {
return yield* new TeamCatalogError({
detail: "A voice agent memory is yours alone: accept it for yourself or reject it.",
});
Expand Down Expand Up @@ -816,9 +829,14 @@ const make = Effect.gen(function* () {
];
const change = edit
? yield* editedLine(proposal, line)
: proposal.category === undefined
? yield* appendedLine(input, line)
: yield* supersededVoiceAgentMemory(input, proposal, line, decidedAt.slice(0, 10));
: input.choice === "accept"
? yield* new TeamCatalogError({
detail:
"Choose who this memory is for: accept it for yourself or the team, or reject it.",
})
: proposal.category === undefined
? yield* appendedLine(input.projectId, input.choice, line)
: yield* supersededVoiceAgentMemory(input.choice, proposal, line, decidedAt.slice(0, 10));
const pullRequest = yield* proposeAndMerge(change, origin);
return yield* record(
proposal.id,
Expand Down
104 changes: 82 additions & 22 deletions apps/web/src/components/memory/MemoryProposalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
type ScopedThreadRef,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import { CheckIcon } from "lucide-react";
import { CheckIcon, ChevronDownIcon } from "lucide-react";
import { useState } from "react";

import { useOpenLink } from "~/browser/useOpenLink";
Expand All @@ -28,8 +28,11 @@ import { useEnvironmentScope } from "~/state/session";
import { useAtomCommand } from "~/state/use-atom-command";
import { Badge } from "../ui/badge";
import { Button } from "../ui/button";
import { Group, GroupSeparator } from "../ui/group";
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
import { Textarea } from "../ui/textarea";
import {
memoryAcceptedLabel,
memoryProposalInput,
memoryScopeLabel,
proposalForCall,
Expand Down Expand Up @@ -66,6 +69,56 @@ export function useMemoryDecision(environmentId: EnvironmentId, projectId: Proje
return { canDecide, busy, error, submit };
}

function MemoryAcceptSplitButton(props: {
readonly label: string;
readonly menuLabel: string;
readonly projectChoice: "me" | "team";
readonly allProjectsChoice: "me-all" | "team-all";
readonly projectItemLabel: string;
readonly allProjectsItemLabel: string;
readonly busy: MemoryChoice | null;
readonly disabled: boolean;
readonly onChoose: (choice: MemoryChoice) => void;
}) {
const saving = props.busy === props.projectChoice || props.busy === props.allProjectsChoice;
return (
<Group>
<Button
size="xs"
variant="outline"
disabled={props.disabled}
onClick={() => props.onChoose(props.projectChoice)}
>
{saving ? "Saving…" : props.label}
</Button>
<GroupSeparator />
<Menu>
<MenuTrigger
disabled={props.disabled}
render={
<Button
size="icon-xs"
variant="outline"
aria-label={props.menuLabel}
disabled={props.disabled}
/>
}
>
<ChevronDownIcon aria-hidden />
</MenuTrigger>
<MenuPopup align="start" width="content">
<MenuItem onClick={() => props.onChoose(props.projectChoice)}>
{props.projectItemLabel}
</MenuItem>
<MenuItem onClick={() => props.onChoose(props.allProjectsChoice)}>
{props.allProjectsItemLabel}
</MenuItem>
</MenuPopup>
</Menu>
</Group>
);
}

export function MemoryDecisionButtons(props: {
readonly busy: MemoryChoice | null;
readonly canDecide: boolean;
Expand All @@ -83,26 +136,41 @@ export function MemoryDecisionButtons(props: {
return (
<>
{kind === null ? (
<>
props.personalOnly ? (
<Button
size="xs"
variant="outline"
disabled={acceptDisabled}
onClick={() => props.onChoose("me")}
>
{props.busy === "me" ? "Saving…" : props.personalOnly ? "Accept" : "Accept for me"}
{props.busy === "me" ? "Saving…" : "Accept"}
</Button>
{props.personalOnly ? null : (
<Button
size="xs"
variant="outline"
) : (
<>
<MemoryAcceptSplitButton
label="Accept for me"
menuLabel="More options for Accept for me"
projectChoice="me"
allProjectsChoice="me-all"
projectItemLabel="For me (this project)"
allProjectsItemLabel="For me (all projects)"
busy={props.busy}
disabled={acceptDisabled}
onClick={() => props.onChoose("team")}
>
{props.busy === "team" ? "Saving…" : "Accept for team"}
</Button>
)}
</>
onChoose={props.onChoose}
/>
<MemoryAcceptSplitButton
label="Accept for team"
menuLabel="More options for Accept for team"
projectChoice="team"
allProjectsChoice="team-all"
projectItemLabel="For team (this project)"
allProjectsItemLabel="For team (all projects)"
busy={props.busy}
disabled={acceptDisabled}
onChoose={props.onChoose}
/>
</>
)
) : (
<Button
size="xs"
Expand Down Expand Up @@ -223,15 +291,7 @@ function MemoryOutcome(props: {
{label}
</button>
);
const where = props.projectName === null ? "" : ` in ${props.projectName}`;
const accepted =
proposal.category !== undefined
? "Accepted for the voice agent"
: proposal.kind === "change"
? "Change accepted"
: proposal.kind === "remove"
? "Removal accepted"
: `Accepted ${decision.choice === "me" ? "for you" : "for the team"}${where}`;
const accepted = memoryAcceptedLabel(proposal, decision.choice, props.projectName);
const undone = decision.undo;
const undoable =
proposal.category === undefined && decision.merged && undone === undefined && canUndo;
Expand Down
Loading
Loading