mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-28 10:54:30 +01:00
fix(capy): a lease records its holder's start identity, so a pid reused after a wake is reclaimed; a skipped session-start upgrade ends fail behind, never ok (GBRA-105, GBRA-106)
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
d7a650fcea
commit
7fa9a1bdcf
10 files changed
+162
-35
No files matched your search
@@ -14,7 +14,7 @@
|
||||
* depth-1 file:// clone at A, on the simulated Capy host.
|
||||
*/
|
||||
import { afterEach, describe, expect, test } from 'bun:test';
|
||||
import { spawnSync } from 'child_process';
|
||||
import { spawn, spawnSync } from 'child_process';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { RESULT_CODES } from '../lib/result-codes';
|
||||
@@ -311,7 +311,7 @@ describe('capy skill bootstrapper: GSTACK_CAPY_REMOTE_VERSION', () => {
|
||||
// Field list 2 item 13: lanes relay only the trailer, so a session that ends
|
||||
// behind origin hands the installer the gap and the fix for its trailer note.
|
||||
describe('capy skill: the session hands the installer GSTACK_CAPY_BEHIND', () => {
|
||||
const ENV_INSTALLER = '#!/usr/bin/env bash\necho "gstack: ok $(cat "$(dirname "$0")/../VERSION") for=autoplan behind=${GSTACK_CAPY_BEHIND:-none} to=${GSTACK_CAPY_BEHIND_TO:-none} fix=${GSTACK_CAPY_BEHIND_FIX:-none}"\n';
|
||||
const ENV_INSTALLER = '#!/usr/bin/env bash\necho "gstack: ok $(cat "$(dirname "$0")/../VERSION") for=autoplan behind=${GSTACK_CAPY_BEHIND:-none} to=${GSTACK_CAPY_BEHIND_TO:-none} fix=${GSTACK_CAPY_BEHIND_FIX:-none}${GSTACK_CAPY_BEHIND_BLOCKED:+ blocked=$GSTACK_CAPY_BEHIND_BLOCKED}"\n';
|
||||
const rerun = `bash ${BOOT} --for autoplan`;
|
||||
|
||||
test('--check behind origin: the commit count, origin\'s VERSION and the fast-forwarding command; a checkout the rerun would not move points at gstack-upgrade', () => {
|
||||
@@ -339,13 +339,39 @@ describe('capy skill: the session hands the installer GSTACK_CAPY_BEHIND', () =>
|
||||
write(path.join(f.root, 'bin/gstack-capy-install'), ENV_INSTALLER, 0o755);
|
||||
const skipped = f.run(['--for', 'autoplan']);
|
||||
expect(skipped.stdout).toMatch(/^SELF_UPGRADE: skipped \(tracked files changed: bin\/gstack-capy-install; fix: .+\)$/m);
|
||||
expect(skipped.last).toBe(`gstack: ok 1.0.1 for=autoplan behind=1 to=1.0.2 fix=${f.root}/bin/gstack-upgrade`);
|
||||
expect(skipped.last).toMatch(new RegExp(`^gstack: ok 1\\.0\\.1 for=autoplan behind=1 to=1\\.0\\.2 fix=${f.root.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/bin/gstack-upgrade blocked=tracked files changed: bin/gstack-capy-install; fix: .+ \\(UPGRADE_REFUSED\\)$`));
|
||||
const disabled = f.run(['--for', 'autoplan'], { GSTACK_CAPY_SELF_UPGRADE: 'off' });
|
||||
expect(disabled.last).toBe(`gstack: ok 1.0.1 for=autoplan behind=1 to=1.0.2 fix=unset GSTACK_CAPY_SELF_UPGRADE, then ${rerun}`);
|
||||
for (const r of [skipped, disabled]) expect(r.last.split(' fix=')[1]).not.toContain('; ');
|
||||
for (const r of [skipped, disabled]) expect(r.last.split(' fix=')[1].split(' blocked=')[0]).not.toContain('; ');
|
||||
expect(f.git(['rev-parse', 'HEAD'])).toBe(f.b);
|
||||
});
|
||||
|
||||
// GBRA-105 / GBRA-106: a lease naming a pid that a machine wake handed to
|
||||
// another process wedged every upgrade, and the skipped upgrade still ended ok.
|
||||
test('install mode: a lease whose pid was reused is reclaimed and the checkout upgrades; a live holder blocks it and the session hands the installer the busy reason with the holder', () => {
|
||||
const f = fixture({ cloneAt: 'B' });
|
||||
write(path.join(f.root, 'bin/gstack-capy-install'), ENV_INSTALLER, 0o755);
|
||||
f.git(['update-index', '--assume-unchanged', 'bin/gstack-capy-install']);
|
||||
const leaseDir = path.join(f.host.base, 'leases');
|
||||
const holder = spawn('sleep', ['300'], { stdio: 'ignore' });
|
||||
try {
|
||||
const lease = (args: string[]) => spawnSync('bash', [path.join(f.root, 'bin/gstack-lease'), ...args, '--root', f.root], { encoding: 'utf8', env: { ...f.host.env, GSTACK_LEASE_DIR: leaseDir }, timeout: 30_000 });
|
||||
expect(lease(['touch', '--pid', String(holder.pid)]).status).toBe(0);
|
||||
const busy = f.run(['--for', 'autoplan'], { GSTACK_LEASE_DIR: leaseDir });
|
||||
expect(busy.stdout).toMatch(new RegExp(`^SELF_UPGRADE: skipped \\(live gstack sessions hold this installation: pid ${holder.pid} \\(\\d+m sleep\\); fix: .+ \\(UPGRADE_BUSY\\)\\)$`, 'm'));
|
||||
expect(busy.last).toMatch(new RegExp(`behind=1 to=1\\.0\\.2 fix=.+ blocked=live gstack sessions hold this installation: pid ${holder.pid} \\(\\d+m sleep\\); fix: .+GSTACK_UPGRADE_FORCE=1.+ \\(UPGRADE_BUSY\\)$`));
|
||||
expect(f.git(['rev-parse', 'HEAD'])).toBe(f.b);
|
||||
const file = path.join(leaseDir, String(holder.pid));
|
||||
fs.writeFileSync(file, fs.readFileSync(file, 'utf8').trim().replace(/\S+$/, 'x1') + '\n');
|
||||
const reclaimed = f.run(['--for', 'autoplan'], { GSTACK_LEASE_DIR: leaseDir });
|
||||
expect(reclaimed.stdout).toMatch(/^SELF_UPGRADE: done 1\.0\.1 -> 1\.0\.2$/m);
|
||||
expect(f.git(['rev-parse', 'HEAD'])).toBe(f.c);
|
||||
expect(fs.existsSync(file)).toBe(false);
|
||||
} finally {
|
||||
holder.kill('SIGKILL');
|
||||
}
|
||||
});
|
||||
|
||||
test('current, or the fetch failed: nothing is exported, and values inherited from a parent session are dropped', () => {
|
||||
const f = fixture({ cloneAt: 'B' });
|
||||
f.git(['fetch', '-q', 'origin', 'main']);
|
||||
|
||||
@@ -279,6 +279,17 @@ describe('gstack-capy-install', () => {
|
||||
expect(lastLine(run(f, [], { ...behind, STUB_DOCTOR_FAIL: '1' }).stdout)).toBe('gstack: fail codex');
|
||||
});
|
||||
|
||||
test('a skipped session-start upgrade is never ok: the trailer is `fail behind` with the reason, the fix and the code, exit 1, and the verdict records the failure', () => {
|
||||
const f = fixture();
|
||||
const busy = { GSTACK_CAPY_BEHIND: '3', GSTACK_CAPY_BEHIND_TO: '1.2.6', GSTACK_CAPY_BEHIND_FIX: `${f.root}/bin/gstack-upgrade`, GSTACK_CAPY_BEHIND_BLOCKED: 'live gstack sessions hold this installation: pid 379 (124m dbus-daemon); fix: wait for those sessions to finish, or re-run with GSTACK_UPGRADE_FORCE=1 when you know they are idle (UPGRADE_BUSY)' };
|
||||
const r = run(f, [], busy);
|
||||
expect(r.status, r.stdout + r.stderr).toBe(1);
|
||||
expect(lastLine(r.stdout)).toBe('gstack: fail behind — installed 1.2.3 is 3 commits behind origin (latest 1.2.6) and the session-start upgrade was skipped: live gstack sessions hold this installation: pid 379 (124m dbus-daemon); fix: wait for those sessions to finish, or re-run with GSTACK_UPGRADE_FORCE=1 when you know they are idle (UPGRADE_BUSY)');
|
||||
const bare = run(f, [], { ...busy, GSTACK_CAPY_BEHIND_BLOCKED: 'gstack-upgrade exit 9' });
|
||||
expect(lastLine(bare.stdout)).toBe(`gstack: fail behind — installed 1.2.3 is 3 commits behind origin (latest 1.2.6) and the session-start upgrade was skipped: gstack-upgrade exit 9; fix: ${f.root}/bin/gstack-upgrade (UPGRADE_REFUSED)`);
|
||||
expect(lastLine(run(f, [], { ...busy, STUB_DOCTOR_FAIL: '1' }).stdout)).toBe('gstack: fail codex');
|
||||
});
|
||||
|
||||
test('--root clones when the directory is absent and refuses a non-gstack directory', () => {
|
||||
const f = fixture();
|
||||
const clone = path.join(f.base, 'clone');
|
||||
|
||||
@@ -86,7 +86,7 @@ describe('gstack-lease', () => {
|
||||
expect(run(['touch', '--root', dir, '--pid', String(live)]).status).toBe(0);
|
||||
const some = run(['live', '--root', dir]);
|
||||
expect(some.status, some.stdout + some.stderr).toBe(0);
|
||||
expect(some.stdout.trim().split('\n')).toEqual([expect.stringMatching(new RegExp(`^${live} \\d+m$`))]);
|
||||
expect(some.stdout.trim().split('\n')).toEqual([expect.stringMatching(new RegExp(`^${live} \\d+m sleep$`))]);
|
||||
|
||||
const list = run(['list', '--root', dir]);
|
||||
expect(list.status).toBe(0);
|
||||
@@ -94,12 +94,42 @@ describe('gstack-lease', () => {
|
||||
expect(list.stdout).toMatch(new RegExp(`^${dead} \\d+m dead$`, 'm'));
|
||||
});
|
||||
|
||||
// GBRA-105 / GBRA-106: after a machine wake the recorded pid was dbus-daemon
|
||||
// and systemd-logind, and every upgrade refused with UPGRADE_BUSY.
|
||||
test('a reused pid is not a holder: a lease whose recorded start identity differs is reclaimed by live and listed stale', () => {
|
||||
const { dir, leases } = checkout();
|
||||
const reused = sleeper();
|
||||
expect(run(['touch', '--root', dir, '--pid', String(reused)]).status).toBe(0);
|
||||
const recorded = fs.readFileSync(path.join(leases, String(reused)), 'utf8').trim();
|
||||
expect(recorded).toMatch(/^(linux [0-9a-f-]+ \d+|ps .+)$/);
|
||||
fs.writeFileSync(path.join(leases, String(reused)), recorded.replace(/\S+$/, 'x1') + '\n');
|
||||
expect(run(['list', '--root', dir]).stdout).toMatch(new RegExp(`^${reused} \\d+m stale$`, 'm'));
|
||||
const live = run(['live', '--root', dir]);
|
||||
expect(live.status, live.stdout + live.stderr).toBe(1);
|
||||
expect(live.stdout.trim()).toBe('');
|
||||
expect(live.stderr).toMatch(new RegExp(`^gstack-lease: reclaimed stale lease ${reused} \\(\\d+m; recorded '.+', pid ${reused} is now sleep '.+'\\)$`, 'm'));
|
||||
expect(fs.existsSync(path.join(leases, String(reused)))).toBe(false);
|
||||
});
|
||||
|
||||
test('a lease without a recorded identity (older gstack) counts while fresh and is stale past the 120-minute window', () => {
|
||||
const { dir, leases } = checkout();
|
||||
const holder = sleeper();
|
||||
fs.mkdirSync(leases, { recursive: true });
|
||||
fs.writeFileSync(path.join(leases, String(holder)), '');
|
||||
expect(run(['live', '--root', dir]).status).toBe(0);
|
||||
const old = new Date(Date.now() - 130 * 60_000);
|
||||
fs.utimesSync(path.join(leases, String(holder)), old, old);
|
||||
const r = run(['live', '--root', dir]);
|
||||
expect(r.status, r.stdout + r.stderr).toBe(1);
|
||||
expect(r.stderr).toContain(`reclaimed stale lease ${holder}`);
|
||||
});
|
||||
|
||||
test('live --exclude-ancestry-of <pid> drops a lease held by that pid or one of its ancestors (this test process)', () => {
|
||||
const { dir } = checkout();
|
||||
expect(run(['touch', '--root', dir, '--pid', String(process.pid)]).status).toBe(0);
|
||||
const unfiltered = run(['live', '--root', dir]);
|
||||
expect(unfiltered.status, unfiltered.stdout + unfiltered.stderr).toBe(0);
|
||||
expect(unfiltered.stdout).toMatch(new RegExp(`^${process.pid} \\d+m$`, 'm'));
|
||||
expect(unfiltered.stdout).toMatch(new RegExp(`^${process.pid} \\d+m \\S+$`, 'm'));
|
||||
// $$ inside the bin is a child of this bun process, so our own lease is ancestry.
|
||||
const own = spawnSync('bash', ['-c', `"$LEASE" live --root "$ROOT" --exclude-ancestry-of $$`], {
|
||||
encoding: 'utf8', env: { ...baseEnv, LEASE, ROOT: dir }, timeout: 30_000,
|
||||
@@ -112,7 +142,7 @@ describe('gstack-lease', () => {
|
||||
encoding: 'utf8', env: { ...baseEnv, LEASE, ROOT: dir }, timeout: 30_000,
|
||||
});
|
||||
expect(mixed.status).toBe(0);
|
||||
expect(mixed.stdout.trim().split('\n')).toEqual([expect.stringMatching(new RegExp(`^${foreign} \\d+m$`))]);
|
||||
expect(mixed.stdout.trim().split('\n')).toEqual([expect.stringMatching(new RegExp(`^${foreign} \\d+m sleep$`))]);
|
||||
});
|
||||
|
||||
test('touch without --pid uses the same harness pid gstack-skill-start derives (this process past the tool-call shell)', () => {
|
||||
|
||||
@@ -188,7 +188,7 @@ describe('gstack-upgrade', () => {
|
||||
expect(r.status, r.stdout + r.stderr).toBe(3);
|
||||
expect(r.stderr).toContain('(UPGRADE_BUSY)');
|
||||
expect(r.stderr).toContain('nothing was changed');
|
||||
expect(r.stderr).toMatch(new RegExp(`pid ${live} \\(\\d+m\\)`));
|
||||
expect(r.stderr).toMatch(new RegExp(`pid ${live} \\(\\d+m sleep\\)`));
|
||||
expect(r.stderr).not.toContain(`pid ${process.pid} `);
|
||||
expect(r.stderr).toContain('wait for those sessions to finish, or re-run with GSTACK_UPGRADE_FORCE=1 when you know they are idle');
|
||||
expect(f.git(['rev-parse', 'HEAD'])).toBe(f.a);
|
||||
|
||||
Reference in new issue
Block a user