prompt-optimizer-magpie 0.2.6 → 0.2.7

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/hooks.mjs CHANGED
@@ -20,6 +20,10 @@ const DEFAULTS = {
20
20
  minChars: 6,
21
21
  maxChars: 12000,
22
22
  bypassPrefix: ["!!", "##"],
23
+ // 机器注入包装(Hermes out-of-band/system-notice、title-request、progress-check、
24
+ // job 完成通知、recap 等)不是人类输入,改写它们只会破坏协议语义且必然触发
25
+ // output-guard。命中即整条放行,不进优化。
26
+ bypassWrappers: ["<system-notice>", "<title-request>", "<progress-check>", "<job>", "<recap>"],
23
27
  bypassModels: [],
24
28
  bypassAgents: [],
25
29
  allowedOrigins: [],
@@ -113,6 +117,10 @@ export async function onRequest(body, ctx) {
113
117
  }
114
118
  const prefixes = [].concat(o.bypassPrefix).filter(Boolean);
115
119
  if (typeof (ctx && ctx.query) === "string" && prefixes.some((p) => ctx.query.trimStart().startsWith(p))) return pass("bypass-prefix");
120
+ // Machine-injected wrappers (bypassWrappers) anywhere in the newest turn:
121
+ // they wrap protocol text the rewrite must not touch.
122
+ const wrappers = [].concat(o.bypassWrappers || []).filter(Boolean);
123
+ if (typeof (ctx && ctx.query) === "string" && wrappers.length && wrappers.some((w) => ctx.query.includes(w))) return pass("bypass-wrapper");
116
124
 
117
125
  const protocol = (ctx && ctx.protocol) || "chat";
118
126
  if (o.skipToolless && !hasTools(body, protocol)) return pass("no-tools");
@@ -129,6 +137,7 @@ export async function onRequest(body, ctx) {
129
137
  } catch {}
130
138
  }
131
139
  if (prefixes.some((p) => orig.startsWith(p))) return pass("bypass-prefix");
140
+ if (wrappers.length && wrappers.some((w) => orig.includes(w))) return pass("bypass-wrapper");
132
141
  if (orig.length < o.minChars) return pass("short-text", { in: orig.length, inUnits: countUnits(orig) });
133
142
  if (orig.length > o.maxChars) return pass("too-long", { in: orig.length, inUnits: countUnits(orig) });
134
143
 
@@ -3,17 +3,26 @@
3
3
  // {{ name }} ≡ {{name}} (inner whitespace stripped) and backtick tokens compare
4
4
  // their content, so a wider fence around the same identifier still passes.
5
5
  // Per-category counts must still match exactly — a swallowed or invented
6
- // placeholder always trips the guard (fail-safe).
6
+ // placeholder always trips the guard (fail-safe). Backticks are one-way
7
+ // tolerant: the optimizer ADDING code fences around bare text is legitimate
8
+ // emphasis and cannot corrupt meaning, so added backticks are ignored while
9
+ // any backticked token present in the original must survive verbatim
10
+ // (2026-11-23: 4/5 guard reverts were bare paths gaining fences, 0→3).
7
11
  const protectedTokens = [
8
12
  { re: /\[REDACTED\]/g, norm: (t) => t },
9
13
  { re: /\{\{[^{}\r\n]+\}\}/g, norm: (t) => t.replace(/\s+/g, "") },
10
- { re: /(?<!`)(`+)(?!`)[^`\r\n]+\1(?!`)/g, norm: (t) => t.replace(/^`+/, "").replace(/`+$/, "") },
14
+ { re: /(?<!`)(`+)(?!`)[^`\r\n]+\1(?!`)/g, norm: (t) => t.replace(/^`+/, "").replace(/`+$/, ""), addedOk: true },
11
15
  ];
12
16
  export function preservesTokens(orig, out) {
13
- return protectedTokens.every(({ re, norm }) => {
14
- const before = (String(orig).match(re) || []).map(norm).sort();
15
- const after = (String(out).match(re) || []).map(norm).sort();
16
- return before.length === after.length && before.every((token, i) => token === after[i]);
17
+ const o = String(orig), u = String(out);
18
+ return protectedTokens.every(({ re, norm, addedOk }) => {
19
+ const before = (o.match(re) || []).map(norm).sort();
20
+ const after = (u.match(re) || []).map(norm).sort();
21
+ if (!before.every((token, i) => token === after[i] || after.includes(token))) return false;
22
+ if (!addedOk) return before.length === after.length;
23
+ // addedOk: new tokens are fencing text that already existed in the original;
24
+ // an invented identifier is still a corruption.
25
+ return after.every((token) => before.includes(token) || o.includes(token));
17
26
  });
18
27
  }
19
28
 
@@ -131,6 +131,7 @@ export const T = {
131
131
  "r_bypass-agent": ["agent 名命中旁路", "agent bypassed"],
132
132
  "r_bypass-model": ["模型名命中旁路", "model bypassed"],
133
133
  "r_bypass-prefix": ["前缀命中旁路", "prefix bypassed"],
134
+ "r_bypass-wrapper": ["机器注入包装旁路", "machine-injected wrapper bypassed"],
134
135
  r_subagent: ["子代理请求放行", "subagent request passed"],
135
136
  "r_self-loop": ["自身回路放行", "self loop passed"],
136
137
  r_disabled: ["插件未启用", "plugin disabled"],
@@ -339,7 +340,7 @@ export function shortId(id) {
339
340
  export const ACTION_TAG = { optimize: "green", decide: "amber", pass: "muted", summary: "accent" };
340
341
  export const REASON_GROUPS = {
341
342
  green: ["optimized", "merged-optimized", "cache-hit", "dry-run"],
342
- amber: ["not-turn", "no-tools", "short-text", "too-long", "bypass-agent", "bypass-model", "bypass-prefix", "subagent", "self-loop", "disabled", "origin", "followup", "tpl-skip", "experiment-control", "no-decider-followup", "merged-passthrough", "merged-circuit-open", "budget-exhausted", "low-confidence", "unknown-choice"],
343
+ amber: ["not-turn", "no-tools", "short-text", "too-long", "bypass-agent", "bypass-model", "bypass-prefix", "bypass-wrapper", "subagent", "self-loop", "disabled", "origin", "followup", "tpl-skip", "experiment-control", "no-decider-followup", "merged-passthrough", "merged-circuit-open", "budget-exhausted", "low-confidence", "unknown-choice"],
343
344
  red: ["optimize-empty", "output-guard", "output-guard-short", "output-guard-long", "output-guard-tokens", "no-span", "error"],
344
345
  };
345
346
  export function reasonGroup(reason) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prompt-optimizer-magpie",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Self-contained Magpie gateway request hook that rewrites vague first-turn prompts into precise ones before they reach your model. Fail-open by design.",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0-only",