@ziptie-soft/orca-local-decider 0.0.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,218 @@
1
+ {
2
+ "$schema": "https://raw.githubusercontent.com/stablyai/orca/main/docs/schemas/plugins/orca-plugin.v2.schema.json",
3
+ "manifestVersion": 2,
4
+ "id": "local-decider",
5
+ "name": "local-decider",
6
+ "version": "1.0.0",
7
+ "description": "Refuses risky Claude Code tool calls and terminal sends in Orca, routes new agents to a model tier and flags turns that need you, using a deterministic guard and a local decision model.",
8
+ "author": {
9
+ "name": "ziptie-soft",
10
+ "url": "https://github.com/ziptie-soft"
11
+ },
12
+ "repository": "https://github.com/ziptie-soft/local-decider",
13
+ "homepage": "https://github.com/ziptie-soft/local-decider/tree/master/packages/orca-plugin#readme",
14
+ "license": "Apache-2.0",
15
+ "icon": "images/icon.png",
16
+ "cover": {
17
+ "path": "images/cover.png",
18
+ "alt": "local-decider: a local model that decides which agent tool calls are risky"
19
+ },
20
+ "engines": {
21
+ "orca": ">=1.4.276"
22
+ },
23
+ "execution": "trusted-worker",
24
+ "pluginApi": 1,
25
+ "main": "src/worker.ts",
26
+ "capabilities": [
27
+ {
28
+ "kind": "workspace:read"
29
+ },
30
+ {
31
+ "kind": "agent:read"
32
+ },
33
+ {
34
+ "kind": "terminal:read"
35
+ },
36
+ {
37
+ "kind": "notifications:show"
38
+ },
39
+ {
40
+ "kind": "settings:own"
41
+ }
42
+ ],
43
+ "contributes": {
44
+ "hooks": [
45
+ {
46
+ "event": "agent.tool.before",
47
+ "onError": "pass"
48
+ },
49
+ {
50
+ "event": "agent.launch.before",
51
+ "onError": "pass"
52
+ },
53
+ {
54
+ "event": "terminal.send",
55
+ "onError": "pass"
56
+ }
57
+ ],
58
+ "events": [
59
+ {
60
+ "on": "agent.status.changed"
61
+ }
62
+ ],
63
+ "mcpServers": [
64
+ {
65
+ "name": "local-decider",
66
+ "command": "local-decider",
67
+ "args": [
68
+ "mcp"
69
+ ]
70
+ }
71
+ ],
72
+ "panels": [
73
+ {
74
+ "id": "local-decider",
75
+ "title": "local-decider",
76
+ "icon": "zap",
77
+ "entry": "src/panel/panel.tsx",
78
+ "ui": "components"
79
+ }
80
+ ],
81
+ "commands": [
82
+ {
83
+ "id": "local-decider.status",
84
+ "title": "local-decider: Show status"
85
+ },
86
+ {
87
+ "id": "local-decider.start-gateway",
88
+ "title": "local-decider: Start the gateway"
89
+ },
90
+ {
91
+ "id": "local-decider.command-risk-act",
92
+ "title": "local-decider: Enforce command-risk (act)"
93
+ },
94
+ {
95
+ "id": "local-decider.command-risk-shadow",
96
+ "title": "local-decider: Record command-risk only (shadow)"
97
+ }
98
+ ],
99
+ "statusBarItems": [
100
+ {
101
+ "id": "status",
102
+ "tooltip": "local-decider",
103
+ "command": "local-decider.status"
104
+ }
105
+ ],
106
+ "settings": [
107
+ {
108
+ "key": "mode",
109
+ "title": "Mode",
110
+ "description": "guard: refuse calls the built-in guard flags, and ask the gateway's command-risk decision. Whether that decision can refuse is set in the gateway (panel: Command risk). off: the plugin does nothing.",
111
+ "type": "string",
112
+ "default": "guard",
113
+ "enum": [
114
+ "guard",
115
+ "off"
116
+ ],
117
+ "enumLabels": [
118
+ "Guard",
119
+ "Off"
120
+ ]
121
+ },
122
+ {
123
+ "key": "askAction",
124
+ "title": "When unsure",
125
+ "description": "What to do when a call can't be checked (a shell command too large or nested too deeply) or when the gateway's command-risk, in act mode, would ask. pass leaves it to Claude Code's prompt; deny refuses it.",
126
+ "type": "string",
127
+ "default": "pass",
128
+ "enum": [
129
+ "pass",
130
+ "deny"
131
+ ],
132
+ "enumLabels": [
133
+ "Pass to Claude Code",
134
+ "Refuse"
135
+ ]
136
+ },
137
+ {
138
+ "key": "routing",
139
+ "title": "Routing",
140
+ "description": "on: send each Claude Code or Codex launch prompt to the gateway's routing policy. In act mode the agent starts on the model and effort of the chosen preset (config.yaml routing.presets.orca, model as claude/sonnet or codex/<id>); in shadow the choice is only recorded. A route never changes permission flags. off: launches are left alone.",
141
+ "type": "string",
142
+ "default": "on",
143
+ "enum": [
144
+ "on",
145
+ "off"
146
+ ],
147
+ "enumLabels": [
148
+ "On",
149
+ "Off"
150
+ ]
151
+ },
152
+ {
153
+ "key": "turnOutcome",
154
+ "title": "End of turn",
155
+ "description": "on: when an agent in an Orca terminal ends its turn, send the end of its terminal to the gateway's turn-outcome policy. In act mode, a turn that needs input, is stuck or failed shows an Orca notification; in shadow the answer is only recorded. off: turns are not checked.",
156
+ "type": "string",
157
+ "default": "on",
158
+ "enum": [
159
+ "on",
160
+ "off"
161
+ ],
162
+ "enumLabels": [
163
+ "On",
164
+ "Off"
165
+ ]
166
+ },
167
+ {
168
+ "key": "statusBar",
169
+ "title": "Status bar item",
170
+ "description": "When the local-decider item shows in the status bar. The panel and the commands work in every mode.",
171
+ "type": "string",
172
+ "default": "problems",
173
+ "enum": [
174
+ "problems",
175
+ "always",
176
+ "off"
177
+ ],
178
+ "enumLabels": [
179
+ "Only on a problem",
180
+ "Always (icon)",
181
+ "Never"
182
+ ]
183
+ },
184
+ {
185
+ "key": "gatewayUrl",
186
+ "title": "Gateway URL",
187
+ "description": "Empty uses the local gateway (its socket, or http://127.0.0.1:8765). The token, if any, is read from LOCAL_DECIDER_TOKEN.",
188
+ "type": "string",
189
+ "default": "",
190
+ "maxLength": 2048,
191
+ "placeholder": "http://127.0.0.1:8765"
192
+ },
193
+ {
194
+ "key": "timeoutMs",
195
+ "title": "Decision timeout (ms)",
196
+ "description": "How long one decision may take. Orca stops waiting for a hook after 2 s, and a slow answer lets the call through.",
197
+ "type": "integer",
198
+ "default": 1500,
199
+ "minimum": 200,
200
+ "maximum": 1700
201
+ },
202
+ {
203
+ "key": "autoStart",
204
+ "title": "Start the gateway",
205
+ "description": "Start an installed gateway when Orca loads the plugin and it is not running. Set LOCAL_DECIDER_AUTOINSTALL=1 to let it also install one.",
206
+ "type": "boolean",
207
+ "default": true
208
+ },
209
+ {
210
+ "key": "preloadModel",
211
+ "title": "Load the model when Orca starts",
212
+ "description": "Load the gateway's model at start-up, so the first decision is not slowed by a cold model. Needs a local gateway (it uses the local control token) and does nothing for a remote URL.",
213
+ "type": "boolean",
214
+ "default": false
215
+ }
216
+ ]
217
+ }
218
+ }
package/package.json CHANGED
@@ -1,7 +1,47 @@
1
1
  {
2
2
  "name": "@ziptie-soft/orca-local-decider",
3
- "version": "0.0.0",
4
- "description": "Placeholder; the real package is published from github.com/ziptie-soft/local-decider",
5
- "license": "MIT",
6
- "repository": { "type": "git", "url": "git+https://github.com/ziptie-soft/local-decider.git" }
7
- }
3
+ "version": "1.0.0",
4
+ "description": "Orca plugin for local-decider: refuses risky Claude Code tool calls in Orca terminals, using a local decision model.",
5
+ "keywords": [
6
+ "ai-agents",
7
+ "coding-agents",
8
+ "decision-model",
9
+ "local-decider",
10
+ "local-llm",
11
+ "orca",
12
+ "orca-plugin",
13
+ "permissions"
14
+ ],
15
+ "homepage": "https://github.com/ziptie-soft/local-decider/tree/master/packages/orca-plugin#readme",
16
+ "bugs": {
17
+ "url": "https://github.com/ziptie-soft/local-decider/issues"
18
+ },
19
+ "license": "Apache-2.0",
20
+ "author": "ziptie-soft",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/ziptie-soft/local-decider.git",
24
+ "directory": "packages/orca-plugin"
25
+ },
26
+ "files": [
27
+ "orca-plugin.json",
28
+ "src/",
29
+ "images/",
30
+ "README.md",
31
+ "CHANGELOG.md",
32
+ "LICENSE",
33
+ "NOTICE"
34
+ ],
35
+ "type": "module",
36
+ "engines": {
37
+ "node": ">=24.15"
38
+ },
39
+ "localDecider": {
40
+ "gateway": "3.5.0"
41
+ },
42
+ "scripts": {
43
+ "build": "pnpm run typecheck",
44
+ "test": "node --test --import ../../scripts/test-tmpdir.mjs 'test/**/*.test.ts'",
45
+ "typecheck": "tsc -p tsconfig.json"
46
+ }
47
+ }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Which terminals an agent runs in, for the `terminal.send` guard. `agent.status.changed` names
3
+ * them, but Orca sends no event when an agent exits; it only clears that agent's status, which the
4
+ * `agents.list` host call reflects. So a remembered terminal is confirmed with `agents.list`
5
+ * before the guard skips it. Every failure keeps the skip, as before.
6
+ */
7
+ import { setBounded } from './bounded.ts';
8
+
9
+ /** Longest wait for `agents.list`; the `terminal.send` hook has 2 s in all. */
10
+ const AGENTS_LIST_TIMEOUT_MS = 300;
11
+
12
+ export interface AgentTerminals {
13
+ /** Note that an agent runs in `terminalId`; `workspace` is the workspace it belongs to, if known. */
14
+ remember(terminalId: string, workspace: unknown): void;
15
+ /** True unless the terminal is unknown, or `agents.list` shows its agent is gone (forgotten). */
16
+ isAgent(terminalId: string): Promise<boolean>;
17
+ }
18
+
19
+ export interface AgentTerminalsOptions {
20
+ readonly timeoutMs?: number;
21
+ readonly setTimer?: (fn: () => void, ms: number) => unknown;
22
+ readonly clearTimer?: (handle: unknown) => void;
23
+ }
24
+
25
+ interface Entry {
26
+ readonly workspace: string | undefined;
27
+ }
28
+
29
+ /** `undefined` when `agents.list` gave an unexpected shape, else the terminal ids it lists. */
30
+ function listedTerminals(reply: unknown): ReadonlySet<string> | undefined {
31
+ if (typeof reply !== 'object' || reply === null) return undefined;
32
+ const agents = (reply as { agents?: unknown }).agents;
33
+ if (!Array.isArray(agents)) return undefined;
34
+ const ids = new Set<string>();
35
+ for (const agent of agents) {
36
+ const id =
37
+ typeof agent === 'object' && agent !== null
38
+ ? (agent as { terminalId?: unknown }).terminalId
39
+ : null;
40
+ if (typeof id === 'string') ids.add(id);
41
+ }
42
+ return ids;
43
+ }
44
+
45
+ export function createAgentTerminals(
46
+ call: (method: string, params?: unknown) => Promise<unknown>,
47
+ options: AgentTerminalsOptions = {},
48
+ ): AgentTerminals {
49
+ const timeoutMs = options.timeoutMs ?? AGENTS_LIST_TIMEOUT_MS;
50
+ const setTimer = options.setTimer ?? ((fn, ms) => setTimeout(fn, ms));
51
+ const clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle as NodeJS.Timeout));
52
+ const known = new Map<string, Entry>();
53
+
54
+ /** The listed terminal ids, or `undefined` on a failure, a timeout or an odd reply. */
55
+ async function listed(workspace: string | undefined): Promise<ReadonlySet<string> | undefined> {
56
+ let handle: unknown;
57
+ const timeout = new Promise<undefined>((resolve) => {
58
+ handle = setTimer(() => resolve(undefined), timeoutMs);
59
+ });
60
+ try {
61
+ const request = call(
62
+ 'agents.list',
63
+ workspace === undefined ? undefined : { workspaceId: workspace },
64
+ );
65
+ return listedTerminals(await Promise.race([request, timeout]));
66
+ } catch {
67
+ return undefined;
68
+ } finally {
69
+ clearTimer(handle);
70
+ }
71
+ }
72
+
73
+ return {
74
+ remember(terminalId, workspace) {
75
+ const named = typeof workspace === 'string' && workspace !== '' ? workspace : undefined;
76
+ setBounded(known, terminalId, { workspace: named ?? known.get(terminalId)?.workspace });
77
+ },
78
+ async isAgent(terminalId) {
79
+ const entry = known.get(terminalId);
80
+ if (entry === undefined) return false;
81
+ const ids = await listed(entry.workspace);
82
+ if (ids === undefined || ids.has(terminalId)) return true;
83
+ // A status event that arrived meanwhile replaced the entry: the agent is alive.
84
+ if (known.get(terminalId) === entry) known.delete(terminalId);
85
+ return known.has(terminalId);
86
+ },
87
+ };
88
+ }
package/src/bounded.ts ADDED
@@ -0,0 +1,22 @@
1
+ /** Most entries kept per map or set of per-terminal state. */
2
+ export const MAX_TRACKED = 512;
3
+
4
+ /** Set `key` as the newest entry of `map`, dropping the oldest ones beyond `MAX_TRACKED`. */
5
+ export function setBounded<K, V>(map: Map<K, V>, key: K, value: V): void {
6
+ map.delete(key);
7
+ map.set(key, value);
8
+ for (const oldest of map.keys()) {
9
+ if (map.size <= MAX_TRACKED) break;
10
+ map.delete(oldest);
11
+ }
12
+ }
13
+
14
+ /** Add `key` as the newest member of `set`, dropping the oldest ones beyond `MAX_TRACKED`. */
15
+ export function addBounded<K>(set: Set<K>, key: K): void {
16
+ set.delete(key);
17
+ set.add(key);
18
+ for (const oldest of set) {
19
+ if (set.size <= MAX_TRACKED) break;
20
+ set.delete(oldest);
21
+ }
22
+ }
package/src/breaker.ts ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * A back-off shared by the tool gate and the launch router: once the gateway fails to give a
3
+ * decision, calls that would wait on it are skipped for `BREAKER_MS` so agents are not slowed.
4
+ */
5
+
6
+ export const BREAKER_MS = 30_000;
7
+
8
+ export interface Breaker {
9
+ /** True while a tripped breaker has not expired at `at` (default: now). */
10
+ isOpen(at?: number): boolean;
11
+ /** Skip the gateway for `BREAKER_MS` from now. */
12
+ trip(): void;
13
+ /** Forget a tripped breaker, e.g. after the gateway was started. */
14
+ reset(): void;
15
+ }
16
+
17
+ export function createBreaker(now: () => number): Breaker {
18
+ let openUntil = 0;
19
+ return {
20
+ isOpen: (at = now()) => at < openUntil,
21
+ trip() {
22
+ openUntil = now() + BREAKER_MS;
23
+ },
24
+ reset() {
25
+ openUntil = 0;
26
+ },
27
+ };
28
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Deterministic serialisation. Key order changes decision-model outputs (the Amazon study saw
3
+ * shifts of up to 0.19), so every object sent to an engine or hashed for the log is key-sorted.
4
+ */
5
+ export function sortKeysDeep<T>(value: T): T {
6
+ if (Array.isArray(value)) {
7
+ return value.map((item: unknown) => sortKeysDeep(item)) as T;
8
+ }
9
+ if (value !== null && typeof value === 'object') {
10
+ const entries = Object.entries(value as Record<string, unknown>)
11
+ .toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
12
+ .map(([key, item]) => [key, sortKeysDeep(item)] as const);
13
+ return Object.fromEntries(entries) as T;
14
+ }
15
+ return value;
16
+ }
17
+
18
+ /**
19
+ * JSON with keys sorted at every depth. Total: a value JSON cannot encode (`undefined`, a
20
+ * function or a symbol) encodes as `null`, as it already does inside an array.
21
+ */
22
+ export function canonicalJson(value: unknown): string {
23
+ return JSON.stringify(sortKeysDeep(value)) ?? 'null';
24
+ }