mtech-ai-local-runner 0.1.54 → 0.1.56
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 +45 -0
- package/dist/src/asks/commands.js +111 -18
- package/dist/src/asks/hook-client.js +31 -4
- package/dist/src/asks/hub-protocol.js +8 -1
- package/dist/src/asks/inbox.js +1 -1
- package/dist/src/asks/messenger.js +9 -3
- package/dist/src/asks/routing.js +31 -5
- package/dist/src/asks/shim.js +18 -11
- package/dist/src/asks/store.js +4 -0
- package/dist/src/cli.js +15 -6
- package/dist/src/daemon/runtime.js +10 -2
- package/dist/src/gateway-client.js +14 -0
- package/dist/src/guide/aggregate.js +58 -0
- package/dist/src/guide/company.js +102 -0
- package/dist/src/guide/doctor.js +47 -0
- package/dist/src/guide/files.js +237 -0
- package/dist/src/guide/hook-client.js +289 -0
- package/dist/src/guide/integrations.js +335 -0
- package/dist/src/guide/matching.js +314 -0
- package/dist/src/guide/runtime.js +173 -0
- package/dist/src/guide/templates.js +22 -0
- package/dist/src/guide/types.js +95 -0
- package/dist/src/transport/sync-dispatch.js +12 -0
- package/package.json +1 -1
|
@@ -31,6 +31,7 @@ import { buildTerminalEvidenceCollector } from "../safety/terminal-evidence.js";
|
|
|
31
31
|
import { packagedHookCommandParts } from "../safety/hook-command.js";
|
|
32
32
|
import { composeReviewedP1Host } from '../verification/p1-host-composition.js';
|
|
33
33
|
import { AskRuntime, withAsks } from '../asks/runtime.js';
|
|
34
|
+
import { GuideRuntime, withGuide } from '../guide/runtime.js';
|
|
34
35
|
export async function buildDaemon(input) {
|
|
35
36
|
const claudeParts = [input.claudeSecurityHost, input.claudeSupportedProbe, input.claudeBinaryIdentity].filter(Boolean).length;
|
|
36
37
|
if (claudeParts !== 0 && claudeParts !== 3)
|
|
@@ -54,6 +55,10 @@ export async function buildDaemon(input) {
|
|
|
54
55
|
const asks = new AskRuntime({ stateRoot: input.stateRoot, env: input.env,
|
|
55
56
|
report: (deliveryId, report, signal) => gateway.reportAskDelivery({ deliveryId, ...report }, signal),
|
|
56
57
|
log: event => { process.stderr.write(`${JSON.stringify(event)}\n`); } });
|
|
58
|
+
// Guide hooks (Work Item d38d60f9) ride on the same sync and heartbeat; they start no executor.
|
|
59
|
+
const guide = new GuideRuntime({ stateRoot: input.stateRoot, env: input.env,
|
|
60
|
+
report: (day, signal) => gateway.reportGuidance(day, signal),
|
|
61
|
+
log: event => { process.stderr.write(`${JSON.stringify(event)}\n`); } });
|
|
57
62
|
const localFixHost = input.localFixHost ?? await loadInstalledLocalFixHost({ stateRoot: input.stateRoot, runnerId: input.runnerId, gateway });
|
|
58
63
|
// A test-injected host wins; otherwise only the bundled reviewed registration can enable P1.
|
|
59
64
|
let p1Host = input.p1Host;
|
|
@@ -201,12 +206,14 @@ export async function buildDaemon(input) {
|
|
|
201
206
|
sessionOpen = undefined;
|
|
202
207
|
}
|
|
203
208
|
const askInbox = await asks.advertisement();
|
|
209
|
+
const guidance = await guide.advertisement();
|
|
204
210
|
return {
|
|
205
211
|
deviceFingerprint: createHash("sha256").update(`${process.getuid?.() ?? 0}|${runnerDisplayName()}|${input.gatewayMcpUrl}`).digest("hex"),
|
|
206
212
|
displayName: runnerDisplayName(), packageVersion: packageVersion(),
|
|
207
213
|
capabilities: acceptingJobs(localRunnerCapabilities(probes, !!localFixHost, localFixHost?.verificationMode), poller.accepting),
|
|
208
214
|
mode: "full",
|
|
209
215
|
...(sessionOpen ? { sessionOpen: { ...sessionOpen, ...(askInbox ? { ask_inbox: askInbox } : {}) } } : {}),
|
|
216
|
+
...(guidance ? { guidance } : {}),
|
|
210
217
|
};
|
|
211
218
|
},
|
|
212
219
|
send: async (payload) => { await gateway.heartbeatLocalRunner(payload); },
|
|
@@ -221,6 +228,7 @@ export async function buildDaemon(input) {
|
|
|
221
228
|
await gateway.reportSessionOpen({ openId, status: result.status, surface: result.surface,
|
|
222
229
|
failureCode: result.status === "failed" ? result.failureCode : undefined }, signal);
|
|
223
230
|
},
|
|
231
|
+
guide,
|
|
224
232
|
asks,
|
|
225
233
|
closureAcks,
|
|
226
234
|
jobs: poller,
|
|
@@ -233,7 +241,7 @@ export async function buildDaemon(input) {
|
|
|
233
241
|
const acknowledged = closureAcks.take();
|
|
234
242
|
// A runner that takes no jobs right now (reconciling, or suspended for recovery) names no adapter.
|
|
235
243
|
try {
|
|
236
|
-
return await gateway.syncRunner({ adapterVersions: poller.accepting ? versions : {}, reason, acknowledgedClosureIds: acknowledged }, signal);
|
|
244
|
+
return await gateway.syncRunner({ adapterVersions: poller.accepting ? versions : {}, reason, acknowledgedClosureIds: acknowledged, guidancePolicyVersion: await guide.policyVersion() }, signal);
|
|
237
245
|
}
|
|
238
246
|
catch (error) {
|
|
239
247
|
closureAcks.restore(acknowledged);
|
|
@@ -259,7 +267,7 @@ export async function buildDaemon(input) {
|
|
|
259
267
|
return;
|
|
260
268
|
throw new Error('Runner state requires authoritative reconciliation');
|
|
261
269
|
},
|
|
262
|
-
poller, sessionOpens: withAsks(asks, sessionOpens),
|
|
270
|
+
poller, sessionOpens: withGuide(guide, withAsks(asks, sessionOpens)),
|
|
263
271
|
reportRecoveryRequired: reason => { process.stderr.write(`${JSON.stringify({ event: 'runner_recovery_required', reason })}\n`); },
|
|
264
272
|
});
|
|
265
273
|
// Model tools have no Gateway relay. Each adapter owns and closes its job endpoint.
|
|
@@ -29,6 +29,8 @@ export function parseRunnerSync(result, runnerId) {
|
|
|
29
29
|
opens: body.opens,
|
|
30
30
|
...(Array.isArray(body.ask_deliveries) ? { askDeliveries: body.ask_deliveries } : {}),
|
|
31
31
|
...(Array.isArray(body.closed_delivery_ids) ? { closedDeliveryIds: body.closed_delivery_ids } : {}),
|
|
32
|
+
...("guidance_policy" in body ? { guidancePolicy: body.guidance_policy } : {}),
|
|
33
|
+
...(typeof body.guidance_policy_version === "string" ? { guidancePolicyVersion: body.guidance_policy_version } : {}),
|
|
32
34
|
jobAvailable: body.job_available === true,
|
|
33
35
|
push: parsePush(body.push, runnerId),
|
|
34
36
|
syncSeconds: clampSeconds(body.sync_seconds, 15, 120, 30),
|
|
@@ -489,6 +491,7 @@ export class McpGatewayClient {
|
|
|
489
491
|
capabilities: input.capabilities,
|
|
490
492
|
...(input.mode ? { mode: input.mode } : {}),
|
|
491
493
|
...(input.sessionOpen ? { session_open: input.sessionOpen } : {}),
|
|
494
|
+
...(input.guidance ? { guidance: input.guidance } : {}),
|
|
492
495
|
},
|
|
493
496
|
}, signal);
|
|
494
497
|
}
|
|
@@ -524,6 +527,16 @@ export class McpGatewayClient {
|
|
|
524
527
|
...(input.failureCode ? { failure_code: input.failureCode } : {}),
|
|
525
528
|
}, signal);
|
|
526
529
|
}
|
|
530
|
+
/** One UTC day of guide counts for one app and project (Work Item d38d60f9). Fixed keys and whole numbers only. */
|
|
531
|
+
async reportGuidance(day, signal) {
|
|
532
|
+
return this.callTool("local_agent_guidance_report", {
|
|
533
|
+
runner_id: this.runnerId,
|
|
534
|
+
date: day.date,
|
|
535
|
+
app: day.app,
|
|
536
|
+
project_key: day.projectKey,
|
|
537
|
+
counts: day.counts,
|
|
538
|
+
}, signal);
|
|
539
|
+
}
|
|
527
540
|
/** acknowledgedClosureIds: closures handled since the last sync; the Gateway lists each again until acknowledged. */
|
|
528
541
|
async syncRunner(input, signal) {
|
|
529
542
|
const acknowledged = (input.acknowledgedClosureIds ?? []).slice(0, 50);
|
|
@@ -532,6 +545,7 @@ export class McpGatewayClient {
|
|
|
532
545
|
adapter_versions: input.adapterVersions,
|
|
533
546
|
reason: input.reason,
|
|
534
547
|
...(acknowledged.length > 0 ? { acknowledged_closure_ids: acknowledged } : {}),
|
|
548
|
+
...(input.guidancePolicyVersion ? { guidance_policy_version: input.guidancePolicyVersion } : {}),
|
|
535
549
|
}, signal);
|
|
536
550
|
return parseRunnerSync(result, this.runnerId);
|
|
537
551
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { COUNT_KEYS, PROJECT_KEY, RULE_IDS, emptyDayCounts, isGuideApp, isOutcome, isRuleId } from "./types.js";
|
|
2
|
+
/*
|
|
3
|
+
* One UTC day of count lines turned into the counts of local_agent_guidance_report (spec section 7), per app and project.
|
|
4
|
+
* A session counts once, through its "session" line, which the hook writes at its first event; it counts as reading,
|
|
5
|
+
* searching or both from its lines of the same day.
|
|
6
|
+
*/
|
|
7
|
+
const SESSION_KEYS = new Set(["sessions", "sessions_with_kb_read", "sessions_with_search", "sessions_with_both"]);
|
|
8
|
+
export function aggregateDay(lines) {
|
|
9
|
+
const groups = new Map();
|
|
10
|
+
for (const line of lines) {
|
|
11
|
+
if (!isGuideApp(line.a) || !PROJECT_KEY.test(line.p))
|
|
12
|
+
continue;
|
|
13
|
+
const key = `${line.a}|${line.p}`;
|
|
14
|
+
let group = groups.get(key);
|
|
15
|
+
if (group === undefined) {
|
|
16
|
+
group = { counts: emptyDayCounts(), sessions: new Set(), read: new Set(), searched: new Set() };
|
|
17
|
+
groups.set(key, group);
|
|
18
|
+
}
|
|
19
|
+
const n = Number.isSafeInteger(line.n) && line.n >= 0 ? line.n : 0;
|
|
20
|
+
if (line.m === "session") {
|
|
21
|
+
group.sessions.add(line.s);
|
|
22
|
+
}
|
|
23
|
+
else if (line.m === "rule") {
|
|
24
|
+
if (isRuleId(line.r) && isOutcome(line.o))
|
|
25
|
+
group.counts.rules[line.r][line.o] += n;
|
|
26
|
+
}
|
|
27
|
+
else if (COUNT_KEYS.includes(line.m) && !SESSION_KEYS.has(line.m)) {
|
|
28
|
+
group.counts[line.m] += n;
|
|
29
|
+
if (line.m.startsWith("kb_reads_"))
|
|
30
|
+
group.read.add(line.s);
|
|
31
|
+
if (line.m === "knowledge_search_calls")
|
|
32
|
+
group.searched.add(line.s);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const result = new Map();
|
|
36
|
+
for (const [key, group] of groups) {
|
|
37
|
+
const sessions = [...group.sessions];
|
|
38
|
+
group.counts.sessions = sessions.length;
|
|
39
|
+
group.counts.sessions_with_kb_read = sessions.filter(session => group.read.has(session)).length;
|
|
40
|
+
group.counts.sessions_with_search = sessions.filter(session => group.searched.has(session)).length;
|
|
41
|
+
group.counts.sessions_with_both = sessions.filter(session => group.read.has(session) && group.searched.has(session)).length;
|
|
42
|
+
result.set(key, group.counts);
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
/** The sum of several days or groups, for doctor's today line. */
|
|
47
|
+
export function sumDays(days) {
|
|
48
|
+
const total = emptyDayCounts();
|
|
49
|
+
for (const day of days) {
|
|
50
|
+
for (const key of COUNT_KEYS)
|
|
51
|
+
total[key] += day[key];
|
|
52
|
+
for (const rule of RULE_IDS) {
|
|
53
|
+
total.rules[rule].observed += day.rules[rule].observed;
|
|
54
|
+
total.rules[rule].warned += day.rules[rule].warned;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return total;
|
|
58
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
2
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { MULTI_REPOSITORY, UNREGISTERED } from "./types.js";
|
|
4
|
+
const NOT_COMPANY = Object.freeze({ company: false, projectKey: null });
|
|
5
|
+
const SCAN_LIMIT = 200;
|
|
6
|
+
const SCAN_MS = 50;
|
|
7
|
+
const MAX_DEPTH = 64;
|
|
8
|
+
const URL_REMOTE = /^(?:https?|ssh|git):\/\/(?:[^@/]*@)?([^/:]+)(?::\d+)?\/([^/]+)\/([^/]+?)(?:\.git)?\/?$/i;
|
|
9
|
+
const SCP_REMOTE = /^[^@\s/]+@([^:\s/]+):([^/\s]+)\/([^/\s]+?)(?:\.git)?$/i;
|
|
10
|
+
/** The host, workspace and repository of a remote URL. The user part, and any token in it, is dropped. */
|
|
11
|
+
export function parseRemote(url) {
|
|
12
|
+
const trimmed = url.trim();
|
|
13
|
+
const match = URL_REMOTE.exec(trimmed) ?? SCP_REMOTE.exec(trimmed);
|
|
14
|
+
if (!match)
|
|
15
|
+
return null;
|
|
16
|
+
return { host: match[1].toLowerCase(), workspace: match[2].toLowerCase(), repository: match[3].toLowerCase() };
|
|
17
|
+
}
|
|
18
|
+
/** The url of [remote "origin"] in a git config text, or null. */
|
|
19
|
+
export function originUrl(config) {
|
|
20
|
+
let inOrigin = false;
|
|
21
|
+
for (const raw of config.split(/\r?\n/)) {
|
|
22
|
+
const line = raw.trim();
|
|
23
|
+
if (line.startsWith("[")) {
|
|
24
|
+
inOrigin = /^\[remote\s+"origin"\]$/.test(line);
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const match = inOrigin ? /^url\s*=\s*(.+)$/.exec(line) : null;
|
|
28
|
+
if (match)
|
|
29
|
+
return match[1].trim();
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const isBitbucket = (host) => host === "bitbucket.org" || host.endsWith(".bitbucket.org");
|
|
34
|
+
/** The repository name when the config's origin is a company remote, else null. */
|
|
35
|
+
async function companyRepository(configPath, policy) {
|
|
36
|
+
let text;
|
|
37
|
+
try {
|
|
38
|
+
text = await readFile(configPath, "utf8");
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const url = originUrl(text);
|
|
44
|
+
const remote = url === null ? null : parseRemote(url);
|
|
45
|
+
return remote !== null && isBitbucket(remote.host) && policy.companyWorkspaces.includes(remote.workspace) ? remote.repository : null;
|
|
46
|
+
}
|
|
47
|
+
/** The config file of the clone that holds dir, following a worktree's .git file to the main clone; null outside one. */
|
|
48
|
+
async function gitConfigFor(start) {
|
|
49
|
+
let dir = resolve(start);
|
|
50
|
+
for (let depth = 0; depth < MAX_DEPTH; depth += 1) {
|
|
51
|
+
const dotGit = join(dir, ".git");
|
|
52
|
+
const info = await stat(dotGit).catch(() => null);
|
|
53
|
+
if (info?.isDirectory())
|
|
54
|
+
return join(dotGit, "config");
|
|
55
|
+
if (info?.isFile()) {
|
|
56
|
+
const pointer = /^gitdir:\s*(.+)$/m.exec(await readFile(dotGit, "utf8").catch(() => ""))?.[1]?.trim();
|
|
57
|
+
if (!pointer)
|
|
58
|
+
return null;
|
|
59
|
+
const gitDir = isAbsolute(pointer) ? pointer : resolve(dir, pointer);
|
|
60
|
+
const common = (await readFile(join(gitDir, "commondir"), "utf8").catch(() => "")).trim();
|
|
61
|
+
return join(common === "" ? gitDir : isAbsolute(common) ? common : resolve(gitDir, common), "config");
|
|
62
|
+
}
|
|
63
|
+
const parent = dirname(dir);
|
|
64
|
+
if (parent === dir)
|
|
65
|
+
return null;
|
|
66
|
+
dir = parent;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
export async function classifyWorkspace(cwd, policy, clock = Date.now) {
|
|
71
|
+
if (typeof cwd !== "string" || !isAbsolute(cwd))
|
|
72
|
+
return NOT_COMPANY;
|
|
73
|
+
const config = await gitConfigFor(cwd);
|
|
74
|
+
if (config !== null) {
|
|
75
|
+
const repository = await companyRepository(config, policy);
|
|
76
|
+
if (repository === null)
|
|
77
|
+
return NOT_COMPANY;
|
|
78
|
+
return { company: true, projectKey: Object.hasOwn(policy.repositories, repository) ? policy.repositories[repository] : UNREGISTERED };
|
|
79
|
+
}
|
|
80
|
+
// Not inside a clone: a folder that holds company clones, such as ~/Projects, counts as company work.
|
|
81
|
+
const started = clock();
|
|
82
|
+
let names;
|
|
83
|
+
try {
|
|
84
|
+
names = (await readdir(cwd)).sort().slice(0, SCAN_LIMIT);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return NOT_COMPANY;
|
|
88
|
+
}
|
|
89
|
+
for (const name of names) {
|
|
90
|
+
if (clock() - started > SCAN_MS)
|
|
91
|
+
break;
|
|
92
|
+
if (name.startsWith("."))
|
|
93
|
+
continue;
|
|
94
|
+
const dotGit = join(cwd, name, ".git");
|
|
95
|
+
const info = await stat(dotGit).catch(() => null);
|
|
96
|
+
if (!info?.isDirectory())
|
|
97
|
+
continue;
|
|
98
|
+
if (await companyRepository(join(dotGit, "config"), policy) !== null)
|
|
99
|
+
return { company: true, projectKey: MULTI_REPOSITORY };
|
|
100
|
+
}
|
|
101
|
+
return NOT_COMPANY;
|
|
102
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { guideStatusOf } from "../asks/commands.js";
|
|
2
|
+
import { aggregateDay, sumDays } from "./aggregate.js";
|
|
3
|
+
import { eventDays, lastEventAt, readDetails, readEventLines, readPolicyCache, utcDate } from "./files.js";
|
|
4
|
+
import { codexGuideTrust } from "./integrations.js";
|
|
5
|
+
import { GUIDE_APPS } from "./types.js";
|
|
6
|
+
const RECENT_LINES = 20;
|
|
7
|
+
const CODEX_GUIDE_TRUST = "Codex runs the two guide hooks only after you trust them once. In the Codex app, trust them under Settings → Hooks "
|
|
8
|
+
+ "or with the Review hooks button. In the Codex CLI in a terminal, trust them with /hooks.";
|
|
9
|
+
export async function guideDoctorReport(input) {
|
|
10
|
+
const now = input.now ?? Date.now();
|
|
11
|
+
try {
|
|
12
|
+
const cached = await readPolicyCache(input.stateRoot, now);
|
|
13
|
+
const integrations = [];
|
|
14
|
+
for (const app of GUIDE_APPS) {
|
|
15
|
+
const status = await guideStatusOf(app, input.env, input.stateRoot);
|
|
16
|
+
if (status.state === "not_installed") {
|
|
17
|
+
integrations.push(status);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const last = await lastEventAt(input.stateRoot, app);
|
|
21
|
+
if (app === "codex") {
|
|
22
|
+
const trusted = await codexGuideTrust(input.env);
|
|
23
|
+
integrations.push({ ...status, trusted, last_event_at: last, ...(trusted === "yes" ? {} : { action: CODEX_GUIDE_TRUST }) });
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
integrations.push({ ...status, last_event_at: last });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const today = (await eventDays(input.stateRoot)).get(utcDate(now));
|
|
30
|
+
const lines = today === undefined ? [] : await readEventLines([today.reported, today.fresh].filter((path) => path !== undefined));
|
|
31
|
+
const groups = [...aggregateDay(lines).values()];
|
|
32
|
+
return {
|
|
33
|
+
policy: cached === null ? null : { version: cached.policy.version, confirmed_seconds_ago: Math.max(0, Math.floor((now - cached.confirmedAt) / 1000)),
|
|
34
|
+
stale: cached.stale, rules: cached.policy.rules },
|
|
35
|
+
integrations,
|
|
36
|
+
today: groups.length === 0 ? null : sumDays(groups),
|
|
37
|
+
recent: (await readDetails(input.stateRoot, [utcDate(now - 86_400_000), utcDate(now)])).slice(-RECENT_LINES),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return { policy: null, integrations: [], today: null, recent: [] };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Doctor prints its guide section only once guide has a policy or a hook on this Mac. */
|
|
45
|
+
export function guideReportShown(report) {
|
|
46
|
+
return report.policy !== null || report.integrations.some(item => item.state !== "not_installed");
|
|
47
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { appendFile, mkdir, readFile, readdir, rename, rm, stat, utimes, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { GUIDE_DIR, STALE_POLICY_MS, isGuideApp, isOutcome, isRuleId, parseGuidancePolicy } from "./types.js";
|
|
5
|
+
const DIR_MODE = 0o700;
|
|
6
|
+
const FILE_MODE = 0o600;
|
|
7
|
+
const MAX_OWN_WRITES = 200;
|
|
8
|
+
export const SESSION_TTL_MS = 48 * 3_600_000;
|
|
9
|
+
export const LOCAL_RETENTION_DAYS = 14;
|
|
10
|
+
const DAY_FILE = /^(\d{4}-\d{2}-\d{2})(\.reported)?\.ndjson$/;
|
|
11
|
+
export const guidePath = (stateRoot, ...parts) => join(stateRoot, GUIDE_DIR, ...parts);
|
|
12
|
+
export const utcDate = (ms) => new Date(ms).toISOString().slice(0, 10);
|
|
13
|
+
/** sha256 of the app and the session id. It names the session's file; its first 16 characters tag the session's count lines. */
|
|
14
|
+
export function sessionHash(app, sessionId) {
|
|
15
|
+
return createHash("sha256").update(`${app}\0${sessionId}`).digest("hex");
|
|
16
|
+
}
|
|
17
|
+
async function writeAtomic(path, content) {
|
|
18
|
+
await mkdir(dirname(path), { recursive: true, mode: DIR_MODE });
|
|
19
|
+
const temporary = `${path}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`;
|
|
20
|
+
try {
|
|
21
|
+
await writeFile(temporary, content, { mode: FILE_MODE, flag: "wx" });
|
|
22
|
+
await rename(temporary, path);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
await rm(temporary, { force: true });
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async function readJson(path) {
|
|
30
|
+
try {
|
|
31
|
+
return JSON.parse(await readFile(path, "utf8"));
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export async function readPolicyCache(stateRoot, now) {
|
|
38
|
+
const raw = await readJson(guidePath(stateRoot, "policy.json"));
|
|
39
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw))
|
|
40
|
+
return null;
|
|
41
|
+
const { policy, confirmed_at: confirmedAt } = raw;
|
|
42
|
+
const parsed = parseGuidancePolicy(policy);
|
|
43
|
+
if (parsed === null || typeof confirmedAt !== "number" || !Number.isSafeInteger(confirmedAt))
|
|
44
|
+
return null;
|
|
45
|
+
return { policy: parsed, confirmedAt, stale: now - confirmedAt > STALE_POLICY_MS };
|
|
46
|
+
}
|
|
47
|
+
/** Caches the policy exactly as the Gateway sent it, confirmed at confirmedAt. */
|
|
48
|
+
export async function writePolicyCache(stateRoot, wirePolicy, confirmedAt) {
|
|
49
|
+
await writeAtomic(guidePath(stateRoot, "policy.json"), `${JSON.stringify({ policy: wirePolicy, confirmed_at: confirmedAt })}\n`);
|
|
50
|
+
}
|
|
51
|
+
/** Moves the cached policy's confirmation time. Resolves false when there is no valid cache. */
|
|
52
|
+
export async function confirmPolicyCache(stateRoot, confirmedAt) {
|
|
53
|
+
const raw = await readJson(guidePath(stateRoot, "policy.json"));
|
|
54
|
+
const policy = raw !== null && typeof raw === "object" ? raw.policy : undefined;
|
|
55
|
+
if (parseGuidancePolicy(policy) === null)
|
|
56
|
+
return false;
|
|
57
|
+
await writePolicyCache(stateRoot, policy, confirmedAt);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
/** Removes everything guide keeps. Used when the Gateway says guide is off for this person, so nothing waits to be sent. */
|
|
61
|
+
export async function deleteGuideState(stateRoot) {
|
|
62
|
+
await rm(join(stateRoot, GUIDE_DIR), { recursive: true, force: true });
|
|
63
|
+
}
|
|
64
|
+
function isSession(value) {
|
|
65
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
66
|
+
return false;
|
|
67
|
+
const state = value;
|
|
68
|
+
return typeof state.company === "boolean" && (state.projectKey === null || typeof state.projectKey === "string")
|
|
69
|
+
&& typeof state.counted === "boolean" && typeof state.searched === "boolean"
|
|
70
|
+
&& Array.isArray(state.warned) && state.warned.every(isRuleId)
|
|
71
|
+
&& Array.isArray(state.ownWrites) && state.ownWrites.every(path => typeof path === "string")
|
|
72
|
+
&& state.details !== null && typeof state.details === "object" && !Array.isArray(state.details);
|
|
73
|
+
}
|
|
74
|
+
export async function readSession(stateRoot, hash) {
|
|
75
|
+
const value = await readJson(guidePath(stateRoot, "sessions", `${hash}.json`));
|
|
76
|
+
return isSession(value) ? value : null;
|
|
77
|
+
}
|
|
78
|
+
export async function writeSession(stateRoot, hash, state) {
|
|
79
|
+
await writeAtomic(guidePath(stateRoot, "sessions", `${hash}.json`), `${JSON.stringify({ ...state, ownWrites: state.ownWrites.slice(-MAX_OWN_WRITES) })}\n`);
|
|
80
|
+
}
|
|
81
|
+
async function appendLines(stateRoot, folder, lines) {
|
|
82
|
+
if (lines.length === 0)
|
|
83
|
+
return;
|
|
84
|
+
const byDate = new Map();
|
|
85
|
+
for (const line of lines) {
|
|
86
|
+
const date = utcDate(line.t * 1000);
|
|
87
|
+
byDate.set(date, `${byDate.get(date) ?? ""}${JSON.stringify(line)}\n`);
|
|
88
|
+
}
|
|
89
|
+
await mkdir(guidePath(stateRoot, folder), { recursive: true, mode: DIR_MODE });
|
|
90
|
+
for (const [date, text] of byDate)
|
|
91
|
+
await appendFile(guidePath(stateRoot, folder, `${date}.ndjson`), text, { mode: FILE_MODE });
|
|
92
|
+
}
|
|
93
|
+
export async function appendEvents(stateRoot, lines) { await appendLines(stateRoot, "events", lines); }
|
|
94
|
+
export async function appendDetails(stateRoot, lines) { await appendLines(stateRoot, "detail", lines); }
|
|
95
|
+
export async function stampLastEvent(stateRoot, app, now) {
|
|
96
|
+
const path = guidePath(stateRoot, "last", app);
|
|
97
|
+
const time = new Date(now);
|
|
98
|
+
try {
|
|
99
|
+
await utimes(path, time, time);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
catch { /* Not there yet. */ }
|
|
103
|
+
await mkdir(dirname(path), { recursive: true, mode: DIR_MODE });
|
|
104
|
+
await writeFile(path, "", { mode: FILE_MODE });
|
|
105
|
+
await utimes(path, time, time);
|
|
106
|
+
}
|
|
107
|
+
/** The last hook run of an app, rounded down to the hour, so the heartbeat changes at most hourly; null before any. */
|
|
108
|
+
export async function lastEventAt(stateRoot, app) {
|
|
109
|
+
try {
|
|
110
|
+
return Math.floor((await stat(guidePath(stateRoot, "last", app))).mtimeMs / 3_600_000) * 3600;
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export async function eventDays(stateRoot) {
|
|
117
|
+
const days = new Map();
|
|
118
|
+
let names = [];
|
|
119
|
+
try {
|
|
120
|
+
names = await readdir(guidePath(stateRoot, "events"));
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return days;
|
|
124
|
+
}
|
|
125
|
+
for (const name of names) {
|
|
126
|
+
const match = DAY_FILE.exec(name);
|
|
127
|
+
if (!match)
|
|
128
|
+
continue;
|
|
129
|
+
const day = days.get(match[1]) ?? {};
|
|
130
|
+
if (match[2])
|
|
131
|
+
day.reported = guidePath(stateRoot, "events", name);
|
|
132
|
+
else
|
|
133
|
+
day.fresh = guidePath(stateRoot, "events", name);
|
|
134
|
+
days.set(match[1], day);
|
|
135
|
+
}
|
|
136
|
+
return days;
|
|
137
|
+
}
|
|
138
|
+
export async function readEventLines(paths) {
|
|
139
|
+
const lines = [];
|
|
140
|
+
for (const path of paths) {
|
|
141
|
+
let text = "";
|
|
142
|
+
try {
|
|
143
|
+
text = await readFile(path, "utf8");
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
for (const raw of text.split("\n")) {
|
|
149
|
+
if (raw === "")
|
|
150
|
+
continue;
|
|
151
|
+
try {
|
|
152
|
+
const line = JSON.parse(raw);
|
|
153
|
+
if (typeof line.t === "number" && isGuideApp(line.a) && typeof line.p === "string" && typeof line.s === "string"
|
|
154
|
+
&& typeof line.m === "string" && typeof line.n === "number")
|
|
155
|
+
lines.push(line);
|
|
156
|
+
}
|
|
157
|
+
catch { /* A line a crash cut short is skipped. */ }
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return lines;
|
|
161
|
+
}
|
|
162
|
+
export async function readDetails(stateRoot, dates) {
|
|
163
|
+
const lines = [];
|
|
164
|
+
for (const date of dates) {
|
|
165
|
+
let text = "";
|
|
166
|
+
try {
|
|
167
|
+
text = await readFile(guidePath(stateRoot, "detail", `${date}.ndjson`), "utf8");
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
for (const raw of text.split("\n")) {
|
|
173
|
+
if (raw === "")
|
|
174
|
+
continue;
|
|
175
|
+
try {
|
|
176
|
+
const line = JSON.parse(raw);
|
|
177
|
+
if (typeof line.t === "number" && isGuideApp(line.a) && isRuleId(line.r) && isOutcome(line.o) && typeof line.x === "string")
|
|
178
|
+
lines.push(line);
|
|
179
|
+
}
|
|
180
|
+
catch { /* Skipped. */ }
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return lines;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Moves a reported day's fresh lines into <date>.reported.ndjson. A hook that appends while this runs writes a new fresh
|
|
187
|
+
* file, which the next pass reports together with everything else of that day.
|
|
188
|
+
*/
|
|
189
|
+
export async function markReported(stateRoot, date) {
|
|
190
|
+
const fresh = guidePath(stateRoot, "events", `${date}.ndjson`);
|
|
191
|
+
const merging = `${fresh}.${process.pid}.merging`;
|
|
192
|
+
try {
|
|
193
|
+
await rename(fresh, merging);
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const text = await readFile(merging, "utf8").catch(() => "");
|
|
199
|
+
await appendFile(guidePath(stateRoot, "events", `${date}.reported.ndjson`), text, { mode: FILE_MODE });
|
|
200
|
+
await rm(merging, { force: true });
|
|
201
|
+
}
|
|
202
|
+
/** Removes a day the Gateway refused, so it is not sent again. */
|
|
203
|
+
export async function dropDay(stateRoot, date) {
|
|
204
|
+
await rm(guidePath(stateRoot, "events", `${date}.ndjson`), { force: true });
|
|
205
|
+
await rm(guidePath(stateRoot, "events", `${date}.reported.ndjson`), { force: true });
|
|
206
|
+
}
|
|
207
|
+
/** Day files older than fourteen days, and sessions untouched for two days, are deleted. */
|
|
208
|
+
export async function pruneGuideFiles(stateRoot, now) {
|
|
209
|
+
const oldest = utcDate(now - LOCAL_RETENTION_DAYS * 86_400_000);
|
|
210
|
+
for (const folder of ["events", "detail"]) {
|
|
211
|
+
let names = [];
|
|
212
|
+
try {
|
|
213
|
+
names = await readdir(guidePath(stateRoot, folder));
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
for (const name of names) {
|
|
219
|
+
const date = /^(\d{4}-\d{2}-\d{2})/.exec(name)?.[1];
|
|
220
|
+
if (date !== undefined && date < oldest)
|
|
221
|
+
await rm(guidePath(stateRoot, folder, name), { force: true });
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
let sessions = [];
|
|
225
|
+
try {
|
|
226
|
+
sessions = await readdir(guidePath(stateRoot, "sessions"));
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
for (const name of sessions) {
|
|
232
|
+
const path = guidePath(stateRoot, "sessions", name);
|
|
233
|
+
const info = await stat(path).catch(() => null);
|
|
234
|
+
if (info !== null && now - info.mtimeMs > SESSION_TTL_MS)
|
|
235
|
+
await rm(path, { force: true });
|
|
236
|
+
}
|
|
237
|
+
}
|