architecture-harness 0.25.0 → 0.26.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/package.json +3 -22
- package/src/cli/ask.mjs +42 -0
- package/src/cli/ask.test.mjs +40 -0
- package/src/cli/branch-sync.mjs +126 -0
- package/src/cli/branch-sync.test.mjs +428 -0
- package/src/cli/budget-guard.mjs +74 -5
- package/src/cli/budget-guard.test.mjs +185 -2
- package/src/cli/budget-report.mjs +85 -0
- package/src/cli/budget-report.test.mjs +173 -0
- package/src/cli/budget.mjs +8 -5
- package/src/cli/check-commands.mjs +123 -11
- package/src/cli/check-commands.test.mjs +360 -6
- package/src/cli/check-feature-switches.test.mjs +110 -0
- package/src/cli/check-full.test.mjs +152 -0
- package/src/cli/check-verdict.mjs +46 -0
- package/src/cli/check-verdict.test.mjs +32 -0
- package/src/cli/check.mjs +137 -4
- package/src/cli/check.test.mjs +45 -0
- package/src/cli/checkpoint.mjs +143 -0
- package/src/cli/checkpoint.test.mjs +146 -0
- package/src/cli/child-run.mjs +7 -2
- package/src/cli/child-run.test.mjs +17 -2
- package/src/cli/ci-failures.mjs +304 -0
- package/src/cli/ci-failures.test.mjs +615 -0
- package/src/cli/ci-preview.mjs +62 -0
- package/src/cli/ci-preview.test.mjs +85 -0
- package/src/cli/ci-state.mjs +25 -29
- package/src/cli/ci-state.test.mjs +174 -2
- package/src/cli/ci-template.test.mjs +86 -0
- package/src/cli/claude-core.test.mjs +37 -0
- package/src/cli/codex-implement.mjs +580 -47
- package/src/cli/codex-implement.test.mjs +1946 -29
- package/src/cli/codex-limit.mjs +85 -0
- package/src/cli/codex-limit.test.mjs +179 -0
- package/src/cli/codex-reset.mjs +78 -0
- package/src/cli/codex-reset.test.mjs +192 -0
- package/src/cli/codex-verify.mjs +230 -0
- package/src/cli/codex-verify.test.mjs +402 -0
- package/src/cli/comment-watch.mjs +391 -0
- package/src/cli/comment-watch.test.mjs +392 -0
- package/src/cli/commit-guard.mjs +219 -0
- package/src/cli/commit-guard.test.mjs +809 -0
- package/src/cli/context-budgets.mjs +47 -0
- package/src/cli/context-budgets.test.mjs +96 -0
- package/src/cli/context-watch.mjs +116 -0
- package/src/cli/context-watch.test.mjs +395 -0
- package/src/cli/contract-history.mjs +1 -1
- package/src/cli/controller-guard.mjs +38 -0
- package/src/cli/controller-guard.test.mjs +66 -2
- package/src/cli/cost-stop.mjs +5 -1
- package/src/cli/cost-stop.test.mjs +26 -0
- package/src/cli/diff-shape.mjs +162 -0
- package/src/cli/diff-shape.test.mjs +200 -0
- package/src/cli/direct-lane.mjs +32 -19
- package/src/cli/direct-lane.test.mjs +21 -3
- package/src/cli/dispatch-guard.mjs +11 -0
- package/src/cli/dispatch-guard.test.mjs +145 -10
- package/src/cli/dispatch-reminder.mjs +23 -10
- package/src/cli/dispatch-reminder.test.mjs +63 -11
- package/src/cli/dispatch-route.mjs +54 -0
- package/src/cli/dispatch-route.test.mjs +101 -0
- package/src/cli/dispatch-slot.mjs +2 -2
- package/src/cli/dispatch-slot.test.mjs +18 -19
- package/src/cli/dispatch.test.mjs +3 -3
- package/src/cli/e2e-affected.mjs +15 -0
- package/src/cli/e2e-affected.test.mjs +73 -0
- package/src/cli/fleet.mjs +180 -0
- package/src/cli/fleet.test.mjs +213 -0
- package/src/cli/gate-slot.mjs +2 -1
- package/src/cli/gh-merge-command.mjs +96 -31
- package/src/cli/gh-merge-command.test.mjs +74 -0
- package/src/cli/gh-run.mjs +64 -3
- package/src/cli/gh-run.test.mjs +66 -1
- package/src/cli/git-hooks.test.mjs +25 -3
- package/src/cli/git-read.mjs +2 -1
- package/src/cli/integrate-tree.mjs +47 -0
- package/src/cli/integrate.mjs +143 -40
- package/src/cli/integrate.test.mjs +347 -1
- package/src/cli/integration-lane.mjs +62 -0
- package/src/cli/integration-lane.test.mjs +213 -0
- package/src/cli/ledger.mjs +4 -2
- package/src/cli/ledger.test.mjs +12 -3
- package/src/cli/legs.mjs +53 -0
- package/src/cli/legs.test.mjs +70 -0
- package/src/cli/lint-changed.mjs +59 -10
- package/src/cli/lint-changed.test.mjs +112 -1
- package/src/cli/locked-tests.mjs +134 -0
- package/src/cli/locked-tests.test.mjs +188 -0
- package/src/cli/merge-guard.mjs +234 -62
- package/src/cli/merge-guard.test.mjs +768 -39
- package/src/cli/metrics.mjs +53 -0
- package/src/cli/metrics.test.mjs +73 -0
- package/src/cli/milestone.mjs +297 -0
- package/src/cli/milestone.test.mjs +294 -0
- package/src/cli/mutate.mjs +203 -0
- package/src/cli/mutate.test.mjs +134 -0
- package/src/cli/output-redaction.mjs +26 -0
- package/src/cli/output-redaction.test.mjs +117 -0
- package/src/cli/overview.mjs +131 -0
- package/src/cli/overview.test.mjs +137 -0
- package/src/cli/pause.mjs +93 -0
- package/src/cli/pause.test.mjs +109 -0
- package/src/cli/plan-check.mjs +84 -12
- package/src/cli/plan-check.test.mjs +152 -2
- package/src/cli/plan-stop.mjs +7 -4
- package/src/cli/plan-stop.test.mjs +87 -0
- package/src/cli/platform-risk.mjs +46 -0
- package/src/cli/platform-risk.test.mjs +38 -0
- package/src/cli/plugin-skills.test.mjs +25 -0
- package/src/cli/pr-guard.mjs +128 -0
- package/src/cli/pr-guard.test.mjs +154 -0
- package/src/cli/promotion-branch.mjs +2 -0
- package/src/cli/qc.mjs +160 -27
- package/src/cli/qc.test.mjs +93 -0
- package/src/cli/report-stop.mjs +5 -0
- package/src/cli/report-stop.test.mjs +23 -1
- package/src/cli/retry-failed.mjs +3 -1
- package/src/cli/review-gate.mjs +24 -6
- package/src/cli/review-gate.test.mjs +116 -0
- package/src/cli/review-promotion.mjs +107 -0
- package/src/cli/review-promotion.test.mjs +251 -0
- package/src/cli/role-budget.mjs +50 -0
- package/src/cli/role-budget.test.mjs +91 -0
- package/src/cli/run-log.mjs +73 -0
- package/src/cli/run-log.test.mjs +121 -0
- package/src/cli/secret-patterns.mjs +16 -0
- package/src/cli/secret-patterns.test.mjs +73 -0
- package/src/cli/setup-block.mjs +24 -0
- package/src/cli/setup-block.test.mjs +96 -0
- package/src/cli/setup-checks.mjs +65 -0
- package/src/cli/setup-checks.test.mjs +158 -0
- package/src/cli/setup-compaction.mjs +55 -0
- package/src/cli/setup-compaction.test.mjs +115 -0
- package/src/cli/setup-skills.mjs +65 -0
- package/src/cli/setup-skills.test.mjs +171 -0
- package/src/cli/setup.mjs +107 -0
- package/src/cli/setup.test.mjs +275 -0
- package/src/cli/skill-length.test.mjs +8 -0
- package/src/cli/spawn-command.mjs +47 -0
- package/src/cli/spawn-command.test.mjs +64 -0
- package/src/cli/task-gate-review.test.mjs +21 -2
- package/src/cli/task-gate.mjs +25 -12
- package/src/cli/task-gate.test.mjs +43 -5
- package/src/cli/tokens.mjs +46 -3
- package/src/cli/tokens.test.mjs +103 -3
- package/src/cli/tools.mjs +49 -0
- package/src/cli/tools.test.mjs +89 -0
- package/src/cli/transcript-usage.mjs +27 -0
- package/src/cli/transcript-usage.test.mjs +34 -0
- package/src/cli/user-config-package.mjs +1 -1
- package/src/cli/user-config-toml.mjs +63 -7
- package/src/cli/user-config.mjs +39 -7
- package/src/cli/user-config.test.mjs +238 -5
- package/src/cli/watch-arm.mjs +39 -0
- package/src/cli/watch-arm.test.mjs +87 -0
- package/src/cli/watch.mjs +80 -35
- package/src/cli/watch.test.mjs +233 -27
- package/src/cli/work-order-guard.mjs +2 -1
- package/src/cli/workflow-hooks.test.mjs +18 -4
- package/src/cli/workflow-settings.mjs +44 -8
- package/src/cli/workflow-settings.test.mjs +124 -1
- package/src/cli/worktree.mjs +14 -1
- package/src/cli/worktree.test.mjs +20 -1
- package/src/config.mjs +174 -7
- package/src/config.test.mjs +327 -5
- package/src/consumer-package.mjs +31 -0
- package/src/consumer-package.test.mjs +54 -0
- package/src/descriptions.mjs +2 -0
- package/src/eslint/index.mjs +12 -0
- package/src/eslint/jsx.mjs +20 -0
- package/src/eslint/preset.mjs +26 -2
- package/src/eslint/preset.test.mjs +104 -2
- package/src/eslint/rules/no-arbitrary-class-value.mjs +32 -0
- package/src/eslint/rules/no-literal-color.mjs +94 -0
- package/src/eslint/rules/no-static-inline-style.mjs +41 -0
- package/src/eslint/rules/one-component-per-file.mjs +70 -0
- package/src/eslint/rules/raw-element-in-primitive.mjs +31 -0
- package/src/eslint/rules/timeless-comment.mjs +92 -0
- package/src/eslint/rules.test.mjs +189 -0
- package/src/eslint/ui-regressions.test.mjs +39 -0
- package/src/gates/contract-operations.mjs +3 -3
- package/src/gates/contract-operations.test.mjs +28 -0
- package/src/gates/feature-switches.mjs +19 -0
- package/src/gates/feature-switches.test.mjs +55 -0
- package/src/gates/plain-language.mjs +396 -0
- package/src/gates/plain-language.test.mjs +210 -0
- package/src/gates/runtime-imports.test.mjs +114 -0
- package/src/gates/saga-key.test.mjs +1 -1
- package/src/global-skills.mjs +9 -0
- package/src/global-skills.test.mjs +53 -0
- package/src/plugin.test.mjs +3 -2
- package/src/ui/class-sites.mjs +38 -6
- package/src/ui/codemod.mjs +4 -1
- package/src/ui/codemod.test.mjs +52 -0
- package/src/ui/components.mjs +5 -3
- package/src/ui/components.test.mjs +8 -0
- package/src/ui/dupes.test.mjs +18 -0
- package/templates/docs/enforcement.md +12 -2
- package/templates/docs/ui.md +33 -0
- package/templates/githooks/pre-commit +1 -0
- package/templates/githooks/pre-push +11 -2
- package/templates/github/workflows/ci.yml +57 -3
- package/user-config/claude/CLAUDE.core.md +28 -0
- package/user-config/claude/settings.shared.json +41 -1
- package/src/cli/screens-discovery.mjs +0 -60
- package/src/cli/screens.mjs +0 -84
- package/src/cli/stack.mjs +0 -14
- package/src/cli/throwaway-db.mjs +0 -127
- package/src/cli/throwaway-db.test.mjs +0 -83
- package/src/screens/auth-context.test.mjs +0 -41
- package/src/screens/auth-run.test.mjs +0 -147
- package/src/screens/auth.mjs +0 -223
- package/src/screens/auth.test.mjs +0 -76
- package/src/screens/capture-idle.test.mjs +0 -27
- package/src/screens/capture.browser-test.mjs +0 -73
- package/src/screens/capture.mjs +0 -175
- package/src/screens/capture.test.mjs +0 -28
- package/src/screens/catalog.browser-test.mjs +0 -32
- package/src/screens/catalog.mjs +0 -72
- package/src/screens/catalog.test.mjs +0 -36
- package/src/screens/chrome.mjs +0 -21
- package/src/screens/compare.mjs +0 -40
- package/src/screens/compare.test.mjs +0 -30
- package/src/screens/discovery.browser-test.mjs +0 -29
- package/src/screens/discovery.mjs +0 -158
- package/src/screens/discovery.test.mjs +0 -27
- package/src/screens/inventory.mjs +0 -69
- package/src/screens/inventory.test.mjs +0 -10
- package/src/screens/map.mjs +0 -63
- package/src/screens/map.test.mjs +0 -35
- package/src/screens/open.browser-test.mjs +0 -50
- package/src/screens/regions.mjs +0 -137
- package/src/screens/regions.test.mjs +0 -38
- package/src/screens/session-files.mjs +0 -39
- package/src/screens/session-files.test.mjs +0 -39
- package/src/screens/setup.browser-test.mjs +0 -49
- package/src/screens/setup.mjs +0 -78
- package/src/screens/stack-auth.browser-test.mjs +0 -30
- package/src/screens/stack-auth.mjs +0 -106
- package/src/screens/stack-auth.test.mjs +0 -65
- package/src/screens/write-guard.mjs +0 -26
- package/src/stack/stack.mjs +0 -131
- package/src/stack/stack.test.mjs +0 -40
- package/src/stack/supervisor.mjs +0 -30
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Keep the limit record separate so the runner and status command share one source.
|
|
2
|
+
import { lstatSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
export function limitFileOf(env = process.env, tmp = os.tmpdir()) {
|
|
8
|
+
return env.QC_CODEX_LIMIT_FILE || path.join(tmp, "architecture-kit", `codex-${process.getuid?.() ?? os.userInfo().username}`, "codex-limit.json");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function resetTimeOf(line, now) {
|
|
12
|
+
const relative = line.split(/\btry\s+again\s+in\b/i).slice(1).at(-1);
|
|
13
|
+
if (relative) {
|
|
14
|
+
const units = { d: 86_400_000, h: 3_600_000, m: 60_000, s: 1000, ms: 1 };
|
|
15
|
+
let duration = 0;
|
|
16
|
+
for (const run of relative.matchAll(/(?<![\p{L}\p{N}.])(?:\d+(?:\.\d+)?\s*(?:ms|days?|hours?|minutes?|seconds?|[dhms]))+(?![\p{L}\p{N}])/giu)) {
|
|
17
|
+
for (const match of run[0].matchAll(/(\d+(?:\.\d+)?)\s*([a-z]+)/gi)) {
|
|
18
|
+
const unit = match[2].toLowerCase();
|
|
19
|
+
duration += Number(match[1]) * units[unit === "ms" ? unit : unit[0]];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const until = now + Math.ceil(duration / 1000) * 1000;
|
|
23
|
+
if (Number.isFinite(until) && until > now) return until;
|
|
24
|
+
}
|
|
25
|
+
const absolute = /\bat\s+(.+)/i.exec(line);
|
|
26
|
+
if (!absolute) return null;
|
|
27
|
+
const time = absolute[1].trim().replace(/\.$/, "").replace(/\b(\d+)(?:st|nd|rd|th)\b/gi, "$1");
|
|
28
|
+
let until;
|
|
29
|
+
if (/^\d{1,2}:\d{2}\s*[AP]M$/i.test(time)) {
|
|
30
|
+
const today = new Date(now);
|
|
31
|
+
until = Date.parse(`${today.toDateString()} ${time}`);
|
|
32
|
+
if (until <= now) {
|
|
33
|
+
today.setDate(today.getDate() + 1);
|
|
34
|
+
until = Date.parse(`${today.toDateString()} ${time}`);
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
until = Date.parse(time);
|
|
38
|
+
}
|
|
39
|
+
return Number.isFinite(until) && until > now ? until : null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Branch: refuses a shared or redirected folder before the limit state is read or written. */
|
|
43
|
+
function validateLimitFolder(file) {
|
|
44
|
+
const folder = lstatSync(path.dirname(file));
|
|
45
|
+
if (!folder.isDirectory() || folder.isSymbolicLink() ||
|
|
46
|
+
(process.platform !== "win32" && (folder.uid !== process.getuid() || (folder.mode & 0o022) !== 0))) {
|
|
47
|
+
throw new Error(`unsafe limit folder: ${path.dirname(file)}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function writeLimit(file, { until, source }, now = Date.now()) {
|
|
52
|
+
const record = { until: new Date(until).toISOString(), source, recordedAt: new Date(now).toISOString() };
|
|
53
|
+
mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
54
|
+
validateLimitFolder(file);
|
|
55
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
56
|
+
try {
|
|
57
|
+
writeFileSync(temporary, `${JSON.stringify(record)}\n`, { flag: "wx", mode: 0o600 });
|
|
58
|
+
renameSync(temporary, file);
|
|
59
|
+
} finally {
|
|
60
|
+
rmSync(temporary, { force: true });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function readLimit(file, now = Date.now()) {
|
|
65
|
+
try {
|
|
66
|
+
validateLimitFolder(file);
|
|
67
|
+
const record = JSON.parse(readFileSync(file, "utf8"));
|
|
68
|
+
if (typeof record?.until !== "string" || !["codex", "default", "wait"].includes(record.source)) return null;
|
|
69
|
+
const until = new Date(record.until);
|
|
70
|
+
return until.getTime() > now ? { until, source: record.source } : null;
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function runCodexStatus(args, { env = process.env, tmp = os.tmpdir(), now = Date.now(), out = console.log } = {}) {
|
|
77
|
+
const limit = readLimit(limitFileOf(env, tmp), now);
|
|
78
|
+
if (limit) {
|
|
79
|
+
const source = { codex: "reset time from Codex", default: "default: 60 minutes after the limit", wait: "reset wait bound" }[limit.source];
|
|
80
|
+
out(`Codex: limited until ${limit.until.toISOString()} (${source})`);
|
|
81
|
+
} else {
|
|
82
|
+
out("Codex: free");
|
|
83
|
+
}
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { chmodSync, linkSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { test } from "node:test";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
const T = Date.UTC(2026, 9, 26);
|
|
10
|
+
const limit = () => import("./codex-limit.mjs");
|
|
11
|
+
const QC = fileURLToPath(new URL("./qc.mjs", import.meta.url));
|
|
12
|
+
|
|
13
|
+
function setup(t) {
|
|
14
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), "qc-codex-limit-"));
|
|
15
|
+
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
16
|
+
return { dir, file: path.join(dir, "state", "limit.json") };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test("limitFileOf uses the override or its private folder in the temporary directory", async () => {
|
|
20
|
+
const { limitFileOf } = await limit();
|
|
21
|
+
assert.equal(limitFileOf({ QC_CODEX_LIMIT_FILE: "custom.json" }, "tmp"), "custom.json");
|
|
22
|
+
const folder = `codex-${process.getuid?.() ?? os.userInfo().username}`;
|
|
23
|
+
assert.equal(limitFileOf({}, "tmp"), path.join("tmp", "architecture-kit", folder, "codex-limit.json"));
|
|
24
|
+
assert.equal(limitFileOf({ QC_CODEX_LIMIT_FILE: "" }, "tmp"), path.join("tmp", "architecture-kit", folder, "codex-limit.json"));
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("the private limit folder works under a group-writable shared parent", { skip: process.platform === "win32" }, async (t) => {
|
|
28
|
+
const { limitFileOf, readLimit, writeLimit } = await limit();
|
|
29
|
+
const { dir } = setup(t);
|
|
30
|
+
const parent = path.join(dir, "architecture-kit");
|
|
31
|
+
mkdirSync(parent);
|
|
32
|
+
chmodSync(parent, 0o775);
|
|
33
|
+
const file = limitFileOf({}, dir);
|
|
34
|
+
writeLimit(file, { until: T + 1000, source: "codex" }, T);
|
|
35
|
+
assert.deepEqual(readLimit(file, T), { until: new Date(T + 1000), source: "codex" });
|
|
36
|
+
assert.equal(statSync(parent).mode & 0o777, 0o775);
|
|
37
|
+
assert.equal(statSync(path.dirname(file)).mode & 0o777, 0o700);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("resetTimeOf adds every relative day, hour, and minute unit regardless of case", async () => {
|
|
41
|
+
const { resetTimeOf } = await limit();
|
|
42
|
+
assert.equal(resetTimeOf("Try again in 2 hours 13 minutes.", T), T + 133 * 60_000);
|
|
43
|
+
assert.equal(resetTimeOf("TRY AGAIN IN 1 DAY 2 HOURS 3 MINUTES 4 minutes.", T), T + 1567 * 60_000);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("resetTimeOf parses an absolute date with an ordinal suffix and a trailing period", async () => {
|
|
47
|
+
const { resetTimeOf } = await limit();
|
|
48
|
+
assert.equal(resetTimeOf("try again at Oct 29th, 2026 3:12 PM.", T), new Date("Oct 29, 2026 3:12 PM").getTime());
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("resetTimeOf parses seconds and abbreviated relative units", async () => {
|
|
52
|
+
const { resetTimeOf } = await limit();
|
|
53
|
+
for (const [duration, milliseconds] of [
|
|
54
|
+
["20s", 20_000], ["20 seconds", 20_000], ["1 SECOND", 1000],
|
|
55
|
+
["1h 2m 3s", 3_723_000], ["2 hours 13 minutes 20 seconds", 8_000_000],
|
|
56
|
+
]) {
|
|
57
|
+
assert.equal(resetTimeOf(`rate limit: try again in ${duration}`, T), T + milliseconds, duration);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
for (const [duration, seconds] of [["1.898s", 2], ["500ms", 1], ["6m0s", 360], ["1h2m3s", 3723]]) {
|
|
62
|
+
test(`resetTimeOf parses ${duration} and rounds up to a whole second`, async () => {
|
|
63
|
+
const { resetTimeOf } = await limit();
|
|
64
|
+
assert.equal(resetTimeOf(`try again in ${duration}`, T), T + seconds * 1000);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
test("resetTimeOf ignores organization IDs before the last retry hint", async () => {
|
|
69
|
+
const { resetTimeOf } = await limit();
|
|
70
|
+
const line = "Rate limit reached for gpt-4o in organization org-3h7Kq2s9xYz on tokens per min (TPM). Please try again in 1.898s.";
|
|
71
|
+
assert.equal(resetTimeOf(line, T), T + 2000);
|
|
72
|
+
assert.equal(resetTimeOf("Try again in 3h. TRY AGAIN IN 500ms.", T), T + 1000);
|
|
73
|
+
assert.equal(resetTimeOf("Rate limit in 3h without a retry hint.", T), null);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("resetTimeOf excludes durations embedded in IDs or decimal fragments", async () => {
|
|
77
|
+
const { resetTimeOf } = await limit();
|
|
78
|
+
for (const fragment of ["org-3h7Kq2s9xYz", "abc20s", ".898s", "1.2.3s", "é20s", "3h7K"]) {
|
|
79
|
+
assert.equal(resetTimeOf(`Try again in ${fragment}.`, T), null, fragment);
|
|
80
|
+
assert.equal(resetTimeOf(`Try again in ${fragment}; 500ms.`, T), T + 1000, fragment);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("resetTimeOf uses today or tomorrow for a bare clock time", async () => {
|
|
85
|
+
const { resetTimeOf } = await limit();
|
|
86
|
+
const morning = new Date(2026, 9, 26, 10).getTime();
|
|
87
|
+
const evening = new Date(2026, 9, 26, 20).getTime();
|
|
88
|
+
assert.equal(resetTimeOf("Try again at 3:12 PM.", morning), new Date(2026, 9, 26, 15, 12).getTime());
|
|
89
|
+
assert.equal(resetTimeOf("Try again at 3:12 PM.", evening), new Date(2026, 9, 27, 15, 12).getTime());
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
for (const line of ["You have hit your usage limit", "Try again at a mystery time.", "Try again at Oct 20th, 2026 3:12 PM."]) {
|
|
93
|
+
test(`resetTimeOf returns null for ${line}`, async () => {
|
|
94
|
+
const { resetTimeOf } = await limit();
|
|
95
|
+
assert.equal(resetTimeOf(line, T), null);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
test("readLimit returns null for a missing file, bad JSON, an invalid shape, or an expired time", async (t) => {
|
|
100
|
+
const { readLimit } = await limit();
|
|
101
|
+
const { dir } = setup(t);
|
|
102
|
+
const file = path.join(dir, "limit.json");
|
|
103
|
+
assert.equal(readLimit(file, T), null);
|
|
104
|
+
for (const value of ["bad json", "null", "{}", '{"until":"invalid"}', JSON.stringify({ until: new Date(T).toISOString(), source: "codex" })]) {
|
|
105
|
+
writeFileSync(file, value);
|
|
106
|
+
assert.equal(readLimit(file, T), null, value);
|
|
107
|
+
}
|
|
108
|
+
assert.equal(readLimit(dir, T), null);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("writeLimit creates the folder and stores ISO times, and readLimit returns a Date and source", async (t) => {
|
|
112
|
+
const { readLimit, writeLimit } = await limit();
|
|
113
|
+
const { file } = setup(t);
|
|
114
|
+
const until = T + 3_600_000;
|
|
115
|
+
writeLimit(file, { until, source: "codex" }, T);
|
|
116
|
+
assert.deepEqual(JSON.parse(readFileSync(file, "utf8")), {
|
|
117
|
+
until: new Date(until).toISOString(), source: "codex", recordedAt: new Date(T).toISOString(),
|
|
118
|
+
});
|
|
119
|
+
assert.deepEqual(readLimit(file, T), { until: new Date(until), source: "codex" });
|
|
120
|
+
if (process.platform !== "win32") assert.equal(statSync(path.dirname(file)).mode & 0o777, 0o700);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("writeLimit refuses a symbolic link for its state folder", async (t) => {
|
|
124
|
+
const { writeLimit } = await limit();
|
|
125
|
+
const { dir, file } = setup(t);
|
|
126
|
+
const target = path.join(dir, "other-user");
|
|
127
|
+
mkdirSync(target);
|
|
128
|
+
symlinkSync(target, path.dirname(file), process.platform === "win32" ? "junction" : "dir");
|
|
129
|
+
assert.throws(() => writeLimit(file, { until: T + 1000, source: "codex" }, T), /unsafe limit folder/i);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("writeLimit replaces a state-file link without changing its target", async (t) => {
|
|
133
|
+
const { writeLimit, readLimit } = await limit();
|
|
134
|
+
const { dir, file } = setup(t);
|
|
135
|
+
mkdirSync(path.dirname(file), { mode: 0o700 });
|
|
136
|
+
const target = path.join(dir, "target.txt");
|
|
137
|
+
writeFileSync(target, "keep");
|
|
138
|
+
linkSync(target, file);
|
|
139
|
+
writeLimit(file, { until: T + 1000, source: "codex" }, T);
|
|
140
|
+
assert.equal(readFileSync(target, "utf8"), "keep");
|
|
141
|
+
assert.ok(readLimit(file, T));
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
for (const [source, suffix] of [["codex", "reset time from Codex"], ["default", "default: 60 minutes after the limit"], ["wait", "reset wait bound"]]) {
|
|
145
|
+
test(`runCodexStatus prints the limit with source ${source} and returns zero`, async (t) => {
|
|
146
|
+
const { readLimit, runCodexStatus, writeLimit } = await limit();
|
|
147
|
+
const { file } = setup(t);
|
|
148
|
+
writeLimit(file, { until: T + 3_600_000, source }, T);
|
|
149
|
+
assert.deepEqual(readLimit(file, T), { until: new Date(T + 3_600_000), source });
|
|
150
|
+
const lines = [];
|
|
151
|
+
assert.equal(runCodexStatus([], { env: { QC_CODEX_LIMIT_FILE: file }, now: T, out: (line) => lines.push(line) }), 0);
|
|
152
|
+
assert.deepEqual(lines, [`Codex: limited until ${new Date(T + 3_600_000).toISOString()} (${suffix})`]);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
test("runCodexStatus prints free for missing and expired state and returns zero", async (t) => {
|
|
157
|
+
const { runCodexStatus, writeLimit } = await limit();
|
|
158
|
+
const { dir, file } = setup(t);
|
|
159
|
+
const lines = [];
|
|
160
|
+
const options = { env: {}, tmp: dir, now: T, out: (line) => lines.push(line) };
|
|
161
|
+
assert.equal(runCodexStatus([], options), 0);
|
|
162
|
+
writeLimit(file, { until: T - 1, source: "default" }, T - 3_600_000);
|
|
163
|
+
assert.equal(runCodexStatus([], { ...options, env: { QC_CODEX_LIMIT_FILE: file } }), 0);
|
|
164
|
+
assert.deepEqual(lines, ["Codex: free", "Codex: free"]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("qc codex status runs with a broken config, and other codex commands print usage and exit one", (t) => {
|
|
168
|
+
const { dir, file } = setup(t);
|
|
169
|
+
writeFileSync(path.join(dir, "qc.config.json"), "bad json");
|
|
170
|
+
const options = { cwd: dir, env: { ...process.env, QC_CODEX_LIMIT_FILE: file }, encoding: "utf8" };
|
|
171
|
+
const status = spawnSync(process.execPath, [QC, "codex", "status"], options);
|
|
172
|
+
assert.equal(status.status, 0, status.stderr);
|
|
173
|
+
assert.equal(status.stdout.trim(), "Codex: free");
|
|
174
|
+
for (const rest of [[], ["unknown"]]) {
|
|
175
|
+
const result = spawnSync(process.execPath, [QC, "codex", ...rest], options);
|
|
176
|
+
assert.equal(result.status, 1);
|
|
177
|
+
assert.match(result.stdout + result.stderr, /qc codex status/);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
2
|
+
|
|
3
|
+
const STATUS_URL = "https://codex-resets.com/api/v1/status";
|
|
4
|
+
const MINUTE_MS = 60 * 1000;
|
|
5
|
+
const HOUR_MS = 60 * 60 * 1000;
|
|
6
|
+
const MAX_TIMER_MS = 2147483647;
|
|
7
|
+
const MAX_POLL_MINUTES = Math.floor(MAX_TIMER_MS / MINUTE_MS);
|
|
8
|
+
const USAGE = "usage: qc codex-wait-reset --since <iso> [--max-hours <n>] [--poll-minutes <n>]\nexit 0 on a reset, 1 at the bound or on bad input";
|
|
9
|
+
|
|
10
|
+
/** Branch: validates the shared poll interval before a request starts. */
|
|
11
|
+
export function parsePollMinutes(value = 30) {
|
|
12
|
+
const minutes = Number(value);
|
|
13
|
+
if (!Number.isFinite(minutes) || minutes <= 0 || minutes > MAX_POLL_MINUTES) {
|
|
14
|
+
throw new Error(`--poll-minutes must be a positive finite number up to ${MAX_POLL_MINUTES} minutes`);
|
|
15
|
+
}
|
|
16
|
+
return minutes;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Branch: validates the shared wait bound before a request starts. */
|
|
20
|
+
export function parseMaxHours(value = 6) {
|
|
21
|
+
const hours = Number(value);
|
|
22
|
+
if (!Number.isFinite(hours) || hours <= 0 || !Number.isFinite(hours * HOUR_MS)) {
|
|
23
|
+
throw new Error("--max-hours must be a positive finite number");
|
|
24
|
+
}
|
|
25
|
+
return hours;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Pipeline: polls without a model until a reset follows the usage limit or the bound expires. */
|
|
29
|
+
export async function waitForCodexReset({
|
|
30
|
+
since, maxHours = 6, pollMinutes = 30, fetch = globalThis.fetch, now = Date.now, sleep: pause = sleep,
|
|
31
|
+
say = (line) => console.error(line),
|
|
32
|
+
}) {
|
|
33
|
+
const hit = Date.parse(since);
|
|
34
|
+
if (!Number.isFinite(hit)) throw new Error("--since must be an ISO timestamp");
|
|
35
|
+
const deadline = now() + parseMaxHours(maxHours) * HOUR_MS;
|
|
36
|
+
const pollMs = parsePollMinutes(pollMinutes) * MINUTE_MS;
|
|
37
|
+
say(`qc codex-wait-reset: waiting for a reset after ${since}`);
|
|
38
|
+
while (now() < deadline) {
|
|
39
|
+
try {
|
|
40
|
+
const signal = AbortSignal.timeout(Math.max(1, Math.min(30000, Math.ceil(deadline - now()))));
|
|
41
|
+
const response = await fetch(STATUS_URL, { signal });
|
|
42
|
+
if (!response.ok) throw new Error(`status endpoint returned HTTP ${response.status}`);
|
|
43
|
+
const body = await response.json();
|
|
44
|
+
const announced = Date.parse(body?.data?.latest_reset?.announced_at);
|
|
45
|
+
if (now() < deadline && announced > hit) {
|
|
46
|
+
say(`qc codex-wait-reset: reset announced at ${body.data.latest_reset.announced_at}`);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
} catch (error) {
|
|
50
|
+
say(`qc codex-wait-reset: ${error.message}; polling continues`);
|
|
51
|
+
}
|
|
52
|
+
const remaining = deadline - now();
|
|
53
|
+
if (remaining > 0) await pause(Math.min(pollMs, remaining));
|
|
54
|
+
}
|
|
55
|
+
say("qc codex-wait-reset: wait bound reached");
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Trigger: runs the standalone wait command with injectable network and clock functions. */
|
|
60
|
+
export async function runCodexWaitReset(argv, { say = (line) => console.error(line), ...options } = {}) {
|
|
61
|
+
if (argv.length === 1 && argv[0] === "--help") { say(USAGE); return 0; }
|
|
62
|
+
try {
|
|
63
|
+
const flags = {};
|
|
64
|
+
for (let index = 0; index < argv.length; index += 2) {
|
|
65
|
+
const flag = argv[index];
|
|
66
|
+
if (flag !== "--since" && flag !== "--max-hours" && flag !== "--poll-minutes") throw new Error(`unknown argument ${flag}`);
|
|
67
|
+
if (argv[index + 1] === undefined) throw new Error(`${flag} needs a value`);
|
|
68
|
+
flags[flag] = argv[index + 1];
|
|
69
|
+
}
|
|
70
|
+
return await waitForCodexReset({
|
|
71
|
+
...options, since: flags["--since"], maxHours: parseMaxHours(flags["--max-hours"]),
|
|
72
|
+
pollMinutes: parsePollMinutes(flags["--poll-minutes"]), say,
|
|
73
|
+
}) ? 0 : 1;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
say(`qc codex-wait-reset: ${error.message}\n${USAGE}`);
|
|
76
|
+
return 1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { test } from "node:test";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { runCodexWaitReset, waitForCodexReset } from "./codex-reset.mjs";
|
|
6
|
+
|
|
7
|
+
const SINCE = "2026-01-01T00:00:00.000Z";
|
|
8
|
+
const INTERVAL = 30 * 60 * 1000;
|
|
9
|
+
const quiet = () => {};
|
|
10
|
+
|
|
11
|
+
function clock() {
|
|
12
|
+
let time = Date.parse(SINCE);
|
|
13
|
+
const sleeps = [];
|
|
14
|
+
return {
|
|
15
|
+
now: () => time,
|
|
16
|
+
sleeps,
|
|
17
|
+
sleep: async (ms) => { sleeps.push(ms); time += ms; },
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function response(announcedAt) {
|
|
22
|
+
return { ok: true, json: async () => ({ data: { latest_reset: { announced_at: announcedAt } } }) };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
test("a reset strictly after the limit is found on the second poll", async () => {
|
|
26
|
+
const timer = clock();
|
|
27
|
+
const calls = [];
|
|
28
|
+
const fetch = async (url) => {
|
|
29
|
+
calls.push(url);
|
|
30
|
+
return response(calls.length === 1 ? SINCE : "2026-01-01T00:15:00.000Z");
|
|
31
|
+
};
|
|
32
|
+
assert.equal(await waitForCodexReset({ since: SINCE, fetch, ...timer, say: quiet }), true);
|
|
33
|
+
assert.deepEqual(calls, Array(2).fill("https://codex-resets.com/api/v1/status"));
|
|
34
|
+
assert.deepEqual(timer.sleeps, [INTERVAL]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("the bound stops polling and clips the final sleep", async () => {
|
|
38
|
+
const timer = clock();
|
|
39
|
+
let calls = 0;
|
|
40
|
+
const fetch = async () => { calls += 1; return response(SINCE); };
|
|
41
|
+
assert.equal(await waitForCodexReset({ since: SINCE, maxHours: 0.6, fetch, ...timer, say: quiet }), false);
|
|
42
|
+
assert.equal(calls, 2);
|
|
43
|
+
assert.deepEqual(timer.sleeps, [INTERVAL, 6 * 60 * 1000]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("a long bound pauses for the poll interval and clips the final pause", async () => {
|
|
47
|
+
const timer = clock();
|
|
48
|
+
let calls = 0;
|
|
49
|
+
const fetch = async () => { calls += 1; return response(SINCE); };
|
|
50
|
+
assert.equal(await waitForCodexReset({
|
|
51
|
+
since: SINCE, maxHours: 1200, pollMinutes: 35791, fetch, ...timer, say: quiet,
|
|
52
|
+
}), false);
|
|
53
|
+
assert.equal(calls, 3);
|
|
54
|
+
assert.deepEqual(timer.sleeps, [2147460000, 2147460000, 25080000]);
|
|
55
|
+
assert.equal(timer.now() - Date.parse(SINCE), 1200 * 60 * 60 * 1000);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("a fetch error keeps polling", async () => {
|
|
59
|
+
const timer = clock();
|
|
60
|
+
let calls = 0;
|
|
61
|
+
const messages = [];
|
|
62
|
+
const fetch = async () => {
|
|
63
|
+
calls += 1;
|
|
64
|
+
if (calls === 1) throw new Error("offline");
|
|
65
|
+
return response("2026-01-01T00:15:00.000Z");
|
|
66
|
+
};
|
|
67
|
+
assert.equal(await waitForCodexReset({ since: SINCE, fetch, ...timer, say: (line) => messages.push(line) }), true);
|
|
68
|
+
assert.equal(calls, 2);
|
|
69
|
+
assert.deepEqual(timer.sleeps, [INTERVAL]);
|
|
70
|
+
assert.match(messages.join("\n"), /offline/);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("the standalone command exits zero on a reset and one at its bound", async () => {
|
|
74
|
+
assert.equal(await runCodexWaitReset(["--since", SINCE], {
|
|
75
|
+
...clock(), fetch: async () => response("2026-01-01T00:15:00.000Z"), say: quiet,
|
|
76
|
+
}), 0);
|
|
77
|
+
const timer = clock();
|
|
78
|
+
let calls = 0;
|
|
79
|
+
assert.equal(await runCodexWaitReset(["--since", SINCE], {
|
|
80
|
+
...timer, fetch: async () => { calls += 1; return response(SINCE); }, say: quiet,
|
|
81
|
+
}), 1);
|
|
82
|
+
assert.equal(calls, 12);
|
|
83
|
+
assert.equal(timer.now() - Date.parse(SINCE), 6 * 60 * 60 * 1000);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("invalid wait arguments fail before fetching", async () => {
|
|
87
|
+
for (const argv of [[], ["--since"], ["--since", "invalid"], ["--since", SINCE, "--unknown", "1"],
|
|
88
|
+
["--since", SINCE, "--max-hours"], ...["0", "-1", "Infinity", "NaN"].map((hours) => ["--since", SINCE, "--max-hours", hours])]) {
|
|
89
|
+
assert.equal(await runCodexWaitReset(argv, { fetch: async () => assert.fail("no fetch"), say: quiet }), 1);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
for (const minutes of ["0.5", "7.5", "90"]) {
|
|
94
|
+
test(`the standalone command polls every ${minutes} minutes`, async () => {
|
|
95
|
+
const timer = clock();
|
|
96
|
+
let calls = 0;
|
|
97
|
+
assert.equal(await runCodexWaitReset(["--since", SINCE, "--poll-minutes", minutes], {
|
|
98
|
+
...timer, say: quiet,
|
|
99
|
+
fetch: async () => response(++calls === 1 ? SINCE : "2026-01-01T00:00:01.000Z"),
|
|
100
|
+
}), 0);
|
|
101
|
+
assert.equal(calls, 2);
|
|
102
|
+
assert.deepEqual(timer.sleeps, [Number(minutes) * 60_000]);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
test("an invalid poll interval fails before fetching with usage", async () => {
|
|
107
|
+
for (const value of [undefined, "0", "-1", "Infinity", "-Infinity", "NaN", "invalid", "", " ", "1e308"]) {
|
|
108
|
+
const messages = [];
|
|
109
|
+
const argv = ["--since", SINCE, "--poll-minutes", ...(value === undefined ? [] : [value])];
|
|
110
|
+
assert.equal(await runCodexWaitReset(argv, {
|
|
111
|
+
fetch: async () => assert.fail("no fetch"), say: (line) => messages.push(line),
|
|
112
|
+
}), 1);
|
|
113
|
+
assert.match(messages.join("\n"), /--poll-minutes (must be a positive finite number up to 35791 minutes|needs a value)/);
|
|
114
|
+
assert.match(messages.join("\n"), /usage: qc codex-wait-reset/);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("the maximum poll interval is accepted with a long wait bound", async () => {
|
|
119
|
+
const timer = clock();
|
|
120
|
+
let calls = 0;
|
|
121
|
+
assert.equal(await runCodexWaitReset(["--since", SINCE, "--poll-minutes", "35791", "--max-hours", "1200"], {
|
|
122
|
+
...timer, say: quiet,
|
|
123
|
+
fetch: async () => response(++calls === 1 ? SINCE : "2026-01-01T00:00:01.000Z"),
|
|
124
|
+
}), 0);
|
|
125
|
+
assert.equal(calls, 2);
|
|
126
|
+
assert.deepEqual(timer.sleeps, [35791 * 60_000]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("a poll interval above 35791 minutes fails before fetching", async () => {
|
|
130
|
+
for (const minutes of ["35791.000001", String(2147483647 / 60_000), "35792", "36000"]) {
|
|
131
|
+
const messages = [];
|
|
132
|
+
let calls = 0;
|
|
133
|
+
assert.equal(await runCodexWaitReset(["--since", SINCE, "--poll-minutes", minutes], {
|
|
134
|
+
...clock(), say: (line) => messages.push(line),
|
|
135
|
+
fetch: async () => { calls += 1; return response("2026-01-01T00:00:01.000Z"); },
|
|
136
|
+
}), 1);
|
|
137
|
+
assert.equal(calls, 0);
|
|
138
|
+
assert.match(messages.join("\n"), /--poll-minutes must be a positive finite number up to 35791 minutes/);
|
|
139
|
+
assert.match(messages.join("\n"), /usage: qc codex-wait-reset/);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("the selected poll interval clips its final sleep at the bound", async () => {
|
|
144
|
+
const timer = clock();
|
|
145
|
+
let calls = 0;
|
|
146
|
+
assert.equal(await runCodexWaitReset(["--since", SINCE, "--poll-minutes", "7.5", "--max-hours", "0.2"], {
|
|
147
|
+
...timer, say: quiet, fetch: async () => { calls += 1; return response(SINCE); },
|
|
148
|
+
}), 1);
|
|
149
|
+
assert.equal(calls, 2);
|
|
150
|
+
assert.deepEqual(timer.sleeps, [7.5 * 60_000, 4.5 * 60_000]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("HTTP errors, invalid JSON, and missing reset data keep polling", async () => {
|
|
154
|
+
const timer = clock();
|
|
155
|
+
const responses = [
|
|
156
|
+
{ ok: false, status: 503 },
|
|
157
|
+
{ ok: true, json: async () => { throw new Error("invalid JSON"); } },
|
|
158
|
+
{ ok: true, json: async () => ({}) },
|
|
159
|
+
response("invalid"),
|
|
160
|
+
response("2026-01-01T01:00:00.000Z"),
|
|
161
|
+
];
|
|
162
|
+
assert.equal(await waitForCodexReset({ since: SINCE, ...timer, fetch: async () => responses.shift(), say: quiet }), true);
|
|
163
|
+
assert.equal(responses.length, 0);
|
|
164
|
+
assert.deepEqual(timer.sleeps, Array(4).fill(INTERVAL));
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("a response after the deadline cannot resume the runner", async () => {
|
|
168
|
+
const timer = clock();
|
|
169
|
+
const fetch = async (_url, { signal }) => {
|
|
170
|
+
assert.ok(signal instanceof AbortSignal);
|
|
171
|
+
await timer.sleep(INTERVAL);
|
|
172
|
+
return response("2026-01-01T00:15:00.000Z");
|
|
173
|
+
};
|
|
174
|
+
assert.equal(await waitForCodexReset({ since: SINCE, maxHours: 0.25, ...timer, fetch, say: quiet }), false);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("the dispatcher returns after fetch and exposes the wait command in help", () => {
|
|
178
|
+
const cli = new URL("./qc.mjs", import.meta.url).href;
|
|
179
|
+
const source = `globalThis.fetch = async () => ({ ok: true, json: async () => ({ data: { latest_reset: { announced_at: '2026-01-01T00:15:00.000Z' } } }) });
|
|
180
|
+
process.exit = () => { throw new Error('forced exit after fetch'); };
|
|
181
|
+
process.argv = [process.execPath, ${JSON.stringify(cli)}, 'codex-wait-reset', '--since', ${JSON.stringify(SINCE)}];
|
|
182
|
+
await import(${JSON.stringify(cli)});`;
|
|
183
|
+
const result = spawnSync(process.execPath, ["--input-type=module", "-e", source], { encoding: "utf8", timeout: 10000 });
|
|
184
|
+
assert.equal(result.status, 0, result.stderr);
|
|
185
|
+
const help = spawnSync(process.execPath, [fileURLToPath(new URL("./qc.mjs", import.meta.url)), "help"], { encoding: "utf8", timeout: 10000 });
|
|
186
|
+
assert.equal(help.status, 0, help.stderr);
|
|
187
|
+
assert.match(help.stdout, /codex-wait-reset --since <iso> \[--max-hours <n>\]/);
|
|
188
|
+
assert.match(help.stdout, /--wait-reset/);
|
|
189
|
+
assert.match(help.stdout, /codex-implement[^\n]+\[--effort <low\|medium\|high>\][^\n]+\[--poll-minutes <n>\]/);
|
|
190
|
+
assert.match(help.stdout, /codex-wait-reset[^\n]+\[--poll-minutes <n>\]/);
|
|
191
|
+
assert.match(help.stdout, /--wait-reset polls every 30 minutes/);
|
|
192
|
+
});
|