aegiscode 6.8.21 → 6.8.23

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "aegiscode",
3
3
  "productName": "AEGIS Code",
4
- "version": "6.8.21",
4
+ "version": "6.8.23",
5
5
  "description": "aegiscode — the command-line version of AEGIS Desktop. The shared tool surface in your shell, over the same thin transport and tool registry as the MCP plugin and the desktop app. Ships transport + UI only; no brain.",
6
6
  "author": {
7
7
  "name": "AEGIS Code",
package/src/app.js CHANGED
@@ -21,7 +21,7 @@
21
21
  const readline = require('node:readline');
22
22
  const os = require('node:os');
23
23
  const { randomUUID } = require('node:crypto');
24
- const { createTools, createClient, usageTokens, usageBuckets, buildSystemPrompt, autoProvisionApiKey } = require('./deps.js');
24
+ const { createTools, createClient, usageTokens, usageBuckets, buildSystemPrompt, autoProvisionApiKey, hostEnv } = require('./deps.js');
25
25
  const { createEngine, HOST_CLASSES, DEFAULT_CLASS, byokNamespace } = require('./engine.js');
26
26
  const { GLYPH, VERBS, themeOf, RESET, THEME_TABLE, AUTO_THEME_INDEX, themeIsLight, reconcileThemeIndex } = require('./theme.js');
27
27
  const { probeTerminalBackground } = require('./caps.js');
@@ -66,12 +66,13 @@ let _defaultSystem = null;
66
66
  function defaultSystemPrompt() {
67
67
  if (_defaultSystem === null) {
68
68
  try {
69
- _defaultSystem = buildSystemPrompt({
70
- platform: process.platform,
71
- arch: process.arch,
72
- homedir: os.homedir(),
73
- cwd: process.cwd(),
74
- });
69
+ // The SAME environment block the desktop engine builds (engine.js
70
+ // hostEnv): platform, the real home and Desktop directories, the working
71
+ // directory, and — the field this host was missing — the shell `exec`
72
+ // really runs in. Building a three-field subset here is what let a
73
+ // Windows CLI session be told it was on linux and reach for `ls`, `cat`
74
+ // and `mkdir -p`: nothing in the prompt said otherwise.
75
+ _defaultSystem = buildSystemPrompt(hostEnv({}, {}, null));
75
76
  } catch {
76
77
  _defaultSystem = ''; // never let persona construction break a turn
77
78
  }
package/src/commands.js CHANGED
@@ -1432,6 +1432,17 @@ const COMMANDS = [
1432
1432
  const audit = auditExposure(dataDir);
1433
1433
  if (!audit.exists) {
1434
1434
  ok('data dir', true, `${dataDir} (not created yet)`);
1435
+ } else if (!audit.modesMeaningful) {
1436
+ // Windows: fs.stat's mode is synthesised from the read-only attribute, so
1437
+ // there are no group/other bits to audit (securefs.js hasPosixModes).
1438
+ // Saying so beats the old output, which listed every file in the store as
1439
+ // world-accessible and then could not fix a single one.
1440
+ ok(
1441
+ 'data dir',
1442
+ true,
1443
+ `${dataDir} (${audit.total} path(s)) — this filesystem has no POSIX permission bits; ` +
1444
+ 'access is governed by the profile directory\'s own ACL',
1445
+ );
1435
1446
  } else {
1436
1447
  const shown = audit.exposed.slice(0, 4).map((e) => `${e.rel} ${e.mode}`).join(', ');
1437
1448
  ok(
@@ -1443,7 +1454,9 @@ const COMMANDS = [
1443
1454
  `${shown}${audit.exposed.length > 4 ? ', …' : ''} — run /doctor fix`
1444
1455
  );
1445
1456
  }
1446
- if (repaired) {
1457
+ if (repaired && !repaired.modesMeaningful) {
1458
+ ok('repair', true, 'nothing to tighten — this filesystem has no POSIX permission bits');
1459
+ } else if (repaired) {
1447
1460
  const sample = repaired.fixed
1448
1461
  .slice(0, 3)
1449
1462
  .map((f) => `${f.rel} ${f.from}→${f.to}`)
package/src/deps.js CHANGED
@@ -71,7 +71,7 @@ const autoProvisionPath = resolveShared(path.join('client', 'auto-provision.js')
71
71
  const { createClient } = require(clientPath);
72
72
  const { createTools } = require(toolsPath);
73
73
  const { usageTokens, usageBuckets } = require(usagePath);
74
- const { createLocalEngine } = require(enginePath);
74
+ const { createLocalEngine, hostEnv } = require(enginePath);
75
75
  const { createSettingsStore, isReservedNamespace } = require(settingsPath);
76
76
  const { agentRoles, agentRoleLabel } = require(agentsPath);
77
77
  const { buildSystemPrompt } = require(promptPath);
@@ -87,6 +87,11 @@ module.exports = {
87
87
  usageTokens,
88
88
  usageBuckets,
89
89
  createLocalEngine,
90
+ // The environment block the two hosts must agree on (see engine.js hostEnv).
91
+ // Re-exported so app.js builds its persona from the SAME function the desktop
92
+ // engine uses, instead of a three-field subset that omitted the shell dialect
93
+ // and the user's real Desktop.
94
+ hostEnv,
90
95
  createSettingsStore,
91
96
  isReservedNamespace,
92
97
  agentRoles,
package/src/securefs.js CHANGED
@@ -51,6 +51,27 @@ const path = require('node:path');
51
51
  // rename protections (which do not depend on it) in place.
52
52
  const NOFOLLOW = fs.constants.O_NOFOLLOW || 0;
53
53
 
54
+ /**
55
+ * Do `fs.stat().mode` permission bits mean anything on this host?
56
+ *
57
+ * Windows: no. libuv has no NTFS ACL to report, so it synthesises the mode from
58
+ * the read-only attribute and returns 0o666 for every writable file and
59
+ * directory. `exposedBits()` below is therefore ALWAYS true there, which made
60
+ * `/doctor` report the entire data directory as group/world-accessible and
61
+ * `/doctor fix` report it as still exposed after "repairing" it — a permanent
62
+ * false alarm with no action that could clear it. The boundary on Windows is the
63
+ * profile directory's ACL (see client/authorship-key.js, which has always
64
+ * skipped its own mode assert there).
65
+ *
66
+ * Duplicated from client/platform.js `hasPosixModes()` rather than imported
67
+ * because this module is deliberately dependency-free (see the header) so it can
68
+ * be required from anything without a cycle. Injectable, so both branches are
69
+ * exercised on one host.
70
+ */
71
+ function hasPosixModes(platform = process.platform) {
72
+ return platform !== 'win32';
73
+ }
74
+
54
75
  /** True when `st` is owned by the current user (always true where getuid is absent). */
55
76
  function ownedByUs(st) {
56
77
  if (typeof process.getuid !== 'function') return true;
@@ -89,7 +110,9 @@ function ensurePrivateDir(dir) {
89
110
  try {
90
111
  const st = fs.lstatSync(dir);
91
112
  if (st.isSymbolicLink() || !st.isDirectory()) return false;
92
- if (ownedByUs(st) && (st.mode & 0o077) !== 0) fs.chmodSync(dir, 0o700);
113
+ // Guarded on hasPosixModes: on Windows the test is always true, so this
114
+ // chmod'ed the data directory on literally every write and never changed it.
115
+ if (hasPosixModes() && ownedByUs(st) && (st.mode & 0o077) !== 0) fs.chmodSync(dir, 0o700);
93
116
  return true;
94
117
  } catch {
95
118
  return false;
@@ -238,8 +261,10 @@ function exposedBits(mode) {
238
261
  * entry of `exposed` is `{ rel, mode, kind }` — `rel` is `.` for the root
239
262
  * itself, and `kind` is `'dir'` or `'file'`.
240
263
  */
241
- function auditExposure(root) {
242
- const report = { root, exists: false, rootMode: null, exposed: [], total: 0 };
264
+ function auditExposure(root, options = {}) {
265
+ const platform = options.platform || process.platform;
266
+ const modesMeaningful = hasPosixModes(platform);
267
+ const report = { root, exists: false, rootMode: null, exposed: [], total: 0, modesMeaningful };
243
268
  let rst;
244
269
  try {
245
270
  rst = fs.lstatSync(root);
@@ -249,6 +274,13 @@ function auditExposure(root) {
249
274
  if (rst.isSymbolicLink() || !rst.isDirectory()) return report;
250
275
  report.exists = true;
251
276
  report.rootMode = fmtMode(rst.mode);
277
+ // Where the mode is synthesised there is nothing to audit: every path would be
278
+ // listed, none of it true. `modesMeaningful: false` is the honest answer and
279
+ // the caller renders it as "not applicable", never as "all clear".
280
+ if (!modesMeaningful) {
281
+ report.total = scanTree(root).length;
282
+ return report;
283
+ }
252
284
  if (exposedBits(rst.mode)) report.exposed.push({ rel: '.', mode: report.rootMode, kind: 'dir' });
253
285
 
254
286
  const items = scanTree(root);
@@ -274,8 +306,10 @@ function auditExposure(root) {
274
306
  * `remaining` is the exposure audit taken after the pass — non-empty only when
275
307
  * something could not be repaired.
276
308
  */
277
- function repairExposure(root) {
278
- const report = { root, exists: false, fixed: [], failed: [], remaining: [] };
309
+ function repairExposure(root, options = {}) {
310
+ const platform = options.platform || process.platform;
311
+ const modesMeaningful = hasPosixModes(platform);
312
+ const report = { root, exists: false, fixed: [], failed: [], remaining: [], modesMeaningful };
279
313
  let rst;
280
314
  try {
281
315
  rst = fs.lstatSync(root);
@@ -284,6 +318,9 @@ function repairExposure(root) {
284
318
  }
285
319
  if (rst.isSymbolicLink() || !rst.isDirectory()) return report;
286
320
  report.exists = true;
321
+ // A chmod here cannot change what stat reports, so the old code "fixed" every
322
+ // path and then re-audited it as still exposed, forever. Report a clean no-op.
323
+ if (!modesMeaningful) return report;
287
324
 
288
325
  const tighten = (full, st, rel) => {
289
326
  if (!ownedByUs(st)) {
@@ -305,7 +342,7 @@ function repairExposure(root) {
305
342
  tighten(it.full, it.st, it.rel);
306
343
  }
307
344
 
308
- report.remaining = auditExposure(root).exposed;
345
+ report.remaining = auditExposure(root, { platform }).exposed;
309
346
  return report;
310
347
  }
311
348
 
@@ -66,46 +66,309 @@ function envIdleMs(name) {
66
66
  }
67
67
 
68
68
  /**
69
- * DeepSeek's chat template wraps tool calls and turn boundaries in special
70
- * tokens — fullwidth pipe (U+FF5C) delimiting an identifier that uses ▁
71
- * (U+2581) as its word separator, e.g. |tool▁calls▁begin|, |tool▁call▁end|,
72
- * |Assistant|, |begin▁of▁sentence|. These are chat-template internals, never
73
- * user-facing text; when the provider's streaming tool-call path leaks them
74
- * into `delta.content` they used to reach the screen verbatim — reported as
75
- * the pasted "||DSML||" envelope on Windows/macOS. Returns a per-stream
76
- * stripper closure with a short lookback buffer so a token split across two
77
- * SSE chunks isn't half-rendered before its closing pipe arrives.
69
+ * DeepSeek emits its tool calls as DSML — an XML-ish dialect of its chat
70
+ * template that the OpenAI-compatible wire format does NOT structure for us:
71
+ *
72
+ * <|DSML|tool_calls>
73
+ * <|DSML|invoke name="write">
74
+ * <|DSML|parameter name="path" string="true">src/app.js</|DSML|parameter>
75
+ * <|DSML|parameter name="content" string="true">…the file body…</|DSML|parameter>
76
+ * </|DSML|invoke>
77
+ * </|DSML|tool_calls>
78
+ *
79
+ * It arrives as plain `delta.content` text, and that produces two failures:
80
+ * the markup rendering verbatim (reported as the pasted "||DSML||" envelope),
81
+ * and the call itself never running because text is not a tool call. Ported
82
+ * from aegiscodex-dev's src/backend.js, which carries the full history of this
83
+ * grammar's quirks (provider/template/model revision disagree about the exact
84
+ * identifier, angle brackets sometimes absent, tag names drift under
85
+ * sampling) — every part of the pattern below is deliberately optional and
86
+ * spacing-insensitive for that reason. `string="false"` marks a JSON
87
+ * parameter; `string="true"` (or an absent attribute) is the literal value,
88
+ * kept verbatim so a multi-line `content` is never mangled by a JSON
89
+ * round-trip.
90
+ */
91
+ // `\b` after each tag-name alternative matters: without it, "call" matches as
92
+ // a bare PREFIX of a drifted wrapper tag like "calls" (observed verbatim —
93
+ // `<|| calls>`, no "tool_"/"function_" prefix at all), swallowing everything
94
+ // up to the next real `</invoke>` as that spurious match's unnamed body and
95
+ // silently discarding a whole tool call with it. "call" and "calls" are both
96
+ // word characters, so plain alternation order cannot tell them apart.
97
+ const DSML_RE_INVOKE = /<\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call)\b\s*([^>]*?)\s*>([\s\S]*?)<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call)\b\s*>/g;
98
+ const DSML_RE_PARAM = /<\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*([^>]*?)\s*>([\s\S]*?)<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*>/g;
99
+ const DSML_RE_LEADER = /<\s*\/?\s*|\s*(?:DSML\s*)?|\s*(?:(?:tool|function)[_▁ ]*calls?|reasoning)[_▁ ]*(?:begin|end)?\s*>/gi;
100
+ const DSML_RE_ANY_TAG = /<\s*\/?\s*|\s*(?:DSML\s*)?|\s*[A-Za-z_][A-Za-z0-9_]*[^>]{0,120}>/g;
101
+ const DSML_RE_SKELETON = /<\s*\/?\s*(?:tool_calls|function_calls|invoke|parameter|param|reasoning)\b[^>]{0,120}>/gi;
102
+ const DSML_RE_JSON_CALLS = /|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*begin[ \t]*|([\s\S]*?)|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*end[ \t]*|/gi;
103
+ // The leader/closer halves of DSML_RE_JSON_CALLS above, split out so the hold
104
+ // logic can detect "the leader arrived but its closer has not" — an arbitrary
105
+ // JSON blob sits between them with no '<' or recognizable structure of its
106
+ // own, so nothing else below would otherwise hold it back.
107
+ const DSML_RE_JSON_CALLS_OPEN = /|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*begin[ \t]*|/gi;
108
+ const DSML_RE_JSON_CALLS_CLOSE = /|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*end[ \t]*|/gi;
109
+ const DSML_RE_ENVELOPE_TOKEN = /|[ \t]?[A-Za-z0-9_▁]*[ \t]?|/g;
110
+ const DSML_RE_DRIFT_INVOKE = /<\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call)\b\s*([^>]*?)\s*>/gi;
111
+ const DSML_RE_DRIFT_PARAM = /<\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*([^>]*?)\s*>/gi;
112
+ const DSML_RE_PARAM_CLOSE = /<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*>/gi;
113
+ const DSML_RE_ATTR = /([A-Za-z_][A-Za-z0-9_.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
114
+ const DSML_RE_STRUCT_TAG = /<\s*\/?\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call|parameter|param|arg|reasoning|(?:tool|function)[_▁ ]*calls?)\b/gi;
115
+ const DSML_RE_STRUCT_CLOSER = /<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call|parameter|param|arg|reasoning|(?:tool|function)[_▁ ]*calls?)\b/gi;
116
+ /**
117
+ * A COMPLETE (its own '>' has arrived) wrapper/leader tag that carries no
118
+ * call of its own — observed verbatim: a bare `<||calls>`, with neither a
119
+ * "tool_"/"function_" prefix (so DSML_RE_LEADER above misses it) nor a JSON
120
+ * envelope pair. Safe to drop the moment it closes, UNLIKE invoke/parameter —
121
+ * excluded here via negative lookahead so a *partial* invoke/parameter head is
122
+ * never erased before its own close has a chance to pair it up with
123
+ * DSML_RE_INVOKE/DSML_RE_PARAM on a later call once more of it has arrived.
124
+ */
125
+ const DSML_RE_WRAPPER_TAG = /<\s*\/?\s*|\s*(?:DSML\s*)?|\s*(?!(?:invoke|call|tool_call|parameter|param|arg)\b)[A-Za-z_][A-Za-z0-9_]*[^>]{0,120}>/g;
126
+ /** A '<' (or '</') at the tail: the head of a tag whose pipe is still in flight. */
127
+ const DSML_RE_TRAILING_LT = /<\s*\/?\s*$/;
128
+ /** An in-flight identifier token: a pipe whose partner has not arrived yet. */
129
+ const DSML_RE_OPEN_TOKEN = /^|[ \t]?[A-Za-z0-9_▁]*[ \t]?$/;
130
+ /** Past this much buffered markup, a held tail is released as-is. */
131
+ const DSML_HOLD_MAX = 4 * 1024 * 1024;
132
+
133
+ /** The attributes of one tag head, lower-cased: name="write" → { name: 'write' }. */
134
+ function dsmlAttrs(text) {
135
+ const out = {};
136
+ if (!text) return out;
137
+ DSML_RE_ATTR.lastIndex = 0;
138
+ let m;
139
+ while ((m = DSML_RE_ATTR.exec(text))) out[m[1].toLowerCase()] = m[2] != null ? m[2] : m[3];
140
+ return out;
141
+ }
142
+
143
+ /** A JSON parameter — or, when it is not JSON after all, the text it came as. */
144
+ function dsmlJson(raw) {
145
+ const s = String(raw == null ? '' : raw).trim();
146
+ try {
147
+ const v = JSON.parse(s);
148
+ return v === undefined ? s : v;
149
+ } catch {
150
+ return s;
151
+ }
152
+ }
153
+
154
+ /** The `parameter` tags of one `invoke` body, as a tool call's args object. */
155
+ function dsmlArgs(body) {
156
+ const args = {};
157
+ DSML_RE_PARAM.lastIndex = 0;
158
+ let p;
159
+ while ((p = DSML_RE_PARAM.exec(body))) {
160
+ const a = dsmlAttrs(p[1]);
161
+ const key = a.name || a.key || `arg${Object.keys(args).length}`;
162
+ const isJson = String(a.string || '').toLowerCase() === 'false' || String(a.json || '').toLowerCase() === 'true';
163
+ args[key] = isJson ? dsmlJson(p[2]) : p[2];
164
+ }
165
+ return args;
166
+ }
167
+
168
+ /**
169
+ * Decode every DSML tool call in `text` and return the two things a caller
170
+ * needs: what may be shown, and what must be executed — `{ text, calls }`.
171
+ * The generic `|…|` tokens are deliberately left in place here: an `invoke`
172
+ * block still arriving is identified by its delimiters on the next pass, and
173
+ * eating them early would lose the call's name and arguments. They are
174
+ * removed from the *released* text instead (see dsmlResidue).
175
+ */
176
+ function decodeDeepSeekDSML(input) {
177
+ if (typeof input !== 'string' || !input || input.indexOf('|') === -1) {
178
+ return { text: input || '', calls: [] };
179
+ }
180
+ const calls = [];
181
+ let text = input;
182
+
183
+ // 1. One `invoke` block per call, the well-formed spelling.
184
+ DSML_RE_INVOKE.lastIndex = 0;
185
+ const spans = [];
186
+ let m;
187
+ while ((m = DSML_RE_INVOKE.exec(text))) {
188
+ const a = dsmlAttrs(m[1]);
189
+ const name = a.name || a.function || a.tool || '';
190
+ if (name) calls.push({ name, args: dsmlArgs(m[2]) });
191
+ spans.push([m.index, m.index + m[0].length]);
192
+ }
193
+ for (let i = spans.length - 1; i >= 0; i--) {
194
+ text = text.slice(0, spans[i][0]) + text.slice(spans[i][1]);
195
+ }
196
+
197
+ // 2. The older spelling: a JSON array wrapped in envelope tokens.
198
+ text = text.replace(DSML_RE_JSON_CALLS, (full, body) => {
199
+ const parsed = dsmlJson(body);
200
+ const list = Array.isArray(parsed) ? parsed : (parsed && Array.isArray(parsed.tool_calls) ? parsed.tool_calls : []);
201
+ for (const c of list) {
202
+ const fn = (c && c.function) || c || {};
203
+ const name = fn.name || (c && c.name) || '';
204
+ if (!name) continue;
205
+ let args = fn.arguments != null ? fn.arguments : (c && c.arguments);
206
+ if (typeof args === 'string') args = dsmlJson(args);
207
+ calls.push({ name, args: args && typeof args === 'object' ? args : {} });
208
+ }
209
+ return '';
210
+ });
211
+
212
+ // 3. The wrapper tags carry no call of their own.
213
+ text = text.replace(DSML_RE_LEADER, '');
214
+ return { text, calls };
215
+ }
216
+
217
+ /**
218
+ * Last-resort decode for markup that drifted out of well-formedness: a missing
219
+ * closing tag, or a stream cut mid-call. The call still names a tool and its
220
+ * parameters are still readable, so it is decoded rather than deleted — a call
221
+ * that reached the wire must not vanish because its closing tag did.
222
+ */
223
+ function decodeDeepSeekDSMLDrift(input) {
224
+ if (typeof input !== 'string' || !input || input.indexOf('|') === -1) return { text: input || '', calls: [] };
225
+ const opens = [];
226
+ DSML_RE_DRIFT_INVOKE.lastIndex = 0;
227
+ let m;
228
+ while ((m = DSML_RE_DRIFT_INVOKE.exec(input))) {
229
+ opens.push({ at: m.index, attrs: m[1], body: DSML_RE_DRIFT_INVOKE.lastIndex });
230
+ }
231
+ if (!opens.length) return { text: input, calls: [] };
232
+ const calls = [];
233
+ let text = '';
234
+ let cursor = 0;
235
+ for (let i = 0; i < opens.length; i++) {
236
+ // One call runs to the next `invoke` head, or to the end of the text.
237
+ const end = i + 1 < opens.length ? opens[i + 1].at : input.length;
238
+ const body = input.slice(opens[i].body, end);
239
+ const a = dsmlAttrs(opens[i].attrs);
240
+ const name = a.name || a.function || a.tool || '';
241
+ const args = {};
242
+ const params = [];
243
+ DSML_RE_DRIFT_PARAM.lastIndex = 0;
244
+ let pm;
245
+ while ((pm = DSML_RE_DRIFT_PARAM.exec(body))) {
246
+ params.push({ at: pm.index, attrs: pm[1], from: DSML_RE_DRIFT_PARAM.lastIndex });
247
+ }
248
+ for (let k = 0; k < params.length; k++) {
249
+ // A value runs to the next parameter tag — its closing tag may be the
250
+ // thing that never arrived.
251
+ const stop = k + 1 < params.length ? params[k + 1].at : body.length;
252
+ const raw = body.slice(params[k].from, stop).replace(DSML_RE_PARAM_CLOSE, '');
253
+ const pa = dsmlAttrs(params[k].attrs);
254
+ const key = pa.name || pa.key || `arg${Object.keys(args).length}`;
255
+ const isJson = String(pa.string || '').toLowerCase() === 'false';
256
+ args[key] = isJson ? dsmlJson(raw) : raw;
257
+ }
258
+ text += input.slice(cursor, opens[i].at);
259
+ cursor = end;
260
+ if (name) calls.push({ name, args });
261
+ }
262
+ text += input.slice(cursor);
263
+ return { text, calls };
264
+ }
265
+
266
+ /**
267
+ * Residue removal for text that is about to be shown. Two spellings have to go:
268
+ * a leftover envelope token (`|DSML|`, `|tool▁calls▁begin|`, `|Assistant|`),
269
+ * and the skeleton an earlier release left behind by stripping the delimiters
270
+ * out of a DSML block — `<tool_calls> <invoke name="write"> <parameter …>`,
271
+ * which is exactly what a user pasted back at us.
272
+ */
273
+ function dsmlResidue(text) {
274
+ if (!text) return text;
275
+ if (text.indexOf('|') === -1) return text.replace(DSML_RE_SKELETON, '');
276
+ return text.replace(DSML_RE_ANY_TAG, '').replace(DSML_RE_SKELETON, '').replace(DSML_RE_ENVELOPE_TOKEN, '');
277
+ }
278
+
279
+ /**
280
+ * Is the buffer, read from the first `|` in it, a construct that is still
281
+ * arriving? Only two shapes may be held back: a pipe whose partner has not
282
+ * arrived, and a DSML opener whose matching close has not landed. Everything
283
+ * else is prose — a `|` in an answer is a character, not a promise of markup,
284
+ * and holding the rest of the answer behind it would swallow the reply.
285
+ */
286
+ function dsmlUnterminated(s) {
287
+ if (DSML_RE_OPEN_TOKEN.test(s)) return true;
288
+ const openers = (s.match(DSML_RE_STRUCT_TAG) || []).length;
289
+ const closers = (s.match(DSML_RE_STRUCT_CLOSER) || []).length;
290
+ return openers > closers;
291
+ }
292
+
293
+ /**
294
+ * Where, if anywhere, an in-flight DSML construct still needs the rest of the
295
+ * buffer held back from view. By the time this runs, decodeDeepSeekDSML() has
296
+ * already removed every COMPLETE invoke/parameter span and the caller has
297
+ * already removed every complete non-invoke wrapper tag (DSML_RE_WRAPPER_TAG),
298
+ * so whatever is left can only be: clean prose, or the start of exactly ONE
299
+ * incomplete construct running to the end of the buffer — an opener whose own
300
+ * '|' has arrived but not its '>' (`<\s*\/?\s*|`, found wherever it starts,
301
+ * not just at the tail: a Write's `content` can be arbitrarily long, so the
302
+ * unresolved opener is usually far behind the cursor, not at it), a '<' that
303
+ * arrived with no '|' yet (the chunk boundary landed first), or a bare pipe
304
+ * token still accumulating its identifier (the plain envelope-leak case, e.g.
305
+ * '|tool▁cal'). Returns -1 when none apply — safe to release in full.
306
+ */
307
+ function dsmlHoldFrom(buf) {
308
+ const tagStart = buf.search(/<\s*\/?\s*|/);
309
+ if (tagStart !== -1) return tagStart;
310
+ // The older JSON-array envelope (DSML_RE_JSON_CALLS): once its leader token
311
+ // has arrived, hold everything until the matching closer token lands. An
312
+ // arbitrary JSON blob sits between them with no '<' and no recognizable
313
+ // per-character structure, so without this check it streams straight
314
+ // through as visible prose the instant the leader's own closing '|'
315
+ // arrives — the hold rule below only looks at ONE pipe token at a time and
316
+ // has no notion of "waiting for a second, different token later on".
317
+ const jsonOpenIdx = buf.search(DSML_RE_JSON_CALLS_OPEN);
318
+ if (jsonOpenIdx !== -1 && buf.slice(jsonOpenIdx).search(DSML_RE_JSON_CALLS_CLOSE) === -1) {
319
+ return jsonOpenIdx;
320
+ }
321
+ const lt = DSML_RE_TRAILING_LT.exec(buf);
322
+ if (lt) return lt.index;
323
+ const openIdx = buf.indexOf('|');
324
+ if (openIdx !== -1 && dsmlUnterminated(buf.slice(openIdx))) return openIdx;
325
+ return -1;
326
+ }
327
+
328
+ /**
329
+ * The per-stream stripper the transports share. Feed it each `delta.content`
330
+ * chunk: it returns the text that is safe to show, holds back whatever is
331
+ * still arriving (a Write's `content` parameter is a whole file — it belongs
332
+ * to the call, not to the screen), and collects the tool calls the model made
333
+ * in text form. `flush()` releases the tail once the stream is drained, and
334
+ * `calls()` returns everything decoded so far, which the caller merges into
335
+ * the turn's tool calls so a Write that arrived as DSML is executed like any
336
+ * other.
78
337
  */
79
- const DEEPSEEK_ENVELOPE_RE = /|[A-Za-z][A-Za-z0-9]*(?:▁[A-Za-z0-9]+)*|/g;
80
338
  function makeDeepSeekEnvelopeStripper() {
81
339
  let buf = '';
340
+ const calls = [];
82
341
  const strip = (chunk) => {
83
- buf += chunk;
84
- buf = buf.replace(DEEPSEEK_ENVELOPE_RE, '');
85
- const openIdx = buf.lastIndexOf('|');
86
- if (openIdx !== -1) {
87
- const tail = buf.slice(openIdx);
88
- // Held back only while the tail still looks like an in-progress token
89
- // (no closing pipe yet); a bare '|' or genuine prose flushes normally.
90
- if (tail.length <= 60 && /^|[A-Za-z0-9▁]*$/.test(tail)) {
91
- const out = buf.slice(0, openIdx);
92
- buf = tail;
93
- return out;
94
- }
342
+ buf += typeof chunk === 'string' ? chunk : '';
343
+ const dec = decodeDeepSeekDSML(buf);
344
+ buf = dec.text;
345
+ for (const c of dec.calls) calls.push(c);
346
+ buf = buf.replace(DSML_RE_WRAPPER_TAG, '');
347
+ const hold = dsmlHoldFrom(buf);
348
+ let out;
349
+ if (hold !== -1 && buf.length - hold <= DSML_HOLD_MAX) {
350
+ out = buf.slice(0, hold);
351
+ buf = buf.slice(hold);
352
+ } else {
353
+ out = buf;
354
+ buf = '';
95
355
  }
96
- const out = buf;
97
- buf = '';
98
- return out;
356
+ return dsmlResidue(out);
99
357
  };
100
358
  // Unconditional release of whatever is left buffered — called once the
101
- // stream is fully drained, when a held-back '|…' is known to never close
102
- // (dropped connection, or it was real prose all along) rather than an
359
+ // stream is fully drained, when a held-back construct is known to never
360
+ // close (dropped connection, or it was real prose all along) rather than an
103
361
  // in-progress token.
104
362
  strip.flush = () => {
105
- const out = buf;
363
+ const dec = decodeDeepSeekDSML(buf);
106
364
  buf = '';
107
- return out;
365
+ for (const c of dec.calls) calls.push(c);
366
+ const drift = decodeDeepSeekDSMLDrift(dec.text);
367
+ for (const c of drift.calls) calls.push(c);
368
+ return dsmlResidue(drift.text);
108
369
  };
370
+ /** Every tool call decoded from text so far, as `{ name, args }`. */
371
+ strip.calls = () => calls.slice();
109
372
  return strip;
110
373
  }
111
374
 
@@ -678,7 +941,7 @@ function createClient(opts = {}) {
678
941
  else if (mode) body.mode = mode;
679
942
  const headers = authHeaders(agentKey ? { 'X-AEGIS-Agent-Key': agentKey } : undefined);
680
943
  if (!stream || typeof onStream !== 'function') {
681
- return apiPost('/api/v1/chat/completions', { ...body, stream: false }, headers);
944
+ return decodeDsmlInResult(await apiPost('/api/v1/chat/completions', { ...body, stream: false }, headers));
682
945
  }
683
946
  // An OpenAI-compatible SSE response carries no `usage` unless the caller
684
947
  // asks for it (`stream_options.include_usage`). Without this the streamed
@@ -784,7 +1047,7 @@ function createClient(opts = {}) {
784
1047
  // billable traffic instead of a free ride.
785
1048
  if (apiKey) headers['X-AEGIS-Key'] = apiKey;
786
1049
  if (!stream || typeof onStream !== 'function') {
787
- return apiPost('/api/v1/byok/chat/completions', { ...body, stream: false }, headers);
1050
+ return decodeDsmlInResult(await apiPost('/api/v1/byok/chat/completions', { ...body, stream: false }, headers));
788
1051
  }
789
1052
  return postStream('/api/v1/byok/chat/completions', body, headers, onStream, signal);
790
1053
  }
@@ -796,6 +1059,31 @@ function createClient(opts = {}) {
796
1059
  return typeof content === 'string' ? content : '';
797
1060
  }
798
1061
 
1062
+ /**
1063
+ * One-shot DSML decode for a non-streamed completion: the whole answer
1064
+ * already arrived in one JSON payload, so there is no partial-token hold-
1065
+ * back to do — just decode, drift-decode what the well-formed pass missed,
1066
+ * and strip whatever residue would otherwise render as garble. Mutates and
1067
+ * returns `data` so every non-stream return site in this module can wrap
1068
+ * its result the same way the streaming path's `message.content` is clean.
1069
+ */
1070
+ function decodeDsmlInResult(data) {
1071
+ const choice = data && data.choices && data.choices[0];
1072
+ const content = choice && choice.message && choice.message.content;
1073
+ if (typeof content !== 'string' || content.indexOf('|') === -1) return data;
1074
+ const once = decodeDeepSeekDSML(content);
1075
+ const drift = decodeDeepSeekDSMLDrift(once.text);
1076
+ choice.message.content = dsmlResidue(drift.text);
1077
+ const calls = [...once.calls, ...drift.calls];
1078
+ if (calls.length) {
1079
+ data.toolCalls = [
1080
+ ...(Array.isArray(data.toolCalls) ? data.toolCalls : []),
1081
+ ...calls.map((c) => ({ id: '', name: c.name, args: c.args || {} })),
1082
+ ];
1083
+ }
1084
+ return data;
1085
+ }
1086
+
799
1087
  /**
800
1088
  * POST body with `stream: true` and forward SSE deltas to onStream.
801
1089
  * Shared by the AEGIS pool and the BYOK relay so every host parses exactly
@@ -881,7 +1169,7 @@ function createClient(opts = {}) {
881
1169
  // request with plain JSON. Dropping the signal here meant an aborted turn
882
1170
  // kept waiting on a request nobody was listening for any more — the
883
1171
  // caller had already moved on, and the host held the socket.
884
- const data = await apiPost(path, { ...cleanBody, stream: false }, headers, { signal });
1172
+ const data = decodeDsmlInResult(await apiPost(path, { ...cleanBody, stream: false }, headers, { signal }));
885
1173
  const fullText = textOf(data);
886
1174
  if (fullText) onStream({ delta: fullText });
887
1175
  return data;
@@ -890,7 +1178,7 @@ function createClient(opts = {}) {
890
1178
  const contentType = res.headers.get('content-type') || '';
891
1179
  if (!contentType.includes('text/event-stream')) {
892
1180
  // Server ignored the stream flag and answered with plain JSON.
893
- const data = await parseResponse(res);
1181
+ const data = decodeDsmlInResult(await parseResponse(res));
894
1182
  const fullText = textOf(data);
895
1183
  if (fullText) {
896
1184
  onStream({ delta: fullText });
@@ -1146,6 +1434,19 @@ function createClient(opts = {}) {
1146
1434
  ],
1147
1435
  };
1148
1436
  if (usage) result.usage = usage;
1437
+ // DSML calls arrive as plain text, decoded by stripEnvelope rather than
1438
+ // accumulated by index like structured delta.tool_calls — merged into one
1439
+ // normalised `{ id, name, args }` list so a tool call that arrived as DSML
1440
+ // (no provider-native delta.tool_calls at all) is executed like any other.
1441
+ // engine.js's extractToolCalls() reads this field first, ahead of
1442
+ // message.tool_calls.
1443
+ const dsmlCalls = stripEnvelope.calls();
1444
+ if (toolCalls.length || dsmlCalls.length) {
1445
+ result.toolCalls = [
1446
+ ...toolCalls.map((c) => ({ id: c.id, name: c.function.name, args: c.function.arguments })),
1447
+ ...dsmlCalls.map((c) => ({ id: '', name: c.name, args: c.args || {} })),
1448
+ ];
1449
+ }
1149
1450
  return result;
1150
1451
  }
1151
1452
 
@@ -87,6 +87,11 @@
87
87
  const fs = require('node:fs');
88
88
  const path = require('node:path');
89
89
  const credentials = require('./credentials.js');
90
+ // `hasPosixModes()` — on Windows fs.stat's mode is synthesised from the read-only
91
+ // attribute, so 0o666 comes back for every file and the group/other test below is
92
+ // always true. Reporting a Windows user's .env as world-readable on every single
93
+ // load is a false alarm they cannot act on.
94
+ const platform = require('./platform.js');
90
95
 
91
96
  const ENV_FILE = '.env';
92
97
 
@@ -339,8 +344,9 @@ function loadEnvFile(o = {}) {
339
344
  const st = fs.statSync(file);
340
345
  result.mode = st.mode & 0o777;
341
346
  // Group- or world-readable: worth one line to the user, never a rewrite of
342
- // a file we did not create.
343
- result.loose = (st.mode & 0o077) !== 0;
347
+ // a file we did not create. Only asked where the answer can be true or false
348
+ // (see the platform require above).
349
+ result.loose = platform.hasPosixModes() && (st.mode & 0o077) !== 0;
344
350
  } catch { /* stat is advisory only */ }
345
351
 
346
352
  const parsed = parseEnvText(text);
@@ -406,7 +412,7 @@ function loadForeignSecrets(o = {}) {
406
412
  try {
407
413
  const st = fs.statSync(file);
408
414
  result.mode = st.mode & 0o777;
409
- result.loose = (st.mode & 0o077) !== 0;
415
+ result.loose = platform.hasPosixModes() && (st.mode & 0o077) !== 0;
410
416
  } catch { /* advisory */ }
411
417
 
412
418
  const parsed = parseEnvText(text);
@@ -708,7 +714,8 @@ function setEnvValue(name, value, o = {}) {
708
714
  out.changed = true;
709
715
  out.replaced = replaced;
710
716
  out.mode = SECRET_FILE_MODE;
711
- out.tightened = !out.created && previousMode !== 0 && (previousMode & 0o077) !== 0;
717
+ out.tightened =
718
+ platform.hasPosixModes() && !out.created && previousMode !== 0 && (previousMode & 0o077) !== 0;
712
719
  if (o.apply !== false) {
713
720
  env[name] = secret;
714
721
  // Just written to the file and applied here: file-sourced (see above).