Files
loki/scripts/test/session-open-url.ts
CatoandClaude Opus 5.5 12e0357d8f feat(sessions): starting a session answers with where a person opens it (#1657)
* feat(sessions): starting a session answers with where a person opens it

An agent that starts Claude Code in a Loki session hands the owner the
session to look at, on a phone or a computer. The Terminal's address was
built only inside the app's own cards, so a caller had to guess it.

- lib/dispatch-card sessionOpenUrl: the full link to the session in Loki's
  Terminal, on that run when there is one, This computer for a local one
- tab-inject (live, dispatch, queued), /api/inject and MCP loki_dispatch
  (through inject-core, a parallel lane included) and agent/launch return
  openUrl
- LOOP-WALK's Session walk says so; scripts/test/session-open-url pins it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtCjm3CvrFm3kYaTgy

* fix(sessions): a session's link names its builder

A cloud openUrl carried no source, so the Terminal opened whatever source
that browser last chose: a phone that last picked 'Your computer' opened the
laptop's session of the same name instead of the cloud one the agent drove
(traced 2026-11-12). sessionOpenUrl now says source=cloud for the cloud
builder, and the screen capture's openUrl (POST /api/control/peek-tab) is
the same full link instead of a bare path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtCjm3CvrFm3kYaTgy

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 08:46:37 +01:00

58 lines
2.2 KiB
TypeScript

/**
* Starting a session answers with where a person opens it.
*
* An agent that starts Claude Code in a Loki session (tab-inject, /api/inject,
* MCP loki_dispatch, agent/launch) hands the owner the session to look at. The
* Terminal's URL was built only inside the app's own cards, so a caller had to
* guess it. Every start now returns `openUrl`, built by the same helper.
*/
import assert from "node:assert/strict";
import fs from "node:fs";
import { sessionOpenUrl, terminalHref } from "../../src/lib/dispatch-card";
import { APP_URL } from "../../src/config/brand";
const read = (p: string) => fs.readFileSync(p, "utf8");
const run = "11111111-1111-4111-8111-111111111111";
assert.equal(
sessionOpenUrl("loki", "cloud"),
`${APP_URL}/terminal?source=cloud&project=loki`,
"a cloud link names its builder, so a phone that last chose its computer still opens the cloud session",
);
assert.equal(sessionOpenUrl("loki", null, run), `${APP_URL}/terminal?project=loki&run=${run}`);
assert.equal(
sessionOpenUrl("site", "local", run),
`${APP_URL}/terminal?source=machine&tab=site&run=${run}`,
"a session on the owner's computer opens on This computer",
);
assert.equal(
sessionOpenUrl("site~1a2b3c4d", "cloud"),
`${APP_URL}/terminal?source=cloud&project=${encodeURIComponent("site~1a2b3c4d")}`,
"a parallel lane opens as its own tab",
);
assert.equal(
sessionOpenUrl("site", null),
`${APP_URL}${terminalHref("site", null)}`,
"no builder known: the Terminal's own choice",
);
assert.ok(APP_URL.startsWith("https://"), "a link a person can open");
const inject = read("src/app/api/control/tab-inject/route.ts");
assert.equal(
(inject.match(/openUrl: sessionOpenUrl\(/g) ?? []).length,
3,
"tab-inject: the live, dispatch and queued answers all carry openUrl",
);
assert.match(
read("src/lib/inject-core.ts"),
/openUrl: sessionOpenUrl\(queued\?\.parallelTab \?\? effectiveTab, channel, runId \?\? null\)/,
"/api/inject and MCP loki_dispatch: the session it went to, lane included",
);
assert.equal(
(read("src/app/api/agent/launch/route.ts").match(/openUrl: sessionOpenUrl\(/g) ?? []).length,
2,
"agent/launch: queued and direct",
);
console.log("session-open-url ok");