@niwa-labs/harness-claude 0.0.0-stage → 0.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.
- package/LICENSE +21 -0
- package/README.md +4 -2
- package/package.json +19 -4
- package/src/code/acp/adapter-patch.ts +43 -0
- package/src/code/acp/adapter.ts +131 -0
- package/src/code/acp/control.ts +1773 -0
- package/src/code/acp/protocol.ts +85 -0
- package/src/code/acp/tool-guard.ts +70 -0
- package/src/code/control.ts +258 -0
- package/src/code/fixtures.ts +155 -0
- package/src/code/format.ts +189 -0
- package/src/code/index.ts +133 -0
- package/src/code/insert/constants.ts +15 -0
- package/src/code/insert/hook.ts +209 -0
- package/src/code/insert/index.ts +30 -0
- package/src/code/insert/install.ts +129 -0
- package/src/code/launch.ts +11 -0
- package/src/code/needs.ts +68 -0
- package/src/code/options.ts +33 -0
- package/src/code/paths.ts +107 -0
- package/src/code/reading.ts +702 -0
- package/src/code/skills.ts +13 -0
- package/src/code/tools.ts +59 -0
- package/src/index.ts +53 -0
- package/src/sdk/control.ts +1060 -0
- package/src/sdk/index.ts +126 -0
- package/src/sdk/messages.ts +279 -0
- package/src/sdk/reverse.ts +122 -0
- package/src/sdk/stream.ts +41 -0
- package/test/code/adapter-patch.test.ts +37 -0
- package/test/code/blocks.ts +58 -0
- package/test/code/conformance.test.ts +32 -0
- package/test/code/control.test.ts +1142 -0
- package/test/code/discovery.test.ts +85 -0
- package/test/code/home.test.ts +68 -0
- package/test/code/insert.test.ts +36 -0
- package/test/code/mcp-sources.test.ts +88 -0
- package/test/code/reading.test.ts +419 -0
- package/test/sdk/control.test.ts +506 -0
- package/test/sdk/messages.test.ts +213 -0
- package/test/sdk/scripted.ts +129 -0
- package/test/sdk/session.test.ts +231 -0
- package/tsconfig.json +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Niwa Labs contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @niwa-labs/harness-claude
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Claude harness kinds: `code`, driven over ACP, and `sdk`, driven through the agent SDK. Both share transcript reading and format parsing.
|
|
4
|
+
|
|
5
|
+
It is a zone I library: it needs no host. Its dependencies are `@niwa-labs/harness` (the common model), `@niwa-labs/acp` (the protocol client of the `code` kind), the contracts entry of `@niwa-labs/core`, and process launch from `@niwa-labs/sandbox`; `scripts/check-boundaries.ts` holds the boundary.
|
package/package.json
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@niwa-labs/harness-claude",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.ts"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@anthropic-ai/claude-agent-sdk": "^0.3.325",
|
|
11
|
+
"@niwa-labs/acp": "0.0.0",
|
|
12
|
+
"@niwa-labs/core": "0.0.0",
|
|
13
|
+
"@niwa-labs/harness": "0.0.0",
|
|
14
|
+
"@niwa-labs/sandbox": "0.0.0",
|
|
15
|
+
"@zed-industries/claude-code-acp": "0.16.2",
|
|
16
|
+
"zod": "^4.6.7"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@niwa-labs/testing": "0.0.0"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lines niwa's entry changes in the source of Claude Code's ACP adapter as it loads
|
|
3
|
+
* (`adapter.ts`). Each is replaced whole; a source that lacks one is a build of the adapter niwa
|
|
4
|
+
* does not know, and it fails to load: a session must never run with a tool on by accident, or
|
|
5
|
+
* off silently.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** One change: what it does, the line as the adapter has it, and the line niwa runs instead. */
|
|
9
|
+
export interface AdapterPatch {
|
|
10
|
+
readonly what: string;
|
|
11
|
+
readonly from: string;
|
|
12
|
+
readonly to: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const ADAPTER_PATCHES: readonly AdapterPatch[] = [
|
|
16
|
+
{
|
|
17
|
+
what: "switch AskUserQuestion off",
|
|
18
|
+
from: 'const disallowedTools = ["AskUserQuestion"];',
|
|
19
|
+
to: 'const disallowedTools = params._meta?.niwa?.askUserQuestion === true ? [] : ["AskUserQuestion"];',
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
what: "give every session Claude Code's preset of tools",
|
|
23
|
+
from: 'tools: { type: "preset", preset: "claude_code" },',
|
|
24
|
+
to: 'tools: Array.isArray(params._meta?.niwa?.tools) ? params._meta.niwa.tools : { type: "preset", preset: "claude_code" },',
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
what: "put niwa's guard before every tool call",
|
|
28
|
+
from: "hooks: [createPreToolUseHook(settingsManager, this.logger)],",
|
|
29
|
+
to: "hooks: [...globalThis.niwaToolGuardHooks(params._meta?.niwa), createPreToolUseHook(settingsManager, this.logger)],",
|
|
30
|
+
},
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
/** The adapter's source with every change made, or an error naming the one it does not have. */
|
|
34
|
+
export function patchAdapter(source: string, path: string): string {
|
|
35
|
+
let patched = source;
|
|
36
|
+
for (const patch of ADAPTER_PATCHES) {
|
|
37
|
+
if (!patched.includes(patch.from)) {
|
|
38
|
+
throw new Error(`the Claude Code ACP adapter at ${path} does not ${patch.what} the way niwa expects`);
|
|
39
|
+
}
|
|
40
|
+
patched = patched.replace(patch.from, patch.to);
|
|
41
|
+
}
|
|
42
|
+
return patched;
|
|
43
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ACP adapter niwa runs for Claude Code: `@zed-industries/claude-code-acp`, with a few things
|
|
3
|
+
* changed.
|
|
4
|
+
*
|
|
5
|
+
* The adapter switches `AskUserQuestion` off for every session, and carries no way to ask the
|
|
6
|
+
* client a question: its permission request has nowhere to put an answer. niwa answers the
|
|
7
|
+
* questions of its sessions, so this entry:
|
|
8
|
+
*
|
|
9
|
+
* - lets a session that asks for it (`_meta.niwa.askUserQuestion` in `session/new` or
|
|
10
|
+
* `session/load`) keep the tool; every other session has it off as before;
|
|
11
|
+
* - sends the tool's questions to the client as the extension request `_niwa/ask_user_question`
|
|
12
|
+
* and hands the client's answers to the tool; when the tool call is aborted it says so with the
|
|
13
|
+
* notification `_niwa/cancel_request`;
|
|
14
|
+
* - tells the client the model the session really runs, with the notification `_niwa/session_model`:
|
|
15
|
+
* the `system/init` message of the program names it, and the adapter drops that message;
|
|
16
|
+
* - gives a session the built-in tools `_meta.niwa.tools` names, and only those, where the adapter
|
|
17
|
+
* gives every session Claude Code's whole preset: an allow-list, and with none named only the
|
|
18
|
+
* tools of the MCP servers the session was given.
|
|
19
|
+
*
|
|
20
|
+
* - denies, before it runs, a call of a tool a restricted session is not held to: a built-in tool
|
|
21
|
+
* its allow-list leaves out, and a tool of an MCP server the session was not given (`tool-guard.ts`).
|
|
22
|
+
*
|
|
23
|
+
* The tools are changed by lines of the adapter's source, replaced as the adapter loads
|
|
24
|
+
* (`adapter-patch.ts`). The adapter is pinned to one version, and a version that does not have a
|
|
25
|
+
* line fails to start: a session must never run with a tool on by accident, or off silently.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { BUILTIN_TOOL_NAMES } from "@anthropic-ai/claude-agent-sdk";
|
|
29
|
+
|
|
30
|
+
import { patchAdapter } from "./adapter-patch.ts";
|
|
31
|
+
import { toolGuardHooks } from "./tool-guard.ts";
|
|
32
|
+
|
|
33
|
+
// The patched adapter reads its guard from here: a build that lacks it fails the session's start.
|
|
34
|
+
Object.assign(globalThis, {
|
|
35
|
+
niwaToolGuardHooks: (niwa: Parameters<typeof toolGuardHooks>[0]) => toolGuardHooks(niwa, BUILTIN_TOOL_NAMES),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
Bun.plugin({
|
|
39
|
+
name: "niwa-claude-code-adapter",
|
|
40
|
+
setup(build) {
|
|
41
|
+
build.onLoad({ filter: /claude-code-acp\/dist\/acp-agent\.js$/ }, async ({ path }) => ({
|
|
42
|
+
contents: patchAdapter(await Bun.file(path).text(), path),
|
|
43
|
+
loader: "js",
|
|
44
|
+
}));
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const { ClaudeAcpAgent, applyEnvironmentSettings, loadManagedSettings, runAcp } = await import("@zed-industries/claude-code-acp");
|
|
49
|
+
|
|
50
|
+
/** What the client answers to the questions of one tool call. */
|
|
51
|
+
interface QuestionAnswers {
|
|
52
|
+
readonly answers?: Readonly<Record<string, string>>;
|
|
53
|
+
readonly cancelled?: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const canUseTool = ClaudeAcpAgent.prototype.canUseTool;
|
|
57
|
+
|
|
58
|
+
ClaudeAcpAgent.prototype.canUseTool = function (this: InstanceType<typeof ClaudeAcpAgent>, sessionId) {
|
|
59
|
+
const adapters = canUseTool.call(this, sessionId);
|
|
60
|
+
return async (toolName, input, options) => {
|
|
61
|
+
if (toolName !== "AskUserQuestion") return await adapters(toolName, input, options);
|
|
62
|
+
const questions: unknown = input.questions;
|
|
63
|
+
const cancelled = (): void => {
|
|
64
|
+
void this.client.extNotification("_niwa/cancel_request", { sessionId, toolCallId: options.toolUseID }).catch(() => {});
|
|
65
|
+
};
|
|
66
|
+
options.signal.addEventListener("abort", cancelled, { once: true });
|
|
67
|
+
try {
|
|
68
|
+
const reply = (await this.client.extMethod("_niwa/ask_user_question", {
|
|
69
|
+
sessionId,
|
|
70
|
+
toolCallId: options.toolUseID,
|
|
71
|
+
questions,
|
|
72
|
+
})) as QuestionAnswers;
|
|
73
|
+
if (reply.cancelled === true || reply.answers === undefined) {
|
|
74
|
+
return { behavior: "deny", message: "The user did not answer the question." };
|
|
75
|
+
}
|
|
76
|
+
return { behavior: "allow", updatedInput: { ...input, answers: reply.answers } };
|
|
77
|
+
} catch (error) {
|
|
78
|
+
return { behavior: "deny", message: `The question could not be asked: ${error instanceof Error ? error.message : String(error)}` };
|
|
79
|
+
} finally {
|
|
80
|
+
options.signal.removeEventListener("abort", cancelled);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Forwards the model of the session's `system/init` message to the client.
|
|
87
|
+
*
|
|
88
|
+
* The messages of the program are read by the adapter through the `next` of the session's query,
|
|
89
|
+
* so that is where the init message is seen. A session the adapter did not make is not one to guess
|
|
90
|
+
* about: it fails, as the switch above does, rather than leaving the model unreported.
|
|
91
|
+
*/
|
|
92
|
+
function forwardModel(agent: InstanceType<typeof ClaudeAcpAgent>, sessionId: string): void {
|
|
93
|
+
const query = agent.sessions[sessionId]?.query;
|
|
94
|
+
if (query === undefined) throw new Error(`the Claude Code ACP adapter made no session ${sessionId} to report the model of`);
|
|
95
|
+
const next = query.next.bind(query);
|
|
96
|
+
query.next = async (...args) => {
|
|
97
|
+
const step = await next(...args);
|
|
98
|
+
if (step.done !== true && step.value.type === "system" && step.value.subtype === "init") {
|
|
99
|
+
void agent.client.extNotification("_niwa/session_model", { sessionId, model: step.value.model }).catch(() => {});
|
|
100
|
+
}
|
|
101
|
+
return step;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const newSession = ClaudeAcpAgent.prototype.newSession;
|
|
106
|
+
const loadSession = ClaudeAcpAgent.prototype.loadSession;
|
|
107
|
+
|
|
108
|
+
ClaudeAcpAgent.prototype.newSession = async function (this: InstanceType<typeof ClaudeAcpAgent>, params) {
|
|
109
|
+
const created = await newSession.call(this, params);
|
|
110
|
+
forwardModel(this, created.sessionId);
|
|
111
|
+
return created;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
ClaudeAcpAgent.prototype.loadSession = async function (this: InstanceType<typeof ClaudeAcpAgent>, params) {
|
|
115
|
+
const loaded = await loadSession.call(this, params);
|
|
116
|
+
forwardModel(this, params.sessionId);
|
|
117
|
+
return loaded;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// What the adapter's own entry does before it serves: the managed settings, and stdout kept for ACP.
|
|
121
|
+
const managedSettings = loadManagedSettings();
|
|
122
|
+
if (managedSettings) applyEnvironmentSettings(managedSettings);
|
|
123
|
+
console.log = console.error;
|
|
124
|
+
console.info = console.error;
|
|
125
|
+
console.warn = console.error;
|
|
126
|
+
console.debug = console.error;
|
|
127
|
+
process.on("unhandledRejection", (reason, promise) => {
|
|
128
|
+
console.error("Unhandled Rejection at:", promise, "reason:", reason);
|
|
129
|
+
});
|
|
130
|
+
runAcp();
|
|
131
|
+
process.stdin.resume();
|