This repository is an active V1 → V2 migration.
The primary goal is to restore behavioral parity with V1 while preserving the V2 architecture. Changes must be evidence-driven, minimal, testable, and limited to the requested scope.
When V1 and V2 behavior differ, do not assume V2 is correct because it is newer. Treat V1 executable behavior and tests as the compatibility ground truth unless an explicit migration decision documents otherwise.
- Never run tests from the repo root. Root
bun testis disabled by design (rootpackage.jsontest scriptexit 1; rootbunfig.toml[test] root = "./do-not-run-tests-from-root"). Run tests from the affected package. - Root
bun typecheck=bun turbo typecheck(all packages). Per-package typecheck istsgo --noEmit(native@typescript/native-preview, nottsc);packages/appusestsgo -b. - Workspace dependency versions live in the root
package.jsonworkspaces.catalog; packages reference them ascatalog:. Bump a version in the catalog, never in a single package. - Install has a 3-day cooldown: root
bunfig.tomlsets[install] exact = trueandminimumReleaseAge = 259200. A version published <3 days ago is rejected unless listed inminimumReleaseAgeExcludes. - Many dependencies are locally patched (
patchedDependencies→patches/). Do not bump a pinned version without reviewing its patch. - App dev servers require
--conditions=browser; the packagedevscripts already set it. - Generated SDK: after a public Protocol/Server HttpApi change run
bun run generatefrompackages/client. CI gatebun run check:generatedregenerates, thengit diff --exit-code -- src/generated src/generated-effect. packages/opencodeHttpApi gate:bun run test:httpapi(coverage + auth + effect modes). Web e2e:bun --cwd packages/app test:e2e:local(Playwright).- Husky
pre-pushrunsbun knowledge:check(advisory), asserts the Bun version frompackageManager, thenbun typecheck.
Work from the repository root when investigating architecture, dependencies, or repository-wide behavior.
For package-local tests and typechecks, run commands from the affected package directory when required by that package.
Always report the exact working directory and command used for verification.
For non-trivial changes:
- Inspect the relevant V1 implementation.
- Inspect relevant V1 tests.
- Trace the corresponding V2 implementation end-to-end.
- Identify the exact behavioral difference.
- State the invariant that must be preserved.
- Implement the smallest V2-native change that restores the invariant.
- Add regression coverage.
- Verify the result.
Do not implement based only on names, comments, documentation, or assumptions when executable behavior can establish the answer.
When restoring behavior from V1 into V2:
- Treat V1 behavior as the compatibility ground truth unless an explicit migration decision says otherwise.
- Locate the exact V1 implementation before designing the V2 change.
- Locate relevant V1 tests before designing the V2 change.
- State the V1 invariant explicitly.
- Trace the equivalent V2 lifecycle/path.
- Identify the precise semantic regression.
- Distinguish behavioral differences from intentional architectural changes.
- Prefer the smallest V2-native implementation.
- Reuse existing V2 primitives.
- Do not recreate V1 subsystems merely to obtain parity.
- Do not introduce a second source of truth.
- Do not create parallel event buses, persistence systems, registries, coordinators, or abstractions when an existing V2 primitive already owns the responsibility.
- Preserve existing V2 ownership and dependency boundaries.
- Add regression tests for the behavioral invariant, not merely implementation details.
- Verify restart/recovery behavior whenever the invariant involves durability, persistence, ownership, or lifecycle state.
- Keep each parity fix independently scoped.
Every parity change should be explainable as:
V1 ground truth → V2 regression → invariant → smallest V2-native fix → regression tests → verification
For architecture-sensitive work, inspect the repository's current architecture and parity documentation before making changes.
Do not redesign architecture before understanding the existing system.
Before introducing a new abstraction, establish:
- what currently owns the responsibility;
- where the state lives;
- who creates it;
- who mutates it;
- who reads it;
- how lifecycle transitions occur;
- how errors/cancellation propagate;
- how restart/recovery behaves;
- which package owns the behavior;
- which existing primitive can be reused.
Prefer extending an existing ownership boundary over creating another one.
Preserve the V2 architecture unless the requested task explicitly changes it.
The dependency direction is:
Schema → Core/Protocol → Server
Client runtime may depend on Schema/Protocol but must not depend on Core/Server.
sdk-next may compose Client/Core/Server.
Do not introduce reverse dependencies.
The following are architectural invariants:
- Durable prompt admission is separate from model execution.
SessionV2.prompt()admits one durablesession_inputrow beforeSessionExecution.wake(sessionID), unlessresume:false.- Serialized runners promote admitted inputs at safe boundaries.
- Reusing a Session ID adopts the existing Session.
- Prompt message IDs are exact-retry identifiers only when Session/prompt/ delivery identity matches.
- Conflicting reuse must fail rather than silently create divergent state.
- Historical projected prompts may lazily synthesize promoted inbox records.
SessionExecutionis process-global and Session-ID based.- The local coordinator owns process-local Session coordination.
- Session placement occurs through SessionStore + LocationServiceMap when draining starts.
- Interruption targets the active process-local ownership chain.
- Idle/missing interruption is a no-op.
- SessionRunner/model/tool/permission/filesystem are Location-scoped.
- Omitted workspaceID means implicit-local.
- A provider turn uses one explicit
llm.stream(request). - Projected history is reloaded before durable continuation.
- Do not reintroduce the legacy
SessionPrompt.loop. - Local Sessions drain process-locally until clustering.
SessionRunCoordinatorjoins/coalesces wakes for the same Session and allows different Sessions to run concurrently.- Advisory wakes drain durable inbox state.
- Post-crash continuation recovery requires explicit design before retrying provider work.
- A drain has no durable identity/transcript boundary unless explicitly designed otherwise.
- Delivery vocabulary must remain explicit: steers by default; queued inputs promote at the next safe provider-turn boundary.
- Queued input remains pending until the Session is idle.
- One queued input is promoted and continuation is reevaluated.
- New user input resets the selected agent's provider-turn allowance.
- Batch steering resets the allowance once.
- EventV2 replay ownership is separate from clustered Session execution ownership.
- System Context algebra/registry/built-ins remain under
src/system-context. - Context Sources declare their observed domains.
- Session History owns history selection.
- Context Epoch persistence is Session-owned.
Do not weaken or bypass these boundaries for convenience.
Only change what the task requires.
Do not combine:
- unrelated bug fixes;
- opportunistic refactors;
- cleanup;
- formatting-only changes;
- dependency upgrades;
- architectural rewrites;
- migration of unrelated V1 behavior.
If investigation discovers an unrelated issue:
- document it;
- leave it untouched;
- mention it in the final report.
A parity fix must remain independently reviewable.
Prefer simple, direct code.
- Prefer one function unless decomposition provides genuine reuse or composability.
- Do not extract single-use helpers preemptively.
- Prefer early returns over unnecessary
else. - Keep the main happy path obvious.
- Use small helpers only for genuinely complex supporting logic.
- Avoid unnecessary destructuring.
- Avoid aliases when the original name is clear.
- Avoid star imports.
- Prefer type inference.
- Avoid
any. - Prefer Bun APIs where appropriate.
- Prefer functional array methods.
- Avoid unnecessary variable creation.
- Avoid
try/catchunless required for actual error handling. - Do not introduce Effect into synchronous helpers.
- In Effect generators, bind services to named variables rather than nesting yields.
- Prefer schema helpers over manual JSON parsing and
Effect.try. - Use dynamic imports for heavy startup-sensitive modules where appropriate.
- Comments should explain non-obvious constraints, not restate the code.
- Follow existing local conventions before introducing new patterns.
- Drizzle columns use snake_case.
Do not optimize for fewer lines at the expense of clarity or correctness.
The SDK is generated from the source definitions.
When changing a public Protocol or Server HttpApi:
- Change the source definition.
- Run the appropriate generation workflow.
- Regenerate the SDK.
- Verify generated output.
Generation:
./packages/sdk/js/script/build.ts
After a public Protocol or Server HttpApi change, run:
bun run generate
from:
packages/client
Never manually edit:
src/generatedsrc/generated-effect
Generated changes must come from the generator.
Tests must validate behavior, not merely reproduce implementation logic.
Prefer real implementations over mocks.
Avoid mocks unless there is a concrete reason they are required.
Do not use globalThis in tests unless there is no practical alternative.
For parity work, tests should prove the invariant at the boundary where the behavior matters.
Where applicable, test:
- normal behavior;
- edge cases;
- failure behavior;
- cancellation;
- repeated operations;
- lifecycle transitions;
- persistence;
- restart/reload;
- recovery;
- stale state;
- concurrency/coalescing;
- terminal-state behavior.
For durable state, a test that only checks an in-memory object is insufficient.
For restart/recovery semantics, simulate reconstruction from durable state.
Do not write a test that simply duplicates the implementation algorithm.
Verification is mandatory for code changes.
At minimum:
- Run targeted tests for the changed behavior.
- Run the affected package test suite.
- Run broader/core tests when appropriate.
- Run package typecheck.
- Run formatting checks.
- Run relevant lint/static analysis.
Tests and typechecks must be run from the package directory when required.
Never run tsc directly when the repository provides a package typecheck
command.
Use:
bun typecheck
from the appropriate package directory.
Separate:
- failures introduced by the change;
- failures caused by the environment;
- pre-existing failures;
- unrelated warnings.
Do not hide failures merely because they existed before the change.
If a verification command fails, determine whether the failure is related to the change before claiming completion.
Default branch:
dev
Do not assume a local main branch exists.
Use dev / origin/dev when establishing repository history.
Branch names:
- maximum 3 words;
- hyphenated;
- no slash;
- no type prefixes.
Follow conventional commit style.
Keep commits focused and reviewable.
Every non-trivial implementation task must end with a concise evidence-based report.
For parity work use:
What V1 actually does, with file/test references.
What V2 currently does differently.
The exact behavior that must be preserved.
What changed and why it is the smallest V2-native fix.
What regression tests were added and what they prove.
Exact commands and results.
Anything failing independently of the change.
What was intentionally not changed.
Do not claim parity from code inspection alone when the behavior can be tested.
Stop and investigate instead of guessing when:
- V1 and V2 ownership are unclear;
- the durable source of truth is unclear;
- lifecycle transitions are unclear;
- a proposed fix requires a new subsystem;
- existing V2 primitives appear insufficient;
- tests contradict implementation behavior;
- restart/recovery semantics are ambiguous;
- the change would cross package dependency boundaries;
- the requested change appears to require an architectural decision.
Do not silently invent behavior.
If an architectural decision is genuinely required, document the evidence and surface the decision before expanding the implementation.
When rules conflict, prioritize:
- Correct observable behavior.
- V1 compatibility for migration work.
- Existing V2 architectural ownership.
- Data/state integrity and durability.
- Regression coverage.
- Minimal implementation.
- Repository style preferences.
- Cosmetic cleanup.
Never sacrifice correctness or architectural integrity merely to satisfy a style preference.
Before broad repository exploration, read .knowledge/index.md and only the
focused documents relevant to the task. Treat knowledge as navigation/context,
not authority. Verify implementation-critical assumptions against current source
and tests; source wins on disagreement. After changes, review affected curated
notes in script/knowledge/catalog.ts, run bun knowledge:update, then
bun knowledge:validate. Do not load every knowledge document by default.