agent-plugin-lifecycle 0.1.1 → 0.2.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.
@@ -22,6 +22,9 @@ function _array_like_to_array(arr, len) {
22
22
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
23
23
  return arr2;
24
24
  }
25
+ function _array_with_holes(arr) {
26
+ if (Array.isArray(arr)) return arr;
27
+ }
25
28
  function _array_without_holes(arr) {
26
29
  if (Array.isArray(arr)) return _array_like_to_array(arr);
27
30
  }
@@ -67,9 +70,39 @@ function _iterable_to_array(iter) {
67
70
  return Array.from(iter);
68
71
  }
69
72
  }
73
+ function _iterable_to_array_limit(arr, i) {
74
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
75
+ if (_i == null) return;
76
+ var _arr = [];
77
+ var _n = true;
78
+ var _d = false;
79
+ var _s, _e;
80
+ try {
81
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
82
+ _arr.push(_s.value);
83
+ if (i && _arr.length === i) break;
84
+ }
85
+ } catch (err) {
86
+ _d = true;
87
+ _e = err;
88
+ } finally{
89
+ try {
90
+ if (!_n && _i["return"] != null) _i["return"]();
91
+ } finally{
92
+ if (_d) throw _e;
93
+ }
94
+ }
95
+ return _arr;
96
+ }
97
+ function _non_iterable_rest() {
98
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
99
+ }
70
100
  function _non_iterable_spread() {
71
101
  throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
72
102
  }
103
+ function _sliced_to_array(arr, i) {
104
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
105
+ }
73
106
  function _to_consumable_array(arr) {
74
107
  return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
75
108
  }
@@ -240,197 +273,207 @@ function existingWithin(root, path, label) {
240
273
  function pluginRoot(plugin, apmHome) {
241
274
  return (0, _nodepath.join)(apmHome, 'apm_modules', plugin.repo, plugin.path);
242
275
  }
276
+ function missing(error) {
277
+ return !!error && (typeof error === "undefined" ? "undefined" : _type_of(error)) === 'object' && 'code' in error && error.code === 'ENOENT';
278
+ }
279
+ function existingOrNone(root, path, label) {
280
+ return _async_to_generator(function() {
281
+ var error;
282
+ return _ts_generator(this, function(_state) {
283
+ switch(_state.label){
284
+ case 0:
285
+ _state.trys.push([
286
+ 0,
287
+ 2,
288
+ ,
289
+ 3
290
+ ]);
291
+ return [
292
+ 4,
293
+ existingWithin(root, path, label)
294
+ ];
295
+ case 1:
296
+ return [
297
+ 2,
298
+ _state.sent()
299
+ ];
300
+ case 2:
301
+ error = _state.sent();
302
+ if (missing(error)) return [
303
+ 2,
304
+ undefined
305
+ ];
306
+ throw error;
307
+ case 3:
308
+ return [
309
+ 2
310
+ ];
311
+ }
312
+ });
313
+ })();
314
+ }
315
+ var SKILL_ROOTS = [
316
+ '.agents/skills/',
317
+ '.hermes/skills/',
318
+ '.claude/skills/'
319
+ ];
320
+ function deployedSkillFolders(plugin, skill) {
321
+ var folders = new Set();
322
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
323
+ try {
324
+ var _loop = function() {
325
+ var file = _step.value;
326
+ var parts = file.split('/');
327
+ var index = parts.lastIndexOf('skills');
328
+ if (index < 0 || parts[index + 1] !== skill) return "continue";
329
+ var deployment = parts.slice(0, index + 2).join('/');
330
+ if (SKILL_ROOTS.some(function(candidate) {
331
+ return deployment.startsWith(candidate);
332
+ })) folders.add(deployment);
333
+ };
334
+ for(var _iterator = plugin.deployedFiles[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
335
+ } catch (err) {
336
+ _didIteratorError = true;
337
+ _iteratorError = err;
338
+ } finally{
339
+ try {
340
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
341
+ _iterator.return();
342
+ }
343
+ } finally{
344
+ if (_didIteratorError) {
345
+ throw _iteratorError;
346
+ }
347
+ }
348
+ }
349
+ return _to_consumable_array(folders);
350
+ }
351
+ // APM's kept copy (`apm_modules/...`) is a destination only when the lock names no other copy: a target such as copilot reads it in place.
352
+ // When the lock names a native plugin root or deployed skill copies, the kept copy is APM's cache and is never used, even if the listed copies are missing on disk (they are skipped, and check reports them).
243
353
  function destinations(plugin, policy, home, apmHome) {
244
354
  return _async_to_generator(function() {
245
- var output, root, _, nativeBase, _1, _plugin_nativePluginRoot, _2, error, nativeRoot, _3, error1, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err;
355
+ var _plugin_nativePluginRoot, found, add, nativeRoot, deployed, kept, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, deployment, err;
246
356
  return _ts_generator(this, function(_state) {
247
357
  switch(_state.label){
248
358
  case 0:
249
- output = new Set();
250
- root = pluginRoot(plugin, apmHome);
359
+ found = new Set();
360
+ add = function add(path) {
361
+ if (path) found.add(path);
362
+ };
363
+ nativeRoot = ((_plugin_nativePluginRoot = plugin.nativePluginRoot) === null || _plugin_nativePluginRoot === void 0 ? void 0 : _plugin_nativePluginRoot.startsWith('.agents/plugins/')) ? plugin.nativePluginRoot.slice('.agents/plugins/'.length) : undefined;
251
364
  if (!!policy.skill) return [
252
365
  3,
253
366
  4
254
367
  ];
255
- _ = output.add;
368
+ if (!!nativeRoot) return [
369
+ 3,
370
+ 2
371
+ ];
256
372
  return [
257
373
  4,
258
374
  existingWithin((0, _nodepath.join)(apmHome, 'apm_modules'), (0, _nodepath.join)(plugin.repo, plugin.path), 'kept plugin')
259
375
  ];
260
376
  case 1:
261
- _.apply(output, [
262
- _state.sent()
263
- ]);
264
- if (!plugin.nativePluginRoot) return [
265
- 3,
266
- 3
267
- ];
268
- nativeBase = (0, _nodepath.join)(home, '.agents', 'plugins');
269
- if (!plugin.nativePluginRoot.startsWith('.agents/plugins/')) return [
270
- 3,
271
- 3
377
+ return [
378
+ 2,
379
+ [
380
+ _state.sent()
381
+ ]
272
382
  ];
273
- _1 = output.add;
383
+ case 2:
274
384
  return [
275
385
  4,
276
- existingWithin(nativeBase, plugin.nativePluginRoot.slice('.agents/plugins/'.length), 'native plugin')
386
+ existingOrNone((0, _nodepath.join)(home, '.agents', 'plugins'), nativeRoot, 'native plugin')
277
387
  ];
278
- case 2:
279
- _1.apply(output, [
388
+ case 3:
389
+ add.apply(void 0, [
280
390
  _state.sent()
281
391
  ]);
282
- _state.label = 3;
283
- case 3:
392
+ if (!found.size) throw new Error("no native plugin copy found for ".concat(plugin.repo));
284
393
  return [
285
- 3,
286
- 20
394
+ 2,
395
+ _to_consumable_array(found)
287
396
  ];
288
397
  case 4:
289
- _state.trys.push([
290
- 4,
291
- 6,
292
- ,
293
- 7
294
- ]);
295
- _2 = output.add;
398
+ deployed = deployedSkillFolders(plugin, policy.skill);
399
+ if (!(!nativeRoot && !deployed.length)) return [
400
+ 3,
401
+ 6
402
+ ];
296
403
  return [
297
404
  4,
298
- existingWithin(root, "skills/".concat(policy.skill), "kept skill ".concat(policy.skill))
405
+ existingOrNone(pluginRoot(plugin, apmHome), "skills/".concat(policy.skill), "kept skill ".concat(policy.skill))
299
406
  ];
300
407
  case 5:
301
- _2.apply(output, [
302
- _state.sent()
303
- ]);
408
+ kept = _state.sent();
409
+ if (!kept) throw new Error("no deployed copy found for skill ".concat(policy.skill));
304
410
  return [
305
- 3,
306
- 7
411
+ 2,
412
+ [
413
+ kept
414
+ ]
307
415
  ];
308
416
  case 6:
309
- error = _state.sent();
310
- if (!(error && (typeof error === "undefined" ? "undefined" : _type_of(error)) === 'object' && 'code' in error && error.code === 'ENOENT')) throw error;
311
- return [
417
+ if (!nativeRoot) return [
312
418
  3,
313
- 7
314
- ];
315
- case 7:
316
- if (!((_plugin_nativePluginRoot = plugin.nativePluginRoot) === null || _plugin_nativePluginRoot === void 0 ? void 0 : _plugin_nativePluginRoot.startsWith('.agents/plugins/'))) return [
317
- 3,
318
- 11
419
+ 8
319
420
  ];
320
- nativeRoot = plugin.nativePluginRoot.slice('.agents/plugins/'.length);
321
- _state.label = 8;
322
- case 8:
323
- _state.trys.push([
324
- 8,
325
- 10,
326
- ,
327
- 11
328
- ]);
329
- _3 = output.add;
330
421
  return [
331
422
  4,
332
- existingWithin((0, _nodepath.join)(home, '.agents', 'plugins'), "".concat(nativeRoot, "/skills/").concat(policy.skill), "native skill ".concat(policy.skill))
423
+ existingOrNone((0, _nodepath.join)(home, '.agents', 'plugins'), "".concat(nativeRoot, "/skills/").concat(policy.skill), "native skill ".concat(policy.skill))
333
424
  ];
334
- case 9:
335
- _3.apply(output, [
425
+ case 7:
426
+ add.apply(void 0, [
336
427
  _state.sent()
337
428
  ]);
338
- return [
339
- 3,
340
- 11
341
- ];
342
- case 10:
343
- error1 = _state.sent();
344
- if (!(error1 && (typeof error1 === "undefined" ? "undefined" : _type_of(error1)) === 'object' && 'code' in error1 && error1.code === 'ENOENT')) throw error1;
345
- return [
346
- 3,
347
- 11
348
- ];
349
- case 11:
429
+ _state.label = 8;
430
+ case 8:
350
431
  _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
351
- _state.label = 12;
352
- case 12:
432
+ _state.label = 9;
433
+ case 9:
353
434
  _state.trys.push([
354
- 12,
355
- 17,
356
- 18,
357
- 19
435
+ 9,
436
+ 14,
437
+ 15,
438
+ 16
358
439
  ]);
359
- _loop = function() {
360
- var file, parts, index, deployment, prefix, _;
361
- return _ts_generator(this, function(_state) {
362
- switch(_state.label){
363
- case 0:
364
- file = _step.value;
365
- parts = file.split('/');
366
- index = parts.lastIndexOf('skills');
367
- if (index < 0 || parts[index + 1] !== policy.skill) return [
368
- 2,
369
- "continue"
370
- ];
371
- deployment = parts.slice(0, index + 2).join('/');
372
- prefix = [
373
- '.agents/skills/',
374
- '.hermes/skills/',
375
- '.claude/skills/'
376
- ].find(function(candidate) {
377
- return deployment.startsWith(candidate);
378
- });
379
- if (!prefix) return [
380
- 3,
381
- 2
382
- ];
383
- _ = output.add;
384
- return [
385
- 4,
386
- existingWithin(home, deployment, "skill ".concat(policy.skill))
387
- ];
388
- case 1:
389
- _.apply(output, [
390
- _state.sent()
391
- ]);
392
- _state.label = 2;
393
- case 2:
394
- return [
395
- 2
396
- ];
397
- }
398
- });
399
- };
400
- _iterator = plugin.deployedFiles[Symbol.iterator]();
401
- _state.label = 13;
402
- case 13:
440
+ _iterator = deployed[Symbol.iterator]();
441
+ _state.label = 10;
442
+ case 10:
403
443
  if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
404
444
  3,
405
- 16
445
+ 13
406
446
  ];
447
+ deployment = _step.value;
407
448
  return [
408
- 5,
409
- _ts_values(_loop())
449
+ 4,
450
+ existingOrNone(home, deployment, "skill ".concat(policy.skill))
410
451
  ];
411
- case 14:
412
- _state.sent();
413
- _state.label = 15;
414
- case 15:
452
+ case 11:
453
+ add.apply(void 0, [
454
+ _state.sent()
455
+ ]);
456
+ _state.label = 12;
457
+ case 12:
415
458
  _iteratorNormalCompletion = true;
416
459
  return [
417
460
  3,
418
- 13
461
+ 10
419
462
  ];
420
- case 16:
463
+ case 13:
421
464
  return [
422
465
  3,
423
- 19
466
+ 16
424
467
  ];
425
- case 17:
468
+ case 14:
426
469
  err = _state.sent();
427
470
  _didIteratorError = true;
428
471
  _iteratorError = err;
429
472
  return [
430
473
  3,
431
- 19
474
+ 16
432
475
  ];
433
- case 18:
476
+ case 15:
434
477
  try {
435
478
  if (!_iteratorNormalCompletion && _iterator.return != null) {
436
479
  _iterator.return();
@@ -443,13 +486,11 @@ function destinations(plugin, policy, home, apmHome) {
443
486
  return [
444
487
  7
445
488
  ];
446
- case 19:
447
- if (output.size === 0) throw new Error("no deployed copy found for skill ".concat(policy.skill));
448
- _state.label = 20;
449
- case 20:
489
+ case 16:
490
+ if (!found.size) throw new Error("no deployed copy found for skill ".concat(policy.skill));
450
491
  return [
451
492
  2,
452
- _to_consumable_array(output)
493
+ _to_consumable_array(found)
453
494
  ];
454
495
  }
455
496
  });
@@ -457,14 +498,14 @@ function destinations(plugin, policy, home, apmHome) {
457
498
  }
458
499
  function readState(path) {
459
500
  return _async_to_generator(function() {
460
- var value, _, item;
501
+ var value, _, item, receipts, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, key, receipt;
461
502
  return _ts_generator(this, function(_state) {
462
503
  switch(_state.label){
463
504
  case 0:
464
505
  if (!(0, _nodefs.existsSync)(path)) return [
465
506
  2,
466
507
  {
467
- version: 1,
508
+ version: 2,
468
509
  receipts: {}
469
510
  }
470
511
  ];
@@ -479,13 +520,39 @@ function readState(path) {
479
520
  ]);
480
521
  if (!value || (typeof value === "undefined" ? "undefined" : _type_of(value)) !== 'object' || Array.isArray(value)) throw new Error('state must be an object');
481
522
  item = value;
482
- if (item.version !== 1 || !item.receipts || _type_of(item.receipts) !== 'object' || Array.isArray(item.receipts)) throw new Error('unsupported state format');
483
- if (Object.values(item.receipts).some(function(receipt) {
484
- return typeof receipt !== 'string';
485
- })) throw new Error('invalid state receipt');
523
+ if (item.version !== 1 && item.version !== 2 || !item.receipts || _type_of(item.receipts) !== 'object' || Array.isArray(item.receipts)) throw new Error('unsupported state format');
524
+ receipts = {};
525
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
526
+ try {
527
+ for(_iterator = Object.entries(item.receipts)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
528
+ _step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], receipt = _step_value[1];
529
+ if (item.version === 1 && typeof receipt === 'string') receipts[key] = {
530
+ digest: receipt,
531
+ revision: ''
532
+ };
533
+ else if (item.version === 2 && receipt && (typeof receipt === "undefined" ? "undefined" : _type_of(receipt)) === 'object' && typeof receipt.digest === 'string' && typeof receipt.revision === 'string') receipts[key] = receipt;
534
+ else throw new Error('invalid state receipt');
535
+ }
536
+ } catch (err) {
537
+ _didIteratorError = true;
538
+ _iteratorError = err;
539
+ } finally{
540
+ try {
541
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
542
+ _iterator.return();
543
+ }
544
+ } finally{
545
+ if (_didIteratorError) {
546
+ throw _iteratorError;
547
+ }
548
+ }
549
+ }
486
550
  return [
487
551
  2,
488
- item
552
+ {
553
+ version: 2,
554
+ receipts: receipts
555
+ }
489
556
  ];
490
557
  }
491
558
  });
@@ -601,12 +668,19 @@ function execute(command, cwd, environment) {
601
668
  });
602
669
  })();
603
670
  }
671
+ function receiptRecord(key) {
672
+ try {
673
+ return JSON.parse(key)[0];
674
+ } catch (unused) {
675
+ return undefined;
676
+ }
677
+ }
604
678
  function lockLoadError(error) {
605
679
  return !!error && (typeof error === "undefined" ? "undefined" : _type_of(error)) === 'object' && 'code' in error && error.code === 'ENOENT';
606
680
  }
607
681
  function runLifecycle(_0) {
608
682
  return _async_to_generator(function(mode) {
609
- var options, _options_home, _options_apmHome, _options_configPath, _options_environment, _options_output, home, apmHome, configPath, environment, output, config, locked, error, _options_event, apmDirectory, statePath, release, _tmp, _ref, _options_event1, _options_event2, state, failed, eventNames, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err, event, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _loop1, _iterator1, _step1, err;
683
+ var options, _options_home, _options_apmHome, _options_configPath, _options_environment, _options_output, home, apmHome, configPath, environment, output, config, locked, error, _options_event, apmDirectory, statePath, release, _tmp, _ref, _options_event1, _options_event2, state, failed, eventNames, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err, event, installed, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _loop1, _iterator1, _step1, err;
610
684
  var _arguments = arguments;
611
685
  return _ts_generator(this, function(_state) {
612
686
  switch(_state.label){
@@ -660,7 +734,14 @@ function runLifecycle(_0) {
660
734
  7
661
735
  ];
662
736
  case 6:
663
- throw error;
737
+ if (mode === 'check' && lockLoadError(error)) {
738
+ output('nothing installed');
739
+ return [
740
+ 2,
741
+ 0
742
+ ];
743
+ } else throw error;
744
+ _state.label = 7;
664
745
  case 7:
665
746
  return [
666
747
  3,
@@ -737,7 +818,7 @@ function runLifecycle(_0) {
737
818
  23
738
819
  ]);
739
820
  _loop = function() {
740
- var policy, _ref, _ref1, id, receiptKeys, sourceCandidate, sourcePartsValue, removedNameMatch, anyStillInstalled, command, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step1, key, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step2, key1, error;
821
+ var policy, _ref, _ref1, id, receiptKeys, sourceCandidate, sourcePartsValue, removedNameMatch, anyStillInstalled, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step1, key, destination, command, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step2, key1, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step3, key2, error;
741
822
  return _ts_generator(this, function(_state) {
742
823
  switch(_state.label){
743
824
  case 0:
@@ -771,16 +852,13 @@ function runLifecycle(_0) {
771
852
  2,
772
853
  "continue"
773
854
  ];
774
- command = policy['post-uninstall'];
775
- if (!!command) return [
776
- 3,
777
- 2
778
- ];
779
855
  _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
780
856
  try {
857
+ // APM keeps a folder holding files it did not write; report it, delete nothing
781
858
  for(_iterator = receiptKeys[Symbol.iterator](); !(_iteratorNormalCompletion = (_step1 = _iterator.next()).done); _iteratorNormalCompletion = true){
782
859
  key = _step1.value;
783
- delete state.receipts[key];
860
+ destination = JSON.parse(key)[1];
861
+ if ((0, _nodefs.existsSync)(destination)) output("left ".concat(destination));
784
862
  }
785
863
  } catch (err) {
786
864
  _didIteratorError = true;
@@ -796,6 +874,31 @@ function runLifecycle(_0) {
796
874
  }
797
875
  }
798
876
  }
877
+ command = policy['post-uninstall'];
878
+ if (!!command) return [
879
+ 3,
880
+ 2
881
+ ];
882
+ _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
883
+ try {
884
+ for(_iterator1 = receiptKeys[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step2 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
885
+ key1 = _step2.value;
886
+ delete state.receipts[key1];
887
+ }
888
+ } catch (err) {
889
+ _didIteratorError1 = true;
890
+ _iteratorError1 = err;
891
+ } finally{
892
+ try {
893
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
894
+ _iterator1.return();
895
+ }
896
+ } finally{
897
+ if (_didIteratorError1) {
898
+ throw _iteratorError1;
899
+ }
900
+ }
901
+ }
799
902
  return [
800
903
  4,
801
904
  saveState(statePath, state)
@@ -819,23 +922,23 @@ function runLifecycle(_0) {
819
922
  ];
820
923
  case 3:
821
924
  _state.sent();
822
- _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
925
+ _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = undefined;
823
926
  try {
824
- for(_iterator1 = receiptKeys[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step2 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
825
- key1 = _step2.value;
826
- delete state.receipts[key1];
927
+ for(_iterator2 = receiptKeys[Symbol.iterator](); !(_iteratorNormalCompletion2 = (_step3 = _iterator2.next()).done); _iteratorNormalCompletion2 = true){
928
+ key2 = _step3.value;
929
+ delete state.receipts[key2];
827
930
  }
828
931
  } catch (err) {
829
- _didIteratorError1 = true;
830
- _iteratorError1 = err;
932
+ _didIteratorError2 = true;
933
+ _iteratorError2 = err;
831
934
  } finally{
832
935
  try {
833
- if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
834
- _iterator1.return();
936
+ if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
937
+ _iterator2.return();
835
938
  }
836
939
  } finally{
837
- if (_didIteratorError1) {
838
- throw _iteratorError1;
940
+ if (_didIteratorError2) {
941
+ throw _iteratorError2;
839
942
  }
840
943
  }
841
944
  }
@@ -919,6 +1022,7 @@ function runLifecycle(_0) {
919
1022
  case 24:
920
1023
  if (mode === 'run' && !options.event) throw new Error('run requires an APM event');
921
1024
  event = (_ref = (_options_event2 = options.event) === null || _options_event2 === void 0 ? void 0 : _options_event2.event) !== null && _ref !== void 0 ? _ref : 'post-install';
1025
+ installed = 0;
922
1026
  _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
923
1027
  _state.label = 25;
924
1028
  case 25:
@@ -929,7 +1033,7 @@ function runLifecycle(_0) {
929
1033
  32
930
1034
  ]);
931
1035
  _loop1 = function() {
932
- var policy, _policy_postupdate, command, matches, plugin, found, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, destination, key, validDigests, ready, err, error;
1036
+ var policy, _policy_postupdate, command, matches, plugin, id, recorded, found, error, gone, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, key, validDigests, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step2, key1, receipt, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step3, destination, receipt1, status, current, _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _iterator3, _step4, destination1, key2, wanted, receipt2, err, error1;
933
1037
  return _ts_generator(this, function(_state) {
934
1038
  switch(_state.label){
935
1039
  case 0:
@@ -952,49 +1056,100 @@ function runLifecycle(_0) {
952
1056
  ];
953
1057
  }
954
1058
  if (!matches.length) {
955
- output("skipped source not installed ".concat((0, _config.recordId)(policy)));
1059
+ if (mode === 'run') output("skipped source not installed ".concat((0, _config.recordId)(policy)));
956
1060
  return [
957
1061
  2,
958
1062
  "continue"
959
1063
  ];
960
1064
  }
1065
+ installed++;
961
1066
  plugin = matches[0];
1067
+ id = (0, _config.recordId)(policy);
962
1068
  _state.label = 1;
963
1069
  case 1:
964
1070
  _state.trys.push([
965
1071
  1,
966
- 14,
1072
+ 20,
967
1073
  ,
968
- 15
1074
+ 21
1075
+ ]);
1076
+ recorded = Object.keys(state.receipts).filter(function(key) {
1077
+ return receiptRecord(key) === id;
1078
+ });
1079
+ found = void 0;
1080
+ _state.label = 2;
1081
+ case 2:
1082
+ _state.trys.push([
1083
+ 2,
1084
+ 4,
1085
+ ,
1086
+ 5
969
1087
  ]);
970
1088
  return [
971
1089
  4,
972
1090
  destinations(plugin, policy, home, apmHome)
973
1091
  ];
974
- case 2:
975
- found = _state.sent();
976
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
977
- _state.label = 3;
978
1092
  case 3:
979
- _state.trys.push([
1093
+ found = _state.sent();
1094
+ return [
980
1095
  3,
981
- 11,
982
- 12,
983
- 13
984
- ]);
985
- _iterator = found[Symbol.iterator]();
986
- _state.label = 4;
1096
+ 5
1097
+ ];
987
1098
  case 4:
988
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
1099
+ error = _state.sent();
1100
+ // check still reports a copy that was recorded and is now gone
1101
+ if (mode === 'check' && recorded.length && (missing(error) || _instanceof(error, Error) && error.message.startsWith('no '))) found = [];
1102
+ else throw error;
1103
+ return [
989
1104
  3,
990
- 10
1105
+ 5
991
1106
  ];
992
- destination = _step.value;
993
- key = receiptKey(policy, destination);
994
- if (!(mode === 'check')) return [
1107
+ case 5:
1108
+ gone = recorded.filter(function(key) {
1109
+ return !(0, _nodefs.existsSync)(JSON.parse(key)[1]);
1110
+ });
1111
+ if (!(mode === 'run')) return [
995
1112
  3,
996
- 5
1113
+ 8
1114
+ ];
1115
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1116
+ try {
1117
+ // a copy that no longer exists loses its receipt, so the copy APM recreates in its place runs again
1118
+ for(_iterator = gone[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1119
+ key = _step.value;
1120
+ delete state.receipts[key];
1121
+ }
1122
+ } catch (err) {
1123
+ _didIteratorError = true;
1124
+ _iteratorError = err;
1125
+ } finally{
1126
+ try {
1127
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1128
+ _iterator.return();
1129
+ }
1130
+ } finally{
1131
+ if (_didIteratorError) {
1132
+ throw _iteratorError;
1133
+ }
1134
+ }
1135
+ }
1136
+ if (!gone.length) return [
1137
+ 3,
1138
+ 7
1139
+ ];
1140
+ return [
1141
+ 4,
1142
+ saveState(statePath, state)
1143
+ ];
1144
+ case 6:
1145
+ _state.sent();
1146
+ _state.label = 7;
1147
+ case 7:
1148
+ return [
1149
+ 3,
1150
+ 9
997
1151
  ];
1152
+ case 8:
998
1153
  validDigests = [
999
1154
  policy['post-install'],
1000
1155
  policy['post-update']
@@ -1003,82 +1158,160 @@ function runLifecycle(_0) {
1003
1158
  }).map(function(item) {
1004
1159
  return digest(policy, item);
1005
1160
  });
1006
- ready = validDigests.includes(state.receipts[key]);
1007
- output("".concat(ready ? 'ok' : 'missing', " ").concat((0, _config.recordId)(policy), " ").concat(destination));
1008
- if (!ready) failed++;
1161
+ _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
1162
+ try {
1163
+ for(_iterator1 = gone[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step2 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
1164
+ key1 = _step2.value;
1165
+ receipt = state.receipts[key1];
1166
+ if (validDigests.includes(receipt.digest) && receipt.revision === plugin.revision) {
1167
+ output("broken ".concat(id, " ").concat(JSON.parse(key1)[1]));
1168
+ failed++;
1169
+ }
1170
+ }
1171
+ } catch (err) {
1172
+ _didIteratorError1 = true;
1173
+ _iteratorError1 = err;
1174
+ } finally{
1175
+ try {
1176
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
1177
+ _iterator1.return();
1178
+ }
1179
+ } finally{
1180
+ if (_didIteratorError1) {
1181
+ throw _iteratorError1;
1182
+ }
1183
+ }
1184
+ }
1185
+ _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = undefined;
1186
+ try {
1187
+ for(_iterator2 = found[Symbol.iterator](); !(_iteratorNormalCompletion2 = (_step3 = _iterator2.next()).done); _iteratorNormalCompletion2 = true){
1188
+ destination = _step3.value;
1189
+ receipt1 = state.receipts[receiptKey(policy, destination)];
1190
+ status = 'missing';
1191
+ if (receipt1) {
1192
+ current = validDigests.includes(receipt1.digest) && receipt1.revision === plugin.revision;
1193
+ status = current ? 'ok' : 'stale';
1194
+ }
1195
+ output("".concat(status, " ").concat(id, " ").concat(destination));
1196
+ if (status !== 'ok') failed++;
1197
+ }
1198
+ } catch (err) {
1199
+ _didIteratorError2 = true;
1200
+ _iteratorError2 = err;
1201
+ } finally{
1202
+ try {
1203
+ if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
1204
+ _iterator2.return();
1205
+ }
1206
+ } finally{
1207
+ if (_didIteratorError2) {
1208
+ throw _iteratorError2;
1209
+ }
1210
+ }
1211
+ }
1009
1212
  return [
1213
+ 2,
1214
+ "continue"
1215
+ ];
1216
+ case 9:
1217
+ _iteratorNormalCompletion3 = true, _didIteratorError3 = false, _iteratorError3 = undefined;
1218
+ _state.label = 10;
1219
+ case 10:
1220
+ _state.trys.push([
1221
+ 10,
1222
+ 17,
1223
+ 18,
1224
+ 19
1225
+ ]);
1226
+ _iterator3 = found[Symbol.iterator]();
1227
+ _state.label = 11;
1228
+ case 11:
1229
+ if (!!(_iteratorNormalCompletion3 = (_step4 = _iterator3.next()).done)) return [
1010
1230
  3,
1011
- 9
1231
+ 16
1012
1232
  ];
1013
- case 5:
1014
- delete state.receipts[key];
1233
+ destination1 = _step4.value;
1234
+ key2 = receiptKey(policy, destination1);
1235
+ wanted = digest(policy, command);
1236
+ receipt2 = state.receipts[key2];
1237
+ if (receipt2 && receipt2.digest === wanted && receipt2.revision === plugin.revision) {
1238
+ output("current ".concat(id, " ").concat(destination1));
1239
+ return [
1240
+ 3,
1241
+ 15
1242
+ ];
1243
+ }
1244
+ delete state.receipts[key2];
1015
1245
  return [
1016
1246
  4,
1017
1247
  saveState(statePath, state)
1018
1248
  ];
1019
- case 6:
1249
+ case 12:
1020
1250
  _state.sent();
1021
1251
  return [
1022
1252
  4,
1023
- execute(command, destination, environment)
1253
+ execute(command, destination1, environment)
1024
1254
  ];
1025
- case 7:
1255
+ case 13:
1026
1256
  _state.sent();
1027
- state.receipts[key] = digest(policy, command);
1257
+ state.receipts[key2] = {
1258
+ digest: wanted,
1259
+ revision: plugin.revision
1260
+ };
1028
1261
  return [
1029
1262
  4,
1030
1263
  saveState(statePath, state)
1031
1264
  ];
1032
- case 8:
1265
+ case 14:
1033
1266
  _state.sent();
1034
- output("ran ".concat((0, _config.recordId)(policy), " ").concat(destination));
1035
- _state.label = 9;
1036
- case 9:
1037
- _iteratorNormalCompletion = true;
1267
+ output("ran ".concat(id, " ").concat(destination1));
1268
+ _state.label = 15;
1269
+ case 15:
1270
+ _iteratorNormalCompletion3 = true;
1038
1271
  return [
1039
1272
  3,
1040
- 4
1273
+ 11
1041
1274
  ];
1042
- case 10:
1275
+ case 16:
1043
1276
  return [
1044
1277
  3,
1045
- 13
1278
+ 19
1046
1279
  ];
1047
- case 11:
1280
+ case 17:
1048
1281
  err = _state.sent();
1049
- _didIteratorError = true;
1050
- _iteratorError = err;
1282
+ _didIteratorError3 = true;
1283
+ _iteratorError3 = err;
1051
1284
  return [
1052
1285
  3,
1053
- 13
1286
+ 19
1054
1287
  ];
1055
- case 12:
1288
+ case 18:
1056
1289
  try {
1057
- if (!_iteratorNormalCompletion && _iterator.return != null) {
1058
- _iterator.return();
1290
+ if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
1291
+ _iterator3.return();
1059
1292
  }
1060
1293
  } finally{
1061
- if (_didIteratorError) {
1062
- throw _iteratorError;
1294
+ if (_didIteratorError3) {
1295
+ throw _iteratorError3;
1063
1296
  }
1064
1297
  }
1065
1298
  return [
1066
1299
  7
1067
1300
  ];
1068
- case 13:
1301
+ case 19:
1069
1302
  return [
1070
1303
  3,
1071
- 15
1304
+ 21
1072
1305
  ];
1073
- case 14:
1074
- error = _state.sent();
1075
- output("failed ".concat((0, _config.recordId)(policy), ": ").concat(_instanceof(error, Error) ? error.message : String(error)));
1306
+ case 20:
1307
+ error1 = _state.sent();
1308
+ output("failed ".concat(id, ": ").concat(_instanceof(error1, Error) ? error1.message : String(error1)));
1076
1309
  failed++;
1077
1310
  return [
1078
1311
  3,
1079
- 15
1312
+ 21
1080
1313
  ];
1081
- case 15:
1314
+ case 21:
1082
1315
  return [
1083
1316
  2
1084
1317
  ];
@@ -1132,6 +1365,7 @@ function runLifecycle(_0) {
1132
1365
  7
1133
1366
  ];
1134
1367
  case 32:
1368
+ if (mode === 'check' && !installed && !failed) output('nothing installed');
1135
1369
  return [
1136
1370
  2,
1137
1371
  failed ? 1 : 0