nuwa-skill
alchaincyf
你想蒸馏的下一个员工,何必是同事。蒸馏任何人的思维方式——心智模型、决策启发式、表达DNA。Distill how anyone thinks.
mpinaev/dsh-context-governor
Context chip for DeepSeek Harness: cost per step, cache-hit, bands, compaction threshold, balance, tariff. Never calls a model. | Индикатор контекста для DeepSeek Harness: цена шага, кэш-хит, полосы, порог компакции, баланс, тариф. Модель не вызывает и токены не тратит.
PROJECT TOPICS
INSTALL REFERENCE
dsh plugin --profile web add github:mpinaev/dsh-context-governor
该命令指向仓库当前默认分支;尚无绑定当前 commit 的完整验证结果。
PROJECT README
A DeepSeek Harness plugin: a session context indicator — prompt size, step cost,
cache-hit, bands and the compaction threshold, DeepSeek balance and the
peak/off-peak tariff, plus a handoff button and a compact button that runs the
built-in /compact.
On its own it never calls a model and spends no tokens — the one exception is
the compact button, which asks the harness to run /compact when you click it.
The chip in the session header opens the panel below. The same panel is available in English, Chinese and Russian; the ⇄ button in the panel header moves the chip to the composer dock or the sidebar foot (see Badge placement):
| English | 中文 | Русский |
|---|---|---|
![]() |
![]() |
![]() |
The handoff button sits next to the model selector; the compact button sits beside it:

Everything the plugin shows is measurement and arithmetic. It reads tokens and the window from harness projections, computes the cost with the configured rates and fetches the balance over HTTP.
/compact, which does the summarization.resolveModelInfo) for the window and limits; that is not
generation and not billable tokens. Tokens are spent only when you press
compact.api.deepseek.com/user/balance are
not a model; the Chinese holiday calendar comes from holidayUrl (the
chinese-days data). Plus git status on a handoff click, locally.The compact button is the deliberate exception: it does not summarize anything
itself, it calls the harness's own /compact — the same command you would type.
That command invokes the harness summarizer and therefore spends tokens. It runs
only when you click, and the plugin shows the host's own result or refusal.
As a package:
dsh plugin --profile web add dsh-context-governor
From source:
dsh plugin --profile web add github:mpinaev/dsh-context-governor
Locally, without installing: drop the directory into
~/.dsh/profiles/web/plugins/ and reference the file from the profile's
cordis.patch.yml:
- insert:
- id: context-governor
name: /absolute/path/to/dsh-context-governor/index.js
Installed as a package, the host half is read at startup, so restart dsh web
and reload the page — the client half is served as a revision snapshot.
Check it with npm test: the smoke test runs the host half against a stub
harness, with no network and no model calls, and pins the compaction threshold,
the cache buckets, the step cost and the balance provider gate.
| DSH version | State |
|---|---|
0.1.7-rc.2 … <0.2.0 |
supported, verified live |
0.2.0-rc.1 … <0.3.0 |
supported, 0.2.2-rc.3 verified live; CI probes the newest published DSH weekly and fails if the version gate disables the plugin |
DSH disables the plugin when its declared range does not cover the running
version. The plugin declares that range in engines.dsh, which DSH 0.2.1 and
newer prefer over peerDependencies; either way the match includes prereleases.
That is why the host logs its token source and tariff at startup: if those lines
are missing, the plugin is probably disabled, and dsh --dump-config will say so.
git clone https://github.com/mpinaev/dsh-context-governor.git
cd dsh-context-governor
npm test # 148 assertions: no network, no model calls
./scripts/compat-check.sh # boots a spare instance on 3099 and asks its API
dsh plugin --profile web add "link:$PWD"
Rules and the release flow live in CONTRIBUTING.md.
base (fresh input = 1.0, cache read =
cacheReadRate/freshRate, about 0.02 for deepseek-flash);Why a multiplier over the raw prompt lies. For deepseek-flash a cache read is
50 times cheaper than fresh input ($0.003 versus $0.15 per 1M off-peak). At 99%
cache-hit a 260k context costs about $0.0015 per step, while prompt/base would
report x2.6. That is why the cost is computed per bucket: fresh x freshRate,
cache read x cacheReadRate, cache write x cacheWriteRate, output x
outputRate.
Tokens. Prompt size, cache-hit and output come from the harness projection
tokenUsage (service ctx.sessionProjections): uncachedInputTokens,
cacheReadTokens, cacheWriteTokens, outputTokens. These are the
provider's authoritative numbers: they survive paging and compaction and close the
slot correctly on a retry (llm/retry-started), so a repeated attempt does not
double count. Without projections in the build, the plugin falls back to its own
fold of llm/stream.
Cache-hit is computed from the session totals, not from the last step alone, so
it agrees with the harness cache-hit pill. A partial hit is never rounded up to
100 — extra decimals are added instead, and only a miss-free hit shows exactly
100%.
Window and threshold. The model window comes from the contextPressure
projection (contextWindow), with request/context (contextWindow) as an early
hint; the output reserve comes from request/header (the request maxTokens),
falling back to the catalog's defaultMaxTokens. The compaction
threshold is computed exactly as the harness does (dsh-compaction-basic):
threshold = min(thresholdRatio * window, window - reserve - headroom)
The threshold belongs to its own session. Window and reserve are kept per session: a session whose route is not known yet reports a window unknown instead of another model's threshold. No bands are invented while the window is unknown.
Three input buckets, each with its own rate. A cache read (default $0.003/1M)
is 50 times cheaper than fresh input, while a cache write on DeepSeek is billed as
ordinary input (cacheWriteRate, default = freshRate). Cache-hit is the
share of the prompt served from cache on read; a cache write does not count as
a hit. A provider that has no prompt cache at all declares cached: false in its
providerRates entry: the cache rows, the cache rates and the cache-hit marker
disappear, and the cacheHitLow and cold-prefill warnings stay quiet. Without the
flag they would hang on every step — a cacheless provider reads 0% cache-hit and
sends the whole prompt as fresh input by definition.
Money. Neither DSH nor the provider reports token cost in dollars anywhere, so the plugin keeps its own rates table (off by default for providers it does not know). The chip/balance logic is therefore:
deepseek-official, ids starting with deepseek), plus any
provider declared in providerRates or listed in pricedProviders. For any
other provider (cline, clinebot, OpenRouter, pi-ai, …) nothing price-related is
computed or displayed: the chip drops the cost segment, and the panel drops the
step-cost, relative-to-base and rates rows — no invented dollar figure and no
"price unavailable" placeholder either;providerRates.<id> overrides the default
freshRate / cacheReadRate / cacheWriteRate / outputRate, so a second
provider can carry its own numbers (and its own peakMultiplier);×N multiplier, Chinese holidays) is
DeepSeek's, so it is shown only for a seasonal provider: by default an id
starting with deepseek. A foreign provider has a flat rate; declare
seasonal: true in its providerRates entry if it really follows DeepSeek's
schedule. A flat provider gets neither the time marker nor the tariff rows.Tariff. The DeepSeek rule is fixed: peak is Beijing time (UTC+8), on weekdays, 09:00–12:00 and 14:00–18:00; everything else, weekends included, is off-peak and costs half. The local timezone plays no part in the decision, only in the display. The configured rates (freshRate, cacheReadRate, cacheWriteRate, outputRate) are off-peak; at peak they are multiplied by peakMultiplier (2 by default), so the step cost and the warnings follow the tariff of the moment. The countdown runs to the next real switch: boundaries inside a weekend are skipped, so after Friday 18:00 it counts to Monday 09:00, not to Saturday. On a provider with that schedule, at peak the tariff marker, the Beijing time and the balance are drawn in red, in both the chip and the panel, so the expensive hours are visible at a glance.
Chinese public holidays. The official rule has an easy-to-miss caveat: peak is
weekdays excluding Chinese public holidays, and on those holidays DeepSeek
stays off-peak around the clock. The plugin takes the year's calendar from a
maintained source (holidayUrl, by default the data of the chinese-days
package), caches it on disk once (holidayCacheDir, default
<DSH_HOME>/cache/context-governor) and refreshes itself — no code to edit every
year. The network is used in the background only: if the calendar has not arrived,
the tariff falls back to the weekday rule and the panel honestly says the holiday
calendar is not loaded. Your own dates can be set with the holidays config — an
array of 'YYYY-MM-DD' or 'YYYY-MM-DD..YYYY-MM-DD' ranges; they apply
immediately, take priority over the source and work without a network. On a
holiday the tariff row reads "off-peak (Chinese holiday)" and the countdown skips
the whole holiday to the next working peak. The network can be turned off
entirely: holidayFetch: false.
Balance. Sources in order:
ctx.deepseekAccount
(getBalance). It works when the account is signed in and the client has sent
its build version; wallets arrive as strings, the currency may be CNY or USD,
and bonus wallets come as a separate list.GET https://api.deepseek.com/user/balance, with the key
from the credentials seam (DEEPSEEK_API_KEY) or from the environment.The source field says where the numbers came from: account or api-key.
The fallback to the second source is deliberate: another build may have no platform
sign-in, and the balance must not disappear because of that.
The total is the topped-up amount plus the granted one, in the primary wallet's
currency. Wallets in another currency are not added to it (yuan and dollars do
not sum); they are shown as a + ¥1.45 suffix, the same way the DSH account
panel does.
The balance is shown only for DeepSeek providers. On cline, clinebot and any
other provider a foreign total on the chip is plain misinformation: no balance is
displayed there, and the panel shows a DeepSeek-only note instead of a number. The
provider list is configurable through balanceProviders (default
['deepseek-official']), and any id starting with deepseek is accepted. If a
session has no route of its own yet, the provider comes from the selected default
model (agentDefaultModel.currentSelection()); without one the balance stays
hidden.
The key never leaves the host: only numbers reach the client (total, bonus, top-up, currency, availability, other wallets). Concurrent reads collapse, and failures degrade into a state (no-credential, error, disabled) rather than an exception. The refresh button bypasses the balance cache and re-reads the balance and the tariff.
The currency is not converted. The step cost and the configured rates
(freshRate, cacheReadRate, cacheWriteRate, outputRate — all $/1M) are in
US dollars, while the balance is printed in whatever currency the source reports
(USD or CNY for DeepSeek), to the cent. The plugin never converts between them, so
a ¥ balance and a $ step cost in the same chip are different units — compare
them only after converting yourself.
Route access. /context-governor/api/* are closed by a guard header and an
Origin check: a request must carry x-dsh-context-governor: 1 (only the plugin
client sets it) and must not be cross-origin. Without the header — 403, with a
foreign Origin — 403. A plain loopback GET without Origin is allowed, which
keeps curl diagnostics convenient.
The practical point: at 99% cache-hit an expensive step is not a large context but fresh input or a cache miss, and the tariff doubles that difference.
One click: the host assembles a session summary (the first task, the session
arc — every request and what came of it, the current state, the files touched
(git status plus tool calls), child sessions), the client opens a new session in the same workspace through
ctx.uiWorkspace.startSession() and puts the summary into its draft. There are
no branches or fallbacks: no Alt+click, no insertion into the current session, no
clipboard.
The arc is built from the whole model surface, not from the latest turn: the first
task, then each user request with the last assistant answer to it, then the
current state. Harness-injected messages (AGENTS.md, runtime context, the skill
catalog) carry source.kind !== 'user' and never become steps; several messages
of one turn (text plus an image) fold into a single step, and an image is marked
[image]. A very long session keeps its beginning and its end — 24 steps — and
says how many middle requests were skipped.
Two declared services keep it reliable:
inject = ['slots', 'uiWorkspace']: without the declaration
Cordis does not hand over the service, and the button used to slip into the
fallback paths instead of opening a session;inject = [... 'sessionQuery']: without it the summary came
out with no task, state or paths (placeholders only).The summary reaches the new session lazily: the click remembers the text,
startSession() opens the session, its input slot renders, and the same
component puts the text into the draft through inputActions.setDraft.
Languages: en (default), zh, ru. The switch is a small button next to
the refresh control in the chip header: it shows the current code (EN / Chinese /
RU) and cycles the language. The choice is remembered in the browser
(localStorage: dsh-context-governor.lang) and immediately re-renders the chip and
both composer buttons (compact and handoff).
The language covers all interface text (panel labels, warnings, tooltips) and the
language of the document the handoff button inserts into the new session: the
client passes lang to /api/handoff. The host returns data without text
(band, tariff and warnings are codes), so switching the language needs no data
refetch.
By default the chip stays in the session header. The ⇄ button in the panel header, next to the language switch, moves it round-robin between three places:
Only one copy renders at a time, and the choice is remembered in the browser
(localStorage: dsh-context-governor.place) so a reload keeps it. The panel opens
downward in the header and upward in the composer and the sidebar, where there is
no room below. In the sidebar the chip drops the cache-hit segment (the standard
status row already shows it); when the sidebar is collapsed to the icon rail, the
chip becomes a single band-coloured icon that opens the same panel.
By default the plugin does not hardcode the model window — it reads it from the
harness (windowTokens can force one):
contextPressure projection (contextWindow) of
ctx.sessionProjections: the authoritative window of the live session;
request/context (contextWindow) is an early hint before the projection
warms up.maxTokens from the request/header
event; until it arrives, the catalog's defaultMaxTokens serves as a hint.ctx.llm.resolveModelInfo for
agentDefaultModel.currentSelection()) remains a hint until the first request.Window and reserve are kept per session.
The compaction threshold and the bands are derived from the window:
reserve = request maxTokens (else catalog defaultMaxTokens)
compaction threshold = min(thresholdRatio * window, window - reserve - headroomTokens)
band i = bandRatios[i] * compaction threshold
With a 1,000,000 window and a 256,000 reserve the threshold is 678,464 and the
bands are 237k / 407k / 577k. With an 800,000 window the threshold is 478,464 and
the bands 167k / 287k / 407k. The formula matches dsh-compaction-basic
(window - reserve - headroom); with constant bands, critical would sit below the
real prefix-rewrite threshold.
The plugin adapts to different models in different sessions: every session keeps its own route window, and another session's window is never substituted. If the window has not been resolved yet, the bands are not invented (there are none) and the panel shows a model-window-unknown warning.
In the context-governor block of ~/.dsh/profiles/web/cordis.patch.yml you
can tune only base, bandRatios, headroomTokens, thresholdRatio, the default rates
(freshRate, cacheReadRate, cacheWriteRate, outputRate,
peakMultiplier), the per-provider overrides (pricedProviders,
providerRates — rates, seasonal, cached, peakMultiplier) and the signal thresholds
(anomalyDelta, anomalyCostUsd, cacheHitFloorPct); for the holiday calendar —
holidays, holidayFetch, holidayUrl, holidayCacheDir, holidayRetryMs,
holidayTimeoutMs; for the balance — balanceEnabled,
useAccountBalance, balanceProviders, balanceTtlMs,
balanceTimeoutMs. windowTokens and reservedTokens can be forced, but
they default to 0, meaning ask the harness; hardcoding them loses the
adaptivity.
The bands are derived from the model window; the normal/attention/high/critical labels are not read back. The plugin measures and never trims: it does not change history or the prompt.
MIT — see LICENSE.
CLASSIFICATION EVIDENCE
系统优先读取 GitHub Topics,再与站内分类词典和词根规则比对。当前命中: context。