Files
gbrain/src/commands/self-upgrade.ts
Garry Tan 3464179ce9 v0.46.35.0 fix: wave-k maintainer train — 31 red-proven fixes (5 fix-wave-j salvages, 11 verified issues, 15 credited reimplementations) (#4650)
Wave K's maintainer train: 31 red-proven fixes — 5 salvaged from fix-wave-j (#4363), 11 newly verified open issues, and 15 community PRs reimplemented with credit. Every fix carries a regression test proven RED at its base commit and passed an independent fresh-context review (32/32 approved). Retargeted across the megawave, wave-g, backup-coverage, and db-availability trains with zero suite regressions vs a same-sandbox master comparator; verify 54/54; PR CI 28/28 green.

Behavior change: `gbrain config get` now redacts secrets by default (--raw opts out) — see skills/migrations/v0.46.35.0.md. Migration v143 (dream_verdicts_ttl).

Full-E2E note: PR CI ran Tier 1+2; the nightly full-suite validates this head post-merge (merge authorized by maintainer).

Closes #3910, #4041, #3943, #4107, #4108, #4365, #4385, #4366, #4373, #4392, #4394, #4412, #4364, #4375, #4376, #4380. Completes #4363.

Co-authored-by: Aditya Vikram Singh <247195684+avs-io@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: DarkNightForge <DarkNightForge@users.noreply.github.com>
Co-authored-by: DarkNightForge <eashanbabber@gmail.com>
Co-authored-by: Dr Deeb <drdeebtech@users.noreply.github.com>
Co-authored-by: Kkkkkuro <180382840+Kkkkkuro@users.noreply.github.com>
Co-authored-by: francisco guzman <49572136+Crackde@users.noreply.github.com>
Co-authored-by: harshsahrawat-commits <233312378+harshsahrawat-commits@users.noreply.github.com>
Co-authored-by: jared-voss <262417861+jared-voss@users.noreply.github.com>
Co-authored-by: kyle944 <297432264+kyle944@users.noreply.github.com>
Co-authored-by: leihaozhou <leihao_zhou@qingsongchou.com>
Co-authored-by: miroslavb <31091373+miroslavb@users.noreply.github.com>
Co-authored-by: y2688 <172344182+y2688@users.noreply.github.com>
2026-08-27 13:09:55 -07:00

108 lines
4.0 KiB
TypeScript

import { VERSION } from '../version.ts';
import { isNewerVersion, isValidVersionString } from '../core/semver.ts';
import { fetchChangelog, fetchLatestRelease } from './check-update.ts';
import { detectInstallMethod, runUpgrade } from './upgrade.ts';
import { writeUpdateCache } from '../core/self-upgrade.ts';
/**
* `gbrain self-upgrade [--check-only] [--force] [--json]`
*
* The universal substrate every agent ecosystem (Codex / Claude Code / Hermes /
* OpenClaw / Perplexity-server) can call to stay current. The CLI startup hook
* emits a marker; the agent skill / autopilot daemon act on it by running THIS
* command. The action is always the hardcoded `gbrain upgrade` — never
* parameterized by any marker content (forged-marker guard).
*
* --check-only Report whether an upgrade is available; never apply.
* --force Apply even if not behind (re-run the install-method swap).
* --json Machine-readable output for the check.
*/
export async function runSelfUpgrade(args: string[]): Promise<void> {
if (args.includes('--help') || args.includes('-h')) {
console.log(
'Usage: gbrain self-upgrade [--check-only] [--force] [--json]\n\n' +
'Check for and apply gbrain updates. The shared entry point used by the\n' +
'CLI startup marker, the gbrain-upgrade agent skill, and the autopilot\n' +
'silent channel.\n\n' +
' --check-only Report whether an upgrade is available; do not apply.\n' +
' --force Apply even when not behind.\n' +
' --json Machine-readable output (with --check-only).',
);
return;
}
const checkOnly = args.includes('--check-only');
const force = args.includes('--force');
const json = args.includes('--json');
const result = await fetchLatestRelease();
const release = result.ok ? result : null;
const latest = release ? release.tag.replace(/^v/, '') : null;
const behind = !!latest && isValidVersionString(latest) && isNewerVersion(VERSION, latest);
// Warm the cache so the next invocation's startup hook can emit without a fetch.
try {
if (latest && isValidVersionString(latest)) {
writeUpdateCache(
behind
? { kind: 'upgrade_available', current: VERSION, latest }
: { kind: 'up_to_date', current: VERSION },
);
}
} catch {
/* best-effort */
}
if (checkOnly) {
// Tell the operator WHAT they'd get: fetch the changelog only when actually
// behind (so an up-to-date check stays a single release fetch). The agent
// skill surfaces these "what's new" bullets in the notify prompt.
let changelogDiff = '';
if (behind && latest) {
try {
changelogDiff = await fetchChangelog(VERSION, latest);
} catch {
/* best-effort: an unavailable changelog must not block the check */
}
}
if (json) {
console.log(
JSON.stringify(
{
current_version: VERSION,
latest_version: latest ?? '',
update_available: behind,
install_method: detectInstallMethod(),
release_url: release?.url ?? '',
changelog_diff: changelogDiff,
},
null,
2,
),
);
} else if (behind) {
console.log(`Update available: ${VERSION} -> ${latest}. Run: gbrain self-upgrade`);
if (changelogDiff) {
console.log('\nWhat changed:\n');
console.log(changelogDiff);
}
if (release?.url) console.log(`\nRelease: ${release.url}`);
} else {
console.log(`gbrain ${VERSION} is up to date.`);
}
return;
}
if (!behind && !force) {
console.log(`gbrain ${VERSION} is up to date.`);
return;
}
// Apply: delegate to the hardcoded upgrade path (full swap + post-upgrade).
// Pass the fetched target so runUpgrade can detect exact-tag no-op
// "upgrades" instead of reporting false success (#4366).
await runUpgrade([], {
targetVersion: latest && isValidVersionString(latest) ? latest : undefined,
});
}