// PEP-256: the agents' tmux server lives in its own container (peppa-tmux); a platform deploy // recreates web/bot/watchdog/observer only, so working agents survive it. The watchdog and the // web are clients of its socket and never start a server of their own. import assert from "node:assert/strict"; import { chmodSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, test } from "node:test"; import { tmux } from "@peppa/runner"; const read = (p: string) => readFileSync(new URL(`../../../${p}`, import.meta.url), "utf8"); /** mock-tmux: logs its argv; `show-options` fails while MOCK_TMUX_DOWN is set (no server). */ function mockTmux(dir: string): { bin: string; calls: () => string[][] } { const log = join(dir, "calls.jsonl"); const bin = join(dir, "mock-tmux"); writeFileSync( bin, `#!/usr/bin/env node const fs = require("node:fs"); const args = process.argv.slice(2); fs.appendFileSync(${JSON.stringify(log)}, JSON.stringify(args) + "\\n"); if (args[0] === "show-options" && process.env.MOCK_TMUX_DOWN) { process.stderr.write("no server running on /x/default\\n"); process.exit(1); } if (args[0] === "display-message") { process.stdout.write("1 4242\\n"); } `, ); chmodSync(bin, 0o755); return { bin, calls: () => readFileSync(log, "utf8").trim().split("\n").filter(Boolean).map((l) => JSON.parse(l)) }; } describe("PEP-256: выкатка платформы не обрывает работающих агентов", () => { const saved = { ...process.env }; let mock: ReturnType; beforeEach(() => { const dir = mkdtempSync(join(tmpdir(), "pep256-")); writeFileSync(join(dir, "calls.jsonl"), ""); mock = mockTmux(dir); process.env.PEPPA_TMUX_BIN = mock.bin; delete process.env.PEPPA_TMUX_SOCKET; process.env.PEPPA_TMUX_EXTERNAL = "1"; delete process.env.MOCK_TMUX_DOWN; }); afterEach(() => { process.env = { ...saved }; }); const session = { name: "peppa-PEP-1-s1", cwd: "/w", command: "/run/launch.sh", env: { PEPPA_SESSION_ID: "1", SECRET_X: "v" } }; test("Сценарий: сервер tmux живёт отдельно — вачдог не поднимает свой, окружение агента идёт через new-session -e", async () => { await tmux.newSession(session); const calls = mock.calls(); assert.ok(!calls.some((a) => a.includes("start-server")), "клиент не стартует сервер"); assert.deepEqual(calls[0], ["show-options", "-s", "-v", "exit-empty"]); const ns = calls.find((a) => a[0] === "new-session")!; assert.ok(ns, "сессия создана"); assert.ok(ns.includes("PEPPA_SESSION_ID=1") && ns.includes("SECRET_X=v")); }); test("Сценарий: peppa-tmux не запущен — запуск агента отклоняется, свой сервер не поднимается", async () => { process.env.MOCK_TMUX_DOWN = "1"; await assert.rejects(tmux.newSession(session), (e: Error) => e.message.includes(tmux.TMUX_SERVER_DOWN)); assert.ok(!mock.calls().some((a) => a[0] === "new-session" || a.includes("start-server"))); }); test("Сценарий: без внешнего сервера (Mac, тесты) — как раньше, start-server с exit-empty off", async () => { delete process.env.PEPPA_TMUX_EXTERNAL; await tmux.newSession(session); assert.ok(mock.calls().some((a) => a[0] === "start-server")); }); test("Сценарий: pid панели из чужого пространства процессов не читается из /proc", async () => { // display-message: dead pane without a status — with an external server the /proc lookup is skipped. const st = await tmux.paneState("peppa-PEP-1-s1"); assert.deepEqual(st, { dead: true, exitStatus: null }); }); test("Сценарий: выкатка пересоздаёт web, bot, watchdog, observer — но не tmux", () => { const deploy = read("scripts/platform-deploy.sh"); const up = deploy.match(/^up\(\) \{.*$/m)![0]; assert.match(up, /up -d --remove-orphans web bot watchdog observer;/); assert.ok(!/\btmux\b/.test(up)); assert.match(deploy, /up -d --no-recreate tmux/); assert.match(deploy, /--tmux\) TMUX_ONLY=1/); assert.match(deploy, /up -d --force-recreate tmux/); assert.match(deploy, /tmux\.tag/); }); test("Сценарий: сервис tmux — тот же образ и пути, свой тег, без окружения платформы", () => { const compose = read("infra/docker-compose.yml"); const svc = compose.slice(compose.indexOf("\n tmux:"), compose.indexOf("\n observer:")); assert.match(svc, /image: peppa\/platform:\$\{PEPPA_TMUX_IMAGE_TAG:-latest\}/); assert.match(svc, /container_name: peppa-tmux/); assert.match(svc, /restart: unless-stopped/); assert.match(svc, /command: \["tmux", "-D"\]/); assert.match(svc, /TMUX_TMPDIR: \$\{PEPPA_HOME\}\/tmux/); assert.match(svc, /- \$\{PEPPA_HOME\}:\$\{PEPPA_HOME\}/); assert.match(svc, /\/var\/run\/docker\.sock:\/var\/run\/docker\.sock/); assert.match(svc, /user: "\$\{PEPPA_UID:-0\}:\$\{PEPPA_GID:-0\}"/); assert.ok(!/env_file/.test(svc), "секреты платформы не попадают в окружение tmux"); assert.ok(!/depends_on/.test(compose.slice(compose.indexOf("\n web:"))), "up сервисов не тянет tmux за собой"); assert.match(compose, /PEPPA_TMUX_EXTERNAL: "1"/); }); });