From e9d1378010a477582f99f0caaedddb291a4c01fe Mon Sep 17 00:00:00 2001 From: EthanHuo Date: Wed, 4 Nov 2026 19:40:37 +0800 Subject: [PATCH] feat: refuse agent pipes on help, --schema and @skill (0.3.3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gkit parses its own argv, so argc 7.12's output-capture guard is applied in cli.ts: an agent piping the guide into head/grep/$(…) gets OUTPUT_CAPTURED on stderr with runnable rewrites. --- packages/gkit/package.json | 2 +- packages/gkit/src/capture.test.ts | 32 ++++++++++++++++++++++++++++ packages/gkit/src/cli.ts | 35 +++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 packages/gkit/src/capture.test.ts diff --git a/packages/gkit/package.json b/packages/gkit/package.json index dd23942..f1fb2ef 100644 --- a/packages/gkit/package.json +++ b/packages/gkit/package.json @@ -1,6 +1,6 @@ { "name": "gkit", - "version": "0.3.2", + "version": "0.3.3", "private": true, "description": "Profile-bound, agent-first growth provider CLI.", "bin": { diff --git a/packages/gkit/src/capture.test.ts b/packages/gkit/src/capture.test.ts new file mode 100644 index 0000000..5b5a22b --- /dev/null +++ b/packages/gkit/src/capture.test.ts @@ -0,0 +1,32 @@ +import { spawnSync } from "node:child_process"; +import { fileURLToPath } from "node:url"; + +import { describe, expect, it } from "vitest"; + +const entry = fileURLToPath(new URL("../bin/gkit.js", import.meta.url)); + +// Real pipes: spawnSync hands the child a piped stdout, the shape of `gkit @skill | head`. +function run(args: string[], env: Record) { + return spawnSync("bun", [entry, ...args], { + env: { PATH: process.env.PATH, HOME: process.env.HOME, ...env }, + stdio: ["ignore", "pipe", "pipe"], + encoding: "utf8", + }); +} + +describe("output-capture guard", () => { + it("refuses an agent piping the guide, schema and help", () => { + for (const args of [["@skill"], ["--schema"], ["--help"]]) { + const r = run(args, { CLAUDE_CODE_SESSION_ID: "t" }); + expect(r.status).toBe(1); + expect(r.stdout).toBe(""); + expect(r.stderr).toContain("OUTPUT_CAPTURED"); + } + }); + + it("lets a person, the opt-out and data commands through", () => { + expect(run(["@skill"], {}).stdout).not.toBe(""); + expect(run(["@skill"], { CLAUDE_CODE_SESSION_ID: "t", ARGC_ALLOW_CAPTURE: "1" }).status).toBe(0); + expect(run(["docs"], { CLAUDE_CODE_SESSION_ID: "t" }).status).toBe(0); + }); +}); diff --git a/packages/gkit/src/cli.ts b/packages/gkit/src/cli.ts index c4b5409..a383f11 100644 --- a/packages/gkit/src/cli.ts +++ b/packages/gkit/src/cli.ts @@ -1,5 +1,6 @@ import { fileURLToPath } from "node:url"; +import { isAgentCapture } from "@celados/argc"; import { ensureTrailingNewline, formatBareSkill } from "@celados/argc/skill"; import { parseArgs, renderHelp } from "./args"; @@ -137,6 +138,10 @@ export async function main( try { const command = parseArgs(argv); + if (refuseCapturedGuide(command, argv)) { + process.exitCode = 1; + return; + } if (command.kind === "help") { await emitter.writeText(renderHelp()); process.exitCode = abortController.signal.aborted ? 130 : 0; @@ -429,6 +434,36 @@ export async function main( } } +/** + * gkit parses its own argv, so argc's output-capture guard (argc + * docs/spec-7.12-output-capture.md) is applied here: help, --schema and @skill + * must be read whole, and an agent piping them into head/grep/$(…) is refused + * before anything prints. Written to stderr, since stdout is the filter. + */ +function refuseCapturedGuide(command: ReturnType, argv: string[]): boolean { + if (command.kind !== "help" && command.kind !== "schema" && command.kind !== "skill") return false; + if (!isAgentCapture()) return false; + const line = ["gkit", ...argv.map(quoteWord)].join(" "); + const out = ["gkit", command.kind].join("-"); + process.stderr.write( + [ + `error: OUTPUT_CAPTURED: gkit ${command.kind === "skill" ? "@skill" : command.kind === "schema" ? "--schema" : "help"} is meant to be read whole, and stdout is a pipe or $(…): head/tail/grep/sed/rg would hand you a fragment that reads like the whole. Nothing ran.`, + "run it alone — the shell tool shows all of it:", + ` ${line}`, + "or keep it whole in a file, then read the whole file:", + ` ${line} > "\${TMPDIR:-/tmp}/${out}.out"`, + "", + ].join("\n"), + ); + return true; +} + +function quoteWord(word: string): string { + if (/^[A-Za-z0-9_@%+:,./-][A-Za-z0-9_@%+=:,./-]*$/.test(word)) return word; + if (!/["$`\\!]/.test(word)) return `"${word}"`; + return `'${word.replaceAll("'", `'\\''`)}'`; +} + async function renderEmbeddedSkill( path: string | null, emitter: TerminalEmitter,