dsh-buddy-guard
开发工具 活跃维护

dsh-buddy-guard

AsterLuo/dsh-buddy-guard

Fix CodeBuddy (Tencent) channel returning HTTP 400 Illegal API invocation from an unapproved channel in DeepSeek Harness by prefixing a space to the system prompt.

1
Stars 标星
0
Forks 分支
1
Watchers 关注
1
Open Issues
JavaScript
主要语言
MIT
开源协议
24 KB
仓库大小
1 个月前
最后推送
一键安装扩展 / 插件指令
dsh plugin --profile web add github:AsterLuo/dsh-buddy-guard
git clone https://github.com/AsterLuo/dsh-buddy-guard.git
git clone git@github.com:AsterLuo/dsh-buddy-guard.git
README.md main

dsh-buddy-guard

Fix the CodeBuddy / buddy (Tencent) channel returning HTTP 400 on every request
in DeepSeek Harness:

buddy: 请求被拦截,请重新发送 Illegal API invocation from an unapproved channel

中文说明

dsh plugin add github:AsterLuo/dsh-buddy-guard --profile <your-profile>

# pin a version (recommended):
dsh plugin add github:AsterLuo/dsh-buddy-guard#v0.1.1 --profile <your-profile>

Restart DSH afterwards — it is required.

The problem

Pick any model on the buddy channel (hy4-preview, deepseek-v4.1-flash, hy3, …) and every turn fails
with HTTP 400 INVALID_REQUEST. The same account and the same endpoint work fine as soon
as the request stops carrying the DSH system prompt.

First block (measured 2026-10-21)

The upstream does a prefix match on the system content. If the system text starts
with

You are an AI agent powered by DeepSeek Harness.

it is rejected outright. None of the following matter — every one was changed and the
status stayed 400:

  • X-Domain (copilot.tencent.com vs www.codebuddy.cn)
  • X-Product-Code / X-Product / X-Agent-Purpose
  • X-IDE-Name / X-IDE-Type / X-IDE-Version / User-Agent
  • the endpoint (copilot.tencent.com and www.codebuddy.cn behave identically)
  • the model (hy4-preview, hy3, deepseek-v4.1-flash are all rejected)

Boundary tests (same account, same endpoint, same user message):

system content result
You are an AI agent powered by DeepSeek Harness. 400
the same line with one leading space 200
one leading letter / period 200
trailing space / newline / period / a whole paragraph 400
the sentence inside a user message 200
only DeepSeek Harness, or lowercase deepseek harness 200

It matches the start of the system text; prefixing any character breaks the match,
and appending anything does not help.

Second block (measured 2026-10-27)

There is a second, independent rule. The upstream also rejects the request when the
system text contains both of these phrases at once (AND match — either one alone passes):

  • Tokens prefixed with @ are paths the user explicitly referenced
  • Relative paths resolve from the workspace root

Both are part of the DSH system prompt's description of @ path references, so they ship
with every request. The leading space above does not help here.

Measured (same account, same endpoint):

system text result
phrase A alone 200
phrase B alone 200
A and B together 400 11128
A rewritten, B kept as-is 200
B rewritten, A kept as-is 200

The plugin therefore rewrites phrase A only and leaves B intact — B states the relative-path
rule that read / write / glob depend on, so keeping its wording is friendlier to the model.
The rewrite is semantically equivalent and keeps the same meaning.

History is a blind spot

The upstream scans every message, so phrase A/B sitting in conversation history triggers
the same block. A plugin cannot fix that: agent/request documents that it cannot mutate
messages
, and the llm/stream options object arrives deep-frozen (mutation throws). If a
session has ever quoted these two phrases, start a new session.

How this was found (repeatable)

  1. Hook a read-only llm/stream listener and dump the complete outgoing request to disk.
  2. Replay that dump against the endpoint directly, using the account's own token.
  3. Binary-search the dumped system text: cut it in half, resend, repeat. The block is monotone,
    so this converges on the exact trigger in about ten requests.
  4. Replay the full request with the trigger rewritten, and confirm 200.

A blocked request costs no tokens, so this is cheap.

How it works

The plugin listens on system-prompt/assemble and returns a modified assembly that:

  1. gives the first non-empty section one leading space — renderPrompt joins sections with
    \n\n, so the whole rendered system text then starts with that space;
  2. scans every section and rewrites phrase A to its equivalent.

Registration uses { global: true, prepend: true }, and prepend is required: without it,
an outer listener that never calls next() (for example dsh-ops' discovery branch, which does
return prompt.assemble(context)) would veto the rest of the chain — per cordis, "a listener
that does not call next() vetoes the rest of the chain"
. The plugin would load, log, and show
up in --dump-config, yet change nothing.

Why not llm/stream

The first version hooked llm/stream and called next(modifiedOptions). That did nothing,
silently. Cordis implements waterfall as:

const next = () => (cbs.shift() ?? inner)(...args);

next takes no arguments — the modified options were dropped on the floor. The plugin
loaded, logged, showed up in --dump-config, and the outgoing request was byte-for-byte
unchanged.

Replacing arguments only works through a listener's return value.

Install

dsh plugin add github:AsterLuo/dsh-buddy-guard --profile <your-profile>

This copies the package into your profile's node_modules and registers it in
dsh.profile.bundles. Your own cordis.patch.yml is left untouched.

Then restart DSH — the config is read once at startup.

Manual install

  1. Copy this repo into <DSH_HOME>/profiles/<profile>/node_modules/dsh-buddy-guard/
  2. Append "dsh-buddy-guard" to dsh.profile.bundles in that profile's package.json
  3. Add "dsh-buddy-guard": "0.1.1" to dependencies in the same file
  4. Verify with --dump-config and look for the buddy-guard row
  5. Restart DSH

Configuration

- id: buddy-guard
  name: 'dsh-buddy-guard'
  config:
    enabled: true
    providers:
      - buddy
  • enabled — master switch.
  • providers — which providers get the prefix. Defaults to ['buddy']; other
    providers pass through untouched.
  • debug — when true, appends one line per guarded request to $DSH_HOME/buddy-guard.log.
    Off by default, so nothing is written on the hot path.

Verify it worked

After restarting, switch to a buddy model and send a message. A normal reply means it
works. Still getting 400?

  1. Re-run --dump-config and confirm the buddy-guard row is there.
  2. Confirm lib/index.js exists and is not empty.
  3. Confirm the package is listed in dsh.profile.bundles — the most commonly missed step.
  4. Confirm you really restarted DSH.

Caveat

This bypasses upstream content moderation. The rules can change at any time, and there
are now two known ones. If the same error returns, re-run the request-dump + binary-search
recipe above before changing anything.

The account in question is a CodeBuddy China account (www.codebuddy.cn, the JWT iss
points there) while the client sends the international product shape. That mismatch is
itself what the upstream means by "unapproved channel" — this plugin only makes it work for
now, it does not fix the underlying mismatch. Using workbuddy (international) avoids the
check entirely.

Test

npm test

License

MIT