@indigoai-us/hq-cloud 5.23.0 → 5.25.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/dist/bin/sync-runner.d.ts +58 -3
- package/dist/bin/sync-runner.d.ts.map +1 -1
- package/dist/bin/sync-runner.js +84 -2
- package/dist/bin/sync-runner.js.map +1 -1
- package/dist/bin/sync-runner.test.js +90 -3
- package/dist/bin/sync-runner.test.js.map +1 -1
- package/dist/cli/share.d.ts +86 -20
- package/dist/cli/share.d.ts.map +1 -1
- package/dist/cli/share.js +332 -62
- package/dist/cli/share.js.map +1 -1
- package/dist/cli/share.test.js +490 -6
- package/dist/cli/share.test.js.map +1 -1
- package/dist/cli/sync.d.ts +48 -0
- package/dist/cli/sync.d.ts.map +1 -1
- package/dist/cli/sync.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/personal-vault-exclusions.d.ts +128 -0
- package/dist/personal-vault-exclusions.d.ts.map +1 -0
- package/dist/personal-vault-exclusions.js +231 -0
- package/dist/personal-vault-exclusions.js.map +1 -0
- package/dist/personal-vault-exclusions.test.d.ts +22 -0
- package/dist/personal-vault-exclusions.test.d.ts.map +1 -0
- package/dist/personal-vault-exclusions.test.js +198 -0
- package/dist/personal-vault-exclusions.test.js.map +1 -0
- package/package.json +1 -1
- package/src/bin/sync-runner.test.ts +113 -3
- package/src/bin/sync-runner.ts +125 -5
- package/src/cli/share.test.ts +585 -6
- package/src/cli/share.ts +461 -86
- package/src/cli/sync.ts +50 -0
- package/src/index.ts +10 -0
- package/src/personal-vault-exclusions.test.ts +256 -0
- package/src/personal-vault-exclusions.ts +277 -0
package/dist/cli/share.test.js
CHANGED
|
@@ -19,7 +19,7 @@ vi.mock("../s3.js", () => ({
|
|
|
19
19
|
deleteRemoteFile: vi.fn().mockResolvedValue(undefined),
|
|
20
20
|
headRemoteFile: vi.fn().mockResolvedValue(null),
|
|
21
21
|
}));
|
|
22
|
-
import { share } from "./share.js";
|
|
22
|
+
import { share, _testing as shareTesting } from "./share.js";
|
|
23
23
|
import { deleteRemoteFile, headRemoteFile, uploadFile, uploadSymlink } from "../s3.js";
|
|
24
24
|
const mockConfig = {
|
|
25
25
|
apiUrl: "https://vault-api.test",
|
|
@@ -485,9 +485,12 @@ describe("share", () => {
|
|
|
485
485
|
vaultConfig: mockConfig,
|
|
486
486
|
hqRoot: tmpDir,
|
|
487
487
|
onEvent: (e) => {
|
|
488
|
-
// Only file-level events carry `.path`. The Stage-1 `plan` event
|
|
489
|
-
//
|
|
490
|
-
|
|
488
|
+
// Only file-level events carry `.path`. The Stage-1 `plan` event +
|
|
489
|
+
// the new-files event + the personal-vault-out-of-policy summary
|
|
490
|
+
// event are surfaced separately and tested in their own blocks.
|
|
491
|
+
if (e.type === "plan" ||
|
|
492
|
+
e.type === "new-files" ||
|
|
493
|
+
e.type === "personal-vault-out-of-policy")
|
|
491
494
|
return;
|
|
492
495
|
events.push({
|
|
493
496
|
type: e.type,
|
|
@@ -745,6 +748,10 @@ describe("share", () => {
|
|
|
745
748
|
hqRoot: tmpDir,
|
|
746
749
|
skipUnchanged: true,
|
|
747
750
|
propagateDeletes: true,
|
|
751
|
+
// Pin owned-only — this test predates the currency-gated default and
|
|
752
|
+
// asserts the direction-of-origin branch. A separate currency-gated
|
|
753
|
+
// test covers the new default semantics for the same scenario.
|
|
754
|
+
propagateDeletePolicy: "owned-only",
|
|
748
755
|
});
|
|
749
756
|
expect(result.filesDeleted).toBe(1);
|
|
750
757
|
expect(deleteRemoteFile).toHaveBeenCalledWith(expect.anything(), "knowledge");
|
|
@@ -788,6 +795,9 @@ describe("share", () => {
|
|
|
788
795
|
hqRoot: tmpDir,
|
|
789
796
|
skipUnchanged: true,
|
|
790
797
|
propagateDeletes: true,
|
|
798
|
+
// Owned-only — predates currency-gated default; asserts the lstat
|
|
799
|
+
// guard which is independent of the policy branch.
|
|
800
|
+
propagateDeletePolicy: "owned-only",
|
|
791
801
|
});
|
|
792
802
|
expect(result.filesDeleted).toBe(0);
|
|
793
803
|
expect(deleteRemoteFile).not.toHaveBeenCalled();
|
|
@@ -829,6 +839,8 @@ describe("share", () => {
|
|
|
829
839
|
hqRoot: tmpDir,
|
|
830
840
|
skipUnchanged: true,
|
|
831
841
|
propagateDeletes: true,
|
|
842
|
+
// Owned-only — currency-gated semantics covered separately below.
|
|
843
|
+
propagateDeletePolicy: "owned-only",
|
|
832
844
|
});
|
|
833
845
|
expect(result.filesDeleted).toBe(1);
|
|
834
846
|
expect(deleteRemoteFile).toHaveBeenCalledTimes(1);
|
|
@@ -862,6 +874,9 @@ describe("share", () => {
|
|
|
862
874
|
hqRoot: tmpDir,
|
|
863
875
|
skipUnchanged: true,
|
|
864
876
|
propagateDeletes: true,
|
|
877
|
+
// Owned-only — currency-gated emits a separate event variant; this test
|
|
878
|
+
// pins the legacy progress-with-deleted-flag shape.
|
|
879
|
+
propagateDeletePolicy: "owned-only",
|
|
865
880
|
onEvent: (e) => events.push(e),
|
|
866
881
|
});
|
|
867
882
|
const planEvent = events.find((e) => e.type === "plan");
|
|
@@ -934,6 +949,9 @@ describe("share", () => {
|
|
|
934
949
|
hqRoot: tmpDir,
|
|
935
950
|
skipUnchanged: true,
|
|
936
951
|
propagateDeletes: true,
|
|
952
|
+
// Owned-only — this test asserts scope containment, independent of
|
|
953
|
+
// the policy branch. Currency-gated covered separately.
|
|
954
|
+
propagateDeletePolicy: "owned-only",
|
|
937
955
|
});
|
|
938
956
|
expect(result.filesDeleted).toBe(1);
|
|
939
957
|
expect(deleteRemoteFile).toHaveBeenCalledTimes(1);
|
|
@@ -969,6 +987,9 @@ describe("share", () => {
|
|
|
969
987
|
hqRoot: tmpDir,
|
|
970
988
|
skipUnchanged: true,
|
|
971
989
|
propagateDeletes: true,
|
|
990
|
+
// Owned-only — pinning so the retry-survival assertion is independent
|
|
991
|
+
// of currency-gated's HEAD-driven bucketing.
|
|
992
|
+
propagateDeletePolicy: "owned-only",
|
|
972
993
|
onEvent: (e) => events.push(e),
|
|
973
994
|
});
|
|
974
995
|
expect(result.filesDeleted).toBe(0);
|
|
@@ -1002,7 +1023,7 @@ describe("share", () => {
|
|
|
1002
1023
|
// Both fixes live in `computeDeletePlan` and are belt-and-suspenders:
|
|
1003
1024
|
// (i) `direction === "up"` requirement under the default policy.
|
|
1004
1025
|
// (ii) `shouldSync` must accept the key — same filter the pull uses.
|
|
1005
|
-
it("propagateDeletes: under owned-only (default), skips direction:'down' entries", async () => {
|
|
1026
|
+
it("propagateDeletes: under owned-only (legacy pre-5.24 default), skips direction:'down' entries", async () => {
|
|
1006
1027
|
const companyRoot = path.join(tmpDir, "companies", "acme");
|
|
1007
1028
|
fs.mkdirSync(companyRoot, { recursive: true });
|
|
1008
1029
|
// No local files. One journal entry pulled from elsewhere (direction:'down')
|
|
@@ -1030,7 +1051,8 @@ describe("share", () => {
|
|
|
1030
1051
|
hqRoot: tmpDir,
|
|
1031
1052
|
skipUnchanged: true,
|
|
1032
1053
|
propagateDeletes: true,
|
|
1033
|
-
//
|
|
1054
|
+
// Explicit opt-in — `currency-gated` is the new (5.24+) default.
|
|
1055
|
+
propagateDeletePolicy: "owned-only",
|
|
1034
1056
|
});
|
|
1035
1057
|
// Only the 'up' entry is deleted; the 'down' entry is left alone so a
|
|
1036
1058
|
// behind-machine first-sync can't erase peer uploads.
|
|
@@ -1093,6 +1115,10 @@ describe("share", () => {
|
|
|
1093
1115
|
hqRoot: tmpDir,
|
|
1094
1116
|
skipUnchanged: true,
|
|
1095
1117
|
propagateDeletes: true,
|
|
1118
|
+
// Owned-only — filter symmetry is policy-independent; pinning so the
|
|
1119
|
+
// legacy direction-of-origin gate (not currency-gated's HEAD path)
|
|
1120
|
+
// resolves the "delete vs skip" decision for active/current.md.
|
|
1121
|
+
propagateDeletePolicy: "owned-only",
|
|
1096
1122
|
});
|
|
1097
1123
|
// legacy/old-layout.md is filter-skipped; only active/current.md is
|
|
1098
1124
|
// eligible for delete.
|
|
@@ -1100,6 +1126,344 @@ describe("share", () => {
|
|
|
1100
1126
|
expect(deleteRemoteFile).toHaveBeenCalledWith(expect.anything(), "active/current.md");
|
|
1101
1127
|
expect(deleteRemoteFile).not.toHaveBeenCalledWith(expect.anything(), "legacy/old-layout.md");
|
|
1102
1128
|
});
|
|
1129
|
+
// ── Delete propagation: currency-gated policy (5.24+ default) ──────────────
|
|
1130
|
+
//
|
|
1131
|
+
// The pre-5.24 `owned-only` default refused to delete-propagate any journal
|
|
1132
|
+
// entry whose `direction !== "up"`. That made sense as a safety net against
|
|
1133
|
+
// a behind machine's first push erasing peer uploads — but it had a worse
|
|
1134
|
+
// failure mode in practice: every `/update-hq` writes `core/`, `.claude/`,
|
|
1135
|
+
// `.codex/` with `direction: "down"` (they came from upstream), so any
|
|
1136
|
+
// subsequent local delete during a cleanup or upgrade was silently dropped.
|
|
1137
|
+
// Net effect: remote vault accumulated permanent litter the user could not
|
|
1138
|
+
// clean up without manually invoking `policy: "all"` (which has its own
|
|
1139
|
+
// safety problems).
|
|
1140
|
+
//
|
|
1141
|
+
// `currency-gated` solves this by gating on per-file proof of currency: HEAD
|
|
1142
|
+
// the remote, compare ETag, only propagate when this device has the
|
|
1143
|
+
// latest version. Direction-of-origin becomes irrelevant — a file the
|
|
1144
|
+
// upstream `/update-hq` wrote can be cleanly deleted by the device that
|
|
1145
|
+
// wrote it, as long as no other device modified it in the meantime.
|
|
1146
|
+
it("currency-gated: propagates delete when remote ETag matches journal", async () => {
|
|
1147
|
+
const companyRoot = path.join(tmpDir, "companies", "acme");
|
|
1148
|
+
fs.mkdirSync(companyRoot, { recursive: true });
|
|
1149
|
+
// direction:"down" file (e.g. arrived via /update-hq), locally deleted.
|
|
1150
|
+
// Under owned-only this would be stuck on remote forever; currency-gated
|
|
1151
|
+
// checks the ETag and propagates the delete cleanly when match holds.
|
|
1152
|
+
const journalPath = path.join(stateDir, "sync-journal.acme.json");
|
|
1153
|
+
fs.writeFileSync(journalPath, JSON.stringify({
|
|
1154
|
+
version: "1",
|
|
1155
|
+
lastSync: new Date().toISOString(),
|
|
1156
|
+
files: {
|
|
1157
|
+
"core/policies/old.md": {
|
|
1158
|
+
hash: "h", size: 100, syncedAt: new Date().toISOString(),
|
|
1159
|
+
direction: "down",
|
|
1160
|
+
remoteEtag: "abc123",
|
|
1161
|
+
},
|
|
1162
|
+
},
|
|
1163
|
+
}));
|
|
1164
|
+
// HEAD returns the same etag — this device is current for the file.
|
|
1165
|
+
vi.mocked(headRemoteFile).mockResolvedValueOnce({
|
|
1166
|
+
lastModified: new Date(),
|
|
1167
|
+
etag: '"abc123"',
|
|
1168
|
+
size: 100,
|
|
1169
|
+
});
|
|
1170
|
+
const result = await share({
|
|
1171
|
+
paths: [companyRoot],
|
|
1172
|
+
company: "acme",
|
|
1173
|
+
vaultConfig: mockConfig,
|
|
1174
|
+
hqRoot: tmpDir,
|
|
1175
|
+
skipUnchanged: true,
|
|
1176
|
+
propagateDeletes: true,
|
|
1177
|
+
propagateDeletePolicy: "currency-gated",
|
|
1178
|
+
});
|
|
1179
|
+
expect(result.filesDeleted).toBe(1);
|
|
1180
|
+
expect(deleteRemoteFile).toHaveBeenCalledWith(expect.anything(), "core/policies/old.md");
|
|
1181
|
+
const journal = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
|
|
1182
|
+
expect(journal.files["core/policies/old.md"]).toBeUndefined();
|
|
1183
|
+
});
|
|
1184
|
+
it("currency-gated: refuses delete + emits stale-etag event when remote moved since last sync", async () => {
|
|
1185
|
+
const companyRoot = path.join(tmpDir, "companies", "acme");
|
|
1186
|
+
fs.mkdirSync(companyRoot, { recursive: true });
|
|
1187
|
+
// Another device modified `shared.md` after this device's last sync.
|
|
1188
|
+
// The journal still records the old etag; HEAD returns the new one.
|
|
1189
|
+
const journalPath = path.join(stateDir, "sync-journal.acme.json");
|
|
1190
|
+
fs.writeFileSync(journalPath, JSON.stringify({
|
|
1191
|
+
version: "1",
|
|
1192
|
+
lastSync: new Date().toISOString(),
|
|
1193
|
+
files: {
|
|
1194
|
+
"shared.md": {
|
|
1195
|
+
hash: "h", size: 50, syncedAt: new Date().toISOString(),
|
|
1196
|
+
direction: "down",
|
|
1197
|
+
remoteEtag: "stale-etag",
|
|
1198
|
+
},
|
|
1199
|
+
},
|
|
1200
|
+
}));
|
|
1201
|
+
vi.mocked(headRemoteFile).mockResolvedValueOnce({
|
|
1202
|
+
lastModified: new Date(),
|
|
1203
|
+
etag: '"fresh-etag"',
|
|
1204
|
+
size: 51,
|
|
1205
|
+
});
|
|
1206
|
+
const events = [];
|
|
1207
|
+
const result = await share({
|
|
1208
|
+
paths: [companyRoot],
|
|
1209
|
+
company: "acme",
|
|
1210
|
+
vaultConfig: mockConfig,
|
|
1211
|
+
hqRoot: tmpDir,
|
|
1212
|
+
skipUnchanged: true,
|
|
1213
|
+
propagateDeletes: true,
|
|
1214
|
+
propagateDeletePolicy: "currency-gated",
|
|
1215
|
+
onEvent: (e) => events.push(e),
|
|
1216
|
+
});
|
|
1217
|
+
// No remote DELETE issued, journal entry preserved (pull will re-pull).
|
|
1218
|
+
expect(result.filesDeleted).toBe(0);
|
|
1219
|
+
expect(deleteRemoteFile).not.toHaveBeenCalled();
|
|
1220
|
+
const journal = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
|
|
1221
|
+
expect(journal.files["shared.md"]).toBeDefined();
|
|
1222
|
+
// Dedicated event fired so UIs surface the refusal. `reason` field
|
|
1223
|
+
// discriminates "stale-etag" (real drift) from "legacy-no-etag" so
|
|
1224
|
+
// consumers don't have to string-compare placeholder etag values.
|
|
1225
|
+
const refusedEvent = events.find((e) => e.type === "delete-refused-stale-etag");
|
|
1226
|
+
expect(refusedEvent).toMatchObject({
|
|
1227
|
+
type: "delete-refused-stale-etag",
|
|
1228
|
+
path: "shared.md",
|
|
1229
|
+
journalEtag: "stale-etag",
|
|
1230
|
+
remoteEtag: "fresh-etag",
|
|
1231
|
+
reason: "stale-etag",
|
|
1232
|
+
});
|
|
1233
|
+
// Counter exposed on ShareResult so callers don't need to re-count events.
|
|
1234
|
+
expect(result.filesRefusedStale).toBe(1);
|
|
1235
|
+
expect(result.filesTombstoned).toBe(0);
|
|
1236
|
+
});
|
|
1237
|
+
it("currency-gated: tombstones journal entry when remote returns 404 (out-of-band cleanup)", async () => {
|
|
1238
|
+
const companyRoot = path.join(tmpDir, "companies", "acme");
|
|
1239
|
+
fs.mkdirSync(companyRoot, { recursive: true });
|
|
1240
|
+
// Remote file was deleted out-of-band (e.g. someone removed it via the
|
|
1241
|
+
// S3 console). Local copy also missing. Currency-gated should drop the
|
|
1242
|
+
// journal entry without issuing a DELETE — the remote is already gone.
|
|
1243
|
+
const journalPath = path.join(stateDir, "sync-journal.acme.json");
|
|
1244
|
+
fs.writeFileSync(journalPath, JSON.stringify({
|
|
1245
|
+
version: "1",
|
|
1246
|
+
lastSync: new Date().toISOString(),
|
|
1247
|
+
files: {
|
|
1248
|
+
"removed-out-of-band.md": {
|
|
1249
|
+
hash: "h", size: 25, syncedAt: new Date().toISOString(),
|
|
1250
|
+
direction: "down",
|
|
1251
|
+
remoteEtag: "doesnt-matter",
|
|
1252
|
+
},
|
|
1253
|
+
},
|
|
1254
|
+
}));
|
|
1255
|
+
vi.mocked(headRemoteFile).mockResolvedValueOnce(null);
|
|
1256
|
+
const events = [];
|
|
1257
|
+
const result = await share({
|
|
1258
|
+
paths: [companyRoot],
|
|
1259
|
+
company: "acme",
|
|
1260
|
+
vaultConfig: mockConfig,
|
|
1261
|
+
hqRoot: tmpDir,
|
|
1262
|
+
skipUnchanged: true,
|
|
1263
|
+
propagateDeletes: true,
|
|
1264
|
+
propagateDeletePolicy: "currency-gated",
|
|
1265
|
+
onEvent: (e) => events.push(e),
|
|
1266
|
+
});
|
|
1267
|
+
// No DeleteObject (remote was already gone), but journal converges.
|
|
1268
|
+
expect(deleteRemoteFile).not.toHaveBeenCalled();
|
|
1269
|
+
// filesDeleted counts actual S3 deletes; tombstones aren't counted here.
|
|
1270
|
+
expect(result.filesDeleted).toBe(0);
|
|
1271
|
+
// Tombstones have their own counter so callers can distinguish them
|
|
1272
|
+
// from real deletes without re-counting events.
|
|
1273
|
+
expect(result.filesTombstoned).toBe(1);
|
|
1274
|
+
expect(result.filesRefusedStale).toBe(0);
|
|
1275
|
+
const journal = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
|
|
1276
|
+
expect(journal.files["removed-out-of-band.md"]).toBeUndefined();
|
|
1277
|
+
// Synthetic progress event carries the tombstone marker. Without this,
|
|
1278
|
+
// the tty stream renders tombstones identically to real deletes — operator
|
|
1279
|
+
// can't tell from logs alone that no S3 call was issued.
|
|
1280
|
+
const tombstoneEvent = events.find((e) => e.type === "progress" && e.deleted === true && e.message?.includes("tombstone"));
|
|
1281
|
+
expect(tombstoneEvent).toMatchObject({
|
|
1282
|
+
type: "progress",
|
|
1283
|
+
path: "removed-out-of-band.md",
|
|
1284
|
+
bytes: 0,
|
|
1285
|
+
deleted: true,
|
|
1286
|
+
message: expect.stringContaining("tombstone"),
|
|
1287
|
+
});
|
|
1288
|
+
});
|
|
1289
|
+
it("currency-gated: refuses delete for legacy journal entry with no recorded remoteEtag", async () => {
|
|
1290
|
+
const companyRoot = path.join(tmpDir, "companies", "acme");
|
|
1291
|
+
fs.mkdirSync(companyRoot, { recursive: true });
|
|
1292
|
+
// Journal entry from before remoteEtag tracking — no etag to compare
|
|
1293
|
+
// against. Refuse the delete in the safe direction; a future sync with
|
|
1294
|
+
// a recorded etag can re-evaluate.
|
|
1295
|
+
const journalPath = path.join(stateDir, "sync-journal.acme.json");
|
|
1296
|
+
fs.writeFileSync(journalPath, JSON.stringify({
|
|
1297
|
+
version: "1",
|
|
1298
|
+
lastSync: new Date().toISOString(),
|
|
1299
|
+
files: {
|
|
1300
|
+
"legacy-no-etag.md": {
|
|
1301
|
+
hash: "h", size: 5, syncedAt: new Date().toISOString(),
|
|
1302
|
+
direction: "down",
|
|
1303
|
+
// No remoteEtag.
|
|
1304
|
+
},
|
|
1305
|
+
},
|
|
1306
|
+
}));
|
|
1307
|
+
const events = [];
|
|
1308
|
+
const result = await share({
|
|
1309
|
+
paths: [companyRoot],
|
|
1310
|
+
company: "acme",
|
|
1311
|
+
vaultConfig: mockConfig,
|
|
1312
|
+
hqRoot: tmpDir,
|
|
1313
|
+
skipUnchanged: true,
|
|
1314
|
+
propagateDeletes: true,
|
|
1315
|
+
propagateDeletePolicy: "currency-gated",
|
|
1316
|
+
onEvent: (e) => events.push(e),
|
|
1317
|
+
});
|
|
1318
|
+
expect(result.filesDeleted).toBe(0);
|
|
1319
|
+
expect(deleteRemoteFile).not.toHaveBeenCalled();
|
|
1320
|
+
// HEAD must NOT be called — we short-circuit on the missing journal etag.
|
|
1321
|
+
expect(headRemoteFile).not.toHaveBeenCalled();
|
|
1322
|
+
const refused = events.find((e) => e.type === "delete-refused-stale-etag");
|
|
1323
|
+
expect(refused).toMatchObject({
|
|
1324
|
+
journalEtag: "<legacy-no-etag>",
|
|
1325
|
+
reason: "legacy-no-etag",
|
|
1326
|
+
});
|
|
1327
|
+
expect(result.filesRefusedStale).toBe(1);
|
|
1328
|
+
});
|
|
1329
|
+
it("currency-gated: real-world /update-hq scenario — direction:'down' delete propagates when current", async () => {
|
|
1330
|
+
// Regression for the exact bug that motivated the 5.24 default flip:
|
|
1331
|
+
// every `/update-hq` writes core/.claude/.codex from upstream and
|
|
1332
|
+
// journals them as direction:"down". When the user later moves or
|
|
1333
|
+
// deletes one locally (e.g. during cleanup, dir-restructure, or the
|
|
1334
|
+
// next upgrade), pre-5.24 `owned-only` silently kept it on remote
|
|
1335
|
+
// forever. Currency-gated propagates the delete cleanly as long as
|
|
1336
|
+
// this device is current for the file.
|
|
1337
|
+
const companyRoot = path.join(tmpDir, "companies", "acme");
|
|
1338
|
+
fs.mkdirSync(companyRoot, { recursive: true });
|
|
1339
|
+
const journalPath = path.join(stateDir, "sync-journal.acme.json");
|
|
1340
|
+
fs.writeFileSync(journalPath, JSON.stringify({
|
|
1341
|
+
version: "1",
|
|
1342
|
+
lastSync: new Date().toISOString(),
|
|
1343
|
+
files: {
|
|
1344
|
+
".claude/commands/retired-command.md": {
|
|
1345
|
+
hash: "h", size: 200, syncedAt: new Date().toISOString(),
|
|
1346
|
+
direction: "down",
|
|
1347
|
+
remoteEtag: "upstream-etag",
|
|
1348
|
+
},
|
|
1349
|
+
},
|
|
1350
|
+
}));
|
|
1351
|
+
vi.mocked(headRemoteFile).mockResolvedValueOnce({
|
|
1352
|
+
lastModified: new Date(),
|
|
1353
|
+
etag: '"upstream-etag"',
|
|
1354
|
+
size: 200,
|
|
1355
|
+
});
|
|
1356
|
+
const result = await share({
|
|
1357
|
+
paths: [companyRoot],
|
|
1358
|
+
company: "acme",
|
|
1359
|
+
vaultConfig: mockConfig,
|
|
1360
|
+
hqRoot: tmpDir,
|
|
1361
|
+
skipUnchanged: true,
|
|
1362
|
+
propagateDeletes: true,
|
|
1363
|
+
// Explicit opt-in. 5.24 ships the currency-gated CODE PATH but keeps
|
|
1364
|
+
// `owned-only` as the default while it soaks; the default flips to
|
|
1365
|
+
// `currency-gated` in 5.25. This test pins the user-facing semantics
|
|
1366
|
+
// (the /update-hq delete-propagation bug) under the new policy
|
|
1367
|
+
// regardless of which default the surrounding release ships.
|
|
1368
|
+
propagateDeletePolicy: "currency-gated",
|
|
1369
|
+
});
|
|
1370
|
+
expect(result.filesDeleted).toBe(1);
|
|
1371
|
+
expect(deleteRemoteFile).toHaveBeenCalledWith(expect.anything(), ".claude/commands/retired-command.md");
|
|
1372
|
+
});
|
|
1373
|
+
// ── Conflict-mirror exclusion (ephemeral pattern) ──────────────────────────
|
|
1374
|
+
//
|
|
1375
|
+
// Conflict mirrors (`*.conflict-<ISO>-<machineHash>.<ext>`) are local-only
|
|
1376
|
+
// safety backups written by the pull leg whenever a 3-way merge keeps
|
|
1377
|
+
// local and wants to preserve the remote version for inspection. They
|
|
1378
|
+
// MUST never round-trip to S3. Pre-fix, the push walker uploaded them,
|
|
1379
|
+
// the journal tracked them, and the owned-only delete policy then refused
|
|
1380
|
+
// to clean them up — permanent ratchet of remote litter.
|
|
1381
|
+
it("conflict-mirror exclusion: push walker skips local conflict-mirror files", async () => {
|
|
1382
|
+
const companyRoot = path.join(tmpDir, "companies", "acme");
|
|
1383
|
+
fs.mkdirSync(path.join(companyRoot, "skills"), { recursive: true });
|
|
1384
|
+
// Two files: a normal one and a conflict mirror. Only the normal one
|
|
1385
|
+
// should reach S3; the mirror is local-only state.
|
|
1386
|
+
fs.writeFileSync(path.join(companyRoot, "skills", "real.md"), "real content");
|
|
1387
|
+
fs.writeFileSync(path.join(companyRoot, "skills", "real.md.conflict-2026-05-13T19-40-40Z-e5797a.md"), "conflict mirror content");
|
|
1388
|
+
await share({
|
|
1389
|
+
paths: [companyRoot],
|
|
1390
|
+
company: "acme",
|
|
1391
|
+
vaultConfig: mockConfig,
|
|
1392
|
+
hqRoot: tmpDir,
|
|
1393
|
+
skipUnchanged: true,
|
|
1394
|
+
});
|
|
1395
|
+
expect(uploadFile).toHaveBeenCalledTimes(1);
|
|
1396
|
+
expect(uploadFile).toHaveBeenCalledWith(expect.anything(), expect.stringContaining("real.md"), "skills/real.md");
|
|
1397
|
+
// Spy was called once total — the conflict mirror never reaches uploadFile.
|
|
1398
|
+
// (Asserted by count above; the explicit non-call below is defensive.)
|
|
1399
|
+
const calls = vi.mocked(uploadFile).mock.calls;
|
|
1400
|
+
expect(calls.every((c) => !String(c[1] ?? "").includes("conflict-"))).toBe(true);
|
|
1401
|
+
});
|
|
1402
|
+
it("conflict-mirror exclusion: explicit user-supplied conflict-mirror path is also refused", async () => {
|
|
1403
|
+
const companyRoot = path.join(tmpDir, "companies", "acme");
|
|
1404
|
+
fs.mkdirSync(companyRoot, { recursive: true });
|
|
1405
|
+
const mirrorPath = path.join(companyRoot, "CLAUDE.md.conflict-2026-05-13T19-40-40Z-e5797a.md");
|
|
1406
|
+
fs.writeFileSync(mirrorPath, "mirror content");
|
|
1407
|
+
await share({
|
|
1408
|
+
paths: [mirrorPath],
|
|
1409
|
+
company: "acme",
|
|
1410
|
+
vaultConfig: mockConfig,
|
|
1411
|
+
hqRoot: tmpDir,
|
|
1412
|
+
skipUnchanged: true,
|
|
1413
|
+
});
|
|
1414
|
+
// Explicit caller path matching the ephemeral pattern is filtered the
|
|
1415
|
+
// same as a walker-discovered one. Belt-and-suspenders against any
|
|
1416
|
+
// tooling that hands a conflict mirror to share() directly.
|
|
1417
|
+
expect(uploadFile).not.toHaveBeenCalled();
|
|
1418
|
+
});
|
|
1419
|
+
it("conflict-mirror exclusion: journaled mirror with local-missing is NOT swept by delete plan", async () => {
|
|
1420
|
+
const companyRoot = path.join(tmpDir, "companies", "acme");
|
|
1421
|
+
fs.mkdirSync(companyRoot, { recursive: true });
|
|
1422
|
+
// Simulate the existing-litter state: a conflict mirror that leaked
|
|
1423
|
+
// into the journal in a prior buggy version. Locally missing (user
|
|
1424
|
+
// already deleted it). The regular delete plan must NOT issue a
|
|
1425
|
+
// DeleteObject — that's the dedicated reconcile command's job, and
|
|
1426
|
+
// a sync should not accidentally race a user reviewing the mirror.
|
|
1427
|
+
const journalPath = path.join(stateDir, "sync-journal.acme.json");
|
|
1428
|
+
fs.writeFileSync(journalPath, JSON.stringify({
|
|
1429
|
+
version: "1",
|
|
1430
|
+
lastSync: new Date().toISOString(),
|
|
1431
|
+
files: {
|
|
1432
|
+
"CLAUDE.md.conflict-2026-05-13T19-40-40Z-e5797a.md": {
|
|
1433
|
+
hash: "h", size: 10, syncedAt: new Date().toISOString(),
|
|
1434
|
+
direction: "up",
|
|
1435
|
+
remoteEtag: "mirror-etag",
|
|
1436
|
+
},
|
|
1437
|
+
"regular.md": {
|
|
1438
|
+
hash: "h", size: 5, syncedAt: new Date().toISOString(),
|
|
1439
|
+
direction: "up",
|
|
1440
|
+
remoteEtag: "regular-etag",
|
|
1441
|
+
},
|
|
1442
|
+
},
|
|
1443
|
+
}));
|
|
1444
|
+
// HEAD needed for the non-mirror entry under currency-gated.
|
|
1445
|
+
vi.mocked(headRemoteFile).mockResolvedValue({
|
|
1446
|
+
lastModified: new Date(),
|
|
1447
|
+
etag: '"regular-etag"',
|
|
1448
|
+
size: 5,
|
|
1449
|
+
});
|
|
1450
|
+
const result = await share({
|
|
1451
|
+
paths: [companyRoot],
|
|
1452
|
+
company: "acme",
|
|
1453
|
+
vaultConfig: mockConfig,
|
|
1454
|
+
hqRoot: tmpDir,
|
|
1455
|
+
skipUnchanged: true,
|
|
1456
|
+
propagateDeletes: true,
|
|
1457
|
+
});
|
|
1458
|
+
expect(result.filesDeleted).toBe(1);
|
|
1459
|
+
expect(deleteRemoteFile).toHaveBeenCalledTimes(1);
|
|
1460
|
+
expect(deleteRemoteFile).toHaveBeenCalledWith(expect.anything(), "regular.md");
|
|
1461
|
+
expect(deleteRemoteFile).not.toHaveBeenCalledWith(expect.anything(), expect.stringContaining("conflict-"));
|
|
1462
|
+
// Mirror's journal entry survives — reconcile command (separate skill)
|
|
1463
|
+
// sweeps it once the user explicitly opts in.
|
|
1464
|
+
const journal = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
|
|
1465
|
+
expect(journal.files["CLAUDE.md.conflict-2026-05-13T19-40-40Z-e5797a.md"]).toBeDefined();
|
|
1466
|
+
});
|
|
1103
1467
|
// ── personalMode ───────────────────────────────────────────────────────────
|
|
1104
1468
|
//
|
|
1105
1469
|
// The personal vault (slug "personal" in the runner's fanout plan) shares
|
|
@@ -1422,4 +1786,124 @@ describe("share", () => {
|
|
|
1422
1786
|
});
|
|
1423
1787
|
});
|
|
1424
1788
|
});
|
|
1789
|
+
// ── Pure-function unit coverage: isEphemeralPath ─────────────────────────────
|
|
1790
|
+
//
|
|
1791
|
+
// EPHEMERAL_PATH_PATTERN is the single source of truth for "this is a conflict
|
|
1792
|
+
// mirror and must never round-trip to S3." Integration tests already cover the
|
|
1793
|
+
// behavior end-to-end (uploadFile is or isn't called), but a direct regex
|
|
1794
|
+
// contract test makes intent unambiguous and catches future drift in the
|
|
1795
|
+
// pattern — much cheaper than reproducing each case through share().
|
|
1796
|
+
describe("isEphemeralPath (conflict-mirror pattern contract)", () => {
|
|
1797
|
+
const { isEphemeralPath } = shareTesting;
|
|
1798
|
+
it.each([
|
|
1799
|
+
// Canonical: basename and relativeKey both match (the two callsites).
|
|
1800
|
+
[".claude/CLAUDE.md.conflict-2026-05-13T19-40-40Z-e5797a.md", true],
|
|
1801
|
+
["CLAUDE.md.conflict-2026-05-13T19-40-40Z-e5797a.md", true],
|
|
1802
|
+
[".claude/commands/adr.md.conflict-2026-05-13T19-40-41Z-e5797a.md", true],
|
|
1803
|
+
// Longer machine hash (no upper bound on `[a-f0-9]+`).
|
|
1804
|
+
["foo.conflict-2026-05-19T17-05-56Z-deadbeef.md", true],
|
|
1805
|
+
// Non-markdown extensions also valid (sh scripts, ts files, etc.).
|
|
1806
|
+
["foo.sh.conflict-2026-05-19T17-05-56Z-abc123.sh", true],
|
|
1807
|
+
])("matches conflict mirror: %s", (p, expected) => {
|
|
1808
|
+
expect(isEphemeralPath(p)).toBe(expected);
|
|
1809
|
+
});
|
|
1810
|
+
it.each([
|
|
1811
|
+
// Normal files: regular markdown, scripts, etc.
|
|
1812
|
+
["README.md", false],
|
|
1813
|
+
[".claude/CLAUDE.md", false],
|
|
1814
|
+
["companies/acme/knowledge/note.md", false],
|
|
1815
|
+
// Strings containing the word "conflict" but not the timestamp+hash token.
|
|
1816
|
+
["conflict-resolution.md", false],
|
|
1817
|
+
["my-conflict.md", false],
|
|
1818
|
+
["foo.conflict-handler.md", false],
|
|
1819
|
+
// Date-shaped but missing the trailing dot + extension (real conflicts
|
|
1820
|
+
// always carry a file extension; the trailing `\.` in the pattern is the
|
|
1821
|
+
// safety against bare-substring false positives).
|
|
1822
|
+
["foo.conflict-2026-05-13T19-40-40Z-abc", false],
|
|
1823
|
+
// Wrong-case or non-hex machine hash.
|
|
1824
|
+
["foo.conflict-2026-05-13T19-40-40Z-ZZZZZZ.md", false],
|
|
1825
|
+
// Wrong timestamp format (real conflicts use UTC ISO with Z suffix).
|
|
1826
|
+
["foo.conflict-2026-05-13-abc123.md", false],
|
|
1827
|
+
// Missing leading dot before "conflict" (this protects against legitimate
|
|
1828
|
+
// files that happen to contain the word "conflict" mid-name).
|
|
1829
|
+
["fooconflict-2026-05-13T19-40-40Z-abc.md", false],
|
|
1830
|
+
])("rejects non-mirror: %s", (p, expected) => {
|
|
1831
|
+
expect(isEphemeralPath(p)).toBe(expected);
|
|
1832
|
+
});
|
|
1833
|
+
});
|
|
1834
|
+
// ── Currency-gated coverage against journal version "2" fixtures ─────────────
|
|
1835
|
+
//
|
|
1836
|
+
// Production journals on disk are at `version: "2"` (verified via jq on the
|
|
1837
|
+
// real-world ~/.hq/sync-journal.personal.json). The currency-gated tests above
|
|
1838
|
+
// use v1 fixtures by historical convention — this block pins the behavior at
|
|
1839
|
+
// v2 explicitly so a future schema change can't silently strand the new policy
|
|
1840
|
+
// on a stale fixture format. The bucket logic ignores `version` (only reads
|
|
1841
|
+
// `journal.files[*]`), so this should pass identically — but we want the
|
|
1842
|
+
// regression test on record.
|
|
1843
|
+
describe("currency-gated: journal version 2 fixtures", () => {
|
|
1844
|
+
let tmpDir;
|
|
1845
|
+
let stateDir;
|
|
1846
|
+
let origDataHome;
|
|
1847
|
+
beforeEach(() => {
|
|
1848
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-share-test-v2-"));
|
|
1849
|
+
stateDir = path.join(tmpDir, ".hq");
|
|
1850
|
+
fs.mkdirSync(stateDir, { recursive: true });
|
|
1851
|
+
origDataHome = process.env.XDG_DATA_HOME;
|
|
1852
|
+
process.env.XDG_DATA_HOME = stateDir;
|
|
1853
|
+
process.env.HQ_STATE_DIR = stateDir;
|
|
1854
|
+
clearContextCache();
|
|
1855
|
+
setupFetchMock();
|
|
1856
|
+
vi.mocked(uploadFile).mockClear();
|
|
1857
|
+
vi.mocked(uploadSymlink).mockClear();
|
|
1858
|
+
vi.mocked(deleteRemoteFile).mockClear();
|
|
1859
|
+
vi.mocked(headRemoteFile).mockReset();
|
|
1860
|
+
vi.mocked(headRemoteFile).mockResolvedValue(null);
|
|
1861
|
+
});
|
|
1862
|
+
afterEach(() => {
|
|
1863
|
+
if (origDataHome === undefined) {
|
|
1864
|
+
delete process.env.XDG_DATA_HOME;
|
|
1865
|
+
}
|
|
1866
|
+
else {
|
|
1867
|
+
process.env.XDG_DATA_HOME = origDataHome;
|
|
1868
|
+
}
|
|
1869
|
+
delete process.env.HQ_STATE_DIR;
|
|
1870
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
1871
|
+
});
|
|
1872
|
+
it("propagates delete on etag match against a v2-shaped journal", async () => {
|
|
1873
|
+
const companyRoot = path.join(tmpDir, "companies", "acme");
|
|
1874
|
+
fs.mkdirSync(companyRoot, { recursive: true });
|
|
1875
|
+
const journalPath = path.join(stateDir, "sync-journal.acme.json");
|
|
1876
|
+
// v2-shaped fixture — same field set the production journal uses.
|
|
1877
|
+
fs.writeFileSync(journalPath, JSON.stringify({
|
|
1878
|
+
version: "2",
|
|
1879
|
+
lastSync: new Date().toISOString(),
|
|
1880
|
+
files: {
|
|
1881
|
+
"core/policies/old.md": {
|
|
1882
|
+
hash: "h",
|
|
1883
|
+
size: 100,
|
|
1884
|
+
syncedAt: new Date().toISOString(),
|
|
1885
|
+
direction: "down",
|
|
1886
|
+
remoteEtag: "v2-etag",
|
|
1887
|
+
},
|
|
1888
|
+
},
|
|
1889
|
+
pulls: [],
|
|
1890
|
+
}));
|
|
1891
|
+
vi.mocked(headRemoteFile).mockResolvedValueOnce({
|
|
1892
|
+
lastModified: new Date(),
|
|
1893
|
+
etag: '"v2-etag"',
|
|
1894
|
+
size: 100,
|
|
1895
|
+
});
|
|
1896
|
+
const result = await share({
|
|
1897
|
+
paths: [companyRoot],
|
|
1898
|
+
company: "acme",
|
|
1899
|
+
vaultConfig: mockConfig,
|
|
1900
|
+
hqRoot: tmpDir,
|
|
1901
|
+
skipUnchanged: true,
|
|
1902
|
+
propagateDeletes: true,
|
|
1903
|
+
propagateDeletePolicy: "currency-gated",
|
|
1904
|
+
});
|
|
1905
|
+
expect(result.filesDeleted).toBe(1);
|
|
1906
|
+
expect(deleteRemoteFile).toHaveBeenCalledWith(expect.anything(), "core/policies/old.md");
|
|
1907
|
+
});
|
|
1908
|
+
});
|
|
1425
1909
|
//# sourceMappingURL=share.test.js.map
|