@venturewild/workspace 0.4.2 → 0.5.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/LICENSE +21 -21
- package/README.md +112 -112
- package/package.json +85 -83
- package/server/bin/wild-workspace.mjs +1096 -1096
- package/server/src/account.mjs +114 -114
- package/server/src/agent-login.mjs +146 -146
- package/server/src/agent-readiness.mjs +200 -200
- package/server/src/agent.mjs +468 -468
- package/server/src/bazaar/core.mjs +790 -579
- package/server/src/bazaar/index.mjs +88 -75
- package/server/src/bazaar/mcp-server.mjs +417 -328
- package/server/src/bazaar/mock-tickup.mjs +97 -97
- package/server/src/bazaar/preview-server.mjs +95 -95
- package/server/src/bazaar/seed-recipes/customer-feedback-form/know-how.md +23 -23
- package/server/src/bazaar/seed-recipes/customer-feedback-form/recipe.json +24 -24
- package/server/src/bazaar/seed-recipes/landing-page-launch/know-how.md +29 -29
- package/server/src/bazaar/seed-recipes/landing-page-launch/recipe.json +25 -25
- package/server/src/bazaar/seed-recipes/personal-portfolio/know-how.md +21 -21
- package/server/src/bazaar/seed-recipes/personal-portfolio/recipe.json +24 -24
- package/server/src/bazaar/seed-recipes/receipt-sorter/know-how.md +31 -31
- package/server/src/bazaar/seed-recipes/receipt-sorter/recipe.json +25 -25
- package/server/src/bazaar/seed-recipes/tickup-hr-matching/know-how.md +79 -79
- package/server/src/bazaar/seed-recipes/tickup-hr-matching/recipe.json +40 -32
- package/server/src/canvas/core.mjs +446 -446
- package/server/src/canvas/index.mjs +42 -42
- package/server/src/canvas/mcp-server.mjs +253 -253
- package/server/src/canvas-rails.mjs +108 -108
- package/server/src/config.mjs +404 -404
- package/server/src/daemon-bin.mjs +110 -110
- package/server/src/daemon-supervisor.mjs +285 -285
- package/server/src/doctor.mjs +375 -375
- package/server/src/inbox.mjs +86 -86
- package/server/src/index.mjs +3279 -3032
- package/server/src/listings-rails.mjs +126 -0
- package/server/src/logpaths.mjs +98 -98
- package/server/src/observability.mjs +45 -45
- package/server/src/operator.mjs +92 -92
- package/server/src/pairing.mjs +137 -137
- package/server/src/service.mjs +515 -515
- package/server/src/session-reporter.mjs +201 -201
- package/server/src/settings.mjs +145 -145
- package/server/src/share.mjs +182 -182
- package/server/src/skills.mjs +213 -213
- package/server/src/supervisor.mjs +647 -647
- package/server/src/support-consent.mjs +133 -133
- package/server/src/sync.mjs +248 -248
- package/server/src/transcript.mjs +121 -121
- package/server/src/turn-mcp.mjs +46 -46
- package/server/src/usage.mjs +405 -405
- package/server/src/workspace-registry.mjs +295 -295
- package/server/src/workspaces.mjs +145 -135
- package/web/dist/assets/index-B8tHt7x-.css +32 -0
- package/web/dist/assets/index-BRY-IKaC.js +131 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-DahRXN26.js +0 -91
- package/web/dist/assets/index-NXZN2LU2.css +0 -1
|
@@ -1,295 +1,295 @@
|
|
|
1
|
-
// Workspace registry — the per-install list of LOCAL workspaces (lobby M1).
|
|
2
|
-
//
|
|
3
|
-
// One install can hold many workspaces; this is the list the lobby shows and
|
|
4
|
-
// switches between. Stored in the GLOBAL dir (~/.wild-workspace/workspaces.json),
|
|
5
|
-
// NEVER inside a synced workspace folder (core principle #1: the user's repo
|
|
6
|
-
// folder is sacred — no system state in it).
|
|
7
|
-
//
|
|
8
|
-
// Each entry points at a folder on THIS host. The server re-roots to the chosen
|
|
9
|
-
// workspace per request via the X-Workspace-Id seam (see index.mjs `workspaceFor`).
|
|
10
|
-
// Creating/opening a workspace is a HOST action (the folder lives here); a remote
|
|
11
|
-
// browser only ENTERS existing ones — enforced at the route, not here.
|
|
12
|
-
//
|
|
13
|
-
// A workspace is `kind:'local'` until the owner explicitly SHARES it (sharing
|
|
14
|
-
// slice / design §4 decision 7): `markShared` promotes the entry to `kind:'shared'`
|
|
15
|
-
// and records a `shared:{slug,ownerEmail,role,sharedAt}` block — the slug is the
|
|
16
|
-
// workspace's first-class identity on the bmo-sync rails (membership/canvas), minted
|
|
17
|
-
// by `POST /api/workspaces/create`. The folder stays exactly where it is; promoting
|
|
18
|
-
// is a metadata flip on the LIST, never a move. `removeWorkspace` drops a folder
|
|
19
|
-
// from the LIST only — it never deletes files.
|
|
20
|
-
|
|
21
|
-
import fs from 'node:fs';
|
|
22
|
-
import path from 'node:path';
|
|
23
|
-
import os from 'node:os';
|
|
24
|
-
import crypto from 'node:crypto';
|
|
25
|
-
import { globalDir } from './logpaths.mjs';
|
|
26
|
-
|
|
27
|
-
const FILE = 'workspaces.json';
|
|
28
|
-
|
|
29
|
-
function registryPath(env = process.env) {
|
|
30
|
-
return path.join(globalDir(env), FILE);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Where "New workspace" (name-only, no path) creates folders — a visible home so
|
|
34
|
-
// the user always knows where their data lives (design §8.1). Overridable for tests.
|
|
35
|
-
function workspacesHome(env = process.env) {
|
|
36
|
-
return env.WILD_WORKSPACE_HOME || path.join(os.homedir(), 'Workspaces');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// A stable id derived from the absolute dir, so re-adding the same folder is
|
|
40
|
-
// idempotent and the id survives a display-name rename. Case-folded on win/mac
|
|
41
|
-
// (case-insensitive filesystems) so one folder maps to exactly one id. Exported
|
|
42
|
-
// so the server can compute the boot/default workspace's id WITHOUT writing the
|
|
43
|
-
// registry file (keeps startup side-effect-free + test-pollution-free).
|
|
44
|
-
export function workspaceIdForDir(dir) {
|
|
45
|
-
const abs = path.resolve(dir);
|
|
46
|
-
const key = process.platform === 'linux' ? abs : abs.toLowerCase();
|
|
47
|
-
return 'ws_' + crypto.createHash('sha256').update(key).digest('hex').slice(0, 12);
|
|
48
|
-
}
|
|
49
|
-
const idForDir = workspaceIdForDir;
|
|
50
|
-
|
|
51
|
-
function nameForDir(dir) {
|
|
52
|
-
return path.basename(path.resolve(dir)) || 'workspace';
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// folder-name-safe slug of a display name (for the New-workspace folder).
|
|
56
|
-
function folderSlug(name) {
|
|
57
|
-
const s = String(name || '')
|
|
58
|
-
.trim()
|
|
59
|
-
.toLowerCase()
|
|
60
|
-
.replace(/[^\p{L}\p{N}]+/gu, '-')
|
|
61
|
-
.replace(/^-+|-+$/g, '')
|
|
62
|
-
.slice(0, 60);
|
|
63
|
-
return s || 'workspace';
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function readRaw(env) {
|
|
67
|
-
try {
|
|
68
|
-
const parsed = JSON.parse(fs.readFileSync(registryPath(env), 'utf8'));
|
|
69
|
-
if (Array.isArray(parsed?.workspaces)) {
|
|
70
|
-
return parsed.workspaces.map(sanitize).filter(Boolean);
|
|
71
|
-
}
|
|
72
|
-
} catch {
|
|
73
|
-
/* missing / unreadable / malformed → empty */
|
|
74
|
-
}
|
|
75
|
-
return [];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// A shared workspace carries a `shared` block (the rails identity + the local
|
|
79
|
-
// member's role). Only kept when the entry is actually `kind:'shared'` AND the
|
|
80
|
-
// block has a slug; anything else collapses back to a plain local entry so a
|
|
81
|
-
// half-written record can't masquerade as shared.
|
|
82
|
-
function sanitizeShared(s) {
|
|
83
|
-
if (!s || typeof s !== 'object' || typeof s.slug !== 'string' || !s.slug.trim()) return null;
|
|
84
|
-
const out = {
|
|
85
|
-
slug: s.slug.trim().toLowerCase(),
|
|
86
|
-
ownerEmail: typeof s.ownerEmail === 'string' ? s.ownerEmail : '',
|
|
87
|
-
role: s.role === 'owner' ? 'owner' : 'member',
|
|
88
|
-
sharedAt: Number(s.sharedAt) || 0,
|
|
89
|
-
};
|
|
90
|
-
// Slice 3: the file-sync project this workspace replicates through, recorded
|
|
91
|
-
// once the daemon is paired ("going live"). Absent until file-sync starts.
|
|
92
|
-
if (typeof s.projectCode === 'string' && s.projectCode.trim()) {
|
|
93
|
-
out.projectCode = s.projectCode.trim();
|
|
94
|
-
}
|
|
95
|
-
return out;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function sanitize(e) {
|
|
99
|
-
if (!e || typeof e !== 'object' || typeof e.dir !== 'string' || !e.dir) return null;
|
|
100
|
-
const dir = path.resolve(e.dir);
|
|
101
|
-
const shared = sanitizeShared(e.shared);
|
|
102
|
-
const out = {
|
|
103
|
-
id: typeof e.id === 'string' && e.id ? e.id : idForDir(dir),
|
|
104
|
-
name: typeof e.name === 'string' && e.name.trim() ? e.name.trim() : nameForDir(dir),
|
|
105
|
-
dir,
|
|
106
|
-
kind: e.kind === 'shared' && shared ? 'shared' : 'local',
|
|
107
|
-
createdAt: Number(e.createdAt) || nowMs(),
|
|
108
|
-
lastOpenedAt: Number(e.lastOpenedAt) || 0,
|
|
109
|
-
};
|
|
110
|
-
if (out.kind === 'shared') out.shared = shared;
|
|
111
|
-
return out;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function writeRaw(list, env) {
|
|
115
|
-
const dir = globalDir(env);
|
|
116
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
117
|
-
const tmp = `${registryPath(env)}.tmp`;
|
|
118
|
-
fs.writeFileSync(tmp, JSON.stringify({ workspaces: list }, null, 2), { mode: 0o600 });
|
|
119
|
-
fs.renameSync(tmp, registryPath(env)); // atomic-ish replace
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function nowMs() {
|
|
123
|
-
return Date.now();
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Recency ordering (listWorkspaces) needs strictly-increasing open stamps. Two
|
|
127
|
-
// opens in the same millisecond — easy on a fast machine or CI — would otherwise
|
|
128
|
-
// tie on lastOpenedAt and fall back to the createdAt tiebreak, which does NOT
|
|
129
|
-
// reflect open-order, so the just-touched workspace could fail to sort to the
|
|
130
|
-
// front. Clamp every new open stamp strictly above every existing stamp so the
|
|
131
|
-
// order is deterministic regardless of clock resolution.
|
|
132
|
-
function nextOpenStamp(list) {
|
|
133
|
-
let max = 0;
|
|
134
|
-
for (const w of list) {
|
|
135
|
-
if (w.lastOpenedAt > max) max = w.lastOpenedAt;
|
|
136
|
-
if (w.createdAt > max) max = w.createdAt;
|
|
137
|
-
}
|
|
138
|
-
return Math.max(nowMs(), max + 1);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// --- public API ------------------------------------------------------------
|
|
142
|
-
|
|
143
|
-
/** All workspaces, most-recently-opened first (then newest-created). */
|
|
144
|
-
export function listWorkspaces(env = process.env) {
|
|
145
|
-
return readRaw(env).sort(
|
|
146
|
-
(a, b) => b.lastOpenedAt - a.lastOpenedAt || b.createdAt - a.createdAt,
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/** One workspace by id, or null. */
|
|
151
|
-
export function getWorkspace(id, env = process.env) {
|
|
152
|
-
return readRaw(env).find((w) => w.id === id) || null;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Find a registered workspace by its directory (idempotency helper), or null. */
|
|
156
|
-
export function findByDir(dir, env = process.env) {
|
|
157
|
-
const want = idForDir(dir);
|
|
158
|
-
return readRaw(env).find((w) => w.id === want) || null;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Ensure `dir` exists in the registry (idempotent) — used at startup to seed the
|
|
163
|
-
* boot/default workspace so the lobby is never blank for an existing install.
|
|
164
|
-
* Does NOT touch lastOpenedAt. Returns the entry.
|
|
165
|
-
*/
|
|
166
|
-
export function seedDefault({ dir, name } = {}, env = process.env) {
|
|
167
|
-
if (!dir) return null;
|
|
168
|
-
const list = readRaw(env);
|
|
169
|
-
const existing = list.find((w) => w.id === idForDir(dir));
|
|
170
|
-
if (existing) return existing;
|
|
171
|
-
const entry = sanitize({
|
|
172
|
-
dir,
|
|
173
|
-
name: name || nameForDir(dir),
|
|
174
|
-
createdAt: nowMs(),
|
|
175
|
-
lastOpenedAt: nowMs(), // the boot workspace is "the one you were just in"
|
|
176
|
-
});
|
|
177
|
-
list.push(entry);
|
|
178
|
-
writeRaw(list, env);
|
|
179
|
-
return entry;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Create a NEW local workspace. With `dir` → register that existing folder
|
|
184
|
-
* ("Open an existing folder"). With only `name` → make a fresh folder under the
|
|
185
|
-
* Workspaces home ("New workspace"); collisions get a -2/-3 suffix. Idempotent on
|
|
186
|
-
* an already-registered dir (returns it, touched). HOST-only is enforced at the route.
|
|
187
|
-
*/
|
|
188
|
-
export function createWorkspace({ name, dir } = {}, env = process.env) {
|
|
189
|
-
let targetDir;
|
|
190
|
-
let isNew = false;
|
|
191
|
-
|
|
192
|
-
if (dir) {
|
|
193
|
-
targetDir = path.resolve(dir);
|
|
194
|
-
if (!fs.existsSync(targetDir)) {
|
|
195
|
-
throw new Error(`folder does not exist: ${targetDir}`);
|
|
196
|
-
}
|
|
197
|
-
if (!fs.statSync(targetDir).isDirectory()) {
|
|
198
|
-
throw new Error(`not a folder: ${targetDir}`);
|
|
199
|
-
}
|
|
200
|
-
} else {
|
|
201
|
-
const home = workspacesHome(env);
|
|
202
|
-
fs.mkdirSync(home, { recursive: true });
|
|
203
|
-
const base = folderSlug(name);
|
|
204
|
-
let candidate = path.join(home, base);
|
|
205
|
-
let n = 2;
|
|
206
|
-
while (fs.existsSync(candidate)) {
|
|
207
|
-
candidate = path.join(home, `${base}-${n}`);
|
|
208
|
-
n += 1;
|
|
209
|
-
}
|
|
210
|
-
fs.mkdirSync(candidate, { recursive: true });
|
|
211
|
-
targetDir = candidate;
|
|
212
|
-
isNew = true;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const list = readRaw(env);
|
|
216
|
-
const id = idForDir(targetDir);
|
|
217
|
-
const existing = list.find((w) => w.id === id);
|
|
218
|
-
if (existing) {
|
|
219
|
-
existing.lastOpenedAt = nextOpenStamp(list);
|
|
220
|
-
if (name && isNew) existing.name = name;
|
|
221
|
-
writeRaw(list, env);
|
|
222
|
-
return existing;
|
|
223
|
-
}
|
|
224
|
-
const entry = sanitize({
|
|
225
|
-
dir: targetDir,
|
|
226
|
-
name: name || nameForDir(targetDir),
|
|
227
|
-
createdAt: nowMs(),
|
|
228
|
-
lastOpenedAt: nextOpenStamp(list),
|
|
229
|
-
});
|
|
230
|
-
list.push(entry);
|
|
231
|
-
writeRaw(list, env);
|
|
232
|
-
return entry;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/** Mark a workspace opened now (drives the recency sort + pre-select-last). */
|
|
236
|
-
export function touchWorkspace(id, env = process.env) {
|
|
237
|
-
const list = readRaw(env);
|
|
238
|
-
const w = list.find((x) => x.id === id);
|
|
239
|
-
if (!w) return null;
|
|
240
|
-
w.lastOpenedAt = nextOpenStamp(list);
|
|
241
|
-
writeRaw(list, env);
|
|
242
|
-
return w;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Promote a registered workspace to SHARED (sharing slice / design §4 decision 7).
|
|
247
|
-
* The workspace must already be in the registry (the boot/default workspace is
|
|
248
|
-
* seeded first by the caller). Records the rails identity (`slug`) + the local
|
|
249
|
-
* member's role; the folder is untouched. Idempotent: re-sharing updates the block.
|
|
250
|
-
* Returns the updated entry, or null if the id is unknown.
|
|
251
|
-
*/
|
|
252
|
-
export function markShared(id, { slug, ownerEmail = '', role = 'owner' } = {}, env = process.env) {
|
|
253
|
-
if (!slug || typeof slug !== 'string') throw new Error('markShared requires a slug');
|
|
254
|
-
const list = readRaw(env);
|
|
255
|
-
const w = list.find((x) => x.id === id);
|
|
256
|
-
if (!w) return null;
|
|
257
|
-
w.kind = 'shared';
|
|
258
|
-
w.shared = sanitizeShared({
|
|
259
|
-
slug,
|
|
260
|
-
ownerEmail,
|
|
261
|
-
role,
|
|
262
|
-
sharedAt: w.shared?.sharedAt || nowMs(),
|
|
263
|
-
// Preserve a file-sync link already recorded (re-share/re-mark must not drop it).
|
|
264
|
-
projectCode: w.shared?.projectCode,
|
|
265
|
-
});
|
|
266
|
-
writeRaw(list, env);
|
|
267
|
-
return w;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Record the file-sync project a shared workspace replicates through, once the
|
|
272
|
-
* daemon has paired the folder (Slice 3 "going live"). The entry must already be
|
|
273
|
-
* `kind:'shared'`. Idempotent. Returns the updated entry, or null if the id is
|
|
274
|
-
* unknown / not shared.
|
|
275
|
-
*/
|
|
276
|
-
export function setSyncProject(id, projectCode, env = process.env) {
|
|
277
|
-
if (!projectCode || typeof projectCode !== 'string') {
|
|
278
|
-
throw new Error('setSyncProject requires a projectCode');
|
|
279
|
-
}
|
|
280
|
-
const list = readRaw(env);
|
|
281
|
-
const w = list.find((x) => x.id === id);
|
|
282
|
-
if (!w || w.kind !== 'shared' || !w.shared) return null;
|
|
283
|
-
w.shared = sanitizeShared({ ...w.shared, projectCode });
|
|
284
|
-
writeRaw(list, env);
|
|
285
|
-
return w;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/** Remove a workspace from the LIST only. NEVER deletes the folder or its files. */
|
|
289
|
-
export function removeWorkspace(id, env = process.env) {
|
|
290
|
-
const list = readRaw(env);
|
|
291
|
-
const next = list.filter((w) => w.id !== id);
|
|
292
|
-
if (next.length === list.length) return false;
|
|
293
|
-
writeRaw(next, env);
|
|
294
|
-
return true;
|
|
295
|
-
}
|
|
1
|
+
// Workspace registry — the per-install list of LOCAL workspaces (lobby M1).
|
|
2
|
+
//
|
|
3
|
+
// One install can hold many workspaces; this is the list the lobby shows and
|
|
4
|
+
// switches between. Stored in the GLOBAL dir (~/.wild-workspace/workspaces.json),
|
|
5
|
+
// NEVER inside a synced workspace folder (core principle #1: the user's repo
|
|
6
|
+
// folder is sacred — no system state in it).
|
|
7
|
+
//
|
|
8
|
+
// Each entry points at a folder on THIS host. The server re-roots to the chosen
|
|
9
|
+
// workspace per request via the X-Workspace-Id seam (see index.mjs `workspaceFor`).
|
|
10
|
+
// Creating/opening a workspace is a HOST action (the folder lives here); a remote
|
|
11
|
+
// browser only ENTERS existing ones — enforced at the route, not here.
|
|
12
|
+
//
|
|
13
|
+
// A workspace is `kind:'local'` until the owner explicitly SHARES it (sharing
|
|
14
|
+
// slice / design §4 decision 7): `markShared` promotes the entry to `kind:'shared'`
|
|
15
|
+
// and records a `shared:{slug,ownerEmail,role,sharedAt}` block — the slug is the
|
|
16
|
+
// workspace's first-class identity on the bmo-sync rails (membership/canvas), minted
|
|
17
|
+
// by `POST /api/workspaces/create`. The folder stays exactly where it is; promoting
|
|
18
|
+
// is a metadata flip on the LIST, never a move. `removeWorkspace` drops a folder
|
|
19
|
+
// from the LIST only — it never deletes files.
|
|
20
|
+
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
import os from 'node:os';
|
|
24
|
+
import crypto from 'node:crypto';
|
|
25
|
+
import { globalDir } from './logpaths.mjs';
|
|
26
|
+
|
|
27
|
+
const FILE = 'workspaces.json';
|
|
28
|
+
|
|
29
|
+
function registryPath(env = process.env) {
|
|
30
|
+
return path.join(globalDir(env), FILE);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Where "New workspace" (name-only, no path) creates folders — a visible home so
|
|
34
|
+
// the user always knows where their data lives (design §8.1). Overridable for tests.
|
|
35
|
+
function workspacesHome(env = process.env) {
|
|
36
|
+
return env.WILD_WORKSPACE_HOME || path.join(os.homedir(), 'Workspaces');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// A stable id derived from the absolute dir, so re-adding the same folder is
|
|
40
|
+
// idempotent and the id survives a display-name rename. Case-folded on win/mac
|
|
41
|
+
// (case-insensitive filesystems) so one folder maps to exactly one id. Exported
|
|
42
|
+
// so the server can compute the boot/default workspace's id WITHOUT writing the
|
|
43
|
+
// registry file (keeps startup side-effect-free + test-pollution-free).
|
|
44
|
+
export function workspaceIdForDir(dir) {
|
|
45
|
+
const abs = path.resolve(dir);
|
|
46
|
+
const key = process.platform === 'linux' ? abs : abs.toLowerCase();
|
|
47
|
+
return 'ws_' + crypto.createHash('sha256').update(key).digest('hex').slice(0, 12);
|
|
48
|
+
}
|
|
49
|
+
const idForDir = workspaceIdForDir;
|
|
50
|
+
|
|
51
|
+
function nameForDir(dir) {
|
|
52
|
+
return path.basename(path.resolve(dir)) || 'workspace';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// folder-name-safe slug of a display name (for the New-workspace folder).
|
|
56
|
+
function folderSlug(name) {
|
|
57
|
+
const s = String(name || '')
|
|
58
|
+
.trim()
|
|
59
|
+
.toLowerCase()
|
|
60
|
+
.replace(/[^\p{L}\p{N}]+/gu, '-')
|
|
61
|
+
.replace(/^-+|-+$/g, '')
|
|
62
|
+
.slice(0, 60);
|
|
63
|
+
return s || 'workspace';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function readRaw(env) {
|
|
67
|
+
try {
|
|
68
|
+
const parsed = JSON.parse(fs.readFileSync(registryPath(env), 'utf8'));
|
|
69
|
+
if (Array.isArray(parsed?.workspaces)) {
|
|
70
|
+
return parsed.workspaces.map(sanitize).filter(Boolean);
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
/* missing / unreadable / malformed → empty */
|
|
74
|
+
}
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// A shared workspace carries a `shared` block (the rails identity + the local
|
|
79
|
+
// member's role). Only kept when the entry is actually `kind:'shared'` AND the
|
|
80
|
+
// block has a slug; anything else collapses back to a plain local entry so a
|
|
81
|
+
// half-written record can't masquerade as shared.
|
|
82
|
+
function sanitizeShared(s) {
|
|
83
|
+
if (!s || typeof s !== 'object' || typeof s.slug !== 'string' || !s.slug.trim()) return null;
|
|
84
|
+
const out = {
|
|
85
|
+
slug: s.slug.trim().toLowerCase(),
|
|
86
|
+
ownerEmail: typeof s.ownerEmail === 'string' ? s.ownerEmail : '',
|
|
87
|
+
role: s.role === 'owner' ? 'owner' : 'member',
|
|
88
|
+
sharedAt: Number(s.sharedAt) || 0,
|
|
89
|
+
};
|
|
90
|
+
// Slice 3: the file-sync project this workspace replicates through, recorded
|
|
91
|
+
// once the daemon is paired ("going live"). Absent until file-sync starts.
|
|
92
|
+
if (typeof s.projectCode === 'string' && s.projectCode.trim()) {
|
|
93
|
+
out.projectCode = s.projectCode.trim();
|
|
94
|
+
}
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function sanitize(e) {
|
|
99
|
+
if (!e || typeof e !== 'object' || typeof e.dir !== 'string' || !e.dir) return null;
|
|
100
|
+
const dir = path.resolve(e.dir);
|
|
101
|
+
const shared = sanitizeShared(e.shared);
|
|
102
|
+
const out = {
|
|
103
|
+
id: typeof e.id === 'string' && e.id ? e.id : idForDir(dir),
|
|
104
|
+
name: typeof e.name === 'string' && e.name.trim() ? e.name.trim() : nameForDir(dir),
|
|
105
|
+
dir,
|
|
106
|
+
kind: e.kind === 'shared' && shared ? 'shared' : 'local',
|
|
107
|
+
createdAt: Number(e.createdAt) || nowMs(),
|
|
108
|
+
lastOpenedAt: Number(e.lastOpenedAt) || 0,
|
|
109
|
+
};
|
|
110
|
+
if (out.kind === 'shared') out.shared = shared;
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function writeRaw(list, env) {
|
|
115
|
+
const dir = globalDir(env);
|
|
116
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
117
|
+
const tmp = `${registryPath(env)}.tmp`;
|
|
118
|
+
fs.writeFileSync(tmp, JSON.stringify({ workspaces: list }, null, 2), { mode: 0o600 });
|
|
119
|
+
fs.renameSync(tmp, registryPath(env)); // atomic-ish replace
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function nowMs() {
|
|
123
|
+
return Date.now();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Recency ordering (listWorkspaces) needs strictly-increasing open stamps. Two
|
|
127
|
+
// opens in the same millisecond — easy on a fast machine or CI — would otherwise
|
|
128
|
+
// tie on lastOpenedAt and fall back to the createdAt tiebreak, which does NOT
|
|
129
|
+
// reflect open-order, so the just-touched workspace could fail to sort to the
|
|
130
|
+
// front. Clamp every new open stamp strictly above every existing stamp so the
|
|
131
|
+
// order is deterministic regardless of clock resolution.
|
|
132
|
+
function nextOpenStamp(list) {
|
|
133
|
+
let max = 0;
|
|
134
|
+
for (const w of list) {
|
|
135
|
+
if (w.lastOpenedAt > max) max = w.lastOpenedAt;
|
|
136
|
+
if (w.createdAt > max) max = w.createdAt;
|
|
137
|
+
}
|
|
138
|
+
return Math.max(nowMs(), max + 1);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// --- public API ------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
/** All workspaces, most-recently-opened first (then newest-created). */
|
|
144
|
+
export function listWorkspaces(env = process.env) {
|
|
145
|
+
return readRaw(env).sort(
|
|
146
|
+
(a, b) => b.lastOpenedAt - a.lastOpenedAt || b.createdAt - a.createdAt,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** One workspace by id, or null. */
|
|
151
|
+
export function getWorkspace(id, env = process.env) {
|
|
152
|
+
return readRaw(env).find((w) => w.id === id) || null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Find a registered workspace by its directory (idempotency helper), or null. */
|
|
156
|
+
export function findByDir(dir, env = process.env) {
|
|
157
|
+
const want = idForDir(dir);
|
|
158
|
+
return readRaw(env).find((w) => w.id === want) || null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Ensure `dir` exists in the registry (idempotent) — used at startup to seed the
|
|
163
|
+
* boot/default workspace so the lobby is never blank for an existing install.
|
|
164
|
+
* Does NOT touch lastOpenedAt. Returns the entry.
|
|
165
|
+
*/
|
|
166
|
+
export function seedDefault({ dir, name } = {}, env = process.env) {
|
|
167
|
+
if (!dir) return null;
|
|
168
|
+
const list = readRaw(env);
|
|
169
|
+
const existing = list.find((w) => w.id === idForDir(dir));
|
|
170
|
+
if (existing) return existing;
|
|
171
|
+
const entry = sanitize({
|
|
172
|
+
dir,
|
|
173
|
+
name: name || nameForDir(dir),
|
|
174
|
+
createdAt: nowMs(),
|
|
175
|
+
lastOpenedAt: nowMs(), // the boot workspace is "the one you were just in"
|
|
176
|
+
});
|
|
177
|
+
list.push(entry);
|
|
178
|
+
writeRaw(list, env);
|
|
179
|
+
return entry;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Create a NEW local workspace. With `dir` → register that existing folder
|
|
184
|
+
* ("Open an existing folder"). With only `name` → make a fresh folder under the
|
|
185
|
+
* Workspaces home ("New workspace"); collisions get a -2/-3 suffix. Idempotent on
|
|
186
|
+
* an already-registered dir (returns it, touched). HOST-only is enforced at the route.
|
|
187
|
+
*/
|
|
188
|
+
export function createWorkspace({ name, dir } = {}, env = process.env) {
|
|
189
|
+
let targetDir;
|
|
190
|
+
let isNew = false;
|
|
191
|
+
|
|
192
|
+
if (dir) {
|
|
193
|
+
targetDir = path.resolve(dir);
|
|
194
|
+
if (!fs.existsSync(targetDir)) {
|
|
195
|
+
throw new Error(`folder does not exist: ${targetDir}`);
|
|
196
|
+
}
|
|
197
|
+
if (!fs.statSync(targetDir).isDirectory()) {
|
|
198
|
+
throw new Error(`not a folder: ${targetDir}`);
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
const home = workspacesHome(env);
|
|
202
|
+
fs.mkdirSync(home, { recursive: true });
|
|
203
|
+
const base = folderSlug(name);
|
|
204
|
+
let candidate = path.join(home, base);
|
|
205
|
+
let n = 2;
|
|
206
|
+
while (fs.existsSync(candidate)) {
|
|
207
|
+
candidate = path.join(home, `${base}-${n}`);
|
|
208
|
+
n += 1;
|
|
209
|
+
}
|
|
210
|
+
fs.mkdirSync(candidate, { recursive: true });
|
|
211
|
+
targetDir = candidate;
|
|
212
|
+
isNew = true;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const list = readRaw(env);
|
|
216
|
+
const id = idForDir(targetDir);
|
|
217
|
+
const existing = list.find((w) => w.id === id);
|
|
218
|
+
if (existing) {
|
|
219
|
+
existing.lastOpenedAt = nextOpenStamp(list);
|
|
220
|
+
if (name && isNew) existing.name = name;
|
|
221
|
+
writeRaw(list, env);
|
|
222
|
+
return existing;
|
|
223
|
+
}
|
|
224
|
+
const entry = sanitize({
|
|
225
|
+
dir: targetDir,
|
|
226
|
+
name: name || nameForDir(targetDir),
|
|
227
|
+
createdAt: nowMs(),
|
|
228
|
+
lastOpenedAt: nextOpenStamp(list),
|
|
229
|
+
});
|
|
230
|
+
list.push(entry);
|
|
231
|
+
writeRaw(list, env);
|
|
232
|
+
return entry;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Mark a workspace opened now (drives the recency sort + pre-select-last). */
|
|
236
|
+
export function touchWorkspace(id, env = process.env) {
|
|
237
|
+
const list = readRaw(env);
|
|
238
|
+
const w = list.find((x) => x.id === id);
|
|
239
|
+
if (!w) return null;
|
|
240
|
+
w.lastOpenedAt = nextOpenStamp(list);
|
|
241
|
+
writeRaw(list, env);
|
|
242
|
+
return w;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Promote a registered workspace to SHARED (sharing slice / design §4 decision 7).
|
|
247
|
+
* The workspace must already be in the registry (the boot/default workspace is
|
|
248
|
+
* seeded first by the caller). Records the rails identity (`slug`) + the local
|
|
249
|
+
* member's role; the folder is untouched. Idempotent: re-sharing updates the block.
|
|
250
|
+
* Returns the updated entry, or null if the id is unknown.
|
|
251
|
+
*/
|
|
252
|
+
export function markShared(id, { slug, ownerEmail = '', role = 'owner' } = {}, env = process.env) {
|
|
253
|
+
if (!slug || typeof slug !== 'string') throw new Error('markShared requires a slug');
|
|
254
|
+
const list = readRaw(env);
|
|
255
|
+
const w = list.find((x) => x.id === id);
|
|
256
|
+
if (!w) return null;
|
|
257
|
+
w.kind = 'shared';
|
|
258
|
+
w.shared = sanitizeShared({
|
|
259
|
+
slug,
|
|
260
|
+
ownerEmail,
|
|
261
|
+
role,
|
|
262
|
+
sharedAt: w.shared?.sharedAt || nowMs(),
|
|
263
|
+
// Preserve a file-sync link already recorded (re-share/re-mark must not drop it).
|
|
264
|
+
projectCode: w.shared?.projectCode,
|
|
265
|
+
});
|
|
266
|
+
writeRaw(list, env);
|
|
267
|
+
return w;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Record the file-sync project a shared workspace replicates through, once the
|
|
272
|
+
* daemon has paired the folder (Slice 3 "going live"). The entry must already be
|
|
273
|
+
* `kind:'shared'`. Idempotent. Returns the updated entry, or null if the id is
|
|
274
|
+
* unknown / not shared.
|
|
275
|
+
*/
|
|
276
|
+
export function setSyncProject(id, projectCode, env = process.env) {
|
|
277
|
+
if (!projectCode || typeof projectCode !== 'string') {
|
|
278
|
+
throw new Error('setSyncProject requires a projectCode');
|
|
279
|
+
}
|
|
280
|
+
const list = readRaw(env);
|
|
281
|
+
const w = list.find((x) => x.id === id);
|
|
282
|
+
if (!w || w.kind !== 'shared' || !w.shared) return null;
|
|
283
|
+
w.shared = sanitizeShared({ ...w.shared, projectCode });
|
|
284
|
+
writeRaw(list, env);
|
|
285
|
+
return w;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Remove a workspace from the LIST only. NEVER deletes the folder or its files. */
|
|
289
|
+
export function removeWorkspace(id, env = process.env) {
|
|
290
|
+
const list = readRaw(env);
|
|
291
|
+
const next = list.filter((w) => w.id !== id);
|
|
292
|
+
if (next.length === list.length) return false;
|
|
293
|
+
writeRaw(next, env);
|
|
294
|
+
return true;
|
|
295
|
+
}
|