@a5c-ai/babysitter-sdk 0.0.170-staging.e67b79d3 → 0.0.170-staging.ebcc6e80
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/dist/cli/commands/hookLog.d.ts.map +1 -1
- package/dist/cli/commands/hookLog.js +10 -8
- package/dist/cli/commands/hookRun.d.ts +5 -4
- package/dist/cli/commands/hookRun.d.ts.map +1 -1
- package/dist/cli/commands/hookRun.js +24 -465
- package/dist/cli/commands/profile.d.ts +26 -0
- package/dist/cli/commands/profile.d.ts.map +1 -0
- package/dist/cli/commands/profile.js +213 -0
- package/dist/cli/commands/runIterate.d.ts.map +1 -1
- package/dist/cli/commands/runIterate.js +90 -2
- package/dist/cli/commands/session.d.ts.map +1 -1
- package/dist/cli/commands/session.js +46 -42
- package/dist/cli/commands/skill.d.ts +79 -5
- package/dist/cli/commands/skill.d.ts.map +1 -1
- package/dist/cli/commands/skill.js +486 -69
- package/dist/cli/main.d.ts +3 -2
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +172 -29
- package/dist/cli/resolveInputPath.d.ts +61 -0
- package/dist/cli/resolveInputPath.d.ts.map +1 -0
- package/dist/cli/resolveInputPath.js +188 -0
- package/dist/harness/claudeCode.d.ts +15 -0
- package/dist/harness/claudeCode.d.ts.map +1 -0
- package/dist/harness/claudeCode.js +772 -0
- package/dist/harness/index.d.ts +5 -0
- package/dist/harness/index.d.ts.map +1 -0
- package/dist/harness/index.js +14 -0
- package/dist/harness/nullAdapter.d.ts +9 -0
- package/dist/harness/nullAdapter.d.ts.map +1 -0
- package/dist/harness/nullAdapter.js +46 -0
- package/dist/harness/registry.d.ts +35 -0
- package/dist/harness/registry.d.ts.map +1 -0
- package/dist/harness/registry.js +79 -0
- package/dist/harness/types.d.ts +60 -0
- package/dist/harness/types.d.ts.map +1 -0
- package/dist/harness/types.js +10 -0
- package/dist/hooks/dispatcher.d.ts +3 -0
- package/dist/hooks/dispatcher.d.ts.map +1 -1
- package/dist/hooks/dispatcher.js +8 -6
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/profiles/index.d.ts +25 -0
- package/dist/profiles/index.d.ts.map +1 -0
- package/dist/profiles/index.js +40 -0
- package/dist/profiles/projectProfile.d.ts +73 -0
- package/dist/profiles/projectProfile.d.ts.map +1 -0
- package/dist/profiles/projectProfile.js +747 -0
- package/dist/profiles/types.d.ts +586 -0
- package/dist/profiles/types.d.ts.map +1 -0
- package/dist/profiles/types.js +36 -0
- package/dist/profiles/userProfile.d.ts +73 -0
- package/dist/profiles/userProfile.d.ts.map +1 -0
- package/dist/profiles/userProfile.js +547 -0
- package/dist/runtime/orchestrateIteration.d.ts.map +1 -1
- package/dist/runtime/orchestrateIteration.js +7 -0
- package/dist/runtime/replay/effectIndex.d.ts.map +1 -1
- package/dist/runtime/replay/effectIndex.js +3 -4
- package/dist/session/write.d.ts +2 -2
- package/dist/session/write.js +5 -5
- package/package.json +5 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hookLog.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/hookLog.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;
|
|
1
|
+
{"version":3,"file":"hookLog.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/hookLog.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAWH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;CACf;AAoLD,wBAAsB,aAAa,CAAC,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,MAAM,CAAC,CAwG7E"}
|
|
@@ -44,6 +44,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
44
44
|
exports.handleHookLog = handleHookLog;
|
|
45
45
|
const node_fs_1 = require("node:fs");
|
|
46
46
|
const path = __importStar(require("node:path"));
|
|
47
|
+
const resolveInputPath_1 = require("../resolveInputPath");
|
|
47
48
|
// ---------------------------------------------------------------------------
|
|
48
49
|
// Event label mapping (matches the label used in the original logger.sh)
|
|
49
50
|
// ---------------------------------------------------------------------------
|
|
@@ -189,7 +190,7 @@ async function handleHookLog(args) {
|
|
|
189
190
|
if (!hookType) {
|
|
190
191
|
const error = { error: "MISSING_HOOK_TYPE", message: "--hook-type is required" };
|
|
191
192
|
if (json) {
|
|
192
|
-
console.error(JSON.stringify(error));
|
|
193
|
+
console.error(JSON.stringify(error, null, 2));
|
|
193
194
|
}
|
|
194
195
|
else {
|
|
195
196
|
console.error("Error: --hook-type is required for hook:log");
|
|
@@ -200,7 +201,7 @@ async function handleHookLog(args) {
|
|
|
200
201
|
if (!logFile) {
|
|
201
202
|
const error = { error: "MISSING_LOG_FILE", message: "--log-file is required" };
|
|
202
203
|
if (json) {
|
|
203
|
-
console.error(JSON.stringify(error));
|
|
204
|
+
console.error(JSON.stringify(error, null, 2));
|
|
204
205
|
}
|
|
205
206
|
else {
|
|
206
207
|
console.error("Error: --log-file is required for hook:log");
|
|
@@ -216,7 +217,7 @@ async function handleHookLog(args) {
|
|
|
216
217
|
const msg = e instanceof Error ? e.message : String(e);
|
|
217
218
|
const error = { error: "STDIN_READ_ERROR", message: msg };
|
|
218
219
|
if (json) {
|
|
219
|
-
console.error(JSON.stringify(error));
|
|
220
|
+
console.error(JSON.stringify(error, null, 2));
|
|
220
221
|
}
|
|
221
222
|
else {
|
|
222
223
|
console.error(`Error: Failed to read stdin: ${msg}`);
|
|
@@ -236,7 +237,7 @@ async function handleHookLog(args) {
|
|
|
236
237
|
const msg = e instanceof Error ? e.message : String(e);
|
|
237
238
|
const error = { error: "INVALID_PAYLOAD", message: msg };
|
|
238
239
|
if (json) {
|
|
239
|
-
console.error(JSON.stringify(error));
|
|
240
|
+
console.error(JSON.stringify(error, null, 2));
|
|
240
241
|
}
|
|
241
242
|
else {
|
|
242
243
|
console.error(`Error: Failed to parse JSON payload: ${msg}`);
|
|
@@ -255,15 +256,16 @@ async function handleHookLog(args) {
|
|
|
255
256
|
const logLine = formatLogLine(hookType, eventLabel, fields);
|
|
256
257
|
// Ensure log directory exists and append
|
|
257
258
|
try {
|
|
258
|
-
const
|
|
259
|
+
const resolvedLogFile = (0, resolveInputPath_1.resolveInputPath)(logFile);
|
|
260
|
+
const logDir = path.dirname(resolvedLogFile);
|
|
259
261
|
await node_fs_1.promises.mkdir(logDir, { recursive: true });
|
|
260
|
-
await node_fs_1.promises.appendFile(
|
|
262
|
+
await node_fs_1.promises.appendFile(resolvedLogFile, logLine + "\n", "utf8");
|
|
261
263
|
}
|
|
262
264
|
catch (e) {
|
|
263
265
|
const msg = e instanceof Error ? e.message : String(e);
|
|
264
266
|
const error = { error: "LOG_WRITE_ERROR", message: msg };
|
|
265
267
|
if (json) {
|
|
266
|
-
console.error(JSON.stringify(error));
|
|
268
|
+
console.error(JSON.stringify(error, null, 2));
|
|
267
269
|
}
|
|
268
270
|
else {
|
|
269
271
|
console.error(`Error: Failed to write to log file: ${msg}`);
|
|
@@ -275,7 +277,7 @@ async function handleHookLog(args) {
|
|
|
275
277
|
console.log(JSON.stringify({
|
|
276
278
|
hookType,
|
|
277
279
|
eventLabel,
|
|
278
|
-
logFile:
|
|
280
|
+
logFile: (0, resolveInputPath_1.resolveInputPath)(logFile),
|
|
279
281
|
logLine,
|
|
280
282
|
}));
|
|
281
283
|
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* hook:run CLI command.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* - "session-start" → handleHookRunSessionStart (replaces babysitter-session-start-hook.sh)
|
|
4
|
+
* Dispatches hook handling to the appropriate harness adapter.
|
|
5
|
+
* Each harness (e.g. "claude-code") implements its own stop and
|
|
6
|
+
* session-start handlers via the HarnessAdapter interface.
|
|
8
7
|
*/
|
|
9
8
|
export interface HookRunCommandArgs {
|
|
10
9
|
hookType: string;
|
|
10
|
+
/** Which host tool is invoking the hook. Defaults to "claude-code". */
|
|
11
|
+
harness: string;
|
|
11
12
|
pluginRoot?: string;
|
|
12
13
|
stateDir?: string;
|
|
13
14
|
runsDir?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hookRun.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/hookRun.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"hookRun.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/hookRun.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,uEAAuE;IACvE,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAMD,wBAAsB,aAAa,CAAC,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,MAAM,CAAC,CAmD7E"}
|
|
@@ -2,499 +2,58 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* hook:run CLI command.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* - "session-start" → handleHookRunSessionStart (replaces babysitter-session-start-hook.sh)
|
|
5
|
+
* Dispatches hook handling to the appropriate harness adapter.
|
|
6
|
+
* Each harness (e.g. "claude-code") implements its own stop and
|
|
7
|
+
* session-start handlers via the HarnessAdapter interface.
|
|
9
8
|
*/
|
|
10
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
11
|
-
if (k2 === undefined) k2 = k;
|
|
12
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
13
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
14
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
15
|
-
}
|
|
16
|
-
Object.defineProperty(o, k2, desc);
|
|
17
|
-
}) : (function(o, m, k, k2) {
|
|
18
|
-
if (k2 === undefined) k2 = k;
|
|
19
|
-
o[k2] = m[k];
|
|
20
|
-
}));
|
|
21
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
22
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
23
|
-
}) : function(o, v) {
|
|
24
|
-
o["default"] = v;
|
|
25
|
-
});
|
|
26
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
27
|
-
var ownKeys = function(o) {
|
|
28
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
29
|
-
var ar = [];
|
|
30
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
31
|
-
return ar;
|
|
32
|
-
};
|
|
33
|
-
return ownKeys(o);
|
|
34
|
-
};
|
|
35
|
-
return function (mod) {
|
|
36
|
-
if (mod && mod.__esModule) return mod;
|
|
37
|
-
var result = {};
|
|
38
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
39
|
-
__setModuleDefault(result, mod);
|
|
40
|
-
return result;
|
|
41
|
-
};
|
|
42
|
-
})();
|
|
43
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
10
|
exports.handleHookRun = handleHookRun;
|
|
45
|
-
const
|
|
46
|
-
const node_fs_1 = require("node:fs");
|
|
47
|
-
const journal_1 = require("../../storage/journal");
|
|
48
|
-
const runFiles_1 = require("../../storage/runFiles");
|
|
49
|
-
const effectIndex_1 = require("../../runtime/replay/effectIndex");
|
|
50
|
-
const completionProof_1 = require("../completionProof");
|
|
51
|
-
const skill_1 = require("./skill");
|
|
52
|
-
const session_1 = require("../../session");
|
|
53
|
-
const session_2 = require("./session");
|
|
54
|
-
function createHookLogger(hookName) {
|
|
55
|
-
const logDir = process.env.BABYSITTER_LOG_DIR;
|
|
56
|
-
const logFile = logDir ? path.join(logDir, `${hookName}.log`) : null;
|
|
57
|
-
const context = {};
|
|
58
|
-
if (logFile) {
|
|
59
|
-
try {
|
|
60
|
-
(0, node_fs_1.mkdirSync)(logDir, { recursive: true });
|
|
61
|
-
}
|
|
62
|
-
catch {
|
|
63
|
-
// Best-effort
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function write(level, message) {
|
|
67
|
-
if (!logFile)
|
|
68
|
-
return;
|
|
69
|
-
const ts = new Date().toISOString();
|
|
70
|
-
const ctxParts = Object.entries(context).map(([k, v]) => `${k}=${v}`);
|
|
71
|
-
const ctxStr = ctxParts.length > 0 ? ` [${ctxParts.join(" ")}]` : "";
|
|
72
|
-
const line = `[${level}] ${ts}${ctxStr} ${message}\n`;
|
|
73
|
-
try {
|
|
74
|
-
(0, node_fs_1.appendFileSync)(logFile, line);
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
// Best-effort
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return {
|
|
81
|
-
info: (msg) => write("INFO", msg),
|
|
82
|
-
warn: (msg) => write("WARN", msg),
|
|
83
|
-
error: (msg) => write("ERROR", msg),
|
|
84
|
-
setContext: (key, value) => {
|
|
85
|
-
context[key] = value;
|
|
86
|
-
},
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
// ---------------------------------------------------------------------------
|
|
90
|
-
// Stdin reader
|
|
91
|
-
// ---------------------------------------------------------------------------
|
|
92
|
-
async function readStdin() {
|
|
93
|
-
return new Promise((resolve, reject) => {
|
|
94
|
-
let data = "";
|
|
95
|
-
process.stdin.setEncoding("utf8");
|
|
96
|
-
process.stdin.on("data", (chunk) => {
|
|
97
|
-
data += chunk;
|
|
98
|
-
});
|
|
99
|
-
process.stdin.on("end", () => resolve(data));
|
|
100
|
-
process.stdin.on("error", reject);
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
function parseHookInput(raw) {
|
|
104
|
-
const trimmed = raw.trim();
|
|
105
|
-
if (!trimmed)
|
|
106
|
-
return {};
|
|
107
|
-
try {
|
|
108
|
-
const parsed = JSON.parse(trimmed);
|
|
109
|
-
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
110
|
-
return parsed;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
catch {
|
|
114
|
-
// Malformed JSON — treat as empty
|
|
115
|
-
}
|
|
116
|
-
return {};
|
|
117
|
-
}
|
|
118
|
-
function safeStr(obj, key) {
|
|
119
|
-
const val = obj[key];
|
|
120
|
-
return typeof val === "string" ? val : "";
|
|
121
|
-
}
|
|
122
|
-
// ---------------------------------------------------------------------------
|
|
123
|
-
// Pending-by-kind helper (mirrors session.ts countPendingByKind)
|
|
124
|
-
// ---------------------------------------------------------------------------
|
|
125
|
-
function countPendingByKind(records) {
|
|
126
|
-
const counts = new Map();
|
|
127
|
-
for (const record of records) {
|
|
128
|
-
const key = record.kind ?? "unknown";
|
|
129
|
-
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
130
|
-
}
|
|
131
|
-
return Object.fromEntries(Array.from(counts.entries()).sort(([a], [b]) => a.localeCompare(b)));
|
|
132
|
-
}
|
|
133
|
-
// ---------------------------------------------------------------------------
|
|
134
|
-
// handleHookRunStop — replaces babysitter-stop-hook.sh (249 lines)
|
|
135
|
-
// ---------------------------------------------------------------------------
|
|
136
|
-
async function handleHookRunStop(args) {
|
|
137
|
-
const { verbose } = args;
|
|
138
|
-
const log = createHookLogger("babysitter-stop-hook");
|
|
139
|
-
log.info("handleHookRunStop started");
|
|
140
|
-
// 1. Read hook input JSON from stdin
|
|
141
|
-
let rawInput;
|
|
142
|
-
try {
|
|
143
|
-
rawInput = await readStdin();
|
|
144
|
-
}
|
|
145
|
-
catch (e) {
|
|
146
|
-
const msg = e instanceof Error ? e.message : String(e);
|
|
147
|
-
log.warn(`stdin read error: ${msg}`);
|
|
148
|
-
if (verbose) {
|
|
149
|
-
process.stderr.write(`[hook:run stop] stdin read error: ${msg}\n`);
|
|
150
|
-
}
|
|
151
|
-
// Allow exit on error
|
|
152
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
153
|
-
return 0;
|
|
154
|
-
}
|
|
155
|
-
const hookInput = parseHookInput(rawInput);
|
|
156
|
-
log.info("Hook input received");
|
|
157
|
-
const sessionId = safeStr(hookInput, "session_id");
|
|
158
|
-
if (!sessionId) {
|
|
159
|
-
// No session ID — allow exit
|
|
160
|
-
log.info("No session ID in hook input — allowing exit");
|
|
161
|
-
if (verbose) {
|
|
162
|
-
process.stderr.write("[hook:run stop] No session ID in hook input\n");
|
|
163
|
-
}
|
|
164
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
165
|
-
return 0;
|
|
166
|
-
}
|
|
167
|
-
log.setContext("session", sessionId);
|
|
168
|
-
// 2. Resolve pluginRoot and stateDir
|
|
169
|
-
const pluginRoot = args.pluginRoot || process.env.CLAUDE_PLUGIN_ROOT || "";
|
|
170
|
-
const stateDir = args.stateDir ||
|
|
171
|
-
(pluginRoot ? path.join(pluginRoot, "skills", "babysit", "state") : "");
|
|
172
|
-
if (!stateDir) {
|
|
173
|
-
if (verbose) {
|
|
174
|
-
process.stderr.write("[hook:run stop] Cannot determine state directory\n");
|
|
175
|
-
}
|
|
176
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
177
|
-
return 0;
|
|
178
|
-
}
|
|
179
|
-
const runsDir = args.runsDir || ".a5c/runs";
|
|
180
|
-
// 3. Check iteration (replaces session:check-iteration CLI call)
|
|
181
|
-
const filePath = (0, session_1.getSessionFilePath)(stateDir, sessionId);
|
|
182
|
-
let sessionFile;
|
|
183
|
-
try {
|
|
184
|
-
if (!(await (0, session_1.sessionFileExists)(filePath))) {
|
|
185
|
-
// No active loop
|
|
186
|
-
if (verbose) {
|
|
187
|
-
process.stderr.write(`[hook:run stop] No active loop found for session ${sessionId}\n`);
|
|
188
|
-
}
|
|
189
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
190
|
-
return 0;
|
|
191
|
-
}
|
|
192
|
-
sessionFile = await (0, session_1.readSessionFile)(filePath);
|
|
193
|
-
}
|
|
194
|
-
catch {
|
|
195
|
-
// Cannot read session — allow exit
|
|
196
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
197
|
-
return 0;
|
|
198
|
-
}
|
|
199
|
-
const { state } = sessionFile;
|
|
200
|
-
const prompt = sessionFile.prompt ?? "";
|
|
201
|
-
// Check max iterations
|
|
202
|
-
if (state.maxIterations > 0 && state.iteration >= state.maxIterations) {
|
|
203
|
-
if (verbose) {
|
|
204
|
-
process.stderr.write(`[hook:run stop] Max iterations (${state.maxIterations}) reached\n`);
|
|
205
|
-
}
|
|
206
|
-
await cleanupSession(filePath);
|
|
207
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
208
|
-
return 0;
|
|
209
|
-
}
|
|
210
|
-
// Check iteration timing (runaway loop detection)
|
|
211
|
-
const now = (0, session_1.getCurrentTimestamp)();
|
|
212
|
-
const updatedTimes = state.iteration >= 5
|
|
213
|
-
? (0, session_1.updateIterationTimes)(state.iterationTimes, state.lastIterationAt, now)
|
|
214
|
-
: state.iterationTimes;
|
|
215
|
-
if ((0, session_1.isIterationTooFast)(updatedTimes)) {
|
|
216
|
-
if (verbose) {
|
|
217
|
-
const avg = updatedTimes.reduce((a, b) => a + b, 0) / updatedTimes.length;
|
|
218
|
-
process.stderr.write(`[hook:run stop] Iteration too fast (avg ${avg}s)\n`);
|
|
219
|
-
}
|
|
220
|
-
await cleanupSession(filePath);
|
|
221
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
222
|
-
return 0;
|
|
223
|
-
}
|
|
224
|
-
// shouldContinue=true at this point
|
|
225
|
-
const iteration = state.iteration;
|
|
226
|
-
const maxIterations = state.maxIterations;
|
|
227
|
-
const runId = state.runId ?? "";
|
|
228
|
-
if (runId) {
|
|
229
|
-
log.setContext("run", runId);
|
|
230
|
-
}
|
|
231
|
-
// 4. Parse transcript for last assistant message
|
|
232
|
-
const transcriptPath = safeStr(hookInput, "transcript_path");
|
|
233
|
-
let lastText = null;
|
|
234
|
-
let hasPromise = false;
|
|
235
|
-
let promiseValue = null;
|
|
236
|
-
if (transcriptPath) {
|
|
237
|
-
const resolvedTranscript = path.resolve(transcriptPath);
|
|
238
|
-
if ((0, node_fs_1.existsSync)(resolvedTranscript)) {
|
|
239
|
-
try {
|
|
240
|
-
const content = (0, node_fs_1.readFileSync)(resolvedTranscript, "utf-8");
|
|
241
|
-
const parsed = (0, session_2.parseTranscriptLastAssistantMessage)(content);
|
|
242
|
-
lastText = parsed.text;
|
|
243
|
-
if (parsed.found && parsed.text) {
|
|
244
|
-
promiseValue = (0, session_2.extractPromiseTag)(parsed.text);
|
|
245
|
-
hasPromise = promiseValue !== null;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
catch {
|
|
249
|
-
// Transcript parse error — cleanup and allow exit
|
|
250
|
-
if (verbose) {
|
|
251
|
-
process.stderr.write(`[hook:run stop] Transcript parse error: ${resolvedTranscript}\n`);
|
|
252
|
-
}
|
|
253
|
-
await cleanupSession(filePath);
|
|
254
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
255
|
-
return 0;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
else {
|
|
259
|
-
if (verbose) {
|
|
260
|
-
process.stderr.write(`[hook:run stop] Transcript not found: ${resolvedTranscript}\n`);
|
|
261
|
-
}
|
|
262
|
-
await cleanupSession(filePath);
|
|
263
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
264
|
-
return 0;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
if (!lastText) {
|
|
268
|
-
if (verbose) {
|
|
269
|
-
process.stderr.write("[hook:run stop] No assistant text content found in transcript\n");
|
|
270
|
-
}
|
|
271
|
-
await cleanupSession(filePath);
|
|
272
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
273
|
-
return 0;
|
|
274
|
-
}
|
|
275
|
-
// 5. If runId is present, get run status
|
|
276
|
-
let runState = "";
|
|
277
|
-
let completionProof = "";
|
|
278
|
-
let pendingKinds = "";
|
|
279
|
-
if (runId) {
|
|
280
|
-
try {
|
|
281
|
-
const runDir = path.isAbsolute(runId)
|
|
282
|
-
? runId
|
|
283
|
-
: path.join(runsDir, runId);
|
|
284
|
-
const metadata = await (0, runFiles_1.readRunMetadata)(runDir);
|
|
285
|
-
const journal = await (0, journal_1.loadJournal)(runDir);
|
|
286
|
-
const index = await (0, effectIndex_1.buildEffectIndex)({ runDir, events: journal });
|
|
287
|
-
const hasCompleted = journal.some((e) => e.type === "RUN_COMPLETED");
|
|
288
|
-
const hasFailed = journal.some((e) => e.type === "RUN_FAILED");
|
|
289
|
-
const pendingRecords = index.listPendingEffects();
|
|
290
|
-
const pendingByKind = countPendingByKind(pendingRecords);
|
|
291
|
-
const kindKeys = Object.keys(pendingByKind);
|
|
292
|
-
if (kindKeys.length > 0) {
|
|
293
|
-
pendingKinds = kindKeys.join(", ");
|
|
294
|
-
}
|
|
295
|
-
if (hasCompleted) {
|
|
296
|
-
runState = "completed";
|
|
297
|
-
completionProof = (0, completionProof_1.resolveCompletionProof)(metadata);
|
|
298
|
-
}
|
|
299
|
-
else if (hasFailed) {
|
|
300
|
-
runState = "failed";
|
|
301
|
-
}
|
|
302
|
-
else if (pendingRecords.length > 0) {
|
|
303
|
-
runState = "waiting";
|
|
304
|
-
}
|
|
305
|
-
else {
|
|
306
|
-
runState = "created";
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
catch {
|
|
310
|
-
runState = "";
|
|
311
|
-
}
|
|
312
|
-
log.info(`Run state: ${runState || "unknown"}`);
|
|
313
|
-
if (completionProof) {
|
|
314
|
-
log.info("Completion proof available");
|
|
315
|
-
}
|
|
316
|
-
// If state is empty (couldn't determine) → cleanup and allow
|
|
317
|
-
if (!runState) {
|
|
318
|
-
if (verbose) {
|
|
319
|
-
process.stderr.write(`[hook:run stop] Run state is empty for ${runId}; run may be misconfigured\n`);
|
|
320
|
-
}
|
|
321
|
-
await cleanupSession(filePath);
|
|
322
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
323
|
-
return 0;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
// 6. If completionProof matches promiseValue → complete
|
|
327
|
-
if (hasPromise) {
|
|
328
|
-
log.info("Detected valid promise tag");
|
|
329
|
-
}
|
|
330
|
-
if (completionProof && hasPromise && promiseValue === completionProof) {
|
|
331
|
-
log.info("Promise matches completion proof — allowing exit");
|
|
332
|
-
if (verbose) {
|
|
333
|
-
process.stderr.write(`[hook:run stop] Valid promise tag detected - run complete\n`);
|
|
334
|
-
}
|
|
335
|
-
await cleanupSession(filePath);
|
|
336
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
337
|
-
return 0;
|
|
338
|
-
}
|
|
339
|
-
// 7. Not complete → continue loop
|
|
340
|
-
if (!prompt) {
|
|
341
|
-
if (verbose) {
|
|
342
|
-
process.stderr.write(`[hook:run stop] State file corrupted - no prompt text\n`);
|
|
343
|
-
}
|
|
344
|
-
await cleanupSession(filePath);
|
|
345
|
-
process.stdout.write('{"decision":"allow"}\n');
|
|
346
|
-
return 0;
|
|
347
|
-
}
|
|
348
|
-
const nextIteration = iteration + 1;
|
|
349
|
-
const currentTime = (0, session_1.getCurrentTimestamp)();
|
|
350
|
-
// Update session state
|
|
351
|
-
const updatedState = {
|
|
352
|
-
...state,
|
|
353
|
-
iteration: nextIteration,
|
|
354
|
-
lastIterationAt: currentTime,
|
|
355
|
-
iterationTimes: updatedTimes,
|
|
356
|
-
};
|
|
357
|
-
try {
|
|
358
|
-
await (0, session_1.writeSessionFile)(filePath, updatedState, prompt);
|
|
359
|
-
}
|
|
360
|
-
catch {
|
|
361
|
-
if (verbose) {
|
|
362
|
-
process.stderr.write(`[hook:run stop] Failed to update session state\n`);
|
|
363
|
-
}
|
|
364
|
-
// Continue anyway — state might be stale but loop should work
|
|
365
|
-
}
|
|
366
|
-
// 8. Build system message
|
|
367
|
-
let systemMessage;
|
|
368
|
-
if (completionProof) {
|
|
369
|
-
systemMessage = `\u{1F504} Babysitter iteration ${nextIteration} | Run completed! To finish: agent must call 'run:status --json' on your run, extract 'completionProof' from the output, then output it in <promise>SECRET</promise> tags. Do not mention or reveal the secret otherwise.`;
|
|
370
|
-
}
|
|
371
|
-
else if (runState === "waiting" && pendingKinds) {
|
|
372
|
-
systemMessage = `\u{1F504} Babysitter iteration ${nextIteration} | Waiting on: ${pendingKinds}. Check if pending effects are resolved, then call run:iterate.`;
|
|
373
|
-
}
|
|
374
|
-
else if (runState === "failed") {
|
|
375
|
-
systemMessage = `\u{1F504} Babysitter iteration ${nextIteration} | Failed. agent must fix the run, journal or process (inspect the sdk.md if needed) and proceed.`;
|
|
376
|
-
}
|
|
377
|
-
else {
|
|
378
|
-
systemMessage = `\u{1F504} Babysitter iteration ${nextIteration} | Agent should continue orchestration (run:iterate)`;
|
|
379
|
-
}
|
|
380
|
-
// 9. Try to resolve skill context
|
|
381
|
-
if (pluginRoot) {
|
|
382
|
-
try {
|
|
383
|
-
const discoverResult = await (0, skill_1.discoverSkillsInternal)({
|
|
384
|
-
pluginRoot,
|
|
385
|
-
runId: runId || undefined,
|
|
386
|
-
runsDir,
|
|
387
|
-
});
|
|
388
|
-
if (discoverResult.summary) {
|
|
389
|
-
systemMessage = `${systemMessage} | Available skills for this task: ${discoverResult.summary}. Use the Skill tool or skill-discovery to load any of these.`;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
catch {
|
|
393
|
-
// Skill discovery failure is non-fatal
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
// Cap system message at 1200 chars
|
|
397
|
-
if (systemMessage.length > 1200) {
|
|
398
|
-
systemMessage = systemMessage.slice(0, 1197) + "...";
|
|
399
|
-
}
|
|
400
|
-
// 10. Output block decision
|
|
401
|
-
const output = {
|
|
402
|
-
decision: "block",
|
|
403
|
-
instructions: "use the babysitter skill to advance the orchestration to the next state (run:iterate) or perform the pending effects (task:list --pending --json), or fix the run if it failed.",
|
|
404
|
-
reason: prompt,
|
|
405
|
-
systemMessage,
|
|
406
|
-
};
|
|
407
|
-
process.stdout.write(JSON.stringify(output) + "\n");
|
|
408
|
-
log.info(`Decision: block (iteration=${nextIteration}, maxIterations=${maxIterations})`);
|
|
409
|
-
if (verbose) {
|
|
410
|
-
process.stderr.write(`[hook:run stop] Blocking stop, iteration=${nextIteration} maxIterations=${maxIterations}\n`);
|
|
411
|
-
}
|
|
412
|
-
return 0;
|
|
413
|
-
}
|
|
414
|
-
// ---------------------------------------------------------------------------
|
|
415
|
-
// handleHookRunSessionStart — replaces babysitter-session-start-hook.sh
|
|
416
|
-
// ---------------------------------------------------------------------------
|
|
417
|
-
async function handleHookRunSessionStart(args) {
|
|
418
|
-
const { verbose } = args;
|
|
419
|
-
// 1. Read hook input JSON from stdin
|
|
420
|
-
let rawInput;
|
|
421
|
-
try {
|
|
422
|
-
rawInput = await readStdin();
|
|
423
|
-
}
|
|
424
|
-
catch {
|
|
425
|
-
process.stdout.write("{}\n");
|
|
426
|
-
return 0;
|
|
427
|
-
}
|
|
428
|
-
const hookInput = parseHookInput(rawInput);
|
|
429
|
-
const sessionId = safeStr(hookInput, "session_id");
|
|
430
|
-
if (!sessionId) {
|
|
431
|
-
process.stdout.write("{}\n");
|
|
432
|
-
return 0;
|
|
433
|
-
}
|
|
434
|
-
// 2. If CLAUDE_ENV_FILE is set, append session ID export
|
|
435
|
-
const envFile = process.env.CLAUDE_ENV_FILE;
|
|
436
|
-
if (envFile) {
|
|
437
|
-
try {
|
|
438
|
-
(0, node_fs_1.appendFileSync)(envFile, `export CLAUDE_SESSION_ID="${sessionId}"\n`);
|
|
439
|
-
}
|
|
440
|
-
catch {
|
|
441
|
-
// Non-fatal — env file write failure
|
|
442
|
-
if (verbose) {
|
|
443
|
-
process.stderr.write(`[hook:run session-start] Failed to write to CLAUDE_ENV_FILE: ${envFile}\n`);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
if (verbose) {
|
|
448
|
-
process.stderr.write(`Babysitter session started: ${sessionId}\n`);
|
|
449
|
-
}
|
|
450
|
-
// 3. Output empty object
|
|
451
|
-
process.stdout.write("{}\n");
|
|
452
|
-
return 0;
|
|
453
|
-
}
|
|
454
|
-
// ---------------------------------------------------------------------------
|
|
455
|
-
// Cleanup helper
|
|
456
|
-
// ---------------------------------------------------------------------------
|
|
457
|
-
async function cleanupSession(filePath) {
|
|
458
|
-
try {
|
|
459
|
-
await (0, session_1.deleteSessionFile)(filePath);
|
|
460
|
-
}
|
|
461
|
-
catch {
|
|
462
|
-
// Best-effort cleanup
|
|
463
|
-
}
|
|
464
|
-
}
|
|
11
|
+
const harness_1 = require("../../harness");
|
|
465
12
|
// ---------------------------------------------------------------------------
|
|
466
13
|
// Main dispatcher
|
|
467
14
|
// ---------------------------------------------------------------------------
|
|
468
15
|
async function handleHookRun(args) {
|
|
469
|
-
const { hookType, json } = args;
|
|
470
|
-
// Early diagnostic log — helps verify CLI dispatch reaches this handler
|
|
471
|
-
const dispatchLog = createHookLogger("babysitter-stop-hook");
|
|
472
|
-
dispatchLog.info(`handleHookRun dispatching hookType=${hookType || "(empty)"}`);
|
|
16
|
+
const { hookType, harness, json } = args;
|
|
473
17
|
if (!hookType) {
|
|
474
18
|
const error = {
|
|
475
19
|
error: "MISSING_HOOK_TYPE",
|
|
476
20
|
message: "--hook-type is required for hook:run",
|
|
477
21
|
};
|
|
478
22
|
if (json) {
|
|
479
|
-
process.stderr.write(JSON.stringify(error) + "\n");
|
|
23
|
+
process.stderr.write(JSON.stringify(error, null, 2) + "\n");
|
|
480
24
|
}
|
|
481
25
|
else {
|
|
482
26
|
process.stderr.write("Error: --hook-type is required for hook:run\n");
|
|
483
27
|
}
|
|
484
28
|
return 1;
|
|
485
29
|
}
|
|
30
|
+
const adapter = (0, harness_1.getAdapterByName)(harness);
|
|
31
|
+
if (!adapter) {
|
|
32
|
+
const supported = (0, harness_1.listSupportedHarnesses)();
|
|
33
|
+
const error = {
|
|
34
|
+
error: "UNSUPPORTED_HARNESS",
|
|
35
|
+
message: `Unsupported harness: "${harness}". Supported: ${supported.join(", ")}`,
|
|
36
|
+
};
|
|
37
|
+
if (json) {
|
|
38
|
+
process.stderr.write(JSON.stringify(error, null, 2) + "\n");
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
process.stderr.write(`Error: ${error.message}\n`);
|
|
42
|
+
}
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
486
45
|
switch (hookType) {
|
|
487
46
|
case "stop":
|
|
488
|
-
return await
|
|
47
|
+
return await adapter.handleStopHook(args);
|
|
489
48
|
case "session-start":
|
|
490
|
-
return await
|
|
49
|
+
return await adapter.handleSessionStartHook(args);
|
|
491
50
|
default: {
|
|
492
51
|
const error = {
|
|
493
52
|
error: "UNKNOWN_HOOK_TYPE",
|
|
494
53
|
message: `Unknown hook type: ${hookType}. Supported: stop, session-start`,
|
|
495
54
|
};
|
|
496
55
|
if (json) {
|
|
497
|
-
process.stderr.write(JSON.stringify(error) + "\n");
|
|
56
|
+
process.stderr.write(JSON.stringify(error, null, 2) + "\n");
|
|
498
57
|
}
|
|
499
58
|
else {
|
|
500
59
|
process.stderr.write(`Error: Unknown hook type: ${hookType}. Supported: stop, session-start\n`);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* profile:* commands — Read, write, merge, and render user/project profiles.
|
|
3
|
+
*
|
|
4
|
+
* Commands:
|
|
5
|
+
* profile:read --user|--project [--dir <dir>] [--json]
|
|
6
|
+
* profile:write --user|--project --input <file> [--dir <dir>] [--json]
|
|
7
|
+
* profile:merge --user|--project --input <file> [--dir <dir>] [--json]
|
|
8
|
+
* profile:render --user|--project [--dir <dir>] [--json]
|
|
9
|
+
*/
|
|
10
|
+
export interface ProfileCommandArgs {
|
|
11
|
+
subcommand: "read" | "write" | "merge" | "render";
|
|
12
|
+
user: boolean;
|
|
13
|
+
project: boolean;
|
|
14
|
+
inputPath?: string;
|
|
15
|
+
dir?: string;
|
|
16
|
+
json: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare function handleProfileRead(args: ProfileCommandArgs): number;
|
|
19
|
+
export declare function handleProfileWrite(args: ProfileCommandArgs): Promise<number>;
|
|
20
|
+
export declare function handleProfileMerge(args: ProfileCommandArgs): Promise<number>;
|
|
21
|
+
export declare function handleProfileRender(args: ProfileCommandArgs): number;
|
|
22
|
+
/**
|
|
23
|
+
* Route a profile:* command to the appropriate handler.
|
|
24
|
+
*/
|
|
25
|
+
export declare function handleProfileCommand(subcommand: string, args: ProfileCommandArgs): Promise<number>;
|
|
26
|
+
//# sourceMappingURL=profile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"profile.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/profile.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AA4BH,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,QAAQ,CAAC;IAClD,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,OAAO,CAAC;CACf;AAMD,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,kBAAkB,GAAG,MAAM,CAyClE;AAED,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,MAAM,CAAC,CAwClF;AAED,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,MAAM,CAAC,CAkDlF;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,kBAAkB,GAAG,MAAM,CAmCpE;AAED;;GAEG;AACH,wBAAsB,oBAAoB,CACxC,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,MAAM,CAAC,CAcjB"}
|