@gaunt-sloth/core 2.0.0-alpha.4 → 2.0.0-alpha.40
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 +71 -20
- package/dist/config/colour.d.ts +38 -0
- package/dist/config/colour.js +36 -0
- package/dist/config/colour.js.map +1 -0
- package/dist/config/configDiscovery.d.ts +79 -0
- package/dist/config/configDiscovery.js +80 -0
- package/dist/config/configDiscovery.js.map +1 -0
- package/dist/config/defaults.d.ts +21 -21
- package/dist/config/defaults.js +11 -9
- package/dist/config/defaults.js.map +1 -1
- package/dist/config/filesystem-tools.d.ts +41 -0
- package/dist/config/filesystem-tools.js +56 -0
- package/dist/config/filesystem-tools.js.map +1 -0
- package/dist/config/jsonc.d.ts +12 -0
- package/dist/config/jsonc.js +41 -0
- package/dist/config/jsonc.js.map +1 -0
- package/dist/config/loader.d.ts +165 -6
- package/dist/config/loader.js +963 -109
- package/dist/config/loader.js.map +1 -1
- package/dist/config/mouse.d.ts +50 -0
- package/dist/config/mouse.js +44 -0
- package/dist/config/mouse.js.map +1 -0
- package/dist/config/profiles.d.ts +68 -0
- package/dist/config/profiles.js +93 -0
- package/dist/config/profiles.js.map +1 -0
- package/dist/config/providerKeys.d.ts +69 -0
- package/dist/config/providerKeys.js +69 -0
- package/dist/config/providerKeys.js.map +1 -0
- package/dist/config/schema.d.ts +2695 -130
- package/dist/config/schema.js +1385 -68
- package/dist/config/schema.js.map +1 -1
- package/dist/config/shell-policy.d.ts +899 -111
- package/dist/config/shell-policy.js +800 -70
- package/dist/config/shell-policy.js.map +1 -1
- package/dist/config/tool-descriptions.d.ts +211 -0
- package/dist/config/tool-descriptions.js +272 -0
- package/dist/config/tool-descriptions.js.map +1 -0
- package/dist/config/types.d.ts +372 -34
- package/dist/config/types.js +1 -0
- package/dist/config/types.js.map +1 -1
- package/dist/config.d.ts +35 -1
- package/dist/config.js +16 -1
- package/dist/config.js.map +1 -1
- package/dist/constants.d.ts +45 -0
- package/dist/constants.js +45 -0
- package/dist/constants.js.map +1 -1
- package/dist/core/GthAbstractAgent.d.ts +190 -11
- package/dist/core/GthAbstractAgent.js +551 -35
- package/dist/core/GthAbstractAgent.js.map +1 -1
- package/dist/core/GthAgentRunner.d.ts +565 -57
- package/dist/core/GthAgentRunner.js +1546 -140
- package/dist/core/GthAgentRunner.js.map +1 -1
- package/dist/core/GthLangChainAgent.d.ts +117 -2
- package/dist/core/GthLangChainAgent.js +644 -18
- package/dist/core/GthLangChainAgent.js.map +1 -1
- package/dist/core/approvals/annotations.d.ts +122 -0
- package/dist/core/approvals/annotations.js +137 -0
- package/dist/core/approvals/annotations.js.map +1 -0
- package/dist/core/approvals/grants.d.ts +216 -0
- package/dist/core/approvals/grants.js +469 -0
- package/dist/core/approvals/grants.js.map +1 -0
- package/dist/core/approvals/matcher.d.ts +202 -0
- package/dist/core/approvals/matcher.js +267 -0
- package/dist/core/approvals/matcher.js.map +1 -0
- package/dist/core/approvals/mcpSubjects.d.ts +40 -0
- package/dist/core/approvals/mcpSubjects.js +99 -0
- package/dist/core/approvals/mcpSubjects.js.map +1 -0
- package/dist/core/approvals/promptHeader.d.ts +28 -0
- package/dist/core/approvals/promptHeader.js +62 -0
- package/dist/core/approvals/promptHeader.js.map +1 -0
- package/dist/core/approvals/toolAnnotationSources.d.ts +105 -0
- package/dist/core/approvals/toolAnnotationSources.js +277 -0
- package/dist/core/approvals/toolAnnotationSources.js.map +1 -0
- package/dist/core/approvals/toolHost.d.ts +46 -0
- package/dist/core/approvals/toolHost.js +108 -0
- package/dist/core/approvals/toolHost.js.map +1 -0
- package/dist/core/debugCapture.d.ts +74 -0
- package/dist/core/debugCapture.js +100 -0
- package/dist/core/debugCapture.js.map +1 -0
- package/dist/core/gthLeanAgentFactory.d.ts +9 -0
- package/dist/core/gthLeanAgentFactory.js +10 -0
- package/dist/core/gthLeanAgentFactory.js.map +1 -0
- package/dist/core/launchBanner.d.ts +127 -0
- package/dist/core/launchBanner.js +414 -0
- package/dist/core/launchBanner.js.map +1 -0
- package/dist/core/modelLabel.d.ts +19 -0
- package/dist/core/modelLabel.js +26 -0
- package/dist/core/modelLabel.js.map +1 -0
- package/dist/core/plainToolIndication.d.ts +15 -0
- package/dist/core/plainToolIndication.js +174 -0
- package/dist/core/plainToolIndication.js.map +1 -0
- package/dist/core/reasoningBlocks.d.ts +65 -0
- package/dist/core/reasoningBlocks.js +103 -0
- package/dist/core/reasoningBlocks.js.map +1 -0
- package/dist/core/refusal.d.ts +53 -0
- package/dist/core/refusal.js +133 -0
- package/dist/core/refusal.js.map +1 -0
- package/dist/core/runHeader.d.ts +38 -0
- package/dist/core/runHeader.js +42 -0
- package/dist/core/runHeader.js.map +1 -0
- package/dist/core/runStats.d.ts +52 -0
- package/dist/core/runStats.js +118 -0
- package/dist/core/runStats.js.map +1 -0
- package/dist/core/shell/ShellCommandFailedError.d.ts +53 -0
- package/dist/core/shell/ShellCommandFailedError.js +67 -0
- package/dist/core/shell/ShellCommandFailedError.js.map +1 -0
- package/dist/core/shell/abstention.d.ts +88 -0
- package/dist/core/shell/abstention.js +184 -0
- package/dist/core/shell/abstention.js.map +1 -0
- package/dist/core/shell/approvalCapture.d.ts +271 -0
- package/dist/core/shell/approvalCapture.js +108 -0
- package/dist/core/shell/approvalCapture.js.map +1 -0
- package/dist/core/shell/approvalStop.d.ts +123 -0
- package/dist/core/shell/approvalStop.js +269 -0
- package/dist/core/shell/approvalStop.js.map +1 -0
- package/dist/core/shell/arity.d.ts +6 -0
- package/dist/core/shell/arity.js +20 -6
- package/dist/core/shell/arity.js.map +1 -1
- package/dist/core/shell/denylist.d.ts +11 -0
- package/dist/core/shell/denylist.js +37 -0
- package/dist/core/shell/denylist.js.map +1 -0
- package/dist/core/shell/escalationSeverity.d.ts +141 -0
- package/dist/core/shell/escalationSeverity.js +89 -0
- package/dist/core/shell/escalationSeverity.js.map +1 -0
- package/dist/core/shell/framing.d.ts +190 -0
- package/dist/core/shell/framing.js +633 -0
- package/dist/core/shell/framing.js.map +1 -0
- package/dist/core/shell/hardline.d.ts +103 -0
- package/dist/core/shell/hardline.js +780 -0
- package/dist/core/shell/hardline.js.map +1 -0
- package/dist/core/shell/negotiation.d.ts +328 -0
- package/dist/core/shell/negotiation.js +488 -0
- package/dist/core/shell/negotiation.js.map +1 -0
- package/dist/core/shell/normalize.d.ts +44 -4
- package/dist/core/shell/normalize.js +61 -7
- package/dist/core/shell/normalize.js.map +1 -1
- package/dist/core/shell/openWorld.d.ts +263 -0
- package/dist/core/shell/openWorld.js +1188 -0
- package/dist/core/shell/openWorld.js.map +1 -0
- package/dist/core/shell/rater.d.ts +873 -0
- package/dist/core/shell/rater.js +1454 -0
- package/dist/core/shell/rater.js.map +1 -0
- package/dist/core/shell/raterModel.d.ts +41 -0
- package/dist/core/shell/raterModel.js +51 -0
- package/dist/core/shell/raterModel.js.map +1 -0
- package/dist/core/shell/raterVocabulary.d.ts +121 -0
- package/dist/core/shell/raterVocabulary.js +116 -0
- package/dist/core/shell/raterVocabulary.js.map +1 -0
- package/dist/core/shell/rejection.d.ts +69 -0
- package/dist/core/shell/rejection.js +38 -0
- package/dist/core/shell/rejection.js.map +1 -0
- package/dist/core/toolCallRepair/grammar.d.ts +41 -0
- package/dist/core/toolCallRepair/grammar.js +116 -0
- package/dist/core/toolCallRepair/grammar.js.map +1 -0
- package/dist/core/toolCallRepair/index.d.ts +2 -0
- package/dist/core/toolCallRepair/index.js +7 -0
- package/dist/core/toolCallRepair/index.js.map +1 -0
- package/dist/core/toolCallRepair/payload.d.ts +36 -0
- package/dist/core/toolCallRepair/payload.js +341 -0
- package/dist/core/toolCallRepair/payload.js.map +1 -0
- package/dist/core/toolCallRepair/promote.d.ts +45 -0
- package/dist/core/toolCallRepair/promote.js +90 -0
- package/dist/core/toolCallRepair/promote.js.map +1 -0
- package/dist/core/toolDisplay.d.ts +123 -0
- package/dist/core/toolDisplay.js +451 -0
- package/dist/core/toolDisplay.js.map +1 -0
- package/dist/core/toolOutputChannel.d.ts +95 -0
- package/dist/core/toolOutputChannel.js +165 -0
- package/dist/core/toolOutputChannel.js.map +1 -0
- package/dist/core/types.d.ts +378 -16
- package/dist/core/types.js.map +1 -1
- package/dist/history/historyFormat.d.ts +28 -0
- package/dist/history/historyFormat.js +127 -0
- package/dist/history/historyFormat.js.map +1 -0
- package/dist/history/historyStore.d.ts +198 -0
- package/dist/history/historyStore.js +482 -0
- package/dist/history/historyStore.js.map +1 -0
- package/dist/history/recordSession.d.ts +37 -0
- package/dist/history/recordSession.js +56 -0
- package/dist/history/recordSession.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/providers/anthropic.d.ts +1 -1
- package/dist/providers/anthropic.js +17 -10
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/configurationPassthrough.d.ts +107 -0
- package/dist/providers/configurationPassthrough.js +148 -0
- package/dist/providers/configurationPassthrough.js.map +1 -0
- package/dist/providers/deepseek.d.ts +1 -1
- package/dist/providers/deepseek.js +5 -10
- package/dist/providers/deepseek.js.map +1 -1
- package/dist/providers/geminiSchemaSanitizer.d.ts +55 -0
- package/dist/providers/geminiSchemaSanitizer.js +347 -0
- package/dist/providers/geminiSchemaSanitizer.js.map +1 -0
- package/dist/providers/geminiThinking.d.ts +60 -0
- package/dist/providers/geminiThinking.js +92 -0
- package/dist/providers/geminiThinking.js.map +1 -0
- package/dist/providers/google-genai.d.ts +1 -1
- package/dist/providers/google-genai.js +23 -11
- package/dist/providers/google-genai.js.map +1 -1
- package/dist/providers/groq.d.ts +1 -1
- package/dist/providers/groq.js +17 -10
- package/dist/providers/groq.js.map +1 -1
- package/dist/providers/huggingface.d.ts +25 -0
- package/dist/providers/huggingface.js +69 -0
- package/dist/providers/huggingface.js.map +1 -0
- package/dist/providers/modelCatalog.d.ts +109 -0
- package/dist/providers/modelCatalog.js +245 -0
- package/dist/providers/modelCatalog.js.map +1 -0
- package/dist/providers/modelDiscovery.d.ts +153 -6
- package/dist/providers/modelDiscovery.js +295 -41
- package/dist/providers/modelDiscovery.js.map +1 -1
- package/dist/providers/ollama.d.ts +19 -5
- package/dist/providers/ollama.js +72 -52
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.d.ts +1 -1
- package/dist/providers/openai.js +39 -10
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/openrouter.d.ts +27 -5
- package/dist/providers/openrouter.js +88 -36
- package/dist/providers/openrouter.js.map +1 -1
- package/dist/providers/vertexai.d.ts +1 -1
- package/dist/providers/vertexai.js +24 -11
- package/dist/providers/vertexai.js.map +1 -1
- package/dist/providers/xai.d.ts +1 -1
- package/dist/providers/xai.js +25 -10
- package/dist/providers/xai.js.map +1 -1
- package/dist/runtime/askStructured.d.ts +105 -0
- package/dist/runtime/askStructured.js +120 -0
- package/dist/runtime/askStructured.js.map +1 -0
- package/dist/runtime/conversation.d.ts +64 -0
- package/dist/runtime/conversation.js +171 -0
- package/dist/runtime/conversation.js.map +1 -0
- package/dist/runtime/singleShot.d.ts +39 -5
- package/dist/runtime/singleShot.js +115 -36
- package/dist/runtime/singleShot.js.map +1 -1
- package/dist/runtime/structuredOutput.d.ts +104 -0
- package/dist/runtime/structuredOutput.js +393 -0
- package/dist/runtime/structuredOutput.js.map +1 -0
- package/dist/utils/ProgressIndicator.d.ts +21 -0
- package/dist/utils/ProgressIndicator.js +30 -3
- package/dist/utils/ProgressIndicator.js.map +1 -1
- package/dist/utils/aiignoreUtils.js.map +1 -1
- package/dist/utils/binaryOutputUtils.js.map +1 -1
- package/dist/utils/consoleUtils.d.ts +95 -0
- package/dist/utils/consoleUtils.js +112 -2
- package/dist/utils/consoleUtils.js.map +1 -1
- package/dist/utils/crashHandler.d.ts +87 -0
- package/dist/utils/crashHandler.js +128 -0
- package/dist/utils/crashHandler.js.map +1 -0
- package/dist/utils/debugDump.d.ts +134 -0
- package/dist/utils/debugDump.js +381 -0
- package/dist/utils/debugDump.js.map +1 -0
- package/dist/utils/debugUtils.d.ts +13 -4
- package/dist/utils/debugUtils.js +36 -13
- package/dist/utils/debugUtils.js.map +1 -1
- package/dist/utils/displayWidth.d.ts +53 -0
- package/dist/utils/displayWidth.js +195 -0
- package/dist/utils/displayWidth.js.map +1 -0
- package/dist/utils/fileUtils.d.ts +20 -1
- package/dist/utils/fileUtils.js +35 -2
- package/dist/utils/fileUtils.js.map +1 -1
- package/dist/utils/llmUtils.d.ts +39 -8
- package/dist/utils/llmUtils.js +76 -8
- package/dist/utils/llmUtils.js.map +1 -1
- package/dist/utils/redactSecrets.d.ts +63 -0
- package/dist/utils/redactSecrets.js +286 -0
- package/dist/utils/redactSecrets.js.map +1 -0
- package/dist/utils/systemPromptNotes.d.ts +222 -0
- package/dist/utils/systemPromptNotes.js +338 -0
- package/dist/utils/systemPromptNotes.js.map +1 -0
- package/dist/utils/systemUtils.d.ts +18 -1
- package/dist/utils/systemUtils.js +38 -3
- package/dist/utils/systemUtils.js.map +1 -1
- package/dist/utils/toolMatching.d.ts +30 -0
- package/dist/utils/toolMatching.js +44 -0
- package/dist/utils/toolMatching.js.map +1 -0
- package/dist/utils/untrustedText.d.ts +86 -0
- package/dist/utils/untrustedText.js +101 -0
- package/dist/utils/untrustedText.js.map +1 -0
- package/package.json +21 -6
- package/schema/gsloth-config.schema.json +1979 -353
- package/dist/core/shell/allowlist.d.ts +0 -75
- package/dist/core/shell/allowlist.js +0 -187
- package/dist/core/shell/allowlist.js.map +0 -1
- package/dist/core/shell/judge.d.ts +0 -161
- package/dist/core/shell/judge.js +0 -261
- package/dist/core/shell/judge.js.map +0 -1
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* GS2-16 — pure, fail-soft helpers that harvest per-run analytics (token usage + invoked tool
|
|
4
|
+
* names) from LangChain messages, so the opt-in history recorder can populate `gth insights`
|
|
5
|
+
* with real numbers instead of zeros.
|
|
6
|
+
*
|
|
7
|
+
* The extraction is deliberately structural and defensive (duck-typed reads guarded by a
|
|
8
|
+
* try/catch) rather than `instanceof`-based: the same accumulator serves both the non-streaming
|
|
9
|
+
* `invoke` path (a full `messages[]` from graph state) and the streaming paths (individual
|
|
10
|
+
* message chunks / `ToolMessage`s as they arrive), across providers whose message shapes vary.
|
|
11
|
+
* Nothing here may throw into a run — a missing/odd field just means that datum is skipped.
|
|
12
|
+
*/
|
|
13
|
+
import type { GthRunStats, GthToolResult } from '#src/core/types.js';
|
|
14
|
+
/**
|
|
15
|
+
* BATCH-21 — cap on a captured tool-result `content` (characters). Keeps a giant payload (a whole
|
|
16
|
+
* file read, a long shell log) from bloating run stats; anything longer is truncated to this
|
|
17
|
+
* length. Sized so realistic structured payloads (the `gth eval` tool-result-assertion use case)
|
|
18
|
+
* survive intact.
|
|
19
|
+
*/
|
|
20
|
+
export declare const TOOL_RESULT_CONTENT_CAP = 8192;
|
|
21
|
+
/** Mutable tally behind {@link finalizeRunStats}; see {@link createRunStatsAccumulator}. */
|
|
22
|
+
export interface RunStatsAccumulator {
|
|
23
|
+
/** Running sum of input/prompt tokens. */
|
|
24
|
+
input: number;
|
|
25
|
+
/** Running sum of output/completion tokens. */
|
|
26
|
+
output: number;
|
|
27
|
+
/** Whether ANY message reported `usage_metadata` — gates whether tokens are recorded at all. */
|
|
28
|
+
sawUsage: boolean;
|
|
29
|
+
/** Deduplicated set of invoked tool names. */
|
|
30
|
+
tools: Set<string>;
|
|
31
|
+
/** BATCH-21 — one record per executed tool result (`ToolMessage`), in arrival order, un-deduped. */
|
|
32
|
+
toolResults: GthToolResult[];
|
|
33
|
+
}
|
|
34
|
+
/** A fresh, empty accumulator. */
|
|
35
|
+
export declare function createRunStatsAccumulator(): RunStatsAccumulator;
|
|
36
|
+
/**
|
|
37
|
+
* Fold one LangChain message (or message chunk) into the accumulator. Fail-soft: any unexpected
|
|
38
|
+
* shape is swallowed so a run is never affected. Harvests, when present:
|
|
39
|
+
* - `usage_metadata.input_tokens` / `.output_tokens` (summed; marks `sawUsage`),
|
|
40
|
+
* - tool names from an AIMessage's requested `tool_calls[].name` AND from a `ToolMessage`'s own
|
|
41
|
+
* `.name` (the executed tool), so both "requested" and "executed" tools are captured, and
|
|
42
|
+
* - (BATCH-21) a per-`ToolMessage` result record — `name` + `isError` (from `.status`) + capped
|
|
43
|
+
* `content` — into `acc.toolResults`, so tool-RESULT assertions can grade what a tool returned.
|
|
44
|
+
*/
|
|
45
|
+
export declare function accumulateMessage(acc: RunStatsAccumulator, message: unknown): void;
|
|
46
|
+
/** Freeze the accumulator into the public {@link GthRunStats}. Tokens omitted unless observed. */
|
|
47
|
+
export declare function finalizeRunStats(acc: RunStatsAccumulator): GthRunStats;
|
|
48
|
+
/**
|
|
49
|
+
* One-shot convenience for the non-streaming path: fold a full `messages[]` (e.g. the final graph
|
|
50
|
+
* state) into a fresh accumulator and finalize. Fail-soft (a non-iterable input yields empties).
|
|
51
|
+
*/
|
|
52
|
+
export declare function extractRunStats(messages: unknown): GthRunStats;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BATCH-21 — cap on a captured tool-result `content` (characters). Keeps a giant payload (a whole
|
|
3
|
+
* file read, a long shell log) from bloating run stats; anything longer is truncated to this
|
|
4
|
+
* length. Sized so realistic structured payloads (the `gth eval` tool-result-assertion use case)
|
|
5
|
+
* survive intact.
|
|
6
|
+
*/
|
|
7
|
+
export const TOOL_RESULT_CONTENT_CAP = 8192;
|
|
8
|
+
/** A fresh, empty accumulator. */
|
|
9
|
+
export function createRunStatsAccumulator() {
|
|
10
|
+
return { input: 0, output: 0, sawUsage: false, tools: new Set(), toolResults: [] };
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* BATCH-21 — derive a tool result's text payload from a `ToolMessage.content`, fail-soft. A string
|
|
14
|
+
* passes through; anything else non-`undefined` is JSON-stringified (the same derivation the
|
|
15
|
+
* `tool_result` stream event uses in `GthAbstractAgent`); the result is capped at
|
|
16
|
+
* {@link TOOL_RESULT_CONTENT_CAP}. Returns `undefined` (payload omitted) when nothing textual can
|
|
17
|
+
* be derived — never throws.
|
|
18
|
+
*/
|
|
19
|
+
function toolResultContentText(content) {
|
|
20
|
+
try {
|
|
21
|
+
let text;
|
|
22
|
+
if (typeof content === 'string') {
|
|
23
|
+
text = content;
|
|
24
|
+
}
|
|
25
|
+
else if (content !== undefined) {
|
|
26
|
+
text = JSON.stringify(content);
|
|
27
|
+
}
|
|
28
|
+
if (text === undefined)
|
|
29
|
+
return undefined;
|
|
30
|
+
return text.length > TOOL_RESULT_CONTENT_CAP ? text.slice(0, TOOL_RESULT_CONTENT_CAP) : text;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
/* fail-soft: an unstringifiable payload just means no content is recorded */
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Fold one LangChain message (or message chunk) into the accumulator. Fail-soft: any unexpected
|
|
39
|
+
* shape is swallowed so a run is never affected. Harvests, when present:
|
|
40
|
+
* - `usage_metadata.input_tokens` / `.output_tokens` (summed; marks `sawUsage`),
|
|
41
|
+
* - tool names from an AIMessage's requested `tool_calls[].name` AND from a `ToolMessage`'s own
|
|
42
|
+
* `.name` (the executed tool), so both "requested" and "executed" tools are captured, and
|
|
43
|
+
* - (BATCH-21) a per-`ToolMessage` result record — `name` + `isError` (from `.status`) + capped
|
|
44
|
+
* `content` — into `acc.toolResults`, so tool-RESULT assertions can grade what a tool returned.
|
|
45
|
+
*/
|
|
46
|
+
export function accumulateMessage(acc, message) {
|
|
47
|
+
try {
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
+
const m = message;
|
|
50
|
+
if (!m || typeof m !== 'object')
|
|
51
|
+
return;
|
|
52
|
+
const usage = m.usage_metadata;
|
|
53
|
+
if (usage && typeof usage === 'object') {
|
|
54
|
+
acc.sawUsage = true;
|
|
55
|
+
if (typeof usage.input_tokens === 'number' && Number.isFinite(usage.input_tokens)) {
|
|
56
|
+
acc.input += usage.input_tokens;
|
|
57
|
+
}
|
|
58
|
+
if (typeof usage.output_tokens === 'number' && Number.isFinite(usage.output_tokens)) {
|
|
59
|
+
acc.output += usage.output_tokens;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Requested tool calls (AIMessage / AIMessageChunk). Continuation chunks in a streamed
|
|
63
|
+
// tool call carry an empty name, so guard on a non-empty string; the Set dedupes repeats.
|
|
64
|
+
const toolCalls = m.tool_calls;
|
|
65
|
+
if (Array.isArray(toolCalls)) {
|
|
66
|
+
for (const tc of toolCalls) {
|
|
67
|
+
const name = tc?.name;
|
|
68
|
+
if (typeof name === 'string' && name.length > 0)
|
|
69
|
+
acc.tools.add(name);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Executed tool result (ToolMessage). Its `.name` is the tool that produced the result.
|
|
73
|
+
const type = typeof m.getType === 'function' ? m.getType() : m._getType?.();
|
|
74
|
+
if (type === 'tool' && typeof m.name === 'string' && m.name.length > 0) {
|
|
75
|
+
acc.tools.add(m.name);
|
|
76
|
+
// BATCH-21 — capture the RESULT record too (same capture site, same fail-soft discipline):
|
|
77
|
+
// `.status === 'error'` is LangChain's real tool-error signal, `.content` the returned
|
|
78
|
+
// payload (capped; omitted when no text can be derived). One record per ToolMessage, in
|
|
79
|
+
// arrival order — deliberately NOT deduplicated, unlike the name set above.
|
|
80
|
+
const content = toolResultContentText(m.content);
|
|
81
|
+
acc.toolResults.push({
|
|
82
|
+
name: m.name,
|
|
83
|
+
isError: m.status === 'error',
|
|
84
|
+
...(content !== undefined ? { content } : {}),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
/* fail-soft: never let stats capture affect a run */
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Freeze the accumulator into the public {@link GthRunStats}. Tokens omitted unless observed. */
|
|
93
|
+
export function finalizeRunStats(acc) {
|
|
94
|
+
return {
|
|
95
|
+
tokensInput: acc.sawUsage ? acc.input : undefined,
|
|
96
|
+
tokensOutput: acc.sawUsage ? acc.output : undefined,
|
|
97
|
+
tools: [...acc.tools],
|
|
98
|
+
toolResults: [...acc.toolResults],
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* One-shot convenience for the non-streaming path: fold a full `messages[]` (e.g. the final graph
|
|
103
|
+
* state) into a fresh accumulator and finalize. Fail-soft (a non-iterable input yields empties).
|
|
104
|
+
*/
|
|
105
|
+
export function extractRunStats(messages) {
|
|
106
|
+
const acc = createRunStatsAccumulator();
|
|
107
|
+
try {
|
|
108
|
+
if (Array.isArray(messages)) {
|
|
109
|
+
for (const m of messages)
|
|
110
|
+
accumulateMessage(acc, m);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
/* fail-soft */
|
|
115
|
+
}
|
|
116
|
+
return finalizeRunStats(acc);
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=runStats.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runStats.js","sourceRoot":"","sources":["../../src/core/runStats.ts"],"names":[],"mappings":"AAcA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,CAAC;AAgB5C,kCAAkC;AAClC,MAAM,UAAU,yBAAyB;IACvC,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,GAAG,EAAU,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC;AAC7F,CAAC;AAED;;;;;;GAMG;AACH,SAAS,qBAAqB,CAAC,OAAgB;IAC7C,IAAI,CAAC;QACH,IAAI,IAAwB,CAAC;QAC7B,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YAChC,IAAI,GAAG,OAAO,CAAC;QACjB,CAAC;aAAM,IAA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,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;QACjD,YAAY,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS;QACnD,KAAK,EAAE,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC;QACrB,WAAW,EAAE,CAAC,GAAG,GAAG,CAAC,WAAW,CAAC;KAClC,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,QAAiB;IAC/C,MAAM,GAAG,GAAG,yBAAyB,EAAE,CAAC;IACxC,IAAI,CAAC;QACH,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC5B,KAAK,MAAM,CAAC,IAAI,QAAQ;gBAAE,iBAAiB,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QACtD,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,eAAe;IACjB,CAAC;IACD,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module core/shell/ShellCommandFailedError
|
|
3
|
+
*
|
|
4
|
+
* The typed error a `run_*` shell/dev tool throws when a command did NOT exit cleanly
|
|
5
|
+
* (non-zero exit code, or killed for exceeding the timeout). It carries the FULL model-facing
|
|
6
|
+
* body so a softening middleware can hand the model the exact observation it saw before — the
|
|
7
|
+
* only change being that the tool result's status flips to `'error'` (→ `isError` → the ✗ glyph).
|
|
8
|
+
*
|
|
9
|
+
* Canonical home is **core** so any agent can recognise a shell failure without violating the
|
|
10
|
+
* `agent → core` dependency direction — today the lean agent (`exec` / `ask --write`, core
|
|
11
|
+
* `GthLangChainAgent` `GthLeanShellExitSoftening`).
|
|
12
|
+
*
|
|
13
|
+
* The throw site (`GthDevToolkit.executeCommand`) lives in the `agent` package and re-exports this
|
|
14
|
+
* class, so its `throw new ShellCommandFailedError(...)` is one and the same type both agents catch.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* A `run_*` command that did NOT exit cleanly (non-zero exit code, or was killed for exceeding the
|
|
18
|
+
* timeout). Carries the FULL model-facing body text ({@link output}) so a softening middleware can
|
|
19
|
+
* hand the model the exact same observation it saw before — the only change is the tool result's
|
|
20
|
+
* status flips to `'error'`, which drives the ✗ (`isError`) glyph.
|
|
21
|
+
*
|
|
22
|
+
* `executeCommand` previously `resolve()`d on a non-zero exit, so the LangChain `ToolMessage` stayed
|
|
23
|
+
* `status: 'success'` and every failure rendered a ✓. Throwing this typed error instead lets each
|
|
24
|
+
* agent's softening middleware convert it into an error `ToolMessage`. A clean exit (`code === 0`)
|
|
25
|
+
* still `resolve()`s; a spawn-level `child.on('error')` still rejects with a plain `Error`.
|
|
26
|
+
*/
|
|
27
|
+
export declare class ShellCommandFailedError extends Error {
|
|
28
|
+
/** The full model-facing body (command echo + `<COMMAND_OUTPUT>` + the failure/timeout tail). */
|
|
29
|
+
readonly output: string;
|
|
30
|
+
/** The process exit code; `null` when the command was killed (timeout) and never exited cleanly. */
|
|
31
|
+
readonly exitCode: number | null;
|
|
32
|
+
/** The exact command string that was executed. */
|
|
33
|
+
readonly command: string;
|
|
34
|
+
/** The run_* tool name that invoked the command (e.g. `run_tests`, `run_shell_command`). */
|
|
35
|
+
readonly toolName: string;
|
|
36
|
+
constructor(params: {
|
|
37
|
+
output: string;
|
|
38
|
+
exitCode: number | null;
|
|
39
|
+
command: string;
|
|
40
|
+
toolName: string;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Recognise a {@link ShellCommandFailedError} for the softening middleware in either agent.
|
|
45
|
+
*
|
|
46
|
+
* Prefers a plain `instanceof` (both agents share this one core module, so the class identity is
|
|
47
|
+
* the same), but falls back to a STRUCTURAL check keyed on `name === 'ShellCommandFailedError'`
|
|
48
|
+
* plus the carried fields. The structural arm is deliberate defence against a dual-package /
|
|
49
|
+
* realm-boundary hazard: if the error ever crossed a module boundary that broke `instanceof`, we
|
|
50
|
+
* would otherwise silently rethrow a real shell failure and regress the ✗ signal. Every field the
|
|
51
|
+
* softener reads (`output`) is asserted so a narrowed value is safe to use.
|
|
52
|
+
*/
|
|
53
|
+
export declare function isShellCommandFailedError(e: unknown): e is ShellCommandFailedError;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module core/shell/ShellCommandFailedError
|
|
3
|
+
*
|
|
4
|
+
* The typed error a `run_*` shell/dev tool throws when a command did NOT exit cleanly
|
|
5
|
+
* (non-zero exit code, or killed for exceeding the timeout). It carries the FULL model-facing
|
|
6
|
+
* body so a softening middleware can hand the model the exact observation it saw before — the
|
|
7
|
+
* only change being that the tool result's status flips to `'error'` (→ `isError` → the ✗ glyph).
|
|
8
|
+
*
|
|
9
|
+
* Canonical home is **core** so any agent can recognise a shell failure without violating the
|
|
10
|
+
* `agent → core` dependency direction — today the lean agent (`exec` / `ask --write`, core
|
|
11
|
+
* `GthLangChainAgent` `GthLeanShellExitSoftening`).
|
|
12
|
+
*
|
|
13
|
+
* The throw site (`GthDevToolkit.executeCommand`) lives in the `agent` package and re-exports this
|
|
14
|
+
* class, so its `throw new ShellCommandFailedError(...)` is one and the same type both agents catch.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* A `run_*` command that did NOT exit cleanly (non-zero exit code, or was killed for exceeding the
|
|
18
|
+
* timeout). Carries the FULL model-facing body text ({@link output}) so a softening middleware can
|
|
19
|
+
* hand the model the exact same observation it saw before — the only change is the tool result's
|
|
20
|
+
* status flips to `'error'`, which drives the ✗ (`isError`) glyph.
|
|
21
|
+
*
|
|
22
|
+
* `executeCommand` previously `resolve()`d on a non-zero exit, so the LangChain `ToolMessage` stayed
|
|
23
|
+
* `status: 'success'` and every failure rendered a ✓. Throwing this typed error instead lets each
|
|
24
|
+
* agent's softening middleware convert it into an error `ToolMessage`. A clean exit (`code === 0`)
|
|
25
|
+
* still `resolve()`s; a spawn-level `child.on('error')` still rejects with a plain `Error`.
|
|
26
|
+
*/
|
|
27
|
+
export class ShellCommandFailedError extends Error {
|
|
28
|
+
/** The full model-facing body (command echo + `<COMMAND_OUTPUT>` + the failure/timeout tail). */
|
|
29
|
+
output;
|
|
30
|
+
/** The process exit code; `null` when the command was killed (timeout) and never exited cleanly. */
|
|
31
|
+
exitCode;
|
|
32
|
+
/** The exact command string that was executed. */
|
|
33
|
+
command;
|
|
34
|
+
/** The run_* tool name that invoked the command (e.g. `run_tests`, `run_shell_command`). */
|
|
35
|
+
toolName;
|
|
36
|
+
constructor(params) {
|
|
37
|
+
// Use the full body as the Error message so any generic logger/handler still surfaces the
|
|
38
|
+
// real command output rather than an opaque wrapper string.
|
|
39
|
+
super(params.output);
|
|
40
|
+
this.name = 'ShellCommandFailedError';
|
|
41
|
+
this.output = params.output;
|
|
42
|
+
this.exitCode = params.exitCode;
|
|
43
|
+
this.command = params.command;
|
|
44
|
+
this.toolName = params.toolName;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Recognise a {@link ShellCommandFailedError} for the softening middleware in either agent.
|
|
49
|
+
*
|
|
50
|
+
* Prefers a plain `instanceof` (both agents share this one core module, so the class identity is
|
|
51
|
+
* the same), but falls back to a STRUCTURAL check keyed on `name === 'ShellCommandFailedError'`
|
|
52
|
+
* plus the carried fields. The structural arm is deliberate defence against a dual-package /
|
|
53
|
+
* realm-boundary hazard: if the error ever crossed a module boundary that broke `instanceof`, we
|
|
54
|
+
* would otherwise silently rethrow a real shell failure and regress the ✗ signal. Every field the
|
|
55
|
+
* softener reads (`output`) is asserted so a narrowed value is safe to use.
|
|
56
|
+
*/
|
|
57
|
+
export function isShellCommandFailedError(e) {
|
|
58
|
+
if (e instanceof ShellCommandFailedError)
|
|
59
|
+
return true;
|
|
60
|
+
return (typeof e === 'object' &&
|
|
61
|
+
e !== null &&
|
|
62
|
+
e.name === 'ShellCommandFailedError' &&
|
|
63
|
+
typeof e.output === 'string' &&
|
|
64
|
+
typeof e.command === 'string' &&
|
|
65
|
+
typeof e.toolName === 'string');
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=ShellCommandFailedError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShellCommandFailedError.js","sourceRoot":"","sources":["../../../src/core/shell/ShellCommandFailedError.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;GAUG;AACH,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAChD,iGAAiG;IACxF,MAAM,CAAS;IACxB,oGAAoG;IAC3F,QAAQ,CAAgB;IACjC,kDAAkD;IACzC,OAAO,CAAS;IACzB,4FAA4F;IACnF,QAAQ,CAAS;IAE1B,YAAY,MAKX;QACC,0FAA0F;QAC1F,4DAA4D;QAC5D,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;QACtC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;QAChC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC9B,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;IAClC,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,yBAAyB,CAAC,CAAU;IAClD,IAAI,CAAC,YAAY,uBAAuB;QAAE,OAAO,IAAI,CAAC;IACtD,OAAO,CACL,OAAO,CAAC,KAAK,QAAQ;QACrB,CAAC,KAAK,IAAI;QACT,CAAwB,CAAC,IAAI,KAAK,yBAAyB;QAC5D,OAAQ,CAA0B,CAAC,MAAM,KAAK,QAAQ;QACtD,OAAQ,CAA2B,CAAC,OAAO,KAAK,QAAQ;QACxD,OAAQ,CAA4B,CAAC,QAAQ,KAAK,QAAQ,CAC3D,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape in the command that the gate's parser could not resolve.
|
|
3
|
+
*
|
|
4
|
+
* These are FORMS, not judgements — nothing here says a command is dangerous, and nothing here is a
|
|
5
|
+
* rating. They exist to select the sentence the rater is shown.
|
|
6
|
+
*/
|
|
7
|
+
export type AbstentionMechanism =
|
|
8
|
+
/** A git commit whose message contains a substitution the SHELL runs before git sees it. */
|
|
9
|
+
'commit-message-substitution'
|
|
10
|
+
/** More than one command: `;`, `&`, `&&`, `|`, `||`, or a line break. */
|
|
11
|
+
| 'composition'
|
|
12
|
+
/** Command / variable / process substitution: `$(…)`, backticks, `${…}`, `<(…)`. */
|
|
13
|
+
| 'substitution'
|
|
14
|
+
/** A stream redirect: `>`, `>>`, `<`, `2>`, `&>`. */
|
|
15
|
+
| 'redirect'
|
|
16
|
+
/** None of the above — an unbalanced quote, an empty command, or no resolvable program name. */
|
|
17
|
+
| 'unparseable';
|
|
18
|
+
/** What the parser saw in a command it could not resolve: the shapes, and the note for each. */
|
|
19
|
+
export interface AbstentionDefect {
|
|
20
|
+
/** The most specific mechanism found — what the note leads with. */
|
|
21
|
+
mechanism: AbstentionMechanism;
|
|
22
|
+
/** Every mechanism found, in note order. A command may carry more than one. */
|
|
23
|
+
mechanisms: AbstentionMechanism[];
|
|
24
|
+
/** One note per mechanism, in the same order. Never empty. */
|
|
25
|
+
notes: string[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* **The per-family note text — one distinct string per {@link AbstentionMechanism}.**
|
|
29
|
+
*
|
|
30
|
+
* Typed as a total `Record`, so adding a mechanism is a COMPILE ERROR until someone writes its
|
|
31
|
+
* sentence. A family that silently fell back to another family's note would be the failure this
|
|
32
|
+
* table exists to prevent: [[QA-17]] measured that the note's effect is family-specific, and
|
|
33
|
+
* `docs/test-sessions/qa-17-substitution-note-2026-08-03/` records a narrowing by family name that
|
|
34
|
+
* was FALSIFIED — `gh pr comment 42 --body "…"` carrying an executing substitution classifies as
|
|
35
|
+
* generic `substitution`, and three of three hosted raters called it safe unassisted.
|
|
36
|
+
*
|
|
37
|
+
* Every entry states a fact about what THE SHELL does, and then asks a question. Neither is a
|
|
38
|
+
* verdict about the command in front of the rater, which is what keeps the note neutral while still
|
|
39
|
+
* carrying the mechanism that the measurement showed is load-bearing.
|
|
40
|
+
*/
|
|
41
|
+
export declare const MECHANISM_NOTES: Readonly<Record<AbstentionMechanism, string>>;
|
|
42
|
+
/**
|
|
43
|
+
* Classify a command the gate's parser could not resolve, or return `null` when it resolves.
|
|
44
|
+
*
|
|
45
|
+
* **The `null` arm is the guard, and it is structural on purpose.** Every mechanism below is a
|
|
46
|
+
* regex, and `unparseable` is the fallback — so without this gate the function would classify `ls
|
|
47
|
+
* -la` as `unparseable` and a note would be attached to every command in the session. The predicate
|
|
48
|
+
* is {@link classifyCommand}'s own, not a second reading of it, so the note appears on exactly the
|
|
49
|
+
* set of commands the gate could not resolve and on no others.
|
|
50
|
+
*
|
|
51
|
+
* Detection runs on the NORMALIZED command (the same form `classifyCommand` refused), so obfuscation
|
|
52
|
+
* that the normalizer collapses cannot hide a mechanism from the note — while the command shown to
|
|
53
|
+
* the rater stays the one the rater is being asked about, fenced by its own caller.
|
|
54
|
+
*
|
|
55
|
+
* The line-break check reads the RAW command, exactly as `classifyCommand` does: the normalizer is
|
|
56
|
+
* not required to preserve that separator, and EXT-55 is the node about what happens when a caller
|
|
57
|
+
* assumes it did.
|
|
58
|
+
*
|
|
59
|
+
* **Order is note order, most specific first**, and `commit-message-substitution` SUPPRESSES the
|
|
60
|
+
* generic substitution note it is a special case of — telling the rater the same thing twice about
|
|
61
|
+
* one `$(…)` spends attention on a repetition.
|
|
62
|
+
*
|
|
63
|
+
* @param command The raw command string as the model proposed it.
|
|
64
|
+
* @returns What the parser saw, or `null` when the command's target statically resolves.
|
|
65
|
+
*/
|
|
66
|
+
export declare function describeAbstention(command: string): AbstentionDefect | null;
|
|
67
|
+
/**
|
|
68
|
+
* The opening line of the preflight note: **what this is, and what it is not.**
|
|
69
|
+
*
|
|
70
|
+
* It says the finding is about OUR PARSER rather than about the command, and that nothing has been
|
|
71
|
+
* judged. Both halves are load-bearing. The first stops the rater reading the note as a report of
|
|
72
|
+
* something found — the failure mode a bare observation produced, where a rater treats our
|
|
73
|
+
* uncertainty as evidence and either inflates or explains it away. The second is what keeps this
|
|
74
|
+
* note out of the open-world note's register: that one may say the command *"has ALREADY been
|
|
75
|
+
* floored deterministically and will be shown to the user whatever you return"*, and here that
|
|
76
|
+
* sentence would simply be false.
|
|
77
|
+
*/
|
|
78
|
+
export declare const PARSER_NOTE_PREAMBLE: string;
|
|
79
|
+
/**
|
|
80
|
+
* Build the neutral preflight note for a command, or `null` when the gate resolved it.
|
|
81
|
+
*
|
|
82
|
+
* One note per mechanism the command carries, most specific first, bulleted when there is more than
|
|
83
|
+
* one — a command can compose AND substitute AND redirect, and a note that mentioned only the first
|
|
84
|
+
* would leave the rater reasoning about a fragment of the shape.
|
|
85
|
+
*
|
|
86
|
+
* @param command The raw command string as the model proposed it.
|
|
87
|
+
*/
|
|
88
|
+
export declare function buildParserPreflightNote(command: string): string | null;
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module core/shell/abstention
|
|
3
|
+
*
|
|
4
|
+
* EXT-81 — **what the RATER is told about a command our parser could not resolve.**
|
|
5
|
+
*
|
|
6
|
+
* `classifyCommand` resolves a command's target, or returns `null` when it cannot. That `null` used
|
|
7
|
+
* to be an ACTION: the gate refused the call before any rating, on the theory that a reading of a
|
|
8
|
+
* command it could not parse was not worth buying. §6.1's rule reverses that — *deterministic checks
|
|
9
|
+
* fire only where we are confident something is a threat; where we cannot tell, the model decides* —
|
|
10
|
+
* and a parser reporting that it could not resolve a string is not a detection of anything. So the
|
|
11
|
+
* finding is now **neutral context handed to the rater**, and the flow is one path: preflight → note
|
|
12
|
+
* → rate → approve / escalate / halt.
|
|
13
|
+
*
|
|
14
|
+
* This module turns the `null` into that note: **which shape the parser saw, and the mechanism that
|
|
15
|
+
* makes the shape worth a second look.** Two properties are the whole design, and each has a
|
|
16
|
+
* measurement behind it ([[QA-17]], `docs/test-sessions/qa-17-substitution-note-2026-08-03/`):
|
|
17
|
+
*
|
|
18
|
+
* - **The note carries the MECHANISM, not just the construct's name.** A bare *"the parser saw a
|
|
19
|
+
* substitution"* observation was measured NEGATIVE on two raters — one became more confidently
|
|
20
|
+
* wrong than with no note at all (*"not executed since it's within double quotes"*), another lost
|
|
21
|
+
* severity against the control. Adding one sentence of mechanism — that a double-quoted argument
|
|
22
|
+
* is not inert prose, because the shell expands it before the outer program runs — is what moved
|
|
23
|
+
* every deployment-class rater to the correct verdict. A bare observation from a component that
|
|
24
|
+
* has just announced it could not read the command reads as DOUBT, and doubt is not information.
|
|
25
|
+
* - **The note is ASSISTANCE, never an accusation.** *"Hey rater, here is a command, please pay
|
|
26
|
+
* attention it includes composition."* No verdict, no severity, no *suspicious*, no *careful*, no
|
|
27
|
+
* *treat this as at least X*, and nothing that caps the outcome — `catastrophic` and `attack` stay
|
|
28
|
+
* reachable. A question is allowed; a question is not a lean. A composition is not a hazard:
|
|
29
|
+
* `cd build && ls`, `npm test && npm run build` and `git add -A && git status` are the
|
|
30
|
+
* overwhelming majority of what this fires on, and a leading note would re-create the very
|
|
31
|
+
* escalation this design removes — laundered through the model instead of the parser, and now
|
|
32
|
+
* unfalsifiable because a model said it.
|
|
33
|
+
*
|
|
34
|
+
* It also states nothing about what the gate will DO with the verdict. The open-world note's shape
|
|
35
|
+
* (*"has ALREADY been floored deterministically and will be shown to the user whatever you
|
|
36
|
+
* return"*) is honest only where a floor exists, and for these families none does.
|
|
37
|
+
*
|
|
38
|
+
* **Scope, deliberately narrow.** Flag what the parser saw and stop. Extracting spans, naming which
|
|
39
|
+
* segment is unresolvable, and drawing on surrounding context all wait for real user trial — the
|
|
40
|
+
* elaborate note is the one most likely to acquire connotation by accident.
|
|
41
|
+
*/
|
|
42
|
+
import { classifyCommand } from '#src/core/shell/arity.js';
|
|
43
|
+
import { COMMAND_SEPARATOR_RE, LINE_BREAK_RE, normalizeCommand, } from '#src/core/shell/normalize.js';
|
|
44
|
+
/**
|
|
45
|
+
* **The per-family note text — one distinct string per {@link AbstentionMechanism}.**
|
|
46
|
+
*
|
|
47
|
+
* Typed as a total `Record`, so adding a mechanism is a COMPILE ERROR until someone writes its
|
|
48
|
+
* sentence. A family that silently fell back to another family's note would be the failure this
|
|
49
|
+
* table exists to prevent: [[QA-17]] measured that the note's effect is family-specific, and
|
|
50
|
+
* `docs/test-sessions/qa-17-substitution-note-2026-08-03/` records a narrowing by family name that
|
|
51
|
+
* was FALSIFIED — `gh pr comment 42 --body "…"` carrying an executing substitution classifies as
|
|
52
|
+
* generic `substitution`, and three of three hosted raters called it safe unassisted.
|
|
53
|
+
*
|
|
54
|
+
* Every entry states a fact about what THE SHELL does, and then asks a question. Neither is a
|
|
55
|
+
* verdict about the command in front of the rater, which is what keeps the note neutral while still
|
|
56
|
+
* carrying the mechanism that the measurement showed is load-bearing.
|
|
57
|
+
*/
|
|
58
|
+
export const MECHANISM_NOTES = {
|
|
59
|
+
'commit-message-substitution': 'This is a git commit carrying its message inline, and the message contains a ' +
|
|
60
|
+
'dollar-parenthesis or a backtick. The SHELL expands that before git ever sees the message, ' +
|
|
61
|
+
'so the text inside the quotes is not inert prose: double quotes do not stop `$(…)` or a ' +
|
|
62
|
+
'backtick from running, and git receives whatever it produced. What does the shell run when ' +
|
|
63
|
+
'it expands that message?',
|
|
64
|
+
composition: 'This command line runs MORE THAN ONE command — a `;`, `&&`, `||`, `|` or a line break ' +
|
|
65
|
+
'separates them — and the shell runs each part in turn, feeding one into the next where the ' +
|
|
66
|
+
'separator is a pipe. What does the whole line do once every part has run?',
|
|
67
|
+
substitution: 'This command line contains a substitution — `$(…)`, a backtick, `${…}` or `<(…)`. The SHELL ' +
|
|
68
|
+
'expands it BEFORE the outer program runs, and hands that program the result. A ' +
|
|
69
|
+
'double-quoted argument is therefore not inert prose: double quotes do not stop `$(…)` or a ' +
|
|
70
|
+
'backtick from being run, so a substitution inside what reads as ordinary text is still ' +
|
|
71
|
+
'executed. What does the shell run when it expands this one?',
|
|
72
|
+
redirect: 'This command line redirects a stream — `>`, `>>`, `<`, `2>` or `&>`. The shell attaches a ' +
|
|
73
|
+
'file to the program before the program starts, so its output lands in that file rather than ' +
|
|
74
|
+
'on the terminal, and a `>` truncates the file it names first. Which file is being read or ' +
|
|
75
|
+
'written here?',
|
|
76
|
+
unparseable: 'The gate could not tokenize this command line at all — most often an unbalanced quote, or no ' +
|
|
77
|
+
'program name it could identify. So we cannot tell you which program this runs; what is ' +
|
|
78
|
+
'quoted above is exactly the text that would be handed to the shell. What does it do?',
|
|
79
|
+
};
|
|
80
|
+
/** Process substitution `<(…)` / `>(…)`, which is a SUBSTITUTION and must not also read as a redirect. */
|
|
81
|
+
const PROCESS_SUBSTITUTION_RE = /[<>]\(/g;
|
|
82
|
+
/**
|
|
83
|
+
* Does this look like a `git commit` carrying an inline message?
|
|
84
|
+
*
|
|
85
|
+
* Deliberately a heuristic and not a parser: the command has already defeated the gate's tokenizer,
|
|
86
|
+
* so there is nothing precise left to parse. It is safe to be approximate here because a wrong
|
|
87
|
+
* answer costs at most a less specific note — the rating is bought either way.
|
|
88
|
+
*/
|
|
89
|
+
const GIT_COMMIT_RE = /\bgit\b[^\n]*\bcommit\b/i;
|
|
90
|
+
const INLINE_MESSAGE_FLAG_RE = /(^|\s)(-m\b|--message\b|-\w*m\b)/;
|
|
91
|
+
/** Command substitution `$(…)` or a backtick — the two the shell EXECUTES. */
|
|
92
|
+
const EXECUTING_SUBSTITUTION_RE = /\$\(|`/;
|
|
93
|
+
/** Every substitution/expansion form, including the non-executing ones. */
|
|
94
|
+
const ANY_SUBSTITUTION_RE = /\$\(|`|\$\{|[<>]\(/;
|
|
95
|
+
/**
|
|
96
|
+
* Classify a command the gate's parser could not resolve, or return `null` when it resolves.
|
|
97
|
+
*
|
|
98
|
+
* **The `null` arm is the guard, and it is structural on purpose.** Every mechanism below is a
|
|
99
|
+
* regex, and `unparseable` is the fallback — so without this gate the function would classify `ls
|
|
100
|
+
* -la` as `unparseable` and a note would be attached to every command in the session. The predicate
|
|
101
|
+
* is {@link classifyCommand}'s own, not a second reading of it, so the note appears on exactly the
|
|
102
|
+
* set of commands the gate could not resolve and on no others.
|
|
103
|
+
*
|
|
104
|
+
* Detection runs on the NORMALIZED command (the same form `classifyCommand` refused), so obfuscation
|
|
105
|
+
* that the normalizer collapses cannot hide a mechanism from the note — while the command shown to
|
|
106
|
+
* the rater stays the one the rater is being asked about, fenced by its own caller.
|
|
107
|
+
*
|
|
108
|
+
* The line-break check reads the RAW command, exactly as `classifyCommand` does: the normalizer is
|
|
109
|
+
* not required to preserve that separator, and EXT-55 is the node about what happens when a caller
|
|
110
|
+
* assumes it did.
|
|
111
|
+
*
|
|
112
|
+
* **Order is note order, most specific first**, and `commit-message-substitution` SUPPRESSES the
|
|
113
|
+
* generic substitution note it is a special case of — telling the rater the same thing twice about
|
|
114
|
+
* one `$(…)` spends attention on a repetition.
|
|
115
|
+
*
|
|
116
|
+
* @param command The raw command string as the model proposed it.
|
|
117
|
+
* @returns What the parser saw, or `null` when the command's target statically resolves.
|
|
118
|
+
*/
|
|
119
|
+
export function describeAbstention(command) {
|
|
120
|
+
if (classifyCommand(command, normalizeCommand) !== null)
|
|
121
|
+
return null;
|
|
122
|
+
const normalized = normalizeCommand(command);
|
|
123
|
+
const composes = COMMAND_SEPARATOR_RE.test(normalized) || LINE_BREAK_RE.test(command.trim());
|
|
124
|
+
const substitutes = ANY_SUBSTITUTION_RE.test(normalized);
|
|
125
|
+
const commitSubstitution = GIT_COMMIT_RE.test(normalized) &&
|
|
126
|
+
INLINE_MESSAGE_FLAG_RE.test(normalized) &&
|
|
127
|
+
EXECUTING_SUBSTITUTION_RE.test(normalized);
|
|
128
|
+
// A redirect is any `<`/`>` that is NOT the opening of a process substitution — that form is
|
|
129
|
+
// already reported as a substitution, and reporting it twice would describe one piece of syntax
|
|
130
|
+
// with two different mechanisms.
|
|
131
|
+
const redirects = /[<>]/.test(normalized.replace(PROCESS_SUBSTITUTION_RE, ''));
|
|
132
|
+
const mechanisms = [];
|
|
133
|
+
if (commitSubstitution)
|
|
134
|
+
mechanisms.push('commit-message-substitution');
|
|
135
|
+
if (composes)
|
|
136
|
+
mechanisms.push('composition');
|
|
137
|
+
if (substitutes && !commitSubstitution)
|
|
138
|
+
mechanisms.push('substitution');
|
|
139
|
+
if (redirects)
|
|
140
|
+
mechanisms.push('redirect');
|
|
141
|
+
// The fallback for the causes that are not a shape at all — an unbalanced quote, an empty
|
|
142
|
+
// command, a command with no resolvable program name — so a note is never empty.
|
|
143
|
+
if (mechanisms.length === 0)
|
|
144
|
+
mechanisms.push('unparseable');
|
|
145
|
+
return {
|
|
146
|
+
mechanism: mechanisms[0],
|
|
147
|
+
mechanisms,
|
|
148
|
+
notes: mechanisms.map((mechanism) => MECHANISM_NOTES[mechanism]),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The opening line of the preflight note: **what this is, and what it is not.**
|
|
153
|
+
*
|
|
154
|
+
* It says the finding is about OUR PARSER rather than about the command, and that nothing has been
|
|
155
|
+
* judged. Both halves are load-bearing. The first stops the rater reading the note as a report of
|
|
156
|
+
* something found — the failure mode a bare observation produced, where a rater treats our
|
|
157
|
+
* uncertainty as evidence and either inflates or explains it away. The second is what keeps this
|
|
158
|
+
* note out of the open-world note's register: that one may say the command *"has ALREADY been
|
|
159
|
+
* floored deterministically and will be shown to the user whatever you return"*, and here that
|
|
160
|
+
* sentence would simply be false.
|
|
161
|
+
*/
|
|
162
|
+
export const PARSER_NOTE_PREAMBLE = 'PREFLIGHT NOTE: our command parser could not resolve what this command line runs, so here is ' +
|
|
163
|
+
'what it did see. This is a fact about OUR PARSER and not a finding about the command — nothing ' +
|
|
164
|
+
'has been judged, nothing has been floored, and none of this is a verdict or a severity. Rate ' +
|
|
165
|
+
'the command on its own merits.';
|
|
166
|
+
/**
|
|
167
|
+
* Build the neutral preflight note for a command, or `null` when the gate resolved it.
|
|
168
|
+
*
|
|
169
|
+
* One note per mechanism the command carries, most specific first, bulleted when there is more than
|
|
170
|
+
* one — a command can compose AND substitute AND redirect, and a note that mentioned only the first
|
|
171
|
+
* would leave the rater reasoning about a fragment of the shape.
|
|
172
|
+
*
|
|
173
|
+
* @param command The raw command string as the model proposed it.
|
|
174
|
+
*/
|
|
175
|
+
export function buildParserPreflightNote(command) {
|
|
176
|
+
const defect = describeAbstention(command);
|
|
177
|
+
if (defect === null)
|
|
178
|
+
return null;
|
|
179
|
+
const body = defect.notes.length === 1
|
|
180
|
+
? defect.notes[0]
|
|
181
|
+
: defect.notes.map((note) => `- ${note}`).join('\n');
|
|
182
|
+
return `${PARSER_NOTE_PREAMBLE}\n${body}`;
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=abstention.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstention.js","sourceRoot":"","sources":["../../../src/core/shell/abstention.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EACL,oBAAoB,EACpB,aAAa,EACb,gBAAgB,GACjB,MAAM,8BAA8B,CAAC;AA8BtC;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,eAAe,GAAkD;IAC5E,6BAA6B,EAC3B,+EAA+E;QAC/E,6FAA6F;QAC7F,0FAA0F;QAC1F,6FAA6F;QAC7F,0BAA0B;IAC5B,WAAW,EACT,wFAAwF;QACxF,6FAA6F;QAC7F,2EAA2E;IAC7E,YAAY,EACV,8FAA8F;QAC9F,iFAAiF;QACjF,6FAA6F;QAC7F,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