@primitive.ai/prim 0.1.0-alpha.11 → 0.1.0-alpha.111
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 +450 -38
- package/SKILL.md +366 -0
- package/dist/chunk-2IRLFOEL.js +766 -0
- package/dist/chunk-2Q3OGSTV.js +164 -0
- package/dist/chunk-43ZKL27W.js +258 -0
- package/dist/chunk-4GLVJLEQ.js +55 -0
- package/dist/chunk-5BLGBSDM.js +233 -0
- package/dist/chunk-5LRKJTLA.js +155 -0
- package/dist/chunk-7VIMU2HR.js +209 -0
- package/dist/chunk-AOJ254C2.js +511 -0
- package/dist/chunk-CTRAJCPN.js +964 -0
- package/dist/chunk-EZXEWFDG.js +210 -0
- package/dist/chunk-GA2RXPJL.js +168 -0
- package/dist/chunk-HTRFPXMD.js +135 -0
- package/dist/chunk-IQZA65NV.js +1871 -0
- package/dist/chunk-JJI7FU24.js +439 -0
- package/dist/chunk-KGBMU76T.js +175 -0
- package/dist/chunk-KQ4WBK5R.js +262 -0
- package/dist/chunk-NH3VC7YO.js +264 -0
- package/dist/chunk-OYHYCK4K.js +13 -0
- package/dist/chunk-PO5QAYD7.js +1499 -0
- package/dist/chunk-UDBBJWQH.js +20383 -0
- package/dist/chunk-W4ZEPZLK.js +36 -0
- package/dist/chunk-XG7S3LIL.js +1372 -0
- package/dist/daemon/server.js +24051 -0
- package/dist/hook-runtime/daemon/server.js +24051 -0
- package/dist/hook-runtime/hooks/post-commit.js +21524 -0
- package/dist/hook-runtime/hooks/post-rewrite.js +21423 -0
- package/dist/hook-runtime/hooks/post-tool-use.js +28917 -0
- package/dist/hook-runtime/hooks/pre-commit.js +1657 -0
- package/dist/hook-runtime/hooks/pre-tool-use.js +28425 -0
- package/dist/hook-runtime/hooks/prim-hook.js +28977 -0
- package/dist/hook-runtime/hooks/session-end.js +1176 -0
- package/dist/hook-runtime/hooks/session-start.js +31553 -0
- package/dist/hook-runtime/index.js +59630 -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 +292 -0
- package/dist/hooks/pre-commit.js +15 -108
- package/dist/hooks/pre-tool-use.js +470 -0
- package/dist/hooks/prim-hook.js +242 -0
- package/dist/hooks/session-end.js +64 -0
- package/dist/hooks/session-start.js +309 -0
- package/dist/index.js +10651 -427
- package/dist/statusline-main.js +886 -0
- package/package.json +41 -16
- 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,258 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isCanonicalWorkspaceId
|
|
3
|
+
} from "./chunk-EZXEWFDG.js";
|
|
4
|
+
import {
|
|
5
|
+
scrubEnvironmentPaths
|
|
6
|
+
} from "./chunk-KQ4WBK5R.js";
|
|
7
|
+
import {
|
|
8
|
+
isValidRepoSyncId
|
|
9
|
+
} from "./chunk-XG7S3LIL.js";
|
|
10
|
+
|
|
11
|
+
// src/hooks/prim-hook-core.ts
|
|
12
|
+
import { createHash, randomUUID } from "crypto";
|
|
13
|
+
import { platform } from "os";
|
|
14
|
+
|
|
15
|
+
// src/protocol/move.ts
|
|
16
|
+
var LEGACY_ENVELOPE_VERSION = 1;
|
|
17
|
+
var AGENT_ENVELOPE_VERSION = 3;
|
|
18
|
+
var ENVELOPE_VERSION = LEGACY_ENVELOPE_VERSION;
|
|
19
|
+
|
|
20
|
+
// src/hooks/prim-hook-core.ts
|
|
21
|
+
var CORRELATED_TOOL_EVENTS = /* @__PURE__ */ new Set([
|
|
22
|
+
"PostToolUse",
|
|
23
|
+
"PostToolUseFailure",
|
|
24
|
+
"post_tool_call",
|
|
25
|
+
"post_approval_response"
|
|
26
|
+
]);
|
|
27
|
+
function postToolInvocationId(parsed, agent) {
|
|
28
|
+
if (typeof parsed.hook_event_name !== "string" || !CORRELATED_TOOL_EVENTS.has(parsed.hook_event_name)) {
|
|
29
|
+
return void 0;
|
|
30
|
+
}
|
|
31
|
+
const raw = agent === "hermes" ? parsed.extra?.tool_call_id : parsed.tool_use_id;
|
|
32
|
+
return typeof raw === "string" && raw.length > 0 ? raw : void 0;
|
|
33
|
+
}
|
|
34
|
+
function lowerString(value) {
|
|
35
|
+
return typeof value === "string" ? value.toLowerCase() : void 0;
|
|
36
|
+
}
|
|
37
|
+
function toolOutcomeFor(parsed, agent) {
|
|
38
|
+
const event = parsed.hook_event_name;
|
|
39
|
+
if (agent === "claude_code") {
|
|
40
|
+
if (event === "PostToolUseFailure") {
|
|
41
|
+
return parsed.is_interrupt === true ? "interrupted" : "failed";
|
|
42
|
+
}
|
|
43
|
+
return event === "PostToolUse" ? "succeeded" : void 0;
|
|
44
|
+
}
|
|
45
|
+
if (agent === "codex") {
|
|
46
|
+
return event === "PostToolUse" ? "returned" : void 0;
|
|
47
|
+
}
|
|
48
|
+
if (agent === "cursor") {
|
|
49
|
+
if (event === "PostToolUse") return "succeeded";
|
|
50
|
+
if (event === "PostToolUseFailure") {
|
|
51
|
+
if (parsed.is_interrupt === true) return "interrupted";
|
|
52
|
+
const failureType = lowerString(parsed.failure_type);
|
|
53
|
+
if (failureType === "permission_denied") return "prevented";
|
|
54
|
+
if (failureType === "error" || failureType === "timeout") return "failed";
|
|
55
|
+
return "unknown";
|
|
56
|
+
}
|
|
57
|
+
return void 0;
|
|
58
|
+
}
|
|
59
|
+
const extra = parsed.extra;
|
|
60
|
+
if (event === "post_approval_response") {
|
|
61
|
+
const choice = lowerString(extra?.choice);
|
|
62
|
+
return (choice === "deny" || choice === "smart_deny" || choice === "timeout") && postToolInvocationId(parsed, agent) ? "prevented" : void 0;
|
|
63
|
+
}
|
|
64
|
+
if (event !== "PostToolUse" && event !== "post_tool_call") {
|
|
65
|
+
return void 0;
|
|
66
|
+
}
|
|
67
|
+
const status = lowerString(extra?.status);
|
|
68
|
+
if (status === "ok" || status === "success") {
|
|
69
|
+
return "succeeded";
|
|
70
|
+
}
|
|
71
|
+
if (status === "error" || status === "timeout") {
|
|
72
|
+
return "failed";
|
|
73
|
+
}
|
|
74
|
+
if (status === "cancelled") {
|
|
75
|
+
return "interrupted";
|
|
76
|
+
}
|
|
77
|
+
if (status === "blocked") {
|
|
78
|
+
return "prevented";
|
|
79
|
+
}
|
|
80
|
+
return "unknown";
|
|
81
|
+
}
|
|
82
|
+
function toMove(parsed, cliVersion, agent = "claude_code", workspaceId, repository, invocationId) {
|
|
83
|
+
const sessionId = parsed.session_id ?? "";
|
|
84
|
+
const eventType = parsed.hook_event_name ?? "unknown";
|
|
85
|
+
const toolOutcome = toolOutcomeFor(parsed, agent);
|
|
86
|
+
let moveId;
|
|
87
|
+
if (CORRELATED_TOOL_EVENTS.has(eventType) && sessionId && invocationId) {
|
|
88
|
+
moveId = `posttool:v1:${createHash("sha256").update(JSON.stringify([agent, sessionId, eventType, invocationId])).digest("hex")}`;
|
|
89
|
+
} else if (CORRELATED_TOOL_EVENTS.has(eventType) && sessionId && agent !== "cursor") {
|
|
90
|
+
moveId = `posttool:fallback:v1:${createHash("sha256").update(
|
|
91
|
+
JSON.stringify([
|
|
92
|
+
agent,
|
|
93
|
+
sessionId,
|
|
94
|
+
eventType,
|
|
95
|
+
parsed.turn_id ?? null,
|
|
96
|
+
parsed.tool_name ?? null,
|
|
97
|
+
parsed.tool_input ?? null
|
|
98
|
+
])
|
|
99
|
+
).digest("hex")}`;
|
|
100
|
+
} else {
|
|
101
|
+
moveId = randomUUID();
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
moveId,
|
|
105
|
+
capturedAt: Date.now(),
|
|
106
|
+
sessionId,
|
|
107
|
+
eventType,
|
|
108
|
+
payload: parsed,
|
|
109
|
+
env: scrubEnvironmentPaths({
|
|
110
|
+
cwd: parsed.cwd ?? process.cwd(),
|
|
111
|
+
cliVersion,
|
|
112
|
+
osPlatform: platform(),
|
|
113
|
+
...repository ? {
|
|
114
|
+
repoRoot: repository.repoRoot,
|
|
115
|
+
repoKey: repository.repoKey,
|
|
116
|
+
gitRoot: repository.repoRoot,
|
|
117
|
+
repoFullName: repository.repoFullName,
|
|
118
|
+
repoSyncId: repository.repoSyncId
|
|
119
|
+
} : {},
|
|
120
|
+
...workspaceId ? { workspaceId } : {}
|
|
121
|
+
}),
|
|
122
|
+
envelopeVersion: AGENT_ENVELOPE_VERSION,
|
|
123
|
+
producer: agent,
|
|
124
|
+
...invocationId ? { invocationId } : {},
|
|
125
|
+
...toolOutcome ? { toolOutcome } : {}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
var MAX_REWRITE_PAIRS_PER_MOVE = 40;
|
|
129
|
+
var MAX_REWRITE_PAIRS_PER_HOOK = 2e3;
|
|
130
|
+
var FULL_SHA_RE = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u;
|
|
131
|
+
var MAX_CLASSIFIER_ID_CHARS = 256;
|
|
132
|
+
var INVALID_CLASSIFIER_ID_RE = /[\u0000-\u001f\u007f\uD800-\uDFFF]/u;
|
|
133
|
+
function validSessionHint(value) {
|
|
134
|
+
return typeof value === "string" && value.trim().length > 0 && value === value.trim() && value.length <= MAX_CLASSIFIER_ID_CHARS && !INVALID_CLASSIFIER_ID_RE.test(value);
|
|
135
|
+
}
|
|
136
|
+
function commitAttributionFromEnvironment(env, workspaceId) {
|
|
137
|
+
if (env.HERMES_INTERACTIVE) return void 0;
|
|
138
|
+
if (!workspaceId || !isCanonicalWorkspaceId(workspaceId)) return void 0;
|
|
139
|
+
const candidates = [
|
|
140
|
+
validSessionHint(env.CLAUDE_CODE_SESSION_ID) ? { sessionId: env.CLAUDE_CODE_SESSION_ID, producer: "claude_code" } : void 0,
|
|
141
|
+
validSessionHint(env.CODEX_THREAD_ID) ? { sessionId: env.CODEX_THREAD_ID, producer: "codex" } : void 0
|
|
142
|
+
].filter((candidate) => candidate !== void 0);
|
|
143
|
+
return candidates.length === 1 ? candidates[0] : void 0;
|
|
144
|
+
}
|
|
145
|
+
function toCommitMove(commit, cliVersion, cwd, context = {}) {
|
|
146
|
+
const { repository, repoFullName, repoSyncId, workspaceId, attribution, capturedAt } = context;
|
|
147
|
+
const validatedRepoSyncId = isValidRepoSyncId(repoSyncId) ? repoSyncId : void 0;
|
|
148
|
+
const validatedWorkspaceId = workspaceId && isCanonicalWorkspaceId(workspaceId) ? workspaceId : void 0;
|
|
149
|
+
const validatedAttribution = validatedWorkspaceId ? attribution : void 0;
|
|
150
|
+
const validatedCapturedAt = typeof capturedAt === "number" && Number.isSafeInteger(capturedAt) && capturedAt > 0 ? capturedAt : Date.now();
|
|
151
|
+
const fullSha = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u.test(commit.sha);
|
|
152
|
+
const moveId = validatedRepoSyncId && validatedWorkspaceId && fullSha ? `commit:v2:${createHash("sha256").update(
|
|
153
|
+
JSON.stringify(["commit:v2", validatedRepoSyncId, validatedWorkspaceId, commit.sha])
|
|
154
|
+
).digest("hex")}` : `commit:v2:unscoped:${randomUUID()}`;
|
|
155
|
+
const base = {
|
|
156
|
+
moveId,
|
|
157
|
+
capturedAt: validatedCapturedAt,
|
|
158
|
+
sessionId: validatedAttribution?.sessionId ?? "",
|
|
159
|
+
eventType: "git.commit",
|
|
160
|
+
payload: {
|
|
161
|
+
kind: "git.commit",
|
|
162
|
+
sha: commit.sha,
|
|
163
|
+
parentSha: commit.parentSha,
|
|
164
|
+
branch: commit.branch,
|
|
165
|
+
changedFiles: commit.files,
|
|
166
|
+
changedFilesComplete: commit.filesComplete
|
|
167
|
+
},
|
|
168
|
+
env: scrubEnvironmentPaths({
|
|
169
|
+
cwd,
|
|
170
|
+
cliVersion,
|
|
171
|
+
osPlatform: platform(),
|
|
172
|
+
...repository ? {
|
|
173
|
+
repoRoot: repository.repoRoot,
|
|
174
|
+
repoKey: repository.repoKey,
|
|
175
|
+
gitRoot: repository.repoRoot
|
|
176
|
+
} : {},
|
|
177
|
+
...repoFullName ? { repoFullName } : {},
|
|
178
|
+
...validatedRepoSyncId ? { repoSyncId: validatedRepoSyncId } : {},
|
|
179
|
+
...validatedWorkspaceId ? { workspaceId: validatedWorkspaceId } : {}
|
|
180
|
+
})
|
|
181
|
+
};
|
|
182
|
+
return validatedAttribution ? {
|
|
183
|
+
...base,
|
|
184
|
+
envelopeVersion: AGENT_ENVELOPE_VERSION,
|
|
185
|
+
producer: validatedAttribution.producer
|
|
186
|
+
} : { ...base, envelopeVersion: ENVELOPE_VERSION };
|
|
187
|
+
}
|
|
188
|
+
function canonicalRewritePairs(pairs) {
|
|
189
|
+
const unique = /* @__PURE__ */ new Map();
|
|
190
|
+
for (const pair of pairs) {
|
|
191
|
+
const oldSha = pair.oldSha.toLowerCase();
|
|
192
|
+
const newSha = pair.newSha.toLowerCase();
|
|
193
|
+
if (!FULL_SHA_RE.test(oldSha) || !FULL_SHA_RE.test(newSha)) return null;
|
|
194
|
+
if (oldSha === newSha) continue;
|
|
195
|
+
unique.set(`${oldSha}\0${newSha}`, { oldSha, newSha });
|
|
196
|
+
}
|
|
197
|
+
return [...unique.values()].sort((left, right) => {
|
|
198
|
+
const leftKey = `${left.oldSha}\0${left.newSha}`;
|
|
199
|
+
const rightKey = `${right.oldSha}\0${right.newSha}`;
|
|
200
|
+
return leftKey < rightKey ? -1 : leftKey > rightKey ? 1 : 0;
|
|
201
|
+
}).slice(0, MAX_REWRITE_PAIRS_PER_HOOK);
|
|
202
|
+
}
|
|
203
|
+
function toRewriteMove(rewrite, cliVersion, cwd, context) {
|
|
204
|
+
const { repository, repoFullName, repoSyncId, workspaceId, capturedAt } = context;
|
|
205
|
+
if (rewrite.source !== "amend" && rewrite.source !== "rebase" || !isValidRepoSyncId(repoSyncId) || !workspaceId || !isCanonicalWorkspaceId(workspaceId)) {
|
|
206
|
+
return [];
|
|
207
|
+
}
|
|
208
|
+
const pairs = canonicalRewritePairs(rewrite.pairs);
|
|
209
|
+
if (!pairs || pairs.length === 0) return [];
|
|
210
|
+
const branch = rewrite.branch && rewrite.branch === rewrite.branch.trim() && ![...rewrite.branch].some((char) => char < " " || char === "\x7F") ? rewrite.branch : void 0;
|
|
211
|
+
const observedAt = typeof capturedAt === "number" && Number.isSafeInteger(capturedAt) && capturedAt > 0 ? capturedAt : Date.now();
|
|
212
|
+
const moves = [];
|
|
213
|
+
for (let offset = 0; offset < pairs.length; offset += MAX_REWRITE_PAIRS_PER_MOVE) {
|
|
214
|
+
const sortedPairs = pairs.slice(offset, offset + MAX_REWRITE_PAIRS_PER_MOVE);
|
|
215
|
+
const moveId = `rewrite:v1:${createHash("sha256").update(JSON.stringify(["rewrite:v1", repoSyncId, workspaceId, rewrite.source, sortedPairs])).digest("hex")}`;
|
|
216
|
+
moves.push({
|
|
217
|
+
moveId,
|
|
218
|
+
capturedAt: observedAt,
|
|
219
|
+
sessionId: "",
|
|
220
|
+
eventType: "git.rewrite",
|
|
221
|
+
payload: {
|
|
222
|
+
kind: "git.rewrite",
|
|
223
|
+
source: rewrite.source,
|
|
224
|
+
pairs: sortedPairs,
|
|
225
|
+
...branch ? { branch } : {}
|
|
226
|
+
},
|
|
227
|
+
env: scrubEnvironmentPaths({
|
|
228
|
+
cwd,
|
|
229
|
+
cliVersion,
|
|
230
|
+
osPlatform: platform(),
|
|
231
|
+
...repository ? {
|
|
232
|
+
repoRoot: repository.repoRoot,
|
|
233
|
+
repoKey: repository.repoKey,
|
|
234
|
+
gitRoot: repository.repoRoot
|
|
235
|
+
} : {},
|
|
236
|
+
...repoFullName ? { repoFullName } : {},
|
|
237
|
+
repoSyncId,
|
|
238
|
+
workspaceId
|
|
239
|
+
}),
|
|
240
|
+
envelopeVersion: ENVELOPE_VERSION
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return moves;
|
|
244
|
+
}
|
|
245
|
+
function shouldFlushAfter(eventType, agent = "claude_code") {
|
|
246
|
+
return eventType === "SessionEnd" || agent === "cursor" && eventType === "Stop";
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export {
|
|
250
|
+
postToolInvocationId,
|
|
251
|
+
toolOutcomeFor,
|
|
252
|
+
toMove,
|
|
253
|
+
commitAttributionFromEnvironment,
|
|
254
|
+
toCommitMove,
|
|
255
|
+
canonicalRewritePairs,
|
|
256
|
+
toRewriteMove,
|
|
257
|
+
shouldFlushAfter
|
|
258
|
+
};
|
|
@@ -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
|
+
};
|