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
22 changes: 14 additions & 8 deletions AGENTS.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ All notable changes to **tau** (`@ompweb-dev/tau`) are documented in this file.

### Fixed

- **A session id on two hosts runs as two sessions on the server.** Each host's copy has its own live process, exit mark and command queue; `POST`/`GET /api/agent/[id]?hostId=`, the `session` event channel's `hostId` and `POST /api/notifications/answer`'s `hostId` name one copy. A request without a host takes a live copy (this machine's first), else the host that lists the id.
- **The browser keeps each host's copy of a session apart.** Every agent command and state read names its host (`/api/agent/[id]?hostId=`, `local` for this machine); a chat follows only its own copy's run, and switching between two copies of an id is switching sessions. The sidebar's running, needs-input, stopped, finished and recently-completed marks belong to one copy, a notification names its host (`hostId` on the event) so viewing one copy does not silence the other and its click opens the right copy, and each copy has its own composer draft. Drafts, goals, sidebar marks and notification tags, history ids and presence all use one key, `<hostId>/<id>` (`local/<id>` for this machine); stored drafts, unread marks, recent runs and notification history keys change once (pre-alpha: no migration).
- **A `hostId` that is not a host id is refused.** `/api/agent/[id]`, `/api/sessions/[id]/*` and `POST /api/notifications/answer` answer 400 `invalid_host_id` for an empty value or one with characters other than letters, digits, `-` and `_`. The `running` event frame and `GET /api/sessions` no longer carry `runningSessionIds` (use `runningSessions`).
- **Held messages and GitHub watches keep each host's copy of a session apart.** Outbox entries record their host (`host_id` in `outbox.db`; `hostId` on `OutboxEntry` and browser entries, posted as `?hostId=`); an add naming no host whose host cannot be determined yet answers a retryable 503 instead of being stored. Pre-alpha: messages held from before this change are bound to this machine; one for a remote session is not shown in that session's chat and cannot be retried there, so send it again; delivery, claims, wakes, retry lineage, archive holds and deletes act on that copy only. GitHub watches, pending updates, read grants and Actions receipts are keyed by host and session; a GitHub watch store from before this change is dropped once (pre-alpha: watches must be set again).
- **In-app updates can replace an npm-global Windows install started by the tray.** The update stops the tray and any process still using that install and runs the install from outside the package, so Windows can rename it. (ompweb-dev/ompweb#519)
- **A host update's failed candidate is stopped on Windows even when it got the old daemon's pid.** (ompweb-dev/ompweb#526)
- **In-app updates work on a Windows install without administrator rights.** Task Scheduler's service host is recognized without reading its executable path. (ompweb-dev/ompweb#539)
Expand Down
6 changes: 4 additions & 2 deletions app/api/agent/[id]/bash-output/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apiErrorResponse, opErrorResponse, resolveWorkspaceHost } from "@/lib/api-utils";
import { apiErrorResponse, hostIdParam, opErrorResponse, resolveWorkspaceHost } from "@/lib/api-utils";
import { workspaceReadStream } from "@/lib/workspace-http";

export async function GET(
Expand All @@ -10,7 +10,9 @@ export async function GET(
const query = new URL(request.url).searchParams;
const path = query.get("path");
if (!path) return Response.json({ error: "path required" }, { status: 400 });
const resolved = await resolveWorkspaceHost(query.get("hostId") ?? undefined, id);
const named = hostIdParam(query.get("hostId"));
if ("response" in named) return named.response;
const resolved = await resolveWorkspaceHost(named.hostId ?? undefined, id);
if ("response" in resolved) return resolved.response;
const { host } = resolved;
if (query.get("download") === "1") {
Expand Down
18 changes: 13 additions & 5 deletions app/api/agent/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { getRpcSession, WebRpcError } from "@/lib/rpc-manager";
import { getLiveRpcSession, WebRpcError } from "@/lib/rpc-manager";
import { commandErrorResponse, runAgentCommand } from "@/lib/agent-command";
import { parseJsonWithinLimit } from "@/lib/bounded-form-data";
import { MAX_AGENT_COMMAND_REQUEST_BYTES } from "@/lib/image-attachments";
import { requestOwner } from "@/lib/web-auth";
import { hostIdParam } from "@/lib/api-utils";

/** Stamp when the handler had the request and when it answered, so the browser
* can split network time from server time. */
Expand All @@ -12,7 +13,7 @@ function timed(receivedAt: number, res: Response): Response {
return res;
}

// POST /api/agent/[id] - Send a command to an existing session
// POST /api/agent/[id]?hostId= - Send a command to an existing session (that host's copy when named)
export async function POST(
req: Request,
{ params }: { params: Promise<{ id: string }> }
Expand All @@ -25,7 +26,9 @@ export async function POST(
try {
const body = await parseJsonWithinLimit<unknown>(req, MAX_AGENT_COMMAND_REQUEST_BYTES);
receivedAt = Date.now();
return timed(receivedAt, await runAgentCommand(id, body, requestOwner(req)));
const host = hostIdParam(new URL(req.url).searchParams.get("hostId"));
if ("response" in host) return timed(receivedAt, host.response);
return timed(receivedAt, await runAgentCommand(id, body, requestOwner(req), { hostId: host.hostId }));
} catch (error) {
return timed(receivedAt ?? Date.now(), commandErrorResponse(error));
}
Expand All @@ -39,10 +42,15 @@ export async function GET(
const { id } = await params;
// Default stays the full shape; `?systemPrompt=0` drops the ~13 KB prompt
// from the 15 s state poll. Any other value (including "1") keeps it.
const includeSystemPrompt = new URL(req.url).searchParams.get("systemPrompt") !== "0";
const { searchParams } = new URL(req.url);
const includeSystemPrompt = searchParams.get("systemPrompt") !== "0";

const host = hostIdParam(searchParams.get("hostId"));
if ("response" in host) return host.response;
try {
const session = getRpcSession(id);
// `?hostId=`: only that host's copy; the same id live on another host is another session.
// Without one, a live copy: a dead wrapper on this machine must not hide a live one elsewhere.
const session = getLiveRpcSession(id, host.hostId);
if (!session || !session.isAlive()) {
return Response.json({ running: false });
}
Expand Down
9 changes: 7 additions & 2 deletions app/api/notifications/answer/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { hostIdParam } from "@/lib/api-utils";
import { commandErrorResponse } from "@/lib/agent-command";
import { parseJsonWithinLimit, RequestBodyTooLargeError } from "@/lib/bounded-form-data";
import type { ExtensionUiResponse, RpcAskDialogAnswer } from "@/lib/pi-types";
import { getRpcSession, isAskAnswers } from "@/lib/rpc-manager";
import { getLiveRpcSession, isAskAnswers } from "@/lib/rpc-manager";
import { isRecord } from "@/lib/type-guards";

/** Answers are text; 64 KB leaves room for a long ask customInput without becoming a payload channel. */
Expand Down Expand Up @@ -63,7 +64,11 @@ export async function POST(req: Request) {
const response = parseResponse(body.response);
if (!response) return invalid("response must be exactly one of value, confirmed, cancelled or answers");

const session = getRpcSession(body.sessionId);
const host = hostIdParam(body.hostId);
if ("response" in host) return host.response;
const { hostId } = host;
// `hostId`: only that host's copy of the id; without it, as before (this machine's copy first).
const session = getLiveRpcSession(body.sessionId, hostId);
if (!session || !session.isAlive()) return Response.json({ status: "sessionGone" });

try {
Expand Down
12 changes: 7 additions & 5 deletions app/api/sessions/[id]/archive/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apiErrorResponse, hostOfflineResponse, opErrorResponse, resolveSessionHostOr404, sessionCopiedElsewhere } from "@/lib/api-utils";
import { apiErrorResponse, hostIdParam, hostOfflineResponse, opErrorResponse, resolveSessionHostOr404 } from "@/lib/api-utils";
import { HostOpError } from "@/lib/hosts/link-protocol";
import { invalidateHostSessionList } from "@/lib/hosts/session-index";
import { clearExitedRpcSession, getRpcSession } from "@/lib/rpc-manager";
Expand All @@ -11,7 +11,9 @@ export async function POST(
) {
const { id } = await params;
try {
const resolved = await resolveSessionHostOr404(id, new URL(req.url).searchParams.get("hostId"));
const host = hostIdParam(new URL(req.url).searchParams.get("hostId"));
if ("response" in host) return host.response;
const resolved = await resolveSessionHostOr404(id, host.hostId);
if ("response" in resolved) return resolved.response;
const { sessionOps } = resolved.host;
// Parent-session paths are native branch metadata. Moving only a parent
Expand All @@ -31,12 +33,12 @@ export async function POST(
const offline = hostOfflineResponse(resolved.host);
if (offline) return offline;
// Only this host's child: the same id live on another host is another session.
const live = getRpcSession(id);
if (live?.host.id === resolved.host.id && !live.hostOwnedLifecycle) await live.destroyAndWait?.("archive", true);
const live = getRpcSession(id, resolved.host.id);
if (live && !live.hostOwnedLifecycle) await live.destroyAndWait?.("archive", true);
const result = await sessionOps.archiveSession(id);
if (!result.ok) return opErrorResponse(result);
invalidateHostSessionList(resolved.host.id, id);
if (!sessionCopiedElsewhere(id, resolved.host.id)) clearExitedRpcSession(id);
clearExitedRpcSession(id, resolved.host.id);
return Response.json({ ok: true, archived: true, archivedPath: result.value.archivedPath });
} catch (error) {
if (error instanceof HostOpError) return apiErrorResponse(error);
Expand Down
11 changes: 7 additions & 4 deletions app/api/sessions/[id]/auto-name/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { deriveSessionTitleFromFirstMessage, sanitizeSessionTitle } from "@/lib/
import { getRpcSession } from "@/lib/rpc-manager";
import { invalidateHostSessionList } from "@/lib/hosts/session-index";
import { HostOpError } from "@/lib/hosts/link-protocol";
import { apiErrorResponse, opErrorResponse, resolveSessionHostOr404 } from "@/lib/api-utils";
import { apiErrorResponse, hostIdParam, opErrorResponse, resolveSessionHostOr404 } from "@/lib/api-utils";

/**
* POST /api/sessions/[id]/auto-name
Expand All @@ -18,7 +18,9 @@ export async function POST(
const { id } = await params;

try {
const hostId = new URL(req.url).searchParams.get("hostId");
const host = hostIdParam(new URL(req.url).searchParams.get("hostId"));
if ("response" in host) return host.response;
const { hostId } = host;
const resolved = await resolveSessionHostOr404(id, hostId);
if ("response" in resolved) return resolved.response;
const { sessionOps } = resolved.host;
Expand All @@ -38,12 +40,13 @@ export async function POST(
}

// The session list read yielded; Edit may now own this session.
if (getRpcSession(id, hostId)?.isEditingHistory()) {
if (getRpcSession(id, resolved.host.id)?.isEditingHistory()) {
return Response.json({ error: "Wait for the history edit to finish", code: "session_busy" }, { status: 409 });
}

// A live pi holds the session: name it there. Otherwise append to the file.
const rpc = getRpcSession(id, hostId);
// The resolved host's copy: another host's live copy of the id is another session.
const rpc = getRpcSession(id, resolved.host.id);
let named = false;
if (rpc?.isAlive?.()) {
try {
Expand Down
12 changes: 7 additions & 5 deletions app/api/sessions/[id]/context/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { buildSessionContext } from "@/lib/session-reader";
import { apiErrorResponse, ifNoneMatchSatisfied, opErrorResponse, resolveSessionHostOr404, strongEtag } from "@/lib/api-utils";
import { getRpcSession } from "@/lib/rpc-manager";
import { apiErrorResponse, hostIdParam, ifNoneMatchSatisfied, opErrorResponse, resolveSessionHostOr404, strongEtag } from "@/lib/api-utils";
import { getLiveRpcSession } from "@/lib/rpc-manager";
import { MAX_SYNC_MESSAGES, parseHistoryCursor, selectSessionHistory, type SessionHistoryCursor, type SessionHistoryPage, type SessionSyncResponse } from "@/lib/session-sync";

/**
Expand Down Expand Up @@ -29,7 +29,9 @@ export async function GET(
const { id } = await params;
const url = new URL(req.url);
const leafId = url.searchParams.get("leafId") ?? undefined;
const hostId = url.searchParams.get("hostId");
const host = hostIdParam(url.searchParams.get("hostId"));
if ("response" in host) return host.response;
const { hostId } = host;
const deferThinking = url.searchParams.has("deferThinking");
const deferToolResultImages = url.searchParams.has("deferMedia");
// Read-only transcript mode: include entries omitted from the active agent context.
Expand Down Expand Up @@ -80,7 +82,7 @@ export async function GET(
// and its persisted boundary is empty (the next prompt must not 404).
// Never replace previously confirmed history when its file is missing.
const notFound = "response" in result ? result.response.status === 404 : result.status === 404;
const session = notFound && (boundary === "1" || (sync === "1" && !cursor?.lastEntryId && leafId === undefined)) ? getRpcSession(id, hostId) : undefined;
const session = notFound && (boundary === "1" || (sync === "1" && !cursor?.lastEntryId && leafId === undefined)) ? getLiveRpcSession(id, "host" in resolved ? resolved.host.id : hostId) : undefined;
if (session?.isAlive() && !(await session.host.sessionFileExists(session.sessionFile))) {
if (boundary === "1") {
return jsonBodyResponse(req, { entryIds: [] }, "no-store");
Expand All @@ -102,7 +104,7 @@ export async function GET(
const page = result.value as { history: SessionHistoryPage; leafId: string | null; tipId: string | null };
// Sample AFTER the disk read: a quiet active run still supplies its most
// recent partial output, and events arriving during the read win.
const session = getRpcSession(id, hostId);
const session = "host" in resolved ? getLiveRpcSession(id, resolved.host.id) : undefined;
const live = session?.isAlive() && (leafId === undefined || leafId === page.tipId)
? session.getStreamSnapshot()
: null;
Expand Down
6 changes: 4 additions & 2 deletions app/api/sessions/[id]/entries/[entryId]/thinking/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apiErrorResponse, opErrorResponse, resolveSessionHostOr404 } from "@/lib/api-utils";
import { apiErrorResponse, hostIdParam, opErrorResponse, resolveSessionHostOr404 } from "@/lib/api-utils";

export async function GET(
req: Request,
Expand All @@ -13,7 +13,9 @@ export async function GET(
}

try {
const resolved = await resolveSessionHostOr404(id, query.get("hostId"));
const host = hostIdParam(query.get("hostId"));
if ("response" in host) return host.response;
const resolved = await resolveSessionHostOr404(id, host.hostId);
if ("response" in resolved) return resolved.response;
const result = await resolved.host.sessionOps.readThinking(id, entryId, blockIndex);
return result.ok ? Response.json(result.value) : opErrorResponse(result);
Expand Down
6 changes: 4 additions & 2 deletions app/api/sessions/[id]/export/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apiErrorResponse, opErrorResponse, resolveSessionHostOr404 } from "@/lib/api-utils";
import { apiErrorResponse, hostIdParam, opErrorResponse, resolveSessionHostOr404 } from "@/lib/api-utils";
import { getContentDisposition } from "@/lib/content-disposition";
import { LOCAL_HOST_ID } from "@/lib/hosts/registry";

Expand All @@ -12,7 +12,9 @@ export async function GET(
const inline = query.get("inline") === "1";

try {
const resolved = await resolveSessionHostOr404(id, query.get("hostId"));
const host = hostIdParam(query.get("hostId"));
if ("response" in host) return host.response;
const resolved = await resolveSessionHostOr404(id, host.hostId);
if ("response" in resolved) return resolved.response;
const result = await resolved.host.sessionOps.exportSession(id);
if (!result.ok) return opErrorResponse(result);
Expand Down
6 changes: 4 additions & 2 deletions app/api/sessions/[id]/matches/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apiErrorResponse, opErrorResponse, resolveSessionHostOr404 } from "@/lib/api-utils";
import { apiErrorResponse, hostIdParam, opErrorResponse, resolveSessionHostOr404 } from "@/lib/api-utils";
import { listSessionMatches } from "@/lib/session-match-listing";
import { MAX_SEARCH_MATCHES, SEARCH_MATCH_PAGE_SIZE } from "@/lib/session-search-matches";
import type { SessionContext } from "@/lib/types";
Expand All @@ -23,7 +23,9 @@ export async function GET(req: Request, { params }: { params: Promise<{ id: stri
return Response.json({ error: "Invalid limit", code: "invalid_limit" }, { status: 400 });
}
try {
const resolved = await resolveSessionHostOr404(id, url.searchParams.get("hostId"));
const host = hostIdParam(url.searchParams.get("hostId"));
if ("response" in host) return host.response;
const resolved = await resolveSessionHostOr404(id, host.hostId);
if ("response" in resolved) return resolved.response;
const result = await resolved.host.sessionOps.readContext(id, undefined, {
deferThinking: true,
Expand Down
Loading
Loading