@cleocode/cleo 2026.3.71 → 2026.3.73
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/index.js +731 -107
- package/dist/cli/index.js.map +4 -4
- package/dist/mcp/index.js +661 -108
- package/dist/mcp/index.js.map +4 -4
- package/package.json +4 -4
package/dist/mcp/index.js
CHANGED
|
@@ -733,22 +733,42 @@ __export(registry_exports, {
|
|
|
733
733
|
HookRegistry: () => HookRegistry,
|
|
734
734
|
hooks: () => hooks
|
|
735
735
|
});
|
|
736
|
-
var DEFAULT_HOOK_CONFIG, HookRegistry, hooks;
|
|
736
|
+
var LEGACY_EVENT_MAP, DEFAULT_HOOK_CONFIG, HookRegistry, hooks;
|
|
737
737
|
var init_registry = __esm({
|
|
738
738
|
"packages/core/src/hooks/registry.ts"() {
|
|
739
739
|
"use strict";
|
|
740
740
|
init_logger();
|
|
741
|
+
LEGACY_EVENT_MAP = {
|
|
742
|
+
onSessionStart: "SessionStart",
|
|
743
|
+
onSessionEnd: "SessionEnd",
|
|
744
|
+
onToolStart: "PreToolUse",
|
|
745
|
+
onToolComplete: "PostToolUse",
|
|
746
|
+
onFileChange: "Notification",
|
|
747
|
+
onError: "PostToolUseFailure",
|
|
748
|
+
onPromptSubmit: "PromptSubmit",
|
|
749
|
+
onResponseComplete: "ResponseComplete"
|
|
750
|
+
};
|
|
741
751
|
DEFAULT_HOOK_CONFIG = {
|
|
742
752
|
enabled: true,
|
|
743
753
|
events: {
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
754
|
+
// CAAMP canonical events (16)
|
|
755
|
+
SessionStart: true,
|
|
756
|
+
SessionEnd: true,
|
|
757
|
+
PromptSubmit: true,
|
|
758
|
+
ResponseComplete: true,
|
|
759
|
+
PreToolUse: true,
|
|
760
|
+
PostToolUse: true,
|
|
761
|
+
PostToolUseFailure: true,
|
|
762
|
+
PermissionRequest: true,
|
|
763
|
+
SubagentStart: true,
|
|
764
|
+
SubagentStop: true,
|
|
765
|
+
PreModel: true,
|
|
766
|
+
PostModel: true,
|
|
767
|
+
PreCompact: true,
|
|
768
|
+
PostCompact: true,
|
|
769
|
+
Notification: true,
|
|
770
|
+
ConfigChange: true,
|
|
771
|
+
// CLEO internal coordination events (5)
|
|
752
772
|
onWorkAvailable: true,
|
|
753
773
|
onAgentSpawn: true,
|
|
754
774
|
onAgentComplete: true,
|
|
@@ -759,12 +779,33 @@ var init_registry = __esm({
|
|
|
759
779
|
HookRegistry = class {
|
|
760
780
|
handlers = /* @__PURE__ */ new Map();
|
|
761
781
|
config = DEFAULT_HOOK_CONFIG;
|
|
782
|
+
/**
|
|
783
|
+
* Resolve a potentially-legacy event name to its canonical equivalent.
|
|
784
|
+
*
|
|
785
|
+
* If the event name matches a known legacy `on`-prefix name, it is
|
|
786
|
+
* remapped and a deprecation warning is logged. Unknown names pass through
|
|
787
|
+
* unchanged so callers using the new canonical names are unaffected.
|
|
788
|
+
*/
|
|
789
|
+
resolveEvent(event) {
|
|
790
|
+
const canonical = LEGACY_EVENT_MAP[event];
|
|
791
|
+
if (canonical) {
|
|
792
|
+
getLogger("hooks").warn(
|
|
793
|
+
{ legacyEvent: event, canonicalEvent: canonical },
|
|
794
|
+
`[DEPRECATED] Hook event '${event}' has been renamed to '${canonical}'. Update your handler registration.`
|
|
795
|
+
);
|
|
796
|
+
return canonical;
|
|
797
|
+
}
|
|
798
|
+
return event;
|
|
799
|
+
}
|
|
762
800
|
/**
|
|
763
801
|
* Register a hook handler for a specific event.
|
|
764
802
|
*
|
|
765
803
|
* Handlers are sorted by priority (highest first) and executed
|
|
766
804
|
* in parallel when the event is dispatched.
|
|
767
805
|
*
|
|
806
|
+
* Backward compatibility: legacy `on`-prefix event names are automatically
|
|
807
|
+
* remapped to their canonical equivalents.
|
|
808
|
+
*
|
|
768
809
|
* @param registration - The hook registration containing event, handler, priority, and ID
|
|
769
810
|
* @returns A function to unregister the handler
|
|
770
811
|
*
|
|
@@ -772,7 +813,7 @@ var init_registry = __esm({
|
|
|
772
813
|
* ```typescript
|
|
773
814
|
* const unregister = hooks.register({
|
|
774
815
|
* id: 'my-handler',
|
|
775
|
-
* event: '
|
|
816
|
+
* event: 'SessionStart',
|
|
776
817
|
* handler: async (root, payload) => { console.log('Session started'); },
|
|
777
818
|
* priority: 100
|
|
778
819
|
* });
|
|
@@ -781,12 +822,14 @@ var init_registry = __esm({
|
|
|
781
822
|
* ```
|
|
782
823
|
*/
|
|
783
824
|
register(registration) {
|
|
784
|
-
const
|
|
785
|
-
|
|
825
|
+
const resolvedEvent = this.resolveEvent(registration.event);
|
|
826
|
+
const resolvedRegistration = { ...registration, event: resolvedEvent };
|
|
827
|
+
const list = this.handlers.get(resolvedEvent) || [];
|
|
828
|
+
list.push(resolvedRegistration);
|
|
786
829
|
list.sort((a, b) => b.priority - a.priority);
|
|
787
|
-
this.handlers.set(
|
|
830
|
+
this.handlers.set(resolvedEvent, list);
|
|
788
831
|
return () => {
|
|
789
|
-
const handlers = this.handlers.get(
|
|
832
|
+
const handlers = this.handlers.get(resolvedEvent);
|
|
790
833
|
if (handlers) {
|
|
791
834
|
const idx = handlers.findIndex((h) => h.id === registration.id);
|
|
792
835
|
if (idx !== -1) handlers.splice(idx, 1);
|
|
@@ -800,14 +843,17 @@ var init_registry = __esm({
|
|
|
800
843
|
* execution. Errors in individual handlers are logged but do not block
|
|
801
844
|
* other handlers or propagate to the caller.
|
|
802
845
|
*
|
|
803
|
-
*
|
|
846
|
+
* Backward compatibility: legacy `on`-prefix event names are automatically
|
|
847
|
+
* remapped to their canonical equivalents.
|
|
848
|
+
*
|
|
849
|
+
* @param event - The CAAMP canonical hook event to dispatch
|
|
804
850
|
* @param projectRoot - The project root directory path
|
|
805
851
|
* @param payload - The event payload (typed by event)
|
|
806
852
|
* @returns Promise that resolves when all handlers have completed
|
|
807
853
|
*
|
|
808
854
|
* @example
|
|
809
855
|
* ```typescript
|
|
810
|
-
* await hooks.dispatch('
|
|
856
|
+
* await hooks.dispatch('SessionStart', '/project', {
|
|
811
857
|
* timestamp: new Date().toISOString(),
|
|
812
858
|
* sessionId: 'sess-123',
|
|
813
859
|
* name: 'My Session',
|
|
@@ -817,15 +863,19 @@ var init_registry = __esm({
|
|
|
817
863
|
*/
|
|
818
864
|
async dispatch(event, projectRoot, payload) {
|
|
819
865
|
if (!this.config.enabled) return;
|
|
820
|
-
|
|
821
|
-
|
|
866
|
+
const resolvedEvent = this.resolveEvent(event);
|
|
867
|
+
if (!this.config.events[resolvedEvent]) return;
|
|
868
|
+
const handlers = this.handlers.get(resolvedEvent);
|
|
822
869
|
if (!handlers || handlers.length === 0) return;
|
|
823
870
|
await Promise.allSettled(
|
|
824
871
|
handlers.map(async (reg) => {
|
|
825
872
|
try {
|
|
826
873
|
await reg.handler(projectRoot, payload);
|
|
827
874
|
} catch (error40) {
|
|
828
|
-
getLogger("hooks").warn(
|
|
875
|
+
getLogger("hooks").warn(
|
|
876
|
+
{ err: error40, hookId: reg.id, event: resolvedEvent },
|
|
877
|
+
"Hook handler failed"
|
|
878
|
+
);
|
|
829
879
|
}
|
|
830
880
|
})
|
|
831
881
|
);
|
|
@@ -834,12 +884,14 @@ var init_registry = __esm({
|
|
|
834
884
|
* Check if a specific event is currently enabled.
|
|
835
885
|
*
|
|
836
886
|
* Both the global enabled flag and the per-event flag must be true.
|
|
887
|
+
* Automatically resolves legacy `on`-prefix event names.
|
|
837
888
|
*
|
|
838
889
|
* @param event - The CAAMP hook event to check
|
|
839
890
|
* @returns True if the event is enabled
|
|
840
891
|
*/
|
|
841
892
|
isEnabled(event) {
|
|
842
|
-
|
|
893
|
+
const resolvedEvent = this.resolveEvent(event);
|
|
894
|
+
return this.config.enabled && this.config.events[resolvedEvent];
|
|
843
895
|
}
|
|
844
896
|
/**
|
|
845
897
|
* Update the hook system configuration.
|
|
@@ -851,7 +903,7 @@ var init_registry = __esm({
|
|
|
851
903
|
* @example
|
|
852
904
|
* ```typescript
|
|
853
905
|
* hooks.setConfig({ enabled: false }); // Disable all hooks
|
|
854
|
-
* hooks.setConfig({ events: {
|
|
906
|
+
* hooks.setConfig({ events: { PostToolUseFailure: false } }); // Disable specific event
|
|
855
907
|
* ```
|
|
856
908
|
*/
|
|
857
909
|
setConfig(config2) {
|
|
@@ -869,12 +921,14 @@ var init_registry = __esm({
|
|
|
869
921
|
* List all registered handlers for a specific event.
|
|
870
922
|
*
|
|
871
923
|
* Returns handlers in priority order (highest first).
|
|
924
|
+
* Automatically resolves legacy `on`-prefix event names.
|
|
872
925
|
*
|
|
873
926
|
* @param event - The CAAMP hook event
|
|
874
927
|
* @returns Array of hook registrations
|
|
875
928
|
*/
|
|
876
929
|
listHandlers(event) {
|
|
877
|
-
|
|
930
|
+
const resolvedEvent = this.resolveEvent(event);
|
|
931
|
+
return [...this.handlers.get(resolvedEvent) || []];
|
|
878
932
|
}
|
|
879
933
|
};
|
|
880
934
|
hooks = new HookRegistry();
|
|
@@ -13695,7 +13749,7 @@ async function saveJson(filePath, data, options) {
|
|
|
13695
13749
|
}
|
|
13696
13750
|
await atomicWriteJson(filePath, data, { indent: options?.indent });
|
|
13697
13751
|
Promise.resolve().then(() => (init_registry(), registry_exports)).then(
|
|
13698
|
-
({ hooks: h }) => h.dispatch("
|
|
13752
|
+
({ hooks: h }) => h.dispatch("Notification", process.cwd(), {
|
|
13699
13753
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
13700
13754
|
filePath,
|
|
13701
13755
|
changeType: "write"
|
|
@@ -20929,13 +20983,13 @@ var init_session_hooks = __esm({
|
|
|
20929
20983
|
init_memory_bridge_refresh();
|
|
20930
20984
|
hooks.register({
|
|
20931
20985
|
id: "brain-session-start",
|
|
20932
|
-
event: "
|
|
20986
|
+
event: "SessionStart",
|
|
20933
20987
|
handler: handleSessionStart,
|
|
20934
20988
|
priority: 100
|
|
20935
20989
|
});
|
|
20936
20990
|
hooks.register({
|
|
20937
20991
|
id: "brain-session-end",
|
|
20938
|
-
event: "
|
|
20992
|
+
event: "SessionEnd",
|
|
20939
20993
|
handler: handleSessionEnd,
|
|
20940
20994
|
priority: 100
|
|
20941
20995
|
});
|
|
@@ -20982,13 +21036,13 @@ var init_task_hooks = __esm({
|
|
|
20982
21036
|
init_memory_bridge_refresh();
|
|
20983
21037
|
hooks.register({
|
|
20984
21038
|
id: "brain-tool-start",
|
|
20985
|
-
event: "
|
|
21039
|
+
event: "PreToolUse",
|
|
20986
21040
|
handler: handleToolStart,
|
|
20987
21041
|
priority: 100
|
|
20988
21042
|
});
|
|
20989
21043
|
hooks.register({
|
|
20990
21044
|
id: "brain-tool-complete",
|
|
20991
|
-
event: "
|
|
21045
|
+
event: "PostToolUse",
|
|
20992
21046
|
handler: handleToolComplete,
|
|
20993
21047
|
priority: 100
|
|
20994
21048
|
});
|
|
@@ -21018,7 +21072,7 @@ var init_error_hooks = __esm({
|
|
|
21018
21072
|
init_registry();
|
|
21019
21073
|
hooks.register({
|
|
21020
21074
|
id: "brain-error",
|
|
21021
|
-
event: "
|
|
21075
|
+
event: "PostToolUseFailure",
|
|
21022
21076
|
handler: handleError,
|
|
21023
21077
|
priority: 100
|
|
21024
21078
|
});
|
|
@@ -21049,6 +21103,7 @@ async function isFileCaptureEnabled(projectRoot) {
|
|
|
21049
21103
|
}
|
|
21050
21104
|
}
|
|
21051
21105
|
async function handleFileChange(projectRoot, payload) {
|
|
21106
|
+
if (!payload.filePath || !payload.changeType) return;
|
|
21052
21107
|
if (!await isFileCaptureEnabled(projectRoot)) return;
|
|
21053
21108
|
const now2 = Date.now();
|
|
21054
21109
|
const lastWrite = recentWrites.get(payload.filePath);
|
|
@@ -21095,7 +21150,7 @@ var init_file_hooks = __esm({
|
|
|
21095
21150
|
];
|
|
21096
21151
|
hooks.register({
|
|
21097
21152
|
id: "brain-file-change",
|
|
21098
|
-
event: "
|
|
21153
|
+
event: "Notification",
|
|
21099
21154
|
handler: handleFileChange,
|
|
21100
21155
|
priority: 100
|
|
21101
21156
|
});
|
|
@@ -21149,22 +21204,51 @@ async function handleResponseComplete(projectRoot, payload) {
|
|
|
21149
21204
|
if (!isMissingBrainSchemaError4(err)) throw err;
|
|
21150
21205
|
}
|
|
21151
21206
|
}
|
|
21207
|
+
async function handleSystemNotification(projectRoot, payload) {
|
|
21208
|
+
if (payload.filePath || payload.changeType) return;
|
|
21209
|
+
if (!payload.message) return;
|
|
21210
|
+
try {
|
|
21211
|
+
const { loadConfig: loadConfig4 } = await Promise.resolve().then(() => (init_config(), config_exports));
|
|
21212
|
+
const config2 = await loadConfig4(projectRoot);
|
|
21213
|
+
if (!config2.brain?.autoCapture) return;
|
|
21214
|
+
} catch {
|
|
21215
|
+
return;
|
|
21216
|
+
}
|
|
21217
|
+
const { observeBrain: observeBrain2 } = await Promise.resolve().then(() => (init_brain_retrieval(), brain_retrieval_exports));
|
|
21218
|
+
try {
|
|
21219
|
+
await observeBrain2(projectRoot, {
|
|
21220
|
+
text: `System notification: ${payload.message}`,
|
|
21221
|
+
title: `Notification: ${payload.message.slice(0, 60)}`,
|
|
21222
|
+
type: "discovery",
|
|
21223
|
+
sourceSessionId: payload.sessionId,
|
|
21224
|
+
sourceType: "agent"
|
|
21225
|
+
});
|
|
21226
|
+
} catch (err) {
|
|
21227
|
+
if (!isMissingBrainSchemaError4(err)) throw err;
|
|
21228
|
+
}
|
|
21229
|
+
}
|
|
21152
21230
|
var init_mcp_hooks = __esm({
|
|
21153
21231
|
"packages/core/src/hooks/handlers/mcp-hooks.ts"() {
|
|
21154
21232
|
"use strict";
|
|
21155
21233
|
init_registry();
|
|
21156
21234
|
hooks.register({
|
|
21157
21235
|
id: "brain-prompt-submit",
|
|
21158
|
-
event: "
|
|
21236
|
+
event: "PromptSubmit",
|
|
21159
21237
|
handler: handlePromptSubmit,
|
|
21160
21238
|
priority: 100
|
|
21161
21239
|
});
|
|
21162
21240
|
hooks.register({
|
|
21163
21241
|
id: "brain-response-complete",
|
|
21164
|
-
event: "
|
|
21242
|
+
event: "ResponseComplete",
|
|
21165
21243
|
handler: handleResponseComplete,
|
|
21166
21244
|
priority: 100
|
|
21167
21245
|
});
|
|
21246
|
+
hooks.register({
|
|
21247
|
+
id: "brain-system-notification",
|
|
21248
|
+
event: "Notification",
|
|
21249
|
+
handler: handleSystemNotification,
|
|
21250
|
+
priority: 90
|
|
21251
|
+
});
|
|
21168
21252
|
}
|
|
21169
21253
|
});
|
|
21170
21254
|
|
|
@@ -21241,19 +21325,158 @@ var init_work_capture_hooks = __esm({
|
|
|
21241
21325
|
]);
|
|
21242
21326
|
hooks.register({
|
|
21243
21327
|
id: "work-capture-prompt-submit",
|
|
21244
|
-
event: "
|
|
21328
|
+
event: "PromptSubmit",
|
|
21245
21329
|
handler: handleWorkPromptSubmit,
|
|
21246
21330
|
priority: 90
|
|
21247
21331
|
});
|
|
21248
21332
|
hooks.register({
|
|
21249
21333
|
id: "work-capture-response-complete",
|
|
21250
|
-
event: "
|
|
21334
|
+
event: "ResponseComplete",
|
|
21251
21335
|
handler: handleWorkResponseComplete,
|
|
21252
21336
|
priority: 90
|
|
21253
21337
|
});
|
|
21254
21338
|
}
|
|
21255
21339
|
});
|
|
21256
21340
|
|
|
21341
|
+
// packages/core/src/hooks/handlers/agent-hooks.ts
|
|
21342
|
+
function isMissingBrainSchemaError6(err) {
|
|
21343
|
+
if (!(err instanceof Error)) return false;
|
|
21344
|
+
const message = String(err.message || "").toLowerCase();
|
|
21345
|
+
return message.includes("no such table") && message.includes("brain_");
|
|
21346
|
+
}
|
|
21347
|
+
async function isAutoCaptureEnabled(projectRoot) {
|
|
21348
|
+
try {
|
|
21349
|
+
const { loadConfig: loadConfig4 } = await Promise.resolve().then(() => (init_config(), config_exports));
|
|
21350
|
+
const config2 = await loadConfig4(projectRoot);
|
|
21351
|
+
return config2.brain?.autoCapture ?? false;
|
|
21352
|
+
} catch {
|
|
21353
|
+
return false;
|
|
21354
|
+
}
|
|
21355
|
+
}
|
|
21356
|
+
async function handleSubagentStart(projectRoot, payload) {
|
|
21357
|
+
if (!await isAutoCaptureEnabled(projectRoot)) return;
|
|
21358
|
+
const { observeBrain: observeBrain2 } = await Promise.resolve().then(() => (init_brain_retrieval(), brain_retrieval_exports));
|
|
21359
|
+
const rolePart = payload.role ? ` role=${payload.role}` : "";
|
|
21360
|
+
const taskPart = payload.taskId ? ` task=${payload.taskId}` : "";
|
|
21361
|
+
try {
|
|
21362
|
+
await observeBrain2(projectRoot, {
|
|
21363
|
+
text: `Subagent spawned: ${payload.agentId}${rolePart}${taskPart}`,
|
|
21364
|
+
title: `Subagent start: ${payload.agentId}`,
|
|
21365
|
+
type: "discovery",
|
|
21366
|
+
sourceSessionId: payload.sessionId,
|
|
21367
|
+
sourceType: "agent"
|
|
21368
|
+
});
|
|
21369
|
+
} catch (err) {
|
|
21370
|
+
if (!isMissingBrainSchemaError6(err)) throw err;
|
|
21371
|
+
}
|
|
21372
|
+
}
|
|
21373
|
+
async function handleSubagentStop(projectRoot, payload) {
|
|
21374
|
+
if (!await isAutoCaptureEnabled(projectRoot)) return;
|
|
21375
|
+
const { observeBrain: observeBrain2 } = await Promise.resolve().then(() => (init_brain_retrieval(), brain_retrieval_exports));
|
|
21376
|
+
const statusPart = payload.status ? ` status=${payload.status}` : "";
|
|
21377
|
+
const taskPart = payload.taskId ? ` task=${payload.taskId}` : "";
|
|
21378
|
+
const summaryPart = payload.summary ? `
|
|
21379
|
+
Summary: ${payload.summary}` : "";
|
|
21380
|
+
try {
|
|
21381
|
+
await observeBrain2(projectRoot, {
|
|
21382
|
+
text: `Subagent completed: ${payload.agentId}${statusPart}${taskPart}${summaryPart}`,
|
|
21383
|
+
title: `Subagent stop: ${payload.agentId}`,
|
|
21384
|
+
type: "change",
|
|
21385
|
+
sourceSessionId: payload.sessionId,
|
|
21386
|
+
sourceType: "agent"
|
|
21387
|
+
});
|
|
21388
|
+
} catch (err) {
|
|
21389
|
+
if (!isMissingBrainSchemaError6(err)) throw err;
|
|
21390
|
+
}
|
|
21391
|
+
}
|
|
21392
|
+
var init_agent_hooks = __esm({
|
|
21393
|
+
"packages/core/src/hooks/handlers/agent-hooks.ts"() {
|
|
21394
|
+
"use strict";
|
|
21395
|
+
init_registry();
|
|
21396
|
+
hooks.register({
|
|
21397
|
+
id: "brain-subagent-start",
|
|
21398
|
+
event: "SubagentStart",
|
|
21399
|
+
handler: handleSubagentStart,
|
|
21400
|
+
priority: 100
|
|
21401
|
+
});
|
|
21402
|
+
hooks.register({
|
|
21403
|
+
id: "brain-subagent-stop",
|
|
21404
|
+
event: "SubagentStop",
|
|
21405
|
+
handler: handleSubagentStop,
|
|
21406
|
+
priority: 100
|
|
21407
|
+
});
|
|
21408
|
+
}
|
|
21409
|
+
});
|
|
21410
|
+
|
|
21411
|
+
// packages/core/src/hooks/handlers/context-hooks.ts
|
|
21412
|
+
function isMissingBrainSchemaError7(err) {
|
|
21413
|
+
if (!(err instanceof Error)) return false;
|
|
21414
|
+
const message = String(err.message || "").toLowerCase();
|
|
21415
|
+
return message.includes("no such table") && message.includes("brain_");
|
|
21416
|
+
}
|
|
21417
|
+
async function isAutoCaptureEnabled2(projectRoot) {
|
|
21418
|
+
try {
|
|
21419
|
+
const { loadConfig: loadConfig4 } = await Promise.resolve().then(() => (init_config(), config_exports));
|
|
21420
|
+
const config2 = await loadConfig4(projectRoot);
|
|
21421
|
+
return config2.brain?.autoCapture ?? false;
|
|
21422
|
+
} catch {
|
|
21423
|
+
return false;
|
|
21424
|
+
}
|
|
21425
|
+
}
|
|
21426
|
+
async function handlePreCompact(projectRoot, payload) {
|
|
21427
|
+
if (!await isAutoCaptureEnabled2(projectRoot)) return;
|
|
21428
|
+
const { observeBrain: observeBrain2 } = await Promise.resolve().then(() => (init_brain_retrieval(), brain_retrieval_exports));
|
|
21429
|
+
const tokensPart = payload.tokensBefore != null ? ` (~${payload.tokensBefore.toLocaleString()} tokens)` : "";
|
|
21430
|
+
const reasonPart = payload.reason ? ` Reason: ${payload.reason}` : "";
|
|
21431
|
+
try {
|
|
21432
|
+
await observeBrain2(projectRoot, {
|
|
21433
|
+
text: `Context compaction about to begin${tokensPart}.${reasonPart}`,
|
|
21434
|
+
title: "Pre-compaction context snapshot",
|
|
21435
|
+
type: "discovery",
|
|
21436
|
+
sourceSessionId: payload.sessionId,
|
|
21437
|
+
sourceType: "agent"
|
|
21438
|
+
});
|
|
21439
|
+
} catch (err) {
|
|
21440
|
+
if (!isMissingBrainSchemaError7(err)) throw err;
|
|
21441
|
+
}
|
|
21442
|
+
}
|
|
21443
|
+
async function handlePostCompact(projectRoot, payload) {
|
|
21444
|
+
if (!await isAutoCaptureEnabled2(projectRoot)) return;
|
|
21445
|
+
const { observeBrain: observeBrain2 } = await Promise.resolve().then(() => (init_brain_retrieval(), brain_retrieval_exports));
|
|
21446
|
+
const statusPart = payload.success ? "succeeded" : "failed";
|
|
21447
|
+
const beforePart = payload.tokensBefore != null ? ` before=${payload.tokensBefore.toLocaleString()}` : "";
|
|
21448
|
+
const afterPart = payload.tokensAfter != null ? ` after=${payload.tokensAfter.toLocaleString()}` : "";
|
|
21449
|
+
try {
|
|
21450
|
+
await observeBrain2(projectRoot, {
|
|
21451
|
+
text: `Context compaction ${statusPart}${beforePart}${afterPart}`,
|
|
21452
|
+
title: "Post-compaction record",
|
|
21453
|
+
type: "change",
|
|
21454
|
+
sourceSessionId: payload.sessionId,
|
|
21455
|
+
sourceType: "agent"
|
|
21456
|
+
});
|
|
21457
|
+
} catch (err) {
|
|
21458
|
+
if (!isMissingBrainSchemaError7(err)) throw err;
|
|
21459
|
+
}
|
|
21460
|
+
}
|
|
21461
|
+
var init_context_hooks = __esm({
|
|
21462
|
+
"packages/core/src/hooks/handlers/context-hooks.ts"() {
|
|
21463
|
+
"use strict";
|
|
21464
|
+
init_registry();
|
|
21465
|
+
hooks.register({
|
|
21466
|
+
id: "brain-pre-compact",
|
|
21467
|
+
event: "PreCompact",
|
|
21468
|
+
handler: handlePreCompact,
|
|
21469
|
+
priority: 100
|
|
21470
|
+
});
|
|
21471
|
+
hooks.register({
|
|
21472
|
+
id: "brain-post-compact",
|
|
21473
|
+
event: "PostCompact",
|
|
21474
|
+
handler: handlePostCompact,
|
|
21475
|
+
priority: 100
|
|
21476
|
+
});
|
|
21477
|
+
}
|
|
21478
|
+
});
|
|
21479
|
+
|
|
21257
21480
|
// packages/core/src/hooks/handlers/index.ts
|
|
21258
21481
|
var init_handlers = __esm({
|
|
21259
21482
|
"packages/core/src/hooks/handlers/index.ts"() {
|
|
@@ -21264,6 +21487,10 @@ var init_handlers = __esm({
|
|
|
21264
21487
|
init_file_hooks();
|
|
21265
21488
|
init_mcp_hooks();
|
|
21266
21489
|
init_work_capture_hooks();
|
|
21490
|
+
init_agent_hooks();
|
|
21491
|
+
init_context_hooks();
|
|
21492
|
+
init_agent_hooks();
|
|
21493
|
+
init_context_hooks();
|
|
21267
21494
|
init_error_hooks();
|
|
21268
21495
|
init_file_hooks();
|
|
21269
21496
|
init_mcp_hooks();
|
|
@@ -23114,7 +23341,7 @@ async function startSession(options, cwd, accessor) {
|
|
|
23114
23341
|
});
|
|
23115
23342
|
}
|
|
23116
23343
|
const { hooks: hooks2 } = await Promise.resolve().then(() => (init_registry(), registry_exports));
|
|
23117
|
-
hooks2.dispatch("
|
|
23344
|
+
hooks2.dispatch("SessionStart", cwd ?? process.cwd(), {
|
|
23118
23345
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
23119
23346
|
sessionId: session.id,
|
|
23120
23347
|
name: options.name,
|
|
@@ -23152,7 +23379,7 @@ async function endSession(options = {}, cwd, accessor) {
|
|
|
23152
23379
|
session.endedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
23153
23380
|
const duration3 = Math.floor((Date.now() - new Date(session.startedAt).getTime()) / 1e3);
|
|
23154
23381
|
const { hooks: hooks2 } = await Promise.resolve().then(() => (init_registry(), registry_exports));
|
|
23155
|
-
hooks2.dispatch("
|
|
23382
|
+
hooks2.dispatch("SessionEnd", cwd ?? process.cwd(), {
|
|
23156
23383
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
23157
23384
|
sessionId: session.id,
|
|
23158
23385
|
duration: duration3,
|
|
@@ -35888,7 +36115,7 @@ function validatePayload(event, payload) {
|
|
|
35888
36115
|
const errors = result.error.issues.map((issue2) => `${issue2.path.join(".")}: ${issue2.message}`);
|
|
35889
36116
|
return { valid: false, errors };
|
|
35890
36117
|
}
|
|
35891
|
-
var HookPayloadSchema, OnSessionStartPayloadSchema, OnSessionEndPayloadSchema, OnToolStartPayloadSchema, OnToolCompletePayloadSchema, OnFileChangePayloadSchema, OnErrorPayloadSchema, OnPromptSubmitPayloadSchema, OnResponseCompletePayloadSchema, OnWorkAvailablePayloadSchema, OnAgentSpawnPayloadSchema, OnAgentCompletePayloadSchema, OnCascadeStartPayloadSchema, OnPatrolPayloadSchema, EVENT_SCHEMA_MAP;
|
|
36118
|
+
var HookPayloadSchema, SessionStartPayloadSchema, OnSessionStartPayloadSchema, SessionEndPayloadSchema, OnSessionEndPayloadSchema, PreToolUsePayloadSchema, OnToolStartPayloadSchema, PostToolUsePayloadSchema, OnToolCompletePayloadSchema, NotificationPayloadSchema, OnFileChangePayloadSchema, PostToolUseFailurePayloadSchema, OnErrorPayloadSchema, PromptSubmitPayloadSchema, OnPromptSubmitPayloadSchema, ResponseCompletePayloadSchema, OnResponseCompletePayloadSchema, SubagentStartPayloadSchema, SubagentStopPayloadSchema, PreCompactPayloadSchema, PostCompactPayloadSchema, ConfigChangePayloadSchema, OnWorkAvailablePayloadSchema, OnAgentSpawnPayloadSchema, OnAgentCompletePayloadSchema, OnCascadeStartPayloadSchema, OnPatrolPayloadSchema, EVENT_SCHEMA_MAP;
|
|
35892
36119
|
var init_payload_schemas = __esm({
|
|
35893
36120
|
"packages/core/src/hooks/payload-schemas.ts"() {
|
|
35894
36121
|
"use strict";
|
|
@@ -35900,33 +36127,42 @@ var init_payload_schemas = __esm({
|
|
|
35900
36127
|
providerId: external_exports.string().optional(),
|
|
35901
36128
|
metadata: external_exports.record(external_exports.string(), external_exports.unknown()).optional()
|
|
35902
36129
|
});
|
|
35903
|
-
|
|
36130
|
+
SessionStartPayloadSchema = HookPayloadSchema.extend({
|
|
35904
36131
|
sessionId: external_exports.string(),
|
|
35905
36132
|
name: external_exports.string(),
|
|
35906
36133
|
scope: external_exports.string(),
|
|
35907
36134
|
agent: external_exports.string().optional()
|
|
35908
36135
|
});
|
|
35909
|
-
|
|
36136
|
+
OnSessionStartPayloadSchema = SessionStartPayloadSchema;
|
|
36137
|
+
SessionEndPayloadSchema = HookPayloadSchema.extend({
|
|
35910
36138
|
sessionId: external_exports.string(),
|
|
35911
36139
|
duration: external_exports.number(),
|
|
35912
36140
|
tasksCompleted: external_exports.array(external_exports.string())
|
|
35913
36141
|
});
|
|
35914
|
-
|
|
36142
|
+
OnSessionEndPayloadSchema = SessionEndPayloadSchema;
|
|
36143
|
+
PreToolUsePayloadSchema = HookPayloadSchema.extend({
|
|
35915
36144
|
taskId: external_exports.string(),
|
|
35916
36145
|
taskTitle: external_exports.string(),
|
|
35917
|
-
previousTask: external_exports.string().optional()
|
|
36146
|
+
previousTask: external_exports.string().optional(),
|
|
36147
|
+
toolName: external_exports.string().optional(),
|
|
36148
|
+
toolInput: external_exports.record(external_exports.string(), external_exports.unknown()).optional()
|
|
35918
36149
|
});
|
|
35919
|
-
|
|
36150
|
+
OnToolStartPayloadSchema = PreToolUsePayloadSchema;
|
|
36151
|
+
PostToolUsePayloadSchema = HookPayloadSchema.extend({
|
|
35920
36152
|
taskId: external_exports.string(),
|
|
35921
36153
|
taskTitle: external_exports.string(),
|
|
35922
|
-
status: external_exports.enum(["done", "archived", "cancelled"])
|
|
36154
|
+
status: external_exports.enum(["done", "archived", "cancelled"]),
|
|
36155
|
+
toolResult: external_exports.record(external_exports.string(), external_exports.unknown()).optional()
|
|
35923
36156
|
});
|
|
35924
|
-
|
|
35925
|
-
|
|
35926
|
-
|
|
35927
|
-
|
|
36157
|
+
OnToolCompletePayloadSchema = PostToolUsePayloadSchema;
|
|
36158
|
+
NotificationPayloadSchema = HookPayloadSchema.extend({
|
|
36159
|
+
filePath: external_exports.string().optional(),
|
|
36160
|
+
changeType: external_exports.enum(["write", "create", "delete"]).optional(),
|
|
36161
|
+
sizeBytes: external_exports.number().optional(),
|
|
36162
|
+
message: external_exports.string().optional()
|
|
35928
36163
|
});
|
|
35929
|
-
|
|
36164
|
+
OnFileChangePayloadSchema = NotificationPayloadSchema;
|
|
36165
|
+
PostToolUseFailurePayloadSchema = HookPayloadSchema.extend({
|
|
35930
36166
|
errorCode: external_exports.union([external_exports.number(), external_exports.string()]),
|
|
35931
36167
|
message: external_exports.string(),
|
|
35932
36168
|
domain: external_exports.string().optional(),
|
|
@@ -35934,13 +36170,15 @@ var init_payload_schemas = __esm({
|
|
|
35934
36170
|
gateway: external_exports.string().optional(),
|
|
35935
36171
|
stack: external_exports.string().optional()
|
|
35936
36172
|
});
|
|
35937
|
-
|
|
36173
|
+
OnErrorPayloadSchema = PostToolUseFailurePayloadSchema;
|
|
36174
|
+
PromptSubmitPayloadSchema = HookPayloadSchema.extend({
|
|
35938
36175
|
gateway: external_exports.string(),
|
|
35939
36176
|
domain: external_exports.string(),
|
|
35940
36177
|
operation: external_exports.string(),
|
|
35941
36178
|
source: external_exports.string().optional()
|
|
35942
36179
|
});
|
|
35943
|
-
|
|
36180
|
+
OnPromptSubmitPayloadSchema = PromptSubmitPayloadSchema;
|
|
36181
|
+
ResponseCompletePayloadSchema = HookPayloadSchema.extend({
|
|
35944
36182
|
gateway: external_exports.string(),
|
|
35945
36183
|
domain: external_exports.string(),
|
|
35946
36184
|
operation: external_exports.string(),
|
|
@@ -35948,6 +36186,32 @@ var init_payload_schemas = __esm({
|
|
|
35948
36186
|
durationMs: external_exports.number().optional(),
|
|
35949
36187
|
errorCode: external_exports.string().optional()
|
|
35950
36188
|
});
|
|
36189
|
+
OnResponseCompletePayloadSchema = ResponseCompletePayloadSchema;
|
|
36190
|
+
SubagentStartPayloadSchema = HookPayloadSchema.extend({
|
|
36191
|
+
agentId: external_exports.string(),
|
|
36192
|
+
role: external_exports.string().optional(),
|
|
36193
|
+
taskId: external_exports.string().optional()
|
|
36194
|
+
});
|
|
36195
|
+
SubagentStopPayloadSchema = HookPayloadSchema.extend({
|
|
36196
|
+
agentId: external_exports.string(),
|
|
36197
|
+
status: external_exports.enum(["complete", "partial", "blocked", "failed"]).optional(),
|
|
36198
|
+
taskId: external_exports.string().optional(),
|
|
36199
|
+
summary: external_exports.string().optional()
|
|
36200
|
+
});
|
|
36201
|
+
PreCompactPayloadSchema = HookPayloadSchema.extend({
|
|
36202
|
+
tokensBefore: external_exports.number().optional(),
|
|
36203
|
+
reason: external_exports.string().optional()
|
|
36204
|
+
});
|
|
36205
|
+
PostCompactPayloadSchema = HookPayloadSchema.extend({
|
|
36206
|
+
tokensBefore: external_exports.number().optional(),
|
|
36207
|
+
tokensAfter: external_exports.number().optional(),
|
|
36208
|
+
success: external_exports.boolean()
|
|
36209
|
+
});
|
|
36210
|
+
ConfigChangePayloadSchema = HookPayloadSchema.extend({
|
|
36211
|
+
key: external_exports.string(),
|
|
36212
|
+
previousValue: external_exports.unknown().optional(),
|
|
36213
|
+
newValue: external_exports.unknown().optional()
|
|
36214
|
+
});
|
|
35951
36215
|
OnWorkAvailablePayloadSchema = HookPayloadSchema.extend({
|
|
35952
36216
|
taskIds: external_exports.array(external_exports.string()),
|
|
35953
36217
|
epicId: external_exports.string().optional(),
|
|
@@ -35979,14 +36243,21 @@ var init_payload_schemas = __esm({
|
|
|
35979
36243
|
scope: external_exports.string().optional()
|
|
35980
36244
|
});
|
|
35981
36245
|
EVENT_SCHEMA_MAP = {
|
|
35982
|
-
|
|
35983
|
-
|
|
35984
|
-
|
|
35985
|
-
|
|
35986
|
-
|
|
35987
|
-
|
|
35988
|
-
|
|
35989
|
-
|
|
36246
|
+
// CAAMP canonical events (16)
|
|
36247
|
+
SessionStart: SessionStartPayloadSchema,
|
|
36248
|
+
SessionEnd: SessionEndPayloadSchema,
|
|
36249
|
+
PreToolUse: PreToolUsePayloadSchema,
|
|
36250
|
+
PostToolUse: PostToolUsePayloadSchema,
|
|
36251
|
+
Notification: NotificationPayloadSchema,
|
|
36252
|
+
PostToolUseFailure: PostToolUseFailurePayloadSchema,
|
|
36253
|
+
PromptSubmit: PromptSubmitPayloadSchema,
|
|
36254
|
+
ResponseComplete: ResponseCompletePayloadSchema,
|
|
36255
|
+
SubagentStart: SubagentStartPayloadSchema,
|
|
36256
|
+
SubagentStop: SubagentStopPayloadSchema,
|
|
36257
|
+
PreCompact: PreCompactPayloadSchema,
|
|
36258
|
+
PostCompact: PostCompactPayloadSchema,
|
|
36259
|
+
ConfigChange: ConfigChangePayloadSchema,
|
|
36260
|
+
// CLEO internal coordination events (5)
|
|
35990
36261
|
onWorkAvailable: OnWorkAvailablePayloadSchema,
|
|
35991
36262
|
onAgentSpawn: OnAgentSpawnPayloadSchema,
|
|
35992
36263
|
onAgentComplete: OnAgentCompletePayloadSchema,
|
|
@@ -36016,10 +36287,15 @@ __export(hooks_exports, {
|
|
|
36016
36287
|
OnWorkAvailablePayloadSchema: () => OnWorkAvailablePayloadSchema,
|
|
36017
36288
|
handleError: () => handleError,
|
|
36018
36289
|
handleFileChange: () => handleFileChange,
|
|
36290
|
+
handlePostCompact: () => handlePostCompact,
|
|
36291
|
+
handlePreCompact: () => handlePreCompact,
|
|
36019
36292
|
handlePromptSubmit: () => handlePromptSubmit,
|
|
36020
36293
|
handleResponseComplete: () => handleResponseComplete,
|
|
36021
36294
|
handleSessionEnd: () => handleSessionEnd,
|
|
36022
36295
|
handleSessionStart: () => handleSessionStart,
|
|
36296
|
+
handleSubagentStart: () => handleSubagentStart,
|
|
36297
|
+
handleSubagentStop: () => handleSubagentStop,
|
|
36298
|
+
handleSystemNotification: () => handleSystemNotification,
|
|
36023
36299
|
handleToolComplete: () => handleToolComplete,
|
|
36024
36300
|
handleToolStart: () => handleToolStart,
|
|
36025
36301
|
handleWorkPromptSubmit: () => handleWorkPromptSubmit,
|
|
@@ -62451,7 +62727,7 @@ async function startTask(taskId, cwd, accessor) {
|
|
|
62451
62727
|
accessor
|
|
62452
62728
|
);
|
|
62453
62729
|
const { hooks: hooks2 } = await Promise.resolve().then(() => (init_registry(), registry_exports));
|
|
62454
|
-
hooks2.dispatch("
|
|
62730
|
+
hooks2.dispatch("PreToolUse", cwd ?? process.cwd(), {
|
|
62455
62731
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
62456
62732
|
taskId,
|
|
62457
62733
|
taskTitle: task.title
|
|
@@ -62477,7 +62753,7 @@ async function stopTask(cwd, accessor) {
|
|
|
62477
62753
|
const now2 = (/* @__PURE__ */ new Date()).toISOString();
|
|
62478
62754
|
if (taskId && task) {
|
|
62479
62755
|
const { hooks: hooks2 } = await Promise.resolve().then(() => (init_registry(), registry_exports));
|
|
62480
|
-
hooks2.dispatch("
|
|
62756
|
+
hooks2.dispatch("PostToolUse", cwd ?? process.cwd(), {
|
|
62481
62757
|
timestamp: now2,
|
|
62482
62758
|
taskId,
|
|
62483
62759
|
taskTitle: task.title,
|
|
@@ -67855,6 +68131,162 @@ var init_bootstrap = __esm({
|
|
|
67855
68131
|
}
|
|
67856
68132
|
});
|
|
67857
68133
|
|
|
68134
|
+
// packages/core/src/sessions/snapshot.ts
|
|
68135
|
+
async function serializeSession(projectRoot, options = {}, accessor) {
|
|
68136
|
+
const acc = accessor ?? await getAccessor(projectRoot);
|
|
68137
|
+
const maxObs = options.maxObservations ?? 10;
|
|
68138
|
+
const maxDescLen = options.maxDescriptionLength ?? 500;
|
|
68139
|
+
const sessions2 = await acc.loadSessions();
|
|
68140
|
+
let session;
|
|
68141
|
+
if (options.sessionId) {
|
|
68142
|
+
session = sessions2.find((s) => s.id === options.sessionId);
|
|
68143
|
+
} else {
|
|
68144
|
+
session = sessions2.filter((s) => s.status === "active").sort((a, b) => new Date(b.startedAt).getTime() - new Date(a.startedAt).getTime())[0];
|
|
68145
|
+
}
|
|
68146
|
+
if (!session) {
|
|
68147
|
+
throw new CleoError(
|
|
68148
|
+
31 /* SESSION_NOT_FOUND */,
|
|
68149
|
+
options.sessionId ? `Session '${options.sessionId}' not found` : "No active session to serialize",
|
|
68150
|
+
{ fix: "Use 'cleo session list' to see available sessions" }
|
|
68151
|
+
);
|
|
68152
|
+
}
|
|
68153
|
+
const handoff = await computeHandoff(projectRoot, { sessionId: session.id });
|
|
68154
|
+
const decisionLog = await getDecisionLog(projectRoot, { sessionId: session.id });
|
|
68155
|
+
const decisions = decisionLog.map((d) => ({
|
|
68156
|
+
decision: d.decision,
|
|
68157
|
+
rationale: d.rationale,
|
|
68158
|
+
taskId: d.taskId,
|
|
68159
|
+
recordedAt: d.timestamp ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
68160
|
+
}));
|
|
68161
|
+
let observations = [];
|
|
68162
|
+
try {
|
|
68163
|
+
const { searchBrainCompact: searchBrainCompact2 } = await Promise.resolve().then(() => (init_brain_retrieval(), brain_retrieval_exports));
|
|
68164
|
+
const results = await searchBrainCompact2(projectRoot, {
|
|
68165
|
+
query: session.id,
|
|
68166
|
+
limit: maxObs,
|
|
68167
|
+
tables: ["observations"]
|
|
68168
|
+
});
|
|
68169
|
+
if (Array.isArray(results)) {
|
|
68170
|
+
observations = results.map(
|
|
68171
|
+
(r) => ({
|
|
68172
|
+
id: r.id,
|
|
68173
|
+
text: r.text ?? "",
|
|
68174
|
+
type: r.type ?? "discovery",
|
|
68175
|
+
createdAt: r.createdAt ?? ""
|
|
68176
|
+
})
|
|
68177
|
+
);
|
|
68178
|
+
}
|
|
68179
|
+
} catch {
|
|
68180
|
+
}
|
|
68181
|
+
let activeTask = null;
|
|
68182
|
+
if (session.taskWork?.taskId) {
|
|
68183
|
+
try {
|
|
68184
|
+
const { tasks: tasks2 } = await acc.queryTasks({});
|
|
68185
|
+
const task = tasks2.find((t) => t.id === session.taskWork?.taskId);
|
|
68186
|
+
if (task) {
|
|
68187
|
+
const desc7 = task.description ?? "";
|
|
68188
|
+
activeTask = {
|
|
68189
|
+
taskId: task.id,
|
|
68190
|
+
title: task.title,
|
|
68191
|
+
status: task.status,
|
|
68192
|
+
priority: task.priority ?? "medium",
|
|
68193
|
+
description: desc7.length > maxDescLen ? desc7.slice(0, maxDescLen) + "..." : desc7,
|
|
68194
|
+
acceptance: Array.isArray(task.acceptance) ? task.acceptance.join("\n") : task.acceptance ?? void 0
|
|
68195
|
+
};
|
|
68196
|
+
}
|
|
68197
|
+
} catch {
|
|
68198
|
+
}
|
|
68199
|
+
}
|
|
68200
|
+
const startTime = new Date(session.startedAt).getTime();
|
|
68201
|
+
const now2 = Date.now();
|
|
68202
|
+
const durationMinutes = Math.round((now2 - startTime) / 6e4);
|
|
68203
|
+
return {
|
|
68204
|
+
version: SNAPSHOT_VERSION,
|
|
68205
|
+
capturedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
68206
|
+
session,
|
|
68207
|
+
handoff,
|
|
68208
|
+
decisions,
|
|
68209
|
+
observations,
|
|
68210
|
+
activeTask,
|
|
68211
|
+
durationMinutes
|
|
68212
|
+
};
|
|
68213
|
+
}
|
|
68214
|
+
async function restoreSession(projectRoot, snapshot, options = {}, accessor) {
|
|
68215
|
+
if (snapshot.version > SNAPSHOT_VERSION) {
|
|
68216
|
+
throw new CleoError(
|
|
68217
|
+
6 /* VALIDATION_ERROR */,
|
|
68218
|
+
`Snapshot version ${snapshot.version} is newer than supported version ${SNAPSHOT_VERSION}`,
|
|
68219
|
+
{ fix: "Upgrade @cleocode/core to a newer version that supports this snapshot format" }
|
|
68220
|
+
);
|
|
68221
|
+
}
|
|
68222
|
+
const acc = accessor ?? await getAccessor(projectRoot);
|
|
68223
|
+
const activate = options.activate ?? true;
|
|
68224
|
+
if (activate) {
|
|
68225
|
+
const sessions2 = await acc.loadSessions();
|
|
68226
|
+
const scope = snapshot.session.scope;
|
|
68227
|
+
const activeConflict = sessions2.find(
|
|
68228
|
+
(s) => s.status === "active" && s.scope.type === scope.type && s.scope.epicId === scope.epicId && s.id !== snapshot.session.id
|
|
68229
|
+
);
|
|
68230
|
+
if (activeConflict) {
|
|
68231
|
+
throw new CleoError(
|
|
68232
|
+
32 /* SCOPE_CONFLICT */,
|
|
68233
|
+
`Active session '${activeConflict.id}' already exists for scope ${scope.type}${scope.epicId ? ":" + scope.epicId : ""}`,
|
|
68234
|
+
{
|
|
68235
|
+
fix: `End the active session first with 'cleo session end' or restore without activating`,
|
|
68236
|
+
alternatives: [
|
|
68237
|
+
{ action: "End conflicting session", command: "cleo session end" },
|
|
68238
|
+
{ action: "Restore without activating", command: "Restore with activate: false" }
|
|
68239
|
+
]
|
|
68240
|
+
}
|
|
68241
|
+
);
|
|
68242
|
+
}
|
|
68243
|
+
}
|
|
68244
|
+
const restoredSession = {
|
|
68245
|
+
...snapshot.session,
|
|
68246
|
+
status: activate ? "active" : snapshot.session.status,
|
|
68247
|
+
notes: [
|
|
68248
|
+
...snapshot.session.notes ?? [],
|
|
68249
|
+
`Restored from snapshot at ${(/* @__PURE__ */ new Date()).toISOString()} (captured ${snapshot.capturedAt}, duration ${snapshot.durationMinutes}m)`
|
|
68250
|
+
],
|
|
68251
|
+
resumeCount: (snapshot.session.resumeCount ?? 0) + 1
|
|
68252
|
+
};
|
|
68253
|
+
if (options.agent) {
|
|
68254
|
+
restoredSession.agent = options.agent;
|
|
68255
|
+
restoredSession.notes = [
|
|
68256
|
+
...restoredSession.notes ?? [],
|
|
68257
|
+
`Agent handoff: ${snapshot.session.agent ?? "unknown"} \u2192 ${options.agent}`
|
|
68258
|
+
];
|
|
68259
|
+
}
|
|
68260
|
+
restoredSession.handoffJson = JSON.stringify(snapshot.handoff);
|
|
68261
|
+
await acc.upsertSingleSession(restoredSession);
|
|
68262
|
+
try {
|
|
68263
|
+
const { hooks: hooks2 } = await Promise.resolve().then(() => (init_registry(), registry_exports));
|
|
68264
|
+
await hooks2.dispatch("SessionStart", projectRoot, {
|
|
68265
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
68266
|
+
sessionId: restoredSession.id,
|
|
68267
|
+
name: restoredSession.name,
|
|
68268
|
+
scope: restoredSession.scope,
|
|
68269
|
+
agent: restoredSession.agent,
|
|
68270
|
+
restored: true,
|
|
68271
|
+
snapshotCapturedAt: snapshot.capturedAt
|
|
68272
|
+
});
|
|
68273
|
+
} catch {
|
|
68274
|
+
}
|
|
68275
|
+
return restoredSession;
|
|
68276
|
+
}
|
|
68277
|
+
var SNAPSHOT_VERSION;
|
|
68278
|
+
var init_snapshot2 = __esm({
|
|
68279
|
+
"packages/core/src/sessions/snapshot.ts"() {
|
|
68280
|
+
"use strict";
|
|
68281
|
+
init_src();
|
|
68282
|
+
init_errors3();
|
|
68283
|
+
init_data_accessor();
|
|
68284
|
+
init_decisions();
|
|
68285
|
+
init_handoff();
|
|
68286
|
+
SNAPSHOT_VERSION = 1;
|
|
68287
|
+
}
|
|
68288
|
+
});
|
|
68289
|
+
|
|
67858
68290
|
// packages/core/src/cleo.ts
|
|
67859
68291
|
import path from "node:path";
|
|
67860
68292
|
var Cleo;
|
|
@@ -67877,6 +68309,7 @@ var init_cleo = __esm({
|
|
|
67877
68309
|
init_link_store();
|
|
67878
68310
|
init_release2();
|
|
67879
68311
|
init_sessions();
|
|
68312
|
+
init_snapshot2();
|
|
67880
68313
|
init_sticky();
|
|
67881
68314
|
init_data_accessor();
|
|
67882
68315
|
init_task_work();
|
|
@@ -67986,7 +68419,15 @@ var init_cleo = __esm({
|
|
|
67986
68419
|
recordAssumption: (p) => recordAssumption(root, p),
|
|
67987
68420
|
contextDrift: (p) => getContextDrift(root, p),
|
|
67988
68421
|
decisionLog: (p) => getDecisionLog(root, { sessionId: p?.sessionId, taskId: p?.taskId }),
|
|
67989
|
-
lastHandoff: (scope) => getLastHandoff(root, scope)
|
|
68422
|
+
lastHandoff: (scope) => getLastHandoff(root, scope),
|
|
68423
|
+
serialize: (p) => serializeSession(root, {
|
|
68424
|
+
sessionId: p?.sessionId,
|
|
68425
|
+
maxObservations: p?.maxObservations
|
|
68426
|
+
}),
|
|
68427
|
+
restore: (snapshot, p) => restoreSession(root, snapshot, {
|
|
68428
|
+
agent: p?.agent,
|
|
68429
|
+
activate: p?.activate
|
|
68430
|
+
})
|
|
67990
68431
|
};
|
|
67991
68432
|
}
|
|
67992
68433
|
// === Memory ===
|
|
@@ -68235,6 +68676,7 @@ var init_src2 = __esm({
|
|
|
68235
68676
|
init_migration();
|
|
68236
68677
|
init_reconciliation();
|
|
68237
68678
|
init_sessions();
|
|
68679
|
+
init_snapshot2();
|
|
68238
68680
|
init_migrate();
|
|
68239
68681
|
init_storage_preflight();
|
|
68240
68682
|
init_task_work();
|
|
@@ -69409,6 +69851,14 @@ var init_protocol_enforcement = __esm({
|
|
|
69409
69851
|
});
|
|
69410
69852
|
|
|
69411
69853
|
// packages/core/src/hooks/types.ts
|
|
69854
|
+
import {
|
|
69855
|
+
buildHookMatrix,
|
|
69856
|
+
CANONICAL_HOOK_EVENTS,
|
|
69857
|
+
HOOK_CATEGORIES,
|
|
69858
|
+
supportsHook,
|
|
69859
|
+
toCanonical,
|
|
69860
|
+
toNative
|
|
69861
|
+
} from "@cleocode/caamp";
|
|
69412
69862
|
import { getCommonHookEvents, getProvidersByHookEvent } from "@cleocode/caamp";
|
|
69413
69863
|
function isProviderHookEvent(event) {
|
|
69414
69864
|
return !INTERNAL_HOOK_EVENT_SET.has(event);
|
|
@@ -79483,6 +79933,7 @@ __export(internal_exports, {
|
|
|
79483
79933
|
resolveTask: () => resolveTask,
|
|
79484
79934
|
restoreBackup: () => restoreBackup,
|
|
79485
79935
|
restoreFromBackup: () => restoreFromBackup,
|
|
79936
|
+
restoreSession: () => restoreSession,
|
|
79486
79937
|
resumeSession: () => resumeSession,
|
|
79487
79938
|
roadmap: () => roadmap_exports,
|
|
79488
79939
|
rollbackRelease: () => rollbackRelease,
|
|
@@ -79510,6 +79961,7 @@ __export(internal_exports, {
|
|
|
79510
79961
|
selectSessionSchema: () => selectSessionSchema,
|
|
79511
79962
|
selectTaskSchema: () => selectTaskSchema,
|
|
79512
79963
|
sequence: () => sequence_exports,
|
|
79964
|
+
serializeSession: () => serializeSession,
|
|
79513
79965
|
sessionStatus: () => sessionStatus,
|
|
79514
79966
|
sessionStatusSchema: () => sessionStatusSchema,
|
|
79515
79967
|
sessions: () => sessions_exports,
|
|
@@ -82216,6 +82668,30 @@ var init_registry5 = __esm({
|
|
|
82216
82668
|
sessionRequired: false,
|
|
82217
82669
|
requiredParams: []
|
|
82218
82670
|
},
|
|
82671
|
+
{
|
|
82672
|
+
gateway: "query",
|
|
82673
|
+
domain: "admin",
|
|
82674
|
+
operation: "hooks.matrix",
|
|
82675
|
+
description: "admin.hooks.matrix (query) \u2014 cross-provider hook support matrix using CAAMP canonical taxonomy",
|
|
82676
|
+
tier: 1,
|
|
82677
|
+
idempotent: true,
|
|
82678
|
+
sessionRequired: false,
|
|
82679
|
+
requiredParams: [],
|
|
82680
|
+
params: [
|
|
82681
|
+
{
|
|
82682
|
+
name: "providerIds",
|
|
82683
|
+
type: "string",
|
|
82684
|
+
required: false,
|
|
82685
|
+
description: "Limit matrix to specific provider IDs (default: all mapped providers)"
|
|
82686
|
+
},
|
|
82687
|
+
{
|
|
82688
|
+
name: "detectProvider",
|
|
82689
|
+
type: "boolean",
|
|
82690
|
+
required: false,
|
|
82691
|
+
description: "Detect current runtime provider (default: true)"
|
|
82692
|
+
}
|
|
82693
|
+
]
|
|
82694
|
+
},
|
|
82219
82695
|
{
|
|
82220
82696
|
gateway: "query",
|
|
82221
82697
|
domain: "admin",
|
|
@@ -83040,6 +83516,114 @@ var init_config_engine = __esm({
|
|
|
83040
83516
|
}
|
|
83041
83517
|
});
|
|
83042
83518
|
|
|
83519
|
+
// packages/cleo/src/dispatch/engines/hooks-engine.ts
|
|
83520
|
+
var hooks_engine_exports = {};
|
|
83521
|
+
__export(hooks_engine_exports, {
|
|
83522
|
+
queryCommonHooks: () => queryCommonHooks,
|
|
83523
|
+
queryHookProviders: () => queryHookProviders,
|
|
83524
|
+
systemHooksMatrix: () => systemHooksMatrix
|
|
83525
|
+
});
|
|
83526
|
+
async function queryHookProviders(event) {
|
|
83527
|
+
if (!isProviderHookEvent(event)) {
|
|
83528
|
+
return engineSuccess({
|
|
83529
|
+
event,
|
|
83530
|
+
providers: []
|
|
83531
|
+
});
|
|
83532
|
+
}
|
|
83533
|
+
const { getProvidersByHookEvent: getProvidersByHookEvent2 } = await import("@cleocode/caamp");
|
|
83534
|
+
const providers = getProvidersByHookEvent2(event);
|
|
83535
|
+
return engineSuccess({
|
|
83536
|
+
event,
|
|
83537
|
+
providers: providers.map((p) => ({
|
|
83538
|
+
id: p.id,
|
|
83539
|
+
name: p.name,
|
|
83540
|
+
supportedHooks: p.capabilities?.hooks?.supported ?? []
|
|
83541
|
+
}))
|
|
83542
|
+
});
|
|
83543
|
+
}
|
|
83544
|
+
async function queryCommonHooks(providerIds) {
|
|
83545
|
+
const { getCommonHookEvents: getCommonHookEvents2 } = await import("@cleocode/caamp");
|
|
83546
|
+
const commonEvents = getCommonHookEvents2(providerIds);
|
|
83547
|
+
return engineSuccess({
|
|
83548
|
+
providerIds,
|
|
83549
|
+
commonEvents
|
|
83550
|
+
});
|
|
83551
|
+
}
|
|
83552
|
+
async function systemHooksMatrix(params) {
|
|
83553
|
+
try {
|
|
83554
|
+
const { buildHookMatrix: buildHookMatrix2, getProviderSummary, getHookMappingsVersion, detectAllProviders: detectAllProviders3 } = await import("@cleocode/caamp");
|
|
83555
|
+
const caampVersion = getHookMappingsVersion();
|
|
83556
|
+
const raw = buildHookMatrix2(params?.providerIds);
|
|
83557
|
+
const boolMatrix = {};
|
|
83558
|
+
for (const event of raw.events) {
|
|
83559
|
+
boolMatrix[event] = {};
|
|
83560
|
+
for (const providerId of raw.providers) {
|
|
83561
|
+
const mapping = raw.matrix[event]?.[providerId];
|
|
83562
|
+
boolMatrix[event][providerId] = mapping?.supported ?? false;
|
|
83563
|
+
}
|
|
83564
|
+
}
|
|
83565
|
+
const summary = raw.providers.map((providerId) => {
|
|
83566
|
+
const provSummary = getProviderSummary(providerId);
|
|
83567
|
+
if (provSummary) {
|
|
83568
|
+
return {
|
|
83569
|
+
providerId,
|
|
83570
|
+
supportedCount: provSummary.supportedCount,
|
|
83571
|
+
totalCanonical: provSummary.totalCanonical,
|
|
83572
|
+
coverage: provSummary.coverage,
|
|
83573
|
+
supported: provSummary.supported,
|
|
83574
|
+
unsupported: provSummary.unsupported
|
|
83575
|
+
};
|
|
83576
|
+
}
|
|
83577
|
+
const supported = raw.events.filter((ev) => boolMatrix[ev]?.[providerId] === true);
|
|
83578
|
+
const unsupported = raw.events.filter((ev) => boolMatrix[ev]?.[providerId] !== true);
|
|
83579
|
+
const totalCanonical = raw.events.length;
|
|
83580
|
+
const coverage = totalCanonical > 0 ? Math.round(supported.length / totalCanonical * 100) : 0;
|
|
83581
|
+
return {
|
|
83582
|
+
providerId,
|
|
83583
|
+
supportedCount: supported.length,
|
|
83584
|
+
totalCanonical,
|
|
83585
|
+
coverage,
|
|
83586
|
+
supported,
|
|
83587
|
+
unsupported
|
|
83588
|
+
};
|
|
83589
|
+
});
|
|
83590
|
+
let detectedProvider = null;
|
|
83591
|
+
const shouldDetect = params?.detectProvider !== false;
|
|
83592
|
+
if (shouldDetect) {
|
|
83593
|
+
try {
|
|
83594
|
+
const detectionResults = detectAllProviders3();
|
|
83595
|
+
const detected = detectionResults.find((r) => r.installed && r.projectDetected);
|
|
83596
|
+
if (detected) {
|
|
83597
|
+
detectedProvider = detected.provider.id;
|
|
83598
|
+
} else {
|
|
83599
|
+
const anyInstalled = detectionResults.find((r) => r.installed);
|
|
83600
|
+
if (anyInstalled) {
|
|
83601
|
+
detectedProvider = anyInstalled.provider.id;
|
|
83602
|
+
}
|
|
83603
|
+
}
|
|
83604
|
+
} catch {
|
|
83605
|
+
}
|
|
83606
|
+
}
|
|
83607
|
+
return engineSuccess({
|
|
83608
|
+
caampVersion,
|
|
83609
|
+
events: raw.events,
|
|
83610
|
+
providers: raw.providers,
|
|
83611
|
+
matrix: boolMatrix,
|
|
83612
|
+
summary,
|
|
83613
|
+
detectedProvider
|
|
83614
|
+
});
|
|
83615
|
+
} catch (err) {
|
|
83616
|
+
return engineError("E_GENERAL", err.message);
|
|
83617
|
+
}
|
|
83618
|
+
}
|
|
83619
|
+
var init_hooks_engine = __esm({
|
|
83620
|
+
"packages/cleo/src/dispatch/engines/hooks-engine.ts"() {
|
|
83621
|
+
"use strict";
|
|
83622
|
+
init_internal();
|
|
83623
|
+
init_error();
|
|
83624
|
+
}
|
|
83625
|
+
});
|
|
83626
|
+
|
|
83043
83627
|
// packages/cleo/src/dispatch/engines/init-engine.ts
|
|
83044
83628
|
async function initProject2(projectRoot, options) {
|
|
83045
83629
|
try {
|
|
@@ -86405,7 +86989,7 @@ async function dispatchFromCli(gateway, domain2, operation, params, outputOpts)
|
|
|
86405
86989
|
const dispatcher = getCliDispatcher();
|
|
86406
86990
|
const projectRoot = getProjectRoot();
|
|
86407
86991
|
const dispatchStart = Date.now();
|
|
86408
|
-
hooks.dispatch("
|
|
86992
|
+
hooks.dispatch("PromptSubmit", projectRoot, {
|
|
86409
86993
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
86410
86994
|
gateway,
|
|
86411
86995
|
domain: domain2,
|
|
@@ -86421,7 +87005,7 @@ async function dispatchFromCli(gateway, domain2, operation, params, outputOpts)
|
|
|
86421
87005
|
source: "cli",
|
|
86422
87006
|
requestId: randomUUID10()
|
|
86423
87007
|
});
|
|
86424
|
-
hooks.dispatch("
|
|
87008
|
+
hooks.dispatch("ResponseComplete", projectRoot, {
|
|
86425
87009
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
86426
87010
|
gateway,
|
|
86427
87011
|
domain: domain2,
|
|
@@ -86478,7 +87062,7 @@ async function dispatchRaw(gateway, domain2, operation, params) {
|
|
|
86478
87062
|
const dispatcher = getCliDispatcher();
|
|
86479
87063
|
const projectRoot = getProjectRoot();
|
|
86480
87064
|
const dispatchStart = Date.now();
|
|
86481
|
-
hooks.dispatch("
|
|
87065
|
+
hooks.dispatch("PromptSubmit", projectRoot, {
|
|
86482
87066
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
86483
87067
|
gateway,
|
|
86484
87068
|
domain: domain2,
|
|
@@ -86494,7 +87078,7 @@ async function dispatchRaw(gateway, domain2, operation, params) {
|
|
|
86494
87078
|
source: "cli",
|
|
86495
87079
|
requestId: randomUUID10()
|
|
86496
87080
|
});
|
|
86497
|
-
hooks.dispatch("
|
|
87081
|
+
hooks.dispatch("ResponseComplete", projectRoot, {
|
|
86498
87082
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
86499
87083
|
gateway,
|
|
86500
87084
|
domain: domain2,
|
|
@@ -88326,6 +88910,7 @@ var init_engine2 = __esm({
|
|
|
88326
88910
|
init_internal();
|
|
88327
88911
|
init_codebase_map_engine();
|
|
88328
88912
|
init_config_engine();
|
|
88913
|
+
init_hooks_engine();
|
|
88329
88914
|
init_init_engine();
|
|
88330
88915
|
init_lifecycle_engine();
|
|
88331
88916
|
init_memory_engine();
|
|
@@ -88854,6 +89439,13 @@ var init_admin2 = __esm({
|
|
|
88854
89439
|
const result = await systemSmoke();
|
|
88855
89440
|
return wrapResult(result, "query", "admin", operation, startTime);
|
|
88856
89441
|
}
|
|
89442
|
+
case "hooks.matrix": {
|
|
89443
|
+
const result = await systemHooksMatrix({
|
|
89444
|
+
providerIds: params?.providerIds,
|
|
89445
|
+
detectProvider: params?.detectProvider !== false
|
|
89446
|
+
});
|
|
89447
|
+
return wrapResult(result, "query", "admin", operation, startTime);
|
|
89448
|
+
}
|
|
88857
89449
|
default:
|
|
88858
89450
|
return unsupportedOp("query", "admin", operation, startTime);
|
|
88859
89451
|
}
|
|
@@ -89338,7 +89930,8 @@ var init_admin2 = __esm({
|
|
|
89338
89930
|
"backup",
|
|
89339
89931
|
"export",
|
|
89340
89932
|
"map",
|
|
89341
|
-
"smoke"
|
|
89933
|
+
"smoke",
|
|
89934
|
+
"hooks.matrix"
|
|
89342
89935
|
],
|
|
89343
89936
|
mutate: [
|
|
89344
89937
|
"init",
|
|
@@ -93040,46 +93633,6 @@ var init_tasks4 = __esm({
|
|
|
93040
93633
|
}
|
|
93041
93634
|
});
|
|
93042
93635
|
|
|
93043
|
-
// packages/cleo/src/dispatch/engines/hooks-engine.ts
|
|
93044
|
-
var hooks_engine_exports = {};
|
|
93045
|
-
__export(hooks_engine_exports, {
|
|
93046
|
-
queryCommonHooks: () => queryCommonHooks,
|
|
93047
|
-
queryHookProviders: () => queryHookProviders
|
|
93048
|
-
});
|
|
93049
|
-
async function queryHookProviders(event) {
|
|
93050
|
-
if (!isProviderHookEvent(event)) {
|
|
93051
|
-
return engineSuccess({
|
|
93052
|
-
event,
|
|
93053
|
-
providers: []
|
|
93054
|
-
});
|
|
93055
|
-
}
|
|
93056
|
-
const { getProvidersByHookEvent: getProvidersByHookEvent2 } = await import("@cleocode/caamp");
|
|
93057
|
-
const providers = getProvidersByHookEvent2(event);
|
|
93058
|
-
return engineSuccess({
|
|
93059
|
-
event,
|
|
93060
|
-
providers: providers.map((p) => ({
|
|
93061
|
-
id: p.id,
|
|
93062
|
-
name: p.name,
|
|
93063
|
-
supportedHooks: p.capabilities?.hooks?.supported ?? []
|
|
93064
|
-
}))
|
|
93065
|
-
});
|
|
93066
|
-
}
|
|
93067
|
-
async function queryCommonHooks(providerIds) {
|
|
93068
|
-
const { getCommonHookEvents: getCommonHookEvents2 } = await import("@cleocode/caamp");
|
|
93069
|
-
const commonEvents = getCommonHookEvents2(providerIds);
|
|
93070
|
-
return engineSuccess({
|
|
93071
|
-
providerIds,
|
|
93072
|
-
commonEvents
|
|
93073
|
-
});
|
|
93074
|
-
}
|
|
93075
|
-
var init_hooks_engine = __esm({
|
|
93076
|
-
"packages/cleo/src/dispatch/engines/hooks-engine.ts"() {
|
|
93077
|
-
"use strict";
|
|
93078
|
-
init_internal();
|
|
93079
|
-
init_error();
|
|
93080
|
-
}
|
|
93081
|
-
});
|
|
93082
|
-
|
|
93083
93636
|
// packages/cleo/src/dispatch/engines/tools-engine.ts
|
|
93084
93637
|
import {
|
|
93085
93638
|
buildInjectionContent,
|
|
@@ -98768,7 +99321,7 @@ async function main() {
|
|
|
98768
99321
|
};
|
|
98769
99322
|
}
|
|
98770
99323
|
}
|
|
98771
|
-
hooks.dispatch("
|
|
99324
|
+
hooks.dispatch("PromptSubmit", process.cwd(), {
|
|
98772
99325
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
98773
99326
|
gateway: name2,
|
|
98774
99327
|
domain: domain2,
|
|
@@ -98778,7 +99331,7 @@ async function main() {
|
|
|
98778
99331
|
});
|
|
98779
99332
|
const dispatchStart = Date.now();
|
|
98780
99333
|
let result = await handleMcpToolCall(name2, domain2, operation, params);
|
|
98781
|
-
hooks.dispatch("
|
|
99334
|
+
hooks.dispatch("ResponseComplete", process.cwd(), {
|
|
98782
99335
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
98783
99336
|
gateway: name2,
|
|
98784
99337
|
domain: domain2,
|
|
@@ -98846,7 +99399,7 @@ async function main() {
|
|
|
98846
99399
|
} catch (error40) {
|
|
98847
99400
|
reqLog.error({ err: error40 }, "Tool call error");
|
|
98848
99401
|
const errorMessage = error40 instanceof Error ? error40.message : String(error40);
|
|
98849
|
-
hooks.dispatch("
|
|
99402
|
+
hooks.dispatch("PostToolUseFailure", process.cwd(), {
|
|
98850
99403
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
98851
99404
|
errorCode: "E_INTERNAL_ERROR",
|
|
98852
99405
|
message: errorMessage,
|