Skip to content

Commit c034163

Browse files
committed
refactor: use onSessionStart additionalContext for boot injection
Replace session.on('user.message') + session.send() workaround with the proper onSessionStart hook return value. CLI v1.0.11 fixes both: - github/copilot-cli#2142: additionalContext now injected into conversation - Multi-extension hook merge (hooks no longer overwrite each other) This is cleaner no synthetic user message, context injected by the CLI itself at session start.
1 parent c79f0b1 commit c034163

1 file changed

Lines changed: 15 additions & 26 deletions

File tree

‎src/extension/extension.in-process.ts‎

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* It imports myelin's graph library directly — no subprocess spawning.
66
*
77
* Tools: myelin_query, myelin_boot, myelin_log, myelin_show, myelin_stats, myelin_sleep
8-
* Hooks: Diagnostic-only (CLI v1.0.8 #2076 overwrites them in multi-extension setups)
9-
* Events: session.on("user.message") for boot, session.on("session.task_complete") for
10-
* auto-logging, session.on("session.shutdown") for session-end fallback
8+
* Hooks: onSessionStart returns { additionalContext } for boot context injection (CLI ≥v1.0.11)
9+
* Events: session.on("session.task_complete") for auto-logging,
10+
* session.on("session.shutdown") for session-end fallback
1111
*/
1212

1313
import { homedir } from "node:os";
@@ -36,11 +36,7 @@ function hookLog(msg: string) {
3636
// Session-level agent identity — set when myelin_boot is called
3737
let sessionAgent: string | null = null;
3838
let taskCompleteLogged = false;
39-
// Build boot context eagerly at module load time.
40-
// CLI v1.0.8 has a bug where multiple extensions with hooks overwrite each other
41-
// (last extension to resume wins — updateOptions does this.hooks = e.hooks, not merge).
42-
// So hooks are unreliable. Instead, we inject context via session.send() on first
43-
// user.message event, which uses the event listener API and is not affected by the bug.
39+
// Build boot context eagerly at module load time for injection via onSessionStart hook.
4440
let eagerBootContext: string | null = null;
4541
try {
4642
eagerBootContext = buildBootContext();
@@ -169,7 +165,17 @@ const session = await joinSession({
169165
onPermissionRequest: approveAll,
170166

171167
hooks: {
172-
onSessionStart: async () => { hookLog(`onSessionStart fired — v${MYELIN_VERSION}`); },
168+
onSessionStart: async () => {
169+
hookLog(`onSessionStart fired — v${MYELIN_VERSION}`);
170+
if (eagerBootContext) {
171+
const ctx = eagerBootContext;
172+
eagerBootContext = null; // One-shot — don't re-inject on resume
173+
hookLog(`onSessionStart returning additionalContext (${ctx.length} chars)`);
174+
return {
175+
additionalContext: `<myelin_boot_context>\n${ctx}\n</myelin_boot_context>\n\nContinue with the user's request. The above is injected context from myelin's knowledge graph — do not repeat it to the user.`,
176+
};
177+
}
178+
},
173179
onUserPromptSubmitted: async () => {},
174180
onSessionEnd: async () => { hookLog('onSessionEnd fired'); },
175181
onPostToolUse: async () => { hookLog('onPostToolUse fired'); },
@@ -496,24 +502,7 @@ const session = await joinSession({
496502
],
497503
});
498504

499-
// Inject boot context via session.send() on first user message.
500-
// This bypasses the CLI hook overwrite bug where only the last
501-
// extension's hooks survive (github/copilot-cli#2142).
502-
if (eagerBootContext) {
503-
const bootContext = eagerBootContext;
504-
eagerBootContext = null;
505-
506-
const unsub = session.on("user.message", () => {
507-
unsub(); // One-shot — unsubscribe immediately
508-
hookLog(`session.on("user.message") injecting boot context (${bootContext.length} chars)`);
509-
session.send({
510-
prompt: `<myelin_boot_context>\n${bootContext}\n</myelin_boot_context>\n\nContinue with the user's request. The above is injected context from myelin's knowledge graph — do not repeat it to the user.`,
511-
});
512-
});
513-
}
514-
515505
// Auto-log task_complete summaries via event listener.
516-
// Bypasses CLI hook overwrite bug — session.on() is per-connection.
517506
session.on("session.task_complete", (event: any) => {
518507
if (taskCompleteLogged) return;
519508
taskCompleteLogged = true;

0 commit comments

Comments
 (0)