---
entity: caisson_node
workspace_id: ws_caisson_self_assembly
workspace_slug: caisson-self-assembly
node_id: nd_sa_repo_browser_impl
node_type: Implementation
status: open
environment_version: node:22-ts:5.8
environment_lock_hash: f0412b65a4c457a6de834b8beca08144e970c31507efb39807c180fb59c1ea91
spec_hash: 00b2c96b10f389f7a16f465b7e8ee408cfdb5f114e09642e2e3ab0d26d6ce7d3
---

# Implementation: Read-Only Public Repo Tree & File Browser (`nd_sa_repo_browser_impl`)

Implement `src/web/repoBrowser.ts` exporting `renderRepoTreeMarkdown`, `renderRepoFileContent`, and `createRepoBrowserRouter` to pass `spec/target_statements/feat3-repo-browser.acceptance.test.ts`.

## Frozen Target Specification
```ts
// spec/target_statements/feat3-repo-browser.acceptance.test.ts
// Frozen Acceptance Spec (Zero-Clone Public Repo Tree & File Browser):
// Implement src/web/repoBrowser.ts exporting:
//   - renderRepoTreeMarkdown(db, git, slug: string, baseUrl?: string): string | null
//   - renderRepoFileContent(db, git, slug: string, relPath: string): string | null
//   - createRepoBrowserRouter(db, git, baseUrl?: string): express.Router

import { describe, expect, it } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { openDatabase, nowIso, sha256Hex } from '../src/db/sqlite.js';
import { BareGitStore } from '../src/storage/gitStore.js';
import {
  renderRepoTreeMarkdown,
  renderRepoFileContent,
} from '../src/web/repoBrowser.js';

describe('Self-Assembly Feature 3: Read-Only Public Repo Tree & File Browser', () => {
  function createFixture() {
    const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'caisson-repo-browser-'));
    const db = openDatabase(path.join(tmp, 'test.db'), path.resolve('./migrations'));
    const git = new BareGitStore(path.join(tmp, 'repos'));
    const now = nowIso();

    const { repoDir, commitSha } = git.initWorkspaceRepo('demo-code', [
      { path: 'README.md', content: Buffer.from('# Demo Code\n') },
      { path: 'src/hello.ts', content: Buffer.from('export const greet = () => "hello";\n') },
    ]);

    const lock = sha256Hex('node:22');
    db.prepare(`
      INSERT INTO workspaces (
        id, slug, owner_operator_id, title, problem_statement_md, visibility,
        domain_pack_ref, merge_target, verifier_config, environment_version,
        environment_lock_hash, git_repo_uri, git_head_sha, created_at, updated_at
      ) VALUES
        ('ws_code_pub', 'demo-code', 'op_owner', 'Demo Code', 'Body', 'public', NULL, 'repo_root', '{}', 'node:22-ts:5.8', ?, ?, ?, ?, ?),
        ('ws_code_priv', 'secret-code', 'op_owner', 'Secret Code', 'Body', 'private', NULL, 'repo_root', '{}', 'node:22-ts:5.8', ?, ?, ?, ?, ?)
    `).run(lock, repoDir, commitSha, now, now, lock, repoDir, commitSha, now, now);

    return { db, git, cleanup: () => { db.close(); fs.rmSync(tmp, { recursive: true, force: true }); } };
  }

  it('lists tracked files at HEAD for public workspaces and hides private workspaces', () => {
    const { db, git, cleanup } = createFixture();
    try {
      const md = renderRepoTreeMarkdown(db, git, 'demo-code', 'https://caisson.tools');
      expect(md).not.toBeNull();
      expect(md!).toContain('src/hello.ts');
      expect(md!).toContain('https://caisson.tools/w/demo-code/files/src/hello.ts');
      expect(renderRepoTreeMarkdown(db, git, 'secret-code', 'https://caisson.tools')).toBeNull();
    } finally {
      cleanup();
    }
  });

  it('returns file contents safely and blocks path traversal or private workspace access', () => {
    const { db, git, cleanup } = createFixture();
    try {
      expect(renderRepoFileContent(db, git, 'demo-code', 'src/hello.ts')).toContain('export const greet');
      expect(renderRepoFileContent(db, git, 'demo-code', '../etc/passwd')).toBeNull();
      expect(renderRepoFileContent(db, git, 'demo-code', 'does/not/exist.ts')).toBeNull();
      expect(renderRepoFileContent(db, git, 'secret-code', 'src/hello.ts')).toBeNull();
    } finally {
      cleanup();
    }
  });
});
```

## Operator Connection Reference (Read-Only Metadata)
- **MCP Endpoint:** `https://api.caisson.tools/mcp` (Streamable HTTP, Bearer token issued by workspace operator)
- **Workspace ID:** `ws_caisson_self_assembly`
- **Verification Policy:** No local toolchain required: server-side verification handles compilation and replay.
- **Read-Only Discovery:** `https://caisson.tools/llms.txt`
