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: 2 additions & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ The tests do not depend on who runs them, also from a bot's own shell. Before th

**The fake model.** `test/support/fake-model.ts` is a small server that speaks the OpenAI chat completions API, so a real gateway uses it through pi's model layer like any OpenAI-compatible endpoint. Its judge says yes to events about pricing; its agent answers each wake with one `send` in the wake's place. It says the provider's cache served 80 of each request's 100 input tokens. It answers a memory pass with "Nothing to save." and a tidy with "Nothing changed.", unless a test gives answers for them. It is also a judge with TypeSafe's System One API, for pi's provider `typesafe` pointed at it in models.json: yes to events about pricing, and TypeSafe's 401 for a key that starts with `wrong`. It records each request's provider session (the `session_id` header, which the tests' models.json asks pi to send). It also records the answer cap a request asks for (1 for a prompt cache warm), the prompt cache retention it asks for (`24h` for pi-ai's long one) and the app it names to OpenRouter. The tests point pi at it with `PI_CODING_AGENT_DIR`, so your own `~/.pi/agent` is not used.

**Waiting for the bot.** A test waits for what it can see, with `waitFor` in `test/support/gateway.ts`, never for a fixed time; `pause` is for showing that something does not happen. The fake model runs in the test's process, so a check that is polled while the bot works runs its pots command with `gateway.command` or `gateway.run`, which do not block. `gateway.pots` blocks the process while the command runs, a second or more, and the fake model cannot answer meanwhile.

**Known gap in secret masking.** Masking is a guard the person does not notice, not a wall. A `codemode` script's result is masked by the result hook, but pi-durable keeps the script's own result in its run record (`CodemodeRunDoc`) in the same commit that ends the script, before the hook runs, and keeps a tool call's arguments as the model or the script wrote them. So a script that puts a secret together itself, or a call whose arguments hold one, can leave it in `bot.sqlite`. Masking it there needs a change in pi-durable.

**Crash tests.** An empty file in a bot's folder makes the gateway kill itself at one point, once. This works only in a gateway with `POTS_TEST_CRASH_FILES=1`, which `test/support/gateway.ts` sets for the gateways it starts; a gateway without it ignores these files and the hold files below:
Expand Down
96 changes: 51 additions & 45 deletions test/children.test.ts

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions test/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AgentDoc, type ConversationId, configure, createRegistry, defineExtensi
import { SqliteStorage } from "@earendil-works/pi-durable/storage/sqlite";
import { openNodeSqliteDatabase } from "@earendil-works/pi-durable/storage/sqlite/node";
import { afterEach, describe, expect, it } from "vitest";
import { agentRequests, type FakeModel, type FakeReply, type FakeRequest, startFakeModel } from "./support/fake-model.ts";
import { agentRequests, type FakeModel, type FakeReply, type FakeRequest, isChild, startFakeModel } from "./support/fake-model.ts";
import { ME, TestGateway, waitFor } from "./support/gateway.ts";

// Code mode and MCP servers from outside: a real gateway with a fake model, pi's mcp.json in the gateway's own pi folder
Expand Down Expand Up @@ -57,7 +57,6 @@ afterEach(async () => {
expect(gateway.output.filter((line) => /failed|unhandled|could not/i.test(String(line.message)))).toEqual([]);
});

const isChild = (request: FakeRequest) => request.system.includes("You are a child agent of");
const mainRequests = () => agentRequests(model).filter((request) => !isChild(request));
/** The first result of a child's tool call that contains `text`. */
const childResult = (text: string) =>
Expand All @@ -66,7 +65,7 @@ const childResult = (text: string) =>
.flatMap((request) => request.messages.filter((message) => message.role === "tool" && message.content.includes(text)))
.at(0);
/** Wait until the child c1 ended its last work: called once its work started, so an earlier end does not count. */
const reported = () => waitFor(() => /^c1 "[^"]*": ended/m.test(gateway.pots("children")), { what: "the child's end" });
const reported = () => waitFor(async () => /^c1 "[^"]*": ended/m.test(await gateway.command("children")), { what: "the child's end" });

/**
* Give the children of the stopped gateway's bot the extensions as pots 0.8.7 stored them: a list of names, with one
Expand Down
4 changes: 2 additions & 2 deletions test/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { afterEach, describe, expect, it } from "vitest";
import { forkRefusal, type MemoryFolders, tidyDue, withAgeNote } from "../src/agent/memory.ts";
import { cutIndex, indexStatus, listMemoryFiles, loadIndex } from "../src/bot/memory.ts";
import { type PromptFacts, renderExtraction, renderTidy } from "../src/bot/prompt.ts";
import { agentRequests, coworker, type FakeModel, type FakeReply, type FakeRequest, isTidy, isMemoryPass, lastUser, startFakeModel, wakes } from "./support/fake-model.ts";
import { agentRequests, coworker, type FakeModel, type FakeReply, type FakeRequest, isChild, isTidy, isMemoryPass, lastUser, startFakeModel, wakes } from "./support/fake-model.ts";
import { ME, pause, TestGateway, waitFor } from "./support/gateway.ts";

// The memory from outside, and the parts of it that code must hold. Their failures would be silent: a fork whose request
Expand Down Expand Up @@ -397,7 +397,7 @@ describe("memory", { timeout: 90_000 }, () => {
it("counts a memory change once for each agent that changed a memory folder between two ends of a main run, however it wrote", async () => {
const script = (request: FakeRequest): FakeReply => {
const last = request.messages.at(-1)!;
const child = request.system.includes("You are a child agent of");
const child = isChild(request);
const memory = child ? /Contents of (\S+)[\\/]MEMORY\.md \(the memory of/.exec(request.system)![1]! : /Your memory is at `(\S+)\/`/.exec(request.system)?.[1];
const ran = request.messages.filter((message) => message.role === "tool").length;
if (child) return last.role === "user" ? { call: { name: "bash", arguments: { command: `echo '- [Bob](bob.md) — likes coffee' >> '${join(memory!, "MEMORY.md")}'` } } } : { text: "noted" };
Expand Down
Loading
Loading