opencode-claude-code-acpbridge 0.15.2 → 0.16.0
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/CHANGELOG.md +43 -0
- package/README.md +49 -11
- package/dist/acp/base-prompt.d.ts +5 -0
- package/dist/acp/base-prompt.d.ts.map +1 -0
- package/dist/acp/base-prompt.js +10 -0
- package/dist/acp/base-prompt.js.map +1 -0
- package/dist/acp/claude-config.d.ts +21 -10
- package/dist/acp/claude-config.d.ts.map +1 -1
- package/dist/acp/claude-config.js.map +1 -1
- package/dist/acp/harness.d.ts +4 -0
- package/dist/acp/harness.d.ts.map +1 -0
- package/dist/acp/harness.js +37 -0
- package/dist/acp/harness.js.map +1 -0
- package/dist/plugin/environment.d.ts +19 -0
- package/dist/plugin/environment.d.ts.map +1 -0
- package/dist/plugin/environment.js +146 -0
- package/dist/plugin/environment.js.map +1 -0
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +39 -2
- package/dist/plugin/index.js.map +1 -1
- package/dist/plugin/sessions.d.ts +5 -0
- package/dist/plugin/sessions.d.ts.map +1 -1
- package/dist/plugin/sessions.js +1 -0
- package/dist/plugin/sessions.js.map +1 -1
- package/dist/provider/model.d.ts +19 -3
- package/dist/provider/model.d.ts.map +1 -1
- package/dist/provider/model.js +148 -40
- package/dist/provider/model.js.map +1 -1
- package/docs/DEVELOPMENT.md +20 -9
- package/examples/README.md +4 -2
- package/package.json +1 -1
package/dist/provider/model.js
CHANGED
|
@@ -27,11 +27,13 @@ import { spawn } from "node:child_process";
|
|
|
27
27
|
import { Writable, Readable } from "node:stream";
|
|
28
28
|
import { randomBytes } from "node:crypto";
|
|
29
29
|
import { homedir } from "node:os";
|
|
30
|
-
import { join } from "node:path";
|
|
30
|
+
import { join, resolve } from "node:path";
|
|
31
31
|
import { ClientSideConnection, ndJsonStream, PROTOCOL_VERSION } from "@agentclientprotocol/sdk";
|
|
32
32
|
import { APICallError } from "@ai-sdk/provider";
|
|
33
33
|
import { McpHost } from "../acp/mcp-host.js";
|
|
34
34
|
import { buildClaudeCodeOptions, resolveConfigDir, } from "../acp/claude-config.js";
|
|
35
|
+
import { BASE_PROMPT, POINTER_PROMPT } from "../acp/base-prompt.js";
|
|
36
|
+
import { sanitizeHarnessText, systemUpdate } from "../acp/harness.js";
|
|
35
37
|
import { setLink, updateUsage } from "../plugin/sessions.js";
|
|
36
38
|
import { addAlwaysRule, evaluate as evaluateBridge } from "../plugin/bridge-rules.js";
|
|
37
39
|
import { DEBUG, logger } from "../log.js";
|
|
@@ -185,10 +187,22 @@ function isChildCall(options) {
|
|
|
185
187
|
}
|
|
186
188
|
/** Identity of what a session is CREATED with (`_meta.claudeCode.options` + system
|
|
187
189
|
* prompt mode). Model, effort and mode are applied live, so they don't count. */
|
|
190
|
+
/** The system prompt mode a session runs under: `pointer` unless the config names another one. */
|
|
191
|
+
function systemModeOf(ctrl) {
|
|
192
|
+
return ctrl.systemPromptMode ?? "pointer";
|
|
193
|
+
}
|
|
188
194
|
function profileKey(ctrl, systemMode) {
|
|
189
195
|
const { model: _m, effort: _e, ...creation } = buildClaudeCodeOptions(ctrl);
|
|
190
196
|
return hashText(JSON.stringify({ o: creation, s: systemMode }));
|
|
191
197
|
}
|
|
198
|
+
/** `_meta.claudeCode.options` for a session working in `cwd`: the clean brain (plus `extra`),
|
|
199
|
+
* with `PWD` set to that directory. Without it Claude Code takes PWD from the agent process
|
|
200
|
+
* (the OpenCode service's directory) while its cwd is the session's. Kept out of
|
|
201
|
+
* `buildClaudeCodeOptions`, so a directory never changes the creation profile (profileKey). */
|
|
202
|
+
function sessionOptions(ctrl, cwd, extra) {
|
|
203
|
+
const base = { ...buildClaudeCodeOptions(ctrl), ...(extra ?? {}) };
|
|
204
|
+
return { ...base, env: { ...(base.env ?? {}), PWD: cwd } };
|
|
205
|
+
}
|
|
192
206
|
/** Message + structured data of an ACP error (`RequestError.data.errorKind`, details). */
|
|
193
207
|
function errorText(err) {
|
|
194
208
|
if (!err || typeof err !== "object")
|
|
@@ -468,13 +482,17 @@ export class ACPOrchestratorModel {
|
|
|
468
482
|
const convKey = hashText(firstUserText(options.prompt));
|
|
469
483
|
const toolsKey = tools.map((x) => x.name).sort().join(",");
|
|
470
484
|
// The system prompt is part of the session identity ONLY when it actually
|
|
471
|
-
// travels. Under the
|
|
485
|
+
// travels. Under `preset` (the opt-out from the `pointer` default) OpenCode's system is never sent
|
|
472
486
|
// (see buildSystemPromptMeta), so hashing it would tear the session down —
|
|
473
487
|
// and replay the whole history — every time OpenCode swaps agents or its
|
|
474
488
|
// dynamic blocks (date, AGENTS.md, skills) shift. The factual part of that
|
|
475
489
|
// system is forwarded separately as user context (see contextBlocks).
|
|
476
|
-
const systemMode = ctrl
|
|
477
|
-
|
|
490
|
+
const systemMode = systemModeOf(ctrl);
|
|
491
|
+
// `pointer` is fixed too: the client's text changes as a `<system-update>`, never by dropping the
|
|
492
|
+
// session. Its base prompt is part of the identity: another base is a new session, not a silent swap.
|
|
493
|
+
const systemKey = systemMode === "preset" ? "preset"
|
|
494
|
+
: systemMode === "pointer" ? `pointer:${hashText(ctrl.basePrompt ?? BASE_PROMPT)}`
|
|
495
|
+
: hashText(systemText(options.prompt));
|
|
478
496
|
// Params baked into newSession's _meta (thinking/systemPromptMode) can't change
|
|
479
497
|
// mid-session → a change forces a fresh session. mode/effort are applied at
|
|
480
498
|
// runtime via set_mode/set_config_option instead (no reset needed).
|
|
@@ -520,13 +538,13 @@ export class ACPOrchestratorModel {
|
|
|
520
538
|
// fixed at creation. Fork the live session with the new profile — same context,
|
|
521
539
|
// no replay — instead of starting over; only a failed fork falls back to that.
|
|
522
540
|
const cls = this.classify(t, hist, keys, ctrl);
|
|
523
|
-
if (cls.kind !== "append" || !(await this.forkInPlace(t, ctrl, tools,
|
|
541
|
+
if (cls.kind !== "append" || !(await this.forkInPlace(t, ctrl, tools, options.prompt, cfgKey))) {
|
|
524
542
|
drop = "creation config changed";
|
|
525
543
|
}
|
|
526
544
|
}
|
|
527
545
|
// The previous prompt failed agent-side: talk to a new agent process, not to the one
|
|
528
546
|
// still holding that turn's leftovers (see `resync`).
|
|
529
|
-
if (!drop && t.resync && !(await this.reopenInPlace(t, ctrl, tools,
|
|
547
|
+
if (!drop && t.resync && !(await this.reopenInPlace(t, ctrl, tools, options.prompt, cfgKey, "recovering after an agent error"))) {
|
|
530
548
|
drop = "agent session out of sync";
|
|
531
549
|
}
|
|
532
550
|
if (!drop) {
|
|
@@ -564,8 +582,10 @@ export class ACPOrchestratorModel {
|
|
|
564
582
|
: cur.checkpoints.find((c) => c.at === legacyAt);
|
|
565
583
|
if (!cp)
|
|
566
584
|
dbg(`smartRevert: no checkpoint for the rewind (extent ${mode === "v2" ? sharedExtent(cur.sentAnchors, hist) : legacyAt}, hist ${hist.length}, checkpoints [${cur.checkpoints.map((c) => c.at).join(",")}])`);
|
|
567
|
-
if (cp && (await this.forkInPlace(t, ctrl, tools,
|
|
585
|
+
if (cp && (await this.forkInPlace(t, ctrl, tools, options.prompt, cfgKey, undefined, "revert", cp))) {
|
|
568
586
|
const at = cp.at;
|
|
587
|
+
// The fork is cut before the reverted turn: its transcript has only what was there then.
|
|
588
|
+
this.forgetSystem(t, false);
|
|
569
589
|
this.markKnown(t, hist.slice(0, at));
|
|
570
590
|
t.checkpoints = t.checkpoints.filter((c) => c.at < at);
|
|
571
591
|
this.decide(options, { decision: "smart-revert", session: t.sessionId, at, uuid: cp.uuid, from: cp.sid });
|
|
@@ -589,7 +609,7 @@ export class ACPOrchestratorModel {
|
|
|
589
609
|
live.pending = [];
|
|
590
610
|
live.doneResp = null;
|
|
591
611
|
live.doneErr = null;
|
|
592
|
-
live.reopen = this.reopener(live, ctrl, tools,
|
|
612
|
+
live.reopen = this.reopener(live, ctrl, tools, options.prompt, cfgKey);
|
|
593
613
|
live.controller = controller;
|
|
594
614
|
live.lastActive = Date.now();
|
|
595
615
|
live.isChild = isChildCall(options);
|
|
@@ -607,6 +627,7 @@ export class ACPOrchestratorModel {
|
|
|
607
627
|
...this.contextBlocks(live, options.prompt, ctrl),
|
|
608
628
|
...lead,
|
|
609
629
|
...(tail.length ? this.blocksFor(tail, live) : rawTail.length ? [] : this.lastUserBlocks(options.prompt)),
|
|
630
|
+
...this.cwdNote(live, po),
|
|
610
631
|
];
|
|
611
632
|
this.decide(options, {
|
|
612
633
|
decision: "reuse", session: live.sessionId, from: cls.fromIndex, sent: live.sentKeys.length,
|
|
@@ -655,6 +676,9 @@ export class ACPOrchestratorModel {
|
|
|
655
676
|
let replayFull = true;
|
|
656
677
|
let defaultEffort;
|
|
657
678
|
let carriedCps = [];
|
|
679
|
+
// The registry's `sysHash` of a plain resume (not a fork: a fork is cut at a checkpoint, and
|
|
680
|
+
// the stored hash describes the latest state): the operator's instructions it holds.
|
|
681
|
+
let seedSysHash;
|
|
658
682
|
// A registry entry for a DIFFERENT conversation (first message changed: compaction,
|
|
659
683
|
// or a reused id) must not be resumed — the agent would carry the wrong context.
|
|
660
684
|
const sameConv = !po.resumeConvKey || po.resumeConvKey === convKey;
|
|
@@ -671,20 +695,25 @@ export class ACPOrchestratorModel {
|
|
|
671
695
|
const rewound = typeof po.resumeSentHash === "string" && prefixHash(hist, resumeLen) !== po.resumeSentHash;
|
|
672
696
|
if (!rewound && hist.length > resumeLen) {
|
|
673
697
|
try {
|
|
698
|
+
// Re-apply the clean-brain options and the system prompt on resume too. Without the
|
|
699
|
+
// options the agent falls back to its defaults (--tools default, --setting-sources=user,
|
|
700
|
+
// project,local, no --strict-mcp-config) and the resumed session regains native tools +
|
|
701
|
+
// the claude.ai connectors; without the prompt it loses the session's system prompt.
|
|
702
|
+
const resumeMeta = { claudeCode: { options: sessionOptions(ctrl, resumeCwd) } };
|
|
703
|
+
const resumeSys = this.buildSystemPromptMeta(options.prompt, ctrl);
|
|
704
|
+
if (resumeSys !== undefined)
|
|
705
|
+
resumeMeta.systemPrompt = resumeSys;
|
|
674
706
|
const resumed = await this.connection.unstable_resumeSession({
|
|
675
707
|
sessionId: resumeId,
|
|
676
708
|
cwd: resumeCwd,
|
|
677
709
|
mcpServers: [serverConfig],
|
|
678
|
-
|
|
679
|
-
// falls back to its defaults (--tools default, --setting-sources=user,
|
|
680
|
-
// project,local, no --strict-mcp-config) and the resumed session regains
|
|
681
|
-
// native tools + the claude.ai connectors.
|
|
682
|
-
_meta: { claudeCode: { options: buildClaudeCodeOptions(ctrl) } },
|
|
710
|
+
_meta: resumeMeta,
|
|
683
711
|
});
|
|
684
712
|
sessionId = resumeId;
|
|
685
713
|
replayFull = false;
|
|
686
714
|
cwd = resumeCwd;
|
|
687
715
|
carriedCps = resumeCps;
|
|
716
|
+
seedSysHash = typeof po.resumeSysHash === "string" ? po.resumeSysHash : undefined;
|
|
688
717
|
defaultEffort = effortOption(resumed);
|
|
689
718
|
dbg(`resume ${resumeId} (oc=${ocSession ?? "?"}) — no history replay`);
|
|
690
719
|
}
|
|
@@ -706,8 +735,8 @@ export class ACPOrchestratorModel {
|
|
|
706
735
|
dbg(`smartRevert: no registry checkpoint matches (hist ${hist.length}, sent ${resumeLen}, ${po.resumeSentHash}→${prefixHash(hist, resumeLen)}; checkpoints ${seen.join(" ") || "none"})`);
|
|
707
736
|
}
|
|
708
737
|
if (cp) {
|
|
709
|
-
const meta = { claudeCode: { options:
|
|
710
|
-
const sysMeta = this.buildSystemPromptMeta(options.prompt,
|
|
738
|
+
const meta = { claudeCode: { options: sessionOptions(ctrl, resumeCwd) } };
|
|
739
|
+
const sysMeta = this.buildSystemPromptMeta(options.prompt, ctrl);
|
|
711
740
|
if (sysMeta !== undefined)
|
|
712
741
|
meta.systemPrompt = sysMeta;
|
|
713
742
|
try {
|
|
@@ -731,8 +760,8 @@ export class ACPOrchestratorModel {
|
|
|
731
760
|
}
|
|
732
761
|
if (!sessionId) {
|
|
733
762
|
// OpenCode's system prompt → _meta.systemPrompt (preset/append/replace).
|
|
734
|
-
const meta = { claudeCode: { options:
|
|
735
|
-
const sysMeta = this.buildSystemPromptMeta(options.prompt,
|
|
763
|
+
const meta = { claudeCode: { options: sessionOptions(ctrl, cwd) } };
|
|
764
|
+
const sysMeta = this.buildSystemPromptMeta(options.prompt, ctrl);
|
|
736
765
|
if (sysMeta !== undefined)
|
|
737
766
|
meta.systemPrompt = sysMeta;
|
|
738
767
|
const session = await this.connection.newSession({
|
|
@@ -764,6 +793,8 @@ export class ACPOrchestratorModel {
|
|
|
764
793
|
hasQuestion: typeof po.hasQuestion === "boolean" ? po.hasQuestion : undefined,
|
|
765
794
|
isChild: isChildCall(options),
|
|
766
795
|
defaultEffort,
|
|
796
|
+
resumed: !replayFull,
|
|
797
|
+
sysHash: seedSysHash,
|
|
767
798
|
// Resume path: carry the persisted snapshot over so the registry keeps a
|
|
768
799
|
// value even if the resumed session never completes another turn.
|
|
769
800
|
lastUsage: replayFull ? undefined : po.resumeLastUsage,
|
|
@@ -802,7 +833,7 @@ export class ACPOrchestratorModel {
|
|
|
802
833
|
lastActive: Date.now(),
|
|
803
834
|
};
|
|
804
835
|
ref.t = t;
|
|
805
|
-
t.reopen = this.reopener(t, ctrl, tools,
|
|
836
|
+
t.reopen = this.reopener(t, ctrl, tools, options.prompt, cfgKey);
|
|
806
837
|
host.onToolCall((c) => this.onToolCall(t, c));
|
|
807
838
|
host.onToolCancelled((id) => this.onToolCancelled(t, id));
|
|
808
839
|
this.sessions.set(key, t);
|
|
@@ -813,16 +844,18 @@ export class ACPOrchestratorModel {
|
|
|
813
844
|
// The state of the agent before the tail goes out: where a later revert can rewind to.
|
|
814
845
|
if (!replayFull && ctrl.smartRevert !== false)
|
|
815
846
|
this.recordCheckpoint(t, hist, resumeLen);
|
|
816
|
-
this.persistLink(t, hist.length, anchorsOf(hist).map((a) => a.key));
|
|
817
847
|
// Resume: the registry may lag behind (process died mid-turn), and the agent's own
|
|
818
848
|
// last answer follows the recorded point — the resumed session already has it.
|
|
819
849
|
const tail = replayFull ? hist : this.dropOwnLead(t, hist.slice(resumeLen));
|
|
820
850
|
if (DEBUG && !replayFull)
|
|
821
851
|
dbg(`resume — catch-up ${tail.length} msg(s)`);
|
|
822
852
|
const ctxBlocks = this.contextBlocks(t, options.prompt, ctrl);
|
|
853
|
+
// After the context blocks: the stored `sysHash` says what the transcript now holds.
|
|
854
|
+
this.persistLink(t, hist.length, anchorsOf(hist).map((a) => a.key));
|
|
823
855
|
const promptBlocks = [
|
|
824
856
|
...ctxBlocks,
|
|
825
857
|
...(tail.length ? this.blocksFor(tail, t, replayFull ? "replay" : "tail") : [{ type: "text", text: "" }]),
|
|
858
|
+
...this.cwdNote(t, po),
|
|
826
859
|
];
|
|
827
860
|
// Split replay: a replayed history is ONE transcript entry, so a later revert to the
|
|
828
861
|
// prompt that came with it had nowhere to fork and replayed everything again (and again).
|
|
@@ -839,7 +872,7 @@ export class ACPOrchestratorModel {
|
|
|
839
872
|
if (await this.primeReplay(t, histBlocks)) {
|
|
840
873
|
this.recordCheckpoint(t, hist, split);
|
|
841
874
|
this.persistLink(t, hist.length, anchorsOf(hist).map((a) => a.key));
|
|
842
|
-
this.sendPrompt(t, this.blocksFor(hist.slice(split), t, "tail"));
|
|
875
|
+
this.sendPrompt(t, [...this.blocksFor(hist.slice(split), t, "tail"), ...this.cwdNote(t, po)]);
|
|
843
876
|
return;
|
|
844
877
|
}
|
|
845
878
|
if (t.aborted)
|
|
@@ -923,7 +956,7 @@ export class ACPOrchestratorModel {
|
|
|
923
956
|
/** Swap `t` onto a fork of its ACP session created with the current profile. The
|
|
924
957
|
* fork carries the whole transcript, so what `t` knows (sentKeys, own output) still
|
|
925
958
|
* holds; it gets its own MCP host. The old session is closed. */
|
|
926
|
-
async forkInPlace(t, ctrl, tools,
|
|
959
|
+
async forkInPlace(t, ctrl, tools, prompt, cfgKey, extra, why = "agent profile changed", at) {
|
|
927
960
|
if (t.inflight || t.pendingTools.size || t.pendingPermissions.size) {
|
|
928
961
|
this.abortTurn(t, why);
|
|
929
962
|
await this.settle(t);
|
|
@@ -933,8 +966,8 @@ export class ACPOrchestratorModel {
|
|
|
933
966
|
try {
|
|
934
967
|
await host.start();
|
|
935
968
|
host.setTools(tools);
|
|
936
|
-
const meta = { claudeCode: { options:
|
|
937
|
-
const sysMeta = this.buildSystemPromptMeta(prompt,
|
|
969
|
+
const meta = { claudeCode: { options: sessionOptions(ctrl, t.cwd, extra) } };
|
|
970
|
+
const sysMeta = this.buildSystemPromptMeta(prompt, ctrl);
|
|
938
971
|
if (sysMeta !== undefined)
|
|
939
972
|
meta.systemPrompt = sysMeta;
|
|
940
973
|
const res = await this.forkSessionAt(t.sessionId, t.cwd, [host.getServerConfig()], meta, at);
|
|
@@ -967,9 +1000,9 @@ export class ACPOrchestratorModel {
|
|
|
967
1000
|
}
|
|
968
1001
|
/** `reopenInPlace` bound to the latest turn's profile, with its mode/model/effort
|
|
969
1002
|
* re-applied — for a recovery that happens outside `startFresh`. */
|
|
970
|
-
reopener(t, ctrl, tools,
|
|
1003
|
+
reopener(t, ctrl, tools, prompt, cfgKey) {
|
|
971
1004
|
return async (why) => {
|
|
972
|
-
if (!(await this.reopenInPlace(t, ctrl, tools,
|
|
1005
|
+
if (!(await this.reopenInPlace(t, ctrl, tools, prompt, cfgKey, why)))
|
|
973
1006
|
return false;
|
|
974
1007
|
await this.applySessionConfig(t, ctrl);
|
|
975
1008
|
return true;
|
|
@@ -979,7 +1012,7 @@ export class ACPOrchestratorModel {
|
|
|
979
1012
|
* resume it from its transcript. Whatever the old process still had queued is gone, and
|
|
980
1013
|
* unlike a fork no transcript copy is written (a retry loop would write one per try).
|
|
981
1014
|
* Falls back to a fork when the resume fails. */
|
|
982
|
-
async reopenInPlace(t, ctrl, tools,
|
|
1015
|
+
async reopenInPlace(t, ctrl, tools, prompt, cfgKey, why) {
|
|
983
1016
|
if (t.inflight || t.pendingTools.size || t.pendingPermissions.size) {
|
|
984
1017
|
this.abortTurn(t, why);
|
|
985
1018
|
await this.settle(t);
|
|
@@ -996,8 +1029,8 @@ export class ACPOrchestratorModel {
|
|
|
996
1029
|
new Promise((r) => { timer = setTimeout(r, SETTLE_MS); }),
|
|
997
1030
|
]);
|
|
998
1031
|
clearTimeout(timer);
|
|
999
|
-
const meta = { claudeCode: { options:
|
|
1000
|
-
const sysMeta = this.buildSystemPromptMeta(prompt,
|
|
1032
|
+
const meta = { claudeCode: { options: sessionOptions(ctrl, t.cwd) } };
|
|
1033
|
+
const sysMeta = this.buildSystemPromptMeta(prompt, ctrl);
|
|
1001
1034
|
if (sysMeta !== undefined)
|
|
1002
1035
|
meta.systemPrompt = sysMeta;
|
|
1003
1036
|
const res = await this.connection.unstable_resumeSession({
|
|
@@ -1025,7 +1058,7 @@ export class ACPOrchestratorModel {
|
|
|
1025
1058
|
catch (e) {
|
|
1026
1059
|
host.stop();
|
|
1027
1060
|
dbg(`re-open of ${t.sessionId} failed (${e instanceof Error ? e.message : String(e)}) — forking instead`);
|
|
1028
|
-
return this.forkInPlace(t, ctrl, tools,
|
|
1061
|
+
return this.forkInPlace(t, ctrl, tools, prompt, cfgKey, undefined, why);
|
|
1029
1062
|
}
|
|
1030
1063
|
}
|
|
1031
1064
|
/** Remember where this turn starts (index of its first user message) and Claude's
|
|
@@ -1048,6 +1081,7 @@ export class ACPOrchestratorModel {
|
|
|
1048
1081
|
setLink(t.ocSession, t.sessionId, len, t.convKey, t.ownPrefixes, t.cwd, {
|
|
1049
1082
|
sentHash: anchorsHash(anchorKeys),
|
|
1050
1083
|
checkpoints: t.checkpoints.slice(-48),
|
|
1084
|
+
sysHash: t.sysHash,
|
|
1051
1085
|
});
|
|
1052
1086
|
}
|
|
1053
1087
|
/** Free the agent-side session (its Claude Code process and MCP servers). The client
|
|
@@ -1103,8 +1137,12 @@ export class ACPOrchestratorModel {
|
|
|
1103
1137
|
const gen = ++t.gen;
|
|
1104
1138
|
const done = this.connection
|
|
1105
1139
|
.prompt({ sessionId: t.sessionId, prompt: [{ type: "text", text: "/compact" }] })
|
|
1106
|
-
.then(() => {
|
|
1107
|
-
|
|
1140
|
+
.then(() => {
|
|
1141
|
+
// The agent's own compaction: the summary replaces what it held of the instructions.
|
|
1142
|
+
this.forgetSystem(t);
|
|
1143
|
+
if (gen === t.gen)
|
|
1144
|
+
t.adoptCheckpoint = true;
|
|
1145
|
+
}, (e) => {
|
|
1108
1146
|
note(`native /compact of ${t.sessionId} failed (${errorText(e)}) — the session is re-opened on the next turn`);
|
|
1109
1147
|
t.resync = true;
|
|
1110
1148
|
})
|
|
@@ -1734,8 +1772,15 @@ export class ACPOrchestratorModel {
|
|
|
1734
1772
|
}
|
|
1735
1773
|
switch (update?.sessionUpdate) {
|
|
1736
1774
|
case "usage_update":
|
|
1737
|
-
if (typeof update.used === "number")
|
|
1775
|
+
if (typeof update.used === "number") {
|
|
1776
|
+
// Claude Code's own auto-compaction shows as the occupancy falling by half at once
|
|
1777
|
+
// (a /compact of the bridge is handled where it completes, see nativeCompact).
|
|
1778
|
+
if (t.contextUsed && update.used < t.contextUsed / 2) {
|
|
1779
|
+
dbg(`context ${t.contextUsed} → ${update.used}: compacted by the agent`);
|
|
1780
|
+
this.forgetSystem(t);
|
|
1781
|
+
}
|
|
1738
1782
|
t.contextUsed = update.used;
|
|
1783
|
+
}
|
|
1739
1784
|
if (typeof update.size === "number" && update.size > 0)
|
|
1740
1785
|
t.contextSize = update.size;
|
|
1741
1786
|
break;
|
|
@@ -1881,8 +1926,8 @@ export class ACPOrchestratorModel {
|
|
|
1881
1926
|
return own;
|
|
1882
1927
|
const SHAPE_KEYS = new Set([
|
|
1883
1928
|
"ocSession", "ocAgent", "resumeId", "resumeLen", "resumeImported", "resumeConvKey", "resumeLastUsage", "resumeIdPrefixes",
|
|
1884
|
-
"requestKind", "hasQuestion", "ocDirectory", "resumeCwd",
|
|
1885
|
-
"mode", "effort", "thinking", "systemPromptMode", "forwardEnvContext",
|
|
1929
|
+
"requestKind", "hasQuestion", "ocDirectory", "resumeCwd", "resumeSysHash",
|
|
1930
|
+
"mode", "effort", "thinking", "systemPromptMode", "basePrompt", "forwardEnvContext",
|
|
1886
1931
|
"tools", "allowedTools", "disallowedTools", "settingSources", "strictMcpConfig",
|
|
1887
1932
|
"nativeTools", "resourceSearch", "nativeSubagents", "nativeSkills", "projectMemory", "skillShell",
|
|
1888
1933
|
]);
|
|
@@ -1907,6 +1952,19 @@ export class ACPOrchestratorModel {
|
|
|
1907
1952
|
return this.config.cwd;
|
|
1908
1953
|
return typeof po.ocDirectory === "string" && po.ocDirectory ? po.ocDirectory : process.cwd();
|
|
1909
1954
|
}
|
|
1955
|
+
/** A live or resumed session keeps the directory it was created in (no fork on a change), so
|
|
1956
|
+
* when OpenCode's directory moved, the prompt names the new one for paths and commands. */
|
|
1957
|
+
cwdNote(t, po) {
|
|
1958
|
+
if (this.config.cwd || typeof po.ocDirectory !== "string" || !po.ocDirectory)
|
|
1959
|
+
return [];
|
|
1960
|
+
const wanted = this.sessionCwd(po);
|
|
1961
|
+
if (resolve(wanted) === resolve(t.cwd))
|
|
1962
|
+
return [];
|
|
1963
|
+
return [{
|
|
1964
|
+
type: "text",
|
|
1965
|
+
text: `(The working directory for this session is now ${wanted}. Use it for paths and commands, not the one in the "# Environment" note.)`,
|
|
1966
|
+
}];
|
|
1967
|
+
}
|
|
1910
1968
|
/** Provider config overlaid with per-call `providerOptions.acp` (per-agent mode/effort/…). */
|
|
1911
1969
|
effectiveCtrl(options) {
|
|
1912
1970
|
const ctrl = { ...(this.config.claudeCode ?? {}) };
|
|
@@ -1915,7 +1973,7 @@ export class ACPOrchestratorModel {
|
|
|
1915
1973
|
// Per-agent overrides from `agent.<name>.options` (→ providerOptions.acp):
|
|
1916
1974
|
// mode/effort/reasoning plus the native-feature toggles.
|
|
1917
1975
|
const KEYS = [
|
|
1918
|
-
"mode", "effort", "thinking", "systemPromptMode", "forwardEnvContext",
|
|
1976
|
+
"mode", "effort", "thinking", "systemPromptMode", "basePrompt", "forwardEnvContext",
|
|
1919
1977
|
"tools", "allowedTools", "disallowedTools", "settingSources", "strictMcpConfig",
|
|
1920
1978
|
"nativeTools", "resourceSearch", "nativeSubagents", "nativeSkills", "projectMemory", "skillShell",
|
|
1921
1979
|
];
|
|
@@ -2180,8 +2238,8 @@ export class ACPOrchestratorModel {
|
|
|
2180
2238
|
return out;
|
|
2181
2239
|
}
|
|
2182
2240
|
/** Nudges the agent can only get from the tool description. Under the default
|
|
2183
|
-
* `systemPromptMode: "preset"` OpenCode's system prompt never
|
|
2184
|
-
* so the instruction that would normally push it toward `question` is missing —
|
|
2241
|
+
* `systemPromptMode: "preset"` (the opt-out from `pointer`) OpenCode's system prompt never
|
|
2242
|
+
* reaches the agent, so the instruction that would normally push it toward `question` is missing —
|
|
2185
2243
|
* and its own `AskUserQuestion` is disabled by the ACP agent. Without this the
|
|
2186
2244
|
* agent silently assumes instead of asking. A tool description is NOT a system
|
|
2187
2245
|
* prompt, so this doesn't affect how Anthropic bills the session. */
|
|
@@ -2339,11 +2397,16 @@ export class ACPOrchestratorModel {
|
|
|
2339
2397
|
return data.includes(",") ? data.split(",")[1] : data; // data URL or raw base64
|
|
2340
2398
|
return null; // URL or other — not embeddable
|
|
2341
2399
|
}
|
|
2342
|
-
/** OpenCode's system text → `_meta.systemPrompt` per mode.
|
|
2343
|
-
|
|
2400
|
+
/** OpenCode's system text → `_meta.systemPrompt` per mode. `pointer` is a FIXED prompt
|
|
2401
|
+
* (basePrompt + the pointer) whatever OpenCode sends: the client's text travels in the first
|
|
2402
|
+
* message instead (see pointerBlocks). */
|
|
2403
|
+
buildSystemPromptMeta(prompt, ctrl) {
|
|
2404
|
+
const mode = systemModeOf(ctrl);
|
|
2344
2405
|
if (mode === "preset")
|
|
2345
2406
|
return undefined;
|
|
2346
|
-
|
|
2407
|
+
if (mode === "pointer")
|
|
2408
|
+
return [ctrl.basePrompt ?? BASE_PROMPT, POINTER_PROMPT].map((s) => s.trim()).filter(Boolean).join("\n\n");
|
|
2409
|
+
const text = sanitizeHarnessText(systemText(prompt));
|
|
2347
2410
|
if (!text)
|
|
2348
2411
|
return undefined;
|
|
2349
2412
|
return mode === "replace" ? text : { append: text };
|
|
@@ -2367,10 +2430,55 @@ export class ACPOrchestratorModel {
|
|
|
2367
2430
|
}
|
|
2368
2431
|
return out.join("\n");
|
|
2369
2432
|
}
|
|
2433
|
+
/** The agent's context was compacted, or rewound to an earlier point: it no longer holds the
|
|
2434
|
+
* operator's instructions as they are now. The next prompt restates them in full, and the
|
|
2435
|
+
* factual context goes out again. `contextUsed` is 0 until the agent reports again, so the
|
|
2436
|
+
* new occupancy is not read as one more drop. */
|
|
2437
|
+
forgetSystem(t, factual = true) {
|
|
2438
|
+
t.lastSystem = undefined;
|
|
2439
|
+
t.sysHash = undefined;
|
|
2440
|
+
t.resumed = true;
|
|
2441
|
+
// A revert keeps the factual context as it was (it is still in the transcript before the cut):
|
|
2442
|
+
// resending it there would change the prompt of every smartRevert in the other modes.
|
|
2443
|
+
if (factual)
|
|
2444
|
+
t.lastContextBlock = undefined;
|
|
2445
|
+
t.contextUsed = 0;
|
|
2446
|
+
}
|
|
2447
|
+
/** `pointer` mode: the client's whole system prompt is the operator's instructions, and it
|
|
2448
|
+
* reaches the agent as user content — in full on the first prompt
|
|
2449
|
+
* (`<operator_instructions>`; restated as a `<system-update>` on a resumed session, whose
|
|
2450
|
+
* transcript holds only the earlier copy), then as `<system-update>` diffs. The session's own
|
|
2451
|
+
* system prompt never changes, so it is the same for every session (see buildSystemPromptMeta). */
|
|
2452
|
+
pointerBlocks(t, prompt) {
|
|
2453
|
+
const full = sanitizeHarnessText(systemText(prompt)).trim();
|
|
2454
|
+
const before = t.lastSystem;
|
|
2455
|
+
t.lastSystem = full;
|
|
2456
|
+
if (before === undefined) {
|
|
2457
|
+
if (!full)
|
|
2458
|
+
return [];
|
|
2459
|
+
const sysHash = hashText(full);
|
|
2460
|
+
// A resumed session whose transcript already holds these very instructions: nothing to send.
|
|
2461
|
+
if (t.resumed && t.sysHash === sysHash)
|
|
2462
|
+
return [];
|
|
2463
|
+
t.sysHash = sysHash;
|
|
2464
|
+
const text = t.resumed
|
|
2465
|
+
? `<system-update>\nThe operator's instructions for this session, restated. From now on they read:\n${full}\n</system-update>`
|
|
2466
|
+
: `<operator_instructions>\n${full}\n</operator_instructions>`;
|
|
2467
|
+
return [{ type: "text", text }];
|
|
2468
|
+
}
|
|
2469
|
+
if (full === before)
|
|
2470
|
+
return [];
|
|
2471
|
+
t.sysHash = hashText(full);
|
|
2472
|
+
dbg(`operator instructions changed — system-update (${full.length} chars)`);
|
|
2473
|
+
return [{ type: "text", text: systemUpdate(before, full) }];
|
|
2474
|
+
}
|
|
2370
2475
|
/** Send the context block once per session, then only what changed. Appending a
|
|
2371
2476
|
* small delta at the END of the conversation is cache-safe (the cached prefix is
|
|
2372
2477
|
* untouched); resending the whole block every turn would not be. */
|
|
2373
2478
|
contextBlocks(t, prompt, ctrl) {
|
|
2479
|
+
// Pointer mode carries the whole system prompt already: no env extraction, no forwardEnvContext.
|
|
2480
|
+
if (systemModeOf(ctrl) === "pointer")
|
|
2481
|
+
return this.pointerBlocks(t, prompt);
|
|
2374
2482
|
if (ctrl.forwardEnvContext === false)
|
|
2375
2483
|
return [];
|
|
2376
2484
|
const block = this.extractContextBlock(prompt);
|