-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.sh
More file actions
357 lines (312 loc) · 11 KB
/
Copy pathcheck.sh
File metadata and controls
357 lines (312 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/lib/json-node.sh"
require_node
print_help() {
cat <<'EOF'
Usage: check.sh
Report managed-file drift for the current repo.
EOF
}
for arg in "$@"; do
case "$arg" in
--help|-h) print_help; exit 0 ;;
*)
echo "Unknown argument: $arg" >&2
print_help >&2
exit 1
;;
esac
done
node - "$SCRIPT_DIR/lib/merge-hooks.cjs" "$SCRIPT_DIR/lib/managed-file-hash.cjs" <<'NODE'
const fs = require("fs");
const os = require("os");
const path = require("path");
const { directoryFoldsCase, validateHooksDocument, wizardCommandPath } = require(process.argv[2]);
const { inspectManagedFile } = require(process.argv[3]);
const cwd = process.cwd();
const manifestPath = path.join(cwd, ".codex-sdlc", "manifest.json");
function printJson(value) {
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`);
}
if (!fs.existsSync(manifestPath)) {
printJson({
repo_state: "uninitialized",
reason: "manifest_missing",
managed_files: {}
});
process.exit(0);
}
let manifest;
try {
manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
} catch (error) {
printJson({
repo_state: "broken",
reason: "manifest_invalid",
error: error.message,
managed_files: {}
});
process.exit(0);
}
function hasPlatformHookDrift(relativePath, absolutePath) {
if (relativePath !== ".codex/hooks.json") {
return false;
}
let document;
try {
const content = fs.readFileSync(absolutePath, "utf8").replace(/^\uFEFF/, "");
document = JSON.parse(content);
validateHooksDocument(document, relativePath);
} catch (_error) {
return true;
}
if (!document || typeof document !== "object" || Array.isArray(document)) {
return true;
}
const foldPathCase = directoryFoldsCase(path.dirname(absolutePath));
const commands = Object.values(document.hooks || {}).flatMap((entries) =>
Array.isArray(entries) ? entries.flatMap((entry) =>
Array.isArray(entry?.hooks) ? entry.hooks.map((hook) => hook?.command) : []) : [],
);
const scriptPaths = commands
.map((command) => wizardCommandPath(command, foldPathCase))
.filter(Boolean);
if (scriptPaths.some((scriptPath) => /\/(?:git-guard|session-start)\.js$/.test(scriptPath))) {
return true;
}
if (process.platform === "win32") {
return scriptPaths.some((scriptPath) => /\/(?:bash-guard|session-start)\.sh$/.test(scriptPath));
}
return scriptPaths.some((scriptPath) => /\/(?:git-guard|session-start)\.ps1$/.test(scriptPath));
}
function hasManagedHookSurfaceDrift(relativePath, absolutePath) {
if (relativePath !== ".codex/hooks.json") {
return false;
}
const content = fs.readFileSync(absolutePath, "utf8");
return !content.includes('"PreCompact"') ||
!content.includes('"PostCompact"') ||
!content.includes("node .codex/hooks/compact-guard.cjs");
}
function isRetiredManagedPath(relativePath) {
return relativePath === ".codex/hooks/git-guard.js" || relativePath === ".codex/hooks/session-start.js";
}
function parseTomlSectionKey(rawValue) {
const value = rawValue.trim();
if (value.startsWith("'") && value.endsWith("'")) {
return value.slice(1, -1);
}
if (value.startsWith('"') && value.endsWith('"')) {
try {
return JSON.parse(value);
} catch (_error) {
return "";
}
}
return value;
}
function normalizedHookStatePath(value, foldCase) {
const normalized = value.replace(/\\/g, "/").replace(/\/+$/, "");
return foldCase ? normalized.toLowerCase() : normalized;
}
function disabledManagedHookEvents() {
const codexHome = process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
const configPath = path.join(codexHome, "config.toml");
const hooksPath = path.resolve(cwd, ".codex", "hooks.json");
if (!fs.existsSync(configPath) || !fs.existsSync(hooksPath)) {
return [];
}
const foldCase = directoryFoldsCase(path.dirname(hooksPath));
const expectedPath = normalizedHookStatePath(hooksPath, foldCase);
const eventNames = {
pre_tool_use: "PreToolUse",
session_start: "SessionStart",
pre_compact: "PreCompact",
post_compact: "PostCompact"
};
const disabled = new Map();
let currentState = null;
for (const line of fs.readFileSync(configPath, "utf8").replace(/^\uFEFF/, "").split(/\r?\n/)) {
const section = line.match(/^\s*\[hooks\.state\.(.+)\]\s*$/);
if (section) {
const stateKey = parseTomlSectionKey(section[1]);
const match = stateKey.match(/^(.*):([a-z_]+):(\d+):(\d+)$/i);
currentState = match && normalizedHookStatePath(match[1], foldCase) === expectedPath
? { event: eventNames[match[2].toLowerCase()] || match[2] }
: null;
continue;
}
if (/^\s*\[/.test(line)) {
currentState = null;
continue;
}
if (currentState && /^\s*enabled\s*=\s*false\s*(?:#.*)?$/i.test(line)) {
disabled.set(currentState.event, { event: currentState.event });
}
}
return [...disabled.values()];
}
const disabledHookEvents = disabledManagedHookEvents();
function resolvedModelPolicy() {
const profilePath = path.join(cwd, ".codex-sdlc", "model-profile.json");
if (!fs.existsSync(profilePath)) {
return null;
}
try {
const metadata = JSON.parse(fs.readFileSync(profilePath, "utf8"));
const selectedProfile = metadata.selected_profile || manifest.model_profile?.selected_profile;
const selected = metadata.profiles?.[selectedProfile];
if (!selected) {
return null;
}
const allowedModels = new Set();
for (const profile of Object.values(metadata.profiles || {})) {
if (typeof profile?.main_model === "string") allowedModels.add(profile.main_model.toLowerCase());
if (typeof profile?.review_model === "string") allowedModels.add(profile.review_model.toLowerCase());
}
if (typeof metadata.policy?.default_driver === "string") {
allowedModels.add(metadata.policy.default_driver.toLowerCase());
}
return {
selected_profile: selectedProfile,
main_model: selected.main_model || "",
main_reasoning: selected.main_reasoning || manifest.model_profile?.baseline_reasoning || "",
allowed_models: allowedModels
};
} catch (_error) {
return null;
}
}
const modelPolicy = resolvedModelPolicy();
function customizedDocumentPolicyWarnings(relativePath, absolutePath, status) {
if (status !== "customized" || !relativePath.toLowerCase().endsWith(".md") || !modelPolicy) {
return [];
}
const content = fs.readFileSync(absolutePath, "utf8");
const warnings = [];
const modelReferences = new Set(content.match(/\bgpt-[a-z0-9][a-z0-9.-]*\b/gi) || []);
for (const modelReference of modelReferences) {
if (!modelPolicy.allowed_models.has(modelReference.toLowerCase())) {
warnings.push({
path: relativePath,
kind: "stale_model_reference",
stale_value: modelReference,
expected_main_model: modelPolicy.main_model,
message: "Customized managed document references a model outside the resolved profile metadata; review manually"
});
}
}
const reasoningClaims = new Set();
const effort = "(low|medium|high|xhigh)";
const defaultRole = "(?:default|baseline|normal(?: standing)? root driver|standing root driver)";
const patterns = [
new RegExp(`\\b${effort}\\b[^.\\r\\n]{0,80}\\b(?:is|as)\\s+(?:the\\s+)?${defaultRole}\\b`, "i"),
new RegExp(`\\b${defaultRole}\\b[^.\\r\\n]{0,80}\\b${effort}\\b`, "i"),
new RegExp(`\\b${effort}\\b[^.\\r\\n]{0,40}\\bby default\\b`, "i")
];
for (const line of content.split(/\r?\n/)) {
for (const pattern of patterns) {
const match = line.match(pattern);
if (match) {
const claim = match.slice(1).find((value) => /^(?:low|medium|high|xhigh)$/i.test(value || ""));
if (claim) reasoningClaims.add(claim.toLowerCase());
break;
}
}
}
for (const claim of reasoningClaims) {
if (modelPolicy.main_reasoning && claim !== modelPolicy.main_reasoning.toLowerCase()) {
warnings.push({
path: relativePath,
kind: "stale_default_reasoning",
stale_value: claim,
expected_main_reasoning: modelPolicy.main_reasoning,
selected_profile: modelPolicy.selected_profile,
message: "Customized managed document claims a default reasoning effort that conflicts with the resolved profile; review manually"
});
}
}
return warnings;
}
const managedFiles = {};
const policyWarnings = [];
const summary = {
match: 0,
missing: 0,
customized: 0,
warning: 0,
policy_warnings: 0,
"drift / broken": 0
};
for (const [relativePath, expectedHash] of Object.entries(manifest.managed_files || {})) {
if (!expectedHash) {
continue;
}
const absolutePath = path.join(cwd, relativePath);
let actualHash = "";
let canonicalHash = "";
let hashMode = "";
let carriageReturns = 0;
let manifestHashMigration = false;
let contentStatus;
let status;
if (!fs.existsSync(absolutePath)) {
status = "missing";
} else {
const hashInfo = inspectManagedFile(absolutePath, expectedHash);
actualHash = hashInfo.raw_hash;
canonicalHash = hashInfo.canonical_hash;
hashMode = hashInfo.hash_mode;
carriageReturns = hashInfo.carriage_returns;
manifestHashMigration = hashInfo.manifest_hash_migration;
if (hashMode === "raw" && relativePath.toLowerCase().endsWith(".sh") && carriageReturns > 0) {
status = "drift / broken";
} else if (isRetiredManagedPath(relativePath) || hasPlatformHookDrift(relativePath, absolutePath)) {
status = "drift / broken";
} else if (hashInfo.matches_expected) {
status = hasManagedHookSurfaceDrift(relativePath, absolutePath) ? "drift / broken" : "match";
} else {
status = "customized";
}
}
contentStatus = status;
if (relativePath === ".codex/hooks.json" && status === "match" && disabledHookEvents.length > 0) {
status = "warning";
}
const documentPolicyWarnings = fs.existsSync(absolutePath)
? customizedDocumentPolicyWarnings(relativePath, absolutePath, status)
: [];
policyWarnings.push(...documentPolicyWarnings);
managedFiles[relativePath] = {
status,
...(status === "warning" ? {
content_status: contentStatus,
warning: "Codex user hook state explicitly disables one or more managed events",
disabled_events: disabledHookEvents
} : {}),
...(documentPolicyWarnings.length > 0 ? { policy_warnings: documentPolicyWarnings } : {}),
expected_hash: expectedHash,
actual_hash: actualHash,
canonical_hash: canonicalHash,
hash_mode: hashMode,
carriage_returns: carriageReturns,
...(manifestHashMigration ? { manifest_hash_migration: true } : {})
};
summary[status] += 1;
summary.policy_warnings += documentPolicyWarnings.length;
}
printJson({
repo_state: "initialized",
adapter_version: manifest.adapter_version || "",
scan: manifest.scan || {},
summary,
hook_activation: {
status: disabledHookEvents.length > 0 ? "warning" : "no_disabled_state_detected",
disabled_events: disabledHookEvents
},
policy_warnings: policyWarnings,
managed_files: managedFiles
});
NODE