Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

10.3 KB
Raw Blame
/**
 * The review-lifecycle prompt hands CoS agents a copy-pasteable `curl` command
 * aimed at this install's own API for the challenge protocol. That must resolve
 * through `localApiBaseUrl()` rather than a hardcoded origin: on an install that
 * ran `npm run setup:cert`, `:5555` is TLS-only and a plain-HTTP request to it
 * dies at the transport layer, so the challenge-protocol dispute silently cannot
 * be filed (#5656). The local-LLM reviewer itself goes through the
 * auth-independent local-review bridge (`server/scripts/run-local-code-review.mjs`)
 * instead of an HTTP call, so it needs no origin and no credential at all (#7670).
 */

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';

vi.mock('../../lib/httpsState.js', () => ({
  getHttpsEnabledAtBoot: vi.fn(() => ({ value: false, initialized: true })),
}));

import { getHttpsEnabledAtBoot } from '../../lib/httpsState.js';
import { buildReviewLoopFollowUpSection, prepareSandboxedReviewLoopBody } from './reviewLifecycle.js';
import { buildLocalReviewerInstructions } from '../cosTaskPrompts.js';
import { CLI_REVIEW_OUTCOME_GUIDE } from './reviewerOutcome.js';
import { readFileSync } from 'node:fs';

const metadata = {
  reviewLoopFollowUp: true,
  reviewLoopPRUrl: 'https://github.com/example-org/example-repo/pull/9',
  reviewLoopPRBranch: 'feature-branch',
  reviewLoopPRNumber: 9,
  reviewLoopReviewers: ['lmstudio'],
  sourceTaskId: 'task-example',
};

describe('reviewLifecycle agent-facing API origin', () => {
  const ORIGINAL_ENV = { ...process.env };

  beforeEach(() => {
    vi.clearAllMocks();
    getHttpsEnabledAtBoot.mockReturnValue({ value: false, initialized: true });
    delete process.env.PORT;
    delete process.env.PORTOS_HTTP_PORT;
  });

  afterEach(() => {
    process.env = { ...ORIGINAL_ENV };
  });

  it('reports CLI health from claims and review loops without giving reviewers the token', () => {
    getHttpsEnabledAtBoot.mockReturnValue({ value: true, initialized: true });
    const sections = [
      buildLocalReviewerInstructions(['opencode', 'codex']),
      buildReviewLoopFollowUpSection({ ...metadata, reviewLoopReviewers: ['opencode', 'codex'], reviewLoopOptionalReviewers: ['opencode'] }),
    ];
    for (const section of sections) {
      expect(section).toContain('http://127.0.0.1:5553/api/code-review/cli-outcome');
      expect(section).toContain(CLI_REVIEW_OUTCOME_GUIDE);
      expect(section).toContain('Never give reviewers the API token');
    }
    const procedure = readFileSync(CLI_REVIEW_OUTCOME_GUIDE, 'utf8');
    expect(procedure).toContain('-H "Authorization: Bearer ${PORTOS_API_TOKEN:-}"');
    expect(procedure).toContain('A recorded failure is INCONCLUSIVE, never clean');
    expect(buildLocalReviewerInstructions(['ollama'])).not.toContain('/cli-outcome');
  });

  it('points the challenge-protocol curl at the loopback HTTP mirror when HTTPS is active', () => {
    getHttpsEnabledAtBoot.mockReturnValue({ value: true, initialized: true });

    const section = buildReviewLoopFollowUpSection(metadata);

    expect(section).not.toContain(':5555');
    expect(section).toContain('http://127.0.0.1:5553/api/cos/tasks/task-example/challenge');
  });

  it('uses the API port directly when HTTPS is off and no mirror is bound', () => {
    getHttpsEnabledAtBoot.mockReturnValue({ value: false, initialized: true });

    const section = buildReviewLoopFollowUpSection(metadata);

    expect(section).toContain('http://127.0.0.1:5555/api/cos/tasks/task-example/challenge');
  });
});

describe('reviewLifecycle reviewer invocation details', () => {
  beforeEach(() => {
    getHttpsEnabledAtBoot.mockReturnValue({ value: false, initialized: true });
  });

  it('uses resolved provider pins for code review without requiring them to screen public comments', () => {
    const token = 'provider:example-reviewer';
    const section = buildLocalReviewerInstructions([token], { [token]: 'example-model' }, { [token]: 'high' }, { claimCommentGate: true });
    expect(section).toContain('--arg model example-model');
    expect(section).toContain('--arg effort high');
    expect(section.match(/inheritDefaults: false/g)).toHaveLength(1);
    expect(section).not.toContain('## Tool-Free Public Comment Gate');
    const cleared = buildLocalReviewerInstructions([token], {}, {}, { claimCommentGate: true });
    expect(cleared.match(/inheritDefaults: false/g)).toHaveLength(1);
    expect(cleared).not.toContain('--arg model');
    expect(cleared).not.toContain('--arg effort');
  });

  // The route validates `backend` against a z.enum of the local-LLM reviewers, so
  // a leftover `<lmstudio|ollama>` placeholder in a run configured for MTPLX is a
  // 400 the agent has to guess its way out of.
  // Each local review runs as one bash block with fresh mktemp files; without an
  // EXIT trap the response JSON and its .findings file accumulate in <tmpdir>.
  it('removes its mktemp review response and findings files on exit', () => {
    const section = buildReviewLoopFollowUpSection({ ...metadata, reviewLoopReviewers: ['ollama'] });
    expect(section).toMatch(/REVIEW_RESPONSE=\$\(mktemp\)\ntrap 'rm -f "\$REVIEW_RESPONSE" "\$\{REVIEW_RESPONSE\}\.findings"' EXIT\n/);
  });

  it('names the local backend this run actually configured', () => {
    const section = buildReviewLoopFollowUpSection({ ...metadata, reviewLoopReviewers: ['mtplx'] });
    expect(section).toContain('backend: "mtplx"');
    expect(section).not.toContain('<lmstudio|ollama>');
    expect(section).not.toContain('Substitute the active reviewer name');
  });

  it('keeps a substitution placeholder when several local backends are configured', () => {
    const section = buildReviewLoopFollowUpSection({ ...metadata, reviewLoopReviewers: ['ollama', 'mtplx'] });
    expect(section).toContain('backend: "<ollama|mtplx>"');
    expect(section).toContain('Substitute the active reviewer name');
  });

  it('renders separate executable requests for provider identities with different pins', () => {
    const section = buildReviewLoopFollowUpSection({
      ...metadata,
      reviewLoopReviewers: ['provider:first', 'ollama', 'provider:second'],
      reviewLoopReviewerModels: { 'provider:first': 'first-model', 'provider:second': 'second-model' },
      reviewLoopReviewerEfforts: { 'provider:first': 'high', 'provider:second': 'low' },
    });
    const requests = [...section.matchAll(/jq -Rs '(\{[^'\n]+\}) \+ \{ diff: \. \}'/g)].map(match => JSON.parse(match[1]));
    expect(requests).toEqual([
      { backend: 'provider:first', inheritDefaults: false, model: 'first-model', effort: 'high', timeoutMs: 1800000 },
      { backend: 'provider:second', inheritDefaults: false, model: 'second-model', effort: 'low', timeoutMs: 1800000 },
    ]);
    expect(section).toContain('backend: "ollama"');
  });

  // The challenge-protocol curl hits `/api/*`, which the optional instance
  // password gates. An agent holds no browser cookie, so without the injected
  // session token the dispute came back `401 AUTH_REQUIRED` and read as a
  // broken protocol (#7660 follow-on). The whole header is one quoted argument
  // so a token can never be word-split, and the `:-` default keeps the command
  // valid on an install with no password set.
  it('spends the injected loopback session token on the challenge-protocol curl', () => {
    const section = buildReviewLoopFollowUpSection(metadata);
    const authHeader = '-H "Authorization: Bearer ${PORTOS_API_TOKEN:-}"';

    expect(section).toContain(`/api/cos/tasks/task-example/challenge -H 'Content-Type: application/json' ${authHeader}`);
  });

  // #7670: the local-LLM reviewer drives the same auth-independent stdin bridge
  // the claim prompt already uses — no HTTP route, no 401, no credential.
  it('pipes the local-LLM reviewer through the stdin bridge, not the HTTP route', () => {
    const section = buildReviewLoopFollowUpSection(metadata);

    expect(section).not.toContain('/api/code-review/local');
    expect(section).not.toContain('An `HTTP 401` is not a review result');
    expect(section).toContain('run-local-code-review.mjs');
    expect(section).toContain('timeoutMs: 1800000');
  });

  // `opencode run -m <provider/model>`: rendering `--model` here had agents
  // probing for a flag OpenCode does not document.
  it('renders each CLI reviewer\'s own model flag', () => {
    const section = buildReviewLoopFollowUpSection({
      ...metadata,
      reviewLoopReviewers: ['opencode', 'codex'],
      reviewLoopReviewerModels: { opencode: 'mtplx/qwen38', codex: 'gpt-5.6-sol' },
    });
    expect(section).toContain('`opencode -m mtplx/qwen38 …`');
    expect(section).toContain('`codex --model gpt-5.6-sol …`');
  });
});

describe('public review CLI procedure sanitization', () => {
  it('keeps a native read-only invocation when the recipe supports it', () => {
    const body = prepareSandboxedReviewLoopBody(
      'codex --sandbox danger-full-access -a never exec "$CODEX_APPLY_PROMPT"',
    );

    expect(body).toContain('codex ');
    expect(body).toContain('--sandbox read-only review');
    expect(body).not.toContain('danger-full-access');
    expect(body).not.toContain('exec');
  });

  it('falls back to the supported agy procedure without inventing isolation flags', () => {
    const body = prepareSandboxedReviewLoopBody(
      'agy --dangerously-skip-permissions --model "$AGY_REVIEW_MODEL" --print-timeout 30m -p "$LOCAL_PROMPT"',
    );

    expect(body).toContain('agy --model "$AGY_REVIEW_MODEL" --print-timeout 30m --mode plan --sandbox -p "$LOCAL_PROMPT"');
    expect(body).not.toContain('Reviewer unavailable');
    expect(body).not.toContain('--dangerously-skip-permissions');
  });

  it('fails closed when no maintained procedure can be recognized', () => {
    const body = prepareSandboxedReviewLoopBody('codex --danger-full-access exec "$LOCAL_PROMPT"');

    expect(body).toContain('Reviewer unavailable');
  });
});

it.each([{}, { verbose: true }, { reviewLoopMergeOnly: true }])('keeps fork push destinations explicit for review and merge variants %j', options => {
  const section = buildReviewLoopFollowUpSection({ ...metadata, ...options, reviewLoopPRBranch: 'main',
    forkHead: { remoteUrl: 'https://github.com/contributor/example.git', ownerLogin: 'contributor' } }, options);
  expect(section).toContain('local branch `fork/contributor/main`');
  expect(section).toContain('git push fork-contributor HEAD:refs/heads/main');
  expect(section).toContain('never push it to origin or delete the contributor');
});