@imdeadpool/guardex 8.2.0 → 8.3.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 +47 -8
- package/bin/multiagent-safety.js +18 -1
- package/package.json +1 -1
- package/skills/gitguardex/SKILL.md +26 -2
- package/src/cli/args.js +10 -1
- package/src/cli/commands/agents.js +1 -0
- package/src/cli/commands/branch.js +52 -1
- package/src/cli/commands/finish.js +44 -2
- package/src/cli/shared-locks.js +4 -0
- package/src/doctor/index.js +26 -26
- package/src/finish/index.js +10 -2
- package/src/finish/post-branch-finish-cleanup.js +324 -0
- package/src/finish/review-gate.js +47 -6
- package/src/git/index.js +9 -4
- package/src/pr-checks.js +20 -5
- package/src/pr.js +122 -1
- package/src/scaffold/index.js +10 -0
- package/templates/AGENTS.multiagent-safety.md +37 -9
- package/templates/AGENTS.multiagent-safety.min.md +10 -4
- package/templates/codex/skills/gitguardex/SKILL.md +26 -2
- package/templates/githooks/pre-commit +40 -5
- package/templates/githooks/pre-push +72 -25
- package/templates/scripts/agent-branch-finish.sh +300 -21
- package/templates/scripts/agent-branch-start.sh +22 -2
- package/templates/scripts/agent-file-locks.py +88 -3
- package/templates/scripts/agent-preflight.sh +30 -0
- package/templates/scripts/agent-reconcile-openspec-tasks.js +105 -0
- package/templates/scripts/agent-worktree-prune.sh +396 -31
- package/templates/scripts/guardex-env.sh +291 -0
|
@@ -13,6 +13,11 @@ guardex_normalize_bool() {
|
|
|
13
13
|
esac
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
guardex_git_clean_env() (
|
|
17
|
+
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_PREFIX
|
|
18
|
+
git "$@"
|
|
19
|
+
)
|
|
20
|
+
|
|
16
21
|
guardex_read_repo_dotenv_var() {
|
|
17
22
|
local repo_root="$1"
|
|
18
23
|
local key="${2:-GUARDEX_ON}"
|
|
@@ -72,3 +77,289 @@ guardex_repo_is_enabled() {
|
|
|
72
77
|
fi
|
|
73
78
|
return 0
|
|
74
79
|
}
|
|
80
|
+
|
|
81
|
+
guardex_repo_worktree_mode_raw() {
|
|
82
|
+
local repo_root="$1"
|
|
83
|
+
local configured
|
|
84
|
+
if [[ -n "${GUARDEX_WORKTREE_MODE:-}" ]]; then
|
|
85
|
+
printf '%s' "$GUARDEX_WORKTREE_MODE"
|
|
86
|
+
return 0
|
|
87
|
+
fi
|
|
88
|
+
if configured="$(guardex_read_repo_dotenv_var "$repo_root" "GUARDEX_WORKTREE_MODE")" && [[ -n "$configured" ]]; then
|
|
89
|
+
printf '%s' "$configured"
|
|
90
|
+
return 0
|
|
91
|
+
fi
|
|
92
|
+
guardex_git_clean_env -C "$repo_root" config --local --get multiagent.worktreeMode 2>/dev/null || true
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
guardex_repo_worktree_mode() {
|
|
96
|
+
local repo_root="$1"
|
|
97
|
+
local raw lowered
|
|
98
|
+
raw="$(guardex_repo_worktree_mode_raw "$repo_root")"
|
|
99
|
+
lowered="$(printf '%s' "$raw" | tr '[:upper:]' '[:lower:]')"
|
|
100
|
+
case "$lowered" in
|
|
101
|
+
adaptive) printf 'adaptive' ;;
|
|
102
|
+
*) printf 'always' ;;
|
|
103
|
+
esac
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
guardex_repo_has_shared_agent_activity() {
|
|
107
|
+
local repo_root="$1"
|
|
108
|
+
local mode remote shared status
|
|
109
|
+
|
|
110
|
+
if [[ -v GUARDEX_SHARED_STATE ]]; then
|
|
111
|
+
mode="${GUARDEX_SHARED_STATE:-}"
|
|
112
|
+
elif mode="$(guardex_git_clean_env -C "$repo_root" config --local --get multiagent.sharedState 2>/dev/null)"; then
|
|
113
|
+
:
|
|
114
|
+
else
|
|
115
|
+
status=$?
|
|
116
|
+
[[ "$status" -eq 1 ]] && return 1
|
|
117
|
+
return 0
|
|
118
|
+
fi
|
|
119
|
+
[[ "${mode,,}" == "git" ]] || return 1
|
|
120
|
+
|
|
121
|
+
if [[ -v GUARDEX_SHARED_STATE_REMOTE ]]; then
|
|
122
|
+
remote="${GUARDEX_SHARED_STATE_REMOTE:-}"
|
|
123
|
+
elif remote="$(guardex_git_clean_env -C "$repo_root" config --local --get multiagent.sharedStateRemote 2>/dev/null)"; then
|
|
124
|
+
:
|
|
125
|
+
else
|
|
126
|
+
status=$?
|
|
127
|
+
[[ "$status" -eq 1 ]] || return 0
|
|
128
|
+
remote="origin"
|
|
129
|
+
fi
|
|
130
|
+
remote="${remote:-origin}"
|
|
131
|
+
[[ "$remote" =~ ^[A-Za-z0-9][A-Za-z0-9._/-]{0,127}$ ]] || return 0
|
|
132
|
+
|
|
133
|
+
local timeout_bin=""
|
|
134
|
+
timeout_bin="$(command -v timeout 2>/dev/null || command -v gtimeout 2>/dev/null || true)"
|
|
135
|
+
if [[ -n "$timeout_bin" ]]; then
|
|
136
|
+
shared="$("$timeout_bin" 15s bash -c '
|
|
137
|
+
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_PREFIX
|
|
138
|
+
export GIT_TERMINAL_PROMPT=0
|
|
139
|
+
git "$@"
|
|
140
|
+
' guardex-shared-state -C "$repo_root" ls-remote --refs "$remote" \
|
|
141
|
+
'refs/gitguardex/locks/*' 2>/dev/null)" || return 0
|
|
142
|
+
else
|
|
143
|
+
return 0
|
|
144
|
+
fi
|
|
145
|
+
[[ -n "$shared" ]]
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
guardex_repo_has_competing_worktree_activity() {
|
|
149
|
+
local repo_root="$1"
|
|
150
|
+
local node_bin="${2:-node}"
|
|
151
|
+
local current_path line worktree_list worktree_path worktree_real_path dirty lock_file lock_status
|
|
152
|
+
|
|
153
|
+
if ! current_path="$(cd "$repo_root" && pwd -P)"; then
|
|
154
|
+
return 0
|
|
155
|
+
fi
|
|
156
|
+
|
|
157
|
+
if guardex_repo_has_shared_agent_activity "$repo_root"; then
|
|
158
|
+
return 0
|
|
159
|
+
fi
|
|
160
|
+
|
|
161
|
+
if ! worktree_list="$(guardex_git_clean_env -C "$repo_root" worktree list --porcelain 2>/dev/null)"; then
|
|
162
|
+
return 0
|
|
163
|
+
fi
|
|
164
|
+
|
|
165
|
+
while IFS= read -r line; do
|
|
166
|
+
[[ "$line" == worktree\ * ]] || continue
|
|
167
|
+
worktree_path="${line#worktree }"
|
|
168
|
+
[[ -d "$worktree_path" ]] || continue
|
|
169
|
+
if ! worktree_real_path="$(cd "$worktree_path" && pwd -P)"; then
|
|
170
|
+
return 0
|
|
171
|
+
fi
|
|
172
|
+
[[ "$worktree_real_path" != "$current_path" ]] || continue
|
|
173
|
+
|
|
174
|
+
if ! dirty="$(
|
|
175
|
+
guardex_git_clean_env -C "$worktree_path" status --porcelain --untracked-files=normal -- \
|
|
176
|
+
. ':(exclude).omx/**' ':(exclude).omc/**' 2>/dev/null
|
|
177
|
+
)"; then
|
|
178
|
+
return 0
|
|
179
|
+
fi
|
|
180
|
+
if [[ -n "$dirty" ]]; then
|
|
181
|
+
return 0
|
|
182
|
+
fi
|
|
183
|
+
|
|
184
|
+
lock_file="${worktree_path}/.omx/state/agent-file-locks.json"
|
|
185
|
+
if [[ -f "$lock_file" ]]; then
|
|
186
|
+
if "$node_bin" -e '
|
|
187
|
+
const fs = require("node:fs");
|
|
188
|
+
try {
|
|
189
|
+
const data = JSON.parse(fs.readFileSync(process.argv[1], "utf8"));
|
|
190
|
+
if (!data || typeof data !== "object" || Array.isArray(data)) process.exit(0);
|
|
191
|
+
const locks = data.locks;
|
|
192
|
+
if (locks !== undefined && (!locks || typeof locks !== "object" || Array.isArray(locks))) {
|
|
193
|
+
process.exit(0);
|
|
194
|
+
}
|
|
195
|
+
process.exit(Object.keys(locks || {}).length > 0 ? 0 : 1);
|
|
196
|
+
} catch {
|
|
197
|
+
process.exit(0);
|
|
198
|
+
}
|
|
199
|
+
' "$lock_file" >/dev/null 2>&1; then
|
|
200
|
+
return 0
|
|
201
|
+
else
|
|
202
|
+
lock_status=$?
|
|
203
|
+
if [[ "$lock_status" -ne 1 ]]; then
|
|
204
|
+
return 0
|
|
205
|
+
fi
|
|
206
|
+
fi
|
|
207
|
+
fi
|
|
208
|
+
done <<< "$worktree_list"
|
|
209
|
+
|
|
210
|
+
return 1
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
guardex_agent_session_id() {
|
|
214
|
+
printf '%s' "${CODEX_THREAD_ID:-${OMX_SESSION_ID:-${CLAUDE_CODE_SESSION_ID:-}}}"
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
guardex_claim_adaptive_session_lease() {
|
|
218
|
+
local repo_root="$1"
|
|
219
|
+
local session_id="${2:-$(guardex_agent_session_id)}"
|
|
220
|
+
local python_bin="${GUARDEX_PYTHON_BIN:-python3}"
|
|
221
|
+
local common_dir
|
|
222
|
+
|
|
223
|
+
[[ -n "$session_id" ]] || return 1
|
|
224
|
+
command -v "$python_bin" >/dev/null 2>&1 || return 1
|
|
225
|
+
common_dir="$(guardex_git_clean_env -C "$repo_root" rev-parse --git-common-dir 2>/dev/null)" || return 1
|
|
226
|
+
if [[ "$common_dir" != /* ]]; then
|
|
227
|
+
common_dir="${repo_root}/${common_dir}"
|
|
228
|
+
fi
|
|
229
|
+
common_dir="$(cd "$common_dir" 2>/dev/null && pwd -P)" || return 1
|
|
230
|
+
|
|
231
|
+
"$python_bin" - "$common_dir" "$repo_root" "$session_id" "${GUARDEX_ADAPTIVE_SESSION_LEASE_SEC:-900}" <<'PY'
|
|
232
|
+
import fcntl
|
|
233
|
+
import hmac
|
|
234
|
+
import json
|
|
235
|
+
import os
|
|
236
|
+
import sys
|
|
237
|
+
import time
|
|
238
|
+
from pathlib import Path
|
|
239
|
+
|
|
240
|
+
common_dir, repo_root, session_id, raw_ttl = sys.argv[1:]
|
|
241
|
+
try:
|
|
242
|
+
ttl_seconds = float(raw_ttl)
|
|
243
|
+
except ValueError:
|
|
244
|
+
raise SystemExit(1)
|
|
245
|
+
if not (0 < ttl_seconds < float("inf")):
|
|
246
|
+
raise SystemExit(1)
|
|
247
|
+
|
|
248
|
+
state_dir = Path(common_dir) / "gitguardex"
|
|
249
|
+
lease_path = state_dir / "adaptive-direct-session.json"
|
|
250
|
+
lock_path = state_dir / "adaptive-direct-session.lock"
|
|
251
|
+
agent_lock_path = Path(common_dir) / "agent-file-locks.lock"
|
|
252
|
+
try:
|
|
253
|
+
state_dir.mkdir(parents=True, exist_ok=True)
|
|
254
|
+
agent_lock_handle = open(agent_lock_path, "a+")
|
|
255
|
+
lock_handle = open(lock_path, "a+")
|
|
256
|
+
except OSError:
|
|
257
|
+
raise SystemExit(1)
|
|
258
|
+
|
|
259
|
+
acquired = False
|
|
260
|
+
agent_lock_acquired = False
|
|
261
|
+
def wrapper_marker_matches(active_lease):
|
|
262
|
+
wrapper_session = os.environ.get("GUARDEX_ADAPTIVE_COMMAND_LOCK_SESSION_ID", "")
|
|
263
|
+
presented_token = os.environ.get("GUARDEX_ADAPTIVE_COMMAND_LOCK_TOKEN", "")
|
|
264
|
+
expected_token = active_lease.get("lock_token")
|
|
265
|
+
if wrapper_session != session_id:
|
|
266
|
+
return False
|
|
267
|
+
if not isinstance(expected_token, str) or not expected_token or not presented_token:
|
|
268
|
+
return False
|
|
269
|
+
return hmac.compare_digest(presented_token, expected_token)
|
|
270
|
+
|
|
271
|
+
try:
|
|
272
|
+
try:
|
|
273
|
+
fcntl.flock(agent_lock_handle.fileno(), fcntl.LOCK_EX)
|
|
274
|
+
agent_lock_acquired = True
|
|
275
|
+
except OSError:
|
|
276
|
+
raise SystemExit(1)
|
|
277
|
+
|
|
278
|
+
current_root = Path(repo_root).resolve()
|
|
279
|
+
for gitdir_path in (Path(common_dir) / "worktrees").glob("*/gitdir"):
|
|
280
|
+
try:
|
|
281
|
+
worktree_root = Path(gitdir_path.read_text().strip()).resolve().parent
|
|
282
|
+
except OSError:
|
|
283
|
+
raise SystemExit(1)
|
|
284
|
+
if worktree_root == current_root:
|
|
285
|
+
continue
|
|
286
|
+
registry_path = worktree_root / ".omx/state/agent-file-locks.json"
|
|
287
|
+
try:
|
|
288
|
+
registry = json.loads(registry_path.read_text())
|
|
289
|
+
except FileNotFoundError:
|
|
290
|
+
continue
|
|
291
|
+
except (OSError, json.JSONDecodeError, ValueError):
|
|
292
|
+
raise SystemExit(1)
|
|
293
|
+
locks = registry.get("locks") if isinstance(registry, dict) else None
|
|
294
|
+
if not isinstance(locks, dict):
|
|
295
|
+
raise SystemExit(1)
|
|
296
|
+
if locks:
|
|
297
|
+
print("Adaptive direct work is blocked by an active isolated lane.", file=sys.stderr)
|
|
298
|
+
raise SystemExit(1)
|
|
299
|
+
|
|
300
|
+
try:
|
|
301
|
+
fcntl.flock(lock_handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
302
|
+
acquired = True
|
|
303
|
+
except BlockingIOError:
|
|
304
|
+
try:
|
|
305
|
+
active_lease = json.loads(lease_path.read_text())
|
|
306
|
+
except (OSError, json.JSONDecodeError, ValueError):
|
|
307
|
+
raise SystemExit(1)
|
|
308
|
+
if (
|
|
309
|
+
isinstance(active_lease, dict)
|
|
310
|
+
and active_lease.get("session_id") == session_id
|
|
311
|
+
and wrapper_marker_matches(active_lease)
|
|
312
|
+
):
|
|
313
|
+
raise SystemExit(0)
|
|
314
|
+
print("Adaptive direct work is owned by another active agent session.", file=sys.stderr)
|
|
315
|
+
raise SystemExit(1)
|
|
316
|
+
try:
|
|
317
|
+
lease = json.loads(lease_path.read_text())
|
|
318
|
+
except FileNotFoundError:
|
|
319
|
+
lease = {}
|
|
320
|
+
except (OSError, json.JSONDecodeError, ValueError):
|
|
321
|
+
raise SystemExit(1)
|
|
322
|
+
if not isinstance(lease, dict):
|
|
323
|
+
raise SystemExit(1)
|
|
324
|
+
|
|
325
|
+
owner = lease.get("session_id")
|
|
326
|
+
last_seen = lease.get("last_seen_epoch")
|
|
327
|
+
if owner is not None and not isinstance(owner, str):
|
|
328
|
+
raise SystemExit(1)
|
|
329
|
+
if last_seen is not None and (
|
|
330
|
+
isinstance(last_seen, bool) or not isinstance(last_seen, (int, float))
|
|
331
|
+
):
|
|
332
|
+
raise SystemExit(1)
|
|
333
|
+
|
|
334
|
+
now = time.time()
|
|
335
|
+
if owner and owner != session_id and last_seen is not None and now - float(last_seen) < ttl_seconds:
|
|
336
|
+
print("Adaptive direct work is owned by another active agent session.", file=sys.stderr)
|
|
337
|
+
raise SystemExit(1)
|
|
338
|
+
|
|
339
|
+
tmp_path = lease_path.with_name(f"{lease_path.name}.tmp-{os.getpid()}")
|
|
340
|
+
try:
|
|
341
|
+
tmp_path.write_text(
|
|
342
|
+
json.dumps({"session_id": session_id, "last_seen_epoch": now}, sort_keys=True) + "\n"
|
|
343
|
+
)
|
|
344
|
+
os.replace(tmp_path, lease_path)
|
|
345
|
+
except OSError:
|
|
346
|
+
try:
|
|
347
|
+
tmp_path.unlink()
|
|
348
|
+
except OSError:
|
|
349
|
+
pass
|
|
350
|
+
raise SystemExit(1)
|
|
351
|
+
finally:
|
|
352
|
+
try:
|
|
353
|
+
if acquired:
|
|
354
|
+
fcntl.flock(lock_handle.fileno(), fcntl.LOCK_UN)
|
|
355
|
+
finally:
|
|
356
|
+
try:
|
|
357
|
+
lock_handle.close()
|
|
358
|
+
finally:
|
|
359
|
+
try:
|
|
360
|
+
if agent_lock_acquired:
|
|
361
|
+
fcntl.flock(agent_lock_handle.fileno(), fcntl.LOCK_UN)
|
|
362
|
+
finally:
|
|
363
|
+
agent_lock_handle.close()
|
|
364
|
+
PY
|
|
365
|
+
}
|