@lanes-sh/link 0.3.0 → 0.3.2

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.
@@ -0,0 +1,330 @@
1
+ import { parseDocument } from 'yaml';
2
+ import {
3
+ ConfigError,
4
+ parseConfig,
5
+ readWorkspaceFile,
6
+ workspaceFiles,
7
+ writeWorkspaceFile,
8
+ type Config,
9
+ } from '#profile';
10
+ import { ConfigDocument } from '#cli/config-edit.ts';
11
+ import { diffConfigs, keyOfElement, keyedArrayFor, type Change } from './sync.ts';
12
+ import { isWorkspaceConfig } from './upload.ts';
13
+
14
+ /**
15
+ * Deciding a difference and writing it down.
16
+ *
17
+ * The rule is union, and refusal where a union is not possible. Anything one
18
+ * side is missing is copied to it; anything both sides hold differently stops
19
+ * the sync and prints the diff. Last-writer-wins was the alternative and it is
20
+ * the failure this whole command exists because of — a copy of a profile
21
+ * quietly replaced another that held six connections it did not.
22
+ *
23
+ * `--prefer` is how a conflict is resolved, and it is deliberately one flag for
24
+ * the whole run rather than a prompt per key: a sync that asks twelve questions
25
+ * is one answered by pressing return, and the twelfth answer is the one that
26
+ * matters.
27
+ */
28
+
29
+ export type Prefer = 'local' | 'remote';
30
+
31
+ export interface ProfileSync {
32
+ readonly profile: string;
33
+ readonly changes: readonly Change[];
34
+ /** Only on the local side: the remote file does not exist at all. */
35
+ readonly onlyRemote: boolean;
36
+ readonly onlyLocal: boolean;
37
+ }
38
+
39
+ const profileKey = (profile: string): string => `profiles/${profile}.yaml`;
40
+
41
+ /** Parse a profile out of a workspace, or `undefined` when it is not there. */
42
+ async function readProfile(root: string, profile: string): Promise<Config | undefined> {
43
+ const text = await readWorkspaceFile(workspaceFiles(root), profileKey(profile));
44
+ if (text === null) return undefined;
45
+
46
+ // A remote copy that will not parse is a conflict the operator has to look
47
+ // at, not something to overwrite with the local one — it may be the only copy
48
+ // of something.
49
+ try {
50
+ return parseConfig(text, `${root}/${profileKey(profile)}`).config;
51
+ } catch (error) {
52
+ throw new ConfigError(
53
+ `${root}/${profileKey(profile)} could not be read, so it cannot be merged:\n` +
54
+ ` ${error instanceof Error ? (error.message.split('\n')[0] ?? '') : String(error)}\n` +
55
+ ' Fix it there, or pass --prefer local to overwrite it.',
56
+ );
57
+ }
58
+ }
59
+
60
+ /**
61
+ * The same file, unvalidated, as the source of what actually gets written.
62
+ *
63
+ * The diff compares *validated* configs, because that is the only comparison
64
+ * that gets equality right: `policy.allow: [gmail.*]` and
65
+ * `[{capability: gmail.*}]` are the same grant written two ways, and a raw
66
+ * comparison would call that a conflict.
67
+ *
68
+ * What is written has to come from here all the same. Writing the validated
69
+ * value back means writing every default zod filled in on the way through —
70
+ * `min_instances: 0`, an OAuth token lifetime nobody set — into a file the
71
+ * operator reads. Recovering six connections should not also silently expand
72
+ * three policy rules into a shape they were not written in.
73
+ *
74
+ * The two cannot disagree about whether something is missing: a default is
75
+ * filled in identically on both sides, so it is equal, so it is never a change.
76
+ */
77
+ async function readRawProfile(root: string, profile: string): Promise<unknown> {
78
+ const text = await readWorkspaceFile(workspaceFiles(root), profileKey(profile));
79
+ return text === null ? undefined : parseDocument(text).toJSON();
80
+ }
81
+
82
+ /** Every profile either side has, so one that exists only remotely is still seen. */
83
+ export async function profilesInEither(local: string, remote: string): Promise<string[]> {
84
+ const names = async (root: string): Promise<string[]> =>
85
+ (await workspaceFiles(root).list('profiles/'))
86
+ .map((entry) => entry.key.slice('profiles/'.length))
87
+ .filter((name) => name.endsWith('.yaml') && !name.endsWith('.example.yaml'))
88
+ .filter((name) => !name.includes('/'))
89
+ .map((name) => name.slice(0, -'.yaml'.length));
90
+
91
+ return [...new Set([...(await names(local)), ...(await names(remote))])].sort();
92
+ }
93
+
94
+ /** What differs, for one profile, between the workspace and a target's copy. */
95
+ export async function planProfile(
96
+ localRoot: string,
97
+ remoteRoot: string,
98
+ profile: string,
99
+ prefer: Prefer | undefined,
100
+ ): Promise<ProfileSync> {
101
+ const local = await readProfile(localRoot, profile);
102
+ const remote = prefer === 'local' && local !== undefined
103
+ ? // Told local wins outright: there is nothing to ask the remote copy, and
104
+ // reading it could only produce conflicts already decided.
105
+ undefined
106
+ : await readProfile(remoteRoot, profile);
107
+
108
+ return {
109
+ profile,
110
+ changes: diffConfigs(local, remote),
111
+ onlyRemote: local === undefined && remote !== undefined,
112
+ onlyLocal: remote === undefined && local !== undefined,
113
+ };
114
+ }
115
+
116
+ /** The changes that will be written, once `--prefer` has decided the rest. */
117
+ export function resolved(changes: readonly Change[], prefer: Prefer | undefined): Change[] {
118
+ return changes.map((change) => {
119
+ if (change.direction !== 'conflict') return change;
120
+ if (prefer === undefined) return change;
121
+ return { ...change, direction: prefer === 'remote' ? 'pull' : 'push' } as Change;
122
+ });
123
+ }
124
+
125
+ /**
126
+ * Write everything the local copy is missing into the local profile.
127
+ *
128
+ * Through `ConfigDocument`, so the comments an operator wrote survive a
129
+ * recovery, and so the result is validated before it lands — a merged config
130
+ * that would not load is a worse outcome than the one being fixed.
131
+ *
132
+ * A keyed-array element is written by rewriting its array. A YAML sequence has
133
+ * no addressable slot for "the entry whose provider is gmail", and merging by
134
+ * index is exactly the comparison this avoided making in the first place.
135
+ */
136
+ export async function applyPulls(
137
+ localRoot: string,
138
+ remoteRoot: string,
139
+ profile: string,
140
+ changes: readonly Change[],
141
+ ): Promise<number> {
142
+ const pulls = changes.filter((change) => change.direction === 'pull');
143
+ if (pulls.length === 0) return 0;
144
+
145
+ // A profile local does not have at all is a file copy: there is no document
146
+ // to edit, and every key in it is a pull.
147
+ if (pulls.some((change) => change.path.length === 0)) {
148
+ const text = await readWorkspaceFile(workspaceFiles(remoteRoot), profileKey(profile));
149
+ if (text === null) return 0;
150
+ await writeWorkspaceFile(workspaceFiles(localRoot), profileKey(profile), text);
151
+ return 1;
152
+ }
153
+
154
+ const document = await ConfigDocument.open(localRoot, profile);
155
+ const remote = await readRawProfile(remoteRoot, profile);
156
+ if (remote === undefined) return 0;
157
+
158
+ const local = document.toJSON();
159
+ const written = new Set<string>();
160
+
161
+ for (const change of pulls) {
162
+ const array = keyedArrayFor(change.path);
163
+
164
+ if (array) {
165
+ // One write per array, however many of its elements were missing — and
166
+ // the value written is the *merge*, never the remote array.
167
+ //
168
+ // It used to be `setIn(array, remoteArray)`, which reads as "pull the
169
+ // connections" and means "replace the connections". A profile that had
170
+ // gained six accounts locally since the last deploy lost all six to a
171
+ // command whose entire purpose is not losing things. The tests missed it
172
+ // because every one of them had a local array that was a subset of the
173
+ // remote — the case where replacing and merging agree.
174
+ const key = array.join('.');
175
+ if (written.has(key)) continue;
176
+ written.add(key);
177
+
178
+ const merged = mergeKeyed(
179
+ array,
180
+ valueAt(local, array),
181
+ valueAt(remote, array),
182
+ pulls
183
+ .filter((one) => keyedArrayFor(one.path)?.join('.') === key)
184
+ .map((one) => one.path[array.length])
185
+ .filter((name): name is string => name !== undefined),
186
+ );
187
+ if (merged !== undefined) document.setIn([...array], merged);
188
+ continue;
189
+ }
190
+
191
+ const value = valueAt(remote, change.path);
192
+ // Absent from the raw document means the remote side only has it as a
193
+ // default, which the local side fills in identically. Nothing to write.
194
+ if (value === undefined) continue;
195
+
196
+ document.setIn([...change.path], value);
197
+ }
198
+
199
+ await document.save();
200
+ return written.size;
201
+ }
202
+
203
+ /**
204
+ * The local array, with the named elements taken from the remote one.
205
+ *
206
+ * Element order is local's, with anything new appended, so a pull reads as a
207
+ * diff in the file rather than as a reshuffle. An element named in `wanted` and
208
+ * absent from the remote array is skipped rather than removed: the diff said it
209
+ * was missing locally, so it cannot also be missing remotely.
210
+ */
211
+ function mergeKeyed(
212
+ arrayPath: readonly string[],
213
+ local: unknown,
214
+ remote: unknown,
215
+ wanted: readonly string[],
216
+ ): unknown[] | undefined {
217
+ if (!Array.isArray(remote)) return undefined;
218
+
219
+ const merged = Array.isArray(local) ? [...(local as unknown[])] : [];
220
+ const keyOf = (item: unknown): string | undefined => keyOfElement(arrayPath, item);
221
+
222
+ for (const name of new Set(wanted)) {
223
+ const incoming = remote.find((item) => keyOf(item) === name);
224
+ if (incoming === undefined) continue;
225
+
226
+ const at = merged.findIndex((item) => keyOf(item) === name);
227
+ if (at >= 0) merged[at] = incoming;
228
+ else merged.push(incoming);
229
+ }
230
+
231
+ return merged;
232
+ }
233
+
234
+ /** Read a path out of the raw document, for handing to `setIn`. */
235
+ function valueAt(config: unknown, path: readonly string[]): unknown {
236
+ let value: unknown = config;
237
+ for (const step of path) {
238
+ if (!(typeof value === 'object' && value !== null)) return undefined;
239
+ value = (value as Record<string, unknown>)[step];
240
+ }
241
+ return value;
242
+ }
243
+
244
+ /**
245
+ * Send the local profile up, once it holds everything both sides had.
246
+ *
247
+ * Wholesale rather than key by key, and only *after* the pulls have been
248
+ * applied: at that point the local file is the union, so copying it up makes
249
+ * the remote one the union too. The remote copy has no comments of its own to
250
+ * lose — `uploadWorkspace` wrote it — so there is nothing finer-grained to
251
+ * preserve, and one PUT cannot half-apply a merge.
252
+ */
253
+ export async function applyPushes(
254
+ localRoot: string,
255
+ remoteRoot: string,
256
+ profile: string,
257
+ changes: readonly Change[],
258
+ ): Promise<boolean> {
259
+ if (!changes.some((change) => change.direction === 'push')) return false;
260
+
261
+ const text = await readWorkspaceFile(workspaceFiles(localRoot), profileKey(profile));
262
+ if (text === null) return false;
263
+
264
+ await writeWorkspaceFile(workspaceFiles(remoteRoot), profileKey(profile), text);
265
+ return true;
266
+ }
267
+
268
+ /**
269
+ * The authored areas inside `data/` — skills and provider manifests.
270
+ *
271
+ * They ride the same allowlist a deploy uploads by, and for the reason that
272
+ * comment gives at length: the list is what keeps a credential store out of a
273
+ * bucket, and a second answer to "which files" is how the two drift. Compared
274
+ * by content, since they are documents rather than config, and differing
275
+ * content is a conflict like any other.
276
+ */
277
+ export interface BlobSync {
278
+ readonly key: string;
279
+ readonly direction: 'pull' | 'push' | 'conflict';
280
+ }
281
+
282
+ export async function planBlobs(localRoot: string, remoteRoot: string): Promise<BlobSync[]> {
283
+ const read = async (root: string): Promise<Map<string, Uint8Array>> => {
284
+ const files = workspaceFiles(root);
285
+ const found = new Map<string, Uint8Array>();
286
+
287
+ for (const entry of await files.list('')) {
288
+ // `undefined` profile: every profile's authored areas, since sync is
289
+ // scoped to the target and not to one of them.
290
+ if (!isWorkspaceConfig(entry.key) || entry.key.endsWith('.yaml')) continue;
291
+ const bytes = await files.get(entry.key);
292
+ if (bytes !== null) found.set(entry.key, bytes);
293
+ }
294
+ return found;
295
+ };
296
+
297
+ const here = await read(localRoot);
298
+ const there = await read(remoteRoot);
299
+ const equal = (a: Uint8Array, b: Uint8Array): boolean =>
300
+ a.length === b.length && a.every((byte, index) => byte === b[index]);
301
+
302
+ return [...new Set([...here.keys(), ...there.keys()])].sort().flatMap((key): BlobSync[] => {
303
+ const local = here.get(key);
304
+ const remote = there.get(key);
305
+ if (local === undefined) return [{ key, direction: 'pull' }];
306
+ if (remote === undefined) return [{ key, direction: 'push' }];
307
+ return equal(local, remote) ? [] : [{ key, direction: 'conflict' }];
308
+ });
309
+ }
310
+
311
+ export async function applyBlobs(
312
+ localRoot: string,
313
+ remoteRoot: string,
314
+ blobs: readonly BlobSync[],
315
+ ): Promise<number> {
316
+ let copied = 0;
317
+
318
+ for (const blob of blobs) {
319
+ const from = blob.direction === 'pull' ? remoteRoot : localRoot;
320
+ const to = blob.direction === 'pull' ? localRoot : remoteRoot;
321
+
322
+ const bytes = await workspaceFiles(from).get(blob.key);
323
+ if (bytes === null) continue;
324
+
325
+ await workspaceFiles(to).put(blob.key, bytes, { contentType: 'application/octet-stream' });
326
+ copied += 1;
327
+ }
328
+
329
+ return copied;
330
+ }
@@ -0,0 +1,164 @@
1
+ import type { Config } from '#profile';
2
+
3
+ /**
4
+ * What differs between a workspace and a target's copy of it.
5
+ *
6
+ * A deployed endpoint reads its config out of a bucket, and `deploy` puts it
7
+ * there — so from the moment of the first deploy there are two copies of every
8
+ * profile. They are supposed to agree. When they do not, one of them holds
9
+ * something the other has lost, and until now nothing could say which or bring
10
+ * it back.
11
+ *
12
+ * This file only *finds* the difference. Deciding it and writing it are
13
+ * `sync-apply.ts`, and choosing which bucket to compare against is the command.
14
+ * Split that way because the diff is the part worth testing exhaustively and
15
+ * the only part with no I/O in it.
16
+ */
17
+
18
+ /** Which side is missing what, or that both have it and disagree. */
19
+ export type Direction = 'pull' | 'push' | 'conflict';
20
+
21
+ export interface Change {
22
+ /** Where in the config, e.g. `['targets', 'cloud']` or `['connections']`. */
23
+ readonly path: readonly string[];
24
+ readonly direction: Direction;
25
+ /** What remote holds. Absent when remote is the side that is missing it. */
26
+ readonly remote?: unknown;
27
+ /** What local holds. Absent when local is the side that is missing it. */
28
+ readonly local?: unknown;
29
+ }
30
+
31
+ /**
32
+ * Arrays whose elements are records rather than an ordered list.
33
+ *
34
+ * `connections` is a set of accounts keyed by `provider.id`; comparing it
35
+ * positionally would call a reordered file a conflict and, worse, would call an
36
+ * *added* connection a change to whichever one now sits at that index. The key
37
+ * function is what makes "personal gained a mailbox" a different fact from
38
+ * "personal's third connection changed".
39
+ *
40
+ * `identity` is deliberately absent. Its declaration order is meaningful — the
41
+ * first entry of a kind is the one to reach for — so it is an ordered list and
42
+ * compares as one.
43
+ */
44
+ const KEYED: Record<string, (item: unknown) => string | undefined> = {
45
+ connections: (item) =>
46
+ isRecord(item) ? `${String(item['provider'])}.${String(item['id'])}` : undefined,
47
+ // Two shapes, and both are reached. The diff runs over validated configs,
48
+ // where `allow: [gmail.*]` has become `[{capability: gmail.*}]`; the writer
49
+ // runs over the raw document, where it is still a string. A key function that
50
+ // knew only the validated shape found nothing to merge and silently wrote the
51
+ // array without it.
52
+ 'policy.allow': capabilityOf,
53
+ 'policy.deny': capabilityOf,
54
+ };
55
+
56
+ function capabilityOf(item: unknown): string | undefined {
57
+ if (typeof item === 'string') return item;
58
+ return isRecord(item) ? String(item['capability']) : undefined;
59
+ }
60
+
61
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
62
+ typeof value === 'object' && value !== null && !Array.isArray(value);
63
+
64
+ const same = (a: unknown, b: unknown): boolean => JSON.stringify(a) === JSON.stringify(b);
65
+
66
+ /**
67
+ * Every difference between one profile's two copies.
68
+ *
69
+ * Recursive over objects so a change reports at the narrowest path that
70
+ * describes it: two profiles differing only in `auth.authorization` produce
71
+ * that path, not `auth`, and applying it cannot take `token_ref` with it.
72
+ */
73
+ export function diffConfigs(local: Config | undefined, remote: Config | undefined): Change[] {
74
+ if (local === undefined && remote === undefined) return [];
75
+
76
+ // A whole profile that only one side has. Reported at the root, because the
77
+ // unit that has to be copied is the file.
78
+ if (local === undefined) return [{ path: [], direction: 'pull', remote }];
79
+ if (remote === undefined) return [{ path: [], direction: 'push', local }];
80
+
81
+ return walk([], local as unknown, remote as unknown);
82
+ }
83
+
84
+ function walk(path: readonly string[], local: unknown, remote: unknown): Change[] {
85
+ if (same(local, remote)) return [];
86
+
87
+ if (local === undefined) return [{ path, direction: 'pull', remote }];
88
+ if (remote === undefined) return [{ path, direction: 'push', local }];
89
+
90
+ const key = path.join('.');
91
+ const keyOf = KEYED[key];
92
+ if (keyOf && Array.isArray(local) && Array.isArray(remote)) {
93
+ return walkKeyed(path, keyOf, local, remote);
94
+ }
95
+
96
+ if (isRecord(local) && isRecord(remote)) {
97
+ const keys = [...new Set([...Object.keys(local), ...Object.keys(remote)])].sort();
98
+ return keys.flatMap((name) => walk([...path, name], local[name], remote[name]));
99
+ }
100
+
101
+ // Two scalars, or two ordered arrays, that are not equal. There is no
102
+ // narrower path to report and no way to have both.
103
+ return [{ path, direction: 'conflict', local, remote }];
104
+ }
105
+
106
+ /**
107
+ * A keyed array, compared as the set of records it is.
108
+ *
109
+ * Each element reports under `path.key` — `connections.gmail.work` — so a
110
+ * missing account names itself in the output instead of appearing as an index.
111
+ * Applying still rewrites the whole array, because a YAML sequence has no
112
+ * addressable slot for "the element whose provider is gmail".
113
+ */
114
+ function walkKeyed(
115
+ path: readonly string[],
116
+ keyOf: (item: unknown) => string | undefined,
117
+ local: readonly unknown[],
118
+ remote: readonly unknown[],
119
+ ): Change[] {
120
+ const index = (items: readonly unknown[]): Map<string, unknown> =>
121
+ new Map(
122
+ items
123
+ .map((item) => [keyOf(item), item] as const)
124
+ .filter((pair): pair is readonly [string, unknown] => pair[0] !== undefined),
125
+ );
126
+
127
+ const here = index(local);
128
+ const there = index(remote);
129
+ const names = [...new Set([...here.keys(), ...there.keys()])].sort();
130
+
131
+ return names.flatMap((name) => walk([...path, name], here.get(name), there.get(name)));
132
+ }
133
+
134
+ /**
135
+ * The array a change belongs to, when it belongs to one.
136
+ *
137
+ * `['connections', 'gmail.work']` is applied by rewriting `connections`, so
138
+ * both the writer and the renderer need to know where the element stops and the
139
+ * key begins. Longest prefix first, so `policy.allow` wins over `policy`.
140
+ */
141
+ export function keyedArrayFor(path: readonly string[]): readonly string[] | undefined {
142
+ for (let depth = path.length - 1; depth > 0; depth--) {
143
+ if (path.slice(0, depth).join('.') in KEYED) return path.slice(0, depth);
144
+ }
145
+ return undefined;
146
+ }
147
+
148
+ /**
149
+ * How an element of a keyed array identifies itself, for the writer.
150
+ *
151
+ * The diff indexes these to compare them; applying one has to find the same
152
+ * element again in *both* raw documents, and it cannot re-derive the key from
153
+ * the path — `connections.gmail.work` is one key containing a dot, not two
154
+ * steps. Exported so the two halves cannot disagree about what identifies a
155
+ * connection.
156
+ */
157
+ export function keyOfElement(arrayPath: readonly string[], item: unknown): string | undefined {
158
+ return KEYED[arrayPath.join('.')]?.(item);
159
+ }
160
+
161
+ /** Whether a set of changes can be applied without being told which side wins. */
162
+ export function conflictsIn(changes: readonly Change[]): Change[] {
163
+ return changes.filter((change) => change.direction === 'conflict');
164
+ }
@@ -64,17 +64,21 @@ export function deployedWorkspace(declared: TargetConfig): string | undefined {
64
64
  * prefix: `data/personal/skills.detour/` is not `skills.d`, and the difference
65
65
  * between matching it and not is a credential in a bucket.
66
66
  */
67
- export function isWorkspaceConfig(key: string, profile?: string | undefined): boolean {
67
+ export function isWorkspaceConfig(key: string, profiles?: readonly string[]): boolean {
68
68
  if (key === WORKSPACE_FILE) return true;
69
69
 
70
- // One profile when the deploy names one, so a workspace holding personal and
71
- // work does not push both into a bucket only one of them is for. The same
72
- // question for both shapes, asked once.
70
+ // A set rather than one name, because a deploy now sends every profile that
71
+ // declares the target rather than the single one it was told. `undefined`
72
+ // still means the whole workspace, and an *empty* set means nothing — which
73
+ // is a distinction a bare string could not make.
74
+ const wanted = profiles === undefined ? undefined : new Set(profiles);
75
+
73
76
  const owner = authoredAreaOwner(key);
74
- if (owner !== null) return profile === undefined || owner === profile;
77
+ if (owner !== null) return wanted === undefined || wanted.has(owner);
75
78
 
76
79
  if (!key.startsWith('profiles/') || !key.endsWith('.yaml')) return false;
77
- return profile === undefined || key === `profiles/${profile}.yaml`;
80
+ const name = key.slice('profiles/'.length, -'.yaml'.length);
81
+ return wanted === undefined || wanted.has(name);
78
82
  }
79
83
 
80
84
  /**
@@ -120,10 +124,12 @@ function authoredAreaOwner(key: string): string | null {
120
124
  */
121
125
  export async function repairSetupSurface(
122
126
  workspaceRoot: string,
123
- profile: string | undefined,
127
+ profiles: readonly string[] | undefined,
124
128
  ): Promise<void> {
129
+ const wanted = profiles === undefined ? undefined : new Set(profiles);
130
+
125
131
  for (const name of await listProfiles(workspaceRoot)) {
126
- if (profile !== undefined && name !== profile) continue;
132
+ if (wanted !== undefined && !wanted.has(name)) continue;
127
133
 
128
134
  try {
129
135
  const document = await ConfigDocument.open(workspaceRoot, name);
@@ -151,14 +157,14 @@ export async function repairSetupSurface(
151
157
  export async function uploadWorkspace(
152
158
  root: string,
153
159
  destination: string,
154
- profile: string | undefined,
160
+ profiles: readonly string[] | undefined,
155
161
  ): Promise<void> {
156
162
  const local = workspaceFiles(root);
157
163
  const remote = workspaceFiles(destination);
158
164
 
159
165
  let copied = 0;
160
166
  for (const entry of await local.list('')) {
161
- if (!isWorkspaceConfig(entry.key, profile)) continue;
167
+ if (!isWorkspaceConfig(entry.key, profiles)) continue;
162
168
 
163
169
  const bytes = await local.get(entry.key);
164
170
  if (bytes === null) continue;
@@ -203,6 +209,6 @@ export async function publishWorkspace(input: {
203
209
  const destination = deployedWorkspace(declared);
204
210
  if (!destination) return null;
205
211
 
206
- await uploadWorkspace(input.workspaceRoot, destination, input.profile);
212
+ await uploadWorkspace(input.workspaceRoot, destination, [input.profile]);
207
213
  return destination;
208
214
  }
@@ -0,0 +1,80 @@
1
+ import { parseDocument } from 'yaml';
2
+ import { readWorkspaceFile, workspaceFiles, writeWorkspaceFile } from './files.ts';
3
+ import { workspaceSchema, type DeploymentRecord } from './schema.ts';
4
+ import { WORKSPACE_FILE, readWorkspace } from './workspace.ts';
5
+
6
+ /**
7
+ * The workspace's record of where its deployments live.
8
+ *
9
+ * A target is declared by a profile, and that declaration is what every command
10
+ * resolves from. This is the index beside it, and the distinction is the whole
11
+ * design: a profile file rewritten by hand or by a tool took a live Cloud Run
12
+ * service, its bucket, and its credential store out of reach in one edit,
13
+ * because the four lines naming them were the only copy. The service was still
14
+ * running. Nothing could find it.
15
+ *
16
+ * So the record is kept where the thing it describes is not: one level up, in
17
+ * `lanes-link.yaml`. `sync targets` reads it to know which bucket to open, and
18
+ * nothing else reads it at all — an index that starts being resolved from is a
19
+ * second source of truth, which is the failure ADR-037 spent a release
20
+ * removing (ADR-044).
21
+ */
22
+
23
+ /** Every deployment the workspace has recorded. Empty for a workspace with none. */
24
+ export async function readDeployments(workspaceRoot: string): Promise<DeploymentRecord[]> {
25
+ // A workspace file that will not parse is not a reason to fail a recovery:
26
+ // the caller has other ways to find a target, and this is the cheapest.
27
+ try {
28
+ return (await readWorkspace(workspaceRoot))?.deployments ?? [];
29
+ } catch {
30
+ return [];
31
+ }
32
+ }
33
+
34
+ /** What the workspace knows about one target, if anything. */
35
+ export async function findDeployment(
36
+ workspaceRoot: string,
37
+ target: string,
38
+ ): Promise<DeploymentRecord | undefined> {
39
+ return (await readDeployments(workspaceRoot)).find((entry) => entry.target === target);
40
+ }
41
+
42
+ /**
43
+ * Record a deployment, replacing any earlier entry for the same target.
44
+ *
45
+ * Keyed by target rather than appended, because a target has one deployment by
46
+ * definition — a second entry would be a history, and a history is a thing to
47
+ * read wrong. Redeploying the same target to a new bucket should leave one
48
+ * record naming the new one.
49
+ *
50
+ * Merged into the existing entry so a field this caller does not know about —
51
+ * `primary`, on a redeploy that did not ask — is carried forward rather than
52
+ * dropped.
53
+ *
54
+ * Written through the YAML document API, so the comments in a workspace file an
55
+ * operator has annotated survive being indexed.
56
+ */
57
+ export async function recordDeployment(
58
+ workspaceRoot: string,
59
+ entry: DeploymentRecord,
60
+ ): Promise<void> {
61
+ const files = workspaceFiles(workspaceRoot);
62
+ const text = (await readWorkspaceFile(files, WORKSPACE_FILE)) ?? 'contract: 1\n';
63
+
64
+ const document = parseDocument(text);
65
+ const existing = await readDeployments(workspaceRoot);
66
+ const previous = existing.find((record) => record.target === entry.target);
67
+
68
+ const merged = [
69
+ ...existing.filter((record) => record.target !== entry.target),
70
+ { ...previous, ...entry },
71
+ ].sort((a, b) => a.target.localeCompare(b.target));
72
+
73
+ document.setIn(['deployments'], merged);
74
+
75
+ // Validated before it lands, on the rendered tree rather than the input, so
76
+ // what is checked is what would be read back.
77
+ workspaceSchema.parse(document.toJSON());
78
+
79
+ await writeWorkspaceFile(files, WORKSPACE_FILE, String(document));
80
+ }
@@ -14,11 +14,13 @@
14
14
  export {
15
15
  SUPPORTED_CONTRACT,
16
16
  configSchema,
17
+ deploymentRecordSchema,
17
18
  workspaceSchema,
18
19
  type AuthorizationConfig,
19
20
  type Config,
20
21
  type ConnectionConfig,
21
22
  type DeployConfig,
23
+ type DeploymentRecord,
22
24
  type IdentityEntry,
23
25
  type PolicyRuleConfig,
24
26
  type TargetConfig,
@@ -54,19 +56,25 @@ export {
54
56
  installRoot,
55
57
  listProfiles,
56
58
  loadProfileConfig,
59
+ loadWorkspaceProfiles,
57
60
  noProfileNamed,
58
61
  profilePath,
59
62
  readWorkspace,
60
63
  resolveSelection,
61
64
  resolveWorkspaceRoot,
65
+ targetsByName,
62
66
  workspacePath,
67
+ type LoadedProfile,
68
+ type WorkspaceProfiles,
63
69
  } from './workspace.ts';
64
70
  export {
65
71
  LEGACY_TARGET_ENV,
72
+ noTargetInWorkspace,
66
73
  noTargetNamed,
67
74
  requireTarget,
68
75
  undeclaredTarget,
69
76
  } from './targets.ts';
77
+ export { findDeployment, readDeployments, recordDeployment } from './deployments.ts';
70
78
  export {
71
79
  isRemoteWorkspace,
72
80
  readWorkspaceFile,