@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,164 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getClient,
|
|
3
|
+
getSiteUrl,
|
|
4
|
+
terminalSafeLine
|
|
5
|
+
} from "./chunk-2IRLFOEL.js";
|
|
6
|
+
import {
|
|
7
|
+
daemonRequest
|
|
8
|
+
} from "./chunk-5LRKJTLA.js";
|
|
9
|
+
import {
|
|
10
|
+
canonicalGitRoot,
|
|
11
|
+
canonicalRepositoryPath,
|
|
12
|
+
currentBranch,
|
|
13
|
+
repoSyncId
|
|
14
|
+
} from "./chunk-XG7S3LIL.js";
|
|
15
|
+
|
|
16
|
+
// src/daemon/proxy.ts
|
|
17
|
+
var DAEMON_HTTP_TIMEOUT_MS = 1e4;
|
|
18
|
+
var DAEMON_PROBE_TIMEOUT_MS = 250;
|
|
19
|
+
async function daemonOrDirect(method, params, direct) {
|
|
20
|
+
const fromDaemon = await daemonRequest(
|
|
21
|
+
method,
|
|
22
|
+
{ ...params, callerEnv: getSiteUrl() },
|
|
23
|
+
{ timeoutMs: DAEMON_PROBE_TIMEOUT_MS }
|
|
24
|
+
);
|
|
25
|
+
if (fromDaemon !== null) {
|
|
26
|
+
return fromDaemon;
|
|
27
|
+
}
|
|
28
|
+
return await direct();
|
|
29
|
+
}
|
|
30
|
+
async function daemonOrDirectGet(method, path, client, timeoutMs = DAEMON_HTTP_TIMEOUT_MS) {
|
|
31
|
+
return await daemonOrDirect(
|
|
32
|
+
method,
|
|
33
|
+
{ path },
|
|
34
|
+
async () => await client.get(path, {
|
|
35
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/hooks/decisions-check.ts
|
|
41
|
+
var DECISIONS_CHECK_TIMEOUT_MS = 1e4;
|
|
42
|
+
var MAX_FILES_PER_REQUEST = 25;
|
|
43
|
+
var defaultDeps = { getClient };
|
|
44
|
+
function chunk(items, size) {
|
|
45
|
+
const out = [];
|
|
46
|
+
for (let i = 0; i < items.length; i += size) {
|
|
47
|
+
out.push(items.slice(i, i + size));
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
async function fetchAffecting(client, batch, repository) {
|
|
52
|
+
const params = new URLSearchParams();
|
|
53
|
+
params.set("repoSyncId", repository.repoSyncId);
|
|
54
|
+
if (repository.branch) params.set("branch", repository.branch);
|
|
55
|
+
for (const file of batch) {
|
|
56
|
+
params.append("files", file);
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const response = await daemonOrDirectGet(
|
|
60
|
+
"decisions_affecting",
|
|
61
|
+
`/api/cli/decisions/affecting?${params.toString()}`,
|
|
62
|
+
client,
|
|
63
|
+
DECISIONS_CHECK_TIMEOUT_MS
|
|
64
|
+
);
|
|
65
|
+
if (response.repoSyncId !== repository.repoSyncId) {
|
|
66
|
+
return {
|
|
67
|
+
decisions: [],
|
|
68
|
+
truncated: response.truncated === true,
|
|
69
|
+
unavailable: response.unavailable ?? "service did not verify repository-scoped discovery"
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return response;
|
|
73
|
+
} catch (err) {
|
|
74
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
75
|
+
return { decisions: [], truncated: false, unavailable: `decision check failed: ${detail}` };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async function checkAffectedDecisions(filePaths, deps = defaultDeps) {
|
|
79
|
+
if (filePaths.length === 0) {
|
|
80
|
+
return { decisions: [], truncated: false };
|
|
81
|
+
}
|
|
82
|
+
const cwd = process.cwd();
|
|
83
|
+
const root = canonicalGitRoot(cwd);
|
|
84
|
+
const binding = repoSyncId(cwd);
|
|
85
|
+
if (!root || !binding) {
|
|
86
|
+
return {
|
|
87
|
+
decisions: [],
|
|
88
|
+
truncated: false,
|
|
89
|
+
unavailable: "this checkout has no current Primitive repository binding; run prim enable"
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const canonical = filePaths.map(
|
|
93
|
+
(file) => canonicalRepositoryPath(file, { repoRoot: root }, root)
|
|
94
|
+
);
|
|
95
|
+
if (canonical.some((path) => !path.ok)) {
|
|
96
|
+
return {
|
|
97
|
+
decisions: [],
|
|
98
|
+
truncated: false,
|
|
99
|
+
unavailable: "files must resolve inside this repository; use repository-relative paths"
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const paths = [...new Set(canonical.flatMap((path) => path.ok ? [path.file] : []))];
|
|
103
|
+
const repository = { repoSyncId: binding, branch: currentBranch(cwd) };
|
|
104
|
+
const client = deps.getClient();
|
|
105
|
+
const responses = await Promise.all(
|
|
106
|
+
chunk(paths, MAX_FILES_PER_REQUEST).map((batch) => fetchAffecting(client, batch, repository))
|
|
107
|
+
);
|
|
108
|
+
const byId = /* @__PURE__ */ new Map();
|
|
109
|
+
let truncated = false;
|
|
110
|
+
let unavailable;
|
|
111
|
+
for (const res of responses) {
|
|
112
|
+
if (res.unavailable !== void 0 && unavailable === void 0) {
|
|
113
|
+
unavailable = res.unavailable;
|
|
114
|
+
}
|
|
115
|
+
truncated ||= res.truncated === true;
|
|
116
|
+
for (const d of res.decisions) {
|
|
117
|
+
const existing = byId.get(d.id);
|
|
118
|
+
if (existing) {
|
|
119
|
+
existing.matchedFiles = [.../* @__PURE__ */ new Set([...existing.matchedFiles, ...d.matchedFiles])];
|
|
120
|
+
} else {
|
|
121
|
+
byId.set(d.id, { ...d, matchedFiles: [...d.matchedFiles] });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const result = { decisions: [...byId.values()], truncated };
|
|
126
|
+
if (unavailable !== void 0) {
|
|
127
|
+
result.unavailable = unavailable;
|
|
128
|
+
}
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
var FILES_PREVIEW_LIMIT = 3;
|
|
132
|
+
function formatDecisionsWarning(result) {
|
|
133
|
+
const lines = [];
|
|
134
|
+
if (result.unavailable !== void 0) {
|
|
135
|
+
lines.push(`[prim] decision check not verified \u2014 ${terminalSafeLine(result.unavailable)}`);
|
|
136
|
+
}
|
|
137
|
+
if (result.decisions.length > 0) {
|
|
138
|
+
lines.push(
|
|
139
|
+
`[prim] ${String(result.decisions.length)} active decision(s) reference staged files:`
|
|
140
|
+
);
|
|
141
|
+
for (const d of result.decisions) {
|
|
142
|
+
const statusMark = d.status === "under_review" ? " (under review)" : "";
|
|
143
|
+
const preview = d.matchedFiles.slice(0, FILES_PREVIEW_LIMIT).map(terminalSafeLine).join(", ");
|
|
144
|
+
const overflow = d.matchedFiles.length > FILES_PREVIEW_LIMIT ? ` (+${String(d.matchedFiles.length - FILES_PREVIEW_LIMIT)} more)` : "";
|
|
145
|
+
lines.push(` \xB7 ${terminalSafeLine(d.intent)}${statusMark}`);
|
|
146
|
+
lines.push(` files: ${preview}${overflow}`);
|
|
147
|
+
if (d.rationale) {
|
|
148
|
+
lines.push(` rationale: ${terminalSafeLine(d.rationale)}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (result.truncated) {
|
|
153
|
+
lines.push(
|
|
154
|
+
"[prim] result truncated \u2014 a discovery scan or result limit was reached; not all decisions shown"
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
return lines.join("\n");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export {
|
|
161
|
+
daemonOrDirectGet,
|
|
162
|
+
checkAffectedDecisions,
|
|
163
|
+
formatDecisionsWarning
|
|
164
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/hooks/decision-feedback-core.ts
|
|
2
|
+
function buildHookOutput(options) {
|
|
3
|
+
const output = {};
|
|
4
|
+
if (options.systemMessage) output.systemMessage = options.systemMessage;
|
|
5
|
+
const fields = {};
|
|
6
|
+
if (options.additionalContext) fields.additionalContext = options.additionalContext;
|
|
7
|
+
if (options.reloadSkills) fields.reloadSkills = true;
|
|
8
|
+
if (Object.keys(fields).length > 0) {
|
|
9
|
+
output.hookSpecificOutput = { hookEventName: "SessionStart", ...fields };
|
|
10
|
+
}
|
|
11
|
+
return output;
|
|
12
|
+
}
|
|
13
|
+
function writeHookOutput(output, stream = process.stdout) {
|
|
14
|
+
return new Promise((resolve) => {
|
|
15
|
+
let settled = false;
|
|
16
|
+
const finish = (ok, retainErrorListener = false) => {
|
|
17
|
+
if (settled) return;
|
|
18
|
+
settled = true;
|
|
19
|
+
if (!retainErrorListener) stream.off("error", onError);
|
|
20
|
+
resolve(ok);
|
|
21
|
+
};
|
|
22
|
+
const onError = () => {
|
|
23
|
+
if (settled) {
|
|
24
|
+
stream.off("error", onError);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
finish(false);
|
|
28
|
+
};
|
|
29
|
+
stream.once("error", onError);
|
|
30
|
+
try {
|
|
31
|
+
stream.write(
|
|
32
|
+
`${JSON.stringify(output)}
|
|
33
|
+
`,
|
|
34
|
+
(error) => error ? finish(false, true) : finish(true)
|
|
35
|
+
);
|
|
36
|
+
} catch {
|
|
37
|
+
finish(false);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
async function handoffHookOutput(output, acknowledge, stream = process.stdout) {
|
|
42
|
+
const handedOff = await writeHookOutput(output, stream);
|
|
43
|
+
if (handedOff && acknowledge) {
|
|
44
|
+
try {
|
|
45
|
+
await acknowledge();
|
|
46
|
+
} catch {
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return handedOff;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export {
|
|
53
|
+
buildHookOutput,
|
|
54
|
+
handoffHookOutput
|
|
55
|
+
};
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import {
|
|
2
|
+
boundedHealthError
|
|
3
|
+
} from "./chunk-GSBHBU7N.js";
|
|
4
|
+
import {
|
|
5
|
+
stripControlChars
|
|
6
|
+
} from "./chunk-2IRLFOEL.js";
|
|
7
|
+
|
|
8
|
+
// src/lib/delivery-backlog.ts
|
|
9
|
+
var MS_PER_SECOND = 1e3;
|
|
10
|
+
var MS_PER_MINUTE = 60 * MS_PER_SECOND;
|
|
11
|
+
var MS_PER_HOUR = 60 * MS_PER_MINUTE;
|
|
12
|
+
var MS_PER_DAY = 24 * MS_PER_HOUR;
|
|
13
|
+
function isNonNegativeInteger(value) {
|
|
14
|
+
return typeof value === "number" && Number.isInteger(value) && value >= 0;
|
|
15
|
+
}
|
|
16
|
+
function isPositiveInteger(value) {
|
|
17
|
+
return isNonNegativeInteger(value) && value > 0;
|
|
18
|
+
}
|
|
19
|
+
function deliveryBacklogState(ingestion) {
|
|
20
|
+
if (ingestion?.healthy !== false) return void 0;
|
|
21
|
+
const failures = ingestion.consecutiveFailures;
|
|
22
|
+
const failedWithoutProgress = failures !== 0 && !(isPositiveInteger(failures) && isPositiveInteger(ingestion.lastFailedDrainAcknowledgedCount));
|
|
23
|
+
if (failedWithoutProgress) return "failing";
|
|
24
|
+
const retained = ingestion.lastRetainedBucketCount;
|
|
25
|
+
if (!isNonNegativeInteger(retained)) return "failing";
|
|
26
|
+
return retained > 0 ? "retained" : "draining";
|
|
27
|
+
}
|
|
28
|
+
function formatBacklogAge(ageMs) {
|
|
29
|
+
if (ageMs >= MS_PER_DAY) return `${String(Math.floor(ageMs / MS_PER_DAY))}d`;
|
|
30
|
+
if (ageMs >= MS_PER_HOUR) return `${String(Math.floor(ageMs / MS_PER_HOUR))}h`;
|
|
31
|
+
if (ageMs >= MS_PER_MINUTE) return `${String(Math.floor(ageMs / MS_PER_MINUTE))}m`;
|
|
32
|
+
return `${String(Math.max(0, Math.floor(ageMs / MS_PER_SECOND)))}s`;
|
|
33
|
+
}
|
|
34
|
+
function formatPendingBacklog(backlog, now = Date.now()) {
|
|
35
|
+
const count = backlog.pendingCount;
|
|
36
|
+
const moves = typeof count === "number" && count > 0 ? `${backlog.pendingSampled ? "at least " : ""}${String(count)} pending move${count === 1 ? "" : "s"}` : "an unknown number of pending moves";
|
|
37
|
+
const age = backlog.oldestPendingAt === void 0 ? "" : ` (oldest ${backlog.pendingSampled ? "\u2265 " : ""}${formatBacklogAge(now - backlog.oldestPendingAt)})`;
|
|
38
|
+
return `${moves}${age}`;
|
|
39
|
+
}
|
|
40
|
+
function deliveryBacklogSummary(ingestion, now = Date.now(), backlog = ingestion) {
|
|
41
|
+
const state = deliveryBacklogState(ingestion);
|
|
42
|
+
if (!(state && ingestion && backlog)) return void 0;
|
|
43
|
+
const pending = formatPendingBacklog(backlog, now);
|
|
44
|
+
if (state === "draining") return `draining ${pending} in the background`;
|
|
45
|
+
if (state === "retained") {
|
|
46
|
+
const buckets = ingestion.lastRetainedBucketCount ?? 0;
|
|
47
|
+
const reasons = boundedHealthError(ingestion.lastRetainedReasons);
|
|
48
|
+
return `delivery held back: ${String(buckets)} journal bucket${buckets === 1 ? "" : "s"} retained${reasons ? ` (${reasons})` : ""} \xB7 holding ${pending} \u2014 run \`prim doctor\``;
|
|
49
|
+
}
|
|
50
|
+
const failures = ingestion.consecutiveFailures;
|
|
51
|
+
const count = isPositiveInteger(failures) ? ` (${String(failures)} consecutive failure${failures === 1 ? "" : "s"})` : "";
|
|
52
|
+
const error = boundedHealthError(ingestion.lastError);
|
|
53
|
+
return `delivery failing${count}${error ? `: ${error}` : ""} \xB7 retrying ${pending} in the background`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/lib/presence.ts
|
|
57
|
+
var DECISION_ORIGIN = "https://app.getprimitive.ai";
|
|
58
|
+
function validDecisionUrl(value) {
|
|
59
|
+
if (typeof value !== "string" || stripControlChars(value) !== value) {
|
|
60
|
+
return void 0;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const url = new URL(value);
|
|
64
|
+
if (url.origin !== DECISION_ORIGIN || url.username !== "" || url.password !== "" || url.search !== "" || url.hash !== "" || !/^\/decisions\/[^/]+$/u.test(url.pathname)) {
|
|
65
|
+
return void 0;
|
|
66
|
+
}
|
|
67
|
+
return url.href;
|
|
68
|
+
} catch {
|
|
69
|
+
return void 0;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function decisionLink(label, decisionUrl, plain) {
|
|
73
|
+
const url = validDecisionUrl(decisionUrl);
|
|
74
|
+
if (plain || !url) {
|
|
75
|
+
return label;
|
|
76
|
+
}
|
|
77
|
+
return `\x1B]8;;${url}\x07\x1B[34;4m${label}\x1B[0m\x1B]8;;\x07`;
|
|
78
|
+
}
|
|
79
|
+
function formatLabeled(labels, cap) {
|
|
80
|
+
if (labels === void 0) {
|
|
81
|
+
return "\u2014";
|
|
82
|
+
}
|
|
83
|
+
if (labels.length === 0) {
|
|
84
|
+
return "just you";
|
|
85
|
+
}
|
|
86
|
+
if (labels.length <= cap) {
|
|
87
|
+
return labels.join(", ");
|
|
88
|
+
}
|
|
89
|
+
return `${labels.slice(0, cap).join(", ")} +${String(labels.length - cap)}`;
|
|
90
|
+
}
|
|
91
|
+
function formatTeammates(names, cap) {
|
|
92
|
+
return formatLabeled(names?.map(stripControlChars), cap);
|
|
93
|
+
}
|
|
94
|
+
function formatTeammatesWithArea(teammates, cap, plainLinks = false) {
|
|
95
|
+
return formatLabeled(
|
|
96
|
+
teammates?.map((t) => {
|
|
97
|
+
const name = stripControlChars(t.name);
|
|
98
|
+
const area = stripControlChars(t.area ?? "").trim();
|
|
99
|
+
const label = area ? `${name} - ${area}` : name;
|
|
100
|
+
return decisionLink(label, t.decisionUrl, plainLinks);
|
|
101
|
+
}),
|
|
102
|
+
cap
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// src/lib/statusline-render.ts
|
|
107
|
+
function repositoryBindingDiagnosticLabel(state) {
|
|
108
|
+
if (state === "unbound") return "GitHub repo connection: required (run `prim github connect`)";
|
|
109
|
+
if (state === "invalid") return "GitHub repo connection: invalid (run `prim doctor`)";
|
|
110
|
+
return void 0;
|
|
111
|
+
}
|
|
112
|
+
var STATUSLINE_NAME_CAP = 3;
|
|
113
|
+
function formatStatusline(version, snapshot, resolveIngestionStatus, options = {}) {
|
|
114
|
+
const ingestionSuffix = (status) => status === void 0 ? "" : ` \xB7 Decision ingestion ${status}`;
|
|
115
|
+
const ingestionStatus = snapshot === null || snapshot.healthy === false ? options.includeIngestionWhenUnavailable ? resolveIngestionStatus() : void 0 : resolveIngestionStatus();
|
|
116
|
+
if (snapshot?.healthy === false && snapshot.needsReauth) {
|
|
117
|
+
return `primitive ${version} (daemon: paused \xB7 run \`prim auth login\`${ingestionSuffix(ingestionStatus)})`;
|
|
118
|
+
}
|
|
119
|
+
const repositoryLabel = repositoryBindingDiagnosticLabel(
|
|
120
|
+
snapshot !== null && snapshot.healthy !== false || options.includeIngestionWhenUnavailable === true ? options.resolveRepositoryBindingState?.() : void 0
|
|
121
|
+
);
|
|
122
|
+
const repositorySuffix = repositoryLabel ? ` \xB7 ${repositoryLabel}` : "";
|
|
123
|
+
if (!snapshot) {
|
|
124
|
+
return `primitive ${version} (daemon: down${repositorySuffix}${ingestionSuffix(ingestionStatus)})`;
|
|
125
|
+
}
|
|
126
|
+
if (snapshot.healthy === false) {
|
|
127
|
+
if (snapshot.ingestion?.healthy === false) {
|
|
128
|
+
const pending = snapshot.ingestion.pendingCount;
|
|
129
|
+
const qualifier = snapshot.ingestion.pendingSampled ? "at least " : "";
|
|
130
|
+
const delivery = deliveryBacklogState(snapshot.ingestion) === "draining" && snapshot.heartbeat?.healthy === true ? "draining" : "stalled";
|
|
131
|
+
return `primitive ${version} (daemon: degraded \xB7 delivery: ${delivery}${typeof pending === "number" ? ` \xB7 ${qualifier}${String(pending)} pending` : ""}${repositorySuffix}${ingestionSuffix(ingestionStatus)})`;
|
|
132
|
+
}
|
|
133
|
+
if (snapshot.heartbeat?.healthy === false) {
|
|
134
|
+
return `primitive ${version} (daemon: degraded \xB7 presence: unavailable${repositorySuffix}${ingestionSuffix(ingestionStatus)})`;
|
|
135
|
+
}
|
|
136
|
+
return `primitive ${version} (daemon: starting${repositorySuffix}${ingestionSuffix(ingestionStatus)})`;
|
|
137
|
+
}
|
|
138
|
+
if (snapshot.envMismatch) {
|
|
139
|
+
return `primitive ${version} (daemon: live, Decision ingestion ${ingestionStatus}${repositorySuffix} \xB7 presence: other env)`;
|
|
140
|
+
}
|
|
141
|
+
if (snapshot.principalMismatch) {
|
|
142
|
+
return `primitive ${version} (daemon: live, Decision ingestion ${ingestionStatus}${repositorySuffix} \xB7 presence: other account)`;
|
|
143
|
+
}
|
|
144
|
+
if (snapshot.presenceStale) {
|
|
145
|
+
return `primitive ${version} (daemon: live, Decision ingestion ${ingestionStatus}${repositorySuffix} \xB7 presence: stale)`;
|
|
146
|
+
}
|
|
147
|
+
let team;
|
|
148
|
+
if (snapshot.onlineTeammates !== void 0) {
|
|
149
|
+
team = `team: ${formatTeammatesWithArea(snapshot.onlineTeammates, STATUSLINE_NAME_CAP, options.plainLinks === true)}`;
|
|
150
|
+
} else if (snapshot.onlineNames !== void 0) {
|
|
151
|
+
team = `team: ${formatTeammates(snapshot.onlineNames, STATUSLINE_NAME_CAP)}`;
|
|
152
|
+
} else if (typeof snapshot.onlineCount === "number") {
|
|
153
|
+
team = `team: ${String(snapshot.onlineCount)} online`;
|
|
154
|
+
} else {
|
|
155
|
+
team = "team: \u2014";
|
|
156
|
+
}
|
|
157
|
+
return `primitive ${version} (daemon: live, Decision ingestion ${ingestionStatus}${repositorySuffix} \xB7 ${team})`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export {
|
|
161
|
+
MS_PER_SECOND,
|
|
162
|
+
deliveryBacklogState,
|
|
163
|
+
formatPendingBacklog,
|
|
164
|
+
deliveryBacklogSummary,
|
|
165
|
+
formatTeammates,
|
|
166
|
+
repositoryBindingDiagnosticLabel,
|
|
167
|
+
formatStatusline
|
|
168
|
+
};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decodeJwtPayload,
|
|
3
|
+
primConfigDirectory,
|
|
4
|
+
resolveAuthCredential
|
|
5
|
+
} from "./chunk-XG7S3LIL.js";
|
|
6
|
+
|
|
7
|
+
// src/daemon/client.ts
|
|
8
|
+
import { createConnection } from "net";
|
|
9
|
+
import { join } from "path";
|
|
10
|
+
|
|
11
|
+
// src/daemon/principal.ts
|
|
12
|
+
import { createHash } from "crypto";
|
|
13
|
+
var PRINCIPAL_COMPONENT_MAX_LENGTH = 512;
|
|
14
|
+
function boundedClaim(value) {
|
|
15
|
+
return typeof value === "string" && value.length > 0 && value.length <= PRINCIPAL_COMPONENT_MAX_LENGTH ? value : void 0;
|
|
16
|
+
}
|
|
17
|
+
function sha256(value) {
|
|
18
|
+
return createHash("sha256").update(value).digest("hex");
|
|
19
|
+
}
|
|
20
|
+
function resolveDaemonPrincipal(token = resolveAuthCredential()?.token) {
|
|
21
|
+
if (!token) return void 0;
|
|
22
|
+
const payload = decodeJwtPayload(token);
|
|
23
|
+
const subject = boundedClaim(payload?.sub);
|
|
24
|
+
const organizationId = boundedClaim(payload?.org_id);
|
|
25
|
+
if (!(subject && organizationId)) return void 0;
|
|
26
|
+
const issuer = boundedClaim(payload?.iss) ?? "unknown-issuer";
|
|
27
|
+
return {
|
|
28
|
+
principalId: sha256(`${issuer}\0${subject}`),
|
|
29
|
+
organizationId,
|
|
30
|
+
credentialFingerprint: sha256(token)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function daemonPrincipalsMatch(caller, daemon) {
|
|
34
|
+
return caller !== void 0 && daemon !== void 0 && caller.principalId === daemon.principalId && caller.organizationId === daemon.organizationId && caller.credentialFingerprint === daemon.credentialFingerprint;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/daemon/protocol.ts
|
|
38
|
+
var DAEMON_JSON_REQUEST_MAX_BYTES = 64 * 1024;
|
|
39
|
+
var DAEMON_JSON_RESPONSE_MAX_BYTES = 8 * 1024 * 1024;
|
|
40
|
+
function isRecord(value) {
|
|
41
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
42
|
+
}
|
|
43
|
+
function parseDaemonResponseEnvelope(value) {
|
|
44
|
+
if (!isRecord(value)) return void 0;
|
|
45
|
+
if (!(typeof value.id === "number" && Number.isSafeInteger(value.id) && value.id >= 0)) {
|
|
46
|
+
return void 0;
|
|
47
|
+
}
|
|
48
|
+
if (typeof value.ok !== "boolean") return void 0;
|
|
49
|
+
if (value.error !== void 0 && typeof value.error !== "string") return void 0;
|
|
50
|
+
return {
|
|
51
|
+
id: value.id,
|
|
52
|
+
ok: value.ok,
|
|
53
|
+
...value.result === void 0 ? {} : { result: value.result },
|
|
54
|
+
...value.error === void 0 ? {} : { error: value.error }
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/daemon/client.ts
|
|
59
|
+
var SOCK_PATH = join(primConfigDirectory(), "sock");
|
|
60
|
+
var DEFAULT_TIMEOUT_MS = 250;
|
|
61
|
+
var nextRequestId = 1;
|
|
62
|
+
function daemonRequest(method, params = {}, opts = {}) {
|
|
63
|
+
const id = nextRequestId++;
|
|
64
|
+
const caller = resolveDaemonPrincipal();
|
|
65
|
+
const request = {
|
|
66
|
+
id,
|
|
67
|
+
method,
|
|
68
|
+
params,
|
|
69
|
+
...caller ? { caller } : {}
|
|
70
|
+
};
|
|
71
|
+
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
72
|
+
return new Promise((resolve) => {
|
|
73
|
+
let settled = false;
|
|
74
|
+
let socket;
|
|
75
|
+
let buffer = Buffer.alloc(0);
|
|
76
|
+
const settle = (value) => {
|
|
77
|
+
if (settled) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
settled = true;
|
|
81
|
+
if (socket) {
|
|
82
|
+
try {
|
|
83
|
+
socket.destroy();
|
|
84
|
+
} catch {
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
resolve(value);
|
|
88
|
+
};
|
|
89
|
+
const timer = setTimeout(() => settle(null), timeoutMs);
|
|
90
|
+
timer.unref();
|
|
91
|
+
try {
|
|
92
|
+
socket = createConnection(SOCK_PATH);
|
|
93
|
+
} catch {
|
|
94
|
+
clearTimeout(timer);
|
|
95
|
+
settle(null);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
socket.on("error", () => {
|
|
99
|
+
clearTimeout(timer);
|
|
100
|
+
settle(null);
|
|
101
|
+
});
|
|
102
|
+
socket.on("connect", () => {
|
|
103
|
+
try {
|
|
104
|
+
socket?.write(`${JSON.stringify(request)}
|
|
105
|
+
`);
|
|
106
|
+
} catch {
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
settle(null);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
socket.on("data", (chunk) => {
|
|
112
|
+
buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
|
|
113
|
+
if (buffer.length > DAEMON_JSON_RESPONSE_MAX_BYTES) {
|
|
114
|
+
clearTimeout(timer);
|
|
115
|
+
settle(null);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const newlineIdx = buffer.indexOf(10);
|
|
119
|
+
if (newlineIdx === -1) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const line = buffer.subarray(0, newlineIdx).toString("utf8");
|
|
123
|
+
try {
|
|
124
|
+
const res = parseDaemonResponseEnvelope(JSON.parse(line));
|
|
125
|
+
if (!res || res.id !== id) {
|
|
126
|
+
clearTimeout(timer);
|
|
127
|
+
settle(null);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
clearTimeout(timer);
|
|
131
|
+
settle(res.ok && res.result !== void 0 ? res.result : null);
|
|
132
|
+
} catch {
|
|
133
|
+
clearTimeout(timer);
|
|
134
|
+
settle(null);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
socket.on("end", () => {
|
|
138
|
+
if (!settled) {
|
|
139
|
+
clearTimeout(timer);
|
|
140
|
+
settle(null);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async function daemonIsLive(timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
146
|
+
const result = await daemonRequest("ping", {}, { timeoutMs });
|
|
147
|
+
return result?.pong === true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export {
|
|
151
|
+
resolveDaemonPrincipal,
|
|
152
|
+
daemonPrincipalsMatch,
|
|
153
|
+
daemonRequest,
|
|
154
|
+
daemonIsLive
|
|
155
|
+
};
|