@0perator/operator 0.1.336 → 0.1.337

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.
@@ -5,7 +5,6 @@
5
5
  import { readFileSync, appendFileSync, mkdirSync } from "node:fs";
6
6
  import { join, dirname } from "node:path";
7
7
  import { operatorPath } from "./home.js";
8
- import { scratchDirFor } from "./scratch.js";
9
8
  import { renderHarnessContext } from "./operator-harness.js";
10
9
  import { renderToolCatalogue } from "../tools/registry.js";
11
10
  import { renderSkillsIndex } from "./skills-index.js";
@@ -206,8 +205,8 @@ export function basePersona(cwd = process.cwd(), opts = {}) {
206
205
  `find, and verify by running.) I record the non-obvious WHY in the repo's _ai-docs/, and commit clean: ` +
207
206
  `a conventional \`type(scope): subject\` with explicit paths (never a blanket add in a shared checkout) ` +
208
207
  `and no AI / Co-Authored-By / Claude-Session trailers.` +
209
- `\n## This machine\nWorking dir: ${cwd}` +
210
- `\nScratch dir: ${scratchDirFor(cwd)} (also \`$OPERATOR_SCRATCH\` in every shell) — temporary scripts, probe ` +
208
+ `\n## This machine\nWorking dir and scratch dir: named in the session context at the top of the conversation.` +
209
+ `\nScratch dir (also \`$OPERATOR_SCRATCH\` in every shell) — temporary scripts, probe ` +
211
210
  `output, downloads to inspect, intermediate files: ALWAYS here, never /tmp and never the repo root. It is ` +
212
211
  `git-ignored and swept after a week, so nothing you keep belongs in it; name files \`<subject>-<thing>\`.` +
213
212
  ctxLine +
@@ -89,12 +89,35 @@ export function sanitizeToolCallArgs(messages) {
89
89
  });
90
90
  return changed ? out : messages;
91
91
  }
92
+ // Per-session facts (working dir, scratch dir) ride between these tags in the system prompt string, and
93
+ // placeSessionContext moves them onto the first user message. Chat templates render tool schemas AFTER the
94
+ // system text, so anything that differs between sessions inside the system message makes the server re-prefill
95
+ // every tool schema (~20K tokens) for each new session or delegated sub-agent. Out of it, [system + tools] is
96
+ // byte-identical across sessions on the same model and stays in the server's prompt cache.
97
+ export const SESSION_CONTEXT_OPEN = "\n<session-context>\n";
98
+ export const SESSION_CONTEXT_CLOSE = "\n</session-context>";
99
+ /** Moves the tagged session context out of the system prompt and in front of the first user message. Text
100
+ * appended after the closing tag (a think directive, a turn directive, MOA drafts) stays in the system prompt.
101
+ * With no user message to carry it, the system prompt is returned untouched. */
102
+ export function placeSessionContext(systemPrompt, conversation) {
103
+ const open = systemPrompt?.indexOf(SESSION_CONTEXT_OPEN) ?? -1;
104
+ const close = open >= 0 ? systemPrompt.indexOf(SESSION_CONTEXT_CLOSE, open) : -1;
105
+ const first = conversation.findIndex((m) => m.role === "user");
106
+ if (close < 0 || first < 0)
107
+ return { system: systemPrompt, conversation };
108
+ const sys = systemPrompt;
109
+ const context = sys.slice(open + SESSION_CONTEXT_OPEN.length, close);
110
+ const system = sys.slice(0, open) + sys.slice(close + SESSION_CONTEXT_CLOSE.length);
111
+ const carried = conversation.map((m, i) => i === first ? { ...m, content: `<session-context>\n${context}\n</session-context>\n\n${m.content}` } : m);
112
+ return { system: system || undefined, conversation: carried };
113
+ }
92
114
  /** Prepends the system prompt (if any), repairs interrupted tool pairs, neutralizes any unparseable tool-call
93
115
  * arguments (so a poisoned history never 500s the server), and validates the result. */
94
116
  export function buildMessages(systemPrompt, conversation) {
95
- const withSystem = systemPrompt
96
- ? [{ role: "system", content: systemPrompt }, ...conversation]
97
- : [...conversation];
117
+ const placed = placeSessionContext(systemPrompt, conversation);
118
+ const withSystem = placed.system
119
+ ? [{ role: "system", content: placed.system }, ...placed.conversation]
120
+ : [...placed.conversation];
98
121
  const messages = sanitizeToolCallArgs(repairToolPairs(withSystem));
99
122
  assertValidMessages(messages);
100
123
  return messages;
package/dist/session.js CHANGED
@@ -3,7 +3,8 @@
3
3
  // that maps these callbacks onto React state; a test can drive the same `send()` directly.
4
4
  import { runTurn, TurnAbortedError } from "./engine/agent.js";
5
5
  import { recordTruncation } from "./observability/blackbox.js";
6
- import { Conversation } from "./engine/messages.js";
6
+ import { Conversation, placeSessionContext, SESSION_CONTEXT_OPEN, SESSION_CONTEXT_CLOSE } from "./engine/messages.js";
7
+ import { scratchDirFor } from "./config/scratch.js";
7
8
  import { chat, health, requiresOperatorToken, operatorTrustedUrl } from "./engine/aurora.js";
8
9
  import { compact, fitToWindow, turnStarts, SUMMARY_INSTRUCTION, stripTrailingOrphanToolCall, pickSummaryContent, renderAnchorIndex, verbatimUserTail, applyAnchorVerification } from "./engine/compaction.js";
9
10
  import { globalTokenMeter, endpointKey } from "./engine/token-meter.js";
@@ -492,9 +493,15 @@ export class Session {
492
493
  const name = p ? `${p.label}${p.params ? ` ${p.params}` : ""}` : this.modelId;
493
494
  return `\n\n## Active model\nThis session is served by ${name}${p?.box ? ` on ${p.box}` : ""} (model id \`${this.modelId}\`). ` +
494
495
  `This is authoritative: it overrides any model named elsewhere in this prompt or in project files, and it changes ` +
495
- `when the owner runs /model. Asked which model you run on, say you are Operator running on ${name}.` +
496
- `\n\n## Working directory\n${this.cwd ?? process.cwd()}\nRelative paths and shell commands resolve here. This is ` +
497
- `authoritative over any working dir named elsewhere in this prompt; create files here unless the owner names another path.`;
496
+ `when the owner runs /model. Asked which model you run on, say you are Operator running on ${name}.`;
497
+ }
498
+ /** This session's working and scratch dirs, tagged so buildMessages carries them on the first user message
499
+ * rather than in the system prompt (see engine/messages.ts SESSION_CONTEXT_OPEN for why). */
500
+ sessionContextBlock() {
501
+ const cwd = this.cwd ?? process.cwd();
502
+ return `${SESSION_CONTEXT_OPEN}Working directory: ${cwd}\nScratch dir: ${scratchDirFor(cwd)}\nRelative paths and shell ` +
503
+ `commands resolve in the working directory. This is authoritative over any working dir named elsewhere; create ` +
504
+ `files there unless the owner names another path.${SESSION_CONTEXT_CLOSE}`;
498
505
  }
499
506
  /** The effective system prompt: the base persona plus any memory recalled this session, plus any
500
507
  * active skill. Injected as ONE system prompt — never a second system message, which 400s llama.cpp's
@@ -505,7 +512,7 @@ export class Session {
505
512
  : this.systemPrompt;
506
513
  if (this.activeSkillBlock)
507
514
  base = (base ?? "") + this.activeSkillBlock;
508
- base = (base ?? "") + this.activeModelBlock();
515
+ base = (base ?? "") + this.activeModelBlock() + this.sessionContextBlock();
509
516
  if (!this.planMode)
510
517
  return base;
511
518
  // Plan mode: the model researches read-only and proposes a plan; the read-only enforcement is in
@@ -895,10 +902,10 @@ export class Session {
895
902
  // message. The server prefix-matches to the end of span and prefills only the instruction, instead of
896
903
  // re-prefilling the whole span from scratch (the old system-less/tool-less summaryPrompt path did the
897
904
  // latter — the documented cost trap). Empty content falls back to Aurora's reasoning channel.
898
- const sys = this.lastSys ?? this.sys();
905
+ const placed = placeSessionContext(this.lastSys ?? this.sys(), stripTrailingOrphanToolCall(span));
899
906
  const messages = [
900
- ...(sys ? [{ role: "system", content: sys }] : []),
901
- ...stripTrailingOrphanToolCall(span),
907
+ ...(placed.system ? [{ role: "system", content: placed.system }] : []),
908
+ ...placed.conversation,
902
909
  { role: "user", content: SUMMARY_INSTRUCTION },
903
910
  ];
904
911
  // A small-context active model (e.g. a self-hosted 8K Venice) can't summarize its OWN overflow — the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0perator/operator",
3
- "version": "0.1.336",
3
+ "version": "0.1.337",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "operator": "dist/cli.js",