@polygraph/claude-plugin 0.4.47 → 0.4.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
function nonEmptyString(value) {
|
|
4
|
+
return typeof value === 'string' && value.trim() ? value : undefined;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function isManagedChildEnvironment(env) {
|
|
8
|
+
return Boolean(env && Object.hasOwn(env, 'POLYGRAPH_CHILD_AGENT'));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function buildFinalizeAgentSessionArgs({
|
|
12
|
+
agentType,
|
|
13
|
+
agentSessionId,
|
|
14
|
+
cwd,
|
|
15
|
+
transcriptPath,
|
|
16
|
+
source,
|
|
17
|
+
}) {
|
|
18
|
+
const harnessSession = nonEmptyString(agentSessionId);
|
|
19
|
+
const hookSource = nonEmptyString(source);
|
|
20
|
+
if (agentType !== 'claude') throw new Error(`Unsupported agent type: ${agentType}`);
|
|
21
|
+
if (!harnessSession) throw new Error('agentSessionId is required');
|
|
22
|
+
if (!hookSource) throw new Error('source is required');
|
|
23
|
+
|
|
24
|
+
const args = [
|
|
25
|
+
'_finalize-agent-session',
|
|
26
|
+
'--agent-type',
|
|
27
|
+
agentType,
|
|
28
|
+
'--agent-session-id',
|
|
29
|
+
harnessSession,
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
const workingDirectory = nonEmptyString(cwd);
|
|
33
|
+
if (workingDirectory) args.push('--cwd', workingDirectory);
|
|
34
|
+
|
|
35
|
+
const transcript = nonEmptyString(transcriptPath);
|
|
36
|
+
if (transcript) args.push('--transcript-path', transcript);
|
|
37
|
+
|
|
38
|
+
args.push('--source', hookSource);
|
|
39
|
+
return args;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function finalizeAgentSession(claim, spawn = spawnSync, env = process.env) {
|
|
43
|
+
if (isManagedChildEnvironment(env)) return false;
|
|
44
|
+
|
|
45
|
+
const command = nonEmptyString(env?.POLYGRAPH_CLI) ?? 'polygraph';
|
|
46
|
+
const commandEnv = { ...env };
|
|
47
|
+
delete commandEnv.POLYGRAPH_SESSION_ID;
|
|
48
|
+
delete commandEnv.POLYGRAPH_CAPTURE_TOKEN;
|
|
49
|
+
|
|
50
|
+
const result = spawn(command, buildFinalizeAgentSessionArgs(claim), {
|
|
51
|
+
encoding: 'utf8',
|
|
52
|
+
env: commandEnv,
|
|
53
|
+
stdio: ['ignore', 'ignore', 'pipe'],
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (result?.error) throw result.error;
|
|
57
|
+
if (result?.status !== 0) {
|
|
58
|
+
const detail = nonEmptyString(result?.stderr);
|
|
59
|
+
throw new Error(
|
|
60
|
+
`polygraph _finalize-agent-session exited with status ${String(result?.status)}` +
|
|
61
|
+
(detail ? `: ${detail}` : '')
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function buildCommandHookFinalize(payload, agentType, env = process.env) {
|
|
69
|
+
if (!payload || typeof payload !== 'object') return undefined;
|
|
70
|
+
if (isManagedChildEnvironment(env)) return undefined;
|
|
71
|
+
if (agentType !== 'claude' || payload.hook_event_name !== 'SessionEnd') {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const agentSessionId = nonEmptyString(payload.session_id);
|
|
76
|
+
if (!agentSessionId) return undefined;
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
agentType,
|
|
80
|
+
agentSessionId,
|
|
81
|
+
cwd: nonEmptyString(payload.cwd),
|
|
82
|
+
transcriptPath: nonEmptyString(payload.transcript_path),
|
|
83
|
+
source: 'hook',
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -102,7 +102,11 @@ export function buildCommandHookLink(payload, agentType, env = process.env) {
|
|
|
102
102
|
|
|
103
103
|
if (payload.hook_event_name === 'SessionStart') {
|
|
104
104
|
const polygraphSessionId = nonEmptyString(env.POLYGRAPH_SESSION_ID);
|
|
105
|
-
|
|
105
|
+
if (polygraphSessionId) return { ...common, polygraphSessionId };
|
|
106
|
+
|
|
107
|
+
// Ordinary Claude sessions are eligible for speculative capture. Other
|
|
108
|
+
// harnesses still require launch-provided Polygraph session evidence.
|
|
109
|
+
return agentType === 'claude' ? common : undefined;
|
|
106
110
|
}
|
|
107
111
|
|
|
108
112
|
if (payload.hook_event_name === 'PostToolUse') {
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { readFileSync, realpathSync } from 'node:fs';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
buildCommandHookFinalize,
|
|
6
|
+
finalizeAgentSession,
|
|
7
|
+
} from './agent-session-finalize.mjs';
|
|
8
|
+
import { logHookFailure } from './agent-session-link.mjs';
|
|
9
|
+
|
|
10
|
+
function readPayload() {
|
|
11
|
+
try {
|
|
12
|
+
const raw = readFileSync(0, 'utf8');
|
|
13
|
+
return raw ? JSON.parse(raw) : undefined;
|
|
14
|
+
} catch {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function main({
|
|
20
|
+
payload = readPayload(),
|
|
21
|
+
agentType = process.argv[2],
|
|
22
|
+
env = process.env,
|
|
23
|
+
spawn,
|
|
24
|
+
} = {}) {
|
|
25
|
+
try {
|
|
26
|
+
const finalize = buildCommandHookFinalize(payload, agentType, env);
|
|
27
|
+
if (!finalize) return false;
|
|
28
|
+
return finalizeAgentSession(
|
|
29
|
+
{
|
|
30
|
+
...finalize,
|
|
31
|
+
cwd: finalize.cwd ?? process.cwd(),
|
|
32
|
+
},
|
|
33
|
+
spawn,
|
|
34
|
+
env
|
|
35
|
+
);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
logHookFailure(`${agentType || 'unknown'}:finalize-agent-session`, error, {
|
|
38
|
+
hookEventName: payload?.hook_event_name,
|
|
39
|
+
agentSessionId: payload?.session_id,
|
|
40
|
+
});
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isMainModule() {
|
|
46
|
+
if (!process.argv[1]) return false;
|
|
47
|
+
try {
|
|
48
|
+
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
49
|
+
} catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (isMainModule()) {
|
|
55
|
+
main();
|
|
56
|
+
}
|
package/hooks/hooks.json
CHANGED
|
@@ -33,7 +33,18 @@
|
|
|
33
33
|
},
|
|
34
34
|
{
|
|
35
35
|
"type": "command",
|
|
36
|
-
"command": "node ${CLAUDE_PLUGIN_ROOT}/hooks/record-session-mapping.mjs claude"
|
|
36
|
+
"command": "node ${CLAUDE_PLUGIN_ROOT}/hooks/record-session-mapping.mjs claude",
|
|
37
|
+
"async": true
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
],
|
|
42
|
+
"SessionEnd": [
|
|
43
|
+
{
|
|
44
|
+
"hooks": [
|
|
45
|
+
{
|
|
46
|
+
"type": "command",
|
|
47
|
+
"command": "node ${CLAUDE_PLUGIN_ROOT}/hooks/finalize-agent-session.mjs claude"
|
|
37
48
|
}
|
|
38
49
|
]
|
|
39
50
|
}
|
|
@@ -27,15 +27,18 @@ export function main({
|
|
|
27
27
|
const link = buildCommandHookLink(payload, agentType, env);
|
|
28
28
|
if (!link) return false;
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
const claim = {
|
|
31
|
+
...link,
|
|
32
|
+
cwd: link.cwd ?? process.cwd(),
|
|
33
|
+
};
|
|
34
|
+
// Claude lifecycle hooks deliberately forward only the exact harness
|
|
35
|
+
// identity, transcript, cwd, and hook source. PID is not identity and can
|
|
36
|
+
// be stale by the time an asynchronous SessionStart hook runs.
|
|
37
|
+
if (!(agentType === 'claude' && payload?.hook_event_name === 'SessionStart')) {
|
|
38
|
+
claim.pid = pid;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return linkAgentSession(claim, spawn, env);
|
|
39
42
|
} catch (error) {
|
|
40
43
|
logHookFailure(`${agentType || 'unknown'}:link-agent-session`, error, {
|
|
41
44
|
hookEventName: payload?.hook_event_name,
|
package/package.json
CHANGED