diff --git a/.github/workflows/omp-runner.yml b/.github/workflows/omp-runner.yml deleted file mode 100644 index 56c17e663..000000000 --- a/.github/workflows/omp-runner.yml +++ /dev/null @@ -1,97 +0,0 @@ -name: omp runner binaries - -on: - push: - branches: - - main - pull_request: - paths: - - "runner/**" - - ".github/workflows/omp-runner.yml" - workflow_dispatch: - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -permissions: - contents: read - -jobs: - test: - name: Typecheck and test - runs-on: ubuntu-latest - defaults: - run: - working-directory: runner - steps: - - name: Checkout repository - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 - - - name: Setup Bun - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 - with: - bun-version: 1.4.4 - - - name: Fetch the pinned omp packages - run: bun scripts/use-omp-build.ts - - - name: Install dependencies - run: bun install --frozen-lockfile - - - name: Typecheck - run: bunx tsc -p . - - - name: Test - run: bun test - - build: - name: Build (${{ matrix.target }}) - # Bun cross-compiles every target from Linux, as omp's own release does. - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - include: - - { target: linux-x64, os: linux, cpu: x64, binary: ompweb-omp-linux-x64 } - - { target: linux-arm64, os: linux, cpu: arm64, binary: ompweb-omp-linux-arm64 } - - { target: darwin-x64, os: darwin, cpu: x64, binary: ompweb-omp-darwin-x64 } - - { target: darwin-arm64, os: darwin, cpu: arm64, binary: ompweb-omp-darwin-arm64 } - - { target: windows-x64, os: win32, cpu: x64, binary: ompweb-omp-windows-x64.exe } - defaults: - run: - working-directory: runner - - steps: - - name: Checkout repository - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 - - - name: Setup Bun - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 - with: - bun-version: 1.4.4 - - - name: Fetch the pinned omp packages - run: bun scripts/use-omp-build.ts - - # The host's own addon package is not needed; the target's is what gets embedded. - - name: Install dependencies - run: bun install --frozen-lockfile --os=${{ matrix.os }} --cpu=${{ matrix.cpu }} - - - name: Build binary - run: bun scripts/build-binary.ts ${{ matrix.target }} - - - name: Smoke test - if: matrix.target == 'linux-x64' - run: | - set -euo pipefail - runtime_dir="$(mktemp -d)" - HOME="$runtime_dir/home" XDG_DATA_HOME="$runtime_dir/xdg" release/${{ matrix.binary }} --version - HOME="$runtime_dir/home" XDG_DATA_HOME="$runtime_dir/xdg" release/${{ matrix.binary }} --smoke-test - - - name: Upload binary - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: ${{ matrix.binary }} - path: runner/release/${{ matrix.binary }} - if-no-files-found: error diff --git a/.github/workflows/prerelease.yml b/.github/workflows/prerelease.yml index 13b2a4ab0..5c9555693 100644 --- a/.github/workflows/prerelease.yml +++ b/.github/workflows/prerelease.yml @@ -39,25 +39,18 @@ jobs: cache: npm - run: npm ci - - run: npm run build - # The omp the runners compile against: the fork's newest integration build, not runner/omp-build.json's pin - # (not committed back). No such release or a runner that fails tsc against it fails the release. - - name: Find the newest omp integration build + # The omp runner every binary bundles and records: the newest published release of the fork (ompweb-dev/oh-my-pi, + # which builds and releases it) that attaches every platform's runner and whose ompweb-omp.json names this ompweb's + # RUNNER_PROTOCOL, written into omp-runner.json here (the binaries bake it in; not committed back). None fails the + # release. The build below downloads each platform's runner (SHA-256-checked) from it. + - name: Pin the newest omp runner of the fork env: GH_TOKEN: ${{ github.token }} run: | set -euo pipefail - sha=$(gh api repos/ompweb-dev/oh-my-pi/commits/integration-release --jq .sha) - tag=$(gh api --paginate 'repos/ompweb-dev/oh-my-pi/releases?per_page=100' --jq '.[].tag_name' \ - | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+-int\.[0-9]{12}\.g${sha:0:7}\$" | head -n 1 || true) - if [ -z "$tag" ]; then - echo "::error::ompweb-dev/oh-my-pi has no integration release for integration-release ($sha)" - exit 1 - fi - echo "OMP_TAG=$tag" >> "$GITHUB_ENV" - # Without GH_TOKEN: bun install runs the omp packages' scripts. - - name: Pin the runner to it - run: npx bun runner/scripts/use-omp-build.ts "$OMP_TAG" && (cd runner && npx bun x tsc -p .) + npx bun scripts/fetch-runner.ts --latest linux-x64 + echo "OMP_TAG=$(node -p 'require("./omp-runner.json").tag')" >> "$GITHUB_ENV" + - run: npm run build - name: Pin the binary release identity run: | now=$(date -u +%s) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 1fb840beb..79c040be8 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -34,25 +34,17 @@ jobs: - run: npm install --global npm@^11.5.1 - run: npm ci - # The omp the runners compile against: the fork's newest integration build, not runner/omp-build.json's pin - # (not committed back). No such release or a runner that fails tsc against it fails the release. Pinned before - # the tests: they import the runner (lib/hosts/omp-socket.test.mjs), and must run against the omp that ships. - - name: Find the newest omp integration build + # The omp runner this release ships against and records: the newest published release of the fork (ompweb-dev/oh-my-pi, + # which builds and releases it) that attaches every platform's runner and whose ompweb-omp.json names this ompweb's + # RUNNER_PROTOCOL, written into omp-runner.json here (the npm package and the binaries bake it in; not committed back). + # None fails the release. Pinned before the tests and builds so everything runs against the runner that ships. + - name: Pin the newest omp runner of the fork env: GH_TOKEN: ${{ github.token }} run: | set -euo pipefail - sha=$(gh api repos/ompweb-dev/oh-my-pi/commits/integration-release --jq .sha) - tag=$(gh api --paginate 'repos/ompweb-dev/oh-my-pi/releases?per_page=100' --jq '.[].tag_name' \ - | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+-int\.[0-9]{12}\.g${sha:0:7}\$" | head -n 1 || true) - if [ -z "$tag" ]; then - echo "::error::ompweb-dev/oh-my-pi has no integration release for integration-release ($sha)" - exit 1 - fi - echo "OMP_TAG=$tag" >> "$GITHUB_ENV" - # Without GH_TOKEN: bun install runs the omp packages' scripts. - - name: Pin the runner to it - run: npx bun runner/scripts/use-omp-build.ts "$OMP_TAG" && (cd runner && npx bun x tsc -p .) + npx bun scripts/fetch-runner.ts --latest linux-x64 + echo "OMP_TAG=$(node -p 'require("./omp-runner.json").tag')" >> "$GITHUB_ENV" # Match Verify PR: allow isolated fixtures, never control of the runner's service. - name: Allow isolated native fixture scopes run: | @@ -61,13 +53,11 @@ jobs: systemd-run --scope --quiet --collect --no-ask-password true - run: npm test - run: npm run build - # Builds the omp runners too, which the source smoke spawns from runner/release/. The archives leave them out: - # every install fetches its runner from this release (lib/omp/runner-fetch.ts), attached below. + # The archives leave the runner out: every install fetches it from the pinned fork release (lib/omp/runner-fetch.ts). - name: Build the release binaries run: sh scripts/build-release-binaries.sh --no-runner - run: npm run smoke - # --omp: the draft release cannot serve the runner to the binary yet. - - run: npm run smoke -- --binary release/ompweb-linux-x64/ompweb --omp runner/release/ompweb-omp-linux-x64 + - run: npm run smoke -- --binary release/ompweb-linux-x64/ompweb - name: Create draft GitHub Release env: @@ -79,18 +69,12 @@ jobs: gh release create "$TAG" --repo "$GITHUB_REPOSITORY" --verify-tag --title "$TAG" --notes-file release-notes.md --draft fi - # The runners, by the names and checksum listing lib/omp/runner-fetch.ts fetches (OMP_RUNNER_ASSET, - # OMP_RUNNER_CHECKSUMS), for binary and npm installs alike. - - name: Attach the binaries and the omp runners to the draft release + - name: Attach the binaries to the draft release env: GH_TOKEN: ${{ github.token }} - run: | - set -euo pipefail - runners=(ompweb-omp-linux-x64 ompweb-omp-linux-arm64 ompweb-omp-darwin-x64 ompweb-omp-darwin-arm64 ompweb-omp-windows-x64.exe) - (cd runner/release && sha256sum "${runners[@]}" > ompweb-omp.sha256) - gh release upload "$TAG" release/ompweb-*.tar.gz release/ompweb-*.zip "${runners[@]/#/runner/release/}" runner/release/ompweb-omp.sha256 --repo "$GITHUB_REPOSITORY" --clobber + run: gh release upload "$TAG" release/ompweb-*.tar.gz release/ompweb-*.zip --repo "$GITHUB_REPOSITORY" --clobber - # Before npm: an npm install fetches its runner from this release, which a draft does not serve. + # Before npm: a draft serves no archives, which the in-app update notice and host updates download. - name: Publish GitHub Release env: GH_TOKEN: ${{ github.token }} diff --git a/.github/workflows/verify-pr.yml b/.github/workflows/verify-pr.yml index 168ac2c75..8cb1c0e28 100644 --- a/.github/workflows/verify-pr.yml +++ b/.github/workflows/verify-pr.yml @@ -46,12 +46,8 @@ jobs: node-version-file: .nvmrc cache: npm - # The runner's own deps too: lib/hosts/omp-socket.test.mjs checks the host against runner/src/channel.ts. - name: Install dependencies - run: | - npm ci - npx bun runner/scripts/use-omp-build.ts - npx bun install --cwd runner --frozen-lockfile + run: npm ci - name: TypeScript typecheck run: npm run typecheck @@ -86,8 +82,10 @@ jobs: - name: Verify production build run: npm run build - - name: Build the omp runner - run: npx bun runner/scripts/build-binary.ts + # The omp runner is built and released by the oh-my-pi fork: this downloads the release omp-runner.json pins + # (SHA-256-checked) into release/runners/, where the smoke tests, the binary build and the upgrade test find it. + - name: Fetch the pinned omp runner + run: npx bun scripts/fetch-runner.ts - name: Smoke test run: npm run smoke @@ -127,7 +125,8 @@ jobs: env: OMPWEB_COMPILED_UPGRADE_E2E: "1" run: | - export OMPWEB_UPGRADE_E2E_RUNNER="$(node -p "require('node:path').resolve('runner', 'release', 'ompweb-omp-' + (process.platform === 'win32' ? 'windows' : process.platform) + '-' + process.arch + (process.platform === 'win32' ? '.exe' : ''))")" + OMPWEB_UPGRADE_E2E_RUNNER="$(npx bun scripts/fetch-runner.ts)" + export OMPWEB_UPGRADE_E2E_RUNNER npx bun test --parallel --timeout 900000 lib/compiled-upgrade.integration.test.mjs - name: Packed install smoke test @@ -135,14 +134,14 @@ jobs: run: | npm pack --ignore-scripts npm install -g --prefix "$RUNNER_TEMP/ompweb-prefix" ./kahme247-ompweb-*.tgz - npm run smoke -- --omp "$(ls runner/release/ompweb-omp-*)" --package "$(npm root -g --prefix "$RUNNER_TEMP/ompweb-prefix")/@kahme247/ompweb" + npm run smoke -- --package "$(npm root -g --prefix "$RUNNER_TEMP/ompweb-prefix")/@kahme247/ompweb" # Bun's postinstall does not run here: the launcher must fall back to @oven/bun-- (spec R1). - name: Packed install smoke test (install scripts disabled) shell: bash run: | npm install -g --ignore-scripts --prefix "$RUNNER_TEMP/ompweb-prefix-no-scripts" ./kahme247-ompweb-*.tgz - npm run smoke -- --omp "$(ls runner/release/ompweb-omp-*)" --package "$(npm root -g --prefix "$RUNNER_TEMP/ompweb-prefix-no-scripts")/@kahme247/ompweb" + npm run smoke -- --package "$(npm root -g --prefix "$RUNNER_TEMP/ompweb-prefix-no-scripts")/@kahme247/ompweb" - name: Parse Windows PowerShell scripts (Windows only) if: runner.os == 'Windows' diff --git a/AGENTS.md b/AGENTS.md index 5ec4b9461..233249467 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -9,14 +9,15 @@ npm start # serve dist/ + the API, port 30177 npm test # bun test --parallel npm run smoke # start the built server and `ompweb host` (or a packed install: -- --package ; a binary: -- --binary ) and check them npm run build:binary [-- --target bun--] # build, then compile release/ompweb--/ and its .tar.gz/.zip (scripts/build-binary.ts) +npm run fetch-omp # download the pinned omp runner (live-agent features need it) npm run typecheck && npm run lint ``` -The dev server needs ompweb's omp build (`runner/`). Fetch the pinned omp packages first, then install -(`npm test` needs this too): -`bun runner/scripts/use-omp-build.ts && cd runner && bun install --frozen-lockfile`; then build -`runner/release/ompweb-omp--` (`bun runner/scripts/build-binary.ts`, this machine's target), which a source checkout -finds by itself, or point `OMP_WEB_OMP_BIN` at it or at a wrapper running `bun runner/src/cli.ts`. +The dev server needs ompweb's omp runner, which the oh-my-pi fork builds and releases (`omp-runner.json` pins the release this +checkout fetches). `npm run fetch-omp` downloads it, SHA-256-checked, to `/omp-web/omp///`, where +the dev server, `ompweb host` and the tests find it (`npm run smoke`, `npm run build:binary` and CI download theirs to +`release/runners//`: `scripts/fetch-runner.ts`). A runner you build from the fork: point `OMP_WEB_OMP_BIN` at it, or at a +wrapper script running it. All live-agent features go through it; session browsing works without it. --- @@ -25,7 +26,7 @@ All live-agent features go through it; session browsing works without it. omp-web never imports `@oh-my-pi/*` or `@earendil-works/*` packages, even though the server now runs on Bun: live work goes through the `ompweb-omp` -child (`runner/`, ompweb's own omp build). See `DESIGN.md` for the full porting contract. +child (the fork's `packages/ompweb-runner`, released as `ompweb-omp`; `omp-runner.json` pins the release). See `DESIGN.md` for the full porting contract. ``` Browser Bun server (server/app.ts) omp child process @@ -65,13 +66,27 @@ Shared foundations in `lib/omp/`: - `paths.ts` — Node port of omp's directory resolution (`~/.omp/agent`, XDG, session dir slugs) and `sessionLocalRoot`, omp's `local://` root rule. -- `omp-cli.ts` — locate/probe the `ompweb-omp` binary (`resolveOmpBin`, - `getOmpVersion`): `OMP_WEB_OMP_BIN`, else a source checkout's `runner/release/`, else the fetched copy - (`fetchedOmpBin`: beside a compiled binary, otherwise `/omp-web/omp//`). -- `runner-fetch.ts` — `fetchOmpRunner`/`ensureOmpRunner`/`ompweb fetch-omp`: downloads this version's - `ompweb-omp-` from its own GitHub release (repo from `package.json` `repository`, `lib/release-repo.ts`), - checks it against the release's `ompweb-omp.sha256` and writes it atomically. The web server and the daemon run it in - the background at start when the runner is missing. There are no runner npm packages. +- `omp-cli.ts` — locate/probe the `ompweb-omp` binary (`resolveOmpBin`, `getOmpVersion`), and the two constants that tie this + ompweb to the fork's runner: `RUNNER_PROTOCOL` (the runner protocol this ompweb speaks; the runner prints its own with + `--runner-info` and in its release's `ompweb-omp.json`; bump it only with a change an older runner cannot speak) and + `OMP_RUNNER_PIN` (root `omp-runner.json` `{ repo, tag }`: the fork release this build fetches its runner from). Resolution: + `OMP_WEB_OMP_BIN`, else the runner "Update omp" pointed at (the pointer + `/omp-web/omp//current.json` `{ tag, path, sha256 }`, trusted only while `path` is that version's own + `/ompweb-omp[.exe]` and the file still has the recorded SHA-256; read on every call, hashed again only when the + pointer or file changes), else the fetched copy (`fetchedOmpBin`: beside a compiled binary, otherwise the pinned tag's + directory `/omp-web/omp///`, so a new pin never reuses an older runner). +- `runner-fetch.ts` — `fetchOmpRunner`/`ensureOmpRunner`/`ompweb fetch-omp`: downloads `ompweb-omp-` from the fork + release this build pins (`downloadRunner`), checks it against that release's `ompweb-omp.sha256` and writes it atomically; a + release without the asset fails naming it. The web server and the daemon run it in the background at start when the runner + is missing. There are no runner npm packages. "Update omp" takes the same download and checks from the newest fork release + whose `ompweb-omp.json` names `RUNNER_PROTOCOL` and that attaches this platform's runner and the checksum listing + (`findRunnerRelease`: releases read newest first, 100 a page up to `RELEASE_PAGE_CAP` pages, each tag's protocol asked once; + `latestOmpRunnerRelease` caches the answer 5 min). `installOmpRunner` stages the download in a fresh `.staging-*` directory + beside the tag directories, probes it there (`--runner-info` protocol, `--smoke-test`) and only then renames it to + `/` and writes the pointer: any failure leaves the pointer and the selected runner as they were (a copy of the same tag + is set aside for the swap and put back if it fails; Windows refuses to move a running one). `sweepOmpRunners` runs at daemon + start, after the host control endpoint is acquired (`lib/hosts/host-cli.ts`: a second `ompweb host` must not sweep a running + daemon's runners) and removes every tag directory but the pointed and the pinned one, and unfinished stagings. - `rpc-process.ts` — process + NDJSON channel layer (`RpcProcess`; `rpc-*` names are historical, the wire is the `ompweb-omp` channel of `docs/specs/2026-11-12-omp-runner-design.md`). - `rpc-transport.ts` — `RpcTransport`: the byte pipe under `RpcProcess` (`spawnLocalRpcTransport` for a local child; `RemoteRpcTransport` in @@ -173,8 +188,8 @@ lib/ cluster-config-server.ts main side: `currentClusterConfig` (the manifest cached per fingerprint, one build at a time, `omp plugin list` cached 120 s), `clusterConfigVersion`, `admitClusterMember`, `configErrorResponse`; used by the two `app/api/hosts/cluster-config/` routes and `hub.ts` daemon-cluster-config.ts daemon side: `createClusterConfig` (`ensure`/`apply`/`restore`/`state`/`holds`/`stop`): download, verify, apply whole or not at all, the member's backup (`own-config/`), plugins, leaving; Node only, relative imports host-bearer.ts `hostFromBearer`: the host an `Authorization: Bearer ` request names, or the 401/503 answer; used by `admitClusterMember` - update-types.ts shared per-host software status and operation contract - updates.ts daemon-side version checks, compatibility gate and durable update state + update-types.ts shared per-host software status and operation contract (targets `ompweb` and `omp`) + updates.ts daemon-side version checks, compatibility gate and durable update state; the `omp` target (`evaluateOmpUpdate`, `startOmpUpdate`) runs in the daemon with no worker update-worker.ts host-only install/restart helper; never starts or stops the web-server service github/ GitHub agent tools (server only; spec `docs/specs/2026-11-13-github-watch-tool-design.md`) gh.ts the server's `gh` runner: `GhRunner`, `checkGh` (60 s login cache), target parsing; env from `createChildProcessEnvironment` @@ -290,14 +305,18 @@ bin/ scripts/ build-client.ts `npm run build`: bundles app/index.html into dist/ (Bun.build + tailwind-plugin.ts; fonts emitted as /assets files) tailwind-plugin.ts bun-plugin-tailwind minus Tailwind's color-mix() polyfill (Bun 1.4.4 drops repeated @supports rules); build and dev - smoke.ts `npm run smoke`: starts the built server (or `--binary `) with a throwaway HOME, verifies automatic local-host startup, the SPA, assets, session access and SSE, then stops its own processes + smoke.ts `npm run smoke`: starts the built server (or `--binary `, or `--package `) with a throwaway HOME and the runner of the pinned fork release (`pinnedRunner`, downloaded to `release/runners/`; `--omp ` names another; a binary with its own `ompweb-omp` beside it uses that), verifies automatic local-host startup, the SPA, assets, session access, a live session and SSE, then stops its own processes transport-metrics.ts `bun scripts/transport-metrics.ts [--since 2h|] [--until ] [--client ] [--session ] [--file ]`: per-transport summary of the browser transport samples and per host and direction of the host link samples (`--session`: link samples of one session; default: the last 24 h of the agent dir's file) link-hol-probe.ts `bun scripts/link-hol-probe.ts --host --model `: head-of-line load on one host link (large prompts down, large session reads up, get_state beside them) for the link samples; usage at its top - build-binary.ts `npm run build:binary`: compiles server/binary.ts (NODE_ENV define, no .env/bunfig autoload) into release/ompweb--/ beside dist/, public/, the KaTeX files, the omp runner (`ompweb-omp`, built first by `runner/scripts/build-binary.ts`; not with `--no-runner`, which releases use: they attach the runners as separate assets) and the licenses; archives it + build-binary.ts `npm run build:binary`: compiles server/binary.ts (NODE_ENV define, no .env/bunfig autoload) into release/ompweb--/ beside dist/, public/, the KaTeX files, the omp runner (`ompweb-omp`, downloaded by `fetch-runner.ts` from the fork release `omp-runner.json` pins; not with `--no-runner`, which releases use: their installs fetch it themselves) and the licenses; archives it + fetch-runner.ts `bun scripts/fetch-runner.ts [--latest] [- ...]`: downloads the pinned fork release's runner (default this machine's) into `release/runners//`, SHA-256-checked, and prints its path; `pinnedRunner` is the one function build-binary.ts and smoke.ts call. `--latest` first rewrites `omp-runner.json` to the newest published fork release whose `ompweb-omp.json` names `RUNNER_PROTOCOL` and that attaches every platform's runner (what publish.yml and prerelease.yml record; never committed back, `GH_TOKEN` lifts the API rate limit) build-release-binaries.sh the five release targets (one list), run by publish.yml and prerelease.yml (a prerelease per verified `main` commit, titled by build time; one concurrency group per commit, so no commit's run is dropped) licenses.ts the release's `licenses/`: license files of every npm package the server and client bundles hold (Bun.build metafiles; build-client.ts writes build/client-packages.json) and `bun-LICENSE-.md`, the vendored license of the embedded Bun runtime. Bumping Bun: vendor its LICENSE.md for the new version, or build-binary.ts stops +.github/workflows/verify-pr.yml downloads the committed pin's runner (`fetch-runner.ts`) for the smoke tests, the binary build and the upgrade test; `publish.yml` and `prerelease.yml` pin the newest matching fork release first (`fetch-runner.ts --latest`) and record it in `omp-runner.json` of the build and in the release notes. There are no runner-only releases and no runner build here: the fork's `oml` build compiles and releases the runners (`ompweb-omp-[.exe]`, `ompweb-omp.sha256`, `ompweb-omp.json`) + dist/ build output (git-ignored, shipped in the package); the production server reads index.html and assets from here +omp-runner.json `{ repo, tag }`: the oh-my-pi fork release this ompweb build fetches its runner from (`OMP_RUNNER_PIN`, `lib/omp/omp-cli.ts`); shipped in the npm package and baked into the binary bunfig.toml Bun dev-server bundling: tailwind plugin and the OMP_WEB_PUBLIC_* inlining ``` @@ -1766,7 +1785,8 @@ during the wait. ### Updates: web server versus host software - **Settings → System** prepares and reviews a durable server-and-host upgrade plan, including owned compiled releases. `POST /api/app-update {action:"prepare"}` returns the plan; commit requires `{action:"commit",attemptId,approval:{planId,force}}`. Approval is bound to release identity and the registered-host/session snapshot. An incompatible host without a verified recovery path blocks server cutover. -- **Settings → Hosts** manages individual daemons, including **This machine**; omp updates with ompweb. Normal `POST {target:"ompweb",version}` does not interrupt activity. `force:true` requires distinct GUI confirmation and may end sessions/subagents. Machine-readable `canForceUpdate` and blockers govern the action, never English message parsing. Force does not waive integrity, ownership, process identity, disabled updates, local drains or service permissions. +- **Settings → Hosts** manages individual daemons, including **This machine**. Normal `POST {target:"ompweb",version}` does not interrupt activity. `force:true` requires distinct GUI confirmation and may end sessions/subagents. Machine-readable `canForceUpdate` and blockers govern the action, never English message parsing. Force does not waive integrity, ownership, process identity, disabled updates, local drains or service permissions. +- **Update omp** (`POST {target:"omp",version}`, no `force`; same auth/origin checks and `OMP_WEB_DISABLE_AUTOUPDATE` gate). Between ompweb releases a host gets a newer omp from the fork's releases: `checkUpdates` reports `omp: { currentVersion, availableVersion, updateAvailable, canUpdate, blockedReason, blockers, operation, runnerChannel: true }` (a daemon without `runnerChannel` reports only a version, and the hub keeps just that). The source is the newest published release of `omp-runner.json`'s repo that attaches this platform's runner and whose `ompweb-omp.json` names this ompweb's `RUNNER_PROTOCOL` (`findRunnerRelease`: 100 releases a page, at most `RELEASE_PAGE_CAP` pages, so a release that speaks another protocol is skipped, never offered); it is available when that release's tag is another build than the running omp and, where both have a stamp, a newer one (never a downgrade). The daemon installs it in-process (no worker, no restart; `hostUpdateBlocksNewWork` is false for it) into `/omp-web/omp///`: downloaded into a fresh staging directory, checked there (SHA-256, `--runner-info` protocol, `--smoke-test`), renamed into place and only then pointed at by `current.json`. A failure at any step leaves the pointer and the selected runner exactly as they were. Running sessions keep their binary, new spawns use the pointed one, and an ompweb update starts from its own pinned runner (a new version has a new directory). A caller's `operationId` names one omp update: a replay returns the stored operation, running or settled, installing nothing again; the same id for another version, an id an ompweb operation holds, or the id of an earlier attempt a later one superseded (`omp-update.json` keeps the last 32 in `earlier`) is `operation_conflict`; an ompweb update request with an omp operation's id is too. **It never touches ompweb's record.** It keeps its own (`/omp-web/host-update/omp-update.json`, reported as `omp.operation`; `HostUpdateInfo.operation` stays ompweb's, which a coordinated upgrade verifies in `status.json`) and takes the shared claim lock (`lock.json`, which records its `target`), so the two never run together: an omp update is refused with blocker/error code `ompweb_update_active` while an ompweb operation is running (prepared, committing and recovery-required included) or any ompweb claim is live, and `omp_install_active` while another omp install runs; an ompweb update is blocked by `omp_install_active` (its start fails `update_in_progress`) while an omp install holds the claim. Also blocked by the disable switch, or by an omp the install does not choose (`OMP_WEB_OMP_BIN`: `unsupported_install`). The hub answers a blocked omp POST with the first blocker's code. - **Recovery plane.** Modern candidates may acknowledge an approved future server protocol through an operation-bound preflight nonce and maintenance receipt. Legacy recovery admits only a fixed bootstrap on one operation-bound runner channel, never general old application traffic. Windows uses native process/service/task handling. Old daemons copy only `update-worker.ts`; its startup must work without sibling modules and load shared lifecycle code from verified sources before replacing an install. - **Completion and rollback.** `upgrade-plan.ts`, `self-update.ts` and `server-update-worker.ts` persist progress; POST acceptance and shell output are not success. Verify the expected release, live process identity and operation/boot receipt after reconnect. Retain the prior install until healthy, and report partial recovery explicitly. See `docs/superpowers/specs/2026-11-14-coordinated-gui-upgrades-design.md` when changing upgrade or bootstrap contracts. - `/api/omp-update` keeps only `restart`, an explicit server-side maintenance API for existing callers. The settings UI and AppShell no longer use it as a global update control or send a server-wide session restart after a host update. diff --git a/CHANGELOG.md b/CHANGELOG.md index c879aa1ae..26269dc34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ All notable changes to **omp-web** (`@kahme247/ompweb`) are documented in this f ## [Unreleased] +### Added + +- **Update omp on a host without a new ompweb release.** Settings → Hosts shows "Update omp to …" for a host whose omp is older than the newest published release of the oh-my-pi fork whose runner speaks this ompweb's runner protocol (`RUNNER_PROTOCOL`; a release that speaks another one is never offered). The daemon downloads the runner into a fresh staging directory, checks its SHA-256, its `--runner-info` protocol and `--smoke-test` there, and only then moves it into place and points new sessions at it; any failure leaves the runner in use and its pointer as they were, and running sessions are not interrupted. A caller's `operationId` names one omp update: a replay returns the stored operation and installs nothing again, and a conflicting request, or a replay of an attempt a later one superseded, is refused (`operation_conflict`). An omp update keeps its own record and never touches an ompweb update's: the two never run together, and each is refused with a machine-readable blocker (`ompweb_update_active`, `omp_install_active`) while the other runs. + +### Changed + +- **The omp runner is built and released by the oh-my-pi fork.** `runner/` and its workflows are gone from this repository: the fork's `oml` build compiles `ompweb-omp` for every platform and attaches it to its releases with `ompweb-omp.sha256` and `ompweb-omp.json` (the runner protocol). Root `omp-runner.json` pins the fork release an ompweb build fetches its runner from (`ompweb fetch-omp`, and in the background when it is missing), SHA-256-checked; source checkouts, CI, smoke tests and binary builds download it (`scripts/fetch-runner.ts`, `npm run fetch-omp`) instead of building it, and releases and prereleases record the newest published fork release with a matching protocol. `OMP_WEB_OMP_BIN` still overrides it. A source checkout needs `npm run fetch-omp` once per pin, not a runner build. + ### Fixed - **GitHub workflow watches no longer regress to stale run states.** Workflow updates retain GitHub's run timestamp and ignore older snapshots for the same or an earlier attempt, so a delayed `queued` response cannot follow a newer `in_progress` delivery. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 69247457b..d675fc2ad 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,13 +8,13 @@ Thanks for your interest in contributing! ```bash npm install -npx bun runner/scripts/use-omp-build.ts && (cd runner && npx bun install --frozen-lockfile) # ompweb's omp build; npm test needs it too +npm run fetch-omp # ompweb's omp runner, from the oh-my-pi fork release omp-runner.json pins (SHA-256-checked) npm run dev # http://127.0.0.1:30178, Bun hot reload ``` -For live sessions, build the runner for this machine with `npx bun runner/scripts/build-binary.ts`: ompweb finds -`runner/release/ompweb-omp--` by itself (`OMP_WEB_OMP_BIN` overrides it). A stock `omp` does not -speak ompweb's channel. +For live sessions the dev server and `ompweb host` need that runner: `npm run fetch-omp` puts it where they find it by +themselves (`OMP_WEB_OMP_BIN` overrides it, e.g. with a runner built from the fork). A stock `omp` does not +speak ompweb's channel. The runner's source is in the fork (`packages/ompweb-runner` of `ompweb-dev/oh-my-pi`), not here. ## Checks (must pass before submitting) @@ -51,7 +51,7 @@ The jsdom dependency stays on 29.x for now; moving to a newer major is a separat `lib/i18n/locales/{en,zh-CN,zh-TW,ja}.json`. - **Architecture**: the ompweb server and daemon never import `@oh-my-pi/*` or `@earendil-works/*` packages (Bun-only). Live agent features go through the `ompweb-omp` child process - (`runner/`) over ompweb's channel; see `DESIGN.md` and `AGENTS.md` for the full contract. + (built and released by the oh-my-pi fork) over ompweb's channel; see `DESIGN.md` and `AGENTS.md` for the full contract. ## Pull requests diff --git a/DESIGN.md b/DESIGN.md index c2d1e12b7..a32c23989 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -68,21 +68,27 @@ OMP SDK packages are Bun-only TypeScript and import Bun APIs. Importing Bun, would tie it to omp's internals and make the application unreliable. Therefore, the ompweb server and daemon must not add those runtime dependencies. -Live work goes through ompweb's own omp build, `runner/`, compiled against the -fork's integration build: an `ompweb-dev/oh-my-pi` release that is an upstream omp -release plus the PRs the fork carries (among them `runSessionHost`, which the -headless mode needs), pinned to one release in `runner/omp-build.json` and -re-pinned to the newest one by each ompweb release. The runner replaces omp's +Live work goes through ompweb's own omp build, the runner (`ompweb-omp`). It is +built and released by the fork, `ompweb-dev/oh-my-pi` (workspace package +`packages/ompweb-runner`, compiled by the fork's `oml` build against the +upstream omp release plus the PRs the fork carries, among them `runSessionHost`, +which the headless mode needs); ompweb holds no runner source. Each fork release +attaches the runners (`ompweb-omp-[.exe]`), their `ompweb-omp.sha256` +and `ompweb-omp.json`, which names the runner protocol (`RUNNER_PROTOCOL`, +`lib/omp/omp-cli.ts`, printed by `ompweb-omp --runner-info` too): the version of +the contract below, bumped only by a change an older ompweb cannot speak. The +runner replaces omp's launch command: its session factory connects every session to `ompweb host` over ompweb's channel. The host receives the same `AgentSession` events and calls the same session methods omp's TUI does, so nothing depends on omp's RPC mode. A session started from the runner's TUI connects to the daemon's Unix socket and shows up live in the web; dialogs open on both sides and the first -answer wins. Each ompweb release attaches its runners (`ompweb-omp-`), -and every install downloads the one of its own version -(`lib/omp/runner-fetch.ts`), so omp updates with ompweb. The design and the wire are in -`docs/specs/2026-11-12-omp-runner-design.md`; the fork build in -`docs/specs/2026-11-12-omp-build-in-ompweb-design.md`. +answer wins. `omp-runner.json` pins the fork release an ompweb build fetches its +runner from (`lib/omp/runner-fetch.ts`, SHA-256-checked); a release build +records the newest published fork release whose protocol is `RUNNER_PROTOCOL`. +Between ompweb releases, "Update omp" installs a newer runner from such a fork +release (AGENTS.md, "Updates"). The design and the wire are in +`docs/specs/2026-11-12-omp-runner-design.md`. ### The Host seam diff --git a/README.ja.md b/README.ja.md index ccd149108..4d6461d88 100644 --- a/README.ja.md +++ b/README.ja.md @@ -34,7 +34,7 @@ ## 必要条件 -- omp を別途インストールする必要はありません。ompweb は独自の [omp](https://github.com/can1357/oh-my-pi) ビルド `ompweb-omp` を使い、`~/.omp` の設定、認証、セッションをそのまま使います。どのインストール(npm、npx、単一バイナリ)も、初回起動時に同じバージョンの `ompweb-omp` をそのバージョンの [GitHub リリース](https://github.com/ompweb-dev/ompweb/releases)からダウンロードし、SHA-256 を確認して保存します(単一バイナリでは同じディレクトリ、それ以外は `~/.omp/agent/omp-web/omp//`)。`ompweb fetch-omp` でも取得できます。対応プラットフォームは linux-x64、linux-arm64、darwin-x64、darwin-arm64、windows-x64 です。それ以外(musl の Linux、Arm 版 Windows、FreeBSD)ではセッションを開始できません。GitHub に接続できないマシンでは、`OMP_WEB_OMP_BIN` にそのリリースの `ompweb-omp--` のコピーを指定してください +- omp を別途インストールする必要はありません。ompweb は独自の [omp](https://github.com/can1357/oh-my-pi) ビルド `ompweb-omp` を使い、`~/.omp` の設定、認証、セッションをそのまま使います。このビルドは ompweb ではなく [oh-my-pi フォーク](https://github.com/ompweb-dev/oh-my-pi/releases)がビルド・リリースします。どのインストール(npm、npx、単一バイナリ)も、初回起動時にその ompweb バージョンが固定しているフォークのリリース(`omp-runner.json`)から `ompweb-omp` をダウンロードし、SHA-256 を確認して保存します(単一バイナリでは同じディレクトリ、それ以外は `~/.omp/agent/omp-web/omp//<リリースタグ>/`)。`ompweb fetch-omp` でも取得できます。対応プラットフォームは linux-x64、linux-arm64、darwin-x64、darwin-arm64、windows-x64 です。それ以外(musl の Linux、Arm 版 Windows、FreeBSD)ではセッションを開始できません。GitHub に接続できないマシンでは、`OMP_WEB_OMP_BIN` にそのリリースの `ompweb-omp--` のコピーを指定してください - Node.js `>= 24`。サーバーは [Bun](https://bun.sh) で動作し、npm パッケージの依存関係として自動的にインストールされます(同梱の Bun が見つからない場合は `PATH` 上の Bun を使用します) キュー内のメッセージに対する **Steer**、**Delete**、**Edit** 操作は `ompweb-omp` チャネルのコマンド(`promote_queued_message`、`remove_queued_message`)です。omp がキャンセルを確認してから表示を削除し、編集時はキャンセル成功後にテキストを入力欄へ戻します。すでにキューに存在しないメッセージでは、キューを変更せず通知を表示します。ompweb が重複したステアリングメッセージを送信することはありません。 @@ -66,7 +66,7 @@ ompweb ompweb --port 8080 # ポート番号指定 ompweb --hostname 0.0.0.0 # ネットワーク公開 ompweb hash-password # OMP_WEB_PASSWORD_HASH 用の値を出力 -ompweb fetch-omp # このバージョンの ompweb-omp を GitHub リリースからダウンロード(SHA-256 確認、未取得の場合のみ) +ompweb fetch-omp # 固定されたフォークのリリースから ompweb-omp をダウンロード(SHA-256 確認、未取得の場合のみ) ompweb preflight # 新しいセッションと同じように omp を一度起動して hello を待つ。失敗時は理由を表示して終了コード 1 ompweb --no-open # ブラウザ自動起動を無効化 ``` @@ -137,7 +137,7 @@ Linux トレイ、Windows サービス)はインストール時に `OMP_WEB_PA | `OMP_WEB_NO_OPEN` | `1` でブラウザ自動起動を無効化 | `0` | | `OMP_WEB_DISABLE_AUTOUPDATE` | `1` で更新チェックとアプリ内更新を無効化(変更後は再起動) | `0` | | `OMP_WEB_NAME` | ブラウザのタブとインストールしたアプリに表示する名前。`url`・`host`・`domain`(大文字小文字を区別しない)を指定すると、ブラウザが接続したホスト名(ポートなし)を使用します(localhost と IP アドレスは `omp web` のまま)。それ以外の値はそのまま使用します(変更後は再起動) | `omp web` | -| `OMP_WEB_OMP_BIN` | ダウンロード版の代わりに使う `ompweb-omp` ビルドのパス(GitHub に接続できないマシンではリリースの `ompweb-omp--` のコピー、ソースチェックアウトでは `runner/release/ompweb-omp--`)。通常の `omp` は ompweb のチャネルを話さないため使えません。サービスのインストーラーは、これを設定したときだけサービス環境に書き込みます | _ダウンロードした `ompweb-omp`_ | +| `OMP_WEB_OMP_BIN` | ダウンロード版の代わりに使う `ompweb-omp` ビルドのパス(GitHub に接続できないマシンではフォークのリリースの `ompweb-omp--` のコピー、またはフォークからビルドしたランナー)。通常の `omp` は ompweb のチャネルを話さないため使えません。サービスのインストーラーは、これを設定したときだけサービス環境に書き込みます | _ダウンロードした `ompweb-omp`_ | | `PI_CODING_AGENT_DIR` | カスタム omp エージェントディレクトリ | `~/.omp/agent` | | `OMP_WEB_STT_ENDPOINT` | OpenAI 互換の音声認識エンドポイント URL。未設定の場合は `OMP_WEB_STT_MODEL` を omp 自身の認証情報で使って文字起こしします | _なし_ | | `OMP_WEB_STT_KEY` | STT エンドポイント用の API キー | _なし_ | @@ -151,12 +151,11 @@ Linux トレイ、Windows サービス)はインストール時に `OMP_WEB_PA git clone https://github.com/ompweb-dev/ompweb.git cd ompweb npm install -npx bun runner/scripts/use-omp-build.ts && (cd runner && npx bun install --frozen-lockfile) -npx bun runner/scripts/build-binary.ts # このマシン用の ompweb の omp ビルド: runner/release/ompweb-omp-- +npm run fetch-omp # ompweb の omp ランナー(omp-runner.json が固定するフォークのリリース) npm run dev ``` -ローカル開発サーバーは [http://127.0.0.1:30178](http://127.0.0.1:30178) で起動します。`runner/release/` のランナーは自動的に見つかります。`npm test` と `npm run build:binary` にもランナーのセットアップが必要です。 +ローカル開発サーバーは [http://127.0.0.1:30178](http://127.0.0.1:30178) で起動します。ランナーは `npm run fetch-omp` でダウンロードしたものを使います(固定が変わるたびに 1 回)。`npm run smoke` と `npm run build:binary` は自分でダウンロードします。 `npm run dev` は Bun のホットリロード付きでサーバーを起動します。`npm run build` はブラウザアプリを `dist/` にバンドルし、`npm start` はそれをポート 30177 で配信します(`npm run smoke` で結果を確認できます)。`npm run dev:lan` は開発サーバーをすべてのインターフェースにバインドします。 diff --git a/README.md b/README.md index bcdedbe7d..ad586a6cb 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ A clean, modern web UI for the [oh-my-pi (omp)](https://github.com/can1357/oh-my ## Requirements -- Nothing else for omp: ompweb runs its own [omp](https://github.com/can1357/oh-my-pi) build, `ompweb-omp`, which reads your `~/.omp` settings, auth and sessions. Every install (npm, npx or the single binary) downloads the `ompweb-omp` of its exact version from that version's [GitHub release](https://github.com/ompweb-dev/ompweb/releases) the first time it starts, checks its SHA-256 and keeps it (beside the single binary, otherwise in `~/.omp/agent/omp-web/omp//`); `ompweb fetch-omp` does the same on demand. Sessions start once it is there. It exists for linux-x64, linux-arm64, darwin-x64, darwin-arm64 and windows-x64; on any other platform (Linux on musl, Windows on Arm, FreeBSD) ompweb cannot start sessions. A machine that cannot reach GitHub needs `OMP_WEB_OMP_BIN` pointing at a copy of that release's `ompweb-omp--` +- Nothing else for omp: ompweb runs its own [omp](https://github.com/can1357/oh-my-pi) build, `ompweb-omp`, which reads your `~/.omp` settings, auth and sessions. It is built and released by the [oh-my-pi fork](https://github.com/ompweb-dev/oh-my-pi/releases), not by ompweb. Every install (npm, npx or the single binary) downloads the `ompweb-omp` of the fork release its ompweb version pins (`omp-runner.json`) the first time it starts, checks its SHA-256 and keeps it (beside the single binary, otherwise in `~/.omp/agent/omp-web/omp///`); `ompweb fetch-omp` does the same on demand. Sessions start once it is there. It exists for linux-x64, linux-arm64, darwin-x64, darwin-arm64 and windows-x64; on any other platform (Linux on musl, Windows on Arm, FreeBSD) ompweb cannot start sessions. A machine that cannot reach GitHub needs `OMP_WEB_OMP_BIN` pointing at a copy of that release's `ompweb-omp--` - Node.js `>= 24`. The server runs on [Bun](https://bun.sh), which installs automatically as a dependency of the npm package (a Bun on your `PATH` is used if the bundled one is missing) The **Steer**, **Delete** and **Edit** actions on a queued message are commands of the `ompweb-omp` channel (`promote_queued_message`, `remove_queued_message`). Deletion is confirmed by omp before the chip disappears; editing recalls text only after cancellation succeeds. A message that is no longer pending leaves the queue unchanged and displays a notice; ompweb does not send a duplicate steering message. @@ -155,7 +155,7 @@ Open [http://127.0.0.1:30177](http://127.0.0.1:30177) in your browser. ompweb --port 8080 # Custom port ompweb --hostname 0.0.0.0 # Listen on network ompweb hash-password # Print the value for OMP_WEB_PASSWORD_HASH -ompweb fetch-omp # Download this version's ompweb-omp from its GitHub release (SHA-256-checked) if it is missing +ompweb fetch-omp # Download the pinned ompweb-omp from its fork release (SHA-256-checked) if it is missing ompweb preflight # Start the session omp once (as a new session would) and wait for its hello; exit 1 says why it failed ompweb --no-open # Don't auto-open the browser ompweb --install-tray # Install Windows System Tray service & Desktop shortcuts @@ -1042,7 +1042,7 @@ Stop it with `Stop-ScheduledTask ompweb-host`, then end the running host with | `OMP_WEB_NO_OPEN` | Set to `1` to prevent auto-opening browser | `0` | | `OMP_WEB_DISABLE_AUTOUPDATE` | Set to `1` to disable update checks and in-app updates; restart after changing | `0` | | `OMP_WEB_NAME` | Name shown in browser tabs and installed-app names. `url`, `host` or `domain` (any case) uses the hostname the browser connected to, without port; localhost and IP addresses keep `omp web`. Any other value is used as-is. Restart after changing | `omp web` | -| `OMP_WEB_OMP_BIN` | Path to an `ompweb-omp` build to use instead of the downloaded one: a copy of the release's `ompweb-omp--` on a machine that cannot reach GitHub, or `runner/release/ompweb-omp--` from a source checkout. A stock `omp` does not work: it does not speak ompweb's channel. The service installers write it into the service environment only when you set it | _the downloaded `ompweb-omp`_ | +| `OMP_WEB_OMP_BIN` | Path to an `ompweb-omp` build to use instead of the downloaded one: a copy of the fork release's `ompweb-omp--` on a machine that cannot reach GitHub, or a runner you built from the fork. A stock `omp` does not work: it does not speak ompweb's channel. The service installers write it into the service environment only when you set it | _the downloaded `ompweb-omp`_ | | `PI_CODING_AGENT_DIR` | Custom omp agent directory | `~/.omp/agent` | | `OMP_WEB_STT_ENDPOINT` | OpenAI-compatible transcription endpoint URL. Leave unset to transcribe with `OMP_WEB_STT_MODEL` through omp's own credentials | _None_ | | `OMP_WEB_STT_KEY` | Optional API key for the STT endpoint | _None_ | @@ -1165,13 +1165,12 @@ addresses you list in `set_real_ip_from`: git clone https://github.com/ompweb-dev/ompweb.git cd ompweb npm install -npx bun runner/scripts/use-omp-build.ts && (cd runner && npx bun install --frozen-lockfile) -npx bun runner/scripts/build-binary.ts # ompweb's omp build for this machine: runner/release/ompweb-omp-- +npm run fetch-omp # ompweb's omp runner (from the fork release omp-runner.json pins), where the dev server finds it npm run dev ``` -The dev server runs at [http://127.0.0.1:30178](http://127.0.0.1:30178). It finds the runner in -`runner/release/` by itself; `npm test` and `npm run build:binary` need the runner set up too. +The dev server runs at [http://127.0.0.1:30178](http://127.0.0.1:30178). It needs +the runner `npm run fetch-omp` downloaded (once per pin); `npm run smoke` and `npm run build:binary` download theirs themselves. A dev server does not take over the local daemon: only the production web server writes the daemon's `host.json`, so "This machine" shows offline in dev diff --git a/README.zh-CN.md b/README.zh-CN.md index 876e0437a..5cd613219 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -34,7 +34,7 @@ ## 环境要求 -- 无需另行安装 omp:ompweb 使用自己的 [omp](https://github.com/can1357/oh-my-pi) 构建 `ompweb-omp`,直接使用 `~/.omp` 中的设置、认证和会话。每种安装方式(npm、npx 或单一可执行文件)都会在首次启动时从该版本的 [GitHub 发布](https://github.com/ompweb-dev/ompweb/releases)下载同一版本的 `ompweb-omp`,校验 SHA-256 后保存(单一可执行文件放在同一目录,其他方式放在 `~/.omp/agent/omp-web/omp//`);也可用 `ompweb fetch-omp` 手动获取。支持的平台为 linux-x64、linux-arm64、darwin-x64、darwin-arm64 和 windows-x64;在其他平台(musl 的 Linux、Arm 版 Windows、FreeBSD)上无法启动会话。无法访问 GitHub 的机器请将 `OMP_WEB_OMP_BIN` 指向该发布的 `ompweb-omp--` 副本 +- 无需另行安装 omp:ompweb 使用自己的 [omp](https://github.com/can1357/oh-my-pi) 构建 `ompweb-omp`,直接使用 `~/.omp` 中的设置、认证和会话。该构建由 [oh-my-pi 分支](https://github.com/ompweb-dev/oh-my-pi/releases)构建并发布,而不是 ompweb。每种安装方式(npm、npx 或单一可执行文件)都会在首次启动时从其 ompweb 版本固定的分支发布(`omp-runner.json`)下载 `ompweb-omp`,校验 SHA-256 后保存(单一可执行文件放在同一目录,其他方式放在 `~/.omp/agent/omp-web/omp//<发布标签>/`);也可用 `ompweb fetch-omp` 手动获取。支持的平台为 linux-x64、linux-arm64、darwin-x64、darwin-arm64 和 windows-x64;在其他平台(musl 的 Linux、Arm 版 Windows、FreeBSD)上无法启动会话。无法访问 GitHub 的机器请将 `OMP_WEB_OMP_BIN` 指向该发布的 `ompweb-omp--` 副本 - Node.js `>= 24`。服务器运行在 [Bun](https://bun.sh) 上,Bun 会作为 npm 包的依赖自动安装(找不到随包安装的 Bun 时使用 `PATH` 中的 Bun) 排队消息的 **Steer**、**Delete** 和 **Edit** 操作是 `ompweb-omp` 通道的命令(`promote_queued_message`、`remove_queued_message`)。只有 omp 确认取消后才会移除消息;编辑时也必须先取消成功,再将文本恢复到输入框。消息已不在队列中时,界面保留队列并显示提示;ompweb 不会重复发送一条引导消息。 @@ -66,7 +66,7 @@ ompweb ompweb --port 8080 # 自定义端口 ompweb --hostname 0.0.0.0 # 监听网络地址 ompweb hash-password # 输出 OMP_WEB_PASSWORD_HASH 的值 -ompweb fetch-omp # 从 GitHub 发布下载本版本的 ompweb-omp(校验 SHA-256,仅在缺失时) +ompweb fetch-omp # 从固定的分支发布下载 ompweb-omp(校验 SHA-256,仅在缺失时) ompweb preflight # 像新会话一样启动一次 omp 并等待其 hello;失败时说明原因并以 1 退出 ompweb --no-open # 不自动打开浏览器 ``` @@ -130,7 +130,7 @@ Windows 服务)仍在安装时接受 `OMP_WEB_PASSWORD`,但在写入配置 | `OMP_WEB_NO_OPEN` | 设为 `1` 时禁止自动打开浏览器 | `0` | | `OMP_WEB_DISABLE_AUTOUPDATE` | 设为 `1` 时禁用更新检查和应用内更新;修改后需重启 | `0` | | `OMP_WEB_NAME` | 浏览器标签页和已安装应用中显示的名称。设为 `url`、`host` 或 `domain`(不区分大小写)时使用浏览器所连接的主机名(不含端口),localhost 和 IP 地址仍显示 `omp web`;其他值按原样使用。修改后需重启 | `omp web` | -| `OMP_WEB_OMP_BIN` | 用于替代下载版本的 `ompweb-omp` 构建路径(无法访问 GitHub 的机器上为发布中 `ompweb-omp--` 的副本,源码检出中为 `runner/release/ompweb-omp--`)。普通的 `omp` 不支持 ompweb 的通道,无法使用。服务安装程序仅在你设置了它时才写入服务环境 | _下载的 `ompweb-omp`_ | +| `OMP_WEB_OMP_BIN` | 用于替代下载版本的 `ompweb-omp` 构建路径(无法访问 GitHub 的机器上为分支发布中 `ompweb-omp--` 的副本,或从分支自行构建的 runner)。普通的 `omp` 不支持 ompweb 的通道,无法使用。服务安装程序仅在你设置了它时才写入服务环境 | _下载的 `ompweb-omp`_ | | `PI_CODING_AGENT_DIR` | 自定义 omp agent 目录 | `~/.omp/agent` | | `OMP_WEB_STT_ENDPOINT` | OpenAI 兼容的语音转文字接口 URL。不设置时使用 `OMP_WEB_STT_MODEL` 通过 omp 自身的凭据转录 | _无_ | | `OMP_WEB_STT_KEY` | STT 接口对应的 API Key | _无_ | @@ -144,12 +144,11 @@ Windows 服务)仍在安装时接受 `OMP_WEB_PASSWORD`,但在写入配置 git clone https://github.com/ompweb-dev/ompweb.git cd ompweb npm install -npx bun runner/scripts/use-omp-build.ts && (cd runner && npx bun install --frozen-lockfile) -npx bun runner/scripts/build-binary.ts # 为本机构建 ompweb 的 omp:runner/release/ompweb-omp-- +npm run fetch-omp # ompweb 的 omp runner(omp-runner.json 固定的分支发布) npm run dev ``` -本地开发服务器运行在 [http://127.0.0.1:30178](http://127.0.0.1:30178)。它会自动找到 `runner/release/` 中的 runner;`npm test` 和 `npm run build:binary` 也需要先准备好 runner。 +本地开发服务器运行在 [http://127.0.0.1:30178](http://127.0.0.1:30178)。它使用 `npm run fetch-omp` 下载的 runner(每个固定版本一次);`npm run smoke` 和 `npm run build:binary` 会自行下载。 `npm run dev` 以 Bun 热重载方式运行服务器。`npm run build` 将浏览器端应用打包到 `dist/`,`npm start` 在 30177 端口提供该产物(可用 `npm run smoke` 检查结果)。`npm run dev:lan` 会将开发服务器绑定到所有网络接口。 diff --git a/README.zh-TW.md b/README.zh-TW.md index 16fcb934e..13983463e 100644 --- a/README.zh-TW.md +++ b/README.zh-TW.md @@ -34,7 +34,7 @@ ## 系統需求 -- 無需另行安裝 omp:ompweb 使用自己的 [omp](https://github.com/can1357/oh-my-pi) 建置版本 `ompweb-omp`,直接使用 `~/.omp` 中的設定、認證與對話。每種安裝方式(npm、npx 或單一二進位檔)都會在首次啟動時從該版本的 [GitHub 發行版本](https://github.com/ompweb-dev/ompweb/releases)下載同一版本的 `ompweb-omp`,驗證 SHA-256 後保存(單一二進位檔放在同一資料夾,其他方式放在 `~/.omp/agent/omp-web/omp//`);也可用 `ompweb fetch-omp` 手動取得。支援的平台為 linux-x64、linux-arm64、darwin-x64、darwin-arm64 與 windows-x64;在其他平台(musl 的 Linux、Arm 版 Windows、FreeBSD)上無法啟動對話。無法連線至 GitHub 的機器請將 `OMP_WEB_OMP_BIN` 指向該發行版本 `ompweb-omp--` 的副本 +- 無需另行安裝 omp:ompweb 使用自己的 [omp](https://github.com/can1357/oh-my-pi) 建置版本 `ompweb-omp`,直接使用 `~/.omp` 中的設定、認證與對話。該建置版本由 [oh-my-pi 分支](https://github.com/ompweb-dev/oh-my-pi/releases)建置並發行,而非 ompweb。每種安裝方式(npm、npx 或單一二進位檔)都會在首次啟動時從其 ompweb 版本固定的分支發行版本(`omp-runner.json`)下載 `ompweb-omp`,驗證 SHA-256 後保存(單一二進位檔放在同一資料夾,其他方式放在 `~/.omp/agent/omp-web/omp//<發行標籤>/`);也可用 `ompweb fetch-omp` 手動取得。支援的平台為 linux-x64、linux-arm64、darwin-x64、darwin-arm64 與 windows-x64;在其他平台(musl 的 Linux、Arm 版 Windows、FreeBSD)上無法啟動對話。無法連線至 GitHub 的機器請將 `OMP_WEB_OMP_BIN` 指向該發行版本 `ompweb-omp--` 的副本 - Node.js `>= 24`。伺服器運作於 [Bun](https://bun.sh) 上,Bun 會作為 npm 套件的相依套件自動安裝(找不到隨套件安裝的 Bun 時會使用 `PATH` 中的 Bun) 佇列中訊息的**引導(Steer)**、**刪除(Delete)**與**編輯(Edit)**操作皆為 `ompweb-omp` 通道的指令(`promote_queued_message`、`remove_queued_message`)。刪除操作會先經 omp 確認後,標籤才會自畫面消失;編輯操作則在成功取消佇列後,才會將文字取回輸入框。若該訊息已不再處於等待狀態,佇列將維持原狀並顯示提示;ompweb 不會重複傳送引導訊息。 @@ -90,7 +90,7 @@ ompweb ompweb --port 8080 # 自訂連接埠 ompweb --hostname 0.0.0.0 # 監聽網路介面 ompweb hash-password # 輸出供 OMP_WEB_PASSWORD_HASH 使用的雜湊值 -ompweb fetch-omp # 從 GitHub 發行版本下載此版本的 ompweb-omp(驗證 SHA-256,僅在缺少時) +ompweb fetch-omp # 從固定的分支發行版本下載 ompweb-omp(驗證 SHA-256,僅在缺少時) ompweb preflight # 如同新對話般啟動一次 omp 並等待其 hello;失敗時說明原因並以 1 結束 ompweb --no-open # 不自動開啟瀏覽器 ompweb --install-tray # 安裝 Windows 系統匣服務與桌面捷徑 @@ -478,7 +478,7 @@ Start-ScheduledTask ompweb-host | `OMP_WEB_NO_OPEN` | 設為 `1` 可防止自動開啟瀏覽器 | `0` | | `OMP_WEB_DISABLE_AUTOUPDATE` | 設為 `1` 可停用更新檢查與應用程式內更新;變更後需重新啟動 | `0` | | `OMP_WEB_NAME` | 顯示於瀏覽器分頁與已安裝應用程式的名稱。若設為 `url`、`host` 或 `domain`(不分大小寫),則採用瀏覽器連線的主機名稱(不含連接埠);localhost 與 IP 位址仍會保持 `omp web`。其他任何值將原樣採用。變更後需重新啟動 | `omp web` | -| `OMP_WEB_OMP_BIN` | 用於取代下載版本的 `ompweb-omp` 建置版本路徑(無法連線至 GitHub 的機器上為發行版本中 `ompweb-omp--` 的副本,原始碼檢出中為 `runner/release/ompweb-omp--`)。一般的 `omp` 不支援 ompweb 的通道,無法使用。服務安裝程式僅在你設定它時才會寫入服務環境 | _下載的 `ompweb-omp`_ | +| `OMP_WEB_OMP_BIN` | 用於取代下載版本的 `ompweb-omp` 建置版本路徑(無法連線至 GitHub 的機器上為分支發行版本中 `ompweb-omp--` 的副本,或從分支自行建置的 runner)。一般的 `omp` 不支援 ompweb 的通道,無法使用。服務安裝程式僅在你設定它時才會寫入服務環境 | _下載的 `ompweb-omp`_ | | `PI_CODING_AGENT_DIR` | 自訂 omp 代理目錄 | `~/.omp/agent` | | `OMP_WEB_STT_ENDPOINT` | 相容於 OpenAI 的語音轉文字(STT)端點網址。若保留未設定,則會透過 omp 自身的憑證搭配 `OMP_WEB_STT_MODEL` 進行轉錄 | _無_ | | `OMP_WEB_STT_KEY` | STT 端點的選用 API 金鑰 | _無_ | @@ -562,12 +562,11 @@ proxy_set_header X-Omp-Web-Auth $ompweb_auth; git clone https://github.com/ompweb-dev/ompweb.git cd ompweb npm install -npx bun runner/scripts/use-omp-build.ts && (cd runner && npx bun install --frozen-lockfile) -npx bun runner/scripts/build-binary.ts # 為本機建置 ompweb 的 omp:runner/release/ompweb-omp-- +npm run fetch-omp # ompweb 的 omp runner(omp-runner.json 固定的分支發行版本) npm run dev ``` -開發伺服器將執行於 [http://127.0.0.1:30178](http://127.0.0.1:30178)。它會自動找到 `runner/release/` 中的 runner;`npm test` 與 `npm run build:binary` 也需要先準備好 runner。 +開發伺服器將執行於 [http://127.0.0.1:30178](http://127.0.0.1:30178)。它使用 `npm run fetch-omp` 下載的 runner(每個固定版本一次);`npm run smoke` 與 `npm run build:binary` 會自行下載。 開發伺服器不會接管本機守護行程:只有正式環境的網頁伺服器會寫入守護行程的 `host.json`,因此「本機」在開發環境中會顯示為離線,而守護行程會保持與您已安裝的 ompweb 連結。 `npm run dev` 以 Bun 熱重載(hot reload)方式執行伺服器。`npm run build` 將瀏覽器端應用打包至 `dist/`,`npm start` 則在 30177 連接埠提供該產物(可用 `npm run smoke` 檢查結果)。`npm run dev:lan` 會將開發伺服器繫結至所有網路介面。`npm run build:binary` 會將適用於本機的[單一二進位檔](#單一二進位檔)建置到 `release/`(可使用 `-- --target bun-linux-arm64` 等參數為其他平台建置;`npm run smoke -- --binary release/ompweb-linux-x64/ompweb` 進行檢查)。 diff --git a/app/api/auth/login/[provider]/route.ts b/app/api/auth/login/[provider]/route.ts index 920ce2651..b29350976 100644 --- a/app/api/auth/login/[provider]/route.ts +++ b/app/api/auth/login/[provider]/route.ts @@ -6,7 +6,7 @@ import { disposeUtilityRpc } from "@/lib/omp/rpc-utility"; /** * Interactive login over a dedicated headless runner. The runner drives the - * flow with login frames (runner/src/commands/agents.ts): `login_open_url` + * flow with login frames (packages/ompweb-runner/src/commands/agents.ts of the oh-my-pi fork): `login_open_url` * carries the OAuth URL, `login_input` asks for the pasted code/redirect URL, * `login_progress` reports progress. The SSE stream keeps pi-web's event names * (auth, prompt_request, progress, success, error, cancelled) so the client diff --git a/app/api/hosts/[id]/updates/route.ts b/app/api/hosts/[id]/updates/route.ts index 0d6f7429b..dd180c234 100644 --- a/app/api/hosts/[id]/updates/route.ts +++ b/app/api/hosts/[id]/updates/route.ts @@ -28,8 +28,9 @@ export async function POST(request: Request, { params }: Context) { if (request.headers.get("content-type")?.split(";", 1)[0].trim().toLowerCase() !== "application/json") return fail("Content-Type must be application/json", "unsupported_media_type", 415); try { const body = await parseJsonWithinLimit(request, 4096); - if (!isRecord(body) || Object.keys(body).some((key) => !["target", "version", "force"].includes(key)) || body.target !== "ompweb" - || (body.force !== undefined && typeof body.force !== "boolean") + if (!isRecord(body) || Object.keys(body).some((key) => !["target", "version", "force"].includes(key)) || (body.target !== "ompweb" && body.target !== "omp") + // An omp update interrupts nothing, so there is nothing to force. + || (body.force !== undefined && (typeof body.force !== "boolean" || body.target === "omp")) || typeof body.version !== "string" || body.version.length > 100 || !/^\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?$/.test(body.version)) { return fail("target and a release version are required", "invalid_host_update", 400); } diff --git a/components/HostUpdates.test.mjs b/components/HostUpdates.test.mjs index c7ac8ddff..695bca1ff 100644 --- a/components/HostUpdates.test.mjs +++ b/components/HostUpdates.test.mjs @@ -13,13 +13,13 @@ const info = { }; /** Render the real host row and record every request it sends. */ -async function renderHost(t, host, ompweb = {}, operation = null) { +async function renderHost(t, host, ompweb = {}, operation = null, omp = {}) { const requests = []; const originalFetch = globalThis.fetch; globalThis.fetch = async (url, init) => { requests.push({ url: String(url), method: init?.method ?? "GET", body: init?.body }); - if (init?.method === "POST") return Response.json({ accepted: true, operation: { state: "running", target: "ompweb", version: "0.7.1" } }); - return Response.json({ ...info, operation, ompweb: { ...info.ompweb, ...ompweb } }); + if (init?.method === "POST") return Response.json({ accepted: true, operation: { state: "running", target: JSON.parse(init.body).target, version: "0.7.1" } }); + return Response.json({ ...info, operation, ompweb: { ...info.ompweb, ...ompweb }, omp: { ...info.omp, ...omp } }); }; const container = document.createElement("div"); document.body.append(container); @@ -178,3 +178,51 @@ test("uncertain shutdown recovery shows its durable error instead of polling Upd assert.equal(container.querySelector('[role="status"]'), null); assert.deepEqual(posts(requests), []); }); + +const runnerChannel = { currentVersion: "18.22.0-int.202611171027.gec3956c", availableVersion: "18.23.0-int.202611201200.gabc1234", updateAvailable: true, canUpdate: true, blockedReason: null, runnerChannel: true }; + +test("omp has its own row: Update omp posts the omp target after a confirmation that says sessions are not interrupted", async (t) => { + const { container, requests } = await renderHost(t, modern, { updateAvailable: false, canUpdate: false }, null, runnerChannel); + assert.ok(container.textContent.includes("Current: 18.22.0-int.202611171027.gec3956c"), container.textContent); + assert.ok(container.textContent.includes("Available: 18.23.0-int.202611201200.gabc1234")); + assert.equal(button(container, "Update"), undefined, "ompweb has no update of its own here"); + const update = button(container, "Update omp to 18.23.0-int.202611201200.gabc1234"); + assert.ok(update && !update.disabled); + + act(() => update.click()); + assert.ok(document.body.textContent.includes("not interrupted"), "the confirmation says running sessions are not interrupted"); + assert.deepEqual(posts(requests), [], "opening the confirmation must not submit"); + await act(async () => dialogButton("Update").click()); + await act(() => new Promise((resolve) => setTimeout(resolve, 20))); + assert.equal(posts(requests).length, 1); + assert.deepEqual(JSON.parse(posts(requests)[0].body), { target: "omp", version: "18.23.0-int.202611201200.gabc1234" }, "an omp update has no force"); +}); + +test("a blocked omp update cannot be started", async (t) => { + const { container } = await renderHost(t, modern, {}, null, { ...runnerChannel, canUpdate: false, blockedReason: "Another host update is in progress" }); + assert.equal(button(container, "Update omp to 18.23.0-int.202611201200.gabc1234").disabled, true); + assert.ok(container.textContent.includes("Blocked: Another host update is in progress")); + assert.equal(button(container, "Force update"), undefined); +}); + +test("a daemon without the runner channel says omp updates with ompweb and offers no omp update", async (t) => { + const { container } = await renderHost(t, modern); + assert.ok(container.textContent.includes("Updated with ompweb"), container.textContent); + assert.ok(![...container.querySelectorAll("button")].some((candidate) => candidate.textContent.includes("Update omp"))); +}); + +test("an omp update's own record is shown beside ompweb's: running it is watched, a failure is reported, and neither hides the other", async (t) => { + const ompOperation = { id: "omp-op", target: "omp", version: "18.23.0-int.202611201200.gabc1234", state: "running" }; + const settled = { id: "ompweb-op", target: "ompweb", version: "0.7.0", state: "succeeded" }; + const running = await renderHost(t, modern, {}, settled, { ...runnerChannel, canUpdate: false, operation: ompOperation }); + assert.ok(running.container.textContent.includes("Updating omp runtime to 18.23.0-int.202611201200.gabc1234"), running.container.textContent); + assert.equal(button(running.container, "Check updates").disabled, true, "an omp update in flight is watched like an ompweb one"); +}); + +test("a failed omp update is reported next to a settled ompweb update", async (t) => { + const failed = { id: "omp-op", target: "omp", version: "18.23.0-int.202611201200.gabc1234", state: "failed", error: "its SHA-256 is not the one listed" }; + const { container } = await renderHost(t, modern, {}, { id: "ompweb-op", target: "ompweb", version: "0.7.0", state: "succeeded" }, { ...runnerChannel, operation: failed }); + const alert = container.querySelector('[role="alert"]'); + assert.ok(alert.textContent.includes("its SHA-256 is not the one listed"), container.textContent); + assert.ok(container.textContent.includes("Update completed."), "ompweb's own result is still there"); +}); diff --git a/components/HostUpdates.tsx b/components/HostUpdates.tsx index a2d2fc4e8..a4f420c22 100644 --- a/components/HostUpdates.tsx +++ b/components/HostUpdates.tsx @@ -1,6 +1,6 @@ "use client"; -import { useEffect, useRef, useState } from "react"; +import { Fragment, useEffect, useRef, useState } from "react"; import { AlertTriangle, Download, RefreshCw } from "lucide-react"; import { ConfirmDialog } from "@/components/ui/field"; import type { HostRow } from "@/lib/hosts/hub"; @@ -88,7 +88,7 @@ export function HostUpdates({ host }: { host: HostRow }) { setInfo(next); setError(null); setNeedsStatus(false); - if (next.operation?.state === "running" && !next.operation.recoveryRequired) { + if ([next.operation, next.omp.operation].some((item) => item?.state === "running" && !item.recoveryRequired)) { watching.current = true; deadline.current ||= Date.now() + RECONNECT_MS; setMonitoring(true); @@ -145,7 +145,8 @@ export function HostUpdates({ host }: { host: HostRow }) { } if (!data.accepted || !data.operation) throw new Error(t("hostUpdates.invalidResponse")); const operation = data.operation as HostUpdateOperation; - setInfo((previous) => previous && { ...previous, operation: operation.state === "running" ? operation : null }); + // An omp update has its own record, beside ompweb's: neither replaces the other. + setInfo((previous) => previous && (target === "omp" ? { ...previous, omp: { ...previous.omp, operation: operation.state === "running" ? operation : null } } : { ...previous, operation: operation.state === "running" ? operation : null })); watching.current = true; deadline.current = Date.now() + RECONNECT_MS; setMonitoring(true); @@ -171,6 +172,8 @@ export function HostUpdates({ host }: { host: HostRow }) { const disabled = busy || unavailable || unsupported; const unavailableReason = (host.status === "needs-upgrade" && unavailable) || unsupported ? t("hostUpdates.unsupported") : host.status === "offline" && unavailable ? t("hostUpdates.offline") : null; const operation = info?.operation; + const operations = [info?.operation, info?.omp.operation].filter((item): item is HostUpdateOperation => !!item); + const running = operations.find((item) => item.state === "running"); return (
@@ -179,45 +182,46 @@ export function HostUpdates({ host }: { host: HostRow }) {
- {(["ompweb"] as const).map((target) => { + {(["ompweb", "omp"] as const).map((target) => { const software = info?.[target]; const label = t(`hostUpdates.${target}`); + // A daemon without the runner channel updates omp only with ompweb. + const withOmpweb = target === "omp" && info !== null && info.omp.runnerChannel !== true; const updateDisabled = disabled || needsStatus || operation?.recoveryRequired === true || !software?.canUpdate; const forceable = operation?.recoveryRequired !== true && software?.updateAvailable === true && !software.canUpdate && software.canForceUpdate === true; return (
{label} -
{t("hostUpdates.current", { version: software?.currentVersion ?? host.ompwebVersion ?? t("hostUpdates.unknown") })}
+
{t("hostUpdates.current", { version: software?.currentVersion ?? (target === "ompweb" ? host.ompwebVersion : host.ompVersion?.replace(/^omp\//, "")) ?? t("hostUpdates.unknown") })}
{software?.updateAvailable && software.availableVersion &&
{t("hostUpdates.available", { version: software.availableVersion })}
} - {software?.currentVersion && !software.updateAvailable && !software.blockedReason && !error && !busy && !unavailableReason &&
{t("hostUpdates.upToDate")}
} + {withOmpweb &&
{t("hostUpdates.ompWithOmpweb")}
} + {!withOmpweb && software?.currentVersion && !software.updateAvailable && !software.blockedReason && !error && !busy && !unavailableReason &&
{t("hostUpdates.upToDate")}
} {software?.blockedReason &&
{t("hostUpdates.blocked", { reason: software.blockedReason })}
} {software?.updateAvailable && !software.canUpdate && !software.blockedReason && !forceable &&
{t("hostUpdates.manualOnly")}
} {forceable &&
{t("hostUpdates.forceAvailable")}
}
- {software?.updateAvailable && software.availableVersion && } + {software?.updateAvailable && software.availableVersion && } {forceable && software.availableVersion && }
); })} - {/* omp updates with ompweb: each ompweb release ships its omp runner. */} -
- {t("hostUpdates.omp")} -
{t("hostUpdates.current", { version: info?.omp.currentVersion ?? host.ompVersion?.replace(/^omp\//, "") ?? t("hostUpdates.unknown") })}
-
{t("hostUpdates.ompWithOmpweb")}
-
{unavailableReason &&
{unavailableReason}
} - {monitoring &&
{operation?.state === "running" ? t("hostUpdates.updating", { software: t(`hostUpdates.${operation.target}`), version: operation.version }) : t("hostUpdates.reconnecting")}
} - {!monitoring && !posting && !error && operation?.state === "succeeded" &&
{t(operation.awaitingServerCutover ? "hostUpdates.awaitingServerCutover" : "hostUpdates.succeeded")}
} - {!monitoring && operation?.force === true && operation.interruptedSessions !== undefined && operation.state !== "running" &&
{t("hostUpdates.interruptedSessions", { count: operation.interruptedSessions })}
} - {!monitoring && (operation?.state === "failed" || operation?.recoveryRequired) &&
{t("hostUpdates.updateFailed", { error: operation.error || t("hostUpdates.unknownError") })}
} + {monitoring &&
{running ? t("hostUpdates.updating", { software: t(`hostUpdates.${running.target}`), version: running.version }) : t("hostUpdates.reconnecting")}
} + {operations.map((item) => ( + + {!monitoring && !posting && !error && item.state === "succeeded" &&
{t(item.awaitingServerCutover ? "hostUpdates.awaitingServerCutover" : item.target === "omp" ? "hostUpdates.ompSucceeded" : "hostUpdates.succeeded")}
} + {!monitoring && item.force === true && item.interruptedSessions !== undefined && item.state !== "running" &&
{t("hostUpdates.interruptedSessions", { count: item.interruptedSessions })}
} + {!monitoring && (item.state === "failed" || item.recoveryRequired) &&
{t("hostUpdates.updateFailed", { error: item.error || t("hostUpdates.unknownError") })}
} +
+ ))} {error &&
{error}
} {needsStatus &&
{t("hostUpdates.statusRequired")}
}
- { if (!open) setConfirm(null); }} title={t(confirm?.force ? "hostUpdates.forceConfirmTitle" : "hostUpdates.confirmTitle", { software: confirm ? t(`hostUpdates.${confirm.target}`) : "", host: hostName(t, host) })} description={t(confirm?.force ? "hostUpdates.forceHostWarning" : "hostUpdates.hostWarning", { host: hostName(t, host) })} confirmLabel={t(confirm?.force ? "hostUpdates.forceConfirm" : "hostUpdates.update")} cancelLabel={t("hostsConfig.cancel")} danger={confirm?.force === true} busy={posting} onConfirm={() => void update()} /> + { if (!open) setConfirm(null); }} title={t(confirm?.force ? "hostUpdates.forceConfirmTitle" : "hostUpdates.confirmTitle", { software: confirm ? t(`hostUpdates.${confirm.target}`) : "", host: hostName(t, host) })} description={t(confirm?.force ? "hostUpdates.forceHostWarning" : confirm?.target === "omp" ? "hostUpdates.ompHostWarning" : "hostUpdates.hostWarning", { host: hostName(t, host) })} confirmLabel={t(confirm?.force ? "hostUpdates.forceConfirm" : "hostUpdates.update")} cancelLabel={t("hostsConfig.cancel")} danger={confirm?.force === true} busy={posting} onConfirm={() => void update()} />
); } diff --git a/docs/specs/2026-11-12-omp-runner-design.md b/docs/specs/2026-11-12-omp-runner-design.md index 0d42754bc..8b897749c 100644 --- a/docs/specs/2026-11-12-omp-runner-design.md +++ b/docs/specs/2026-11-12-omp-runner-design.md @@ -11,6 +11,8 @@ ompweb's live features ride omp's RPC mode, and every gap or regression there is ### R1. Our own omp build +> **Superseded location (2026-11-17).** The runner moved into the fork: it is `packages/ompweb-runner` on `ompweb-dev/oh-my-pi`'s `oml` branch, built against the workspace omp packages, and every `oml` release attaches `ompweb-omp-`, `ompweb-omp.sha256` and `ompweb-omp.json` (`{ protocol, omp }`). ompweb no longer builds it: root `omp-runner.json` pins the fork release, `lib/omp/runner-fetch.ts` downloads and checks it, and a host's "Update omp" installs the newest fork release whose `protocol` equals `RUNNER_PROTOCOL`. The paths and the release flow below describe the original in-repo layout; the channel, transports and runtime decisions still hold. + `runner/` is a Bun package pinned to one `ompweb-dev/oh-my-pi` integration release (`runner/omp-build.json`): `runner/scripts/use-omp-build.ts` unzips that release's npm tarballs into `runner/vendor/`, and `runner/package.json` installs every `@oh-my-pi/*` package and native leaf from there (`file:` dependencies and overrides, sha512-pinned in `bun.lock`). `runner/src/cli.ts` stands in for omp's own entry (worker-host declaration, SQLite lock shim, postmortem handlers) and runs omp's `runCli` with two changes passed to `runRootCommand`: a session factory that connects every session to the host (`runner/src/launch.ts`, a copy of upstream's to keep in step on every bump) and, under `--headless`, a `runSessionHost` hook that hands the finished session to `runner/src/headless.ts` instead of the TUI. `runSessionHost` is an upstream PR that is not released yet; the integration build carries it. Every ompweb (pre)release re-pins to the newest `integration-release` build; pin bumps on `main` are manual (`bun scripts/use-omp-build.ts `). Everything else (settings, auth, models, extensions, MCP, plugins, the TUI itself) is upstream omp, unpatched. - It is compiled per platform like omp's release binary (`ompweb-omp--`, `runner/scripts/build-binary.ts`, `.github/workflows/omp-runner.yml`) and every ompweb release attaches it as `ompweb-omp-` for linux-x64, linux-arm64, darwin-x64, darwin-arm64 and windows-x64, with `ompweb-omp.sha256`. Every install, npm and npx included, downloads the runner of its exact version from that release (`lib/omp/runner-fetch.ts`, `ompweb fetch-omp`; at start in the background when missing, and by the updaters before the candidate's preflight), checks its SHA-256 and keeps it beside a compiled binary or in `/omp-web/omp//`. Release archives do not contain it; prerelease archives, whose binaries carry another release's version, do. There are no runner npm packages. ompweb starts it for every session (`lib/omp/omp-cli.ts`); `OMP_WEB_OMP_BIN` overrides it with another runner build, for example on a machine that cannot reach GitHub. diff --git a/eslint.config.mjs b/eslint.config.mjs index fd9e1a125..43c614700 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -12,6 +12,4 @@ export default tseslint.config( "react-hooks/set-state-in-effect": "off", }, }, - // The runner drops fields from omp's wire objects by destructuring them out of a rest spread. - { files: ["runner/**/*.ts"], rules: { "@typescript-eslint/no-unused-vars": ["error", { ignoreRestSiblings: true }] } }, ); diff --git a/lib/auth-login-route.test.mjs b/lib/auth-login-route.test.mjs index ce5c9fac0..d7d9b875e 100644 --- a/lib/auth-login-route.test.mjs +++ b/lib/auth-login-route.test.mjs @@ -6,7 +6,7 @@ import test, { after } from "node:test"; import { mock } from "bun:test"; // Real /api/auth/login/[provider] handlers over a fake runner that sends the -// login frames runner/src/commands/agents.ts documents. +// login frames packages/ompweb-runner/src/commands/agents.ts of the oh-my-pi fork documents. process.env.PI_CODING_AGENT_DIR = mkdtempSync(join(tmpdir(), "ompweb-login-")); const runners = []; diff --git a/lib/hosts/channel-lifecycle.test.mjs b/lib/hosts/channel-lifecycle.test.mjs index a0b7ed4c3..e99ebc092 100644 --- a/lib/hosts/channel-lifecycle.test.mjs +++ b/lib/hosts/channel-lifecycle.test.mjs @@ -2,7 +2,7 @@ import assert from "node:assert/strict"; import test from "node:test"; import { ChannelLifecycle } from "./channel-lifecycle.ts"; -/** The cases below read as RPC frames; the channel speaks the runner wire (runner/src/channel.ts). */ +/** The cases below read as RPC frames; the channel speaks the runner wire (packages/ompweb-runner/src/channel.ts of the oh-my-pi fork). */ function stdoutWire({ type, ...rest }) { if (type === "response") return { t: "res", id: rest.id, ok: rest.success !== false, data: rest.data }; if (type === "extension_ui_request") return rest.method === "cancel" ? { t: "ui_end", id: rest.targetId } : { t: "ui_request", ...rest }; diff --git a/lib/hosts/daemon-update-safety.test.mjs b/lib/hosts/daemon-update-safety.test.mjs index ed67c605c..c9e1800e0 100644 --- a/lib/hosts/daemon-update-safety.test.mjs +++ b/lib/hosts/daemon-update-safety.test.mjs @@ -138,12 +138,17 @@ test("a malformed update status does not prevent ordinary host sessions from sta assert.equal((await spawn("normal")).ok, true); }); -test("the daemon refuses an omp update: omp updates only with ompweb", async (t) => { +test("the daemon's omp update takes only a version: a force is refused, and a build it does not offer starts nothing", async (t) => { updatableInstall(t); await start(t); - const answer = await request("updateSoftware", { target: "omp", version: "99.0.0", compatibility: { min: LINK_PROTOCOL_VERSION, max: LINK_PROTOCOL_VERSION } }); - assert.equal(answer.ok, false); - assert.equal(answer.error.code, "invalid_request"); + const compatibility = { min: LINK_PROTOCOL_VERSION, max: LINK_PROTOCOL_VERSION }; + const forced = await request("updateSoftware", { target: "omp", version: "99.0.0", force: true, compatibility }); + assert.equal(forced.ok, false); + assert.equal(forced.error.code, "invalid_request"); + // OMP_WEB_OMP_BIN names this daemon's runner, so no runner release is on offer. + const unavailable = await request("updateSoftware", { target: "omp", version: "99.0.0", compatibility }); + assert.equal(unavailable.ok, false); + assert.equal(unavailable.error.code, "candidate_changed"); assert.equal(existsSync(join(root, "status.json")), false, "no update operation was started"); }); diff --git a/lib/hosts/daemon.ts b/lib/hosts/daemon.ts index ffd4f67e0..252f958fc 100644 --- a/lib/hosts/daemon.ts +++ b/lib/hosts/daemon.ts @@ -150,7 +150,7 @@ export function runHostDaemon(options: DaemonOptions): HostDaemon { let operation: HostUpdateOperation | null = null; try { operation = getHostUpdateOperation(); } catch (error) { if (!updateStateWarningLogged) log("could not read host update status: " + reason(error)); updateStateWarningLogged = true; } - if (operation?.target === "ompweb" && hostUpdateBlocksNewWork(operation)) throw new HostOpError(409, "host_updating", "This host is updating its daemon; new sessions must wait"); + if (hostUpdateBlocksNewWork(operation)) throw new HostOpError(409, "host_updating", "This host is updating its daemon; new sessions must wait"); const resolved = launch.creationId === undefined ? resolveSpawnCwdResult(launch.recordedCwd ?? launch.cwd) : { cwd: launch.cwd, fellBack: false }; // A sharing session never starts on this host's own logins: no link, or no usable broker, refuses. if (launch.credentialsServer !== undefined && !links.has(launch.credentialsServer)) throw new HostOpError(409, "credentials_link_gone", "The web server that shares its credentials with this session is no longer linked to this host"); @@ -465,7 +465,7 @@ export function runHostDaemon(options: DaemonOptions): HostDaemon { return checkHostUpdates(updateCompatibility(args.compatibility), args.force === true, args.targetCompatibility === undefined ? undefined : updateCompatibility(args.targetCompatibility), releasePin); } case "updateSoftware": - if (args.target !== "ompweb" || typeof args.version !== "string") throw new HostOpError(400, "invalid_request", "Invalid host update"); + if ((args.target !== "ompweb" && args.target !== "omp") || typeof args.version !== "string") throw new HostOpError(400, "invalid_request", "Invalid host update"); return startHostUpdate(args.target, args.version, updateCompatibility(args.compatibility), updateRequest(args)); case "preparePoolProject": { if (hostUpdateBlocksNewWork(getHostUpdateOperation())) throw new HostUpdateError("host_updating", "This host is updating; new project preparation must wait"); diff --git a/lib/hosts/host-cli.ts b/lib/hosts/host-cli.ts index 0bcae2696..4f2b9c57d 100644 --- a/lib/hosts/host-cli.ts +++ b/lib/hosts/host-cli.ts @@ -69,15 +69,19 @@ async function main(argv: string[]): Promise { return 1; } // Login/logout need none of the daemon, which jiti would otherwise transpile on every run. - const [{ runHostDaemon }, { hostSocketPath }, { ensureOmpRunner }] = await Promise.all([import("./daemon"), import("./omp-socket"), import("../omp/runner-fetch")]); - // Sessions need the runner; until it is here they fail with OMP_NOT_FOUND and the next start retries. - void ensureOmpRunner().catch((error: unknown) => console.error(`ompweb host: ${error instanceof Error ? error.message : String(error)}`)); + const [{ runHostDaemon }, { hostSocketPath }, { ensureOmpRunner, sweepOmpRunners }] = await Promise.all([import("./daemon"), import("./omp-socket"), import("../omp/runner-fetch")]); let daemon: HostDaemon | undefined; + // The endpoint is the proof that no other daemon runs on this machine: a second `ompweb host` stops here, before it + // touches anything the first one's sessions run from. const endpoint = await createHostControlServer((command) => { if (!daemon) throw new Error("The host daemon is starting"); return daemon.control(command); }); try { + // The runners of earlier "Update omp" installs: no session of this daemon is running from them any more. + sweepOmpRunners(); + // Sessions need the runner; until it is here they fail with OMP_NOT_FOUND and the next start retries. + void ensureOmpRunner().catch((error: unknown) => console.error(`ompweb host: ${error instanceof Error ? error.message : String(error)}`)); // Windows has no host socket yet (a current-user named pipe is open in the runner spec). daemon = runHostDaemon({ allowHttp: argv.includes("--allow-http"), hostSocket: process.platform === "win32" ? undefined : hostSocketPath() }); // Worker cutover starts fail-closed; ordinary hosts have no maintenance environment. diff --git a/lib/hosts/host-updates-route.test.mjs b/lib/hosts/host-updates-route.test.mjs index 956eaccc1..c075ff408 100644 --- a/lib/hosts/host-updates-route.test.mjs +++ b/lib/hosts/host-updates-route.test.mjs @@ -90,7 +90,7 @@ for (const protocol of [null, LINK_PROTOCOL_VERSION + 1, MIN_LINK_PROTOCOL_VERSI assert.ok(result.ompweb.blockedReason); assert.equal((await post(r, link.id)).status, 409); assert.equal(installs, 0); - assert.equal((await post(r, link.id, { target: "omp", version: "19.0.0" })).status, 400, "omp updates with ompweb"); + assert.equal((await post(r, link.id, { target: "omp", version: "19.0.0" })).status, 409, "a daemon without the runner channel updates omp only with ompweb"); assert.equal(installs, 0); }); @@ -103,6 +103,44 @@ test("the omp row carries only the host's omp version, also from a daemon that s assert.deepEqual(result.omp, { currentVersion: "18.20.0-int.202611130355.gd2c2eff" }); }); +test("a daemon with the runner channel reports omp updates and takes the omp target, never with force", async (t) => { + const r = await route(); + const link = await linkHost("Runner channel host"); + t.after(() => link.drop()); + const ompVersion = "18.23.0-int.202611201200.gabc1234"; + const omp = { currentVersion: "18.22.0-int.202611171027.gec3956c", availableVersion: ompVersion, updateAvailable: true, canUpdate: true, blockedReason: null, blockers: [], operation: null, runnerChannel: true }; + const seen = []; + link.serve({ + checkUpdates: () => ({ ...info(), omp: { ...omp, undeclared: "dropped" } }), + updateSoftware: (args) => { seen.push(args); return { accepted: true, operation: { id: "omp-1", target: "omp", version: ompVersion, state: "running" } }; }, + }); + const status = await (await r.GET(new Request(`http://main.test/api/hosts/${link.id}/updates`), context(link.id))).json(); + assert.deepEqual(status.omp, omp); + + const accepted = await post(r, link.id, { target: "omp", version: ompVersion }); + assert.equal(accepted.status, 202); + assert.equal((await accepted.json()).operation.target, "omp"); + assert.deepEqual(seen.map(({ target, version, force }) => [target, version, force]), [["omp", ompVersion, undefined]]); + const forced = await post(r, link.id, { target: "omp", version: ompVersion, force: true }); + assert.deepEqual([forced.status, (await forced.json()).code], [400, "invalid_host_update"]); + assert.equal(seen.length, 1); + + omp.canUpdate = false; + omp.blockedReason = "An ompweb update is active on this host; update omp once it has settled"; + omp.blockers = [{ code: "ompweb_update_active", message: omp.blockedReason, forceable: false }]; + omp.operation = { id: "omp-1", target: "omp", version: ompVersion, state: "failed", error: "earlier attempt" }; + const reported = await (await r.GET(new Request(`http://main.test/api/hosts/${link.id}/updates`), context(link.id))).json(); + assert.deepEqual([reported.omp.blockers, reported.omp.operation.state, reported.operation], [omp.blockers, "failed", null], "the omp record rides beside ompweb's, which stays null"); + const blocked = await post(r, link.id, { target: "omp", version: ompVersion }); + assert.deepEqual([blocked.status, (await blocked.json()).code], [409, "ompweb_update_active"], "a blocked omp update is not sent, and names its blocker"); + assert.equal(seen.length, 1); + omp.canUpdate = true; + omp.blockedReason = null; + omp.blockers = []; + assert.equal((await post(r, link.id, { target: "omp", version: "18.99.0" })).status, 409, "only the version the host offers"); + assert.equal(seen.length, 1); +}); + test("a recorded omp update operation from before omp updated with ompweb still reads; a bad omp version does not", async (t) => { const r = await route(); const link = await linkHost("Leftover omp operation"); @@ -138,7 +176,7 @@ test("the browser cannot supply compatibility, commands or an arbitrary target v t.after(() => link.drop()); let installs = 0; link.serve({ checkUpdates: () => info(), updateSoftware: () => { installs++; return { accepted: true, operation }; } }); - for (const bad of [{ ...body, compatibility: { min: 1, max: 99 } }, { target: "shell", version: "1.0.0" }, { target: "omp", version: "0.7.0" }, { ...body, version: "$(touch x)" }, { ...body, command: "reboot" }]) { + for (const bad of [{ ...body, compatibility: { min: 1, max: 99 } }, { target: "shell", version: "1.0.0" }, { target: "omp", version: "0.7.0", force: true }, { ...body, version: "$(touch x)" }, { ...body, command: "reboot" }]) { assert.equal((await post(r, link.id, bad)).status, 400); } assert.equal((await post(r, link.id, { ...body, version: "0.8.0" })).status, 409, "a stale candidate cannot be installed"); diff --git a/lib/hosts/hub.ts b/lib/hosts/hub.ts index cfacf465c..e5d488e9d 100644 --- a/lib/hosts/hub.ts +++ b/lib/hosts/hub.ts @@ -51,7 +51,7 @@ import type { SessionOps } from "./session-ops"; import { confirmPooledChild, pooledStartForChild } from "./pool-starts"; import { loadPools, type HostPool } from "./pools"; import { validateHostTools } from "./shared-requests"; -import type { ApprovedReleasePin, HostSoftwareUpdate, HostUpdateCompatibility, HostUpdateInfo, HostUpdateOperation, HostUpdateTarget } from "./update-types"; +import type { ApprovedReleasePin, HostOmpUpdate, HostSoftwareUpdate, HostUpdateBlocker, HostUpdateCompatibility, HostUpdateInfo, HostUpdateOperation, HostUpdateTarget } from "./update-types"; import { isUpdateDisabled } from "../update-policy"; import { WORKSPACE_OP_PARAMS, type WorkspaceFileEvent, type WorkspaceOps } from "../workspace-types"; import { validWorkspaceResult } from "./workspace-results"; @@ -1666,6 +1666,22 @@ function softwareUpdate(value: unknown): value is HostSoftwareUpdate & Record): HostOmpUpdate { + const blockers = value.blockers as Record[] | undefined; + const operation = value.operation; + if (!nullableString(value.currentVersion) || !nullableString(value.availableVersion) || typeof value.updateAvailable !== "boolean" || typeof value.canUpdate !== "boolean" || !nullableString(value.blockedReason) + || !(blockers === undefined || Array.isArray(blockers) && blockers.every((blocker) => isRecord(blocker) && typeof blocker.code === "string" && typeof blocker.message === "string" && typeof blocker.forceable === "boolean")) + || !(operation === undefined || operation === null || updateOperation(operation))) { + throw new HostOpError(502, "host_bad_result", "The host returned an invalid update status"); + } + return { + currentVersion: value.currentVersion, availableVersion: value.availableVersion, updateAvailable: value.updateAvailable, canUpdate: value.canUpdate, blockedReason: value.blockedReason, runnerChannel: true, + ...(blockers ? { blockers: blockers.map(({ code, message, forceable }) => ({ code, message, forceable }) as HostUpdateBlocker) } : {}), + ...(operation === undefined ? {} : { operation }), + }; +} + function updateOperation(value: unknown): value is HostUpdateOperation { return isRecord(value) && typeof value.id === "string" && value.id.length > 0 && value.id.length <= 128 && (value.target === "ompweb" || value.target === "omp") && typeof value.version === "string" @@ -1726,8 +1742,9 @@ export async function getHostSoftwareUpdates(id: string, force = false, compatib || !(result.operation === null || updateOperation(result.operation))) { throw new HostOpError(502, "host_bad_result", "The host returned an invalid update status"); } - // A daemon from before omp updated with ompweb reports a full update status for omp: keep only its version. - const info = { ...result, omp: { currentVersion: result.omp.currentVersion } } as unknown as HostUpdateInfo; + // A daemon that predates the runner channel reports only the omp version (and one older still reports a full update status for omp, which it ran as `omp update`): keep just the version unless the daemon declares the runner channel. + const omp = result.omp.runnerChannel === true ? ompRunnerUpdate(result.omp) : { currentVersion: result.omp.currentVersion }; + const info = { ...result, omp } as unknown as HostUpdateInfo; const protocol = info.ompweb.protocolVersion; if ((!releasePin || info.ompweb.supportsReleasePin === true) && info.ompweb.updateAvailable && (protocol === null || protocol < compatibility.min || protocol > compatibility.max)) { return { ...info, ompweb: { ...info.ompweb, canUpdate: false, canForceUpdate: false, blockedReason: protocol === null @@ -1809,7 +1826,8 @@ export async function updateHostSoftware(id: string, target: HostUpdateTarget, v const recovery = candidate.forceMethod === "recovery" || options.phase !== undefined && candidate.supportsTwoPhaseUpdate !== true; const pinnedRecovery = options.releasePin !== undefined && recovery; if (!matching && (!candidate.updateAvailable || !(options.force ? candidate.canForceUpdate || candidate.canUpdate : candidate.canUpdate) || candidate.availableVersion !== version && !pinnedRecovery)) { - throw new HostOpError(409, "host_update_blocked", candidate.blockedReason ?? "The available version changed; check for updates again"); + // An omp update names its blocker (ompweb_update_active, omp_install_active...) for a caller that must not read English. + throw new HostOpError(409, target === "omp" ? candidate.blockers?.[0]?.code ?? "host_update_blocked" : "host_update_blocked", candidate.blockedReason ?? "The available version changed; check for updates again"); } if (recovery && !options.force) throw new HostOpError(409, "host_update_blocked", "This host requires explicitly approved recovery"); if (options.force && recovery && id === LOCAL_HOST_ID) { diff --git a/lib/hosts/legacy-recovery.test.mjs b/lib/hosts/legacy-recovery.test.mjs index d609486b4..253bec56e 100644 --- a/lib/hosts/legacy-recovery.test.mjs +++ b/lib/hosts/legacy-recovery.test.mjs @@ -1,14 +1,13 @@ // The legacy recovery bootstrap (lib/hosts/legacy-recovery.ts). Generation and refusal tests run everywhere; the // execution tests run the real generated bootstrap on this machine's own platform against a local server that lays a // release out as GitHub does: POSIX uses the real sh, curl, tar, sha256sum/shasum and grep; Windows uses the real -// powershell.exe (.NET HttpClient, SHA256, ZipArchive) and a real compiled helper executable through both the runner's -// embedded shell and, when installed, Git Bash. Nothing is mocked on the executing side. +// powershell.exe (.NET HttpClient, SHA256, ZipArchive) and a real compiled helper executable through Git Bash when +// installed (omp's embedded shell is exercised by the runner's own tests in the oh-my-pi fork). Nothing is mocked on the executing side. import assert from "node:assert/strict"; import { spawn, spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs"; import http from "node:http"; -import { createRequire } from "node:module"; import { tmpdir } from "node:os"; import { join } from "node:path"; import test, { after } from "node:test"; @@ -306,19 +305,7 @@ function listen(routes) { return new Promise((resolve) => server.listen(0, "127.0.0.1", () => resolve({ server, requests, base: `http://127.0.0.1:${server.address().port}` }))); } -async function run(command, env, cwd, bash, embedded = false) { - if (embedded) { - const { Shell } = createRequire(new URL("../../runner/package.json", import.meta.url))("@oh-my-pi/pi-natives"); - const shell = new Shell({ sessionEnv: env }); - let stdout = ""; - try { - const result = await shell.run({ command, env, cwd, timeoutMs: 60_000 }, (error, chunk) => { - if (error) throw error; - stdout += chunk; - }); - return { code: result.exitCode, stdout, stderr: "" }; - } finally { await shell.abort(); } - } +async function run(command, env, cwd, bash) { // Native CreateProcess -> MSYS globbing truncates quoted -c arguments over 8192 characters (msys2-runtime#178). // Feed the script on stdin; the PowerShell argv still exercises MSYS conversion and its native command-line limit. const [file, args] = bash ? [GIT_BASH, ["-s"]] @@ -344,7 +331,7 @@ const defaultMembers = (helper) => [ ]; /** Runs the real generated bootstrap for this platform against a local server; returns what it did on disk. */ -async function scenario(t, { platform = process.platform, members = defaultMembers, digest, serve = true, exitCode = 0, force = false, bash = false, embedded = false, releasePin, phase } = {}) { +async function scenario(t, { platform = process.platform, members = defaultMembers, digest, serve = true, exitCode = 0, force = false, bash = false, releasePin, phase } = {}) { const tmp = realpathSync(mkdtempSync(join(tmpdir(), "legacy-recovery-"))); t.after(() => rmSync(tmp, { recursive: true, force: true })); const scratch = join(tmp, "scratch"); @@ -361,7 +348,7 @@ async function scenario(t, { platform = process.platform, members = defaultMembe ...process.env, TMPDIR: scratch, TMP: scratch, TEMP: scratch, RECOVERY_TEST_OUT: out, RECOVERY_TEST_EXIT: String(exitCode), NO_PROXY: "*", no_proxy: "*", HTTP_PROXY: "", http_proxy: "", ALL_PROXY: "", all_proxy: "", }; - const result = await run(command, env, cwd, bash, embedded); + const result = await run(command, env, cwd, bash); const prefix = `ompweb-recovery-${OPERATION}-`; return { ...result, requests, scratch: readdirSync(scratch), scratchDir: scratch, prefix, cwd, tmp, @@ -454,7 +441,6 @@ test("a bootstrap built for another operating system refuses to run here", { ski }); for (const [transport, options, available] of [ - ["the runner's embedded shell", { embedded: true }, WINDOWS], ["Git Bash", { bash: true }, WINDOWS && GIT_BASH], ]) { test(`the Windows bootstrap preserves the decoded script through ${transport}`, { skip: !SUPPORTED || !available, timeout: 120_000 }, async (t) => { @@ -466,7 +452,7 @@ for (const [transport, options, available] of [ assert.ok(command.length > 8192, "exercise the native-to-MSYS argument truncation boundary"); const result = await run(command, { ...process.env, PATH: `${tmp};${process.env.PATH}`, RECOVERY_TEST_DECODE_OUT: out, - }, tmp, options.bash, options.embedded); + }, tmp, options.bash); assert.equal(result.code, 0, result.stdout + result.stderr); const decoded = readFileSync(out); assert.deepEqual(decoded, Buffer.from(windowsScript(command), "utf16le"), "the native consumer receives every encoded byte"); diff --git a/lib/hosts/omp-socket.test.mjs b/lib/hosts/omp-socket.test.mjs index 51b14d43b..ab1741741 100644 --- a/lib/hosts/omp-socket.test.mjs +++ b/lib/hosts/omp-socket.test.mjs @@ -7,7 +7,6 @@ import { createInterface } from "node:readline"; import test, { after } from "node:test"; import { ChannelTable } from "./daemon-channels.ts"; import { CHANNEL_PROTO, hostSocketPath, listenHostSocket } from "./omp-socket.ts"; -import * as runner from "../../runner/src/channel.ts"; const SESSION = "33333333-3333-4333-8333-333333333333"; const scratch = mkdtempSync(join(tmpdir(), "omp-socket-test-")); @@ -75,14 +74,20 @@ test("an existing directory others can read is narrowed to 0700 before the socke await socket.close(); }); -test("the host and the runner agree on the socket path and the channel protocol for the same env", () => { - assert.equal(CHANNEL_PROTO, runner.CHANNEL_PROTO); +// The runner lives in the oh-my-pi fork (packages/ompweb-runner/src/channel.ts), which must give the same answers: a +// change here that an older runner cannot speak bumps RUNNER_PROTOCOL (omp-cli.ts). +test("the host's socket path and channel protocol, as the runner implements them", () => { + assert.equal(CHANNEL_PROTO, 1); const saved = { OMPWEB_HOST_SOCKET: process.env.OMPWEB_HOST_SOCKET, XDG_RUNTIME_DIR: process.env.XDG_RUNTIME_DIR }; try { - for (const env of [{}, { XDG_RUNTIME_DIR: "/run/user/1" }, { OMPWEB_HOST_SOCKET: "/x/h.sock", XDG_RUNTIME_DIR: "/run/user/1" }]) { + for (const [env, expected] of [ + [{}, join(tmpdir(), "ompweb", "host.sock")], + [{ XDG_RUNTIME_DIR: "/run/user/1" }, join("/run/user/1", "ompweb", "host.sock")], + [{ OMPWEB_HOST_SOCKET: "/x/h.sock", XDG_RUNTIME_DIR: "/run/user/1" }, "/x/h.sock"], + ]) { delete process.env.OMPWEB_HOST_SOCKET; delete process.env.XDG_RUNTIME_DIR; Object.assign(process.env, env); - assert.equal(hostSocketPath(), runner.hostSocketPath(), JSON.stringify(env)); + assert.equal(hostSocketPath(), expected, JSON.stringify(env)); } } finally { for (const [key, value] of Object.entries(saved)) if (value === undefined) delete process.env[key]; else process.env[key] = value; diff --git a/lib/hosts/omp-socket.ts b/lib/hosts/omp-socket.ts index 803200cee..08ee67c99 100644 --- a/lib/hosts/omp-socket.ts +++ b/lib/hosts/omp-socket.ts @@ -1,4 +1,4 @@ -// The host socket an omp started from a TUI dials (runner/src/channel.ts): each connection becomes a daemon channel, +// The host socket an omp started from a TUI dials (the runner's channel.ts, packages/ompweb-runner/src/ of the oh-my-pi fork): each connection becomes a daemon channel, // its lines the child's stdout, the server's lines written back, its close the child's exit. Unix only, never TCP. import { chmodSync, lstatSync, mkdirSync, unlinkSync } from "node:fs"; import { createConnection, createServer, type Socket } from "node:net"; @@ -12,10 +12,10 @@ import type { SpawnRequest, SpawnResult } from "./host"; const HELLO_TIMEOUT_MS = 10_000; const errno = (error: unknown) => (error as NodeJS.ErrnoException)?.code; -/** Must match `CHANNEL_PROTO` in runner/src/channel.ts (omp-socket.test.mjs checks). */ +/** Must match `CHANNEL_PROTO` in the runner's channel.ts (see above); a change there that an older ompweb cannot speak bumps RUNNER_PROTOCOL (omp-cli.ts) too. */ export const CHANNEL_PROTO = 1; -/** Must match `hostSocketPath()` in runner/src/channel.ts (omp-socket.test.mjs checks). */ +/** Must match `hostSocketPath()` in the runner's channel.ts (see above); omp-socket.test.mjs pins this side. */ export function hostSocketPath(): string { return process.env.OMPWEB_HOST_SOCKET || join(process.env.XDG_RUNTIME_DIR || tmpdir(), "ompweb", "host.sock"); } @@ -87,7 +87,7 @@ function helloLaunch(line: string): { pid: number; launch: SpawnRequest; session ...(typeof hello.sessionId === "string" ? { sessionId: hello.sessionId } : {}) }; } -/** Tells the runner not to redial (runner/src/channel.ts), then closes. */ +/** Tells the runner not to redial (its channel.ts), then closes. */ function refuse(socket: Socket, reason: string): void { socket.end(JSON.stringify({ t: "refused", reason }) + "\n"); } diff --git a/lib/hosts/omp-update.test.mjs b/lib/hosts/omp-update.test.mjs new file mode 100644 index 000000000..42fbb8777 --- /dev/null +++ b/lib/hosts/omp-update.test.mjs @@ -0,0 +1,297 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import test, { after, before } from "node:test"; + +// "Update omp": the daemon-side `omp` target of lib/hosts/updates.ts against fake fork releases. The runners are +// POSIX shell scripts answering --version, --runner-info and --smoke-test, the probe the real install runs. +const posix = process.platform !== "win32"; +const root = realpathSync(mkdtempSync(join(tmpdir(), "host-omp-update-"))); +const saved = { ...process.env }; +const originalFetch = globalThis.fetch; +const packageJson = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")); +const RUNNING = "18.22.0-int.202611171027.gec3956c"; +const NEWER = "18.23.0-int.202611201200.gabc1234"; +const OLDER = "18.21.0-int.202611100000.g1234567"; +const compatibility = { min: 1, max: 10 }; +let api, cli, fetched; +let releases = []; +let listing = 200; +/** What each fork release's assets hold: the binary served, the SHA-256 its listing claims for it, and its ompweb-omp.json protocol. */ +let assets = {}; +/** Runner binaries downloaded so far. */ +let downloads = 0; +/** While set, the runner binary's download waits for it: an omp install held in flight. */ +let gate = null; + +const runner = (version, protocol = cli.RUNNER_PROTOCOL) => `#!/bin/sh\ncase "$1" in\n --version) echo omp/${version} ;;\n --runner-info) echo '{"protocol":${protocol},"omp":"${version}"}' ;;\n --smoke-test) exit 0 ;;\nesac\n`; +const sha = (bytes) => createHash("sha256").update(bytes).digest("hex"); +const tag = (version) => `v${version}`; +const state = (name) => join(root, "agent", "omp-web", "host-update", name); +const settle = async () => { + for (let waited = 0; waited < 10_000; waited += 25) { + const operation = api.getOmpUpdateOperation(); + if (operation?.state !== "running") return operation; + await new Promise((resolve) => setTimeout(resolve, 25)); + } + throw new Error("the omp update did not finish"); +}; +const publish = (version, { protocol = cli.RUNNER_PROTOCOL, binary = runner(version, protocol), listed = sha(binary) } = {}) => { + assets[tag(version)] = { binary, listed, protocol }; + releases.unshift({ tag_name: tag(version), draft: false, prerelease: true, assets: [cli.OMP_RUNNER_ASSET, "ompweb-omp.json", "ompweb-omp.sha256"].map((name) => ({ name })) }); +}; + +before(() => { + if (!posix) return; + process.env.HOME = root; + process.env.USERPROFILE = root; + process.env.PI_CODING_AGENT_DIR = join(root, "agent"); + delete process.env.OMP_WEB_OMP_BIN; + delete process.env.OMP_WEB_PACKAGE_DIR; + delete process.env.OMP_WEB_DISABLE_AUTOUPDATE; + globalThis.fetch = async (url) => { + url = String(url); + if (url.startsWith("https://registry.npmjs.org/")) return new Response(JSON.stringify({ version: packageJson.version })); + if (url.startsWith(`https://api.github.com/repos/${cli.OMP_RUNNER_PIN.repo}/releases?per_page=100&page=`)) return listing === 200 ? new Response(JSON.stringify(url.endsWith("page=1") ? releases : [])) : new Response("busy", { status: listing }); + for (const [name, { binary, listed, protocol }] of Object.entries(assets)) { + if (url.endsWith(`/download/${name}/ompweb-omp.json`)) return new Response(JSON.stringify({ protocol, omp: name.slice(1) })); + if (url.endsWith(`/download/${name}/ompweb-omp.sha256`)) return new Response(`${listed} ${cli.OMP_RUNNER_ASSET}\n`); + if (url.endsWith(`/download/${name}/${cli.OMP_RUNNER_ASSET}`)) { downloads++; await gate; return new Response(binary); } + } + return new Response("missing", { status: 404 }); + }; +}); +after(() => { + globalThis.fetch = originalFetch; + for (const key of Object.keys(process.env)) if (!(key in saved)) delete process.env[key]; + Object.assign(process.env, saved); + rmSync(root, { recursive: true, force: true }); +}); +const run = (name, body) => test(name, { skip: posix ? false : "the fake runners are POSIX shell scripts" }, body); + +run("the check offers the newest fork release whose runner speaks this protocol, never an older omp, and says when it cannot look", async () => { + api = await import("./updates.ts"); + cli = await import("../omp/omp-cli.ts"); + fetched = cli.fetchedOmpBin(); + mkdirSync(dirname(fetched), { recursive: true }); + writeFileSync(fetched, runner(RUNNING), { mode: 0o755 }); + // Not integration builds, so no runner of theirs. + releases = [{ tag_name: "v18.9.0", draft: false, assets: [] }, { tag_name: `v${packageJson.version}`, draft: false, assets: [] }]; + const check = async () => (await api.checkHostUpdates(compatibility, true)).omp; + + const blockers = []; + assert.deepEqual(await check(), { currentVersion: RUNNING, availableVersion: null, updateAvailable: false, canUpdate: false, blockedReason: null, blockers, operation: null, runnerChannel: true }, "no fork release carries a runner"); + publish(OLDER); + const older = await check(); + assert.deepEqual([older.availableVersion, older.updateAvailable], [OLDER, false], "a build older than the running one is no update"); + publish(NEWER); + publish("18.20.0-int.202611050000.gfffffff"); + publish("18.23.9-int.202611210000.gdeadbee", { protocol: cli.RUNNER_PROTOCOL + 1 }); + assert.deepEqual(await check(), { currentVersion: RUNNING, availableVersion: NEWER, updateAvailable: true, canUpdate: true, blockedReason: null, blockers, operation: null, runnerChannel: true }); + + listing = 503; + const failed = await check(); + assert.deepEqual([failed.updateAvailable, failed.canUpdate, failed.availableVersion], [false, false, null]); + assert.match(failed.blockedReason, /could not check/i); + assert.equal(failed.blockers[0].code, "check_failed"); + listing = 200; + + process.env.OMP_WEB_DISABLE_AUTOUPDATE = "1"; + const disabled = await check(); + assert.deepEqual([disabled.updateAvailable, disabled.canUpdate], [true, false]); + assert.match(disabled.blockedReason, /disabled/i); + assert.equal(disabled.blockers[0].code, "updates_disabled"); + await assert.rejects(api.startHostUpdate("omp", NEWER, compatibility), (error) => error.code === "updates_disabled"); + delete process.env.OMP_WEB_DISABLE_AUTOUPDATE; +}); + +run("an omp update takes only a version: force, phases and release pins are refused, and so is a stale candidate", async () => { + for (const request of [{ force: true }, { phase: "prepare", operationId: "12345678-1234-4234-8234-123456789012" }, { releasePin: { version: "9.9.9", releaseRepo: "ompweb-dev/ompweb", assets: {} } }, { targetCompatibility: compatibility }]) { + await assert.rejects(api.startHostUpdate("omp", NEWER, compatibility, request), (error) => error.code === "invalid_request", JSON.stringify(request)); + } + await assert.rejects(api.startHostUpdate("omp", OLDER, compatibility), (error) => error.code === "candidate_changed"); + await assert.rejects(api.startHostUpdate("omp", "18.23.0; rm -rf /", compatibility), (error) => error.code === "invalid_version"); + assert.equal(api.getHostUpdateOperation(), null, "a refused start records nothing"); + assert.equal(api.getOmpUpdateOperation(), null); + assert.equal(existsSync(state("lock.json")), false, "and keeps no claim"); +}); + +run("a build whose checksum does not match fails the operation and changes nothing", async () => { + assets[tag(NEWER)] = { binary: runner(NEWER), listed: sha("not this binary"), protocol: cli.RUNNER_PROTOCOL }; + const started = await api.startHostUpdate("omp", NEWER, compatibility); + assert.deepEqual([started.accepted, started.operation.target, started.operation.version, started.operation.state], [true, "omp", NEWER, "running"]); + const finished = await settle(); + assert.deepEqual([finished.id, finished.state], [started.operation.id, "failed"]); + assert.match(finished.error, /SHA-256/); + assert.equal(cli.readOmpPointer(), null); + assert.equal(cli.resolveOmpBin(true), fetched); + assert.equal(existsSync(join(cli.ompRunnerRoot(), `v${NEWER}`)), false); + assert.equal(existsSync(state("status.json")), false, "ompweb's operation record is never written by an omp update"); + assert.equal(existsSync(state("lock.json")), false, "the claim is released"); +}); + +run("an omp update installs beside the running runner, interrupts no work, and new spawns use it afterwards", async () => { + assets[tag(NEWER)] = { binary: runner(NEWER), listed: sha(runner(NEWER)), protocol: cli.RUNNER_PROTOCOL }; + const started = await api.startHostUpdate("omp", NEWER, compatibility); + assert.equal(started.operation.state, "running"); + assert.equal(api.hostUpdateBlocksNewWork(started.operation), false, "sessions start and prepare as usual while it runs"); + const finished = await settle(); + assert.equal(finished.state, "succeeded", finished.error); + + const pointer = cli.readOmpPointer(); + assert.equal(pointer.tag, `v${NEWER}`); + assert.equal(cli.resolveOmpBin(), pointer.path); + assert.notEqual(pointer.path, fetched); + assert.equal(readFileSync(fetched, "utf8"), runner(RUNNING), "the runner already in use is left alone"); + assert.equal(await cli.getOmpVersion(), `omp/${NEWER}`); + const after = (await api.checkHostUpdates(compatibility, true)).omp; + assert.deepEqual([after.currentVersion, after.updateAvailable, after.operation.state], [NEWER, false, "succeeded"], "the check reports the omp update's own record"); + assert.equal(api.getHostUpdateOperation(), null, "ompweb's record stays empty"); + assert.equal(existsSync(state("status.json")), false); + assert.equal(existsSync(state("lock.json")), false); +}); + +const NEXT = "18.24.0-int.202611250000.g7654321"; +const AFTER_NEXT = "18.25.0-int.202611260000.g1111111"; +const ompwebOperation = (extra) => ({ operation: { id: "ompweb-update-1", target: "ompweb", version: "99.0.0", state: "running", ...extra }, startedAt: Date.now(), daemonPid: process.pid }); + +run("an omp update never overwrites or races an ompweb update: it is refused while one is active or unsettled, and leaves its record alone when it runs", async () => { + publish(NEXT); + const check = async () => (await api.checkHostUpdates(compatibility, true)).omp; + const refused = async (why) => { + const omp = await check(); + assert.deepEqual([omp.updateAvailable, omp.canUpdate, omp.blockers.map((blocker) => blocker.code)], [true, false, ["ompweb_update_active"]], why); + await assert.rejects(api.startHostUpdate("omp", NEXT, compatibility), (error) => error.code === "ompweb_update_active", why); + assert.equal(api.getOmpUpdateOperation().version, NEWER, `${why}: the omp record is as it was`); + }; + + // An ompweb claim with no record yet (between its claim and its first status write). + writeFileSync(state("lock.json"), JSON.stringify({ id: "ompweb-update-1", pid: process.pid, at: Date.now() }), { mode: 0o600 }); + await refused("a live ompweb claim"); + rmSync(state("lock.json")); + + // A running ompweb operation: the record is ompweb's own and must come out byte for byte. + writeFileSync(state("status.json"), JSON.stringify(ompwebOperation()), { mode: 0o600 }); + const running = readFileSync(state("status.json"), "utf8"); + await refused("a running ompweb operation"); + assert.equal(readFileSync(state("status.json"), "utf8"), running); + + // A settled one does not hold anything: the omp update runs, and still leaves ompweb's record untouched. + writeFileSync(state("status.json"), JSON.stringify(ompwebOperation({ state: "succeeded" })), { mode: 0o600 }); + const settled = readFileSync(state("status.json"), "utf8"); + const started = await api.startHostUpdate("omp", NEXT, compatibility); + assert.deepEqual([started.operation.target, started.operation.id === "ompweb-update-1"], ["omp", false]); + assert.equal((await settle()).state, "succeeded"); + assert.equal(readFileSync(state("status.json"), "utf8"), settled, "ompweb's record is not overwritten"); + assert.equal(api.getHostUpdateOperation().id, "ompweb-update-1", "and still reads as ompweb's"); + assert.equal(cli.readOmpPointer().tag, `v${NEXT}`); +}); + +run("an ompweb update is refused while an omp install is in flight, which keeps its own record", async () => { + publish(AFTER_NEXT); + const ompwebRecord = readFileSync(state("status.json"), "utf8"); + let release; + gate = new Promise((resolve) => { release = resolve; }); + try { + const started = await api.startHostUpdate("omp", AFTER_NEXT, compatibility); + assert.equal(started.operation.state, "running"); + + const info = await api.checkHostUpdates(compatibility, true); + assert.deepEqual(info.ompweb.blockers.map((blocker) => blocker.code), ["omp_install_active"]); + assert.equal(info.ompweb.canUpdate, false); + assert.deepEqual([info.omp.canUpdate, info.omp.blockers.map((blocker) => blocker.code), info.omp.operation.id], [false, ["omp_install_active"], started.operation.id]); + assert.equal(info.operation.id, "ompweb-update-1", "the status's operation stays ompweb's"); + await assert.rejects(api.startHostUpdate("ompweb", "99.0.0", compatibility), (error) => error.code === "update_in_progress"); + await assert.rejects(api.startHostUpdate("omp", AFTER_NEXT, compatibility), (error) => error.code === "omp_install_active"); + assert.equal(readFileSync(state("status.json"), "utf8"), ompwebRecord, "ompweb's record is untouched while the install runs"); + } finally { + release(); + gate = null; + } + assert.equal((await settle()).state, "succeeded"); + assert.equal(readFileSync(state("status.json"), "utf8"), ompwebRecord); + assert.equal(existsSync(state("lock.json")), false, "the install released the claim, so ompweb can update again"); +}); + +const REPLAYED = "9d3d3f5e-3c33-4d6e-8c1f-5a5a5a5a5a5a"; +const OMPWEB_OPERATION = "0f0f0f0f-1111-4222-8333-444444444444"; +const AFTER_REPLAY = "18.26.0-int.202611270000.g2222222"; +const AFTER_THAT = "18.27.0-int.202611280000.g3333333"; + +run("an operationId names one omp update: a replay returns the stored operation, while running or settled, and never installs again; anything else about that id is a conflict", async () => { + publish(AFTER_REPLAY); + let release; + gate = new Promise((resolve) => { release = resolve; }); + let started; + try { + started = await api.startHostUpdate("omp", AFTER_REPLAY, compatibility, { operationId: REPLAYED }); + assert.deepEqual([started.operation.id, started.operation.version, started.operation.state], [REPLAYED, AFTER_REPLAY, "running"]); + assert.deepEqual(await api.startHostUpdate("omp", AFTER_REPLAY, compatibility, { operationId: REPLAYED }), started, "a replay while it runs returns the running operation, not omp_install_active"); + } finally { + release(); + gate = null; + } + assert.equal((await settle()).state, "succeeded"); + const installs = downloads; + const pointer = readFileSync(join(cli.ompRunnerRoot(), "current.json"), "utf8"); + + const settled = await api.startHostUpdate("omp", AFTER_REPLAY, compatibility, { operationId: REPLAYED }); + assert.deepEqual([settled.accepted, settled.operation.id, settled.operation.state], [true, REPLAYED, "succeeded"], "a replay after it settled returns the stored operation, though no update is left to offer"); + assert.equal(downloads, installs, "nothing is downloaded again"); + assert.equal(readFileSync(join(cli.ompRunnerRoot(), "current.json"), "utf8"), pointer); + assert.equal(existsSync(state("lock.json")), false, "and no claim is taken"); + + publish(AFTER_THAT); + await assert.rejects(api.startHostUpdate("omp", AFTER_THAT, compatibility, { operationId: REPLAYED }), (error) => error.code === "operation_conflict", "the same id for another version"); + assert.equal(downloads, installs); + await assert.rejects(api.startHostUpdate("omp", AFTER_REPLAY, compatibility, { operationId: REPLAYED, force: true }), (error) => error.code === "invalid_request", "a replay that adds anything but the id is refused too"); + + writeFileSync(state("status.json"), JSON.stringify(ompwebOperation({ id: OMPWEB_OPERATION, state: "succeeded" })), { mode: 0o600 }); + await assert.rejects(api.startHostUpdate("omp", AFTER_THAT, compatibility, { operationId: OMPWEB_OPERATION }), (error) => error.code === "operation_conflict", "an ompweb operation's id is not an omp update's"); + await assert.rejects(api.startHostUpdate("ompweb", "99.0.0", compatibility, { operationId: REPLAYED }), (error) => error.code === "operation_conflict", "an omp operation's id is not an ompweb update's"); + assert.equal(api.getOmpUpdateOperation().id, REPLAYED, "the omp record is as it was"); + assert.equal(downloads, installs); + + const fresh = await api.startHostUpdate("omp", AFTER_THAT, compatibility); + assert.notEqual(fresh.operation.id, REPLAYED, "an update without an id still gets its own"); + assert.equal((await settle()).state, "succeeded"); +}); + +const BROKEN = "18.28.0-int.202611290000.g4444444"; +const attemptId = (n) => `00000000-0000-4000-8000-${String(n).padStart(12, "0")}`; + +run("an operationId an earlier attempt used and a later one superseded is refused, never installed again; only the newest attempt's id replays", async () => { + publish(BROKEN, { listed: sha("not this binary") }); + const [first, second] = [attemptId(1), attemptId(2)]; + for (const operationId of [first, second]) { + await api.startHostUpdate("omp", BROKEN, compatibility, { operationId }); + assert.equal((await settle()).state, "failed"); + } + const installs = downloads; + const record = readFileSync(state("omp-update.json"), "utf8"); + + await assert.rejects(api.startHostUpdate("omp", BROKEN, compatibility, { operationId: first }), (error) => error.code === "operation_conflict" && /superseded/.test(error.message), "A failed, B failed: replaying A"); + assert.equal(downloads, installs, "nothing is downloaded again"); + assert.equal(readFileSync(state("omp-update.json"), "utf8"), record, "the record is as it was"); + assert.equal(existsSync(state("lock.json")), false, "no claim is taken"); + const replayed = await api.startHostUpdate("omp", BROKEN, compatibility, { operationId: second }); + assert.deepEqual([replayed.operation.id, replayed.operation.state], [second, "failed"], "the newest attempt still replays"); + assert.equal(downloads, installs); +}); + +run("the superseded ids are a bounded list: the oldest ages out, the latest 32 are kept", async () => { + const ids = Array.from({ length: 40 }, (_, n) => attemptId(100 + n)); + for (const operationId of ids) { + await api.startHostUpdate("omp", BROKEN, compatibility, { operationId }); + assert.equal((await settle()).state, "failed"); + } + const { earlier, operation } = JSON.parse(readFileSync(state("omp-update.json"), "utf8")); + assert.equal(operation.id, ids[39]); + assert.deepEqual(earlier, [...ids.slice(7, 39)], "the 32 attempts before the newest, oldest first"); + await assert.rejects(api.startHostUpdate("omp", BROKEN, compatibility, { operationId: ids[7] }), (error) => error.code === "operation_conflict"); + const aged = await api.startHostUpdate("omp", BROKEN, compatibility, { operationId: attemptId(2) }); + assert.deepEqual([aged.operation.id, aged.operation.state], [attemptId(2), "running"], "an id older than the bound is as unknown as any new one"); + await settle(); +}); diff --git a/lib/hosts/pending-requests.ts b/lib/hosts/pending-requests.ts index cb0c7adce..9ec1469b9 100644 --- a/lib/hosts/pending-requests.ts +++ b/lib/hosts/pending-requests.ts @@ -4,7 +4,7 @@ import { isValidSessionId } from "../session-file-references-core"; import { isRecord } from "../type-guards"; -/** One line of the runner channel (runner/src/channel.ts), keyed by `t`. */ +/** One line of the runner channel (packages/ompweb-runner/src/channel.ts of the oh-my-pi fork), keyed by `t`. */ export type WireFrame = { t: string; [key: string]: unknown }; /** An AgentSession event inside an `event` frame. */ export type WireEvent = { type: string; [key: string]: unknown }; diff --git a/lib/hosts/update-fixture.test-helper.mjs b/lib/hosts/update-fixture.test-helper.mjs index 9d53f2c01..93ff7e797 100644 --- a/lib/hosts/update-fixture.test-helper.mjs +++ b/lib/hosts/update-fixture.test-helper.mjs @@ -20,8 +20,10 @@ const RELEASE_MODULES = [ "lib/self-update.ts", "lib/project-command-env.ts", "lib/omp/omp-cli.ts", + "lib/omp/runner-fetch.ts", "lib/omp/paths.ts", "lib/release-repo.ts", + "omp-runner.json", "lib/process-identity.ts", "lib/upgrade-process.ts", "lib/type-guards.ts", diff --git a/lib/hosts/update-types.ts b/lib/hosts/update-types.ts index d9d7dfbf8..a0d09046a 100644 --- a/lib/hosts/update-types.ts +++ b/lib/hosts/update-types.ts @@ -1,7 +1,7 @@ import type { NpmUpdateStatus } from "../npm-update"; -/** omp is not a target: it updates with ompweb, whose release ships its runner. */ -export type HostUpdateTarget = "ompweb"; +/** What a host updates: its ompweb install, and its omp runner (a newer fork release's, between ompweb releases: lib/omp/runner-fetch.ts). */ +export type HostUpdateTarget = "ompweb" | "omp"; /** The running web server's supported daemon protocol range. Supplied by the hub, never by the browser. */ export interface HostUpdateCompatibility { @@ -26,7 +26,7 @@ export interface ApprovedReleasePin { * maintenance drain) is never forceable. */ export interface HostUpdateBlocker { - code: "updates_disabled" | "check_failed" | "protocol_unverified" | "protocol_incompatible" | "update_in_progress" | "unsupported_install" | "shared_install" | "supervisor_unsupported" | "host_drained" | "host_busy"; + code: "updates_disabled" | "check_failed" | "protocol_unverified" | "protocol_incompatible" | "update_in_progress" | "ompweb_update_active" | "omp_install_active" | "unsupported_install" | "shared_install" | "supervisor_unsupported" | "host_drained" | "host_busy"; message: string; forceable: boolean; coordination?: "server"; @@ -54,8 +54,8 @@ export interface HostSoftwareUpdate { export interface HostUpdateOperation { /** The operation id the caller supplied (HostUpdateRequest.operationId), else a generated one; the same id names the attempt, its worker and every acknowledgement. */ id: string; - /** "omp": an `omp update` a host recorded before omp updated with ompweb. */ - target: HostUpdateTarget | "omp"; + /** "ompweb" restarts the daemon; "omp" only changes the runner new sessions start. */ + target: HostUpdateTarget; version: string; state: "running" | "succeeded" | "failed"; /** All candidate artifacts passed preflight; the worker is waiting for an explicit coordinated commit. */ @@ -87,8 +87,17 @@ export interface HostUpdateRequest { phase?: "prepare" | "commit" | "cancel"; } +/** + * The runner (`omp`) on a host. `runnerChannel` is set by a daemon that checks fork releases for runners; a daemon without it + * reports only `currentVersion`, and its other fields are never read. No force: an omp update interrupts nothing, so a + * blocked one waits for its blocker (`ompweb_update_active` while an ompweb update holds or has unsettled the host's update + * claim, `omp_install_active` while an omp install runs; an ompweb update is blocked by `omp_install_active` likewise). + * `operation` is the omp update's own record, never `HostUpdateInfo.operation`, which stays ompweb's. + */ +export type HostOmpUpdate = Partial & { currentVersion: string | null; runnerChannel?: true; operation?: HostUpdateOperation | null }; + export interface HostUpdateInfo { ompweb: HostSoftwareUpdate & Pick & { protocolVersion: number | null; activeSessions?: number }; - omp: { currentVersion: string | null }; + omp: HostOmpUpdate; operation: HostUpdateOperation | null; } diff --git a/lib/hosts/updates.test.mjs b/lib/hosts/updates.test.mjs index 539295797..195f8288d 100644 --- a/lib/hosts/updates.test.mjs +++ b/lib/hosts/updates.test.mjs @@ -55,6 +55,8 @@ after(() => { rmSync(root, { recursive: true, force: true }); }); +const OVERRIDDEN_OMP = { currentVersion: STAMPED, availableVersion: null, updateAvailable: false, canUpdate: false, blockedReason: "This host runs omp from OMP_WEB_OMP_BIN; update that build instead", blockers: [{ code: "unsupported_install", message: "This host runs omp from OMP_WEB_OMP_BIN; update that build instead", forceable: false }], operation: null, runnerChannel: true }; + test("candidate protocol outside server range is visible but cannot execute", async () => { const info = await api.checkHostUpdates({ min: 1, max: 2 }, true); assert.equal(info.ompweb.availableVersion, "99.0.0"); @@ -64,7 +66,7 @@ test("candidate protocol outside server range is visible but cannot execute", as assert.equal(info.ompweb.canUpdate, false); assert.match(info.ompweb.blockedReason, /server first/i); await assert.rejects(api.startHostUpdate("ompweb", "99.0.0", { min: 1, max: 2 }), /server first/i); - assert.deepEqual(info.omp, { currentVersion: STAMPED }, "omp updates with ompweb: its row carries only the version"); + assert.deepEqual(info.omp, OVERRIDDEN_OMP, "an omp the install does not choose (OMP_WEB_OMP_BIN) has no runner update"); assert.ok(worker.HOST_VERSION_PATTERN.test(STAMPED)); }); @@ -88,8 +90,9 @@ test("npm discovery failure stays explicit instead of claiming up to date", asyn failedNpm = false; }); -test("omp, version syntax and a stale candidate are rejected before execution", async () => { - await assert.rejects(api.startHostUpdate("omp", "2.0.0", { min: 1, max: 10 }), (error) => error.code === "invalid_target"); +test("an unknown target, version syntax and a stale candidate are rejected before execution", async () => { + await assert.rejects(api.startHostUpdate("node", "2.0.0", { min: 1, max: 10 }), (error) => error.code === "invalid_target"); + await assert.rejects(api.startHostUpdate("omp", "18.23.0-int.202611201200.gabc1234", { min: 1, max: 10 }), (error) => error.code === "candidate_changed"); await assert.rejects(api.startHostUpdate("ompweb", "2.0.0; touch /tmp/unwanted", { min: 1, max: 10 }), /version/i); await assert.rejects(api.startHostUpdate("ompweb", "1.9.0", { min: 1, max: 10 }), /changed|candidate|available/i); }); @@ -100,7 +103,7 @@ test("disable policy blocks checks and starts", async () => { assert.equal(info.ompweb.canUpdate, false); assert.match(info.ompweb.blockedReason, /disabled/i); await assert.rejects(api.startHostUpdate("ompweb", "2.0.0", { min: 1, max: 10 }), /disabled/i); - assert.deepEqual(info.omp, { currentVersion: STAMPED }); + assert.deepEqual(info.omp, OVERRIDDEN_OMP); delete process.env.OMP_WEB_DISABLE_AUTOUPDATE; }); @@ -141,7 +144,7 @@ test("an approved pinned version still refuses incompatible protocol", async () test("owned durable state survives reload and refuses a symlinked update directory", async () => { resetState(); ownedDirectory(stateRoot); - // An `omp update` a host recorded before omp updated with ompweb stays readable. + // An omp operation (the "Update omp" target) is recorded and read like an ompweb one. const operation = { id: "a", target: "omp", version: "2.0.0", state: "failed", error: "fixture failure" }; writeFileSync(join(stateRoot, "status.json"), JSON.stringify({ operation, startedAt: Date.now(), daemonPid: process.pid }), { mode: 0o600 }); assert.deepEqual(api.getHostUpdateOperation(), operation); diff --git a/lib/hosts/updates.ts b/lib/hosts/updates.ts index adc08c3b9..3e5258e59 100644 --- a/lib/hosts/updates.ts +++ b/lib/hosts/updates.ts @@ -5,6 +5,7 @@ import { randomUUID } from "node:crypto"; import packageJson from "../../package.json"; import { checkNpmUpdate, detectInstallMethod } from "../npm-update"; import { getOmpVersion } from "../omp/omp-cli"; +import { installOmpRunner, latestOmpRunnerRelease } from "../omp/runner-fetch"; import { createChildProcessEnvironment } from "../project-command-env"; import { isUpdateDisabled } from "../update-policy"; import { isNewerVersion } from "../version-compare"; @@ -12,7 +13,7 @@ import { releaseRepo } from "../release-repo"; import { assertUpgradeProcessControl, inspectUpgradeProcess, launchDetachedUpgrade, type UpgradeProcess } from "../upgrade-process"; import { processStartIdentity, isProcessAlive } from "../process-identity"; import { isRecord } from "../type-guards"; -import type { ApprovedReleasePin, HostSoftwareUpdate, HostUpdateBlocker, HostUpdateCompatibility, HostUpdateInfo, HostUpdateOperation, HostUpdateRequest, HostUpdateTarget } from "./update-types"; +import type { ApprovedReleasePin, HostOmpUpdate, HostSoftwareUpdate, HostUpdateBlocker, HostUpdateCompatibility, HostUpdateInfo, HostUpdateOperation, HostUpdateRequest, HostUpdateTarget } from "./update-types"; import { assertOwnedPath, ensureHostUpdateRoot, fetchHostUpdateBytes, finishHostUpdate, HOST_INSTALL_SHARED, HOST_VERSION_PATTERN, hostLaunch, hostReleaseName, hostUpdateRoot, installedHostVersionMatches, pruneHostUpdateArtifacts, readHostUpdateState, requireHostUpdateRecovery, sharedHostInstallReason, validateApprovedReleasePin, writeHostUpdateDecision, writeHostUpdateJson, type HostUpdateDescriptor } from "./update-worker"; // Covers an ordinary worker's 15 min release download plus preflight, stop and restart; coordinated prepared/committing workers remain governed by liveness. @@ -53,9 +54,9 @@ export function getHostUpdateOperation(): HostUpdateOperation | null { return state.operation; } -/** Forced staging permits work until preparation finishes; the coordinated gate then fences the approved inventory. */ +/** Forced staging permits work until preparation finishes; the coordinated gate then fences the approved inventory. An omp update only swaps the runner new sessions start: it never holds work back. */ export function hostUpdateBlocksNewWork(operation: HostUpdateOperation | null): boolean { - return operation?.state === "running" && !(operation.force === true && operation.committing !== true && operation.prepared !== true); + return operation?.target === "ompweb" && operation.state === "running" && !(operation.force === true && operation.committing !== true && operation.prepared !== true); } /** Called only after the new daemon's link is welcomed. Rollback daemons also acknowledge their old version. */ @@ -203,7 +204,7 @@ async function evaluateHostUpdate(compatibility: HostUpdateCompatibility, refres base.supportsTwoPhaseUpdate = true; const installMethod = typeof Bun !== "undefined" && Bun.isStandaloneExecutable ? "binary" : detectInstallMethod(installDirectory()); const ompweb: HostUpdateInfo["ompweb"] = { ...base, ...npm, currentVersion: packageJson.version, installMethod, protocolVersion: null, activeSessions: safetyState.sessions(), updateCommand: installMethod === "binary" ? `https://github.com/${releaseRepo()}/releases/latest` : installMethod === "bun" ? "bun add -g @kahme247/ompweb" : "npm install -g @kahme247/ompweb" }; - // omp updates with ompweb (its runner ships inside each release): the row only reports the running version. + // Only the running version here; checkHostUpdates adds the runner channel's update status (evaluateOmpUpdate). const runtime: HostUpdateInfo["omp"] = { currentVersion: ompVersion?.replace(/^omp\//, "") ?? null }; const blockers: HostUpdateBlocker[] = []; let plan: HostRestartPlan | null = null; @@ -226,6 +227,7 @@ async function evaluateHostUpdate(compatibility: HostUpdateCompatibility, refres } } if (operation?.state === "running") blockers.push({ code: "update_in_progress", message: "Another host update is in progress", forceable: false }); + else if (liveClaim()?.target === "omp") blockers.push({ code: "omp_install_active", message: "An omp update is installing on this host; update ompweb once it has finished", forceable: false }); ompweb.blockers = blockers; ompweb.blockedReason = blockers[0]?.message ?? null; ompweb.canUpdate = ompweb.updateAvailable && blockers.length === 0; @@ -234,19 +236,114 @@ async function evaluateHostUpdate(compatibility: HostUpdateCompatibility, refres return { info: { ompweb, omp: runtime, operation }, plan, awaitsServerCutover }; } +/** The update claim while it is live (its holder runs and its lease is young), and what it was taken for; a lock without a target is an ompweb update's. */ +function liveClaim(): { id: string | undefined; target: HostUpdateTarget } | null { + try { + const lock = join(hostUpdateRoot(), "lock.json"); + if (!existsSync(lock)) return null; + assertOwnedPath(lock); + const held: unknown = JSON.parse(readFileSync(lock, "utf8")); + if (!isRecord(held) || typeof held.at !== "number" || Date.now() - held.at >= LEASE_MS || !isProcessAlive(typeof held.pid === "number" ? held.pid : undefined)) return null; + return { id: typeof held.id === "string" ? held.id : undefined, target: held.target === "omp" ? "omp" : "ompweb" }; + } catch { return null; } +} + +// An omp update keeps its own record. status.json belongs to the ompweb update that wrote it (a coordinated upgrade +// verifies its receipt there), so an omp update never writes it; the claim lock keeps the two from running together. +const OMP_STATE = "omp-update.json"; +/** How many superseded attempts' ids the record keeps: a replay of an older one is the same as any unknown id. */ +const OMP_EARLIER_MAX = 32; +interface OmpUpdateState { + operation: HostUpdateOperation; + startedAt: number; + daemonPid: number; + /** Ids of the attempts `operation` superseded, oldest first (at most OMP_EARLIER_MAX): a replay of one is `operation_conflict`, not a new install. */ + earlier?: string[]; +} +function readOmpUpdate(): OmpUpdateState | null { + const root = hostUpdateRoot(); + const file = join(root, OMP_STATE); + if (!existsSync(file)) return null; + assertOwnedPath(root, true); + assertOwnedPath(file); + const state: unknown = JSON.parse(readFileSync(file, "utf8")); + if (!isRecord(state) || !isRecord(state.operation) || state.operation.target !== "omp" || !["running", "failed", "succeeded"].includes(String(state.operation.state)) + || typeof state.operation.id !== "string" || !/^[a-zA-Z0-9-]{1,80}$/.test(state.operation.id) || typeof state.operation.version !== "string" || !HOST_VERSION_PATTERN.test(state.operation.version) + || typeof state.startedAt !== "number" || !Number.isFinite(state.startedAt) || typeof state.daemonPid !== "number") throw new Error("Invalid omp update status"); + const earlier = Array.isArray(state.earlier) ? state.earlier.filter((id): id is string => typeof id === "string" && /^[a-zA-Z0-9-]{1,80}$/.test(id)).slice(-OMP_EARLIER_MAX) : []; + return { ...state, earlier } as unknown as OmpUpdateState; +} +/** The stored omp record, null when there is none or it is unreadable (an unreadable one never blocks an update, and keeps no history). */ +function ompUpdateRecord(): OmpUpdateState | null { + try { return readOmpUpdate(); } catch { return null; } +} +function finishOmpUpdate(id: string, error?: string): void { + const state = readOmpUpdate(); + if (state?.operation.id === id) writeHostUpdateJson(join(hostUpdateRoot(), OMP_STATE), { ...state, operation: { ...state.operation, state: error ? "failed" : "succeeded", ...(error ? { error: error.slice(0, 1000) } : {}) } }); + releaseHostUpdateClaim(hostUpdateRoot(), id); +} +/** The omp update's own record. A running one whose daemon died, or whose lease ran out, is recorded as failed. An unreadable record reads as none: it never blocks an ompweb check. */ +export function getOmpUpdateOperation(): HostUpdateOperation | null { + try { + const state = readOmpUpdate(); + if (!state) return null; + if (state.operation.state === "running" && (Date.now() - state.startedAt > LEASE_MS || (Date.now() - state.startedAt > 30000 && !isProcessAlive(state.daemonPid)))) { + finishOmpUpdate(state.operation.id, "omp update was interrupted before completion"); + return readOmpUpdate()?.operation ?? null; + } + return state.operation; + } catch { return null; } +} + +/** Why an omp update cannot start now: the disable switch, another omp install, or an ompweb update (any live claim or unsettled operation that is not `ownClaim`). */ +function ompUpdateBlocker(ownClaim?: string): HostUpdateBlocker | null { + if (isUpdateDisabled()) return { code: "updates_disabled", message: "Updates are disabled by OMP_WEB_DISABLE_AUTOUPDATE", forceable: false }; + const claim = liveClaim(); + if (getOmpUpdateOperation()?.state === "running" || (claim?.target === "omp" && claim.id !== ownClaim)) return { code: "omp_install_active", message: "An omp update is already installing on this host", forceable: false }; + if (getHostUpdateOperation()?.state === "running" || (claim && claim.id !== ownClaim)) return { code: "ompweb_update_active", message: "An ompweb update is active on this host; update omp once it has settled", forceable: false }; + return null; +} + +/** + * The runner channel's view of this host's omp: the newest published fork release with a runner for this platform and + * RUNNER_PROTOCOL (lib/omp/runner-fetch.ts) against the running omp, available when it is a different build and, where both + * carry a build stamp, a newer one (never a downgrade). Blocked by the disable switch, by an ompweb update or another omp + * install (ompUpdateBlocker), and by an omp this install does not choose (OMP_WEB_OMP_BIN), which the pointer "Update omp" + * writes would not change. + */ +async function evaluateOmpUpdate(currentVersion: string | null, refresh: boolean, ownClaim?: string): Promise { + const omp: HostOmpUpdate = { currentVersion, availableVersion: null, updateAvailable: false, canUpdate: false, blockedReason: null, blockers: [], operation: getOmpUpdateOperation(), runnerChannel: true }; + const blocked = (blocker: HostUpdateBlocker): HostOmpUpdate => ({ ...omp, blockers: [blocker], blockedReason: blocker.message }); + if (process.env.OMP_WEB_OMP_BIN) return blocked({ code: "unsupported_install", message: "This host runs omp from OMP_WEB_OMP_BIN; update that build instead", forceable: false }); + let release; + try { release = await latestOmpRunnerRelease(refresh); } + catch { return blocked({ code: "check_failed", message: "Could not check the host's omp update version", forceable: false }); } + const stampOf = (version: string | null) => (version === null ? null : /-int\.(\d{12})\./.exec(version)?.[1] ?? null); + const currentStamp = stampOf(currentVersion); + omp.availableVersion = release?.tag.slice(1) ?? null; + omp.updateAvailable = release !== null && omp.availableVersion !== currentVersion && (currentStamp === null || release.stamp > currentStamp); + const blocker = ompUpdateBlocker(ownClaim); + if (blocker) return { ...blocked(blocker), availableVersion: omp.availableVersion, updateAvailable: omp.updateAvailable }; + omp.canUpdate = omp.updateAvailable; + return omp; +} + /** `refresh` bypasses the registry and protocol caches (the existing meaning of the request's `force` for a check, not the update force). An approved pin evaluates its immutable version instead of the moving registry latest. */ export async function checkHostUpdates(compatibility: HostUpdateCompatibility, refresh = false, targetCompatibility?: HostUpdateCompatibility, releasePin?: ApprovedReleasePin): Promise { const targetVersion = releasePin === undefined ? undefined : validateApprovedReleasePin(releasePin, undefined, releaseRepo()).version; - return (await evaluateHostUpdate(compatibility, refresh, targetCompatibility, false, targetVersion)).info; + const { info } = await evaluateHostUpdate(compatibility, refresh, targetCompatibility, false, targetVersion); + // A pinned check belongs to a coordinated ompweb upgrade, which never asks about the runner. + if (targetVersion === undefined) info.omp = await evaluateOmpUpdate(info.omp.currentVersion, refresh); + return info; } const inProgress = () => new HostUpdateError("update_in_progress", "Another host update is in progress"); -/** The lock file is the one atomic gate: `wx` creation lets exactly one concurrent start through. `installDir` is the installation whose finished attempts are pruned. */ -export function claimHostUpdate(id: string, installDir = installDirectory()): string { +/** The lock file is the one atomic gate: `wx` creation lets exactly one concurrent start through. `installDir` is the installation whose finished attempts an ompweb claim prunes; `target` is recorded so a check can say which kind of update holds it. */ +export function claimHostUpdate(id: string, installDir = installDirectory(), target: HostUpdateTarget = "ompweb"): string { const root = ensureHostUpdateRoot(); if (getHostUpdateOperation()?.state === "running") throw inProgress(); const lock = join(root, "lock.json"); - const lease = JSON.stringify({ id, pid: process.pid, at: Date.now() }); + const lease = JSON.stringify({ id, pid: process.pid, at: Date.now(), target }); for (let reclaimed = false; ; reclaimed = true) { try { writeFileSync(lock, lease, { flag: "wx", mode: 0o600 }); @@ -260,7 +357,7 @@ export function claimHostUpdate(id: string, installDir = installDirectory()): st if (isProcessAlive(held.pid) && typeof held.at === "number" && Date.now() - held.at < LEASE_MS) throw inProgress(); rmSync(lock, { force: true }); } - try { pruneHostUpdateArtifacts(installDir); } catch (error) { console.error("Could not prune old host update artifacts:", messageOf(error)); } + if (target === "ompweb") try { pruneHostUpdateArtifacts(installDir); } catch (error) { console.error("Could not prune old host update artifacts:", messageOf(error)); } return root; } /** Releases the claim only while it is still this attempt's: an expired attempt must never erase a newer claim. */ @@ -362,6 +459,50 @@ function assertMatchingHostUpdate(descriptor: HostUpdateDescriptor, target: Host } } +/** + * `omp` target: installs the newest fork release's runner (runner-fetch.ts `installOmpRunner`) in this process, with no + * worker, no restart and no session touched; new sessions use it once it has passed its checks. It takes the shared update + * claim, so it never runs beside an ompweb update (and refuses, with its own blocker code, while one is active or + * unsettled), but keeps its own record (OMP_STATE), never ompweb's status.json. A force, a phase or an approved pin (all + * about replacing the ompweb install) are refused. A caller's `operationId` names this one update: a replay of it returns + * the stored operation whatever its state; an id that names another version or an ompweb update, or that an earlier attempt + * used and a later one superseded (the record keeps the last OMP_EARLIER_MAX), is `operation_conflict`. + */ +async function startOmpUpdate(version: string, request: HostUpdateRequest): Promise<{ accepted: true; operation: HostUpdateOperation }> { + if (request.force || request.phase || request.releasePin || request.requireReleasePin || request.targetCompatibility) throw new HostUpdateError("invalid_request", "An omp update takes only a version", 400); + if (request.operationId !== undefined) { + const recorded = getOmpUpdateOperation(); + if (recorded?.id === request.operationId) { + if (recorded.version !== version) throw new HostUpdateError("operation_conflict", "That operation id already names a different host update"); + return { accepted: true, operation: recorded }; + } + if (getHostUpdateOperation()?.id === request.operationId) throw new HostUpdateError("operation_conflict", "That operation id already names a different host update"); + if (ompUpdateRecord()?.earlier?.includes(request.operationId)) throw new HostUpdateError("operation_conflict", "That omp update operation was superseded by a later attempt"); + } + const refused = ompUpdateBlocker(); + if (refused) throw new HostUpdateError(refused.code, refused.message, refused.code === "updates_disabled" ? 403 : 409); + const id = request.operationId ?? randomUUID(); + const root = claimHostUpdate(id, installDirectory(), "omp"); + try { + const omp = await evaluateOmpUpdate((await getOmpVersion())?.replace(/^omp\//, "") ?? null, true, id); + if (!omp.updateAvailable || omp.availableVersion !== version) throw new HostUpdateError("candidate_changed", "The available omp update changed; refresh host updates"); + if (!omp.canUpdate) throw new HostUpdateError("update_blocked", omp.blockedReason || "This omp update is blocked"); + const operation: HostUpdateOperation = { id, target: "omp", version, state: "running" }; + const previous = ompUpdateRecord(); + const earlier = previous ? [...(previous.earlier ?? []), previous.operation.id].slice(-OMP_EARLIER_MAX) : []; + writeHostUpdateJson(join(root, OMP_STATE), { operation, startedAt: Date.now(), daemonPid: process.pid, earlier } satisfies OmpUpdateState); + void installOmpRunner(`v${version}`) + .then(() => undefined, messageOf) + .then((failure) => finishOmpUpdate(id, failure)) + .catch((error: unknown) => console.error("Could not record the omp update's result:", messageOf(error))); + return { accepted: true, operation }; + } catch (error) { + releaseHostUpdateClaim(root, id); + if (error instanceof HostUpdateError) throw error; + throw new HostUpdateError("host_update_failed", messageOf(error), 500); + } +} + /** * Starts the update of this host. Without `request.force` a host with activity is refused (`host_busy`) and nothing is * touched. With it, only that activity is waived: the release is still staged, digest-verified and preflighted first, a @@ -375,9 +516,11 @@ export async function startHostUpdate(target: HostUpdateTarget, version: string, if (request.force !== undefined && typeof request.force !== "boolean") throw new HostUpdateError("invalid_request", "force must be a boolean", 400); if (request.phase !== undefined && !["prepare", "commit", "cancel"].includes(request.phase)) throw new HostUpdateError("invalid_request", "Invalid host update phase", 400); const force = request.force === true; - if (target !== "ompweb") throw new HostUpdateError("invalid_target", "Invalid host software target", 400); + if (target !== "ompweb" && target !== "omp") throw new HostUpdateError("invalid_target", "Invalid host software target", 400); if (typeof version !== "string" || version.length > 100 || !HOST_VERSION_PATTERN.test(version)) throw new HostUpdateError("invalid_version", "Invalid host update version", 400); if (request.operationId !== undefined && (typeof request.operationId !== "string" || !OPERATION_ID.test(request.operationId))) throw new HostUpdateError("invalid_request", "operationId must be a UUID", 400); + if (target === "omp") return startOmpUpdate(version, request); + if (request.operationId !== undefined && getOmpUpdateOperation()?.id === request.operationId) throw new HostUpdateError("operation_conflict", "That operation id already names a different host update"); if (request.phase && (!request.operationId || request.requireReleasePin !== true || !request.releasePin)) throw new HostUpdateError("invalid_request", "Phased host updates require an operationId and approved release pin", 400); if (request.requireReleasePin !== undefined && typeof request.requireReleasePin !== "boolean" || request.requireReleasePin && !request.releasePin) throw new HostUpdateError("invalid_request", "Missing approved release pin", 400); const releasePin = request.releasePin === undefined ? undefined : validateApprovedReleasePin(request.releasePin, version, releaseRepo()); diff --git a/lib/hosts/v10-upgrade.integration.test.mjs b/lib/hosts/v10-upgrade.integration.test.mjs index b8ec02f4a..196bc097e 100644 --- a/lib/hosts/v10-upgrade.integration.test.mjs +++ b/lib/hosts/v10-upgrade.integration.test.mjs @@ -18,7 +18,7 @@ // checkout (daemon dependencies are symlinked from its node_modules), a POSIX machine (Linux or macOS). // Disposable stand-ins besides delivery: one POSIX `omp` script (OMP_WEB_OMP_BIN) that answers `--version`, `--check` and // the session start with the runner's channel hello, so the v11 updater's real `fetch-omp` and `preflight` of the candidate -// pass without the runner binary (no `bun runner/scripts/build-binary.ts` needed) or any network. +// pass without the runner binary (nothing to download) or any network. // Not covered: compiled-binary installs, systemd/launchd-supervised hosts, Windows, a daemon with live sessions (a legacy // link carries none; the old busy checks are its own unchanged code). The server side serves only /api/hosts/link, so the // upgraded daemon's maintenance connection is refused there; its application link and health acknowledgement are real. @@ -159,6 +159,7 @@ function release() { cpSync(join(repo, "bin"), join(releaseDir, "bin"), { recursive: true }); cpSync(join(repo, "lib"), join(releaseDir, "lib"), { recursive: true, filter: (src) => !/\.test(-helper)?\.(mjs|ts|tsx)$/.test(src) }); cpSync(join(repo, "package.json"), join(releaseDir, "package.json")); + cpSync(join(repo, "omp-runner.json"), join(releaseDir, "omp-runner.json")); return releaseDir; } diff --git a/lib/i18n/locales/en.json b/lib/i18n/locales/en.json index 76a40d343..7013dc26b 100644 --- a/lib/i18n/locales/en.json +++ b/lib/i18n/locales/en.json @@ -1435,6 +1435,9 @@ "hostUpdates.confirmTitle": "Update {software} on {host}?", "hostUpdates.hostWarning": "Close all sessions and stop subagents on {host} first. This update restarts the host daemon; sessions will not resume automatically. Continue?", "hostUpdates.ompWithOmpweb": "Updated with ompweb", + "hostUpdates.updateOmp": "Update omp to {version}", + "hostUpdates.ompHostWarning": "New sessions on {host} will use the new omp. Running sessions keep the omp they started with and are not interrupted. Continue?", + "hostUpdates.ompSucceeded": "omp updated. New sessions use it; running sessions are not interrupted.", "hostUpdates.updating": "Updating {software} to {version}…", "hostUpdates.reconnecting": "Waiting for update status and host reconnection…", "hostUpdates.succeeded": "Update completed.", diff --git a/lib/i18n/locales/ja.json b/lib/i18n/locales/ja.json index e4467fc41..93da3edf9 100644 --- a/lib/i18n/locales/ja.json +++ b/lib/i18n/locales/ja.json @@ -1395,6 +1395,9 @@ "hostUpdates.confirmTitle": "{host} の {software} を更新しますか?", "hostUpdates.hostWarning": "先に {host} のすべてのセッションを閉じ、サブエージェントを停止してください。この更新ではホストデーモンを再起動します。セッションは自動では再開されません。続行しますか?", "hostUpdates.ompWithOmpweb": "ompweb と一緒に更新されます", + "hostUpdates.updateOmp": "omp を {version} に更新", + "hostUpdates.ompHostWarning": "{host} の新しいセッションは新しい omp を使います。実行中のセッションは開始時の omp のまま、中断されません。続行しますか?", + "hostUpdates.ompSucceeded": "omp を更新しました。新しいセッションから使われ、実行中のセッションは中断されません。", "hostUpdates.updating": "{software} を {version} に更新中…", "hostUpdates.reconnecting": "更新状況とホストの再接続を待っています…", "hostUpdates.succeeded": "更新が完了しました。", diff --git a/lib/i18n/locales/zh-CN.json b/lib/i18n/locales/zh-CN.json index b98b9dddc..cf8d7e4fe 100644 --- a/lib/i18n/locales/zh-CN.json +++ b/lib/i18n/locales/zh-CN.json @@ -1395,6 +1395,9 @@ "hostUpdates.confirmTitle": "更新 {host} 上的 {software}?", "hostUpdates.hostWarning": "请先关闭 {host} 上的所有会话并停止子智能体。此更新将重启主机守护进程,不会自动恢复会话。是否继续?", "hostUpdates.ompWithOmpweb": "随 ompweb 一起更新", + "hostUpdates.updateOmp": "将 omp 更新到 {version}", + "hostUpdates.ompHostWarning": "{host} 上的新会话将使用新的 omp。正在运行的会话继续使用启动时的 omp,不会被中断。是否继续?", + "hostUpdates.ompSucceeded": "omp 已更新。新会话将使用它,正在运行的会话不会被中断。", "hostUpdates.updating": "正在将 {software} 更新到 {version}…", "hostUpdates.reconnecting": "正在等待更新状态和主机重新连接…", "hostUpdates.succeeded": "更新完成。", diff --git a/lib/i18n/locales/zh-TW.json b/lib/i18n/locales/zh-TW.json index ca85ab4e1..8ae164ba8 100644 --- a/lib/i18n/locales/zh-TW.json +++ b/lib/i18n/locales/zh-TW.json @@ -1435,6 +1435,9 @@ "hostUpdates.confirmTitle": "更新 {host} 上的 {software}?", "hostUpdates.hostWarning": "請先關閉 {host} 上的所有對話並停止子代理。此更新會重新啟動主機守護行程;對話不會自動接續。要繼續嗎?", "hostUpdates.ompWithOmpweb": "隨 ompweb 一起更新", + "hostUpdates.updateOmp": "將 omp 更新至 {version}", + "hostUpdates.ompHostWarning": "{host} 上的新對話將使用新的 omp。執行中的對話會沿用啟動時的 omp,不會被中斷。要繼續嗎?", + "hostUpdates.ompSucceeded": "omp 已更新。新對話會使用它,執行中的對話不會被中斷。", "hostUpdates.updating": "正在將 {software} 更新至 {version}…", "hostUpdates.reconnecting": "正在等待更新狀態與主機重新連線…", "hostUpdates.succeeded": "更新已完成。", diff --git a/lib/omp/omp-cli.test.mjs b/lib/omp/omp-cli.test.mjs index 58f273746..cb7799f45 100644 --- a/lib/omp/omp-cli.test.mjs +++ b/lib/omp/omp-cli.test.mjs @@ -1,5 +1,6 @@ import assert from "node:assert/strict"; import { copyFileSync, mkdtempSync, readFileSync, realpathSync, renameSync, rmSync, statSync, symlinkSync, mkdirSync, utimesSync, writeFileSync } from "node:fs"; +import { createHash } from "node:crypto"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { pathToFileURL } from "node:url"; @@ -163,6 +164,7 @@ test("a packaged runner is resolved from its actual module root, not cwd, the pa .replace('"./paths"', JSON.stringify(new URL("./paths.ts", import.meta.url).href)); writeFileSync(modulePath, source); copyFileSync(new URL("../../package.json", import.meta.url), join(packageDir, "package.json")); + copyFileSync(new URL("../../omp-runner.json", import.meta.url), join(packageDir, "omp-runner.json")); const api = await import(`${pathToFileURL(modulePath).href}?fresh=${freshImport++}`); const cache = api.fetchedOmpBin(); mkdirSync(join(cache, ".."), { recursive: true }); @@ -178,3 +180,67 @@ test("a packaged runner is resolved from its actual module root, not cwd, the pa const overridden = await import(pathToFileURL(overrideModulePath).href); assert.equal(overridden.resolveOmpBin(true), override); }); + +test("resolveOmpBin prefers a valid runner pointer and falls back when its file is missing or its SHA-256 differs", async (t) => { + const root = realpathSync.native(mkdtempSync(join(tmpdir(), "omp-pointer-"))); + const keys = ["OMP_WEB_OMP_BIN", "OMP_WEB_PACKAGE_DIR", "PI_CODING_AGENT_DIR"]; + const previous = keys.map((key) => process.env[key]); + t.after(() => { + keys.forEach((key, i) => { if (previous[i] === undefined) delete process.env[key]; else process.env[key] = previous[i]; }); + rmSync(root, { recursive: true, force: true }); + }); + delete process.env.OMP_WEB_OMP_BIN; + process.env.PI_CODING_AGENT_DIR = join(root, "agent"); + const api = await import(`./omp-cli.ts?fresh=${freshImport++}`); + const tag = "v18.23.0-int.202611201200.gabc1234"; + const sha = (bytes) => createHash("sha256").update(bytes).digest("hex"); + const point = (pointer) => writeFileSync(join(api.ompRunnerRoot(), "current.json"), JSON.stringify(pointer)); + + const bundled = api.fetchedOmpBin(); + mkdirSync(join(bundled, ".."), { recursive: true }); + writeFileSync(bundled, "the release's own runner"); + assert.equal(api.resolveOmpBin(true), bundled, "without a pointer the fetched runner is used"); + + const pointed = api.ompRunnerPath(tag); + mkdirSync(join(pointed, ".."), { recursive: true }); + writeFileSync(pointed, "the newer runner"); + const valid = { tag, path: pointed, sha256: sha("the newer runner") }; + point(valid); + assert.equal(api.resolveOmpBin(), pointed, "a pointer whose file still has the recorded SHA-256 wins"); + + writeFileSync(pointed, "a different runner"); + assert.equal(api.resolveOmpBin(), bundled, "a file with another SHA-256 is not used"); + writeFileSync(pointed, "the newer runner"); + assert.equal(api.resolveOmpBin(), pointed, "restoring the recorded bytes makes it valid again"); + + rmSync(pointed); + assert.equal(api.resolveOmpBin(), bundled, "a missing pointed file falls back"); + writeFileSync(pointed, "the newer runner"); + + for (const pointer of [ + { ...valid, path: join(root, "elsewhere") }, + { ...valid, tag: "v18.23.0/../x" }, + { ...valid, sha256: "not a digest" }, + "not an object", + ]) { + point(pointer); + assert.equal(api.resolveOmpBin(), bundled, `${JSON.stringify(pointer)} is not trusted`); + } + + point(valid); + assert.equal(api.resolveOmpBin(), pointed); + const override = join(root, "explicit-user-runner"); + writeFileSync(override, "operator override"); + process.env.OMP_WEB_OMP_BIN = override; + const overridden = await import(`./omp-cli.ts?fresh=${freshImport++}`); + assert.equal(overridden.resolveOmpBin(true), override, "the operator's override is never replaced by a pointer"); +}); + +test("omp-runner.json pins a release of the fork by an omp build tag, and the protocol this ompweb speaks is an integer", async () => { + const { OMP_RUNNER_PIN, OMP_TAG, RUNNER_PROTOCOL, fetchedOmpBin, ompRunnerPath } = await import(`./omp-cli.ts?fresh=${freshImport++}`); + assert.deepEqual(Object.keys(OMP_RUNNER_PIN).sort(), ["repo", "tag"]); + assert.match(OMP_RUNNER_PIN.repo, /^[\w.-]+\/[\w.-]+$/); + assert.match(OMP_RUNNER_PIN.tag, OMP_TAG, "the tag becomes a path segment"); + if (!globalThis.Bun?.isStandaloneExecutable) assert.equal(fetchedOmpBin(), ompRunnerPath(OMP_RUNNER_PIN.tag), "a new pin has a new directory"); + assert.ok(Number.isSafeInteger(RUNNER_PROTOCOL) && RUNNER_PROTOCOL >= 1); +}); diff --git a/lib/omp/omp-cli.ts b/lib/omp/omp-cli.ts index 69d0488e6..15589cfe9 100644 --- a/lib/omp/omp-cli.ts +++ b/lib/omp/omp-cli.ts @@ -1,15 +1,17 @@ import { execFile } from "child_process"; -import { existsSync, realpathSync, statSync } from "fs"; +import { createHash } from "node:crypto"; +import { closeSync, existsSync, openSync, readFileSync, readSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "fs"; import { dirname, join } from "path"; import { fileURLToPath } from "node:url"; import packageJson from "../../package.json"; +import runnerPin from "../../omp-runner.json"; import { createChildProcessEnvironment } from "../project-command-env"; import { getAgentDir } from "./paths"; /** - * Locating and probing ompweb's own omp build (`ompweb-omp`, built from runner/). omp-web never - * embeds the (Bun-only) @oh-my-pi SDK — every live-agent capability goes through that binary, so - * its absence is a first-class, user-visible state. + * Locating and probing ompweb's own omp build (`ompweb-omp`, built and released by the oh-my-pi fork: omp-runner.json + * names the release this ompweb build fetches). omp-web never embeds the (Bun-only) @oh-my-pi SDK — every live-agent + * capability goes through that binary, so its absence is a first-class, user-visible state. */ let cachedBin: string | null = null; @@ -19,8 +21,10 @@ let versionMiss: { fingerprint: string | null; retryAt: number } | null = null; let versionProbe: Promise | null = null; const BIN_NAME = process.platform === "win32" ? "ompweb-omp.exe" : "ompweb-omp"; -/** This platform's runner: `runner/release/` in a source checkout and the asset of that name on each release. */ -export const OMP_RUNNER_ASSET = `ompweb-omp-${process.platform === "win32" ? "windows" : process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`; +/** The release asset of `-`'s runner (`linux-x64`, `windows-x64`...): `ompweb-omp--[.exe]`. */ +export const runnerAsset = (target: string): string => `ompweb-omp-${target}${target.startsWith("windows-") ? ".exe" : ""}`; +/** This platform's runner asset. */ +export const OMP_RUNNER_ASSET = runnerAsset(`${process.platform === "win32" ? "windows" : process.platform}-${process.arch}`); /** Pinned package updates require this shipped-source capability before executing the candidate. */ export const OMP_PACKAGED_RUNNER_CAPABILITY = "ompweb-packaged-runner-v1"; // .cmd/.bat launchers (e.g. OMP_WEB_OMP_BIN pointing at a wrapper script) cannot @@ -41,25 +45,99 @@ const MISS_TTL_MS = 30_000; // Launchers can stay unchanged while their underlying package is updated. const VERSION_TTL_MS = 5 * 60_000; -/** Where this install keeps the runner it fetches from its own release (lib/omp/runner-fetch.ts): beside a compiled - * ompweb, else in a per-version cache under the agent dir (an npm install's package dir may not be writable). */ +/** An omp integration build, `v-int..g`: the tag of a fork release. */ +export const OMP_TAG = /^v\d+\.\d+\.\d+-int\.\d{12}\.g[0-9a-f]{7}$/; +const SHA256_HEX = /^[a-f0-9]{64}$/; + +/** The protocol this ompweb speaks with its runner: the runner prints its own with `--runner-info` and in its release's + * `ompweb-omp.json`. Bump it only with a change an older runner cannot speak; hosts then take no runner whose protocol differs. */ +export const RUNNER_PROTOCOL = 1; + +/** The fork release this ompweb build fetches its runner from (root `omp-runner.json`; a release build records the + * newest one with a matching protocol, scripts/fetch-runner.ts). `tag` becomes a path segment: an OMP_TAG (a test checks). */ +export const OMP_RUNNER_PIN: { repo: string; tag: string } = runnerPin; + +/** This ompweb version's runner directory under the agent dir (an npm install's package dir may not be writable): one + * `/ompweb-omp` per fork release (the pinned one this build fetches, and each "Update omp" install) and the + * pointer `current.json` that names the "Update omp" one in use. */ +export function ompRunnerRoot(version = packageJson.version): string { + return join(getAgentDir(), "omp-web", "omp", version); +} +/** `tag` must already be an OMP_TAG: it becomes a path segment. */ +export function ompRunnerPath(tag: string, version = packageJson.version): string { + return join(ompRunnerRoot(version), tag, BIN_NAME); +} + +/** Where this install keeps the runner it fetches from the pinned release (lib/omp/runner-fetch.ts): beside a compiled + * ompweb, else the pinned tag's directory in ompRunnerRoot (a new pin has a new directory, never a stale runner). */ export function fetchedOmpBin(): string { return typeof Bun !== "undefined" && Bun.isStandaloneExecutable ? join(dirname(process.execPath), BIN_NAME) - : join(getAgentDir(), "omp-web", "omp", packageJson.version, BIN_NAME); + : ompRunnerPath(OMP_RUNNER_PIN.tag); +} + +/** What "Update omp" installed: a fork release's runner (lib/omp/runner-fetch.ts `installOmpRunner`) and the SHA-256 it was verified with. */ +export interface OmpPointer { + tag: string; + path: string; + sha256: string; +} + +/** The pointer of `version`, or null when absent, malformed, or naming anything but that version's own `/ompweb-omp`. */ +export function readOmpPointer(version = packageJson.version): OmpPointer | null { + try { + const value: unknown = JSON.parse(readFileSync(join(ompRunnerRoot(version), "current.json"), "utf8")); + if (typeof value !== "object" || value === null) return null; + const { tag, path, sha256 } = value as Record; + return typeof tag === "string" && OMP_TAG.test(tag) && typeof sha256 === "string" && SHA256_HEX.test(sha256) && path === ompRunnerPath(tag, version) ? { tag, path, sha256 } : null; + } catch { + return null; + } } -/** A source checkout builds its runner (`bun runner/scripts/build-binary.ts`) instead of fetching one. */ -export function sourceCheckoutOmpBin(): string | null { - const packageDir = process.env.OMP_WEB_PACKAGE_DIR ?? process.cwd(); - return existsSync(join(packageDir, "runner", "package.json")) ? join(packageDir, "runner", "release", OMP_RUNNER_ASSET) : null; +/** Atomically points `version` at an installed runner: a reader sees the old pointer or the new one. */ +export function writeOmpPointer(pointer: OmpPointer, version = packageJson.version): void { + const file = join(ompRunnerRoot(version), "current.json"); + const temporary = `${file}.${process.pid}.${Date.now()}.tmp`; + writeFileSync(temporary, JSON.stringify(pointer), { flag: "wx" }); + try { renameSync(temporary, file); } + catch (error) { rmSync(temporary, { force: true }); throw error; } +} + +function fileSha256(path: string): string { + const hash = createHash("sha256"); + const fd = openSync(path, "r"); + try { + const chunk = Buffer.allocUnsafe(1 << 20); + for (let read = readSync(fd, chunk, 0, chunk.length, null); read > 0; read = readSync(fd, chunk, 0, chunk.length, null)) hash.update(chunk.subarray(0, read)); + } finally { + closeSync(fd); + } + return hash.digest("hex"); +} + +// The verdict on the pointed binary, per pointer and file identity: a ~120 MB hash is paid again only when either changes. +let pointerVerdict: { key: string; fingerprint: string; ok: boolean } | null = null; + +/** The pointed runner while it still is the file whose SHA-256 the pointer records; null for the override (it names its + * own runner), a missing, replaced or damaged binary, and no pointer. */ +function pointedOmpBin(): string | null { + const pointer = readOmpPointer(); + if (!pointer || process.env.OMP_WEB_OMP_BIN) return null; + const fingerprint = versionFingerprint(pointer.path); + if (!fingerprint) return null; + const key = `${pointer.tag}:${pointer.sha256}`; + if (pointerVerdict?.key !== key || pointerVerdict.fingerprint !== fingerprint) { + let ok = false; + try { ok = fileSha256(pointer.path) === pointer.sha256; } catch { /* unreadable: not the recorded file */ } + pointerVerdict = { key, fingerprint, ok }; + } + return pointerVerdict.ok ? pointer.path : null; } function probeOmpBin(): string | null { const override = process.env.OMP_WEB_OMP_BIN; if (override) return existsSync(override) ? override : null; - const source = sourceCheckoutOmpBin(); - if (source) return existsSync(source) ? source : null; if (typeof Bun !== "undefined" && Bun.isStandaloneExecutable) { const adjacent = fetchedOmpBin(); return existsSync(adjacent) ? adjacent : null; @@ -73,11 +151,14 @@ function probeOmpBin(): string | null { /** What every caller that needs omp reports when resolveOmpBin finds none. */ export const OMP_NOT_FOUND = "ompweb's omp binary (ompweb-omp) not found. Reinstall ompweb or set OMP_WEB_OMP_BIN."; -/** Resolve ompweb's omp binary: OMP_WEB_OMP_BIN override, else the source checkout's build or the fetched runner - * (see probeOmpBin). Returns null when none exists. A hit is cached while the file exists; a miss is re-probed after - * MISS_TTL_MS, or at once with `fresh` (after a fetch). */ +/** Resolve ompweb's omp binary: OMP_WEB_OMP_BIN override, else the runner "Update omp" pointed at (pointedOmpBin), else + * the fetched runner (see probeOmpBin). Returns null when none exists. A hit is cached + * while the file exists; a miss is re-probed after MISS_TTL_MS, or at once with `fresh` (after a fetch). The pointer is read + * on every call, so a new session after an update starts the new runner and running ones keep theirs. */ export function resolveOmpBin(fresh = false): string | null { if (fresh) binMissAt = 0; + const pointed = pointedOmpBin(); + if (pointed) return pointed; // A global Bun/npm update can replace or remove its launcher while this // server process is still alive. Never keep returning a stale cache entry. if (cachedBin && existsSync(cachedBin)) return cachedBin; diff --git a/lib/omp/rpc-process.ts b/lib/omp/rpc-process.ts index 36b7e8d69..93b5f69a5 100644 --- a/lib/omp/rpc-process.ts +++ b/lib/omp/rpc-process.ts @@ -7,7 +7,7 @@ import { LINK_LIMITS } from "../hosts/link-protocol"; /** * Process + protocol layer for ompweb's omp (`ompweb-omp --headless`), which speaks the channel of - * runner/src/channel.ts as NDJSON over stdio. Frames pass through as `{type: t, ...rest}`, except: + * packages/ompweb-runner/src/channel.ts of the oh-my-pi fork as NDJSON over stdio. Frames pass through as `{type: t, ...rest}`, except: * * hello → readiness (`waitReady` resolves `{type:"ready", ...hello}`) * event {event} → the event object itself diff --git a/lib/omp/runner-fetch.test.mjs b/lib/omp/runner-fetch.test.mjs index 5057e22a1..d8dd6d452 100644 --- a/lib/omp/runner-fetch.test.mjs +++ b/lib/omp/runner-fetch.test.mjs @@ -1,63 +1,280 @@ import assert from "node:assert/strict"; import { createHash } from "node:crypto"; -import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync } from "node:fs"; +import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import test, { after } from "node:test"; -const { fetchOmpRunner, OMP_RUNNER_CHECKSUMS } = await import("./runner-fetch.ts"); -const { OMP_RUNNER_ASSET } = await import("./omp-cli.ts"); -const { releaseRepo } = await import("../release-repo.ts"); +const { fetchOmpRunner, findRunnerRelease, installOmpRunner, OMP_RUNNER_CHECKSUMS, OMP_RUNNER_INFO, RELEASE_PAGE_CAP, sweepOmpRunners } = await import("./runner-fetch.ts"); +const { OMP_RUNNER_ASSET, OMP_RUNNER_PIN, ompRunnerPath, ompRunnerRoot, readOmpPointer, RUNNER_PROTOCOL } = await import("./omp-cli.ts"); const dir = mkdtempSync(join(tmpdir(), "ompweb-runner-fetch-")); const original = globalThis.fetch; after(() => { globalThis.fetch = original; rmSync(dir, { recursive: true, force: true }); }); +const digest = (bytes) => createHash("sha256").update(bytes).digest("hex"); +const base = (repo, tag) => `https://github.com/${repo}/releases/download/${tag}`; +const TAG = "v18.23.0-int.202611201200.gabc1234"; +const OLD_TAG = "v18.22.0-int.202611171027.gec3956c"; +const FORK = { repo: "fork/oh-my-pi", tag: TAG }; const runner = Buffer.from("#!/bin/sh\necho runner\n"); -const sha = createHash("sha256").update(runner).digest("hex"); -const base = `https://github.com/${releaseRepo()}/releases/download/v9.9.9`; -/** Serves the release's checksum listing and runner; `served` records each URL asked for. */ -function serve(sums, served = []) { + +/** Serves one fork release's checksum listing and runner asset; `served` records each URL asked for. */ +function serveRelease({ repo, tag }, sums, binary = runner, served = []) { globalThis.fetch = async (url) => { served.push(String(url)); - if (String(url) === `${base}/${OMP_RUNNER_CHECKSUMS}`) return new Response(sums); - if (String(url) === `${base}/${OMP_RUNNER_ASSET}`) return new Response(runner); + if (String(url) === `${base(repo, tag)}/${OMP_RUNNER_CHECKSUMS}`) return new Response(sums); + if (String(url) === `${base(repo, tag)}/${OMP_RUNNER_ASSET}`) return new Response(binary); return new Response("missing", { status: 404 }); }; return served; } -test("the runner comes from this version's release, checked against its checksum listing, and lands executable", async () => { - const served = serve(`${"0".repeat(64)} ompweb-omp-other\n${sha} ${OMP_RUNNER_ASSET}\n`); +test("the runner comes from the pinned fork release, checked against its checksum listing, and lands executable", async () => { + const served = serveRelease(FORK, `${"0".repeat(64)} ompweb-omp-other\n${digest(runner)} ${OMP_RUNNER_ASSET}\n`); const target = join(dir, "ok", "ompweb-omp"); - assert.equal(await fetchOmpRunner(target, "9.9.9"), target); + assert.equal(await fetchOmpRunner(target, FORK), target); assert.deepEqual(readFileSync(target), runner); if (process.platform !== "win32") assert.equal(statSync(target).mode & 0o777, 0o755); - assert.deepEqual(served, [`${base}/${OMP_RUNNER_CHECKSUMS}`, `${base}/${OMP_RUNNER_ASSET}`]); + assert.deepEqual(served, [`${base(FORK.repo, TAG)}/${OMP_RUNNER_CHECKSUMS}`, `${base(FORK.repo, TAG)}/${OMP_RUNNER_ASSET}`]); assert.deepEqual(readdirSync(join(dir, "ok")), ["ompweb-omp"], "no partial file is left"); }); +test("the baked-in pin is what an install fetches from by default", async () => { + const served = serveRelease(OMP_RUNNER_PIN, `${digest(runner)} ${OMP_RUNNER_ASSET}\n`); + const target = join(dir, "default", "ompweb-omp"); + await fetchOmpRunner(target); + assert.deepEqual(served, [`${base(OMP_RUNNER_PIN.repo, OMP_RUNNER_PIN.tag)}/${OMP_RUNNER_CHECKSUMS}`, `${base(OMP_RUNNER_PIN.repo, OMP_RUNNER_PIN.tag)}/${OMP_RUNNER_ASSET}`]); +}); + test("a runner whose SHA-256 differs from the listed one is refused and nothing is written", async () => { - serve(`${"a".repeat(64)} ${OMP_RUNNER_ASSET}\n`); + serveRelease(FORK, `${"a".repeat(64)} ${OMP_RUNNER_ASSET}\n`); const target = join(dir, "mismatch", "ompweb-omp"); - await assert.rejects(fetchOmpRunner(target, "9.9.9"), /SHA-256/); + await assert.rejects(fetchOmpRunner(target, FORK), /SHA-256/); assert.equal(existsSync(target), false); assert.deepEqual(readdirSync(join(dir, "mismatch")), []); - serve(`${sha} ompweb-omp-other\n`); - await assert.rejects(fetchOmpRunner(target, "9.9.9"), new RegExp(`lists no ${OMP_RUNNER_ASSET}`)); + serveRelease(FORK, `${digest(runner)} ompweb-omp-other\n`); + await assert.rejects(fetchOmpRunner(target, FORK), new RegExp(`lists no ${OMP_RUNNER_ASSET}`)); +}); + +test("a release without the runner's assets fails with a message that names the missing asset", async () => { + globalThis.fetch = async () => new Response("Not Found", { status: 404 }); + await assert.rejects(fetchOmpRunner(join(dir, "none", "ompweb-omp"), FORK), (error) => { + assert.ok(error.message.includes(`${base(FORK.repo, TAG)}/${OMP_RUNNER_CHECKSUMS}`), error.message); + assert.ok(error.message.includes(`${FORK.repo} release ${TAG} attaches no ${OMP_RUNNER_CHECKSUMS}`), error.message); + return true; + }); + globalThis.fetch = async (url) => (String(url).endsWith(OMP_RUNNER_CHECKSUMS) ? new Response(`${digest(runner)} ${OMP_RUNNER_ASSET}\n`) : new Response("Not Found", { status: 404 })); + await assert.rejects(fetchOmpRunner(join(dir, "none", "ompweb-omp"), FORK), new RegExp(`attaches no ${OMP_RUNNER_ASSET}`)); + assert.equal(existsSync(join(dir, "none", "ompweb-omp")), false); }); test("offline, the error names the runner URL and OMP_WEB_OMP_BIN", async () => { globalThis.fetch = async () => { throw new TypeError("fetch failed"); }; const target = join(dir, "offline", "ompweb-omp"); - await assert.rejects(fetchOmpRunner(target, "9.9.9"), (error) => { - assert.ok(error.message.includes(`${base}/${OMP_RUNNER_ASSET}`), error.message); + await assert.rejects(fetchOmpRunner(target, FORK), (error) => { + assert.ok(error.message.includes(base(FORK.repo, TAG)), error.message); assert.match(error.message, /fetch failed.*OMP_WEB_OMP_BIN/); return true; }); assert.equal(existsSync(target), false); }); -test("the release repository is package.json's", async () => { - const packageJson = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")); - assert.equal(`git+https://github.com/${releaseRepo()}.git`, packageJson.repository.url); +/** A GitHub release listing entry attaching the runner assets (`assets` overrides the list). */ +const release = (tag_name, extra = {}) => ({ tag_name, draft: false, prerelease: true, assets: [OMP_RUNNER_INFO, OMP_RUNNER_CHECKSUMS, OMP_RUNNER_ASSET].map((name) => ({ name })), ...extra }); +/** Serves `pages` of a fork's release listing and each tag's ompweb-omp.json (`protocols[tag]`); `served` records every URL. */ +function serveListing(repo, pages, protocols, served = []) { + globalThis.fetch = async (url) => { + url = String(url); + served.push(url); + const page = new RegExp(`^https://api.github.com/repos/${repo}/releases\\?per_page=100&page=(\\d+)$`).exec(url); + if (page) return Number(page[1]) <= pages.length ? new Response(JSON.stringify(pages[Number(page[1]) - 1])) : new Response("[]"); + for (const [tag, protocol] of Object.entries(protocols)) if (url === `${base(repo, tag)}/${OMP_RUNNER_INFO}`) return new Response(JSON.stringify({ protocol, omp: tag.slice(1) })); + return new Response("missing", { status: 404 }); + }; + return served; +} + +test("a release whose runner speaks another protocol is skipped, whatever its stamp; so is anything that is not a usable fork build", async () => { + const repo = "fork/skips"; + const NEWEST = "v18.25.0-int.202611250000.g1111111"; + const NEWER = "v18.24.0-int.202611220000.g2222222"; + const listing = [ + release(NEWEST), // newest, but another protocol + release("v18.26.0-int.202611260000.g3333333", { draft: true }), + release("v18.26.0-int.202611260000.g4444444", { assets: [{ name: OMP_RUNNER_INFO }] }), // no runner assets yet + release("v18.26.0-int.202611260000.g5555555", { assets: [{ name: OMP_RUNNER_INFO }, { name: OMP_RUNNER_CHECKSUMS }, { name: "ompweb-omp-other" }] }), // none for this platform + release("v18.26.0"), // not an integration build + release("v18.26.0-int.20261126.g6666666"), + release("v18.26.0-int.202611260000.g6666666/../x"), + { tag_name: 7 }, + null, + release(OLD_TAG), + release(NEWER), + ]; + const served = serveListing(repo, [listing], { [NEWEST]: RUNNER_PROTOCOL + 1, [NEWER]: RUNNER_PROTOCOL, [OLD_TAG]: RUNNER_PROTOCOL }); + assert.deepEqual(await findRunnerRelease(repo), { tag: NEWER, stamp: "202611220000" }); + assert.ok(served.includes(`${base(repo, NEWEST)}/${OMP_RUNNER_INFO}`), "the newer build's protocol was read, and refused"); + assert.equal(served.some((url) => url.includes("g3333333") || url.includes("g4444444") || url.includes("g5555555")), false, "a release without the assets is never asked about"); + assert.equal(await findRunnerRelease(repo, ["ompweb-omp-no-such-platform"]), null, "an asset list that no release attaches finds none"); +}); + +/** `count` releases, newest first from `stamp`, each a distinct valid tag. */ +const releases = (count, offset) => Array.from({ length: count }, (_, index) => release(`v18.0.0-int.${String(202611200000 - offset - index).padStart(12, "0")}.g${(offset + index).toString(16).padStart(7, "0")}`)); + +test("a runner release found on the second page is the one chosen; a first page that is not full ends the search", async () => { + const repo = "fork/pages"; + const first = releases(100, 0); + const wanted = releases(1, 100)[0]; + const second = [...releases(5, 101), wanted]; + const protocols = Object.fromEntries([...first, ...second].map((entry) => [entry.tag_name, RUNNER_PROTOCOL + 1])); + protocols[wanted.tag_name] = RUNNER_PROTOCOL; + const served = serveListing(repo, [first, second], protocols); + assert.equal((await findRunnerRelease(repo))?.tag, wanted.tag_name); + assert.ok(served.includes(`https://api.github.com/repos/${repo}/releases?per_page=100&page=2`)); + + const short = serveListing("fork/short", [releases(3, 0)], Object.fromEntries(releases(3, 0).map((entry) => [entry.tag_name, RUNNER_PROTOCOL + 1]))); + assert.equal(await findRunnerRelease("fork/short"), null); + assert.deepEqual(short.filter((url) => url.startsWith("https://api.github.com/")), ["https://api.github.com/repos/fork/short/releases?per_page=100&page=1"], "a short page is the last"); +}); + +test("the search reads at most RELEASE_PAGE_CAP pages and finds none beyond them; a listing that is not a list fails", async () => { + const repo = "fork/capped"; + const pages = Array.from({ length: RELEASE_PAGE_CAP + 2 }, (_, page) => releases(100, page * 100)); + const protocols = Object.fromEntries(pages.flat().map((entry) => [entry.tag_name, RUNNER_PROTOCOL + 1])); + protocols[pages[RELEASE_PAGE_CAP][0].tag_name] = RUNNER_PROTOCOL; // a match one page past the cap + const served = serveListing(repo, pages, protocols); + assert.equal(await findRunnerRelease(repo), null); + assert.equal(served.filter((url) => url.startsWith("https://api.github.com/")).length, RELEASE_PAGE_CAP); + + globalThis.fetch = async () => new Response(JSON.stringify({ message: "API rate limit exceeded" })); + await assert.rejects(findRunnerRelease("fork/limited"), /not a list/); + globalThis.fetch = async () => new Response("busy", { status: 503 }); + await assert.rejects(findRunnerRelease("fork/busy"), /503/); +}); + +const pointerFile = () => join(ompRunnerRoot("9.9.9"), "current.json"); +const asBuild = (tag) => Buffer.from(`#!/bin/sh\necho omp/${tag.slice(1)}\n`); +/** Serves the fork releases' checksum listings and binaries; a binary maps to the SHA-256 its listing claims. */ +function serveBuilds(builds, repo = FORK.repo) { + globalThis.fetch = async (url) => { + url = String(url); + for (const [tag, { binary, listed, status }] of Object.entries(builds)) { + if (status && url.startsWith(base(repo, tag))) return new Response("error", { status }); + if (url === `${base(repo, tag)}/${OMP_RUNNER_CHECKSUMS}`) return new Response(`${listed} ${OMP_RUNNER_ASSET}\n`); + if (url === `${base(repo, tag)}/${OMP_RUNNER_ASSET}`) return new Response(binary); + } + return new Response("missing", { status: 404 }); + }; +} +const good = (tag) => { const binary = asBuild(tag); return { binary, listed: digest(binary) }; }; + +let agents = 0; +async function inAgentDir(t) { + const previous = process.env.PI_CODING_AGENT_DIR; + process.env.PI_CODING_AGENT_DIR = join(dir, `agent-${agents++}`); + t.after(() => { if (previous === undefined) delete process.env.PI_CODING_AGENT_DIR; else process.env.PI_CODING_AGENT_DIR = previous; }); +} +const install = (tag, probe = async () => {}) => installOmpRunner(tag, "9.9.9", probe, FORK.repo); + +test("a build is staged and probed where it was downloaded, and only then moved into place and pointed at", async (t) => { + await inAgentDir(t); + serveBuilds({ [TAG]: good(TAG) }); + const probes = []; + const path = await install(TAG, async (bin) => { probes.push([bin, existsSync(join(ompRunnerRoot("9.9.9"), TAG)), existsSync(pointerFile())]); }); + assert.equal(path, ompRunnerPath(TAG, "9.9.9")); + assert.deepEqual(readFileSync(path), asBuild(TAG)); + if (process.platform !== "win32") assert.equal(statSync(path).mode & 0o777, 0o755); + assert.equal(probes.length, 1); + assert.ok(probes[0][0].startsWith(join(ompRunnerRoot("9.9.9"), ".staging-")), "the probe runs on the staged file"); + assert.deepEqual(probes[0].slice(1), [false, false], "neither the final directory nor the pointer exists while it is probed"); + assert.deepEqual(readOmpPointer("9.9.9"), { tag: TAG, path, sha256: digest(asBuild(TAG)) }); + assert.deepEqual(readdirSync(ompRunnerRoot("9.9.9")).sort(), ["current.json", TAG], "no staging directory is left"); + assert.deepEqual(readdirSync(join(ompRunnerRoot("9.9.9"), TAG)), [process.platform === "win32" ? "ompweb-omp.exe" : "ompweb-omp"]); +}); + +test("a failed download or probe leaves the pointer and the selected runner exactly as they were, a same-tag reinstall included", async (t) => { + await inAgentDir(t); + serveBuilds({ [OLD_TAG]: good(OLD_TAG), [TAG]: good(TAG) }); + const old = await install(OLD_TAG); + const selected = await install(TAG); + const pointer = readFileSync(pointerFile(), "utf8"); + const untouched = (why) => { + assert.equal(readFileSync(pointerFile(), "utf8"), pointer, why); + assert.deepEqual(readFileSync(selected), asBuild(TAG), why); + assert.deepEqual(readFileSync(old), asBuild(OLD_TAG), why); + assert.deepEqual(readdirSync(ompRunnerRoot("9.9.9")).sort(), ["current.json", OLD_TAG, TAG].sort(), `${why}: nothing is left behind`); + }; + + serveBuilds({ [TAG]: { binary: asBuild("v18.99.0-int.202611990000.gfffffff"), listed: digest("something else") } }); + await assert.rejects(install(TAG, async () => assert.fail("a refused download is never run")), /SHA-256/); + untouched("a checksum mismatch"); + + serveBuilds({ [TAG]: { ...good(TAG), status: 503 } }); + await assert.rejects(install(TAG), /503/); + untouched("a failed download"); + + serveBuilds({ [TAG]: good(TAG) }); + await assert.rejects(install(TAG, async () => { throw new Error("smoke test failed"); }), /did not pass its checks: smoke test failed/); + untouched("a probe that fails"); + + await assert.rejects(installOmpRunner("v18.23.0-int.202611201200.g0000000", "9.9.9", async () => {}, FORK.repo), /attaches no/); + untouched("a release without the runner"); + assert.equal(existsSync(join(ompRunnerRoot("9.9.9"), "v18.23.0-int.202611201200.g0000000")), false); +}); + +test("a build that fails the --runner-info protocol check is not installed", { skip: process.platform === "win32" && "the fake runners are POSIX shell scripts" }, async (t) => { + await inAgentDir(t); + const fake = (protocol, smoke = 0) => Buffer.from(`#!/bin/sh\ncase "$1" in\n --runner-info) echo '{"protocol":${protocol},"omp":"18.23.0"}' ;;\n --smoke-test) exit ${smoke} ;;\nesac\n`); + const serve = (binary) => serveBuilds({ [TAG]: { binary, listed: digest(binary) } }); + const root = ompRunnerRoot("9.9.9"); + + serve(fake(RUNNER_PROTOCOL + 1)); + await assert.rejects(installOmpRunner(TAG, "9.9.9", undefined, FORK.repo), new RegExp(`protocol ${RUNNER_PROTOCOL + 1}, this ompweb speaks ${RUNNER_PROTOCOL}`)); + serve(Buffer.from("#!/bin/sh\necho omp/18.23.0\n")); + await assert.rejects(installOmpRunner(TAG, "9.9.9", undefined, FORK.repo), /--runner-info printed/); + serve(fake(RUNNER_PROTOCOL, 1)); + await assert.rejects(installOmpRunner(TAG, "9.9.9", undefined, FORK.repo), /did not pass its checks/); + assert.equal(readOmpPointer("9.9.9"), null); + assert.deepEqual(readdirSync(root), [], "no refused build stays"); + + serve(fake(RUNNER_PROTOCOL)); + assert.equal(await installOmpRunner(TAG, "9.9.9", undefined, FORK.repo), ompRunnerPath(TAG, "9.9.9")); + assert.equal(readOmpPointer("9.9.9").tag, TAG); +}); + +test("reinstalling the selected tag swaps its directory and repoints, and puts the old runner back when the swap fails", async (t) => { + await inAgentDir(t); + const first = asBuild(TAG); + const second = Buffer.concat([first, Buffer.from("# rebuilt\n")]); + serveBuilds({ [TAG]: { binary: first, listed: digest(first) } }); + const path = await install(TAG); + serveBuilds({ [TAG]: { binary: second, listed: digest(second) } }); + await install(TAG); + assert.deepEqual(readFileSync(path), second); + assert.equal(readOmpPointer("9.9.9").sha256, digest(second)); + assert.deepEqual(readdirSync(ompRunnerRoot("9.9.9")).sort(), ["current.json", TAG], "the displaced copy is removed"); + + // A pointer that cannot be written (its name is taken by a directory) fails after the swap: the old copy comes back. + const third = Buffer.concat([first, Buffer.from("# once more\n")]); + rmSync(pointerFile()); + mkdirSync(pointerFile()); + serveBuilds({ [TAG]: { binary: third, listed: digest(third) } }); + await assert.rejects(install(TAG)); + assert.deepEqual(readFileSync(path), second, "the runner in use was put back"); + assert.deepEqual(readdirSync(ompRunnerRoot("9.9.9")).sort(), ["current.json", TAG]); +}); + +test("only an omp build tag is installed, and a start-up sweep keeps the pointed and the pinned build, and removes the rest and unfinished installs", async (t) => { + await inAgentDir(t); + await assert.rejects(install("v18.23.0/../../x"), /not an omp build tag/); + const THIRD = "v18.24.0-int.202611250000.g7654321"; + serveBuilds({ [OLD_TAG]: good(OLD_TAG), [TAG]: good(TAG), [THIRD]: good(THIRD) }); + for (const tag of [OLD_TAG, TAG]) await install(tag); + const root = ompRunnerRoot("9.9.9"); + mkdirSync(join(root, THIRD)); + mkdirSync(join(root, "not-a-build")); + mkdirSync(join(root, ".staging-abc")); + writeFileSync(join(root, ".staging-abc.old"), "x"); + sweepOmpRunners("9.9.9", THIRD); + assert.deepEqual(readdirSync(root).sort(), ["current.json", "not-a-build", THIRD, TAG].sort()); }); diff --git a/lib/omp/runner-fetch.ts b/lib/omp/runner-fetch.ts index 9c86bc27f..20534a11a 100644 --- a/lib/omp/runner-fetch.ts +++ b/lib/omp/runner-fetch.ts @@ -1,46 +1,200 @@ -import { chmodSync, mkdirSync, renameSync, rmSync } from "node:fs"; -import { dirname } from "node:path"; +import { execFile } from "node:child_process"; +import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, renameSync, rmSync } from "node:fs"; +import { basename, dirname, join } from "node:path"; +import { promisify } from "node:util"; import packageJson from "../../package.json"; -import { releaseRepo } from "../release-repo"; import { downloadHostUpdateFile, fetchHostUpdateBytes, HOST_RELEASE_MAX_MEMBER } from "../hosts/update-worker"; -import { fetchedOmpBin, OMP_RUNNER_ASSET, resolveOmpBin, sourceCheckoutOmpBin } from "./omp-cli"; +import { createChildProcessEnvironment } from "../project-command-env"; +import { isRecord } from "../type-guards"; +import { fetchedOmpBin, OMP_RUNNER_ASSET, OMP_RUNNER_PIN, OMP_TAG, ompRunnerPath, ompRunnerRoot, readOmpPointer, resolveOmpBin, RUNNER_PROTOCOL, writeOmpPointer } from "./omp-cli"; -// Every install gets its omp runner (ompweb-omp) from its own version's GitHub release: the asset OMP_RUNNER_ASSET, -// checked against that release's OMP_RUNNER_CHECKSUMS. Releases attach both (.github/workflows/publish.yml). +// The omp runner (ompweb-omp) is built and released by the oh-my-pi fork. Every install fetches the one its build pins +// (omp-runner.json): the asset OMP_RUNNER_ASSET, checked against that release's OMP_RUNNER_CHECKSUMS. "Update omp" takes +// the same two files from the newest fork release whose OMP_RUNNER_INFO names RUNNER_PROTOCOL. // Loaded by Node/jiti and the compiled binary: no `@/` imports. -/** The `sha256sum` listing of every runner asset a release attaches. */ +/** The `sha256sum` listing of every runner asset a fork release attaches. */ export const OMP_RUNNER_CHECKSUMS = "ompweb-omp.sha256"; +/** `{ "protocol": , "omp": "" }`: a fork release's runner metadata. */ +export const OMP_RUNNER_INFO = "ompweb-omp.json"; -/** Downloads this version's runner to `target` (atomically, executable) once its SHA-256 matches the release's. */ -export async function fetchOmpRunner(target = fetchedOmpBin(), version = packageJson.version): Promise { - const base = `https://github.com/${releaseRepo()}/releases/download/v${version}`; - const url = `${base}/${OMP_RUNNER_ASSET}`; +type Pin = typeof OMP_RUNNER_PIN; +const downloadBase = ({ repo, tag }: Pin) => `https://github.com/${repo}/releases/download/${tag}`; + +/** Downloads `asset` of the fork release `pin` to `target` (atomically, executable) once its SHA-256 matches the one the + * release's OMP_RUNNER_CHECKSUMS lists; returns that SHA-256. A failure names the file it was fetching. */ +export async function downloadRunner(pin: Pin, asset: string, target: string): Promise { const partial = `${target}.${process.pid}.${Date.now()}.partial`; + let url = `${downloadBase(pin)}/${OMP_RUNNER_CHECKSUMS}`; try { - const sums = (await fetchHostUpdateBytes(`${base}/${OMP_RUNNER_CHECKSUMS}`, 64 * 1024, 30_000)).toString("utf8"); - const expected = sums.split("\n").map((line) => line.trim().split(/\s+\*?/)).find(([, name]) => name === OMP_RUNNER_ASSET)?.[0]; - if (!expected || !/^[a-f0-9]{64}$/.test(expected)) throw new Error(`${OMP_RUNNER_CHECKSUMS} lists no ${OMP_RUNNER_ASSET}`); + const sums = (await fetchHostUpdateBytes(url, 64 * 1024, 30_000)).toString("utf8"); + const expected = sums.split("\n").map((line) => line.trim().split(/\s+\*?/)).find(([, name]) => name === asset)?.[0]; + if (!expected || !/^[a-f0-9]{64}$/.test(expected)) throw new Error(`${OMP_RUNNER_CHECKSUMS} lists no ${asset}`); mkdirSync(dirname(target), { recursive: true }); + url = `${downloadBase(pin)}/${asset}`; const digest = await downloadHostUpdateFile(url, partial, HOST_RELEASE_MAX_MEMBER); if (digest !== `sha256:${expected}`) throw new Error(`its SHA-256 is not the one ${OMP_RUNNER_CHECKSUMS} lists`); chmodSync(partial, 0o755); renameSync(partial, target); - return target; + return expected; } catch (error) { rmSync(partial, { force: true }); const reason = error instanceof Error ? error.message : String(error); - throw new Error(`Could not fetch ompweb's omp from ${url}: ${reason}. Check this machine's connection to GitHub, or set OMP_WEB_OMP_BIN to an ompweb-omp binary.`); + const missing = /\(404\)/.test(reason) ? ` (${pin.repo} release ${pin.tag} attaches no ${basename(url)})` : ""; + throw new Error(`Could not fetch ompweb's omp from ${url}: ${reason}${missing}. Check this machine's connection to GitHub, or set OMP_WEB_OMP_BIN to an ompweb-omp binary.`); + } +} + +/** Downloads the runner of the release this build pins to `target` (atomically, executable) once its SHA-256 matches the release's. */ +export async function fetchOmpRunner(target = fetchedOmpBin(), pin = OMP_RUNNER_PIN, asset = OMP_RUNNER_ASSET): Promise { + await downloadRunner(pin, asset, target); + return target; +} + +/** A fork release a runner can be taken from: its tag and that tag's build stamp, `YYYYMMDDHHMM`. */ +export interface OmpRunnerRelease { + tag: string; + stamp: string; +} + +type ReadBytes = (url: string, maxBytes: number, timeout: number) => Promise; + +const RELEASE_PAGE_SIZE = 100; +/** Pages of releases (newest first) read to find a runner. shortcut: a fork whose newest 500 releases carry no matching + * runner reads as having none; raise it if that ever happens. */ +export const RELEASE_PAGE_CAP = 5; + +// A published release's metadata never changes: each tag is asked once per process (a failed read is asked again). +const protocols = new Map(); +async function releaseProtocol(pin: Pin, read: ReadBytes): Promise { + const key = `${pin.repo}@${pin.tag}`; + const known = protocols.get(key); + if (known !== undefined) return known; + const bytes = await read(`${downloadBase(pin)}/${OMP_RUNNER_INFO}`, 4096, 15_000); + let protocol: number | null = null; + try { + const info: unknown = JSON.parse(bytes.toString("utf8")); + if (isRecord(info) && Number.isSafeInteger(info.protocol)) protocol = info.protocol as number; + } catch { /* not JSON: this release speaks no protocol this ompweb knows */ } + protocols.set(key, protocol); + return protocol; +} + +/** + * The newest published release of the fork (`repo`) whose runner speaks RUNNER_PROTOCOL: the releases are read newest + * first, RELEASE_PAGE_SIZE a page up to RELEASE_PAGE_CAP pages; those that are drafts, are not integration builds + * (OMP_TAG) or do not attach OMP_RUNNER_INFO, OMP_RUNNER_CHECKSUMS and every one of `assets` are skipped, and so is one + * whose OMP_RUNNER_INFO names another protocol. Within a page the newest build stamp wins. `read` is a seam for callers + * with a token (scripts/fetch-runner.ts) and for tests. + */ +export async function findRunnerRelease(repo = OMP_RUNNER_PIN.repo, assets = [OMP_RUNNER_ASSET], read: ReadBytes = fetchHostUpdateBytes): Promise { + const needed = [OMP_RUNNER_INFO, OMP_RUNNER_CHECKSUMS, ...assets]; + for (let page = 1; page <= RELEASE_PAGE_CAP; page++) { + const listing: unknown = JSON.parse((await read(`https://api.github.com/repos/${repo}/releases?per_page=${RELEASE_PAGE_SIZE}&page=${page}`, 8 * 1024 * 1024, 15_000)).toString("utf8")); + if (!Array.isArray(listing)) throw new Error(`GitHub's release listing of ${repo} is not a list`); + const candidates: OmpRunnerRelease[] = []; + for (const release of listing) { + if (!isRecord(release) || release.draft === true || typeof release.tag_name !== "string" || !OMP_TAG.test(release.tag_name) || !Array.isArray(release.assets)) continue; + const names = new Set(release.assets.map((asset: unknown) => (isRecord(asset) ? asset.name : undefined))); + if (needed.every((name) => names.has(name))) candidates.push({ tag: release.tag_name, stamp: /-int\.(\d{12})\./.exec(release.tag_name)![1] }); + } + for (const candidate of candidates.sort((a, b) => (a.stamp < b.stamp ? 1 : a.stamp > b.stamp ? -1 : 0))) { + if (await releaseProtocol({ repo, tag: candidate.tag }, read) === RUNNER_PROTOCOL) return candidate; + } + if (listing.length < RELEASE_PAGE_SIZE) return null; + } + return null; +} + +const RELEASES_TTL_MS = 5 * 60_000; +const RELEASES_FAILURE_TTL_MS = 30_000; +let releasesCheck: { at: number; failed: boolean; result: Promise } | undefined; +/** The newest fork release with a runner for this platform and RUNNER_PROTOCOL (findRunnerRelease), null when there is + * none. The listing is cached (a status poll must not spend GitHub's unauthenticated rate limit); `refresh` bypasses the cache. */ +export function latestOmpRunnerRelease(refresh = false): Promise { + if (!refresh && releasesCheck && Date.now() - releasesCheck.at < (releasesCheck.failed ? RELEASES_FAILURE_TTL_MS : RELEASES_TTL_MS)) return releasesCheck.result; + const check: NonNullable = { + at: Date.now(), + failed: false, + result: findRunnerRelease().catch((error: unknown) => { check.failed = true; throw error; }), + }; + releasesCheck = check; + return check.result; +} + +const execFileAsync = promisify(execFile); +/** The runner speaks this ompweb's RUNNER_PROTOCOL (`--runner-info`) and passes omp's own start-up checks. */ +async function probeOmpRunner(bin: string): Promise { + const env = createChildProcessEnvironment(); + // 60 s: a fresh ~120 MB runner's first start on Windows waits on the antivirus scan. + const run = (argument: string) => execFileAsync(bin, [argument], { timeout: 60_000, windowsHide: true, env }); + const printed = (await run("--runner-info")).stdout.trim(); + let info: unknown; + try { info = JSON.parse(printed); } catch { throw new Error(`--runner-info printed ${printed ? JSON.stringify(printed.slice(0, 200)) : "nothing"}, not its JSON`); } + if (!isRecord(info) || info.protocol !== RUNNER_PROTOCOL) throw new Error(`it speaks runner protocol ${isRecord(info) ? String(info.protocol) : "(none)"}, this ompweb speaks ${RUNNER_PROTOCOL}`); + await run("--smoke-test"); +} + +const STAGING = ".staging-"; +const discard = (path: string) => { try { rmSync(path, { recursive: true, force: true }); } catch { /* in use (Windows): the next daemon start's sweep retries */ } }; + +/** + * "Update omp": installs the runner of the fork release `tag` as `/ompweb-omp[.exe]` under this version's runner + * directory. It is downloaded into a fresh staging directory beside it, checked there (SHA-256 from the release's listing, + * then `probe`: --runner-info protocol and --smoke-test), and only then renamed to `` and pointed at by `current.json`. + * Nothing already installed is touched before that: a failure at any step leaves the pointer and the selected runner as + * they were (a copy of `tag` already there is set aside for the swap and put back if the swap fails; running sessions keep + * their file, and Windows, which cannot move a running one, refuses the swap). Returns the installed path. + * `probe` and `repo` are seams for tests. + */ +export async function installOmpRunner(tag: string, ompweb = packageJson.version, probe = probeOmpRunner, repo = OMP_RUNNER_PIN.repo): Promise { + if (!OMP_TAG.test(tag)) throw new Error(`${tag} is not an omp build tag`); + const target = ompRunnerPath(tag, ompweb); + const final = dirname(target); + mkdirSync(ompRunnerRoot(ompweb), { recursive: true }); + const staging = mkdtempSync(join(ompRunnerRoot(ompweb), STAGING)); + const old = `${staging}.old`; + try { + const staged = join(staging, basename(target)); + const sha256 = await downloadRunner({ repo, tag }, OMP_RUNNER_ASSET, staged); + try { await probe(staged); } + catch (error) { throw new Error(`omp ${tag} downloaded but did not pass its checks: ${error instanceof Error ? error.message : String(error)}`); } + const swapped = existsSync(final); + if (swapped) renameSync(final, old); + try { + renameSync(staging, final); + writeOmpPointer({ tag, path: target, sha256 }, ompweb); + } catch (error) { + if (swapped) { rmSync(final, { recursive: true, force: true }); renameSync(old, final); } + throw error; + } + if (swapped) discard(old); + } finally { + discard(staging); + } + return target; +} + +/** At daemon start, once it owns the host's control endpoint (so no other daemon runs from these directories): removes + * every fork release's directory but the pointed one and this build's pinned one, and the leftovers of an install that + * never finished. A runner a process still holds open (Windows) stays until the next start. */ +export function sweepOmpRunners(ompweb = packageJson.version, pinned = OMP_RUNNER_PIN.tag): void { + const keep = new Set([readOmpPointer(ompweb)?.tag, pinned]); + const root = ompRunnerRoot(ompweb); + let names: string[]; + try { names = readdirSync(root); } catch { return; } + for (const name of names) { + if (name.startsWith(STAGING) || (OMP_TAG.test(name) && !keep.has(name))) discard(join(root, name)); } } let ensuring: Promise | null = null; -/** The runner, fetched first when this install has none; null in a source checkout that has not built one. One fetch - * at a time per process; a web server and a daemon fetching together each write their own partial file. */ +/** The runner, fetched first when this install has none. One fetch at a time per process; a web server and a daemon + * fetching together each write their own partial file. */ export function ensureOmpRunner(): Promise { ensuring ??= (async () => { const found = resolveOmpBin(true); - if (found || process.env.OMP_WEB_OMP_BIN || sourceCheckoutOmpBin()) return found; + if (found || process.env.OMP_WEB_OMP_BIN) return found; await fetchOmpRunner(); return resolveOmpBin(true); })().finally(() => { ensuring = null; }); @@ -51,7 +205,7 @@ export function ensureOmpRunner(): Promise { export async function runFetchOmp(): Promise { try { const bin = await ensureOmpRunner(); - if (!bin) throw new Error(process.env.OMP_WEB_OMP_BIN ? `OMP_WEB_OMP_BIN=${process.env.OMP_WEB_OMP_BIN} does not exist` : `this source checkout has no ${sourceCheckoutOmpBin()}: build it with \`bun runner/scripts/build-binary.ts\``); + if (!bin) throw new Error(process.env.OMP_WEB_OMP_BIN ? `OMP_WEB_OMP_BIN=${process.env.OMP_WEB_OMP_BIN} does not exist` : "the runner is still missing after the fetch"); console.log(`omp: ${bin}`); return 0; } catch (error) { diff --git a/lib/rpc-manager.ts b/lib/rpc-manager.ts index 8392d0046..fcff6ac53 100644 --- a/lib/rpc-manager.ts +++ b/lib/rpc-manager.ts @@ -226,7 +226,7 @@ function patchEstimatedTokensAfter(result: unknown): void { // ============================================================================ // AgentSessionWrapper -// Wraps one session's ompweb-omp runner (its channel, runner/src/channel.ts) with the interface the rest of +// Wraps one session's ompweb-omp runner (its channel, packages/ompweb-runner/src/channel.ts of the oh-my-pi fork) with the interface the rest of // the app expects (same command surface pi-web's in-process wrapper offered). // ============================================================================ diff --git a/omp-runner.json b/omp-runner.json new file mode 100644 index 000000000..9dcce21b3 --- /dev/null +++ b/omp-runner.json @@ -0,0 +1,4 @@ +{ + "repo": "ompweb-dev/oh-my-pi", + "tag": "v18.22.0-int.202611172157.ga645f02" +} diff --git a/package.json b/package.json index c35bc791f..da0343abe 100644 --- a/package.json +++ b/package.json @@ -34,13 +34,15 @@ "proxy.ts", "instrumentation.node.ts", "tsconfig.json", - "package.json" + "package.json", + "omp-runner.json" ], "scripts": { "dev": "bun --hot server/dev.ts -H 127.0.0.1 -p 30178", "dev:lan": "bun --hot server/dev.ts -H 0.0.0.0 -p 30178", "build": "bun scripts/build-client.ts", "build:binary": "npm run build && bun scripts/build-binary.ts", + "fetch-omp": "node bin/omp-web.js fetch-omp", "start": "bun server/main.ts -H 127.0.0.1 -p 30177", "start:lan": "bun server/main.ts -H 0.0.0.0 -p 30177", "test": "bun test --parallel --timeout 30000 ./app ./bin ./components ./hooks ./lib ./scripts ./server ./tests", diff --git a/runner/.gitignore b/runner/.gitignore deleted file mode 100644 index 4a7d1e9d6..000000000 --- a/runner/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -node_modules -/release/ -/vendor/ -/vendor.new/ diff --git a/runner/bun.lock b/runner/bun.lock deleted file mode 100644 index 7f1bbdec9..000000000 --- a/runner/bun.lock +++ /dev/null @@ -1,229 +0,0 @@ -{ - "lockfileVersion": 2, - "configVersion": 1, - "workspaces": { - "": { - "name": "@kahme247/ompweb-omp", - "dependencies": { - "@oh-my-pi/pi-coding-agent": "file:./vendor/oh-my-pi-pi-coding-agent-18.20.0.tgz", - "@oh-my-pi/pi-natives": "file:./vendor/oh-my-pi-pi-natives-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-utils": "file:./vendor/oh-my-pi-pi-utils-18.20.0-int.202611131515.g11d9dc7.tgz", - }, - "devDependencies": { - "@types/bun": "1.4.4", - "typescript": "7.0.2", - }, - }, - }, - "overrides": { - "@oh-my-pi/omp-stats": "file:./vendor/oh-my-pi-omp-stats-18.20.0.tgz", - "@oh-my-pi/omptype": "file:./vendor/oh-my-pi-omptype-18.20.0.tgz", - "@oh-my-pi/pi-agent-core": "file:./vendor/oh-my-pi-pi-agent-core-18.20.0.tgz", - "@oh-my-pi/pi-ai": "file:./vendor/oh-my-pi-pi-ai-18.20.0.tgz", - "@oh-my-pi/pi-catalog": "file:./vendor/oh-my-pi-pi-catalog-18.20.0.tgz", - "@oh-my-pi/pi-coding-agent": "file:./vendor/oh-my-pi-pi-coding-agent-18.20.0.tgz", - "@oh-my-pi/pi-mnemopi": "file:./vendor/oh-my-pi-pi-mnemopi-18.20.0.tgz", - "@oh-my-pi/pi-natives": "file:./vendor/oh-my-pi-pi-natives-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-darwin-arm64": "file:./vendor/oh-my-pi-pi-natives-darwin-arm64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-darwin-x64": "file:./vendor/oh-my-pi-pi-natives-darwin-x64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-linux-arm64": "file:./vendor/oh-my-pi-pi-natives-linux-arm64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-linux-x64": "file:./vendor/oh-my-pi-pi-natives-linux-x64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-win32-arm64": "file:./vendor/oh-my-pi-pi-natives-win32-arm64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-win32-x64": "file:./vendor/oh-my-pi-pi-natives-win32-x64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-tui": "file:./vendor/oh-my-pi-pi-tui-18.20.0.tgz", - "@oh-my-pi/pi-utils": "file:./vendor/oh-my-pi-pi-utils-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-wire": "file:./vendor/oh-my-pi-pi-wire-18.20.0.tgz", - "@oh-my-pi/snapcompact": "file:./vendor/oh-my-pi-snapcompact-18.20.0.tgz", - }, - "packages": { - "@babel/helper-string-parser": ["@babel/helper-string-parser@7.29.14", "", {}, "sha512-CXvKMFvM80ihI7Vox9jb+kQjnyjESQNPM+vB7U0Zz6TWuZA+Z/FL0QPMH300h33qJtIx+REeBhXqWOPW48TcAQ=="], - - "@babel/helper-validator-identifier": ["@babel/helper-validator-identifier@7.29.7", "", {}, "sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg=="], - - "@babel/parser": ["@babel/parser@7.29.14", "", { "dependencies": { "@babel/types": "^7.29.14" }, "bin": "./bin/babel-parser.js" }, "sha512-VL220vXuyO2z3rNIQwY7ftj9UwpqOt+33sH+sBWkOekbsSR2g7EEMnOx24ZlCH0xOA3DM/33SifVFGbnJD8r/w=="], - - "@babel/types": ["@babel/types@7.29.14", "", { "dependencies": { "@babel/helper-string-parser": "^7.29.14", "@babel/helper-validator-identifier": "^7.29.7" } }, "sha512-UjI/dKmwJwT9pOszJitq2G6EObZPnsZUfdCr92z2TlF3EZK9J3EdfOKUrCzHl6wFthbzwhzl1Soc/UIwxRiq6w=="], - - "@oh-my-pi/omp-stats": ["@oh-my-pi/omp-stats@./vendor/oh-my-pi-omp-stats-18.20.0.tgz", { "dependencies": { "@oh-my-pi/pi-ai": "18.20.0", "@oh-my-pi/pi-catalog": "18.20.0", "@oh-my-pi/pi-utils": "18.20.0", "lucide-react": "^1.24.0", "react": "19.2.7", "react-dom": "19.2.7" }, "bin": { "omp-stats": "./src/index.ts" } }, "sha512-KIjc4TEwokONT0UZ5RxSRieHaL8zu3qnpxanP8YMSc1r0F+SzXkzynw9rS8Hxc61SBUCxM1Q+IgLXiw99M8Uhw=="], - - "@oh-my-pi/omptype": ["@oh-my-pi/omptype@./vendor/oh-my-pi-omptype-18.20.0.tgz", {}, "sha512-Z+Gbo56PjzzlM1IDnKna8bVD8zaEE+SWOGliuzG9NpalnXwK12NQki7vVP09N4IgDQdXK6jQmEg+VZbFFXLYSw=="], - - "@oh-my-pi/pi-agent-core": ["@oh-my-pi/pi-agent-core@./vendor/oh-my-pi-pi-agent-core-18.20.0.tgz", { "dependencies": { "@oh-my-pi/pi-ai": "18.20.0", "@oh-my-pi/pi-catalog": "18.20.0", "@oh-my-pi/pi-natives": "18.20.0", "@oh-my-pi/pi-utils": "18.20.0", "@oh-my-pi/pi-wire": "18.20.0", "@oh-my-pi/snapcompact": "18.20.0", "@opentelemetry/api": "^1.9.1" } }, "sha512-NBkX95BovCEu2iCgfv/jq0OqcES+bTdBUJc+h7V8H0v5TGrDXm7VdKmBn09aN4AuUTO9TQ9oj+fQNONHEntODA=="], - - "@oh-my-pi/pi-ai": ["@oh-my-pi/pi-ai@./vendor/oh-my-pi-pi-ai-18.20.0.tgz", { "dependencies": { "@oh-my-pi/omptype": "18.20.0", "@oh-my-pi/pi-catalog": "18.20.0", "@oh-my-pi/pi-natives": "18.20.0", "@oh-my-pi/pi-utils": "18.20.0", "@oh-my-pi/pi-wire": "18.20.0" } }, "sha512-nKiyG1GdOvF/lB7+1BJvGZunU+ScnKRcM4mL6hzVV4lMn5pxsTpdzY7VDhYooaPmYK/KjtRAPkbytJY5yriXvg=="], - - "@oh-my-pi/pi-catalog": ["@oh-my-pi/pi-catalog@./vendor/oh-my-pi-pi-catalog-18.20.0.tgz", { "dependencies": { "@oh-my-pi/omptype": "18.20.0", "@oh-my-pi/pi-utils": "18.20.0" } }, "sha512-dvW4aF9GW9myjosmrDau7cllISORh83BKdMRzaISCqgeJdJodvyH56GXSRjCFu9H8/Oz/0gO8/VaZiRM2TNJQg=="], - - "@oh-my-pi/pi-coding-agent": ["@oh-my-pi/pi-coding-agent@./vendor/oh-my-pi-pi-coding-agent-18.20.0.tgz", { "dependencies": { "@babel/parser": "^7.29.7", "@oh-my-pi/omp-stats": "18.20.0", "@oh-my-pi/omptype": "18.20.0", "@oh-my-pi/pi-agent-core": "18.20.0", "@oh-my-pi/pi-ai": "18.20.0", "@oh-my-pi/pi-catalog": "18.20.0", "@oh-my-pi/pi-mnemopi": "18.20.0", "@oh-my-pi/pi-natives": "18.20.0", "@oh-my-pi/pi-tui": "18.20.0", "@oh-my-pi/pi-utils": "18.20.0", "@oh-my-pi/pi-wire": "18.20.0", "@oh-my-pi/snapcompact": "18.20.0", "@opentelemetry/api": "^1.9.1", "@opentelemetry/api-logs": "^0.220.0", "@opentelemetry/context-async-hooks": "2.9.0", "@opentelemetry/exporter-logs-otlp-proto": "^0.220.0", "@opentelemetry/exporter-metrics-otlp-proto": "^0.220.0", "@opentelemetry/exporter-trace-otlp-proto": "^0.220.0", "@opentelemetry/resources": "2.9.0", "@opentelemetry/sdk-logs": "^0.220.0", "@opentelemetry/sdk-metrics": "2.9.0", "@opentelemetry/sdk-trace-base": "2.9.0", "@opentelemetry/sdk-trace-node": "2.9.0", "axe-core": "4.13.0", "puppeteer-core": "25.3.0", "yaml": "2.9.1" }, "bin": { "omp": "dist/cli.js" } }, "sha512-d0AOI4AAbUgHBhnv2kMNDaBKJWZLHJEbZcsgSpGEPczpsqaHxJFTDeCehvx3Z12AAEaFCojd4FCiRQzB8qd/iA=="], - - "@oh-my-pi/pi-mnemopi": ["@oh-my-pi/pi-mnemopi@./vendor/oh-my-pi-pi-mnemopi-18.20.0.tgz", { "dependencies": { "@oh-my-pi/pi-ai": "18.20.0", "@oh-my-pi/pi-catalog": "18.20.0", "@oh-my-pi/pi-natives": "18.20.0", "@oh-my-pi/pi-utils": "18.20.0" }, "peerDependencies": { "fastembed": "3.0.0", "onnxruntime-node": "1.21.0" }, "optionalPeers": ["fastembed", "onnxruntime-node"], "bin": { "mnemopi": "src/cli.ts" } }, "sha512-cNUCh9uWyAwpKVf7jLO6bq3HQDHIw3Ydn39EMqY3tuounFZDpx7K+eQ4MzSUPrnv5a5ztaJjF38brBmNFYjLgQ=="], - - "@oh-my-pi/pi-natives": ["@oh-my-pi/pi-natives@./vendor/oh-my-pi-pi-natives-18.20.0-int.202611131515.g11d9dc7.tgz", { "optionalDependencies": { "@oh-my-pi/pi-natives-darwin-arm64": "18.20.0-int.202611131515.g11d9dc7", "@oh-my-pi/pi-natives-darwin-x64": "18.20.0-int.202611131515.g11d9dc7", "@oh-my-pi/pi-natives-linux-arm64": "18.20.0-int.202611131515.g11d9dc7", "@oh-my-pi/pi-natives-linux-x64": "18.20.0-int.202611131515.g11d9dc7", "@oh-my-pi/pi-natives-win32-arm64": "18.20.0-int.202611131515.g11d9dc7", "@oh-my-pi/pi-natives-win32-x64": "18.20.0-int.202611131515.g11d9dc7" } }, "sha512-yAqFgPHDyC4QfGHH73vwjaio3y17FvE9B3aLVjeFdT24oEB60MP5LZmYxwqAhbztdWY/iKdSNw0ONA9TGLDpFA=="], - - "@oh-my-pi/pi-natives-darwin-arm64": ["@oh-my-pi/pi-natives-darwin-arm64@./vendor/oh-my-pi-pi-natives-darwin-arm64-18.20.0-int.202611131515.g11d9dc7.tgz", {}, "sha512-JdQYZAoYLD3hmWC+XJd7eIa0ezTm1JsQDFdpSYGqWtsa10HidnWDF4/FEsNSuEh3VfZx4lrHyb5K/YOws/3E9Q=="], - - "@oh-my-pi/pi-natives-darwin-x64": ["@oh-my-pi/pi-natives-darwin-x64@./vendor/oh-my-pi-pi-natives-darwin-x64-18.20.0-int.202611131515.g11d9dc7.tgz", {}, "sha512-ZvQ+vVRv/w88LLbzriOTWBISvJB1RWTG6QeSifDZm+hWE8LOvWqlhVCCZNqhXmrnJnAJzkcRZ9hvQ0LdrDrgqg=="], - - "@oh-my-pi/pi-natives-linux-arm64": ["@oh-my-pi/pi-natives-linux-arm64@./vendor/oh-my-pi-pi-natives-linux-arm64-18.20.0-int.202611131515.g11d9dc7.tgz", {}, "sha512-+gkkfbQtGq3v8uWB5IeNvJg2P9RW4Qyd/bpwXbG8xIA0pTH2TZbQ3inS+4W9al0Fv8LxzRxDZOS3vtClVYXoTw=="], - - "@oh-my-pi/pi-natives-linux-x64": ["@oh-my-pi/pi-natives-linux-x64@./vendor/oh-my-pi-pi-natives-linux-x64-18.20.0-int.202611131515.g11d9dc7.tgz", {}, "sha512-+3NBcj1sw5ICLJi7VDCvtZpQ5stvqXce21KNWKx03LwiF+Sme9Wiyk2VGk+nyfcVacGrTuk6AhwtN11IWTPirg=="], - - "@oh-my-pi/pi-natives-win32-arm64": ["@oh-my-pi/pi-natives-win32-arm64@./vendor/oh-my-pi-pi-natives-win32-arm64-18.20.0-int.202611131515.g11d9dc7.tgz", {}, "sha512-bMplxB8IYfztG+y75uC5Tk3LaKhBtz79oIG+T9jzV4BDcw30ytz0z45MQ7HIJcY5o1kick0Irz/EIb/406NRvQ=="], - - "@oh-my-pi/pi-natives-win32-x64": ["@oh-my-pi/pi-natives-win32-x64@./vendor/oh-my-pi-pi-natives-win32-x64-18.20.0-int.202611131515.g11d9dc7.tgz", {}, "sha512-DV///y1oYQpUa7+wcojYBzx20f4C+CeoBKWzuli8ou7isTpBN5MTFzt6BnEmjNRvzokUc0ZT7k9TAeFuMe2fbQ=="], - - "@oh-my-pi/pi-tui": ["@oh-my-pi/pi-tui@./vendor/oh-my-pi-pi-tui-18.20.0.tgz", { "dependencies": { "@oh-my-pi/omptype": "18.20.0", "@oh-my-pi/pi-agent-core": "18.20.0", "@oh-my-pi/pi-ai": "18.20.0", "@oh-my-pi/pi-catalog": "18.20.0", "@oh-my-pi/pi-natives": "18.20.0", "@oh-my-pi/pi-utils": "18.20.0", "@oh-my-pi/pi-wire": "18.20.0", "@oh-my-pi/snapcompact": "18.20.0" } }, "sha512-Lxmg7twgyCCNUCpor4mWrQA3RMZhnoGt//Xbbvl7aNHXImxO2FLPC+W4LvaeFNHikGc4vv1RT9YqKpCvN49yQw=="], - - "@oh-my-pi/pi-utils": ["@oh-my-pi/pi-utils@./vendor/oh-my-pi-pi-utils-18.20.0-int.202611131515.g11d9dc7.tgz", { "dependencies": { "@oh-my-pi/omptype": "18.20.0", "@oh-my-pi/pi-natives": "18.20.0" } }, "sha512-DNOzJE3hXBvFEjcHfQ+eRjbsT0BhbsSFBDESMP/DOKXbNtzKkkcM3m4zQAP9CP+xvlFGvH85fdYUWgYDlsdUTg=="], - - "@oh-my-pi/pi-wire": ["@oh-my-pi/pi-wire@./vendor/oh-my-pi-pi-wire-18.20.0.tgz", {}, "sha512-fiwQsxyxwb6eGLgg8QFWU6SKB/4K12wDEhRhgPzLL0O/Q5kExDnMndMp7TDFSX8Ysn6/jkU/Fn/CvWW9UXVAbg=="], - - "@oh-my-pi/snapcompact": ["@oh-my-pi/snapcompact@./vendor/oh-my-pi-snapcompact-18.20.0.tgz", { "dependencies": { "@oh-my-pi/omptype": "18.20.0", "@oh-my-pi/pi-ai": "18.20.0", "@oh-my-pi/pi-catalog": "18.20.0", "@oh-my-pi/pi-natives": "18.20.0", "@oh-my-pi/pi-utils": "18.20.0", "@oh-my-pi/pi-wire": "18.20.0" } }, "sha512-z7OahHZ21VUwnU/rZlJpzBYC0ibJPCiEaytGuegOXfpbxRjLIHhEGxycRRW9I26dA9s/mEqiTX/Q3rdDBPsYcg=="], - - "@opentelemetry/api": ["@opentelemetry/api@1.10.0", "", {}, "sha512-lMkyes8iHrG+mk6WH7X70MdCLI2RS0FwNBY1PyQ8UemaijE2m5mv5A0Niy3HtbYTFrgkUEQjFMXmSAk2hQDuPQ=="], - - "@opentelemetry/api-logs": ["@opentelemetry/api-logs@0.220.0", "", { "dependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-CmVa4ImJ+ynfrPMNaAXHET6Bhb44SwzmfyVJFq9ni2jgXJR/l7C6gfVFddNmHP+ZOkP9cf4f9DBe68qVLTHc9w=="], - - "@opentelemetry/context-async-hooks": ["@opentelemetry/context-async-hooks@2.9.0", "", { "peerDependencies": { "@opentelemetry/api": ">=1.0.0 <1.10.0" } }, "sha512-OQ0vzvbZBiUhjqLnUaoNfYmP8553Crr3aggB4y0ZUi815mZ7idpdJXQmoKdeBKJelYttoBlLSSHubmyw3wvX4w=="], - - "@opentelemetry/core": ["@opentelemetry/core@2.9.0", "", { "dependencies": { "@opentelemetry/semantic-conventions": "^1.29.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.0.0 <1.10.0" } }, "sha512-m2nckMT80NnmjTYSPjJQObBJ+8dgkoajEOUbznL8AHZ3T3yHRk2P7gI1PhEBc1+lOnrYE9UWrWHqJDsmqjmNbw=="], - - "@opentelemetry/exporter-logs-otlp-proto": ["@opentelemetry/exporter-logs-otlp-proto@0.220.0", "", { "dependencies": { "@opentelemetry/otlp-exporter-base": "0.220.0", "@opentelemetry/otlp-transformer": "0.220.0", "@opentelemetry/sdk-logs": "0.220.0" }, "peerDependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-8LZAxdJ0ENDAFwr4j0oY35mHBltiSzvlhdQAPGiC7p9VnxtuSq4SW1gfBAdW6t6hiQG6OwUl8w7KHaOdJPKHWg=="], - - "@opentelemetry/exporter-metrics-otlp-http": ["@opentelemetry/exporter-metrics-otlp-http@0.220.0", "", { "dependencies": { "@opentelemetry/core": "2.9.0", "@opentelemetry/otlp-exporter-base": "0.220.0", "@opentelemetry/otlp-transformer": "0.220.0", "@opentelemetry/resources": "2.9.0", "@opentelemetry/sdk-metrics": "2.9.0" }, "peerDependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-Yqt3RBw/bRVncaE9qIIhk4WfjbAQqXuP9FgAaU+IKPndnLEp/cUqZlSC324+bpmduRz7DoTjig8Ub0PeILWXUA=="], - - "@opentelemetry/exporter-metrics-otlp-proto": ["@opentelemetry/exporter-metrics-otlp-proto@0.220.0", "", { "dependencies": { "@opentelemetry/core": "2.9.0", "@opentelemetry/exporter-metrics-otlp-http": "0.220.0", "@opentelemetry/otlp-exporter-base": "0.220.0", "@opentelemetry/otlp-transformer": "0.220.0", "@opentelemetry/resources": "2.9.0", "@opentelemetry/sdk-metrics": "2.9.0" }, "peerDependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-lyO+IQBdSvqHN/ZOW/OzrSWemtfD+HgWngn+HBNLhjy0YrCQQTz0OE/kSekH2Pl340dn9DWzhqHdz5Eftr+HLA=="], - - "@opentelemetry/exporter-trace-otlp-proto": ["@opentelemetry/exporter-trace-otlp-proto@0.220.0", "", { "dependencies": { "@opentelemetry/core": "2.9.0", "@opentelemetry/otlp-exporter-base": "0.220.0", "@opentelemetry/otlp-transformer": "0.220.0", "@opentelemetry/resources": "2.9.0", "@opentelemetry/sdk-trace": "2.9.0" }, "peerDependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-voTAD8XgJxlK7zLkXh8EzMB09zrQr3tyY/BsnDTlDiQU/UdK58MZ63A3mUjdEDrxMjCVmBHU3WQJhRmQe+Dvzg=="], - - "@opentelemetry/otlp-exporter-base": ["@opentelemetry/otlp-exporter-base@0.220.0", "", { "dependencies": { "@opentelemetry/core": "2.9.0", "@opentelemetry/otlp-transformer": "0.220.0" }, "peerDependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-CXYo8UD5Mn9YbgebO2EL4wejtA+gxLmLiu6HCk2KH2BR7XhFN6/6p1UlCb23DYCjeYkndevLHuejCCN1yx4+OQ=="], - - "@opentelemetry/otlp-transformer": ["@opentelemetry/otlp-transformer@0.220.0", "", { "dependencies": { "@opentelemetry/api-logs": "0.220.0", "@opentelemetry/core": "2.9.0", "@opentelemetry/resources": "2.9.0", "@opentelemetry/sdk-logs": "0.220.0", "@opentelemetry/sdk-metrics": "2.9.0", "@opentelemetry/sdk-trace": "2.9.0" }, "peerDependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-lXGrv7KXZ0gNH9SVNUaa6vv6phVYGvJxfXAlMbzbakiXru75f5MZl8Z7oqiMMQD77riVHJCFlQvbZs/VVN2/4A=="], - - "@opentelemetry/resources": ["@opentelemetry/resources@2.9.0", "", { "dependencies": { "@opentelemetry/core": "2.9.0", "@opentelemetry/semantic-conventions": "^1.29.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.3.0 <1.10.0" } }, "sha512-jyA5MBLQ+Dkl3+JsZkUoUvL7yHvU64kLsvpXKarWm6347Sl1t1bXFTFykUePNpT5WH5pm9a2Qtt03iIYQhZ1Fg=="], - - "@opentelemetry/sdk-logs": ["@opentelemetry/sdk-logs@0.220.0", "", { "dependencies": { "@opentelemetry/api-logs": "0.220.0", "@opentelemetry/core": "2.9.0", "@opentelemetry/resources": "2.9.0", "@opentelemetry/semantic-conventions": "^1.29.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.4.0 <1.10.0" } }, "sha512-WywcTkQtv2iNmt+6y5Kcd4rzvx9bLVsBa2Nwcmg01IUaBTkTow3W4d9KE5vNBpEDtb9tp21WcRBY/lANRrApYA=="], - - "@opentelemetry/sdk-metrics": ["@opentelemetry/sdk-metrics@2.9.0", "", { "dependencies": { "@opentelemetry/core": "2.9.0", "@opentelemetry/resources": "2.9.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.9.0 <1.10.0" } }, "sha512-Xx8RGS4H5XEBl01WuCreMIpiah9cCXMbSkeuIePPdD2cUpq/vUzYmj8E/MK1OsbOc93FuAD4jfn2WOacKwLn7Q=="], - - "@opentelemetry/sdk-trace": ["@opentelemetry/sdk-trace@2.9.0", "", { "dependencies": { "@opentelemetry/core": "2.9.0", "@opentelemetry/resources": "2.9.0", "@opentelemetry/semantic-conventions": "^1.29.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.3.0 <1.10.0" } }, "sha512-sGA19HvtrrSKYsseHphluH6j3p6Xa3fqc7c7y8f/7mYWejc1lyDFcpSdD1kYa50HCLUeEo4zA5bW0pniaPszuw=="], - - "@opentelemetry/sdk-trace-base": ["@opentelemetry/sdk-trace-base@2.9.0", "", { "dependencies": { "@opentelemetry/core": "2.9.0", "@opentelemetry/resources": "2.9.0", "@opentelemetry/sdk-trace": "2.9.0", "@opentelemetry/semantic-conventions": "^1.29.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.3.0 <1.10.0" } }, "sha512-cp9zmTl62R8PJrpvFcmc8N2JQU/xfa0S+61q511Nji+QxCfZ8Ifvg7H27G8cANe4crg4RTrWsVvanHiXjSp6ag=="], - - "@opentelemetry/sdk-trace-node": ["@opentelemetry/sdk-trace-node@2.9.0", "", { "dependencies": { "@opentelemetry/context-async-hooks": "2.9.0", "@opentelemetry/core": "2.9.0", "@opentelemetry/sdk-trace-base": "2.9.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.0.0 <1.10.0" } }, "sha512-ec9a7ps37huy5itYk0MalaZdSLlM6AXWp/FhtEjgMpp5leEGojBDvAl/UWttQnkMZOvFHKzRESn8TD3yKTF5nQ=="], - - "@opentelemetry/semantic-conventions": ["@opentelemetry/semantic-conventions@1.45.0", "", {}, "sha512-aOP45mbcOKbQVvFWfQmgoOPT8R2iwbbbgl83k4CujGV9BrJhFr3qG55MFpwFs4YLabpuW3HhzUY9eQxPlDqaZw=="], - - "@puppeteer/browsers": ["@puppeteer/browsers@3.0.6", "", { "dependencies": { "modern-tar": "^0.7.6", "yargs": "^18.0.0" }, "peerDependencies": { "proxy-agent": ">=8.0.1", "yauzl": "^2.10.0 || ^3.4.0" }, "optionalPeers": ["proxy-agent", "yauzl"], "bin": { "browsers": "lib/main-cli.js" } }, "sha512-B/gKoqlFkzhvzsI6jo9K1cZz9o5ypviVv/xu8CwA4grZzyVwN+XfkT+tu8T1zrauuEXv6VhS2oGX+6NL95WcKA=="], - - "@types/bun": ["@types/bun@1.4.4", "", { "dependencies": { "bun-types": "1.4.4" } }, "sha512-9wkRDXUXK124pEnAutE6n/l18j+i9GC5IAHK8TmX/2nlySETmky0OU6yL5U8pW1AoKh++AN9auDCr647BfurjQ=="], - - "@types/node": ["@types/node@26.6.6", "", { "dependencies": { "undici-types": "~8.9.0" } }, "sha512-MzQj+8Kx3XI40OmUAE6Qoh58ZMNstNNMwn2nfd3qWsfe7nDNSqJNzes4njhz6AHwrSHOffSLwXU6yb474Q4aog=="], - - "@typescript/typescript-aix-ppc64": ["@typescript/typescript-aix-ppc64@7.0.2", "", { "os": "aix", "cpu": "ppc64" }, "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ=="], - - "@typescript/typescript-darwin-arm64": ["@typescript/typescript-darwin-arm64@7.0.2", "", { "os": "darwin", "cpu": "arm64" }, "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA=="], - - "@typescript/typescript-darwin-x64": ["@typescript/typescript-darwin-x64@7.0.2", "", { "os": "darwin", "cpu": "x64" }, "sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA=="], - - "@typescript/typescript-freebsd-arm64": ["@typescript/typescript-freebsd-arm64@7.0.2", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ=="], - - "@typescript/typescript-freebsd-x64": ["@typescript/typescript-freebsd-x64@7.0.2", "", { "os": "freebsd", "cpu": "x64" }, "sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw=="], - - "@typescript/typescript-linux-arm": ["@typescript/typescript-linux-arm@7.0.2", "", { "os": "linux", "cpu": "arm" }, "sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ=="], - - "@typescript/typescript-linux-arm64": ["@typescript/typescript-linux-arm64@7.0.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ=="], - - "@typescript/typescript-linux-loong64": ["@typescript/typescript-linux-loong64@7.0.2", "", { "os": "linux", "cpu": "none" }, "sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ=="], - - "@typescript/typescript-linux-mips64el": ["@typescript/typescript-linux-mips64el@7.0.2", "", { "os": "linux", "cpu": "none" }, "sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA=="], - - "@typescript/typescript-linux-ppc64": ["@typescript/typescript-linux-ppc64@7.0.2", "", { "os": "linux", "cpu": "ppc64" }, "sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA=="], - - "@typescript/typescript-linux-riscv64": ["@typescript/typescript-linux-riscv64@7.0.2", "", { "os": "linux", "cpu": "none" }, "sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ=="], - - "@typescript/typescript-linux-s390x": ["@typescript/typescript-linux-s390x@7.0.2", "", { "os": "linux", "cpu": "s390x" }, "sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw=="], - - "@typescript/typescript-linux-x64": ["@typescript/typescript-linux-x64@7.0.2", "", { "os": "linux", "cpu": "x64" }, "sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A=="], - - "@typescript/typescript-netbsd-arm64": ["@typescript/typescript-netbsd-arm64@7.0.2", "", { "os": "none", "cpu": "arm64" }, "sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA=="], - - "@typescript/typescript-netbsd-x64": ["@typescript/typescript-netbsd-x64@7.0.2", "", { "os": "none", "cpu": "x64" }, "sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA=="], - - "@typescript/typescript-openbsd-arm64": ["@typescript/typescript-openbsd-arm64@7.0.2", "", { "os": "openbsd", "cpu": "arm64" }, "sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ=="], - - "@typescript/typescript-openbsd-x64": ["@typescript/typescript-openbsd-x64@7.0.2", "", { "os": "openbsd", "cpu": "x64" }, "sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg=="], - - "@typescript/typescript-sunos-x64": ["@typescript/typescript-sunos-x64@7.0.2", "", { "os": "sunos", "cpu": "x64" }, "sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g=="], - - "@typescript/typescript-win32-arm64": ["@typescript/typescript-win32-arm64@7.0.2", "", { "os": "win32", "cpu": "arm64" }, "sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ=="], - - "@typescript/typescript-win32-x64": ["@typescript/typescript-win32-x64@7.0.2", "", { "os": "win32", "cpu": "x64" }, "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g=="], - - "ansi-regex": ["ansi-regex@6.6.0", "", {}, "sha512-XZ5apbzreCbXSKwPjJstu8R0zOi247w3XYwWxtwBXgpZrCyc1Ka7p9/xY0A9/diMewmHbEKz+FIraWaEWjlN5g=="], - - "ansi-styles": ["ansi-styles@6.2.3", "", {}, "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg=="], - - "axe-core": ["axe-core@4.13.0", "", {}, "sha512-UzGt8zg7Ny8djbYMhxl2zuEevVa7r2gJjYY5Lwr1xM7+XU2nd6CkIWFTVcCIbAP63vSz71NaVyyuSk9lHKcy0A=="], - - "bun-types": ["bun-types@1.4.4", "", { "dependencies": { "@types/node": "*", "undici-types": "*" } }, "sha512-yjrJh5oGX7mMPKGd19YXyIjmEuj4G4zNC30bs7PM9Az7NQ3ndF3j6hbeRdvJTRQEhJ/FYQwW0a2moweEi2hMxQ=="], - - "chromium-bidi": ["chromium-bidi@16.0.1", "", { "dependencies": { "mitt": "^3.0.1", "zod": "^3.24.1" }, "peerDependencies": { "devtools-protocol": "*" } }, "sha512-J63PGu/9PpeCwLIcKYyzWP6yaVL5pxuBc0shlYCYM8BaAkmlwiQboXO1iNbOgSDbVklEyYFfNEcHD8oOAWacUA=="], - - "cliui": ["cliui@9.0.1", "", { "dependencies": { "string-width": "^7.2.0", "strip-ansi": "^7.1.0", "wrap-ansi": "^9.0.0" } }, "sha512-k7ndgKhwoQveBL+/1tqGJYNz097I7WOvwbmmU2AR5+magtbjPWQTS1C5vzGkBC8Ym8UWRzfKUzUUqFLypY4Q+w=="], - - "devtools-protocol": ["devtools-protocol@0.0.1638949", "", {}, "sha512-mXwg4Fqnv0WR4iuAT/gYUmctNkjILwXFHyZ+m7Ty1dfr0ezZt2U3gnrrJTfRobJTHoXf+IbuFvFITzLrLFjwJA=="], - - "emoji-regex": ["emoji-regex@10.6.0", "", {}, "sha512-toUI84YS5YmxW219erniWD0CIVOo46xGKColeNQRgOzDorgBi1v4D71/OFzgD9GO2UGKIv1C3Sp8DAn0+j5w7A=="], - - "escalade": ["escalade@3.2.0", "", {}, "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA=="], - - "get-caller-file": ["get-caller-file@2.0.5", "", {}, "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg=="], - - "get-east-asian-width": ["get-east-asian-width@1.7.0", "", {}, "sha512-XjH1AECxf0giL2V1aU8vKyRR2ppRUb5c0EvT7zuJTokQ74bNo52zOtghqdWIqrhUD79fo3x0WfKZdOqxF6LG1Q=="], - - "lucide-react": ["lucide-react@1.72.0", "", { "peerDependencies": { "@types/react": "*", "react": "^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0" }, "optionalPeers": ["@types/react"] }, "sha512-fbF5i1GU8GODETa3Q7rXqrJrcx3iPYrQtxthom103zGx+Z0BGwKYAW9s+9435ohYJ7hQm0av0PoZcqI1CRwNrg=="], - - "mitt": ["mitt@3.0.1", "", {}, "sha512-vKivATfr97l2/QBCYAkXYDbrIWPM2IIKEl7YPhjCvKlG3kE2gm+uBo6nEXK3M5/Ffh/FLpKExzOQ3JJoJGFKBw=="], - - "modern-tar": ["modern-tar@0.7.7", "", {}, "sha512-t9VmxaqrmANnEOBhpSDI6HD192Ge48k8vmWqQQL7hSFEqHEYwZbbsu49+aKLWZeRvFs3j1pMhXOqqF4kPlvjkQ=="], - - "puppeteer-core": ["puppeteer-core@25.3.0", "", { "dependencies": { "@puppeteer/browsers": "3.0.6", "chromium-bidi": "16.0.1", "devtools-protocol": "0.0.1638949", "typed-query-selector": "^2.12.2", "webdriver-bidi-protocol": "0.4.2", "ws": "^8.21.0" } }, "sha512-fm+wpUr2oigH1PXZvwgATrM2tYWHMDG8ASzTEe9uukCye4X5Ldx1K5BTHPFKITrIWvQQAQ256d1NpbEveBcKjA=="], - - "react": ["react@19.2.7", "", {}, "sha512-HNe9WslTbXmFK8o8cmwgAeJFSBvt1bPdHCVKtaaV+WlAN36mpT4hcRpwbf3fY56ar2oIXzsBpOAiIRHAdY0OlQ=="], - - "react-dom": ["react-dom@19.2.7", "", { "dependencies": { "scheduler": "^0.27.0" }, "peerDependencies": { "react": "^19.2.7" } }, "sha512-t0BRVXvbiE/o20Hfw669rLbMCDWtYZLvmJigy2f0MxsXF+71pxhR3xOkspmsO8h3ZlNzyibAmtCa3l4lYKk6gQ=="], - - "scheduler": ["scheduler@0.27.0", "", {}, "sha512-eNv+WrVbKu1f3vbYJT/xtiF5syA5HPIMtf9IgY/nKg0sWqzAUEvqY/xm7OcZc/qafLx/iO9FgOmeSAp4v5ti/Q=="], - - "string-width": ["string-width@8.3.0", "", { "dependencies": { "get-east-asian-width": "^1.5.0", "strip-ansi": "^7.1.2" } }, "sha512-ZbmZM0JCihQN91dWnxoipT2KOEyHqEyfRXUyjuRhW8b/xnqPDoq4gWEVApTVa9db2wN8mmoikgFBbjh71+cGeQ=="], - - "strip-ansi": ["strip-ansi@7.2.0", "", { "dependencies": { "ansi-regex": "^6.2.2" } }, "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w=="], - - "typed-query-selector": ["typed-query-selector@2.12.3", "", {}, "sha512-/dtuMZyutHLlrxWhhC1uib00ELUr2dZ8C9B8YzZYsJmeE3V56OxRblrSM2UaF142pcMtak7ehZ6+poWGeehNIg=="], - - "typescript": ["typescript@7.0.2", "", { "optionalDependencies": { "@typescript/typescript-aix-ppc64": "7.0.2", "@typescript/typescript-darwin-arm64": "7.0.2", "@typescript/typescript-darwin-x64": "7.0.2", "@typescript/typescript-freebsd-arm64": "7.0.2", "@typescript/typescript-freebsd-x64": "7.0.2", "@typescript/typescript-linux-arm": "7.0.2", "@typescript/typescript-linux-arm64": "7.0.2", "@typescript/typescript-linux-loong64": "7.0.2", "@typescript/typescript-linux-mips64el": "7.0.2", "@typescript/typescript-linux-ppc64": "7.0.2", "@typescript/typescript-linux-riscv64": "7.0.2", "@typescript/typescript-linux-s390x": "7.0.2", "@typescript/typescript-linux-x64": "7.0.2", "@typescript/typescript-netbsd-arm64": "7.0.2", "@typescript/typescript-netbsd-x64": "7.0.2", "@typescript/typescript-openbsd-arm64": "7.0.2", "@typescript/typescript-openbsd-x64": "7.0.2", "@typescript/typescript-sunos-x64": "7.0.2", "@typescript/typescript-win32-arm64": "7.0.2", "@typescript/typescript-win32-x64": "7.0.2" }, "bin": { "tsc": "bin/tsc" } }, "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA=="], - - "undici-types": ["undici-types@8.9.0", "", {}, "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg=="], - - "webdriver-bidi-protocol": ["webdriver-bidi-protocol@0.4.2", "", {}, "sha512-VSV+fzfChirL3e7jay2yUC7B4HQCGtEWEg/MSSQbK+qWbqeGlRLlXTzPpYr3XGUvbpDHumWZBJxgesg4N7dbtA=="], - - "wrap-ansi": ["wrap-ansi@9.0.2", "", { "dependencies": { "ansi-styles": "^6.2.1", "string-width": "^7.0.0", "strip-ansi": "^7.1.0" } }, "sha512-42AtmgqjV+X1VpdOfyTGOYRi0/zsoLqtXQckTmqTeybT+BDIbM/Guxo7x3pE2vtpr1ok6xRqM9OpBe+Jyoqyww=="], - - "ws": ["ws@8.22.2", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-/rEZ2ejown2N5gxVTHUu9bVVC7ttu0/Tgzf5hbTOixR3Fs7mbbXgn2DbdbcKgJOmpQMtrIUWivtYuVMnp/W/yA=="], - - "y18n": ["y18n@5.0.8", "", {}, "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA=="], - - "yaml": ["yaml@2.9.1", "", { "bin": { "yaml": "bin.mjs" } }, "sha512-3NxN8+78OdzbT7C/WjGsyfPAtJaN3FNDsWxv7Y7mcDsT/oOmgW8BpyQQFFBnvZE3j9Y2Sdz1ULFLezL7Eb2yFw=="], - - "yargs": ["yargs@18.2.0", "", { "dependencies": { "cliui": "^9.0.1", "escalade": "^3.1.1", "get-caller-file": "^2.0.5", "string-width": "^8.2.1", "y18n": "^5.0.5", "yargs-parser": "^22.0.0" } }, "sha512-9OpKOLeaoNFecEp7P6iYbzze/5CqWoH7N3SMs/6y1XF4nMvFMspEGZzJ6uFi9MmQwXhyzqYoSEKO3tvA3Z/o2w=="], - - "yargs-parser": ["yargs-parser@22.0.0", "", {}, "sha512-rwu/ClNdSMpkSrUb+d6BRsSkLUq1fmfsY6TOpYzTwvwkg1/NRG85KBy3kq++A8LKQwX6lsu+aWad+2khvuXrqw=="], - - "zod": ["zod@3.25.76", "", {}, "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ=="], - - "cliui/string-width": ["string-width@7.2.0", "", { "dependencies": { "emoji-regex": "^10.3.0", "get-east-asian-width": "^1.0.0", "strip-ansi": "^7.1.0" } }, "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ=="], - - "wrap-ansi/string-width": ["string-width@7.2.0", "", { "dependencies": { "emoji-regex": "^10.3.0", "get-east-asian-width": "^1.0.0", "strip-ansi": "^7.1.0" } }, "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ=="], - } -} diff --git a/runner/omp-build.json b/runner/omp-build.json deleted file mode 100644 index 40ab09256..000000000 --- a/runner/omp-build.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "repo": "ompweb-dev/oh-my-pi", - "tag": "v18.20.0-int.202611131515.g11d9dc7" -} diff --git a/runner/package.json b/runner/package.json deleted file mode 100644 index 083cb6b85..000000000 --- a/runner/package.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "name": "@kahme247/ompweb-omp", - "private": true, - "type": "module", - "scripts": { - "build": "bun scripts/build-binary.ts" - }, - "dependencies": { - "@oh-my-pi/pi-coding-agent": "file:./vendor/oh-my-pi-pi-coding-agent-18.20.0.tgz", - "@oh-my-pi/pi-natives": "file:./vendor/oh-my-pi-pi-natives-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-utils": "file:./vendor/oh-my-pi-pi-utils-18.20.0-int.202611131515.g11d9dc7.tgz" - }, - "devDependencies": { - "@types/bun": "1.4.4", - "typescript": "7.0.2" - }, - "overrides": { - "@oh-my-pi/omp-stats": "file:./vendor/oh-my-pi-omp-stats-18.20.0.tgz", - "@oh-my-pi/omptype": "file:./vendor/oh-my-pi-omptype-18.20.0.tgz", - "@oh-my-pi/pi-agent-core": "file:./vendor/oh-my-pi-pi-agent-core-18.20.0.tgz", - "@oh-my-pi/pi-ai": "file:./vendor/oh-my-pi-pi-ai-18.20.0.tgz", - "@oh-my-pi/pi-catalog": "file:./vendor/oh-my-pi-pi-catalog-18.20.0.tgz", - "@oh-my-pi/pi-coding-agent": "file:./vendor/oh-my-pi-pi-coding-agent-18.20.0.tgz", - "@oh-my-pi/pi-mnemopi": "file:./vendor/oh-my-pi-pi-mnemopi-18.20.0.tgz", - "@oh-my-pi/pi-natives": "file:./vendor/oh-my-pi-pi-natives-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-darwin-arm64": "file:./vendor/oh-my-pi-pi-natives-darwin-arm64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-darwin-x64": "file:./vendor/oh-my-pi-pi-natives-darwin-x64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-linux-arm64": "file:./vendor/oh-my-pi-pi-natives-linux-arm64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-linux-x64": "file:./vendor/oh-my-pi-pi-natives-linux-x64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-win32-arm64": "file:./vendor/oh-my-pi-pi-natives-win32-arm64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-natives-win32-x64": "file:./vendor/oh-my-pi-pi-natives-win32-x64-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-tui": "file:./vendor/oh-my-pi-pi-tui-18.20.0.tgz", - "@oh-my-pi/pi-utils": "file:./vendor/oh-my-pi-pi-utils-18.20.0-int.202611131515.g11d9dc7.tgz", - "@oh-my-pi/pi-wire": "file:./vendor/oh-my-pi-pi-wire-18.20.0.tgz", - "@oh-my-pi/snapcompact": "file:./vendor/oh-my-pi-snapcompact-18.20.0.tgz" - } -} diff --git a/runner/scripts/build-binary.ts b/runner/scripts/build-binary.ts deleted file mode 100755 index 97ee014a1..000000000 --- a/runner/scripts/build-binary.ts +++ /dev/null @@ -1,179 +0,0 @@ -#!/usr/bin/env bun -/** - * Compiles `src/cli.ts` into a standalone `ompweb-omp--` binary under `release/`, the way - * omp builds its release binaries (`scripts/ci-release-build-binaries.ts` and - * `packages/coding-agent/scripts/compile-binary.ts` upstream). Those helpers assume the monorepo - * (addons under `packages/natives/native`, docs and the stats client generated in place), so this - * script feeds the same `Bun.build` the prebuilt equivalents the npm packages ship. The packages are - * the pinned `ompweb-dev/oh-my-pi` build's tarballs in `vendor/` (`scripts/use-omp-build.ts`). - * - * Usage: `bun scripts/build-binary.ts [target...]`; with no target it builds the host's. - * A cross target needs its addon package installed: `bun install --os= --cpu=`. - */ -import * as fs from "node:fs/promises"; -import * as path from "node:path"; -import { USER_AGENT } from "@oh-my-pi/pi-utils/dirs"; -import { createJsonParsePlugin } from "../node_modules/@oh-my-pi/pi-coding-agent/scripts/json-parse-plugin"; -import { createLegacyPiVirtualModulePlugin } from "../node_modules/@oh-my-pi/pi-coding-agent/scripts/legacy-pi-virtual-module"; - -interface Target { - /** Native addon platform tag (`process.platform`). */ - readonly platform: string; - readonly arch: string; - readonly bun: Bun.Build.CompileTarget; - readonly outName: string; -} - -// The five targets omp's release ships to glibc/macOS/Windows users. -const TARGETS: Record = { - "linux-x64": { platform: "linux", arch: "x64", bun: "bun-linux-x64-baseline", outName: "ompweb-omp-linux-x64" }, - "linux-arm64": { platform: "linux", arch: "arm64", bun: "bun-linux-arm64", outName: "ompweb-omp-linux-arm64" }, - "darwin-x64": { platform: "darwin", arch: "x64", bun: "bun-darwin-x64", outName: "ompweb-omp-darwin-x64" }, - "darwin-arm64": { platform: "darwin", arch: "arm64", bun: "bun-darwin-arm64", outName: "ompweb-omp-darwin-arm64" }, - "windows-x64": { - platform: "win32", - arch: "x64", - bun: "bun-windows-x64-baseline", - outName: "ompweb-omp-windows-x64.exe", - }, -}; - -/** Never bundled: omp installs them on demand at runtime (upstream `COMPILED_EXTERNAL_DEPENDENCIES`). */ -const EXTERNAL = ["@huggingface/transformers", "fastembed", "onnxruntime-node"]; -const ADDON_ZSTD_LEVEL = 19; - -const runnerDir = path.resolve(import.meta.dir, ".."); - -function packageDir(name: string, from: string): string { - // Every package here maps `.` to a file one level below its root (`src/index.ts`, `native/index.js`). - return path.dirname(path.dirname(Bun.resolveSync(name, from))); -} - -const codingAgentDir = packageDir("@oh-my-pi/pi-coding-agent", runnerDir); -const nativesDir = await fs.realpath(path.join(packageDir("@oh-my-pi/pi-natives", runnerDir), "native")); -const statsDir = await fs.realpath(packageDir("@oh-my-pi/omp-stats", codingAgentDir)); - -async function readVersion(manifestPath: string): Promise { - const manifest: unknown = await Bun.file(manifestPath).json(); - if (typeof manifest !== "object" || manifest === null || !("version" in manifest)) { - throw new Error(`${manifestPath} has no version`); - } - return String(manifest.version); -} - -/** - * Replaces pi-natives' null `embedded-addon.js` with a manifest pointing at one zstd frame per addon - * variant, which the loader extracts on first run (upstream `natives/scripts/embed-native.ts`). - */ -async function embeddedAddonFiles(target: Target): Promise> { - const platformTag = `${target.platform}-${target.arch}`; - const leafPackage = `@oh-my-pi/pi-natives-${platformTag}`; - let leafManifest: string; - try { - leafManifest = Bun.resolveSync(`${leafPackage}/package.json`, nativesDir); - } catch (error) { - throw new Error( - `${leafPackage} is not installed; run \`bun install --os=${target.platform} --cpu=${target.arch}\``, - { cause: error }, - ); - } - const version = await readVersion(path.join(nativesDir, "..", "package.json")); - const leafVersion = await readVersion(leafManifest); - if (leafVersion !== version) throw new Error(`${leafPackage}@${leafVersion} does not match pi-natives@${version}`); - - const leafDir = path.dirname(leafManifest); - const candidates = - target.arch === "x64" - ? [ - { variant: "modern", filename: `pi_natives.${platformTag}-modern.node` }, - { variant: "baseline", filename: `pi_natives.${platformTag}-baseline.node` }, - ] - : [{ variant: "default", filename: `pi_natives.${platformTag}.node` }]; - const present = new Set(await fs.readdir(leafDir)); - const available = await Promise.all( - candidates - .filter(candidate => present.has(candidate.filename)) - .map(async candidate => ({ ...candidate, bytes: await Bun.file(path.join(leafDir, candidate.filename)).bytes() })), - ); - if (available.length === 0) throw new Error(`${leafPackage} has no pi_natives addon in ${leafDir}`); - - const frames = await Promise.all(available.map(({ bytes }) => Bun.zstdCompress(bytes, { level: ADDON_ZSTD_LEVEL }))); - const imports = available.map( - ({ filename }, index) => `import zstdPath${index} from ${JSON.stringify(`./${filename}.zst`)} with { type: "file" };`, - ); - const files = available.map( - ({ variant, filename, bytes }, index) => - `{ variant: ${JSON.stringify(variant)}, filename: ${JSON.stringify(filename)}, size: ${bytes.length}, zstdPath: zstdPath${index} }`, - ); - const overrides: Record = { - [path.join(nativesDir, "embedded-addon.js")]: `${imports.join("\n")} -export const embeddedAddon = { - platformTag: ${JSON.stringify(platformTag)}, - version: ${JSON.stringify(version)}, - files: [${files.join(", ")}], -}; -`, - }; - available.forEach(({ filename }, index) => { - overrides[path.join(nativesDir, `${filename}.zst`)] = frames[index]; - }); - return overrides; -} - -/** The `omp stats` dashboard as the base64 gzip tar omp-stats serves when compiled (upstream `gen:stats`). */ -async function embeddedStatsClient(): Promise> { - const clientDir = path.join(statsDir, "dist", "client"); - const entries: Record = {}; - for await (const file of new Bun.Glob("**/*").scan({ cwd: clientDir, onlyFiles: true })) { - entries[file.split(path.sep).join("/")] = await Bun.file(path.join(clientDir, file)).bytes(); - } - if (!entries["index.html"]) throw new Error(`omp-stats ships no dashboard in ${clientDir}`); - const archive = Bun.gzipSync(await new Bun.Archive(entries).bytes(), { level: 9 }); - return { [path.join(statsDir, "src", "embedded-client.generated.txt")]: Buffer.from(archive).toString("base64") }; -} - -async function build(id: string, target: Target, statsClient: Record, docsEmbed: string): Promise { - const outfile = path.join(runnerDir, "release", target.outName); - console.log(`Building ${path.relative(runnerDir, outfile)}...`); - const output = await Bun.build({ - entrypoints: [path.join(runnerDir, "src", "cli.ts")], - root: runnerDir, - external: EXTERNAL, - files: { ...(await embeddedAddonFiles(target)), ...statsClient }, - define: { - "process.env.PI_COMPILED": JSON.stringify("true"), - "process.env.PI_DOCS_EMBED": JSON.stringify(docsEmbed), - }, - format: "esm", - bytecode: true, - splitting: true, - // Upstream's `chunks/[name]-[hash]` collides here: type-only modules from different packages - // (`pi-tui/src/render/types.ts`, `pi-coding-agent/src/dap/types.ts`) emit identical chunks. - naming: { chunk: "chunks/[dir]/[name]-[hash].[ext]" }, - minify: { identifiers: true, keepNames: true }, - plugins: [createJsonParsePlugin(), await createLegacyPiVirtualModulePlugin()], - compile: { - execArgv: [`--user-agent=${USER_AGENT}`], - target: target.bun, - outfile, - autoloadBunfig: false, - autoloadDotenv: false, - autoloadTsconfig: false, - autoloadPackageJson: false, - }, - throw: false, - }); - if (!output.success) throw new Error(`${id}: bundle failed:\n${output.logs.map(log => log.message).join("\n")}`); - // shortcut: darwin binaries carry only Bun's built-in ad-hoc signature (no hardened runtime, no - // entitlements); add omp's Developer ID signing step before distributing outside our own machines. -} - -const hostId = `${process.platform === "win32" ? "windows" : process.platform}-${process.arch}`; -const ids = process.argv.length > 2 ? process.argv.slice(2) : [hostId]; -for (const id of ids) { - if (!TARGETS[id]) throw new Error(`unknown target ${id}; expected one of ${Object.keys(TARGETS).join(", ")}`); -} -// npm packs the docs corpus the compiled binary would otherwise embed from the monorepo's `docs/`. -const docsEmbed = (await Bun.file(path.join(codingAgentDir, "dist", "docs-index.generated.txt")).text()).trimEnd(); -const statsClient = await embeddedStatsClient(); -for (const id of ids) await build(id, TARGETS[id], statsClient, docsEmbed); diff --git a/runner/scripts/host-probe.ts b/runner/scripts/host-probe.ts deleted file mode 100644 index 167fd33ab..000000000 --- a/runner/scripts/host-probe.ts +++ /dev/null @@ -1,51 +0,0 @@ -// Channel probe: stands in for `ompweb host` on the host socket and prints what omp sends. -// -// bun scripts/host-probe.ts -// -// stdin lines go to every connected omp: `get_state`, `set_model `, -// `prompt `, `abort`, `answer `. -import * as fs from "node:fs"; -import * as path from "node:path"; -import { hostSocketPath } from "../src/channel"; - -const socketPath = hostSocketPath(); -fs.mkdirSync(path.dirname(socketPath), { recursive: true, mode: 0o700 }); -fs.rmSync(socketPath, { force: true }); - -const clients = new Set(); -let buffered = ""; -Bun.listen({ - unix: socketPath, - socket: { - open: socket => void clients.add(socket), - close: socket => void clients.delete(socket), - data: (_socket, data) => { - buffered += data.toString(); - const lines = buffered.split("\n"); - buffered = lines.pop() ?? ""; - for (const line of lines) { - const message = JSON.parse(line); - // Streaming deltas would flood the probe; their message_end carries the result. - if (message.t === "event" && message.event.type === "message_update") continue; - console.log(`omp> ${line.length > 300 ? `${line.slice(0, 300)}…` : line}`); - } - }, - }, -}); -fs.chmodSync(socketPath, 0o600); -console.log(`listening on ${socketPath}`); - -let id = 0; -for await (const line of console) { - const [op = "", ...rest] = line.trim().split(" "); - const text = rest.join(" "); - const message = - op === "answer" - ? { t: "ui_response", id: Number(rest[0]), value: JSON.parse(rest.slice(1).join(" ")) } - : op === "set_model" - ? { t: "cmd", id: ++id, op, args: { provider: rest[0], modelId: rest[1] } } - : op === "prompt" - ? { t: "cmd", id: ++id, op, args: { text } } - : { t: "cmd", id: ++id, op }; - for (const client of clients) client.write(`${JSON.stringify(message)}\n`); -} diff --git a/runner/scripts/use-omp-build.ts b/runner/scripts/use-omp-build.ts deleted file mode 100755 index a42c977af..000000000 --- a/runner/scripts/use-omp-build.ts +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env bun -/** - * Puts the omp npm packages the runner compiles against into `vendor/`: the `omp-packages-.zip` asset of - * one `ompweb-dev/oh-my-pi` integration release, which `omp-build.json` pins. - * - * `bun scripts/use-omp-build.ts`: fetch the pinned release's packages (then `bun install --frozen-lockfile`). - * `bun scripts/use-omp-build.ts `: re-pin to that release: fetch it, point `omp-build.json`, the `@oh-my-pi/*` - * dependencies and an override for every package and native leaf at it, and `bun install` (updates `bun.lock`). - * `OMP_BUILD_RELEASES` replaces `https://github.com` as the download host, e.g. a local mirror for testing. - */ -import * as fs from "node:fs/promises"; -import * as path from "node:path"; - -interface Pin { - repo: string; - tag: string; -} - -const runnerDir = path.resolve(import.meta.dir, ".."); -const vendorDir = path.join(runnerDir, "vendor"); -const pinPath = path.join(runnerDir, "omp-build.json"); -const manifestPath = path.join(runnerDir, "package.json"); -const TAG = /^v\d+\.\d+\.\d+-int\.\d{12}\.g[0-9a-f]{7}$/; -// Without an override per leaf, bun installs upstream's npm leaf instead and the runner embeds upstream's addon. -const LEAVES = ["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64", "win32-x64", "win32-arm64"].map( - target => `@oh-my-pi/pi-natives-${target}`, -); - -function run(cmd: string[]): void { - const { exitCode } = Bun.spawnSync(cmd, { cwd: runnerDir, stdio: ["ignore", "inherit", "inherit"] }); - if (exitCode !== 0) throw new Error(`${cmd.join(" ")} exited with ${exitCode}`); -} - -/** Replaces `vendor/` with the release's zip contents once they hold every `required` package; returns its - * `index.json` (npm name → tarball file). A failed fetch or check leaves `vendor/` as it was. */ -async function fetchBuild({ repo, tag }: Pin, required: string[] = []): Promise> { - if (!TAG.test(tag)) throw new Error(`${tag} is not an integration release tag (v-int..g)`); - const url = `${process.env.OMP_BUILD_RELEASES ?? "https://github.com"}/${repo}/releases/download/${tag}/omp-packages-${tag.slice(1)}.zip`; - // One retry: a dropped connection should not fail a release build. - let response = await fetch(url).catch(() => undefined); - if (!response?.ok) response = await fetch(url); - if (!response.ok) throw new Error(`${url}: HTTP ${response.status}`); - const staging = `${vendorDir}.new`; - await fs.rm(staging, { recursive: true, force: true }); - await fs.mkdir(staging); - const zip = path.join(staging, "omp-packages.zip"); - await Bun.write(zip, response); - // PATH can select Git Bash's GNU tar, which neither reads zip nor treats drive letters as local paths. - if (process.platform === "win32") { - const systemRoot = process.env.SystemRoot; - if (!systemRoot || !path.isAbsolute(systemRoot)) throw new Error("SystemRoot must identify the Windows directory"); - run([path.join(systemRoot, "System32", "tar.exe"), "-xf", zip, "-C", staging]); - } else { - run(["unzip", "-q", zip, "-d", staging]); - } - await fs.rm(zip); - const index: Record = await Bun.file(path.join(staging, "index.json")).json(); - for (const [name, file] of Object.entries(index)) { - if (!/^[\w.-]+\.tgz$/.test(file)) throw new Error(`index.json: bad file ${file} for ${name}`); - } - const missing = required.filter(name => !index[name]); - if (missing.length > 0) throw new Error(`${tag} has no package for ${missing.join(", ")}`); - await fs.rm(vendorDir, { recursive: true, force: true }); - await fs.rename(staging, vendorDir); - console.log(`omp packages: ${repo} ${tag} (${Object.keys(index).length} tarballs)`); - return index; -} - -const tag = process.argv[2]; -if (!tag) { - const pin: Pin = await Bun.file(pinPath) - .json() - .catch(error => { - throw new Error(`${pinPath} is unreadable; pin a release with \`bun scripts/use-omp-build.ts \``, { - cause: error, - }); - }); - await fetchBuild(pin); -} else { - const pin: Pin = { repo: "ompweb-dev/oh-my-pi", tag }; - const manifest = await Bun.file(manifestPath).json(); - const direct = Object.keys(manifest.dependencies).filter(name => name.startsWith("@oh-my-pi/")); - const index = await fetchBuild(pin, [...direct, ...LEAVES]); - const file = (name: string): string => `file:./vendor/${index[name]}`; - for (const name of direct) manifest.dependencies[name] = file(name); - // Replaced, not merged: a package the new release dropped must not keep pointing at a tarball that is gone. - manifest.overrides = Object.fromEntries(Object.keys(index).map(name => [name, file(name)])); - delete manifest.patchedDependencies; - await Bun.write(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`); - await Bun.write(pinPath, `${JSON.stringify(pin, null, 2)}\n`); - // --force: unstamped tarballs keep their file name across builds (`…pi-coding-agent-18.20.0.tgz`), and without it - // bun keeps the old build's integrity for them, so the next `--frozen-lockfile` install fails its check. - run([process.execPath, "install", "--force"]); - // Any `@oh-my-pi/*` resolved from npm instead of vendor/ is a package the release lacks. - const fromNpm = (await Bun.file(path.join(runnerDir, "bun.lock")).text()).match(/\["@oh-my-pi\/[^"]+@(?!\.\/vendor\/)[^"]*"/g); - if (fromNpm) throw new Error(`bun.lock resolves these from npm, not ${tag}: ${fromNpm.map(entry => entry.slice(2, -1)).join(", ")}`); -} diff --git a/runner/src/channel.ts b/runner/src/channel.ts deleted file mode 100644 index b37a02183..000000000 --- a/runner/src/channel.ts +++ /dev/null @@ -1,501 +0,0 @@ -/** - * The channel between one omp process and the local `ompweb host`: NDJSON lines over either - * transport, with the same frames on both: - * - the host's Unix socket, for an omp the user started in a terminal (`HostChannel.connect`); - * only this user's processes reach it, and the channel redials when the host restarts; - * - stdin/stdout, for a headless omp the host spawned (`HostChannel.stdio`, see `headless.ts`). - * - * omp → host {t:"hello", proto, pid, sessionId, sessionFile, cwd} on every (re)connect - * host → omp {t:"cmd", id, op, args?} - * omp → host {t:"res", id, ok:true, data?} | {t:"res", id, ok:false, error, code?} - * omp → host {t:"ui_request", id, method, args} a dialog opened; `args` positional per ExtensionUIContext, - * dialog options without `signal`. With a TUI it shows there too. - * host → omp {t:"ui_response", id, value} the method's return value; first answer wins - * omp → host {t:"ui_end", id} omp closed the dialog itself (TUI answer, abort, timeout) - * host → omp {t:"refused", reason?} the host will not take this omp (protocol, session live - * elsewhere); the socket closes and omp stops redialling - * omp → host {t:"ui", method, ...fields} a presentation call, fire-and-forget: - * notify{message, notifyType}, setStatus{statusKey, statusText}, - * setWidget{widgetKey, widgetLines, widgetPlacement}, - * setTitle{title}, set_editor_text{text}, setWorkingMessage{message}, - * custom{id, lines, closed?} (a custom component's rendered lines) - * host → omp {t:"ui_input", id, data} keyboard input for custom component `id` - * - * Other frames come from the command groups (`commands/*.ts`), `events.ts` and `headless.ts`. - * The transcript before `hello` is the session file; the host reads it. - */ -import * as fs from "node:fs"; -import * as os from "node:os"; -import * as path from "node:path"; -import type { - ExtensionUIContext, - ExtensionUIDialogOptions, - ExtensionUiComponent, - KeybindingsManager, -} from "@oh-my-pi/pi-coding-agent/extensibility/extensions/types"; -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import type { EventBus } from "@oh-my-pi/pi-coding-agent/utils/event-bus"; -import type { TUI } from "@oh-my-pi/pi-tui"; -import type { Theme } from "@oh-my-pi/pi-tui/theme"; -import { isRecord, logger } from "@oh-my-pi/pi-utils"; -import { commands, frames } from "./commands"; -import { watchSubagents } from "./commands/agents"; -import { watchSessionIdentity } from "./commands/session"; -import { closeToolsForHost } from "./commands/tools"; -import { type CommandContext, CommandError } from "./commands/types"; -import { subscribeHostEvents } from "./events"; - -export const CHANNEL_PROTO = 1; - -const RECONNECT_MS = 2_000; -/** How long TUI startup waits for the first connect; a slower host is still adopted when it answers. */ -const CONNECT_WAIT_MS = 1_000; - -/** One parsed host line; `#receive` narrows it by `t`. */ -export type HostMessage = { t: string } & Record; - -/** Argument index of `ExtensionUIDialogOptions` in each dialog the host can answer. */ -const DIALOG_OPTIONS_INDEX = { - select: 2, - confirm: 2, - input: 2, - editor: 2, - askDialog: 1, - formDialog: 1, - approve: 1, -} as const; - -export type DialogMethod = keyof typeof DIALOG_OPTIONS_INDEX; - -/** The presentation calls the host mirrors. */ -type Presenter = Pick< - ExtensionUIContext, - "notify" | "setStatus" | "setWidget" | "setTitle" | "setEditorText" | "setWorkingMessage" ->; - -export type CustomFactory = ( - tui: TUI, - theme: Theme, - keybindings: KeybindingsManager, - done: (result: T) => void, -) => ExtensionUiComponent | Promise; - -interface PendingDialog { - method: DialogMethod; - args: unknown[]; - answer(value: unknown): void; - /** The host link closed with the dialog open. */ - lost(): void; -} - -/** `$XDG_RUNTIME_DIR/ompweb/host.sock`, or the per-user temp dir where there is no runtime dir (macOS). */ -export function hostSocketPath(): string { - if (process.env.OMPWEB_HOST_SOCKET) return process.env.OMPWEB_HOST_SOCKET; - return path.join(process.env.XDG_RUNTIME_DIR || os.tmpdir(), "ompweb", "host.sock"); -} - -/** Whether only this user can create the socket in `dir`; anyone else could stand in for the host. */ -function isPrivateDir(dir: string): boolean { - const uid = process.getuid?.(); - if (uid === undefined) return true; - try { - const stat = fs.statSync(dir); - return stat.uid === uid && (stat.mode & 0o022) === 0; - } catch { - return false; - } -} - -function isHostMessage(value: unknown): value is HostMessage { - return isRecord(value) && typeof value.t === "string"; -} - -function dialogOptionsOf(method: DialogMethod, args: unknown[]): ExtensionUIDialogOptions | undefined { - const options = args[DIALOG_OPTIONS_INDEX[method]]; - return isRecord(options) ? options : undefined; -} - -/** `args` as sent to the host: dialog options without the signal. */ -function wireArgs(method: DialogMethod, args: unknown[]): unknown[] { - const options = dialogOptionsOf(method, args); - if (!options) return args; - const wire = [...args]; - wire[DIALOG_OPTIONS_INDEX[method]] = { ...options, signal: undefined }; - return wire; -} - -export class HostChannel { - readonly #session: AgentSession; - readonly #subagentEventBus: EventBus | undefined; - /** Read once, so a channel never follows a later change of `OMPWEB_HOST_SOCKET`. */ - readonly #socketPath = hostSocketPath(); - /** The host refused this omp: no more dials. */ - #refused = false; - #write: ((line: string) => void) | undefined; - #unwatch: (() => void)[] = []; - /** Frames other than dialog answers wait here until `release()` (headless startup). */ - #held: HostMessage[] | undefined; - #warnedUnsafeDir = false; - #nextUiId = 1; - readonly #pendingUi = new Map(); - readonly #customInput = new Map void>(); - - readonly presenter: Presenter = { - notify: (message, type) => this.#send({ t: "ui", method: "notify", message, notifyType: type }), - setStatus: (key, text) => this.#send({ t: "ui", method: "setStatus", statusKey: key, statusText: text }), - setWidget: (key, content, options) => { - // Component factories need a terminal, so only plain lines reach the host. - if (content !== undefined && !Array.isArray(content)) return; - this.#send({ t: "ui", method: "setWidget", widgetKey: key, widgetLines: content, widgetPlacement: options?.placement }); - }, - setTitle: title => this.#send({ t: "ui", method: "setTitle", title }), - setEditorText: text => this.#send({ t: "ui", method: "set_editor_text", text }), - setWorkingMessage: message => this.#send({ t: "ui", method: "setWorkingMessage", message }), - }; - - private constructor(session: AgentSession, subagentEventBus: EventBus | undefined) { - this.#session = session; - this.#subagentEventBus = subagentEventBus; - } - - /** - * Connect `session` to the host socket, and keep redialling while omp runs: no host listening - * means a plain omp run whose dialogs are the TUI's alone, until a host starts. - */ - static async connect(session: AgentSession, subagentEventBus?: EventBus): Promise { - const channel = new HostChannel(session, subagentEventBus); - // shortcut: the Windows host has no named pipe yet; dial there once it does. - if (process.platform === "win32") return channel; - const { promise: waited, resolve } = Promise.withResolvers(); - setTimeout(resolve, CONNECT_WAIT_MS).unref(); - await Promise.race([channel.#dial(), waited]); - return channel; - } - - /** Serve the host on this process's stdio; frames other than dialog answers wait for `release()`. */ - static stdio(session: AgentSession, subagentEventBus: EventBus | undefined, write: (line: string) => void): HostChannel { - const channel = new HostChannel(session, subagentEventBus); - channel.#held = []; - channel.#open(write); - return channel; - } - - /** Read host lines from `input` until it ends, then close the channel. */ - async read(input: ReadableStreamDefaultReader): Promise { - const feed = this.#lineReader(); - const decoder = new TextDecoder(); - try { - for (let chunk = await input.read(); !chunk.done; chunk = await input.read()) { - feed(decoder.decode(chunk.value, { stream: true })); - } - } finally { - this.#close(); - } - } - - /** Dispatch the frames held since `stdio()`, and every later frame as it arrives. */ - release(): void { - const held = this.#held ?? []; - this.#held = undefined; - for (const message of held) this.#receive(message); - } - - async #dial(): Promise { - const socketPath = this.#socketPath; - if (!isPrivateDir(path.dirname(socketPath))) { - if (!this.#warnedUnsafeDir) { - this.#warnedUnsafeDir = true; - logger.warn("ompweb channel: socket directory is not private to this user; not connecting", { socketPath }); - } - this.#redial(); - return; - } - const feed = this.#lineReader(); - const decoder = new TextDecoder(); - const encoder = new TextEncoder(); - const queue: Uint8Array[] = []; - let opened = false; - // `write` takes what the kernel buffer holds; the rest waits for `drain`. - const flush = (socket: Bun.Socket) => { - while (queue.length > 0) { - const chunk = queue[0]; - const written = Math.max(socket.write(chunk), 0); - if (written < chunk.byteLength) { - queue[0] = chunk.subarray(written); - return; - } - queue.shift(); - } - }; - const socket = await Bun.connect({ - unix: socketPath, - socket: { - data: (_socket, data) => feed(decoder.decode(data, { stream: true })), - drain: flush, - close: () => { - if (!opened) return; - opened = false; - this.#close(); - this.#redial(); - }, - }, - }).catch(() => undefined); - if (!socket) { - this.#redial(); - return; - } - // The channel must not keep a finished omp alive. - socket.unref(); - opened = true; - this.#open(line => { - queue.push(encoder.encode(line)); - if (queue.length === 1) flush(socket); - }); - } - - #redial(): void { - if (!this.#refused) setTimeout(() => void this.#dial(), RECONNECT_MS).unref(); - } - - #open(write: (line: string) => void): void { - this.#write = write; - const session = this.#session; - const send: CommandContext["send"] = frame => this.#send(frame); - this.#send({ - t: "hello", - proto: CHANNEL_PROTO, - pid: process.pid, - sessionId: session.sessionId, - sessionFile: session.sessionFile, - cwd: session.sessionManager.getCwd(), - }); - // Per connection: a redialled host starts from a fresh subscription, not mid-delta. - this.#unwatch = [watchSessionIdentity(session, send), subscribeHostEvents(session, send)]; - if (this.#subagentEventBus) this.#unwatch.push(watchSubagents({ session, send }, this.#subagentEventBus)); - // Dialogs the TUI still shows open on the new host too. - for (const [id, { method, args }] of this.#pendingUi) this.#send({ t: "ui_request", id, method, args }); - } - - #close(): void { - if (!this.#write) return; - this.#write = undefined; - for (const unwatch of this.#unwatch.splice(0)) unwatch(); - for (const pending of [...this.#pendingUi.values()]) pending.lost(); - closeToolsForHost(this.#session).catch(err => - logger.warn("ompweb channel: closing host tools failed", { error: String(err) }), - ); - } - - #lineReader(): (text: string) => void { - let buffered = ""; - return text => { - buffered += text; - let newline = buffered.indexOf("\n"); - while (newline >= 0) { - const line = buffered.slice(0, newline); - buffered = buffered.slice(newline + 1); - newline = buffered.indexOf("\n"); - if (line.trim()) this.#receiveLine(line); - } - }; - } - - #receiveLine(line: string): void { - let message: unknown; - try { - message = JSON.parse(line); - } catch (err) { - logger.warn("ompweb channel: unparsable host line", { error: String(err) }); - return; - } - if (!isHostMessage(message)) { - logger.warn("ompweb channel: host line without a frame type"); - return; - } - this.#receive(message); - } - - #send(message: object): void { - this.#write?.(`${JSON.stringify(message)}\n`); - } - - #receive(message: HostMessage): void { - if (message.t === "refused") { - if (!this.#refused) logger.warn("ompweb channel: the host refused this omp; not redialling", { reason: message.reason }); - this.#refused = true; - return; - } - if (message.t === "ui_response") { - if (typeof message.id === "number") this.#pendingUi.get(message.id)?.answer(message.value); - return; - } - if (message.t === "ui_input") { - if (typeof message.id === "number" && typeof message.data === "string") this.#customInput.get(message.id)?.(message.data); - return; - } - if (this.#held) { - this.#held.push(message); - return; - } - const session = this.#session; - const send: CommandContext["send"] = frame => this.#send(frame); - if (message.t !== "cmd") { - const handler = Object.hasOwn(frames, message.t) ? frames[message.t] : undefined; - try { - handler?.({ session, send }, message); - } catch (err) { - logger.warn("ompweb channel: bad host frame", { t: message.t, error: String(err) }); - } - return; - } - const { id, op, args } = message; - if (typeof id !== "number" || typeof op !== "string") return; - if (args !== undefined && !isRecord(args)) { - this.#send({ t: "res", id, ok: false, error: `${op}: args must be an object` }); - return; - } - const handler = Object.hasOwn(commands, op) ? commands[op] : undefined; - Promise.try(() => { - if (!handler) throw new Error(`unknown command: ${op}`); - return handler({ session, send, id }, args ?? {}); - }).then( - data => this.#send({ t: "res", id, ok: true, data }), - (err: unknown) => - this.#send({ - t: "res", - id, - ok: false, - error: err instanceof Error ? err.message : String(err), - ...(err instanceof CommandError ? { code: err.code } : {}), - }), - ); - } - - /** - * Open a dialog on the host alone (headless omp). An abort or the caller's timeout closes it - * with `fallback`, as does losing the host. - */ - dialog(method: DialogMethod, args: unknown[], fallback: (timedOut: boolean) => T): Promise { - const options = dialogOptionsOf(method, args); - if (options?.signal?.aborted || !this.#write) return Promise.resolve(fallback(false)); - const id = this.#nextUiId++; - const { promise, resolve } = Promise.withResolvers(); - let timer: Timer | undefined; - const settle = (value: T, closeOnHost: boolean) => { - clearTimeout(timer); - options?.signal?.removeEventListener("abort", onAbort); - this.#pendingUi.delete(id); - if (closeOnHost) this.#send({ t: "ui_end", id }); - resolve(value); - }; - const onAbort = () => settle(fallback(false), true); - options?.signal?.addEventListener("abort", onAbort, { once: true }); - if (options?.timeout !== undefined) { - timer = setTimeout(() => { - options.onTimeout?.(); - settle(fallback(true), true); - }, options.timeout); - } - this.#pendingUi.set(id, { - method, - args: wireArgs(method, args), - answer: value => settle(value as T, false), - lost: () => settle(fallback(false), false), - }); - this.#send({ t: "ui_request", id, method, args: wireArgs(method, args) }); - return promise; - } - - /** - * Race a dialog between the local UI and the host: the first answer wins and the other side's - * dialog closes. Without a host the local UI answers alone, and a host that connects meanwhile gets it. - */ - #race(method: DialogMethod, args: unknown[], local: (args: unknown[]) => Promise): Promise { - const index = DIALOG_OPTIONS_INDEX[method]; - const callerOptions = dialogOptionsOf(method, args); - const localAbort = new AbortController(); - const signal = callerOptions?.signal ? AbortSignal.any([callerOptions.signal, localAbort.signal]) : localAbort.signal; - const localArgs = [...args]; - localArgs[index] = { ...callerOptions, signal }; - - const id = this.#nextUiId++; - const { promise: remote, resolve: answer } = Promise.withResolvers(); - const wire = wireArgs(method, args); - // A lost host leaves the local dialog to answer alone. - this.#pendingUi.set(id, { method, args: wire, answer: value => answer(value as T), lost: () => {} }); - this.#send({ t: "ui_request", id, method, args: wire }); - - const localAnswer = local(localArgs).then(value => ({ from: "local" as const, value })); - const remoteAnswer = remote.then(value => ({ from: "host" as const, value })); - return Promise.race([localAnswer, remoteAnswer]) - .finally(() => this.#pendingUi.delete(id)) - .then( - winner => { - if (winner.from === "host") localAbort.abort(); - else this.#send({ t: "ui_end", id }); - return winner.value; - }, - (err: unknown) => { - this.#send({ t: "ui_end", id }); - throw err; - }, - ); - } - - /** - * Run a custom component through `show` (the TUI's, or the headless stand-in), mirroring its - * rendered lines to the host and the host's keys to it. - */ - custom(factory: CustomFactory, show: (factory: CustomFactory) => Promise): Promise { - const id = this.#nextUiId++; - let last: string | undefined; - return show(async (tui, theme, keybindings, done) => { - const component = await factory(tui, theme, keybindings, done); - const render = component.render.bind(component); - component.render = width => { - const lines = render(width); - const key = lines.join("\n"); - if (key !== last) { - last = key; - this.#send({ t: "ui", method: "custom", id, lines }); - } - return lines; - }; - this.#customInput.set(id, data => { - component.handleInput?.(data); - tui.requestRender(); - }); - return component; - }).finally(() => { - this.#customInput.delete(id); - if (last !== undefined) this.#send({ t: "ui", method: "custom", id, lines: [], closed: true }); - }); - } - - /** The local UI with its dialogs raced against the host and its presentation mirrored there. */ - wrapUi(ui: ExtensionUIContext): ExtensionUIContext { - return new Proxy(ui, { - get: (target, prop, receiver) => { - const value: unknown = Reflect.get(target, prop, receiver); - if (typeof prop !== "string" || typeof value !== "function") return value; - if (prop in DIALOG_OPTIONS_INDEX) { - const method = prop as DialogMethod; - return (...args: unknown[]) => this.#race(method, args, localArgs => value.apply(target, localArgs)); - } - if (prop === "custom") { - return (factory: CustomFactory, options?: unknown) => - this.custom(factory, mirrored => value.call(target, mirrored, options)); - } - if (Object.hasOwn(this.presenter, prop)) { - const mirror: unknown = Reflect.get(this.presenter, prop); - return (...args: unknown[]) => { - const result: unknown = value.apply(target, args); - if (typeof mirror === "function") mirror(...args); - return result; - }; - } - return value; - }, - }); - } -} diff --git a/runner/src/cli.ts b/runner/src/cli.ts deleted file mode 100755 index 317ab0178..000000000 --- a/runner/src/cli.ts +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bun -/** - * ompweb's omp: upstream omp's CLI with the root launch command replaced (see `launch.ts`). - * - * Upstream's `cli.ts` only acts as the process entry when it is `Bun.main` or the build defines - * `PI_COMPILED`. From source, this module stands in: it declares itself the worker host (workers and - * worker subprocesses re-enter `Bun.main` with a hidden argv selector, which `runCli` dispatches), - * installs the SQLite lock shim before any store opens, and installs the postmortem handlers. In the - * compiled binary upstream's module already does all that and runs the CLI (its first `await` comes - * before it loads the commands, so it sees our launch), and a second run would race it for stdin. - * Worker threads re-enter this module too; importing upstream's `cli.ts` already dispatches them - * (`!Bun.isMainThread`), so the main-thread branch is ours. - */ -import { runCli } from "@oh-my-pi/pi-coding-agent/cli"; -import { commands } from "@oh-my-pi/pi-coding-agent/cli-commands"; -import { installSqliteLockShim } from "@oh-my-pi/pi-natives/sqlite-locks"; -import * as postmortem from "@oh-my-pi/pi-utils/postmortem"; -import { declareWorkerHostEntry } from "@oh-my-pi/pi-utils/worker-host"; -import Launch from "./launch"; - -if (Bun.isMainThread) { - const launch = commands.find(entry => entry.name === "launch"); - if (!launch) throw new Error("omp has no launch command to replace"); - launch.load = async () => Launch; - - if (process.env.PI_COMPILED !== "true") { - declareWorkerHostEntry(); - installSqliteLockShim(); - postmortem.installProcessHandlers(); - postmortem.registerStdioDisconnectHandling(); - runCli(process.argv.slice(2)).catch(error => postmortem.fatal(error)); - } -} diff --git a/runner/src/commands/agents.ts b/runner/src/commands/agents.ts deleted file mode 100644 index 1e49bdd5e..000000000 --- a/runner/src/commands/agents.ts +++ /dev/null @@ -1,438 +0,0 @@ -/** - * Subagents and provider login. - * - * Unsolicited frames omp sends: - * omp → host {t:"subagent_lifecycle", payload} a subagent started or settled (subscription "progress" or "events") - * omp → host {t:"subagent_progress", payload} a subagent's progress (subscription "progress" or "events") - * omp → host {t:"subagent_event", payload} one subagent session event (subscription "events") - * omp → host {t:"login_open_url", url, launchUrl?, instructions?} open the provider's sign-in page - * omp → host {t:"login_progress", message} a login status line - * omp → host {t:"login_input", id, prompt, placeholder?, secret} ask the user for a value (secret: mask it) - * omp → host {t:"login_input_end", id} close that prompt: it timed out or the login ended first - * - * Host frames: - * host → omp {t:"login_input_response", id, value} value: string, or null/absent for cancelled - */ -import * as fs from "node:fs/promises"; -import { getOAuthProviders } from "@oh-my-pi/pi-ai/oauth"; -import { AgentLifecycleManager } from "@oh-my-pi/pi-coding-agent/registry/agent-lifecycle"; -import { AgentRegistry } from "@oh-my-pi/pi-coding-agent/registry/agent-registry"; -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import type { AgentSessionHandle } from "@oh-my-pi/pi-coding-agent/session/agent-session-handle"; -import type { FileEntry, SessionMessageEntry } from "@oh-my-pi/pi-coding-agent/session/session-entries"; -import { parseSessionEntries } from "@oh-my-pi/pi-coding-agent/session/session-loader"; -import { USER_INTERRUPT_LABEL } from "@oh-my-pi/pi-coding-agent/session/messages"; -import { listLogoutAccounts, logoutCredential } from "@oh-my-pi/pi-coding-agent/slash-commands/helpers/logout"; -import { - type SubagentEventPayload, - type SubagentLifecyclePayload, - type SubagentProgressPayload, - TASK_SUBAGENT_EVENT_CHANNEL, - TASK_SUBAGENT_LIFECYCLE_CHANNEL, - TASK_SUBAGENT_PROGRESS_CHANNEL, -} from "@oh-my-pi/pi-coding-agent/task"; -import type { EventBus } from "@oh-my-pi/pi-coding-agent/utils/event-bus"; -import { isEnoent, logger } from "@oh-my-pi/pi-utils"; -import type { CommandContext, CommandTable, FrameTable } from "./types"; - -const LOGIN_INPUT_TIMEOUT_MS = 600_000; -/** Settled subagents whose transcript stays readable, and ids remembered from a previous session. */ -const MAX_RETAINED_SUBAGENTS = 256; - -type SubscriptionLevel = "off" | "progress" | "events"; -type Progress = SubagentProgressPayload["progress"]; - -/** A `get_subagents` row. */ -interface SubagentSnapshot { - id: string; - index: number; - agent: string; - agentSource: Progress["agentSource"]; - description?: string; - status: Progress["status"]; - task?: string; - assignment?: string; - sessionFile?: string; - lastUpdate: number; - progress?: Progress; - parentToolCallId?: string; -} - -function prune(keys: Set | Map): void { - for (const key of keys.keys()) { - if (keys.size <= MAX_RETAINED_SUBAGENTS) return; - keys.delete(key); - } -} - -/** Agent ids repeat across parent sessions, so a frame only updates the row of the same spawn. */ -function sameOwner(payload: SubagentLifecyclePayload | SubagentProgressPayload, row: SubagentSnapshot): boolean { - if (payload.parentToolCallId !== undefined && row.parentToolCallId !== undefined) { - return payload.parentToolCallId === row.parentToolCallId; - } - if (payload.sessionFile !== undefined && row.sessionFile !== undefined) return payload.sessionFile === row.sessionFile; - return true; -} - -/** The running subagents of one session, as the task tool reports them on the event bus. */ -class Roster { - readonly #running = new Map(); - /** Transcript file per subagent id, kept after it settles. */ - readonly #transcripts = new Map(); - /** Ids from a previous session: their late events must not reach the host. */ - readonly #stale = new Set(); - readonly #unsubscribe: Array<() => void>; - readonly #send: CommandContext["send"]; - level: SubscriptionLevel = "off"; - /** A new/switched/forked session starts an empty roster. */ - sessionId: string; - - constructor(ctx: CommandContext, bus: EventBus) { - this.#send = ctx.send; - this.sessionId = ctx.session.sessionId; - this.#unsubscribe = [ - bus.on(TASK_SUBAGENT_LIFECYCLE_CHANNEL, data => this.#lifecycle(data as SubagentLifecyclePayload)), - bus.on(TASK_SUBAGENT_PROGRESS_CHANNEL, data => this.#progress(data as SubagentProgressPayload)), - bus.on(TASK_SUBAGENT_EVENT_CHANNEL, data => this.#event(data as SubagentEventPayload)), - ]; - } - - dispose(): void { - for (const unsubscribe of this.#unsubscribe) unsubscribe(); - } - - clear(): void { - for (const id of [...this.#running.keys(), ...this.#transcripts.keys()]) { - this.#stale.delete(id); - this.#stale.add(id); - } - prune(this.#stale); - this.#running.clear(); - this.#transcripts.clear(); - } - - list(): SubagentSnapshot[] { - return [...this.#running.values()].sort((a, b) => a.index - b.index || a.id.localeCompare(b.id)); - } - - transcriptFile(subagentId: string | undefined, sessionFile: string | undefined): string { - if (subagentId) { - const file = this.#running.get(subagentId)?.sessionFile ?? this.#transcripts.get(subagentId); - if (!file) throw new Error(`Unknown subagent or session file unavailable: ${subagentId}`); - return file; - } - if (!sessionFile) throw new Error("get_subagent_messages requires subagentId or sessionFile"); - // Only transcripts of this session's subagents: the host must not read arbitrary files. - if ([...this.#transcripts.values()].includes(sessionFile) || this.list().some(row => row.sessionFile === sessionFile)) { - return sessionFile; - } - throw new Error("Unknown subagent session file"); - } - - /** - * The live registry ref of a running subagent in this roster, or undefined when the host must not reach it - * (unknown, finished, accepted, parked, aborted, or another session's agent). The process-global registry keeps - * the latest ref per id, so it must carry this roster's transcript file; and it goes `idle` once the parent - * accepts the result, before the terminal lifecycle frame prunes the roster. - */ - owned(subagentId: string) { - const row = this.#running.get(subagentId); - // Progress can report a terminal status just before the terminal lifecycle frame. - if ((row?.status !== "running" && row?.status !== "pending") || !row.sessionFile) return undefined; - const ref = AgentRegistry.global().get(subagentId); - if (ref?.kind !== "sub" || ref.status !== "running" || !ref.session || ref.sessionFile !== row.sessionFile) { - return undefined; - } - return { ref, session: ref.session }; - } - - #remember(id: string, sessionFile: string | undefined): void { - if (!sessionFile) return; - this.#transcripts.delete(id); - this.#transcripts.set(id, sessionFile); - prune(this.#transcripts); - } - - #lifecycle(payload: SubagentLifecyclePayload): void { - const existing = this.#running.get(payload.id); - if (existing ? !sameOwner(payload, existing) : payload.status !== "started") return; - const sessionFile = payload.sessionFile ?? existing?.sessionFile; - this.#remember(payload.id, sessionFile); - if (payload.status === "started") { - this.#stale.delete(payload.id); - this.#running.set(payload.id, { - ...existing, - id: payload.id, - index: payload.index, - agent: payload.agent, - agentSource: payload.agentSource, - description: payload.description ?? existing?.description, - status: "running", - sessionFile, - parentToolCallId: payload.parentToolCallId ?? existing?.parentToolCallId, - lastUpdate: Date.now(), - }); - } else { - this.#running.delete(payload.id); - } - if (this.level !== "off") this.#send({ t: "subagent_lifecycle", payload }); - } - - #progress(payload: SubagentProgressPayload): void { - const { progress } = payload; - const existing = this.#running.get(progress.id); - if (!existing || !sameOwner(payload, existing)) return; - const sessionFile = payload.sessionFile ?? existing.sessionFile; - this.#remember(progress.id, sessionFile); - this.#running.set(progress.id, { - id: progress.id, - index: payload.index, - agent: payload.agent, - agentSource: payload.agentSource, - description: progress.description ?? existing.description, - status: progress.status, - task: payload.task, - assignment: payload.assignment, - sessionFile, - lastUpdate: Date.now(), - parentToolCallId: payload.parentToolCallId ?? existing.parentToolCallId, - progress, - }); - if (this.level !== "off") this.#send({ t: "subagent_progress", payload }); - } - - #event(payload: SubagentEventPayload): void { - if (this.level === "events" && !this.#stale.has(payload.id)) this.#send({ t: "subagent_event", payload }); - } -} - -/** Complete transcript lines from `fromByte` on; a `fromByte` past the end (file rewritten) restarts at 0. */ -async function readTranscript(sessionFile: string, fromByte = 0) { - let start = Number.isFinite(fromByte) ? Math.max(0, Math.trunc(fromByte)) : 0; - let size: number; - try { - ({ size } = await fs.stat(sessionFile)); - } catch (err) { - if (!isEnoent(err)) throw err; - return { sessionFile, fromByte: start, nextByte: start, reset: false, entries: [], messages: [] }; - } - const reset = start > size; - if (reset) start = 0; - const text = start >= size ? "" : await Bun.file(sessionFile).slice(start).text(); - const complete = text.slice(0, text.lastIndexOf("\n") + 1); - const entries: FileEntry[] = complete ? parseSessionEntries(complete) : []; - return { - sessionFile, - fromByte: start, - nextByte: start + Buffer.byteLength(complete, "utf8"), - reset, - entries, - messages: entries.filter((entry): entry is SessionMessageEntry => entry.type === "message").map(entry => entry.message), - }; -} - -const rosters = new WeakMap(); -const pendingInputs = new Map void>(); -let nextInputId = 1; - -/** - * Track `session`'s subagents from now on and forward the subscribed frames to the host. Call once - * when the channel connects: the roster only knows subagents whose lifecycle it saw. - */ -export function watchSubagents(ctx: CommandContext, bus: EventBus): () => void { - const roster = new Roster(ctx, bus); - rosters.set(ctx.session, roster); - return () => { - roster.dispose(); - rosters.delete(ctx.session); - }; -} - -function subagents(session: AgentSession, op: string): Roster { - const roster = rosters.get(session); - if (!roster) throw new Error(`${op}: Subagent event bus is unavailable`); - if (roster.sessionId !== session.sessionId) { - roster.clear(); - roster.sessionId = session.sessionId; - } - return roster; -} - -function subagentId(op: string, args: Record): string { - if (typeof args.subagentId !== "string" || args.subagentId.length === 0) { - throw new Error(`${op}: subagentId must be a non-empty string`); - } - return args.subagentId; -} - -function isSubscriptionLevel(value: unknown): value is SubscriptionLevel { - return value === "off" || value === "progress" || value === "events"; -} - -/** Close an open host prompt, answering it with "" as a cancelled input. */ -function endInput(ctx: CommandContext, id: number): void { - const resolve = pendingInputs.get(id); - if (!resolve) return; - pendingInputs.delete(id); - ctx.send({ t: "login_input_end", id }); - resolve(""); -} - -/** Ask the host for a value; resolves "" when cancelled or unanswered, like RPC's input dialog. */ -function askHost(ctx: CommandContext, open: Set, prompt: string, placeholder: string | undefined, secret: boolean): Promise { - const id = nextInputId++; - const { promise, resolve } = Promise.withResolvers(); - const timer = setTimeout(() => endInput(ctx, id), LOGIN_INPUT_TIMEOUT_MS); - pendingInputs.set(id, resolve); - open.add(id); - ctx.send({ t: "login_input", id, prompt, placeholder, secret }); - return promise.finally(() => { - clearTimeout(timer); - pendingInputs.delete(id); - open.delete(id); - }); -} - -export const agentsCommands: CommandTable = { - get_subagents: async ({ session }) => ({ subagents: subagents(session, "get_subagents").list() }), - - get_subagent_messages: async ({ session }, args) => { - const roster = subagents(session, "get_subagent_messages"); - const { subagentId, sessionFile, fromByte } = args; - if (subagentId !== undefined && typeof subagentId !== "string") { - throw new Error("get_subagent_messages: subagentId must be a string"); - } - if (sessionFile !== undefined && typeof sessionFile !== "string") { - throw new Error("get_subagent_messages: sessionFile must be a string"); - } - if (fromByte !== undefined && (typeof fromByte !== "number" || !Number.isFinite(fromByte))) { - throw new Error("get_subagent_messages: fromByte must be a finite number"); - } - return readTranscript(roster.transcriptFile(subagentId, sessionFile), fromByte); - }, - - set_subagent_subscription: async ({ session }, args) => { - const roster = subagents(session, "set_subagent_subscription"); - if (!isSubscriptionLevel(args.level)) { - throw new Error(`set_subagent_subscription: invalid level ${String(args.level)}`); - } - roster.level = args.level; - return { level: roster.level }; - }, - - /** Hard-kill a running subagent, as the Agent Hub `kill` does; `cancelled:false` when it is already gone. */ - cancel_subagent: async ({ session }, args) => { - const id = subagentId("cancel_subagent", args); - const owned = subagents(session, "cancel_subagent").owned(id); - if (!owned) return { cancelled: false }; - // Release first: its synchronous `aborted` tombstone keeps the executor from accepting the result mid-abort. - const [released, aborted] = await Promise.allSettled([ - AgentLifecycleManager.global().release(id, owned.ref, { tombstone: true }), - owned.session.abort({ reason: USER_INTERRUPT_LABEL }), - ]); - if (released.status === "rejected") throw released.reason; - if (aborted.status === "rejected") throw aborted.reason; - return { cancelled: released.value }; - }, - - /** Send a message to a running subagent as its user (Agent Hub chat); resolves once the subagent accepts it. */ - steer_subagent: async ({ session }, args) => { - const roster = subagents(session, "steer_subagent"); - const id = subagentId("steer_subagent", args); - const { message } = args; - if (typeof message !== "string" || !message.trim()) throw new Error("steer_subagent: message is required"); - const notRunning = `steer_subagent: subagent not running: ${id}`; - const owned = roster.owned(id); - if (!owned) throw new Error(notRunning); - let live: AgentSessionHandle; - try { - live = await AgentLifecycleManager.global().ensureLive(id); - } catch { - throw new Error(notRunning); - } - // ensureLive awaits: the id may now name another agent, or the subagent may have finished. - const current = roster.owned(id); - if (current?.ref !== owned.ref || current.session !== live) throw new Error(notRunning); - - const accepted = Promise.withResolvers(); - const unsubscribe = live.subscribe(event => { - if (event.type === "agent_start") accepted.resolve(); - }); - live.prompt(message, { streamingBehavior: "steer", throwOnDrop: true }).then( - () => accepted.resolve(), - (err: unknown) => { - accepted.reject(err); - logger.warn("steer_subagent message failed", { subagentId: id, error: String(err) }); - }, - ); - try { - await accepted.promise; - } catch (err) { - throw new Error(`steer_subagent: subagent refused the message: ${err instanceof Error ? err.message : String(err)}`); - } finally { - unsubscribe(); - } - }, - - get_login_providers: async ({ session }) => ({ - providers: getOAuthProviders().map(provider => ({ - id: provider.id, - name: provider.name, - available: provider.available, - authenticated: session.modelRegistry.authStorage.keys.source(provider.storeCredentialsAs ?? provider.id) !== undefined, - })), - }), - - login: async (ctx, args) => { - const { session } = ctx; - const { providerId } = args; - if (typeof providerId !== "string") throw new Error("login: providerId must be a string"); - const provider = getOAuthProviders().find(p => p.id === providerId); - if (!provider) throw new Error(`login: unknown OAuth provider: ${providerId}`); - // The host is a UI, so unlike RPC it answers secret prompts and prompts before the sign-in URL. - const open = new Set(); - try { - await session.modelRegistry.authStorage.oauth.login(providerId, { - onAuth: info => - ctx.send({ t: "login_open_url", url: info.url, launchUrl: info.launchUrl, instructions: info.instructions }), - onProgress: message => ctx.send({ t: "login_progress", message }), - onPrompt: prompt => askHost(ctx, open, prompt.message, prompt.placeholder, prompt.secret === true), - }); - } finally { - for (const id of open) endInput(ctx, id); - } - // Online refresh so the new credential re-runs discovery instead of reusing a fresh cache row. - await session.modelRegistry.refreshProvider(provider.storeCredentialsAs ?? provider.id, "online"); - await session.refreshScopedModels(); - return { providerId }; - }, - - get_logout_accounts: async ({ session }, args) => { - // An absent provider would list every provider's credentials. - if (typeof args.providerId !== "string") throw new Error("get_logout_accounts: providerId must be a string"); - return { - accounts: await listLogoutAccounts(session.modelRegistry.authStorage, args.providerId, session.sessionId), - }; - }, - - logout: async ({ session }, args) => { - const { providerId, credentialId } = args; - if (typeof providerId !== "string" || typeof credentialId !== "number" || !Number.isInteger(credentialId)) { - throw new Error("logout: providerId must be a string and credentialId an integer"); - } - const { removed, remainingSource } = await logoutCredential(session, providerId, credentialId); - if (!removed) throw new Error(`logout: credential ${credentialId} is not stored for ${providerId}`); - return { remainingSource }; - }, -}; - -export const agentsFrames: FrameTable = { - login_input_response: (_ctx, frame) => { - const { id, value } = frame; - if (typeof id !== "number") throw new Error("login_input_response: id must be a number"); - if (value !== undefined && value !== null && typeof value !== "string") { - throw new Error("login_input_response: value must be a string"); - } - pendingInputs.get(id)?.(value ?? ""); - }, -}; diff --git a/runner/src/commands/index.ts b/runner/src/commands/index.ts deleted file mode 100644 index 503c40b54..000000000 --- a/runner/src/commands/index.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { correlationCommands } from "../correlation"; -import { eventsCommands } from "../events"; -import { agentsCommands, agentsFrames } from "./agents"; -import { modelCommands, modelFrames } from "./model"; -import { promptingCommands, promptingFrames } from "./prompting"; -import { sessionCommands, sessionFrames } from "./session"; -import { toolsCommands, toolsFrames } from "./tools"; -import type { CommandTable, FrameTable } from "./types"; - -/** Every host command by name; each group file owns its names. */ -export const commands: CommandTable = { - ...promptingCommands, - ...modelCommands, - ...sessionCommands, - ...toolsCommands, - ...agentsCommands, - ...eventsCommands, - ...correlationCommands, -}; - -/** Every non-command host frame by `t`; each group file owns its frame types. */ -export const frames: FrameTable = { - ...promptingFrames, - ...modelFrames, - ...sessionFrames, - ...toolsFrames, - ...agentsFrames, -}; diff --git a/runner/src/commands/model.ts b/runner/src/commands/model.ts deleted file mode 100644 index 1f2769ea8..000000000 --- a/runner/src/commands/model.ts +++ /dev/null @@ -1,149 +0,0 @@ -import { ThinkingLevel } from "@oh-my-pi/pi-agent-core"; -import { toolWireSchema } from "@oh-my-pi/pi-ai/utils/schema"; -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import { CACHE_WARMING_MODES } from "@oh-my-pi/pi-coding-agent/session/cache-warmer"; -import { isSessionSettled } from "@oh-my-pi/pi-coding-agent/session/session-settle"; -import { buildAvailableSlashCommands } from "@oh-my-pi/pi-coding-agent/slash-commands/available-commands"; -import { calculateTokensPerSecond } from "@oh-my-pi/pi-coding-agent/utils/token-rate"; -import { parseConfiguredThinkingLevel } from "@oh-my-pi/pi-tui/thinking"; -import type { CommandTable, FrameTable } from "./types"; -import { queuedMessageOrigins } from "../correlation"; - -// shortcut: RPC's get_state first awaits its goal controller and counts a scheduled goal turn as unsettled; -// that controller is RPC transport state, so add it here when the runner ports goals. -export function sessionState(session: AgentSession) { - const queued = session.getQueuedMessages(); - const messageOrigins = queuedMessageOrigins(session); - return { - model: session.model, - thinkingLevel: session.thinkingLevel, - isStreaming: session.isStreaming, - isCompacting: session.isCompacting, - steeringMode: session.steeringMode, - followUpMode: session.followUpMode, - interruptMode: session.interruptMode, - sessionFile: session.sessionFile, - sessionId: session.sessionId, - sessionName: session.sessionName, - autoCompactionEnabled: session.autoCompactionEnabled, - queuedMessageCount: session.queuedMessageCount, - hasPendingAsyncWork: session.hasPendingAsyncWork(), - isSettled: isSessionSettled(session), - queuedMessages: { - steering: [...queued.steering], - followUp: [...queued.followUp], - liveSteered: queued.liveSteered, - ...(messageOrigins ? { messageOrigins } : {}), - }, - todoPhases: session.getTodoPhases(), - fastModeEnabled: session.isFastModeEnabled(), - tokensPerSecond: calculateTokensPerSecond(session.messages, session.isStreaming), - fastModeActive: session.isFastModeActive(), - slowModeSupported: session.isSlowModeSupported(), - slowModeEnabled: session.isSlowModeEnabled(), - slowModeScope: session.getSlowModeScope(), - usageLimit: session.getUsageLimitState(), - messageCount: session.messages.length, - systemPrompt: session.systemPrompt, - dumpTools: session.agent.state.tools.map(tool => ({ - name: tool.name, - description: tool.description, - parameters: toolWireSchema(tool), - examples: tool.examples, - })), - contextUsage: session.getContextUsage(), - goal: session.getGoalModeState() ?? null, - }; -} - -/** Exact provider/id match; a miss waits for background discovery (proxy/ollama models land after startup). */ -export async function findModel(session: AgentSession, provider: string, modelId: string) { - const find = () => session.getAvailableModels().find(m => m.provider === provider && m.id === modelId); - const model = find(); - if (model) return model; - await session.modelRegistry.awaitBackgroundRefresh(); - return find(); -} - -function bool(op: string, value: unknown): boolean { - if (typeof value !== "boolean") throw new Error(`${op}: enabled must be a boolean`); - return value; -} - -export const modelCommands: CommandTable = { - async get_state({ session }) { - return sessionState(session); - }, - async set_model({ session }, { provider, modelId }) { - if (typeof provider !== "string" || typeof modelId !== "string") throw new Error("set_model: provider and modelId must be strings"); - const model = await findModel(session, provider, modelId); - if (!model) throw new Error(`Model not found: ${provider}/${modelId}`); - await session.setModel(model); - return model; - }, - async cycle_model({ session }) { - return (await session.cycleModel()) ?? null; - }, - async get_available_models({ session }) { - await session.modelRegistry.awaitBackgroundRefresh(); - return { models: session.getAvailableModels() }; - }, - async set_thinking_level({ session }, { level }) { - const parsed = typeof level === "string" ? parseConfiguredThinkingLevel(level) : undefined; - if (!parsed) throw new Error(`set_thinking_level: invalid level ${String(level)}`); - session.setThinkingLevel(parsed); - }, - async cycle_thinking_level({ session }) { - const level = session.cycleThinkingLevel(); - return level ? { level } : null; - }, - async get_available_thinking_levels({ session }) { - return { levels: [ThinkingLevel.Off, ...session.getAvailableThinkingLevels()] }; - }, - async set_fast_mode({ session }, { enabled }) { - const on = bool("set_fast_mode", enabled); - if (!session.setFastMode(on) && on) throw new Error("Fast mode is unavailable for the current model."); - return { enabled: session.isFastModeEnabled(), active: session.isFastModeActive() }; - }, - async set_slow_mode({ session }, { enabled }) { - const on = bool("set_slow_mode", enabled); - if (!session.setSlowMode(on) && on) throw new Error("Slow mode is unavailable for the current model."); - return { enabled: session.isSlowModeEnabled() }; - }, - async set_auto_compaction({ session }, { enabled }) { - session.setAutoCompactionEnabled(bool("set_auto_compaction", enabled)); - }, - async set_auto_retry({ session }, { enabled }) { - session.setAutoRetryEnabled(bool("set_auto_retry", enabled)); - }, - async abort_retry({ session }) { - session.abortRetry(); - }, - async set_cache_warming({ session }, { mode }) { - const valid = CACHE_WARMING_MODES.find(m => m === mode); - if (!valid) throw new Error(`Invalid cache warming mode: ${String(mode)}`); - return { mode: session.setCacheWarmingMode(valid) }; - }, - async compact({ session }, { customInstructions }) { - if (customInstructions !== undefined && typeof customInstructions !== "string") throw new Error("compact: customInstructions must be a string"); - return session.compact(customInstructions); - }, - async abort_compaction({ session }) { - session.abortCompaction(); - }, - // The runner's own command; mirrors argument-less `/rename`: generate, persist as a user title, return it. - async generate_title({ session }) { - const title = await session.renameTitle(); - if (!title) return { title: null }; - if (!(await session.sessionManager.setSessionName(title, "user"))) throw new Error("generate_title: a user-set name takes precedence"); - return { title }; - }, - async get_session_stats({ session }) { - return session.getSessionStats(); - }, - async get_available_commands({ session }) { - return { commands: await buildAvailableSlashCommands(session) }; - }, -}; - -export const modelFrames: FrameTable = {}; diff --git a/runner/src/commands/prompting.ts b/runner/src/commands/prompting.ts deleted file mode 100644 index 4936fb431..000000000 --- a/runner/src/commands/prompting.ts +++ /dev/null @@ -1,459 +0,0 @@ -/** - * User input and what steers it: prompts, the steering/follow-up queue, aborts, queue modes, - * handoff, goal mode, todos and `!` bash. Prompt outcomes are `../prompt-results`, goal mode `../goal`. - * - * Unsolicited frames (omp → host): - * {t:"prompt_result", id, agentInvoked, status, error?, sessionSettled} - * once per accepted `prompt`/`abort_and_prompt` (`id` = its cmd id), after its work yields - * {t:"prompt_error", id, command, error} a prompt failed after its cmd was answered - * {t:"bash_output", id, chunk} output of the running `bash` cmd `id` - * {t:"command_output", text} a builtin slash command's output - * {t:"available_commands_update", commands} a builtin changed the slash command list - * {t:"session_info_update", title, sessionId} a builtin renamed the session - * {t:"config_update", model, thinkingLevel} a builtin changed model or thinking level - */ -import { clearPluginRootsAndCaches, resolveActiveProjectRegistryPath } from "@oh-my-pi/pi-coding-agent/discovery/helpers"; -import { loadSkillPrompt, parseSkillInvocation } from "@oh-my-pi/pi-coding-agent/extensibility/skills"; -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import { SKILL_PROMPT_MESSAGE_TYPE, USER_INTERRUPT_LABEL } from "@oh-my-pi/pi-coding-agent/session/messages"; -import { executeAcpBuiltinSlashCommand } from "@oh-my-pi/pi-coding-agent/slash-commands/acp-builtins"; -import { buildAvailableSlashCommands } from "@oh-my-pi/pi-coding-agent/slash-commands/available-commands"; -import { logger } from "@oh-my-pi/pi-utils"; -import { type ClientMessageClaim, claimClientMessage, submitCorrelated } from "../correlation"; -import { checkSettled, scheduledTurnProbe } from "../events"; -import { GOAL_OPS, GoalController } from "../goal"; -import { PromptResults, type PromptTicket, reportPromptResult } from "../prompt-results"; -import type { CommandContext, CommandTable, FrameTable } from "./types"; - -type ImageContent = NonNullable[1]>[number]; -type TodoPhase = Parameters[0][number]; -type StreamingBehavior = "steer" | "followUp"; -type InputOutcome = "local" | "cancelled" | "admitted" | "builtin-agent"; - -interface UserInput { - op: "prompt" | "steer" | "follow_up" | "abort_and_prompt"; - id: number | undefined; - message: string; - images: ImageContent[] | undefined; - streamingBehavior: StreamingBehavior | undefined; - /** Accept order; an abort or session change accepted later voids input not yet dispatched. */ - sequence: number; - ticket?: PromptTicket; - claim?: ClientMessageClaim; -} - -/** Per-session prompting state: prompt results, goal mode, and the ordered user-input gate. */ -export class Prompting { - readonly results: PromptResults; - readonly goals: GoalController; - /** Builtin commands whose work outlives the prompt (`/mcp reauth`); aborts tear them down. */ - readonly builtinAborts = new Set(); - #tail: Promise = Promise.resolve(); - #sequence = 0; - #validFrom = 0; - - constructor(session: AgentSession, send: CommandContext["send"]) { - this.goals = new GoalController(session, () => checkSettled(session)); - this.results = new PromptResults( - session, - send, - scheduledTurnProbe(session, () => this.goals.continuationPending), - () => checkSettled(session), - ); - session.subscribe(event => { - // Goal first: a continuation decided at this agent_end is pending before results read settlement. - this.goals.observe(event); - this.results.observe(event); - }); - } - - /** Take an accept-order slot; call synchronously when the command arrives. */ - accept(): number { - return ++this.#sequence; - } - - /** Void user input accepted before `sequence` (an abort, or a session change that succeeded). */ - invalidateBefore(sequence: number): void { - if (sequence > this.#validFrom) this.#validFrom = sequence; - } - - isCurrent(sequence: number): boolean { - return sequence >= this.#validFrom; - } - - /** Run user-input work in accept order; the tail releases when `work` settles, not when a turn ends. */ - enqueue(work: () => Promise): Promise { - const run = this.#tail.then(work, work); - this.#tail = run.then( - () => {}, - () => {}, - ); - return run; - } - - abortBuiltins(reason: string): void { - for (const controller of this.builtinAborts) controller.abort(reason); - this.builtinAborts.clear(); - } -} - -const states = new WeakMap(); - -/** The session's prompting state; session changes use it to void input and close open prompts. */ -export function prompting({ session, send }: CommandContext): Prompting { - let state = states.get(session); - if (!state) { - state = new Prompting(session, send); - states.set(session, state); - } - return state; -} - -function isImage(value: unknown): value is ImageContent { - return ( - typeof value === "object" && - value !== null && - "type" in value && - value.type === "image" && - "data" in value && - typeof value.data === "string" && - "mimeType" in value && - typeof value.mimeType === "string" - ); -} - -function isTodoPhase(value: unknown): value is TodoPhase { - return ( - typeof value === "object" && - value !== null && - "name" in value && - typeof value.name === "string" && - "tasks" in value && - Array.isArray(value.tasks) && - value.tasks.every( - (task: unknown) => - typeof task === "object" && - task !== null && - "content" in task && - typeof task.content === "string" && - "status" in task && - (task.status === "pending" || - task.status === "in_progress" || - task.status === "completed" || - task.status === "abandoned"), - ) - ); -} - -/** Read the fields every user-input command shares; throws before anything is accepted. */ -function readInput(ctx: CommandContext, op: UserInput["op"], args: Record): UserInput { - if (typeof args.message !== "string") throw new Error(`${op}: message must be a string`); - const images = args.images; - if (images !== undefined && !(Array.isArray(images) && images.every(isImage))) { - throw new Error(`${op}: images must be an array of {type:"image", data, mimeType}`); - } - const streamingBehavior = op === "prompt" ? args.streamingBehavior : undefined; - if (streamingBehavior !== undefined && streamingBehavior !== "steer" && streamingBehavior !== "followUp") { - throw new Error(`${op}: streamingBehavior must be "steer" or "followUp"`); - } - // Throws on a malformed or reused id, before the input takes a slot. - const claim = claimClientMessage(ctx.session, args.clientMessageId, args.origin); - const state = prompting(ctx); - const sequence = state.accept(); - // abort_and_prompt is itself an abort: it voids input accepted before it. - if (op === "abort_and_prompt") state.invalidateBefore(sequence); - return { op, id: ctx.id, message: args.message, images, streamingBehavior, sequence, claim }; -} - -const onPromptError = (ctx: CommandContext, input: UserInput) => (error: Error) => - ctx.send({ t: "prompt_error", id: input.id, command: input.op, error: error.message }); - -/** - * RPC's `dispatchOrderedUserInput`: input hooks, then skills and builtins (`prompt` only), then the - * agent. A `clientMessageId` claim is journaled as resolved when the input never reaches the agent - * and freed when it is cancelled or fails; an admitted one is settled by `submitCorrelated`. - */ -function dispatchInput(ctx: CommandContext, input: UserInput): Promise { - const { session } = ctx; - const state = prompting(ctx); - const run = state.enqueue(async (): Promise => { - const sessionId = session.sessionId; - const isCurrent = () => state.isCurrent(input.sequence) && session.sessionId === sessionId; - if (!isCurrent()) return "cancelled"; - let text = input.message; - let images = input.images; - const runner = session.extensionRunner; - if (runner?.hasHandlers("input")) { - const result = await runner.emitInput(text, images, "rpc"); - if (!isCurrent()) return "cancelled"; - if (result.handled) return "local"; - if (result.text !== undefined) text = result.text; - if (result.images !== undefined) images = result.images; - } - if (!text.trim() && !images?.length) return "local"; - if (input.op === "steer") { - await submitCorrelated(input.claim, undefined, hooks => session.steer(text, images, hooks)); - return "admitted"; - } - if (input.op === "follow_up") { - await submitCorrelated(input.claim, undefined, hooks => session.followUp(text, images, hooks)); - return "admitted"; - } - const ticket = input.ticket; - if (!ticket) return "cancelled"; - const report = (startPrompt: (onPromptAdmitted: () => void) => Promise) => - reportPromptResult(ticket, state.results, onPromptError(ctx, input), startPrompt); - if (input.op === "prompt") { - const parsed = session.skillsSettings?.enableSkillCommands ? parseSkillInvocation(text) : undefined; - const skill = parsed && session.skills.find(candidate => candidate.name === parsed.name); - if (parsed && skill) { - // Read the skill before answering so a missing SKILL.md fails the command itself. - const loaded = await loadSkillPrompt(skill, session); - if (!isCurrent()) return "cancelled"; - const built = session.renderUserSkillPrompt(loaded.skill, parsed, loaded.body); - const content = images?.length ? [{ type: "text" as const, text: built.message }, ...images] : built.message; - await report(onPromptAdmitted => - submitCorrelated(input.claim, onPromptAdmitted, hooks => - session.promptCustomMessage( - { customType: SKILL_PROMPT_MESSAGE_TYPE, content, display: true, details: built.details, attribution: "user" }, - { streamingBehavior: input.streamingBehavior ?? "steer", queueChipText: text, ...hooks }, - ), - ), - ); - return "admitted"; - } - const builtin = await runBuiltin(ctx, state, text); - if (!isCurrent()) return "cancelled"; - if (builtin !== false) { - if (!("prompt" in builtin)) { - if (builtin.agentInvoked !== true) return "local"; - void session.waitForIdle().then( - () => state.results.settle(ticket), - (error: unknown) => state.results.fail(ticket, error instanceof Error ? error.message : String(error)), - ); - return "builtin-agent"; - } - text = builtin.prompt; - } - } - if (!isCurrent()) return "cancelled"; - await report(onPromptAdmitted => - submitCorrelated( - input.claim, - onPromptAdmitted, - hooks => session.prompt(text, { images, streamingBehavior: input.streamingBehavior, userInitiated: true, ...hooks }), - // prompt() resolving false means the input was handled locally. - true, - ), - ); - return "admitted"; - }); - return run.then( - outcome => { - if (outcome === "local" || outcome === "builtin-agent") input.claim?.consumed(); - else if (outcome === "cancelled") input.claim?.release(); - return outcome; - }, - (error: unknown) => { - input.claim?.release(); - throw error; - }, - ); -} - -/** A builtin slash command (`/compact`, `/model`, …) as RPC runs them; `false` when `text` is not one. */ -async function runBuiltin(ctx: CommandContext, state: Prompting, text: string) { - const { session, send } = ctx; - const refreshCommands = async () => { - send({ t: "available_commands_update", commands: await buildAvailableSlashCommands(session) }); - }; - const abort = new AbortController(); - state.builtinAborts.add(abort); - let backgrounded = false; - try { - return await executeAcpBuiltinSlashCommand(text, { - session, - sessionManager: session.sessionManager, - settings: session.settings, - cwd: session.sessionManager.getCwd(), - signal: abort.signal, - output: text => send({ t: "command_output", text }), - refreshCommands, - reloadPlugins: async () => { - const projectPath = await resolveActiveProjectRegistryPath(session.sessionManager.getCwd()); - clearPluginRootsAndCaches(projectPath ? [projectPath] : undefined); - await session.refreshSkillsAndCommands(); - await refreshCommands(); - }, - runCommandInBackground: task => { - backgrounded = true; - void task() - .catch((error: unknown) => logger.warn("ompweb: background slash command failed", { error: String(error) })) - .finally(() => state.builtinAborts.delete(abort)); - }, - notifyTitleChanged: () => send({ t: "session_info_update", title: session.sessionName, sessionId: session.sessionId }), - notifyConfigChanged: () => send({ t: "config_update", model: session.model, thinkingLevel: session.thinkingLevel }), - }); - } finally { - if (!backgrounded) state.builtinAborts.delete(abort); - } -} - -/** What RPC's `abort` does before aborting: stop goal continuation and builtin work, void queued input. */ -function stopForAbort(ctx: CommandContext): void { - const state = prompting(ctx); - state.invalidateBefore(state.accept()); - state.goals.stopForHostAbort(); - state.abortBuiltins("Command aborted by host"); -} - -function readQueueMode(op: string, mode: unknown): "all" | "one-at-a-time" { - if (mode !== "all" && mode !== "one-at-a-time") throw new Error(`${op}: mode must be "all" or "one-at-a-time"`); - return mode; -} - -export const promptingCommands: CommandTable = { - async prompt(ctx, args) { - const input = readInput(ctx, "prompt", args); - const { results } = prompting(ctx); - // Taken before dispatch so a builtin that schedules a turn (`/retry`) cannot run ahead of it. - const ticket = results.begin(input.id); - input.ticket = ticket; - try { - // Answered after admission, so a queue edit sent after the answer finds the message. - const outcome = await dispatchInput(ctx, input); - if (outcome === "local") { - results.discard(ticket); - return { agentInvoked: false }; - } - if (outcome === "builtin-agent") return { agentInvoked: true }; - if (outcome === "cancelled") results.settle(ticket); - return undefined; - } catch (error) { - results.discard(ticket); - throw error; - } - }, - async steer(ctx, args) { - await dispatchInput(ctx, readInput(ctx, "steer", args)); - return undefined; - }, - async follow_up(ctx, args) { - await dispatchInput(ctx, readInput(ctx, "follow_up", args)); - return undefined; - }, - async remove_queued_message({ session }, args) { - if (typeof args.message !== "string") throw new Error("remove_queued_message: message must be a string"); - if (args.queue !== "steering" && args.queue !== "followUp") { - throw new Error('remove_queued_message: queue must be "steering" or "followUp"'); - } - const removed = session.takeQueuedMessage(args.message, args.queue); - // RPC drops images past its frame limit; the channel has none. - return removed?.images ? { removed: true, images: removed.images } : { removed: removed !== undefined }; - }, - async promote_queued_message({ session }, args) { - if (typeof args.message !== "string") throw new Error("promote_queued_message: message must be a string"); - return { promoted: session.promoteQueuedMessage(args.message) }; - }, - async abort(ctx) { - stopForAbort(ctx); - await ctx.session.abort({ reason: USER_INTERRUPT_LABEL }); - return undefined; - }, - async abort_and_restore_queue(ctx) { - // The TUI's Esc restore: withdraw queued user input (including input the run dequeued but never - // recorded) before aborting, so neither the aborted turn nor abort()'s queue drain runs it. - const restored = ctx.session.clearQueue({ forInterrupt: true }); - stopForAbort(ctx); - await ctx.session.abort({ reason: USER_INTERRUPT_LABEL }); - return restored; - }, - async abort_and_prompt(ctx, args) { - const input = readInput(ctx, "abort_and_prompt", args); - const state = prompting(ctx); - state.goals.stopForHostAbort(); - state.abortBuiltins("Command aborted by host"); - try { - await ctx.session.abort({ reason: USER_INTERRUPT_LABEL }); - } catch (error) { - // Never dispatched: free the id so the host can resend it. - input.claim?.release(); - throw error; - } - const ticket = state.results.begin(input.id); - input.ticket = ticket; - void dispatchInput(ctx, input).then( - outcome => { - if (outcome === "cancelled") state.results.settle(ticket); - else if (outcome === "local") state.results.completeLocal(ticket); - }, - (cause: unknown) => { - // Already answered: owe a prompt_error and a failed prompt_result. - const error = cause instanceof Error ? cause : new Error(String(cause)); - onPromptError(ctx, input)(error); - state.results.fail(ticket, error.message); - }, - ); - return undefined; - }, - // Queue modes apply to this session only (`persist: false`), as in RPC. - async set_steering_mode({ session }, args) { - session.setSteeringMode(readQueueMode("set_steering_mode", args.mode), false); - return undefined; - }, - async set_follow_up_mode({ session }, args) { - session.setFollowUpMode(readQueueMode("set_follow_up_mode", args.mode), false); - return undefined; - }, - async set_interrupt_mode({ session }, args) { - if (args.mode !== "immediate" && args.mode !== "wait") { - throw new Error('set_interrupt_mode: mode must be "immediate" or "wait"'); - } - session.setInterruptMode(args.mode, false); - return undefined; - }, - async handoff({ session }, args) { - if (args.customInstructions !== undefined && typeof args.customInstructions !== "string") { - throw new Error("handoff: customInstructions must be a string"); - } - // Resetting the agent mid-stream lets the live turn emit into a torn-down session (the TUI's guard). - if (session.isStreaming) throw new Error("Cannot hand off while a response is in progress"); - const result = await session.handoff(args.customInstructions); - return result ? { savedPath: result.savedPath } : null; - }, - async goal(ctx, args) { - const op = GOAL_OPS.find(candidate => candidate === args.op); - if (!op) throw new Error(`goal: op must be one of ${GOAL_OPS.join(", ")}`); - if (args.objective !== undefined && typeof args.objective !== "string") { - throw new Error("goal: objective must be a string"); - } - if (args.token_budget !== undefined && typeof args.token_budget !== "number") { - throw new Error("goal: token_budget must be a number"); - } - return prompting(ctx).goals.handle({ op, objective: args.objective, token_budget: args.token_budget }); - }, - async set_todos({ session }, args) { - const phases = args.phases; - if (!Array.isArray(phases) || !phases.every(isTodoPhase)) { - throw new Error("set_todos: phases must be an array of {name, tasks: [{content, status}]}"); - } - session.commitTodoPhases(phases); - return { todoPhases: session.getTodoPhases() }; - }, - async bash({ session, send, id }, args) { - if (typeof args.command !== "string") throw new Error("bash: command must be a string"); - // Reject rather than coerce: a falsy non-boolean such as `null` would silently share the output. - if (args.excludeFromContext !== undefined && typeof args.excludeFromContext !== "boolean") { - throw new Error("bash: excludeFromContext must be a boolean"); - } - return session.executeBash(args.command, chunk => send({ t: "bash_output", id, chunk }), { - excludeFromContext: args.excludeFromContext, - }); - }, - async abort_bash({ session }) { - session.abortBash(); - return undefined; - }, -}; - -export const promptingFrames: FrameTable = {}; diff --git a/runner/src/commands/session.ts b/runner/src/commands/session.ts deleted file mode 100644 index 53180fe97..000000000 --- a/runner/src/commands/session.ts +++ /dev/null @@ -1,381 +0,0 @@ -/** - * Session lifecycle, tree and transcript commands. - * - * Unsolicited frame (see {@link watchSessionIdentity}): - * omp → host {t:"session", sessionId, sessionFile, cwd} the session now writes to another file - * - * Errors carry a `code` (`session_busy`, `entry_not_found`, `unknown_since`, `stale_cursor`, - * `ask_reanswer_unsupported`) as a `CommandError`. - */ -import * as path from "node:path"; -import type { Model } from "@oh-my-pi/pi-ai"; -import { modelsAreEqual } from "@oh-my-pi/pi-catalog/models"; -import { type AgentSession, SessionBusyError } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import { findMostRecentNonEmptySession, findRecentSessionContinuation } from "@oh-my-pi/pi-coding-agent/session/session-listing"; -import { probeSessionLeases } from "@oh-my-pi/pi-coding-agent/session/session-storage"; -import { isRecord } from "@oh-my-pi/pi-utils"; -import { afterSessionChange } from "../events"; -import { prompting } from "./prompting"; -import { settleToolsBeforeSessionChange } from "./tools"; -import { type CommandContext, CommandError, type CommandTable, type FrameTable } from "./types"; - -function optionalString(op: string, args: Record, field: string): string | undefined { - const value = args[field]; - if (value !== undefined && typeof value !== "string") throw new Error(`${op}: ${field} must be a string`); - return value; -} - -function requiredString(op: string, args: Record, field: string): string { - const value = args[field]; - if (typeof value !== "string") throw new Error(`${op}: ${field} must be a string`); - return value; -} - -/** `fork`/`navigateTree` recheck idleness after their awaits; RPC reports that refusal as `session_busy`. */ -function rethrowBusy(err: unknown): never { - throw err instanceof SessionBusyError ? new CommandError(err.message, "session_busy") : err; -} - -/** The optional `provider`/`modelId` pair of `open_session`/`switch_session`, as RPC resolves it. */ -async function requestedModel(op: string, session: AgentSession, args: Record): Promise { - const provider = optionalString(op, args, "provider"); - const modelId = optionalString(op, args, "modelId"); - if (provider === undefined && modelId === undefined) return undefined; - if (provider === undefined || modelId === undefined) throw new Error("provider and modelId must be given together"); - const find = () => session.getAvailableModels().find(m => m.provider === provider && m.id === modelId); - // Discovery-backed providers populate after startup; only a miss waits for them. - let model = find(); - if (!model) { - await session.modelRegistry.awaitBackgroundRefresh(); - model = find(); - } - if (!model) throw new Error(`Model not found: ${provider}/${modelId}`); - return model; -} - -/** - * RPC's session-change bracket: settle tools, hold goal continuations across `change`, then on - * success void input accepted before this command (`sequence`, taken when it arrived) and close the - * prompts a detached run was answering. Tree navigation passes no `sequence`: it stays in the same - * session, so queued input and the post-change refresh are untouched, as in RPC. - */ -async function changeSession( - ctx: CommandContext, - sequence: number | undefined, - change: () => Promise, - detachesRun: (result: T | undefined) => boolean = () => false, -): Promise { - const state = prompting(ctx); - await settleToolsBeforeSessionChange(ctx.session); - await state.goals.beginSessionChange(); - let result: T | undefined; - let detached = false; - try { - result = await change(); - } finally { - // A change that throws may already have detached the run: count it as detached. - detached = detachesRun(result); - await state.goals.endSessionChange({ detachedRun: detached }); - // Respond only once this change's reattach (and any queued ahead of it) has run. - await state.goals.settled(); - } - if (!result.cancelled && sequence !== undefined) { - state.invalidateBefore(sequence); - if (detached) state.results.abortOpen(); - await afterSessionChange(ctx.session); - } - return result; -} - -const DEFAULT_MESSAGE_PAGE_LIMIT = 100; -const MAX_MESSAGE_PAGE_LIMIT = 256; -// Bounds one page so a run of large tool outputs never becomes one huge frame. -const MAX_MESSAGE_PAGE_BYTES = 768 * 1024; -const MAX_MESSAGE_CURSOR_CHARS = 2048; - -/** The transcript a cursor pages; any change (new message, branch, other session) makes it stale. */ -interface MessageSnapshot { - sessionId: string; - leafId: string | null; - messageCount: number; -} - -function encodeCursor(snapshot: MessageSnapshot, offset: number): string { - return Buffer.from(JSON.stringify({ version: 1, ...snapshot, offset }), "utf8").toString("base64url"); -} - -function decodeCursor(cursor: string): MessageSnapshot & { offset: number } { - const invalid = new Error("Invalid message cursor"); - if (cursor.length === 0 || cursor.length > MAX_MESSAGE_CURSOR_CHARS || !/^[A-Za-z0-9_-]+$/.test(cursor)) throw invalid; - const bytes = Buffer.from(cursor, "base64url"); - if (bytes.toString("base64url") !== cursor) throw invalid; - let value: unknown; - try { - value = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)); - } catch { - throw invalid; - } - if (!isRecord(value)) throw invalid; - const { version, sessionId, leafId, messageCount, offset } = value; - if ( - version !== 1 || - typeof sessionId !== "string" || - sessionId.length === 0 || - sessionId.length > 256 || - !((typeof leafId === "string" && leafId.length > 0 && leafId.length <= 256) || leafId === null) || - typeof messageCount !== "number" || - !Number.isSafeInteger(messageCount) || - messageCount < 0 || - typeof offset !== "number" || - !Number.isSafeInteger(offset) || - offset < 0 || - offset > messageCount - ) - throw invalid; - return { sessionId, leafId, messageCount, offset }; -} - -/** One page of the in-memory transcript, from `cursor` on, bounded by `limit` messages and the byte budget. */ -function pageMessages(session: AgentSession, cursor: string | undefined, limit = DEFAULT_MESSAGE_PAGE_LIMIT) { - if (!Number.isSafeInteger(limit) || limit < 1 || limit > MAX_MESSAGE_PAGE_LIMIT) { - throw new Error(`Message page limit must be between 1 and ${MAX_MESSAGE_PAGE_LIMIT}`); - } - const messages = session.messages; - const snapshot: MessageSnapshot = { - sessionId: session.sessionId, - leafId: session.sessionManager.getLeafId(), - messageCount: messages.length, - }; - let offset = 0; - if (cursor !== undefined) { - const decoded = decodeCursor(cursor); - if ( - decoded.sessionId !== snapshot.sessionId || - decoded.leafId !== snapshot.leafId || - decoded.messageCount !== snapshot.messageCount - ) { - throw new CommandError("Message cursor is stale", "stale_cursor"); - } - offset = decoded.offset; - } - const page: typeof messages = []; - let pageBytes = 2; - while (offset + page.length < messages.length && page.length < limit) { - const message = messages[offset + page.length]; - const messageBytes = Buffer.byteLength(JSON.stringify(message), "utf8") + (page.length === 0 ? 0 : 1); - if (page.length > 0 && pageBytes + messageBytes > MAX_MESSAGE_PAGE_BYTES) break; - page.push(message); - pageBytes += messageBytes; - } - const nextOffset = offset + page.length; - return { - messages: page, - ...(nextOffset < messages.length ? { nextCursor: encodeCursor(snapshot, nextOffset) } : {}), - totalMessages: messages.length, - }; -} - -/** new_session and switch_session detach a live run unless they were cancelled. */ -const detachesUnlessCancelled = (result: { cancelled: boolean } | undefined) => result?.cancelled !== true; - -/** - * Send `{t:"session", …}` whenever `session` moves to another session file: `/new`, `/resume`, - * fork, branch, open_session, or a write that relocates a contended file, whoever triggered it. - */ -export function watchSessionIdentity(session: AgentSession, send: CommandContext["send"]): () => void { - let sent = `${session.sessionId}\0${session.sessionFile ?? ""}`; - const check = () => { - const now = `${session.sessionId}\0${session.sessionFile ?? ""}`; - if (now === sent) return; - sent = now; - send({ t: "session", sessionId: session.sessionId, sessionFile: session.sessionFile, cwd: session.sessionManager.getCwd() }); - }; - const offChange = session.registerSessionChangeCallback(check); - // Relocation off a contended file changes the id without an AgentSession transition. Deferred past every observer of - // the same notice, so the host reads the relocation `notice` before this frame whatever the subscription order: a - // `session` frame first reads as another conversation and drops the original id's aliases. - const offMove = session.sessionManager.onPersistenceNotice(() => queueMicrotask(check)); - return () => { - offChange(); - offMove(); - }; -} - -export const sessionCommands: CommandTable = { - async new_session(ctx, args) { - const sequence = prompting(ctx).accept(); - const parentSession = optionalString("new_session", args, "parentSession"); - return changeSession( - ctx, - sequence, - async () => ({ cancelled: !(await ctx.session.newSession(parentSession ? { parentSession } : undefined)) }), - detachesUnlessCancelled, - ); - }, - async switch_session(ctx, args) { - const sequence = prompting(ctx).accept(); - const { session } = ctx; - const sessionPath = requiredString("switch_session", args, "sessionPath"); - const model = await requestedModel("switch_session", session, args); - return changeSession( - ctx, - sequence, - async () => ({ cancelled: !(await session.switchSession(sessionPath, model ? { model } : undefined)) }), - detachesUnlessCancelled, - ); - }, - /** - * Continue the newest non-empty session in `sessionDir`, or start one there: `--session-dir - * --continue` at runtime. Reopening the active session is a no-op and leaves a live run going. - */ - async open_session(ctx, args) { - const sequence = prompting(ctx).accept(); - const { session } = ctx; - const sessionDir = requiredString("open_session", args, "sessionDir"); - const model = await requestedModel("open_session", session, args); - const fileBeforeOpen = session.sessionFile; - return changeSession( - ctx, - sequence, - async () => { - if (!session.sessionFile) throw new Error("open_session requires session persistence (omit --no-session)"); - const dir = path.resolve(sessionDir); - const recent = await findMostRecentNonEmptySession(dir); - const latest = recent ? ((await findRecentSessionContinuation(recent))?.session.path ?? recent) : null; - const current = path.resolve(session.sessionFile); - const alreadyOpen = latest - ? current === path.resolve(latest) - : path.dirname(current) === dir && session.messages.length === 0; - let cancelled = false; - if (!alreadyOpen) { - cancelled = latest - ? !(await session.switchSession(latest, model ? { model } : undefined)) - : !(await session.newSession({ sessionDir: dir })); - } - // A resumed session is bound to the model by the switch; an already-open or fresh one selects it. - if (!cancelled && model && !modelsAreEqual(session.model, model)) await session.setModel(model); - return { cancelled, resumed: !cancelled && latest !== null, sessionId: session.sessionId, sessionFile: session.sessionFile }; - }, - // Any real open changes the file, even when an aliased path reopens a transcript with the same id. - () => session.sessionFile !== fileBeforeOpen, - ); - }, - async fork(ctx, args) { - const sequence = prompting(ctx).accept(); - const { session } = ctx; - const entryId = optionalString("fork", args, "entryId"); - // A fork is a snapshot: refuse while work could still write into the transcript. - if (session.isBusyForSnapshot) throw new CommandError(new SessionBusyError("fork the session").message, "session_busy"); - // Fork switches files in-process without detaching a run (it requires idle). - return changeSession(ctx, sequence, async () => ({ - cancelled: !(await session.fork(entryId, { requireIdle: true }).catch(rethrowBusy)), - })); - }, - async branch(ctx, args) { - const sequence = prompting(ctx).accept(); - const entryId = requiredString("branch", args, "entryId"); - // A live run keeps streaming to its normal end. - const result = await changeSession(ctx, sequence, () => ctx.session.branch(entryId)); - return { text: result.selectedText, cancelled: result.cancelled }; - }, - async navigate_tree(ctx, args) { - const { session } = ctx; - const entryId = requiredString("navigate_tree", args, "entryId"); - const customInstructions = optionalString("navigate_tree", args, "customInstructions"); - if (!session.sessionManager.getEntry(entryId)) throw new CommandError(`Entry ${entryId} not found`, "entry_not_found"); - // As `fork`: a run would keep writing into the branch the leaf just left. - if (session.isBusyForSnapshot) { - throw new CommandError(new SessionBusyError("navigate the session tree").message, "session_busy"); - } - const result = await changeSession(ctx, undefined, () => - session - .navigateTree(entryId, { - summarize: args.summarize === true, - customInstructions, - // Only to detect a re-answer, which moves nothing yet: refused below, since it needs a picker. - allowAskReopen: true, - requireIdle: true, - }) - .catch(rethrowBusy), - ); - if (result.reopenAsk) { - throw new CommandError("Re-answering an ask is not available over RPC; select another entry", "ask_reanswer_unsupported"); - } - return { cancelled: result.cancelled, aborted: result.aborted, editorText: result.editorText }; - }, - /** - * Move the leaf to just before a user message so the host can resend an edited version; the old - * branch stays in the tree. Formerly ompweb's `omp-web-edit` extension command. - */ - async edit_message(ctx, args) { - const { session } = ctx; - const entryId = requiredString("edit_message", args, "entryId"); - if (session.isBusyForSnapshot || session.hasPendingAsyncWork()) { - throw new CommandError("Cannot edit while the agent is running", "session_busy"); - } - const entry = session.sessionManager.getEntry(entryId); - if (entry?.type !== "message" || entry.message.role !== "user") throw new Error("The edit target must be a user message"); - const sessionId = session.sessionId; - const result = await changeSession(ctx, undefined, () => session.navigateTree(entryId, { summarize: false })); - if (session.sessionId !== sessionId) throw new Error("The session changed while editing history"); - // Navigation selects the target's parent; this non-message entry persists that leaf without adding context. - if (!result.cancelled) session.sessionManager.appendCustomEntry("omp-web-history-navigation", { entryId }); - return { sessionId: session.sessionId, cancelled: result.cancelled, leafId: session.sessionManager.getLeafId() }; - }, - async abort_branch_summary({ session }) { - session.abortBranchSummary(); - return undefined; - }, - async get_tree({ session }) { - return { tree: session.sessionManager.getTree(), leafId: session.sessionManager.getLeafId() }; - }, - async get_entries({ session }, args) { - const since = optionalString("get_entries", args, "since"); - const entries = session.sessionManager.getEntries(); - const leafId = session.sessionManager.getLeafId(); - if (since === undefined) return { entries, leafId }; - const index = entries.findIndex(entry => entry.id === since); - if (index === -1) throw new CommandError(`Unknown entries cursor: ${since}`, "unknown_since"); - return { entries: entries.slice(index + 1), leafId }; - }, - async get_messages({ session }) { - return { messages: session.messages }; - }, - async get_messages_page({ session }, args) { - const cursor = optionalString("get_messages_page", args, "cursor"); - const { limit } = args; - if (limit !== undefined && typeof limit !== "number") throw new Error("get_messages_page: limit must be a number"); - if (session.isStreaming || session.isCompacting) { - throw new CommandError("Cannot page messages while the session is changing", "session_busy"); - } - return pageMessages(session, cursor, limit); - }, - async get_branch_messages({ session }) { - return { messages: session.getUserMessagesForBranching() }; - }, - async get_last_assistant_text({ session }) { - return { text: session.getLastAssistantText() }; - }, - async set_session_name({ session }, args) { - const name = requiredString("set_session_name", args, "name").trim(); - if (!name || !(await session.setSessionName(name, "user"))) throw new Error("Session name cannot be empty"); - return undefined; - }, - async set_label({ session }, args) { - const entryId = requiredString("set_label", args, "entryId"); - session.sessionManager.appendLabelChange(entryId, optionalString("set_label", args, "label")); - return undefined; - }, - async export_html({ session }, args) { - return { path: await session.exportToHtml(optionalString("export_html", args, "outputPath")) }; - }, - async get_session_lease(_ctx, args) { - const { sessionIds } = args; - if (!Array.isArray(sessionIds) || !sessionIds.every(id => typeof id === "string")) { - throw new Error("get_session_lease: sessionIds must be an array of strings"); - } - return probeSessionLeases(sessionIds); - }, -}; - -export const sessionFrames: FrameTable = {}; diff --git a/runner/src/commands/tools.ts b/runner/src/commands/tools.ts deleted file mode 100644 index 97102996f..000000000 --- a/runner/src/commands/tools.ts +++ /dev/null @@ -1,268 +0,0 @@ -/** - * Host-provided tools and URI schemes, `/btw` side questions, word prediction, dictation and live voice. - * The session-side bridges live in `../host-bridge`. - * - * omp → host {t:"host_tool_call", id, toolCallId, toolName, arguments} run a tool registered by set_host_tools - * omp → host {t:"host_tool_cancel", id, targetId} the call `targetId` was aborted - * host → omp {t:"host_tool_update", id, partialResult} streamed partial result of call `id` - * host → omp {t:"host_tool_result", id, result, isError?} completes call `id` - * omp → host {t:"host_uri_request", id, operation, url, content?} read/write a set_host_uri_schemes URL - * omp → host {t:"host_uri_cancel", id, targetId} the request `targetId` was aborted - * host → omp {t:"host_uri_result", id, content?, contentType?, notes?, immutable?, isError?, error?} - * omp → host {t:"btw_delta", recordId, delta} answer text of the running side question - * omp → host {t:"btw_record", record} topic snapshot on start/complete/cancel/error - * omp → host {t:"notice", level:"error", message, source:"btw-history"} a side answer could not be saved - * omp → host {t:"dictation_state", captureId, state} starting | recording | transcribing - * omp → host {t:"dictation_transcript", captureId, text} full preview so far - * omp → host {t:"dictation_end", captureId, text, error?} once per capture - * omp → host {t:"live_phase", phase} | {t:"live_levels", input, output} - * omp → host {t:"live_transcript", role, turn, text, final} | {t:"live_end", error?} - */ -import { cfgSpellingAutocomplete } from "@oh-my-pi/pi-coding-agent/modes/settings"; -import { requestTextPrediction, textPredictionBackend } from "@oh-my-pi/pi-coding-agent/predict/client"; -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import { cfgSttEnabled } from "@oh-my-pi/pi-coding-agent/stt/settings"; -import { defaultLoadModeForToolName } from "@oh-my-pi/pi-coding-agent/tools/essential-tools"; -import { - type WordCompletionEngine, - type WordCompletionMethod, - type WordCompletionQuery, - wordCompletionQuery, -} from "@oh-my-pi/pi-tui/prompt/word-completion"; -import { isRecord, logger } from "@oh-my-pi/pi-utils"; -import { BtwBridge, DictationBridge, type HostToolDefinition, HostToolBridge, HostUriBridge, type HostUriSchemeDefinition, LiveBridge } from "../host-bridge"; -import type { CommandContext, CommandTable, FrameTable } from "./types"; - -interface ToolsState { - hostTools: HostToolBridge; - hostUris: HostUriBridge; - btw: BtwBridge; - dictation: DictationBridge; - live: LiveBridge; - predictor: WordPredictor; -} - -const states = new WeakMap(); - -function optionalString(op: string, args: Record, field: string): string | undefined { - const value = args[field]; - if (value !== undefined && typeof value !== "string") throw new Error(`${op}: ${field} must be a string`); - return value; -} - -/** The session's bridges, created on first use; their frames go to the host that sent the command. */ -function stateOf(ctx: CommandContext): ToolsState { - let state = states.get(ctx.session); - if (state) return state; - const send = ctx.send; - state = { - hostTools: new HostToolBridge(send), - hostUris: new HostUriBridge(send), - btw: new BtwBridge(ctx.session, send), - dictation: new DictationBridge(ctx.session, send), - live: new LiveBridge(ctx.session, send), - predictor: new WordPredictor(), - }; - states.set(ctx.session, state); - return state; -} - -/** - * Call before the session is replaced (new_session, switch_session, branch, fork, navigate_tree, - * open_session): stops dictation and lands every `/btw` checkpoint. Throws when a - * side answer cannot be saved, so the caller keeps the session instead of losing it. - */ -export async function settleToolsBeforeSessionChange(session: AgentSession): Promise { - const state = states.get(session); - if (!state) return; - const previousError = state.dictation.lastError; - try { - await state.dictation.stop(); - } catch (cause) { - // Only a transcription failure already reported in `dictation_end` lets the change proceed. - if (state.dictation.active || state.dictation.lastError === previousError || !(cause instanceof Error) || cause.message !== state.dictation.lastError) throw cause; - logger.warn("Dictation failed while changing sessions", { error: String(cause) }); - } - await state.btw.close(); -} - -/** - * Call when the host link closes: pending host tool and URI calls - * fail instead of hanging the agent, host schemes leave the process-wide router, mic and call stop. - */ -export async function closeToolsForHost(session: AgentSession): Promise { - const state = states.get(session); - if (!state) return; - states.delete(session); - state.hostTools.close("ompweb host disconnected before host tool execution completed"); - state.hostUris.close("ompweb host disconnected before host URI request completed"); - state.dictation.dispose(); - await state.live.stop(); -} - -function hostToolDefinitions(value: unknown): HostToolDefinition[] { - if (!Array.isArray(value)) throw new Error("set_host_tools: tools must be an array"); - return value.map((tool: unknown, index) => { - if (!isRecord(tool)) throw new Error(`set_host_tools: tool at index ${index} must be an object`); - const name = typeof tool.name === "string" ? tool.name.trim() : ""; - if (!name) throw new Error(`set_host_tools: tool at index ${index} must provide a non-empty name`); - const description = typeof tool.description === "string" ? tool.description.trim() : ""; - if (!description) throw new Error(`set_host_tools: tool "${name}" must provide a non-empty description`); - if (!isRecord(tool.parameters)) throw new Error(`set_host_tools: tool "${name}" must provide a JSON Schema object`); - const loadMode = tool.loadMode; - if (loadMode !== undefined && loadMode !== "essential" && loadMode !== "discoverable") { - throw new Error(`set_host_tools: tool "${name}" loadMode must be "essential" or "discoverable"`); - } - return { - name, - label: typeof tool.label === "string" && tool.label.trim() ? tool.label.trim() : name, - description, - parameters: tool.parameters, - hidden: tool.hidden === true, - loadMode: defaultLoadModeForToolName(name, loadMode), - readsSkillUris: tool.readsSkillUris === true ? true : undefined, - }; - }); -} - -function hostUriSchemes(value: unknown): HostUriSchemeDefinition[] { - if (!Array.isArray(value)) throw new Error("set_host_uri_schemes: schemes must be an array"); - // The bridge validates and normalizes each scheme name. - return value.map((scheme: unknown, index) => { - if (!isRecord(scheme)) throw new Error(`set_host_uri_schemes: scheme at index ${index} must be an object`); - return { - scheme: typeof scheme.scheme === "string" ? scheme.scheme : "", - description: typeof scheme.description === "string" ? scheme.description : undefined, - writable: scheme.writable === true, - immutable: scheme.immutable === true, - }; - }); -} - -const INVALID_TEXT_CURSOR = "cursor must be an integer UTF-16 offset within text"; - -function textCursor(op: string, args: Record): { text: string; cursor: number } { - const { text, cursor } = args; - if (typeof text !== "string" || typeof cursor !== "number" || !Number.isInteger(cursor) || cursor < 0 || cursor > text.length) { - throw new Error(`${op}: ${INVALID_TEXT_CURSOR}`); - } - return { text, cursor }; -} - -/** Composer ghost-text query at a UTF-16 cursor, gated like the TUI editor's: end of line, prose word. */ -function wordQueryAt(text: string, cursor: number): WordCompletionQuery | undefined { - if (cursor < text.length && text[cursor] !== "\n") return undefined; - const lines = text.split("\n"); - let cursorLine = 0; - let lineStart = 0; - while (lineStart + lines[cursorLine].length < cursor) lineStart += lines[cursorLine++].length + 1; - return wordCompletionQuery(lines, cursorLine, cursor - lineStart); -} - -/** - * One engine request in flight, and a newer - * request replaces the one waiting behind it (which answers `null`), so a burst of typing costs the - * shared daemon at most two inferences. - */ -class WordPredictor { - #busy = false; - #queued: { engine: WordCompletionEngine; query: WordCompletionQuery; resolve(suffix: string | null): void; reject(error: unknown): void } | undefined; - - predict(method: WordCompletionMethod, text: string, cursor: number): Promise { - if (method === "off") return Promise.resolve(null); - const query = wordQueryAt(text, cursor); - if (!query) return Promise.resolve(null); - if (!this.#busy) return this.#run(method, query); - this.#queued?.resolve(null); - const { promise, resolve, reject } = Promise.withResolvers(); - this.#queued = { engine: method, query, resolve, reject }; - return promise; - } - - async #run(engine: WordCompletionEngine, query: WordCompletionQuery): Promise { - this.#busy = true; - try { - const { suggestion } = await requestTextPrediction(engine, query.before, query.prefix); - return suggestion?.suffix || null; - } finally { - this.#busy = false; - const next = this.#queued; - this.#queued = undefined; - if (next) void this.#run(next.engine, next.query).then(next.resolve, next.reject); - } - } -} - -async function liveStart(ctx: CommandContext, args: Record, op: "live_start" | "live_connect") { - const voice = optionalString(op, args, "voice"); - const instructions = optionalString(op, args, "instructions"); - const sdpOffer = op === "live_connect" ? args.sdpOffer : undefined; - // Without an offer, live_connect would open the omp machine's microphone. - if (op === "live_connect" && (typeof sdpOffer !== "string" || !sdpOffer.trim())) throw new Error("live_connect requires a non-empty sdpOffer"); - const state = stateOf(ctx); - if (state.dictation.active) throw new Error(`${op}: End dictation before starting live voice`); - return state.live.start({ voice, instructions, sdpOffer: typeof sdpOffer === "string" ? sdpOffer : undefined }); -} - -// shortcut: live_start/live_connect/dictation_start should also be refused while a session change or shutdown runs; -// that counter lives with the session-change commands, so add it when they export one. -export const toolsCommands: CommandTable = { - set_host_tools: async (ctx, args) => { - const tools = hostToolDefinitions(args.tools); - await ctx.session.refreshRpcHostTools(stateOf(ctx).hostTools.tools(tools)); - return { toolNames: tools.map(tool => tool.name) }; - }, - set_host_uri_schemes: async (ctx, args) => ({ schemes: stateOf(ctx).hostUris.setSchemes(hostUriSchemes(args.schemes)) }), - - btw: async (ctx, args) => { - if (typeof args.question !== "string") throw new Error("btw: question must be a string"); - return { record: await stateOf(ctx).btw.ask(args.question, optionalString("btw", args, "recordId")) }; - }, - btw_cancel: async (ctx, args) => ({ cancelled: stateOf(ctx).btw.cancel(optionalString("btw_cancel", args, "recordId")) }), - get_btw_history: async ctx => ({ records: await stateOf(ctx).btw.history() }), - - predict_word: async (ctx, args) => { - const { text, cursor } = textCursor("predict_word", args); - return { suffix: await stateOf(ctx).predictor.predict(cfgSpellingAutocomplete.get(ctx.session.settings), text, cursor) }; - }, - predict_word_feedback: async (ctx, args) => { - const { text, cursor } = textCursor("predict_word_feedback", args); - const { suggestion, accepted } = args; - if (typeof suggestion !== "string" || typeof accepted !== "boolean") { - throw new Error("predict_word_feedback: suggestion must be a string and accepted a boolean"); - } - const method = cfgSpellingAutocomplete.get(ctx.session.settings); - if (method === "off") return undefined; - const query = wordQueryAt(text, cursor); - if (query) textPredictionBackend(method).feedback(query.before, query.prefix, suggestion, accepted); - return undefined; - }, - - dictation_start: async (ctx, args) => { - const state = stateOf(ctx); - if (state.live.active) throw new Error("dictation_start: End live voice before starting dictation"); - if (!cfgSttEnabled.get(ctx.session.settings)) throw new Error("dictation_start: Speech-to-text is disabled. Enable it in settings: stt.enabled"); - if (typeof args.captureId !== "string") throw new Error("dictation_start: captureId must be a string"); - await state.dictation.start(args.captureId); - return undefined; - }, - dictation_stop: async ctx => stateOf(ctx).dictation.stop(), - - live_start: (ctx, args) => liveStart(ctx, args, "live_start"), - live_connect: (ctx, args) => liveStart(ctx, args, "live_connect"), - live_stop: async ctx => { - await stateOf(ctx).live.stop(); - return undefined; - }, - live_mute: async (ctx, args) => { - const { muted } = args; - if (muted !== undefined && typeof muted !== "boolean") throw new Error("live_mute: muted must be a boolean"); - return stateOf(ctx).live.setMuted(muted); - }, -}; - -export const toolsFrames: FrameTable = { - host_tool_result: (ctx, frame) => stateOf(ctx).hostTools.result(frame), - host_tool_update: (ctx, frame) => stateOf(ctx).hostTools.update(frame), - host_uri_result: (ctx, frame) => stateOf(ctx).hostUris.result(frame), -}; diff --git a/runner/src/commands/types.ts b/runner/src/commands/types.ts deleted file mode 100644 index 2d8598252..000000000 --- a/runner/src/commands/types.ts +++ /dev/null @@ -1,38 +0,0 @@ -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; - -/** What a command handler can reach besides its arguments. */ -export interface CommandContext { - session: AgentSession; - /** The `cmd` frame's id while a command runs; absent for inbound non-command frames. */ - id?: number; - /** Send an unsolicited frame to the host (streamed output, progress, subscriptions). */ - send(frame: { t: string } & Record): void; -} - -/** A command failure with a machine-readable `code` (RPC's `session_busy`, `entry_not_found`, …); the `res` frame carries it. */ -export class CommandError extends Error { - constructor( - message: string, - readonly code: string, - ) { - super(message); - } -} - -/** - * One host command. `args` is the host's JSON as sent (the host is this user's own `ompweb host`, - * reached over a 0600 socket or the child's stdio); a handler checks what it reads and throws on bad - * input. The resolved value is the `res` frame's `data`; a throw becomes `{ok:false, error}`. - */ -export type CommandHandler = (ctx: CommandContext, args: Record) => Promise; - -export type CommandTable = Record; - - -/** - * Host frames that are not commands (results the host sends back for a request omp made, such as a - * host tool's output), keyed by `t`. A handler checks what it reads; a throw is logged and dropped. - */ -export type FrameHandler = (ctx: CommandContext, frame: Record) => void; - -export type FrameTable = Record; \ No newline at end of file diff --git a/runner/src/correlation.ts b/runner/src/correlation.ts deleted file mode 100644 index 25fb4fec2..000000000 --- a/runner/src/correlation.ts +++ /dev/null @@ -1,475 +0,0 @@ -/** - * ompweb's outbox correlation (`clientMessageId`) on the pinned, unpatched omp. - * - * omp's own support is an unmerged branch (`fix/client-message-correlation`) that patches AgentSession and - * SessionManager. This reproduces its wire contract from public API only: - * - * - Message identity. A queued prompt's message object comes from `onPromptQueued`; an idle prompt's is the first - * user-authored `message_start` after its `onPromptAdmitted` (input is serialized, so nothing else can start first). - * - Events. {@link stampClientMessageId} adds `clientMessageId` to `message_start`/`message_end` and aligned - * `clientMessageIds` to `queue_update`. omp emits `queue_update` before `onPromptQueued` names the message, so - * {@link onClientQueueChange} hands the bridge the identity-only `queue_update` that follows. - * - Journal. omp's entries cannot carry an extra field, so a `client-message-entry` custom entry - * `{clientMessageId, entryId}` names the message (or custom_message) entry a correlated message persisted as. - * Withdrawn or locally consumed IDs get the branch's own `client-message-resolution` entry. Both force the journal - * onto disk, as the branch does. - * - * Differences from the branch: - * - Reuse is checked once, when the command arrives, against the session current then; the branch re-checks in the - * destination journal when a session change lands between acceptance and dispatch. - * - A withdrawal is noticed when a queued message leaves every queue without reaching the transcript (checked after - * each `queue_update` and before a status read), not inside omp's removal paths. An abort that requeues and then - * records live-steered input in the same tick can be reported resolved instead of persisted; the transcript still - * shows the message. - * - Message identity is in-process only (as on the branch), and a message that was delivered but never persisted - * (a session change dropped it) is simply unresolved. - */ -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import type { AgentSessionEvent } from "@oh-my-pi/pi-coding-agent/session/agent-session-events"; -import { isUserAuthoredQueuedMessage } from "@oh-my-pi/pi-coding-agent/session/queued-messages"; -import { isRecord, logger } from "@oh-my-pi/pi-utils"; -import type { CommandTable } from "./commands/types"; - -type AgentMessage = AgentSession["messages"][number]; -type AgentMessageEnvelope = AgentMessage & { origin?: MessageOrigin }; - -export const CLIENT_MESSAGE_ENTRY_CUSTOM_TYPE = "client-message-entry"; -export const CLIENT_MESSAGE_RESOLUTION_CUSTOM_TYPE = "client-message-resolution"; -/** The branch's v1 frame budget: native messages past it are omitted whole, and ompweb falls back to history. */ -const STATUS_MAX_BYTES = 1024 * 1024; -const ORIGIN_MAX_BYTES = 4096; -type MessageOrigin = Record; - -export interface QueuedClientMessageIds { - steering: (string | null)[]; - followUp: (string | null)[]; -} - -export interface QueuedMessageOrigins { - steering: (MessageOrigin | null)[]; - followUp: (MessageOrigin | null)[]; -} - -export interface ClientMessageStatus { - messages: { clientMessageId: string; message: AgentMessageEnvelope }[]; - queuedMessages: { steering: string[]; followUp: string[]; liveSteered: number; clientMessageIds?: QueuedClientMessageIds; messageOrigins?: QueuedMessageOrigins }; - resolvedClientMessageIds: string[]; - persistedClientMessageIds: string[]; - unresolvedClientMessageIds: string[]; -} - -/** Options to spread into `prompt`/`promptCustomMessage`/`steer`/`followUp`. */ -export interface SubmitHooks { - onPromptAdmitted?: () => void; - onPromptQueued?: (queued: readonly AgentMessage[]) => void; -} - -export function isClientMessageId(value: unknown): value is string { - return typeof value === "string" && value.length > 0 && value.length <= 128; -} - -/** One submitted ID until it is journaled (persisted or resolved) or dropped. */ -export class ClientMessageClaim { - readonly id: string; - readonly #owner: Correlation; - readonly origin: MessageOrigin | undefined; - admitted = false; - message: AgentMessage | undefined; - - constructor(id: string, owner: Correlation, origin?: MessageOrigin) { - this.id = id; - this.#owner = owner; - this.origin = origin; - } - - hooks(onPromptAdmitted?: () => void): Required { - return { - onPromptQueued: queued => { - const message = queued.findLast(isUserAuthoredQueuedMessage); - if (message && !this.message) this.#owner.map(this, message, true); - }, - onPromptAdmitted: () => { - this.admitted = true; - if (!this.message) this.#owner.arm(this); - onPromptAdmitted?.(); - }, - }; - } - - /** Consumed without reaching the agent (an input hook, empty text, a builtin): journaled as resolved. */ - consumed(): void { - if (!this.message && this.#owner.drop(this)) this.#owner.resolve([this.id]); - } - - /** Cancelled, dropped or failed before it reached the agent: the ID is free again. */ - release(): void { - if (!this.message) this.#owner.drop(this); - } -} - -class Correlation { - readonly #session: AgentSession; - readonly ids = new WeakMap(); - readonly #claims = new Map(); - readonly origins = new WeakMap(); - readonly queueListeners = new Set<(event: AgentSessionEvent) => void>(); - #armed: ClientMessageClaim | undefined; - #writes: Promise = Promise.resolve(); - #sweepTimer: Timer | undefined; - - constructor(session: AgentSession) { - this.#session = session; - session.subscribe(event => { - if (event.type === "message_start") this.idOf(event.message, true); - else if (event.type === "message_end") this.#ended(event.message); - else if (event.type === "queue_update") { - clearTimeout(this.#sweepTimer); - // After the mutation that emitted it settles: a held or dequeued message is briefly in neither place. - this.#sweepTimer = setTimeout(() => this.#sweep(), 0); - } - }); - } - - claim(id: string, origin?: MessageOrigin): ClientMessageClaim { - const journal = journalIds(this.#session); - if (this.#claims.has(id) || journal.submitted.has(id)) throw new Error(`clientMessageId already submitted: ${id}`); - if (journal.resolved.has(id)) throw new Error(`clientMessageId already resolved: ${id}`); - const claim = new ClientMessageClaim(id, this, origin); - this.#claims.set(id, claim); - return claim; - } - - /** The ID of `message`; at its `message_start`, an armed idle prompt claims the first user-authored message. */ - idOf(message: AgentMessage, starting = false): string | undefined { - const id = this.ids.get(message); - if (id !== undefined || !starting || !this.#armed || !isUserAuthoredQueuedMessage(message)) return id; - return this.map(this.#armed, message, false); - } - - map(claim: ClientMessageClaim, message: AgentMessage, queued: boolean): string { - claim.message = message; - this.ids.set(message, claim.id); - if (claim.origin) this.origins.set(message, claim.origin); - if (this.#armed === claim) this.#armed = undefined; - if (queued && this.queueListeners.size > 0) { - const event = queueUpdate(this.#session); - for (const listener of this.queueListeners) listener(event); - } - return claim.id; - } - - arm(claim: ClientMessageClaim): void { - this.#armed = claim; - } - - drop(claim: ClientMessageClaim): boolean { - if (this.#armed === claim) this.#armed = undefined; - return this.#claims.get(claim.id) === claim && this.#claims.delete(claim.id); - } - - resolve(ids: string[]): void { - if (ids.length === 0) return; - this.#write(() => this.#session.sessionManager.appendCustomEntry(CLIENT_MESSAGE_RESOLUTION_CUSTOM_TYPE, { clientMessageIds: ids })); - } - - #ended(message: AgentMessage): void { - const id = this.ids.get(message); - const claim = id === undefined ? undefined : this.#claims.get(id); - if (claim?.message !== message) return; - this.#write(async () => { - // omp persists message_end on its own queue; the entry exists once that settles. - await this.#session.settleInFlightMessagePersistence(); - if (this.#claims.get(claim.id) !== claim) return; - this.#claims.delete(claim.id); - const entryId = persistedEntryId(this.#session, message); - if (entryId) { - this.#session.sessionManager.appendCustomEntry(CLIENT_MESSAGE_ENTRY_CUSTOM_TYPE, { - clientMessageId: claim.id, - entryId, - ...(claim.origin ? { origin: claim.origin } : {}), - }); - } - }); - } - - /** Queued messages that left every queue without reaching the transcript were withdrawn. */ - #sweep(): void { - const session = this.#session; - const { agent } = session; - const pending = new Set([ - ...agent.peekSteeringQueue(), - ...agent.peekFollowUpQueue(), - ...agent.peekUndeliveredQueuedMessages(), - ...session.heldQueuedMessages, - ]); - const withdrawn: string[] = []; - for (const claim of this.#claims.values()) { - const message = claim.message; - if (!message || pending.has(message) || session.messages.includes(message)) continue; - this.#claims.delete(claim.id); - withdrawn.push(claim.id); - } - this.resolve(withdrawn); - } - - #write(work: () => unknown): void { - this.#writes = this.#writes - .then(async () => { - await work(); - // Correlated acceptance must survive a reconnect before the first assistant reply. - await this.#session.sessionManager.ensureOnDisk(); - }) - .catch((error: unknown) => logger.warn("ompweb: client message journal write failed", { error: String(error) })); - } - - async status(ids: string[], maxBytes: number): Promise { - const session = this.#session; - const manager = session.sessionManager; - const journalId = manager.getSessionId(); - await session.settleInFlightMessagePersistence(); - await this.#writes; - clearTimeout(this.#sweepTimer); - this.#sweep(); - await this.#writes; - // Only entries journaled before this flush are confirmed by it. - const journal = journalIds(session); - const persisted = ids.filter(id => journal.submitted.has(id)); - const resolved = ids.filter(id => journal.resolved.has(id)); - await manager.flush(); - if (manager.getSessionId() !== journalId) throw new Error("Session changed during client message reconciliation"); - - const requested = new Set(ids); - const candidates: ClientMessageStatus["messages"] = []; - for (const message of session.messages) { - const id = this.ids.get(message); - if (id !== undefined && requested.has(id)) candidates.push({ - clientMessageId: id, - message: this.withOrigin(message), - }); - } - const queue = queueUpdate(session); - const queuedMessages: ClientMessageStatus["queuedMessages"] = { - steering: queue.steering, - followUp: queue.followUp, - liveSteered: queue.liveSteered, - ...(queue.clientMessageIds ? { clientMessageIds: queue.clientMessageIds } : {}), - ...(queue.messageOrigins ? { messageOrigins: queue.messageOrigins } : {}), - }; - const queued = new Set([...(queue.clientMessageIds?.steering ?? []), ...(queue.clientMessageIds?.followUp ?? [])]); - const build = (messages: ClientMessageStatus["messages"]): ClientMessageStatus => { - const represented = new Set([...queued, ...persisted, ...resolved, ...messages.map(m => m.clientMessageId)]); - return { - messages, - queuedMessages, - resolvedClientMessageIds: resolved, - persistedClientMessageIds: persisted, - unresolvedClientMessageIds: ids.filter(id => !represented.has(id)), - }; - }; - // Measured with no messages, the unresolved list is at its longest; each message added only shortens it. - // shortcut: queue chip texts are never blanked to fit, unlike the branch; add that if huge chips appear. - let remaining = maxBytes - Buffer.byteLength(JSON.stringify(build([])), "utf8"); - const messages: ClientMessageStatus["messages"] = []; - for (const envelope of candidates) { - const bytes = Buffer.byteLength(JSON.stringify(envelope), "utf8") + 1; - if (bytes > remaining) continue; - remaining -= bytes; - messages.push(envelope); - } - return build(messages); - } - - withOrigin(message: AgentMessage): AgentMessageEnvelope { - const origin = this.origins.get(message); - return origin ? { ...message, origin } : message; - } -} - -const states = new WeakMap(); - -function correlation(session: AgentSession): Correlation { - let state = states.get(session); - if (!state) { - state = new Correlation(session); - states.set(session, state); - } - return state; -} - -/** IDs the journal already holds: persisted (`client-message-entry`) and resolved. */ -function journalIds(session: AgentSession): { submitted: Set; resolved: Set } { - const submitted = new Set(); - const resolved = new Set(); - for (const entry of session.sessionManager.getEntries()) { - if (entry.type !== "custom" || !isRecord(entry.data)) continue; - if (entry.customType === CLIENT_MESSAGE_ENTRY_CUSTOM_TYPE && isClientMessageId(entry.data.clientMessageId)) { - submitted.add(entry.data.clientMessageId); - } else if (entry.customType === CLIENT_MESSAGE_RESOLUTION_CUSTOM_TYPE && Array.isArray(entry.data.clientMessageIds)) { - for (const id of entry.data.clientMessageIds) if (isClientMessageId(id)) resolved.add(id); - } - } - return { submitted, resolved }; -} - -/** The entry omp persisted `message` as: the same object for a message entry, customType + timestamp for a custom one. */ -function persistedEntryId(session: AgentSession, message: AgentMessage): string | undefined { - const branch = session.sessionManager.getBranchView(); - for (let index = branch.length - 1; index >= 0; index--) { - const entry = branch[index]; - if (entry.type === "message" && entry.message === message) return entry.id; - if ( - message.role === "custom" && - entry.type === "custom_message" && - entry.customType === message.customType && - Date.parse(entry.timestamp) === message.timestamp - ) { - return entry.id; - } - } - return undefined; -} - -/** Queue IDs aligned with `getQueuedMessages()` slots (legacy slots `null`); undefined when none is correlated. */ -export function queuedClientMessageIds(session: AgentSession): QueuedClientMessageIds | undefined { - const state = states.get(session); - if (!state) return undefined; - const { agent } = session; - const steering = [...agent.peekLiveSteeredMessages(), ...agent.peekSteeringQueue()].filter(isUserAuthoredQueuedMessage); - const followUp = agent.peekFollowUpQueue().filter(isUserAuthoredQueuedMessage); - const ids = { - steering: steering.map(message => state.ids.get(message) ?? null), - followUp: followUp.map(message => state.ids.get(message) ?? null), - }; - return [...ids.steering, ...ids.followUp].some(id => id !== null) ? ids : undefined; -} - -/** Origins aligned with the visible queue slots; null denotes a message without trusted provenance. */ -export function queuedMessageOrigins(session: AgentSession): QueuedMessageOrigins | undefined { - const state = states.get(session); - if (!state) return undefined; - const { agent } = session; - const steering = [...agent.peekLiveSteeredMessages(), ...agent.peekSteeringQueue()].filter(isUserAuthoredQueuedMessage); - const followUp = agent.peekFollowUpQueue().filter(isUserAuthoredQueuedMessage); - const origins = { - steering: steering.map(message => state.origins.get(message) ?? null), - followUp: followUp.map(message => state.origins.get(message) ?? null), - }; - return [...origins.steering, ...origins.followUp].some(origin => origin !== null) ? origins : undefined; -} - -function queueUpdate(session: AgentSession) { - const queue = session.getQueuedMessages(); - const clientMessageIds = queuedClientMessageIds(session); - const messageOrigins = queuedMessageOrigins(session); - return { - type: "queue_update" as const, - steering: [...queue.steering], - followUp: [...queue.followUp], - liveSteered: queue.liveSteered, - ...(clientMessageIds ? { clientMessageIds } : {}), - ...(messageOrigins ? { messageOrigins } : {}), - }; -} - -function parseOrigin(value: unknown): MessageOrigin | undefined { - if (value === undefined) return undefined; - if (!isRecord(value) || value.v !== 1 || typeof value.kind !== "string" || !value.kind - || typeof value.sentAt !== "string" || !value.sentAt) { - throw new Error(`origin is invalid or larger than ${ORIGIN_MAX_BYTES} bytes`); - } - try { - const serialized = JSON.stringify(value); - if (Buffer.byteLength(serialized, "utf8") > ORIGIN_MAX_BYTES) { - throw new Error(`origin is invalid or larger than ${ORIGIN_MAX_BYTES} bytes`); - } - return JSON.parse(serialized) as MessageOrigin; - } catch (error) { - if (error instanceof Error && error.message.startsWith("origin is invalid")) throw error; - throw new Error(`origin is invalid or larger than ${ORIGIN_MAX_BYTES} bytes`); - } -} - -/** - * Reserve a command's `clientMessageId` when it arrives; undefined when it has none. - * @throws when the ID or origin is invalid, or the ID was already submitted, persisted or resolved - */ -export function claimClientMessage(session: AgentSession, value: unknown, originValue?: unknown): ClientMessageClaim | undefined { - if (value === undefined) { - if (originValue !== undefined) throw new Error("origin requires clientMessageId"); - return undefined; - } - if (!isClientMessageId(value)) throw new Error("clientMessageId must be a nonempty string of at most 128 characters"); - return correlation(session).claim(value, parseOrigin(originValue)); -} - -/** - * Run one submission with `claim`'s hooks (just `onPromptAdmitted` without a claim). Afterwards an input that never - * reached a message is released, or, with `localWhenFalse` (`session.prompt`'s "handled locally"), resolved when the - * call admitted it and returned `false`. - */ -export async function submitCorrelated( - claim: ClientMessageClaim | undefined, - onPromptAdmitted: (() => void) | undefined, - submit: (hooks: SubmitHooks) => Promise, - localWhenFalse = false, -): Promise { - if (!claim) return submit({ onPromptAdmitted }); - try { - const result = await submit(claim.hooks(onPromptAdmitted)); - if (result === false && localWhenFalse && claim.admitted) claim.consumed(); - else claim.release(); - return result; - } catch (error) { - claim.release(); - throw error; - } -} - -/** Stamp correlation onto a session event before it is forwarded; call synchronously in the subscriber. */ -export function stampClientMessageId(session: AgentSession, event: AgentSessionEvent): AgentSessionEvent { - const state = states.get(session); - if (!state) return event; - if (event.type === "message_start" || event.type === "message_end") { - const id = state.idOf(event.message, event.type === "message_start"); - if (id === undefined) return event; - const stamped = { ...event, clientMessageId: id, message: state.withOrigin(event.message) }; - return stamped; - } - if (event.type === "queue_update") { - const clientMessageIds = queuedClientMessageIds(session); - const messageOrigins = queuedMessageOrigins(session); - if (!clientMessageIds && !messageOrigins) return event; - const stamped = { - ...event, - ...(clientMessageIds ? { clientMessageIds } : {}), - ...(messageOrigins ? { messageOrigins } : {}), - }; - return stamped; - } - return event; -} - -/** The identity-only `queue_update` omp does not emit: `listener` gets it, already stamped, after a correlated enqueue. */ -export function onClientQueueChange(session: AgentSession, listener: (event: AgentSessionEvent) => void): () => void { - const state = correlation(session); - state.queueListeners.add(listener); - return () => state.queueListeners.delete(listener); -} - -export async function getClientMessageStatus( - session: AgentSession, - value: unknown, - maxBytes = STATUS_MAX_BYTES, -): Promise { - if (!Array.isArray(value) || value.length === 0 || value.length > 32 || !value.every(isClientMessageId)) { - throw new Error("clientMessageIds must contain between 1 and 32 unique IDs"); - } - if (new Set(value).size !== value.length) throw new Error("clientMessageIds must be unique"); - return correlation(session).status(value, maxBytes); -} - -export const correlationCommands: CommandTable = { - async get_client_message_status({ session }, args) { - return getClientMessageStatus(session, args.clientMessageIds); - }, -}; diff --git a/runner/src/events.ts b/runner/src/events.ts deleted file mode 100644 index 0262d2334..000000000 --- a/runner/src/events.ts +++ /dev/null @@ -1,317 +0,0 @@ -/** - * Session events to the host as `{t:"event", event}`. - * - * Every `AgentSession` event goes through {@link SessionEventForwarder}, so - * `message_start` / `message_update` / `message_end` carry a `messageId` ("msg-N", unique per process) and - * `message_update` is a delta by default: - * {type:"message_update", messageId, message:{role}, assistantMessageEvent} - * where `assistantMessageEvent` drops the accumulated `partial` (see {@link toDelta}). - * `set_event_filter {messageUpdates:"full"}` sends the whole partial instead; a host that lost a delta base - * switches to full until the message ends, then back to delta. - * - * Events that are not plain AgentSession events: - * session_settled {type} the session went quiet after activity ({@link SessionSettleWatcher}) - * available_commands_update {type, commands} at subscribe, on command-metadata change, after a session change - * session_info_update {type, title?, sessionId} the session name changed (TUI /rename, auto title, host rename) - * notice (source "session-persistence") {type, level, message, source} store failure (error) or session moved to a new file (warning) - * - * Stamped fields (correlation.ts): `clientMessageId` on message_start/message_end of a host-submitted message, and - * `clientMessageIds` on queue_update; an extra identity-only queue_update follows once a queued message is named. - */ -import type { AssistantMessage, AssistantMessageEvent } from "@oh-my-pi/pi-ai"; -import { formatPersistenceFailure, formatPersistenceNotice } from "@oh-my-pi/pi-coding-agent/modes/persistence-failure"; -import type { AgentSession, AgentSessionEvent } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import { isSessionSettled, waitForSessionSettlement } from "@oh-my-pi/pi-coding-agent/session/session-settle"; -import { buildAvailableSlashCommands } from "@oh-my-pi/pi-coding-agent/slash-commands/available-commands"; -import { logger } from "@oh-my-pi/pi-utils"; -import type { CommandContext, CommandTable } from "./commands/types"; -import { onClientQueueChange, stampClientMessageId } from "./correlation"; - -type MessageUpdates = "full" | "delta"; - -/** - * The shell of a block a `*_start` opens. The snapshot behind a start event is cloned when the agent loop consumes the - * event, after the provider may have streamed more into the same partial, so its streamed content is not trustworthy: - * empty it, and every following delta applies cleanly. The matching `*_end` carries the final content. - */ -function openedBlock(block: AssistantMessage["content"][number] | undefined) { - switch (block?.type) { - case "text": - return { ...block, text: "" }; - case "thinking": - return { ...block, thinking: "" }; - case "toolCall": - return { ...block, arguments: {} }; - default: - return block; - } -} - -/** Drops the accumulated snapshot a streaming event carries, keeping what a client needs to rebuild it; `done` and `error` have none. */ -function toDelta(event: AssistantMessageEvent): object { - switch (event.type) { - case "done": - case "error": - return event; - case "start": { - const { partial, ...rest } = event; - return { ...rest, message: partial }; - } - case "text_delta": - case "thinking_delta": - case "toolcall_delta": { - const { partial: _partial, ...increment } = event; - return increment; - } - case "text_end": { - // `content` already carries the text; the block adds only what it cannot (the signature). - const { partial, ...rest } = event; - const block = partial.content[event.contentIndex]; - if (block?.type !== "text") return rest; - const { text: _text, ...meta } = block; - return { ...rest, block: meta }; - } - case "thinking_end": { - const { partial, ...rest } = event; - const block = partial.content[event.contentIndex]; - if (block?.type !== "thinking") return rest; - const { thinking: _thinking, ...meta } = block; - return { ...rest, block: meta }; - } - case "image_end": - case "toolcall_end": { - // `content` / `toolCall` is the whole block. - const { partial: _partial, ...rest } = event; - return rest; - } - default: { - const { partial, ...rest } = event; - const block = openedBlock(partial.content[event.contentIndex]); - return block === undefined ? rest : { ...rest, block }; - } - } -} - -/** - * Stamps message lifecycle events with a `messageId` and applies `set_event_filter`. Ids are assigned whether or - * not the event passes the filter, so changing the filter mid-message never splits one message across two ids. - */ -class SessionEventForwarder { - #filter: Set | undefined; - #messageUpdates: MessageUpdates = "delta"; - #messageCount = 0; - /** Ids of started, unfinished messages. External records (advisor cards, IRC) nest inside a streaming reply. */ - #openMessageIds: string[] = []; - readonly #output: (frame: object) => void; - - constructor(output: (frame: object) => void) { - this.#output = output; - } - - /** Forward only the listed event types; `null` forwards everything. Returns the active selection. */ - setFilter(events: readonly string[] | null, messageUpdates: MessageUpdates): string[] | null { - this.#filter = events === null ? undefined : new Set(events); - this.#messageUpdates = messageUpdates; - return this.#filter ? Array.from(this.#filter) : null; - } - - forward(event: AgentSessionEvent): void { - const messageId = this.#stamp(event); - if (this.#filter && !this.#filter.has(event.type)) return; - if (event.type === "message_update" && this.#messageUpdates === "delta") { - this.#output({ - ...event, - messageId, - message: { role: event.message.role }, - assistantMessageEvent: toDelta(event.assistantMessageEvent), - }); - return; - } - this.#output(messageId === undefined ? event : { ...event, messageId }); - } - - #stamp(event: AgentSessionEvent): string | undefined { - switch (event.type) { - case "message_start": { - const messageId = this.#mintMessageId(); - this.#openMessageIds.push(messageId); - return messageId; - } - case "message_update": { - let messageId = this.#openMessageIds.at(-1); - if (messageId === undefined) { - messageId = this.#mintMessageId(); - this.#openMessageIds.push(messageId); - } - return messageId; - } - case "message_end": - return this.#openMessageIds.pop() ?? this.#mintMessageId(); - case "agent_end": - // A run never leaves a message open; drop anything a dropped frame stranded. - this.#openMessageIds.length = 0; - return undefined; - default: - return undefined; - } - } - - #mintMessageId(): string { - return `msg-${++this.#messageCount}`; - } -} - -async function nextMacrotask(): Promise { - const { promise, resolve } = Promise.withResolvers(); - setImmediate(resolve); - await promise; -} - -/** - * Emits `session_settled` once per stretch of agent activity, after the final run yields and background work - * that could wake the session has drained. Re-checks on every terminal `agent_end` and after session transitions. - */ -class SessionSettleWatcher { - /** Agent activity since the last `session_settled`. */ - #active = false; - #checking = false; - #recheck = false; - readonly #session: AgentSession; - readonly #output: (frame: object) => void; - readonly #scheduledTurn: () => boolean; - - constructor(session: AgentSession, output: (frame: object) => void, scheduledTurn: () => boolean) { - this.#session = session; - this.#output = output; - this.#scheduledTurn = scheduledTurn; - } - - observe(event: AgentSessionEvent): void { - if (event.type === "agent_start") this.#active = true; - else if (event.type === "agent_end" && event.isTerminal !== false) void this.check(); - } - - /** Record activity no `agent_start` announces (a reported pending goal turn), so its stretch still settles. */ - markActive(): void { - this.#active = true; - } - - /** - * Settle the current stretch of activity if the session is quiet, waiting out background work that will wake - * it. A live run is left alone: its own terminal `agent_end` triggers the next check. - */ - async check(): Promise { - if (this.#checking) { - this.#recheck = true; - return; - } - this.#checking = true; - try { - do { - this.#recheck = false; - // Two hops: prompt_result frames for this yield are written one macrotask after it, and - // session_settled must follow them. - await nextMacrotask(); - await nextMacrotask(); - if (this.#active) { - await waitForSessionSettlement(this.#session, { - isCurrent: () => this.#active, - scheduledTurn: this.#scheduledTurn, - }); - } - } while (this.#recheck); - if (!this.#active || !isSessionSettled(this.#session, this.#scheduledTurn)) return; - this.#active = false; - this.#output({ type: "session_settled" }); - } catch (error) { - logger.warn("ompweb: session settle check failed", { error: String(error) }); - } finally { - this.#checking = false; - } - } -} - -interface HostEvents { - forwarder: SessionEventForwarder; - settle: SessionSettleWatcher; - emitCommands(): Promise; -} - -const bridges = new WeakMap(); -/** A host-scheduled turn not yet admitted (a goal continuation); set by the prompting commands, possibly before the bridge exists. */ -const scheduledTurns = new WeakMap boolean>(); - -export function subscribeHostEvents(session: AgentSession, send: CommandContext["send"]): () => void { - const event = (frame: object) => send({ t: "event", event: frame }); - const forwarder = new SessionEventForwarder(event); - const settle = new SessionSettleWatcher(session, event, () => scheduledTurns.get(session)?.() ?? false); - const emitCommands = async () => { - event({ type: "available_commands_update", commands: await buildAvailableSlashCommands(session) }); - }; - bridges.set(session, { forwarder, settle, emitCommands }); - - const unsubscribers = [ - session.subscribe(sessionEvent => { - // Synchronous: the stamp reads the live queues. - forwarder.forward(stampClientMessageId(session, sessionEvent)); - settle.observe(sessionEvent); - }), - onClientQueueChange(session, queueEvent => forwarder.forward(queueEvent)), - session.subscribeCommandMetadataChanged(() => void emitCommands()), - session.sessionManager.onSessionNameChanged(() => - event({ type: "session_info_update", title: session.sessionName, sessionId: session.sessionId }), - ), - // Straight to the host, not session.emitNotice: dispose clears session listeners before it closes the store. - session.sessionManager.onPersistenceError(error => - event({ type: "notice", level: "error", message: formatPersistenceFailure(error.message), source: "session-persistence" }), - ), - session.sessionManager.onPersistenceNotice(notice => - event({ type: "notice", level: "warning", message: formatPersistenceNotice(notice), source: "session-persistence" }), - ), - ]; - void emitCommands(); - return () => { - for (const unsubscribe of unsubscribers) unsubscribe(); - bridges.delete(session); - }; -} - -/** - * The probe prompt results read: reports whether `pending` holds a turn, and every "pending" answer - * opens a settle stretch so an abandoned continuation still ends with `session_settled`. - */ -export function scheduledTurnProbe(session: AgentSession, pending: () => boolean): () => boolean { - scheduledTurns.set(session, pending); - return () => { - const isPending = pending(); - if (isPending) bridges.get(session)?.settle.markActive(); - return isPending; - }; -} - -/** Re-check settlement where no terminal `agent_end` will (an abandoned goal turn, a prompt that settled without a run). */ -export function checkSettled(session: AgentSession): void { - void bridges.get(session)?.settle.check(); -} - -/** After new/switch/branch/fork/open session: the detached run sends no terminal `agent_end`, and the command catalog may differ. */ -export async function afterSessionChange(session: AgentSession): Promise { - const bridge = bridges.get(session); - if (!bridge) return; - void bridge.settle.check(); - await bridge.emitCommands(); -} - -export const eventsCommands: CommandTable = { - async set_event_filter({ session }, { events, messageUpdates = "delta" }) { - if (events !== null && (!Array.isArray(events) || !events.every(type => typeof type === "string" && type.length > 0))) { - throw new Error("set_event_filter: events must be null or an array of non-empty event type strings"); - } - if (messageUpdates !== "full" && messageUpdates !== "delta") { - throw new Error('set_event_filter: messageUpdates must be "full" or "delta"'); - } - const bridge = bridges.get(session); - if (!bridge) throw new Error("set_event_filter: no host event subscription on this session"); - return { events: bridge.forwarder.setFilter(events, messageUpdates), messageUpdates }; - }, -}; diff --git a/runner/src/goal.ts b/runner/src/goal.ts deleted file mode 100644 index 0e769c87d..000000000 --- a/runner/src/goal.ts +++ /dev/null @@ -1,404 +0,0 @@ -/** - * Goal mode for the host. The durable lifecycle (create/resume/pause/drop, accounting, persistence) is omp's - * `GoalRuntime`, shared with the TUI and the `goal` tool. This controller adds what the TUI's `InteractiveMode` - * otherwise owns: the goal tool's place in the active tool set, completion/drop exit, reattach after a session - * change, and an opt-in continuation driver keyed to agent lifecycle events instead of an editor idle window. - */ -import { cfgGoalContinuationModes, cfgGoalEnabled } from "@oh-my-pi/pi-coding-agent/goals/settings"; -import { type GoalModeState, goalContinuationActivity, goalFromModeData } from "@oh-my-pi/pi-coding-agent/goals/state"; -import type { AgentSession, AgentSessionEvent } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import { logger } from "@oh-my-pi/pi-utils"; - -/** The `goal.continuationModes` value that enables automatic continuation for a host (shared with omp's RPC host). */ -const HOST_GOAL_CONTINUATION_MODE = "rpc"; - -export const GOAL_OPS = ["get", "create", "resume", "pause", "drop"] as const; - -export interface GoalCommand { - op: (typeof GOAL_OPS)[number]; - objective?: string; - token_budget?: number; -} - -interface GoalResult { - goal: GoalModeState["goal"] | null; - state: GoalModeState | null; -} - -export class GoalController { - readonly #session: AgentSession; - /** Active tool set before goal mode added `goal`; restored when the goal ends. */ - #previousTools: string[] | undefined; - /** Continuation turns submitted whose terminal `agent_end` has not arrived. */ - #pendingContinuationTurns = 0; - #previousContinuationActivity: string | undefined; - /** A continuation turn made no new progress; wait for the host before continuing. */ - #suppressContinuation = false; - /** - * Set by a host abort; only host action (a prompt, or `goal create`/`resume`) clears it. Unlike - * {@link #suppressContinuation}, no turn's `agent_end` can re-arm it, so the aborted turn cannot schedule another. - */ - #hostStopped = false; - /** A continuation has been decided and is waiting for the session to go idle. */ - #continuationScheduled = false; - /** Bumped by a host abort or session change; a waiting continuation from before is void. */ - #continuationGeneration = 0; - /** Tool-set restoration triggered by session events; commands and reads wait for it. */ - #exitTask: Promise = Promise.resolve(); - /** Session changes in progress; goal turns are held until they end. */ - #sessionChanges = 0; - /** Transcript (session manager) id when the outermost in-progress change began. */ - #sessionBeforeChange: string | undefined; - /** A goal turn was waiting or became due during the change; report busy until it ends. */ - #heldDuringChange = false; - /** Reconciles run one at a time, each against the transcript current when it starts. */ - #reconcileTask: Promise = Promise.resolve(); - /** Reconciles queued or running. */ - #reconcilesPending = 0; - /** - * The in-progress change began while a reconcile was queued or running, so that reconcile may have read a - * transcript this change later replaced or rolled back: reconcile again when the change ends. - */ - #changeOverlappedReconcile = false; - /** A pending continuation was abandoned, so settle reporting must re-check. */ - readonly #onContinuationDropped: () => void; - - constructor(session: AgentSession, onContinuationDropped: () => void) { - this.#session = session; - this.#onContinuationDropped = onContinuationDropped; - } - - /** A goal continuation has been decided but not yet admitted: the session is busy meanwhile. */ - get continuationPending(): boolean { - // A change that is holding a goal turn may resume it when cancelled: not settled meanwhile. - return this.#continuationScheduled || (this.#sessionChanges > 0 && this.#heldDuringChange); - } - - /** - * The host interrupted the session (`abort`). Called before the abort starts, so the aborted run's own - * `agent_end` cannot schedule another goal turn; only `goal resume`/`create` or a prompt restarts it. - */ - stopForHostAbort(): void { - this.#hostStopped = true; - this.#suppressContinuation = true; - this.#continuationScheduled = false; - this.#continuationGeneration++; - } - - get #state(): GoalResult { - const state = this.#session.getGoalModeState(); - return { goal: state?.goal ?? null, state: state ?? null }; - } - - /** Resolves once queued goal exits and reconciles (including ones queued while waiting) have run. */ - async settled(): Promise { - for (let tail = this.#reconcileTask; ; tail = this.#reconcileTask) { - await this.#exitTask; - await tail; - if (tail === this.#reconcileTask) return; - } - } - - /** - * Call before any session change or tree navigation. Waits for a pending goal exit so it cannot land in the - * next session, voids a waiting continuation, and holds new goal turns until {@link endSessionChange}. - */ - async beginSessionChange(): Promise { - if (this.#sessionChanges++ === 0) { - // The transcript id, not `session.sessionId`: a pinned provider session id does not change with it. - this.#sessionBeforeChange = this.#session.sessionManager.getSessionId(); - this.#heldDuringChange = this.#continuationScheduled || this.#continuationWanted(); - } - if (this.#reconcilesPending > 0) this.#changeOverlappedReconcile = true; - this.#continuationScheduled = false; - this.#continuationGeneration++; - await this.#exitTask; - } - - /** - * Call after the change resolves, is cancelled, or throws. Only a change that switched the transcript adopts - * the target session's goal; an active goal stays active across it, as in the TUI. `detachedRun`: the change - * stopped the running agent, so a detached continuation turn no longer counts as pending. Never throws. - */ - async endSessionChange(options?: { detachedRun?: boolean }): Promise { - if (options?.detachedRun) { - this.#pendingContinuationTurns = 0; - this.#previousContinuationActivity = undefined; - } - if (--this.#sessionChanges > 0) return; - const switched = - this.#changeOverlappedReconcile || this.#session.sessionManager.getSessionId() !== this.#sessionBeforeChange; - this.#changeOverlappedReconcile = false; - this.#sessionBeforeChange = undefined; - this.#heldDuringChange = false; - try { - if (switched && this.#reconcilesPending > 0) { - // A reconcile is running, possibly the one whose extension notification started this change: - // queue behind it without waiting, or the two would wait on each other. - void this.reconcile({ preserveActiveGoal: true }) - .then(() => this.#scheduleContinuation()) - .catch(reportControllerError) - .finally(() => this.#onContinuationDropped()); - return; - } - if (switched) await this.reconcile({ preserveActiveGoal: true }); - this.#scheduleContinuation(); - } catch (error) { - reportControllerError(error); - } - this.#onContinuationDropped(); - } - - #queueExit(exit: () => Promise): void { - this.#exitTask = this.#exitTask.then(exit).catch(reportControllerError); - } - - async handle(command: GoalCommand): Promise { - await this.settled(); - switch (command.op) { - case "get": - return this.#state; - case "create": - return await this.#create(command); - case "resume": - return await this.#resume(); - case "pause": - await this.#session.goalRuntime.pauseGoal(); - await this.#exit(); - return this.#state; - case "drop": - // The runtime's `goal_updated(dropped)` queues the exit. - await this.#session.goalRuntime.dropGoal(); - await this.#exitTask; - return this.#state; - } - } - - #assertCanEnter(): void { - if (!cfgGoalEnabled.get(this.#session.settings)) throw new Error("Goal mode is disabled (goal.enabled)."); - if (this.#session.getPlanModeState()?.enabled) throw new Error("Exit plan mode before starting a goal."); - } - - async #create(command: GoalCommand): Promise { - this.#assertCanEnter(); - const objective = command.objective?.trim(); - if (!objective) throw new Error("objective is required when op=create"); - const tokenBudget = command.token_budget; - if (tokenBudget !== undefined && (!Number.isInteger(tokenBudget) || tokenBudget <= 0)) { - throw new Error("token_budget must be a positive integer when provided"); - } - const current = this.#session.getGoalModeState(); - if (current?.enabled) throw new Error("A goal is already active. Drop it before creating another."); - if (current?.goal.status === "paused") throw new Error("Resume or drop the paused goal before creating another."); - await this.#enter(() => this.#session.goalRuntime.createGoal({ objective, tokenBudget })); - return this.#state; - } - - async #resume(): Promise { - this.#assertCanEnter(); - const current = this.#session.getGoalModeState(); - if (current?.enabled) return this.#state; - if (current?.goal.status !== "paused") throw new Error("No paused goal to resume."); - await this.#enter(() => this.#session.goalRuntime.resumeGoal()); - return this.#state; - } - - async #enter(start: () => Promise): Promise { - // The pre-goal tool set is captured once, when this controller first adds `goal` (a reattached paused goal - // already holds it). A `goal` tool the host enabled before the goal stays enabled afterwards. - const previousTools = this.#previousTools ?? this.#session.getEnabledToolNames(); - const state = await start(); - this.#previousTools = previousTools; - await this.#session.setActiveToolsByName([...new Set([...previousTools, "goal"])]); - this.#session.setGoalModeState(state); - this.#resetContinuation(); - if (this.#session.isStreaming) { - await this.#session.sendGoalModeContext({ deliverAs: "steer" }); - return; - } - this.#scheduleContinuation(); - } - - /** Restore the pre-goal tool set. Idempotent. */ - async #exit(): Promise { - const previousTools = this.#previousTools; - this.#previousTools = undefined; - this.#resetContinuation(); - if (previousTools) await this.#session.setActiveToolsByName(previousTools); - } - - /** Write the completion records into the current transcript and clear goal state. Synchronous. */ - #journalCompletion(): void { - const state = this.#session.getGoalModeState(); - this.#session.setGoalModeState(undefined); - this.#session.sessionManager.appendModeChange("none"); - this.#session.sessionManager.appendCustomEntry("goal-completed", { - objective: state?.goal.objective, - tokensUsed: state?.goal.tokensUsed, - tokenBudget: state?.goal.tokenBudget, - timeUsedSeconds: state?.goal.timeUsedSeconds, - }); - } - - #resetContinuation(): void { - this.#pendingContinuationTurns = 0; - this.#previousContinuationActivity = undefined; - this.#suppressContinuation = false; - this.#hostStopped = false; - } - - /** - * Leave the previous session's goal behind and restore a goal journaled in the current session, mirroring - * the TUI's reattach. `preserveActiveGoal` keeps an active goal active (in-process session changes); without - * it an active goal is paused. Queued behind any reconcile already running. Never awaited from - * {@link beginSessionChange}: `onThreadResumed` notifies extensions, which may start another change. - */ - async reconcile(options?: { preserveActiveGoal?: boolean }): Promise { - this.#reconcilesPending++; - const run = this.#reconcileTask.then(() => this.#reconcileOnce(options)); - this.#reconcileTask = run.catch(reportControllerError).finally(() => { - this.#reconcilesPending--; - }); - await run; - } - - async #reconcileOnce(options: { preserveActiveGoal?: boolean } | undefined): Promise { - // Goal state and the goal tool belong to the session that set them; the session keeps both across a - // switch, so clear them here first. - this.#continuationScheduled = false; - this.#continuationGeneration++; - await this.#exitTask; - await this.#exit(); - this.#session.setGoalModeState(undefined); - const context = this.#session.sessionManager.buildSessionContext(); - const runtime = this.#session.goalRuntime; - if (context.mode !== "goal" && context.mode !== "goal_paused") { - runtime.clearAccounting(); - return; - } - const goal = cfgGoalEnabled.get(this.#session.settings) ? goalFromModeData(context.modeData) : undefined; - if (!goal) { - runtime.clearAccounting(); - this.#session.sessionManager.appendModeChange("none"); - return; - } - this.#session.setGoalModeState({ enabled: context.mode === "goal", mode: "active", goal }); - const restored = await runtime.onThreadResumed({ preserveActiveGoal: options?.preserveActiveGoal }); - if (!restored?.goal) return; - const previousTools = this.#session.getEnabledToolNames(); - this.#previousTools = previousTools; - await this.#session.setActiveToolsByName([...new Set([...previousTools, "goal"])]); - } - - /** Feed session events, before the settle watcher sees them, so a continuation is admitted before settlement is read. */ - observe(event: AgentSessionEvent): void { - if (event.type === "message_start" && event.message.role === "user" && !event.message.synthetic) { - // A host prompt re-arms continuation after a no-progress stop. - this.#resetContinuation(); - return; - } - if (event.type === "goal_updated") { - const status = event.state?.goal.status; - if (status === "dropped") { - this.#queueExit(() => this.#exit()); - } else if (event.state?.enabled && this.#previousTools === undefined) { - // Created by the agent's `goal` tool rather than this controller. - this.#previousTools = this.#session.getEnabledToolNames(); - } - return; - } - if (event.type !== "agent_end" || event.isTerminal === false) return; - if (this.#pendingContinuationTurns > 0) { - this.#pendingContinuationTurns--; - const activity = goalContinuationActivity(event.messages); - this.#suppressContinuation = activity.length === 0 || activity === this.#previousContinuationActivity; - this.#previousContinuationActivity = activity; - } else { - // As in the TUI: a turn that was not a goal continuation re-arms after a no-progress stop. - // A host abort stays in force (#hostStopped). - this.#suppressContinuation = false; - this.#previousContinuationActivity = undefined; - } - if (this.#session.getGoalModeState()?.mode === "exiting") { - // Journal now, while the transcript is certainly the one that completed the goal; only the tool-set - // restore is queued (a later reconcile also restores it). - this.#journalCompletion(); - this.#queueExit(() => this.#exit()); - return; - } - this.#scheduleContinuation(); - } - - /** Whether goal continuation is wanted at all, independent of momentary busyness; read from the live session. */ - #continuationWanted(): boolean { - const session = this.#session; - if (!cfgGoalContinuationModes.get(session.settings).includes(HOST_GOAL_CONTINUATION_MODE)) return false; - if (this.#hostStopped || this.#suppressContinuation || session.isDisposed) return false; - if (session.getPlanModeState()?.enabled) return false; - const state = session.getGoalModeState(); - return state?.enabled === true && state.goal.status === "active"; - } - - /** - * Decide at a yield to continue the goal; admit the continuation once the yielding run has fully unwound. - * While waiting, {@link continuationPending} is true. At admission every gate is re-read: an abort, disposal, - * pause, plan mode, or another turn starting meanwhile drops it. - */ - #scheduleContinuation(): void { - if (this.#sessionChanges > 0) { - // Held until the change ends; a same-session change resumes it. - if (this.#continuationWanted()) this.#heldDuringChange = true; - return; - } - if (this.#continuationScheduled || !this.#continuationWanted()) return; - this.#continuationScheduled = true; - const generation = this.#continuationGeneration; - void (async () => { - const { promise, resolve } = Promise.withResolvers(); - setImmediate(resolve); - await promise; - await this.#session.waitForIdle(); - if (!this.#continuationScheduled || generation !== this.#continuationGeneration) { - this.#onContinuationDropped(); - return; - } - this.#continuationScheduled = false; - const session = this.#session; - const idle = - !session.isStreaming && - !session.hasAdmittedSubmission && - session.queuedMessageCount === 0 && - !session.isSessionTransitioning; - const prompt = idle && this.#continuationWanted() ? session.goalRuntime.buildContinuationPrompt() : undefined; - if (!prompt) { - this.#onContinuationDropped(); - return; - } - this.#pendingContinuationTurns++; - const unclaim = () => { - this.#pendingContinuationTurns = Math.max(0, this.#pendingContinuationTurns - 1); - // No run follows, so nothing else will end this activity stretch. - this.#onContinuationDropped(); - }; - // promptCustomMessage counts the submission as admitted synchronously, so every settle report sees it - // from here on. A continuation refused or bailing before its run starts must not stay counted. - session.promptCustomMessage({ customType: "goal-continuation", content: prompt, display: false }).then( - dispatched => { - if (!dispatched) unclaim(); - }, - (error: unknown) => { - unclaim(); - reportControllerError(error); - }, - ); - })().catch((error: unknown) => { - this.#continuationScheduled = false; - this.#onContinuationDropped(); - reportControllerError(error); - }); - } -} - -function reportControllerError(error: unknown): void { - logger.warn("ompweb: goal controller failed", { error: error instanceof Error ? error.message : String(error) }); -} diff --git a/runner/src/headless.ts b/runner/src/headless.ts deleted file mode 100644 index e1d36dfec..000000000 --- a/runner/src/headless.ts +++ /dev/null @@ -1,205 +0,0 @@ -/** - * Headless omp: `ompweb-omp --headless [omp flags]`, spawned by `ompweb host`, which speaks the - * channel (`channel.ts`) on this process's stdin/stdout. Nothing else may write to stdout. - * - * Startup is omp's own `runRootCommand` (settings, auth, models, --resume/--continue, extensions, - * MCP, plugins, settings watch, subagent revival); its `runSessionHost` hook hands the finished - * session to `runHeadless` instead of the TUI or print mode. - * - * Unsolicited frames: - * omp → host {t:"extension_error", extensionPath, event, error} an extension handler threw - * omp → host {t:"extension_error", action, error} an extension's message send failed - */ -import type { ExtensionUIContext, ExtensionUiComponent } from "@oh-my-pi/pi-coding-agent/extensibility/extensions/types"; -import { timedOutAskDialogResult } from "@oh-my-pi/pi-coding-agent/extensibility/extensions/types"; -import type { SessionHostContext } from "@oh-my-pi/pi-coding-agent/main"; -import { initializeExtensions } from "@oh-my-pi/pi-coding-agent/modes/runtime-init"; -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import { type Terminal, TUI } from "@oh-my-pi/pi-tui"; -import { KeybindingsManager } from "@oh-my-pi/pi-tui/app-keybindings"; -import { theme } from "@oh-my-pi/pi-tui/theme"; -import { logger } from "@oh-my-pi/pi-utils"; -import { type CustomFactory, HostChannel } from "./channel"; - -// shortcut: custom components render at a fixed width; take the browser's width when a host sends one. -const CUSTOM_COLUMNS = 100; -const CUSTOM_ROWS = 40; - -let headless = false; -let writeChannel: (line: string) => void = () => {}; -let stdin: ReadableStreamDefaultReader | undefined; - -/** Whether this process is a headless omp (`--headless`). */ -export function isHeadless(): boolean { - return headless; -} - -/** - * Turn this process into a headless omp before startup runs; returns the argv omp's parser gets. - * stdout becomes the channel's alone: everything else written there goes to stderr. - */ -export function enterHeadless(argv: string[]): string[] { - headless = true; - writeChannel = process.stdout.write.bind(process.stdout); - // Extensions and libraries may print; on stdout that would corrupt the channel. - process.stdout.write = process.stderr.write.bind(process.stderr); - console.log = console.info = console.debug = console.error; - // Locked now, before extensions load, so none can read the host's frames. - stdin = Bun.stdin.stream().getReader(); - return argv.filter(arg => arg !== "--headless"); -} - -/** Byte sink: the stand-in TUI renders through `requestRender` below, never through a terminal. */ -class SinkTerminal implements Terminal { - start(): void {} - stop(): void {} - async drainInput(): Promise {} - write(): void {} - get columns(): number { - return CUSTOM_COLUMNS; - } - get rows(): number { - return CUSTOM_ROWS; - } - get kittyProtocolActive(): boolean { - return false; - } - get kittyEnableSequence(): string | null { - return null; - } - moveBy(): void {} - hideCursor(): void {} - showCursor(): void {} - clearLine(): void {} - clearFromCursor(): void {} - clearScreen(): void {} - setTitle(): void {} - setProgress(): void {} - onAppearanceChange(): void {} - refreshAppearance(): void {} - get appearance(): undefined { - return undefined; - } -} - -/** The `tui` a custom component gets: a render request renders it for the host. */ -class HostTui extends TUI { - readonly #render: () => void; - #scheduled = false; - - constructor(render: () => void) { - super(new SinkTerminal()); - this.#render = render; - } - - override requestRender(): void { - if (this.#scheduled) return; - this.#scheduled = true; - queueMicrotask(() => { - this.#scheduled = false; - this.#render(); - }); - } -} - -/** Show a custom component with the host as its only screen (`ExtensionUIContext.custom`). */ -function showCustom(factory: CustomFactory, signal: AbortSignal | undefined): Promise { - const { promise, resolve, reject } = Promise.withResolvers(); - let component: ExtensionUiComponent | undefined; - let closed = false; - const tui = new HostTui(() => component?.render(CUSTOM_COLUMNS)); - const finish = (settle: () => void) => { - if (closed) return; - closed = true; - signal?.removeEventListener("abort", onAbort); - component?.dispose?.(); - settle(); - }; - const onAbort = () => finish(() => reject(signal?.reason ?? new DOMException("Dialog aborted", "AbortError"))); - if (signal?.aborted) { - onAbort(); - return promise; - } - signal?.addEventListener("abort", onAbort, { once: true }); - Promise.try(() => factory(tui, theme, KeybindingsManager.inMemory(), result => finish(() => resolve(result)))) - .then(created => { - if (closed) { - created.dispose?.(); - return; - } - component = created; - tui.requestRender(); - }) - .catch(err => finish(() => reject(err))); - return promise; -} - -/** The UI of a headless omp: dialogs go to the host alone, presentation calls become frames. */ -function hostUi(channel: HostChannel): ExtensionUIContext { - const { presenter } = channel; - return { - ...presenter, - supportsEditor: true, - select: (...args) => channel.dialog("select", args, () => undefined), - confirm: (...args) => channel.dialog("confirm", args, () => false), - input: (...args) => channel.dialog("input", args, () => undefined), - editor: (...args) => channel.dialog("editor", args, () => undefined), - askDialog: (...args) => channel.dialog("askDialog", args, timedOut => (timedOut ? timedOutAskDialogResult(args[0]) : undefined)), - formDialog: (...args) => channel.dialog("formDialog", args, () => undefined), - approve: (...args) => channel.dialog("approve", args, () => "deny" as const), - custom: (factory, options) => channel.custom(factory, mirrored => showCustom(mirrored, options?.signal)), - // There is no local editor to paste into. - pasteToEditor: text => presenter.setEditorText(text), - getEditorText: () => "", - onTerminalInput: () => () => {}, - rerenderMessage: () => {}, - refreshTranscript: () => {}, - getLinkClicksAvailable: () => false, - setFooter: () => {}, - setHeader: () => {}, - addAutocompleteProvider: () => {}, - get theme() { - return theme; - }, - getAllThemes: async () => [], - getTheme: async () => undefined, - setTheme: async () => ({ success: false, error: "Theme switching is not supported in headless omp" }), - getToolsExpanded: () => false, - setToolsExpanded: () => {}, - setEditorComponent: () => {}, - getEditorComponent: () => undefined, - }; -} - -/** `runRootCommand`'s session host for `--headless`: serve the host on stdio until stdin ends, then exit. */ -export async function runHeadless(session: AgentSession, host: SessionHostContext): Promise { - if (!stdin) throw new Error("runHeadless needs enterHeadless first"); - // Terminal notifications write BEL/OSC to the terminal, which is the channel here. - process.env.PI_NOTIFICATIONS = "off"; - const channel = HostChannel.stdio(session, host.subagentEventBus, writeChannel); - const ui = hostUi(channel); - host.setToolUIContext(ui, true); - // Read now: `session_start` handlers may wait on a dialog only the host can answer. - const reading = channel.read(stdin); - const exit = async (): Promise => { - await session.dispose().catch(err => logger.error("headless omp: dispose failed", { error: String(err) })); - process.exit(0); - }; - await initializeExtensions(session, { - // What extensions see: a UI-capable remote client, as they did under rpc-ui. - mode: "rpc", - uiContext: ui, - reportSendError: (action, err) => writeFrame({ t: "extension_error", action, error: err.message }), - reportRuntimeError: err => - writeFrame({ t: "extension_error", extensionPath: err.extensionPath, event: err.event, error: err.error }), - // shortcut: exits without waiting for a running command's `res`; queue behind it if a host needs that. - onShutdown: () => void exit(), - }); - channel.release(); - await reading.catch(err => logger.warn("headless omp: stdin failed", { error: String(err) })); - await exit(); -} - -function writeFrame(frame: { t: string } & Record): void { - writeChannel(`${JSON.stringify(frame)}\n`); -} diff --git a/runner/src/host-bridge.ts b/runner/src/host-bridge.ts deleted file mode 100644 index a31f87d34..000000000 --- a/runner/src/host-bridge.ts +++ /dev/null @@ -1,762 +0,0 @@ -/** - * The session-side halves of host tools, host URI schemes, `/btw`, dictation and live voice, built on - * omp's own engines (session tool registry, `InternalUrlRouter`, btw turns, `STTController`, - * `LiveSessionController`) and speaking the channel's `{t}` frames directly. Frame shapes: see - * `commands/tools.ts`. - */ -import type { AgentTool, AgentToolResult, AgentToolUpdateCallback, ToolLoadMode } from "@oh-my-pi/pi-agent-core"; -import type { AssistantMessage } from "@oh-my-pi/pi-ai"; -import { InternalUrlRouter } from "@oh-my-pi/pi-coding-agent/internal-urls/router"; -import type { InternalResource, InternalUrl, ProtocolHandler } from "@oh-my-pi/pi-coding-agent/internal-urls/types"; -import { LiveSessionController } from "@oh-my-pi/pi-coding-agent/live/controller"; -import { resolveLiveVoice } from "@oh-my-pi/pi-coding-agent/live/settings"; -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import { - type BtwHistoryRecord, - type BtwHistoryTurn, - BtwHistoryConflictError, - BtwHistoryStore, - getBtwLatestTurn, -} from "@oh-my-pi/pi-coding-agent/session/btw-history"; -import { beginBtwTurn, patchLatestBtwTurn, runBtwTurn } from "@oh-my-pi/pi-coding-agent/session/btw-turn"; -import { STTController, type SttCallbacks, type SttTarget } from "@oh-my-pi/pi-coding-agent/stt/stt-controller"; -import { isRecord, logger, Snowflake, toError } from "@oh-my-pi/pi-utils"; -import type { CommandContext } from "./commands/types"; - -type Send = CommandContext["send"]; - -// --------------------------------------------------------------------------------------------- -// Requests omp makes of the host (host tool calls, host URI reads/writes) -// --------------------------------------------------------------------------------------------- - -interface PendingRequest { - resolve(value: T): void; - reject(error: Error): void; - onUpdate?: AgentToolUpdateCallback; -} - -/** Frames sent under a fresh id that wait for the host's answer; an abort sends `{t: cancelType, targetId}`. */ -class HostRequests { - readonly #send: Send; - readonly #cancelType: string; - readonly #pending = new Map>(); - #closed: Error | undefined; - - constructor(send: Send, cancelType: string) { - this.#send = send; - this.#cancelType = cancelType; - } - - request(frame: { t: string } & Record, abortMessage: string, signal?: AbortSignal, onUpdate?: AgentToolUpdateCallback): Promise { - if (signal?.aborted) return Promise.reject(new Error(abortMessage)); - if (this.#closed) return Promise.reject(this.#closed); - const id: string = Snowflake.next(); - const { promise, resolve, reject } = Promise.withResolvers(); - const onAbort = () => { - if (!this.#pending.delete(id)) return; - this.#send({ t: this.#cancelType, id: Snowflake.next(), targetId: id }); - reject(new Error(abortMessage)); - }; - const done = () => signal?.removeEventListener("abort", onAbort); - signal?.addEventListener("abort", onAbort, { once: true }); - this.#pending.set(id, { - resolve: value => { - done(); - resolve(value); - }, - reject: error => { - done(); - reject(error); - }, - onUpdate, - }); - this.#send({ ...frame, id }); - return promise; - } - - get(id: string): PendingRequest | undefined { - return this.#pending.get(id); - } - - /** Removes and returns request `id` so the host's answer settles it once. */ - take(id: string): PendingRequest | undefined { - const pending = this.#pending.get(id); - this.#pending.delete(id); - return pending; - } - - /** Fails every pending request; `forever` also fails later ones (their adapters outlive this host). */ - close(message: string, forever: boolean): void { - let error = new Error(message); - if (forever) error = this.#closed ??= error; - const pending = [...this.#pending.values()]; - this.#pending.clear(); - for (const entry of pending) entry.reject(error); - } -} - -export interface HostToolDefinition { - name: string; - label: string; - description: string; - parameters: Record; - hidden: boolean; - loadMode: ToolLoadMode; - readsSkillUris?: boolean; -} - -function isToolResult(value: unknown): value is AgentToolResult { - return isRecord(value) && Array.isArray(value.content); -} - -export class HostToolBridge { - readonly #calls: HostRequests>; - - constructor(send: Send) { - this.#calls = new HostRequests(send, "host_tool_cancel"); - } - - /** Session tools whose every call runs on the host as a `host_tool_call`. */ - tools(definitions: HostToolDefinition[]): AgentTool[] { - return definitions.map(definition => ({ - ...definition, - strict: true, - concurrency: "shared", - execute: (toolCallId, params, signal, onUpdate) => - this.#calls.request( - { t: "host_tool_call", toolCallId, toolName: definition.name, arguments: params }, - `Host tool "${definition.name}" was aborted`, - signal, - onUpdate, - ), - })); - } - - result(frame: Record): void { - const { id, result, isError } = frame; - if (typeof id !== "string" || !isToolResult(result)) throw new Error("host_tool_result: needs a string id and a result with content[]"); - const pending = this.#calls.take(id); - if (!pending) return; - if (isError !== true) return pending.resolve(result); - const text = result.content - .flatMap(item => (item.type === "text" && typeof item.text === "string" ? [item.text] : [])) - .join("\n") - .trim(); - pending.reject(new Error(text || "Host tool execution failed")); - } - - update(frame: Record): void { - const { id, partialResult } = frame; - if (typeof id !== "string" || !isToolResult(partialResult)) throw new Error("host_tool_update: needs a string id and a partialResult with content[]"); - this.#calls.get(id)?.onUpdate?.(partialResult); - } - - close(message: string): void { - this.#calls.close(message, true); - } -} - -export interface HostUriSchemeDefinition { - scheme: string; - description?: string; - writable: boolean; - immutable: boolean; -} - -type ContentType = InternalResource["contentType"]; -const CONTENT_TYPES: Record = { "text/markdown": true, "application/json": true, "text/plain": true }; - -function isContentType(value: unknown): value is ContentType { - return typeof value === "string" && Object.hasOwn(CONTENT_TYPES, value); -} - -/** Lets the host own URI schemes: each is a handler in the process-wide router that asks the host. */ -export class HostUriBridge { - readonly #requests: HostRequests>; - readonly #router = InternalUrlRouter.instance(); - #schemes = new Set(); - - constructor(send: Send) { - this.#requests = new HostRequests(send, "host_uri_cancel"); - } - - /** Replaces the host's schemes; returns their normalized names. */ - setSchemes(definitions: HostUriSchemeDefinition[]): string[] { - const normalized = new Map(); - for (const definition of definitions) { - const scheme = definition.scheme.trim().toLowerCase(); - if (!scheme) throw new Error("Host URI scheme must be a non-empty string"); - if (!/^[a-z][a-z0-9+.-]*$/.test(scheme)) throw new Error(`Host URI scheme contains invalid characters: ${definition.scheme}`); - // Built-in schemes are omp's: shadowing one changes it process-wide, and close() would delete it. - if (this.#router.isBuiltin(scheme)) throw new Error(`Host URI scheme is reserved by OMP: ${scheme}://`); - normalized.set(scheme, { ...definition, scheme }); - } - for (const previous of this.#schemes) if (!normalized.has(previous)) this.#router.unregister(previous); - for (const definition of normalized.values()) this.#router.register(this.#handler(definition)); - this.#schemes = new Set(normalized.keys()); - return [...this.#schemes]; - } - - result(frame: Record): void { - if (typeof frame.id !== "string") throw new Error("host_uri_result: needs a string id"); - this.#requests.take(frame.id)?.resolve(frame); - } - - /** Takes the schemes out of the router and fails pending requests. */ - close(message: string): void { - for (const scheme of this.#schemes) this.#router.unregister(scheme); - this.#schemes.clear(); - this.#requests.close(message, false); - } - - #handler({ scheme, writable, immutable }: HostUriSchemeDefinition): ProtocolHandler { - return { - scheme, - spec: { - backing: "remote", - selectors: "none", - immutable, - write: writable ? { via: "handler", payload: "text", scope: "workspace", tier: () => "write" } : undefined, - }, - resolve: async (url, context) => { - const result = await this.#dispatch("read", url, undefined, context?.signal); - const content = typeof result.content === "string" ? result.content : ""; - const notes = Array.isArray(result.notes) ? result.notes.filter(note => typeof note === "string") : []; - return { - url: url.href, - content, - contentType: isContentType(result.contentType) ? result.contentType : "text/plain", - size: Buffer.byteLength(content, "utf-8"), - notes: notes.length > 0 ? notes : undefined, - immutable: typeof result.immutable === "boolean" ? result.immutable : immutable, - }; - }, - write: writable - ? async (url, content, context) => { - await this.#dispatch("write", url, content, context?.signal); - } - : undefined, - }; - } - - async #dispatch(operation: "read" | "write", url: InternalUrl, content: string | undefined, signal: AbortSignal | undefined): Promise> { - const frame: { t: string } & Record = { t: "host_uri_request", operation, url: url.href }; - if (operation === "write") frame.content = content ?? ""; - const result = await this.#requests.request(frame, `Host URI ${operation} for ${url.href} was aborted`, signal); - if (result.isError === true) { - const reason = [result.error, result.content].find(value => typeof value === "string" && value); - throw new Error(typeof reason === "string" ? reason : `Host URI ${operation} failed for ${url.href}`); - } - return result; - } -} - -// --------------------------------------------------------------------------------------------- -// `/btw` side questions -// --------------------------------------------------------------------------------------------- - -interface RunningBtw { - record: BtwHistoryRecord; - store: BtwHistoryStore; - abort: AbortController; -} - -const CANCELLED_WHILE_STARTING = "The /btw question was cancelled before it started"; - -/** The TUI-only `title` heading stays off the wire; hosts head a topic by its question. */ -function wireRecord({ title: _title, ...record }: BtwHistoryRecord): Omit { - return record; -} - -/** - * One side question at a time against the main session, mirroring the TUI's `BtwController` without - * its panels: answers stream as `btw_delta`, every turn is checkpointed into the session's BTW - * history sidecar (shared with the TUI), and each lifecycle change sends a `btw_record` snapshot. - */ -export class BtwBridge { - readonly #session: AgentSession; - readonly #send: Send; - #store: BtwHistoryStore | undefined; - /** `sessionId \0 artifactsDir` the store was opened for. */ - #storeKey: string | undefined; - #running: RunningBtw | undefined; - /** Topic of the `btw` command between validation and its first checkpoint (`null`: a new topic). */ - #startingTopic: string | null | undefined; - /** Bumped by every cancel and session change; a `btw` still starting from before gives up. */ - #epoch = 0; - /** Terminal checkpoint writes still in flight. */ - readonly #writes = new Set>(); - /** Terminal checkpoints that failed, keyed by record id (latest wins); retried before any move. */ - readonly #unsaved = new Map(); - - constructor(session: AgentSession, send: Send) { - this.#session = session; - this.#send = send; - } - - /** Starts a side question (or a follow-up in topic `recordId`); resolves once it is checkpointed as running. */ - async ask(question: string, recordId?: string): Promise> { - const trimmed = question.trim(); - if (!trimmed) throw new Error("btw requires a non-empty question"); - if (this.#startingTopic !== undefined || this.#running) throw new Error("A /btw question is still running; cancel it first"); - this.#startingTopic = recordId ?? null; - const epoch = this.#epoch; - try { - await this.#settleWrites(); - const store = await this.#openStore(true); - if (epoch !== this.#epoch) throw new Error(CANCELLED_WHILE_STARTING); - const previous = recordId === undefined ? undefined : store.getRecords().find(r => r.id === recordId); - if (recordId !== undefined && !previous) throw new Error(`Unknown /btw topic: ${recordId}`); - if (!this.#session.model) throw new Error("No active model available for /btw."); - const manager = this.#session.sessionManager; - await manager.ensureOnDisk(); - if (epoch !== this.#epoch) throw new Error(CANCELLED_WHILE_STARTING); - const { record, history, conversationKey } = beginBtwTurn(trimmed, manager.getLeafId(), previous); - try { - await store.upsert(record); - } catch (error) { - throw new Error(`Could not save /btw history: ${toError(error).message}`, { cause: error }); - } - const running: RunningBtw = { record, store, abort: new AbortController() }; - this.#running = running; - this.#send({ t: "btw_record", record: wireRecord(record) }); - if (epoch !== this.#epoch) { - // Cancelled or the session changed while the first checkpoint was written. - this.#finish(running, { status: "cancelled", updatedAt: Date.now() }); - throw new Error(CANCELLED_WHILE_STARTING); - } - // A macrotask: the `btw` result is written first, so it never trails this turn's frames. - setTimeout(() => void this.#run(running, trimmed, history, conversationKey), 0); - return wireRecord(record); - } finally { - this.#startingTopic = undefined; - } - } - - /** Cancels the running or starting question (only if it is topic `recordId`, when given). */ - cancel(recordId?: string): boolean { - const running = this.#running; - if (running) { - if (recordId !== undefined && running.record.id !== recordId) return false; - this.#epoch++; - this.#finish(running, { status: "cancelled", updatedAt: Date.now() }); - running.abort.abort(); - return true; - } - const starting = this.#startingTopic; - if (starting === undefined || (recordId !== undefined && starting !== recordId)) return false; - this.#epoch++; - return true; - } - - /** Newest first, re-read from disk when idle; a running topic carries its live partial answer. */ - async history(): Promise[]> { - const store = await this.#openStore(this.#running === undefined && this.#startingTopic === undefined); - const running = this.#running; - const records = new Map(store.getRecords().map(record => [record.id, record])); - for (const unsaved of this.#unsaved.values()) { - if (unsaved.store === store) records.set(unsaved.record.id, unsaved.record); - } - if (running?.store === store) records.set(running.record.id, running.record); - return [...records.values()].sort((a, b) => b.createdAt - a.createdAt || a.id.localeCompare(b.id)).map(wireRecord); - } - - /** - * Before the session is replaced or the process exits: cancels the running or starting question - * and lands every terminal checkpoint. Throws, after one retry, when a checkpoint still cannot be - * saved, so the caller keeps the session instead of losing that answer. - */ - async close(): Promise { - this.#epoch++; - this.cancel(); - await this.#settleWrites(); - } - - /** - * Awaits terminal checkpoints, retrying failed ones once against their original revision. A retry - * that conflicts (the topic changed or was removed on disk) can never succeed: the record is - * reported lost and dropped instead of blocking every later session change. - */ - async #settleWrites(): Promise { - while (this.#writes.size > 0) await Promise.all(this.#writes); - for (const [id, failed] of this.#unsaved) { - try { - await failed.store.retry(failed.record); - this.#unsaved.delete(id); - } catch (error) { - if (error instanceof BtwHistoryConflictError) { - this.#unsaved.delete(id); - const message = `/btw answer ${id} was not saved: ${error.message}; it changed or was removed on disk`; - logger.error(message); - this.#send({ t: "notice", level: "error", message, source: "btw-history" }); - continue; - } - throw new Error(`/btw history could not be saved: ${toError(error).message}. Fix the storage and retry; unsaved answers remain in this session.`, { - cause: error, - }); - } - } - } - - /** - * The current session's store with every queued checkpoint landed. `fresh` re-reads a persisted - * store from disk, picking up topics another process (the TUI) wrote; callers pass it only while - * nothing runs here. A store whose write failed (already reported) is replaced by a fresh read. - */ - async #openStore(fresh: boolean): Promise { - const manager = this.#session.sessionManager; - const artifactsDir = manager.getArtifactsDir() ?? undefined; - const key = `${manager.getSessionId()}\0${artifactsDir ?? ""}`; - const cached = this.#storeKey === key ? this.#store : undefined; - if (cached) { - try { - await cached.flush(); - // Without an artifacts dir the store is the only copy: never drop it. - if (!fresh || artifactsDir === undefined) return cached; - } catch { - // A failed checkpoint was already reported as a `notice`; re-read from disk below. - if ([...this.#unsaved.values()].some(unsaved => unsaved.store === cached)) return cached; - } - } else if (this.#running) { - // Replaced outside a host command (an extension switched sessions): the old session's - // question must not keep streaming into this one's view. - this.cancel(); - } - const store = await BtwHistoryStore.open(artifactsDir); - // A concurrent open for the same session already replaced the cache: keep one store. - if (this.#storeKey === key && this.#store && this.#store !== cached) return this.#store; - this.#store = store; - this.#storeKey = key; - return store; - } - - async #run(running: RunningBtw, question: string, history: readonly BtwHistoryTurn[] | undefined, conversationKey: string): Promise { - if (this.#running !== running) return; - try { - const { replyText } = await runBtwTurn(this.#session, { - question, - history, - conversationKey, - signal: running.abort.signal, - onTextDelta: delta => { - if (this.#running !== running) return; - const latest = getBtwLatestTurn(running.record); - running.record = patchLatestBtwTurn(running.record, { answer: latest.answer + delta, updatedAt: Date.now() }); - this.#send({ t: "btw_delta", recordId: running.record.id, delta }); - }, - }); - this.#finish(running, { answer: replyText, status: "complete", updatedAt: Date.now() }); - } catch (error) { - this.#finish(running, { status: "error", error: toError(error).message, updatedAt: Date.now() }); - } - } - - /** Records the turn's terminal state once; later outcomes of the same turn are ignored. */ - #finish(running: RunningBtw, patch: Partial): void { - if (this.#running !== running) return; - this.#running = undefined; - running.record = patchLatestBtwTurn(running.record, patch); - this.#send({ t: "btw_record", record: wireRecord(running.record) }); - const write = running.store.upsert(running.record).then( - () => { - this.#unsaved.delete(running.record.id); - }, - error => { - this.#unsaved.set(running.record.id, running); - const message = `Could not save /btw history: ${toError(error).message}`; - logger.error(message); - this.#send({ t: "notice", level: "error", message, source: "btw-history" }); - }, - ); - this.#writes.add(write); - void write.finally(() => this.#writes.delete(write)); - } -} - -// --------------------------------------------------------------------------------------------- -// Dictation: a draft-only microphone capture -// --------------------------------------------------------------------------------------------- - -interface Capture { - id: string; - controller: STTController; - committed: string; - volatile: string; - lastPreview: string; - error?: string; - stopping: boolean; - ended: boolean; - startDone: Promise; - stopPromise?: Promise<{ text: string }>; -} - -export class DictationBridge { - readonly #session: AgentSession; - readonly #send: Send; - #capture: Capture | undefined; - #disposed = false; - /** The latest `dictation_end` error, so a session change can tell an already-reported failure apart. */ - lastError: string | undefined; - - constructor(session: AgentSession, send: Send) { - this.#session = session; - this.#send = send; - } - - get active(): boolean { - return this.#capture !== undefined; - } - - /** Resolves only when the microphone is recording; stopping a pending start cancels it. */ - async start(id: string): Promise { - if (id.length > 128 || !id.trim()) throw new Error("captureId must be a non-blank string of at most 128 characters"); - if (this.#disposed) throw new Error("Dictation is unavailable"); - if (this.#capture) throw new Error("A dictation capture is already active"); - const { promise: startDone, resolve: resolveStart } = Promise.withResolvers(); - const session = this.#session; - const capture: Capture = { - id, - controller: new STTController({ settings: session.settings, registry: session.modelRegistry, getSessionId: () => session.sessionId }), - committed: "", - volatile: "", - lastPreview: "", - stopping: false, - ended: false, - startDone, - }; - this.#capture = capture; - const preview = () => { - if (capture.ended) return; - const text = capture.committed + capture.volatile; - if (text === capture.lastPreview) return; - capture.lastPreview = text; - this.#send({ t: "dictation_transcript", captureId: id, text }); - }; - const target: SttTarget = { - setVolatileText: text => { - if (capture.ended) return; - capture.volatile = text; - preview(); - }, - clearVolatileText: () => { - if (capture.ended) return; - capture.volatile = ""; - preview(); - }, - commitVolatileText: text => { - if (capture.ended) return; - capture.committed += text; - capture.volatile = ""; - preview(); - }, - submit: () => { - throw new Error("Dictation cannot submit a prompt"); - }, - deleteBeforeCursor: () => { - throw new Error("Dictation cannot edit a prompt"); - }, - }; - const callbacks: SttCallbacks = { - manualSubmit: true, - showStatus: () => {}, - showWarning: message => { - if (!capture.ended) capture.error = message; - }, - onStateChange: state => { - if (capture.ended) return; - if (state === "idle") { - // Capture errors report idle right before their warning: let both land before the one terminal frame. - queueMicrotask(() => { - if (!capture.stopping && !capture.ended) this.#finish(capture); - }); - } else { - this.#send({ t: "dictation_state", captureId: id, state }); - } - }, - }; - this.#send({ t: "dictation_state", captureId: id, state: "starting" }); - try { - if (!capture.stopping && !capture.ended) await capture.controller.start(target, callbacks); - if (capture.stopping || this.#disposed) throw new Error("Dictation start cancelled"); - if (capture.ended || capture.controller.state !== "recording") throw new Error(capture.error || "Failed to start dictation"); - } catch (cause) { - const error = toError(cause); - if (!capture.stopping && !capture.ended) { - capture.error ??= error.message; - this.#finish(capture); - } - throw error; - } finally { - resolveStart(); - } - } - - /** Stops the mic and flushes transcription, or returns empty text with no capture. */ - stop(): Promise<{ text: string }> { - const capture = this.#capture; - if (!capture) return Promise.resolve({ text: "" }); - if (capture.stopPromise) return capture.stopPromise; - capture.stopping = true; - capture.stopPromise = (async () => { - try { - // stop() cancels a pending preflight but returns before start settles; wait for that (and any flush) too. - await capture.controller.stop(); - await capture.startDone; - if (!capture.ended && capture.controller.state === "recording") await capture.controller.stop(); - } catch (cause) { - capture.error ??= toError(cause).message; - } - const text = capture.committed + capture.volatile; - this.#finish(capture); - if (capture.error) throw new Error(capture.error); - return { text }; - })(); - return capture.stopPromise; - } - - /** Closes the microphone at once when the host goes away. */ - dispose(): void { - this.#disposed = true; - const capture = this.#capture; - if (!capture) return; - capture.stopping = true; - this.#finish(capture); - } - - #finish(capture: Capture): void { - if (capture.ended) return; - capture.ended = true; - capture.controller.dispose(); - if (this.#capture === capture) this.#capture = undefined; - if (capture.error) this.lastError = capture.error; - this.#send({ t: "dictation_end", captureId: capture.id, text: capture.committed + capture.volatile, ...(capture.error ? { error: capture.error } : {}) }); - } -} - -// --------------------------------------------------------------------------------------------- -// Live voice -// --------------------------------------------------------------------------------------------- - -/** Minimum spacing between `live_levels` frames. */ -const LIVE_LEVELS_INTERVAL_MS = 100; - -/** Visible assistant text: text blocks only, no thinking or tool calls. */ -function visibleAssistantText(message: AssistantMessage): string { - let text = ""; - for (const content of message.content) if (content.type === "text") text += content.text; - return text.trim(); -} - -/** At most one GPT live voice session, bound to the session so delegated work runs with the host's tools. */ -export class LiveBridge { - readonly #session: AgentSession; - readonly #send: Send; - /** Set from construction until the controller has fully stopped (connecting, active, closing). */ - #controller: LiveSessionController | undefined; - #closing: Promise | undefined; - #pendingLevels: { input: number; output: number } | undefined; - #levelsTimer: NodeJS.Timeout | undefined; - #lastLevelsAt = Number.NEGATIVE_INFINITY; - - constructor(session: AgentSession, send: Send) { - this.#session = session; - this.#send = send; - } - - get active(): boolean { - return this.#controller !== undefined; - } - - /** Resolves once recording (or, with `sdpOffer`, once the call is signaled, with its SDP answer). */ - async start(options: { voice?: string; instructions?: string; sdpOffer?: string }): Promise<{ voice: string; sdpAnswer?: string }> { - if (this.#controller) throw new Error("A live session is already active"); - const voice = resolveLiveVoice(this.#session.settings, options.voice); - let terminated = false; - const controller = new LiveSessionController({ - session: this.#session, - extractAssistantText: visibleAssistantText, - voice, - instructions: options.instructions, - sdpOffer: options.sdpOffer, - callbacks: { - onPhase: phase => this.#send({ t: "live_phase", phase }), - onLevels: (input, output) => { - if (!terminated) this.#queueLevels(input, output); - }, - onTranscript: transcript => { - if (!transcript) return; - this.#send({ t: "live_transcript", role: transcript.role, turn: transcript.turn, text: transcript.text, final: transcript.final }); - }, - onTerminal: error => { - if (terminated) return; - terminated = true; - this.#flushLevels(); - this.#send(error ? { t: "live_end", error: error.message } : { t: "live_end" }); - void this.#retire(controller); - }, - }, - }); - this.#controller = controller; - try { - await controller.start(); - } catch (cause) { - await this.#retire(controller); - throw toError(cause); - } - if (options.sdpOffer === undefined) return { voice }; - const sdpAnswer = controller.sdpAnswer; - if (sdpAnswer === undefined) { - await this.#retire(controller); - throw new Error("The live session stopped before its SDP answer was available"); - } - return { voice, sdpAnswer }; - } - - async stop(): Promise { - if (this.#controller) await this.#retire(this.#controller); - } - - /** Sets (or toggles when omitted) microphone mute. */ - setMuted(muted?: boolean): { muted: boolean } { - const controller = this.#controller; - if (!controller || this.#closing) throw new Error("No live session is active"); - if (muted === undefined || muted !== controller.muted) controller.toggleMute(); - return { muted: controller.muted }; - } - - /** Stops `controller` and releases the slot once it has fully stopped. Idempotent. */ - #retire(controller: LiveSessionController): Promise { - if (this.#controller !== controller) return Promise.resolve(); - this.#closing ??= controller.stop().finally(() => { - if (this.#controller !== controller) return; - this.#controller = undefined; - this.#closing = undefined; - // Levels never follow `live_end`. - clearTimeout(this.#levelsTimer); - this.#levelsTimer = undefined; - this.#pendingLevels = undefined; - }); - return this.#closing; - } - - #queueLevels(input: number, output: number): void { - this.#pendingLevels = { input, output }; - if (this.#levelsTimer) return; - const wait = this.#lastLevelsAt + LIVE_LEVELS_INTERVAL_MS - Date.now(); - if (wait <= 0) return this.#flushLevels(); - this.#levelsTimer = setTimeout(() => { - this.#levelsTimer = undefined; - this.#flushLevels(); - }, wait); - } - - #flushLevels(): void { - clearTimeout(this.#levelsTimer); - this.#levelsTimer = undefined; - const levels = this.#pendingLevels; - if (!levels) return; - this.#pendingLevels = undefined; - this.#lastLevelsAt = Date.now(); - this.#send({ t: "live_levels", input: levels.input, output: levels.output }); - } -} diff --git a/runner/src/launch.ts b/runner/src/launch.ts deleted file mode 100644 index 19790b6a5..000000000 --- a/runner/src/launch.ts +++ /dev/null @@ -1,52 +0,0 @@ -/** - * omp's root `launch` command (`commands/launch.ts`), with two changes: the agent session factory - * hosts the session for ompweb, and `--headless` boots omp for `ompweb host` on stdio (see - * `headless.ts`). Keep it in step with upstream's copy on every omp bump. - */ -import { type Args, parseArgs, reportCliUsageError } from "@oh-my-pi/pi-coding-agent/cli/args"; -import { launchHelp } from "@oh-my-pi/pi-coding-agent/commands/launch-help"; -import { prepareAcpTerminalAuthArgs } from "@oh-my-pi/pi-coding-agent/modes/acp/terminal-auth"; -import { Command } from "@oh-my-pi/pi-utils/cli"; -import { enterHeadless, isHeadless, runHeadless } from "./headless"; -import { createHostedAgentSession } from "./session"; - -export default class Index extends Command { - static description = launchHelp.description; - static hidden = launchHelp.hidden; - static args = launchHelp.args; - static flags = launchHelp.flags; - static examples = launchHelp.examples; - - static strict = false; - - async run(): Promise { - const argv = this.argv.includes("--headless") ? enterHeadless(this.argv) : this.argv; - const { args } = prepareAcpTerminalAuthArgs(argv); - let parsed: Args; - try { - parsed = parseArgs(args); - } catch (error) { - if (reportCliUsageError(error)) { - process.exitCode = 2; - return; - } - throw error; - } - if (parsed.mode === "rpc" || parsed.mode === "rpc-ui") { - process.stderr.write("Error: ompweb-omp has no RPC mode; ompweb host starts it with --headless\n"); - process.exitCode = 2; - return; - } - // Startup boundary, as upstream: the session graph loads only once argv is accepted. - const { runRootCommand } = await import("@oh-my-pi/pi-coding-agent/main"); - try { - await runRootCommand(parsed, args, { - createAgentSession: createHostedAgentSession, - runSessionHost: isHeadless() ? runHeadless : undefined, - }); - } catch (error) { - if (reportCliUsageError(error)) process.exit(2); - throw error; - } - } -} diff --git a/runner/src/prompt-results.ts b/runner/src/prompt-results.ts deleted file mode 100644 index dce033924..000000000 --- a/runner/src/prompt-results.ts +++ /dev/null @@ -1,225 +0,0 @@ -/** - * `prompt_result` reporting: every accepted `prompt`/`abort_and_prompt` not answered with `agentInvoked: false` - * gets exactly one `{t:"prompt_result", id, agentInvoked, status, sessionSettled, error?}` once the work it caused - * has yielded: at once for local-only slash commands and failures, else after the terminal `agent_end` of the run - * the prompt started or joined. The host correlates on the cmd `id` instead of guessing who owns an `agent_end`. - */ -import type { AgentMessage } from "@oh-my-pi/pi-agent-core"; -import type { AssistantMessage } from "@oh-my-pi/pi-ai"; -import * as AIError from "@oh-my-pi/pi-ai/error"; -import { stripRawHttpRequestDiagnostics } from "@oh-my-pi/pi-ai/utils/http-inspector"; -import type { AgentSession, AgentSessionEvent } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import type { CostCapRefusal } from "@oh-my-pi/pi-coding-agent/session/cost-gate"; -import { isSessionSettled } from "@oh-my-pi/pi-coding-agent/session/session-settle"; -import { toError } from "@oh-my-pi/pi-utils"; -import type { CommandContext } from "./commands/types"; - -/** A prompt whose `prompt_result` is still owed; see {@link PromptResults.begin}. */ -export interface PromptTicket { - readonly id: number | undefined; -} - -interface PromptError { - message: string; - provider?: string; - model?: string; - httpStatus?: number; - /** Transient: resubmitting later may succeed. omp's own retries are already exhausted. */ - retryable: boolean; - code?: "cost_cap"; -} - -interface RunOutcome { - status: "completed" | "aborted" | "error"; - error?: PromptError; -} - -interface OpenPrompt { - /** `agent_start` count at acceptance; a later start begins a run this prompt may own. */ - startsAtBegin: number; - /** Outcome of the first run started after acceptance, once the agent yielded it. */ - ownOutcome?: RunOutcome; - /** Settled while another run was live: reported at the next yield that leaves nothing queued. */ - waiting: boolean; - /** The cost gate's latest refusal at acceptance; a newer one refused this prompt before dispatch. */ - refusalAtBegin: CostCapRefusal | undefined; -} - -/** - * Correlates accepted prompts with the run that carries their work. Fed every session event through - * {@link observe}. A prompt reports when the agent yields its work, not when the session is done (background - * jobs may wake it later; `sessionSettled` says which). A prompt dispatched as a fresh turn owns the first run - * started after it was accepted, so a late `agent_end` from an earlier run never settles it; one queued into a - * live run reports at the first yield after its message left the queue. - */ -export class PromptResults { - #agentStarts = 0; - #open = new Map(); - #refusalAtRunStart: CostCapRefusal | undefined; - readonly #session: AgentSession; - readonly #send: CommandContext["send"]; - readonly #scheduledTurn: () => boolean; - /** A prompt reported without a run (refused or aborted before dispatch): no `agent_end` re-checks settlement. */ - readonly #settledWithoutRun: () => void; - - constructor(session: AgentSession, send: CommandContext["send"], scheduledTurn: () => boolean, settledWithoutRun: () => void) { - this.#session = session; - this.#send = send; - this.#scheduledTurn = scheduledTurn; - this.#settledWithoutRun = settledWithoutRun; - } - - /** Open a ticket before the prompt starts any work. Close it with exactly one report or {@link discard}. */ - begin(id: number | undefined): PromptTicket { - const ticket: PromptTicket = { id }; - this.#open.set(ticket, { startsAtBegin: this.#agentStarts, refusalAtBegin: this.#session.costCapRefusal, waiting: false }); - return ticket; - } - - /** Drop a ticket whose command was rejected before it was accepted (no `prompt_result` is owed). */ - discard(ticket: PromptTicket): void { - this.#open.delete(ticket); - } - - /** The prompt's work reached the agent (dispatched or queued). Reports now if its own run already yielded. */ - settle(ticket: PromptTicket): void { - const open = this.#open.get(ticket); - if (!open) return; - if (open.ownOutcome) { - this.#report(ticket, true, open.ownOutcome); - } else if (this.#session.isStreaming || this.#agentStarts > open.startsAtBegin) { - // Queued into a live run, or its own run paused for agent-owned follow-up work (e.g. a retry). - open.waiting = true; - } else { - // Idle with no run since acceptance: the cost gate refused it, or an abort won the race before dispatch. - this.#report(ticket, true, this.#costCapOutcome(open.refusalAtBegin) ?? { status: "aborted" }); - this.#settledWithoutRun(); - } - } - - /** The prompt was handled locally without an agent turn. */ - completeLocal(ticket: PromptTicket): void { - this.#report(ticket, false, { status: "completed" }); - } - - /** The prompt failed before reaching the agent. */ - fail(ticket: PromptTicket, message: string): void { - this.#report(ticket, false, { status: "error", error: { message, retryable: false } }); - } - - /** - * Mark every open prompt aborted after a session transition: transitions detach the agent before aborting it, - * so the interrupted run never publishes a terminal `agent_end` for them. - */ - abortOpen(): void { - for (const [ticket, open] of this.#open) { - if (open.waiting) this.#report(ticket, true, { status: "aborted" }); - // No run started since acceptance: not detached; the command still running keeps its ticket. - else if (this.#agentStarts > open.startsAtBegin) open.ownOutcome ??= { status: "aborted" }; - } - } - - /** Track run boundaries; call after the event was sent so `prompt_result` follows its `agent_end`. */ - observe(event: AgentSessionEvent): void { - if (event.type === "agent_start") { - this.#agentStarts++; - this.#refusalAtRunStart = this.#session.costCapRefusal; - return; - } - if (event.type !== "agent_end" || this.#open.size === 0) return; - // Older sessions omit `yielded`; only their terminal ends were yields. - if (!(event.yielded ?? event.isTerminal !== false)) return; - // A request the cost gate refused during this run is its outcome, whatever message ended it. - const outcome = this.#costCapOutcome(this.#refusalAtRunStart) ?? runOutcome(event.messages); - // A still-queued steer/follow-up has not been read by the agent yet. - const queueDrained = this.#session.queuedMessageCount === 0; - for (const [ticket, open] of this.#open) { - if (open.waiting) { - if (queueDrained) this.#report(ticket, true, outcome); - } else if (!open.ownOutcome && this.#agentStarts > open.startsAtBegin) { - open.ownOutcome = outcome; - } - } - } - - #costCapOutcome(since: CostCapRefusal | undefined): RunOutcome | undefined { - const refusal = this.#session.costCapRefusal; - if (refusal === undefined || refusal === since) return undefined; - return { status: "error", error: { message: refusal.message, retryable: false, code: "cost_cap" } }; - } - - #report(ticket: PromptTicket, agentInvoked: boolean, outcome: RunOutcome): void { - if (!this.#open.delete(ticket)) return; - // Next macrotask: keeps prompt_result behind the cmd's own `res` and lets queue drains land before - // `sessionSettled` is read. - setImmediate(() => - this.#send({ - t: "prompt_result", - id: ticket.id, - agentInvoked, - status: outcome.status, - sessionSettled: isSessionSettled(this.#session, this.#scheduledTurn), - ...(outcome.error ? { error: outcome.error } : {}), - }), - ); - } -} - -/** Outcome of a run, read from its final assistant message. */ -function runOutcome(messages: readonly AgentMessage[]): RunOutcome { - for (let index = messages.length - 1; index >= 0; index--) { - const message = messages[index]; - if (message.role !== "assistant") continue; - if (message.stopReason === "error") return { status: "error", error: promptError(message) }; - if (message.stopReason === "aborted") return { status: "aborted" }; - return { status: "completed" }; - } - return { status: "completed" }; -} - -function promptError(message: AssistantMessage): PromptError { - const errorId = AIError.classifyMessage({ - api: message.api, - provider: message.provider, - model: message.model, - errorId: message.errorId, - errorMessage: message.errorMessage, - errorClassificationMessage: message.errorClassificationMessage, - errorStatus: message.errorStatus, - }); - const error: PromptError = { - message: message.errorMessage ? stripRawHttpRequestDiagnostics(message.errorMessage) : "Provider request failed", - provider: message.provider, - model: message.model, - retryable: AIError.is(errorId, AIError.Flag.Transient), - }; - if (message.errorStatus !== undefined) error.httpStatus = message.errorStatus; - return error; -} - -/** - * Start a prompt and route its resolution into its `prompt_result`: `false` completes locally, agent work - * settles through the run, a rejection goes to `onError` and a failed result. `startPrompt` receives the - * `onPromptAdmitted` callback; the returned promise resolves once the prompt is admitted or settles without - * admission, and never rejects. Await it to answer the cmd only after admission. - */ -export function reportPromptResult( - ticket: PromptTicket, - results: PromptResults, - onError: (error: Error) => void, - startPrompt: (onPromptAdmitted: () => void) => Promise, -): Promise { - const admitted = Promise.withResolvers(); - const prompt = startPrompt(admitted.resolve); - void prompt.then( - agentInvoked => (agentInvoked ? results.settle(ticket) : results.completeLocal(ticket)), - (cause: unknown) => { - const error = toError(cause); - onError(error); - results.fail(ticket, error.message); - }, - ); - const settled = () => admitted.resolve(); - void prompt.then(settled, settled); - return admitted.promise; -} diff --git a/runner/src/session.ts b/runner/src/session.ts deleted file mode 100644 index 1e6a49209..000000000 --- a/runner/src/session.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { - type CreateAgentSessionOptions, - type CreateAgentSessionResult, - createAgentSession, -} from "@oh-my-pi/pi-coding-agent/sdk"; -import { HostChannel } from "./channel"; -import { isHeadless } from "./headless"; - -/** - * omp's session factory, plus the `ompweb host` channel: the host receives the session's events - * and drives it through the same `AgentSession` the TUI uses, and dialogs open on both sides. - * A headless omp serves the host on stdio instead, from `headless.ts`. - */ -export async function createHostedAgentSession(options: CreateAgentSessionOptions = {}): Promise { - // A non-interactive launch throws when a resumed session's model cannot be restored; ompweb instead - // opens it on the default model, or none, so a logged-out user can still open old sessions and log in from them. - if (isHeadless()) return createAgentSession({ ...options, allowSessionModelFallback: true }); - const result = await createAgentSession(options); - // Only the TUI is a session the web may drive; `-p`, json and acp runs leave `hasUI` unset (rpc modes never reach here). - if (!options.hasUI) return result; - const channel = await HostChannel.connect(result.session, result.subagentEventBus); - return { - ...result, - setToolUIContext: (ui, hasUI) => result.setToolUIContext(channel.wrapUi(ui), hasUI), - }; -} diff --git a/runner/test/channel.test.ts b/runner/test/channel.test.ts deleted file mode 100644 index 3c718d188..000000000 --- a/runner/test/channel.test.ts +++ /dev/null @@ -1,308 +0,0 @@ -import * as fs from "node:fs"; -import * as os from "node:os"; -import * as path from "node:path"; -import { afterEach, describe, expect, it } from "bun:test"; -import type { ExtensionUIContext } from "@oh-my-pi/pi-coding-agent/extensibility/extensions/types"; -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import { SessionManager, type SessionPersistenceNotice } from "@oh-my-pi/pi-coding-agent/session/session-manager"; -import { HostChannel } from "../src/channel"; -import { createTestSession, type Frame, startHost, type TestHost, type TestSession } from "./harness"; - -let host: TestHost | undefined; -let test: TestSession | undefined; - -afterEach(async () => { - await test?.dispose(); - host?.close(); - test = undefined; - host = undefined; -}); - -const eventType = (frame: Frame) => { - const event = frame.event; - return frame.t === "event" && typeof event === "object" && event !== null && "type" in event ? event.type : undefined; -}; - -const FORM = '
'; - -/** A session whose first reply calls `ask` (a formDialog) and whose second ends the run. */ -const askSession = (ui?: Partial) => - createTestSession({ - toolNames: ["ask"], - responses: [{ content: [{ type: "toolCall", name: "ask", arguments: { html: FORM } }] }, { content: ["done"] }], - ui, - }); - -describe("ompweb channel", () => { - it("says hello and answers get_state", async () => { - host = await startHost(); - test = await createTestSession(); - const hello = await host.connected; - expect(hello).toMatchObject({ proto: 1, sessionId: test.session.sessionId }); - - const res = await host.command("get_state"); - expect(res).toMatchObject({ - ok: true, - data: { sessionId: test.session.sessionId, model: { provider: "mock", id: "mock-model" }, isStreaming: false }, - }); - }); - - it("rejects unknown commands and unknown models", async () => { - host = await startHost(); - test = await createTestSession(); - await host.connected; - - expect(await host.command("no_such_op")).toMatchObject({ ok: false, error: "unknown command: no_such_op" }); - expect(await host.command("set_model", { provider: "nope", modelId: "nothing" })).toMatchObject({ - ok: false, - error: "Model not found: nope/nothing", - }); - expect(await host.command("set_model", { provider: 1 })).toMatchObject({ ok: false }); - }); - - it("streams a prompt's run from agent_start to agent_end", async () => { - host = await startHost(); - test = await createTestSession({ responses: [{ content: ["scripted reply"] }] }); - await host.connected; - - expect(await host.command("prompt", { message: "hello" })).toMatchObject({ ok: true }); - const end = await host.waitFor(frame => eventType(frame) === "agent_end"); - // `available_commands_update` comes from an async subscription and may land first. - const types = host.frames.map(eventType).filter(type => type && type !== "available_commands_update"); - expect(types[0]).toBe("agent_start"); - expect(types).toContain("message_end"); - expect(JSON.stringify(end)).toContain("scripted reply"); - }); - - it("lets the host answer the ask tool's formDialog", async () => { - const html = '
'; - host = await startHost(); - test = await createTestSession({ - toolNames: ["ask"], - responses: [{ content: [{ type: "toolCall", name: "ask", arguments: { html } }] }, { content: ["done"] }], - }); - await host.connected; - - await host.command("prompt", { message: "ask me" }); - const request = await host.waitFor(frame => frame.t === "ui_request"); - expect(request).toMatchObject({ method: "formDialog", args: [html, {}] }); - - host.send({ t: "ui_response", id: request.id, value: { data: { q: "from host" } } }); - const end = await host.waitFor(frame => eventType(frame) === "tool_execution_end"); - expect(end.event).toMatchObject({ toolName: "ask", isError: false }); - expect(JSON.stringify(end.event)).toContain("from host"); - await host.waitFor(frame => eventType(frame) === "agent_end"); - expect(host.frames.some(frame => frame.t === "ui_end")).toBe(false); - }); - - it("closes the TUI's dialog when the host answers", async () => { - let localSignal: AbortSignal | undefined; - host = await startHost(); - test = await askSession({ - formDialog: (_html, options) => { - localSignal = options?.signal; - return new Promise(() => {}); - }, - }); - await host.connected; - - await host.command("prompt", { message: "ask me" }); - const request = await host.waitFor(frame => frame.t === "ui_request"); - host.send({ t: "ui_response", id: request.id, value: { data: { q: "from host" } } }); - await host.waitFor(frame => eventType(frame) === "agent_end"); - expect(localSignal?.aborted).toBe(true); - }); - - it("tells the host when the TUI answers", async () => { - host = await startHost(); - test = await askSession({ formDialog: async () => ({ data: { q: "from tui" } }) }); - await host.connected; - - await host.command("prompt", { message: "ask me" }); - const request = await host.waitFor(frame => frame.t === "ui_request"); - await host.waitFor(frame => frame.t === "ui_end" && frame.id === request.id); - const end = await host.waitFor(frame => eventType(frame) === "tool_execution_end"); - expect(JSON.stringify(end.event)).toContain("from tui"); - }); - - it("tells the host when the TUI's dialog fails", async () => { - host = await startHost(); - test = await askSession({ formDialog: async () => Promise.reject(new Error("tui gone")) }); - await host.connected; - - await host.command("prompt", { message: "ask me" }); - const request = await host.waitFor(frame => frame.t === "ui_request"); - await host.waitFor(frame => frame.t === "ui_end" && frame.id === request.id); - }); - - it("re-sends an open dialog to a redialled host", async () => { - host = await startHost(); - test = await askSession(); - const hello = await host.connected; - - await host.command("prompt", { message: "ask me" }); - const request = await host.waitFor(frame => frame.t === "ui_request"); - host.drop(); - await host.waitFor(frame => frame.t === "hello" && frame !== hello); - const again = await host.waitFor(frame => frame.t === "ui_request" && frame !== request); - expect(again).toMatchObject({ id: request.id, method: "formDialog" }); - - host.send({ t: "ui_response", id: request.id, value: { data: { q: "after redial" } } }); - const end = await host.waitFor(frame => eventType(frame) === "tool_execution_end"); - expect(JSON.stringify(end.event)).toContain("after redial"); - }); - - it("stops redialling once the host refuses", async () => { - host = await startHost(); - test = await createTestSession(); - await host.connected; - - host.send({ t: "refused", reason: "session live elsewhere" }); - host.drop(); - // Proving an absence: wait out one real redial interval (2 s) of the channel's own timer. - await Bun.sleep(2_500); - expect(host.frames.filter(frame => frame.t === "hello")).toHaveLength(1); - }); - - it("sends a relocation's notice before the moved session's identity", async () => { - // An in-memory session never relocates; capture the persistence-notice observers to raise one by hand. - const observers: ((notice: SessionPersistenceNotice) => void)[] = []; - const subscribe = SessionManager.prototype.onPersistenceNotice; - SessionManager.prototype.onPersistenceNotice = function (cb) { - observers.push(cb); - return subscribe.call(this, cb); - }; - try { - host = await startHost(); - test = await createTestSession(); - await host.connected; - } finally { - SessionManager.prototype.onPersistenceNotice = subscribe; - } - const to = path.join(os.tmpdir(), "moved.jsonl"); - test.session.sessionManager.getSessionFile = () => to; - for (const observer of observers) observer({ reason: "open-elsewhere", from: "/elsewhere/original.jsonl", to }); - - const identity = await host.waitFor(frame => frame.t === "session"); - expect(identity).toMatchObject({ sessionFile: to }); - const notice = host.frames.findIndex(frame => eventType(frame) === "notice"); - expect(notice).toBeGreaterThanOrEqual(0); - expect(notice).toBeLessThan(host.frames.indexOf(identity)); - }); - - it("does not dial a socket in a directory others can write", async () => { - host = await startHost(); - fs.chmodSync(path.dirname(process.env.OMPWEB_HOST_SOCKET ?? ""), 0o777); - test = await createTestSession(); - // Proving an absence: a dial would have said hello well within this. - await Bun.sleep(300); - expect(host.frames).toHaveLength(0); - }); -}); - -describe("headless dialogs", () => { - /** A stdio channel on `session`, as `headless.ts` serves it. */ - function stdio(session: AgentSession) { - const lines: Frame[] = []; - const channel = HostChannel.stdio(session, undefined, line => lines.push(JSON.parse(line) as Frame)); - const { readable, writable } = new TransformStream(); - const writer = writable.getWriter(); - void channel.read(readable.getReader()); - const send = (frame: Frame) => writer.write(new TextEncoder().encode(`${JSON.stringify(frame)}\n`)); - const ends = () => lines.filter(frame => frame.t === "ui_end").map(frame => frame.id); - return { channel, lines, send, ends, close: () => writer.close() }; - } - const requestOf = (lines: Frame[]) => lines.find(frame => frame.t === "ui_request"); - - it("returns the host's answer", async () => { - test = await createTestSession(); - const io = stdio(test.session); - const answer = io.channel.dialog("confirm", ["T", "M"], () => false); - await io.send({ t: "ui_response", id: requestOf(io.lines)?.id, value: true }); - expect(await answer).toBe(true); - expect(io.ends()).toEqual([]); - }); - - it("closes on abort with the fallback and tells the host", async () => { - test = await createTestSession(); - const io = stdio(test.session); - const abort = new AbortController(); - const answer = io.channel.dialog("confirm", ["T", "M", { signal: abort.signal }], timedOut => `fallback ${timedOut}`); - const request = requestOf(io.lines); - expect(request?.args).toEqual(["T", "M", {}]); - abort.abort(); - expect(await answer).toBe("fallback false"); - expect(io.ends()).toEqual([request?.id]); - }); - - it("closes on timeout with the fallback and tells the host", async () => { - test = await createTestSession(); - const io = stdio(test.session); - let timedOutCalls = 0; - const answer = io.channel.dialog("confirm", ["T", "M", { timeout: 20, onTimeout: () => timedOutCalls++ }], timedOut => `fallback ${timedOut}`); - expect(await answer).toBe("fallback true"); - expect(timedOutCalls).toBe(1); - expect(io.ends()).toEqual([requestOf(io.lines)?.id]); - }); - - it("falls back when the host goes away", async () => { - test = await createTestSession(); - const io = stdio(test.session); - const answer = io.channel.dialog("confirm", ["T", "M"], timedOut => `fallback ${timedOut}`); - await io.close(); - expect(await answer).toBe("fallback false"); - expect(io.ends()).toEqual([]); - }); -}); - -describe("edit_message", () => { - async function settledSession() { - host = await startHost(); - test = await createTestSession({ responses: [{ content: ["first reply"] }] }); - await host.connected; - await host.command("prompt", { message: "first" }); - await host.waitFor(frame => eventType(frame) === "agent_end"); - await test.session.waitForIdle(); - const entries = test.session.sessionManager.getEntries(); - const user = entries.find(entry => entry.type === "message" && entry.message.role === "user"); - const assistant = entries.find(entry => entry.type === "message" && entry.message.role === "assistant"); - if (!user || !assistant) throw new Error("the run left no user or assistant message"); - return { user, assistant }; - } - - it("moves the leaf before a user message and records the navigation", async () => { - const { user } = await settledSession(); - const res = await host?.command("edit_message", { entryId: user.id }); - const leafId = test?.session.sessionManager.getLeafId(); - expect(res).toMatchObject({ ok: true, data: { sessionId: test?.session.sessionId, cancelled: false, leafId } }); - const leaf = test?.session.sessionManager.getEntry(leafId ?? ""); - expect(leaf).toMatchObject({ type: "custom", customType: "omp-web-history-navigation", data: { entryId: user.id } }); - expect(leaf?.parentId).toBe(user.parentId); - }); - - it("refuses a target that is not a user message", async () => { - const { assistant } = await settledSession(); - const leafBefore = test?.session.sessionManager.getLeafId(); - for (const entryId of [assistant.id, "no-such-entry"]) { - expect(await host?.command("edit_message", { entryId })).toMatchObject({ - ok: false, - error: "The edit target must be a user message", - }); - } - expect(test?.session.sessionManager.getLeafId()).toBe(leafBefore); - }); - - it("refuses while a run is in flight", async () => { - host = await startHost(); - test = await askSession(); - await host.connected; - await host.command("prompt", { message: "ask me" }); - const request = await host.waitFor(frame => frame.t === "ui_request"); - const user = test.session.sessionManager - .getEntries() - .find(entry => entry.type === "message" && entry.message.role === "user"); - expect(await host.command("edit_message", { entryId: user?.id })).toMatchObject({ ok: false, code: "session_busy" }); - host.send({ t: "ui_response", id: request.id, value: { data: { q: "done" } } }); - await host.waitFor(frame => eventType(frame) === "agent_end"); - }); -}); diff --git a/runner/test/correlation.test.ts b/runner/test/correlation.test.ts deleted file mode 100644 index 1c7132657..000000000 --- a/runner/test/correlation.test.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { afterEach, describe, expect, it } from "bun:test"; -import * as os from "node:os"; -import * as path from "node:path"; -import type { AgentSessionEvent } from "@oh-my-pi/pi-coding-agent/session/agent-session-events"; -import { isRecord } from "@oh-my-pi/pi-utils"; -import { - CLIENT_MESSAGE_ENTRY_CUSTOM_TYPE, - claimClientMessage, - getClientMessageStatus, - onClientQueueChange, - stampClientMessageId, - submitCorrelated, -} from "../src/correlation"; -import { sessionState } from "../src/commands/model"; -import { createTestSession, type TestSession } from "./harness"; - -let test: TestSession | undefined; - -afterEach(async () => { - await test?.dispose(); - test = undefined; -}); - -type Stamped = AgentSessionEvent & { clientMessageId?: string; clientMessageIds?: unknown }; - -const ORIGIN = { - v: 1, - kind: "session", - sentAt: "2026-11-15T12:41:00.000Z", - session: { - id: "source-session", - name: "Source", - hostId: "local", - hostLabel: "This machine", - tool: "send_to_session", - delivery: "prompt", - }, -} as const; - -// The scripted model's `delayMs` keeps the first run streaming while input is queued behind it. -async function correlated(responses: { content: string[]; delayMs?: number }[]) { - process.env.OMPWEB_HOST_SOCKET = path.join(os.tmpdir(), `ompweb-correlation-test-${process.pid}.sock`); - test = await createTestSession({ responses }); - const session = test.session; - const events: Stamped[] = []; - const started = Promise.withResolvers(); - session.subscribe(event => { - events.push(stampClientMessageId(session, event)); - if (event.type === "agent_start") started.resolve(); - }); - onClientQueueChange(session, event => events.push(event)); - return { session, events, started: started.promise }; -} - -const userEnds = (events: Stamped[]) => - events.flatMap(event => - event.type === "message_end" && event.message.role === "user" ? [{ id: event.clientMessageId, message: event.message }] : [], - ); - -describe("client message correlation", () => { - it("stamps an idle prompt, journals its entry, reports it persisted and rejects reuse", async () => { - const { session, events } = await correlated([{ content: ["first"] }, { content: ["second"] }]); - const claim = claimClientMessage(session, "intent-1", ORIGIN); - await submitCorrelated(claim, undefined, hooks => session.prompt("same text", hooks), true); - await session.prompt("same text"); - - const ends = userEnds(events); - expect(ends.map(end => end.id)).toEqual(["intent-1", undefined]); - const start = events.find(event => event.type === "message_start" && event.message.role === "user"); - expect(start?.clientMessageId).toBe("intent-1"); - expect((start?.message as Record)?.origin).toEqual(ORIGIN); - - const status = await getClientMessageStatus(session, ["intent-1", "unknown"]); - expect(status.persistedClientMessageIds).toEqual(["intent-1"]); - expect(status.messages.map(item => item.clientMessageId)).toEqual(["intent-1"]); - expect((status.messages[0]?.message as Record)?.origin).toEqual(ORIGIN); - expect(status.unresolvedClientMessageIds).toEqual(["unknown"]); - - const entries = session.sessionManager.getEntries(); - const link = entries.find(entry => entry.type === "custom" && entry.customType === CLIENT_MESSAGE_ENTRY_CUSTOM_TYPE); - const data = link?.type === "custom" && isRecord(link.data) ? link.data : undefined; - const target = entries.find(entry => entry.id === data?.entryId); - expect(data?.clientMessageId).toBe("intent-1"); - expect(data?.origin).toEqual(ORIGIN); - expect(target?.type === "message" && target.message.role === "user").toBe(true); - expect(target?.type === "message" && "origin" in target.message).toBe(false); - // The model never sees the ID. - expect(JSON.stringify(session.messages)).not.toContain("intent-1"); - expect(JSON.stringify(session.messages)).not.toContain('"origin"'); - - expect(() => claimClientMessage(session, "intent-1")).toThrow("already submitted"); - expect(() => claimClientMessage(session, "")).toThrow("nonempty"); - }); - - it("aligns a queued follow-up's ID in an identity-only queue_update, then on its delivery", async () => { - const { session, events, started } = await correlated([{ content: ["busy"], delayMs: 200 }, { content: ["after"] }]); - const run = session.prompt("start"); - await started; - const claim = claimClientMessage(session, "queued-1", ORIGIN); - await submitCorrelated(claim, undefined, hooks => session.followUp("later", undefined, hooks)); - - const updates = events.filter(event => event.type === "queue_update"); - expect(updates.at(-1)).toMatchObject({ - followUp: ["later"], - clientMessageIds: { steering: [], followUp: ["queued-1"] }, - messageOrigins: { steering: [], followUp: [ORIGIN] }, - }); - const status = await getClientMessageStatus(session, ["queued-1"]); - expect(status.queuedMessages.clientMessageIds?.followUp).toEqual(["queued-1"]); - expect(status.queuedMessages.messageOrigins?.followUp).toEqual([ORIGIN]); - expect(status.unresolvedClientMessageIds).toEqual([]); - expect(sessionState(session).queuedMessages.messageOrigins?.followUp).toEqual([ORIGIN]); - - await run; - await session.waitForIdle(); - expect(userEnds(events).map(end => end.id)).toEqual([undefined, "queued-1"]); - expect((await getClientMessageStatus(session, ["queued-1"])).persistedClientMessageIds).toEqual(["queued-1"]); - }); - - it("journals a withdrawn queued message as resolved", async () => { - const { session, started } = await correlated([{ content: ["busy"], delayMs: 200 }]); - const run = session.prompt("start"); - await started; - const claim = claimClientMessage(session, "withdrawn-1"); - await submitCorrelated(claim, undefined, hooks => session.followUp("take me back", undefined, hooks)); - expect(session.removeQueuedMessage("take me back", "followUp")).toBe(true); - - const status = await getClientMessageStatus(session, ["withdrawn-1"]); - expect(status.resolvedClientMessageIds).toEqual(["withdrawn-1"]); - expect(status.unresolvedClientMessageIds).toEqual([]); - expect(() => claimClientMessage(session, "withdrawn-1")).toThrow("already resolved"); - await run; - }); - - it("frees a released ID and resolves a locally consumed one", async () => { - const { session } = await correlated([]); - claimClientMessage(session, "cancelled")?.release(); - claimClientMessage(session, "consumed")?.consumed(); - const status = await getClientMessageStatus(session, ["cancelled", "consumed"]); - expect(status.resolvedClientMessageIds).toEqual(["consumed"]); - expect(status.unresolvedClientMessageIds).toEqual(["cancelled"]); - expect(claimClientMessage(session, "cancelled")?.id).toBe("cancelled"); - }); - - it("requires a valid bounded origin to have a client message ID", async () => { - const { session } = await correlated([]); - expect(() => claimClientMessage(session, undefined, ORIGIN)).toThrow("origin requires clientMessageId"); - expect(() => claimClientMessage(session, "oversized", { - v: 1, kind: "future", sentAt: ORIGIN.sentAt, padding: "x".repeat(4096), - })).toThrow("origin is invalid or larger than 4096 bytes"); - }); -}); diff --git a/runner/test/events.test.ts b/runner/test/events.test.ts deleted file mode 100644 index fe7f2b366..000000000 --- a/runner/test/events.test.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { afterEach, describe, expect, it } from "bun:test"; -import * as os from "node:os"; -import * as path from "node:path"; -import { eventsCommands, subscribeHostEvents } from "../src/events"; -import { createTestSession, type Frame, type TestSession } from "./harness"; - -let test: TestSession | undefined; -let unsubscribe: (() => void) | undefined; - -afterEach(async () => { - unsubscribe?.(); - await test?.dispose(); - test = undefined; - unsubscribe = undefined; -}); - -type Event = { type: string } & Record; - -async function bridged(responses: { content: string[] }[] = []) { - // No host listens here: the session's own channel stays closed and only this bridge reports. - process.env.OMPWEB_HOST_SOCKET = path.join(os.tmpdir(), `ompweb-events-test-${process.pid}.sock`); - test = await createTestSession({ responses }); - const session = test.session; - const events: Event[] = []; - const waiters = new Map void>(); - const send = (frame: Frame) => { - expect(frame.t).toBe("event"); - const event = frame.event as Event; - events.push(event); - waiters.get(event.type)?.(); - }; - const waitFor = (type: string) => { - const { promise, resolve } = Promise.withResolvers(); - if (events.some(event => event.type === type)) resolve(); - else waiters.set(type, resolve); - return promise; - }; - unsubscribe = subscribeHostEvents(session, send); - const filter = (args: Record) => eventsCommands.set_event_filter({ session, send }, args); - return { session, events, waitFor, filter }; -} - -describe("host events bridge", () => { - it("stamps message ids, sends deltas by default, and settles after the run", async () => { - const { session, events, waitFor } = await bridged([{ content: ["scripted reply"] }]); - await session.prompt("hello"); - await waitFor("session_settled"); - await waitFor("available_commands_update"); - - const types = events.map(event => event.type); - expect(types.indexOf("session_settled")).toBeGreaterThan(types.lastIndexOf("agent_end")); - - const assistantEnd = events.find(event => event.type === "message_end" && JSON.stringify(event).includes("scripted reply")); - const updates = events.filter(event => event.type === "message_update"); - expect(updates.length).toBeGreaterThan(0); - for (const update of updates) { - expect(update).toMatchObject({ messageId: assistantEnd?.messageId, message: { role: "assistant" } }); - expect(Object.keys(update.message as object)).toEqual(["role"]); - } - expect(JSON.stringify(updates)).not.toContain('"partial"'); - }); - - it("sends the whole partial in full mode and validates the filter", async () => { - const { session, events, waitFor, filter } = await bridged([{ content: ["full reply"] }]); - expect(await filter({ events: null, messageUpdates: "full" })).toEqual({ events: null, messageUpdates: "full" }); - await expect(filter({ events: [""], messageUpdates: "full" })).rejects.toThrow("events must be null"); - await expect(filter({ events: null, messageUpdates: "some" })).rejects.toThrow("messageUpdates"); - - await session.prompt("hello"); - await waitFor("agent_end"); - expect(events.find(event => event.type === "message_update")).toMatchObject({ - message: { role: "assistant", content: expect.any(Array) }, - }); - }); - - it("reports session renames", async () => { - const { session, events, waitFor } = await bridged(); - session.sessionManager.setSessionName("renamed", "user"); - await waitFor("session_info_update"); - expect(events.find(event => event.type === "session_info_update")).toMatchObject({ - title: "renamed", - sessionId: session.sessionId, - }); - }); -}); diff --git a/runner/test/harness.ts b/runner/test/harness.ts deleted file mode 100644 index 141fc1e9a..000000000 --- a/runner/test/harness.ts +++ /dev/null @@ -1,221 +0,0 @@ -/** - * In-process channel harness: a fake `ompweb host` on a temporary Unix socket, and a real omp - * `AgentSession` (the runner's own factory) answering from a scripted mock model. No network, no - * paid model calls. - * - * const host = await startHost(); - * const { session } = await createTestSession({ responses: [{ content: ["hi"] }] }); - * await host.connected; - * const res = await host.command("get_state"); - */ -import * as fs from "node:fs"; -import * as os from "node:os"; -import * as path from "node:path"; -import { createMockModel, type MockModel, type MockResponse } from "@oh-my-pi/pi-ai/providers/mock"; -import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; -import { ModelRegistry } from "@oh-my-pi/pi-coding-agent/config/model-registry"; -import type { ExtensionUIContext } from "@oh-my-pi/pi-coding-agent/extensibility/extensions/types"; -import { AuthStorage, SqliteAuthCredentialStore } from "@oh-my-pi/pi-coding-agent/session/auth-storage"; -import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session"; -import { SessionManager } from "@oh-my-pi/pi-coding-agent/session/session-manager"; -import { theme } from "@oh-my-pi/pi-tui/theme"; -import { Database } from "bun:sqlite"; -import { createHostedAgentSession } from "../src/session"; - -export type Frame = { t: string } & Record; -export type Res = { t: "res"; id: number; ok: boolean; data?: unknown; error?: string; code?: string }; - -export interface TestHost { - /** Every frame omp sent, in order. */ - readonly frames: Frame[]; - /** Resolves with omp's `hello` once a session connects. */ - readonly connected: Promise; - /** Send `{t:"cmd"}` and resolve with its `res`. */ - command(op: string, args?: Record): Promise; - /** Send any other frame (e.g. `ui_response`). */ - send(frame: Frame): void; - /** Resolve with the first frame (already received or future) that matches. */ - waitFor(match: (frame: Frame) => boolean, timeoutMs?: number): Promise; - close(): void; - /** End omp's connection but keep listening, as a restarted host would. */ - drop(): void; -} - -/** - * Listen on a fresh socket and point `OMPWEB_HOST_SOCKET` at it, so the next session created - * connects here. Call before `createTestSession`. - */ -export async function startHost(): Promise { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ompweb-runner-test-")); - const socketPath = path.join(dir, "host.sock"); - process.env.OMPWEB_HOST_SOCKET = socketPath; - - const frames: Frame[] = []; - const waiters = new Set<{ match: (frame: Frame) => boolean; resolve: (frame: Frame) => void }>(); - const hello = Promise.withResolvers(); - let client: Bun.Socket | undefined; - let buffered = ""; - let nextId = 1; - - const receive = (frame: Frame) => { - frames.push(frame); - if (frame.t === "hello") hello.resolve(frame); - for (const waiter of waiters) { - if (!waiter.match(frame)) continue; - waiters.delete(waiter); - waiter.resolve(frame); - } - }; - - const server = Bun.listen({ - unix: socketPath, - socket: { - open: socket => { - client = socket; - }, - data: (_socket, data) => { - buffered += data.toString(); - const lines = buffered.split("\n"); - buffered = lines.pop() ?? ""; - for (const line of lines) if (line.trim()) receive(JSON.parse(line) as Frame); - }, - }, - }); - - const send = (frame: Frame) => { - if (!client) throw new Error("no omp connected"); - client.write(`${JSON.stringify(frame)}\n`); - }; - - const waitFor = (match: (frame: Frame) => boolean, timeoutMs = 5000) => { - const seen = frames.find(match); - if (seen) return Promise.resolve(seen); - const { promise, resolve, reject } = Promise.withResolvers(); - const waiter = { match, resolve }; - waiters.add(waiter); - const timer = setTimeout(() => { - waiters.delete(waiter); - reject(new Error(`timed out waiting for a frame; got: ${frames.map(f => f.t).join(", ")}`)); - }, timeoutMs); - return promise.finally(() => clearTimeout(timer)); - }; - - return { - frames, - connected: hello.promise, - send, - waitFor, - async command(op, args) { - const id = nextId++; - send({ t: "cmd", id, op, args }); - const res = await waitFor(frame => frame.t === "res" && frame.id === id); - return res as Res; - }, - close() { - server.stop(true); - fs.rmSync(dir, { recursive: true, force: true }); - }, - drop() { - client?.end(); - }, - }; -} - -/** A local UI that answers no dialog until it is aborted, so the host's answer always wins. */ -function silentUi(): ExtensionUIContext { - const untilAborted = (signal: AbortSignal | undefined) => { - const { promise, resolve } = Promise.withResolvers(); - signal?.addEventListener("abort", () => resolve(undefined), { once: true }); - return promise; - }; - return { - select: async (_title, _options, options) => untilAborted(options?.signal), - confirm: async (_title, _message, options) => (await untilAborted(options?.signal)) ?? false, - input: async (_title, _placeholder, options) => untilAborted(options?.signal), - editor: async (_title, _prefill, options) => untilAborted(options?.signal), - formDialog: async (_html, options) => untilAborted(options?.signal), - notify: () => {}, - onTerminalInput: () => () => {}, - rerenderMessage: () => {}, - refreshTranscript: () => {}, - getLinkClicksAvailable: () => false, - setStatus: () => {}, - setWorkingMessage: () => {}, - setWidget: () => {}, - setFooter: () => {}, - setHeader: () => {}, - setTitle: () => {}, - custom: async () => { - throw new Error("custom UI is not available in tests"); - }, - setEditorText: () => {}, - pasteToEditor: () => {}, - getEditorText: () => "", - addAutocompleteProvider: () => {}, - setEditorComponent: () => {}, - getEditorComponent: () => undefined, - get theme() { - return theme; - }, - getAllThemes: () => Promise.resolve([]), - getTheme: () => Promise.resolve(undefined), - setTheme: () => Promise.resolve({ success: false, error: "UI not available" }), - getToolsExpanded: () => false, - setToolsExpanded: () => {}, - }; -} - -export interface TestSession { - session: AgentSession; - /** The scripted model; `mock.push(...)` adds replies, `mock.calls` records requests. */ - mock: MockModel; - dispose(): Promise; -} - -/** - * A real `AgentSession` from the runner's factory (so it connects to the host), in memory, on a - * mock model (`mock/mock-model`) that replies with `responses` in order. - */ -export async function createTestSession( - options: { responses?: MockResponse[]; toolNames?: string[]; ui?: Partial } = {}, -): Promise { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ompweb-runner-session-")); - const authStorage = new AuthStorage(new SqliteAuthCredentialStore(new Database(":memory:"))); - authStorage.keys.setRuntime("mock", "test-key"); - const modelRegistry = new ModelRegistry(authStorage); - const mock = createMockModel({ responses: options.responses ?? [] }); - const result = await createHostedAgentSession({ - cwd: dir, - agentDir: dir, - modelRegistry, - model: mock, - sessionManager: SessionManager.inMemory(dir), - settings: Settings.isolated({ - "compaction.enabled": false, - "retry.enabled": false, - "todo.enabled": false, - "todo.reminders": false, - }), - disableExtensionDiscovery: true, - skills: [], - contextFiles: [], - promptTemplates: [], - slashCommands: [], - enableMCP: false, - enableLsp: false, - skipPythonPreflight: true, - hasUI: true, - toolNames: options.toolNames ?? [], - }); - result.setToolUIContext({ ...silentUi(), ...options.ui }, true); - result.session.agent.streamFn = mock.stream; - return { - session: result.session, - mock, - async dispose() { - await result.session.dispose(); - authStorage.close(); - fs.rmSync(dir, { recursive: true, force: true }); - }, - }; -} diff --git a/runner/test/host-bridge.test.ts b/runner/test/host-bridge.test.ts deleted file mode 100644 index 202997b8d..000000000 --- a/runner/test/host-bridge.test.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { describe, expect, it } from "bun:test"; -import { parseInternalUrl } from "@oh-my-pi/pi-coding-agent/internal-urls/parse"; -import { InternalUrlRouter } from "@oh-my-pi/pi-coding-agent/internal-urls/router"; -import { HostToolBridge, HostUriBridge } from "../src/host-bridge"; - -type Frame = { t: string } & Record; - -function recorder() { - const frames: Frame[] = []; - return { frames, send: (frame: Frame) => frames.push(frame) }; -} - -const definition = { name: "echo", label: "echo", description: "Echo", parameters: { type: "object" }, hidden: false, loadMode: "discoverable" as const }; - -describe("HostToolBridge", () => { - it("runs calls on the host, streams updates, and settles on the result", async () => { - const { frames, send } = recorder(); - const bridge = new HostToolBridge(send); - const [tool] = bridge.tools([definition]); - const updates: unknown[] = []; - const call = tool.execute("tc1", { a: 1 }, undefined, update => updates.push(update)); - expect(frames[0]).toMatchObject({ t: "host_tool_call", toolCallId: "tc1", toolName: "echo", arguments: { a: 1 } }); - const id = frames[0].id; - bridge.update({ t: "host_tool_update", id, partialResult: { content: [] } }); - bridge.result({ t: "host_tool_result", id, result: { content: [{ type: "text", text: "ok" }] } }); - expect(await call).toEqual({ content: [{ type: "text", text: "ok" }] }); - expect(updates).toEqual([{ content: [] }]); - expect(() => bridge.result({ t: "host_tool_result", id })).toThrow("host_tool_result"); - }); - - it("rejects an error result with its text, cancels on abort, and fails after close", async () => { - const { frames, send } = recorder(); - const bridge = new HostToolBridge(send); - const [tool] = bridge.tools([definition]); - const failing = tool.execute("tc1", {}); - bridge.result({ t: "host_tool_result", id: frames[0].id, isError: true, result: { content: [{ type: "text", text: "boom" }] } }); - await expect(failing).rejects.toThrow("boom"); - - const abort = new AbortController(); - const aborted = tool.execute("tc2", {}, abort.signal); - abort.abort(); - await expect(aborted).rejects.toThrow('Host tool "echo" was aborted'); - expect(frames[2]).toMatchObject({ t: "host_tool_cancel", targetId: frames[1].id }); - - const pending = tool.execute("tc3", {}); - bridge.close("gone"); - await expect(pending).rejects.toThrow("gone"); - await expect(tool.execute("tc4", {})).rejects.toThrow("gone"); - }); -}); - -describe("HostUriBridge", () => { - it("serves reads from the host and leaves the router on close", async () => { - const { frames, send } = recorder(); - const bridge = new HostUriBridge(send); - expect(bridge.setSchemes([{ scheme: " Notes ", writable: false, immutable: true }])).toEqual(["notes"]); - const handler = InternalUrlRouter.instance().getHandler("notes"); - if (!handler) throw new Error("notes:// not registered"); - expect(handler.write).toBeUndefined(); - const read = handler.resolve(parseInternalUrl("notes://a")); - expect(frames[0]).toMatchObject({ t: "host_uri_request", operation: "read", url: "notes://a" }); - bridge.result({ t: "host_uri_result", id: frames[0].id, content: "hi", notes: ["n"] }); - expect(await read).toMatchObject({ url: "notes://a", content: "hi", contentType: "text/plain", notes: ["n"], immutable: true, size: 2 }); - expect(() => bridge.setSchemes([{ scheme: "skill", writable: false, immutable: false }])).toThrow("reserved"); - bridge.close("gone"); - expect(InternalUrlRouter.instance().getHandler("notes")).toBeUndefined(); - }); -}); diff --git a/runner/test/tsconfig.json b/runner/test/tsconfig.json deleted file mode 100644 index 784e2190a..000000000 --- a/runner/test/tsconfig.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "extends": "../tsconfig.json", - "include": ["."] -} diff --git a/runner/tsconfig.json b/runner/tsconfig.json deleted file mode 100644 index 0b4566485..000000000 --- a/runner/tsconfig.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "compilerOptions": { - "target": "ESNext", - "module": "ESNext", - "moduleResolution": "bundler", - "strict": true, - "noEmit": true, - "skipLibCheck": true, - "types": ["bun"] - }, - "include": ["src", "scripts"] -} diff --git a/scripts/build-binary.ts b/scripts/build-binary.ts index 8a92a0722..e354fc785 100644 --- a/scripts/build-binary.ts +++ b/scripts/build-binary.ts @@ -1,11 +1,12 @@ -// `npm run build:binary [-- --target bun--]` (after the client build and the runner build, -// `bun runner/scripts/build-binary.ts -`): compiles server/binary.ts into release/ompweb--/ompweb -// (ompweb.exe for Windows) next to the files it serves, the omp runner it spawns (ompweb-omp; not with --no-runner, -// where ompweb fetches it from its release, lib/omp/runner-fetch.ts) and the licenses of what it redistributes, then +// `npm run build:binary [-- --target bun--]` (after the client build): compiles server/binary.ts into +// release/ompweb--/ompweb (ompweb.exe for Windows) next to the files it serves, the omp runner it spawns +// (ompweb-omp, downloaded from the fork release omp-runner.json pins: scripts/fetch-runner.ts; not with --no-runner, where +// ompweb fetches it from that release itself, lib/omp/runner-fetch.ts) and the licenses of what it redistributes, then // archives that directory as release/ompweb--.tar.gz (.zip for Windows). Default target: this machine. import { cpSync, existsSync, readFileSync, writeFileSync, rmSync } from "node:fs"; import path from "node:path"; import { parseArgs } from "node:util"; +import { pinnedRunner } from "./fetch-runner"; import { bundledPackageDirs, writeLicenses } from "./licenses"; import packageJson from "../package.json"; @@ -47,7 +48,7 @@ if (!result.success) process.exit(1); // The static root (server/binary.ts): the client, the public files, the KaTeX CSS and fonts (where the server // resolves the katex package), and the licenses of what ships. Beside the binary, unless --no-runner: the omp runner -// it spawns (`ompweb-omp`, lib/omp/omp-cli.ts), built first by runner/scripts/build-binary.ts. +// it spawns (`ompweb-omp`, lib/omp/omp-cli.ts), downloaded from the pinned fork release. const copy = (from: string, to: string) => cpSync(from, path.join(out, to), { recursive: true }); for (const file of ["dist", "public", "LICENSE"]) copy(path.join(root, file), file); const releaseTag = process.env.OMP_WEB_RELEASE_TAG || (process.env.GITHUB_REF_TYPE === "tag" ? process.env.GITHUB_REF_NAME : undefined); @@ -55,15 +56,7 @@ if (releaseTag) { if (!/^(?:v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?|\d{8}T\d{6}Z-[a-f0-9]{7,40})$/.test(releaseTag)) throw new Error("Invalid binary release tag"); writeFileSync(path.join(out, "build-info.json"), JSON.stringify({ version: packageJson.version, releaseTag }) + "\n"); } -if (!values["no-runner"]) { - const runnerTarget = name.slice("ompweb-".length); - const runner = path.join(root, "runner", "release", `ompweb-omp-${runnerTarget}${windows ? ".exe" : ""}`); - if (!existsSync(runner)) { - console.error(`No ${path.relative(root, runner)}: run \`bun runner/scripts/build-binary.ts ${runnerTarget}\` first.`); - process.exit(1); - } - copy(runner, windows ? "ompweb-omp.exe" : "ompweb-omp"); -} +if (!values["no-runner"]) copy(await pinnedRunner(name.slice("ompweb-".length)), windows ? "ompweb-omp.exe" : "ompweb-omp"); for (const file of new Bun.Glob("app/fonts/*-{OFL.txt,LICENSE.*}").scanSync(root)) copy(path.join(root, file), file); const katex = path.dirname(Bun.resolveSync("katex/package.json", root)); for (const file of ["package.json", "LICENSE", "dist/katex.min.css", "dist/fonts"]) copy(path.join(katex, file), `node_modules/katex/${file}`); diff --git a/scripts/build-release-binaries.sh b/scripts/build-release-binaries.sh index ac5794cb0..c892e9854 100755 --- a/scripts/build-release-binaries.sh +++ b/scripts/build-release-binaries.sh @@ -1,15 +1,13 @@ #!/bin/sh # The five binaries every release and prerelease ships (after `npm run build`), into release/: -# release/ompweb--.tar.gz, .zip for Windows, and the five omp runners, runner/release/ompweb-omp--. -# A prerelease archive holds its runner as ompweb-omp beside ompweb; with --no-runner (releases) it does not, and -# ompweb fetches it from the release instead, which attaches each runner (lib/omp/runner-fetch.ts). Bun -# cross-compiles all of them from one machine. +# release/ompweb--.tar.gz, .zip for Windows. A prerelease archive holds its runner as ompweb-omp beside ompweb, +# downloaded from the fork release omp-runner.json pins (scripts/fetch-runner.ts); with --no-runner (releases) it does +# not, and ompweb fetches it from that release instead (lib/omp/runner-fetch.ts). Bun cross-compiles all of them from +# one machine. # The x64 targets name -baseline (no AVX2 needed): in Bun 1.4.4 that is the default x64 runtime too (the # same bytes), and naming it keeps a later Bun from shipping a -modern one. They keep the plain # ompweb--x64 names. set -eu -# The pinned omp packages, then every platform's native addon package, which the runner embeds per target. -(cd runner && npx bun scripts/use-omp-build.ts && npx bun install --frozen-lockfile --os='*' --cpu='*' && npx bun scripts/build-binary.ts linux-x64 linux-arm64 darwin-x64 darwin-arm64 windows-x64) for target in bun-linux-x64-baseline bun-linux-arm64 bun-darwin-x64-baseline bun-darwin-arm64 bun-windows-x64-baseline; do npx bun scripts/build-binary.ts --target "$target" "$@" done diff --git a/scripts/fetch-runner.ts b/scripts/fetch-runner.ts new file mode 100644 index 000000000..0b5ed37b3 --- /dev/null +++ b/scripts/fetch-runner.ts @@ -0,0 +1,49 @@ +// `bun scripts/fetch-runner.ts [--latest] [- ...]`: downloads the omp runner (`ompweb-omp--[.exe]`, default +// this machine's) of the oh-my-pi fork release that omp-runner.json pins into release/runners//, checked against the +// release's ompweb-omp.sha256, and prints each file's path. A file already there is kept (its directory names the tag). +// --latest first rewrites omp-runner.json to the newest published fork release whose ompweb-omp.json names +// RUNNER_PROTOCOL and that attaches the runner of every platform: what publish and prerelease builds record (never committed back). +// Also the runner builds and smoke tests use: build-binary.ts, smoke.ts. +import { existsSync, writeFileSync } from "node:fs"; +import path from "node:path"; +import { parseArgs } from "node:util"; +import { fetchHostUpdateBytes } from "../lib/hosts/update-worker"; +import { OMP_RUNNER_PIN, OMP_TAG, RUNNER_PROTOCOL, runnerAsset } from "../lib/omp/omp-cli"; +import { downloadRunner, findRunnerRelease } from "../lib/omp/runner-fetch"; + +const root = path.join(import.meta.dir, ".."); +/** Every platform a release's runner is built for. */ +const RELEASE_TARGETS = ["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64", "windows-x64"]; +/** This machine's `-`. */ +export const thisTarget = `${process.platform === "win32" ? "windows" : process.platform}-${process.arch}`; + +/** `release/runners//` of `target`, downloaded (SHA-256-checked) unless already there. */ +export async function pinnedRunner(target = thisTarget, pin = OMP_RUNNER_PIN): Promise { + if (!OMP_TAG.test(pin.tag)) throw new Error(`omp-runner.json pins ${pin.tag}, which is not an omp build tag`); + const asset = runnerAsset(target); + const file = path.join(root, "release", "runners", pin.tag, asset); + if (!existsSync(file)) await downloadRunner(pin, asset, file); + return file; +} + +if (import.meta.main) { + const { values, positionals } = parseArgs({ args: Bun.argv.slice(2), options: { latest: { type: "boolean" } }, allowPositionals: true }); + const targets = positionals.length ? positionals : [thisTarget]; + let pin = OMP_RUNNER_PIN; + if (values.latest) { + // api.github.com rate-limits a shared CI address without a token; downloads (github.com) need none. + const token = process.env.GH_TOKEN ?? process.env.GITHUB_TOKEN; + const read = async (url: string, maxBytes: number, timeout: number): Promise => { + if (!token || !url.startsWith("https://api.github.com/")) return fetchHostUpdateBytes(url, maxBytes, timeout); + const response = await fetch(url, { headers: { Authorization: `Bearer ${token}`, "User-Agent": "ompweb-fetch-runner" }, signal: AbortSignal.timeout(timeout) }); + if (!response.ok) throw new Error(`${url}: HTTP ${response.status}`); + return Buffer.from(await response.arrayBuffer()); + }; + const found = await findRunnerRelease(pin.repo, RELEASE_TARGETS.map(runnerAsset), read); + if (!found) throw new Error(`${pin.repo} has no published release whose runner speaks protocol ${RUNNER_PROTOCOL} for every platform`); + pin = { repo: pin.repo, tag: found.tag }; + writeFileSync(path.join(root, "omp-runner.json"), `${JSON.stringify(pin, null, 2)}\n`); + console.error(`omp-runner.json: ${pin.repo} ${pin.tag}`); + } + for (const target of targets) console.log(await pinnedRunner(target, pin)); +} diff --git a/scripts/smoke.ts b/scripts/smoke.ts index 53d0d1768..572b8c6b7 100644 --- a/scripts/smoke.ts +++ b/scripts/smoke.ts @@ -2,9 +2,10 @@ // binary) on a free port with a throwaway HOME, waits for its automatic local host, and checks the shipped // bundle, the SPA, a hashed asset, the API, a session listed through the daemon, a live session started through the // omp runner (no model call) and the SSE stream, then stops both. -// : the repo root (after `npm run build` and `bun runner/scripts/build-binary.ts`) or a packed install; +// : the repo root (after `npm run build`) or a packed install; both run the runner of the fork release omp-runner.json +// pins, downloaded to release/runners/ (scripts/fetch-runner.ts) unless --omp names another; // : release/ompweb--/ompweb, whose directory holds the static files and the runner (ompweb-omp) unless -// built --no-runner. Prints one `ok ` / `FAIL : ` line per step. +// built --no-runner (then the pinned one, as above). Prints one `ok ` / `FAIL : ` line per step. import assert from "node:assert/strict"; import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; @@ -12,6 +13,7 @@ import path from "node:path"; import { parseArgs } from "node:util"; import { releaseRepo } from "../lib/release-repo"; import { isProcessAlive } from "../lib/process-identity"; +import { pinnedRunner } from "./fetch-runner"; const { values } = parseArgs({ args: Bun.argv.slice(2), options: { package: { type: "string" }, binary: { type: "string" }, omp: { type: "string" }, "second-server": { type: "boolean" } } }); const binary = values.binary && path.resolve(values.binary); @@ -91,13 +93,15 @@ const env: Record = { }; // OMP_WEB_OMP_BIN: an agent shell points it at its own omp; test this install's runner instead. for (const name of ["OMP_WEB_PASSWORD", "OMP_WEB_PASSWORD_HASH", "OMP_WEB_OMP_BIN"]) delete env[name]; -// --omp : a packed install, or a --no-runner binary, fetches its runner from its release, which a build under -// test has not published yet (lib/omp/runner-fetch.ts). +// --omp , else the pinned release's runner: a packed install, or a --no-runner binary, would fetch the same one itself +// (lib/omp/runner-fetch.ts), into its throwaway HOME. Only a binary that carries its own (ompweb-omp beside it) uses that. +const bundled = binary && existsSync(path.join(dir, process.platform === "win32" ? "ompweb-omp.exe" : "ompweb-omp")); if (values.omp) env.OMP_WEB_OMP_BIN = path.resolve(values.omp); +else if (!bundled) env.OMP_WEB_OMP_BIN = await pinnedRunner(); // A private Windows runner lets cleanup identify this smoke's detached brokers without killing other installs' images. let privateRunner: string | undefined; if (process.platform === "win32") { - const runner = env.OMP_WEB_OMP_BIN || (binary ? path.join(dir, "ompweb-omp.exe") : path.join(dir, "runner", "release", "ompweb-omp-windows-x64.exe")); + const runner = env.OMP_WEB_OMP_BIN || path.join(dir, "ompweb-omp.exe"); if (existsSync(runner)) { privateRunner = path.join(temp, "ompweb-omp.exe"); copyFileSync(runner, privateRunner); @@ -239,7 +243,7 @@ try { await step("a live session starts through the omp runner", async () => { const response = await fetch(`${base}/api/agent/${SESSION_ID}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ type: "get_state" }) }); const text = await response.text(); - assert(response.status === 200 && text.includes(SESSION_ID), `get_state: status ${response.status} ${text} (no runner? build it with \`bun runner/scripts/build-binary.ts\`)`); + assert(response.status === 200 && text.includes(SESSION_ID), `get_state: status ${response.status} ${text} (no runner? \`npm run fetch-omp\` fetches the pinned one)`); const state = await (await fetch(`${base}/api/agent/${SESSION_ID}`)).json() as { running?: boolean }; assert(state.running === true, `GET /api/agent: ${JSON.stringify(state)}`); }); diff --git a/scripts/use-omp-build.test.mjs b/scripts/use-omp-build.test.mjs deleted file mode 100644 index 915183c77..000000000 --- a/scripts/use-omp-build.test.mjs +++ /dev/null @@ -1,42 +0,0 @@ -import assert from "node:assert/strict"; -import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { delimiter, join } from "node:path"; -import test from "node:test"; - -// A ZIP containing index.json and fixture.tgz, readable by both unzip and Windows' bundled bsdtar. -const archive = Buffer.from("UEsDBBQAAAAAAAAAIVyswTZ6JAAAACQAAAAKAAAAaW5kZXguanNvbnsiQG9oLW15LXBpL2ZpeHR1cmUiOiJmaXh0dXJlLnRneiJ9ClBLAwQUAAAAAAAAACFczMCKaRAAAAAQAAAACwAAAGZpeHR1cmUudGd6Zml4dHVyZSBwYWNrYWdlClBLAQIUAxQAAAAAAAAAIVyswTZ6JAAAACQAAAAKAAAAAAAAAAAAAACkgQAAAABpbmRleC5qc29uUEsBAhQDFAAAAAAAAAAhXMzAimkQAAAAEAAAAAsAAAAAAAAAAAAAAKSBTAAAAGZpeHR1cmUudGd6UEsFBgAAAAACAAIAcQAAAIUAAAAAAA==", "base64"); - -test("runner packages extract with the native ZIP tool despite a shadowing tar, and failed extraction keeps the old vendor", async (t) => { - const root = mkdtempSync(join(tmpdir(), "omp-build-extract-")); - t.after(() => rmSync(root, { recursive: true, force: true })); - const scripts = join(root, "scripts"); - const vendor = join(root, "vendor"); - const shadow = join(root, "shadow-path"); - for (const dir of [scripts, vendor, shadow]) mkdirSync(dir); - const script = join(scripts, "use-omp-build.ts"); - copyFileSync(new URL("../runner/scripts/use-omp-build.ts", import.meta.url), script); - writeFileSync(join(root, "omp-build.json"), JSON.stringify({ repo: "fixture/omp", tag: "v1.0.0-int.202601010000.g1234567" })); - writeFileSync(join(vendor, "old-package.tgz"), "previous vendor"); - // Selecting tar from PATH must fail rather than accidentally pass with a ZIP-capable tar. - writeFileSync(join(shadow, process.platform === "win32" ? "tar.exe" : "tar"), "not an executable", { mode: 0o700 }); - let body = Buffer.from("not a ZIP"); - const server = Bun.serve({ hostname: "127.0.0.1", port: 0, fetch: () => new Response(body) }); - t.after(() => server.stop(true)); - const env = { ...process.env, OMP_BUILD_RELEASES: server.url.origin, PATH: `${shadow}${delimiter}${process.env.PATH ?? ""}` }; - const run = async () => { - const child = Bun.spawn([process.execPath, script], { cwd: root, env, stdin: "ignore", stdout: "pipe", stderr: "pipe" }); - const [code, stdout, stderr] = await Promise.all([child.exited, new Response(child.stdout).text(), new Response(child.stderr).text()]); - return { code, output: stdout + stderr }; - }; - const failed = await run(); - assert.notEqual(failed.code, 0, failed.output); - assert.equal(readFileSync(join(vendor, "old-package.tgz"), "utf8"), "previous vendor"); - body = archive; - const installed = await run(); - assert.equal(installed.code, 0, installed.output); - assert.deepEqual(JSON.parse(readFileSync(join(vendor, "index.json"), "utf8")), { "@oh-my-pi/fixture": "fixture.tgz" }); - assert.equal(readFileSync(join(vendor, "fixture.tgz"), "utf8"), "fixture package\n"); - assert.equal(existsSync(join(vendor, "old-package.tgz")), false); - assert.equal(existsSync(`${vendor}.new`), false); -}); diff --git a/server/dev.test.ts b/server/dev.test.ts index 75e416e20..55081317c 100644 --- a/server/dev.test.ts +++ b/server/dev.test.ts @@ -54,7 +54,7 @@ test("a SIGHUP to the dev parent also stops the server it re-ran", { skip: proce test("hot reload of a shared worker dependency keeps the dev server listening", { skip: process.platform === "win32", timeout: 60_000 }, async (t) => { const home = mkdtempSync(path.join(os.tmpdir(), "omp-web-hot-")); const source = path.join(home, "source"); - for (const name of ["server", "app", "lib", "components", "hooks", "public", "scripts", "bin", "tsconfig.json", "bunfig.toml", "package.json", "instrumentation.node.ts", "proxy.ts"]) { + for (const name of ["server", "app", "lib", "components", "hooks", "public", "scripts", "bin", "tsconfig.json", "bunfig.toml", "package.json", "omp-runner.json", "instrumentation.node.ts", "proxy.ts"]) { cpSync(path.join(repo, name), path.join(source, name), { recursive: true }); } symlinkSync(path.join(repo, "node_modules"), path.join(source, "node_modules"), "dir"); diff --git a/tsconfig.json b/tsconfig.json index 9d412d61c..2034c396c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -29,7 +29,6 @@ ], "exclude": [ "node_modules", - "dist", - "runner" + "dist" ] }