Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/gkit/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
32 changes: 32 additions & 0 deletions packages/gkit/src/capture.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string | undefined>) {
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);
});
});
35 changes: 35 additions & 0 deletions packages/gkit/src/cli.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<typeof parseArgs>, 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,
Expand Down
Loading