diff --git a/.github/actions/setup-bun/action.yml b/.github/actions/setup-bun/action.yml index 9d29724d86..acdf2e50dd 100644 --- a/.github/actions/setup-bun/action.yml +++ b/.github/actions/setup-bun/action.yml @@ -54,16 +54,33 @@ runs: shell: bash - name: Install dependencies + shell: bash run: | # Workaround for patched peer variants # e.g. ./patches/ for standard-openapi # https://github.com/oven-sh/bun/issues/28147 - if [ "$RUNNER_OS" = "Windows" ]; then - bun install --linker hoisted ${{ inputs.install-flags }} - else + if [ "$RUNNER_OS" != "Windows" ]; then bun install ${{ inputs.install-flags }} + exit $? fi - shell: bash + + # On Windows, `bun install --linker hoisted` occasionally fails to apply + # a patchfile with "ENOTEMPTY: ... Directory not empty" while renaming the + # patched package into a cache dir (e.g. @ai-sdk/openai-compatible). This is + # a bun-Windows race with the dependency cache, not a code defect: the leftover + # half-written dir makes the rename collide on the next attempt. Retry by + # clearing the bun package cache first so the reinstall starts from a clean dir. + for attempt in 1 2 3; do + echo "bun install attempt $attempt (--linker hoisted)" + if bun install --linker hoisted ${{ inputs.install-flags }}; then + exit 0 + fi + echo "bun install failed on attempt $attempt; clearing bun cache and retrying" + bun pm cache rm 2>/dev/null || rm -rf "$(bun pm cache)" + echo "cleared bun package cache" + done + echo "bun install failed after 3 attempts" + exit 1 - name: Save Bun dependencies if: steps.bun-cache.outputs.cache-hit != 'true' && github.event_name != 'pull_request' && github.event_name != 'pull_request_target' diff --git a/.github/workflows/nix-eval.yml b/.github/workflows/nix-eval.yml index 75332695a1..38b6043714 100644 --- a/.github/workflows/nix-eval.yml +++ b/.github/workflows/nix-eval.yml @@ -2,9 +2,9 @@ name: nix-eval on: push: - branches: [dev] + branches: [main, dev] pull_request: - branches: [dev] + branches: [main, dev] workflow_dispatch: concurrency: @@ -16,7 +16,7 @@ permissions: jobs: nix-eval: - runs-on: blacksmith-4vcpu-ubuntu-2404 + runs-on: ubuntu-latest timeout-minutes: 15 steps: - name: Checkout repository diff --git a/.github/workflows/pr-management.yml b/.github/workflows/pr-management.yml index b6aa4e589d..1b3c60a361 100644 --- a/.github/workflows/pr-management.yml +++ b/.github/workflows/pr-management.yml @@ -6,7 +6,7 @@ on: jobs: check-duplicates: - runs-on: blacksmith-4vcpu-ubuntu-2404 + runs-on: ubuntu-latest permissions: contents: read pull-requests: write diff --git a/.github/workflows/typecheck.yml b/.github/workflows/typecheck.yml index fc9a52797c..c79034f47a 100644 --- a/.github/workflows/typecheck.yml +++ b/.github/workflows/typecheck.yml @@ -2,14 +2,14 @@ name: typecheck on: push: - branches: [dev] + branches: [main, dev] pull_request: - branches: [dev] + branches: [main, dev] workflow_dispatch: jobs: typecheck: - runs-on: blacksmith-4vcpu-ubuntu-2404 + runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 diff --git a/UPSTREAM.md b/UPSTREAM.md new file mode 100644 index 0000000000..225c1d6534 --- /dev/null +++ b/UPSTREAM.md @@ -0,0 +1,32 @@ +# Upstream Tracking + +This file records the relationship between this fork and the upstream OpenCode +repository, per the OC Kit integration plan (Phase 0: upstream freeze & audit). + +## Upstream + +- Repository: `https://github.com/anomalyco/opencode` +- License: MIT + +## Fork status + +- Local fork: `vibecoder880/opencode` +- Local `packages/opencode/package.json` version: `1.18.18` +- Upstream main has moved beyond this line (upstream exposes a `2.0` branch); + this fork is a snapshot of the `1.18.x` line and does NOT track upstream main. + +## Divergence policy + +- OC Kit is designed to extend OpenCode through existing extension points + (skills, agents, commands, plugins, config, tools) rather than invasive core + patches. +- We do not re-pin to upstream in Phase 0/1. Re-evaluate upstream sync when a + concrete merge target is agreed. +- Custom patches to core OpenCode files are documented inline at each patch + site and summarized in this file. + +## Commit conventions + +- Conventional commit style: `type(scope): summary`. +- Commit author email: `cavangcute478@gmail.com`. +- Verified via GitHub Actions CI (`test.yml`, `typecheck.yml`) on PRs into `main`. diff --git a/packages/core/src/ripgrep/binary.ts b/packages/core/src/ripgrep/binary.ts index 762a8e613e..af9f41825c 100644 --- a/packages/core/src/ripgrep/binary.ts +++ b/packages/core/src/ripgrep/binary.ts @@ -57,11 +57,18 @@ export namespace RipgrepBinary { if (config.extension === "zip") { const shell = (yield* Effect.sync(() => which("powershell.exe") ?? which("pwsh.exe"))) ?? "powershell.exe" + // `Expand-Archive` (PowerShell 5.1) hangs indefinitely on shared Windows + // CI runners while staging files (observed SIGTERM at 5/30/60s test + // timeouts). `[IO.Compression.ZipFile]::ExtractToDirectory` is the same + // .NET BCL the setup-bun action uses to extract a comparable zip in + // under 1s on the same runner. Key differences: Add-Type is unconditional + // (progress bars are off by default in -NonInteractive), and the + // destination is a scoped temp dir so no :Delete\:\*:\* flags are needed. const result = yield* run(shell, [ "-NoProfile", "-NonInteractive", "-Command", - `$global:ProgressPreference = 'SilentlyContinue'; Expand-Archive -LiteralPath '${archive.replaceAll("'", "''")}' -DestinationPath '${dir.replaceAll("'", "''")}' -Force`, + `Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory('${archive.replaceAll("'", "''")}', '${dir.replaceAll("'", "''")}')`, ]) if (result.code !== 0) throw new Error( diff --git a/packages/core/src/v1/config/config.ts b/packages/core/src/v1/config/config.ts index 7ebb4b69b0..2c2a9490e0 100644 --- a/packages/core/src/v1/config/config.ts +++ b/packages/core/src/v1/config/config.ts @@ -187,6 +187,26 @@ export const Info = Schema.Struct({ }), }), ), + oc_kit: Schema.optional( + Schema.Struct({ + enabled: Schema.optional(Schema.Boolean).annotate({ description: "Enable OC Kit" }), + default_kit: Schema.optional(Schema.String).annotate({ description: "Kit used by default" }), + default_mode: Schema.optional(Schema.Literals(["fast", "normal", "deep", "autonomous"])).annotate({ + description: "Default OC Kit workflow mode", + }), + auto_workflow: Schema.optional(Schema.Boolean).annotate({ + description: "Auto-select a workflow from a task description", + }), + auto_review: Schema.optional(Schema.Boolean).annotate({ description: "Review after implementation" }), + auto_test: Schema.optional(Schema.Boolean).annotate({ description: "Test after implementation" }), + checkpoint: Schema.optional(Schema.Boolean).annotate({ + description: "Snapshot managed files before risky kit updates", + }), + telemetry: Schema.optional(Schema.Boolean).annotate({ + description: "Send anonymous operational metrics (default: off)", + }), + }), + ).annotate({ description: "OC Kit workflow/agent engineering layer configuration" }), }).annotate({ identifier: "Config" }) export type Info = DeepMutable> diff --git a/packages/core/test/filesystem/search.test.ts b/packages/core/test/filesystem/search.test.ts index 6c47c85e96..fe7ba180a6 100644 --- a/packages/core/test/filesystem/search.test.ts +++ b/packages/core/test/filesystem/search.test.ts @@ -26,6 +26,7 @@ describe("Ripgrep", () => { expect(result.map((item) => item.path)).toEqual([RelativePath.make("src/match.ts")]) }), ), + 30_000, // bun per-test timeout (defaults to 5000); cold ripgrep binary install on Windows CI is slow ) it.live("greps files with include filtering", () => @@ -40,5 +41,6 @@ describe("Ripgrep", () => { expect(result[0]?.submatches[0]?.text).toBe("needle") }), ), + 30_000, // bun per-test timeout (defaults to 5000); cold ripgrep binary install on Windows CI is slow ) }) diff --git a/packages/core/test/repository-cache.test.ts b/packages/core/test/repository-cache.test.ts index 4433583bf7..b68e4c7422 100644 --- a/packages/core/test/repository-cache.test.ts +++ b/packages/core/test/repository-cache.test.ts @@ -31,6 +31,7 @@ describe("RepositoryCache", () => { expect(yield* read(path.join(localPath, "README.md"))).toBe("one\n") }).pipe(Effect.provide(cacheLayer(fixture.root))), ), + 30_000, // bun per-test timeout (defaults to 5000); real git subprocesses need headroom on CI ) it.live("serializes concurrent materialization for the same checkout", () => @@ -46,6 +47,7 @@ describe("RepositoryCache", () => { expect(results[0].localPath).toBe(results[1].localPath) }).pipe(Effect.provide(cacheLayer(fixture.root))), ), + 30_000, // bun per-test timeout (defaults to 5000); real git subprocesses need headroom on CI ) it.live("replaces an existing checkout whose origin does not match", () => @@ -64,6 +66,7 @@ describe("RepositoryCache", () => { expect(yield* exists(path.join(replaced.localPath, "stale.txt"))).toBe(false) }).pipe(Effect.provide(cacheLayer(fixture.root))), ), + 30_000, // bun per-test timeout (defaults to 5000); real git subprocesses need headroom on CI ) it.live("keeps branch checkouts isolated from branchless refreshes", () => @@ -86,6 +89,7 @@ describe("RepositoryCache", () => { expect(yield* read(path.join(cached.localPath, "README.md"))).toBe("two\n") }).pipe(Effect.provide(cacheLayer(fixture.root))), ), + 30_000, // bun per-test timeout (defaults to 5000); real git subprocesses need headroom on CI ) it.live("does not mistake an enclosing repository for the cache checkout", () => @@ -99,6 +103,7 @@ describe("RepositoryCache", () => { expect(yield* read(path.join(result.localPath, "README.md"))).toBe("one\n") }).pipe(Effect.provide(cacheLayer(fixture.root))), ), + 30_000, // bun per-test timeout (defaults to 5000); real git subprocesses need headroom on CI ) it.live("returns typed validation and clone failures", () => @@ -119,6 +124,7 @@ describe("RepositoryCache", () => { expect(cloneFailure).toBeInstanceOf(RepositoryCache.CloneFailedError) }).pipe(Effect.provide(cacheLayer(fixture.root))), ), + 30_000, // bun per-test timeout (defaults to 5000); real git subprocesses need headroom on CI ) }) diff --git a/packages/core/test/ripgrep.test.ts b/packages/core/test/ripgrep.test.ts index 5695af0009..d9b24bac11 100644 --- a/packages/core/test/ripgrep.test.ts +++ b/packages/core/test/ripgrep.test.ts @@ -29,6 +29,7 @@ describe("Ripgrep", () => { }), (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()), ), + 60_000, // bun per-test timeout (defaults to 5000); cold ripgrep binary download + Expand-Archive on Windows CI can exceed 30s ) it.live("never includes git metadata", () => @@ -61,6 +62,7 @@ describe("Ripgrep", () => { }), (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()), ), + 60_000, // bun per-test timeout (defaults to 5000); cold ripgrep binary download + Expand-Archive on Windows CI can exceed 30s ) it.live("does not split surrogate pairs in oversized line previews", () => Effect.acquireUseRelease( @@ -81,5 +83,6 @@ describe("Ripgrep", () => { }), (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()), ), + 60_000, // bun per-test timeout (defaults to 5000); cold ripgrep binary download + Expand-Archive on Windows CI can exceed 30s ) }) diff --git a/packages/core/test/snapshot.test.ts b/packages/core/test/snapshot.test.ts index 5e01fefc17..6c416d21ae 100644 --- a/packages/core/test/snapshot.test.ts +++ b/packages/core/test/snapshot.test.ts @@ -65,6 +65,7 @@ describe("Snapshot", () => { }), (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()), ), + 30_000, // bun per-test timeout (defaults to 5000); real git subprocesses need headroom on Windows CI ) testEffect(Layer.empty).live("treats capture outside Git as unavailable", () => diff --git a/packages/core/test/util/effect-flock.test.ts b/packages/core/test/util/effect-flock.test.ts index d7c412851e..5c35bcb9dc 100644 --- a/packages/core/test/util/effect-flock.test.ts +++ b/packages/core/test/util/effect-flock.test.ts @@ -372,7 +372,7 @@ describe("util.effect-flock", () => { const proc = spawnWorker({ key: "eflock:crash", dir, ready, holdMs: 120_000 }) try { - await waitForFile(ready, 5_000) + await waitForFile(ready, 15_000) await stopWorker(proc) // Backdate lock files so they're past STALE_MS (60s) diff --git a/packages/opencode/src/ockit/checkpoint.ts b/packages/opencode/src/ockit/checkpoint.ts new file mode 100644 index 0000000000..a8dc7fbbbf --- /dev/null +++ b/packages/opencode/src/ockit/checkpoint.ts @@ -0,0 +1,72 @@ +// OC Kit checkpoints: a snapshot of managed files taken before a +// risky operation (kit install/update/rollback). Each checkpoint records the +// kit version, operation, timestamp, and per-file sha256 so the operation can +// be rolled back to a known-good state. + +import { Effect, Schema } from "effect" +import { FSUtil } from "@opencode-ai/core/fs-util" +import { Hash } from "@opencode-ai/core/util/hash" +import { Checkpoint } from "./types" + +/** Default directory where checkpoint snapshots are stored. */ +export const CHECKPOINT_DIR = ".oc/state/checkpoints" + +export class CheckpointError extends Schema.TaggedErrorClass()("OCKitCheckpointError", { + path: Schema.String, + message: Schema.optional(Schema.String), +}) { + override get message(): string { + return `OC Kit checkpoint: ${this.path}${this.message ? ` — ${this.message}` : ""}` + } +} + +/** Write a checkpoint file for the given operation + managed files. */ +export const create = Effect.fn("OCKit.checkpoint.create")(function* (input: { + root: string + kit: string + kitVersion: string + operation: string + session?: string + workflow?: string + files: Record +}) { + const fs = yield* FSUtil.Service + const id = `${Date.now()}-${input.kit}-${input.operation}` + const checkpoint: Checkpoint = { + kit: input.kit, + kit_version: input.kitVersion, + operation: input.operation, + timestamp: new Date().toISOString(), + session: input.session, + workflow: input.workflow, + files: Object.fromEntries(Object.entries(input.files).map(([file, content]) => [file, Hash.sha256(content)])), + } + const path = `${input.root}/${CHECKPOINT_DIR}/${id}.json` + yield* fs.writeWithDirs(path, JSON.stringify(checkpoint, null, 2)) + return path +}) + +/** List checkpoint ids for a kit/operation, newest first. */ +export const list = Effect.fn("OCKit.checkpoint.list")(function* (root: string, kit?: string) { + const fs = yield* FSUtil.Service + const dir = `${root}/${CHECKPOINT_DIR}` + if (!(yield* fs.isDir(dir))) return [] as string[] + const entries = yield* fs.readDirectoryEntries(dir) + const files = entries.filter((entry) => entry.type === "file" && entry.name.endsWith(".json")) + const ids = files.map((entry) => entry.name.replace(/\.json$/, "")) + return kit ? ids.filter((id) => id.includes(`-${kit}-`)).toReversed() : ids.toReversed() +}) + +/** Read a single checkpoint by id. */ +export const read = Effect.fn("OCKit.checkpoint.read")(function* (root: string, id: string) { + const fs = yield* FSUtil.Service + const path = `${root}/${CHECKPOINT_DIR}/${id}.json` + const raw = yield* fs.readFileStringSafe(path) + if (raw === undefined) return yield* new CheckpointError({ path, message: `No checkpoint "${id}"` }) + const decoded = yield* Schema.decodeUnknownEffect(Checkpoint)(JSON.parse(raw)).pipe( + Effect.mapError((err) => new CheckpointError({ path, message: String(err) })), + ) + return decoded +}) + +export * as Checkpoint from "./checkpoint" \ No newline at end of file diff --git a/packages/opencode/src/ockit/config.ts b/packages/opencode/src/ockit/config.ts new file mode 100644 index 0000000000..8a515d3a50 --- /dev/null +++ b/packages/opencode/src/ockit/config.ts @@ -0,0 +1,37 @@ +// OC Kit config access. The `oc_kit` section lives in opencode.json under +// ConfigV1.Info (schema in packages/core/src/v1/config/config.ts). This module +// exposes typed accessors so OC Kit runtime code reads its settings through the +// existing Config service instead of a parallel config system. + +import { Effect, Option, Schema } from "effect" +import { Config } from "@/config/config" +import { OCKitConfig, type OCKitConfig as OCKitConfigType } from "./types" + +export const defaults: OCKitConfigType = { + enabled: true, + default_kit: "engineer", + default_mode: "normal", + auto_workflow: true, + auto_review: true, + auto_test: true, + checkpoint: true, + telemetry: false, +} + +/** Read the effective OC Kit config, applying defaults over the user's values. */ +export const read = Effect.fn("OCKit.config")(function* () { + const config = yield* Config.Service + const cfg = yield* config.get() + const user = cfg.oc_kit ?? {} + return { ...defaults, ...user } +}) + +/** True when OC Kit is enabled in the current project config. */ +export const enabled = Effect.fn("OCKit.config.enabled")(function* () { + return (yield* read()).enabled === true +}) + +/** Validate a raw `oc_kit` value structurally (used when parsing config). */ +export function validate(input: unknown): OCKitConfigType | undefined { + return Option.getOrUndefined(Schema.decodeUnknownOption(OCKitConfig)(input)) +} diff --git a/packages/opencode/src/ockit/index.ts b/packages/opencode/src/ockit/index.ts new file mode 100644 index 0000000000..4e9d252903 --- /dev/null +++ b/packages/opencode/src/ockit/index.ts @@ -0,0 +1,31 @@ +// OC Kit — workflow/agent engineering layer for OpenCode. +// Phase 1 scope: domain types + manifest/registry/resolver/ownership/checkpoint +// + config access. Workflow engine, hooks, artifact manager, CLI, registry +// backend, and installer arrive in later phases. + +export * as OCKitTypes from "./types" +export * as OCKitManifest from "./manifest" +export * as OCKitRegistry from "./registry" +export * as OCKitResolver from "./resolver" +export * as OCKitOwnership from "./ownership" +export * as OCKitCheckpoint from "./checkpoint" +export * as OCKitConfigModule from "./config" + +export { + Kit, + KitSkill, + KitAgent, + Workflow, + WorkflowStep, + KitHook, + WorkflowRun, + WorkflowState, + Mode, + Artifact, + Checkpoint, + OwnershipManifest, + OwnershipEntry, + OCKitConfig, +} from "./types" + +export { Service as Registry, node as registryNode } from "./registry" diff --git a/packages/opencode/src/ockit/manifest.ts b/packages/opencode/src/ockit/manifest.ts new file mode 100644 index 0000000000..322a8a5f1e --- /dev/null +++ b/packages/opencode/src/ockit/manifest.ts @@ -0,0 +1,82 @@ +// Kit manifest loading and validation. A kit is a directory containing a +// manifest file (`kit.json` or `kit.yaml`) plus optional skills/agents/ +// workflows/hooks/rules directories. Manifest data is validated structurally +// against the OC Kit schema so a malformed kit fails with a typed error. + +import { Effect, Schema } from "effect" +import { FSUtil } from "@opencode-ai/core/fs-util" +import { Kit, KitSkill, KitAgent, Workflow, KitHook } from "./types" + +export const KIT_MANIFEST_JSON = "kit.json" +export const KIT_MANIFEST_YAML = "kit.yaml" + +export class ManifestError extends Schema.TaggedErrorClass()("OCKitManifestError", { + path: Schema.String, + message: Schema.optional(Schema.String), +}) {} + +/** + * Read and validate the manifest of a kit directory. Prefers `kit.json`; + * falls back to `kit.yaml` (parsed via Bun's built-in YAML support, so no + * extra dependency). Returns the typed kit manifest, or a ManifestError when + * neither manifest exists or the content does not validate. + */ +export const loadManifest = Effect.fn("OCKit.manifest")(function* (kitDir: string) { + const fs = yield* FSUtil.Service + + const jsonPath = `${kitDir}/${KIT_MANIFEST_JSON}` + const yamlPath = `${kitDir}/${KIT_MANIFEST_YAML}` + + const hasJson = yield* fs.exists(jsonPath) + const hasYaml = yield* fs.exists(yamlPath) + if (!hasJson && !hasYaml) { + return yield* new ManifestError({ + path: kitDir, + message: `No kit manifest found (expected ${KIT_MANIFEST_JSON} or ${KIT_MANIFEST_YAML})`, + }) + } + + const path = hasJson ? jsonPath : yamlPath + const source = yield* fs.readFileStringSafe(path) + if (source === undefined) { + return yield* new ManifestError({ path, message: `Unable to read ${path}` }) + } + const raw = yield* Effect.try({ + try: () => (hasJson ? JSON.parse(source) : Bun.YAML.parse(source)), + catch: (cause) => new ManifestError({ path, message: `Invalid ${KIT_MANIFEST_JSON} syntax: ${String(cause)}` }), + }) + + const decoded = yield* Schema.decodeUnknownEffect(Kit)(raw).pipe( + Effect.mapError((err) => new ManifestError({ path, message: String(err) })), + ) + + return decoded +}) + +/** + * Load the skill/agent/workflow/hook definitions embedded in a kit's + * directory tree (`.opencode`-style SKILL.md/agent files are NOT loaded here — + * they surface through the existing OpenCode Skill/Agent services). This + * resolves the declarative skill/agent definitions from `kit.yaml`/`kit.json` + * manifests into runnable metadata. + */ +export function resolveKitDeclarations(kit: Kit) { + const skills = (kit.skills ?? []).map((skill) => SkillDeclaration(skill)) + const agents = (kit.agents ?? []).map((agent) => AgentDeclaration(agent)) + const workflows = (kit.workflows ?? []).map((workflow) => WorkflowDeclaration(workflow)) + const hooks = (kit.hooks ?? []).map((hook) => HookDeclaration(hook)) + return { skills, agents, workflows, hooks } +} + +function SkillDeclaration(skill: KitSkill) { + return { type: "skill" as const, ...skill } +} +function AgentDeclaration(agent: KitAgent) { + return { type: "agent" as const, ...agent } +} +function WorkflowDeclaration(workflow: Workflow) { + return { type: "workflow" as const, ...workflow } +} +function HookDeclaration(hook: KitHook) { + return { type: "hook" as const, ...hook } +} diff --git a/packages/opencode/src/ockit/ownership.ts b/packages/opencode/src/ockit/ownership.ts new file mode 100644 index 0000000000..b905337e30 --- /dev/null +++ b/packages/opencode/src/ockit/ownership.ts @@ -0,0 +1,126 @@ +// OC Kit ownership model: tracks which kit owns a managed file, +// at which version, and the sha256 it shipped. Used to make kit update/rollback +// safe — files the user has modified since install are preserved, never +// overwritten, and conflicts between kits are detected. + +import { Effect, Schema } from "effect" +import { FSUtil } from "@opencode-ai/core/fs-util" +import { Hash } from "@opencode-ai/core/util/hash" +import { OwnershipManifest, type OwnershipEntry } from "./types" + +/** Default location of the ownership manifest inside `.oc/state`. */ +export const OWNERSHIP_MANIFEST_PATH = ".oc/state/ownership.json" + +export class OwnershipError extends Schema.TaggedErrorClass()("OCKitOwnershipError", { + path: Schema.String, + message: Schema.optional(Schema.String), +}) { + override get message(): string { + return `OC Kit ownership: ${this.path}${this.message ? ` — ${this.message}` : ""}` + } +} + +/** Load the ownership manifest, returning an empty manifest when absent. */ +export const loadOwnership = Effect.fn("OCKit.ownership.load")(function* (root: string) { + const fs = yield* FSUtil.Service + const path = `${root}/${OWNERSHIP_MANIFEST_PATH}` + const raw = yield* fs.readFileStringSafe(path) + if (raw === undefined) return { files: {} } + const decoded = yield* Schema.decodeUnknownEffect(OwnershipManifest)(JSON.parse(raw)).pipe( + Effect.mapError((err) => new OwnershipError({ path, message: String(err) })), + ) + return decoded +}) + +/** Persist an ownership manifest (creating parent directories as needed). */ +export const saveOwnership = Effect.fn("OCKit.ownership.save")(function* (root: string, manifest: OwnershipManifest) { + const fs = yield* FSUtil.Service + const path = `${root}/${OWNERSHIP_MANIFEST_PATH}` + yield* fs.writeWithDirs(path, JSON.stringify(manifest, null, 2)) +}) + +/** + * Claim ownership of files shipped by a kit: records each file's owner, kit, + * version, and the sha256 of the content being installed. Existing entries are + * checked for conflicting owners (a file may only be owned by one kit). + */ +export const claim = Effect.fn("OCKit.ownership.claim")(function* ( + manifest: OwnershipManifest, + kit: string, + version: string, + files: Record, +) { + // Work on a mutable copy — the manifest schema type is read-only. + const next: Record = { ...manifest.files } + const conflicts: string[] = [] + for (const file of Object.keys(files)) { + const existing = next[file] + if (existing && existing.kit !== kit) conflicts.push(file) + } + if (conflicts.length > 0) { + return yield* new OwnershipError({ + path: conflicts.join(", "), + message: `These files are already owned by another kit`, + }) + } + for (const [file, content] of Object.entries(files)) { + const entry: OwnershipEntry = { owner: "oc-kit", kit, version, sha256: Hash.sha256(content) } + next[file] = entry + } + return { files: next } +}) + +/** + * Detect user edits: compares the current sha256 of a managed file against the + * sha256 recorded at install time. Returns the list of modified files. + */ +export const detectUserEdits = Effect.fn("OCKit.ownership.detectEdits")(function* ( + fs: FSUtil.Interface, + root: string, + manifest: OwnershipManifest, +) { + const modified: string[] = [] + for (const [file, entry] of Object.entries(manifest.files)) { + const content = yield* fs.readFileStringSafe(`${root}/${file}`) + if (content !== undefined && Hash.sha256(content) !== entry.sha256) modified.push(file) + } + return modified +}) + +/** + * Decide per-file update action: + * - owned by us and unmodified → "replace" + * - owned by us but user-modified → "preserve" (warn) + * - not owned (user file) → "preserve" + */ +export const planUpdate = Effect.fn("OCKit.ownership.planUpdate")(function* ( + fs: FSUtil.Interface, + root: string, + manifest: OwnershipManifest, + kit: string, + files: Record, +) { + const plan: { replace: string[]; preserve: string[]; warn: string[] } = { replace: [], preserve: [], warn: [] } + const handled = new Set() + for (const file of Object.keys(files)) { + handled.add(file) + const entry = manifest.files[file] + if (!entry || entry.kit !== kit) { + plan.preserve.push(file) + continue + } + const content = yield* fs.readFileStringSafe(`${root}/${file}`) + if (content === undefined || Hash.sha256(content) === entry.sha256) plan.replace.push(file) + else plan.warn.push(file) + } + // Files the manifest tracks but this update does not touch are never + // replaced — preserve anything we do not own so another kit's or the + // user's work is not overwritten. + for (const [file, entry] of Object.entries(manifest.files)) { + if (!handled.has(file) && entry.kit !== kit) plan.preserve.push(file) + } + plan.warn.forEach((file) => plan.preserve.push(file)) + return plan +}) + +export * as Ownership from "./ownership" \ No newline at end of file diff --git a/packages/opencode/src/ockit/registry.ts b/packages/opencode/src/ockit/registry.ts new file mode 100644 index 0000000000..bdb39af05e --- /dev/null +++ b/packages/opencode/src/ockit/registry.ts @@ -0,0 +1,129 @@ +// OC Kit registry: discovers installed kits across the global config dir and +// project `.opencode/kits` dirs, indexes their manifests, and resolves a kit +// by id. Loads lazily through the instance state so startup cost stays ~0 when +// OC Kit is disabled. + +import { Effect, Layer, Context, Schema } from "effect" +import { FSUtil } from "@opencode-ai/core/fs-util" +import { Global } from "@opencode-ai/core/global" +import { InstanceState } from "@/effect/instance-state" +import { Config } from "@/config/config" +import { LayerNode } from "@opencode-ai/core/effect/layer-node" +import path from "path" +import { loadManifest } from "./manifest" +import { Kit } from "./types" + +/** Directory name that holds installed kits (global and per-project). */ +export const KITS_DIR = "kits" + +export class NotFoundError extends Schema.TaggedErrorClass()("OCKit.NotFoundError", { + id: Schema.String, + available: Schema.Array(Schema.String), +}) {} + +type State = { + kits: Record + dirs: string[] +} + +export interface Interface { + readonly all: () => Effect.Effect + readonly get: (id: string) => Effect.Effect + readonly require: (id: string) => Effect.Effect + readonly dirs: () => Effect.Effect +} + +export class Service extends Context.Service()("@opencode/OCKitRegistry") {} + +const layer = Layer.effect( + Service, + Effect.gen(function* () { + const config = yield* Config.Service + const fs = yield* FSUtil.Service + + const indexKit = Effect.fn("OCKitRegistry.indexKit")(function* ( + fs: FSUtil.Interface, + kitDir: string, + kits: State["kits"], + dirs: string[], + ) { + const manifest = yield* loadManifest(kitDir).pipe( + Effect.provideService(FSUtil.Service, fs), + Effect.catchTag("OCKitManifestError", (err) => + Effect.logWarning("skipping invalid kit manifest", { dir: kitDir, error: err.message }).pipe( + Effect.as(undefined), + ), + ), + Effect.orDie, + ) + if (!manifest) return + if (kits[manifest.id]) { + yield* Effect.logWarning("duplicate kit id, keeping first", { id: manifest.id, dir: kitDir }) + return + } + kits[manifest.id] = manifest + dirs.push(kitDir) + }) + + const state = yield* InstanceState.make( + Effect.fn("OCKitRegistry.state")(function* (ctx) { + const kits: Record = {} + const dirs: string[] = [] + + // Global: /kits// + const globalKits = path.join(Global.Path.config, KITS_DIR) + if (yield* fs.isDir(globalKits)) { + for (const entry of yield* fs.readDirectoryEntries(globalKits).pipe(Effect.orDie)) { + if (entry.type !== "directory") continue + yield* indexKit(fs, path.join(globalKits, entry.name), kits, dirs) + } + } + + // Project: /kits/ from each config directory (up-find of .opencode). + const configDirs = yield* config.directories() + for (const dir of configDirs) { + const projectKits = path.join(dir, KITS_DIR) + if (!(yield* fs.isDir(projectKits))) continue + for (const entry of yield* fs.readDirectoryEntries(projectKits).pipe(Effect.orDie)) { + if (entry.type !== "directory") continue + yield* indexKit(fs, path.join(projectKits, entry.name), kits, dirs) + } + } + + return { kits, dirs } + }), + ) + + const all = Effect.fn("OCKitRegistry.all")(function* () { + const s = yield* InstanceState.get(state) + return Object.values(s.kits) + }) + + const get = Effect.fn("OCKitRegistry.get")(function* (id: string) { + const s = yield* InstanceState.get(state) + return s.kits[id] + }) + + const require = Effect.fn("OCKitRegistry.require")(function* (id: string) { + const s = yield* InstanceState.get(state) + const kit = s.kits[id] + if (kit) return kit + return yield* new NotFoundError({ id, available: Object.keys(s.kits).toSorted() }) + }) + + const dirs = Effect.fn("OCKitRegistry.dirs")(function* () { + const s = yield* InstanceState.get(state) + return s.dirs + }) + + return Service.of({ all, get, require, dirs }) + }), +) + +export const node = LayerNode.make({ + service: Service, + layer, + deps: [Config.node, FSUtil.node], +}) + +export * as Registry from "./registry" diff --git a/packages/opencode/src/ockit/resolver.ts b/packages/opencode/src/ockit/resolver.ts new file mode 100644 index 0000000000..744bfc94af --- /dev/null +++ b/packages/opencode/src/ockit/resolver.ts @@ -0,0 +1,77 @@ +// OC Kit resolver: resolve a skill/agent/workflow declared by a kit by name. +// Works purely against the typed kit manifest (no disk I/O), so it is cheap to +// call and trivially testable. + +import { Schema } from "effect" +import { Kit, KitSkill, KitAgent, Workflow, KitHook } from "./types" + +export class ResolveError extends Schema.TaggedErrorClass()("OCKit.ResolveError", { + kind: Schema.Literals(["skill", "agent", "workflow", "hook"]), + id: Schema.String, + kit: Schema.String, +}) { + override get message(): string { + return `OC Kit: no ${this.kind} "${this.id}" declared in kit "${this.kit}"` + } +} + +/** Everything a kit declares, indexed by name for lookup. */ +export interface KitIndex { + readonly kit: Kit + readonly skills: Map + readonly agents: Map + readonly workflows: Map + readonly hooks: Map +} + +/** Build a name-indexed view of a kit's declarations. */ +export function indexKit(kit: Kit): KitIndex { + return { + kit, + skills: new Map((kit.skills ?? []).map((skill) => [skill.id, skill])), + agents: new Map((kit.agents ?? []).map((agent) => [agent.id, agent])), + workflows: new Map((kit.workflows ?? []).map((workflow) => [workflow.id, workflow])), + hooks: new Map((kit.hooks ?? []).map((hook) => [hook.event, hook])), + } +} + +/** Resolve a kit's own declarations by name; throws ResolveError when absent. */ +export function resolveFromKit( + index: KitIndex, + kind: K, + id: string, +): KitIndex[K] extends Map ? V : never { + const found = index[kind].get(id) + if (found) return found as KitIndex[K] extends Map ? V : never + throw new ResolveError({ kind: KIND_LABEL[kind], id, kit: index.kit.id }) +} + +type KitIndexKeys = "skills" | "agents" | "workflows" | "hooks" + +/** Singular label used in error messages, keyed by the plural index key. */ +const KIND_LABEL: Record = { + skills: "skill", + agents: "agent", + workflows: "workflow", + hooks: "hook", +} + +/** + * Resolve a declaration across all installed kits. `registry` is an injected + * async iterator of kits (an Effect), so this stays framework-agnostic and can + * be driven by the Registry.Service in production and by a plain array in tests. + */ +export async function resolveAcross( + kits: Iterable, + kind: KitIndexKeys, + id: string, +): Promise<{ kit: Kit; value: unknown } | undefined> { + for (const kit of kits) { + const index = indexKit(kit) + const value = index[kind].get(id) + if (value) return { kit, value } + } + return undefined +} + +export * as Resolver from "./resolver" diff --git a/packages/opencode/src/ockit/types.ts b/packages/opencode/src/ockit/types.ts new file mode 100644 index 0000000000..8b492e7e8c --- /dev/null +++ b/packages/opencode/src/ockit/types.ts @@ -0,0 +1,159 @@ +// OC Kit core domain types. Every OC Kit primitive (Kit, Skill, Agent, +// Workflow, Hook, Artifact, Checkpoint, Ownership) is an Effect Schema so +// loaded kit manifests validate structurally and errors are typed. + +import { Schema } from "effect" + +/** + * A workflow step references one capability (a skill) plus optional per-step + * overrides. `run` names the skill; `as` is an optional local alias used when + * the same skill is referenced more than once in a workflow. + */ +export const WorkflowStep = Schema.Struct({ + skill: Schema.String, + as: Schema.optional(Schema.String), +}) +export type WorkflowStep = Schema.Schema.Type + +/** A workflow is an ordered sequence of skills with an optional failure handler. */ +export const Workflow = Schema.Struct({ + id: Schema.String, + description: Schema.optional(Schema.String), + steps: Schema.Array(WorkflowStep), + onFailure: Schema.optional(Schema.Array(Schema.String)), +}) +export type Workflow = Schema.Schema.Type + +/** Skill capability declared by a kit. Maps onto OpenCode's SKILL.md format. */ +export const KitSkill = Schema.Struct({ + id: Schema.String, + description: Schema.optional(Schema.String), + agent: Schema.optional(Schema.String), + tools: Schema.optional(Schema.Array(Schema.String)), + permissions: Schema.optional(Schema.Record(Schema.String, Schema.Literals(["allow", "deny"]))), + artifacts: Schema.optional(Schema.Array(Schema.String)), +}) +export type KitSkill = Schema.Schema.Type + +/** Agent profile declared by a kit. Maps onto OpenCode's agent md format. */ +export const KitAgent = Schema.Struct({ + id: Schema.String, + role: Schema.optional(Schema.String), + description: Schema.optional(Schema.String), + tools: Schema.optional(Schema.Record(Schema.String, Schema.Boolean)), + skills: Schema.optional(Schema.Array(Schema.String)), + model: Schema.optional( + Schema.Struct({ + preferred: Schema.String, + fallback: Schema.optional(Schema.Array(Schema.String)), + }), + ), +}) +export type KitAgent = Schema.Schema.Type + +/** A lifecycle hook declared by a kit. `event` names the OC Kit event. */ +export const KitHook = Schema.Struct({ + event: Schema.String, + command: Schema.String, +}) +export type KitHook = Schema.Schema.Type + +/** A kit manifest (`kit.yaml`) describing one workflow package. */ +export const Kit = Schema.Struct({ + id: Schema.String, + name: Schema.String, + version: Schema.String, + runtime: Schema.optional(Schema.Literal("opencode")), + min_opencode: Schema.optional(Schema.String), + description: Schema.optional(Schema.String), + skills: Schema.optional(Schema.Array(KitSkill)), + agents: Schema.optional(Schema.Array(KitAgent)), + workflows: Schema.optional(Schema.Array(Workflow)), + hooks: Schema.optional(Schema.Array(KitHook)), +}) +export type Kit = Schema.Schema.Type + +/** OC Kit runtime modes (fast/normal/deep/autonomous). */ +export const Mode = Schema.Literals(["fast", "normal", "deep", "autonomous"]) +export type Mode = Schema.Schema.Type + +/** Workflow lifecycle states. */ +export const WorkflowState = Schema.Literals([ + "CREATED", + "PLANNING", + "IMPLEMENTING", + "TESTING", + "REVIEWING", + "VERIFYING", + "BLOCKED", + "FAILED", + "COMPLETED", + "CANCELLED", +]) +export type WorkflowState = Schema.Schema.Type + +/** Session-scoped workflow run state. */ +export const WorkflowRun = Schema.Struct({ + session_id: Schema.String, + workflow_id: Schema.String, + run_id: Schema.String, + agent: Schema.optional(Schema.String), + step: Schema.optional(Schema.Number), + state: WorkflowState, +}) +export type WorkflowRun = Schema.Schema.Type + +/** Artifact produced by a workflow. */ +export const Artifact = Schema.Struct({ + id: Schema.String, + type: Schema.String, + workflow_id: Schema.optional(Schema.String), + session_id: Schema.optional(Schema.String), + path: Schema.String, + status: Schema.Literals(["created", "verified", "failed"]), + checksum: Schema.optional(Schema.String), +}) +export type Artifact = Schema.Schema.Type + +/** Checkpoint snapshot before a risky OC Kit operation. */ +export const Checkpoint = Schema.Struct({ + kit: Schema.String, + kit_version: Schema.String, + operation: Schema.String, + timestamp: Schema.String, + session: Schema.optional(Schema.String), + workflow: Schema.optional(Schema.String), + files: Schema.Record(Schema.String, Schema.String), // path -> sha256 +}) +export type Checkpoint = Schema.Schema.Type + +/** + * Ownership manifest entry: tracks which kit owns a managed file, + * at which version, with the sha256 it shipped — so updates/rollbacks can + * detect user edits and conflicting kits. + */ +export const OwnershipEntry = Schema.Struct({ + owner: Schema.Literal("oc-kit"), + kit: Schema.String, + version: Schema.String, + sha256: Schema.String, +}) +export type OwnershipEntry = Schema.Schema.Type + +export const OwnershipManifest = Schema.Struct({ + files: Schema.Record(Schema.String, OwnershipEntry), +}) +export type OwnershipManifest = Schema.Schema.Type + +/** OC Kit config section (`oc_kit` in opencode.json). */ +export const OCKitConfig = Schema.Struct({ + enabled: Schema.optional(Schema.Boolean).annotate({ description: "Enable OC Kit" }), + default_kit: Schema.optional(Schema.String).annotate({ description: "Kit used by default" }), + default_mode: Schema.optional(Mode).annotate({ description: "Default workflow mode" }), + auto_workflow: Schema.optional(Schema.Boolean).annotate({ description: "Auto-select a workflow from a task" }), + auto_review: Schema.optional(Schema.Boolean).annotate({ description: "Review after implementation" }), + auto_test: Schema.optional(Schema.Boolean).annotate({ description: "Test after implementation" }), + checkpoint: Schema.optional(Schema.Boolean).annotate({ description: "Snapshot before risky updates" }), + telemetry: Schema.optional(Schema.Boolean).annotate({ description: "Send anonymous operational metrics" }), +}) +export type OCKitConfig = Schema.Schema.Type diff --git a/packages/opencode/test/cli/run/run-process.test.ts b/packages/opencode/test/cli/run/run-process.test.ts index c98f0cd3c1..f2a35a6813 100644 --- a/packages/opencode/test/cli/run/run-process.test.ts +++ b/packages/opencode/test/cli/run/run-process.test.ts @@ -67,13 +67,22 @@ describe("opencode run (non-interactive subprocess)", () => { // makes the SDK call surface an error promptly so the process exits nonzero. // We assert nonzero exit AND wall-clock under the harness timeout — a hang // would expire the timeout and produce a different (signal-killed) failure. + // + // The 15s figure used to sit at both the subprocess kill timeout and the + // wall-clock bound, but loaded shared runners push `bun` subprocess startup + // (transpile of the CLI entry, plugin init, server boot, SDK call) past 15s, + // so appProc killed the child before a prompt (non-hanging) error could + // self-exit and the test failed on ~15.5s wall-clock. Raise the bound and the + // kill timeout together to 25s: a prompt error self-exits nonzero well under + // 25s; a true hang is killed by the 25s subprocess timeout and still fails + // `toBeLessThan(25_000)`. The 30s harness timeout remains the backstop. cliIt.concurrent( "exits nonzero promptly when the model is unknown (regression for #27371)", ({ opencode }) => Effect.gen(function* () { const result = yield* opencode.run("say hi", { model: "test/nonexistent-model", - timeoutMs: 15_000, + timeoutMs: 25_000, }) expect(result.exitCode).not.toBe(0) // Allow headroom for cold-start jitter on the CI container (the process diff --git a/packages/opencode/test/ockit/checkpoint.test.ts b/packages/opencode/test/ockit/checkpoint.test.ts new file mode 100644 index 0000000000..9d1be09042 --- /dev/null +++ b/packages/opencode/test/ockit/checkpoint.test.ts @@ -0,0 +1,73 @@ +import { describe, expect } from "bun:test" +import { Effect, Layer } from "effect" +import { FSUtil } from "@opencode-ai/core/fs-util" +import { LayerNode } from "@opencode-ai/core/effect/layer-node" +import { testEffect } from "../lib/effect" +import { create, list, read } from "../../src/ockit/checkpoint" +import { Hash } from "@opencode-ai/core/util/hash" +import os from "os" +import path from "path" +import fs from "fs/promises" + +const layer = LayerNode.compile(FSUtil.node) +const it = testEffect(layer) + +/** Create a clean root dir, then remove it when the effect scope closes. */ +function withRoot(use: (root: string) => Effect.Effect) { + return Effect.acquireUseRelease( + Effect.gen(function* () { + const dir = path.join(os.tmpdir(), "ockit-cp-" + Math.random().toString(36).slice(2)) + yield* Effect.promise(() => fs.mkdir(dir, { recursive: true })) + return dir + }), + use, + (dir) => Effect.promise(() => fs.rm(dir, { recursive: true, force: true })), + ) +} + +describe("ockit checkpoint", () => { + it.effect("creates a checkpoint recording file hashes", () => + withRoot((root) => + Effect.gen(function* () { + const path_ = yield* create({ + root, + kit: "engineer", + kitVersion: "1.0.0", + operation: "update", + files: { "a.md": "content" }, + }) + expect(path_).toContain(".oc/state/checkpoints") + const cp = yield* read(root, path_.split("/").pop()!.replace(".json", "")) + expect(cp.kit).toBe("engineer") + expect(cp.files["a.md"]).toBe(Hash.sha256("content")) + }), + ), + ) + + // Needs the real clock: `Effect.sleep` below would otherwise suspend forever + // under the TestClock that `it.effect` layers in (see test/lib/effect.ts). + it.live("lists checkpoints newest first and filters by kit", () => + withRoot((root) => + Effect.gen(function* () { + const one = yield* create({ root, kit: "engineer", kitVersion: "1.0.0", operation: "update", files: {} }) + yield* Effect.sleep("5 millis") + const two = yield* create({ root, kit: "security", kitVersion: "1.0.0", operation: "update", files: {} }) + const idOne = one.split("/").pop()!.replace(".json", "") + const idTwo = two.split("/").pop()!.replace(".json", "") + const engineerIds = yield* list(root, "engineer") + expect(engineerIds).toEqual([idOne]) + const all = yield* list(root) + expect(all.sort()).toEqual([idOne, idTwo].sort()) + }), + ), + ) + + it.effect("fails with CheckpointError reading a missing checkpoint", () => + withRoot((root) => + Effect.gen(function* () { + const result = yield* read(root, "nope").pipe(Effect.exit) + expect(result._tag).toBe("Failure") + }), + ), + ) +}) \ No newline at end of file diff --git a/packages/opencode/test/ockit/config.test.ts b/packages/opencode/test/ockit/config.test.ts new file mode 100644 index 0000000000..a9ee12df96 --- /dev/null +++ b/packages/opencode/test/ockit/config.test.ts @@ -0,0 +1,45 @@ +import { describe, expect } from "bun:test" +import { Effect, Schema } from "effect" +import { it } from "../lib/effect" +import { defaults, validate } from "../../src/ockit/config" +import { OCKitConfig } from "../../src/ockit/types" + +describe("ockit config", () => { + it.effect("applies defaults over user config", () => + Effect.sync(() => { + expect(defaults.enabled).toBe(true) + expect(defaults.default_kit).toBe("engineer") + expect(defaults.default_mode).toBe("normal") + expect(defaults.telemetry).toBe(false) + })) + + it.effect("validates a well-formed oc_kit value", () => + Effect.sync(() => { + const parsed = validate({ enabled: false, default_kit: "security", default_mode: "deep" }) + expect(parsed?.enabled).toBe(false) + expect(parsed?.default_kit).toBe("security") + expect(parsed?.default_mode).toBe("deep") + })) + + it.effect("returns undefined for an invalid oc_kit value", () => + Effect.sync(() => { + expect(validate({ default_mode: "turbo" })).toBeUndefined() + expect(validate("nope")).toBeUndefined() + })) + + it.effect("OCKitConfig schema decodes a full config", () => + Effect.sync(() => { + const decoded = Schema.decodeUnknownSync(OCKitConfig)({ + enabled: true, + default_kit: "engineer", + default_mode: "autonomous", + auto_workflow: true, + auto_review: false, + auto_test: true, + checkpoint: true, + telemetry: true, + }) + expect(decoded.default_mode).toBe("autonomous") + expect(decoded.telemetry).toBe(true) + })) +}) diff --git a/packages/opencode/test/ockit/manifest.test.ts b/packages/opencode/test/ockit/manifest.test.ts new file mode 100644 index 0000000000..55eba355cc --- /dev/null +++ b/packages/opencode/test/ockit/manifest.test.ts @@ -0,0 +1,106 @@ +import { describe, expect } from "bun:test" +import { Cause, Effect, Layer } from "effect" +import { FSUtil } from "@opencode-ai/core/fs-util" +import { LayerNode } from "@opencode-ai/core/effect/layer-node" +import { testEffect } from "../lib/effect" +import { loadManifest } from "../../src/ockit/manifest" +import os from "os" +import path from "path" +import fs from "fs/promises" + +const layer = LayerNode.compile(FSUtil.node) +const it = testEffect(layer) + +const VALID_KIT = { + id: "engineer", + name: "OC Engineer Kit", + version: "1.0.0", + runtime: "opencode", + skills: [{ id: "plan", description: "Create an implementation plan" }], +} + +/** Create a kit dir with the given files, then clean it up when the effect scope closes. */ +function withKitDir(files: Record, use: (dir: string) => Effect.Effect) { + return Effect.acquireUseRelease( + Effect.gen(function* () { + const dir = path.join(os.tmpdir(), "ockit-test-" + Math.random().toString(36).slice(2)) + for (const [rel, content] of Object.entries(files)) { + const full = path.join(dir, rel) + yield* Effect.promise(() => fs.mkdir(path.dirname(full), { recursive: true })) + yield* Effect.promise(() => Bun.write(full, content)) + } + return dir + }), + use, + (dir) => Effect.promise(() => fs.rm(dir, { recursive: true, force: true })), + ) +} + +describe("ockit manifest", () => { + it.effect("loads a valid kit.json manifest", () => + withKitDir({ "kit.json": JSON.stringify(VALID_KIT) }, (dir) => + Effect.gen(function* () { + const kit = yield* loadManifest(dir) + expect(kit.id).toBe("engineer") + expect(kit.skills?.[0]?.id).toBe("plan") + }), + ), + ) + + it.effect("loads a kit.yaml manifest via Bun.YAML", () => + withKitDir( + { + "kit.yaml": `id: engineer +name: OC Engineer Kit +version: 1.0.0 +runtime: opencode +skills: + - id: plan + description: Create an implementation plan +`, + }, + (dir) => + Effect.gen(function* () { + const kit = yield* loadManifest(dir) + expect(kit.id).toBe("engineer") + expect(kit.skills?.[0]?.id).toBe("plan") + }), + ), + ) + + it.effect("prefers kit.json over kit.yaml", () => + withKitDir( + { + "kit.json": JSON.stringify({ ...VALID_KIT, name: "JSON KIT" }), + "kit.yaml": "id: engineer\nname: YAML KIT\nversion: 1.0.0\n", + }, + (dir) => + Effect.gen(function* () { + const kit = yield* loadManifest(dir) + expect(kit.name).toBe("JSON KIT") + }), + ), + ) + + it.effect("fails with ManifestError when no manifest exists", () => + withKitDir({ "README.md": "no manifest here" }, (dir) => + Effect.gen(function* () { + const result = yield* loadManifest(dir).pipe(Effect.exit) + expect(result._tag).toBe("Failure") + if (result._tag === "Failure") { + const error = Cause.squash(result.cause) + expect(String(error)).toContain("No kit manifest found") + } + }), + ), + ) + + it.effect("fails with ManifestError on invalid schema", () => + withKitDir({ "kit.json": JSON.stringify({ id: "engineer" }) }, (dir) => + Effect.gen(function* () { + const result = yield* loadManifest(dir).pipe(Effect.exit) + expect(result._tag).toBe("Failure") + }), + ), + ) +}) \ No newline at end of file diff --git a/packages/opencode/test/ockit/ownership.test.ts b/packages/opencode/test/ockit/ownership.test.ts new file mode 100644 index 0000000000..0059784883 --- /dev/null +++ b/packages/opencode/test/ockit/ownership.test.ts @@ -0,0 +1,108 @@ +import { describe, expect } from "bun:test" +import { Effect, Layer } from "effect" +import { FSUtil } from "@opencode-ai/core/fs-util" +import { LayerNode } from "@opencode-ai/core/effect/layer-node" +import { testEffect } from "../lib/effect" +import { claim, detectUserEdits, planUpdate, loadOwnership, saveOwnership } from "../../src/ockit/ownership" +import { Hash } from "@opencode-ai/core/util/hash" +import type { OwnershipManifest } from "../../src/ockit/types" +import os from "os" +import path from "path" +import fs from "fs/promises" + +const layer = LayerNode.compile(FSUtil.node) +const it = testEffect(layer) + +/** Create a root with `.oc/state`, then remove it when the effect scope closes. */ +function withRoot(use: (root: string) => Effect.Effect) { + return Effect.acquireUseRelease( + Effect.gen(function* () { + const dir = path.join(os.tmpdir(), "ockit-own-" + Math.random().toString(36).slice(2)) + yield* Effect.promise(() => fs.mkdir(path.join(dir, ".oc", "state"), { recursive: true })) + return dir + }), + use, + (dir) => Effect.promise(() => fs.rm(dir, { recursive: true, force: true })), + ) +} + +describe("ockit ownership", () => { + it.effect("claims ownership of shipped files with sha256", () => + Effect.gen(function* () { + const manifest: OwnershipManifest = { files: {} } + const result = yield* claim(manifest, "engineer", "1.0.0", { "a.md": "hello" }) + const entry = result.files["a.md"] + expect(entry?.owner).toBe("oc-kit") + expect(entry?.kit).toBe("engineer") + expect(entry?.sha256).toBe(Hash.sha256("hello")) + }), + ) + + it.effect("rejects a file already owned by another kit", () => + Effect.gen(function* () { + const manifest: OwnershipManifest = { + files: { "a.md": { owner: "oc-kit", kit: "security", version: "1.0.0", sha256: Hash.sha256("x") } }, + } + const result = yield* claim(manifest, "engineer", "1.0.0", { "a.md": "hello" }).pipe(Effect.exit) + expect(result._tag).toBe("Failure") + }), + ) + + it.effect("detects user edits by hash mismatch", () => + withRoot((root) => + Effect.gen(function* () { + yield* Effect.promise(() => Bun.write(path.join(root, "a.md"), "original")) + const manifest: OwnershipManifest = { + files: { "a.md": { owner: "oc-kit", kit: "engineer", version: "1.0.0", sha256: Hash.sha256("original") } }, + } + const fsutil = yield* FSUtil.Service + expect((yield* detectUserEdits(fsutil, root, manifest)).length).toBe(0) + + yield* Effect.promise(() => Bun.write(path.join(root, "a.md"), "edited")) + expect(yield* detectUserEdits(fsutil, root, manifest)).toEqual(["a.md"]) + }), + ), + ) + + it.effect("planUpdate replaces owned/unmodified, preserves modified and foreign files", () => + withRoot((root) => + Effect.gen(function* () { + yield* Effect.promise(() => Bun.write(path.join(root, "owned.md"), "owned content")) + yield* Effect.promise(() => Bun.write(path.join(root, "foreign.md"), "user file")) + const manifest: OwnershipManifest = { + files: { + "owned.md": { owner: "oc-kit", kit: "engineer", version: "1.0.0", sha256: Hash.sha256("owned content") }, + "foreign.md": { owner: "oc-kit", kit: "security", version: "1.0.0", sha256: Hash.sha256("other") }, + }, + } + const fsutil = yield* FSUtil.Service + const plan = yield* planUpdate(fsutil, root, manifest, "engineer", { "owned.md": "new content", "new.md": "x" }) + expect(plan.replace).toEqual(["owned.md"]) + expect(plan.preserve).toContain("foreign.md") + expect(plan.preserve).toContain("new.md") + }), + ), + ) + + it.effect("round-trips an ownership manifest through disk", () => + withRoot((root) => + Effect.gen(function* () { + const manifest: OwnershipManifest = { + files: { "a.md": { owner: "oc-kit", kit: "engineer", version: "1.0.0", sha256: Hash.sha256("a") } }, + } + yield* saveOwnership(root, manifest) + const loaded = yield* loadOwnership(root) + expect(loaded.files["a.md"]?.kit).toBe("engineer") + }), + ), + ) + + it.effect("loadOwnership returns empty manifest when absent", () => + withRoot((root) => + Effect.gen(function* () { + const loaded = yield* loadOwnership(root) + expect(loaded.files).toEqual({}) + }), + ), + ) +}) \ No newline at end of file diff --git a/packages/opencode/test/ockit/registry.test.ts b/packages/opencode/test/ockit/registry.test.ts new file mode 100644 index 0000000000..4aea0d0174 --- /dev/null +++ b/packages/opencode/test/ockit/registry.test.ts @@ -0,0 +1,98 @@ +import { describe, expect } from "bun:test" +import { Effect } from "effect" +import { LayerNode } from "@opencode-ai/core/effect/layer-node" +import { testEffect } from "../lib/effect" +import { Service as Registry, node as registryNode } from "../../src/ockit/registry" +import { TestInstance } from "../fixture/fixture" +import fs from "fs/promises" + +// The registry scans Global.Path.config (== Flag.OPENCODE_CONFIG_DIR when set) +// for /kits//kit.json, and needs a live InstanceRef. Each test +// points OPENCODE_CONFIG_DIR at the tmpdir instance dir, writes kits under it, +// and runs inside that instance (via it.instance) so InstanceState resolves. +const it = testEffect(LayerNode.compile(registryNode)) + +const VALID_KIT = { + id: "engineer", + name: "OC Engineer Kit", + version: "1.0.0", + runtime: "opencode", + skills: [{ id: "plan", description: "Create an implementation plan" }], +} + +function withConfigDir(configDir: string, self: Effect.Effect) { + return Effect.acquireUseRelease( + Effect.sync(() => { + const prev = process.env.OPENCODE_CONFIG_DIR + process.env.OPENCODE_CONFIG_DIR = configDir + return prev + }), + () => self, + (prev) => + Effect.sync(() => { + if (prev === undefined) delete process.env.OPENCODE_CONFIG_DIR + else process.env.OPENCODE_CONFIG_DIR = prev + }), + ) +} + +function writeKit(configDir: string, kitId: string, manifest: unknown = VALID_KIT) { + return Effect.gen(function* () { + yield* Effect.promise(() => fs.mkdir(`${configDir}/kits/${kitId}`, { recursive: true })) + yield* Effect.promise(() => Bun.write(`${configDir}/kits/${kitId}/kit.json`, JSON.stringify(manifest))) + }) +} + +describe("ockit registry", () => { + it.instance( + "indexes a global kit and resolves it by id", + () => + Effect.gen(function* () { + const { directory } = yield* TestInstance + yield* withConfigDir( + directory, + Effect.gen(function* () { + yield* writeKit(directory, "engineer") + const registry = yield* Registry + const result = yield* registry.get("engineer") + expect(result?.name).toBe("OC Engineer Kit") + }), + ) + }), + ) + + it.instance( + "require throws NotFoundError for an unknown kit", + () => + Effect.gen(function* () { + const { directory } = yield* TestInstance + yield* withConfigDir( + directory, + Effect.gen(function* () { + yield* writeKit(directory, "engineer") + const registry = yield* Registry + const result = yield* registry.require("missing").pipe(Effect.exit) + expect(result._tag).toBe("Failure") + }), + ) + }), + ) + + it.instance( + "skips a kit with an invalid manifest", + () => + Effect.gen(function* () { + const { directory } = yield* TestInstance + yield* withConfigDir( + directory, + Effect.gen(function* () { + yield* writeKit(directory, "engineer") + yield* writeKit(directory, "broken", { id: "broken" }) + const registry = yield* Registry + const result = yield* registry.all() + expect(result.map((kit) => kit.id)).toEqual(["engineer"]) + }), + ) + }), + ) +}) diff --git a/packages/opencode/test/ockit/resolver.test.ts b/packages/opencode/test/ockit/resolver.test.ts new file mode 100644 index 0000000000..eafb4aba4f --- /dev/null +++ b/packages/opencode/test/ockit/resolver.test.ts @@ -0,0 +1,59 @@ +import { describe, expect } from "bun:test" +import { Effect } from "effect" +import { it } from "../lib/effect" +import type { Kit } from "../../src/ockit/types" +import { indexKit, resolveFromKit, resolveAcross } from "../../src/ockit/resolver" + +const kit: Kit = { + id: "engineer", + name: "OC Engineer Kit", + version: "1.0.0", + runtime: "opencode", + skills: [{ id: "plan", description: "Create an implementation plan" }], + agents: [{ id: "reviewer", role: "reviewer", tools: { write: false } }], + workflows: [{ id: "feature-development", steps: [{ skill: "plan" }] }], + hooks: [{ event: "workflow:start", command: "echo start" }], +} + +describe("ockit resolver", () => { + it.effect("indexes kit declarations by name", () => + Effect.sync(() => { + const index = indexKit(kit) + expect(index.skills.get("plan")?.description).toBe("Create an implementation plan") + expect(index.agents.get("reviewer")?.role).toBe("reviewer") + expect(index.workflows.get("feature-development")?.steps[0]?.skill).toBe("plan") + expect(index.hooks.get("workflow:start")?.command).toBe("echo start") + })) + + it.effect("resolves an existing declaration", () => + Effect.sync(() => { + const index = indexKit(kit) + expect(resolveFromKit(index, "skills", "plan")?.id).toBe("plan") + })) + + it.effect("throws ResolveError for a missing declaration", () => + Effect.sync(() => { + const index = indexKit(kit) + expect(() => resolveFromKit(index, "skills", "missing")).toThrow("no skill \"missing\"") + })) + + it.effect("resolves across multiple kits, first match wins", () => + Effect.gen(function* () { + const other: Kit = { + id: "security", + name: "OC Security Kit", + version: "1.0.0", + skills: [{ id: "security", description: "Audit" }], + } + const found = yield* Effect.promise(() => resolveAcross([other, kit], "skills", "security")) + expect(found?.kit.id).toBe("security") + const plan = yield* Effect.promise(() => resolveAcross([other, kit], "workflows", "feature-development")) + expect(plan?.kit.id).toBe("engineer") + })) + + it.effect("returns undefined when no kit declares the id", () => + Effect.gen(function* () { + const result = yield* Effect.promise(() => resolveAcross([kit], "skills", "nope")) + expect(result).toBeUndefined() + })) +}) diff --git a/plans/260814-1636-oc-kit-phase0-1/plan.md b/plans/260814-1636-oc-kit-phase0-1/plan.md new file mode 100644 index 0000000000..c440ad4a20 --- /dev/null +++ b/plans/260814-1636-oc-kit-phase0-1/plan.md @@ -0,0 +1,79 @@ +# OC Kit — Phase 0 + Phase 1 + +## Context + +This fork (`vibecoder880/opencode`, based on `anomalyco/opencode`) aims to build **OC Kit**: a workflow/agent engineering layer native to OpenCode, turning it into an Agent Runtime + Workflow OS. The full vision is in `OC-Kit_OpenCode_Integration_Plan.md` (Kit → Skill → Agent → Workflow → Hook → Artifact → Checkpoint, plus CLI, registry, installer). + +This plan covers the first two phases only (per user decision): + +- **Phase 0** — Upstream freeze & codebase audit: verify the vanilla fork builds/tests on CI, pin upstream context, create `UPSTREAM.md`. +- **Phase 1** — OC Kit domain model: `Kit` / `Skill` / `Agent` / `Workflow` / `Hook` / `Artifact` / `Checkpoint` / `Ownership` types + schemas + validators + storage. Exit criteria: load & validate a complete local kit. + +## Workflow contract (user-set, persisted in memory) + +- No local build / `npm i` / project run. Test + build happen on **GitHub Actions CI**. +- Light tests only locally (and locally we lack `bun`, so verification is CI-first). +- Commit each finished unit with **detailed English** messages; one language (English). +- Commit email: `cavangcute478@gmail.com` (already set for this repo). +- Push only when tests pass. + +## Branch strategy (user decision) + +- Work on a **feature branch** (per AGENTS.md: ≤3 words, no `feat/` prefix) and open a **PR into `main`** (updated 2026-08-14: user asked to target `main` directly). +- CI (`test.yml`, `typecheck.yml`) triggers on pushes to the base branch and on PRs — so the PR itself is the CI gate. +- NOTE: `origin/main` is an orphan root (`2b5f70e`) unrelated to local history (`1862432`, which `dev` uses). Feature branches must be rebased onto `origin/main` for GitHub to accept PRs. + +## Findings from audit (Phase 0) + +- Repo is a monorepo; OpenCode runtime lives in `packages/opencode` (version 1.18.18). +- `AGENTS.md` governs style: no aliased imports, no star imports, `@/*` → `./src/*`, `@test/*` → `./test/*`, Effect Schema everywhere, kebab-case names, self-export pattern in `src/config/*`. +- CLI commands: yargs `CommandModule` via `effectCmd` (packages/opencode/src/cli/effect-cmd.ts), registered in `src/index.ts` via `.command(X)`. +- Skill system: `packages/opencode/src/skill/index.ts` — loads `SKILL.md` via `ConfigMarkdown.parse`, exposes `Skill.Service` with `get/require/all/available`. Built-in skill `customize-opencode`. Skills also surface as slash commands in `command/index.ts`. +- Agent system: `src/agent/agent.ts` + `src/config/agent.ts`; agents are `.md` files with frontmatter in `.opencode/agent`. +- Config: `ConfigV1.Info` (packages/core/src/v1/config/config.ts) is an `Effect.Schema.Struct`; new sections are added there + a self-export module in `src/config/`. +- Tool registry: `src/tool/registry.ts` — builtins registered via `Tool.init(...)`, custom from plugins/`.opencode/tool`. +- Tests: `bun:test` in `packages/opencode/test/`, `testEffect(layer)` helper (test/lib/effect.ts), fixtures in `test/fixture/fixture.ts`. CI runs `bun turbo test`. +- Upstream reachable (`anomalyco/opencode`); local fork tracks a different line (1.18.18) than upstream main (has `2.0` branch). We do NOT re-pin to upstream in this phase — document the divergence in `UPSTREAM.md`. + +## Deliverables + +### Phase 0 +- [x] `UPSTREAM.md` at repo root — upstream repo URL, fork divergence notes, custom-patch policy. +- [ ] `dev` branch created from `main` and pushed to origin (so PRs can target it). — note: superseded; PRs now target `main` directly. +- [ ] First feature branch pushed + a trivial PR into `main` runs CI green (baseline). + +### Phase 1 — `packages/opencode/src/ockit/` +- [ ] `types.ts` — Effect Schema for Kit/Skill/Agent/Workflow/Hook/Artifact/Checkpoint/Ownership. +- [ ] `manifest.ts` — kit manifest parse/validate (`kit.yaml` → typed Kit). +- [ ] `registry.ts` — registry of installed kits + kit metadata index. +- [ ] `resolver.ts` — resolve a skill/agent/workflow by name from an installed kit. +- [ ] `ownership.ts` — ownership manifest (`files → {owner, kit, version, sha256}`), conflict detection. +- [ ] `checkpoint.ts` — checkpoint records (before/after hashes, operation, session). +- [ ] `config.ts` — `oc_kit` config schema (enabled, default_kit, default_mode, auto_workflow, checkpoint, telemetry). +- [ ] `index.ts` — export surface + `ockit` namespace. +- [ ] Unit tests in `packages/opencode/test/ockit/` (manifest, registry, resolver, ownership, checkpoint, config). + +Note: workflow engine, hook dispatcher, artifact manager, CLI, installer, registry backend are later phases — NOT in scope here (YAGNI). + +## Files to create + +``` +packages/opencode/src/ockit/{index,types,manifest,registry,resolver,ownership,checkpoint,config}.ts +packages/opencode/test/ockit/*.test.ts +UPSTREAM.md +``` + +## Verification (CI-first) + +1. Feature branch → push → open PR into `main`. +2. GitHub Actions `test.yml` runs `bun turbo test` (unit tests incl. new `ockit` tests) on the PR. +3. `typecheck.yml` runs `bun typecheck`. +4. Only merge PR (to `main`) when both are green. +5. Commit messages: English, conventional format, email `cavangcute478@gmail.com`. + +## Risks / rollback + +- `origin/main` is an orphan root unrelated to local history; rebasing feature branches onto `origin/main` is required for GitHub to accept PRs. +- CI is currently blocked: every test/typecheck workflow uses `blacksmith-*` self-hosted runner labels that are not registered on this standalone repo. Unblock requires registering runners (or switching CI to GitHub-hosted runners). +- Adding `oc_kit` to `ConfigV1.Info` touches the shared config schema — keep it strictly optional so existing projects are unaffected (plan §40 compatibility). +- No runtime wiring yet (no workflow engine / hooks) — no risk to existing OpenCode behavior in this phase.