@primitive.ai/prim 0.1.0-alpha.10 → 0.1.0-alpha.100
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/README.md +413 -37
- package/SKILL.md +272 -0
- package/dist/chunk-2IRLFOEL.js +766 -0
- package/dist/chunk-2Q3OGSTV.js +164 -0
- package/dist/chunk-4GLVJLEQ.js +55 -0
- package/dist/chunk-5HFTJEA6.js +168 -0
- package/dist/chunk-5LRKJTLA.js +155 -0
- package/dist/chunk-6C3CCVDI.js +264 -0
- package/dist/chunk-7VIMU2HR.js +209 -0
- package/dist/chunk-C4D2LMTS.js +511 -0
- package/dist/chunk-EZXEWFDG.js +210 -0
- package/dist/chunk-GSBHBU7N.js +18982 -0
- package/dist/chunk-H6JAQVW4.js +258 -0
- package/dist/chunk-IDMLJ4TM.js +36 -0
- package/dist/chunk-JJI7FU24.js +439 -0
- package/dist/chunk-K4JWJ6OM.js +1496 -0
- package/dist/chunk-KGBMU76T.js +175 -0
- package/dist/chunk-MXVCP33E.js +847 -0
- package/dist/chunk-MZEVEPL3.js +262 -0
- package/dist/chunk-O2WV5SQZ.js +1876 -0
- package/dist/chunk-OYHYCK4K.js +13 -0
- package/dist/chunk-QIHXTYRD.js +135 -0
- package/dist/chunk-XG7S3LIL.js +1372 -0
- package/dist/daemon/server.js +22644 -0
- package/dist/hook-runtime/daemon/server.js +22644 -0
- package/dist/hook-runtime/hooks/post-commit.js +20133 -0
- package/dist/hook-runtime/hooks/post-rewrite.js +20032 -0
- package/dist/hook-runtime/hooks/post-tool-use.js +27307 -0
- package/dist/hook-runtime/hooks/pre-commit.js +1657 -0
- package/dist/hook-runtime/hooks/pre-tool-use.js +26714 -0
- package/dist/hook-runtime/hooks/prim-hook.js +27367 -0
- package/dist/hook-runtime/hooks/session-end.js +1176 -0
- package/dist/hook-runtime/hooks/session-start.js +30167 -0
- package/dist/hook-runtime/index.js +53486 -0
- package/dist/hook-runtime/statusline-main.js +886 -0
- package/dist/hooks/post-commit.js +196 -0
- package/dist/hooks/post-rewrite.js +205 -0
- package/dist/hooks/post-tool-use.js +285 -0
- package/dist/hooks/pre-commit.js +15 -99
- package/dist/hooks/pre-tool-use.js +387 -0
- package/dist/hooks/prim-hook.js +235 -0
- package/dist/hooks/session-end.js +64 -0
- package/dist/hooks/session-start.js +309 -0
- package/dist/index.js +9721 -428
- package/dist/statusline-main.js +886 -0
- package/package.json +35 -13
- package/dist/chunk-3APLWTLB.js +0 -165
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSiteUrl
|
|
3
|
+
} from "./chunk-2IRLFOEL.js";
|
|
4
|
+
import {
|
|
5
|
+
atomicWriteFile,
|
|
6
|
+
ensureDurableDirectory,
|
|
7
|
+
jwtOrganizationId,
|
|
8
|
+
primConfigDirectory,
|
|
9
|
+
resolveAuthCredential
|
|
10
|
+
} from "./chunk-XG7S3LIL.js";
|
|
11
|
+
|
|
12
|
+
// src/journal.ts
|
|
13
|
+
import {
|
|
14
|
+
appendFileSync,
|
|
15
|
+
closeSync,
|
|
16
|
+
existsSync as existsSync2,
|
|
17
|
+
fstatSync,
|
|
18
|
+
mkdirSync,
|
|
19
|
+
openSync,
|
|
20
|
+
opendirSync,
|
|
21
|
+
readFileSync as readFileSync2,
|
|
22
|
+
readSync,
|
|
23
|
+
readdirSync as readdirSync2
|
|
24
|
+
} from "fs";
|
|
25
|
+
import { dirname as dirname2, join as join2 } from "path";
|
|
26
|
+
|
|
27
|
+
// src/drain-progress.ts
|
|
28
|
+
import {
|
|
29
|
+
chmodSync,
|
|
30
|
+
existsSync,
|
|
31
|
+
readFileSync,
|
|
32
|
+
readdirSync,
|
|
33
|
+
rmSync,
|
|
34
|
+
rmdirSync
|
|
35
|
+
} from "fs";
|
|
36
|
+
import { basename, dirname, join } from "path";
|
|
37
|
+
var DIRECTORY_MODE = 448;
|
|
38
|
+
var FILE_MODE = 384;
|
|
39
|
+
var DRAIN_PROGRESS_DIRNAME = "drain-progress";
|
|
40
|
+
var CHECKPOINT_NAME = /^(.+)\.json(?:\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.tmp)?$/;
|
|
41
|
+
function rotationIdentity(stat) {
|
|
42
|
+
return { size: stat.size, ino: stat.ino, dev: stat.dev };
|
|
43
|
+
}
|
|
44
|
+
function drainProgressDirectory(flushingPath) {
|
|
45
|
+
return join(dirname(flushingPath), DRAIN_PROGRESS_DIRNAME);
|
|
46
|
+
}
|
|
47
|
+
function drainProgressPath(flushingPath) {
|
|
48
|
+
return join(drainProgressDirectory(flushingPath), `${basename(flushingPath)}.json`);
|
|
49
|
+
}
|
|
50
|
+
function isByteCount(value) {
|
|
51
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
52
|
+
}
|
|
53
|
+
function drainResumeOffset(flushingPath, rotation) {
|
|
54
|
+
let parsed;
|
|
55
|
+
try {
|
|
56
|
+
parsed = JSON.parse(readFileSync(drainProgressPath(flushingPath), "utf8"));
|
|
57
|
+
} catch {
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
const { v, offset, size, ino, dev } = parsed;
|
|
64
|
+
if (v !== 2 || ino !== rotation.ino || dev !== rotation.dev || size !== rotation.size || !isByteCount(offset) || offset > rotation.size) {
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
return offset;
|
|
68
|
+
}
|
|
69
|
+
function writeDrainCheckpoint(flushingPath, checkpoint) {
|
|
70
|
+
const directory = drainProgressDirectory(flushingPath);
|
|
71
|
+
ensureDurableDirectory(directory, DIRECTORY_MODE);
|
|
72
|
+
chmodSync(directory, DIRECTORY_MODE);
|
|
73
|
+
atomicWriteFile(drainProgressPath(flushingPath), `${JSON.stringify(checkpoint)}
|
|
74
|
+
`, {
|
|
75
|
+
mode: FILE_MODE
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function removeEmptyDirectory(directory) {
|
|
79
|
+
try {
|
|
80
|
+
rmdirSync(directory);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
const code = error.code;
|
|
83
|
+
if (code !== "ENOENT" && code !== "ENOTEMPTY" && code !== "EEXIST") {
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function removeDrainCheckpoint(flushingPath) {
|
|
89
|
+
rmSync(drainProgressPath(flushingPath), { force: true });
|
|
90
|
+
removeEmptyDirectory(drainProgressDirectory(flushingPath));
|
|
91
|
+
}
|
|
92
|
+
function sweepOrphanedDrainCheckpoints(bucketDirectory) {
|
|
93
|
+
const directory = join(bucketDirectory, DRAIN_PROGRESS_DIRNAME);
|
|
94
|
+
let names;
|
|
95
|
+
try {
|
|
96
|
+
names = readdirSync(directory);
|
|
97
|
+
} catch {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
for (const name of names) {
|
|
101
|
+
const rotation = CHECKPOINT_NAME.exec(name)?.[1];
|
|
102
|
+
if (rotation === void 0 || existsSync(join(bucketDirectory, rotation))) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
rmSync(join(directory, name), { force: true });
|
|
107
|
+
} catch {
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
removeEmptyDirectory(directory);
|
|
112
|
+
} catch {
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/journal.ts
|
|
117
|
+
var JOURNAL_DIR = join2(primConfigDirectory(), "moves");
|
|
118
|
+
var UNBOUND_BUCKET = "_unbound";
|
|
119
|
+
var JOURNAL_BASENAME = "journal.ndjson";
|
|
120
|
+
var JOURNAL_STATS_SAMPLE_BYTES = 64 * 1024;
|
|
121
|
+
var PENDING_STATS_MAX_FILES = 128;
|
|
122
|
+
function envSlug(apiUrl) {
|
|
123
|
+
const slug = apiUrl.replace(/^https?:\/\//, "").replace(/[^a-zA-Z0-9._-]/g, "_").replace(/^_+|_+$/g, "").toLowerCase();
|
|
124
|
+
if (slug.length === 0 || slug === "." || slug === "..") {
|
|
125
|
+
return "default";
|
|
126
|
+
}
|
|
127
|
+
return slug;
|
|
128
|
+
}
|
|
129
|
+
function currentEnvDir() {
|
|
130
|
+
return join2(JOURNAL_DIR, envSlug(getSiteUrl()));
|
|
131
|
+
}
|
|
132
|
+
var DIR_MODE = 448;
|
|
133
|
+
var FILE_MODE2 = 384;
|
|
134
|
+
var SAFE_BUCKET = /^[A-Za-z0-9_-]+$/;
|
|
135
|
+
var RESERVED_BUCKETS = /* @__PURE__ */ new Set([UNBOUND_BUCKET, "_legacy"]);
|
|
136
|
+
function appendMoveToPath(path, move) {
|
|
137
|
+
const dir = dirname2(path);
|
|
138
|
+
if (!existsSync2(dir)) {
|
|
139
|
+
mkdirSync(dir, { recursive: true, mode: DIR_MODE });
|
|
140
|
+
}
|
|
141
|
+
appendFileSync(path, `${JSON.stringify(move)}
|
|
142
|
+
`, { mode: FILE_MODE2 });
|
|
143
|
+
}
|
|
144
|
+
function bucketDir(orgId) {
|
|
145
|
+
const safe = orgId !== void 0 && SAFE_BUCKET.test(orgId) && !RESERVED_BUCKETS.has(orgId);
|
|
146
|
+
return join2(currentEnvDir(), safe ? orgId : UNBOUND_BUCKET);
|
|
147
|
+
}
|
|
148
|
+
function journalPath(orgId) {
|
|
149
|
+
return join2(bucketDir(orgId), JOURNAL_BASENAME);
|
|
150
|
+
}
|
|
151
|
+
function appendMove(move, orgId) {
|
|
152
|
+
appendMoveToPath(journalPath(orgId), move);
|
|
153
|
+
}
|
|
154
|
+
function parseMoves(content) {
|
|
155
|
+
const moves = [];
|
|
156
|
+
for (const line of content.split("\n")) {
|
|
157
|
+
if (line.length === 0) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
moves.push(JSON.parse(line));
|
|
162
|
+
} catch {
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return moves;
|
|
166
|
+
}
|
|
167
|
+
function readMovesFromPath(path) {
|
|
168
|
+
if (!existsSync2(path)) {
|
|
169
|
+
return [];
|
|
170
|
+
}
|
|
171
|
+
return parseMoves(readFileSync2(path, "utf-8"));
|
|
172
|
+
}
|
|
173
|
+
function listBuckets() {
|
|
174
|
+
const out = [];
|
|
175
|
+
const envDir = currentEnvDir();
|
|
176
|
+
if (!existsSync2(envDir)) {
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
for (const entry of readdirSync2(envDir, { withFileTypes: true })) {
|
|
180
|
+
if (!entry.isDirectory()) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const path = join2(envDir, entry.name, JOURNAL_BASENAME);
|
|
184
|
+
if (existsSync2(path)) {
|
|
185
|
+
out.push({ bucket: entry.name, path });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return out;
|
|
189
|
+
}
|
|
190
|
+
var FLUSHING_PREFIX = `${JOURNAL_BASENAME}.flushing.`;
|
|
191
|
+
function oldestCapturedAt(moves) {
|
|
192
|
+
let oldest;
|
|
193
|
+
for (const move of moves) {
|
|
194
|
+
if (!Number.isFinite(move.capturedAt)) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
oldest = oldest === void 0 ? move.capturedAt : Math.min(oldest, move.capturedAt);
|
|
198
|
+
}
|
|
199
|
+
return oldest;
|
|
200
|
+
}
|
|
201
|
+
function sampleJournalFile(path, maxBytes = JOURNAL_STATS_SAMPLE_BYTES, options = {}) {
|
|
202
|
+
const fd = openSync(path, "r");
|
|
203
|
+
try {
|
|
204
|
+
const initial = fstatSync(fd);
|
|
205
|
+
const start = options.fromDrainCheckpoint ? drainResumeOffset(path, rotationIdentity(initial)) : 0;
|
|
206
|
+
const target = Math.min(initial.size - start, Math.max(0, maxBytes));
|
|
207
|
+
const buffer = Buffer.allocUnsafe(target);
|
|
208
|
+
let sampledBytes = 0;
|
|
209
|
+
while (sampledBytes < target) {
|
|
210
|
+
const count = readSync(fd, buffer, sampledBytes, target - sampledBytes, start + sampledBytes);
|
|
211
|
+
if (count === 0) {
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
sampledBytes += count;
|
|
215
|
+
}
|
|
216
|
+
const stat = fstatSync(fd);
|
|
217
|
+
const sampled = stat.size - start > sampledBytes;
|
|
218
|
+
let content = buffer.subarray(0, sampledBytes).toString("utf-8");
|
|
219
|
+
if (sampled) {
|
|
220
|
+
const lastNewline = content.lastIndexOf("\n");
|
|
221
|
+
content = lastNewline === -1 ? "" : content.slice(0, lastNewline + 1);
|
|
222
|
+
}
|
|
223
|
+
const lines = content.split("\n").filter((line) => line.length > 0);
|
|
224
|
+
const moves = parseMoves(content);
|
|
225
|
+
return {
|
|
226
|
+
sizeBytes: stat.size,
|
|
227
|
+
pendingBytes: Math.max(0, stat.size - start),
|
|
228
|
+
mtimeMs: stat.mtimeMs,
|
|
229
|
+
lineCount: sampled && stat.size > 0 ? Math.max(1, lines.length) : lines.length,
|
|
230
|
+
oldestCapturedAt: oldestCapturedAt(moves),
|
|
231
|
+
sampled,
|
|
232
|
+
sampledBytes
|
|
233
|
+
};
|
|
234
|
+
} finally {
|
|
235
|
+
closeSync(fd);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function parseFlushingPid(name) {
|
|
239
|
+
const segments = name.slice(FLUSHING_PREFIX.length).split(".");
|
|
240
|
+
const last = segments.length >= 2 ? segments[segments.length - 1] : void 0;
|
|
241
|
+
if (last === void 0 || !/^[0-9]+$/.test(last)) return void 0;
|
|
242
|
+
const pid = Number(last);
|
|
243
|
+
return Number.isSafeInteger(pid) && pid > 0 ? pid : void 0;
|
|
244
|
+
}
|
|
245
|
+
function listFlushingInDir(dir, bucket, options = {}) {
|
|
246
|
+
if (!existsSync2(dir)) {
|
|
247
|
+
return [];
|
|
248
|
+
}
|
|
249
|
+
const out = [];
|
|
250
|
+
let remainingBytes = options.sampleBytes ?? JOURNAL_STATS_SAMPLE_BYTES;
|
|
251
|
+
const maxFiles = options.maxFiles ?? Number.POSITIVE_INFINITY;
|
|
252
|
+
const entries = opendirSync(dir);
|
|
253
|
+
try {
|
|
254
|
+
let entry = entries.readSync();
|
|
255
|
+
while (entry && out.length < maxFiles) {
|
|
256
|
+
if (!entry.isFile() || !entry.name.startsWith(FLUSHING_PREFIX)) {
|
|
257
|
+
entry = entries.readSync();
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
const path = join2(dir, entry.name);
|
|
261
|
+
let sample;
|
|
262
|
+
try {
|
|
263
|
+
sample = sampleJournalFile(path, remainingBytes, { fromDrainCheckpoint: true });
|
|
264
|
+
} catch (err) {
|
|
265
|
+
if (err.code === "ENOENT") {
|
|
266
|
+
entry = entries.readSync();
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
throw err;
|
|
270
|
+
}
|
|
271
|
+
remainingBytes = Math.max(0, remainingBytes - sample.sampledBytes);
|
|
272
|
+
out.push({
|
|
273
|
+
bucket,
|
|
274
|
+
path,
|
|
275
|
+
pid: parseFlushingPid(entry.name),
|
|
276
|
+
...sample
|
|
277
|
+
});
|
|
278
|
+
entry = entries.readSync();
|
|
279
|
+
}
|
|
280
|
+
} finally {
|
|
281
|
+
entries.closeSync();
|
|
282
|
+
}
|
|
283
|
+
return out;
|
|
284
|
+
}
|
|
285
|
+
function listFlushing(options = {}) {
|
|
286
|
+
const envDir = currentEnvDir();
|
|
287
|
+
if (!existsSync2(envDir)) {
|
|
288
|
+
return [];
|
|
289
|
+
}
|
|
290
|
+
const out = [];
|
|
291
|
+
let remainingBytes = options.sampleBytes ?? JOURNAL_STATS_SAMPLE_BYTES;
|
|
292
|
+
const maxFiles = options.maxFiles ?? Number.POSITIVE_INFINITY;
|
|
293
|
+
for (const entry of readdirSync2(envDir, { withFileTypes: true })) {
|
|
294
|
+
if (out.length >= maxFiles) {
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
if (entry.isDirectory()) {
|
|
298
|
+
const found = listFlushingInDir(join2(envDir, entry.name), entry.name, {
|
|
299
|
+
sampleBytes: remainingBytes,
|
|
300
|
+
maxFiles: maxFiles - out.length
|
|
301
|
+
});
|
|
302
|
+
out.push(...found);
|
|
303
|
+
remainingBytes = Math.max(
|
|
304
|
+
0,
|
|
305
|
+
remainingBytes - found.reduce((count, file) => count + file.sampledBytes, 0)
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return out;
|
|
310
|
+
}
|
|
311
|
+
function sweepOrphanedDrainProgress() {
|
|
312
|
+
const envDir = currentEnvDir();
|
|
313
|
+
if (!existsSync2(envDir)) {
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
for (const entry of readdirSync2(envDir, { withFileTypes: true })) {
|
|
317
|
+
if (entry.isDirectory()) {
|
|
318
|
+
sweepOrphanedDrainCheckpoints(join2(envDir, entry.name));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function bucketStats(options = {}) {
|
|
323
|
+
const out = [];
|
|
324
|
+
let remainingBytes = options.sampleBytes ?? JOURNAL_STATS_SAMPLE_BYTES;
|
|
325
|
+
const maxFiles = options.maxFiles ?? Number.POSITIVE_INFINITY;
|
|
326
|
+
for (const { bucket, path } of listBuckets()) {
|
|
327
|
+
if (out.length >= maxFiles) {
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
let sample;
|
|
331
|
+
try {
|
|
332
|
+
sample = sampleJournalFile(path, remainingBytes);
|
|
333
|
+
} catch (err) {
|
|
334
|
+
if (err.code === "ENOENT") {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
throw err;
|
|
338
|
+
}
|
|
339
|
+
remainingBytes = Math.max(0, remainingBytes - sample.sampledBytes);
|
|
340
|
+
out.push({
|
|
341
|
+
bucket,
|
|
342
|
+
path,
|
|
343
|
+
...sample
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
return out;
|
|
347
|
+
}
|
|
348
|
+
function pendingJournalStats() {
|
|
349
|
+
const perKindBytes = Math.floor(JOURNAL_STATS_SAMPLE_BYTES / 2);
|
|
350
|
+
const perKindFiles = Math.floor(PENDING_STATS_MAX_FILES / 2);
|
|
351
|
+
const live = bucketStats({ sampleBytes: perKindBytes, maxFiles: perKindFiles });
|
|
352
|
+
const stranded = listFlushing({ sampleBytes: perKindBytes, maxFiles: perKindFiles });
|
|
353
|
+
const timestamps = [...live, ...stranded].map((entry) => entry.oldestCapturedAt).filter((value) => value !== void 0);
|
|
354
|
+
const liveSampled = live.some((entry) => entry.sampled) || live.length >= perKindFiles;
|
|
355
|
+
const strandedSampled = stranded.some((entry) => entry.sampled) || stranded.length >= perKindFiles;
|
|
356
|
+
return {
|
|
357
|
+
pendingCount: live.reduce((count, entry) => count + entry.lineCount, 0) + stranded.reduce((count, entry) => count + entry.lineCount, 0),
|
|
358
|
+
oldestPendingAt: timestamps.length > 0 ? Math.min(...timestamps) : void 0,
|
|
359
|
+
strandedCount: stranded.reduce((count, entry) => count + entry.lineCount, 0),
|
|
360
|
+
strandedFileCount: stranded.length,
|
|
361
|
+
sampled: liveSampled || strandedSampled,
|
|
362
|
+
strandedSampled
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/binding.ts
|
|
367
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
368
|
+
import { dirname as dirname3, join as join3 } from "path";
|
|
369
|
+
var PRIM_CONFIG_DIR = primConfigDirectory();
|
|
370
|
+
var SESSIONS_DIR = join3(PRIM_CONFIG_DIR, "sessions");
|
|
371
|
+
var WORKSPACE_FILE = ".prim/workspace.json";
|
|
372
|
+
function readJsonSafe(path) {
|
|
373
|
+
if (!existsSync3(path)) {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
try {
|
|
377
|
+
return JSON.parse(readFileSync3(path, "utf-8"));
|
|
378
|
+
} catch {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function fromSessionMarker(sessionId) {
|
|
383
|
+
if (!sessionId) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const marker = readJsonSafe(join3(SESSIONS_DIR, `${sessionId}.json`));
|
|
387
|
+
return marker?.orgId;
|
|
388
|
+
}
|
|
389
|
+
function fromWorkspaceFile(startDir) {
|
|
390
|
+
let dir = startDir;
|
|
391
|
+
while (true) {
|
|
392
|
+
const config = readJsonSafe(join3(dir, WORKSPACE_FILE));
|
|
393
|
+
if (config?.orgId) {
|
|
394
|
+
return config.orgId;
|
|
395
|
+
}
|
|
396
|
+
const parent = dirname3(dir);
|
|
397
|
+
if (parent === dir) {
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
dir = parent;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
function fromDefaultOrg(_cwd) {
|
|
404
|
+
const credential = resolveAuthCredential();
|
|
405
|
+
return credential ? jwtOrganizationId(credential.token) : void 0;
|
|
406
|
+
}
|
|
407
|
+
function resolveOrg(args) {
|
|
408
|
+
const fromSession = fromSessionMarker(args.sessionId);
|
|
409
|
+
if (fromSession) {
|
|
410
|
+
return { orgId: fromSession, source: "session" };
|
|
411
|
+
}
|
|
412
|
+
const fromWorkspace = fromWorkspaceFile(args.cwd);
|
|
413
|
+
if (fromWorkspace) {
|
|
414
|
+
return { orgId: fromWorkspace, source: "workspace" };
|
|
415
|
+
}
|
|
416
|
+
const fromDefault = fromDefaultOrg(args.cwd);
|
|
417
|
+
if (fromDefault) {
|
|
418
|
+
return { orgId: fromDefault, source: "defaultOrg" };
|
|
419
|
+
}
|
|
420
|
+
return { orgId: void 0, source: "unbound" };
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export {
|
|
424
|
+
rotationIdentity,
|
|
425
|
+
drainResumeOffset,
|
|
426
|
+
writeDrainCheckpoint,
|
|
427
|
+
removeDrainCheckpoint,
|
|
428
|
+
JOURNAL_DIR,
|
|
429
|
+
envSlug,
|
|
430
|
+
appendMove,
|
|
431
|
+
readMovesFromPath,
|
|
432
|
+
listBuckets,
|
|
433
|
+
listFlushing,
|
|
434
|
+
sweepOrphanedDrainProgress,
|
|
435
|
+
bucketStats,
|
|
436
|
+
pendingJournalStats,
|
|
437
|
+
SESSIONS_DIR,
|
|
438
|
+
resolveOrg
|
|
439
|
+
};
|