@imdeadpool/guardex 7.1.0 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +122 -322
- package/package.json +25 -5
- package/scripts/prepublish-bump-version.js +191 -0
- package/skills/gitguardex/SKILL.md +83 -3
- package/src/agents/activity.js +279 -0
- package/src/agents/finish.js +15 -0
- package/src/ci-init/index.js +0 -1
- package/src/cli/args.js +203 -6
- package/src/cli/commands/agents.js +17 -0
- package/src/cli/commands/branch.js +267 -14
- package/src/cli/commands/claude.js +15 -0
- package/src/cli/commands/doctor.js +26 -0
- package/src/cli/commands/onboard.js +293 -0
- package/src/cli/commands/prompt.js +15 -3
- package/src/cli/commands/review.js +18 -0
- package/src/cli/commands/setup.js +8 -6
- package/src/cli/commands/status.js +12 -0
- package/src/cli/main.js +2 -0
- package/src/cli/shared-locks.js +304 -0
- package/src/context.js +21 -3
- package/src/core/runtime.js +33 -1
- package/src/doctor/index.js +58 -5
- package/src/finish/heartbeat.js +169 -0
- package/src/finish/index.js +106 -4
- package/src/finish/progress.js +262 -0
- package/src/finish/review-gate.js +453 -32
- package/src/github-api.js +59 -0
- package/src/mcp/collect.js +111 -12
- package/src/mcp/server.js +10 -4
- package/src/output/index.js +210 -0
- package/src/pr-checks.js +121 -0
- package/src/pr-review.js +581 -51
- package/src/pr.js +143 -21
- package/src/provider-binary.js +101 -0
- package/src/review-diff.js +116 -0
- package/src/review-fix.js +229 -0
- package/src/sandbox/index.js +11 -5
- package/src/scaffold/agent-worktree-prep.js +9 -2
- package/src/scaffold/index.js +4 -16
- package/src/scaffold/provision-config.js +314 -0
- package/src/shared-git-state.js +566 -0
- package/src/terminal/kitty.js +20 -0
- package/src/terminal/tmux.js +9 -0
- package/templates/AGENTS.multiagent-safety.md +73 -11
- package/templates/AGENTS.multiagent-safety.min.md +1 -0
- package/templates/codex/skills/gitguardex/SKILL.md +83 -3
- package/templates/githooks/post-checkout +4 -9
- package/templates/githooks/pre-commit +17 -1
- package/templates/github/workflows/README.md +30 -23
- package/templates/scripts/agent-autofinish-watch.sh +280 -0
- package/templates/scripts/agent-branch-finish.sh +363 -26
- package/templates/scripts/agent-branch-start.sh +76 -25
- package/templates/scripts/agent-claude-stop-finish.sh +194 -0
- package/templates/scripts/agent-file-locks.py +390 -52
- package/templates/scripts/agent-preflight.sh +14 -2
- package/templates/scripts/agent-stalled-report.sh +40 -0
- package/templates/scripts/codex-agent.sh +10 -63
- package/templates/github/workflows/cr.yml +0 -46
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const cp = require('node:child_process');
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
|
|
8
|
+
function readJson(filePath) {
|
|
9
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function writeJson(filePath, value) {
|
|
13
|
+
fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function escapeRegexLiteral(value) {
|
|
17
|
+
return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function nextPatchVersion(version) {
|
|
21
|
+
const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(String(version || '').trim());
|
|
22
|
+
if (!match) {
|
|
23
|
+
throw new Error(`Cannot auto-bump non-plain semver version: ${version}`);
|
|
24
|
+
}
|
|
25
|
+
return `${match[1]}.${match[2]}.${Number(match[3]) + 1}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function defaultNpmView(name, version) {
|
|
29
|
+
const result = cp.spawnSync('npm', ['view', `${name}@${version}`, 'version', '--json'], {
|
|
30
|
+
encoding: 'utf8',
|
|
31
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (result.status === 0) {
|
|
35
|
+
return { state: 'published' };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const details = `${result.stdout || ''}\n${result.stderr || ''}`.trim();
|
|
39
|
+
if (/E404|404\s+Not Found|No match found|is not in this registry/i.test(details)) {
|
|
40
|
+
return { state: 'unpublished' };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (result.error) {
|
|
44
|
+
return { state: 'error', message: result.error.message };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { state: 'error', message: details || `npm view exited with status ${result.status}` };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function shouldSkip(env) {
|
|
51
|
+
if (env.GUARDEX_SKIP_PUBLISH_BUMP === '1') {
|
|
52
|
+
return 'GUARDEX_SKIP_PUBLISH_BUMP=1';
|
|
53
|
+
}
|
|
54
|
+
if (env.npm_config_dry_run === 'true') {
|
|
55
|
+
return 'npm publish --dry-run';
|
|
56
|
+
}
|
|
57
|
+
if (env.GITHUB_ACTIONS === 'true' && env.GUARDEX_ALLOW_PUBLISH_BUMP !== '1') {
|
|
58
|
+
return 'GitHub Actions release workflows keep package.json as the release source of truth';
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function findPublishableVersion({ name, version, npmView, maxAttempts = 100 }) {
|
|
64
|
+
let candidate = version;
|
|
65
|
+
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
66
|
+
const check = npmView(name, candidate);
|
|
67
|
+
if (check.state === 'unpublished') {
|
|
68
|
+
return { version: candidate, changed: candidate !== version };
|
|
69
|
+
}
|
|
70
|
+
if (check.state !== 'published') {
|
|
71
|
+
throw new Error(
|
|
72
|
+
`Unable to verify ${name}@${candidate} on npm: ${check.message || `state=${check.state}`}`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
candidate = nextPatchVersion(candidate);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
throw new Error(`Unable to find an unpublished patch version after ${maxAttempts} attempts`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function updatePackageLock(repoRoot, name, version) {
|
|
82
|
+
const lockPath = path.join(repoRoot, 'package-lock.json');
|
|
83
|
+
if (!fs.existsSync(lockPath)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const lockfile = readJson(lockPath);
|
|
88
|
+
lockfile.name = name;
|
|
89
|
+
lockfile.version = version;
|
|
90
|
+
if (lockfile.packages && lockfile.packages['']) {
|
|
91
|
+
lockfile.packages[''].name = name;
|
|
92
|
+
lockfile.packages[''].version = version;
|
|
93
|
+
}
|
|
94
|
+
writeJson(lockPath, lockfile);
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function buildReleaseNote(name, previousVersion, version) {
|
|
99
|
+
return (
|
|
100
|
+
`### v${version}\n` +
|
|
101
|
+
`- Bumped \`${name}\` from \`${previousVersion}\` to \`${version}\` so direct\n` +
|
|
102
|
+
` \`npm publish\` can continue after \`${previousVersion}\` reached the registry.\n\n`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function updateReadmeReleaseNote(repoRoot, name, previousVersion, version) {
|
|
107
|
+
const readmePath = path.join(repoRoot, 'README.md');
|
|
108
|
+
if (!fs.existsSync(readmePath)) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const readme = fs.readFileSync(readmePath, 'utf8');
|
|
113
|
+
const headingPattern = new RegExp(`^###\\s+v${escapeRegexLiteral(version)}\\b`, 'm');
|
|
114
|
+
if (headingPattern.test(readme)) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const releaseNote = buildReleaseNote(name, previousVersion, version);
|
|
119
|
+
const major = String(version).split('.')[0];
|
|
120
|
+
const majorSummaryPattern = new RegExp(
|
|
121
|
+
`(<summary><strong>v${escapeRegexLiteral(major)}\\.x</strong></summary>\\n\\n)`,
|
|
122
|
+
);
|
|
123
|
+
if (majorSummaryPattern.test(readme)) {
|
|
124
|
+
fs.writeFileSync(readmePath, readme.replace(majorSummaryPattern, `$1${releaseNote}`), 'utf8');
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (/## Release notes\n\n/.test(readme)) {
|
|
129
|
+
fs.writeFileSync(readmePath, readme.replace(/## Release notes\n\n/, `## Release notes\n\n${releaseNote}`), 'utf8');
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
throw new Error('README.md is missing a Release notes section for the generated publish bump');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function runPrepublishBump(options = {}) {
|
|
137
|
+
const repoRoot = options.repoRoot || path.resolve(__dirname, '..');
|
|
138
|
+
const env = options.env || process.env;
|
|
139
|
+
const log = options.log || console.log;
|
|
140
|
+
const npmView = options.npmView || defaultNpmView;
|
|
141
|
+
const skipReason = shouldSkip(env);
|
|
142
|
+
|
|
143
|
+
if (skipReason) {
|
|
144
|
+
log(`[guardex] prepublish version bump skipped: ${skipReason}.`);
|
|
145
|
+
return { changed: false, reason: 'skipped' };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const packagePath = path.join(repoRoot, 'package.json');
|
|
149
|
+
const packageJson = readJson(packagePath);
|
|
150
|
+
if (!packageJson.name || !packageJson.version) {
|
|
151
|
+
throw new Error('package.json must include name and version before publish');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const next = findPublishableVersion({
|
|
155
|
+
name: packageJson.name,
|
|
156
|
+
version: packageJson.version,
|
|
157
|
+
npmView,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
if (!next.changed) {
|
|
161
|
+
log(`[guardex] ${packageJson.name}@${packageJson.version} is not published yet; keeping package version.`);
|
|
162
|
+
return { changed: false, version: packageJson.version };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const previousVersion = packageJson.version;
|
|
166
|
+
packageJson.version = next.version;
|
|
167
|
+
writeJson(packagePath, packageJson);
|
|
168
|
+
updatePackageLock(repoRoot, packageJson.name, next.version);
|
|
169
|
+
updateReadmeReleaseNote(repoRoot, packageJson.name, previousVersion, next.version);
|
|
170
|
+
log(`[guardex] bumped ${packageJson.name} from ${previousVersion} to ${next.version} before npm publish.`);
|
|
171
|
+
return { changed: true, version: next.version, previousVersion };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (require.main === module) {
|
|
175
|
+
try {
|
|
176
|
+
runPrepublishBump();
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.error(`[guardex] prepublish version bump failed: ${error.message}`);
|
|
179
|
+
process.exitCode = 1;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
module.exports = {
|
|
184
|
+
defaultNpmView,
|
|
185
|
+
buildReleaseNote,
|
|
186
|
+
findPublishableVersion,
|
|
187
|
+
nextPatchVersion,
|
|
188
|
+
runPrepublishBump,
|
|
189
|
+
shouldSkip,
|
|
190
|
+
updateReadmeReleaseNote,
|
|
191
|
+
};
|
|
@@ -1,13 +1,93 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: gitguardex
|
|
3
|
-
description: "
|
|
3
|
+
description: "Use for GitGuardex branch/worktree safety, agent or subagent collision avoidance, file ownership, and gated PR finishing. Not for code-quality review."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
Use when
|
|
6
|
+
Use when a GitGuardex-managed repository needs safe edits or multi-agent coordination.
|
|
7
7
|
|
|
8
8
|
`gx status` -> `gx doctor` -> `gx status --strict`
|
|
9
9
|
|
|
10
10
|
Bootstrap: `gx setup`
|
|
11
11
|
Ops: `gx branch start "<task>" "<agent>"`, `gx locks claim --branch "<agent-branch>" <file...>`, `gx branch finish --branch "<agent-branch>" --base <base> --via-pr --wait-for-merge --cleanup`, `gx finish --all`, `gx cleanup`
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
## Keep the default workflow lean
|
|
14
|
+
|
|
15
|
+
- Default to direct T1 work in code and tests. Do not create OpenSpec artifacts from task wording alone.
|
|
16
|
+
- OpenSpec is explicit opt-in: use `--tier T2` for a change workspace or `--tier T3` for change plus plan only when the user requests OpenSpec or the caller explicitly selects T2/T3.
|
|
17
|
+
- Treat the commit, targeted test output, and compact final handoff as the routine task record.
|
|
18
|
+
|
|
19
|
+
## Agent and subagent collaboration
|
|
20
|
+
|
|
21
|
+
Before parallel work, inspect the field with the MCP `list_agents` tool or the CLI fallback:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
gx mcp list-agents --no-prs
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
For every file a writer may touch, check ownership, create an isolated lane, then claim it:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
gx mcp who-owns path/to/file.ts
|
|
31
|
+
gx branch start "<bounded task>" "<agent>"
|
|
32
|
+
gx locks claim --branch "<agent-branch>" path/to/file.ts
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- **One owner per file.** Split writer subagents by non-overlapping files or modules. Read-only scouts may share a surface; writers may not.
|
|
36
|
+
- Give each subagent one bounded objective and a verification command. Do not ask one subagent to explore, redesign, implement, and review the same slice.
|
|
37
|
+
- Re-check `list_agents` before expanding scope. If another lane owns or is already editing a file, coordinate a handoff or choose a different slice.
|
|
38
|
+
- Return a compact handoff: conclusion, `file:line`, changed files, verification result, and blocker or next action. Do not relay full logs.
|
|
39
|
+
- Finish each writing lane through its own PR once. Never run duplicate finish commands for the same branch.
|
|
40
|
+
|
|
41
|
+
## Optional cross-machine Git lock and radar state
|
|
42
|
+
|
|
43
|
+
Local worktree locks remain the default. When agents on multiple machines use the same writable Git remote, opt into shared state per repository:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
gx locks shared-enable --remote origin
|
|
47
|
+
gx locks shared-status
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
After enablement, ordinary `claim`, `allow-delete`, `validate`, `release`, and `status` commands also use atomic remote refs under `refs/gitguardex/locks/*`. `gx mcp list-agents` combines local worktrees with remote `agent/*` branches and shared lock owners; `gx mcp who-owns` resolves the same shared owner data.
|
|
51
|
+
|
|
52
|
+
- Shared mode is explicit and fail-closed: if the remote is unreachable, rejects custom refs, or returns invalid metadata, do not keep a local-only claim or approve a commit.
|
|
53
|
+
- The remote must allow authenticated custom-ref creation, compare-and-swap updates, and deletion. GitGuardex stores validated JSON metadata in commits; it never executes remote metadata, and publishes only a hashed machine identifier.
|
|
54
|
+
- Push the agent branch when full remote-lane visibility matters. A lock still makes an unpushed branch visible as a lock-owning remote lane.
|
|
55
|
+
- Release locks before disabling shared state or abandoning a lane. Cross-machine locks are not automatically reaped because one host cannot safely prove that another host's process is dead.
|
|
56
|
+
- `gx locks shared-disable` stops shared reads and writes for this repository; it does not delete remote refs owned by any machine.
|
|
57
|
+
|
|
58
|
+
## Finish checklist in Codex
|
|
59
|
+
|
|
60
|
+
Before starting a long `gx finish`, `gx ship`, or `gx branch finish`, show this compact Markdown checklist in commentary and update it from the CLI's `[gx:finish]` transition lines:
|
|
61
|
+
|
|
62
|
+
- [ ] Prepare branch
|
|
63
|
+
- [ ] Local preflight
|
|
64
|
+
- [ ] Push and open PR
|
|
65
|
+
- [ ] AI review
|
|
66
|
+
- [ ] Review autofix
|
|
67
|
+
- [ ] CI checks
|
|
68
|
+
- [ ] Merge
|
|
69
|
+
- [ ] Cleanup
|
|
70
|
+
|
|
71
|
+
Render each state-change update as a compact card, not as a raw log dump:
|
|
72
|
+
|
|
73
|
+
### 🚀 GX Finish · `3/8`
|
|
74
|
+
`agent/...` → `main` · 🔄 **AI review** · `00:44`
|
|
75
|
+
|
|
76
|
+
- ✅ Prepare branch
|
|
77
|
+
- ✅ Local preflight
|
|
78
|
+
- ✅ Push and open PR · `#123`
|
|
79
|
+
- 🔄 AI review
|
|
80
|
+
- ⬜ Review autofix
|
|
81
|
+
- ⬜ CI checks
|
|
82
|
+
- ⬜ Merge
|
|
83
|
+
- ⬜ Cleanup
|
|
84
|
+
|
|
85
|
+
Start the command with a short initial yield, then poll the background process every 15-30 seconds. Between polls, post an updated checklist when the active stage changes; during a quiet long stage, name the active stage and elapsed time at most every 30 seconds. Never leave the user with only Codex's generic **Working** or **Waiting for background terminal** label. Do not launch a duplicate finish while the first process is still alive, and do not mark a checkbox complete until a matching `✓` line or equivalent terminal evidence arrives.
|
|
86
|
+
|
|
87
|
+
Treat `🏁 Cleanup` as finished-but-best-effort, not as unconditional success: surface any cleanup warnings beside it instead of converting it to `✅`.
|
|
88
|
+
|
|
89
|
+
Structured phase events are persisted privately under `.omx/state/finish-runs/*.jsonl` (`0700` directory, `0600` files). Prefer that JSONL stream over parsing narrative logs when exact state is needed; the visual output prints the active event-file path.
|
|
90
|
+
|
|
91
|
+
When inspecting or verifying, prefer `rtk` compact wrappers if available (`rtk git status`, `rtk grep`, `rtk test <cmd>`, and noisy gx reads like `rtk gx status` / `rtk gx doctor`). Do not wrap commands whose stdout is parsed by scripts (`--json`, `--porcelain`, exact stdout contracts) or shell-ready output (`gx prompt --exec`).
|
|
92
|
+
|
|
93
|
+
To shrink gx's own large narrative output (e.g. `gx prompt`, `gx prompt --snippet`) before it lands in your context, set `GUARDEX_COMPRESS_CMD="<stdin->stdout filter>"`; gx routes that output through the filter (terse/non-TTY mode, fail-open, JSON skipped). Unset = byte-for-byte unchanged. Confirm it is wired with `gx status` or `gx doctor` — both print a `Token compression` line and flag a configured-but-missing binary (doctor's warning is advisory and never changes its safe/unsafe exit code).
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Live agent-lane activity: the W1 producer (`gx agents set-status`) and the
|
|
4
|
+
// jump selector (`gx agents jump`). The core functions are dependency-injected
|
|
5
|
+
// (session store + surface writer) so they unit-test without a live tmux/kitty.
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
|
|
8
|
+
const { TOOL_NAME } = require('../context');
|
|
9
|
+
const {
|
|
10
|
+
readAgentSession,
|
|
11
|
+
listAgentSessions,
|
|
12
|
+
updateAgentSession,
|
|
13
|
+
} = require('./sessions');
|
|
14
|
+
const { selectTerminalBackend } = require('../terminal');
|
|
15
|
+
|
|
16
|
+
// Canonical states a lane can be in, surfaced as cockpit pane icons.
|
|
17
|
+
const ACTIVITY_STATES = ['working', 'waiting', 'done', 'idle'];
|
|
18
|
+
|
|
19
|
+
const DEFAULT_ICONS = {
|
|
20
|
+
working: '🤖',
|
|
21
|
+
waiting: '💬',
|
|
22
|
+
done: '✅',
|
|
23
|
+
idle: '·',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Aliases an agent hook or human might send; everything maps to one canonical
|
|
27
|
+
// state so callers do not need to memorize exact tokens.
|
|
28
|
+
const ACTIVITY_ALIASES = {
|
|
29
|
+
work: 'working',
|
|
30
|
+
working: 'working',
|
|
31
|
+
busy: 'working',
|
|
32
|
+
running: 'working',
|
|
33
|
+
active: 'working',
|
|
34
|
+
thinking: 'working',
|
|
35
|
+
wait: 'waiting',
|
|
36
|
+
waiting: 'waiting',
|
|
37
|
+
input: 'waiting',
|
|
38
|
+
blocked: 'waiting',
|
|
39
|
+
prompt: 'waiting',
|
|
40
|
+
attention: 'waiting',
|
|
41
|
+
done: 'done',
|
|
42
|
+
complete: 'done',
|
|
43
|
+
completed: 'done',
|
|
44
|
+
finished: 'done',
|
|
45
|
+
stop: 'done',
|
|
46
|
+
idle: 'idle',
|
|
47
|
+
none: 'idle',
|
|
48
|
+
clear: 'idle',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Lower-priority jump targets sort after; waiting outranks done.
|
|
52
|
+
const JUMP_PRIORITY = { waiting: 0, done: 1 };
|
|
53
|
+
|
|
54
|
+
function normalizeActivity(raw) {
|
|
55
|
+
const key = String(raw === undefined || raw === null ? '' : raw).trim().toLowerCase();
|
|
56
|
+
if (!key) return '';
|
|
57
|
+
if (ACTIVITY_ALIASES[key]) return ACTIVITY_ALIASES[key];
|
|
58
|
+
return ACTIVITY_STATES.includes(key) ? key : '';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function activityIcon(activity, icons = DEFAULT_ICONS) {
|
|
62
|
+
const normalized = normalizeActivity(activity);
|
|
63
|
+
return (icons && icons[normalized]) || '';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function laneName(session) {
|
|
67
|
+
const value = session && typeof session === 'object' ? session : {};
|
|
68
|
+
const branch = typeof value.branch === 'string' ? value.branch : '';
|
|
69
|
+
const tail = branch.includes('/') ? branch.slice(branch.lastIndexOf('/') + 1) : branch;
|
|
70
|
+
const title = typeof value.title === 'string' ? value.title.trim() : '';
|
|
71
|
+
return title || tail || (typeof value.id === 'string' ? value.id : '') || 'agent';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// "🤖 fix-auth" — icon plus the short lane name. Falls back gracefully when an
|
|
75
|
+
// icon or name is missing.
|
|
76
|
+
function windowLabel(session, activity, icons = DEFAULT_ICONS) {
|
|
77
|
+
const icon = activityIcon(activity, icons);
|
|
78
|
+
const name = laneName(session);
|
|
79
|
+
if (!icon) return name;
|
|
80
|
+
return name ? `${icon} ${name}` : icon;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function sessionPaneTarget(session) {
|
|
84
|
+
const value = session && typeof session === 'object' ? session : {};
|
|
85
|
+
const tmux = value.tmux && typeof value.tmux === 'object' ? value.tmux : {};
|
|
86
|
+
return (
|
|
87
|
+
tmux.target ||
|
|
88
|
+
tmux.paneId ||
|
|
89
|
+
value.tmuxTarget ||
|
|
90
|
+
tmux.session ||
|
|
91
|
+
value.tmuxSession ||
|
|
92
|
+
''
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function sessionBackendName(session) {
|
|
97
|
+
const value = session && typeof session === 'object' ? session : {};
|
|
98
|
+
const tmux = value.tmux && typeof value.tmux === 'object' ? value.tmux : {};
|
|
99
|
+
if (tmux.backend) return String(tmux.backend);
|
|
100
|
+
if (value.terminal) return String(value.terminal);
|
|
101
|
+
return 'tmux';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function resolveSession(repoRoot, options = {}, deps = {}) {
|
|
105
|
+
const read = deps.readAgentSession || readAgentSession;
|
|
106
|
+
const list = deps.listAgentSessions || listAgentSessions;
|
|
107
|
+
|
|
108
|
+
// A malformed --session id or a corrupt session file must surface as a clean
|
|
109
|
+
// "no lane matched", not a thrown stack trace.
|
|
110
|
+
try {
|
|
111
|
+
if (options.sessionId) {
|
|
112
|
+
return read(repoRoot, options.sessionId);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const sessions = list(repoRoot);
|
|
116
|
+
if (options.branch) {
|
|
117
|
+
return sessions.find((session) => session.branch === options.branch) || null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const worktree = options.worktree || options.cwd;
|
|
121
|
+
if (worktree) {
|
|
122
|
+
const target = path.resolve(worktree);
|
|
123
|
+
return (
|
|
124
|
+
sessions.find(
|
|
125
|
+
(session) => session.worktreePath && path.resolve(session.worktreePath) === target,
|
|
126
|
+
) || null
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
} catch (_error) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Best-effort: apply the status label to the lane's pane. Never throws — a dead
|
|
136
|
+
// or absent multiplexer must not fail set-status.
|
|
137
|
+
function applySurface(context, deps = {}) {
|
|
138
|
+
const { target, label } = context;
|
|
139
|
+
if (!target) return { applied: false, reason: 'no-target' };
|
|
140
|
+
const apply = deps.applyWindowStatus;
|
|
141
|
+
if (typeof apply !== 'function') return { applied: false, reason: 'no-surface' };
|
|
142
|
+
try {
|
|
143
|
+
apply({ target, label, backend: sessionBackendName(context.session), activity: context.activity });
|
|
144
|
+
return { applied: true };
|
|
145
|
+
} catch (error) {
|
|
146
|
+
return { applied: false, reason: 'surface-error', error: error && error.message };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Producer core: persist the activity onto the session and (best-effort) write
|
|
151
|
+
// its label to the cockpit pane. `deps` injects the session store + surface.
|
|
152
|
+
function setAgentActivity(repoRoot, options = {}, deps = {}) {
|
|
153
|
+
const activity = normalizeActivity(options.activity);
|
|
154
|
+
if (!activity) {
|
|
155
|
+
throw new Error(
|
|
156
|
+
`Unknown agent activity: ${options.activity}. Use one of: ${ACTIVITY_STATES.join(', ')}.`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const session = resolveSession(repoRoot, options, deps);
|
|
161
|
+
if (!session) {
|
|
162
|
+
return { ok: false, reason: 'session-not-found', activity };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const update = deps.updateAgentSession || updateAgentSession;
|
|
166
|
+
const updated = update(repoRoot, session.id, { activity }) || session;
|
|
167
|
+
const label = windowLabel(updated, activity, options.icons);
|
|
168
|
+
const target = sessionPaneTarget(updated);
|
|
169
|
+
const surface = applySurface({ session: updated, label, target, activity }, deps);
|
|
170
|
+
|
|
171
|
+
return { ok: true, activity, sessionId: session.id, label, target, surface };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function jumpFilter(options = {}) {
|
|
175
|
+
if (options.waiting && !options.done) return new Set(['waiting']);
|
|
176
|
+
if (options.done && !options.waiting) return new Set(['done']);
|
|
177
|
+
return new Set(['waiting', 'done']);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Pick the lane most in need of attention: waiting before done, newest first,
|
|
181
|
+
// restricted to lanes that actually have a cockpit pane to focus.
|
|
182
|
+
function selectJumpTarget(sessions, options = {}) {
|
|
183
|
+
const wanted = jumpFilter(options);
|
|
184
|
+
const candidates = (Array.isArray(sessions) ? sessions : [])
|
|
185
|
+
.filter((session) => wanted.has(normalizeActivity(session.activity)))
|
|
186
|
+
.filter((session) => sessionPaneTarget(session));
|
|
187
|
+
|
|
188
|
+
candidates.sort((left, right) => {
|
|
189
|
+
const leftPriority = JUMP_PRIORITY[normalizeActivity(left.activity)] ?? 9;
|
|
190
|
+
const rightPriority = JUMP_PRIORITY[normalizeActivity(right.activity)] ?? 9;
|
|
191
|
+
if (leftPriority !== rightPriority) return leftPriority - rightPriority;
|
|
192
|
+
return String(right.updatedAt || '').localeCompare(String(left.updatedAt || ''));
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
return candidates[0] || null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// CLI wrapper for `gx agents set-status`: wires the live terminal backend in as
|
|
199
|
+
// the surface writer, then delegates to the pure core.
|
|
200
|
+
function runSetStatusCommand(repoRoot, options = {}, deps = {}) {
|
|
201
|
+
// When invoked from inside a lane (e.g. an agent hook) with no explicit
|
|
202
|
+
// selector, fall back to the current working directory's worktree.
|
|
203
|
+
const resolved = options.sessionId || options.branch || options.worktree
|
|
204
|
+
? options
|
|
205
|
+
: { ...options, worktree: process.cwd() };
|
|
206
|
+
let backend = null;
|
|
207
|
+
const result = setAgentActivity(repoRoot, resolved, {
|
|
208
|
+
...deps,
|
|
209
|
+
applyWindowStatus({ target, label, backend: sessionBackend }) {
|
|
210
|
+
// Honor an explicit --backend, else the lane's recorded backend (matching
|
|
211
|
+
// runJumpCommand), else auto-detect.
|
|
212
|
+
const backendName = options.backend || sessionBackend || 'auto';
|
|
213
|
+
if (!backend) backend = (deps.selectTerminalBackend || selectTerminalBackend)(backendName);
|
|
214
|
+
if (backend && typeof backend.setWindowStatus === 'function') {
|
|
215
|
+
backend.setWindowStatus(target, label);
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
if (!result.ok) {
|
|
221
|
+
return {
|
|
222
|
+
status: 1,
|
|
223
|
+
stderr: `[${TOOL_NAME}] No agent lane matched for set-status (use --session, --branch, or --worktree).\n`,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const applied = result.surface && result.surface.applied;
|
|
228
|
+
const note = applied ? '' : ` (surface ${result.surface ? result.surface.reason : 'skipped'})`;
|
|
229
|
+
return {
|
|
230
|
+
status: 0,
|
|
231
|
+
stdout: `[${TOOL_NAME}] ${result.sessionId} -> ${result.activity} ${result.label}${note}\n`,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// CLI wrapper for `gx agents jump`.
|
|
236
|
+
function runJumpCommand(repoRoot, options = {}, deps = {}) {
|
|
237
|
+
const list = deps.listAgentSessions || listAgentSessions;
|
|
238
|
+
const target = selectJumpTarget(list(repoRoot), options);
|
|
239
|
+
if (!target) {
|
|
240
|
+
return { status: 1, stderr: `[${TOOL_NAME}] No waiting/done agent lane to jump to.\n` };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const paneTarget = sessionPaneTarget(target);
|
|
244
|
+
if (options.print) {
|
|
245
|
+
return { status: 0, stdout: `${paneTarget}\n` };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
const backendName = options.backend || sessionBackendName(target);
|
|
250
|
+
const backend = (deps.selectTerminalBackend || selectTerminalBackend)(backendName);
|
|
251
|
+
backend.focusPane(paneTarget);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
return {
|
|
254
|
+
status: 1,
|
|
255
|
+
stderr: `[${TOOL_NAME}] Could not focus ${paneTarget}: ${error && error.message}\n`,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
status: 0,
|
|
261
|
+
stdout: `[${TOOL_NAME}] Jumped to ${target.id} (${normalizeActivity(target.activity)}) ${paneTarget}\n`,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
module.exports = {
|
|
266
|
+
ACTIVITY_STATES,
|
|
267
|
+
DEFAULT_ICONS,
|
|
268
|
+
normalizeActivity,
|
|
269
|
+
activityIcon,
|
|
270
|
+
laneName,
|
|
271
|
+
windowLabel,
|
|
272
|
+
sessionPaneTarget,
|
|
273
|
+
sessionBackendName,
|
|
274
|
+
resolveSession,
|
|
275
|
+
setAgentActivity,
|
|
276
|
+
selectJumpTarget,
|
|
277
|
+
runSetStatusCommand,
|
|
278
|
+
runJumpCommand,
|
|
279
|
+
};
|
package/src/agents/finish.js
CHANGED
|
@@ -85,9 +85,19 @@ function buildFinishEvidence(session, finishArgs, status, result, captured = {})
|
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
// Patching process.stdout.write only catches what THIS process writes. A child
|
|
89
|
+
// spawned on 'inherit' writes straight to fd 1 and sails past the patch, so an
|
|
90
|
+
// asset that streams has to be told to pipe while a capture is in flight —
|
|
91
|
+
// otherwise the merged-PR line never reaches buildFinishEvidence and the
|
|
92
|
+
// session records MERGED with an empty prUrl. Read by src/core/runtime.js and
|
|
93
|
+
// src/finish/index.js when they pick an asset's stdio.
|
|
94
|
+
const CAPTURE_ENV = 'GUARDEX_CAPTURE_ASSET_OUTPUT';
|
|
95
|
+
|
|
88
96
|
function captureProcessOutput(fn) {
|
|
89
97
|
let stdout = '';
|
|
90
98
|
let stderr = '';
|
|
99
|
+
const previousCapture = process.env[CAPTURE_ENV];
|
|
100
|
+
process.env[CAPTURE_ENV] = '1';
|
|
91
101
|
const originalStdoutWrite = process.stdout.write;
|
|
92
102
|
const originalStderrWrite = process.stderr.write;
|
|
93
103
|
process.stdout.write = function captureStdout(chunk, encoding, callback) {
|
|
@@ -107,6 +117,11 @@ function captureProcessOutput(fn) {
|
|
|
107
117
|
} finally {
|
|
108
118
|
process.stdout.write = originalStdoutWrite;
|
|
109
119
|
process.stderr.write = originalStderrWrite;
|
|
120
|
+
if (previousCapture === undefined) {
|
|
121
|
+
delete process.env[CAPTURE_ENV];
|
|
122
|
+
} else {
|
|
123
|
+
process.env[CAPTURE_ENV] = previousCapture;
|
|
124
|
+
}
|
|
110
125
|
}
|
|
111
126
|
}
|
|
112
127
|
|
package/src/ci-init/index.js
CHANGED
|
@@ -197,7 +197,6 @@ function renderCiInitHelp() {
|
|
|
197
197
|
'Files copied (from gitguardex templates/github/workflows/):',
|
|
198
198
|
` - ci.yml PR-time CI with draft-skip + concurrency-cancel.`,
|
|
199
199
|
` - ci-full.yml Weekly cross-runtime matrix + label opt-in.`,
|
|
200
|
-
` - cr.yml AI code review with agent/* + draft skip.`,
|
|
201
200
|
` - README.md Documents the budget posture and customization knobs.`,
|
|
202
201
|
'',
|
|
203
202
|
'The command stages copied files with git add when the target is a git repo;',
|