Skip to content

Commit cfdbedb

Browse files
authored
fix(enclave): use conversation messages instead of system prompt (#22)
1 parent c3b4902 commit cfdbedb

2 files changed

Lines changed: 41 additions & 18 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"pi-enclave": patch
3+
---
4+
5+
Use conversation messages instead of system prompt for enclave hints.
6+
7+
The enclave context hint is now a regular message added once per session, instead of being injected via `before_agent_start`. This avoids invalidating the prompt cache when toggling enclave on/off mid-session.

packages/enclave/src/index.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ export default function (pi: ExtensionAPI) {
280280
if (isActive()) {
281281
// Start VM eagerly so it's ready when the first tool runs
282282
ensureVm(ctx);
283+
// Send hint if not already in this session's history
284+
if (!isLastHintActive(ctx)) {
285+
sendEnclaveHint();
286+
}
283287
} else if (configEnabled === undefined && sessionOverride === undefined) {
284288
ctx.ui.notify("🧊 pi-enclave is installed but not enabled. Run /enclave init to set up.");
285289
}
@@ -293,6 +297,9 @@ export default function (pi: ExtensionAPI) {
293297

294298
if (isActive()) {
295299
ensureVm(ctx);
300+
if (!isLastHintActive(ctx)) {
301+
sendEnclaveHint();
302+
}
296303
}
297304
});
298305

@@ -351,23 +358,32 @@ export default function (pi: ExtensionAPI) {
351358
});
352359

353360
// -----------------------------------------------------------------------
354-
// System prompt (only when active)
361+
// Enclave context messages (added to conversation, not system prompt)
355362
// -----------------------------------------------------------------------
356-
pi.on("before_agent_start", async (_event, _ctx) => {
357-
if (!isActive()) return;
358-
return {
359-
message: {
360-
customType: "enclave:info",
361-
content: [
362-
{
363-
type: "text" as const,
364-
text: "Commands run inside an isolated Alpine Linux VM (pi-enclave). If a command is not found, ask the user to install it with `/enclave add <tool-name>`. Package names may differ from binary names (e.g. `github-cli` for `gh`).",
365-
},
366-
],
367-
display: false,
368-
},
369-
};
370-
});
363+
const ENCLAVE_HINT =
364+
"🧊 Enclave active. All tools are running inside an isolated Alpine Linux VM. If a command is not found, install it with `/enclave add <package>`. Package names may differ from binary names (e.g. `github-cli` for `gh`).";
365+
366+
/** Check if the most recent enclave message is an "on" hint (not an "off"). */
367+
function isLastHintActive(ctx: ExtensionContext): boolean {
368+
const entries = ctx.sessionManager.getEntries();
369+
for (let i = entries.length - 1; i >= 0; i--) {
370+
const e = entries[i];
371+
if (e.type === "custom_message" && e.customType === "enclave:info") return true;
372+
if (e.type === "custom_message" && e.customType === "enclave:off") return false;
373+
}
374+
return false;
375+
}
376+
377+
function sendEnclaveHint() {
378+
pi.sendMessage({ customType: "enclave:info", content: ENCLAVE_HINT, display: true }, { deliverAs: "nextTurn" });
379+
}
380+
381+
function sendEnclaveOff() {
382+
pi.sendMessage(
383+
{ customType: "enclave:off", content: "🧊 Enclave disabled. Tools are running on the host.", display: true },
384+
{ deliverAs: "nextTurn" },
385+
);
386+
}
371387

372388
// -----------------------------------------------------------------------
373389
// Commands
@@ -449,7 +465,7 @@ export default function (pi: ExtensionAPI) {
449465
case "on": {
450466
sessionOverride = true;
451467
pi.appendEntry(SESSION_ENTRY_TYPE, true);
452-
ctx.ui.notify("🧊 pi-enclave enabled for this session. VM starts on next tool use.");
468+
sendEnclaveHint();
453469
break;
454470
}
455471

@@ -458,7 +474,7 @@ export default function (pi: ExtensionAPI) {
458474
pi.appendEntry(SESSION_ENTRY_TYPE, false);
459475
await shutdownVm();
460476
ctx.ui.setStatus("enclave", undefined);
461-
ctx.ui.notify("🧊 pi-enclave disabled for this session. Tools run on the host.");
477+
sendEnclaveOff();
462478
break;
463479
}
464480

0 commit comments

Comments
 (0)