Files
ghbot/src/github/runtimeInfo.ts
T
lezi-fun 9e47ab339e feat: sync ghbot-Nodeloc v1.2.0 capabilities into upstream
Merges all 33 commits from lza6/ghbot-Nodeloc (v1.0.0 through v1.2.0)
while keeping both sides' features in every conflicted file:

- EventRouter dispatch extracted from runReview.ts (7.1), with the
  runtime environment block preserved on the review-start comment
- Unified command ack messages (6.3) combined with the collapsed
  runtime environment block and bot signature on /recheck replies
- Status banner and collapsible unposted findings in review bodies (6.1)
- Centralized runtime directories behind GHBOT_RUNTIME_DIR (D-13);
  webhook knowledge cache now lives under tempRootDirectory()
- Production hardening for merge races, timeouts, webhook ops;
  atomic temp-file rename for R2 cache restores kept from upstream
- Webhook parity retained: triage events gated by WEBHOOK_TRIAGE_ENABLED,
  read-only command guidance, repository knowledge injection,
  runtime environment block on webhook chat replies
- Sanitization/secret redaction centralization, error-aware retry,
  duplicate merge prevention, in-process webhook retries, /metrics
- Infra: eslint/prettier gates with CI verify workflow, docs archives

typecheck, eslint, prettier format:check, and 164 tests all pass.
2026-08-24 11:47:28 +08:00

106 lines
3.4 KiB
TypeScript

import { config } from "../config.js";
export type RuntimeMode = "actions" | "webhook" | "local";
/**
* Where is this ghbot process running? Action-mode workflow runs set
* GITHUB_ACTIONS; the optional webhook service opts in via WEBHOOK_ENABLED.
* Anything else is a local simulation.
*/
export function detectRuntimeMode(): RuntimeMode {
if (process.env.GITHUB_ACTIONS === "true") {
return "actions";
}
return config.webhookEnabled ? "webhook" : "local";
}
function gooseVersion(): string | undefined {
const version = process.env.GOOSE_VERSION?.trim();
return version ? version : undefined;
}
function hostLine(): string {
const os = process.env.RUNNER_OS ?? process.platform;
return `${os} / ${process.arch} · Node.js ${process.version}`;
}
function eventLine(): string | undefined {
const eventName = process.env.GHBOT_EVENT_NAME ?? process.env.GITHUB_EVENT_NAME;
const eventAction = process.env.GHBOT_EVENT_ACTION;
if (!eventName) {
return undefined;
}
return eventAction ? `\`${eventName}\` / ${eventAction}` : `\`${eventName}\``;
}
function r2CacheEnabled(): boolean {
return Boolean(
config.r2Endpoint && config.r2BucketName && config.r2AccessKeyId && config.r2SecretAccessKey
);
}
/**
* Collapsed <details> block appended to comments in which the bot announces or
* performs an operation. The visual template is identical for every runtime
* mode (Actions, webhook service, local); only the fact lines differ. It must
* never contain credentials: only non-secret configuration and host facts.
*/
export function formatRuntimeEnvironmentDetails(mode: RuntimeMode = detectRuntimeMode()): string {
const lines: string[] = [];
if (mode === "webhook") {
lines.push(
"- **Mode:** GitHub App webhook service (read-only chat)",
`- **Host:** ${hostLine()}`
);
} else {
const event = eventLine();
const modeLabel =
mode === "actions"
? event
? `GitHub Actions (${event})`
: "GitHub Actions"
: event
? `local run (${event})`
: "local run";
lines.push(`- **Mode:** ${modeLabel}`, `- **Runner:** ${hostLine()}`);
}
const gooseParts = [
gooseVersion(),
`model \`${config.gooseModel ?? "unknown"}\``,
`thinking effort \`${config.gooseThinkingEffort ?? "high"}\``
].filter(Boolean);
lines.push(`- **goose:** ${gooseParts.join(" · ")}`);
if (mode === "webhook") {
lines.push(
`- **Chat permission policy:** \`${config.webhookChatPermission}\``,
"- **Tools:** none · cannot edit files, run commands, or push commits"
);
} else {
lines.push(
`- **Review policy:** \`${config.reviewPolicy}\` · strictness \`${config.reviewStrictness}\` · max patch ${config.maxPatchChars} chars`,
`- **Automation:** auto-merge ${config.autoMerge ? "on" : "off"} · conflict repair ${config.autoResolveConflicts ? "on" : "off"}`,
`- **Cache:** R2 ${r2CacheEnabled() ? "enabled" : "disabled"} · repository knowledge ${config.repositoryKnowledgeEnabled ? "enabled" : "disabled"}`
);
}
return [
"<details>",
"<summary>⚙️ Runtime environment</summary>",
"",
...lines,
"",
"</details>"
].join("\n");
}
/**
* Attribution footer appended after the environment block on the comments in
* which the bot announces or performs an operation.
*/
export function formatBotSignature(): string {
return "🤖 Created By [**GHBot**](https://github.com/lezi-fun/ghbot)";
}