pi-openappa 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +120 -2
- package/docs/wire-notes.md +80 -0
- package/extensions/index.ts +183 -0
- package/justfile +32 -0
- package/package.json +28 -4
- package/src/adapter.ts +284 -0
- package/src/gate.ts +135 -0
- package/src/hook-client.ts +133 -0
- package/test/adapter.test.ts +207 -0
- package/test/extension.test.ts +447 -0
- package/test/fixtures/mock-appa.mjs +104 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import {
|
|
4
|
+
mapSessionSource,
|
|
5
|
+
mapToolName,
|
|
6
|
+
parseCallDecision,
|
|
7
|
+
parseResultDecision,
|
|
8
|
+
postToolUsePayload,
|
|
9
|
+
preToolUsePayload,
|
|
10
|
+
sessionStartPayload,
|
|
11
|
+
stopPayload,
|
|
12
|
+
promptPayload,
|
|
13
|
+
renderToolOutput,
|
|
14
|
+
toolResponseFrom,
|
|
15
|
+
} from "../src/adapter.ts";
|
|
16
|
+
|
|
17
|
+
const SESSION = "11111111-1111-1111-1111-111111111111";
|
|
18
|
+
|
|
19
|
+
describe("tool name mapping", () => {
|
|
20
|
+
it("maps pi built-ins to Claude Code policy names", () => {
|
|
21
|
+
assert.equal(mapToolName("bash"), "Bash");
|
|
22
|
+
assert.equal(mapToolName("read"), "Read");
|
|
23
|
+
assert.equal(mapToolName("edit"), "Edit");
|
|
24
|
+
assert.equal(mapToolName("write"), "Write");
|
|
25
|
+
assert.equal(mapToolName("grep"), "Grep");
|
|
26
|
+
assert.equal(mapToolName("find"), "Glob");
|
|
27
|
+
assert.equal(mapToolName("ls"), "LS");
|
|
28
|
+
assert.equal(mapToolName("powershell"), "PowerShell");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("passes custom tool names through verbatim", () => {
|
|
32
|
+
assert.equal(mapToolName("subagent"), "subagent");
|
|
33
|
+
assert.equal(mapToolName("mcp__zoekt__search"), "mcp__zoekt__search");
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe("session source mapping", () => {
|
|
38
|
+
it("maps startup and new to startup", () => {
|
|
39
|
+
assert.equal(mapSessionSource("startup"), "startup");
|
|
40
|
+
assert.equal(mapSessionSource("new"), "startup");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("maps resume, fork, and reload to resume", () => {
|
|
44
|
+
assert.equal(mapSessionSource("resume"), "resume");
|
|
45
|
+
assert.equal(mapSessionSource("fork"), "resume");
|
|
46
|
+
assert.equal(mapSessionSource("reload"), "resume");
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("payload builders", () => {
|
|
51
|
+
it("session start carries source and cwd", () => {
|
|
52
|
+
assert.deepEqual(sessionStartPayload(SESSION, "startup", "/w"), {
|
|
53
|
+
session_id: SESSION,
|
|
54
|
+
hook_event_name: "SessionStart",
|
|
55
|
+
source: "startup",
|
|
56
|
+
cwd: "/w",
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("prompt carries the prompt text", () => {
|
|
61
|
+
assert.equal(promptPayload(SESSION, "list files", "/w").prompt, "list files");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("pre and post tool use echo the same input reference", () => {
|
|
65
|
+
const input = { command: "ls", description: "list" };
|
|
66
|
+
const pre = preToolUsePayload(SESSION, "bash", input, "/w");
|
|
67
|
+
const post = postToolUsePayload(SESSION, "bash", input, { output: "x" }, "/w");
|
|
68
|
+
assert.equal(pre.tool_input, input);
|
|
69
|
+
assert.equal(post.tool_input, input);
|
|
70
|
+
assert.equal(JSON.stringify(pre.tool_input), JSON.stringify(post.tool_input));
|
|
71
|
+
assert.equal(pre.tool_name, "Bash");
|
|
72
|
+
assert.equal(post.tool_name, "Bash");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("stop payload is minimal", () => {
|
|
76
|
+
assert.deepEqual(stopPayload(SESSION), {
|
|
77
|
+
session_id: SESSION,
|
|
78
|
+
hook_event_name: "Stop",
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe("tool response shaping", () => {
|
|
84
|
+
it("carries text and error flag generically", () => {
|
|
85
|
+
assert.deepEqual(toolResponseFrom({ text: "hello", isError: false }), {
|
|
86
|
+
output: "hello",
|
|
87
|
+
isError: false,
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("copies structured fields from details when present", () => {
|
|
92
|
+
const response = toolResponseFrom({
|
|
93
|
+
text: "out",
|
|
94
|
+
isError: true,
|
|
95
|
+
details: { stdout: "out", exitcode: 1, interrupted: false },
|
|
96
|
+
});
|
|
97
|
+
assert.equal(response.stdout, "out");
|
|
98
|
+
assert.equal(response.exitcode, 1);
|
|
99
|
+
assert.equal(response.interrupted, false);
|
|
100
|
+
assert.equal(response.isError, true);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("renders updatedToolOutput with stdout/stderr preference", () => {
|
|
104
|
+
assert.equal(renderToolOutput({ stdout: "kept", exitcode: 0 }), "kept");
|
|
105
|
+
const rendered = renderToolOutput({ structured: true });
|
|
106
|
+
assert.ok(rendered.includes("structured"));
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe("parseCallDecision (wire fixtures from appa 0.31.1)", () => {
|
|
111
|
+
it("allows on silent exit 0", () => {
|
|
112
|
+
assert.deepEqual(parseCallDecision(0, "", ""), { type: "allow" });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("allows on empty json object", () => {
|
|
116
|
+
assert.deepEqual(parseCallDecision(0, "{}", ""), { type: "allow" });
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("allows on structured permission allow", () => {
|
|
120
|
+
const stdout =
|
|
121
|
+
'{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"allow","permissionDecisionReason":"appa: the call is released"}}';
|
|
122
|
+
assert.deepEqual(parseCallDecision(0, stdout, ""), { type: "allow" });
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("denies with reason on exit 2 stderr (undeclared tool)", () => {
|
|
126
|
+
const decision = parseCallDecision(
|
|
127
|
+
2,
|
|
128
|
+
'{"error":"tool host/claude-code/SendWebhook is not declared"}',
|
|
129
|
+
"OpenAPPA hook blocked: tool host/claude-code/SendWebhook is not declared",
|
|
130
|
+
);
|
|
131
|
+
assert.equal(decision.type, "deny");
|
|
132
|
+
if (decision.type === "deny") {
|
|
133
|
+
assert.ok(decision.reason.includes("not declared"));
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("denies on structured permission deny with its reason", () => {
|
|
138
|
+
const stdout =
|
|
139
|
+
'{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"appa: this command may not run here"}}';
|
|
140
|
+
const decision = parseCallDecision(0, stdout, "");
|
|
141
|
+
assert.equal(decision.type, "deny");
|
|
142
|
+
if (decision.type === "deny") {
|
|
143
|
+
assert.equal(decision.reason, "appa: this command may not run here");
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("denies fail-closed on unrecognized exit codes", () => {
|
|
148
|
+
const decision = parseCallDecision(-1, "", "appa hook failed to start:ENOENT");
|
|
149
|
+
assert.equal(decision.type, "deny");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("denies fail-closed on unrecognized structured decisions", () => {
|
|
153
|
+
const decision = parseCallDecision(0, '{"decision":"mystery"}', "");
|
|
154
|
+
assert.equal(decision.type, "deny");
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe("parseResultDecision (wire fixtures from appa 0.31.1)", () => {
|
|
159
|
+
it("passes on silent exit 0", () => {
|
|
160
|
+
assert.deepEqual(parseResultDecision(0, "", ""), { type: "pass" });
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("passes on empty json object", () => {
|
|
164
|
+
assert.deepEqual(parseResultDecision(0, "{}", ""), { type: "pass" });
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("replaces with updatedToolOutput content (withheld result)", () => {
|
|
168
|
+
const stdout = JSON.stringify({
|
|
169
|
+
decision: "block",
|
|
170
|
+
reason: "this outcome does not match the open dispatch; it is not reported",
|
|
171
|
+
hookSpecificOutput: {
|
|
172
|
+
hookEventName: "PostToolUse",
|
|
173
|
+
updatedToolOutput: {
|
|
174
|
+
exitcode: 0,
|
|
175
|
+
stdout: "[appa] the tool result was withheld",
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
const decision = parseResultDecision(0, stdout, "");
|
|
180
|
+
assert.equal(decision.type, "replace");
|
|
181
|
+
if (decision.type === "replace") {
|
|
182
|
+
assert.equal(decision.text, "[appa] the tool result was withheld");
|
|
183
|
+
assert.equal(decision.isError, false);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("withholds as error on exit 2", () => {
|
|
188
|
+
const decision = parseResultDecision(2, "", "OpenAPPA hook blocked: result may not cross");
|
|
189
|
+
assert.equal(decision.type, "replace");
|
|
190
|
+
if (decision.type === "replace") {
|
|
191
|
+
assert.equal(decision.isError, true);
|
|
192
|
+
assert.ok(decision.text.includes("result may not cross"));
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("withholds as error when the runtime is unreachable", () => {
|
|
197
|
+
const decision = parseResultDecision(
|
|
198
|
+
-1,
|
|
199
|
+
"",
|
|
200
|
+
"appa hook failed to start: cannot reach 127.0.0.1:8787",
|
|
201
|
+
);
|
|
202
|
+
assert.equal(decision.type, "replace");
|
|
203
|
+
if (decision.type === "replace") {
|
|
204
|
+
assert.equal(decision.isError, true);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
});
|
|
@@ -0,0 +1,447 @@
|
|
|
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
|
+
});
|