@indigoai-us/hq-cli 5.17.0 → 5.18.1
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/dist/commands/cloud.d.ts +63 -1
- package/dist/commands/cloud.js +214 -11
- package/dist/commands/files-browse.d.ts +178 -0
- package/dist/commands/files-browse.js +348 -0
- package/dist/commands/files.d.ts +1 -1
- package/dist/commands/files.js +6 -2
- package/dist/commands/sync-mode.d.ts +115 -0
- package/dist/commands/sync-mode.js +249 -0
- package/dist/commands/sync-narrow.d.ts +154 -0
- package/dist/commands/sync-narrow.js +327 -0
- package/dist/index.js +11 -3
- package/dist/lib/local-tree-diff.d.ts +94 -0
- package/dist/lib/local-tree-diff.js +244 -0
- package/dist/lib/narrow-hint-banner.d.ts +102 -0
- package/dist/lib/narrow-hint-banner.js +144 -0
- package/package.json +2 -2
- package/src/commands/cloud.pull-all.test.ts +170 -1
- package/src/commands/cloud.pull-per-company.test.ts +188 -0
- package/src/commands/cloud.ts +327 -5
- package/src/commands/files-browse.test.ts +475 -0
- package/src/commands/files-browse.ts +561 -0
- package/src/commands/files.ts +6 -1
- package/src/commands/sync-mode.test.ts +366 -0
- package/src/commands/sync-mode.ts +387 -0
- package/src/commands/sync-narrow.test.ts +573 -0
- package/src/commands/sync-narrow.ts +541 -0
- package/src/index.ts +9 -1
- package/src/lib/hq-cloud-dep.smoke.test.ts +75 -0
- package/src/lib/local-tree-diff.test.ts +262 -0
- package/src/lib/local-tree-diff.ts +330 -0
- package/src/lib/narrow-hint-banner.test.ts +235 -0
- package/src/lib/narrow-hint-banner.ts +212 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helper for `hq sync narrow` (US-007).
|
|
3
|
+
*
|
|
4
|
+
* Walks a local directory tree under `<hqRoot>/companies/{slug}/`, classifies
|
|
5
|
+
* each file against a prospective `shared`-mode prefix set, and groups the
|
|
6
|
+
* results into three buckets:
|
|
7
|
+
*
|
|
8
|
+
* - **staying** — file is covered by the prospective prefix set; survives.
|
|
9
|
+
* - **clean** — file is NOT covered (orphan) AND is provably unchanged
|
|
10
|
+
* since the last journal sync (safe to delete).
|
|
11
|
+
* - **dirty** — file is NOT covered (orphan) AND has been locally
|
|
12
|
+
* modified since the last sync (sacred — abort without
|
|
13
|
+
* `--force`).
|
|
14
|
+
*
|
|
15
|
+
* The clean-vs-dirty rule mirrors hq-cloud's implicit-shrink path
|
|
16
|
+
* (`scope-shrink.ts::classifyOrphan`) verbatim so the two narrowing paths —
|
|
17
|
+
* implicit (next pull after sync-mode flip) and explicit (`hq sync narrow
|
|
18
|
+
* --apply`) — agree on what "dirty" means. See US-000 Task 3 in
|
|
19
|
+
* `companies/indigo/projects/hq-sync-browse-vs-sync/references.md`.
|
|
20
|
+
*
|
|
21
|
+
* Pure: no network. Reads the journal off `SyncJournal` (caller passes it
|
|
22
|
+
* in), stats + hashes files on disk — no journal mutations and no remote
|
|
23
|
+
* calls happen here. The CLI orchestrator (`sync-narrow.ts`) is responsible
|
|
24
|
+
* for the destructive side effects (delete, tombstone, PUT sync-config).
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="77563dec-63f2-52df-9138-c88bfd1600b9")}catch(e){}}();
|
|
28
|
+
import * as fs from "node:fs";
|
|
29
|
+
import * as path from "node:path";
|
|
30
|
+
import { hashFile, isCoveredByAny, } from "@indigoai-us/hq-cloud";
|
|
31
|
+
// ── Pure entry point ────────────────────────────────────────────────────────
|
|
32
|
+
/**
|
|
33
|
+
* Build a NarrowPlan by walking `<hqRoot>/companies/<slug>/` and classifying
|
|
34
|
+
* each regular file against `prospectivePrefixSet` + `journal`.
|
|
35
|
+
*
|
|
36
|
+
* Walk semantics:
|
|
37
|
+
* - Recursive `fs.readdirSync(... withFileTypes)`.
|
|
38
|
+
* - Symlinks are RECORDED but never followed (`lstat`, not `stat`), matching
|
|
39
|
+
* the `share()`/sync engine's contract.
|
|
40
|
+
* - Hidden files + `.DS_Store` + `node_modules` are walked as-is — the
|
|
41
|
+
* caller is expected to point this at HQ content, where `.hqignore`
|
|
42
|
+
* filtering happens upstream of the journal. The journal already
|
|
43
|
+
* reflects what's been pushed/pulled, so `not-in-journal` is the
|
|
44
|
+
* classifier signal for "we didn't put this here".
|
|
45
|
+
* - If the walk root is missing entirely, returns an empty plan (rather
|
|
46
|
+
* than throwing) — a freshly-flipped membership may not have synced any
|
|
47
|
+
* files yet.
|
|
48
|
+
*/
|
|
49
|
+
export function buildNarrowPlan(input) {
|
|
50
|
+
const { hqRoot, companySlug, prospectivePrefixSet, journal } = input;
|
|
51
|
+
const walkRoot = path.join(hqRoot, "companies", companySlug);
|
|
52
|
+
const staying = [];
|
|
53
|
+
const clean = [];
|
|
54
|
+
const dirty = [];
|
|
55
|
+
if (!fs.existsSync(walkRoot)) {
|
|
56
|
+
return emptyPlan();
|
|
57
|
+
}
|
|
58
|
+
walkLocal(walkRoot, hqRoot, (file) => {
|
|
59
|
+
if (isCoveredByAny(file.relPath, prospectivePrefixSet)) {
|
|
60
|
+
staying.push(file);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const verdict = classifyAgainstJournal(file, journal);
|
|
64
|
+
if (verdict.clean) {
|
|
65
|
+
clean.push(file);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
dirty.push({ ...file, reason: verdict.reason });
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
staying,
|
|
73
|
+
clean,
|
|
74
|
+
dirty,
|
|
75
|
+
totalStayingCount: staying.length,
|
|
76
|
+
totalCleanCount: clean.length,
|
|
77
|
+
totalCleanBytes: clean.reduce((sum, f) => sum + f.bytes, 0),
|
|
78
|
+
totalDirtyCount: dirty.length,
|
|
79
|
+
totalDirtyBytes: dirty.reduce((sum, f) => sum + f.bytes, 0),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
// ── Internal: walk + classify ───────────────────────────────────────────────
|
|
83
|
+
function emptyPlan() {
|
|
84
|
+
return {
|
|
85
|
+
staying: [],
|
|
86
|
+
clean: [],
|
|
87
|
+
dirty: [],
|
|
88
|
+
totalStayingCount: 0,
|
|
89
|
+
totalCleanCount: 0,
|
|
90
|
+
totalCleanBytes: 0,
|
|
91
|
+
totalDirtyCount: 0,
|
|
92
|
+
totalDirtyBytes: 0,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Recursive readdir walk yielding regular files (and dangling-target
|
|
97
|
+
* symlinks — those are recorded as files for narrow-plan purposes; the
|
|
98
|
+
* delete path uses `fs.unlinkSync` which handles symlinks correctly).
|
|
99
|
+
*
|
|
100
|
+
* Uses `lstat` rather than `stat` so a symlink's size doesn't follow the
|
|
101
|
+
* target chain (matches the share-engine convention).
|
|
102
|
+
*/
|
|
103
|
+
function walkLocal(dir, hqRoot, emit) {
|
|
104
|
+
let entries;
|
|
105
|
+
try {
|
|
106
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
// Permission-denied or ENOENT mid-walk — skip silently; the CLI
|
|
110
|
+
// surfaces the higher-level "company folder missing" message earlier.
|
|
111
|
+
const code = err.code;
|
|
112
|
+
if (code === "ENOENT" || code === "EACCES")
|
|
113
|
+
return;
|
|
114
|
+
throw err;
|
|
115
|
+
}
|
|
116
|
+
for (const entry of entries) {
|
|
117
|
+
const absPath = path.join(dir, entry.name);
|
|
118
|
+
const relPath = path.relative(hqRoot, absPath);
|
|
119
|
+
if (entry.isSymbolicLink()) {
|
|
120
|
+
// Record the link as a file-like entry. Don't descend — narrow is
|
|
121
|
+
// about pruning files that the LOCAL tree has materialized here; a
|
|
122
|
+
// symlink to a knowledge repo is one such "file" from the
|
|
123
|
+
// walk's POV.
|
|
124
|
+
let bytes = 0;
|
|
125
|
+
try {
|
|
126
|
+
bytes = fs.lstatSync(absPath).size;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
// pass — dangling link still gets emitted with size 0
|
|
130
|
+
}
|
|
131
|
+
emit({ relPath, absPath, bytes });
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (entry.isDirectory()) {
|
|
135
|
+
walkLocal(absPath, hqRoot, emit);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (entry.isFile()) {
|
|
139
|
+
let bytes = 0;
|
|
140
|
+
try {
|
|
141
|
+
bytes = fs.lstatSync(absPath).size;
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
// pass
|
|
145
|
+
}
|
|
146
|
+
emit({ relPath, absPath, bytes });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Mirror of `scope-shrink.ts::classifyOrphan`:
|
|
152
|
+
*
|
|
153
|
+
* clean = (entry exists AND hash(local) === entry.hash AND mtime <= syncedAt + 1s)
|
|
154
|
+
* OR (local file missing)
|
|
155
|
+
*
|
|
156
|
+
* Differences from the implicit path:
|
|
157
|
+
* - If there is NO journal entry at all, classify as **dirty**
|
|
158
|
+
* (`not-in-journal`). The implicit path never sees those files (it
|
|
159
|
+
* iterates the journal); here we walk the local tree so an
|
|
160
|
+
* un-journaled file is a real risk — it could be an unsynced local
|
|
161
|
+
* draft.
|
|
162
|
+
* - We don't honor `direction: "up"` differently. For an explicit
|
|
163
|
+
* narrow migration the user is asking "wipe everything outside the
|
|
164
|
+
* shared scope" — push-only authorship deserves the same dirty-file
|
|
165
|
+
* protection as pulled content (otherwise `--apply` could nuke local
|
|
166
|
+
* drafts).
|
|
167
|
+
*/
|
|
168
|
+
function classifyAgainstJournal(file, journal) {
|
|
169
|
+
const entry = journal.files[file.relPath];
|
|
170
|
+
if (!entry) {
|
|
171
|
+
return { clean: false, reason: "not-in-journal" };
|
|
172
|
+
}
|
|
173
|
+
// Tombstoned entries — the file shouldn't be here at all; treat as
|
|
174
|
+
// not-in-journal for safety so `--apply` doesn't silently delete it.
|
|
175
|
+
if (entry.removedAt) {
|
|
176
|
+
return { clean: false, reason: "not-in-journal" };
|
|
177
|
+
}
|
|
178
|
+
let stat;
|
|
179
|
+
try {
|
|
180
|
+
stat = fs.lstatSync(file.absPath);
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
const code = err.code;
|
|
184
|
+
if (code === "ENOENT") {
|
|
185
|
+
// Already gone — clean (the apply path will skip ENOENT on unlink).
|
|
186
|
+
return { clean: true, reason: "stat-error" };
|
|
187
|
+
}
|
|
188
|
+
return { clean: false, reason: "stat-error" };
|
|
189
|
+
}
|
|
190
|
+
// mtime guard with 1s grace for filesystem clock jitter, matching
|
|
191
|
+
// hq-cloud `scope-shrink.ts`.
|
|
192
|
+
const syncedAtMs = Date.parse(entry.syncedAt);
|
|
193
|
+
if (!Number.isNaN(syncedAtMs) && stat.mtimeMs > syncedAtMs + 1000) {
|
|
194
|
+
return { clean: false, reason: "modified-after-sync" };
|
|
195
|
+
}
|
|
196
|
+
// Hash check — final word. Symlinks: hashFile reads contents which is
|
|
197
|
+
// wrong for symlinks (it would follow the target), so on a symlink we
|
|
198
|
+
// skip the hash check and trust mtime alone. This matches the
|
|
199
|
+
// engine's behavior in the symlink-orphan case.
|
|
200
|
+
if (stat.isSymbolicLink()) {
|
|
201
|
+
return { clean: true, reason: "stat-error" };
|
|
202
|
+
}
|
|
203
|
+
let actualHash;
|
|
204
|
+
try {
|
|
205
|
+
actualHash = hashFile(file.absPath);
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
return { clean: false, reason: "stat-error" };
|
|
209
|
+
}
|
|
210
|
+
if (actualHash !== entry.hash) {
|
|
211
|
+
return { clean: false, reason: "hash-mismatch" };
|
|
212
|
+
}
|
|
213
|
+
return { clean: true, reason: "stat-error" };
|
|
214
|
+
}
|
|
215
|
+
// ── Formatting helper (separable for tests) ────────────────────────────────
|
|
216
|
+
/**
|
|
217
|
+
* Render the dry-run summary as plain text (no chalk — keep it pure). The
|
|
218
|
+
* CLI wrapper can colorize lines afterwards if desired.
|
|
219
|
+
*/
|
|
220
|
+
export function formatNarrowPlanSummary(plan) {
|
|
221
|
+
const lines = [];
|
|
222
|
+
lines.push("Narrow plan summary:");
|
|
223
|
+
lines.push(` files staying: ${plan.totalStayingCount}`);
|
|
224
|
+
lines.push(` clean orphans: ${plan.totalCleanCount} (${formatBytes(plan.totalCleanBytes)})`);
|
|
225
|
+
lines.push(` dirty orphans: ${plan.totalDirtyCount} (${formatBytes(plan.totalDirtyBytes)})`);
|
|
226
|
+
return lines.join("\n");
|
|
227
|
+
}
|
|
228
|
+
/** Human-readable byte counts with a fixed unit ladder. */
|
|
229
|
+
export function formatBytes(n) {
|
|
230
|
+
if (!Number.isFinite(n) || n < 0)
|
|
231
|
+
return "0 B";
|
|
232
|
+
if (n < 1024)
|
|
233
|
+
return `${n} B`;
|
|
234
|
+
const units = ["KB", "MB", "GB", "TB"];
|
|
235
|
+
let v = n / 1024;
|
|
236
|
+
let i = 0;
|
|
237
|
+
while (v >= 1024 && i < units.length - 1) {
|
|
238
|
+
v /= 1024;
|
|
239
|
+
i++;
|
|
240
|
+
}
|
|
241
|
+
return `${v.toFixed(2)} ${units[i]}`;
|
|
242
|
+
}
|
|
243
|
+
//# sourceMappingURL=local-tree-diff.js.map
|
|
244
|
+
//# debugId=77563dec-63f2-52df-9138-c88bfd1600b9
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `narrow-hint-banner` (US-011) — one-time-per-session hint nudging
|
|
3
|
+
* existing all-mode owners to switch to shared-mode sync.
|
|
4
|
+
*
|
|
5
|
+
* Emitted from `hq sync pull --all` and `hq sync now` after the per-target
|
|
6
|
+
* fanout resolves each membership's sync config. Suppressed when:
|
|
7
|
+
*
|
|
8
|
+
* - the membership is NOT on `syncMode: 'all'` (shared / custom users
|
|
9
|
+
* have already opted in to narrowing, so there is nothing to nudge),
|
|
10
|
+
* - the env var `HQ_SYNC_NARROW_HINT=off` is set,
|
|
11
|
+
* - the per-hqRoot CLI config (`<hqRoot>/.hq/config.json`) has
|
|
12
|
+
* `syncNarrowHint: 'off'`,
|
|
13
|
+
* - or the same `{companyUid, level}` pair has already been shown this
|
|
14
|
+
* process (module-singleton dedupe; the runner imports the same module
|
|
15
|
+
* once per `hq` invocation so a single invocation prints at most one
|
|
16
|
+
* banner per company per level).
|
|
17
|
+
*
|
|
18
|
+
* Three escalating levels — `'hint' | 'warning' | 'strict'`. Today's
|
|
19
|
+
* default level is `'hint'`; this release ships the plumbing so a future
|
|
20
|
+
* hq-core-staging release can flip the default to `'warning'` and then
|
|
21
|
+
* `'strict'` without re-touching the call sites.
|
|
22
|
+
*
|
|
23
|
+
* - hint → dim suggestion to stderr, never blocks.
|
|
24
|
+
* - warning → yellow note to stderr, never blocks.
|
|
25
|
+
* - strict → red error to stderr; callers are expected to refuse to
|
|
26
|
+
* proceed unless the operator passes `--mode-all`. The
|
|
27
|
+
* helper itself never throws or exits — the decision lives at
|
|
28
|
+
* the call site so tests don't have to stub `process.exit`.
|
|
29
|
+
*
|
|
30
|
+
* The level is selected by the caller (typically from the
|
|
31
|
+
* `HQ_SYNC_NARROW_HINT_LEVEL` env var). See `resolveBannerLevel` for the
|
|
32
|
+
* default-and-override ladder.
|
|
33
|
+
*
|
|
34
|
+
* TODO(hq-core-staging release N+2): bump default level to 'warning'.
|
|
35
|
+
* TODO(hq-core-staging release N+3): bump default level to 'strict' and
|
|
36
|
+
* wire `--mode-all` as the only opt-out.
|
|
37
|
+
*/
|
|
38
|
+
export type BannerLevel = "hint" | "warning" | "strict";
|
|
39
|
+
export interface BannerInput {
|
|
40
|
+
/** Company UID — used to dedupe per-process so each company emits once. */
|
|
41
|
+
companyUid: string;
|
|
42
|
+
/** Effective sync mode resolved from `getMembershipSyncConfig`. */
|
|
43
|
+
syncMode: "shared" | "all" | "custom";
|
|
44
|
+
/** Escalation level — see file header. */
|
|
45
|
+
level: BannerLevel;
|
|
46
|
+
}
|
|
47
|
+
export interface ShouldShowBannerOpts {
|
|
48
|
+
/**
|
|
49
|
+
* Optional hqRoot — when provided, the helper checks
|
|
50
|
+
* `<hqRoot>/.hq/config.json` for a `syncNarrowHint: 'off'` key in
|
|
51
|
+
* addition to the env override. Falsy paths skip the file check.
|
|
52
|
+
*/
|
|
53
|
+
hqRoot?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Test seam: override `process.env` lookup. Defaults to
|
|
56
|
+
* `process.env.HQ_SYNC_NARROW_HINT`.
|
|
57
|
+
*/
|
|
58
|
+
envValue?: string | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Test seam: override `fs.readFileSync`. Defaults to `node:fs`.
|
|
61
|
+
*/
|
|
62
|
+
readFile?: (p: string) => string;
|
|
63
|
+
/** Test seam: override `fs.existsSync`. */
|
|
64
|
+
existsFile?: (p: string) => boolean;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Decides whether a banner should be printed AT ALL — independent of
|
|
68
|
+
* dedupe and `syncMode` gating. Exposed for tests and for the strict-mode
|
|
69
|
+
* call site (which needs to know whether to refuse-to-proceed even if the
|
|
70
|
+
* banner itself is suppressed).
|
|
71
|
+
*/
|
|
72
|
+
export declare function shouldShowBanner(opts?: ShouldShowBannerOpts): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Resolve the banner level from environment overrides. Defaults to
|
|
75
|
+
* `'hint'`. Recognized values for `HQ_SYNC_NARROW_HINT_LEVEL`:
|
|
76
|
+
* `hint`, `warning`, `strict` (case-insensitive). Unknown values fall
|
|
77
|
+
* back to the default rather than throwing — the env var is operator-
|
|
78
|
+
* facing and a typo shouldn't break a sync.
|
|
79
|
+
*/
|
|
80
|
+
export declare function resolveBannerLevel(envValue?: string | undefined): BannerLevel;
|
|
81
|
+
/**
|
|
82
|
+
* Returns `true` when the strict-mode rollout has been opted into AND
|
|
83
|
+
* the membership in question is still on `'all'`. Call sites should
|
|
84
|
+
* refuse to proceed (exit non-zero) when this returns true and the
|
|
85
|
+
* operator hasn't passed `--mode-all`.
|
|
86
|
+
*/
|
|
87
|
+
export declare function isStrictRefusal(syncMode: BannerInput["syncMode"], level: BannerLevel): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Emit a one-time-per-{company,level} banner. Writes to `stderr` by
|
|
90
|
+
* default; tests inject a sink. Idempotent — repeated calls with the
|
|
91
|
+
* same `{companyUid, level}` are no-ops for the lifetime of the
|
|
92
|
+
* process.
|
|
93
|
+
*/
|
|
94
|
+
export declare function emitNarrowHint(input: BannerInput, opts?: {
|
|
95
|
+
hqRoot?: string;
|
|
96
|
+
write?: (s: string) => void;
|
|
97
|
+
/** Test seam — overrides `shouldShowBanner` env/config lookups. */
|
|
98
|
+
showOverride?: boolean;
|
|
99
|
+
}): void;
|
|
100
|
+
/** Test-only helper — clears the per-process dedupe set. */
|
|
101
|
+
export declare function _resetShownForTests(): void;
|
|
102
|
+
//# sourceMappingURL=narrow-hint-banner.d.ts.map
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `narrow-hint-banner` (US-011) — one-time-per-session hint nudging
|
|
3
|
+
* existing all-mode owners to switch to shared-mode sync.
|
|
4
|
+
*
|
|
5
|
+
* Emitted from `hq sync pull --all` and `hq sync now` after the per-target
|
|
6
|
+
* fanout resolves each membership's sync config. Suppressed when:
|
|
7
|
+
*
|
|
8
|
+
* - the membership is NOT on `syncMode: 'all'` (shared / custom users
|
|
9
|
+
* have already opted in to narrowing, so there is nothing to nudge),
|
|
10
|
+
* - the env var `HQ_SYNC_NARROW_HINT=off` is set,
|
|
11
|
+
* - the per-hqRoot CLI config (`<hqRoot>/.hq/config.json`) has
|
|
12
|
+
* `syncNarrowHint: 'off'`,
|
|
13
|
+
* - or the same `{companyUid, level}` pair has already been shown this
|
|
14
|
+
* process (module-singleton dedupe; the runner imports the same module
|
|
15
|
+
* once per `hq` invocation so a single invocation prints at most one
|
|
16
|
+
* banner per company per level).
|
|
17
|
+
*
|
|
18
|
+
* Three escalating levels — `'hint' | 'warning' | 'strict'`. Today's
|
|
19
|
+
* default level is `'hint'`; this release ships the plumbing so a future
|
|
20
|
+
* hq-core-staging release can flip the default to `'warning'` and then
|
|
21
|
+
* `'strict'` without re-touching the call sites.
|
|
22
|
+
*
|
|
23
|
+
* - hint → dim suggestion to stderr, never blocks.
|
|
24
|
+
* - warning → yellow note to stderr, never blocks.
|
|
25
|
+
* - strict → red error to stderr; callers are expected to refuse to
|
|
26
|
+
* proceed unless the operator passes `--mode-all`. The
|
|
27
|
+
* helper itself never throws or exits — the decision lives at
|
|
28
|
+
* the call site so tests don't have to stub `process.exit`.
|
|
29
|
+
*
|
|
30
|
+
* The level is selected by the caller (typically from the
|
|
31
|
+
* `HQ_SYNC_NARROW_HINT_LEVEL` env var). See `resolveBannerLevel` for the
|
|
32
|
+
* default-and-override ladder.
|
|
33
|
+
*
|
|
34
|
+
* TODO(hq-core-staging release N+2): bump default level to 'warning'.
|
|
35
|
+
* TODO(hq-core-staging release N+3): bump default level to 'strict' and
|
|
36
|
+
* wire `--mode-all` as the only opt-out.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="b62d5114-da5b-5a77-bf3f-8362a2197665")}catch(e){}}();
|
|
40
|
+
import chalk from "chalk";
|
|
41
|
+
import * as fs from "node:fs";
|
|
42
|
+
import * as path from "node:path";
|
|
43
|
+
const SHOWN = new Set();
|
|
44
|
+
/**
|
|
45
|
+
* Decides whether a banner should be printed AT ALL — independent of
|
|
46
|
+
* dedupe and `syncMode` gating. Exposed for tests and for the strict-mode
|
|
47
|
+
* call site (which needs to know whether to refuse-to-proceed even if the
|
|
48
|
+
* banner itself is suppressed).
|
|
49
|
+
*/
|
|
50
|
+
export function shouldShowBanner(opts = {}) {
|
|
51
|
+
const envValue = opts.envValue !== undefined
|
|
52
|
+
? opts.envValue
|
|
53
|
+
: process.env.HQ_SYNC_NARROW_HINT;
|
|
54
|
+
if (typeof envValue === "string" && envValue.toLowerCase() === "off") {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
if (opts.hqRoot) {
|
|
58
|
+
const configPath = path.join(opts.hqRoot, ".hq", "config.json");
|
|
59
|
+
const exists = opts.existsFile ?? fs.existsSync;
|
|
60
|
+
const read = opts.readFile ?? ((p) => fs.readFileSync(p, "utf-8"));
|
|
61
|
+
if (exists(configPath)) {
|
|
62
|
+
try {
|
|
63
|
+
const cfg = JSON.parse(read(configPath));
|
|
64
|
+
if (typeof cfg.syncNarrowHint === "string" &&
|
|
65
|
+
cfg.syncNarrowHint.toLowerCase() === "off") {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// Malformed config is best-effort — fall through and assume "on".
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Resolve the banner level from environment overrides. Defaults to
|
|
78
|
+
* `'hint'`. Recognized values for `HQ_SYNC_NARROW_HINT_LEVEL`:
|
|
79
|
+
* `hint`, `warning`, `strict` (case-insensitive). Unknown values fall
|
|
80
|
+
* back to the default rather than throwing — the env var is operator-
|
|
81
|
+
* facing and a typo shouldn't break a sync.
|
|
82
|
+
*/
|
|
83
|
+
export function resolveBannerLevel(envValue = process.env.HQ_SYNC_NARROW_HINT_LEVEL) {
|
|
84
|
+
if (typeof envValue !== "string")
|
|
85
|
+
return "hint";
|
|
86
|
+
const v = envValue.toLowerCase();
|
|
87
|
+
if (v === "warning" || v === "strict" || v === "hint")
|
|
88
|
+
return v;
|
|
89
|
+
return "hint";
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Returns `true` when the strict-mode rollout has been opted into AND
|
|
93
|
+
* the membership in question is still on `'all'`. Call sites should
|
|
94
|
+
* refuse to proceed (exit non-zero) when this returns true and the
|
|
95
|
+
* operator hasn't passed `--mode-all`.
|
|
96
|
+
*/
|
|
97
|
+
export function isStrictRefusal(syncMode, level) {
|
|
98
|
+
return level === "strict" && syncMode === "all";
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Emit a one-time-per-{company,level} banner. Writes to `stderr` by
|
|
102
|
+
* default; tests inject a sink. Idempotent — repeated calls with the
|
|
103
|
+
* same `{companyUid, level}` are no-ops for the lifetime of the
|
|
104
|
+
* process.
|
|
105
|
+
*/
|
|
106
|
+
export function emitNarrowHint(input, opts = {}) {
|
|
107
|
+
// Only nudge 'all'-mode memberships — shared/custom callers already
|
|
108
|
+
// opted in to narrowing, so the hint would just be noise.
|
|
109
|
+
if (input.syncMode !== "all")
|
|
110
|
+
return;
|
|
111
|
+
const show = opts.showOverride !== undefined
|
|
112
|
+
? opts.showOverride
|
|
113
|
+
: shouldShowBanner({ ...(opts.hqRoot ? { hqRoot: opts.hqRoot } : {}) });
|
|
114
|
+
if (!show)
|
|
115
|
+
return;
|
|
116
|
+
const key = `${input.companyUid}:${input.level}`;
|
|
117
|
+
if (SHOWN.has(key))
|
|
118
|
+
return;
|
|
119
|
+
SHOWN.add(key);
|
|
120
|
+
const write = opts.write ?? ((s) => process.stderr.write(s + "\n"));
|
|
121
|
+
if (input.level === "hint") {
|
|
122
|
+
write(chalk.dim("Tip: switch to shared-mode sync — fewer files, same visibility. " +
|
|
123
|
+
"Run `hq sync narrow --dry-run` to preview."));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (input.level === "warning") {
|
|
127
|
+
write(chalk.yellow("Warning: shared-mode sync is the new default; this membership " +
|
|
128
|
+
"still pulls everything. Run `hq sync narrow --dry-run` to " +
|
|
129
|
+
"preview the migration before the next release flips the " +
|
|
130
|
+
"default to strict."));
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
// strict — caller is responsible for refusing to proceed unless
|
|
134
|
+
// --mode-all was passed. We only emit the message here.
|
|
135
|
+
write(chalk.red("Error: shared-mode sync is now strict; pass --mode-all to keep " +
|
|
136
|
+
"all-mode behavior for this run, or run `hq sync narrow --apply` " +
|
|
137
|
+
"to migrate this membership."));
|
|
138
|
+
}
|
|
139
|
+
/** Test-only helper — clears the per-process dedupe set. */
|
|
140
|
+
export function _resetShownForTests() {
|
|
141
|
+
SHOWN.clear();
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=narrow-hint-banner.js.map
|
|
144
|
+
//# debugId=b62d5114-da5b-5a77-bf3f-8362a2197665
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@indigoai-us/hq-cli",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.18.1",
|
|
4
4
|
"description": "HQ by Indigo management CLI — modules and cloud sync",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"clean": "rm -rf dist"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@indigoai-us/hq-cloud": "^5.
|
|
18
|
+
"@indigoai-us/hq-cloud": "^5.23.0",
|
|
19
19
|
"@indigoai-us/hq-onboarding": "^0.1.0",
|
|
20
20
|
"@sentry/node": "^10.49.0",
|
|
21
21
|
"chalk": "^5.3.0",
|