@robhowley/pi-structured-return 1.10.1 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +37 -83
- package/extensions/structured-return/config/registry.ts +4 -36
- package/extensions/structured-return/index.ts +105 -90
- package/extensions/structured-return/native.ts +223 -0
- package/extensions/structured-return/parsers/ava-text.ts +2 -168
- package/extensions/structured-return/parsers/bandit-json.ts +2 -79
- package/extensions/structured-return/parsers/black-text.ts +2 -73
- package/extensions/structured-return/parsers/cargo-build.ts +2 -78
- package/extensions/structured-return/parsers/cargo-test.ts +2 -135
- package/extensions/structured-return/parsers/checkstyle-xml.ts +2 -182
- package/extensions/structured-return/parsers/clang-text.ts +2 -60
- package/extensions/structured-return/parsers/dbt-json.ts +2 -135
- package/extensions/structured-return/parsers/dotnet-build-text.ts +2 -49
- package/extensions/structured-return/parsers/eslint-json.ts +2 -38
- package/extensions/structured-return/parsers/go-test-json.ts +2 -138
- package/extensions/structured-return/parsers/hadolint-json.ts +2 -60
- package/extensions/structured-return/parsers/htmlhint-json.ts +2 -73
- package/extensions/structured-return/parsers/isort-text.ts +2 -63
- package/extensions/structured-return/parsers/javac-text.ts +2 -82
- package/extensions/structured-return/parsers/jsonlint-text.ts +2 -65
- package/extensions/structured-return/parsers/junit-xml.ts +2 -325
- package/extensions/structured-return/parsers/markdownlint-json.ts +2 -74
- package/extensions/structured-return/parsers/minitest-text.ts +2 -107
- package/extensions/structured-return/parsers/mocha-json.ts +2 -83
- package/extensions/structured-return/parsers/mypy-json.ts +2 -79
- package/extensions/structured-return/parsers/node-test-text.ts +2 -92
- package/extensions/structured-return/parsers/npm-audit-json.ts +2 -90
- package/extensions/structured-return/parsers/prettier-text.ts +2 -66
- package/extensions/structured-return/parsers/pylint-json.ts +2 -63
- package/extensions/structured-return/parsers/pyright-json.ts +2 -82
- package/extensions/structured-return/parsers/rspec-json.ts +2 -75
- package/extensions/structured-return/parsers/rubocop-json.ts +2 -75
- package/extensions/structured-return/parsers/ruff-json.ts +2 -36
- package/extensions/structured-return/parsers/shellcheck-json.ts +2 -60
- package/extensions/structured-return/parsers/stylelint-json.ts +2 -75
- package/extensions/structured-return/parsers/swiftc-text.ts +2 -52
- package/extensions/structured-return/parsers/tail-fallback.ts +2 -19
- package/extensions/structured-return/parsers/tidy-text.ts +2 -66
- package/extensions/structured-return/parsers/tsc-text.ts +2 -46
- package/extensions/structured-return/parsers/unittest-text.ts +2 -125
- package/extensions/structured-return/parsers/utils.ts +28 -0
- package/extensions/structured-return/parsers/vale-json.ts +2 -79
- package/extensions/structured-return/parsers/vitest-json.ts +2 -59
- package/extensions/structured-return/storage/log-store.ts +0 -15
- package/extensions/structured-return/types.ts +1 -14
- package/native/Cargo.lock +193 -0
- package/native/Cargo.toml +21 -0
- package/native/THIRD-PARTY-LICENSES.txt +3660 -0
- package/native/bin/darwin-arm64/build.json +7 -0
- package/native/bin/darwin-arm64/structured-return-native +0 -0
- package/native/bin/darwin-x64/build.json +7 -0
- package/native/bin/darwin-x64/structured-return-native +0 -0
- package/native/bin/linux-arm64-musl/build.json +7 -0
- package/native/bin/linux-arm64-musl/structured-return-native +0 -0
- package/native/bin/linux-x64-musl/build.json +7 -0
- package/native/bin/linux-x64-musl/structured-return-native +0 -0
- package/native/src/common.rs +118 -0
- package/native/src/main.rs +55 -0
- package/native/src/parsers/ava_text.rs +125 -0
- package/native/src/parsers/bandit_json.rs +120 -0
- package/native/src/parsers/black_text.rs +45 -0
- package/native/src/parsers/cargo_build.rs +96 -0
- package/native/src/parsers/cargo_test.rs +140 -0
- package/native/src/parsers/checkstyle_xml.rs +137 -0
- package/native/src/parsers/clang_text.rs +39 -0
- package/native/src/parsers/compiler_helpers.rs +74 -0
- package/native/src/parsers/dbt_json.rs +243 -0
- package/native/src/parsers/dotnet_build_text.rs +50 -0
- package/native/src/parsers/eslint_json.rs +58 -0
- package/native/src/parsers/go_test_json.rs +346 -0
- package/native/src/parsers/hadolint_json.rs +37 -0
- package/native/src/parsers/htmlhint_json.rs +71 -0
- package/native/src/parsers/isort_text.rs +22 -0
- package/native/src/parsers/javac_text.rs +49 -0
- package/native/src/parsers/json_support.rs +287 -0
- package/native/src/parsers/jsonlint_text.rs +50 -0
- package/native/src/parsers/junit_xml.rs +430 -0
- package/native/src/parsers/markdownlint_json.rs +68 -0
- package/native/src/parsers/minitest_text.rs +91 -0
- package/native/src/parsers/mocha_json.rs +75 -0
- package/native/src/parsers/mod.rs +119 -0
- package/native/src/parsers/mypy_json.rs +93 -0
- package/native/src/parsers/node_test_text.rs +106 -0
- package/native/src/parsers/npm_audit_json.rs +119 -0
- package/native/src/parsers/prettier_text.rs +34 -0
- package/native/src/parsers/pylint_json.rs +44 -0
- package/native/src/parsers/pyright_json.rs +88 -0
- package/native/src/parsers/rspec_json.rs +84 -0
- package/native/src/parsers/rubocop_json.rs +46 -0
- package/native/src/parsers/ruff_json.rs +30 -0
- package/native/src/parsers/shellcheck_json.rs +40 -0
- package/native/src/parsers/stylelint_json.rs +46 -0
- package/native/src/parsers/swiftc_text.rs +38 -0
- package/native/src/parsers/tail_fallback.rs +51 -0
- package/native/src/parsers/test_helpers.rs +172 -0
- package/native/src/parsers/tidy_text.rs +77 -0
- package/native/src/parsers/tsc_text.rs +28 -0
- package/native/src/parsers/unittest_text.rs +125 -0
- package/native/src/parsers/vale_json.rs +66 -0
- package/native/src/parsers/vitest_json.rs +71 -0
- package/native/src/parsers/xml_support.rs +241 -0
- package/native/src/runner.rs +252 -0
- package/package.json +21 -7
- package/scripts/build-native.mjs +73 -0
- package/scripts/native-artifacts.mjs +69 -0
- package/skills/structured-return/SKILL.md +103 -249
- package/extensions/structured-return/config/project-config.ts +0 -10
- package/extensions/structured-return/examples/.pi/machine-readable.json +0 -18
- package/extensions/structured-return/examples/.pi/parsers/foo-cli.ts +0 -25
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rob Howley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# pi-structured-return
|
|
2
2
|
|
|
3
|
-
A [Pi](https://pi.dev/) extension that
|
|
3
|
+
A Rust-powered [Pi](https://pi.dev/) extension that cuts command-output tokens by up to 95% while preserving diagnostics and full logs. Adds the `structured_return` tool.
|
|
4
4
|
|
|
5
5
|
A failing test run, before and after:
|
|
6
6
|
|
|
@@ -54,9 +54,24 @@ pytest test_math.py --junitxml=.tmp/report.xml → cwd: project
|
|
|
54
54
|
pi install npm:@robhowley/pi-structured-return
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
+
Supports macOS 11+ and Linux on x64/arm64. Windows is not supported.
|
|
58
|
+
|
|
57
59
|
## Design
|
|
58
60
|
|
|
59
|
-
`structured_return` is a separate tool, not a wrapper around `bash`.
|
|
61
|
+
`structured_return` is a separate tool, not a wrapper around `bash`. It doesn't intercept `bash` calls or silently rewrite commands. The agent chooses which tool to use, with the skill guiding it toward `structured_return` when compact output helps. This follows Pi's approach: build on, don't obfuscate, existing primitives to extend the harness.
|
|
62
|
+
|
|
63
|
+
## How it works
|
|
64
|
+
|
|
65
|
+
1. The agent runs commands through `structured_return` when it would reduce noise and token usage.
|
|
66
|
+
2. Full output is captured.
|
|
67
|
+
3. A parser converts noisy CLI output into a compact structured result. If no parser matches, a successful command returns only `command completed; no parser matched`, with the log path in tool details. An unsuccessful unmatched command returns the last 200 log lines and the log path in the text.
|
|
68
|
+
4. The agent receives the structured result in context — signal only, no noise.
|
|
69
|
+
|
|
70
|
+
Full logs remain on disk for the agent and humans to inspect. Run `/sr-stats` to see token savings for the current session, cwd lifetime, and lifetime.
|
|
71
|
+
|
|
72
|
+
## Supported parsers
|
|
73
|
+
|
|
74
|
+
See the shipped skill's [command recipes](skills/structured-return/SKILL.md#command-recipes) for supported output formats and built-in `parseAs` IDs, and [JUnit reports](skills/structured-return/SKILL.md#junit-reports) for report-file commands and `artifactPaths`.
|
|
60
75
|
|
|
61
76
|
## Token reduction
|
|
62
77
|
|
|
@@ -160,86 +175,6 @@ Evaluated for structured parsing but raw output is already compact enough that a
|
|
|
160
175
|
| `vulture` | 58 | `file:line: message (confidence%)` | single line per finding |
|
|
161
176
|
| `pydocstyle` | 48 | `file:line context + CODE: message` | two lines per issue; structured format would repeat file paths |
|
|
162
177
|
|
|
163
|
-
## How it works
|
|
164
|
-
|
|
165
|
-
1. The agent runs commands through `structured_return` when it would reduce noise and token usage.
|
|
166
|
-
2. Full output is captured and stored as a log.
|
|
167
|
-
3. A parser converts noisy CLI output into a compact structured result. If no parser matches, the last 200 lines and the log path are returned as a fallback.
|
|
168
|
-
4. The agent receives the structured result in context — signal only, no noise.
|
|
169
|
-
5. The full log is always available on disk for both the agent and humans to inspect.
|
|
170
|
-
6. Run `/sr-stats` to see how many tokens structured-return has saved — for the current session, the current working directory, and lifetime across all sessions.
|
|
171
|
-
|
|
172
|
-
Run `/sr-parsers` in a pi session to see all registered parsers with their match rules. Run `/sr-stats` to see token savings for the current session, cwd lifetime, and lifetime.
|
|
173
|
-
|
|
174
|
-
## Extending with project-local parsers
|
|
175
|
-
|
|
176
|
-
Built-in parsers cover common tools. For everything else — internal CLIs, custom test runners, proprietary lint tools — add a `.pi/structured-return.json` to your project root.
|
|
177
|
-
|
|
178
|
-
**Why:** keeps token costs low for tools the built-ins don't know about, without forking the package.
|
|
179
|
-
|
|
180
|
-
**Two options:**
|
|
181
|
-
|
|
182
|
-
### 1. Re-use a built-in parser
|
|
183
|
-
|
|
184
|
-
Route a project-specific command to an existing parser. Use this when your tool's output already matches a supported format (e.g. a test runner that emits JUnit XML).
|
|
185
|
-
|
|
186
|
-
```json
|
|
187
|
-
// .pi/structured-return.json
|
|
188
|
-
{
|
|
189
|
-
"parsers": [
|
|
190
|
-
{
|
|
191
|
-
"id": "acme-tests",
|
|
192
|
-
"match": { "argvIncludes": ["acme", "test"] },
|
|
193
|
-
"parseAs": "junit-xml"
|
|
194
|
-
}
|
|
195
|
-
]
|
|
196
|
-
}
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
### 2. Write a custom parser
|
|
200
|
-
|
|
201
|
-
Point to a local `.ts` file for tools with unique output formats.
|
|
202
|
-
|
|
203
|
-
```json
|
|
204
|
-
// .pi/structured-return.json
|
|
205
|
-
{
|
|
206
|
-
"parsers": [
|
|
207
|
-
{
|
|
208
|
-
"id": "foo-json",
|
|
209
|
-
"match": { "argvIncludes": ["foo-cli", "check"] },
|
|
210
|
-
"module": "parsers/foo-cli.js"
|
|
211
|
-
}
|
|
212
|
-
]
|
|
213
|
-
}
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
```ts
|
|
217
|
-
// .pi/parsers/foo-cli.ts
|
|
218
|
-
import fs from "node:fs";
|
|
219
|
-
import type { RunContext } from "@robhowley/pi-structured-return/types";
|
|
220
|
-
|
|
221
|
-
export default {
|
|
222
|
-
id: "foo-json",
|
|
223
|
-
async parse(ctx: RunContext) {
|
|
224
|
-
const data = JSON.parse(fs.readFileSync(ctx.stdoutPath, "utf8"));
|
|
225
|
-
return {
|
|
226
|
-
tool: "foo-cli",
|
|
227
|
-
status: data.ok ? "pass" : "fail",
|
|
228
|
-
summary: data.ok ? "passed" : `${data.errors.length} errors`,
|
|
229
|
-
failures: data.errors.map((e, i) => ({
|
|
230
|
-
id: e.id ?? `error-${i}`,
|
|
231
|
-
file: e.file,
|
|
232
|
-
line: e.line,
|
|
233
|
-
message: e.message,
|
|
234
|
-
})),
|
|
235
|
-
logPath: ctx.logPath,
|
|
236
|
-
};
|
|
237
|
-
},
|
|
238
|
-
};
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
The parser receives a `RunContext` (command, argv, cwd, stdout/stderr paths, artifact paths, log path) and returns a `ParsedResult`. Match rules support `argvIncludes` (array of required tokens) or `regex` (tested against the full argv string).
|
|
242
|
-
|
|
243
178
|
## Structured result schema
|
|
244
179
|
|
|
245
180
|
Every parser returns the same shape. The model always knows where to look.
|
|
@@ -253,4 +188,23 @@ Every parser returns the same shape. The model always knows where to look.
|
|
|
253
188
|
| `cwd` | `string` | Working directory — anchor for resolving relative paths in failures |
|
|
254
189
|
| `failures` | `{ id, file?, line?, message?, rule? }[]` | Per-failure details with relative file paths |
|
|
255
190
|
| `logPath` | `string` | Path to full stdout+stderr log |
|
|
256
|
-
| `rawTail` | `string?` | Last 200
|
|
191
|
+
| `rawTail` | `string?` | Last 200 log lines on unmatched errors, not successful unmatched commands |
|
|
192
|
+
|
|
193
|
+
## Native engine
|
|
194
|
+
|
|
195
|
+
Command capture and built-in parsers run in Rust.
|
|
196
|
+
|
|
197
|
+
- Prebuilt binaries ship with npm. No Rust toolchain or install-time downloads required.
|
|
198
|
+
- Linux binaries support glibc and musl; Node/Pi requirements still apply.
|
|
199
|
+
- There is no TypeScript runtime fallback.
|
|
200
|
+
|
|
201
|
+
### Building and publishing
|
|
202
|
+
|
|
203
|
+
Source checkouts require Rust 1.88.0 via rustup. Linux also needs `musl-gcc` and the host's Rust musl target.
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
pnpm install --frozen-lockfile
|
|
207
|
+
pnpm --filter @robhowley/pi-structured-return build
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Build before testing or loading the extension. CI assembles all four platform binaries for publishing; `prepack` rejects incomplete or stale artifacts.
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type { ParserModule, ParserRegistration } from '../types';
|
|
1
|
+
import type { ParserModule } from '../types';
|
|
3
2
|
import ruffJson from '../parsers/ruff-json';
|
|
4
3
|
import eslintJson from '../parsers/eslint-json';
|
|
5
4
|
import vitestJson from '../parsers/vitest-json';
|
|
@@ -99,7 +98,7 @@ function hasFlag(argv: string[], flag: string, value: string): boolean {
|
|
|
99
98
|
return false;
|
|
100
99
|
}
|
|
101
100
|
|
|
102
|
-
/**
|
|
101
|
+
/** Ordered detection patterns used when no explicit built-in parser is selected. */
|
|
103
102
|
const AUTO_DETECT: Array<{ parserId: string; detect: (argv: string[]) => boolean }> = [
|
|
104
103
|
{
|
|
105
104
|
parserId: 'eslint-json',
|
|
@@ -265,42 +264,11 @@ const AUTO_DETECT: Array<{ parserId: string; detect: (argv: string[]) => boolean
|
|
|
265
264
|
},
|
|
266
265
|
];
|
|
267
266
|
|
|
268
|
-
export function
|
|
269
|
-
|
|
270
|
-
const autoDetectIds = new Set(AUTO_DETECT.map((d) => d.parserId));
|
|
271
|
-
return builtInIds.map((id) => ({ id, autoDetect: autoDetectIds.has(id) }));
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export async function resolveParser(opts: {
|
|
275
|
-
cwd: string;
|
|
276
|
-
parseAs?: string;
|
|
277
|
-
argv: string[];
|
|
278
|
-
registrations: ParserRegistration[];
|
|
279
|
-
}): Promise<ParserModule> {
|
|
280
|
-
if (opts.parseAs && builtIns[opts.parseAs]) return builtIns[opts.parseAs];
|
|
281
|
-
|
|
282
|
-
const matched = opts.registrations.find((reg) => matches(reg, opts.argv));
|
|
283
|
-
if (matched?.parseAs && builtIns[matched.parseAs]) return builtIns[matched.parseAs];
|
|
284
|
-
if (matched?.module) {
|
|
285
|
-
const modulePath = path.resolve(opts.cwd, '.pi', matched.module);
|
|
286
|
-
const loaded = await import(modulePath);
|
|
287
|
-
return loaded.default ?? loaded;
|
|
288
|
-
}
|
|
267
|
+
export function resolveParser(opts: { parseAs?: string; argv: string[] }): ParserModule {
|
|
268
|
+
if (opts.parseAs && Object.hasOwn(builtIns, opts.parseAs)) return builtIns[opts.parseAs];
|
|
289
269
|
|
|
290
270
|
const autoMatched = AUTO_DETECT.find((d) => d.detect(opts.argv));
|
|
291
271
|
if (autoMatched) return builtIns[autoMatched.parserId];
|
|
292
272
|
|
|
293
273
|
return builtIns['tail-fallback'];
|
|
294
274
|
}
|
|
295
|
-
|
|
296
|
-
function matches(reg: ParserRegistration, argv: string[]): boolean {
|
|
297
|
-
if (reg.match?.argvIncludes?.length) {
|
|
298
|
-
const ok = reg.match.argvIncludes.every((token) => argv.includes(token));
|
|
299
|
-
if (!ok) return false;
|
|
300
|
-
}
|
|
301
|
-
if (reg.match?.regex) {
|
|
302
|
-
const text = argv.join(' ');
|
|
303
|
-
if (!new RegExp(reg.match.regex).test(text)) return false;
|
|
304
|
-
}
|
|
305
|
-
return Boolean(reg.match?.argvIncludes?.length || reg.match?.regex);
|
|
306
|
-
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { spawn } from 'node:child_process';
|
|
2
1
|
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
3
|
import { randomUUID } from 'node:crypto';
|
|
4
4
|
import { globSync } from 'glob';
|
|
5
5
|
import { Type } from '@sinclair/typebox';
|
|
6
6
|
import type { ExtensionAPI, ExtensionContext } from '@mariozechner/pi-coding-agent';
|
|
7
7
|
import { Text } from '@mariozechner/pi-tui';
|
|
8
8
|
import type { ObservedRunArgs, ParsedResult, RunContext } from './types';
|
|
9
|
-
import { ensureRunDir
|
|
10
|
-
import {
|
|
11
|
-
import { resolveParser
|
|
12
|
-
import {
|
|
9
|
+
import { ensureRunDir } from './storage/log-store';
|
|
10
|
+
import { runNative } from './native';
|
|
11
|
+
import { resolveParser } from './config/registry';
|
|
12
|
+
import { readTail } from './parsers/utils';
|
|
13
|
+
import tailFallback from './parsers/tail-fallback';
|
|
13
14
|
import {
|
|
14
15
|
appendRun,
|
|
15
16
|
readLifetimeStats,
|
|
@@ -18,41 +19,6 @@ import {
|
|
|
18
19
|
} from './storage/session-stats';
|
|
19
20
|
|
|
20
21
|
export default function structuredReturn(pi: ExtensionAPI) {
|
|
21
|
-
pi.registerCommand('sr-parsers', {
|
|
22
|
-
description: 'List all structured-return parsers: built-ins and project-local registrations',
|
|
23
|
-
handler: async (_args, ctx) => {
|
|
24
|
-
const lines: string[] = ['structured-return parsers', ''];
|
|
25
|
-
|
|
26
|
-
lines.push('built-in:');
|
|
27
|
-
for (const { id, autoDetect } of listParsers()) {
|
|
28
|
-
lines.push(` ${id}${autoDetect ? ' (auto-detect)' : ''}`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const projectRegistrations = loadProjectConfig(ctx.cwd);
|
|
32
|
-
lines.push('');
|
|
33
|
-
lines.push('project-local (.pi/structured-return.json):');
|
|
34
|
-
if (projectRegistrations.length === 0) {
|
|
35
|
-
lines.push(' (none)');
|
|
36
|
-
} else {
|
|
37
|
-
for (const reg of projectRegistrations) {
|
|
38
|
-
const via = reg.parseAs
|
|
39
|
-
? `→ ${reg.parseAs}`
|
|
40
|
-
: reg.module
|
|
41
|
-
? `→ module: ${reg.module}`
|
|
42
|
-
: '';
|
|
43
|
-
const match = reg.match?.argvIncludes
|
|
44
|
-
? `argv includes [${reg.match.argvIncludes.join(', ')}]`
|
|
45
|
-
: reg.match?.regex
|
|
46
|
-
? `regex: ${reg.match.regex}`
|
|
47
|
-
: '(no match rule)';
|
|
48
|
-
lines.push(` ${reg.id} ${match} ${via}`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
ctx.ui.notify(lines.join('\n'), 'info');
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
|
|
56
22
|
pi.registerCommand('sr-stats', {
|
|
57
23
|
description:
|
|
58
24
|
'Show token savings from structured-return (current session + cwd lifetime + lifetime)',
|
|
@@ -106,7 +72,7 @@ export default function structuredReturn(pi: ExtensionAPI) {
|
|
|
106
72
|
name: 'structured_return',
|
|
107
73
|
label: 'Structured Return',
|
|
108
74
|
description:
|
|
109
|
-
'Run a command, store full logs, apply
|
|
75
|
+
'Run a command, store full logs, apply a built-in parser when available, and fall back to tail + log path.',
|
|
110
76
|
promptGuidelines: [
|
|
111
77
|
'Prefer structured_return over bash for test suites, linters, type checkers, and build commands - it returns compact results. Check the structured-return skill for the right flags and parseAs value for each tool.',
|
|
112
78
|
],
|
|
@@ -119,37 +85,85 @@ export default function structuredReturn(pi: ExtensionAPI) {
|
|
|
119
85
|
async execute(
|
|
120
86
|
_toolCallId: string,
|
|
121
87
|
args: ObservedRunArgs,
|
|
122
|
-
|
|
88
|
+
signal: AbortSignal | undefined,
|
|
123
89
|
_onUpdate: unknown,
|
|
124
90
|
ctx: ExtensionContext,
|
|
125
91
|
) {
|
|
126
|
-
|
|
92
|
+
// Resolve through the filesystem before normalizing symlink/.. components.
|
|
93
|
+
const cwd = fs.realpathSync.native(args.cwd ?? ctx.cwd ?? process.cwd());
|
|
127
94
|
const runDir = ensureRunDir(cwd);
|
|
128
95
|
const runId = randomUUID();
|
|
129
96
|
const argv = shellSplit(args.command);
|
|
130
|
-
const {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
97
|
+
const {
|
|
98
|
+
exitCode,
|
|
99
|
+
rawBytes,
|
|
100
|
+
cancelled: captureCancelled,
|
|
101
|
+
...logs
|
|
102
|
+
} = await runNative(args.command, cwd, runDir, runId, signal);
|
|
103
|
+
let parserId = 'unknown';
|
|
104
|
+
let result: ParsedResult = {
|
|
105
|
+
tool: parserId,
|
|
106
|
+
status: 'error',
|
|
107
|
+
summary: 'command cancelled',
|
|
108
|
+
exitCode,
|
|
136
109
|
cwd,
|
|
137
|
-
|
|
138
|
-
...logs,
|
|
110
|
+
logPath: logs.logPath,
|
|
139
111
|
};
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
112
|
+
if (!captureCancelled && !signal?.aborted) {
|
|
113
|
+
try {
|
|
114
|
+
const artifactPaths = expandArtifactPaths(args.artifactPaths ?? [], cwd);
|
|
115
|
+
const runCtx: RunContext = {
|
|
116
|
+
command: args.command,
|
|
117
|
+
argv,
|
|
118
|
+
cwd,
|
|
119
|
+
artifactPaths,
|
|
120
|
+
signal,
|
|
121
|
+
...logs,
|
|
122
|
+
};
|
|
123
|
+
const parser = resolveParser({ parseAs: args.parseAs, argv });
|
|
124
|
+
parserId = parser.id;
|
|
125
|
+
if (!signal?.aborted) {
|
|
126
|
+
// Successful fallback output is summary-only. Do not read and transmit a
|
|
127
|
+
// potentially enormous tail just to discard it in formatResult.
|
|
128
|
+
const parsed =
|
|
129
|
+
parser === tailFallback && exitCode === 0
|
|
130
|
+
? {
|
|
131
|
+
tool: 'unknown',
|
|
132
|
+
status: 'pass' as const,
|
|
133
|
+
summary: 'command completed; no parser matched',
|
|
134
|
+
}
|
|
135
|
+
: await parser.parse(runCtx);
|
|
136
|
+
result = finalizeResult(parsed, exitCode, logs.logPath, cwd);
|
|
137
|
+
}
|
|
138
|
+
} catch (error) {
|
|
139
|
+
// Capture succeeded. Parsing failures must not hide its
|
|
140
|
+
// exit status or logs, nor reread an oversized log into the response.
|
|
141
|
+
result = {
|
|
142
|
+
...result,
|
|
143
|
+
tool: parserId,
|
|
144
|
+
summary: `could not parse command output: ${error instanceof Error ? error.message : String(error)}`,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const cancelled = captureCancelled || Boolean(signal?.aborted);
|
|
149
|
+
if (cancelled) {
|
|
150
|
+
result = {
|
|
151
|
+
tool: parserId,
|
|
152
|
+
status: 'error',
|
|
153
|
+
summary: 'command cancelled',
|
|
154
|
+
exitCode,
|
|
155
|
+
cwd,
|
|
156
|
+
logPath: logs.logPath,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
144
159
|
const resultText = formatResult(result);
|
|
145
160
|
|
|
146
|
-
const
|
|
147
|
-
const parsedBytes = resultText.length;
|
|
161
|
+
const parsedBytes = Buffer.byteLength(resultText, 'utf8');
|
|
148
162
|
try {
|
|
149
163
|
appendRun({
|
|
150
164
|
ts: new Date().toISOString(),
|
|
151
165
|
session: ctx.sessionManager?.getSessionFile?.() ?? undefined,
|
|
152
|
-
parser:
|
|
166
|
+
parser: parserId,
|
|
153
167
|
rawBytes,
|
|
154
168
|
parsedBytes,
|
|
155
169
|
command: stripCdPrefix(args.command),
|
|
@@ -163,7 +177,14 @@ export default function structuredReturn(pi: ExtensionAPI) {
|
|
|
163
177
|
content: [
|
|
164
178
|
{ type: 'text' as const, text: `${stripCdPrefix(args.command)} → ${resultText}` },
|
|
165
179
|
],
|
|
166
|
-
details: {
|
|
180
|
+
details: {
|
|
181
|
+
exitCode,
|
|
182
|
+
...logs,
|
|
183
|
+
parser: parserId,
|
|
184
|
+
rawBytes,
|
|
185
|
+
parsedBytes,
|
|
186
|
+
cancelled,
|
|
187
|
+
},
|
|
167
188
|
};
|
|
168
189
|
},
|
|
169
190
|
renderCall(args: ObservedRunArgs) {
|
|
@@ -196,17 +217,24 @@ export function formatResult(result: ParsedResult): string {
|
|
|
196
217
|
) {
|
|
197
218
|
if (result.logPath) lines.push(`log: ${result.logPath}`);
|
|
198
219
|
if (result.rawTail) lines.push(result.rawTail);
|
|
220
|
+
} else if (result.tool === 'dbt' && result.rawTail) {
|
|
221
|
+
// DBT returns compiled SQL (and test diffs) here even when diagnostics exist.
|
|
222
|
+
lines.push(result.rawTail);
|
|
199
223
|
}
|
|
200
224
|
return lines.join('\n');
|
|
201
225
|
}
|
|
202
226
|
|
|
203
227
|
/** Resolve and glob-expand artifact paths. Patterns that match nothing are kept as-is (parser handles missing files). */
|
|
204
228
|
export function expandArtifactPaths(raw: string[], cwd: string): string[] {
|
|
205
|
-
return
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
229
|
+
return [
|
|
230
|
+
...new Set(
|
|
231
|
+
raw.flatMap((p) => {
|
|
232
|
+
const resolved = path.isAbsolute(p) ? p : path.join(cwd, p);
|
|
233
|
+
const expanded = globSync(resolved);
|
|
234
|
+
return expanded.length > 0 ? expanded.sort() : [resolved];
|
|
235
|
+
}),
|
|
236
|
+
),
|
|
237
|
+
];
|
|
210
238
|
}
|
|
211
239
|
|
|
212
240
|
export function stripCdPrefix(command: string): string {
|
|
@@ -219,31 +247,18 @@ function shellSplit(command: string): string[] {
|
|
|
219
247
|
);
|
|
220
248
|
}
|
|
221
249
|
|
|
222
|
-
function runCommand(
|
|
223
|
-
command: string,
|
|
224
|
-
cwd: string,
|
|
225
|
-
): Promise<{ stdout: string; stderr: string; exitCode: number }> {
|
|
226
|
-
return new Promise((resolve) => {
|
|
227
|
-
const proc = spawn(command, { cwd, shell: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
228
|
-
let stdout = '';
|
|
229
|
-
let stderr = '';
|
|
230
|
-
proc.stdout.on('data', (d) => {
|
|
231
|
-
stdout += d.toString();
|
|
232
|
-
});
|
|
233
|
-
proc.stderr.on('data', (d) => {
|
|
234
|
-
stderr += d.toString();
|
|
235
|
-
});
|
|
236
|
-
proc.on('close', (code) => resolve({ stdout, stderr, exitCode: code ?? 1 }));
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
|
|
240
250
|
export function finalizeResult(
|
|
241
251
|
result: Omit<ParsedResult, 'exitCode'>,
|
|
242
252
|
exitCode: number,
|
|
243
253
|
logPath: string,
|
|
244
254
|
cwd: string,
|
|
245
255
|
): ParsedResult {
|
|
246
|
-
if (
|
|
256
|
+
if (
|
|
257
|
+
result.tool === 'unknown' &&
|
|
258
|
+
result.status === 'error' &&
|
|
259
|
+
result.summary === 'no parser matched; returning tail + log path' &&
|
|
260
|
+
exitCode === 0
|
|
261
|
+
) {
|
|
247
262
|
return {
|
|
248
263
|
...result,
|
|
249
264
|
exitCode,
|
|
@@ -257,12 +272,14 @@ export function finalizeResult(
|
|
|
257
272
|
};
|
|
258
273
|
}
|
|
259
274
|
const finalized: ParsedResult = { ...result, exitCode, cwd, logPath };
|
|
275
|
+
if (exitCode !== 0 && finalized.status === 'pass') {
|
|
276
|
+
finalized.status = 'error';
|
|
277
|
+
finalized.summary = `command exited ${exitCode}; ${finalized.summary}`;
|
|
278
|
+
}
|
|
260
279
|
// Surface catastrophic failures (command not found, permission denied, missing
|
|
261
280
|
// interpreter, etc.) — the combined log contains the actual diagnostic.
|
|
262
|
-
if (finalized.status === 'error' &&
|
|
263
|
-
|
|
264
|
-
const lines = log.split(/\r?\n/);
|
|
265
|
-
finalized.rawTail = lines.slice(-200).join('\n');
|
|
281
|
+
if (finalized.status === 'error' && !finalized.rawTail) {
|
|
282
|
+
finalized.rawTail = readTail(logPath);
|
|
266
283
|
}
|
|
267
284
|
// Safety net: if the parser reports failures but couldn't extract any details
|
|
268
285
|
// (e.g., tool output format changed), append the log tail so the model isn't
|
|
@@ -272,9 +289,7 @@ export function finalizeResult(
|
|
|
272
289
|
(finalized.failures ?? []).length === 0 &&
|
|
273
290
|
!finalized.rawTail
|
|
274
291
|
) {
|
|
275
|
-
|
|
276
|
-
const lines = log.split(/\r?\n/);
|
|
277
|
-
finalized.rawTail = lines.slice(-200).join('\n');
|
|
292
|
+
finalized.rawTail = readTail(logPath);
|
|
278
293
|
}
|
|
279
294
|
return finalized;
|
|
280
295
|
}
|