Skip to content

feat(server): add HTTP session launch and turn polling - #246

Open
jfinnson wants to merge 1 commit into
TangleML:mainfrom
jfinnson:feat/http-session-launches
Open

jfinnson wants to merge 1 commit into
TangleML:mainfrom
jfinnson:feat/http-session-launches

Conversation

@jfinnson

@jfinnson jfinnson commented Dec 5, 2026

Copy link
Copy Markdown

Description

Adds a request/response way to start a session and read Prime's reply, for clients that cannot hold a Socket.IO connection (for example a browser page that reaches the Shell through a buffered HTTP proxy).

POST /api/session-launches with { bundleId, prompt }

  • Creates a session from a saved bundle, owned by the caller, through the existing session-create helpers, and posts prompt into Prime's primary conversation through the same path as chat:message.
  • The session is restricted from the moment it is created, so no other user can chat:join it before the caller's access settings apply.
  • Answers 201 { sessionId, conversationId, turnId, sessionUrl }. turnId is the prompt's Message id. sessionUrl uses TANGENT_PUBLIC_URL when set, otherwise the request origin.
  • A blank or oversized prompt or an unknown bundle is rejected; a Prime that is not a local Pi is refused with 409 and the session is cleaned up.

GET /api/sessions/:id/turns/:turnId?wait=N

  • Answers { status, done, reply, error }, where status is running, completed (with Prime's reply) or failed (with error).
  • wait is an integer from 0 to 25 seconds (default 0) and holds the request until the turn settles. 25 seconds keeps a long poll under common 30–60 second proxy timeouts.
  • Uses the same conversation-read check as chat:join: 401 without identity, 403 when the caller cannot read Prime's thread, 404 for an unknown, expired, or other-session turn. A poll whose client disconnects during authorization is released.

How a reply is attributed to the turn

TurnTracker (apps/server/src/conversation/turnTracker.ts) follows the exact launch Message id from delivery (DeliveryRequest.messageId) to the Pi prompt and listens for PiAgentManager's placement of that prompt (usePromptPlacement):

  • The Run opened on delivery is provisional: extension commands and input handlers run before Pi answers the prompt, so output in that window is held, never returned.
  • Pi's started or queued answer confirms the Run; only output after confirmation can become the reply.
  • handled, a rejection, a missing disposition, a cleared queue, or the process dying before it answers fails the turn.
  • Pi gives no boundary between prompts that share a Run, so the turn fails with reply: null when its prompt is queued behind other work, when any other prompt reaches the same Prime while the turn is pending, or when its Run is superseded. An exact resend of the launch's own Message id is not a competitor. The underlying Run and UI carry on unchanged; only the HTTP turn settles.

When the tracker cannot tell whose answer it is, it fails the turn rather than return another prompt's reply.

Other changes

  • Pi stdout from a replaced (obsolete) Prime process is ignored before dispatch, so a replacement Run never picks up the old process's reply.
  • Session teardown runs each cleanup step independently (Promise.allSettled), so a failed database delete no longer skips agent disposal.
  • Shared request/response types in packages/shared/src/contracts.ts, and a "Launching a session over HTTP" section in docs/server/index.md.

Screenshots

N/A — server API and documentation only.

How to test

Prerequisites: pnpm 10.28.0 (packageManager).

  1. pnpm install --frozen-lockfile
  2. pnpm --filter @tangent/server test — 972/972 pass on this branch (rebased on main at 78d3283).
  3. pnpm validate — 33/33 tasks pass; lint reports 0 errors and 5 existing max-lines-per-function warnings in files this change does not add.
  4. Focused suites: apps/server/src/routes/sessionLaunches.test.ts, apps/server/src/conversation/turnTracker.test.ts, apps/server/src/conversation/turnTracker.pi.test.ts, and the placement cases in apps/server/src/pi/piAgentManager.test.ts.
  5. Live check against a local server with a saved bundle whose Prime is a local Pi:
    curl -sS -X POST "$SHELL/api/session-launches" -H "Authorization: Bearer $JWT" \
      -H 'Content-Type: application/json' \
      -d '{"bundleId":"<installed-bundle-id>","prompt":"Reply with a short greeting."}'
    # → 201 { sessionId, conversationId, turnId, sessionUrl }
    curl -sS "$SHELL/api/sessions/<sessionId>/turns/<turnId>?wait=25" -H "Authorization: Bearer $JWT"
    # repeat until done: true → status completed with reply, or failed with error
    Then confirm: no token → 401; another user on the restricted session → 403; unknown or cross-session turn → 404; wait=26 or wait=abc → 400; wait=0 returns at once.

Notes

  • Draft for review. The live smoke test ran against Pi 1.7. The production Dockerfiles pin Pi 1.6; its prompt-placement protocol is covered by FakeChild tests, not a live Pi 1.6 process, so a check on the deployed image is still needed.
  • Turn state is in memory and per process: a settled turn is readable for one hour, a turn whose Run never settles fails after six hours, and a restart returns 404 for every turn (the session and transcript remain). With several replicas, polls must reach the process that created the turn.
  • Only a local Pi Prime reports prompt placement, so other Prime connectors get 409.
  • A client that disconnects mid-POST still gets its session; there is no idempotency key, so a blind retry creates a second session.
  • Authentication is the existing ingress/JWT handling; this change adds no new trust boundary.
  • A browser calling from another origin needs that origin allowed by EMBED_ALLOWED_ORIGINS and by its sign-in provider; that configuration is outside this change.

Co-authored-by: AI (Pi/Claude Opus 5.5) <noreply@pi.dev>
@jfinnson
jfinnson marked this pull request as ready for review December 5, 2026 01:06
@jfinnson
jfinnson requested a review from a team as a code owner December 5, 2026 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant