modkh 0.0.0-stage → 0.2.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 +66 -2
- package/dist/agent/bundled-skills.d.ts +13 -0
- package/dist/agent/bundled-skills.js +95 -0
- package/dist/agent/compaction.d.ts +53 -0
- package/dist/agent/compaction.js +191 -0
- package/dist/agent/images.d.ts +10 -0
- package/dist/agent/images.js +33 -0
- package/dist/agent/loop.d.ts +103 -0
- package/dist/agent/loop.js +267 -0
- package/dist/agent/skills.d.ts +26 -0
- package/dist/agent/skills.js +190 -0
- package/dist/agent/types.d.ts +137 -0
- package/dist/agent/types.js +12 -0
- package/dist/cli/agenticreplay.d.ts +1 -0
- package/dist/cli/agenticreplay.js +66 -0
- package/dist/cli/agents.d.ts +8 -0
- package/dist/cli/agents.js +12 -0
- package/dist/cli/answer-markdown.d.ts +3 -0
- package/dist/cli/answer-markdown.js +29 -0
- package/dist/cli/approval.d.ts +29 -0
- package/dist/cli/approval.js +92 -0
- package/dist/cli/catalog.d.ts +37 -0
- package/dist/cli/catalog.js +46 -0
- package/dist/cli/chat.d.ts +18 -0
- package/dist/cli/chat.js +1061 -0
- package/dist/cli/clipboard.d.ts +11 -0
- package/dist/cli/clipboard.js +82 -0
- package/dist/cli/color.d.ts +59 -0
- package/dist/cli/color.js +244 -0
- package/dist/cli/compaction-settings.d.ts +21 -0
- package/dist/cli/compaction-settings.js +54 -0
- package/dist/cli/completion-art.d.ts +2 -0
- package/dist/cli/completion-art.js +62 -0
- package/dist/cli/config.d.ts +50 -0
- package/dist/cli/config.js +192 -0
- package/dist/cli/editor.d.ts +23 -0
- package/dist/cli/editor.js +115 -0
- package/dist/cli/file-mentions.d.ts +14 -0
- package/dist/cli/file-mentions.js +141 -0
- package/dist/cli/head-checks.d.ts +41 -0
- package/dist/cli/head-checks.js +146 -0
- package/dist/cli/initial-connection.d.ts +7 -0
- package/dist/cli/initial-connection.js +20 -0
- package/dist/cli/keys.d.ts +29 -0
- package/dist/cli/keys.js +107 -0
- package/dist/cli/language.d.ts +1 -0
- package/dist/cli/language.js +105 -0
- package/dist/cli/limits.d.ts +13 -0
- package/dist/cli/limits.js +19 -0
- package/dist/cli/lines.d.ts +18 -0
- package/dist/cli/lines.js +80 -0
- package/dist/cli/model-catalog.d.ts +16 -0
- package/dist/cli/model-catalog.js +130 -0
- package/dist/cli/models.d.ts +4 -0
- package/dist/cli/models.js +101 -0
- package/dist/cli/official-models.d.ts +6 -0
- package/dist/cli/official-models.js +77 -0
- package/dist/cli/pdf.d.ts +8 -0
- package/dist/cli/pdf.js +71 -0
- package/dist/cli/permissions.d.ts +25 -0
- package/dist/cli/permissions.js +30 -0
- package/dist/cli/plugins.d.ts +1 -0
- package/dist/cli/plugins.js +53 -0
- package/dist/cli/providers.d.ts +10 -0
- package/dist/cli/providers.js +82 -0
- package/dist/cli/shell-display.d.ts +6 -0
- package/dist/cli/shell-display.js +35 -0
- package/dist/cli/stateright.d.ts +1 -0
- package/dist/cli/stateright.js +37 -0
- package/dist/cli/terminal-mouse.d.ts +21 -0
- package/dist/cli/terminal-mouse.js +51 -0
- package/dist/cli/terminal.d.ts +231 -0
- package/dist/cli/terminal.js +1417 -0
- package/dist/cli/tool-display.d.ts +9 -0
- package/dist/cli/tool-display.js +87 -0
- package/dist/cli/transcript-selection.d.ts +22 -0
- package/dist/cli/transcript-selection.js +75 -0
- package/dist/cli/usage.d.ts +22 -0
- package/dist/cli/usage.js +117 -0
- package/dist/cli/waiting-nyan.d.ts +3 -0
- package/dist/cli/waiting-nyan.js +17 -0
- package/dist/cli/waiting-whales.d.ts +3 -0
- package/dist/cli/waiting-whales.js +24 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +186 -0
- package/dist/language/catalog.d.ts +44 -0
- package/dist/language/catalog.js +100 -0
- package/dist/language/commonlisp-bundled.d.ts +11 -0
- package/dist/language/commonlisp-bundled.js +22 -0
- package/dist/language/commonlisp-dependencies.d.ts +16 -0
- package/dist/language/commonlisp-dependencies.js +68 -0
- package/dist/language/commonlisp-install.d.ts +15 -0
- package/dist/language/commonlisp-install.js +214 -0
- package/dist/language/commonlisp-reader.d.ts +17 -0
- package/dist/language/commonlisp-reader.js +47 -0
- package/dist/language/commonlisp-systems.d.ts +3 -0
- package/dist/language/commonlisp-systems.js +32 -0
- package/dist/language/commonlisp-tls.d.ts +10 -0
- package/dist/language/commonlisp-tls.js +51 -0
- package/dist/language/detect.d.ts +29 -0
- package/dist/language/detect.js +103 -0
- package/dist/language/grammars/LICENSE +24 -0
- package/dist/language/grammars/manifest.json +76 -0
- package/dist/language/grammars/tree-sitter-c.wasm +0 -0
- package/dist/language/grammars/tree-sitter-cpp.wasm +0 -0
- package/dist/language/grammars/tree-sitter-css.wasm +0 -0
- package/dist/language/grammars/tree-sitter-html.wasm +0 -0
- package/dist/language/grammars/tree-sitter-javascript.wasm +0 -0
- package/dist/language/grammars/tree-sitter-php.wasm +0 -0
- package/dist/language/grammars/tree-sitter-python.wasm +0 -0
- package/dist/language/grammars/tree-sitter-ruby.wasm +0 -0
- package/dist/language/grammars/tree-sitter-rust.wasm +0 -0
- package/dist/language/grammars/tree-sitter-swift.wasm +0 -0
- package/dist/language/grammars/tree-sitter-tsx.wasm +0 -0
- package/dist/language/grammars/tree-sitter-typescript.wasm +0 -0
- package/dist/language/install.d.ts +50 -0
- package/dist/language/install.js +293 -0
- package/dist/language/lsp/client.d.ts +97 -0
- package/dist/language/lsp/client.js +340 -0
- package/dist/language/lsp/pool.d.ts +75 -0
- package/dist/language/lsp/pool.js +140 -0
- package/dist/language/lsp/service.d.ts +50 -0
- package/dist/language/lsp/service.js +174 -0
- package/dist/language/native.d.ts +8 -0
- package/dist/language/native.js +100 -0
- package/dist/language/platform.d.ts +14 -0
- package/dist/language/platform.js +20 -0
- package/dist/language/positions.d.ts +20 -0
- package/dist/language/positions.js +46 -0
- package/dist/language/syntax/assets.d.ts +9 -0
- package/dist/language/syntax/assets.js +73 -0
- package/dist/language/syntax/parser.d.ts +33 -0
- package/dist/language/syntax/parser.js +162 -0
- package/dist/language/typescript-capability.d.ts +6 -0
- package/dist/language/typescript-capability.js +42 -0
- package/dist/lisp/builtin-agents.d.ts +5 -0
- package/dist/lisp/builtin-agents.js +52 -0
- package/dist/lisp/config.d.ts +25 -0
- package/dist/lisp/config.js +202 -0
- package/dist/lisp/routing.d.ts +5 -0
- package/dist/lisp/routing.js +24 -0
- package/dist/lisp/runtime.d.ts +26 -0
- package/dist/lisp/runtime.js +107 -0
- package/dist/memory/kiokuko.d.ts +32 -0
- package/dist/memory/kiokuko.js +102 -0
- package/dist/memory/mcp.d.ts +38 -0
- package/dist/memory/mcp.js +211 -0
- package/dist/model/ai-sdk.d.ts +27 -0
- package/dist/model/ai-sdk.js +185 -0
- package/dist/model/chatgpt/login.d.ts +42 -0
- package/dist/model/chatgpt/login.js +180 -0
- package/dist/model/chatgpt/responses.d.ts +41 -0
- package/dist/model/chatgpt/responses.js +254 -0
- package/dist/model/chatgpt/tokens.d.ts +11 -0
- package/dist/model/chatgpt/tokens.js +50 -0
- package/dist/model/connection.d.ts +6 -0
- package/dist/model/connection.js +67 -0
- package/dist/model/context-window.d.ts +3 -0
- package/dist/model/context-window.js +64 -0
- package/dist/model/context.d.ts +11 -0
- package/dist/model/context.js +69 -0
- package/dist/model/factory.d.ts +27 -0
- package/dist/model/factory.js +89 -0
- package/dist/model/fixed.d.ts +17 -0
- package/dist/model/fixed.js +41 -0
- package/dist/model/harness-profile.d.ts +6 -0
- package/dist/model/harness-profile.js +23 -0
- package/dist/model/providers.d.ts +32 -0
- package/dist/model/providers.js +72 -0
- package/dist/model/reasoning.d.ts +16 -0
- package/dist/model/reasoning.js +84 -0
- package/dist/model/record-fetch.d.ts +2 -0
- package/dist/model/record-fetch.js +19 -0
- package/dist/plugins/api.d.ts +60 -0
- package/dist/plugins/api.js +1 -0
- package/dist/plugins/manager.d.ts +35 -0
- package/dist/plugins/manager.js +280 -0
- package/dist/plugins/package.d.ts +13 -0
- package/dist/plugins/package.js +99 -0
- package/dist/plugins/registry.d.ts +8 -0
- package/dist/plugins/registry.js +14 -0
- package/dist/session/store.d.ts +93 -0
- package/dist/session/store.js +311 -0
- package/dist/session/workspace.d.ts +19 -0
- package/dist/session/workspace.js +73 -0
- package/dist/subagents/head-assignment.d.ts +11 -0
- package/dist/subagents/head-assignment.js +47 -0
- package/dist/subagents/limits.d.ts +4 -0
- package/dist/subagents/limits.js +6 -0
- package/dist/subagents/processes.d.ts +18 -0
- package/dist/subagents/processes.js +73 -0
- package/dist/subagents/request.d.ts +4 -0
- package/dist/subagents/request.js +38 -0
- package/dist/subagents/service.d.ts +73 -0
- package/dist/subagents/service.js +645 -0
- package/dist/subagents/settings.d.ts +10 -0
- package/dist/subagents/settings.js +32 -0
- package/dist/subagents/types.d.ts +63 -0
- package/dist/subagents/types.js +1 -0
- package/dist/subagents/verification.d.ts +34 -0
- package/dist/subagents/verification.js +215 -0
- package/dist/tools/browse.d.ts +13 -0
- package/dist/tools/browse.js +117 -0
- package/dist/tools/browser-capability.d.ts +3 -0
- package/dist/tools/browser-capability.js +29 -0
- package/dist/tools/diff.d.ts +6 -0
- package/dist/tools/diff.js +82 -0
- package/dist/tools/edit.d.ts +3 -0
- package/dist/tools/edit.js +152 -0
- package/dist/tools/files.d.ts +6 -0
- package/dist/tools/files.js +96 -0
- package/dist/tools/formal-input.d.ts +6 -0
- package/dist/tools/formal-input.js +29 -0
- package/dist/tools/formal.d.ts +9 -0
- package/dist/tools/formal.js +79 -0
- package/dist/tools/guard.d.ts +7 -0
- package/dist/tools/guard.js +48 -0
- package/dist/tools/job-tools.d.ts +19 -0
- package/dist/tools/job-tools.js +148 -0
- package/dist/tools/jobs.d.ts +79 -0
- package/dist/tools/jobs.js +186 -0
- package/dist/tools/language.d.ts +6 -0
- package/dist/tools/language.js +172 -0
- package/dist/tools/lean-proof.d.ts +38 -0
- package/dist/tools/lean-proof.js +83 -0
- package/dist/tools/registry.d.ts +30 -0
- package/dist/tools/registry.js +113 -0
- package/dist/tools/run.d.ts +13 -0
- package/dist/tools/run.js +163 -0
- package/dist/tools/search.d.ts +2 -0
- package/dist/tools/search.js +156 -0
- package/dist/tools/shell-process.d.ts +115 -0
- package/dist/tools/shell-process.js +297 -0
- package/dist/tools/skills.d.ts +2 -0
- package/dist/tools/skills.js +46 -0
- package/dist/tools/stateright-runtime.d.ts +3 -0
- package/dist/tools/stateright-runtime.js +16 -0
- package/dist/tools/stateright.d.ts +3 -0
- package/dist/tools/stateright.js +78 -0
- package/dist/tools/web.d.ts +24 -0
- package/dist/tools/web.js +391 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/docs/ci-repair-20261116.md +30 -0
- package/docs/commonlisp-build-output.md +14 -0
- package/docs/commonlisp-provider-migration.md +22 -0
- package/docs/commonlisp-qlot-recovery.md +28 -0
- package/docs/commonlisp-tls-recovery.md +28 -0
- package/docs/context-efficiency.md +122 -0
- package/docs/input.md +126 -0
- package/docs/languages.md +117 -0
- package/docs/models.md +109 -0
- package/docs/modkh-evidence.json +641 -0
- package/docs/modkh-failure-comparison.json +675 -0
- package/docs/modkh-namespace.md +39 -0
- package/docs/modkh-qlot-evidence.json +37 -0
- package/docs/modkh-verification.md +173 -0
- package/docs/modkh.md +128 -0
- package/docs/presets.md +64 -0
- package/docs/setup.md +99 -0
- package/docs/stateright.md +45 -0
- package/docs/subagents.md +152 -0
- package/docs/web.md +57 -0
- package/experiments/presets/common-lisp.lisp +38 -0
- package/experiments/presets/css.lisp +38 -0
- package/experiments/presets/formal.lisp +18 -0
- package/experiments/presets/html.lisp +38 -0
- package/experiments/presets/js.lisp +38 -0
- package/experiments/presets/language-microagents.lisp +29 -0
- package/experiments/presets/php.lisp +38 -0
- package/experiments/presets/svelte.lisp +38 -0
- package/experiments/presets/tailwindcss.lisp +24 -0
- package/experiments/presets/ts.lisp +38 -0
- package/native/stateright/Cargo.lock +478 -0
- package/native/stateright/Cargo.toml +11 -0
- package/native/stateright/src/lib.rs +163 -0
- package/native/stateright/src/main.rs +38 -0
- package/package.json +57 -4
- package/plugins/modkh-explorer/index.mjs +3 -0
- package/plugins/modkh-explorer/plugin.json +1 -0
- package/plugins/modkh-worker/index.mjs +3 -0
- package/plugins/modkh-worker/plugin.json +1 -0
- package/skills/kiokuko-dsh/LICENSE +21 -0
- package/skills/kiokuko-dsh/coding-ideal-routine-skill/SKILL.md +106 -0
- package/skills/kiokuko-dsh/japanese-translation-for-oss-models/SKILL.md +65 -0
- package/skills/kiokuko-dsh/kiokuko-architecture/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-architecture/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-benchmark/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-benchmark/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-investigate/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-investigate/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-lisp/SKILL.md +390 -0
- package/skills/kiokuko-dsh/kiokuko-review/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-review/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-simple-work/SKILL.md +70 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/SKILL.md +79 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/boundaries-and-ownership.md +52 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/domain-and-types.md +48 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/effects-and-data.md +43 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/kiokuko-patterns.md +314 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/problem-shaping-and-language.md +90 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/protocols-and-idempotency.md +42 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/review-checklist.md +92 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/verification.md +45 -0
- package/skills/kiokuko-dsh/kiokuko-skill-authoring/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-skill-authoring/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-soul/SKILL.md +25 -0
- package/skills/kiokuko-dsh/kiokuko-technical-writing/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-technical-writing/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/SKILL.md +64 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/accessibility-and-navigation.md +31 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/async-recovery.md +49 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/forms-and-controls.md +35 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/interaction-feedback.md +34 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/responsive-and-platform.md +38 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/safety-and-review.md +43 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/ui-checklist.md +75 -0
- package/skills/kiokuko-dsh/kiokuko-verification/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-verification/references/cases.md +11 -0
- package/skills/kiokuko-dsh/manifest.json +168 -0
- package/skills/kiokuko-dsh/memory-reasoning/SKILL.md +44 -0
- package/skills/kiokuko-dsh/one-shot-software-completion/SKILL.md +113 -0
- package/skills/kiokuko-dsh/one-shot-software-completion/references/boundaries-and-lifecycle.md +70 -0
- package/skills/kiokuko-dsh/one-shot-software-completion/references/discovery-and-scope.md +71 -0
- package/skills/kiokuko-dsh/one-shot-software-completion/references/failure-recovery.md +43 -0
- package/skills/kiokuko-dsh/one-shot-software-completion/references/verification-and-completion.md +73 -0
- package/skills/kiokuko-dsh/veteran-programmer-skill/SKILL.md +66 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
import { ToolError } from "../agent/types.js";
|
|
3
|
+
import { listFilesTool, searchFilesTool, readFileTool } from "./files.js";
|
|
4
|
+
import { createFileTool, editFileTool } from "./edit.js";
|
|
5
|
+
import { runCommandTool } from "./run.js";
|
|
6
|
+
import { jobTools } from "./job-tools.js";
|
|
7
|
+
import { webTools } from "./web.js";
|
|
8
|
+
import { browseTools } from "./browse.js";
|
|
9
|
+
import { skillTools } from "./skills.js";
|
|
10
|
+
/**
|
|
11
|
+
* ツール名・引数形状・宣言外の引数を検証する。
|
|
12
|
+
* 各フィールドの値は個々のツールが boundary で検証する。
|
|
13
|
+
*/
|
|
14
|
+
export class ToolRegistry {
|
|
15
|
+
tools;
|
|
16
|
+
onExecute;
|
|
17
|
+
constructor(tools, onExecute) {
|
|
18
|
+
this.onExecute = onExecute;
|
|
19
|
+
this.tools = new Map();
|
|
20
|
+
for (const tool of tools) {
|
|
21
|
+
if (this.tools.has(tool.name))
|
|
22
|
+
throw new Error(`Duplicate tool: ${tool.name}`);
|
|
23
|
+
this.tools.set(tool.name, tool);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
specs() {
|
|
27
|
+
return [...this.tools.values()].map((t) => ({
|
|
28
|
+
name: t.name,
|
|
29
|
+
description: t.description,
|
|
30
|
+
parameters: t.parameters,
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
get(name) {
|
|
34
|
+
return this.tools.get(name);
|
|
35
|
+
}
|
|
36
|
+
/** Match unresolved effects conservatively. Only trusted tool code defines defaults. */
|
|
37
|
+
sameOperation(a, b, ctx) {
|
|
38
|
+
if (a.name !== b.name)
|
|
39
|
+
return false;
|
|
40
|
+
const tool = this.tools.get(a.name);
|
|
41
|
+
if (!tool?.operationIdentity)
|
|
42
|
+
return isDeepStrictEqual(a.args, b.args);
|
|
43
|
+
try {
|
|
44
|
+
return isDeepStrictEqual(tool.operationIdentity(a.args, ctx), tool.operationIdentity(b.args, ctx));
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// Failure to establish a different operation cannot authorize another effect.
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* モデルの要求を1件ずつ検証して実行する。
|
|
53
|
+
* 未知のツール・引数不正は結果として返す(例外を握らず、モデルに観測させる)。
|
|
54
|
+
* signal が abort された以降は新規実行しない(status:'not-executed')。
|
|
55
|
+
*/
|
|
56
|
+
async executeOne(call, ctx) {
|
|
57
|
+
const base = { callId: call.id, name: call.name };
|
|
58
|
+
if (ctx.signal?.aborted) {
|
|
59
|
+
return { ...base, status: 'not-executed', output: '中断済みのため実行していない' };
|
|
60
|
+
}
|
|
61
|
+
const tool = this.tools.get(call.name);
|
|
62
|
+
if (tool === undefined) {
|
|
63
|
+
return {
|
|
64
|
+
...base,
|
|
65
|
+
status: 'error',
|
|
66
|
+
output: `未知のツール名: ${call.name}(登録済み: ${[...this.tools.keys()].join(', ')})`,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (call.args === null || typeof call.args !== 'object' || Array.isArray(call.args)) {
|
|
70
|
+
return { ...base, status: 'error', output: '引数は JSON オブジェクトで渡す' };
|
|
71
|
+
}
|
|
72
|
+
const properties = tool.parameters.properties;
|
|
73
|
+
if (properties !== null && typeof properties === 'object' && !Array.isArray(properties)) {
|
|
74
|
+
const unknown = Object.keys(call.args).filter(key => !Object.prototype.hasOwnProperty.call(properties, key));
|
|
75
|
+
if (unknown.length) {
|
|
76
|
+
return { ...base, status: 'error', output: `[INVALID_ARGS] 未知の引数: ${unknown.join(', ')}` };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Display observers must not affect whether an accepted tool runs.
|
|
80
|
+
try {
|
|
81
|
+
this.onExecute?.();
|
|
82
|
+
}
|
|
83
|
+
catch { /* advisory only */ }
|
|
84
|
+
try {
|
|
85
|
+
const output = await tool.execute(call.args, { ...ctx, callId: call.id });
|
|
86
|
+
if (typeof output === 'string')
|
|
87
|
+
return { ...base, status: 'ok', output };
|
|
88
|
+
if (!output || !['ok', 'error', 'unknown', 'not-executed'].includes(output.status) || typeof output.output !== 'string')
|
|
89
|
+
throw new Error('Invalid tool outcome');
|
|
90
|
+
return { ...base, status: output.status, output: output.output };
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
if (e instanceof ToolError && (e.code === 'NOT_EXECUTED' || e.code === 'DENIED')) {
|
|
94
|
+
return { ...base, status: e.code === 'DENIED' ? 'error' : 'not-executed', output: `[${e.code}] ${e.message}` };
|
|
95
|
+
}
|
|
96
|
+
if (ctx.signal?.aborted || (e instanceof ToolError && (e.code === 'ABORTED' || e.code === 'RESULT_UNKNOWN'))) {
|
|
97
|
+
return { ...base, status: 'unknown', output: `中断により結果不明: ${e.message}` };
|
|
98
|
+
}
|
|
99
|
+
if (e instanceof ToolError)
|
|
100
|
+
return { ...base, status: 'error', output: `[${e.code}] ${e.message}` };
|
|
101
|
+
return { ...base, status: tool.readOnly ? 'error' : 'unknown',
|
|
102
|
+
output: `内部エラー(副作用の有無は未確認): ${e instanceof Error ? e.message : String(e)}` };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** 初版が提供する5区分のツール群(作成/編集は別ツール)。 */
|
|
107
|
+
export function defaultTools(tabs) {
|
|
108
|
+
return [listFilesTool, searchFilesTool, readFileTool, createFileTool, editFileTool, runCommandTool, ...browseTools(tabs), ...webTools, ...skillTools];
|
|
109
|
+
}
|
|
110
|
+
/** The same tools plus background execution, which needs a per-session job registry. */
|
|
111
|
+
export function defaultToolsWithJobs(jobs, tabs) {
|
|
112
|
+
return [...defaultTools(tabs), ...jobTools(jobs)];
|
|
113
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Tool } from '../agent/types.ts';
|
|
2
|
+
export { runShellProcess, runShell, describeShellResult, resolveShell, SHELLS, TextSink, ShellProcess } from './shell-process.ts';
|
|
3
|
+
export type { ShellName, ShellOptions, ShellResult } from './shell-process.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Whether a foreground command that hits its deadline becomes a job.
|
|
6
|
+
*
|
|
7
|
+
* On by default because killing a build at 120 seconds discards work the human wanted. A
|
|
8
|
+
* caller can turn it off to keep the strict "a deadline is a failure" contract.
|
|
9
|
+
*/
|
|
10
|
+
export declare const DEFAULT_PROMOTE_ON_TIMEOUT = true;
|
|
11
|
+
export declare const runCommandTool: Tool;
|
|
12
|
+
/** Host-only acceptance path: same command authorization, no job promotion, no stdout parsing. */
|
|
13
|
+
export declare function executeAcceptance(command: string, root: string, approval: import('../agent/types.ts').ApprovalGate, signal: AbortSignal, inputDigest: string, env: NodeJS.ProcessEnv): Promise<import('../lisp/runtime.ts').ExecutionReceipt>;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { ToolError } from "../agent/types.js";
|
|
3
|
+
import { resolveInRoot, assertRealInRoot } from "./guard.js";
|
|
4
|
+
import { describeShellResult, ShellProcess, SHELLS } from "./shell-process.js";
|
|
5
|
+
import { prepareCommand } from "./job-tools.js";
|
|
6
|
+
import { browserCapability } from "./browser-capability.js";
|
|
7
|
+
export { runShellProcess, runShell, describeShellResult, resolveShell, SHELLS, TextSink, ShellProcess } from "./shell-process.js";
|
|
8
|
+
const DEFAULT_TIMEOUT_MS = 120_000;
|
|
9
|
+
/** A single foreground call may run longer than the default, but never without a bound. */
|
|
10
|
+
const MAX_TIMEOUT_MS = 600_000;
|
|
11
|
+
function positiveInt(v, name, max) {
|
|
12
|
+
if (v === undefined)
|
|
13
|
+
return undefined;
|
|
14
|
+
const n = typeof v === 'string' && /^\d+$/.test(v.trim()) ? Number(v.trim()) : typeof v === 'number' ? v : Number.NaN;
|
|
15
|
+
if (!Number.isSafeInteger(n) || n < 1 || n > max)
|
|
16
|
+
throw new ToolError('INVALID_ARGS', `${name} は1〜${max}の整数で指定する`);
|
|
17
|
+
return n;
|
|
18
|
+
}
|
|
19
|
+
/** Resolve, approve, and verify the target directory without running anything. */
|
|
20
|
+
async function authorize(args, ctx) {
|
|
21
|
+
const prepared = prepareCommand(args, ctx.root);
|
|
22
|
+
// Capture the real target BEFORE approval; the check after approval compares against it, so a
|
|
23
|
+
// directory swapped for another (even inside the root) cannot silently run the command.
|
|
24
|
+
let approvedCwd;
|
|
25
|
+
try {
|
|
26
|
+
approvedCwd = assertRealInRoot(ctx.root, prepared.cwd);
|
|
27
|
+
if (!fs.statSync(prepared.cwd).isDirectory()) {
|
|
28
|
+
throw new ToolError('INVALID_ARGS', `作業ディレクトリが不正: ${prepared.cwdRel}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
if (e instanceof ToolError)
|
|
33
|
+
throw e;
|
|
34
|
+
throw new ToolError('INVALID_ARGS', `作業ディレクトリが存在しない: ${prepared.cwdRel}`);
|
|
35
|
+
}
|
|
36
|
+
if (ctx.signal?.aborted)
|
|
37
|
+
throw new ToolError('NOT_EXECUTED', '中断済みのため実行しない');
|
|
38
|
+
const ok = await ctx.approval.approve({
|
|
39
|
+
kind: 'command',
|
|
40
|
+
title: `コマンド実行 (${prepared.cwdRel})`,
|
|
41
|
+
detail: `${prepared.command}\n\n作業場所: ${prepared.cwd}`,
|
|
42
|
+
// The cwd was resolved and its real path proven inside the root above.
|
|
43
|
+
outsideWorkspace: false,
|
|
44
|
+
});
|
|
45
|
+
if (!ok)
|
|
46
|
+
throw new ToolError('DENIED', 'コマンドは承認されなかったため実行しない');
|
|
47
|
+
if (ctx.signal?.aborted)
|
|
48
|
+
throw new ToolError('NOT_EXECUTED', '中断済みのため実行しない');
|
|
49
|
+
try {
|
|
50
|
+
if (assertRealInRoot(ctx.root, prepared.cwd) !== approvedCwd || !fs.statSync(prepared.cwd).isDirectory()) {
|
|
51
|
+
throw new Error('承認待ちの間に作業場所が変更された');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
throw new ToolError('NOT_EXECUTED', `承認後の作業場所を確認できないため実行しない: ${e.message}`);
|
|
56
|
+
}
|
|
57
|
+
return prepared;
|
|
58
|
+
}
|
|
59
|
+
const OPERATION_IDENTITY = (args, ctx) => ({
|
|
60
|
+
command: args.command,
|
|
61
|
+
cwd: resolveInRoot(ctx.root, typeof args.cwd === 'string' ? args.cwd : '.'),
|
|
62
|
+
shell: args.shell ?? 'sh',
|
|
63
|
+
});
|
|
64
|
+
const PARAMETERS = {
|
|
65
|
+
type: 'object',
|
|
66
|
+
properties: {
|
|
67
|
+
description: { type: 'string', description: '何をするコマンドか(5〜10語、命令形。例: "run unit tests")' },
|
|
68
|
+
command: { type: 'string', description: '実行するコマンド' },
|
|
69
|
+
cwd: { type: 'string', description: '作業ディレクトリ(ルート相対、既定はルート)' },
|
|
70
|
+
shell: { type: 'string', enum: [...SHELLS], description: '使用するシェル(既定 sh。bash は set -eou pipefail を先頭に追加する)' },
|
|
71
|
+
timeoutMs: { type: 'number', description: `制限時間(ミリ秒、既定 ${DEFAULT_TIMEOUT_MS}、上限 ${MAX_TIMEOUT_MS})` },
|
|
72
|
+
run_in_background: { type: 'boolean', description: 'true にすると即座に job_id を返し、結果は job_output で読む。長時間かかる処理で使う' },
|
|
73
|
+
},
|
|
74
|
+
required: ['command'],
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Whether a foreground command that hits its deadline becomes a job.
|
|
78
|
+
*
|
|
79
|
+
* On by default because killing a build at 120 seconds discards work the human wanted. A
|
|
80
|
+
* caller can turn it off to keep the strict "a deadline is a failure" contract.
|
|
81
|
+
*/
|
|
82
|
+
export const DEFAULT_PROMOTE_ON_TIMEOUT = true;
|
|
83
|
+
const BACKGROUND_JOB_HELP = 'ジョブが終了すると通知が届きます。実行中に出力を確認するには job_output、中止するには job_kill に上のジョブIDを指定してください。';
|
|
84
|
+
export const runCommandTool = {
|
|
85
|
+
name: 'run_command',
|
|
86
|
+
operationIdentity: OPERATION_IDENTITY,
|
|
87
|
+
get description() {
|
|
88
|
+
return 'シェルコマンドを1回実行し、終了コード・stdout・stderr を返す。作業場所は作業ルート内に限る。'
|
|
89
|
+
+ '終了コードが0以外でも実行結果として返る(エラー扱いにはしない)。'
|
|
90
|
+
+ 'cd は次の呼び出しに引き継がれないので cwd を指定する。'
|
|
91
|
+
+ '確認は grep やテスト実行のように終了コードを見て判断する。'
|
|
92
|
+
+ 'run_in_background を true にすると待たずに job_id を返す。ビルドやテストの全体実行など、'
|
|
93
|
+
+ '制限時間に収まらない見込みの処理で使う。' + browserCapability();
|
|
94
|
+
},
|
|
95
|
+
parameters: PARAMETERS,
|
|
96
|
+
async execute(args, ctx) {
|
|
97
|
+
const prepared = await authorize(args, ctx);
|
|
98
|
+
if (args.run_in_background === true) {
|
|
99
|
+
// A background job has no deadline: it runs until it exits or job_kill stops it.
|
|
100
|
+
if (!ctx.jobs)
|
|
101
|
+
throw new ToolError('INVALID_ARGS', 'バックグラウンド実行が有効でない');
|
|
102
|
+
const view = ctx.jobs.start(prepared);
|
|
103
|
+
return [
|
|
104
|
+
`[job_id: ${view.id}] [status: ${view.status}]`,
|
|
105
|
+
`作業場所: ${view.cwd}`,
|
|
106
|
+
BACKGROUND_JOB_HELP,
|
|
107
|
+
].join('\n');
|
|
108
|
+
}
|
|
109
|
+
const timeoutMs = positiveInt(args.timeoutMs, 'timeoutMs', MAX_TIMEOUT_MS) ?? DEFAULT_TIMEOUT_MS;
|
|
110
|
+
// A foreground command that reaches its deadline keeps running as a job instead of being
|
|
111
|
+
// killed: the work already done is not thrown away, and the model keeps a handle to it.
|
|
112
|
+
// The call must return AT the deadline, so it resolves on whichever happens first.
|
|
113
|
+
let resolvePromoted;
|
|
114
|
+
const promotedSignal = ctx.jobs === undefined
|
|
115
|
+
? undefined
|
|
116
|
+
: new Promise(resolve => { resolvePromoted = resolve; });
|
|
117
|
+
const shellProcess = new ShellProcess(prepared.command, prepared.cwd, {
|
|
118
|
+
shell: prepared.shell,
|
|
119
|
+
timeoutMs,
|
|
120
|
+
...(ctx.jobs === undefined ? {} : {
|
|
121
|
+
onTimeout: (process) => {
|
|
122
|
+
const view = ctx.jobs.adopt(process, prepared);
|
|
123
|
+
resolvePromoted?.(view);
|
|
124
|
+
},
|
|
125
|
+
}),
|
|
126
|
+
});
|
|
127
|
+
if (ctx.signal) {
|
|
128
|
+
const onAbort = () => shellProcess.stop('abort');
|
|
129
|
+
if (ctx.signal.aborted)
|
|
130
|
+
shellProcess.stop('abort');
|
|
131
|
+
else
|
|
132
|
+
ctx.signal.addEventListener('abort', onAbort, { once: true });
|
|
133
|
+
void shellProcess.result.finally(() => ctx.signal?.removeEventListener('abort', onAbort)).catch(() => { });
|
|
134
|
+
}
|
|
135
|
+
const outcome = await (promotedSignal
|
|
136
|
+
? Promise.race([
|
|
137
|
+
shellProcess.result.then(result => ({ kind: 'result', result })),
|
|
138
|
+
promotedSignal.then(view => ({ kind: 'promoted', view })),
|
|
139
|
+
])
|
|
140
|
+
: shellProcess.result.then(result => ({ kind: 'result', result })));
|
|
141
|
+
if (outcome.kind === 'promoted') {
|
|
142
|
+
// Promotion is not a failure: report the handle and how to keep reading.
|
|
143
|
+
shellProcess.abandon();
|
|
144
|
+
return [
|
|
145
|
+
`[job_id: ${outcome.view.id}] [status: ${outcome.view.status}]`,
|
|
146
|
+
`制限時間(${timeoutMs}ms)に達したためバックグラウンドジョブへ移した。コマンドは実行を続けている。`,
|
|
147
|
+
BACKGROUND_JOB_HELP,
|
|
148
|
+
].join('\n');
|
|
149
|
+
}
|
|
150
|
+
return describeShellResult(outcome.result);
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
/** Host-only acceptance path: same command authorization, no job promotion, no stdout parsing. */
|
|
154
|
+
export async function executeAcceptance(command, root, approval, signal, inputDigest, env) {
|
|
155
|
+
const { executeBounded } = await import("../lisp/runtime.js");
|
|
156
|
+
try {
|
|
157
|
+
const prepared = await authorize({ command, cwd: '.', shell: 'sh' }, { root, approval, signal });
|
|
158
|
+
return await executeBounded('/bin/sh', ['-c', prepared.command], { cwd: prepared.cwd, env, inputDigest, signal, timeoutMs: 30000 });
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
return { state: 'not-started', exitCode: null, signal: null, stdout: '', stderr: String(e), inputDigest, cleanup: 'settled' };
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { execFileSync } from 'node:child_process';
|
|
4
|
+
import { ToolError } from "../agent/types.js";
|
|
5
|
+
import { resolveInRoot, assertRealInRoot } from "./guard.js";
|
|
6
|
+
const MAX_FILE_BYTES = 1024 * 1024;
|
|
7
|
+
const MAX_SCAN_BYTES = 8 * MAX_FILE_BYTES;
|
|
8
|
+
const MAX_FILES = 5000;
|
|
9
|
+
const MAX_HITS = 50;
|
|
10
|
+
const OUTPUT_BUDGET = 14 * 1024;
|
|
11
|
+
const EXCLUDED = new Set(['.git', 'node_modules', '.agenticreplay', 'dist', 'build', 'coverage', '.next', '.cache']);
|
|
12
|
+
/** Git owns ignore semantics; no handwritten approximation of .gitignore. No hooks or optional index writes. */
|
|
13
|
+
function* candidates(root, start, scan) {
|
|
14
|
+
const relative = path.relative(root, start);
|
|
15
|
+
let listing;
|
|
16
|
+
try {
|
|
17
|
+
listing = execFileSync('git', ['--no-optional-locks', '-c', 'core.fsmonitor=false', '-C', root,
|
|
18
|
+
'ls-files', '-z', '--cached', '--others', '--exclude-standard',
|
|
19
|
+
...(relative ? ['--', `:(literal)${relative}/`] : [])], {
|
|
20
|
+
encoding: 'utf8', maxBuffer: 4 * 1024 * 1024, timeout: 5000, stdio: ['ignore', 'pipe', 'pipe'],
|
|
21
|
+
env: { ...process.env, LC_ALL: 'C', GIT_OPTIONAL_LOCKS: '0' },
|
|
22
|
+
});
|
|
23
|
+
scan.mode = '.gitignore・Gitの除外設定を適用';
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
const e = error;
|
|
27
|
+
if (e.code !== 'ENOENT' && !String(e.stderr).includes('not a git repository')) {
|
|
28
|
+
throw new ToolError('SEARCH_FAILED', 'Gitによる検索対象の列挙に失敗しました。pathで対象を絞ってください');
|
|
29
|
+
}
|
|
30
|
+
scan.mode = 'Git管理外: .git/node_modules/.agenticreplay/dist/build/coverage/.next/.cache を除外';
|
|
31
|
+
}
|
|
32
|
+
if (listing !== undefined) {
|
|
33
|
+
for (const name of [...new Set(listing.split('\0').filter(Boolean))].sort())
|
|
34
|
+
yield resolveInRoot(root, name);
|
|
35
|
+
}
|
|
36
|
+
else
|
|
37
|
+
yield* walk(start, scan);
|
|
38
|
+
}
|
|
39
|
+
function* walk(directory, scan) {
|
|
40
|
+
let entries;
|
|
41
|
+
try {
|
|
42
|
+
entries = fs.readdirSync(directory, { withFileTypes: true });
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
scan.skipped++;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
49
|
+
if (entry.isSymbolicLink() || EXCLUDED.has(entry.name))
|
|
50
|
+
continue;
|
|
51
|
+
const file = path.join(directory, entry.name);
|
|
52
|
+
if (entry.isDirectory())
|
|
53
|
+
yield* walk(file, scan);
|
|
54
|
+
else if (entry.isFile())
|
|
55
|
+
yield file;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function readSmallText(root, file) {
|
|
59
|
+
assertRealInRoot(root, file);
|
|
60
|
+
if (!fs.lstatSync(file).isFile())
|
|
61
|
+
return undefined;
|
|
62
|
+
const fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
|
|
63
|
+
try {
|
|
64
|
+
const size = fs.fstatSync(fd).size;
|
|
65
|
+
if (size > MAX_FILE_BYTES)
|
|
66
|
+
return undefined;
|
|
67
|
+
const buffer = Buffer.alloc(Math.min(MAX_FILE_BYTES, size) + 1);
|
|
68
|
+
const count = fs.readSync(fd, buffer, 0, buffer.length, 0);
|
|
69
|
+
if (count > MAX_FILE_BYTES)
|
|
70
|
+
return undefined;
|
|
71
|
+
const bytes = buffer.subarray(0, count);
|
|
72
|
+
return bytes.includes(0) ? undefined : bytes;
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
fs.closeSync(fd);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function excerpt(line, at) {
|
|
79
|
+
if (line.length <= 400)
|
|
80
|
+
return line;
|
|
81
|
+
let start = Math.max(0, at - 100), end = Math.min(line.length, Math.max(at + 200, 400));
|
|
82
|
+
if (start > 0 && /[\uDC00-\uDFFF]/.test(line[start]))
|
|
83
|
+
start--;
|
|
84
|
+
if (end < line.length && /[\uD800-\uDBFF]/.test(line[end - 1]))
|
|
85
|
+
end++;
|
|
86
|
+
return `${start ? '…' : ''}${line.slice(start, end)}${end < line.length ? '…' : ''} [行の一部省略]`;
|
|
87
|
+
}
|
|
88
|
+
export const searchFilesTool = {
|
|
89
|
+
readOnly: true,
|
|
90
|
+
name: 'search_files',
|
|
91
|
+
description: 'テキストを正規表現で検索する。Gitの除外設定を尊重し、最大50件・出力16 KiB。pathで対象を絞れる。省略がある場合は明示する。',
|
|
92
|
+
parameters: { type: 'object', properties: {
|
|
93
|
+
pattern: { type: 'string', maxLength: 512, description: '正規表現' },
|
|
94
|
+
path: { type: 'string', description: '検索起点ディレクトリ(ルート相対)' },
|
|
95
|
+
}, required: ['pattern'] },
|
|
96
|
+
async execute(args, ctx) {
|
|
97
|
+
if (typeof args.pattern !== 'string' || !args.pattern || args.pattern.length > 512)
|
|
98
|
+
throw new ToolError('INVALID_ARGS', 'patternは1〜512文字の正規表現で指定する');
|
|
99
|
+
let regex;
|
|
100
|
+
try {
|
|
101
|
+
regex = new RegExp(args.pattern);
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
throw new ToolError('INVALID_ARGS', `patternが不正な正規表現: ${e.message}`);
|
|
105
|
+
}
|
|
106
|
+
const start = resolveInRoot(ctx.root, args.path ?? '.');
|
|
107
|
+
assertRealInRoot(ctx.root, start);
|
|
108
|
+
const scan = { skipped: 0, mode: '' }, hits = [];
|
|
109
|
+
let files = 0, bytes = 0, outputBytes = 0, stopped = '';
|
|
110
|
+
search: for (const file of candidates(ctx.root, start, scan)) {
|
|
111
|
+
if (ctx.signal?.aborted) {
|
|
112
|
+
stopped = '中断';
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
if (++files > MAX_FILES || bytes >= MAX_SCAN_BYTES) {
|
|
116
|
+
stopped = '探索上限(5,000ファイル・8 MiB)';
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
let content;
|
|
120
|
+
try {
|
|
121
|
+
content = readSmallText(ctx.root, file);
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
scan.skipped++;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (!content) {
|
|
128
|
+
scan.skipped++;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
bytes += content.length;
|
|
132
|
+
if (bytes > MAX_SCAN_BYTES) {
|
|
133
|
+
stopped = '読取上限8 MiB';
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
const lines = content.toString('utf8').split('\n');
|
|
137
|
+
for (let i = 0; i < lines.length; i++) {
|
|
138
|
+
const match = regex.exec(lines[i]);
|
|
139
|
+
if (!match)
|
|
140
|
+
continue;
|
|
141
|
+
const hit = `${path.relative(ctx.root, file)}:${i + 1}: ${excerpt(lines[i], match.index)}`;
|
|
142
|
+
if (outputBytes + Buffer.byteLength(hit) + 1 > OUTPUT_BUDGET) {
|
|
143
|
+
stopped = '出力上限16 KiB';
|
|
144
|
+
break search;
|
|
145
|
+
}
|
|
146
|
+
hits.push(hit);
|
|
147
|
+
outputBytes += Buffer.byteLength(hit) + 1;
|
|
148
|
+
if (hits.length >= MAX_HITS) {
|
|
149
|
+
stopped = '上限50件';
|
|
150
|
+
break search;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return `検索 "${args.pattern}": ${hits.length} 件${stopped ? `で打ち切り(${stopped})` : ''}\n対象: ${scan.mode}。1 MiB超・バイナリ・読取不可など${scan.skipped}件を除外。\n${hits.join('\n')}`;
|
|
155
|
+
},
|
|
156
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
export type ShellName = 'bash' | 'zsh' | 'sh';
|
|
2
|
+
export declare const SHELLS: readonly ShellName[];
|
|
3
|
+
/** The shell that backs `sh`. Falls back through the usual install locations. */
|
|
4
|
+
export declare function resolveShell(name?: ShellName): {
|
|
5
|
+
file: string;
|
|
6
|
+
name: ShellName;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* A bounded text buffer with two views over the same stream.
|
|
10
|
+
*
|
|
11
|
+
* - `read(from)` serves a background job: it returns everything after an absolute position, so
|
|
12
|
+
* repeated polling reports only what is new. Old text is evicted, and a reader that fell
|
|
13
|
+
* behind is told how much it missed instead of silently receiving a gap.
|
|
14
|
+
* - `text()` serves a foreground result: it keeps the FIRST part and the LAST part, because a
|
|
15
|
+
* failing command's reason is at the end while the beginning says what ran.
|
|
16
|
+
*
|
|
17
|
+
* Keeping the head permanently is what makes both possible; a plain rolling buffer loses it.
|
|
18
|
+
*/
|
|
19
|
+
export declare class TextSink {
|
|
20
|
+
private readonly chunks;
|
|
21
|
+
private first;
|
|
22
|
+
private length;
|
|
23
|
+
private readonly budget;
|
|
24
|
+
private readonly headBudget;
|
|
25
|
+
private head;
|
|
26
|
+
/** Total characters ever appended, including text already evicted. */
|
|
27
|
+
produced: number;
|
|
28
|
+
constructor(budget: number);
|
|
29
|
+
push(text: string): void;
|
|
30
|
+
get retainedLength(): number;
|
|
31
|
+
/** Absolute offset of the first character still available to `read`. */
|
|
32
|
+
get retainedFrom(): number;
|
|
33
|
+
/** The foreground view: head, an elision marker, then the retained tail. */
|
|
34
|
+
text(): string;
|
|
35
|
+
/** Everything appended after absolute position `from` (background-job view). */
|
|
36
|
+
read(from: number): {
|
|
37
|
+
text: string;
|
|
38
|
+
next: number;
|
|
39
|
+
dropped: number;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export interface ShellOptions {
|
|
43
|
+
/** Defaults to POSIX sh; callers that need bash/zsh features ask for them explicitly. */
|
|
44
|
+
shell?: ShellName;
|
|
45
|
+
/** `0` or omitted means no timer: a background job runs until it exits or is stopped. */
|
|
46
|
+
timeoutMs?: number;
|
|
47
|
+
maxOutputBytes?: number;
|
|
48
|
+
env?: NodeJS.ProcessEnv;
|
|
49
|
+
/**
|
|
50
|
+
* Called INSTEAD of killing the group when `timeoutMs` elapses. The receiver gets the live
|
|
51
|
+
* process, so it never has to capture a variable the constructor has not assigned yet.
|
|
52
|
+
*
|
|
53
|
+
* This is the promotion seam: the caller keeps ownership of a still-running process instead of
|
|
54
|
+
* losing its work to a deadline. Whoever receives it must continue to own its lifetime.
|
|
55
|
+
*/
|
|
56
|
+
onTimeout?(process: ShellProcess): void;
|
|
57
|
+
}
|
|
58
|
+
export interface ShellResult {
|
|
59
|
+
shell: ShellName;
|
|
60
|
+
code: number | null;
|
|
61
|
+
signal: NodeJS.Signals | null;
|
|
62
|
+
stdout: string;
|
|
63
|
+
stderr: string;
|
|
64
|
+
truncated: boolean;
|
|
65
|
+
timedOut: boolean;
|
|
66
|
+
durationMs: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* One shell invocation and its process group.
|
|
70
|
+
*
|
|
71
|
+
* `result` settles when the shell closed AND its group is gone, so a command cannot exit while
|
|
72
|
+
* silently abandoning children. A stop is always a reject: the caller cannot tell "the command
|
|
73
|
+
* failed" from "we killed it", and pretending otherwise would hide a cancelled effect.
|
|
74
|
+
*/
|
|
75
|
+
export declare class ShellProcess {
|
|
76
|
+
readonly shell: ShellName;
|
|
77
|
+
readonly startedAt: number;
|
|
78
|
+
readonly out: TextSink;
|
|
79
|
+
readonly err: TextSink;
|
|
80
|
+
readonly result: Promise<ShellResult>;
|
|
81
|
+
private readonly child;
|
|
82
|
+
private readonly timeoutMs;
|
|
83
|
+
private settle;
|
|
84
|
+
private fail;
|
|
85
|
+
private settled;
|
|
86
|
+
private closed;
|
|
87
|
+
private code;
|
|
88
|
+
private exitSignal;
|
|
89
|
+
private stopReason;
|
|
90
|
+
private failure;
|
|
91
|
+
private escalation;
|
|
92
|
+
private cleanupDeadline;
|
|
93
|
+
private poll;
|
|
94
|
+
private timer;
|
|
95
|
+
private readonly promoteOnTimeout;
|
|
96
|
+
constructor(command: string, cwd: string, opts?: ShellOptions);
|
|
97
|
+
/** Detach from the result: the caller no longer awaits it, but the process keeps running. */
|
|
98
|
+
abandon(): void;
|
|
99
|
+
get pid(): number | undefined;
|
|
100
|
+
get isSettled(): boolean;
|
|
101
|
+
get hasSettledResult(): boolean;
|
|
102
|
+
/** True once a stop was requested, so a failure can be reported as a kill. */
|
|
103
|
+
get stoppedByKill(): boolean;
|
|
104
|
+
private groupExists;
|
|
105
|
+
private killGroup;
|
|
106
|
+
private finish;
|
|
107
|
+
/** Terminate the process group; the result rejects with the reason once the group is gone. */
|
|
108
|
+
stop(reason?: 'abort' | 'timeout' | 'descendants' | 'killed'): void;
|
|
109
|
+
}
|
|
110
|
+
/** Model-facing text: the exit code leads so the agent reads it before the body. */
|
|
111
|
+
export declare function describeShellResult(result: ShellResult): string;
|
|
112
|
+
/** Run one command to completion. See {@link ShellProcess} for the stop semantics. */
|
|
113
|
+
export declare function runShellProcess(command: string, cwd: string, signal?: AbortSignal, opts?: ShellOptions): Promise<ShellResult>;
|
|
114
|
+
/** Compatibility wrapper: callers that treat a nonzero exit as a failure keep that contract. */
|
|
115
|
+
export declare function runShell(command: string, cwd: string, signal?: AbortSignal, env?: NodeJS.ProcessEnv): Promise<string>;
|