@indigoai-us/hq-cli 5.124.0 → 5.125.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/dist/command-catalog.generated.d.ts +7 -1
- package/dist/command-catalog.generated.js +9 -1
- package/dist/commands/login.js +49 -6
- package/dist/commands/mesh.js +96 -19
- package/dist/lib/doctor/checks/work-context.js +4 -2
- package/dist/lib/mesh/live/backfill-held.d.ts +2 -0
- package/dist/lib/mesh/live/backfill-held.js +8 -4
- package/dist/lib/mesh/live/daemon/board-refresh.d.ts +3 -0
- package/dist/lib/mesh/live/daemon/board-refresh.js +6 -0
- package/dist/lib/mesh/live/daemon/doctor.d.ts +3 -0
- package/dist/lib/mesh/live/daemon/doctor.js +21 -1
- package/dist/lib/mesh/live/daemon/run.d.ts +11 -0
- package/dist/lib/mesh/live/daemon/run.js +129 -10
- package/dist/lib/mesh/live/daemon/state.d.ts +2 -0
- package/dist/lib/mesh/live/flush.d.ts +3 -0
- package/dist/lib/mesh/live/flush.js +11 -0
- package/dist/lib/work-context/company.d.ts +8 -0
- package/dist/lib/work-context/company.js +62 -18
- package/dist/lib/work-context/config.d.ts +35 -0
- package/dist/lib/work-context/config.js +307 -62
- package/dist/lib/work-context/contract.d.ts +2 -0
- package/dist/lib/work-context/default-company.d.ts +43 -0
- package/dist/lib/work-context/default-company.js +83 -0
- package/dist/lib/work-context/outbox.d.ts +5 -1
- package/dist/lib/work-context/outbox.js +5 -0
- package/dist/lib/work-context/reconcile.d.ts +4 -0
- package/dist/lib/work-context/reconcile.js +59 -3
- package/dist/lib/work-context/state.d.ts +2 -0
- package/dist/lib/work-context/state.js +14 -0
- package/package.json +1 -1
|
@@ -9,12 +9,115 @@ import { DefaultCompanyUnavailableError, UnsafeConfigPathError, WorkContextError
|
|
|
9
9
|
import { workContextConfigPath, workContextRoot } from "./paths.js";
|
|
10
10
|
import { WORK_CONTEXT_CONTRACT_VERSION } from "./contract.js";
|
|
11
11
|
export const DEVICE_CONFIG_SCHEMA_VERSION = 1;
|
|
12
|
+
const CONFIG_LOCK_TIMEOUT_MS = 5_000;
|
|
13
|
+
const CONFIG_LOCK_RETRY_MS = 10;
|
|
14
|
+
export const CONFIG_LOCK_STALE_MS = 30_000;
|
|
15
|
+
function waitForConfigLock(ms) {
|
|
16
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
17
|
+
}
|
|
18
|
+
function readConfigLockRecord(lockPath) {
|
|
19
|
+
try {
|
|
20
|
+
const stat = fs.statSync(lockPath);
|
|
21
|
+
const raw = JSON.parse(fs.readFileSync(lockPath, "utf8"));
|
|
22
|
+
const pid = typeof raw.pid === "number" && Number.isInteger(raw.pid) ? raw.pid : 0;
|
|
23
|
+
const createdAt = typeof raw.createdAt === "string" ? raw.createdAt : "";
|
|
24
|
+
const token = typeof raw.token === "string" && raw.token ? raw.token : undefined;
|
|
25
|
+
const parsedAt = Date.parse(createdAt);
|
|
26
|
+
return {
|
|
27
|
+
record: pid > 0 && createdAt ? { pid, createdAt, token } : null,
|
|
28
|
+
ageMs: Date.now() - (Number.isFinite(parsedAt) ? parsedAt : stat.mtimeMs),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return { record: null, ageMs: Number.POSITIVE_INFINITY };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function isProcessAlive(pid) {
|
|
36
|
+
try {
|
|
37
|
+
process.kill(pid, 0);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function reclaimStaleConfigLock(lockPath) {
|
|
45
|
+
const claim = `${lockPath}.reclaim.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}`;
|
|
46
|
+
try {
|
|
47
|
+
fs.renameSync(lockPath, claim);
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
if (err.code === "ENOENT")
|
|
51
|
+
return false;
|
|
52
|
+
throw err;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
try {
|
|
59
|
+
fs.unlinkSync(claim);
|
|
60
|
+
}
|
|
61
|
+
catch { /* private claim cannot block writers */ }
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Acquire an interprocess config lock; exported for direct lock regression tests. */
|
|
65
|
+
export function acquireDeviceConfigLock(root, options = {}) {
|
|
66
|
+
ensureOwnerDir(root);
|
|
67
|
+
const configPath = resolveRealTarget(workContextConfigPath(root));
|
|
68
|
+
assertSafeConfigPath(configPath);
|
|
69
|
+
const lockPath = `${configPath}.lock`;
|
|
70
|
+
const deadline = Date.now() + (options.timeoutMs ?? CONFIG_LOCK_TIMEOUT_MS);
|
|
71
|
+
const token = `${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}`;
|
|
72
|
+
let fd;
|
|
73
|
+
while (fd === undefined) {
|
|
74
|
+
try {
|
|
75
|
+
fd = fs.openSync(lockPath, "wx", 0o600);
|
|
76
|
+
fs.writeSync(fd, JSON.stringify({ pid: process.pid, createdAt: new Date().toISOString(), token }));
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
if (err.code !== "EEXIST")
|
|
80
|
+
throw err;
|
|
81
|
+
const { record, ageMs } = readConfigLockRecord(lockPath);
|
|
82
|
+
// Age alone is not authority to steal a lock: a live writer may simply
|
|
83
|
+
// be slow. Only a missing/malformed or dead owner can be reclaimed.
|
|
84
|
+
if (!record || !isProcessAlive(record.pid)) {
|
|
85
|
+
reclaimStaleConfigLock(lockPath);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (Date.now() >= deadline) {
|
|
89
|
+
throw new WorkContextError("ConfigLockTimeout", "Timed out waiting for the work-context config lock");
|
|
90
|
+
}
|
|
91
|
+
waitForConfigLock(CONFIG_LOCK_RETRY_MS);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return () => {
|
|
95
|
+
fs.closeSync(fd);
|
|
96
|
+
try {
|
|
97
|
+
const current = readConfigLockRecord(lockPath).record;
|
|
98
|
+
if (current?.token === token)
|
|
99
|
+
fs.unlinkSync(lockPath);
|
|
100
|
+
}
|
|
101
|
+
catch { /* stale reclaimer already retired or replaced this lock */ }
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function withDeviceConfigLock(root, fn) {
|
|
105
|
+
const release = acquireDeviceConfigLock(root);
|
|
106
|
+
try {
|
|
107
|
+
return fn();
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
release();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
12
113
|
function emptyConfig(now) {
|
|
13
114
|
return {
|
|
14
115
|
schemaVersion: DEVICE_CONFIG_SCHEMA_VERSION,
|
|
15
116
|
defaultCompany: null,
|
|
16
117
|
migrationCapability: null,
|
|
17
118
|
repoCompanyMap: null,
|
|
119
|
+
defaultCompanyMemberships: null,
|
|
120
|
+
repairHeldWithDefault: false,
|
|
18
121
|
updatedAt: now().toISOString(),
|
|
19
122
|
};
|
|
20
123
|
}
|
|
@@ -82,11 +185,22 @@ export function readDeviceConfig(deps) {
|
|
|
82
185
|
throw new WorkContextError("ConfigCorruptionError", `Unsupported config schemaVersion ${String(raw.schemaVersion)}`);
|
|
83
186
|
}
|
|
84
187
|
// Never treat a stray activeCompany field as the default.
|
|
188
|
+
// Older `clear` commands persisted `defaultCompany: null`. A config file
|
|
189
|
+
// is durable user history, so preserving a null as "unconfigured" would
|
|
190
|
+
// re-enable automatic selection after an upgrade. Treat every persisted
|
|
191
|
+
// legacy null as the explicit opt-out tombstone instead.
|
|
192
|
+
const legacyNullDefault = Object.prototype.hasOwnProperty.call(raw, "defaultCompany") &&
|
|
193
|
+
raw.defaultCompany === null &&
|
|
194
|
+
raw.defaultCompanyAutomaticEligible !== true;
|
|
85
195
|
const cleaned = {
|
|
86
196
|
schemaVersion: DEVICE_CONFIG_SCHEMA_VERSION,
|
|
87
|
-
defaultCompany:
|
|
197
|
+
defaultCompany: legacyNullDefault
|
|
198
|
+
? { slug: "", enabled: false, source: "disabled" }
|
|
199
|
+
: raw.defaultCompany ?? null,
|
|
88
200
|
migrationCapability: raw.migrationCapability ?? null,
|
|
89
201
|
repoCompanyMap: sanitizeRepoCompanyMap(raw.repoCompanyMap),
|
|
202
|
+
defaultCompanyMemberships: sanitizeMembershipSnapshot(raw.defaultCompanyMemberships),
|
|
203
|
+
repairHeldWithDefault: raw.repairHeldWithDefault === true,
|
|
90
204
|
updatedAt: typeof raw.updatedAt === "string" ? raw.updatedAt : new Date(0).toISOString(),
|
|
91
205
|
};
|
|
92
206
|
return cleaned;
|
|
@@ -101,7 +215,12 @@ function writeDeviceConfig(deps, config) {
|
|
|
101
215
|
ensureOwnerDir(deps.root);
|
|
102
216
|
const configPath = resolveRealTarget(workContextConfigPath(deps.root));
|
|
103
217
|
assertSafeConfigPath(configPath);
|
|
104
|
-
|
|
218
|
+
// New blank configuration is intentionally distinct from the legacy null
|
|
219
|
+
// that the previous `clear` command wrote. Preserve that distinction on
|
|
220
|
+
// disk so a membership-cache write cannot turn into a durable opt-out.
|
|
221
|
+
atomicWriteJson(configPath, config.defaultCompany === null
|
|
222
|
+
? { ...config, defaultCompanyAutomaticEligible: true }
|
|
223
|
+
: config);
|
|
105
224
|
}
|
|
106
225
|
export function getDefaultCompany(deps) {
|
|
107
226
|
const cfg = readDeviceConfig(deps);
|
|
@@ -110,6 +229,11 @@ export function getDefaultCompany(deps) {
|
|
|
110
229
|
return null;
|
|
111
230
|
return d;
|
|
112
231
|
}
|
|
232
|
+
/** A clear is a durable opt-out, not an invitation for automatic reactivation. */
|
|
233
|
+
export function isDefaultCompanyDisabled(deps) {
|
|
234
|
+
const configured = readDeviceConfig(deps).defaultCompany;
|
|
235
|
+
return configured?.source === "disabled";
|
|
236
|
+
}
|
|
113
237
|
export async function setDefaultCompany(slug, deps) {
|
|
114
238
|
const trimmed = slug.trim();
|
|
115
239
|
if (!trimmed || !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(trimmed)) {
|
|
@@ -135,35 +259,44 @@ export async function setDefaultCompany(slug, deps) {
|
|
|
135
259
|
resolvedUid = result.uid.trim() || undefined;
|
|
136
260
|
}
|
|
137
261
|
const now = deps.now ?? (() => new Date());
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
262
|
+
return withDeviceConfigLock(deps.root, () => {
|
|
263
|
+
const prior = readDeviceConfig(deps);
|
|
264
|
+
const defaultCompany = {
|
|
265
|
+
slug: trimmed,
|
|
266
|
+
enabled: true,
|
|
267
|
+
source: "chosen",
|
|
268
|
+
};
|
|
269
|
+
if (resolvedUid)
|
|
270
|
+
defaultCompany.uid = resolvedUid;
|
|
271
|
+
const next = {
|
|
272
|
+
schemaVersion: DEVICE_CONFIG_SCHEMA_VERSION,
|
|
273
|
+
defaultCompany,
|
|
274
|
+
migrationCapability: prior.migrationCapability ?? null,
|
|
275
|
+
repoCompanyMap: prior.repoCompanyMap ?? null,
|
|
276
|
+
defaultCompanyMemberships: prior.defaultCompanyMemberships ?? null,
|
|
277
|
+
repairHeldWithDefault: prior.repairHeldWithDefault === true,
|
|
278
|
+
updatedAt: now().toISOString(),
|
|
279
|
+
};
|
|
280
|
+
writeDeviceConfig(deps, next);
|
|
281
|
+
return next;
|
|
282
|
+
});
|
|
154
283
|
}
|
|
155
284
|
export function clearDefaultCompany(deps) {
|
|
156
285
|
const now = deps.now ?? (() => new Date());
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
286
|
+
return withDeviceConfigLock(deps.root, () => {
|
|
287
|
+
const prior = readDeviceConfig(deps);
|
|
288
|
+
const next = {
|
|
289
|
+
schemaVersion: DEVICE_CONFIG_SCHEMA_VERSION,
|
|
290
|
+
defaultCompany: { slug: "", enabled: false, source: "disabled" },
|
|
291
|
+
migrationCapability: prior.migrationCapability ?? null,
|
|
292
|
+
repoCompanyMap: prior.repoCompanyMap ?? null,
|
|
293
|
+
defaultCompanyMemberships: prior.defaultCompanyMemberships ?? null,
|
|
294
|
+
repairHeldWithDefault: prior.repairHeldWithDefault === true,
|
|
295
|
+
updatedAt: now().toISOString(),
|
|
296
|
+
};
|
|
297
|
+
writeDeviceConfig(deps, next);
|
|
298
|
+
return next;
|
|
299
|
+
});
|
|
167
300
|
}
|
|
168
301
|
/**
|
|
169
302
|
* Read the persisted company mapping for a repo identity key (gap 4).
|
|
@@ -193,43 +326,155 @@ export function setRepoCompany(key, mapping, deps) {
|
|
|
193
326
|
throw new WorkContextError("InvalidCompanySlug", `Invalid company slug: ${mapping.slug}`);
|
|
194
327
|
}
|
|
195
328
|
const now = deps.now ?? (() => new Date());
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
329
|
+
return withDeviceConfigLock(deps.root, () => {
|
|
330
|
+
const prior = readDeviceConfig({ root: deps.root });
|
|
331
|
+
const entry = { slug, updatedAt: now().toISOString() };
|
|
332
|
+
const uid = mapping.uid?.trim();
|
|
333
|
+
if (uid)
|
|
334
|
+
entry.uid = uid;
|
|
335
|
+
const nextMap = { ...(prior.repoCompanyMap ?? {}), [trimmed]: entry };
|
|
336
|
+
const next = {
|
|
337
|
+
schemaVersion: DEVICE_CONFIG_SCHEMA_VERSION,
|
|
338
|
+
defaultCompany: prior.defaultCompany ?? null,
|
|
339
|
+
migrationCapability: prior.migrationCapability ?? null,
|
|
340
|
+
repoCompanyMap: nextMap,
|
|
341
|
+
defaultCompanyMemberships: prior.defaultCompanyMemberships ?? null,
|
|
342
|
+
repairHeldWithDefault: prior.repairHeldWithDefault === true,
|
|
343
|
+
updatedAt: now().toISOString(),
|
|
344
|
+
};
|
|
345
|
+
writeDeviceConfig({ root: deps.root }, next);
|
|
346
|
+
return next;
|
|
347
|
+
});
|
|
211
348
|
}
|
|
212
349
|
/** Persist the last memberships-wide migration capability probe (US-017B). */
|
|
213
350
|
export function recordMigrationCapabilitySnapshot(snapshot, deps) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
351
|
+
return withDeviceConfigLock(deps.root, () => {
|
|
352
|
+
const prior = readDeviceConfig(deps);
|
|
353
|
+
const now = deps.now ?? (() => new Date());
|
|
354
|
+
const next = {
|
|
355
|
+
schemaVersion: DEVICE_CONFIG_SCHEMA_VERSION,
|
|
356
|
+
defaultCompany: prior.defaultCompany ?? null,
|
|
357
|
+
migrationCapability: {
|
|
358
|
+
unlocked: snapshot.unlocked,
|
|
359
|
+
offline: snapshot.offline,
|
|
360
|
+
checkedAt: snapshot.checkedAt,
|
|
361
|
+
companies: snapshot.companies.map((c) => ({
|
|
362
|
+
companyUid: c.companyUid,
|
|
363
|
+
migration: c.migration,
|
|
364
|
+
})),
|
|
365
|
+
},
|
|
366
|
+
repoCompanyMap: prior.repoCompanyMap ?? null,
|
|
367
|
+
defaultCompanyMemberships: prior.defaultCompanyMemberships ?? null,
|
|
368
|
+
repairHeldWithDefault: prior.repairHeldWithDefault === true,
|
|
369
|
+
updatedAt: now().toISOString(),
|
|
370
|
+
};
|
|
371
|
+
writeDeviceConfig({ root: deps.root }, next);
|
|
372
|
+
return next;
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
function sanitizeMembershipSnapshot(raw) {
|
|
376
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
377
|
+
return null;
|
|
378
|
+
const value = raw;
|
|
379
|
+
if (typeof value.checkedAt !== "string" || !Array.isArray(value.memberships))
|
|
380
|
+
return null;
|
|
381
|
+
const memberships = value.memberships.flatMap((entry) => {
|
|
382
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
383
|
+
return [];
|
|
384
|
+
const member = entry;
|
|
385
|
+
const slug = typeof member.slug === "string" ? member.slug.trim() : "";
|
|
386
|
+
const uid = typeof member.uid === "string" ? member.uid.trim() : "";
|
|
387
|
+
return slug && uid ? [{ slug, uid }] : [];
|
|
388
|
+
});
|
|
389
|
+
return { checkedAt: value.checkedAt, memberships };
|
|
390
|
+
}
|
|
391
|
+
export function recordDefaultCompanyMemberships(snapshot, deps) {
|
|
392
|
+
return withDeviceConfigLock(deps.root, () => {
|
|
393
|
+
const prior = readDeviceConfig(deps);
|
|
394
|
+
const now = deps.now ?? (() => new Date());
|
|
395
|
+
const next = {
|
|
396
|
+
schemaVersion: DEVICE_CONFIG_SCHEMA_VERSION,
|
|
397
|
+
defaultCompany: prior.defaultCompany ?? null,
|
|
398
|
+
migrationCapability: prior.migrationCapability ?? null,
|
|
399
|
+
repoCompanyMap: prior.repoCompanyMap ?? null,
|
|
400
|
+
defaultCompanyMemberships: {
|
|
401
|
+
checkedAt: snapshot.checkedAt,
|
|
402
|
+
memberships: snapshot.memberships.map((membership) => ({ ...membership })),
|
|
403
|
+
},
|
|
404
|
+
repairHeldWithDefault: prior.repairHeldWithDefault === true,
|
|
405
|
+
updatedAt: now().toISOString(),
|
|
406
|
+
};
|
|
407
|
+
writeDeviceConfig({ root: deps.root }, next);
|
|
408
|
+
return next;
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
/** Drop cached membership evidence immediately after an authoritative denial. */
|
|
412
|
+
export function invalidateDefaultCompanyMemberships(deps) {
|
|
413
|
+
return withDeviceConfigLock(deps.root, () => {
|
|
414
|
+
const prior = readDeviceConfig(deps);
|
|
415
|
+
const now = deps.now ?? (() => new Date());
|
|
416
|
+
const next = {
|
|
417
|
+
schemaVersion: DEVICE_CONFIG_SCHEMA_VERSION,
|
|
418
|
+
defaultCompany: prior.defaultCompany ?? null,
|
|
419
|
+
migrationCapability: prior.migrationCapability ?? null,
|
|
420
|
+
repoCompanyMap: prior.repoCompanyMap ?? null,
|
|
421
|
+
defaultCompanyMemberships: null,
|
|
422
|
+
repairHeldWithDefault: prior.repairHeldWithDefault === true,
|
|
423
|
+
updatedAt: now().toISOString(),
|
|
424
|
+
};
|
|
425
|
+
writeDeviceConfig({ root: deps.root }, next);
|
|
426
|
+
return next;
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
/** Persist a default only after the complete active-membership list verified it. */
|
|
430
|
+
export function setAutoDefaultCompany(membership, deps) {
|
|
431
|
+
return withDeviceConfigLock(deps.root, () => {
|
|
432
|
+
const prior = readDeviceConfig(deps);
|
|
433
|
+
// Activation may have fetched memberships while another process cleared or
|
|
434
|
+
// changed this preference. Do not let that stale discovery overwrite the
|
|
435
|
+
// newer durable choice.
|
|
436
|
+
if (prior.defaultCompany?.source === "disabled" ||
|
|
437
|
+
prior.defaultCompany?.enabled ||
|
|
438
|
+
(deps.expectedUpdatedAt !== undefined &&
|
|
439
|
+
prior.updatedAt !== deps.expectedUpdatedAt)) {
|
|
440
|
+
return prior;
|
|
441
|
+
}
|
|
442
|
+
deps.afterCompareAndSwap?.();
|
|
443
|
+
const now = deps.now ?? (() => new Date());
|
|
444
|
+
const next = {
|
|
445
|
+
schemaVersion: DEVICE_CONFIG_SCHEMA_VERSION,
|
|
446
|
+
defaultCompany: {
|
|
447
|
+
slug: membership.slug,
|
|
448
|
+
uid: membership.uid,
|
|
449
|
+
enabled: true,
|
|
450
|
+
source: "auto_single",
|
|
451
|
+
},
|
|
452
|
+
migrationCapability: prior.migrationCapability ?? null,
|
|
453
|
+
repoCompanyMap: prior.repoCompanyMap ?? null,
|
|
454
|
+
defaultCompanyMemberships: prior.defaultCompanyMemberships ?? null,
|
|
455
|
+
repairHeldWithDefault: prior.repairHeldWithDefault === true,
|
|
456
|
+
updatedAt: now().toISOString(),
|
|
457
|
+
};
|
|
458
|
+
writeDeviceConfig({ root: deps.root }, next);
|
|
459
|
+
return next;
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
export function setRepairHeldWithDefault(enabled, deps) {
|
|
463
|
+
return withDeviceConfigLock(deps.root, () => {
|
|
464
|
+
const prior = readDeviceConfig(deps);
|
|
465
|
+
const now = deps.now ?? (() => new Date());
|
|
466
|
+
const next = {
|
|
467
|
+
schemaVersion: DEVICE_CONFIG_SCHEMA_VERSION,
|
|
468
|
+
defaultCompany: prior.defaultCompany ?? null,
|
|
469
|
+
migrationCapability: prior.migrationCapability ?? null,
|
|
470
|
+
repoCompanyMap: prior.repoCompanyMap ?? null,
|
|
471
|
+
defaultCompanyMemberships: prior.defaultCompanyMemberships ?? null,
|
|
472
|
+
repairHeldWithDefault: enabled,
|
|
473
|
+
updatedAt: now().toISOString(),
|
|
474
|
+
};
|
|
475
|
+
writeDeviceConfig({ root: deps.root }, next);
|
|
476
|
+
return next;
|
|
477
|
+
});
|
|
233
478
|
}
|
|
234
479
|
/** Convenience: build deps from HOME / injectable root. */
|
|
235
480
|
export function deviceConfigDepsFromHome(home, extras = {}) {
|
|
@@ -54,6 +54,8 @@ export interface ContextResult {
|
|
|
54
54
|
lifecycle: LifecycleState;
|
|
55
55
|
sessionId: string;
|
|
56
56
|
companyUid?: string;
|
|
57
|
+
/** Resolved company slug; paired with companyUid for local hook consumers. */
|
|
58
|
+
companySlug?: string;
|
|
57
59
|
projectId?: string;
|
|
58
60
|
taskId?: string;
|
|
59
61
|
bindingEpisodeId?: string;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Device-default activation. A device may infer a default only from the caller's
|
|
3
|
+
* complete, verified active-membership list; it never infers a tenant from cwd.
|
|
4
|
+
*/
|
|
5
|
+
import { type DefaultCompanyMembership } from "./config.js";
|
|
6
|
+
export declare const DEFAULT_COMPANY_MEMBERSHIP_TTL_MS: number;
|
|
7
|
+
export interface ActiveMembership {
|
|
8
|
+
companySlug: string;
|
|
9
|
+
companyUid: string;
|
|
10
|
+
}
|
|
11
|
+
export type DefaultCompanyActivation = {
|
|
12
|
+
status: "existing";
|
|
13
|
+
memberships: DefaultCompanyMembership[];
|
|
14
|
+
} | {
|
|
15
|
+
status: "auto_single";
|
|
16
|
+
memberships: DefaultCompanyMembership[];
|
|
17
|
+
} | {
|
|
18
|
+
status: "needs_choice";
|
|
19
|
+
memberships: DefaultCompanyMembership[];
|
|
20
|
+
} | {
|
|
21
|
+
status: "disabled";
|
|
22
|
+
memberships: DefaultCompanyMembership[];
|
|
23
|
+
} | {
|
|
24
|
+
status: "identity_file";
|
|
25
|
+
memberships: [];
|
|
26
|
+
} | {
|
|
27
|
+
status: "unavailable";
|
|
28
|
+
memberships: [];
|
|
29
|
+
};
|
|
30
|
+
export interface EnsureDefaultCompanyDeps {
|
|
31
|
+
root: string;
|
|
32
|
+
env?: NodeJS.ProcessEnv;
|
|
33
|
+
listMemberships: () => Promise<ActiveMembership[]>;
|
|
34
|
+
now?: () => Date;
|
|
35
|
+
log?: (message: string) => void;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Populate a blank device from one active membership, or expose the choices for
|
|
39
|
+
* an interactive caller. Identity-file boxes and explicitly disabled devices do
|
|
40
|
+
* not query memberships because a user/default must never override either.
|
|
41
|
+
*/
|
|
42
|
+
export declare function ensureDefaultCompany(deps: EnsureDefaultCompanyDeps): Promise<DefaultCompanyActivation>;
|
|
43
|
+
//# sourceMappingURL=default-company.d.ts.map
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Device-default activation. A device may infer a default only from the caller's
|
|
3
|
+
* complete, verified active-membership list; it never infers a tenant from cwd.
|
|
4
|
+
*/
|
|
5
|
+
import { hasAgentIdentityFile } from "./company.js";
|
|
6
|
+
import { isDefaultCompanyDisabled, readDeviceConfig, recordDefaultCompanyMemberships, setAutoDefaultCompany, } from "./config.js";
|
|
7
|
+
export const DEFAULT_COMPANY_MEMBERSHIP_TTL_MS = 24 * 60 * 60 * 1000;
|
|
8
|
+
function normalizeMemberships(rows) {
|
|
9
|
+
const seen = new Set();
|
|
10
|
+
return rows.flatMap((row) => {
|
|
11
|
+
const slug = row.companySlug?.trim();
|
|
12
|
+
const uid = row.companyUid?.trim();
|
|
13
|
+
if (!slug || !uid || seen.has(uid))
|
|
14
|
+
return [];
|
|
15
|
+
seen.add(uid);
|
|
16
|
+
return [{ slug, uid }];
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
function cachedMemberships(root, now) {
|
|
20
|
+
const snapshot = readDeviceConfig({ root }).defaultCompanyMemberships;
|
|
21
|
+
if (!snapshot)
|
|
22
|
+
return null;
|
|
23
|
+
const checkedAt = Date.parse(snapshot.checkedAt);
|
|
24
|
+
if (!Number.isFinite(checkedAt) || now.getTime() - checkedAt > DEFAULT_COMPANY_MEMBERSHIP_TTL_MS) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
return snapshot.memberships;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Populate a blank device from one active membership, or expose the choices for
|
|
31
|
+
* an interactive caller. Identity-file boxes and explicitly disabled devices do
|
|
32
|
+
* not query memberships because a user/default must never override either.
|
|
33
|
+
*/
|
|
34
|
+
export async function ensureDefaultCompany(deps) {
|
|
35
|
+
const env = deps.env ?? process.env;
|
|
36
|
+
if (hasAgentIdentityFile(env))
|
|
37
|
+
return { status: "identity_file", memberships: [] };
|
|
38
|
+
const now = (deps.now ?? (() => new Date()))();
|
|
39
|
+
const configBeforeDiscovery = readDeviceConfig({ root: deps.root });
|
|
40
|
+
const cached = cachedMemberships(deps.root, now) ?? [];
|
|
41
|
+
if (isDefaultCompanyDisabled({ root: deps.root })) {
|
|
42
|
+
return { status: "disabled", memberships: cached };
|
|
43
|
+
}
|
|
44
|
+
let memberships = cachedMemberships(deps.root, now);
|
|
45
|
+
if (!memberships) {
|
|
46
|
+
try {
|
|
47
|
+
memberships = normalizeMemberships(await deps.listMemberships());
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return { status: "unavailable", memberships: [] };
|
|
51
|
+
}
|
|
52
|
+
// Detect a clear or selection made while the network request was pending.
|
|
53
|
+
// A stale discovery may update the cache, but must not make a default.
|
|
54
|
+
const recorded = recordDefaultCompanyMemberships({ checkedAt: now.toISOString(), memberships }, { root: deps.root, now: deps.now });
|
|
55
|
+
if (recorded.defaultCompany?.source === "disabled") {
|
|
56
|
+
return { status: "disabled", memberships };
|
|
57
|
+
}
|
|
58
|
+
if (recorded.defaultCompany?.enabled) {
|
|
59
|
+
return { status: "existing", memberships };
|
|
60
|
+
}
|
|
61
|
+
configBeforeDiscovery.updatedAt = recorded.updatedAt;
|
|
62
|
+
}
|
|
63
|
+
if (memberships.length === 1) {
|
|
64
|
+
const selected = memberships[0];
|
|
65
|
+
const next = setAutoDefaultCompany(selected, {
|
|
66
|
+
root: deps.root,
|
|
67
|
+
now: deps.now,
|
|
68
|
+
expectedUpdatedAt: configBeforeDiscovery.updatedAt,
|
|
69
|
+
});
|
|
70
|
+
if (next.defaultCompany?.source === "disabled") {
|
|
71
|
+
return { status: "disabled", memberships };
|
|
72
|
+
}
|
|
73
|
+
if (next.defaultCompany?.source !== "auto_single") {
|
|
74
|
+
return { status: "existing", memberships };
|
|
75
|
+
}
|
|
76
|
+
deps.log?.(`Default company set automatically to ${selected.slug}.`);
|
|
77
|
+
return { status: "auto_single", memberships };
|
|
78
|
+
}
|
|
79
|
+
if (memberships.length > 1)
|
|
80
|
+
return { status: "needs_choice", memberships };
|
|
81
|
+
return { status: "unavailable", memberships: [] };
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=default-company.js.map
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import type { DeliveryState } from "./contract.js";
|
|
6
6
|
import { WORK_CONTEXT_CONTRACT_VERSION } from "./contract.js";
|
|
7
7
|
/** Fields allowed on a durable outbox operation (privacy allowlist). */
|
|
8
|
-
export declare const OUTBOX_ALLOWLIST: readonly ["contractVersion", "operationId", "clientOperationId", "sessionId", "harness", "companyUid", "companySlug", "projectId", "taskId", "kind", "digest", "delivery", "createdAt", "updatedAt", "attemptCount", "lastErrorCode", "receiptId", "destinationCompanyUid", "destinationCompanySlug", "nextAttemptAt"];
|
|
8
|
+
export declare const OUTBOX_ALLOWLIST: readonly ["contractVersion", "operationId", "clientOperationId", "sessionId", "harness", "companyUid", "companySlug", "projectId", "taskId", "companyConfidence", "kind", "digest", "delivery", "createdAt", "updatedAt", "attemptCount", "lastErrorCode", "receiptId", "destinationCompanyUid", "destinationCompanySlug", "nextAttemptAt"];
|
|
9
9
|
/** Base delay for outbox retry backoff (attempt 1 → 30s). */
|
|
10
10
|
export declare const OUTBOX_RETRY_BASE_MS = 30000;
|
|
11
11
|
/** Cap for outbox retry backoff (6 hours). */
|
|
@@ -25,6 +25,8 @@ export interface OutboxOperation {
|
|
|
25
25
|
companySlug?: string;
|
|
26
26
|
projectId?: string;
|
|
27
27
|
taskId?: string;
|
|
28
|
+
/** Durable provenance for weak device-default bindings. */
|
|
29
|
+
companyConfidence?: "device_default";
|
|
28
30
|
kind: OutboxKind;
|
|
29
31
|
digest: string;
|
|
30
32
|
delivery: DeliveryState;
|
|
@@ -47,6 +49,7 @@ export interface OutboxEnqueueInput {
|
|
|
47
49
|
companySlug?: string;
|
|
48
50
|
projectId?: string;
|
|
49
51
|
taskId?: string;
|
|
52
|
+
companyConfidence?: "device_default";
|
|
50
53
|
kind?: OutboxKind;
|
|
51
54
|
destinationCompanyUid?: string;
|
|
52
55
|
destinationCompanySlug?: string;
|
|
@@ -62,6 +65,7 @@ export declare function digestOperation(parts: {
|
|
|
62
65
|
companySlug?: string;
|
|
63
66
|
projectId?: string;
|
|
64
67
|
taskId?: string;
|
|
68
|
+
companyConfidence?: "device_default";
|
|
65
69
|
kind: OutboxKind;
|
|
66
70
|
destinationCompanyUid?: string;
|
|
67
71
|
destinationCompanySlug?: string;
|
|
@@ -21,6 +21,7 @@ export const OUTBOX_ALLOWLIST = [
|
|
|
21
21
|
"companySlug",
|
|
22
22
|
"projectId",
|
|
23
23
|
"taskId",
|
|
24
|
+
"companyConfidence",
|
|
24
25
|
"kind",
|
|
25
26
|
"digest",
|
|
26
27
|
"delivery",
|
|
@@ -96,6 +97,8 @@ function projectOutbox(op) {
|
|
|
96
97
|
out.projectId = op.projectId;
|
|
97
98
|
if (op.taskId)
|
|
98
99
|
out.taskId = op.taskId;
|
|
100
|
+
if (op.companyConfidence)
|
|
101
|
+
out.companyConfidence = op.companyConfidence;
|
|
99
102
|
if (op.lastErrorCode)
|
|
100
103
|
out.lastErrorCode = op.lastErrorCode;
|
|
101
104
|
if (op.receiptId)
|
|
@@ -143,6 +146,7 @@ export function enqueueOutbox(input, root) {
|
|
|
143
146
|
companySlug: input.companySlug,
|
|
144
147
|
projectId: input.projectId,
|
|
145
148
|
taskId: input.taskId,
|
|
149
|
+
companyConfidence: input.companyConfidence,
|
|
146
150
|
kind,
|
|
147
151
|
destinationCompanyUid: input.destinationCompanyUid,
|
|
148
152
|
destinationCompanySlug: input.destinationCompanySlug,
|
|
@@ -172,6 +176,7 @@ export function enqueueOutbox(input, root) {
|
|
|
172
176
|
companySlug: input.companySlug,
|
|
173
177
|
projectId: input.projectId,
|
|
174
178
|
taskId: input.taskId,
|
|
179
|
+
companyConfidence: input.companyConfidence,
|
|
175
180
|
kind,
|
|
176
181
|
digest,
|
|
177
182
|
delivery: "queued",
|
|
@@ -52,6 +52,10 @@ export interface ReconcileDeps {
|
|
|
52
52
|
root: string;
|
|
53
53
|
env?: NodeJS.ProcessEnv;
|
|
54
54
|
now?: () => Date;
|
|
55
|
+
/** Used only by explicit historical repair; ordinary reconciliation keeps defaults enabled. */
|
|
56
|
+
allowDeviceDefault?: boolean;
|
|
57
|
+
/** Mark explicit historical default repair as lower-confidence local evidence. */
|
|
58
|
+
deviceDefaultConfidence?: boolean;
|
|
55
59
|
/** Network deliverer; only called after outbox write. */
|
|
56
60
|
deliver?: DeliverFn;
|
|
57
61
|
/** Skip network even when company is resolved (local unit tests). */
|