On Capy, /ship hands its pull request edits and comments to Capy's PR tool, always naming the right repository (GST-3183) (#4518)

On Capy, /ship's PR body edits, comments and new PRs no longer go out
through `gh` under the machine owner's GitHub account.

Each write runs its checks, saves the exact text to a private file and
prints a `HOST_PUBLISH:` line that names the repository (and host, off
github.com) it belongs to, so Capy's PR tool sends it to the same place
`gh` would have.

Nothing changes outside Capy (`GSTACK_CAPY` unset).

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/specialist-batched#1 reader=specialist-batched
Review-Finding: p1/specialist-batched#3 reader=specialist-batched
Review-Finding: p1/red-team#6 reader=red-team
Review-Finding: p1/specialist-batched#1 reader=testing
Review-Finding: p1/specialist-batched#2 reader=testing
This commit is contained in:
Garry Tanandcapy-ai[bot] authored and GitHub committed 2026-11-19 11:30:53 +00:00
1 parent ed267248d3
commit e95cea8404
15 files changed
+479 -18

No files matched your search

+1
View File
@@ -199,6 +199,7 @@ function open(env: EarlyEnv, args: string[]): number {
env.out(created.stdout);
if (created.status !== 0) { env.err(created.stderr); return created.status; }
const url = /^https?:\/\/\S+\/pull\/(\d+)\s*$/m.exec(created.stdout);
if (!url && /^HOST_PUBLISH: /m.test(created.stdout)) { env.err("gstack-post handed the draft to the host's PR tool (HOST_PUBLISH above), so there is no PR to trigger yet; open it there or continue without early review.\n"); return 3; }
if (!url) { env.err("gstack-post did not print the new PR URL.\n"); return 3; }
env.out(`EARLY_PR: ${url[1]}\nEARLY_PR_URL: ${url[0].trim()}\nEARLY_PR_OPENED_AT: ${Math.floor(env.now() / 1000)}\n`);
if (ready) { env.out("GREPTILE_TRIGGER: not needed (ready PR; Greptile reviews it when opened)\n"); return 0; }
+25 -1
View File
@@ -54,8 +54,13 @@ export interface PostEnv {
readFile(path: string): string;
out(text: string): void;
err(text: string): void;
/** On Capy (GSTACK_CAPY=1): write the checked parts for the host's PR tool and return its HOST_PUBLISH: line; gh is never called to write (GST-3183). */
hostPublish?(op: Op, target: string | undefined, parts: Array<[Part, string]>, dest: HostDest): string;
}
/** Where the gh write would have gone, so the host's PR tool can send the hand-off to the same place. */
export interface HostDest { repo: string; host: string; to?: string; base?: string; head?: string; draft: boolean }
export const EXIT = { posted: 0, refused: 1, confirm: 2, cliFailed: 3, plainTitle: 4, usage: 64 } as const;
export const USAGE = `usage: gstack-post <op> [<target>] [options]
@@ -473,7 +478,7 @@ function visibility(env: PostEnv, req: Request, kind: HostKind, configBin: strin
try { return norm(String(JSON.parse(gl.stdout).visibility ?? "")) ?? "unknown"; } catch { return "unknown"; }
}
type Part = "title" | "body";
export type Part = "title" | "body";
interface Located { part: Part; finding: Finding }
/**
@@ -835,6 +840,25 @@ export function runPost(argv: string[], env: PostEnv, configBin = "gstack-config
return EXIT.confirm;
}
}
if (env.hostPublish) {
let head = req.head;
if (req.op === "pr-create" && !head) {
const b = env.run("git", ["branch", "--show-current"]);
head = b.status === 0 ? b.stdout.trim() : "";
if (!head) throw new UsageError("pr-create on Capy needs --head <branch> when HEAD is detached: the host's PR tool cannot infer it");
}
let dest = { repo: remote.segments.join("/"), host: remote.host };
if (remote.kind === "github") {
const view = env.run("gh", ["repo", "view", "--json", "nameWithOwner,url"]);
try { const v = JSON.parse(view.stdout); dest = { repo: String(v.nameWithOwner ?? ""), host: new URL(String(v.url)).host }; } catch { dest = { repo: "", host: "" }; }
if (view.status !== 0 || !dest.repo) { env.err(`gstack-post: cannot resolve the repository gh would have written to (gh repo view --json nameWithOwner,url: ${view.stderr.trim().split("\n")[0] || "no repository"}); nothing was handed off\n`); return EXIT.cliFailed; }
}
let line: string;
try { line = env.hostPublish(req.op, n, parts, { ...dest, to: req.to, base: req.base, head, draft: req.draft }); }
catch (e) { env.err(`gstack-post: could not write the hand-off for the host's PR tool: ${(e as Error).message}; nothing was handed off\n`); return EXIT.cliFailed; }
env.out(line);
return EXIT.posted;
}
return send(env, remote.kind, req, n, title, body);
} catch (e) {
if (!(e instanceof UsageError)) throw e;
+54
View File
@@ -0,0 +1,54 @@
/**
* host-publish — GitHub writes on Capy go through the host's PR tool, never gh (GST-3183).
*
* On Capy (GSTACK_CAPY=1) the machine's gh is signed in as the owner's personal GitHub account, so an edit, comment or
* new PR sent through gh lands under their name (GBEV-105, 2026-11-19: `gstack-ship-receipt stub` edited an outside
* system's PR body as the owner during an unattended /ship). Each /ship command that would write through gh asks
* hostPublishes() first; when it answers true the command writes the exact bytes it would have sent to a private file
* under the state root and prints the one HOST_PUBLISH: line handOff() returns, and the agent publishes that file with
* its host's PR tool (Capy's tools.pr.edit, tools.pr.comment or tools.pr.create). Reads through gh are unchanged.
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import { resolveStateRoot, type StateRootEnv } from './state-root';
export const hostPublishes = (env: StateRootEnv = process.env): boolean => env.GSTACK_CAPY === '1';
/** What the host's PR tool does with each operation's files, as the HOST_PUBLISH: line says it. */
const HOST_TOOL: Record<string, string> = {
'pr-body': "replace the PR's body with body-file (Capy: tools.pr.edit body)",
'pr-title': "set the PR's title to title-file (Capy: tools.pr.edit title)",
'pr-comment': 'post body-file as a PR comment (Capy: tools.pr.comment)',
'issue-comment': 'post body-file as a comment on that issue or PR (Capy: tools.pr.comment on a PR; an issue goes to whoever owns the work)',
reply: 'post body-file as a reply in that review thread (Capy: tools.pr.comment reply_to)',
'pr-create': 'open the PR from title-file and body-file (Capy: tools.pr.create)',
'issue-create': 'open the issue from title-file and body-file, or hand both files to whoever owns the work (Capy has no GitHub issue tool)',
};
/** A repository path the host tool can be pointed at: owner/name, or a GitLab group path. */
const REPO_PATH = /^[\w.-]+(\/[\w.-]+)+$/;
/**
* Write each part to a new private directory <state root>/publish/<op>-<target>-<random>/<part>.md (mode 0600), so no
* hand-off ever overwrites another one still waiting to be published (another repo's PR #7, a second pr-create), and return the HOST_PUBLISH: line naming them and the destination the gh write would have used
* (repo= always, host= unless github.com, reply's to=, pr-create's base=, head= and draft), with a newline. The host tool
* has nothing else to go on and would fall back to the thread's primary repository, so a hand-off without a resolved
* repo throws before any file is written.
*/
export function handOff(o: { op: string; target?: string; repo: string; host?: string; to?: string; base?: string; head?: string; draft?: boolean; parts: Array<[string, string]>; env?: StateRootEnv }): string {
if (!REPO_PATH.test(o.repo ?? '')) throw new Error(`HOST_PUBLISH needs the destination repository (owner/name), got ${JSON.stringify(o.repo ?? '')}`);
if (o.host !== undefined && !/^[\w.-]+(:\d+)?$/.test(o.host)) throw new Error(`HOST_PUBLISH host ${JSON.stringify(o.host)} is not a hostname`);
const host = o.host && o.host.toLowerCase() !== 'github.com' ? o.host.toLowerCase() : undefined;
const dir = path.join(resolveStateRoot(o.env ?? process.env), 'publish');
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
const own = fs.mkdtempSync(path.join(dir, `${o.op}-${(o.target ?? 'new').replace(/[^\w.-]/g, '_')}-`));
fs.chmodSync(own, 0o700);
const files = o.parts.map(([part, text]) => {
const file = path.join(own, `${part}.md`);
fs.writeFileSync(file, text, { mode: 0o600 });
fs.chmodSync(file, 0o600);
return `${part}-file=${file}`;
});
const dest = [`repo=${o.repo}`, host && `host=${host}`, o.to && `to=${o.to}`, o.base && `base=${o.base}`, o.head && `head=${o.head}`, o.draft && 'draft'].filter(Boolean).map((d) => ` ${d}`).join('');
return `HOST_PUBLISH: ${o.op} ${o.target ?? 'new'}${dest} ${files.join(' ')} (gh was not called: on Capy it signs in as the owner's own GitHub account; ${HOST_TOOL[o.op] ?? 'publish these files with the host PR tool'}, before the next gstack step; docs/troubleshooting.md#host-publish)\n`;
}