dsh-agent-plugins-market 0.6.2 → 0.7.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.
Files changed (136) hide show
  1. package/README.md +117 -111
  2. package/README.zh.md +112 -98
  3. package/client/client.js +547 -540
  4. package/client/client.js.map +1 -1
  5. package/lib/application/catalog-context.js +181 -0
  6. package/lib/application/catalog.js +217 -1110
  7. package/lib/application/details.js +6 -6
  8. package/lib/application/install-store.js +95 -0
  9. package/lib/application/lsp-service.js +97 -0
  10. package/lib/application/mcp-service.js +246 -0
  11. package/lib/application/panel-resources.js +29 -12
  12. package/lib/application/ports.js +21 -0
  13. package/lib/application/project-agent-roles.js +1 -1
  14. package/lib/application/snapshot-cache.js +160 -0
  15. package/lib/application/source-store.js +521 -0
  16. package/lib/catalog/archive.js +34 -29
  17. package/lib/catalog/component-files.js +14 -6
  18. package/lib/catalog/fs-probes.js +43 -0
  19. package/lib/catalog/manifests.js +53 -92
  20. package/lib/catalog/native-project.js +2 -2
  21. package/lib/catalog/paths.js +66 -55
  22. package/lib/catalog/project-config.js +2 -1
  23. package/lib/catalog/project-hooks.js +9 -1
  24. package/lib/catalog/scan-resolvers.js +29 -45
  25. package/lib/catalog/skills-parse.js +14 -10
  26. package/lib/catalog/source-catalog.js +3 -2
  27. package/lib/catalog/suite-components.js +5 -5
  28. package/lib/catalog/suite-scanner.js +3 -1
  29. package/lib/catalog/surfaces.js +5 -26
  30. package/lib/catalog/validate.js +7 -11
  31. package/lib/contracts/market.js +1 -0
  32. package/lib/index.js +91 -208
  33. package/lib/model/types.js +26 -3
  34. package/lib/routes.js +45 -15
  35. package/lib/runtime/agent-role-names.js +70 -0
  36. package/lib/runtime/agent-role-router.js +116 -102
  37. package/lib/runtime/commands-mounts.js +9 -6
  38. package/lib/runtime/deadline.js +28 -0
  39. package/lib/runtime/feedback-tool.js +94 -43
  40. package/lib/runtime/hooks-mounts.js +9 -11
  41. package/lib/runtime/host-locale.js +10 -21
  42. package/lib/runtime/lsp-direct-config.js +19 -19
  43. package/lib/runtime/lsp-mounts.js +159 -68
  44. package/lib/runtime/lsp-server-state.js +4 -4
  45. package/lib/runtime/lsp-status.js +1 -1
  46. package/lib/runtime/mcp-auth-record.js +42 -0
  47. package/lib/runtime/mcp-backend.js +6 -5
  48. package/lib/runtime/mcp-client/config.js +0 -1
  49. package/lib/runtime/mcp-client/connection.js +32 -37
  50. package/lib/runtime/mcp-client/host-contract.js +10 -0
  51. package/lib/runtime/mcp-client/oauth.js +5 -7
  52. package/lib/runtime/mcp-client/plugin-identity.js +28 -0
  53. package/lib/runtime/mcp-client/projection.js +202 -0
  54. package/lib/runtime/mcp-client/tools.js +4 -189
  55. package/lib/runtime/mcp-config.js +29 -10
  56. package/lib/runtime/mcp-direct-config.js +33 -16
  57. package/lib/runtime/mcp-mounts.js +35 -75
  58. package/lib/runtime/mcp-overrides.js +5 -5
  59. package/lib/runtime/mcp-redaction.js +3 -2
  60. package/lib/runtime/mcp-status.js +7 -8
  61. package/lib/runtime/mount-lifecycle.js +73 -0
  62. package/lib/runtime/profile-seam.js +321 -0
  63. package/lib/runtime/project-runtime.js +11 -8
  64. package/lib/runtime/reconcile-scheduler.js +99 -0
  65. package/lib/runtime/reconciler.js +4 -0
  66. package/lib/runtime/server-config.js +6 -4
  67. package/lib/runtime/settings-namespace.js +136 -0
  68. package/lib/runtime/skills-provider.js +5 -3
  69. package/lib/runtime/source-auto-update.js +70 -0
  70. package/lib/{model/state.js → runtime/state-store.js} +9 -11
  71. package/lib/runtime/storage-migration.js +35 -18
  72. package/lib/runtime/subagent-catalog.js +5 -4
  73. package/lib/runtime/user-commands.js +2 -2
  74. package/lib/runtime/user-panels.js +31 -21
  75. package/lib/runtime/user-store.js +12 -13
  76. package/lib/types/application/catalog-context.d.ts +118 -0
  77. package/lib/types/application/catalog.d.ts +99 -304
  78. package/lib/types/application/install-store.d.ts +21 -0
  79. package/lib/types/application/lsp-service.d.ts +36 -0
  80. package/lib/types/application/mcp-service.d.ts +71 -0
  81. package/lib/types/application/panel-resources.d.ts +8 -0
  82. package/lib/types/application/ports.d.ts +72 -0
  83. package/lib/types/application/queries.d.ts +13 -26
  84. package/lib/types/application/snapshot-cache.d.ts +51 -0
  85. package/lib/types/application/source-store.d.ts +100 -0
  86. package/lib/types/catalog/archive.d.ts +3 -11
  87. package/lib/types/catalog/component-files.d.ts +4 -0
  88. package/lib/types/catalog/fs-probes.d.ts +10 -0
  89. package/lib/types/catalog/manifests.d.ts +17 -16
  90. package/lib/types/catalog/native-project.d.ts +2 -2
  91. package/lib/types/catalog/paths.d.ts +47 -15
  92. package/lib/types/catalog/scan-pipeline.d.ts +3 -3
  93. package/lib/types/catalog/scan-resolvers.d.ts +3 -9
  94. package/lib/types/catalog/skills-parse.d.ts +0 -7
  95. package/lib/types/catalog/source-catalog.d.ts +3 -3
  96. package/lib/types/catalog/suite-scanner.d.ts +8 -6
  97. package/lib/types/catalog/validate.d.ts +0 -2
  98. package/lib/types/contracts/lsp-status.d.ts +35 -0
  99. package/lib/types/contracts/market.d.ts +3 -2
  100. package/lib/types/contracts/mcp-status.d.ts +4 -0
  101. package/lib/types/model/types.d.ts +29 -5
  102. package/lib/types/runtime/agent-role-names.d.ts +6 -0
  103. package/lib/types/runtime/agent-role-router.d.ts +54 -51
  104. package/lib/types/runtime/deadline.d.ts +18 -0
  105. package/lib/types/runtime/feedback-tool.d.ts +33 -11
  106. package/lib/types/runtime/hooks-mounts.d.ts +1 -1
  107. package/lib/types/runtime/host-locale.d.ts +1 -1
  108. package/lib/types/runtime/lsp-direct-config.d.ts +3 -3
  109. package/lib/types/runtime/lsp-mounts.d.ts +62 -19
  110. package/lib/types/runtime/lsp-status.d.ts +2 -1
  111. package/lib/types/runtime/mcp-auth-record.d.ts +29 -0
  112. package/lib/types/runtime/mcp-backend.d.ts +7 -4
  113. package/lib/types/runtime/mcp-client/config.d.ts +0 -2
  114. package/lib/types/runtime/mcp-client/host-contract.d.ts +78 -0
  115. package/lib/types/runtime/mcp-client/oauth.d.ts +1 -1
  116. package/lib/types/runtime/mcp-client/plugin-identity.d.ts +16 -0
  117. package/lib/types/runtime/mcp-client/projection.d.ts +45 -0
  118. package/lib/types/runtime/mcp-client/tools.d.ts +5 -72
  119. package/lib/types/runtime/mcp-config.d.ts +25 -4
  120. package/lib/types/runtime/mcp-direct-config.d.ts +18 -4
  121. package/lib/types/runtime/mcp-mounts.d.ts +10 -15
  122. package/lib/types/runtime/mcp-overrides.d.ts +1 -1
  123. package/lib/types/runtime/mcp-status.d.ts +1 -1
  124. package/lib/types/runtime/mount-lifecycle.d.ts +53 -0
  125. package/lib/types/runtime/profile-seam.d.ts +64 -0
  126. package/lib/types/runtime/reconcile-scheduler.d.ts +45 -0
  127. package/lib/types/runtime/reconciler.d.ts +2 -0
  128. package/lib/types/runtime/settings-namespace.d.ts +76 -0
  129. package/lib/types/runtime/source-auto-update.d.ts +33 -0
  130. package/lib/types/{model/state.d.ts → runtime/state-store.d.ts} +2 -2
  131. package/lib/types/runtime/storage-migration.d.ts +5 -3
  132. package/lib/types/runtime/subagent-catalog.d.ts +2 -0
  133. package/lib/types/runtime/user-panels.d.ts +8 -11
  134. package/lib/types/runtime/user-store.d.ts +2 -4
  135. package/package.json +48 -21
  136. package/schemas/README.md +1 -1
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Discovery caching: one scan cache shared by every dimension, plus a
3
+ * freshness-bounded snapshot per dimension.
4
+ *
5
+ * Install / enable / surface toggles re-derive a snapshot from cached
6
+ * discovery (a cheap mapping) instead of re-walking every checkout — a full
7
+ * rescan of a 2,500-suite catalog costs ~1s and UI mutations happen in bursts.
8
+ * Content-changing mutations (add / update / remove / adopt / refresh /
9
+ * acquire) clear every cached discovery generation; the TTL bounds staleness
10
+ * for in-place working-tree edits of local sources.
11
+ */
12
+ import { join, resolve } from 'node:path';
13
+ import { resolveProjectRoot, STATE_FILE_NAME } from '../catalog/paths.js';
14
+ import { discoverSourceListWithNotes } from '../catalog/source-catalog.js';
15
+ import { loadState } from '../runtime/state-store.js';
16
+ export const SCAN_CACHE_TTL_MS = 30_000;
17
+ const SCAN_CACHE_MAX_ENTRIES = 8;
18
+ export class SnapshotCache {
19
+ host;
20
+ ttls;
21
+ scanCache = new Map();
22
+ scanPromises = new Map();
23
+ userSnapshot;
24
+ userSnapshotExpiresAt = 0;
25
+ userSnapshotPromise;
26
+ /** Project-dimension snapshots keyed by project root, with TTL freshness. */
27
+ projectSnapshots = new Map();
28
+ projectSnapshotPromises = new Map();
29
+ constructor(host, ttls) {
30
+ this.host = host;
31
+ this.ttls = ttls;
32
+ }
33
+ /** Read one coherent user-dimension snapshot, reusing in-flight discovery. */
34
+ async readUserCatalog() {
35
+ // TTL 0 disables snapshot caching: every read observes fresh discovery,
36
+ // mirroring the project dimension's caching-disabled semantics.
37
+ if (this.ttls.user <= 0)
38
+ return this.build(this.host.state, 'user', this.host.userRoot);
39
+ if (this.userSnapshot !== undefined && Date.now() < this.userSnapshotExpiresAt)
40
+ return this.userSnapshot;
41
+ if (this.userSnapshotPromise !== undefined)
42
+ return this.userSnapshotPromise;
43
+ const revision = this.host.revision;
44
+ const generation = this.host.scanGeneration;
45
+ const promise = this.build(this.host.state, 'user', this.host.userRoot)
46
+ .then(snapshot => {
47
+ if (revision === this.host.revision && generation === this.host.scanGeneration) {
48
+ this.userSnapshot = snapshot;
49
+ this.userSnapshotExpiresAt = Date.now() + this.ttls.user;
50
+ }
51
+ return snapshot;
52
+ })
53
+ .finally(() => {
54
+ if (this.userSnapshotPromise === promise)
55
+ this.userSnapshotPromise = undefined;
56
+ });
57
+ this.userSnapshotPromise = promise;
58
+ return promise;
59
+ }
60
+ /** Read one coherent project-dimension snapshot for a workspace cwd. */
61
+ async readProjectCatalog(cwd) {
62
+ const projectRoot = await resolveProjectRoot(cwd);
63
+ // A cwd with no `.git` ancestor resolves to itself as its own project root,
64
+ // so a session started in the harness home lands on the user dimension root.
65
+ // That directory is the user's catalog, not a project's: reading it would
66
+ // present every user-level suite as a project suite of that one session.
67
+ if (resolve(projectRoot) === resolve(this.host.userRoot)) {
68
+ return { revision: this.host.revision, sources: [], suites: [], enabledSuites: [] };
69
+ }
70
+ if (this.ttls.project <= 0)
71
+ return this.buildProjectSnapshot(projectRoot);
72
+ const cached = this.projectSnapshots.get(projectRoot);
73
+ if (cached !== undefined && cached.expiresAt > Date.now())
74
+ return cached.snapshot;
75
+ const inFlight = this.projectSnapshotPromises.get(projectRoot);
76
+ if (inFlight !== undefined)
77
+ return inFlight;
78
+ const revision = this.host.revision;
79
+ const generation = this.host.scanGeneration;
80
+ const promise = this.buildProjectSnapshot(projectRoot)
81
+ .then(snapshot => {
82
+ if (revision === this.host.revision && generation === this.host.scanGeneration) {
83
+ this.projectSnapshots.set(projectRoot, { snapshot, expiresAt: Date.now() + this.ttls.project });
84
+ }
85
+ return snapshot;
86
+ })
87
+ .finally(() => {
88
+ if (this.projectSnapshotPromises.get(projectRoot) === promise)
89
+ this.projectSnapshotPromises.delete(projectRoot);
90
+ });
91
+ this.projectSnapshotPromises.set(projectRoot, promise);
92
+ return promise;
93
+ }
94
+ /** One snapshot for a dimension root, reusing cached discovery when it is fresh. */
95
+ async build(state, dimension, dimensionRoot, skipScanCache = false) {
96
+ const fingerprint = JSON.stringify([dimension, dimensionRoot, state.sources, this.host.scanProjectLayouts]);
97
+ const cached = this.scanCache.get(fingerprint);
98
+ // The user snapshot's TTL also bounds scan reuse: a snapshot rebuild that
99
+ // replays a 30s scan cache would silently outlive its own staleness bound.
100
+ const scanCacheTtl = dimension === 'user' ? Math.min(this.ttls.user, SCAN_CACHE_TTL_MS) : SCAN_CACHE_TTL_MS;
101
+ const cacheFresh = !skipScanCache && cached !== undefined && Date.now() - cached.at < scanCacheTtl;
102
+ let discovered;
103
+ let scanNotes;
104
+ if (cacheFresh && cached !== undefined) {
105
+ discovered = cached.discovered;
106
+ scanNotes = cached.scanNotes;
107
+ }
108
+ else {
109
+ const generation = this.host.scanGeneration;
110
+ let pending = this.scanPromises.get(fingerprint);
111
+ if (pending === undefined) {
112
+ pending = discoverSourceListWithNotes(state.sources, dimension, dimensionRoot, this.host.scanProjectLayouts);
113
+ this.scanPromises.set(fingerprint, pending);
114
+ }
115
+ let result;
116
+ try {
117
+ result = await pending;
118
+ }
119
+ finally {
120
+ if (this.scanPromises.get(fingerprint) === pending)
121
+ this.scanPromises.delete(fingerprint);
122
+ }
123
+ discovered = result.suites;
124
+ scanNotes = result.scanNotes;
125
+ // A scan started before a content mutation must not repopulate its cache.
126
+ if (generation === this.host.scanGeneration) {
127
+ if (this.scanCache.size >= SCAN_CACHE_MAX_ENTRIES)
128
+ this.scanCache.clear();
129
+ this.scanCache.set(fingerprint, { at: Date.now(), discovered, scanNotes });
130
+ }
131
+ }
132
+ const suites = this.host.project(discovered, state, dimension);
133
+ return {
134
+ revision: this.host.revision,
135
+ sources: [...state.sources],
136
+ suites,
137
+ enabledSuites: suites.filter(suite => suite.enabled),
138
+ ...(Object.keys(scanNotes).length > 0 ? { scanNotes } : {})
139
+ };
140
+ }
141
+ /** Drop every cached dimension snapshot and its in-flight dedupe slot. */
142
+ invalidateSnapshots() {
143
+ this.userSnapshot = undefined;
144
+ this.userSnapshotExpiresAt = 0;
145
+ this.userSnapshotPromise = undefined;
146
+ this.projectSnapshots.clear();
147
+ this.projectSnapshotPromises.clear();
148
+ }
149
+ /** Drop cached discovery and its in-flight dedupe slots. */
150
+ invalidateScans() {
151
+ this.scanCache.clear();
152
+ this.scanPromises.clear();
153
+ }
154
+ async buildProjectSnapshot(projectRoot) {
155
+ const state = await loadState(join(projectRoot, STATE_FILE_NAME));
156
+ // TTL 0 means caching is disabled for this dimension: bypass the scan
157
+ // cache so every read observes the working tree as it stands.
158
+ return this.build(state, 'project', projectRoot, this.ttls.project <= 0);
159
+ }
160
+ }
@@ -0,0 +1,521 @@
1
+ /**
2
+ * Source acquisition and source CRUD.
3
+ *
4
+ * A source is a configured Git repository, archive URL, or local directory the
5
+ * catalog reads suites from. This module owns everything that touches the
6
+ * `.sources/` storage: adoption of pre-existing checkouts, id selection,
7
+ * clone/archive/download, refresh, removal, and the progress state the market
8
+ * page polls while a long acquisition runs.
9
+ */
10
+ import { mkdir, readdir, rename } from 'node:fs/promises';
11
+ import { join } from 'node:path';
12
+ import { archiveInstall } from '../catalog/archive.js';
13
+ import { gitClone, gitCurrentBranch, gitHead, gitRemoteUrl, gitRemove, gitSetRemoteUrl, gitSync } from '../catalog/git.js';
14
+ import { repoName } from '../catalog/manifests.js';
15
+ import { deriveSourceIdCandidates, expandHome, sanitizeId, sourceCheckoutDir, sourcesDir } from '../catalog/paths.js';
16
+ import { isDirectory, pathExists } from '../catalog/fs-probes.js';
17
+ import { canonicalGitUrl } from '../catalog/scan-resolvers.js';
18
+ import { readLocalePreference } from '../runtime/host-locale.js';
19
+ import { githubCloneUrl, resolveRegion } from '../runtime/regions.js';
20
+ import { resolveSourceKind } from '../model/types.js';
21
+ export class SourceStore {
22
+ context;
23
+ ports;
24
+ headCache = new Map();
25
+ /** Progress snapshot of the source mutation currently in flight. */
26
+ currentSourceState;
27
+ constructor(context, ports) {
28
+ this.context = context;
29
+ this.ports = ports;
30
+ }
31
+ /**
32
+ * Add a source and acquire it immediately (clone, download, or in-place).
33
+ *
34
+ * Git sources with an already-present checkout whose `origin` matches the
35
+ * input URL are adopted in place — the manual-clone repair path — so no
36
+ * second clone is made and no `-2` suffixed id is invented. Adopted
37
+ * checkouts are never deleted on source removal.
38
+ */
39
+ async add(input) {
40
+ return this.context.enqueue(async () => {
41
+ const kind = input.local === true ? 'local' : resolveSourceKind({ url: input.url, kind: input.kind });
42
+ if (kind === 'git') {
43
+ // Adoption first: a pre-existing checkout with a matching origin URL
44
+ // is registered as-is, skipping the clone entirely.
45
+ const adoptable = await this.findAdoptableCheckout(input.url, deriveSourceIdCandidates(input.url));
46
+ if (adoptable !== undefined) {
47
+ const source = {
48
+ id: adoptable,
49
+ url: input.url,
50
+ ...(input.branch === undefined ? {} : { branch: input.branch }),
51
+ kind: 'git',
52
+ adopted: true
53
+ };
54
+ const head = await tryHead(sourceCheckoutDir(this.context.userRoot, adoptable));
55
+ if (head !== undefined)
56
+ this.headCache.set(source.id, head);
57
+ await this.context.commit({ ...this.context.state, sources: [...this.context.state.sources, source] });
58
+ await this.context.notifyChanged();
59
+ return source;
60
+ }
61
+ }
62
+ const baseId = await this.pickSourceId(deriveSourceIdCandidates(input.url), kind === 'local');
63
+ const source = {
64
+ id: baseId,
65
+ url: input.url,
66
+ ...(input.branch === undefined ? {} : { branch: input.branch }),
67
+ ...(kind === 'local' ? { local: true } : { kind }),
68
+ ...(input.kind === 'archive' && input.sha256 !== undefined && input.sha256 !== '' ? { sha256: input.sha256 } : {})
69
+ };
70
+ let checkout = this.checkoutPath(source);
71
+ if (kind === 'local') {
72
+ if (!(await isDirectory(checkout)))
73
+ throw new Error(`local source directory ${checkout} is missing`);
74
+ }
75
+ else {
76
+ this.beginSourceState(baseId, kind === 'archive' ? 'downloading' : 'cloning', false);
77
+ try {
78
+ const lock = await this.acquire(source);
79
+ if (lock !== undefined)
80
+ this.headCache.set(source.id, lock);
81
+ }
82
+ catch (error) {
83
+ this.endSourceState();
84
+ throw error;
85
+ }
86
+ this.updateSourceStep('reading');
87
+ }
88
+ // The repo's own manifest names the source; move the checkout along so
89
+ // the registered id and its `.sources/` directory stay coherent. URL
90
+ // derived candidates stay as readable fallbacks before numeric suffixes.
91
+ const named = [...new Set([sanitizeId(await repoName(checkout)), ...deriveSourceIdCandidates(input.url)])];
92
+ const finalId = await this.pickSourceId(named, kind === 'local', kind === 'local' ? undefined : checkout);
93
+ if (kind !== 'local' && finalId !== baseId) {
94
+ const targetDir = sourceCheckoutDir(this.context.userRoot, finalId);
95
+ await rename(checkout, targetDir);
96
+ checkout = targetDir;
97
+ }
98
+ source.id = finalId;
99
+ if (resolveSourceKind(source) === 'git') {
100
+ const head = await tryHead(checkout);
101
+ if (head !== undefined)
102
+ this.headCache.set(source.id, head);
103
+ }
104
+ this.endSourceState();
105
+ await this.context.commit({ ...this.context.state, sources: [...this.context.state.sources, source] });
106
+ await this.context.notifyChanged();
107
+ return source;
108
+ });
109
+ }
110
+ /** Register one unmanaged `.sources/` checkout as a source without touching its files. */
111
+ async adopt(id) {
112
+ return this.context.enqueue(async () => {
113
+ if (this.context.state.sources.some(source => source.id === id))
114
+ throw new Error(`source "${id}" is already registered`);
115
+ // The id becomes one path segment under `.sources/`: only flat safe
116
+ // names qualify. Anything else (separators, `..`, leading dots) could
117
+ // traverse out of the checkouts root and register an outside directory.
118
+ if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(id) || id.includes('..')) {
119
+ throw new Error(`invalid checkout id "${id}" — use letters, digits, dots, dashes, or underscores`);
120
+ }
121
+ const dir = sourceCheckoutDir(this.context.userRoot, id);
122
+ if (!(await isDirectory(dir)))
123
+ throw new Error(`no checkout directory at ${dir}`);
124
+ let source;
125
+ try {
126
+ const origin = await gitRemoteUrl(dir);
127
+ source = { id, url: origin, kind: 'git', adopted: true };
128
+ const head = await tryHead(dir);
129
+ if (head !== undefined)
130
+ this.headCache.set(id, head);
131
+ }
132
+ catch {
133
+ // Not a git checkout: read it in place, like a local source.
134
+ source = { id, url: dir, local: true, adopted: true };
135
+ }
136
+ await this.context.commit({ ...this.context.state, sources: [...this.context.state.sources, source] });
137
+ await this.context.notifyChanged();
138
+ return source;
139
+ });
140
+ }
141
+ /** Unmanaged `.sources/` checkouts: present on disk, absent from state. */
142
+ async unmanaged() {
143
+ const checkoutRoot = sourcesDir(this.context.userRoot);
144
+ let entries;
145
+ try {
146
+ entries = await readdir(checkoutRoot, { withFileTypes: true });
147
+ }
148
+ catch {
149
+ return [];
150
+ }
151
+ const registered = new Set(this.context.state.sources.map(source => source.id));
152
+ const unmanaged = [];
153
+ for (const entry of entries) {
154
+ if (!entry.isDirectory() || entry.name.startsWith('.') || registered.has(entry.name))
155
+ continue;
156
+ const dir = join(checkoutRoot, entry.name);
157
+ try {
158
+ unmanaged.push({ id: entry.name, url: await gitRemoteUrl(dir) });
159
+ }
160
+ catch {
161
+ unmanaged.push({ id: entry.name });
162
+ }
163
+ }
164
+ return unmanaged;
165
+ }
166
+ /** Update one source's URL, branch, kind, or archive digest. */
167
+ async update(sourceId, patch) {
168
+ return this.context.enqueue(async () => {
169
+ const index = this.context.state.sources.findIndex(source => source.id === sourceId);
170
+ const current = this.context.state.sources[index];
171
+ if (current === undefined)
172
+ throw new Error(`unknown source "${sourceId}"`);
173
+ const nextLocal = patch.local !== undefined ? patch.local : current.local === true;
174
+ // A local source carries the legacy flag only; `kind` stays unwritten
175
+ // so the persisted shape matches the pre-kind records.
176
+ const nextKind = nextLocal ? undefined : (patch.kind ?? current.kind);
177
+ const next = {
178
+ id: sourceId,
179
+ url: patch.url ?? current.url,
180
+ ...(patch.branch !== undefined ? { branch: patch.branch } : current.branch === undefined ? {} : { branch: current.branch }),
181
+ ...(nextLocal ? { local: true } : {}),
182
+ ...(nextKind === undefined ? {} : { kind: nextKind }),
183
+ ...(patch.sha256 !== undefined ? (patch.sha256 === '' ? {} : { sha256: patch.sha256 }) : current.sha256 === undefined ? {} : { sha256: current.sha256 }),
184
+ ...(current.adopted === true ? { adopted: true } : {})
185
+ };
186
+ const urlChanged = patch.url !== undefined && patch.url !== current.url;
187
+ // A URL change invalidates the old checkout — except for adopted (user
188
+ // cloned) and local sources, whose directories we never own.
189
+ if (urlChanged && current.local !== true && current.adopted !== true) {
190
+ this.headCache.delete(sourceId);
191
+ await gitRemove(sourceCheckoutDir(this.context.userRoot, sourceId));
192
+ }
193
+ await this.context.commit({ ...this.context.state, sources: this.context.state.sources.map((source, i) => (i === index ? next : source)) });
194
+ await this.context.notifyChanged();
195
+ });
196
+ }
197
+ /**
198
+ * Remove a source and its install entries. `deleteCheckout` (the UI's
199
+ * "also delete the managed market directory" option) physically removes the
200
+ * `.sources/<id>` checkout — including an adopted one, since a directory
201
+ * under `.sources` is manager-owned storage. A `local` source whose URL
202
+ * points outside `.sources` is registration-only: its directory is never
203
+ * touched. Deleting the checkout is what keeps a removed source from
204
+ * reappearing as an unmanaged entry.
205
+ */
206
+ async remove(sourceId, deleteCheckout = false) {
207
+ return this.context.enqueue(async () => {
208
+ const source = this.context.state.sources.find(entry => entry.id === sourceId);
209
+ await this.context.commit({
210
+ ...this.context.state,
211
+ sources: this.context.state.sources.filter(entry => entry.id !== sourceId),
212
+ installed: Object.fromEntries(Object.entries(this.context.state.installed).filter(([key]) => !key.startsWith(`${sourceId}/`)))
213
+ });
214
+ this.headCache.delete(sourceId);
215
+ // A checkout under `.sources` is manager-owned even when it was first
216
+ // discovered/adopted. Only an explicit local path points outside the
217
+ // manager's storage and must remain untouched.
218
+ const externalLocal = source?.local === true && source.url !== sourceCheckoutDir(this.context.userRoot, sourceId);
219
+ if (!externalLocal && deleteCheckout)
220
+ await gitRemove(sourceCheckoutDir(this.context.userRoot, sourceId));
221
+ await this.context.notifyChanged();
222
+ });
223
+ }
224
+ /** Refresh one source checkout, or every source when sourceId is omitted. */
225
+ async refresh(sourceId) {
226
+ return this.context.enqueue(async () => {
227
+ const targets = sourceId === undefined ? this.context.state.sources : this.context.state.sources.filter(source => source.id === sourceId);
228
+ for (const source of targets) {
229
+ const kind = resolveSourceKind(source);
230
+ if (kind === 'local') {
231
+ if (!(await isDirectory(expandHome(source.url))))
232
+ throw new Error(`local source directory ${expandHome(source.url)} is missing`);
233
+ continue;
234
+ }
235
+ const checkout = sourceCheckoutDir(this.context.userRoot, source.id);
236
+ this.headCache.delete(source.id);
237
+ if (kind === 'archive') {
238
+ // Re-download and swap; the fresh digest becomes the lock value.
239
+ const lock = await this.acquire(source);
240
+ if (lock !== undefined)
241
+ this.headCache.set(source.id, lock);
242
+ continue;
243
+ }
244
+ // Adopted checkouts are user-owned working trees: no `reset --hard`
245
+ // (it would destroy uncommitted work), and no re-acquire either —
246
+ // a broken HEAD means the user repairs the checkout, not that the
247
+ // manager may clone or extract over their directory.
248
+ if (source.adopted === true) {
249
+ try {
250
+ this.headCache.set(source.id, await gitHead(checkout));
251
+ }
252
+ catch {
253
+ // A failed HEAD read means the next overview re-probes.
254
+ }
255
+ continue;
256
+ }
257
+ try {
258
+ await gitHead(checkout);
259
+ }
260
+ catch {
261
+ const lock = await this.acquire(source);
262
+ if (lock !== undefined) {
263
+ this.headCache.set(source.id, lock);
264
+ }
265
+ else {
266
+ try {
267
+ this.headCache.set(source.id, await gitHead(checkout));
268
+ }
269
+ catch {
270
+ // A failed HEAD read means the next overview re-probes.
271
+ }
272
+ }
273
+ continue;
274
+ }
275
+ // Shallow-friendly sync: fetch depth 1 into FETCH_HEAD, hard reset.
276
+ const branch = source.branch ?? (await gitCurrentBranch(checkout).catch(() => undefined));
277
+ // Keep origin on the region's route: a clone made before a region
278
+ // switch (or an explicit flip) re-points origin so the fetch follows.
279
+ // Only origins this plugin itself wrote (the direct or proxied
280
+ // source.url) are re-pointed — a foreign remote is left untouched.
281
+ if (source.kind === 'git' || (source.kind === undefined && source.local !== true)) {
282
+ try {
283
+ const region = resolveRegion(await this.ports.downloadRegion(), await readLocalePreference());
284
+ const routed = githubCloneUrl(region, source.url);
285
+ const origin = await gitRemoteUrl(checkout);
286
+ if (origin !== routed && (origin === source.url || origin === githubCloneUrl('china', source.url))) {
287
+ await gitSetRemoteUrl(checkout, routed);
288
+ }
289
+ }
290
+ catch {
291
+ // Origin reconciliation is best effort; plain sync still runs.
292
+ }
293
+ }
294
+ await gitSync(checkout, branch, this.context.git);
295
+ try {
296
+ this.headCache.set(source.id, await gitHead(checkout));
297
+ }
298
+ catch {
299
+ // A failed HEAD read means the next overview re-probes.
300
+ }
301
+ }
302
+ await this.context.notifyChanged();
303
+ });
304
+ }
305
+ /** Progress snapshot for the progress route. */
306
+ progress() {
307
+ const state = this.currentSourceState;
308
+ return state === undefined ? { active: false, sourceId: '', step: '' } : { active: true, sourceId: state.sourceId, step: state.step };
309
+ }
310
+ /**
311
+ * Checkout status of one source for the market overview. A mutation owning
312
+ * the source right now short-circuits the probe: a Git read raced against
313
+ * that mutation's checkout would report a half-swapped directory.
314
+ */
315
+ async describeCheckout(source) {
316
+ const inFlight = this.currentSourceState?.sourceId === source.id ? this.currentSourceState : undefined;
317
+ const checkout = this.checkoutPath(source);
318
+ const kind = resolveSourceKind(source);
319
+ let cloned = false;
320
+ let lockCommit;
321
+ let error;
322
+ if (kind === 'local') {
323
+ cloned = await isDirectory(checkout);
324
+ if (!cloned)
325
+ error = `local source directory ${checkout} is missing`;
326
+ }
327
+ else if (kind === 'archive') {
328
+ cloned = await isDirectory(checkout);
329
+ lockCommit = this.headCache.get(source.id);
330
+ }
331
+ else if (inFlight !== undefined) {
332
+ // A mutation owns this source right now: do not race git against its checkout.
333
+ cloned = inFlight.cloned || (await isDirectory(checkout));
334
+ lockCommit = inFlight.head;
335
+ }
336
+ else {
337
+ const cachedHead = this.headCache.get(source.id);
338
+ if (cachedHead !== undefined && (await isDirectory(checkout))) {
339
+ cloned = true;
340
+ lockCommit = cachedHead;
341
+ }
342
+ else {
343
+ try {
344
+ lockCommit = await gitHead(checkout);
345
+ cloned = true;
346
+ this.headCache.set(source.id, lockCommit);
347
+ }
348
+ catch {
349
+ // Not cloned yet or a broken checkout; refresh reports the actionable error.
350
+ }
351
+ }
352
+ }
353
+ return { cloned, ...(lockCommit === undefined ? {} : { lockCommit }), ...(error === undefined ? {} : { error }) };
354
+ }
355
+ /** Per-source rows of the market overview for one user-dimension snapshot. */
356
+ async overviewRows(snapshot) {
357
+ const rows = [];
358
+ for (const source of snapshot.sources) {
359
+ const status = await this.describeCheckout(source);
360
+ const sourceSuites = snapshot.suites.filter(suite => suite.sourceId === source.id);
361
+ const scanNotes = snapshot.scanNotes?.[source.id];
362
+ rows.push({
363
+ id: source.id,
364
+ url: source.url,
365
+ ...(source.branch === undefined ? {} : { branch: source.branch }),
366
+ ...(source.local === true ? { local: true } : {}),
367
+ kind: resolveSourceKind(source),
368
+ ...(source.adopted === true ? { adopted: true } : {}),
369
+ cloned: status.cloned,
370
+ ...(status.lockCommit === undefined ? {} : { lockCommit: status.lockCommit }),
371
+ ...(status.error === undefined ? {} : { error: status.error }),
372
+ ...(scanNotes === undefined ? {} : { scanNotes }),
373
+ suiteIds: sourceSuites.map(suite => suite.id)
374
+ });
375
+ }
376
+ return rows;
377
+ }
378
+ /**
379
+ * Acquire one source's content by kind: clone (git), download+extract
380
+ * (archive), or an in-place existence check (local). Returns a lock value
381
+ * when the acquisition yields one (archive SHA-256, tarball fallback
382
+ * digest); git HEAD is read separately by the callers.
383
+ */
384
+ async acquire(source) {
385
+ const kind = resolveSourceKind(source);
386
+ if (kind === 'local') {
387
+ if (!(await isDirectory(expandHome(source.url))))
388
+ throw new Error(`local source directory ${expandHome(source.url)} is missing`);
389
+ return undefined;
390
+ }
391
+ const checkout = sourceCheckoutDir(this.context.userRoot, source.id);
392
+ await mkdir(sourcesDir(this.context.userRoot), { recursive: true });
393
+ if (kind === 'archive') {
394
+ const { sha256 } = await archiveInstall(source.url, checkout, {
395
+ ...(source.sha256 === undefined ? {} : { sha256: source.sha256 }),
396
+ ...(this.context.git.timeoutMs === undefined ? {} : { timeoutMs: this.context.git.timeoutMs }),
397
+ ...(this.context.git.allowHttpArchives === true ? { allowHttp: true } : {})
398
+ });
399
+ return sha256;
400
+ }
401
+ try {
402
+ // The region routes github.com clones through the China mirror prefix;
403
+ // the proxied URL becomes `origin`, so refreshes follow the same route.
404
+ const region = resolveRegion(await this.ports.downloadRegion(), await readLocalePreference());
405
+ await gitClone(githubCloneUrl(region, source.url), source.branch, checkout, this.context.git);
406
+ return undefined;
407
+ }
408
+ catch (error) {
409
+ if (this.context.git.fallbackTarball !== true)
410
+ throw error;
411
+ const tarballUrl = codeloadTarballUrl(source.url, source.branch);
412
+ if (tarballUrl === undefined)
413
+ throw error;
414
+ // The codeload URL ends in a branch path, not `.tar.gz`, so the format
415
+ // must be stated explicitly — extension detection cannot see it.
416
+ const { sha256 } = await archiveInstall(tarballUrl, checkout, {
417
+ ...(this.context.git.timeoutMs === undefined ? {} : { timeoutMs: this.context.git.timeoutMs }),
418
+ format: 'targz'
419
+ });
420
+ return sha256;
421
+ }
422
+ }
423
+ /** The filesystem location of one source. */
424
+ checkoutPath(source) {
425
+ return source.local === true ? expandHome(source.url) : sourceCheckoutDir(this.context.userRoot, source.id);
426
+ }
427
+ /** The last HEAD this store observed for one source, when it cached one. */
428
+ knownHead(sourceId) {
429
+ return this.headCache.get(sourceId);
430
+ }
431
+ /**
432
+ * First candidate id whose `.sources/` checkout already exists and whose
433
+ * `origin` remote matches the input URL (canonical-form equality) — the
434
+ * signature of a manual clone of the very same repository. Registered ids
435
+ * and checkouts of differently-origined directories never adopt.
436
+ */
437
+ async findAdoptableCheckout(url, candidates) {
438
+ const registered = new Set(this.context.state.sources.map(source => source.id));
439
+ for (const candidate of candidates) {
440
+ if (registered.has(candidate))
441
+ continue;
442
+ const dir = sourceCheckoutDir(this.context.userRoot, candidate);
443
+ if (!(await isDirectory(dir)))
444
+ continue;
445
+ let origin;
446
+ try {
447
+ origin = await gitRemoteUrl(dir);
448
+ }
449
+ catch {
450
+ continue;
451
+ }
452
+ if (origin !== '' && canonicalGitUrl(origin) === canonicalGitUrl(url))
453
+ return candidate;
454
+ }
455
+ return undefined;
456
+ }
457
+ /**
458
+ * First candidate id not claimed by a registered source; for git candidates
459
+ * the checkout directory must also be free, so a stale or foreign directory
460
+ * under `.sources/` is never cloned over (it would fail `git clone` with a
461
+ * confusing "destination exists" error). `ownedCheckout` marks the directory
462
+ * the in-flight mutation already controls, keeping a rename onto it legal.
463
+ * Local sources skip the disk check: they read their own path in place and
464
+ * never occupy `.sources/`.
465
+ */
466
+ async pickSourceId(candidates, skipDisk, ownedCheckout) {
467
+ const free = async (id) => {
468
+ if (this.context.state.sources.some(source => source.id === id))
469
+ return false;
470
+ if (skipDisk)
471
+ return true;
472
+ const dir = sourceCheckoutDir(this.context.userRoot, id);
473
+ return dir === ownedCheckout || !(await pathExists(dir));
474
+ };
475
+ for (const candidate of candidates) {
476
+ if (await free(candidate))
477
+ return candidate;
478
+ }
479
+ for (let suffix = 2;; suffix++) {
480
+ const candidate = `${candidates[0]}-${suffix}`;
481
+ if (await free(candidate))
482
+ return candidate;
483
+ }
484
+ }
485
+ /** Begin reporting progress for a source mutation. */
486
+ beginSourceState(sourceId, step, cloned) {
487
+ this.currentSourceState = { sourceId, step, cloned };
488
+ }
489
+ /** Advance the in-flight source mutation step. */
490
+ updateSourceStep(step) {
491
+ if (this.currentSourceState !== undefined)
492
+ this.currentSourceState = { ...this.currentSourceState, step };
493
+ }
494
+ /** Stop reporting source mutation progress. */
495
+ endSourceState() {
496
+ this.currentSourceState = undefined;
497
+ }
498
+ }
499
+ /** Read a checkout's HEAD when it is a Git repository. */
500
+ export async function tryHead(dir) {
501
+ try {
502
+ return await gitHead(dir);
503
+ }
504
+ catch {
505
+ return undefined;
506
+ }
507
+ }
508
+ /**
509
+ * The codeload tarball URL of a GitHub repository, or undefined for any
510
+ * other host. `https://github.com/<owner>/<repo>` maps to
511
+ * `https://codeload.github.com/<owner>/<repo>/tar.gz/refs/heads/<branch>`
512
+ * (or `/tar.gz/HEAD` for the default branch) — the git-protocol fallback
513
+ * when a clone cannot get through but plain HTTPS file download can.
514
+ */
515
+ export function codeloadTarballUrl(url, branch) {
516
+ const match = /^https:\/\/github\.com\/([\w.-]+)\/([\w.-]+?)(?:\.git)?\/?$/i.exec(url.trim());
517
+ if (match === null)
518
+ return undefined;
519
+ const [, owner, repo] = match;
520
+ return `https://codeload.github.com/${owner}/${repo}/tar.gz/${branch === undefined ? 'HEAD' : `refs/heads/${branch}`}`;
521
+ }