pi-openappa 0.1.0 → 0.3.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.
- package/README.md +63 -6
- package/extensions/index.ts +174 -28
- package/package.json +15 -1
- package/src/gate.ts +62 -6
- package/templates/appa.toml +1068 -0
- package/traces/coding-loop.appa +31 -0
- package/traces/memory-poison.appa +24 -0
- package/traces/push-day.appa +21 -0
- package/traces/research-then-code.appa +53 -0
- package/traces/secret-ops.appa +23 -0
- package/justfile +0 -32
- package/test/adapter.test.ts +0 -207
- package/test/extension.test.ts +0 -447
- package/test/fixtures/mock-appa.mjs +0 -104
- package/tsconfig.json +0 -16
package/test/extension.test.ts
DELETED
|
@@ -1,447 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { after, beforeEach, describe, it } from "node:test";
|
|
3
|
-
import { mkdtempSync, rmSync, existsSync, readFileSync, mkdirSync, writeFileSync } from "node:fs";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
|
-
import { join, dirname } from "node:path";
|
|
6
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
7
|
-
|
|
8
|
-
const here = dirname(fileURLToPath(import.meta.url));
|
|
9
|
-
const MOCK_BIN = join(here, "fixtures", "mock-appa.mjs");
|
|
10
|
-
const SESSION = "22222222-2222-2222-2222-222222222222";
|
|
11
|
-
|
|
12
|
-
/** Environment keys this suite mutates; restored after every test. */
|
|
13
|
-
const KEYS = ["APPA_GATE", "APPA_HOOK_BIN", "MOCK_MODE", "MOCK_RECORD", "APPA_HOOK_TIMEOUT_MS", "APPA_CONFIG", "XDG_CONFIG_HOME"];
|
|
14
|
-
const saved: Record<string, string | undefined> = {};
|
|
15
|
-
for (const key of KEYS) saved[key] = process.env[key];
|
|
16
|
-
|
|
17
|
-
const workDirs: string[] = [];
|
|
18
|
-
function workDir(): string {
|
|
19
|
-
const dir = mkdtempSync(join(tmpdir(), "pi-openappa-test-"));
|
|
20
|
-
workDirs.push(dir);
|
|
21
|
-
return dir;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
after(() => {
|
|
25
|
-
for (const dir of workDirs) rmSync(dir, { recursive: true, force: true });
|
|
26
|
-
for (const key of KEYS) {
|
|
27
|
-
const value = saved[key];
|
|
28
|
-
if (value === undefined) delete process.env[key];
|
|
29
|
-
else process.env[key] = value;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
type Handler = (event: any, ctx: any) => Promise<any>;
|
|
34
|
-
|
|
35
|
-
interface Harness {
|
|
36
|
-
handlers: Map<string, Handler>;
|
|
37
|
-
commands: Map<string, { handler: Handler }>;
|
|
38
|
-
ctx: unknown;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async function loadExtension(cwd?: string): Promise<Harness> {
|
|
42
|
-
const mod = await import(pathToFileURL(join(here, "..", "extensions", "index.ts")).href);
|
|
43
|
-
const handlers = new Map<string, Handler>();
|
|
44
|
-
const commands = new Map<string, { handler: Handler }>();
|
|
45
|
-
const fakePi = {
|
|
46
|
-
on: (name: string, handler: Handler) => handlers.set(name, handler),
|
|
47
|
-
registerCommand: (name: string, options: { handler: Handler }) =>
|
|
48
|
-
commands.set(name, options),
|
|
49
|
-
};
|
|
50
|
-
mod.default(fakePi);
|
|
51
|
-
const ctx = {
|
|
52
|
-
cwd: cwd ?? "/home/aemonge/projects/pi-openappa",
|
|
53
|
-
hasUI: false,
|
|
54
|
-
sessionManager: { getSessionId: () => SESSION },
|
|
55
|
-
ui: { notify: () => {} },
|
|
56
|
-
};
|
|
57
|
-
return { handlers, commands, ctx };
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async function startSession(harness: Harness, reason = "startup"): Promise<void> {
|
|
61
|
-
const handler = harness.handlers.get("session_start");
|
|
62
|
-
assert.ok(handler, "session_start handler registered");
|
|
63
|
-
await handler({ type: "session_start", reason }, harness.ctx);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function recordedLines(dir: string): string[] {
|
|
67
|
-
const file = join(dir, "record.jsonl");
|
|
68
|
-
if (!existsSync(file)) return [];
|
|
69
|
-
return readFileSync(file, "utf8")
|
|
70
|
-
.split("\n")
|
|
71
|
-
.filter((line) => line !== "");
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function withMock(dir: string, mode: string): void {
|
|
75
|
-
process.env.APPA_GATE = "1";
|
|
76
|
-
process.env.APPA_HOOK_BIN = MOCK_BIN;
|
|
77
|
-
process.env.MOCK_MODE = mode;
|
|
78
|
-
process.env.MOCK_RECORD = join(dir, "record.jsonl");
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const bashCall = { type: "tool_call", toolCallId: "call-1", toolName: "bash", input: { command: "ls", description: "list" } };
|
|
82
|
-
const bashResult = {
|
|
83
|
-
type: "tool_result",
|
|
84
|
-
toolCallId: "call-1",
|
|
85
|
-
toolName: "bash",
|
|
86
|
-
input: { command: "ls", description: "list" },
|
|
87
|
-
content: [{ type: "text", text: "file1 file2" }],
|
|
88
|
-
details: { stdout: "file1 file2", exitcode: 0 },
|
|
89
|
-
isError: false,
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
beforeEach(() => {
|
|
93
|
-
// Isolate gate resolution from real machine state: clean config dir per
|
|
94
|
-
// test, no gate env. Tests that need gating set it up inside themselves.
|
|
95
|
-
process.env.XDG_CONFIG_HOME = join(workDir(), "xdg");
|
|
96
|
-
delete process.env.APPA_GATE;
|
|
97
|
-
delete process.env.APPA_CONFIG;
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
describe("gate off: extension is inert", () => {
|
|
101
|
-
beforeEach(() => {
|
|
102
|
-
delete process.env.APPA_GATE;
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it("never invokes the hook and passes everything through", async () => {
|
|
106
|
-
const dir = workDir();
|
|
107
|
-
process.env.APPA_HOOK_BIN = MOCK_BIN;
|
|
108
|
-
process.env.MOCK_RECORD = join(dir, "record.jsonl");
|
|
109
|
-
const harness = await loadExtension();
|
|
110
|
-
await startSession(harness);
|
|
111
|
-
const prompt = await harness.handlers.get("before_agent_start")!(
|
|
112
|
-
{ type: "before_agent_start", prompt: "hello", systemPrompt: "", systemPromptOptions: {} },
|
|
113
|
-
harness.ctx,
|
|
114
|
-
);
|
|
115
|
-
const call = await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
116
|
-
const result = await harness.handlers.get("tool_result")!(bashResult, harness.ctx);
|
|
117
|
-
const turnEnd = await harness.handlers.get("turn_end")!({ type: "turn_end" }, harness.ctx);
|
|
118
|
-
assert.equal(prompt, undefined);
|
|
119
|
-
assert.equal(call, undefined);
|
|
120
|
-
assert.equal(result, undefined);
|
|
121
|
-
assert.equal(turnEnd, undefined);
|
|
122
|
-
assert.deepEqual(recordedLines(dir), []);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
describe("gated session through the scripted mock", () => {
|
|
127
|
-
it("allows a released call and passes its result", async () => {
|
|
128
|
-
const dir = workDir();
|
|
129
|
-
withMock(dir, "allow");
|
|
130
|
-
const harness = await loadExtension();
|
|
131
|
-
await startSession(harness);
|
|
132
|
-
const call = await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
133
|
-
assert.equal(call, undefined);
|
|
134
|
-
const result = await harness.handlers.get("tool_result")!(bashResult, harness.ctx);
|
|
135
|
-
assert.equal(result, undefined);
|
|
136
|
-
const events = recordedLines(dir).map((line) => JSON.parse(line));
|
|
137
|
-
assert.deepEqual(
|
|
138
|
-
events.map((e) => e.hook_event_name),
|
|
139
|
-
["SessionStart", "PreToolUse", "PostToolUse"],
|
|
140
|
-
);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it("denies with the runtime's reason on exit 2", async () => {
|
|
144
|
-
const dir = workDir();
|
|
145
|
-
withMock(dir, "deny-stderr");
|
|
146
|
-
const harness = await loadExtension();
|
|
147
|
-
await startSession(harness);
|
|
148
|
-
const call = await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
149
|
-
assert.deepEqual(call, {
|
|
150
|
-
block: true,
|
|
151
|
-
reason: "appa: not declared in this policy",
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it("denies with a structured permission reason", async () => {
|
|
156
|
-
const dir = workDir();
|
|
157
|
-
withMock(dir, "deny-permission");
|
|
158
|
-
const harness = await loadExtension();
|
|
159
|
-
await startSession(harness);
|
|
160
|
-
const call = await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
161
|
-
assert.deepEqual(call, {
|
|
162
|
-
block: true,
|
|
163
|
-
reason: "appa: appa: this command may not run here",
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
it("replaces a withheld result with the runtime's output", async () => {
|
|
168
|
-
const dir = workDir();
|
|
169
|
-
withMock(dir, "replace-result");
|
|
170
|
-
const harness = await loadExtension();
|
|
171
|
-
await startSession(harness);
|
|
172
|
-
await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
173
|
-
const result = await harness.handlers.get("tool_result")!(bashResult, harness.ctx);
|
|
174
|
-
assert.deepEqual(result, {
|
|
175
|
-
content: [{ type: "text", text: "[appa] the tool result was withheld by policy" }],
|
|
176
|
-
isError: false,
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it("withholds a result as an error when the runtime blocks it", async () => {
|
|
181
|
-
const dir = workDir();
|
|
182
|
-
withMock(dir, "deny-result");
|
|
183
|
-
const harness = await loadExtension();
|
|
184
|
-
await startSession(harness);
|
|
185
|
-
await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
186
|
-
const result = await harness.handlers.get("tool_result")!(bashResult, harness.ctx);
|
|
187
|
-
assert.equal(result.isError, true);
|
|
188
|
-
assert.ok(result.content[0].text.includes("result may not cross"));
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it("fails closed when the hook binary cannot start", async () => {
|
|
192
|
-
const dir = workDir();
|
|
193
|
-
withMock(dir, "allow");
|
|
194
|
-
process.env.APPA_HOOK_BIN = join(dir, "does-not-exist");
|
|
195
|
-
const harness = await loadExtension();
|
|
196
|
-
await startSession(harness);
|
|
197
|
-
const call = await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
198
|
-
assert.equal(call.block, true);
|
|
199
|
-
assert.ok(call.reason.includes("failed to start"));
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it("fails closed on hook timeout", async () => {
|
|
203
|
-
const dir = workDir();
|
|
204
|
-
withMock(dir, "sleep");
|
|
205
|
-
process.env.APPA_HOOK_TIMEOUT_MS = "150";
|
|
206
|
-
const harness = await loadExtension();
|
|
207
|
-
await startSession(harness);
|
|
208
|
-
const call = await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
209
|
-
assert.equal(call.block, true);
|
|
210
|
-
assert.ok(call.reason.includes("timed out"));
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it("treats a subagent spawn as a plain tool call", async () => {
|
|
214
|
-
const dir = workDir();
|
|
215
|
-
withMock(dir, "allow");
|
|
216
|
-
const harness = await loadExtension();
|
|
217
|
-
await startSession(harness);
|
|
218
|
-
const spawn = await harness.handlers.get("tool_call")!(
|
|
219
|
-
{
|
|
220
|
-
type: "tool_call",
|
|
221
|
-
toolCallId: "call-2",
|
|
222
|
-
toolName: "subagent",
|
|
223
|
-
input: { agent: "scout", prompt: "look around" },
|
|
224
|
-
},
|
|
225
|
-
harness.ctx,
|
|
226
|
-
);
|
|
227
|
-
assert.equal(spawn, undefined);
|
|
228
|
-
const events = recordedLines(dir).map((line) => JSON.parse(line));
|
|
229
|
-
const pre = events.find((e) => e.hook_event_name === "PreToolUse");
|
|
230
|
-
assert.equal(pre.tool_name, "subagent");
|
|
231
|
-
assert.deepEqual(pre.tool_input, { agent: "scout", prompt: "look around" });
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
it("echoes the exact tool_input bytes between Pre and PostToolUse", async () => {
|
|
235
|
-
const dir = workDir();
|
|
236
|
-
withMock(dir, "allow");
|
|
237
|
-
const harness = await loadExtension();
|
|
238
|
-
await startSession(harness);
|
|
239
|
-
await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
240
|
-
// The executed input mutates afterwards (another handler rewrote it);
|
|
241
|
-
// the result event sees different bytes than what was checked.
|
|
242
|
-
const mutatedResult = {
|
|
243
|
-
...bashResult,
|
|
244
|
-
input: { command: "ls -la", description: "list" },
|
|
245
|
-
};
|
|
246
|
-
await harness.handlers.get("tool_result")!(mutatedResult, harness.ctx);
|
|
247
|
-
const lines = recordedLines(dir).map((line) => JSON.parse(line));
|
|
248
|
-
const pre = lines.find((e) => e.hook_event_name === "PreToolUse");
|
|
249
|
-
const post = lines.find((e) => e.hook_event_name === "PostToolUse");
|
|
250
|
-
assert.equal(
|
|
251
|
-
JSON.stringify(pre.tool_input),
|
|
252
|
-
JSON.stringify({ command: "ls", description: "list" }),
|
|
253
|
-
);
|
|
254
|
-
assert.equal(JSON.stringify(post.tool_input), JSON.stringify(pre.tool_input));
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
it("reports turn end with --turn-end semantics (non-blocking)", async () => {
|
|
258
|
-
const dir = workDir();
|
|
259
|
-
withMock(dir, "crash");
|
|
260
|
-
const harness = await loadExtension();
|
|
261
|
-
await startSession(harness);
|
|
262
|
-
const outcome = await harness.handlers.get("turn_end")!({ type: "turn_end" }, harness.ctx);
|
|
263
|
-
assert.equal(outcome, undefined);
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
it("sends the prompt as the turn boundary and stop at turn end", async () => {
|
|
267
|
-
const dir = workDir();
|
|
268
|
-
withMock(dir, "allow");
|
|
269
|
-
const harness = await loadExtension();
|
|
270
|
-
await startSession(harness);
|
|
271
|
-
await harness.handlers.get("before_agent_start")!(
|
|
272
|
-
{ type: "before_agent_start", prompt: "list the files", systemPrompt: "", systemPromptOptions: {} },
|
|
273
|
-
harness.ctx,
|
|
274
|
-
);
|
|
275
|
-
await harness.handlers.get("turn_end")!({ type: "turn_end" }, harness.ctx);
|
|
276
|
-
const events = recordedLines(dir).map((line) => JSON.parse(line));
|
|
277
|
-
const prompt = events.find((e) => e.hook_event_name === "UserPromptSubmit");
|
|
278
|
-
const stop = events.find((e) => e.hook_event_name === "Stop");
|
|
279
|
-
assert.equal(prompt.prompt, "list the files");
|
|
280
|
-
assert.ok(stop);
|
|
281
|
-
});
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
function recordedArgv(dir: string): string[][] {
|
|
285
|
-
const file = `${join(dir, "record.jsonl")}.argv`;
|
|
286
|
-
if (!existsSync(file)) return [];
|
|
287
|
-
return readFileSync(file, "utf8")
|
|
288
|
-
.split("\n")
|
|
289
|
-
.filter((line) => line !== "")
|
|
290
|
-
.map((line) => JSON.parse(line) as string[]);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
describe("auto-start on gated session start", () => {
|
|
294
|
-
it("passes --ensure-runtime and --config from APPA_CONFIG", async () => {
|
|
295
|
-
const dir = workDir();
|
|
296
|
-
withMock(dir, "allow");
|
|
297
|
-
process.env.APPA_CONFIG = "/tmp/playground/appa.toml";
|
|
298
|
-
const harness = await loadExtension();
|
|
299
|
-
await startSession(harness);
|
|
300
|
-
const first = recordedArgv(dir)[0];
|
|
301
|
-
assert.ok(first?.includes("--ensure-runtime"), `argv: ${JSON.stringify(first)}`);
|
|
302
|
-
const configIndex = first?.indexOf("--config");
|
|
303
|
-
assert.notEqual(configIndex, -1);
|
|
304
|
-
assert.equal(first?.[Number(configIndex) + 1], "/tmp/playground/appa.toml");
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
it("omits --config when APPA_CONFIG is unset", async () => {
|
|
308
|
-
const dir = workDir();
|
|
309
|
-
withMock(dir, "allow");
|
|
310
|
-
delete process.env.APPA_CONFIG;
|
|
311
|
-
const harness = await loadExtension();
|
|
312
|
-
await startSession(harness);
|
|
313
|
-
const first = recordedArgv(dir)[0];
|
|
314
|
-
assert.ok(first?.includes("--ensure-runtime"));
|
|
315
|
-
assert.ok(!first?.includes("--config"));
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
it("starts nothing when ungated", async () => {
|
|
319
|
-
const dir = workDir();
|
|
320
|
-
withMock(dir, "allow");
|
|
321
|
-
delete process.env.APPA_GATE;
|
|
322
|
-
delete process.env.APPA_CONFIG;
|
|
323
|
-
const harness = await loadExtension();
|
|
324
|
-
await startSession(harness);
|
|
325
|
-
assert.deepEqual(recordedArgv(dir), []);
|
|
326
|
-
assert.deepEqual(recordedLines(dir), []);
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
describe("project-scoped protection (.pi/openappa)", () => {
|
|
331
|
-
it("gates the project and passes the marker's policy as --config", async () => {
|
|
332
|
-
const dir = workDir();
|
|
333
|
-
withMock(dir, "allow");
|
|
334
|
-
delete process.env.APPA_GATE;
|
|
335
|
-
delete process.env.APPA_CONFIG;
|
|
336
|
-
const proj = join(dir, "proj");
|
|
337
|
-
mkdirSync(join(proj, ".pi"), { recursive: true });
|
|
338
|
-
writeFileSync(join(proj, ".pi", "openappa"), "appa.toml\n");
|
|
339
|
-
const harness = await loadExtension(proj);
|
|
340
|
-
await startSession(harness);
|
|
341
|
-
const first = recordedArgv(dir)[0];
|
|
342
|
-
assert.ok(first?.includes("--ensure-runtime"), `argv: ${JSON.stringify(first)}`);
|
|
343
|
-
const index = first?.indexOf("--config");
|
|
344
|
-
assert.notEqual(index, -1);
|
|
345
|
-
assert.equal(first?.[Number(index) + 1], join(proj, "appa.toml"));
|
|
346
|
-
const call = await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
347
|
-
assert.equal(call, undefined);
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it("empty marker content passes no --config", async () => {
|
|
351
|
-
const dir = workDir();
|
|
352
|
-
withMock(dir, "allow");
|
|
353
|
-
delete process.env.APPA_GATE;
|
|
354
|
-
delete process.env.APPA_CONFIG;
|
|
355
|
-
const proj = join(dir, "proj2");
|
|
356
|
-
mkdirSync(join(proj, ".pi"), { recursive: true });
|
|
357
|
-
writeFileSync(join(proj, ".pi", "openappa"), "");
|
|
358
|
-
const harness = await loadExtension(proj);
|
|
359
|
-
await startSession(harness);
|
|
360
|
-
const first = recordedArgv(dir)[0];
|
|
361
|
-
assert.ok(first?.includes("--ensure-runtime"));
|
|
362
|
-
assert.ok(!first?.includes("--config"));
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
it("explicit APPA_CONFIG wins over marker content", async () => {
|
|
366
|
-
const dir = workDir();
|
|
367
|
-
withMock(dir, "allow");
|
|
368
|
-
delete process.env.APPA_GATE;
|
|
369
|
-
process.env.APPA_CONFIG = "/tmp/env-policy.toml";
|
|
370
|
-
const proj = join(dir, "proj3");
|
|
371
|
-
mkdirSync(join(proj, ".pi"), { recursive: true });
|
|
372
|
-
writeFileSync(join(proj, ".pi", "openappa"), "appa.toml\n");
|
|
373
|
-
const harness = await loadExtension(proj);
|
|
374
|
-
await startSession(harness);
|
|
375
|
-
const first = recordedArgv(dir)[0];
|
|
376
|
-
const index = first?.indexOf("--config");
|
|
377
|
-
assert.equal(first?.[Number(index) + 1], "/tmp/env-policy.toml");
|
|
378
|
-
});
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
describe("always-on mode (marker file)", () => {
|
|
382
|
-
it("gates every session once the marker exists", async () => {
|
|
383
|
-
const dir = workDir();
|
|
384
|
-
withMock(dir, "allow");
|
|
385
|
-
delete process.env.APPA_GATE;
|
|
386
|
-
process.env.XDG_CONFIG_HOME = join(dir, "xdg");
|
|
387
|
-
mkdirSync(join(dir, "xdg", "pi-openappa"), { recursive: true });
|
|
388
|
-
writeFileSync(join(dir, "xdg", "pi-openappa", "always-on"), "");
|
|
389
|
-
const harness = await loadExtension();
|
|
390
|
-
await startSession(harness);
|
|
391
|
-
const call = await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
392
|
-
assert.equal(call, undefined);
|
|
393
|
-
assert.ok(recordedArgv(dir).length > 0, "hook was invoked without APPA_GATE");
|
|
394
|
-
});
|
|
395
|
-
|
|
396
|
-
it("inert when the marker is absent", async () => {
|
|
397
|
-
const dir = workDir();
|
|
398
|
-
withMock(dir, "allow");
|
|
399
|
-
delete process.env.APPA_GATE;
|
|
400
|
-
process.env.XDG_CONFIG_HOME = join(dir, "xdg-clean");
|
|
401
|
-
const harness = await loadExtension();
|
|
402
|
-
await startSession(harness);
|
|
403
|
-
const call = await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
404
|
-
assert.equal(call, undefined);
|
|
405
|
-
assert.deepEqual(recordedArgv(dir), []);
|
|
406
|
-
});
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
describe("/appa on|off", () => {
|
|
410
|
-
it("on creates the marker and gates immediately; off removes it", async () => {
|
|
411
|
-
const dir = workDir();
|
|
412
|
-
process.env.XDG_CONFIG_HOME = join(dir, "xdg");
|
|
413
|
-
delete process.env.APPA_GATE;
|
|
414
|
-
const harness = await loadExtension();
|
|
415
|
-
const command = harness.commands.get("appa");
|
|
416
|
-
assert.ok(command);
|
|
417
|
-
await command.handler("on", harness.ctx);
|
|
418
|
-
assert.ok(existsSync(join(dir, "xdg", "pi-openappa", "always-on")));
|
|
419
|
-
const gated = await harness.handlers.get("tool_call")!(bashCall, harness.ctx);
|
|
420
|
-
assert.equal(gated, undefined); // gated now: would invoke hook (mock unset -> inert record empty ok)
|
|
421
|
-
await command.handler("off", harness.ctx);
|
|
422
|
-
assert.ok(!existsSync(join(dir, "xdg", "pi-openappa", "always-on")));
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
it("status completes without a runtime", async () => {
|
|
426
|
-
const dir = workDir();
|
|
427
|
-
process.env.XDG_CONFIG_HOME = join(dir, "xdg2");
|
|
428
|
-
process.env.APPA_RUNTIME_URL = "http://127.0.0.1:8799";
|
|
429
|
-
const harness = await loadExtension();
|
|
430
|
-
await startSession(harness);
|
|
431
|
-
await harness.commands.get("appa")!.handler("status", harness.ctx);
|
|
432
|
-
});
|
|
433
|
-
});
|
|
434
|
-
|
|
435
|
-
describe("/appa command", () => {
|
|
436
|
-
it("reports protection state without a runtime", async () => {
|
|
437
|
-
const dir = workDir();
|
|
438
|
-
delete process.env.APPA_GATE;
|
|
439
|
-
process.env.APPA_RUNTIME_URL = "http://127.0.0.1:8799";
|
|
440
|
-
const harness = await loadExtension();
|
|
441
|
-
await startSession(harness);
|
|
442
|
-
const command = harness.commands.get("appa");
|
|
443
|
-
assert.ok(command, "/appa registered");
|
|
444
|
-
// No UI in this harness; the handler must still complete without throwing.
|
|
445
|
-
await command.handler("", harness.ctx);
|
|
446
|
-
});
|
|
447
|
-
});
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Scripted `appa hook` stand-in for tests. Behavior is selected with
|
|
4
|
-
* MOCK_MODE; every received payload is appended to MOCK_RECORD as JSONL so
|
|
5
|
-
* tests can assert byte-exact echoes across PreToolUse/PostToolUse.
|
|
6
|
-
*/
|
|
7
|
-
import { appendFileSync } from "node:fs";
|
|
8
|
-
|
|
9
|
-
let raw = "";
|
|
10
|
-
process.stdin.on("data", (chunk) => {
|
|
11
|
-
raw += chunk;
|
|
12
|
-
});
|
|
13
|
-
process.stdin.on("end", () => {
|
|
14
|
-
let payload = {};
|
|
15
|
-
try {
|
|
16
|
-
payload = JSON.parse(raw);
|
|
17
|
-
} catch {
|
|
18
|
-
payload = {};
|
|
19
|
-
}
|
|
20
|
-
if (process.env.MOCK_RECORD) {
|
|
21
|
-
appendFileSync(process.env.MOCK_RECORD, `${JSON.stringify(payload)}\n`);
|
|
22
|
-
appendFileSync(
|
|
23
|
-
`${process.env.MOCK_RECORD}.argv`,
|
|
24
|
-
`${JSON.stringify(process.argv.slice(2))}\n`,
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
const event = payload.hook_event_name;
|
|
28
|
-
const mode = process.env.MOCK_MODE ?? "allow";
|
|
29
|
-
|
|
30
|
-
if (mode === "sleep") {
|
|
31
|
-
setTimeout(() => process.exit(0), 2000);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
if (mode === "crash") {
|
|
35
|
-
console.error("appa hook failed to start: mock crash");
|
|
36
|
-
process.exit(-1);
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (event === "PreToolUse") {
|
|
41
|
-
if (mode === "deny-stderr") {
|
|
42
|
-
console.error("OpenAPPA hook blocked: not declared in this policy");
|
|
43
|
-
process.exit(2);
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
if (mode === "deny-json") {
|
|
47
|
-
process.stdout.write(JSON.stringify({ error: "tool host/claude-code/X is not declared" }));
|
|
48
|
-
console.error("OpenAPPA hook blocked: tool host/claude-code/X is not declared");
|
|
49
|
-
process.exit(2);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
if (mode === "deny-permission") {
|
|
53
|
-
process.stdout.write(
|
|
54
|
-
JSON.stringify({
|
|
55
|
-
hookSpecificOutput: {
|
|
56
|
-
hookEventName: "PreToolUse",
|
|
57
|
-
permissionDecision: "deny",
|
|
58
|
-
permissionDecisionReason: "appa: this command may not run here",
|
|
59
|
-
},
|
|
60
|
-
}),
|
|
61
|
-
);
|
|
62
|
-
process.exit(0);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
process.stdout.write(
|
|
66
|
-
JSON.stringify({
|
|
67
|
-
hookSpecificOutput: {
|
|
68
|
-
hookEventName: "PreToolUse",
|
|
69
|
-
permissionDecision: "allow",
|
|
70
|
-
permissionDecisionReason: "appa: the call is released",
|
|
71
|
-
},
|
|
72
|
-
}),
|
|
73
|
-
);
|
|
74
|
-
process.exit(0);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (event === "PostToolUse" && mode === "replace-result") {
|
|
79
|
-
process.stdout.write(
|
|
80
|
-
JSON.stringify({
|
|
81
|
-
decision: "block",
|
|
82
|
-
reason: "this outcome does not match the open dispatch; it is not reported",
|
|
83
|
-
hookSpecificOutput: {
|
|
84
|
-
hookEventName: "PostToolUse",
|
|
85
|
-
updatedToolOutput: {
|
|
86
|
-
exitcode: 0,
|
|
87
|
-
stdout: "[appa] the tool result was withheld by policy",
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
}),
|
|
91
|
-
);
|
|
92
|
-
process.exit(0);
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (event === "PostToolUse" && mode === "deny-result") {
|
|
97
|
-
console.error("OpenAPPA hook blocked: result may not cross");
|
|
98
|
-
process.exit(2);
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
process.stdout.write("{}");
|
|
103
|
-
process.exit(0);
|
|
104
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2023",
|
|
4
|
-
"module": "NodeNext",
|
|
5
|
-
"moduleResolution": "NodeNext",
|
|
6
|
-
"lib": ["ES2023"],
|
|
7
|
-
"types": ["node"],
|
|
8
|
-
"strict": true,
|
|
9
|
-
"noUncheckedIndexedAccess": true,
|
|
10
|
-
"exactOptionalPropertyTypes": true,
|
|
11
|
-
"noEmit": true,
|
|
12
|
-
"skipLibCheck": true,
|
|
13
|
-
"allowImportingTsExtensions": true
|
|
14
|
-
},
|
|
15
|
-
"include": ["extensions/**/*.ts", "src/**/*.ts", "test/**/*.ts"]
|
|
16
|
-
}
|