refactor: remove legacy tools format, use permission only
BREAKING: Requires OpenCode 1.1.1+ - Remove supportsNewPermissionSystem/usesLegacyToolsSystem checks - Simplify permission-compat.ts to permission format only - Unify explore/librarian deny lists: write, edit, task, sisyphus_task, call_omo_agent - Add sisyphus_task to oracle deny list - Update agent-tool-restrictions.ts with correct per-agent restrictions - Clean config-handler.ts conditional version checks - Update tests for simplified API
This commit is contained in:
@@ -1,27 +1,15 @@
|
||||
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
|
||||
import { describe, test, expect } from "bun:test"
|
||||
import {
|
||||
createAgentToolRestrictions,
|
||||
createAgentToolAllowlist,
|
||||
migrateToolsToPermission,
|
||||
migratePermissionToTools,
|
||||
migrateAgentConfig,
|
||||
} from "./permission-compat"
|
||||
import { setVersionCache, resetVersionCache } from "./opencode-version"
|
||||
|
||||
describe("permission-compat", () => {
|
||||
beforeEach(() => {
|
||||
resetVersionCache()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
resetVersionCache()
|
||||
})
|
||||
|
||||
describe("createAgentToolRestrictions", () => {
|
||||
test("returns permission format for v1.1.1+", () => {
|
||||
// #given version is 1.1.1
|
||||
setVersionCache("1.1.1")
|
||||
|
||||
test("returns permission format with deny values", () => {
|
||||
// #given tools to restrict
|
||||
// #when creating restrictions
|
||||
const result = createAgentToolRestrictions(["write", "edit"])
|
||||
|
||||
@@ -31,38 +19,19 @@ describe("permission-compat", () => {
|
||||
})
|
||||
})
|
||||
|
||||
test("returns tools format for versions below 1.1.1", () => {
|
||||
// #given version is below 1.1.1
|
||||
setVersionCache("1.0.150")
|
||||
|
||||
test("returns empty permission for empty array", () => {
|
||||
// #given empty tools array
|
||||
// #when creating restrictions
|
||||
const result = createAgentToolRestrictions(["write", "edit"])
|
||||
const result = createAgentToolRestrictions([])
|
||||
|
||||
// #then returns tools format
|
||||
expect(result).toEqual({
|
||||
tools: { write: false, edit: false },
|
||||
})
|
||||
})
|
||||
|
||||
test("assumes new format when version unknown", () => {
|
||||
// #given version is null
|
||||
setVersionCache(null)
|
||||
|
||||
// #when creating restrictions
|
||||
const result = createAgentToolRestrictions(["write"])
|
||||
|
||||
// #then returns permission format (assumes new version)
|
||||
expect(result).toEqual({
|
||||
permission: { write: "deny" },
|
||||
})
|
||||
// #then returns empty permission
|
||||
expect(result).toEqual({ permission: {} })
|
||||
})
|
||||
})
|
||||
|
||||
describe("createAgentToolAllowlist", () => {
|
||||
test("returns wildcard deny with explicit allow for v1.1.1+", () => {
|
||||
// #given version is 1.1.1
|
||||
setVersionCache("1.1.1")
|
||||
|
||||
test("returns wildcard deny with explicit allow", () => {
|
||||
// #given tools to allow
|
||||
// #when creating allowlist
|
||||
const result = createAgentToolAllowlist(["read"])
|
||||
|
||||
@@ -72,11 +41,9 @@ describe("permission-compat", () => {
|
||||
})
|
||||
})
|
||||
|
||||
test("returns wildcard deny with multiple allows for v1.1.1+", () => {
|
||||
// #given version is 1.1.1
|
||||
setVersionCache("1.1.1")
|
||||
|
||||
// #when creating allowlist with multiple tools
|
||||
test("returns wildcard deny with multiple allows", () => {
|
||||
// #given multiple tools to allow
|
||||
// #when creating allowlist
|
||||
const result = createAgentToolAllowlist(["read", "glob"])
|
||||
|
||||
// #then returns wildcard deny with both allows
|
||||
@@ -84,35 +51,6 @@ describe("permission-compat", () => {
|
||||
permission: { "*": "deny", read: "allow", glob: "allow" },
|
||||
})
|
||||
})
|
||||
|
||||
test("returns explicit deny list for old versions", () => {
|
||||
// #given version is below 1.1.1
|
||||
setVersionCache("1.0.150")
|
||||
|
||||
// #when creating allowlist
|
||||
const result = createAgentToolAllowlist(["read"])
|
||||
|
||||
// #then returns tools format with common tools denied except read
|
||||
expect(result).toHaveProperty("tools")
|
||||
const tools = (result as { tools: Record<string, boolean> }).tools
|
||||
expect(tools.write).toBe(false)
|
||||
expect(tools.edit).toBe(false)
|
||||
expect(tools.bash).toBe(false)
|
||||
expect(tools.read).toBeUndefined()
|
||||
})
|
||||
|
||||
test("excludes allowed tools from legacy deny list", () => {
|
||||
// #given version is below 1.1.1
|
||||
setVersionCache("1.0.150")
|
||||
|
||||
// #when creating allowlist with glob
|
||||
const result = createAgentToolAllowlist(["read", "glob"])
|
||||
|
||||
// #then glob is not in deny list
|
||||
const tools = (result as { tools: Record<string, boolean> }).tools
|
||||
expect(tools.glob).toBeUndefined()
|
||||
expect(tools.write).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("migrateToolsToPermission", () => {
|
||||
@@ -132,38 +70,9 @@ describe("permission-compat", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("migratePermissionToTools", () => {
|
||||
test("converts permission to boolean tools", () => {
|
||||
// #given permission config
|
||||
const permission = { write: "deny" as const, edit: "allow" as const }
|
||||
|
||||
// #when migrating
|
||||
const result = migratePermissionToTools(permission)
|
||||
|
||||
// #then converts correctly
|
||||
expect(result).toEqual({ write: false, edit: true })
|
||||
})
|
||||
|
||||
test("excludes ask values", () => {
|
||||
// #given permission with ask
|
||||
const permission = {
|
||||
write: "deny" as const,
|
||||
edit: "ask" as const,
|
||||
bash: "allow" as const,
|
||||
}
|
||||
|
||||
// #when migrating
|
||||
const result = migratePermissionToTools(permission)
|
||||
|
||||
// #then ask is excluded
|
||||
expect(result).toEqual({ write: false, bash: true })
|
||||
})
|
||||
})
|
||||
|
||||
describe("migrateAgentConfig", () => {
|
||||
test("migrates tools to permission for v1.1.1+", () => {
|
||||
// #given v1.1.1 and config with tools
|
||||
setVersionCache("1.1.1")
|
||||
test("migrates tools to permission", () => {
|
||||
// #given config with tools
|
||||
const config = {
|
||||
model: "test",
|
||||
tools: { write: false, edit: false },
|
||||
@@ -178,25 +87,8 @@ describe("permission-compat", () => {
|
||||
expect(result.model).toBe("test")
|
||||
})
|
||||
|
||||
test("migrates permission to tools for old versions", () => {
|
||||
// #given old version and config with permission
|
||||
setVersionCache("1.0.150")
|
||||
const config = {
|
||||
model: "test",
|
||||
permission: { write: "deny" as const, edit: "deny" as const },
|
||||
}
|
||||
|
||||
// #when migrating
|
||||
const result = migrateAgentConfig(config)
|
||||
|
||||
// #then converts to tools
|
||||
expect(result.permission).toBeUndefined()
|
||||
expect(result.tools).toEqual({ write: false, edit: false })
|
||||
})
|
||||
|
||||
test("preserves other config fields", () => {
|
||||
// #given config with other fields
|
||||
setVersionCache("1.1.1")
|
||||
const config = {
|
||||
model: "test",
|
||||
temperature: 0.5,
|
||||
@@ -212,5 +104,31 @@ describe("permission-compat", () => {
|
||||
expect(result.temperature).toBe(0.5)
|
||||
expect(result.prompt).toBe("hello")
|
||||
})
|
||||
|
||||
test("merges existing permission with migrated tools", () => {
|
||||
// #given config with both tools and permission
|
||||
const config = {
|
||||
tools: { write: false },
|
||||
permission: { bash: "deny" as const },
|
||||
}
|
||||
|
||||
// #when migrating
|
||||
const result = migrateAgentConfig(config)
|
||||
|
||||
// #then merges permission (existing takes precedence)
|
||||
expect(result.tools).toBeUndefined()
|
||||
expect(result.permission).toEqual({ write: "deny", bash: "deny" })
|
||||
})
|
||||
|
||||
test("returns unchanged config if no tools", () => {
|
||||
// #given config without tools
|
||||
const config = { model: "test", permission: { edit: "deny" as const } }
|
||||
|
||||
// #when migrating
|
||||
const result = migrateAgentConfig(config)
|
||||
|
||||
// #then returns unchanged
|
||||
expect(result).toEqual(config)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user