@indigoai-us/hq-cli 5.345.226 → 5.345.230
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/CHANGELOG.md +156 -0
- package/dist/command-catalog.generated.d.ts +376 -11
- package/dist/command-catalog.generated.js +451 -11
- package/dist/commands/auth.d.ts +2 -2
- package/dist/commands/auth.js +3 -3
- package/dist/commands/core-delegate-w1b.js +92 -69
- package/dist/commands/core-delegate.d.ts +19 -1
- package/dist/commands/core-delegate.js +411 -6
- package/dist/commands/core-dm-bind-native.js +2 -1
- package/dist/commands/core-handoff.d.ts +5 -0
- package/dist/commands/core-handoff.js +1008 -14
- package/dist/commands/core-pin-native.d.ts +1 -0
- package/dist/commands/core-pin-native.js +3 -15
- package/dist/commands/core-register-project-native.d.ts +6 -0
- package/dist/commands/core-register-project-native.js +182 -52
- package/dist/commands/core-work-mesh-bind-trusted-native.js +4 -9
- package/dist/commands/daemon.js +2 -2
- package/dist/commands/index-cmd.d.ts +3 -1
- package/dist/commands/index-cmd.js +12 -0
- package/dist/commands/lanes-checks.js +1 -1
- package/dist/commands/lanes-session-start.d.ts +1 -2
- package/dist/commands/lanes-session-start.js +9 -145
- package/dist/commands/lanes-shared.d.ts +3 -0
- package/dist/commands/lanes.d.ts +3 -0
- package/dist/commands/lanes.js +94 -27
- package/dist/commands/link.d.ts +8 -3
- package/dist/commands/link.js +14 -17
- package/dist/commands/mcp-registration.d.ts +65 -7
- package/dist/commands/mcp-registration.js +289 -51
- package/dist/commands/mesh.d.ts +12 -0
- package/dist/commands/mesh.js +92 -10
- package/dist/commands/reindex.js +6 -0
- package/dist/commands/repo.d.ts +13 -0
- package/dist/commands/repo.js +39 -1
- package/dist/commands/sync-mode.d.ts +3 -1
- package/dist/commands/sync-mode.js +11 -1
- package/dist/hq-cloud-sync-runner/bin/sync-runner.js +1473 -1043
- package/dist/hq-cloud-sync-runner/package.json +1 -1
- package/dist/lazy-commands.js +3 -4
- package/dist/lib/checkpoint-ontology/entity-file.js +4 -4
- package/dist/lib/checkpoint-ontology/merge-copies.js +7 -11
- package/dist/lib/checkpoint-ontology/plan.js +2 -1
- package/dist/lib/core-utils/archive-old-threads.js +5 -2
- package/dist/lib/core-utils/session-company.d.ts +13 -1
- package/dist/lib/core-utils/session-company.js +75 -4
- package/dist/lib/core-utils/worktree-resume.d.ts +6 -0
- package/dist/lib/core-utils/worktree-resume.js +8 -4
- package/dist/lib/daemon/background-qos.d.ts +7 -0
- package/dist/lib/daemon/background-qos.js +13 -0
- package/dist/lib/daemon/catalog.d.ts +9 -0
- package/dist/lib/daemon/catalog.js +8 -1
- package/dist/lib/daemon/hook-requests-acl-suggest.d.ts +18 -0
- package/dist/lib/daemon/hook-requests-acl-suggest.js +245 -0
- package/dist/lib/daemon/hook-requests-conduct-link.d.ts +20 -0
- package/dist/lib/daemon/hook-requests-conduct-link.js +105 -0
- package/dist/lib/daemon/hook-requests-fleet-session-start.d.ts +32 -0
- package/dist/lib/daemon/hook-requests-fleet-session-start.js +214 -0
- package/dist/lib/daemon/hook-requests-mesh-tool-writes.d.ts +44 -0
- package/dist/lib/daemon/hook-requests-mesh-tool-writes.js +292 -0
- package/dist/lib/daemon/hook-requests-mesh.d.ts +54 -0
- package/dist/lib/daemon/hook-requests-mesh.js +559 -0
- package/dist/lib/daemon/hook-requests.d.ts +354 -8
- package/dist/lib/daemon/hook-requests.js +1330 -46
- package/dist/lib/daemon/machine-checks.d.ts +12 -2
- package/dist/lib/daemon/machine-checks.js +27 -10
- package/dist/lib/daemon/merge-sections.d.ts +24 -2
- package/dist/lib/daemon/merge-sections.js +58 -16
- package/dist/lib/daemon/ops/policy.js +4 -2
- package/dist/lib/daemon/resolver.js +9 -2
- package/dist/lib/daemon/run.d.ts +11 -7
- package/dist/lib/daemon/run.js +168 -24
- package/dist/lib/daemon/search-index-worker-v1.d.ts +2 -0
- package/dist/lib/daemon/search-index-worker-v1.js +25 -0
- package/dist/lib/daemon/session-registry.d.ts +0 -1
- package/dist/lib/daemon/session-registry.js +0 -3
- package/dist/lib/daemon/supervisor.d.ts +3 -1
- package/dist/lib/daemon/supervisor.js +1 -1
- package/dist/lib/daemon/tcc-eperm.d.ts +59 -4
- package/dist/lib/daemon/tcc-eperm.js +110 -8
- package/dist/lib/doctor/fix/process-identity.d.ts +20 -2
- package/dist/lib/doctor/fix/process-identity.js +48 -5
- package/dist/lib/flag-registry-worker-source.generated.d.ts +1 -1
- package/dist/lib/flag-registry-worker-source.generated.js +1 -1
- package/dist/lib/hook-engine/canary-gate.js +1 -0
- package/dist/lib/hook-engine/native-stop-compatibility.json +2 -1
- package/dist/lib/hot-channels/adapters/attio.js +2 -1
- package/dist/lib/hot-channels/adapters/meetings.js +2 -1
- package/dist/lib/lanes/company-outposts.d.ts +37 -0
- package/dist/lib/lanes/company-outposts.js +164 -0
- package/dist/lib/lanes/liveness.d.ts +6 -0
- package/dist/lib/lanes/liveness.js +49 -8
- package/dist/lib/lanes/orphan-report.d.ts +15 -0
- package/dist/lib/lanes/orphan-report.js +223 -0
- package/dist/lib/lanes/orphan-sweep.d.ts +26 -0
- package/dist/lib/lanes/orphan-sweep.js +152 -0
- package/dist/lib/lanes/reaper.d.ts +2 -0
- package/dist/lib/lanes/reaper.js +11 -8
- package/dist/lib/lanes/reconcile.d.ts +8 -7
- package/dist/lib/lanes/reconcile.js +39 -0
- package/dist/lib/mesh/live/backfill-held.d.ts +13 -0
- package/dist/lib/mesh/live/backfill-held.js +23 -0
- package/dist/lib/mesh/live/daemon/mode.d.ts +8 -4
- package/dist/lib/mesh/live/daemon/mode.js +8 -4
- package/dist/lib/mesh/live/daemon/run.d.ts +14 -3
- package/dist/lib/mesh/live/daemon/run.js +116 -7
- package/dist/lib/mesh/live/direct-drain.d.ts +123 -0
- package/dist/lib/mesh/live/direct-drain.js +315 -0
- package/dist/lib/mesh/live/emit-replay.d.ts +10 -0
- package/dist/lib/mesh/live/emit-replay.js +10 -0
- package/dist/lib/mesh/live/flush.d.ts +15 -0
- package/dist/lib/mesh/live/flush.js +27 -0
- package/dist/lib/mesh/live/liveness-carry.d.ts +45 -0
- package/dist/lib/mesh/live/liveness-carry.js +141 -0
- package/dist/lib/mesh/live/spool.d.ts +19 -0
- package/dist/lib/mesh/live/spool.js +80 -0
- package/dist/lib/safe-store.d.ts +2 -0
- package/dist/lib/safe-store.js +8 -6
- package/dist/lib/search-index/index.d.ts +9 -3
- package/dist/lib/search-index/index.js +35 -10
- package/dist/lib/search-index/worker-process.d.ts +9 -0
- package/dist/lib/search-index/worker-process.js +171 -0
- package/dist/lib/session-company-lock.d.ts +3 -1
- package/dist/lib/session-company-lock.js +3 -2
- package/dist/utils/delegate-command-supervisor.d.ts +3 -0
- package/dist/utils/delegate-command-supervisor.js +44 -0
- package/dist/utils/delegate-lock.d.ts +12 -0
- package/dist/utils/delegate-lock.js +88 -0
- package/dist/utils/frontmatter.d.ts +27 -0
- package/dist/utils/frontmatter.js +33 -0
- package/dist/utils/hook-trust.d.ts +1 -0
- package/dist/utils/hook-trust.js +18 -2
- package/dist/utils/host-registration.d.ts +13 -1
- package/dist/utils/host-registration.js +98 -5
- package/dist/utils/package-use-lease.d.ts +1 -1
- package/dist/utils/package-use-lease.js +15 -7
- package/docs/anywhere-runtime.md +17 -0
- package/package.json +9 -8
- package/scripts/check-function-manifest.mjs +17 -3
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two Work Mesh requests the native SessionStart hook leaves for the
|
|
3
|
+
* daemon (`35-work-mesh-session-start`, hq-core-staging 4c03a27c5):
|
|
4
|
+
*
|
|
5
|
+
* - `mesh-reconcile`: `_wm_reconcile_drain`. Runs `hq mesh context reconcile`
|
|
6
|
+
* on the newest observation of a session, removes it and the snapshots it
|
|
7
|
+
* supersedes, then the hourly sweep of `workspace/tmp/work-mesh`.
|
|
8
|
+
* - `mesh-register-retry`: `retry_pending` and `register_one` of
|
|
9
|
+
* `core/scripts/register-project.sh`, in process. No bash, no jq.
|
|
10
|
+
*
|
|
11
|
+
* The daemon is the only drainer, so there is no lock folder and no pending
|
|
12
|
+
* folder. A note is data: every path is built here from checked fields.
|
|
13
|
+
*
|
|
14
|
+
* The retry note's `uid` is the session's `HQ_COMPANY_UID`, the one
|
|
15
|
+
* environment value a note may carry (conductor ruling, 2026-11-17): an
|
|
16
|
+
* identifier, not a secret, accepted only as `cmp_` plus 3 to 128 letters
|
|
17
|
+
* or digits (or empty) and refused otherwise.
|
|
18
|
+
*/
|
|
19
|
+
import * as fs from "node:fs";
|
|
20
|
+
import * as path from "node:path";
|
|
21
|
+
// A cycle with hook-requests.ts, which dispatches to this module: each side uses the other only inside functions.
|
|
22
|
+
import { spawnRequestProgram } from "./hook-requests.js";
|
|
23
|
+
export const MESH_RECONCILE_KILL_MS = 10_000;
|
|
24
|
+
export const MESH_ENSURE_KILL_MS = 5_000;
|
|
25
|
+
/** `retry_pending` registers at most this many entries per pass. */
|
|
26
|
+
export const MESH_RETRY_LIMIT = 3;
|
|
27
|
+
const STDOUT_MAX_BYTES = 1024 * 1024;
|
|
28
|
+
const DAY_MS = 1440 * 60_000;
|
|
29
|
+
const HOUR_MS = 60 * 60_000;
|
|
30
|
+
const OBSERVATION = /^[A-Za-z0-9_-][A-Za-z0-9._-]*\.json$/;
|
|
31
|
+
const COMPANY = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
32
|
+
const UID = /^cmp_[A-Za-z0-9]{3,128}$/;
|
|
33
|
+
const exact = (args, keys) => {
|
|
34
|
+
const have = Object.keys(args);
|
|
35
|
+
return have.length === keys.length && keys.every(key => typeof args[key] === "string");
|
|
36
|
+
};
|
|
37
|
+
/** The operation in a note's `kind` and `args`, or undefined for anything the writer does not produce. */
|
|
38
|
+
export function parseMeshOperation(kind, args, session) {
|
|
39
|
+
if (kind === "mesh-reconcile") {
|
|
40
|
+
if (!exact(args, ["observation", "seq"]))
|
|
41
|
+
return undefined;
|
|
42
|
+
const observation = args.observation;
|
|
43
|
+
const seq = args.seq;
|
|
44
|
+
if (!/^[0-9]{1,20}$/.test(seq) || observation.length > 512 || !OBSERVATION.test(observation))
|
|
45
|
+
return undefined;
|
|
46
|
+
// `<sid>.<seq>.<client op>.json`, for this note's session.
|
|
47
|
+
if (!session || !observation.startsWith(`${session}.${seq}.`))
|
|
48
|
+
return undefined;
|
|
49
|
+
if (!/^[A-Za-z0-9_-]+\.json$/.test(observation.slice(session.length + seq.length + 2)))
|
|
50
|
+
return undefined;
|
|
51
|
+
return { kind, observation, seq };
|
|
52
|
+
}
|
|
53
|
+
if (kind === "mesh-register-retry") {
|
|
54
|
+
if (!exact(args, ["company", "uid"]))
|
|
55
|
+
return undefined;
|
|
56
|
+
const company = args.company;
|
|
57
|
+
const uid = args.uid;
|
|
58
|
+
if (!COMPANY.test(company) || company === "." || company === "..")
|
|
59
|
+
return undefined;
|
|
60
|
+
if (uid !== "" && !UID.test(uid))
|
|
61
|
+
return undefined;
|
|
62
|
+
return { kind, company, uid };
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Run the daemon's own CLI. No shell. The stop model of every request kind
|
|
68
|
+
* (`spawnRequestProgram`): its own process group, SIGTERM to the group at
|
|
69
|
+
* `timeoutMs` or on a daemon stop, SIGKILL to the group after the grace.
|
|
70
|
+
* A call that was stopped is a failed call.
|
|
71
|
+
*/
|
|
72
|
+
function runHq(options, root, args, extraEnv, timeoutMs, capture) {
|
|
73
|
+
return new Promise(resolve => {
|
|
74
|
+
if (options.signal?.aborted) {
|
|
75
|
+
resolve({ code: 1, stdout: "" });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const env = { ...(options.env ?? process.env), HQ_ROOT: root, HQ_NO_UPDATE_CHECK: "1", ...extraEnv };
|
|
79
|
+
const { child, signalAll, settle } = spawnRequestProgram(options.nodeBinary, [options.hqBinary, ...args], {
|
|
80
|
+
cwd: root, env, windowsHide: true, stdio: ["ignore", capture ? "pipe" : "ignore", "ignore"],
|
|
81
|
+
}, { timeoutMs, killGraceMs: options.killGraceMs, signal: options.signal });
|
|
82
|
+
const chunks = [];
|
|
83
|
+
let size = 0;
|
|
84
|
+
let over = false;
|
|
85
|
+
child.stdout?.on("data", (chunk) => {
|
|
86
|
+
size += chunk.length;
|
|
87
|
+
if (size > STDOUT_MAX_BYTES) {
|
|
88
|
+
over = true;
|
|
89
|
+
signalAll("SIGKILL");
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
chunks.push(chunk);
|
|
93
|
+
});
|
|
94
|
+
let settled = false;
|
|
95
|
+
const finish = (code) => {
|
|
96
|
+
if (settled)
|
|
97
|
+
return;
|
|
98
|
+
settled = true;
|
|
99
|
+
if (settle()) {
|
|
100
|
+
resolve({ code: code === 0 ? 1 : code, stdout: "" });
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
resolve({ code: over ? 1 : code, stdout: over ? "" : Buffer.concat(chunks).toString("utf8") });
|
|
104
|
+
};
|
|
105
|
+
child.once("error", () => finish(1));
|
|
106
|
+
child.once("close", code => finish(code ?? 1));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
const lstat = (target) => {
|
|
110
|
+
try {
|
|
111
|
+
return fs.lstatSync(target);
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
/** `absent`, `real` (a folder, every part below `root` a real folder) or `refused` (a link or another kind of file on the way). */
|
|
118
|
+
function realFolder(root, parts) {
|
|
119
|
+
let at = root;
|
|
120
|
+
for (const part of parts) {
|
|
121
|
+
at = path.join(at, part);
|
|
122
|
+
const facts = lstat(at);
|
|
123
|
+
if (!facts)
|
|
124
|
+
return "absent";
|
|
125
|
+
if (!facts.isDirectory())
|
|
126
|
+
return "refused";
|
|
127
|
+
}
|
|
128
|
+
return "real";
|
|
129
|
+
}
|
|
130
|
+
const TMP_PARTS = ["workspace", "tmp", "work-mesh"];
|
|
131
|
+
/** `_wm_tmp_sweep_dir`: day-old leftovers in one scratch folder. */
|
|
132
|
+
function sweepFolder(dir, now) {
|
|
133
|
+
const old = (facts) => now - facts.mtimeMs > DAY_MS;
|
|
134
|
+
let names;
|
|
135
|
+
try {
|
|
136
|
+
names = fs.readdirSync(dir);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
for (const name of names) {
|
|
142
|
+
const entry = path.join(dir, name);
|
|
143
|
+
const facts = lstat(entry);
|
|
144
|
+
if (!facts)
|
|
145
|
+
continue;
|
|
146
|
+
if (facts.isFile()) {
|
|
147
|
+
if (old(facts))
|
|
148
|
+
try {
|
|
149
|
+
fs.unlinkSync(entry);
|
|
150
|
+
}
|
|
151
|
+
catch { /* next sweep */ }
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (!facts.isDirectory())
|
|
155
|
+
continue;
|
|
156
|
+
if (old(facts) && (name.endsWith(".lock") || name.endsWith(".reclaim") || name.includes(".stale."))) {
|
|
157
|
+
try {
|
|
158
|
+
fs.rmSync(entry, { recursive: true, force: true });
|
|
159
|
+
}
|
|
160
|
+
catch { /* next sweep */ }
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
let inner;
|
|
164
|
+
try {
|
|
165
|
+
inner = fs.readdirSync(entry);
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
for (const file of inner) {
|
|
171
|
+
const target = path.join(entry, file);
|
|
172
|
+
const fileFacts = lstat(target);
|
|
173
|
+
if (fileFacts?.isFile() && old(fileFacts))
|
|
174
|
+
try {
|
|
175
|
+
fs.unlinkSync(target);
|
|
176
|
+
}
|
|
177
|
+
catch { /* next sweep */ }
|
|
178
|
+
}
|
|
179
|
+
if (name.endsWith(".pending") && old(facts))
|
|
180
|
+
try {
|
|
181
|
+
fs.rmdirSync(entry);
|
|
182
|
+
}
|
|
183
|
+
catch { /* not empty */ }
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/** `_wm_tmp_sweep` for `workspace/tmp/work-mesh`, at most once an hour per root. The legacy folders in the system temp folder are not swept here. */
|
|
187
|
+
export function sweepMeshTmp(root, now) {
|
|
188
|
+
if (realFolder(root, TMP_PARTS) !== "real")
|
|
189
|
+
return false;
|
|
190
|
+
const tmp = path.join(root, ...TMP_PARTS);
|
|
191
|
+
const stamp = path.join(tmp, ".swept");
|
|
192
|
+
const stampFacts = lstat(stamp);
|
|
193
|
+
if (stampFacts && !stampFacts.isFile())
|
|
194
|
+
return false;
|
|
195
|
+
if (stampFacts && now - stampFacts.mtimeMs < HOUR_MS)
|
|
196
|
+
return false;
|
|
197
|
+
for (const name of ["obs", "reconcile", "register-pending"]) {
|
|
198
|
+
if (lstat(path.join(tmp, name))?.isDirectory())
|
|
199
|
+
sweepFolder(path.join(tmp, name), now);
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
fs.writeFileSync(stamp, "");
|
|
203
|
+
const at = new Date(now);
|
|
204
|
+
fs.utimesSync(stamp, at, at);
|
|
205
|
+
}
|
|
206
|
+
catch { /* swept again next time */ }
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
/** `_wm_reconcile_drain` for one session's newest observation. */
|
|
210
|
+
export async function runMeshReconcile(root, session, operation, options) {
|
|
211
|
+
const log = options.log ?? (() => undefined);
|
|
212
|
+
const now = options.now ?? Date.now;
|
|
213
|
+
const folder = realFolder(root, [...TMP_PARTS, "obs"]);
|
|
214
|
+
if (folder === "refused") {
|
|
215
|
+
log(`mesh-reconcile refused: workspace/tmp/work-mesh/obs under ${root} is not a real folder`);
|
|
216
|
+
return { code: 1 };
|
|
217
|
+
}
|
|
218
|
+
if (folder === "absent")
|
|
219
|
+
return { code: 0 };
|
|
220
|
+
const dir = path.join(root, ...TMP_PARTS, "obs");
|
|
221
|
+
const file = path.join(dir, operation.observation);
|
|
222
|
+
const facts = lstat(file);
|
|
223
|
+
if (facts && !facts.isFile()) {
|
|
224
|
+
log(`mesh-reconcile refused: ${operation.observation} is not a regular file`);
|
|
225
|
+
return { code: 1 };
|
|
226
|
+
}
|
|
227
|
+
if (facts) {
|
|
228
|
+
// A daemon stop is not a reconcile: the call was not made, or was cut
|
|
229
|
+
// short. The observation stays and the request is a failed try.
|
|
230
|
+
if (options.signal?.aborted)
|
|
231
|
+
return { code: 1 };
|
|
232
|
+
// The shell ignores the exit status: a failed reconcile is not run again.
|
|
233
|
+
await runHq(options, root, ["mesh", "context", "reconcile", "--observation-file", file, "--machine"], {}, options.reconcileKillMs ?? MESH_RECONCILE_KILL_MS, false);
|
|
234
|
+
if (options.signal?.aborted)
|
|
235
|
+
return { code: 1 };
|
|
236
|
+
try {
|
|
237
|
+
fs.unlinkSync(file);
|
|
238
|
+
}
|
|
239
|
+
catch { /* the sweep takes it */ }
|
|
240
|
+
}
|
|
241
|
+
// Older snapshots of the same session are superseded.
|
|
242
|
+
const seq = BigInt(operation.seq);
|
|
243
|
+
const prefix = `${session}.`;
|
|
244
|
+
let names = [];
|
|
245
|
+
try {
|
|
246
|
+
names = fs.readdirSync(dir);
|
|
247
|
+
}
|
|
248
|
+
catch { /* nothing to remove */ }
|
|
249
|
+
for (const name of names) {
|
|
250
|
+
if (!name.startsWith(prefix))
|
|
251
|
+
continue;
|
|
252
|
+
const rest = /^([0-9]{1,20})\.[A-Za-z0-9_-]+\.json$/.exec(name.slice(prefix.length));
|
|
253
|
+
if (!rest || BigInt(rest[1]) > seq)
|
|
254
|
+
continue;
|
|
255
|
+
const target = path.join(dir, name);
|
|
256
|
+
if (lstat(target)?.isFile())
|
|
257
|
+
try {
|
|
258
|
+
fs.unlinkSync(target);
|
|
259
|
+
}
|
|
260
|
+
catch { /* the sweep takes it */ }
|
|
261
|
+
}
|
|
262
|
+
sweepMeshTmp(root, now());
|
|
263
|
+
return { code: 0 };
|
|
264
|
+
}
|
|
265
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
266
|
+
/** jq's `a // b`: `b` when `a` is null or false. */
|
|
267
|
+
const alt = (value, other) => (value === null || value === undefined || value === false ? other : value);
|
|
268
|
+
const text = (value) => typeof value === "string" ? value : typeof value === "number" ? String(value) : undefined;
|
|
269
|
+
const segment = (value) => value !== "" && value !== "." && value !== ".." && !/[/\\\0]/.test(value) && !value.startsWith("-");
|
|
270
|
+
/** `recorded_uid`: `.company-uid`, then the manifest's `cloud_uid`. */
|
|
271
|
+
export function recordedUid(root, company) {
|
|
272
|
+
const file = path.join(root, "companies", company, ".company-uid");
|
|
273
|
+
try {
|
|
274
|
+
if (fs.statSync(file).isFile())
|
|
275
|
+
return fs.readFileSync(file, "utf8").replace(/[ \t\n\v\f\r]/g, "");
|
|
276
|
+
}
|
|
277
|
+
catch { /* the manifest */ }
|
|
278
|
+
let manifest;
|
|
279
|
+
try {
|
|
280
|
+
const manifestPath = path.join(root, "companies", "manifest.yaml");
|
|
281
|
+
if (!fs.statSync(manifestPath).isFile())
|
|
282
|
+
return "";
|
|
283
|
+
manifest = fs.readFileSync(manifestPath, "utf8");
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
return "";
|
|
287
|
+
}
|
|
288
|
+
let inCompanies = false;
|
|
289
|
+
let inCompany = false;
|
|
290
|
+
for (const line of manifest.split("\n")) {
|
|
291
|
+
const fields = line.split(/[ \t]+/).filter(Boolean);
|
|
292
|
+
if (/^companies:[ \t\n\v\f\r]*$/.test(line)) {
|
|
293
|
+
inCompanies = true;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
if (inCompanies && /^[^ \t\n\v\f\r]/.test(line))
|
|
297
|
+
inCompanies = false;
|
|
298
|
+
if (inCompanies && /^ {2}[^ #][^:]*:/.test(line)) {
|
|
299
|
+
const slug = (fields[0] ?? "").replace(/[ \t\n\v\f\r]+$/, "").replace(/:$/, "");
|
|
300
|
+
inCompany = slug === company;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (inCompany && /^ {2}[^ \t\n\v\f\r#]/.test(line))
|
|
304
|
+
return "";
|
|
305
|
+
if (inCompany && fields[0] === "cloud_uid:")
|
|
306
|
+
return (fields[1] ?? "").replace(/["']/g, "").replace(/[ \t\n\v\f\r]+$/, "");
|
|
307
|
+
}
|
|
308
|
+
return "";
|
|
309
|
+
}
|
|
310
|
+
/** A number as its significant digits and power of ten; `undefined` for zero. */
|
|
311
|
+
function decimal(literal) {
|
|
312
|
+
const parts = /^-?([0-9]+)(?:\.([0-9]+))?(?:[eE]([+-]?[0-9]+))?$/.exec(literal);
|
|
313
|
+
if (!parts)
|
|
314
|
+
return { digits: literal, power: Number.NaN };
|
|
315
|
+
const fraction = parts[2] ?? "";
|
|
316
|
+
const all = (parts[1] + fraction).replace(/^0+/, "");
|
|
317
|
+
const digits = all.replace(/0+$/, "");
|
|
318
|
+
if (!digits)
|
|
319
|
+
return undefined;
|
|
320
|
+
return { digits, power: Number(parts[3] ?? "0") - fraction.length + (all.length - digits.length) };
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* True when every number in valid JSON `source` has the same value after
|
|
324
|
+
* `JSON.parse` and `JSON.stringify`. The spelling may change (`1.0` to `1`);
|
|
325
|
+
* the value may not (an integer past 2^53, more digits than a double holds,
|
|
326
|
+
* a number too large for one).
|
|
327
|
+
*/
|
|
328
|
+
export function numbersSurvive(source) {
|
|
329
|
+
const token = /"(?:[^"\\]|\\.)*"|-?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/g;
|
|
330
|
+
for (const found of source.matchAll(token)) {
|
|
331
|
+
const literal = found[0];
|
|
332
|
+
if (literal.startsWith('"'))
|
|
333
|
+
continue;
|
|
334
|
+
const value = Number(literal);
|
|
335
|
+
if (!Number.isFinite(value))
|
|
336
|
+
return false;
|
|
337
|
+
const before = decimal(literal);
|
|
338
|
+
const after = decimal(String(value));
|
|
339
|
+
if (before === undefined || after === undefined) {
|
|
340
|
+
if (before !== after)
|
|
341
|
+
return false;
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
if (before.digits !== after.digits || before.power !== after.power)
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Read a board the port can edit: an object whose `projects` is an array of
|
|
351
|
+
* objects, and whose numbers all come back with the same value when the
|
|
352
|
+
* board is written again.
|
|
353
|
+
*/
|
|
354
|
+
function readBoard(file) {
|
|
355
|
+
let value;
|
|
356
|
+
let source;
|
|
357
|
+
try {
|
|
358
|
+
source = fs.readFileSync(file, "utf8");
|
|
359
|
+
value = JSON.parse(source);
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
if (!isRecord(value) || !Array.isArray(value.projects) || !value.projects.every(isRecord))
|
|
365
|
+
return undefined;
|
|
366
|
+
if (!numbersSurvive(source))
|
|
367
|
+
return undefined;
|
|
368
|
+
return value;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Replace `file` with `content`: a new file beside it, written in full and
|
|
372
|
+
* flushed, given the mode `file` has now, then renamed over it. A reader
|
|
373
|
+
* sees the old board or the new one, and the mode does not change.
|
|
374
|
+
*/
|
|
375
|
+
function replaceKeepingMode(file, content) {
|
|
376
|
+
const temporary = path.join(path.dirname(file), `.${path.basename(file)}.hq-daemon.${process.pid}.${Date.now().toString(36)}.tmp`);
|
|
377
|
+
let handle;
|
|
378
|
+
try {
|
|
379
|
+
const facts = fs.lstatSync(file);
|
|
380
|
+
if (!facts.isFile())
|
|
381
|
+
return false;
|
|
382
|
+
handle = fs.openSync(temporary, "wx", 0o600);
|
|
383
|
+
fs.writeFileSync(handle, content);
|
|
384
|
+
fs.fchmodSync(handle, facts.mode & 0o7777);
|
|
385
|
+
fs.fsyncSync(handle);
|
|
386
|
+
fs.closeSync(handle);
|
|
387
|
+
handle = undefined;
|
|
388
|
+
fs.renameSync(temporary, file);
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
catch {
|
|
392
|
+
if (handle !== undefined)
|
|
393
|
+
try {
|
|
394
|
+
fs.closeSync(handle);
|
|
395
|
+
}
|
|
396
|
+
catch { /* already closed */ }
|
|
397
|
+
try {
|
|
398
|
+
fs.unlinkSync(temporary);
|
|
399
|
+
}
|
|
400
|
+
catch { /* never written */ }
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
const stamp = (now) => new Date(now).toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
405
|
+
/** `register_one`. True when the entry was registered and verified. */
|
|
406
|
+
async function registerOne(root, company, project, uid, options, log) {
|
|
407
|
+
const boardFile = path.join(root, "companies", company, "board.json");
|
|
408
|
+
const rel = `companies/${company}/projects/${project}/prd.json`;
|
|
409
|
+
const prd = path.join(root, rel);
|
|
410
|
+
const board = readBoard(boardFile);
|
|
411
|
+
if (!board)
|
|
412
|
+
return false;
|
|
413
|
+
// mesh_project_id_for_prd
|
|
414
|
+
const known = board.projects.find(entry => entry.prd_path === rel || entry.id === project);
|
|
415
|
+
const mesh = (known ? text(alt(alt(known.mesh_project_id, known.id), undefined)) : undefined) || project;
|
|
416
|
+
if (!segment(mesh)) {
|
|
417
|
+
log(`mesh-register-retry: ${company}/${project} skipped: its Work Mesh project id is not a plain name`);
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
try {
|
|
421
|
+
if (!fs.statSync(prd).isFile())
|
|
422
|
+
throw new Error("not a file");
|
|
423
|
+
}
|
|
424
|
+
catch {
|
|
425
|
+
log(`mesh-register-retry: registration incomplete: missing ${rel}`);
|
|
426
|
+
return false;
|
|
427
|
+
}
|
|
428
|
+
const matches = board.projects.filter(entry => entry.id === mesh || entry.id === project || entry.prd_path === rel || entry.mesh_project_id === mesh || entry.mesh_project_id === project);
|
|
429
|
+
if (matches.length > 1) {
|
|
430
|
+
log(`mesh-register-retry: registration refused: ${matches.length} Board entries in companies/${company}/board.json match ${project}`);
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
const ran = await runHq(options, root, ["mesh", "project", "ensure", mesh, "--company", company, "--stories-file", prd, "--json"], { HQ_COMPANY_UID: uid }, options.ensureKillMs ?? MESH_ENSURE_KILL_MS, true);
|
|
434
|
+
// A daemon stop cut the call short: not a failed ensure, and nothing is written.
|
|
435
|
+
if (options.signal?.aborted)
|
|
436
|
+
return false;
|
|
437
|
+
if (ran.code !== 0) {
|
|
438
|
+
log(`mesh-register-retry: registration incomplete: hq mesh project ensure failed for ${company}/${project}`);
|
|
439
|
+
return false;
|
|
440
|
+
}
|
|
441
|
+
let ensured;
|
|
442
|
+
try {
|
|
443
|
+
ensured = JSON.parse(ran.stdout);
|
|
444
|
+
}
|
|
445
|
+
catch {
|
|
446
|
+
ensured = undefined;
|
|
447
|
+
}
|
|
448
|
+
const thread = isRecord(ensured) && typeof ensured.threadId === "string" ? ensured.threadId.replace(/\r/g, "") : "";
|
|
449
|
+
const channel = isRecord(ensured) && typeof ensured.channelId === "string" ? ensured.channelId.replace(/\r/g, "") : "";
|
|
450
|
+
if (!thread || !channel) {
|
|
451
|
+
log(`mesh-register-retry: registration incomplete: ensure output missing threadId or channelId for ${company}/${project}`);
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
// The board is read again: the ensure call took time.
|
|
455
|
+
const fresh = readBoard(boardFile);
|
|
456
|
+
if (!fresh)
|
|
457
|
+
return false;
|
|
458
|
+
const now = stamp((options.now ?? Date.now)());
|
|
459
|
+
let title = project;
|
|
460
|
+
try {
|
|
461
|
+
const parsed = JSON.parse(fs.readFileSync(prd, "utf8"));
|
|
462
|
+
const name = isRecord(parsed) ? alt(parsed.name, "") : "";
|
|
463
|
+
if (typeof name === "string" && name.replace(/\r/g, ""))
|
|
464
|
+
title = name.replace(/\r/g, "");
|
|
465
|
+
}
|
|
466
|
+
catch { /* the project id is the title */ }
|
|
467
|
+
const hit = (entry) => entry.id === mesh || entry.id === project || entry.prd_path === rel || entry.mesh_project_id === mesh;
|
|
468
|
+
if (fresh.projects.some(hit)) {
|
|
469
|
+
fresh.projects = fresh.projects.map(entry => {
|
|
470
|
+
if (!hit(entry))
|
|
471
|
+
return entry;
|
|
472
|
+
const next = { ...entry, threadId: thread, channelId: channel, updated_at: now, prd_path: alt(entry.prd_path, rel), mesh_project_id: mesh };
|
|
473
|
+
delete next.pending_registration;
|
|
474
|
+
return next;
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
fresh.projects.push({
|
|
479
|
+
id: mesh, title, status: "prd_created", scope: "company", prd_path: rel,
|
|
480
|
+
threadId: thread, channelId: channel, mesh_project_id: mesh, created_at: now, updated_at: now,
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
if (!replaceKeepingMode(boardFile, `${JSON.stringify(fresh, null, 2)}\n`)) {
|
|
484
|
+
log(`mesh-register-retry: board.json for ${company} could not be written`);
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
const written = readBoard(boardFile);
|
|
488
|
+
const verified = written?.projects.some(entry => (entry.id === project || entry.prd_path === rel || entry.mesh_project_id === mesh)
|
|
489
|
+
&& entry.mesh_project_id === mesh && entry.threadId === thread && entry.channelId === channel) ?? false;
|
|
490
|
+
if (!verified)
|
|
491
|
+
log(`mesh-register-retry: registration incomplete: board.json entry for ${project} did not verify`);
|
|
492
|
+
return verified;
|
|
493
|
+
}
|
|
494
|
+
/** `retry_pending <company>`. */
|
|
495
|
+
export async function runMeshRegisterRetry(root, operation, options) {
|
|
496
|
+
const log = options.log ?? (() => undefined);
|
|
497
|
+
const { company } = operation;
|
|
498
|
+
const companyFacts = realFolder(root, ["companies", company]);
|
|
499
|
+
if (companyFacts !== "real") {
|
|
500
|
+
log(`mesh-register-retry refused: companies/${company} under ${root} is not a real folder`);
|
|
501
|
+
return { code: 1, registered: 0 };
|
|
502
|
+
}
|
|
503
|
+
const boardFile = path.join(root, "companies", company, "board.json");
|
|
504
|
+
const boardFacts = lstat(boardFile);
|
|
505
|
+
if (!boardFacts)
|
|
506
|
+
return { code: 0, registered: 0 };
|
|
507
|
+
if (!boardFacts.isFile()) {
|
|
508
|
+
log(`mesh-register-retry: companies/${company}/board.json is not a regular file; left untouched`);
|
|
509
|
+
return { code: 0, registered: 0 };
|
|
510
|
+
}
|
|
511
|
+
const board = readBoard(boardFile);
|
|
512
|
+
if (!board) {
|
|
513
|
+
log(`mesh-register-retry: companies/${company}/board.json is not a board this daemon can edit; left untouched`);
|
|
514
|
+
return { code: 0, registered: 0 };
|
|
515
|
+
}
|
|
516
|
+
const pending = [];
|
|
517
|
+
for (const entry of board.projects) {
|
|
518
|
+
if (entry.pending_registration !== true)
|
|
519
|
+
continue;
|
|
520
|
+
const id = text(alt(entry.id, undefined));
|
|
521
|
+
if (id)
|
|
522
|
+
pending.push(id);
|
|
523
|
+
}
|
|
524
|
+
if (pending.length === 0)
|
|
525
|
+
return { code: 0, registered: 0 };
|
|
526
|
+
// resolve_uid: the company's own record decides.
|
|
527
|
+
const uid = recordedUid(root, company);
|
|
528
|
+
if (!uid || /\0/.test(uid)) {
|
|
529
|
+
log(`mesh-register-retry: no recorded company uid for ${company}; its pending projects stay local`);
|
|
530
|
+
return { code: 0, registered: 0 };
|
|
531
|
+
}
|
|
532
|
+
if (operation.uid && operation.uid !== uid) {
|
|
533
|
+
log(`mesh-register-retry: the session's HQ_COMPANY_UID does not match ${company}'s recorded uid; nothing registered`);
|
|
534
|
+
return { code: 0, registered: 0 };
|
|
535
|
+
}
|
|
536
|
+
// A daemon stop is not a pass: the request is a failed try and runs again.
|
|
537
|
+
const stopped = () => options.signal?.aborted === true;
|
|
538
|
+
if (stopped())
|
|
539
|
+
return { code: 1, registered: 0 };
|
|
540
|
+
// cli_has_project_ensure
|
|
541
|
+
const probe = await runHq(options, root, ["mesh", "project", "ensure", "--help"], {}, options.ensureKillMs ?? MESH_ENSURE_KILL_MS, false);
|
|
542
|
+
if (stopped())
|
|
543
|
+
return { code: 1, registered: 0 };
|
|
544
|
+
if (probe.code !== 0)
|
|
545
|
+
return { code: 0, registered: 0 };
|
|
546
|
+
let registered = 0;
|
|
547
|
+
for (const id of pending.slice(0, MESH_RETRY_LIMIT)) {
|
|
548
|
+
if (stopped())
|
|
549
|
+
break;
|
|
550
|
+
if (!segment(id)) {
|
|
551
|
+
log(`mesh-register-retry: ${company} has a pending entry whose id is not a plain name; skipped`);
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
if (await registerOne(root, company, id, uid, options, log))
|
|
555
|
+
registered += 1;
|
|
556
|
+
}
|
|
557
|
+
return { code: stopped() ? 1 : 0, registered };
|
|
558
|
+
}
|
|
559
|
+
//# sourceMappingURL=hook-requests-mesh.js.map
|