19 KiB
MASTER COORDINATOR BUILD PROMPT: REPO 1 — opencode-harness
Target Repository: /home/john/Projects/Heretek-AI-Rebuild/opencode-harness
GitHub Remote: https://github.com/Heretek-AI/opencode-harness
Package Name: @heretek-ai/opencode-harness
Primary Session Agent: agency-coordinator (opencode-go/muse-spark-1.3-contributor)
Required Triad Subagents: agency-programmer, agency-qa, agency-drift-guard
0. CRITICAL MUSE 1.3 HARNESS SUPPLEMENT (READ FIRST — NON-NEGOTIABLE)
You are running on opencode-go/muse-spark-1.3-contributor inside the vanilla OpenCode v2 harness. To prevent the known failure modes of Muse 1.3 in the vanilla harness, you MUST obey these six rules on every turn:
- ZERO PROMPT SUMMARIZATION WHEN CALLING SUBAGENTS:
- Muse 1.3 has a tendency to summarize instructions when calling
subagent, which causesagency-programmerto drop types, file paths, and edge cases. - MANDATORY FIX: In Wave 0 below, you (
agency-coordinator) will write this entire specification intodocs/prompts/SPEC.mdandAGENTS.mdon disk. When you invokesubagent, you MUST use the exact JSON payloads in Section 4, which instructagency-programmer,agency-qa, andagency-drift-guardtoreadAGENTS.mdanddocs/prompts/SPEC.mddirectly from disk.
- Muse 1.3 has a tendency to summarize instructions when calling
- EXPLICIT SUBAGENT ROLE NAMES (NEVER DEFAULT TO
general):- Every
subagenttool call MUST explicitly set"agent": "agency-programmer","agent": "agency-qa", or"agent": "agency-drift-guard".
- Every
- VANILLA
read/edit/writeTOOL HYGIENE:- Always use native
read,write,edit,glob, andgreptools—never usebash(cat,sed,awk,echo >) to read or write source files. - When calling
edit, NEVER copy line-number prefixes (e.g.12:) fromreadoutput intooldStringornewString. CompareoldStringandnewStringline-by-line before invokingedit. - Whenever
editorwritereturns a<diagnostics>block, fix all reported TypeScript/syntax errors immediately in your very next tool call.
- Always use native
- STRICT SCOPE & REFUSAL BOUNDARY FOR
opencode-harness:- This repository is ONLY the in-process single-session OpenCode v2 plugin (
@heretek-ai/opencode-harness). - FORBIDDEN IN THIS REPO: Do NOT build any WebUI, HTTP server, multi-agent DAG scheduler, git worktree manager, or epistemic research engine here. Those live in the other 4 Heretek repositories.
- This repository is ONLY the in-process single-session OpenCode v2 plugin (
- ZERO STUBS, ZERO MOCKS, ZERO VACUOUS TESTS:
- Never write
// TODO,cmd: "true",|| true,test.skip, or empty functions. Every module must be 100% implemented and wired intosrc/index.ts.
- Never write
- BINDING TRIAD GATE (PROGRAMMER → QA RED-PROOF → DRIFT GUARD
VERDICT: PASS):- No wave is complete until
agency-qaperforms a Red-Proof Sabotage Test (provingbun test/make acceptfails when code is broken, then restoring cleanly viagit checkout --) ANDagency-drift-guardwritesVERDICT: PASSindocs/reports/wave-<N>-verification.md.
- No wave is complete until
1. TARGET REPOSITORY ARCHITECTURE & FILE TREE
opencode-harness replaces the bloated 6,000-line SwarmOS repo with a lean, ~450-line TypeScript/Bun OpenCode v2 plugin that integrates the active 2026 OSS winners (pi-hashline-readmap, @ast-grep/napi, lsp-mcp-server, fallow-mcp, @axe-core/playwright) with our salvaged permission guard, doom-loop circuit breaker, and KV-cache-preserving context budget hook.
/home/john/Projects/Heretek-AI-Rebuild/opencode-harness/
├── AGENTS.md
├── Makefile
├── package.json
├── tsconfig.json
├── docs/
│ ├── prompts/
│ │ ├── SPEC.md
│ │ └── verify-stage.md
│ ├── reports/
│ └── evidence/
├── scripts/
│ └── accept/
│ ├── wave-1.sh
│ ├── wave-2.sh
│ └── wave-3.sh
├── src/
│ ├── index.ts # Main OpenCode v2 plugin entrypoint
│ ├── editors/
│ │ ├── hashline-tools.ts # Wraps pi-hashline-readmap (file_hash_read, file_hash_edit)
│ │ └── ast-codemod.ts # Wraps @ast-grep/napi (ast_structural_search, ast_structural_rewrite)
│ ├── guards/
│ │ ├── shell-parse.ts # Salvaged AST/token shell command classifier (read-only vs write vs destructive)
│ │ ├── permission-guard.ts # Salvaged permission.hook("evaluate") role/path/contract guard
│ │ └── circuit-breaker.ts # Patched tool doom-loop circuit breaker (edit >= 2 or shell >= 3 consecutive failures)
│ ├── context/
│ │ └── context-budget.ts # KV-Cache-Safe context packer (NEVER mutates event.system; appends to last user msg)
│ └── ui-auditor/
│ ├── dom-auditor.ts # Headless Playwright + @axe-core/playwright WCAG 2.2 + sibling collision & descender clip
│ └── mcp-defaults.ts # Helper generating recommended mcpServers block (lsp-mcp-server + fallow-mcp)
└── test/
├── editors.test.ts # Tests file_hash_read, file_hash_edit, and ast_structural_search/rewrite
├── guards.test.ts # Tests permission-guard (blocks qa-adversary on src/**, blocks rm -rf, blocks director writes)
├── circuit-breaker.test.ts # Tests doom-loop intervention after 2 failed edits or 3 failed shell runs
├── context-budget.test.ts # Asserts event.system is 100% UNCHANGED and float relevance scores are stripped
└── dom-auditor.test.ts # Tests DOM geometry math (overflow, contrast, tiny targets, sibling collision)
2. EXACT FILE-BY-FILE SPECIFICATIONS (WRITE TO docs/prompts/SPEC.md IN WAVE 0)
2.1 package.json
{
"name": "@heretek-ai/opencode-harness",
"version": "1.0.0",
"description": "Lean OpenCode v2 in-process harness plugin: Hashline edits, AST codemods, KV-cache-safe context budgeting, permission guards, and DOM geometry auditing.",
"type": "module",
"main": "./src/index.ts",
"exports": {
".": "./src/index.ts"
},
"scripts": {
"check": "tsc --noEmit",
"test": "bun test",
"accept": "bash scripts/accept/run.sh"
},
"dependencies": {
"@ast-grep/napi": "^0.51.0",
"@axe-core/playwright": "^4.14.0",
"pi-hashline-readmap": "^0.16.1",
"playwright": "^1.64.1",
"zod": "^3.24.0"
},
"devDependencies": {
"@types/bun": "latest",
"typescript": "^5.7.0"
}
}
(Note for agency-programmer: If offline or if a specific version tag in npm registry differs slightly on the host, install with bun install or provide a clean local TypeScript fallback wrapper with identical exports and full FNV-1a shifted-anchor / regex-AST / Playwright evaluation so bun test and bun run check pass 100% deterministically with zero network flakiness.)
2.2 Makefile
.PHONY: check test accept
check:
bun run check
bun test
test:
bun test
accept:
@if [ -z "$(WAVE)" ]; then \
bash scripts/accept/wave-1.sh && \
bash scripts/accept/wave-2.sh && \
bash scripts/accept/wave-3.sh; \
else \
bash scripts/accept/wave-$(WAVE).sh; \
fi
2.3 Wave 1 Modules: Deterministic Editors & Guards (src/editors/* & src/guards/*)
A. src/editors/hashline-tools.ts
Must export:
computeLineHash(line: string): string— deterministic 4-hex-char FNV-1a hash of trimmed line content (upgraded from 2-hex-char to 16-bit 4-hex-char0000–ffffto eliminate birthday-paradox collisions on 40+ line files!).hashReadFile(rootDir: string, relPath: string, offset?: number, limit?: number): Promise<{ path: string; totalLines: number; content: string }>— formats each line as<lineNo>#<4-hex-hash>| <lineContent>.hashEditFile(rootDir: string, relPath: string, hunks: Array<{ startLine: number; startHash: string; endLine: number; endHash: string; replacement: string }>): Promise<{ applied: boolean; shiftedLines: number; path: string; error?: string }>:- Resolves shifted anchors across +/-25 lines when earlier hunks insert/delete lines.
- Validates that
startHashandendHashmatch the target lines before mutating. - Fails closed without writing to disk if any hunk anchor hash mismatches.
B. src/editors/ast-codemod.ts
Must export:
astSearch(code: string, pattern: string, language?: "typescript" | "tsx" | "javascript" | "python" | "rust" | "go"): Array<{ text: string; startLine: number; endLine: number }>:- Uses
@ast-grep/napi(parse(lang, code).root().findAll({ rule: { pattern } })) with a deterministic structural metavariable ($VAR,$$$ARGS) fallback if native bindings are unavailable for a target language.
- Uses
astRewriteFile(rootDir: string, relPath: string, pattern: string, rewrite: string, language?: string): Promise<{ modified: boolean; matchCount: number; output: string }>.
C. src/guards/shell-parse.ts & src/guards/permission-guard.ts
Salvage from /home/john/Projects/SwarmOS/src/agents/shell-parse.ts and guard.ts (decoupled from OrgBoard so it takes a lightweight GuardPolicyConfig with lockedContractGlobs: string[], taskOwnedPaths?: Record<string, string[]>):
- Rule 1 (Destructive Command Block): Any shell command containing
rm -rf /,rm -rf *,git reset --hard,git push --force,mkfs, ordd if=is denied across ALL roles (allowed: false). - Rule 2 (Leadership Read-Only Separation): Roles
director,manager,agency-coordinator, andagency-drift-guardare deniededit,write,patch, and file-mutating shell commands (allowed: false). - Rule 3 (QA Red-Team Source Lock): Roles
qa-adversaryandagency-qaare deniededit/write/patchonsrc/**(allowed: false), and permitted only on**/*.test.*,**/*.spec.*,test/**,tests/**,scripts/accept/**, anddocs/**. - Rule 4 (Architect Contract Lock): Files matching
lockedContractGlobs(default["**/contracts/**", "**/schemas/*.schema.json"]) cannot be modified by any role exceptarchitect.
D. src/guards/circuit-breaker.ts
Implement the exact upgraded Doom-Loop Circuit Breaker from Report 01b §9.2:
- Tracks per-session tool attempts (
sessionAttempts: Map<string, ToolAttemptRecord[]>). - Triggers critical intervention (
shouldEscalateModel: true,<circuit-breaker-intervention severity="critical">) when:editorpatchfails>= 2consecutive times on the same file path, ORshell/bashfails>= 3consecutive times on the same command prefix.
2.4 Wave 2 Modules: KV-Cache-Safe Context Budget & DOM Geometry Auditor (src/context/* & src/ui-auditor/*)
A. src/context/context-budget.ts (CRITICAL KV-CACHE INVARIANT FROM REPORT 01b §9.3)
- Must export:
estimateTokens(text: string): numberpackBlocks(blocks: ContextBlock[], budgetTokens: number): { text: string; usedTokens: number }buildDynamicContextBlocks(rootDir: string, artifacts: { taskMd?: string; planMd?: string; lessons?: Array<{ id: string; trigger: string; lesson: string; needsReview?: boolean }> }): ContextBlock[]injectContextBudgetIntoMessages(event: { agent?: string; kind?: string; system?: Array<{ text: string }>; messages?: Array<{ role: string; content: any }> }, packedText: string): void
- NON-NEGOTIABLE KV-CACHE INVARIANTS (Tested by
test/context-budget.test.ts):injectContextBudgetIntoMessagesMUST NEVER mutateevent.system!event.systemmust remain bit-for-bit identical before and after the hook runs so DeepSeek, Anthropic, and OpenCode Go KV prefix caches stay at >90% hit rate.- Dynamic context (
<opencode-harness-context>...</opencode-harness-context>) MUST be appended strictly to the last message whererole === "user"inevent.messages. - Episodic lessons MUST be sorted deterministically by
a.id.localeCompare(b.id)and formatted as<episodic-repository-memory id="${l.id}">\n- (${l.trigger}): ${l.lesson}\n</episodic-repository-memory>with ZERO floating-point relevance scores or timestamps in the tag attributes.
B. src/ui-auditor/dom-auditor.ts
- Must export:
evaluateDomGeometryInPage(elements: Array<{ tag: string; id?: string; rect: { x: number; y: number; width: number; height: number }; scrollWidth?: number; clientWidth?: number; fontSize?: number; lineHeight?: number; overflow?: string; interactive?: boolean; parentId?: string }>): GeometryDefect[]— Pure deterministic geometry verifier checking:element-overflow-x:scrollWidth > clientWidth + 2tiny-interactive-target:interactive === true && (rect.width < 24 || rect.height < 24)(WCAG 2.2 Target Size)descender-clip-risk:lineHeight / fontSize < 1.15 && overflow === "hidden"sibling-collision: two siblings with sameparentIdwhose bounding boxes intersect byoverlapX > 2 && overlapY > 2row-baseline-jitter: two siblings in the same horizontal row whoserect.ydiffers by0.5 <= Math.abs(y1 - y2) <= 3.5
auditLivePageWithPlaywright(url: string, viewports?: Array<{ name: string; width: number; height: number }>): Promise<DOMAuditReport>— Launches headless Chromium viaplaywright, runs@axe-core/playwright(withTags(["wcag2a", "wcag2aa", "wcag22aa"])) + the in-page DOM geometry collector, and returns structured defects.
2.5 Wave 3 Module: Unified OpenCode v2 Plugin Entrypoint (src/index.ts)
- Must export
activate(context: PluginContext)andcreateHarnessPlugin(options?: HarnessPluginOptions):- Registers tools:
file_hash_read,file_hash_edit,ast_structural_search,ast_structural_rewrite,ui_geometry_audit. - Registers hooks:
permission.hook("evaluate", ...)\rightarrowevaluateBoardGuardtool.execute.after\rightarrowrecordAndEvaluateCircuitBreakersession.hook("context", ...)\rightarrowregisterContextBudgetHook
- Exports
getRecommendedMcpConfig()returning the exactlsp-mcp-server(bunx lsp-mcp-server) andfallow-mcp(npx --yes --package fallow fallow-mcp) blocks from Report01a.
- Registers tools:
3. WAVE-BY-WAVE TRIAD EXECUTION PLAN FOR agency-coordinator
Execute this repository build in 3 Sequential Waves. Do NOT start Wave N+1 until Wave N has received VERDICT: PASS from agency-drift-guard and is merged to main.
Step 0 (Coordinator Bootstrap on main)
- Write
AGENTS.mdwith the Muse 1.3 Operating Contract and the Hard Refusal Rule ("Single-session OpenCode v2 plugin only; refuse WebUIs, fleet daemons, and worktree managers"). - Write this full specification to
docs/prompts/SPEC.mdand write the Drift Reviewer checklist todocs/prompts/verify-stage.md. - Write
Makefile,package.json, andtsconfig.json. - Commit baseline scaffolding to
main.
Wave 1: Editors (hashline-tools.ts, ast-codemod.ts) & Guards (shell-parse.ts, permission-guard.ts, circuit-breaker.ts)
- Create worktree:
git worktree add -b wave-1-editors-guards worktrees/wave-1 main - Dispatch Programmer (
agency-programmer):
{
"agent": "agency-programmer",
"description": "Wave 1 Programmer: Implement editors and guards in worktrees/wave-1",
"prompt": "You are the Programmer Agent for Wave 1 in `/home/john/Projects/Heretek-AI-Rebuild/opencode-harness/worktrees/wave-1` on branch `wave-1-editors-guards`.\n\nFIRST: Read `AGENTS.md` and `docs/prompts/SPEC.md` in full.\nSECOND: Implement:\n1. `src/editors/hashline-tools.ts` (4-hex FNV-1a line hash, `hashReadFile`, `hashEditFile` with +/-25 line shifted-anchor recovery and fail-closed hash check).\n2. `src/editors/ast-codemod.ts` (`astSearch`, `astRewriteFile`).\n3. `src/guards/shell-parse.ts` and `src/guards/permission-guard.ts` (blocking destructive commands, blocking leadership writes, blocking `qa-adversary`/`agency-qa` edits on `src/**`, blocking non-architect edits on `**/contracts/**`).\n4. `src/guards/circuit-breaker.ts` (doom-loop detector triggering at >=2 failed edits or >=3 failed shell commands).\n5. Write comprehensive unit tests in `test/editors.test.ts`, `test/guards.test.ts`, and `test/circuit-breaker.test.ts`.\n6. Write executable acceptance script `scripts/accept/wave-1.sh` (must exit 0 on pass, exit 1 on fail) and report `docs/reports/wave-1.md`.\n7. Run `bun test` and `bun run check` until 100% green, then commit your work on `wave-1-editors-guards`."
}
- Dispatch QA (
agency-qa):
{
"agent": "agency-qa",
"description": "Wave 1 QA: Verify and red-proof Wave 1 in worktrees/wave-1",
"prompt": "You are the QA Agent for Wave 1 in `/home/john/Projects/Heretek-AI-Rebuild/opencode-harness/worktrees/wave-1`.\n\n1. Run `make check` and `bash scripts/accept/wave-1.sh`. Assert both exit 0.\n2. Perform Red-Proof Sabotage: temporarily break `computeLineHash` in `src/editors/hashline-tools.ts` and confirm `bun test` exits non-zero; restore with `git checkout --`. Temporarily allow `qa-adversary` to edit `src/index.ts` in `src/guards/permission-guard.ts` and confirm `bun test` exits non-zero; restore with `git checkout --`.\n3. Record sabotage commands and exit codes in `docs/evidence/wave-1/red-proofs.log` and confirm `git status` is clean."
}
- Dispatch Drift Guard (
agency-drift-guard):
{
"agent": "agency-drift-guard",
"description": "Wave 1 Drift Reviewer: Adversarial audit of Wave 1",
"prompt": "You are the Drift Reviewer Agent (`agency-drift-guard`) for Wave 1 in `/home/john/Projects/Heretek-AI-Rebuild/opencode-harness/worktrees/wave-1`. You change NO code.\n\n1. Read `AGENTS.md`, `docs/prompts/SPEC.md`, and `docs/reports/wave-1.md`.\n2. Run `make check` and `bash scripts/accept/wave-1.sh`.\n3. Scan `src/` and `test/` for stubs, `// TODO`, `|| true`, `test.skip`, or hardcoded pass returns.\n4. Perform your own independent sabotage test on one guard rule, verify failure, and restore cleanly.\n5. Write `docs/reports/wave-1-verification.md` ending with `VERDICT: PASS` or `VERDICT: FAIL`."
}
- Once
VERDICT: PASS, mergewave-1-editors-guardsintomain(git merge --no-ff wave-1-editors-guards) and remove the worktree.
Wave 2: KV-Cache-Safe Context Budget (src/context/context-budget.ts) & DOM Geometry Auditor (src/ui-auditor/*)
- Create worktree:
git worktree add -b wave-2-context-dom worktrees/wave-2 main - Dispatch
agency-programmerto implementsrc/context/context-budget.ts,src/ui-auditor/dom-auditor.ts,src/ui-auditor/mcp-defaults.ts,test/context-budget.test.ts,test/dom-auditor.test.ts,scripts/accept/wave-2.sh, anddocs/reports/wave-2.md. - Dispatch
agency-qato runmake check,bash scripts/accept/wave-2.sh, and red-proof sabotage (specifically testing that mutatingevent.systemor injecting a floatrelevance=attribute causestest/context-budget.test.tsto fail!). - Dispatch
agency-drift-guardto audit Wave 2 and issueVERDICT: PASSindocs/reports/wave-2-verification.md. - Merge
wave-2-context-domintomainand remove the worktree.
Wave 3: Plugin Entrypoint (src/index.ts) & Full End-to-End Acceptance
- Create worktree:
git worktree add -b wave-3-plugin-entry worktrees/wave-3 main - Dispatch
agency-programmerto wire all tools and hooks intosrc/index.ts, writetest/plugin.test.ts,scripts/accept/wave-3.sh,README.md, anddocs/reports/wave-3.md. - Dispatch
agency-qato runmake checkandmake acceptacross all 3 waves with red-proof sabotage. - Dispatch
agency-drift-guardfor final verification (docs/reports/wave-3-verification.md\rightarrowVERDICT: PASS). - Merge
wave-3-plugin-entryintomain, remove worktree, and verifymake check && make acceptexits 0 onmain.