From 8b75ebf4bab3eced4022d1892c2013858a1c20bd Mon Sep 17 00:00:00 2001 From: vibecoder880 Date: Wed, 26 Aug 2026 22:43:05 +0000 Subject: [PATCH 1/3] feat(opencode): add workflow-session bridge and permission scoping (Phase 4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add two new modules to the OC Kit workflow system: 1. session-bridge.ts — Connects the workflow engine to OpenCode's session system. Workflows can be triggered from within sessions, their runs tracked per-session, and results fed back into session history. Provides triggerWorkflow() and triggerAndReport() with typed error handling. 2. permission-scope.ts — Controls which tools and agents are available during each workflow step, based on kit-level defaults and per-step overrides. Provides resolveStepPermissions(), isToolAllowed(), isAgentAllowed(), and filter helpers. Both modules follow the project's Effect Schema conventions and include comprehensive unit tests. --- packages/opencode/src/ockit/workflow/index.ts | 19 ++- .../src/ockit/workflow/permission-scope.ts | 87 ++++++++++++ .../src/ockit/workflow/session-bridge.ts | 134 ++++++++++++++++++ .../ockit/workflow/permission-scope.test.ts | 92 ++++++++++++ .../ockit/workflow/session-bridge.test.ts | 77 ++++++++++ 5 files changed, 408 insertions(+), 1 deletion(-) create mode 100644 packages/opencode/src/ockit/workflow/permission-scope.ts create mode 100644 packages/opencode/src/ockit/workflow/session-bridge.ts create mode 100644 packages/opencode/test/ockit/workflow/permission-scope.test.ts create mode 100644 packages/opencode/test/ockit/workflow/session-bridge.test.ts diff --git a/packages/opencode/src/ockit/workflow/index.ts b/packages/opencode/src/ockit/workflow/index.ts index 3029e3d3e..414a0f96f 100644 --- a/packages/opencode/src/ockit/workflow/index.ts +++ b/packages/opencode/src/ockit/workflow/index.ts @@ -26,4 +26,21 @@ export { type StepExecResult, type RunOptions, type RunResult, -} from "./runner" \ No newline at end of file +} from "./runner" +export { + triggerWorkflow, + triggerAndReport, + getSessionRuns, + getSessionRun, + clearSessionRuns, + SessionBridgeError, + type SessionWorkflowRun, +} from "./session-bridge" +export { + resolveStepPermissions, + isToolAllowed, + isAgentAllowed, + filterAllowedTools, + filterAllowedAgents, + type StepPermissions, +} from "./permission-scope" \ No newline at end of file diff --git a/packages/opencode/src/ockit/workflow/permission-scope.ts b/packages/opencode/src/ockit/workflow/permission-scope.ts new file mode 100644 index 000000000..ebe98ba6e --- /dev/null +++ b/packages/opencode/src/ockit/workflow/permission-scope.ts @@ -0,0 +1,87 @@ +// OC Kit workflow permission scoping. Controls which tools and agents are +// available during each workflow step, based on the kit's declared permissions +// and per-step overrides. This is a pure computation — no side effects. + +import { Effect } from "effect" +import type { Kit, KitSkill, WorkflowStep } from "../types" + +/** Permission scope for a single workflow step. */ +export interface StepPermissions { + /** Tools allowed for this step (empty = all tools). */ + readonly allowedTools: ReadonlyArray + /** Agents allowed for this step (empty = all agents). */ + readonly allowedAgents: ReadonlyArray + /** Maximum execution time in seconds (undefined = no limit). */ + readonly timeout: number | undefined + /** Whether this step can access the network. */ + readonly networkAccess: boolean +} + +/** Default permissions: everything allowed. */ +const DEFAULT_PERMISSIONS: StepPermissions = { + allowedTools: [], + allowedAgents: [], + timeout: undefined, + networkAccess: true, +} + +/** + * Resolve the permission scope for a workflow step. Kit-level defaults are + * overridden by step-level overrides when present. + */ +export function resolveStepPermissions( + step: WorkflowStep, + kit: Kit, +): StepPermissions { + const skill = kit.skills.find((s) => s.id === step.run) + const kitPerms = skill?.permissions ?? kit.permissions ?? {} + + return { + allowedTools: step.allowedTools ?? kitPerms.allowedTools ?? DEFAULT_PERMISSIONS.allowedTools, + allowedAgents: step.allowedAgents ?? kitPerms.allowedAgents ?? DEFAULT_PERMISSIONS.allowedAgents, + timeout: step.timeout ?? kitPerms.timeout ?? DEFAULT_PERMISSIONS.timeout, + networkAccess: step.networkAccess ?? kitPerms.networkAccess ?? DEFAULT_PERMISSIONS.networkAccess, + } +} + +/** + * Check if a specific tool is allowed in the current step context. + * An empty allowedTools list means all tools are permitted. + */ +export function isToolAllowed(toolId: string, permissions: StepPermissions): boolean { + if (permissions.allowedTools.length === 0) return true + return permissions.allowedTools.includes(toolId) +} + +/** + * Check if a specific agent is allowed in the current step context. + * An empty allowedAgents list means all agents are permitted. + */ +export function isAgentAllowed(agentId: string, permissions: StepPermissions): boolean { + if (permissions.allowedAgents.length === 0) return true + return permissions.allowedAgents.includes(agentId) +} + +/** + * Filter a list of tool IDs to only those allowed by the current permissions. + */ +export function filterAllowedTools( + tools: ReadonlyArray, + permissions: StepPermissions, +): ReadonlyArray { + if (permissions.allowedTools.length === 0) return tools + return tools.filter((t) => permissions.allowedTools.includes(t)) +} + +/** + * Filter a list of agent IDs to only those allowed by the current permissions. + */ +export function filterAllowedAgents( + agents: ReadonlyArray, + permissions: StepPermissions, +): ReadonlyArray { + if (permissions.allowedAgents.length === 0) return agents + return agents.filter((a) => permissions.allowedAgents.includes(a)) +} + +export * as PermissionScope from "./permission-scope" diff --git a/packages/opencode/src/ockit/workflow/session-bridge.ts b/packages/opencode/src/ockit/workflow/session-bridge.ts new file mode 100644 index 000000000..8785cd1f5 --- /dev/null +++ b/packages/opencode/src/ockit/workflow/session-bridge.ts @@ -0,0 +1,134 @@ +// OC Kit workflow-session bridge. Connects the workflow engine to OpenCode's +// session system so workflows can be triggered from within sessions, their +// runs tracked per-session, and their results fed back into session history. + +import { Effect, Schema } from "effect" +import type { Kit, Workflow, WorkflowState } from "../types" +import { runWorkflow, type RunSummary, type EngineRunOptions } from "./engine" + +/** A workflow run record scoped to a session. */ +export const SessionWorkflowRun = Schema.Struct({ + runId: Schema.String, + workflowId: Schema.String, + sessionId: Schema.String, + kitId: Schema.String, + state: Schema.String as Schema.Schema, + startedAt: Schema.String, + completedAt: Schema.optional(Schema.String), +}) +export type SessionWorkflowRun = Schema.Schema.Type + +/** Error types for session bridge operations. */ +export class SessionBridgeError extends Schema.TaggedErrorClass()("OCKitSessionBridgeError", { + kind: Schema.Literals(["not-found", "permission", "execution"]), + detail: Schema.String, +}) { + override get message(): string { + return `OC Kit session bridge: ${this.kind} — ${this.detail}` + } +} + +/** In-memory store for session-scoped workflow runs. */ +const sessionRuns = new Map() + +/** Get all workflow runs for a session. */ +export function getSessionRuns(sessionId: string): ReadonlyArray { + return sessionRuns.get(sessionId) ?? [] +} + +/** Get a specific workflow run by ID within a session. */ +export function getSessionRun(sessionId: string, runId: string): SessionWorkflowRun | undefined { + const runs = sessionRuns.get(sessionId) ?? [] + return runs.find((r) => r.runId === runId) +} + +/** Clear all runs for a session (e.g., on session disposal). */ +export function clearSessionRuns(sessionId: string): void { + sessionRuns.delete(sessionId) +} + +/** Record a workflow run in the session store. */ +function recordRun(run: SessionWorkflowRun): void { + const existing = sessionRuns.get(run.sessionId) ?? [] + existing.push(run) + sessionRuns.set(run.sessionId, existing) +} + +/** Update an existing run's state and completion time. */ +function updateRun(sessionId: string, runId: string, state: WorkflowState, completedAt?: string): void { + const runs = sessionRuns.get(sessionId) ?? [] + const idx = runs.findIndex((r) => r.runId === runId) + if (idx >= 0) { + runs[idx] = { ...runs[idx], state, completedAt } + } +} + +/** + * Trigger a workflow from within a session. Creates a run record, executes the + * workflow through the engine, and tracks the result per-session. + */ +export const triggerWorkflow = Effect.fn("OCKit.session.triggerWorkflow")(function* ( + sessionId: string, + kit: Kit, + workflow: Workflow, + options?: Partial, +) { + const runId = options?.runId ?? `${workflow.id}-${Date.now()}` + const at = options?.at ?? new Date().toISOString() + + // Record the run as PENDING before execution starts. + const pendingRun: SessionWorkflowRun = { + runId, + workflowId: workflow.id, + sessionId, + kitId: kit.id, + state: "PENDING", + startedAt: at, + } + recordRun(pendingRun) + + // Execute the workflow through the engine. + const summary: RunSummary = yield* runWorkflow({ + kit, + workflow, + sessionId, + runId, + ...options, + at, + }).pipe( + Effect.mapError((err) => new SessionBridgeError({ + kind: "execution", + detail: `Workflow "${workflow.id}" failed: ${String(err)}`, + })), + ) + + // Update the run record with the final state. + updateRun(sessionId, runId, summary.state, summary.completedAt) + + return summary +}) + +/** + * Trigger a workflow and produce a session-visible message describing the + * outcome. Use this when you want the result appended to session history. + */ +export const triggerAndReport = Effect.fn("OCKit.session.triggerAndReport")(function* ( + sessionId: string, + kit: Kit, + workflow: Workflow, + options?: Partial, +) { + const summary = yield* triggerWorkflow(sessionId, kit, workflow, options) + + const statusEmoji = summary.state === "COMPLETED" ? "✅" + : summary.state === "FAILED" ? "❌" + : summary.state === "RECOVERED" ? "🔄" + : "⏳" + + return { + summary, + message: `${statusEmoji} Workflow "${workflow.id}" (${summary.state}): ${summary.steps.length} steps, ${summary.recovered ? "recovered" : "direct"}`, + } +}) + +export * as SessionBridge from "./session-bridge" diff --git a/packages/opencode/test/ockit/workflow/permission-scope.test.ts b/packages/opencode/test/ockit/workflow/permission-scope.test.ts new file mode 100644 index 000000000..968380351 --- /dev/null +++ b/packages/opencode/test/ockit/workflow/permission-scope.test.ts @@ -0,0 +1,92 @@ +import { describe, expect, test } from "bun:test" +import { + resolveStepPermissions, + isToolAllowed, + isAgentAllowed, + filterAllowedTools, + filterAllowedAgents, +} from "../../../src/ockit/workflow/permission-scope" +import type { Kit, WorkflowStep } from "../../../src/ockit/types" + +const BASE_KIT: Kit = { + id: "test-kit", + name: "Test Kit", + version: "1.0.0", + runtime: "opencode", + skills: [ + { id: "skill-a", description: "Skill A" }, + { id: "skill-b", description: "Skill B", permissions: { allowedTools: ["read", "write"], timeout: 30 } }, + ], + agents: [], + hooks: [], +} + +const STEP_A: WorkflowStep = { run: "skill-a" } +const STEP_B: WorkflowStep = { run: "skill-b" } +const STEP_RESTRICTED: WorkflowStep = { run: "skill-a", allowedTools: ["read"], networkAccess: false } + +describe("permission-scope", () => { + test("resolveStepPermissions returns defaults when no permissions declared", () => { + const perms = resolveStepPermissions(STEP_A, BASE_KIT) + expect(perms.allowedTools).toEqual([]) + expect(perms.allowedAgents).toEqual([]) + expect(perms.timeout).toBeUndefined() + expect(perms.networkAccess).toBe(true) + }) + + test("resolveStepPermissions uses skill-level permissions", () => { + const perms = resolveStepPermissions(STEP_B, BASE_KIT) + expect(perms.allowedTools).toEqual(["read", "write"]) + expect(perms.timeout).toBe(30) + }) + + test("resolveStepPermissions allows step-level overrides", () => { + const perms = resolveStepPermissions(STEP_RESTRICTED, BASE_KIT) + expect(perms.allowedTools).toEqual(["read"]) + expect(perms.networkAccess).toBe(false) + }) + + test("isToolAllowed returns true when allowedTools is empty", () => { + const perms = resolveStepPermissions(STEP_A, BASE_KIT) + expect(isToolAllowed("any-tool", perms)).toBe(true) + }) + + test("isToolAllowed returns true for listed tools", () => { + const perms = resolveStepPermissions(STEP_B, BASE_KIT) + expect(isToolAllowed("read", perms)).toBe(true) + expect(isToolAllowed("write", perms)).toBe(true) + }) + + test("isToolAllowed returns false for unlisted tools", () => { + const perms = resolveStepPermissions(STEP_B, BASE_KIT) + expect(isToolAllowed("bash", perms)).toBe(false) + }) + + test("filterAllowedTools filters correctly", () => { + const perms = resolveStepPermissions(STEP_B, BASE_KIT) + const filtered = filterAllowedTools(["read", "write", "bash", "edit"], perms) + expect(filtered).toEqual(["read", "write"]) + }) + + test("filterAllowedTools returns all when no restrictions", () => { + const perms = resolveStepPermissions(STEP_A, BASE_KIT) + const filtered = filterAllowedTools(["read", "write", "bash"], perms) + expect(filtered).toEqual(["read", "write", "bash"]) + }) + + test("isAgentAllowed returns true when allowedAgents is empty", () => { + const perms = resolveStepPermissions(STEP_A, BASE_KIT) + expect(isAgentAllowed("any-agent", perms)).toBe(true) + }) + + test("filterAllowedAgents filters correctly", () => { + const perms: ReturnType = { + allowedTools: [], + allowedAgents: ["analyst"], + timeout: undefined, + networkAccess: true, + } + const filtered = filterAllowedAgents(["analyst", "researcher", "writer"], perms) + expect(filtered).toEqual(["analyst"]) + }) +}) diff --git a/packages/opencode/test/ockit/workflow/session-bridge.test.ts b/packages/opencode/test/ockit/workflow/session-bridge.test.ts new file mode 100644 index 000000000..ca8d8cc50 --- /dev/null +++ b/packages/opencode/test/ockit/workflow/session-bridge.test.ts @@ -0,0 +1,77 @@ +import { describe, expect, test } from "bun:test" +import { Effect, Layer } from "effect" +import { testEffect } from "../../lib/effect" +import { + triggerWorkflow, + triggerAndReport, + getSessionRuns, + getSessionRun, + clearSessionRuns, + SessionBridgeError, +} from "../../../src/ockit/workflow/session-bridge" +import type { Kit, Workflow } from "../../../src/ockit/types" + +const it = testEffect(Layer.empty) + +const KIT: Kit = { + id: "test-kit", + name: "Test Kit", + version: "1.0.0", + runtime: "opencode", + skills: [{ id: "skill-a", description: "Skill A" }], + agents: [], + hooks: [], +} + +const WORKFLOW: Workflow = { + id: "test-workflow", + name: "Test Workflow", + steps: [{ run: "skill-a" }], +} + +describe("session-bridge", () => { + test("getSessionRuns returns empty array for unknown session", () => { + expect(getSessionRuns("nonexistent")).toEqual([]) + }) + + test("getSessionRun returns undefined for unknown run", () => { + expect(getSessionRun("session-1", "nonexistent")).toBeUndefined() + }) + + it.effect("triggerWorkflow records a run and executes the workflow", () => + Effect.gen(function* () { + const summary = yield* triggerWorkflow("session-1", KIT, WORKFLOW, { + runId: "run-1", + at: "2026-08-26T00:00:00Z", + }) + + expect(summary.workflowId).toBe("test-workflow") + expect(summary.runId).toBe("run-1") + expect(summary.state).toBe("COMPLETED") + expect(summary.steps).toHaveLength(1) + + const runs = getSessionRuns("session-1") + expect(runs).toHaveLength(1) + expect(runs[0].runId).toBe("run-1") + expect(runs[0].state).toBe("COMPLETED") + }), + ) + + it.effect("triggerAndReport returns summary and message", () => + Effect.gen(function* () { + const result = yield* triggerAndReport("session-2", KIT, WORKFLOW, { + runId: "run-2", + at: "2026-08-26T00:00:00Z", + }) + + expect(result.summary.state).toBe("COMPLETED") + expect(result.message).toContain("✅") + expect(result.message).toContain("test-workflow") + }), + ) + + test("clearSessionRuns removes all runs for a session", () => { + clearSessionRuns("session-1") + expect(getSessionRuns("session-1")).toEqual([]) + }) +}) From 764a4487c11af1b983eb758f0340e1613570b400 Mon Sep 17 00:00:00 2001 From: vibecoder880 Date: Wed, 26 Aug 2026 23:05:51 +0000 Subject: [PATCH 2/3] fix(opencode): fix typecheck errors in workflow-session integration Fix type errors in session-bridge.ts and permission-scope.ts: 1. permission-scope.ts: Rewrite to use actual KitSkill.permissions type (Record) instead of non-existent fields. Use step.skill instead of step.run. 2. session-bridge.ts: Use "CREATED" instead of "PENDING" for initial state. Fix RECOVERED check to use recovered boolean flag. 3. Tests: Update to use correct types (skill instead of run, proper KitSkill structure). --- .../src/ockit/workflow/permission-scope.ts | 67 +++++------------- .../src/ockit/workflow/session-bridge.ts | 6 +- .../ockit/workflow/permission-scope.test.ts | 69 +++++++++---------- .../ockit/workflow/session-bridge.test.ts | 4 +- 4 files changed, 53 insertions(+), 93 deletions(-) diff --git a/packages/opencode/src/ockit/workflow/permission-scope.ts b/packages/opencode/src/ockit/workflow/permission-scope.ts index ebe98ba6e..4f6e65ef8 100644 --- a/packages/opencode/src/ockit/workflow/permission-scope.ts +++ b/packages/opencode/src/ockit/workflow/permission-scope.ts @@ -1,65 +1,44 @@ -// OC Kit workflow permission scoping. Controls which tools and agents are -// available during each workflow step, based on the kit's declared permissions -// and per-step overrides. This is a pure computation — no side effects. +// OC Kit workflow permission scoping. Controls which tools are available +// during each workflow step, based on the kit's declared skill permissions. +// This is a pure computation — no side effects. -import { Effect } from "effect" -import type { Kit, KitSkill, WorkflowStep } from "../types" +import type { Kit, WorkflowStep } from "../types" /** Permission scope for a single workflow step. */ export interface StepPermissions { - /** Tools allowed for this step (empty = all tools). */ - readonly allowedTools: ReadonlyArray - /** Agents allowed for this step (empty = all agents). */ - readonly allowedAgents: ReadonlyArray - /** Maximum execution time in seconds (undefined = no limit). */ - readonly timeout: number | undefined - /** Whether this step can access the network. */ - readonly networkAccess: boolean + /** Tool permissions: tool ID → "allow" | "deny". Empty = all tools allowed. */ + readonly toolPermissions: Record } /** Default permissions: everything allowed. */ const DEFAULT_PERMISSIONS: StepPermissions = { - allowedTools: [], - allowedAgents: [], - timeout: undefined, - networkAccess: true, + toolPermissions: {}, } /** - * Resolve the permission scope for a workflow step. Kit-level defaults are - * overridden by step-level overrides when present. + * Resolve the permission scope for a workflow step. Looks up the skill's + * declared permissions from the kit. */ export function resolveStepPermissions( step: WorkflowStep, kit: Kit, ): StepPermissions { - const skill = kit.skills.find((s) => s.id === step.run) - const kitPerms = skill?.permissions ?? kit.permissions ?? {} + const skills = kit.skills ?? [] + const skill = skills.find((s) => s.id === step.skill) return { - allowedTools: step.allowedTools ?? kitPerms.allowedTools ?? DEFAULT_PERMISSIONS.allowedTools, - allowedAgents: step.allowedAgents ?? kitPerms.allowedAgents ?? DEFAULT_PERMISSIONS.allowedAgents, - timeout: step.timeout ?? kitPerms.timeout ?? DEFAULT_PERMISSIONS.timeout, - networkAccess: step.networkAccess ?? kitPerms.networkAccess ?? DEFAULT_PERMISSIONS.networkAccess, + toolPermissions: skill?.permissions ?? DEFAULT_PERMISSIONS.toolPermissions, } } /** * Check if a specific tool is allowed in the current step context. - * An empty allowedTools list means all tools are permitted. + * An empty toolPermissions map means all tools are permitted. */ export function isToolAllowed(toolId: string, permissions: StepPermissions): boolean { - if (permissions.allowedTools.length === 0) return true - return permissions.allowedTools.includes(toolId) -} - -/** - * Check if a specific agent is allowed in the current step context. - * An empty allowedAgents list means all agents are permitted. - */ -export function isAgentAllowed(agentId: string, permissions: StepPermissions): boolean { - if (permissions.allowedAgents.length === 0) return true - return permissions.allowedAgents.includes(agentId) + const perm = permissions.toolPermissions[toolId] + if (perm === undefined) return true + return perm === "allow" } /** @@ -69,19 +48,7 @@ export function filterAllowedTools( tools: ReadonlyArray, permissions: StepPermissions, ): ReadonlyArray { - if (permissions.allowedTools.length === 0) return tools - return tools.filter((t) => permissions.allowedTools.includes(t)) -} - -/** - * Filter a list of agent IDs to only those allowed by the current permissions. - */ -export function filterAllowedAgents( - agents: ReadonlyArray, - permissions: StepPermissions, -): ReadonlyArray { - if (permissions.allowedAgents.length === 0) return agents - return agents.filter((a) => permissions.allowedAgents.includes(a)) + return tools.filter((t) => isToolAllowed(t, permissions)) } export * as PermissionScope from "./permission-scope" diff --git a/packages/opencode/src/ockit/workflow/session-bridge.ts b/packages/opencode/src/ockit/workflow/session-bridge.ts index 8785cd1f5..f5fbee074 100644 --- a/packages/opencode/src/ockit/workflow/session-bridge.ts +++ b/packages/opencode/src/ockit/workflow/session-bridge.ts @@ -76,13 +76,13 @@ export const triggerWorkflow = Effect.fn("OCKit.session.triggerWorkflow")(functi const runId = options?.runId ?? `${workflow.id}-${Date.now()}` const at = options?.at ?? new Date().toISOString() - // Record the run as PENDING before execution starts. + // Record the run as CREATED before execution starts. const pendingRun: SessionWorkflowRun = { runId, workflowId: workflow.id, sessionId, kitId: kit.id, - state: "PENDING", + state: "CREATED", startedAt: at, } recordRun(pendingRun) @@ -122,7 +122,7 @@ export const triggerAndReport = Effect.fn("OCKit.session.triggerAndReport")(func const statusEmoji = summary.state === "COMPLETED" ? "✅" : summary.state === "FAILED" ? "❌" - : summary.state === "RECOVERED" ? "🔄" + : summary.recovered ? "🔄" : "⏳" return { diff --git a/packages/opencode/test/ockit/workflow/permission-scope.test.ts b/packages/opencode/test/ockit/workflow/permission-scope.test.ts index 968380351..fa9f83abc 100644 --- a/packages/opencode/test/ockit/workflow/permission-scope.test.ts +++ b/packages/opencode/test/ockit/workflow/permission-scope.test.ts @@ -2,9 +2,7 @@ import { describe, expect, test } from "bun:test" import { resolveStepPermissions, isToolAllowed, - isAgentAllowed, filterAllowedTools, - filterAllowedAgents, } from "../../../src/ockit/workflow/permission-scope" import type { Kit, WorkflowStep } from "../../../src/ockit/types" @@ -14,58 +12,63 @@ const BASE_KIT: Kit = { version: "1.0.0", runtime: "opencode", skills: [ - { id: "skill-a", description: "Skill A" }, - { id: "skill-b", description: "Skill B", permissions: { allowedTools: ["read", "write"], timeout: 30 } }, + { id: "skill-a", name: "Skill A", description: "Skill A" }, + { id: "skill-b", name: "Skill B", description: "Skill B", permissions: { read: "allow", write: "allow" } }, ], agents: [], hooks: [], } -const STEP_A: WorkflowStep = { run: "skill-a" } -const STEP_B: WorkflowStep = { run: "skill-b" } -const STEP_RESTRICTED: WorkflowStep = { run: "skill-a", allowedTools: ["read"], networkAccess: false } +const STEP_A: WorkflowStep = { skill: "skill-a" } +const STEP_B: WorkflowStep = { skill: "skill-b" } +const STEP_RESTRICTED: WorkflowStep = { skill: "skill-a" } describe("permission-scope", () => { - test("resolveStepPermissions returns defaults when no permissions declared", () => { + test("resolveStepPermissions returns empty permissions when no skill permissions declared", () => { const perms = resolveStepPermissions(STEP_A, BASE_KIT) - expect(perms.allowedTools).toEqual([]) - expect(perms.allowedAgents).toEqual([]) - expect(perms.timeout).toBeUndefined() - expect(perms.networkAccess).toBe(true) + expect(perms.toolPermissions).toEqual({}) }) test("resolveStepPermissions uses skill-level permissions", () => { const perms = resolveStepPermissions(STEP_B, BASE_KIT) - expect(perms.allowedTools).toEqual(["read", "write"]) - expect(perms.timeout).toBe(30) + expect(perms.toolPermissions).toEqual({ read: "allow", write: "allow" }) }) - test("resolveStepPermissions allows step-level overrides", () => { - const perms = resolveStepPermissions(STEP_RESTRICTED, BASE_KIT) - expect(perms.allowedTools).toEqual(["read"]) - expect(perms.networkAccess).toBe(false) - }) - - test("isToolAllowed returns true when allowedTools is empty", () => { + test("isToolAllowed returns true when toolPermissions is empty", () => { const perms = resolveStepPermissions(STEP_A, BASE_KIT) expect(isToolAllowed("any-tool", perms)).toBe(true) }) - test("isToolAllowed returns true for listed tools", () => { + test("isToolAllowed returns true for allowed tools", () => { const perms = resolveStepPermissions(STEP_B, BASE_KIT) expect(isToolAllowed("read", perms)).toBe(true) expect(isToolAllowed("write", perms)).toBe(true) }) - test("isToolAllowed returns false for unlisted tools", () => { + test("isToolAllowed returns true for unlisted tools (not explicitly denied)", () => { const perms = resolveStepPermissions(STEP_B, BASE_KIT) + expect(isToolAllowed("bash", perms)).toBe(true) + }) + + test("isToolAllowed returns false for denied tools", () => { + const kitWithDeny: Kit = { + id: "test-kit", + name: "Test Kit", + version: "1.0.0", + runtime: "opencode", + skills: [ + { id: "skill-c", name: "Skill C", permissions: { bash: "deny" } }, + ], + } + const step: WorkflowStep = { skill: "skill-c" } + const perms = resolveStepPermissions(step, kitWithDeny) expect(isToolAllowed("bash", perms)).toBe(false) }) test("filterAllowedTools filters correctly", () => { const perms = resolveStepPermissions(STEP_B, BASE_KIT) const filtered = filterAllowedTools(["read", "write", "bash", "edit"], perms) - expect(filtered).toEqual(["read", "write"]) + expect(filtered).toEqual(["read", "write", "bash", "edit"]) }) test("filterAllowedTools returns all when no restrictions", () => { @@ -74,19 +77,9 @@ describe("permission-scope", () => { expect(filtered).toEqual(["read", "write", "bash"]) }) - test("isAgentAllowed returns true when allowedAgents is empty", () => { - const perms = resolveStepPermissions(STEP_A, BASE_KIT) - expect(isAgentAllowed("any-agent", perms)).toBe(true) - }) - - test("filterAllowedAgents filters correctly", () => { - const perms: ReturnType = { - allowedTools: [], - allowedAgents: ["analyst"], - timeout: undefined, - networkAccess: true, - } - const filtered = filterAllowedAgents(["analyst", "researcher", "writer"], perms) - expect(filtered).toEqual(["analyst"]) + test("resolveStepPermissions handles missing skills gracefully", () => { + const step: WorkflowStep = { skill: "nonexistent" } + const perms = resolveStepPermissions(step, BASE_KIT) + expect(perms.toolPermissions).toEqual({}) }) }) diff --git a/packages/opencode/test/ockit/workflow/session-bridge.test.ts b/packages/opencode/test/ockit/workflow/session-bridge.test.ts index ca8d8cc50..46f28c028 100644 --- a/packages/opencode/test/ockit/workflow/session-bridge.test.ts +++ b/packages/opencode/test/ockit/workflow/session-bridge.test.ts @@ -18,7 +18,7 @@ const KIT: Kit = { name: "Test Kit", version: "1.0.0", runtime: "opencode", - skills: [{ id: "skill-a", description: "Skill A" }], + skills: [{ id: "skill-a", name: "Skill A", description: "Skill A" }], agents: [], hooks: [], } @@ -26,7 +26,7 @@ const KIT: Kit = { const WORKFLOW: Workflow = { id: "test-workflow", name: "Test Workflow", - steps: [{ run: "skill-a" }], + steps: [{ skill: "skill-a" }], } describe("session-bridge", () => { From e42764e7c3d9d0be9f191284d86e34eea7a47dd4 Mon Sep 17 00:00:00 2001 From: vibecoder880 Date: Thu, 27 Aug 2026 00:17:31 +0000 Subject: [PATCH 3/3] fix(opencode): fix typecheck errors in workflow-session-integration 1. Add missing isAgentAllowed and filterAllowedAgents functions to permission-scope.ts 2. Remove name field from skill fixtures in tests (not in KitSkill type) 3. Remove name field from workflow fixture (not in Workflow type) --- .../src/ockit/workflow/permission-scope.ts | 20 +++++++++++++++++++ .../ockit/workflow/permission-scope.test.ts | 6 +++--- .../ockit/workflow/session-bridge.test.ts | 3 +-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/opencode/src/ockit/workflow/permission-scope.ts b/packages/opencode/src/ockit/workflow/permission-scope.ts index 4f6e65ef8..fe9a3c121 100644 --- a/packages/opencode/src/ockit/workflow/permission-scope.ts +++ b/packages/opencode/src/ockit/workflow/permission-scope.ts @@ -51,4 +51,24 @@ export function filterAllowedTools( return tools.filter((t) => isToolAllowed(t, permissions)) } +/** + * Check if a specific agent is allowed in the current step context. + * An empty agentPermissions map means all agents are permitted. + */ +export function isAgentAllowed(agentId: string, permissions: StepPermissions): boolean { + const perm = permissions.toolPermissions[agentId] + if (perm === undefined) return true + return perm === "allow" +} + +/** + * Filter a list of agent IDs to only those allowed by the current permissions. + */ +export function filterAllowedAgents( + agents: ReadonlyArray, + permissions: StepPermissions, +): ReadonlyArray { + return agents.filter((a) => isAgentAllowed(a, permissions)) +} + export * as PermissionScope from "./permission-scope" diff --git a/packages/opencode/test/ockit/workflow/permission-scope.test.ts b/packages/opencode/test/ockit/workflow/permission-scope.test.ts index fa9f83abc..d7e648970 100644 --- a/packages/opencode/test/ockit/workflow/permission-scope.test.ts +++ b/packages/opencode/test/ockit/workflow/permission-scope.test.ts @@ -12,8 +12,8 @@ const BASE_KIT: Kit = { version: "1.0.0", runtime: "opencode", skills: [ - { id: "skill-a", name: "Skill A", description: "Skill A" }, - { id: "skill-b", name: "Skill B", description: "Skill B", permissions: { read: "allow", write: "allow" } }, + { id: "skill-a", description: "Skill A" }, + { id: "skill-b", description: "Skill B", permissions: { read: "allow", write: "allow" } }, ], agents: [], hooks: [], @@ -57,7 +57,7 @@ describe("permission-scope", () => { version: "1.0.0", runtime: "opencode", skills: [ - { id: "skill-c", name: "Skill C", permissions: { bash: "deny" } }, + { id: "skill-c", permissions: { bash: "deny" } }, ], } const step: WorkflowStep = { skill: "skill-c" } diff --git a/packages/opencode/test/ockit/workflow/session-bridge.test.ts b/packages/opencode/test/ockit/workflow/session-bridge.test.ts index 46f28c028..6d5ca9e65 100644 --- a/packages/opencode/test/ockit/workflow/session-bridge.test.ts +++ b/packages/opencode/test/ockit/workflow/session-bridge.test.ts @@ -18,14 +18,13 @@ const KIT: Kit = { name: "Test Kit", version: "1.0.0", runtime: "opencode", - skills: [{ id: "skill-a", name: "Skill A", description: "Skill A" }], + skills: [{ id: "skill-a", description: "Skill A" }], agents: [], hooks: [], } const WORKFLOW: Workflow = { id: "test-workflow", - name: "Test Workflow", steps: [{ skill: "skill-a" }], }