diff --git a/apps/api/test/fixtures/go-server.ts b/apps/api/test/fixtures/go-server.ts index 5200316..1a28019 100644 --- a/apps/api/test/fixtures/go-server.ts +++ b/apps/api/test/fixtures/go-server.ts @@ -41,7 +41,10 @@ type Reply = { let scripted: Reply[] = []; let gate: PromiseWithResolvers | undefined; let toolGate: PromiseWithResolvers | undefined; -const modelStarted = Promise.withResolvers(); +// True while a fixture tool call that already waited at /tool still owes its +// tool result to a model request, which then must not take the next gate. +let toolReported = false; +let modelStarted = Promise.withResolvers(); function lastUserText(body: { messages?: { role: string; content: unknown }[]; @@ -139,14 +142,24 @@ async function complete( const model = String(body.model ?? ""); const aborted = new AbortController(); response.on("close", () => aborted.abort()); - // Pi runs tools itself, so a gated tool holds the request that reports its result. + // Pi runs tools itself, so a gated tool holds the request that reports its + // result — except the fixture tool, which already waits at /tool. A gate + // armed for the next tool call must not be stolen by the request reporting + // a tool that was already held. const afterTool = (body.messages as { role: string }[] | undefined)?.at(-1)?.role === "tool"; const next = scripted.shift(); - // Like web-server.ts's faux answer factory, gate holds only the unscripted answer. - const waiting = afterTool && toolGate ? toolGate : next ? undefined : gate; - if (waiting === toolGate) toolGate = undefined; - else if (waiting) gate = undefined; + let waiting: PromiseWithResolvers | undefined; + if (afterTool && toolReported) { + toolReported = false; + waiting = next ? undefined : gate; + } else if (afterTool && toolGate) { + waiting = toolGate; + toolGate = undefined; + } else { + waiting = next ? undefined : gate; + } + if (waiting && waiting === gate) gate = undefined; if (waiting) { aborted.signal.addEventListener("abort", () => waiting.resolve(), { once: true, @@ -228,6 +241,21 @@ async function complete( response.end(); } +// The fixture tool's Pi extension asks here before it returns, so a gated +// tool stays running the way web-server.ts's test tool does. +async function runTool(response: ServerResponse) { + // Each "gate-tool" arms one hold for the next tool call. Mark the call's + // result outstanding so the request that reports it skips the tool gate. + const waiting = toolGate; + toolGate = undefined; + toolReported = true; + if (waiting) { + response.on("close", () => waiting.resolve()); + await waiting.promise; + } + response.end(); +} + const ollama = createServer((request, response) => { let raw = ""; request.on("data", (part) => { @@ -252,6 +280,8 @@ const ollama = createServer((request, response) => { }); if (request.url === "/v1/chat/completions" && request.method === "POST") return void complete(JSON.parse(raw || "{}"), response); + if (request.url === "/tool" && request.method === "POST") + return void runTool(response); response.statusCode = 404; response.end(); }); @@ -305,6 +335,32 @@ await writeFile( }, }), ); +// Pi loads extensions from its agent directory. This one stands in for +// web-server.ts's test tool: it returns the supplied text after the fixture +// releases a gated tool. +const toolName = "fixture_echo"; +await mkdir(join(agentDir, "extensions"), { recursive: true }); +await writeFile( + join(agentDir, "extensions", "fixture-tool.js"), + `export default function (pi) { + pi.registerTool({ + name: ${JSON.stringify(toolName)}, + label: ${JSON.stringify(toolName)}, + description: "Return the supplied text.", + parameters: { + type: "object", + properties: { text: { type: "string" }, fail: { type: "boolean" } }, + required: ["text"], + }, + async execute(_id, params, signal) { + await fetch(${JSON.stringify(`http://127.0.0.1:${ollamaPort}/tool`)}, { method: "POST", signal }); + if (params.fail) throw new Error(params.text); + return { content: [{ type: "text", text: params.text }], details: {} }; + }, + }); +} +`, +); const bin = join(root, "packages", "agent", "node_modules", ".bin"); const dataDir = join(directory, "go-data"); // web-server.ts's "hosting" mode: a hosting config puts serve behind device @@ -335,9 +391,12 @@ const args = [ "-pi-extension", join(root, "packages", "agent", "src", "harness", "pi-tools.mjs"), // serve always runs Pi with --no-extensions, so settings.json extensions - // never load; the faux failure stream must be an explicit extension. + // never load; the faux failure stream and the fixture tool must be explicit + // extensions. "-pi-extension", fileURLToPath(new URL("./pi-faux-failure.mjs", import.meta.url)), + "-pi-extension", + join(agentDir, "extensions", "fixture-tool.js"), ]; const options = { // Outside the repository so persona.config.json does not select hosted mode. @@ -415,8 +474,7 @@ const pairing = hosting } ).code : undefined; -// Pi's own ls stands in for web-server.ts's gated test tool. -process.send?.({ type: "ready", port, toolName: "ls", pairing }); +process.send?.({ type: "ready", port, toolName, pairing }); async function hosted(path: string, json: unknown) { const response = await fetch(`${origin}/hosting/${path}`, { @@ -491,6 +549,8 @@ async function configure() { let release: (() => void) | undefined; process.on("message", async (message) => { if (message === "gate") { + // wait-model then waits for the request this gate holds. + modelStarted = Promise.withResolvers(); gate = Promise.withResolvers(); release = gate.resolve; process.send?.({ type: "gated" }); diff --git a/apps/web/test/go-gaps.ts b/apps/web/test/go-gaps.ts index fc8cdc2..89efd2f 100644 --- a/apps/web/test/go-gaps.ts +++ b/apps/web/test/go-gaps.ts @@ -2,23 +2,22 @@ // playwright.config.ts leaves them out when PERSONA_WEB_SERVER=go. Remove a // title when its gap closes. export const goGaps: Record = { - // Waits on the worker's prepare/install/mode/settle/view port (another session). - "computer viewer: the Go worker does not yet stream the work computer's screen or switch browser mode": - [ - "a failed browser input pauses the viewer and reconnect never replays it", - "computer viewing uses separate tabs and releases control when closed", - "continuous wheel input keeps takeover active", - "takeover card first click opens the browser and resumes after completion", - "takeover controls the Agent browser and preserves its layout through fullscreen", - ], - "chat steering: the Go fixture holds the model request after ls, not the tool, so input steered while it waits joins a later turn": - ["chat keeps sending available while the Bot is working"], - "native questions: Pi's ask_user is not stored as a shared question card": [ - "native questions use the shared card and native replies persist", - "shared question and approval cards persist answers and terminal states", + // These drive a real browser page: the fixture needs the takeover page, + // state and metrics endpoints on the service origin, scripted browser tool + // calls (web-server.ts "takeover" mode) and an open-takeover-page command + // that reaches the Bot's agent-browser — none of which exist on the Go + // fixture yet. + "computer viewer: the Go fixture cannot script the takeover browser flow": [ + "a failed browser input pauses the viewer and reconnect never replays it", + "continuous wheel input keeps takeover active", + "takeover card first click opens the browser and resumes after completion", + "takeover controls the Agent browser and preserves its layout through fullscreen", ], - "native Work records: work.records does not yet project native turns": [ - "Work follows a native turn, opens its record and preserves it after stop and reload", + // The spec injects approval requests through the TypeScript runtime; the Go + // approver keeps pending requests in memory, so the fixture cannot open one + // over the wire. + "shared approval cards: the Go fixture cannot open an approval request": [ + "shared question and approval cards persist answers and terminal states", ], }; diff --git a/internal/computers/surface.go b/internal/computers/surface.go index b35b51e..54bffa8 100644 --- a/internal/computers/surface.go +++ b/internal/computers/surface.go @@ -384,7 +384,8 @@ func (c *Catalog) Watch(ctx context.Context, botID int64, service string, emit f } } -// Input sends a viewer action (computer.input), views.ts action(). +// Input sends a viewer action (computer.input), views.ts action(), and +// reports whether the viewer holds control, as views.ts returns. func (c *Catalog) Input(ctx context.Context, botID int64, view string, action json.RawMessage) (any, error) { session := c.vw.get(view) if session == nil { @@ -396,7 +397,10 @@ func (c *Catalog) Input(ctx context.Context, botID int64, view string, action js if err := c.vw.action(ctx, view, action); err != nil { return nil, err } - return map[string]any{"ok": true}, nil + session.mu.Lock() + controlling := session.lease != 0 + session.mu.Unlock() + return map[string]any{"controlling": controlling}, nil } // AssistantKey stores the provider key (computer.assistantKey).