carrick 0.3.143 → 0.3.145
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 +1 -1
- package/bin/carrick.mjs +7 -7
- package/dist/auth/read.d.ts +0 -10
- package/dist/auth/read.js +3 -11
- package/dist/auth/read.js.map +1 -1
- package/dist/cli.d.ts +23 -20
- package/dist/cli.js +17 -32
- package/dist/cli.js.map +1 -1
- package/dist/contract.d.ts +54 -0
- package/dist/contract.js +14 -0
- package/dist/contract.js.map +1 -1
- package/dist/hook/checked.d.ts +11 -0
- package/dist/hook/checked.js +44 -0
- package/dist/hook/checked.js.map +1 -0
- package/dist/hook/edited.d.ts +22 -0
- package/dist/hook/edited.js +84 -0
- package/dist/hook/edited.js.map +1 -0
- package/dist/hook/invoker.d.ts +7 -0
- package/dist/hook/invoker.js +18 -0
- package/dist/hook/invoker.js.map +1 -0
- package/dist/hook/late.d.ts +8 -0
- package/dist/hook/late.js +60 -0
- package/dist/hook/late.js.map +1 -0
- package/dist/hook/post-edit.d.ts +0 -22
- package/dist/hook/post-edit.js +78 -126
- package/dist/hook/post-edit.js.map +1 -1
- package/dist/hook/post-read.js +19 -9
- package/dist/hook/post-read.js.map +1 -1
- package/dist/hook/probe.d.ts +15 -0
- package/dist/hook/probe.js +41 -0
- package/dist/hook/probe.js.map +1 -0
- package/dist/hook/recheck-run.d.ts +2 -0
- package/dist/hook/recheck-run.js +28 -0
- package/dist/hook/recheck-run.js.map +1 -0
- package/dist/hook/rechecks.d.ts +108 -0
- package/dist/hook/rechecks.js +537 -0
- package/dist/hook/rechecks.js.map +1 -0
- package/dist/hook/refresh.d.ts +1 -0
- package/dist/hook/refresh.js +1 -1
- package/dist/hook/refresh.js.map +1 -1
- package/dist/hook/reuse.d.ts +25 -5
- package/dist/hook/reuse.js +61 -14
- package/dist/hook/reuse.js.map +1 -1
- package/dist/hook/session-start.js +14 -0
- package/dist/hook/session-start.js.map +1 -1
- package/dist/hook/stop-wait.d.ts +17 -0
- package/dist/hook/stop-wait.js +120 -0
- package/dist/hook/stop-wait.js.map +1 -0
- package/dist/hook/stop.js +14 -3
- package/dist/hook/stop.js.map +1 -1
- package/dist/hook/user-prompt.js +6 -3
- package/dist/hook/user-prompt.js.map +1 -1
- package/dist/init/agents-section.d.ts +18 -4
- package/dist/init/agents-section.js +66 -18
- package/dist/init/agents-section.js.map +1 -1
- package/dist/init/connect.d.ts +6 -107
- package/dist/init/connect.js +7 -318
- package/dist/init/connect.js.map +1 -1
- package/dist/init/doctor.d.ts +8 -71
- package/dist/init/doctor.js +37 -223
- package/dist/init/doctor.js.map +1 -1
- package/dist/init/hosted.d.ts +7 -7
- package/dist/init/hosted.js +8 -10
- package/dist/init/hosted.js.map +1 -1
- package/dist/init/outdated.d.ts +11 -0
- package/dist/init/outdated.js +1 -1
- package/dist/init/outdated.js.map +1 -1
- package/dist/init/output.d.ts +0 -55
- package/dist/init/output.js +0 -142
- package/dist/init/output.js.map +1 -1
- package/dist/init/projects.d.ts +3 -200
- package/dist/init/projects.js +22 -352
- package/dist/init/projects.js.map +1 -1
- package/dist/init/remove.js +4 -5
- package/dist/init/remove.js.map +1 -1
- package/dist/init/repos.d.ts +25 -53
- package/dist/init/repos.js +28 -65
- package/dist/init/repos.js.map +1 -1
- package/dist/init/run.d.ts +52 -212
- package/dist/init/run.js +252 -988
- package/dist/init/run.js.map +1 -1
- package/dist/init/self-update.d.ts +18 -0
- package/dist/init/self-update.js +147 -0
- package/dist/init/self-update.js.map +1 -0
- package/dist/init/settings.d.ts +2 -0
- package/dist/init/settings.js +13 -2
- package/dist/init/settings.js.map +1 -1
- package/dist/init/task-skills.d.ts +1 -0
- package/dist/init/task-skills.js +1 -1
- package/dist/init/task-skills.js.map +1 -1
- package/dist/render.d.ts +24 -6
- package/dist/render.js +133 -42
- package/dist/render.js.map +1 -1
- package/dist/templates.d.ts +1 -6
- package/dist/templates.js +4 -14
- package/dist/templates.js.map +1 -1
- package/package.json +6 -6
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/hooks/hooks.json +3 -2
- package/sidecar/dist/src/capture/api.d.ts +2 -2
- package/sidecar/dist/src/capture/deno-project.js +5 -1
- package/sidecar/dist/src/index.js +1 -1
- package/sidecar/dist/src/library-claims.d.ts +4 -0
- package/sidecar/dist/src/library-claims.js +2 -2
- package/sidecar/dist/src/project-loader.js +9 -2
- package/sidecar/dist/src/type-inferrer.d.ts +17 -2
- package/sidecar/dist/src/type-inferrer.js +59 -16
- package/sidecar/dist/src/types.d.ts +26 -0
- package/sidecar/dist/src/validators.d.ts +6 -0
- package/sidecar/dist/src/validators.js +2 -0
- package/templates/carrick.json +0 -7
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The background re-check runner: `carrick hook recheck-run <workspace root>`.
|
|
3
|
+
//
|
|
4
|
+
// Started detached by `ensureRunner` (rechecks.ts) from a hook that queued an
|
|
5
|
+
// edit; it works the workspace's queue to empty, one `carrick check --recheck`
|
|
6
|
+
// at a time, and exits. It prints nothing: answers go to session files that the
|
|
7
|
+
// next hook delivers. It exits 0 whatever happens.
|
|
8
|
+
import { createLogger } from "../log.js";
|
|
9
|
+
import { runQueue } from "./rechecks.js";
|
|
10
|
+
const log = createLogger("carrick-recheck-run");
|
|
11
|
+
async function main() {
|
|
12
|
+
// `carrick hook recheck-run <root>`: the bin script leaves `hook` and the name in argv.
|
|
13
|
+
const root = process.argv.slice(2).find((arg) => arg !== "hook" && arg !== "recheck-run");
|
|
14
|
+
if (!root) {
|
|
15
|
+
log("no workspace root given");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const spawner = Number.parseInt(process.env["CARRICK_RECHECK_SPAWNER"] ?? "", 10);
|
|
19
|
+
await runQueue(root, Number.isFinite(spawner) ? { spawner } : {});
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
await main();
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
log("runner failed", String(error));
|
|
26
|
+
}
|
|
27
|
+
process.exit(0);
|
|
28
|
+
//# sourceMappingURL=recheck-run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recheck-run.js","sourceRoot":"","sources":["../../src/hook/recheck-run.ts"],"names":[],"mappings":";AACA,+EAA+E;AAC/E,EAAE;AACF,8EAA8E;AAC9E,+EAA+E;AAC/E,gFAAgF;AAChF,mDAAmD;AAEnD,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,MAAM,GAAG,GAAG,YAAY,CAAC,qBAAqB,CAAC,CAAC;AAEhD,KAAK,UAAU,IAAI;IACjB,wFAAwF;IACxF,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,KAAK,MAAM,IAAI,GAAG,KAAK,aAAa,CAAC,CAAC;IAC1F,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,GAAG,CAAC,yBAAyB,CAAC,CAAC;QAC/B,OAAO;IACT,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;IAClF,MAAM,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;AACpE,CAAC;AAED,IAAI,CAAC;IACH,MAAM,IAAI,EAAE,CAAC;AACf,CAAC;AAAC,OAAO,KAAK,EAAE,CAAC;IACf,GAAG,CAAC,eAAe,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AACtC,CAAC;AACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { CheckResult } from "../contract.ts";
|
|
2
|
+
/**
|
|
3
|
+
* How long a background re-check may run, in ms: what the binary is given as
|
|
4
|
+
* `CARRICK_RECHECK_BUDGET_MS`. 180 s until the route package's uncapped time
|
|
5
|
+
* is measured (C0), then min(300 s, 2 x its slowest uncapped E2): change this
|
|
6
|
+
* one constant.
|
|
7
|
+
*/
|
|
8
|
+
export declare const BACKGROUND_CAP_MS = 180000;
|
|
9
|
+
/**
|
|
10
|
+
* How long a Stop hook waits for a running re-check, in seconds: one full
|
|
11
|
+
* background call (the cap) plus the runner's own start. The Stop entry
|
|
12
|
+
* carries it as `--wait-s`, and its timeout sits `STOP_TIMEOUT_S` above it,
|
|
13
|
+
* because a command hook that reaches its timeout is cancelled and its output
|
|
14
|
+
* discarded, which would drop the late answer with it.
|
|
15
|
+
*/
|
|
16
|
+
export declare const STOP_WAIT_S: number;
|
|
17
|
+
/** The Stop entry's timeout: the wait plus the hook's own work. */
|
|
18
|
+
export declare const STOP_TIMEOUT_S: number;
|
|
19
|
+
export declare function backgroundCapMs(env?: NodeJS.ProcessEnv): number;
|
|
20
|
+
/** Entries older than this are answered "stopped" without being run. */
|
|
21
|
+
export declare function entryExpiryMs(cap: number): number;
|
|
22
|
+
export type AnswerState = "answered" | "superseded" | "stopped";
|
|
23
|
+
/** One queued edit, as the queue and the session's pending file hold it. */
|
|
24
|
+
export type QueueEntry = {
|
|
25
|
+
seq: string;
|
|
26
|
+
session: string;
|
|
27
|
+
/** Absolute path of the edited file. */
|
|
28
|
+
file: string;
|
|
29
|
+
/** The path `carrick check` is asked about: relative to `root`. */
|
|
30
|
+
relative: string;
|
|
31
|
+
root: string;
|
|
32
|
+
enqueued_at: string;
|
|
33
|
+
/** How many runners died holding this entry. */
|
|
34
|
+
attempts: number;
|
|
35
|
+
};
|
|
36
|
+
export type Answer = {
|
|
37
|
+
seq: string;
|
|
38
|
+
session: string;
|
|
39
|
+
file: string;
|
|
40
|
+
relative: string;
|
|
41
|
+
root: string;
|
|
42
|
+
state: AnswerState;
|
|
43
|
+
enqueued_at: string;
|
|
44
|
+
started_at: string | null;
|
|
45
|
+
finished_at: string;
|
|
46
|
+
result?: CheckResult;
|
|
47
|
+
failure?: string;
|
|
48
|
+
};
|
|
49
|
+
export type Pending = QueueEntry & {
|
|
50
|
+
toldRunning: boolean;
|
|
51
|
+
};
|
|
52
|
+
type Options = {
|
|
53
|
+
home?: string;
|
|
54
|
+
};
|
|
55
|
+
export declare function rechecksDir(root: string): string;
|
|
56
|
+
export declare function queueDir(root: string): string;
|
|
57
|
+
export declare function takenDir(root: string, pid: number): string;
|
|
58
|
+
export declare function lockPath(root: string): string;
|
|
59
|
+
/**
|
|
60
|
+
* Queue one entry per file for a session. The session's pending file is
|
|
61
|
+
* written first so that no entry in the queue is ever unknown to its session.
|
|
62
|
+
* `files` are absolute paths; returns the entries queued, in order.
|
|
63
|
+
*/
|
|
64
|
+
export declare function enqueue(root: string, session: string, files: string[], now: number, options?: Options): QueueEntry[];
|
|
65
|
+
export type EnsureOptions = {
|
|
66
|
+
env?: NodeJS.ProcessEnv;
|
|
67
|
+
now?: number;
|
|
68
|
+
/** Injected in tests; the real one detaches a child and forgets it. */
|
|
69
|
+
start?: (command: string, args: string[], env: NodeJS.ProcessEnv) => void;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Make sure one runner is alive for this workspace. Returns whether one was
|
|
73
|
+
* started: false when the lock is held by a live, recently touched process.
|
|
74
|
+
*
|
|
75
|
+
* The lock names this hook until the runner it spawns takes it over: a second
|
|
76
|
+
* hook in that window sees a live holder and starts nothing.
|
|
77
|
+
*/
|
|
78
|
+
export declare function ensureRunner(root: string, options?: EnsureOptions): boolean;
|
|
79
|
+
export type RunOptions = {
|
|
80
|
+
env?: NodeJS.ProcessEnv;
|
|
81
|
+
home?: string;
|
|
82
|
+
/** The hook that spawned this runner, which holds the lock until it takes over. */
|
|
83
|
+
spawner?: number;
|
|
84
|
+
/** The `carrick` to run; defaults to `CARRICK_BIN` or `carrick`. */
|
|
85
|
+
bin?: string;
|
|
86
|
+
now?: () => number;
|
|
87
|
+
/** Called between the runner removing its lock and its last look at the queue (tests). */
|
|
88
|
+
afterRelease?: () => void | Promise<void>;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* The runner's loop: one `carrick check <file> --recheck --json` at a time, in
|
|
92
|
+
* queue order, each answer written when its call ends. Returns when the queue
|
|
93
|
+
* is empty and the lock is released, or at once when another runner holds it.
|
|
94
|
+
*/
|
|
95
|
+
export declare function runQueue(root: string, options?: RunOptions): Promise<void>;
|
|
96
|
+
/** Entries this session queued that have no answer yet, oldest first. */
|
|
97
|
+
export declare function pendingFor(session: string, home?: string): Pending[];
|
|
98
|
+
/**
|
|
99
|
+
* Answers this session has not been given, oldest first. A superseded answer
|
|
100
|
+
* is silent; an answer is held back while the session holds a newer entry for
|
|
101
|
+
* the same file (its newer answer, or the wait for it, is what to say).
|
|
102
|
+
*/
|
|
103
|
+
export declare function finishedFor(session: string, home?: string): Answer[];
|
|
104
|
+
/** Claim the right to print one answer: true for exactly one caller. */
|
|
105
|
+
export declare function claimDelivery(session: string, seq: string, home?: string, now?: number): boolean;
|
|
106
|
+
/** Claim the right to say one entry is still running: true for exactly one caller. */
|
|
107
|
+
export declare function markToldRunning(session: string, seq: string, home?: string): boolean;
|
|
108
|
+
export {};
|
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
// The background re-check queue and its runner (carrick#3198, B3).
|
|
2
|
+
//
|
|
3
|
+
// A hook may not wait for a re-check that costs a scan of the edited file's
|
|
4
|
+
// repo. The post-edit hook queues the file instead, makes sure one runner is
|
|
5
|
+
// alive for the workspace, and a later carrier (the next hook, or Stop) picks
|
|
6
|
+
// up the answer. This file is the queue, the runner's loop and the per-session
|
|
7
|
+
// answer files; the hooks that queue and deliver are separate.
|
|
8
|
+
//
|
|
9
|
+
// Two places hold state:
|
|
10
|
+
//
|
|
11
|
+
// - The workspace, `<root>/.carrick/rechecks/`: `queue/<seq>.json` (one file per
|
|
12
|
+
// edit, created exclusively because Claude Code runs tool hooks in parallel),
|
|
13
|
+
// `taken/<pid>/<seq>.json` (what the runner has claimed, by rename) and
|
|
14
|
+
// `runner.lock`. Not `.carrick/working/`: every new index deletes that.
|
|
15
|
+
// - The session, flat files beside its record in `~/.carrick/sessions/`:
|
|
16
|
+
// `<id>.recheck.<seq>.pending.json` (the hook), `.answer.json` (the runner),
|
|
17
|
+
// `.delivered` and `.told-running` (each claimed exclusively by the hook that
|
|
18
|
+
// prints the answer or says it is running). Flat, so `prune` and
|
|
19
|
+
// `removeSessions` need no change.
|
|
20
|
+
//
|
|
21
|
+
// Nothing here throws at its caller: an edit must not fail on a state file.
|
|
22
|
+
// One runner per workspace, one `carrick check` at a time, in queue order.
|
|
23
|
+
import fs from "node:fs";
|
|
24
|
+
import path from "node:path";
|
|
25
|
+
import { fileURLToPath } from "node:url";
|
|
26
|
+
import { binary, check } from "../cli.js";
|
|
27
|
+
import { createLogger } from "../log.js";
|
|
28
|
+
import { checkoutRelative, newFunctions } from "./edited.js";
|
|
29
|
+
import { startDetached } from "./refresh.js";
|
|
30
|
+
import { record, safeId, sessionsDir, writeAtomic } from "./reuse.js";
|
|
31
|
+
const log = createLogger("carrick-recheck");
|
|
32
|
+
/**
|
|
33
|
+
* How long a background re-check may run, in ms: what the binary is given as
|
|
34
|
+
* `CARRICK_RECHECK_BUDGET_MS`. 180 s until the route package's uncapped time
|
|
35
|
+
* is measured (C0), then min(300 s, 2 x its slowest uncapped E2): change this
|
|
36
|
+
* one constant.
|
|
37
|
+
*/
|
|
38
|
+
export const BACKGROUND_CAP_MS = 180_000;
|
|
39
|
+
/**
|
|
40
|
+
* How long a Stop hook waits for a running re-check, in seconds: one full
|
|
41
|
+
* background call (the cap) plus the runner's own start. The Stop entry
|
|
42
|
+
* carries it as `--wait-s`, and its timeout sits `STOP_TIMEOUT_S` above it,
|
|
43
|
+
* because a command hook that reaches its timeout is cancelled and its output
|
|
44
|
+
* discarded, which would drop the late answer with it.
|
|
45
|
+
*/
|
|
46
|
+
export const STOP_WAIT_S = BACKGROUND_CAP_MS / 1000 + 20;
|
|
47
|
+
/** The Stop entry's timeout: the wait plus the hook's own work. */
|
|
48
|
+
export const STOP_TIMEOUT_S = STOP_WAIT_S + 10;
|
|
49
|
+
/** The runner's own deadline for one call: the binary's budget ends it first. */
|
|
50
|
+
const CALL_SLACK_MS = 15_000;
|
|
51
|
+
/** A lock or an entry this far past 2 x cap is not waited for any longer. */
|
|
52
|
+
const STALE_SLACK_MS = 60_000;
|
|
53
|
+
export function backgroundCapMs(env = process.env) {
|
|
54
|
+
const raw = env["CARRICK_RECHECK_BACKGROUND_MS"];
|
|
55
|
+
const parsed = raw ? Number.parseInt(raw, 10) : Number.NaN;
|
|
56
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : BACKGROUND_CAP_MS;
|
|
57
|
+
}
|
|
58
|
+
/** Entries older than this are answered "stopped" without being run. */
|
|
59
|
+
export function entryExpiryMs(cap) {
|
|
60
|
+
return 2 * cap + STALE_SLACK_MS;
|
|
61
|
+
}
|
|
62
|
+
export function rechecksDir(root) {
|
|
63
|
+
return path.join(root, ".carrick", "rechecks");
|
|
64
|
+
}
|
|
65
|
+
export function queueDir(root) {
|
|
66
|
+
return path.join(rechecksDir(root), "queue");
|
|
67
|
+
}
|
|
68
|
+
export function takenDir(root, pid) {
|
|
69
|
+
return path.join(rechecksDir(root), "taken", String(pid));
|
|
70
|
+
}
|
|
71
|
+
export function lockPath(root) {
|
|
72
|
+
return path.join(rechecksDir(root), "runner.lock");
|
|
73
|
+
}
|
|
74
|
+
/** `<id>.recheck.<seq>.<kind>` in the sessions directory, or null for an unusable id. */
|
|
75
|
+
function sessionPart(session, seq, kind, home) {
|
|
76
|
+
if (safeId(session) === null)
|
|
77
|
+
return null;
|
|
78
|
+
return path.join(sessionsDir(home), `${session}.recheck.${seq}.${kind}`);
|
|
79
|
+
}
|
|
80
|
+
let counter = 0;
|
|
81
|
+
/** Zero-padded epoch ms + pid + counter: sorts by time, never repeats. */
|
|
82
|
+
function nextSeq(now) {
|
|
83
|
+
counter += 1;
|
|
84
|
+
return `${String(now).padStart(13, "0")}-${process.pid}-${String(counter).padStart(4, "0")}`;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Create `file` holding `body`, only if it does not exist yet, and complete
|
|
88
|
+
* when it does: written beside it, then hard-linked into place (the one
|
|
89
|
+
* operation two processes cannot both win).
|
|
90
|
+
*/
|
|
91
|
+
function createExclusive(file, body) {
|
|
92
|
+
const temp = `${file}.${process.pid}.${counter}.tmp`;
|
|
93
|
+
try {
|
|
94
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
95
|
+
fs.writeFileSync(temp, body, { flag: "wx", mode: 0o600 });
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
fs.linkSync(temp, file);
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
try {
|
|
109
|
+
fs.unlinkSync(temp);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
// Already gone.
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function readJson(file) {
|
|
117
|
+
try {
|
|
118
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function names(dir) {
|
|
125
|
+
try {
|
|
126
|
+
return fs.readdirSync(dir).sort();
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Queue one entry per file for a session. The session's pending file is
|
|
134
|
+
* written first so that no entry in the queue is ever unknown to its session.
|
|
135
|
+
* `files` are absolute paths; returns the entries queued, in order.
|
|
136
|
+
*/
|
|
137
|
+
export function enqueue(root, session, files, now, options = {}) {
|
|
138
|
+
const queued = [];
|
|
139
|
+
for (const file of files) {
|
|
140
|
+
const seq = nextSeq(now);
|
|
141
|
+
const entry = {
|
|
142
|
+
seq,
|
|
143
|
+
session,
|
|
144
|
+
file,
|
|
145
|
+
relative: path.relative(root, file),
|
|
146
|
+
root,
|
|
147
|
+
enqueued_at: new Date(now).toISOString(),
|
|
148
|
+
attempts: 0,
|
|
149
|
+
};
|
|
150
|
+
const pending = sessionPart(session, seq, "pending.json", options.home);
|
|
151
|
+
if (pending === null)
|
|
152
|
+
continue;
|
|
153
|
+
const body = `${JSON.stringify(entry, null, 2)}\n`;
|
|
154
|
+
if (!writeAtomic(pending, body))
|
|
155
|
+
continue;
|
|
156
|
+
if (!createExclusive(path.join(queueDir(root), `${seq}.json`), body)) {
|
|
157
|
+
try {
|
|
158
|
+
fs.unlinkSync(pending);
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
// Nothing to take back.
|
|
162
|
+
}
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
queued.push(entry);
|
|
166
|
+
}
|
|
167
|
+
return queued;
|
|
168
|
+
}
|
|
169
|
+
function alive(pid) {
|
|
170
|
+
try {
|
|
171
|
+
process.kill(pid, 0);
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
// EPERM: the process exists and is somebody else's.
|
|
176
|
+
return error.code === "EPERM";
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function lockBody(pid, started, now) {
|
|
180
|
+
return `${JSON.stringify({ pid, started, touched: new Date(now).toISOString() })}\n`;
|
|
181
|
+
}
|
|
182
|
+
/** Whether the lock's holder is a live process that has touched it lately. */
|
|
183
|
+
function lockHeld(lock, now, cap) {
|
|
184
|
+
if (lock === null || typeof lock.pid !== "number")
|
|
185
|
+
return false;
|
|
186
|
+
if (!alive(lock.pid))
|
|
187
|
+
return false;
|
|
188
|
+
const touched = Date.parse(lock.touched);
|
|
189
|
+
return Number.isFinite(touched) && now - touched <= cap + STALE_SLACK_MS;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Take the lock for `pid`, replacing one nobody holds. The dead lock is
|
|
193
|
+
* re-read before it is removed, so a lock another process just replaced is
|
|
194
|
+
* never unlinked from under it (the `claimRefresh` step).
|
|
195
|
+
*/
|
|
196
|
+
function takeLock(root, pid, now, cap, takeOver) {
|
|
197
|
+
const file = lockPath(root);
|
|
198
|
+
const body = lockBody(pid, new Date(now).toISOString(), now);
|
|
199
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
200
|
+
if (createExclusive(file, body))
|
|
201
|
+
return true;
|
|
202
|
+
const seen = readJson(file);
|
|
203
|
+
if (seen !== null && seen.pid === pid) {
|
|
204
|
+
writeAtomic(file, body);
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
// The hook that started this runner holds the lock until it takes over.
|
|
208
|
+
if (seen !== null && takeOver !== undefined && seen.pid === takeOver) {
|
|
209
|
+
writeAtomic(file, body);
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
if (lockHeld(seen, now, cap))
|
|
213
|
+
return false;
|
|
214
|
+
const again = readJson(file);
|
|
215
|
+
if ((again?.pid ?? null) !== (seen?.pid ?? null))
|
|
216
|
+
return false;
|
|
217
|
+
try {
|
|
218
|
+
fs.unlinkSync(file);
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
// Another process removed it first; the retry decides between us.
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
function releaseLock(root, pid) {
|
|
227
|
+
const file = lockPath(root);
|
|
228
|
+
if (readJson(file)?.pid !== pid)
|
|
229
|
+
return;
|
|
230
|
+
try {
|
|
231
|
+
fs.unlinkSync(file);
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
// Already gone.
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function touchLock(root, pid, now) {
|
|
238
|
+
const file = lockPath(root);
|
|
239
|
+
const held = readJson(file);
|
|
240
|
+
if (held?.pid !== pid)
|
|
241
|
+
return;
|
|
242
|
+
writeAtomic(file, lockBody(pid, held.started, now));
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* The command that runs `runQueue` for a workspace: the runner script beside
|
|
246
|
+
* this module (`.js` in the package, `.ts` when a hook is run from source), so
|
|
247
|
+
* it does not depend on which script started this process.
|
|
248
|
+
*/
|
|
249
|
+
function runnerCommand(root) {
|
|
250
|
+
const here = fileURLToPath(import.meta.url);
|
|
251
|
+
const script = path.join(path.dirname(here), `recheck-run${path.extname(here)}`);
|
|
252
|
+
return { command: process.execPath, args: [script, root] };
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Make sure one runner is alive for this workspace. Returns whether one was
|
|
256
|
+
* started: false when the lock is held by a live, recently touched process.
|
|
257
|
+
*
|
|
258
|
+
* The lock names this hook until the runner it spawns takes it over: a second
|
|
259
|
+
* hook in that window sees a live holder and starts nothing.
|
|
260
|
+
*/
|
|
261
|
+
export function ensureRunner(root, options = {}) {
|
|
262
|
+
const env = options.env ?? process.env;
|
|
263
|
+
const now = options.now ?? Date.now();
|
|
264
|
+
const cap = backgroundCapMs(env);
|
|
265
|
+
// A live, recently touched holder (a runner, or a hook that is starting
|
|
266
|
+
// one) is the runner: this process's own earlier claim counts too.
|
|
267
|
+
if (lockHeld(readJson(lockPath(root)), now, cap))
|
|
268
|
+
return false;
|
|
269
|
+
if (!takeLock(root, process.pid, now, cap))
|
|
270
|
+
return false;
|
|
271
|
+
const { command, args } = runnerCommand(root);
|
|
272
|
+
// The runner is nobody's foreground work: it keeps no one awake (carrick#1889).
|
|
273
|
+
const runnerEnv = {
|
|
274
|
+
...env,
|
|
275
|
+
CARRICK_NO_KEEP_AWAKE: "1",
|
|
276
|
+
CARRICK_RECHECK_SPAWNER: String(process.pid),
|
|
277
|
+
};
|
|
278
|
+
try {
|
|
279
|
+
(options.start ?? startDetached)(command, args, runnerEnv);
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
releaseLock(root, process.pid);
|
|
283
|
+
log("could not start the re-check runner", String(error));
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
function writeAnswer(entry, state, startedAt, finishedAt, body, home) {
|
|
289
|
+
const answer = {
|
|
290
|
+
seq: entry.seq,
|
|
291
|
+
session: entry.session,
|
|
292
|
+
file: entry.file,
|
|
293
|
+
relative: entry.relative,
|
|
294
|
+
root: entry.root,
|
|
295
|
+
state,
|
|
296
|
+
enqueued_at: entry.enqueued_at,
|
|
297
|
+
started_at: startedAt === null ? null : new Date(startedAt).toISOString(),
|
|
298
|
+
finished_at: new Date(finishedAt).toISOString(),
|
|
299
|
+
...body,
|
|
300
|
+
};
|
|
301
|
+
const file = sessionPart(entry.session, entry.seq, "answer.json", home);
|
|
302
|
+
if (file !== null)
|
|
303
|
+
writeAtomic(file, `${JSON.stringify(answer, null, 2)}\n`);
|
|
304
|
+
}
|
|
305
|
+
function remove(file) {
|
|
306
|
+
try {
|
|
307
|
+
fs.unlinkSync(file);
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
// Already gone.
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/** Entries a dead runner held: re-queued once, then answered "stopped". */
|
|
314
|
+
function recoverTaken(root, self, now, home) {
|
|
315
|
+
const base = path.join(rechecksDir(root), "taken");
|
|
316
|
+
for (const dir of names(base)) {
|
|
317
|
+
const pid = Number.parseInt(dir, 10);
|
|
318
|
+
if (!Number.isFinite(pid) || pid === self || alive(pid))
|
|
319
|
+
continue;
|
|
320
|
+
const folder = path.join(base, dir);
|
|
321
|
+
for (const name of names(folder)) {
|
|
322
|
+
const file = path.join(folder, name);
|
|
323
|
+
const entry = readJson(file);
|
|
324
|
+
if (entry === null) {
|
|
325
|
+
remove(file);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if ((entry.attempts ?? 0) >= 1) {
|
|
329
|
+
writeAnswer(entry, "stopped", null, now, { failure: "the background re-check stopped" }, home);
|
|
330
|
+
remove(file);
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
const retried = { ...entry, attempts: (entry.attempts ?? 0) + 1 };
|
|
334
|
+
if (createExclusive(path.join(queueDir(root), name), `${JSON.stringify(retried, null, 2)}\n`)) {
|
|
335
|
+
remove(file);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
try {
|
|
339
|
+
fs.rmdirSync(folder);
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
// Something is still in it; the next runner looks again.
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Settle the queue's bookkeeping, then name the entry to run next: entries
|
|
348
|
+
* past their expiry are answered "stopped" unrun, an older entry of the same
|
|
349
|
+
* session and file is answered "superseded" (silently), and what is left runs
|
|
350
|
+
* in queue order. Null when nothing is left. Entries in `skip` (a claim of
|
|
351
|
+
* theirs failed for good) are left where they are and never chosen.
|
|
352
|
+
*/
|
|
353
|
+
function nextEntry(root, now, cap, skip, home) {
|
|
354
|
+
const live = [];
|
|
355
|
+
for (const name of names(queueDir(root))) {
|
|
356
|
+
if (!name.endsWith(".json"))
|
|
357
|
+
continue;
|
|
358
|
+
const file = path.join(queueDir(root), name);
|
|
359
|
+
const entry = readJson(file);
|
|
360
|
+
if (entry === null)
|
|
361
|
+
continue;
|
|
362
|
+
if (now - Date.parse(entry.enqueued_at) > entryExpiryMs(cap)) {
|
|
363
|
+
writeAnswer(entry, "stopped", null, now, { failure: "the background re-check did not start in time" }, home);
|
|
364
|
+
remove(file);
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
live.push({ name, entry });
|
|
368
|
+
}
|
|
369
|
+
const newest = new Map();
|
|
370
|
+
for (const { entry } of live)
|
|
371
|
+
newest.set(`${entry.session}\0${entry.file}`, entry.seq);
|
|
372
|
+
let chosen = null;
|
|
373
|
+
for (const { name, entry } of live) {
|
|
374
|
+
if (newest.get(`${entry.session}\0${entry.file}`) !== entry.seq) {
|
|
375
|
+
writeAnswer(entry, "superseded", null, now, {}, home);
|
|
376
|
+
remove(path.join(queueDir(root), name));
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (!skip.has(entry.seq))
|
|
380
|
+
chosen ??= entry;
|
|
381
|
+
}
|
|
382
|
+
return chosen;
|
|
383
|
+
}
|
|
384
|
+
/** Claim an entry by renaming it into this runner's own folder. */
|
|
385
|
+
function take(root, entry, pid) {
|
|
386
|
+
const from = path.join(queueDir(root), `${entry.seq}.json`);
|
|
387
|
+
const to = path.join(takenDir(root, pid), `${entry.seq}.json`);
|
|
388
|
+
try {
|
|
389
|
+
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
390
|
+
fs.renameSync(from, to);
|
|
391
|
+
return to;
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* The runner's loop: one `carrick check <file> --recheck --json` at a time, in
|
|
399
|
+
* queue order, each answer written when its call ends. Returns when the queue
|
|
400
|
+
* is empty and the lock is released, or at once when another runner holds it.
|
|
401
|
+
*/
|
|
402
|
+
export async function runQueue(root, options = {}) {
|
|
403
|
+
const env = options.env ?? process.env;
|
|
404
|
+
const clock = options.now ?? Date.now;
|
|
405
|
+
const cap = backgroundCapMs(env);
|
|
406
|
+
const pid = process.pid;
|
|
407
|
+
if (!takeLock(root, pid, clock(), cap, options.spawner))
|
|
408
|
+
return;
|
|
409
|
+
const callEnv = {
|
|
410
|
+
...env,
|
|
411
|
+
CARRICK_RECHECK_BUDGET_MS: String(cap),
|
|
412
|
+
CARRICK_TIMEOUT_MS: String(cap + CALL_SLACK_MS),
|
|
413
|
+
};
|
|
414
|
+
const bin = options.bin ?? binary(env);
|
|
415
|
+
// Entries this runner could not claim although their file stayed (a disk
|
|
416
|
+
// that refuses the rename): answered once, then left alone for its life.
|
|
417
|
+
const unclaimable = new Set();
|
|
418
|
+
const waiting = () => names(queueDir(root)).filter((name) => !unclaimable.has(name.replace(/\.json$/, "")));
|
|
419
|
+
for (;;) {
|
|
420
|
+
recoverTaken(root, pid, clock(), options.home);
|
|
421
|
+
for (;;) {
|
|
422
|
+
const entry = nextEntry(root, clock(), cap, unclaimable, options.home);
|
|
423
|
+
if (entry === null)
|
|
424
|
+
break;
|
|
425
|
+
const taken = take(root, entry, pid);
|
|
426
|
+
if (taken === null) {
|
|
427
|
+
if (fs.existsSync(path.join(queueDir(root), `${entry.seq}.json`))) {
|
|
428
|
+
unclaimable.add(entry.seq);
|
|
429
|
+
const failure = "the background re-check could not claim this edit";
|
|
430
|
+
writeAnswer(entry, "stopped", null, clock(), { failure }, options.home);
|
|
431
|
+
}
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
touchLock(root, pid, clock());
|
|
435
|
+
const startedAt = clock();
|
|
436
|
+
const outcome = await check(entry.relative, {
|
|
437
|
+
cwd: root,
|
|
438
|
+
recheck: true,
|
|
439
|
+
limitMs: cap + CALL_SLACK_MS,
|
|
440
|
+
env: callEnv,
|
|
441
|
+
bin,
|
|
442
|
+
});
|
|
443
|
+
const finishedAt = clock();
|
|
444
|
+
if (outcome.result) {
|
|
445
|
+
writeAnswer(entry, "answered", startedAt, finishedAt, { result: outcome.result }, options.home);
|
|
446
|
+
const found = newFunctions(outcome.result, checkoutRelative(root, entry.file));
|
|
447
|
+
if (found.length > 0)
|
|
448
|
+
record(entry.session, found, options.home);
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
const failure = outcome.failure ?? "the re-check gave no answer";
|
|
452
|
+
writeAnswer(entry, "answered", startedAt, finishedAt, { failure }, options.home);
|
|
453
|
+
}
|
|
454
|
+
remove(taken);
|
|
455
|
+
}
|
|
456
|
+
releaseLock(root, pid);
|
|
457
|
+
await options.afterRelease?.();
|
|
458
|
+
// Anything queued between the last look and the release would otherwise
|
|
459
|
+
// wait for the next hook to notice: look once more, and go on if so.
|
|
460
|
+
if (waiting().length === 0)
|
|
461
|
+
return;
|
|
462
|
+
if (!takeLock(root, pid, clock(), cap))
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
// --- the session's side ----------------------------------------------------
|
|
467
|
+
function sessionNames(session, home) {
|
|
468
|
+
if (safeId(session) === null)
|
|
469
|
+
return [];
|
|
470
|
+
const prefix = `${session}.recheck.`;
|
|
471
|
+
return names(sessionsDir(home)).filter((name) => name.startsWith(prefix));
|
|
472
|
+
}
|
|
473
|
+
function seqOf(session, name) {
|
|
474
|
+
return name.slice(`${session}.recheck.`.length).split(".")[0] ?? "";
|
|
475
|
+
}
|
|
476
|
+
/** Entries this session queued that have no answer yet, oldest first. */
|
|
477
|
+
export function pendingFor(session, home) {
|
|
478
|
+
const all = sessionNames(session, home);
|
|
479
|
+
const answered = new Set(all.filter((n) => n.endsWith(".answer.json")).map((n) => seqOf(session, n)));
|
|
480
|
+
const told = new Set(all.filter((n) => n.endsWith(".told-running")).map((n) => seqOf(session, n)));
|
|
481
|
+
const out = [];
|
|
482
|
+
for (const name of all.filter((n) => n.endsWith(".pending.json"))) {
|
|
483
|
+
const seq = seqOf(session, name);
|
|
484
|
+
if (answered.has(seq))
|
|
485
|
+
continue;
|
|
486
|
+
const entry = readJson(path.join(sessionsDir(home), name));
|
|
487
|
+
if (entry !== null)
|
|
488
|
+
out.push({ ...entry, toldRunning: told.has(seq) });
|
|
489
|
+
}
|
|
490
|
+
return out;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Answers this session has not been given, oldest first. A superseded answer
|
|
494
|
+
* is silent; an answer is held back while the session holds a newer entry for
|
|
495
|
+
* the same file (its newer answer, or the wait for it, is what to say).
|
|
496
|
+
*/
|
|
497
|
+
export function finishedFor(session, home) {
|
|
498
|
+
const all = sessionNames(session, home);
|
|
499
|
+
const delivered = new Set(all.filter((n) => n.endsWith(".delivered")).map((n) => seqOf(session, n)));
|
|
500
|
+
const newest = new Map();
|
|
501
|
+
for (const name of all.filter((n) => n.endsWith(".pending.json"))) {
|
|
502
|
+
const entry = readJson(path.join(sessionsDir(home), name));
|
|
503
|
+
if (entry === null)
|
|
504
|
+
continue;
|
|
505
|
+
const held = newest.get(entry.file);
|
|
506
|
+
if (held === undefined || held < entry.seq)
|
|
507
|
+
newest.set(entry.file, entry.seq);
|
|
508
|
+
}
|
|
509
|
+
const out = [];
|
|
510
|
+
for (const name of all.filter((n) => n.endsWith(".answer.json"))) {
|
|
511
|
+
const seq = seqOf(session, name);
|
|
512
|
+
if (delivered.has(seq))
|
|
513
|
+
continue;
|
|
514
|
+
const answer = readJson(path.join(sessionsDir(home), name));
|
|
515
|
+
if (answer === null || answer.state === "superseded")
|
|
516
|
+
continue;
|
|
517
|
+
if ((newest.get(answer.file) ?? seq) > seq)
|
|
518
|
+
continue;
|
|
519
|
+
out.push(answer);
|
|
520
|
+
}
|
|
521
|
+
return out;
|
|
522
|
+
}
|
|
523
|
+
/** Claim the right to print one answer: true for exactly one caller. */
|
|
524
|
+
export function claimDelivery(session, seq, home, now = Date.now()) {
|
|
525
|
+
const file = sessionPart(session, seq, "delivered", home);
|
|
526
|
+
if (file === null)
|
|
527
|
+
return false;
|
|
528
|
+
return createExclusive(file, `${JSON.stringify({ delivered_at: new Date(now).toISOString() })}\n`);
|
|
529
|
+
}
|
|
530
|
+
/** Claim the right to say one entry is still running: true for exactly one caller. */
|
|
531
|
+
export function markToldRunning(session, seq, home) {
|
|
532
|
+
const file = sessionPart(session, seq, "told-running", home);
|
|
533
|
+
if (file === null)
|
|
534
|
+
return false;
|
|
535
|
+
return createExclusive(file, `${JSON.stringify({ told_at: new Date().toISOString() })}\n`);
|
|
536
|
+
}
|
|
537
|
+
//# sourceMappingURL=rechecks.js.map
|