resolve.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * `resolveExtensions`: Expand `{ base, additions, overlays, profile }` during construction into the
  3. * final load list (whose order is handler execution order).
  4. *
  5. * Algorithm (aligned with design document §4.4 + ADR §Consequence 4), in fixed order:
  6. * 0. Build an id → extension pool from base + additions, checking id uniqueness.
  7. * 1. Start with base.
  8. * 2. `disable`: Remove from base; the id must exist in base.
  9. * 3. `enable`: Validate each enabled id for conflicts (it cannot also appear in disable, per spec
  10. * §4.4:332 `if (overlay.disable?.includes(id)) throw ...`), then select from the additions pool.
  11. * Append in additions-array order to keep relative order stable for multiple enabled entries in
  12. * the same profile (§4.4 line 329).
  13. * 4. `replace`: Map target id → replacement id, preserving position.
  14. *
  15. * Throw for all unresolved ids (fail fast during construction; never silently discard). If the
  16. * profile has no overlay, return all of base so hosts without overlays still work.
  17. */
  18. import type { AgentExtension, ProfileOverlay } from './types.js';
  19. export function resolveExtensions(
  20. base: readonly AgentExtension[],
  21. additions: readonly AgentExtension[],
  22. overlays: Readonly<Record<string, ProfileOverlay>>,
  23. profile: string | undefined,
  24. ): AgentExtension[] {
  25. const pool = new Map<string, AgentExtension>();
  26. for (const ext of [...base, ...additions]) {
  27. if (pool.has(ext.id)) {
  28. throw new Error(
  29. `resolveExtensions: duplicate extension id in base+additions pool: '${ext.id}'`,
  30. );
  31. }
  32. pool.set(ext.id, ext);
  33. }
  34. const overlay = profile ? overlays[profile] : undefined;
  35. if (!overlay) return [...base];
  36. const disableIds = new Set(overlay.disable ?? []);
  37. // 1. Start with base.
  38. let result: AgentExtension[] = [...base];
  39. // 2. disable: Remove from base; the id must exist in base.
  40. for (const id of disableIds) {
  41. if (!base.some((e) => e.id === id)) {
  42. throw new Error(`resolveExtensions: overlay '${profile}' disables id not in base: '${id}'`);
  43. }
  44. result = result.filter((e) => e.id !== id);
  45. }
  46. // 3. enable: Two passes: validate 3a+3b over enabled ids, then append in additions order for 3c.
  47. // 3a. Conflict: An id cannot also be disabled (explicitly part of enable in spec §4.4:332).
  48. // 3b. Missing: The id must exist in the additions pool; never silently discard it.
  49. // 3c. Append: Push matching ids in additions-array order to preserve relative order.
  50. const enableSet = new Set(overlay.enable ?? []);
  51. for (const id of enableSet) {
  52. if (disableIds.has(id)) {
  53. throw new Error(`resolveExtensions: overlay '${profile}' both enables and disables '${id}'`);
  54. }
  55. if (!additions.some((e) => e.id === id)) {
  56. throw new Error(
  57. `resolveExtensions: overlay '${profile}' enables id not in additions: '${id}'`,
  58. );
  59. }
  60. }
  61. for (const ext of additions) {
  62. if (enableSet.has(ext.id)) result.push(ext);
  63. }
  64. // 4. replace: Map target id → replacement id, preserving position. All replacements locate
  65. // targets in the same pre-replacement list so earlier replacements cannot disrupt swaps.
  66. const replacements = new Map<string, AgentExtension>();
  67. for (const [targetId, replaceId] of Object.entries(overlay.replace ?? {})) {
  68. if (!result.some((extension) => extension.id === targetId)) {
  69. throw new Error(
  70. `resolveExtensions: overlay '${profile}' replaces non-existent target: '${targetId}'`,
  71. );
  72. }
  73. const replaceExt = pool.get(replaceId);
  74. if (!replaceExt) {
  75. throw new Error(
  76. `resolveExtensions: overlay '${profile}' replace source not in pool: '${replaceId}'`,
  77. );
  78. }
  79. replacements.set(targetId, replaceExt);
  80. }
  81. result = result.map((extension) => replacements.get(extension.id) ?? extension);
  82. const finalIds = new Set<string>();
  83. for (const extension of result) {
  84. if (finalIds.has(extension.id)) {
  85. throw new Error(
  86. `resolveExtensions: duplicate extension id in final load list: '${extension.id}'`,
  87. );
  88. }
  89. finalIds.add(extension.id);
  90. }
  91. return result;
  92. }