@lanes-sh/link 0.6.0 → 0.6.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lanes-sh/link",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "A self-hostable MCP gateway for all your connections, memory, tasks, files, and secrets",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://lanes.sh/link",
@@ -0,0 +1,160 @@
1
+ import { ConfigError, layout, isRemoteWorkspace, type LegacyTarget, type WorkspaceTarget } from '#profile';
2
+ import { deployedWorkspace } from '#deployments/upload.ts';
3
+
4
+ /**
5
+ * What one contract-1 target block becomes, and where.
6
+ *
7
+ * Apart from `workspace-migrate.ts`, which reads and writes: this is the
8
+ * decision, and it is the half that has been wrong twice. Both times the shape
9
+ * was right and the *perspective* was not — the same `cloud:` block is a pointer
10
+ * seen from a laptop and a declaration seen from inside the bucket, and a
11
+ * migration that cannot tell which end it is on writes `gs://b` pointing at
12
+ * `gs://b`.
13
+ *
14
+ * So the workspace being migrated is an argument to every function here, and the
15
+ * tests drive them directly.
16
+ */
17
+
18
+ /**
19
+ * Every profile's target blocks, folded into one registry.
20
+ *
21
+ * Two refusals rather than guesses, because both are cases where one target
22
+ * would need two different answers and picking either silently is the class of
23
+ * bug this whole change is about.
24
+ */
25
+ export async function hoist(
26
+ legacy: readonly { profile: string; targets: Record<string, LegacyTarget> }[],
27
+ workspaceRoot: string,
28
+ ): Promise<Record<string, WorkspaceTarget>> {
29
+ const registry: Record<string, WorkspaceTarget> = {};
30
+ const seenFrom: Record<string, string> = {};
31
+
32
+ for (const { profile, targets } of legacy) {
33
+ for (const [name, declared] of Object.entries(targets)) {
34
+ const entry = toEntry(name, declared, profile, workspaceRoot);
35
+ if (entry === null) continue;
36
+ const previous = registry[name];
37
+
38
+ if (previous === undefined) {
39
+ registry[name] = entry;
40
+ seenFrom[name] = profile;
41
+ continue;
42
+ }
43
+
44
+ if (JSON.stringify(previous) !== JSON.stringify(entry)) {
45
+ throw new ConfigError(
46
+ `Profiles "${seenFrom[name]}" and "${profile}" both declare target "${name}", and they ` +
47
+ `do not agree.\n` +
48
+ ` A target is declared once by the workspace it lives in (ADR-052), so one of these\n` +
49
+ ` has to win and this cannot pick.\n\n` +
50
+ ` ${seenFrom[name]}: ${summarise(previous)}\n` +
51
+ ` ${profile}: ${summarise(entry)}\n\n` +
52
+ ` Edit one of them to match the other, then run this again.`,
53
+ );
54
+ }
55
+ }
56
+ }
57
+
58
+ return registry;
59
+ }
60
+
61
+ /**
62
+ * One contract-1 target block as a registry entry.
63
+ *
64
+ * A block whose storage names a bucket becomes a pointer: that bucket is a
65
+ * workspace, it holds the profiles served there, and under ADR-052 it is the
66
+ * thing that declares the target. The adapters are not copied into the pointer —
67
+ * they travel to the bucket on the next `deploy`, which is the only command that
68
+ * can write there and roll an image that understands what it wrote.
69
+ *
70
+ * Per-profile paths are dropped when they are the layout defaults, which is the
71
+ * ordinary case: `./data/<profile>` and `./data/<profile>/credentials.enc` are
72
+ * exactly what `layout.ts` derives, so a workspace-level target that omits them
73
+ * addresses the same bytes. A genuinely custom path cannot be hoisted — one
74
+ * target cannot hold a different path per profile — and is refused by name.
75
+ */
76
+ export function toEntry(
77
+ name: string,
78
+ declared: LegacyTarget,
79
+ profile: string,
80
+ workspaceRoot: string,
81
+ ): WorkspaceTarget | null {
82
+ const remote = deployedWorkspace(declared);
83
+
84
+ // **A target whose bucket is the workspace being migrated declares itself.**
85
+ //
86
+ // Migrating happens on both ends, and the second one is inside the bucket: the
87
+ // profile there carries the same `cloud` block, and deriving a pointer from it
88
+ // produces `gs://b` pointing at `gs://b`. That is a loop, and `openTarget`
89
+ // refuses it — which made `deploy` unable to run against the bucket it had
90
+ // just migrated, on the one command the refusal names as the fix.
91
+ if (remote && remote !== workspaceRoot) return { workspace: remote };
92
+
93
+ // **A filesystem target is dropped from a remote workspace.**
94
+ //
95
+ // The bucket's copy of a profile was uploaded from a laptop, so it carries
96
+ // that laptop's `local:` block — paths under `./data/` that address a disk the
97
+ // endpoint has never seen. Hoisting it would leave the bucket declaring a
98
+ // target it can never open, and `workspacePath` refuses that combination the
99
+ // moment anything tries.
100
+ //
101
+ // Nothing is lost: the machine that owns `local` has its own copy, and that is
102
+ // the one that was ever real.
103
+ if (isRemoteWorkspace(workspaceRoot) && declared.storage.adapter === 'filesystem') return null;
104
+
105
+ const storagePath = declared.storage.path;
106
+ const credentialsPath = declared.credentials.path;
107
+
108
+ const defaultStorage = `./${layout.blobs(profile)}`;
109
+ const defaultCredentials = `./${layout.credentials(profile)}`;
110
+
111
+ refuseCustomPath(name, profile, workspaceRoot, 'storage.path', storagePath, defaultStorage);
112
+ refuseCustomPath(
113
+ name,
114
+ profile,
115
+ workspaceRoot,
116
+ 'credentials.path',
117
+ credentialsPath,
118
+ defaultCredentials,
119
+ );
120
+
121
+ const { path: _storage, ...storage } = declared.storage;
122
+ const { path: _credentials, ...credentials } = declared.credentials;
123
+
124
+ return {
125
+ credentials,
126
+ storage,
127
+ ...(declared.audit ? { audit: declared.audit } : {}),
128
+ ...(declared.vault ? { vault: declared.vault } : {}),
129
+ ...(declared.deploy ? { deploy: declared.deploy } : {}),
130
+ };
131
+ }
132
+
133
+ function refuseCustomPath(
134
+ target: string,
135
+ profile: string,
136
+ workspaceRoot: string,
137
+ field: string,
138
+ value: string | undefined,
139
+ expected: string,
140
+ ): void {
141
+ if (value === undefined) return;
142
+ // Both spellings of the same directory: `./data/personal` and `data/personal`
143
+ // resolve identically and the template has written each at different times.
144
+ if (value === expected || value === expected.replace(/^\.\//, '')) return;
145
+
146
+ throw new ConfigError(
147
+ `Profile "${profile}" declares target "${target}" with a custom ${field}:\n` +
148
+ ` ${value}\n` +
149
+ ` A target is declared once for the whole workspace now (ADR-052), so it cannot hold a\n` +
150
+ ` different path per profile. The default it would get is ${expected}.\n\n` +
151
+ ` Move the data there and delete the line, or keep this profile in a workspace of its\n` +
152
+ ` own: LANES_LINK_HOME=${workspaceRoot}/<somewhere> lanes link profile add ${profile} --target ${target}`,
153
+ );
154
+ }
155
+
156
+ export function summarise(entry: WorkspaceTarget): string {
157
+ if (entry.workspace !== undefined) return `points at ${entry.workspace}`;
158
+ const parts = [entry.credentials?.adapter, entry.storage?.adapter].filter(Boolean);
159
+ return `${parts.join(' + ')}${entry.deploy ? `, deploys ${entry.deploy.service}` : ''}`;
160
+ }
@@ -6,6 +6,7 @@ import {
6
6
  layout,
7
7
  listProfiles,
8
8
  readWorkspaceFile,
9
+ isRemoteWorkspace,
9
10
  workspaceFiles,
10
11
  isLegacyProfile,
11
12
  legacyConfigSchema,
@@ -13,8 +14,8 @@ import {
13
14
  type LegacyTarget,
14
15
  type WorkspaceTarget,
15
16
  } from '#profile';
16
- import { deployedWorkspace } from '#deployments/upload.ts';
17
17
  import { ConfigDocument } from './config-edit.ts';
18
+ import { hoist, summarise } from './migrate-plan.ts';
18
19
 
19
20
  /**
20
21
  * Contract 1 → 2: the target moves out of the profile and into the workspace.
@@ -168,123 +169,6 @@ export async function migrateWorkspace(
168
169
  };
169
170
  }
170
171
 
171
- /**
172
- * Every profile's target blocks, folded into one registry.
173
- *
174
- * Two refusals rather than guesses, because both are cases where one target
175
- * would need two different answers and picking either silently is the class of
176
- * bug this whole change is about.
177
- */
178
- async function hoist(
179
- legacy: readonly { profile: string; targets: Record<string, LegacyTarget> }[],
180
- workspaceRoot: string,
181
- ): Promise<Record<string, WorkspaceTarget>> {
182
- const registry: Record<string, WorkspaceTarget> = {};
183
- const seenFrom: Record<string, string> = {};
184
-
185
- for (const { profile, targets } of legacy) {
186
- for (const [name, declared] of Object.entries(targets)) {
187
- const entry = toEntry(name, declared, profile, workspaceRoot);
188
- const previous = registry[name];
189
-
190
- if (previous === undefined) {
191
- registry[name] = entry;
192
- seenFrom[name] = profile;
193
- continue;
194
- }
195
-
196
- if (JSON.stringify(previous) !== JSON.stringify(entry)) {
197
- throw new ConfigError(
198
- `Profiles "${seenFrom[name]}" and "${profile}" both declare target "${name}", and they ` +
199
- `do not agree.\n` +
200
- ` A target is declared once by the workspace it lives in (ADR-052), so one of these\n` +
201
- ` has to win and this cannot pick.\n\n` +
202
- ` ${seenFrom[name]}: ${summarise(previous)}\n` +
203
- ` ${profile}: ${summarise(entry)}\n\n` +
204
- ` Edit one of them to match the other, then run this again.`,
205
- );
206
- }
207
- }
208
- }
209
-
210
- return registry;
211
- }
212
-
213
- /**
214
- * One contract-1 target block as a registry entry.
215
- *
216
- * A block whose storage names a bucket becomes a pointer: that bucket is a
217
- * workspace, it holds the profiles served there, and under ADR-052 it is the
218
- * thing that declares the target. The adapters are not copied into the pointer —
219
- * they travel to the bucket on the next `deploy`, which is the only command that
220
- * can write there and roll an image that understands what it wrote.
221
- *
222
- * Per-profile paths are dropped when they are the layout defaults, which is the
223
- * ordinary case: `./data/<profile>` and `./data/<profile>/credentials.enc` are
224
- * exactly what `layout.ts` derives, so a workspace-level target that omits them
225
- * addresses the same bytes. A genuinely custom path cannot be hoisted — one
226
- * target cannot hold a different path per profile — and is refused by name.
227
- */
228
- function toEntry(
229
- name: string,
230
- declared: LegacyTarget,
231
- profile: string,
232
- workspaceRoot: string,
233
- ): WorkspaceTarget {
234
- const remote = deployedWorkspace(declared);
235
- if (remote) return { workspace: remote };
236
-
237
- const storagePath = declared.storage.path;
238
- const credentialsPath = declared.credentials.path;
239
-
240
- const defaultStorage = `./${layout.blobs(profile)}`;
241
- const defaultCredentials = `./${layout.credentials(profile)}`;
242
-
243
- refuseCustomPath(name, profile, workspaceRoot, 'storage.path', storagePath, defaultStorage);
244
- refuseCustomPath(
245
- name,
246
- profile,
247
- workspaceRoot,
248
- 'credentials.path',
249
- credentialsPath,
250
- defaultCredentials,
251
- );
252
-
253
- const { path: _storage, ...storage } = declared.storage;
254
- const { path: _credentials, ...credentials } = declared.credentials;
255
-
256
- return {
257
- credentials,
258
- storage,
259
- ...(declared.audit ? { audit: declared.audit } : {}),
260
- ...(declared.vault ? { vault: declared.vault } : {}),
261
- ...(declared.deploy ? { deploy: declared.deploy } : {}),
262
- };
263
- }
264
-
265
- function refuseCustomPath(
266
- target: string,
267
- profile: string,
268
- workspaceRoot: string,
269
- field: string,
270
- value: string | undefined,
271
- expected: string,
272
- ): void {
273
- if (value === undefined) return;
274
- // Both spellings of the same directory: `./data/personal` and `data/personal`
275
- // resolve identically and the template has written each at different times.
276
- if (value === expected || value === expected.replace(/^\.\//, '')) return;
277
-
278
- throw new ConfigError(
279
- `Profile "${profile}" declares target "${target}" with a custom ${field}:\n` +
280
- ` ${value}\n` +
281
- ` A target is declared once for the whole workspace now (ADR-052), so it cannot hold a\n` +
282
- ` different path per profile. The default it would get is ${expected}.\n\n` +
283
- ` Move the data there and delete the line, or keep this profile in a workspace of its\n` +
284
- ` own: LANES_LINK_HOME=${workspaceRoot}/<somewhere> lanes link profile add ${profile} --target ${target}`,
285
- );
286
- }
287
-
288
172
  /**
289
173
  * Write the registry into the workspace file, creating it when absent.
290
174
  *
@@ -351,12 +235,6 @@ function describe(
351
235
  );
352
236
  }
353
237
 
354
- function summarise(entry: WorkspaceTarget): string {
355
- if (entry.workspace !== undefined) return `points at ${entry.workspace}`;
356
- const parts = [entry.credentials?.adapter, entry.storage?.adapter].filter(Boolean);
357
- return `${parts.join(' + ')}${entry.deploy ? `, deploys ${entry.deploy.service}` : ''}`;
358
- }
359
-
360
238
  /**
361
239
  * Refuse a contract-1 workspace with the command that fixes it.
362
240
  *
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  ConfigError,
3
3
  recordTarget,
4
+ resolveTargetWorkspace,
4
5
  resolveWorkspaceRoot,
5
6
  type DeployConfig,
6
7
  } from '#profile';
@@ -61,6 +62,23 @@ export interface DeployFlags extends GlobalFlags {
61
62
  }
62
63
 
63
64
  export async function deploy(flags: DeployFlags): Promise<void> {
65
+ // **The target's own workspace is migrated before anything resolves it.**
66
+ //
67
+ // This is the first thing the command does, and it has to be. `deploy` is what
68
+ // the refusal on a contract-1 bucket tells you to run — and every other read of
69
+ // that bucket goes through `openTarget`, which refuses it for the same reason.
70
+ // Migrating after resolution made the instruction circular: the command named
71
+ // as the fix could not get past the problem it fixes.
72
+ //
73
+ // `resolveTargetWorkspace` is the one lookup that does not need the far end to
74
+ // declare anything: it reads this machine's pointer and stops. So the bucket is
75
+ // located, migrated, and only then opened.
76
+ //
77
+ // Idempotent, and silent on a workspace already at contract 2 — a listing and
78
+ // no writes. `--dry-run` reports what it would do and writes nothing, like
79
+ // every other step of this command.
80
+ if (!(await migrateTargetWorkspace(requireTargetFlag(flags), flags.dryRun !== true))) return;
81
+
64
82
  // The one command allowed to name a target that does not exist yet: creating
65
83
  // it is what a first deploy is for.
66
84
  //
@@ -258,21 +276,13 @@ export async function deploy(flags: DeployFlags): Promise<void> {
258
276
 
259
277
  // **The bucket's own migration, here and nowhere else.**
260
278
  //
261
- // Contract 1 is not read by this binary at all (ADR-052), so the moment a
262
- // bucket becomes contract 2 the revision in front of it stops being able to
263
- // read its own config. Doing it here puts that window between an upload and
264
- // a rollout that are seconds apart, rather than leaving it open until
265
- // somebody happens to redeploy.
266
- //
267
- // After the upload, because the upload is what puts the profiles there for
268
- // it to migrate. Idempotent, so a bucket already at contract 2 costs one
279
+ // A second pass, and it is not redundant. The one at the top of the command
280
+ // migrated whatever the bucket already held; this catches what the upload
281
+ // just put there profiles from a workspace that is itself at contract 2
282
+ // arrive migrated, but a first deploy of a *newly created* bucket writes
283
+ // them here for the first time. Idempotent, so the ordinary case is one
269
284
  // listing and no writes.
270
- const migrated = await migrateWorkspace(workspace);
271
- if (!migrated.alreadyCurrent) {
272
- heading('Migrated');
273
- for (const change of migrated.changes) print(` ${change}`);
274
- print(style.dim(' The revision below is the first that can read it.'));
275
- }
285
+ await migrateWorkspace(workspace, { apply: true });
276
286
 
277
287
  // **The target hands itself over to the workspace it now lives in.**
278
288
  //
@@ -330,3 +340,42 @@ function requireTargetFlag(flags: DeployFlags): string {
330
340
  }
331
341
  return flags.target;
332
342
  }
343
+
344
+ /**
345
+ * Bring a target's own workspace to the current contract, before it is opened.
346
+ *
347
+ * Deliberately tolerant of a target this workspace has no pointer for: that is a
348
+ * first deploy, where there is nothing to migrate and `deploy` is about to create
349
+ * the workspace itself.
350
+ *
351
+ * Narrated when it does something. This rewrites every profile in somebody's
352
+ * bucket, and a command that reshapes that silently is one they cannot audit
353
+ * afterwards.
354
+ */
355
+ async function migrateTargetWorkspace(target: string, apply: boolean): Promise<boolean> {
356
+ const root = resolveWorkspaceRoot();
357
+
358
+ const workspace = await resolveTargetWorkspace(root, target).catch(() => null);
359
+ if (workspace === null || workspace === root) return true;
360
+
361
+ const migrated = await migrateWorkspace(workspace, { apply });
362
+ if (migrated.alreadyCurrent) return true;
363
+
364
+ heading(apply ? 'Migrated' : 'Would migrate');
365
+ print(style.dim(` ${workspace}`));
366
+ for (const change of migrated.changes) print(` ${change}`);
367
+ if (apply) {
368
+ print(style.dim(' The revision this deploy rolls is the first that can read it.'));
369
+ return true;
370
+ }
371
+
372
+ // A dry run stops here rather than pressing on to survey and plan. Everything
373
+ // past this point opens the target, and the target is not readable until the
374
+ // migration above has actually happened — so continuing would report a second,
375
+ // confusing refusal about the thing the first paragraph just offered to fix.
376
+ print(style.dim(' Nothing was written, and nothing else was checked: the rest of this'));
377
+ print(style.dim(' command opens the target, which is not readable until this has run.'));
378
+ print('');
379
+ print(style.dim(` Run it for real: lanes link deploy --target ${target}`));
380
+ return false;
381
+ }