dsh-model-switch 0.4.71 → 0.4.73

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.
@@ -0,0 +1,186 @@
1
+ /**
2
+ * Model Switch's owned Auto-review gate (the "purge takeover").
3
+ *
4
+ * The official `@deepseek-ai/dsh-experimental-auto-review` row this plugin
5
+ * mounts is host-plane code: a third-party host patch (dsh-purge-community
6
+ * 1.7.20, applied 2026-11-23) inserted `return next()` as the first line of
7
+ * its `tools/pre-execute` listener, which silently disabled review for every
8
+ * call regardless of the permission preset. This module forks that review
9
+ * pipeline — snapshot assembly, reviewer request, decision protocol, denial
10
+ * surfaces — verbatim from the official 0.2.2-rc.5 implementation, so the
11
+ * review path lives in this plugin's own source and survives host-package
12
+ * patches. The official package keeps only the registration choreography
13
+ * (`registerAuto`, teardown, dependency gating) it still mounts.
14
+ *
15
+ * Calibration source: `@deepseek-ai/dsh-experimental-auto-review` 0.2.2-rc.5
16
+ * `lib/index.js` (behavior ported line-for-line; the snapshot logic and the
17
+ * REVIEW_POLICY text must track that file — compat-watch diffs it per line).
18
+ *
19
+ * The gate is armed only when the profile started the dsh-purge-community
20
+ * bundle and the `autoReviewOwnGate` setting is not explicitly off; the
21
+ * official gate stays mounted either way, and the two never double-review:
22
+ * whichever listener engages first marks the ambient call context, and the
23
+ * `permissionPresets` shadow in `./auto-review.ts` answers `custom` for the
24
+ * other (see the coordination notes on {@link OwnGateCallContext}).
25
+ *
26
+ * @module dsh-model-switch/auto-review-gate
27
+ */
28
+ import type { GenerateOptions } from '@deepseek-ai/dsh-llm';
29
+ import type { Session } from '@deepseek-ai/dsh-session';
30
+ import type { PreToolDecision } from '@deepseek-ai/dsh-tools';
31
+ import type { Context } from '@deepseek-ai/cordis';
32
+ /** The decision surface the pre-execute waterfall expects (dsh-tools' own shape). */
33
+ type GateDecision = PreToolDecision;
34
+ /**
35
+ * Minimal structural faces of the session log the snapshot reads.
36
+ *
37
+ * These mirror the official gate's reads; the shapes are part of the released
38
+ * session format and are covered by compat-watch.
39
+ */
40
+ export interface ReviewEvent {
41
+ readonly type: string;
42
+ readonly seq: number;
43
+ readonly data: any;
44
+ }
45
+ /** The pending execution's face (subset of dsh-tools' ToolExecution). */
46
+ export interface OwnGateExecution {
47
+ readonly name: string;
48
+ readonly arguments: unknown;
49
+ readonly rootCallId: string;
50
+ readonly callId: string;
51
+ readonly parent: unknown | undefined;
52
+ readonly signal: AbortSignal;
53
+ readonly agent: {
54
+ readonly session: Session;
55
+ } | undefined;
56
+ readonly schema: {
57
+ readonly name: string;
58
+ readonly description: string;
59
+ readonly parameters: unknown;
60
+ } | undefined;
61
+ }
62
+ /** What one own-gate review carries in the ambient call context. */
63
+ export interface OwnGateCallContext {
64
+ /** Marks a call whose review decision another listener already made. */
65
+ handled: boolean;
66
+ /** Marks a call the official gate's listener is engaging with. */
67
+ officialEngaged: boolean;
68
+ }
69
+ /** One frozen reviewer snapshot. */
70
+ export interface AutoReviewSnapshot {
71
+ readonly provider: string;
72
+ readonly model: string;
73
+ readonly cwd: string;
74
+ readonly history: readonly unknown[];
75
+ readonly pending: {
76
+ readonly key: string;
77
+ readonly schema: {
78
+ name: string;
79
+ description: string;
80
+ parameters: unknown;
81
+ };
82
+ };
83
+ }
84
+ /**
85
+ * Freeze the reviewer input from one session and pending execution (official parity).
86
+ * @param session - the session whose durable surface and request header authorize the call.
87
+ * @param exec - immutable pending execution.
88
+ * @param cwd - the session's current working directory.
89
+ * @param shown - schemas earlier reviews of this session showed.
90
+ * @returns the exact route, environment, and history ending with the pending call.
91
+ */
92
+ export declare function snapshotAutoReview(session: Session, exec: OwnGateExecution, cwd: string, shown: ReadonlyMap<string, {
93
+ name: string;
94
+ description: string;
95
+ parameters: unknown;
96
+ }>): AutoReviewSnapshot;
97
+ /** Render the environment and one message per retained item (official parity). */
98
+ export declare function reviewMessages(snapshot: AutoReviewSnapshot): Array<{
99
+ role: 'user';
100
+ content: Array<{
101
+ type: 'text';
102
+ text: string;
103
+ }>;
104
+ }>;
105
+ /** Count members in the raw top-level JSON object. */
106
+ export declare function topLevelMemberCount(text: string): number;
107
+ /** One parsed reviewer decision. */
108
+ export interface ReviewDecision {
109
+ readonly risk: 'low' | 'medium' | 'high';
110
+ readonly decision: 'allow' | 'deny';
111
+ readonly reason?: string;
112
+ }
113
+ /** Parse the closed risk/decision protocol and its fixed safety combinations. */
114
+ export declare function parseDecision(text: string): ReviewDecision;
115
+ /** Consume zero or more reasoning blocks, one JSON text block, and one terminal stop. */
116
+ export declare function readDecision(stream: AsyncIterable<unknown>): Promise<ReviewDecision>;
117
+ /** Minimal llm face the gate uses (it calls exactly `stream`). */
118
+ export interface GateLlm {
119
+ stream(options: GenerateOptions): AsyncIterable<unknown>;
120
+ }
121
+ /** Structural face of the workingDirectory service the gate reads. */
122
+ export interface GateWorkingDirectory {
123
+ ensure(agent: {
124
+ readonly session: Session;
125
+ }, signal: AbortSignal): Promise<string>;
126
+ }
127
+ /** Structural face of the approval service the gate reads. */
128
+ export interface GateApproval {
129
+ overrideOf(session: Session): string;
130
+ }
131
+ /** Review one frozen pending action with the fixed policy and the scoped llm route. */
132
+ export declare function classifyRisk(deps: {
133
+ readonly llm: GateLlm;
134
+ readonly workingDirectory: GateWorkingDirectory;
135
+ }, session: Session, exec: OwnGateExecution, signal: AbortSignal, shown: Map<string, {
136
+ name: string;
137
+ description: string;
138
+ parameters: unknown;
139
+ }>): Promise<ReviewDecision>;
140
+ /** Materialize the fixed model-facing final Auto denial plus optional UI detail. */
141
+ export declare function denied(exec: OwnGateExecution, reason: string | undefined): GateDecision;
142
+ /** Ask the user to decide one call the reviewer denied. */
143
+ export declare function askUser(exec: OwnGateExecution, reason: string | undefined): GateDecision;
144
+ /** Materialize a reviewer failure as its own error rather than a denial. */
145
+ export declare function failed(exec: OwnGateExecution, error: unknown): GateDecision;
146
+ /** The ambient call context shape the own gate coordinates through. */
147
+ export interface OwnGateAmbient {
148
+ readonly session: Session | undefined;
149
+ readonly tool: string;
150
+ /** Marks a call whose review decision another listener already made. */
151
+ handled?: boolean;
152
+ /** Marks a call the official gate's listener is engaging with. */
153
+ officialEngaged?: boolean;
154
+ }
155
+ /** Wiring the own gate needs from the mounting row. */
156
+ export interface OwnGateOptions {
157
+ /** The ambient store shared with the correlation wrapper and the preset shadow. */
158
+ readonly activeSession: {
159
+ getStore(): OwnGateAmbient | undefined;
160
+ };
161
+ /** Whether the own gate should engage (purge bundle present and the setting on). */
162
+ readonly isActive: () => boolean;
163
+ /** The raw preset read (the real service — never the defensive shadow). */
164
+ readonly currentPreset: (session: Session) => string;
165
+ /** Whether the tool is allowlisted (the live settings read). */
166
+ readonly skipReadOnly: (tool: string) => boolean;
167
+ /** Count one read-only skip on the shared status counter. */
168
+ readonly countReadOnlySkip: () => void;
169
+ }
170
+ /**
171
+ * Register this row's own review listener on the gate context.
172
+ *
173
+ * The listener is registered with `{ prepend: true }` after the official gate's
174
+ * apply, so it sits ahead of the official listener in the waterfall; the
175
+ * correlation wrapper (bound later, also prepended) stays outermost. Whichever
176
+ * listener engages first marks the ambient context (`handled` for ours, the
177
+ * shadow's `custom` answer for the official probe), so a call is never reviewed
178
+ * twice regardless of mount ordering.
179
+ *
180
+ * @param gateCtx - the context the official gate mounted under (services resolve
181
+ * through the review scope: the llm proxy routes the reviewer, the shadowed
182
+ * permissionPresets is bypassed via {@link OwnGateOptions.currentPreset}).
183
+ * @param options - the wiring from the mounting row.
184
+ */
185
+ export declare function installOwnReviewGate(gateCtx: Context, options: OwnGateOptions): void;
186
+ export {};
@@ -66,7 +66,24 @@ export declare const autoReviewStatus: {
66
66
  held: boolean;
67
67
  allowedCalls: number;
68
68
  skippedReadOnly: number;
69
+ /** Whether the profile started the dsh-purge-community bundle (mount-time read). */
70
+ purgeInstalled: boolean;
71
+ /**
72
+ * Whether the official gate's host code carries a third-party patch marker.
73
+ *
74
+ * Best-effort diagnostic only: the own gate does not depend on it. `undefined`
75
+ * means the official package could not be read.
76
+ */
77
+ officialGateNeutered: boolean | undefined;
69
78
  };
79
+ /**
80
+ * Whether the own review gate engages for the current call.
81
+ *
82
+ * The purge bundle must have started (mount-time) and the setting must not be
83
+ * explicitly off (per-call read); on a purge-free host the own gate never
84
+ * engages and the official gate stays the only reviewer.
85
+ */
86
+ export declare function ownGateEngaged(): boolean;
70
87
  /**
71
88
  * What one `tools/pre-execute` call carries into the reviewer's async context.
72
89
  *
@@ -79,6 +96,16 @@ export interface AutoCallContext {
79
96
  readonly session: Session | undefined;
80
97
  /** `exec.name` — the tool under review. */
81
98
  readonly tool: string;
99
+ /**
100
+ * Marks a call whose review decision another listener already made.
101
+ *
102
+ * Set by the own gate when it reviews (or read-only-skips) a call, and by the
103
+ * preset shadow when it takes the official gate's probe off the reviewer —
104
+ * whichever listener engages first wins, so a call is never reviewed twice.
105
+ */
106
+ handled?: boolean;
107
+ /** Marks a call the official gate's listener is engaging with. */
108
+ officialEngaged?: boolean;
82
109
  }
83
110
  /** The read-only skip: which tools, and whether the switch is on. */
84
111
  export interface ReadOnlyBypass {
@@ -264,14 +291,16 @@ export interface MountedAutoReviewGate {
264
291
  * `inject`. Mounting it instead lets cordis resolve the official list, which is
265
292
  * what keeps a released Host's new dependency (0.2.1's `workingDirectory`) from
266
293
  * breaking every tool call. The hooks let the caller observe whether the gate
267
- * actually ran before it binds the session-correlation listener.
294
+ * actually ran before it binds the session-correlation listener; `onApplied`
295
+ * receives the gate's own context so per-mount listeners (the own gate) can
296
+ * register on it and share its lifetime.
268
297
  *
269
298
  * @param gate - the official module (or an equivalent structural gate).
270
299
  * @param hooks - applied/failure observers; the failure is rethrown.
271
300
  * @returns A cordis plugin mounting the official gate unchanged.
272
301
  */
273
302
  export declare function autoReviewGate(gate: OfficialAutoReviewGate, hooks?: {
274
- readonly onApplied?: () => void;
303
+ readonly onApplied?: (gateCtx: Context) => void;
275
304
  readonly onFailure?: (error: unknown) => void;
276
305
  }): MountedAutoReviewGate;
277
306
  /**
@@ -8,6 +8,12 @@ export interface ReviewStatus {
8
8
  readonly held: boolean;
9
9
  readonly allowedCalls: number;
10
10
  readonly skippedReadOnly: number;
11
+ /** Whether this plugin's own review gate engages (purge bundle + setting). */
12
+ readonly ownGate: boolean;
13
+ /** Whether the profile started the dsh-purge-community bundle. */
14
+ readonly purgeInstalled: boolean;
15
+ /** Whether the official gate's host code carries a third-party patch marker; undefined when unreadable. */
16
+ readonly officialGateNeutered?: boolean | undefined;
11
17
  /** Third-party `@log.li/dsh-automode` row, when this Host mounts one. */
12
18
  readonly automode?: {
13
19
  readonly installed: boolean;
@@ -35,6 +35,8 @@ export interface ModelSwitchSettingsFace {
35
35
  enabled: boolean;
36
36
  tools: readonly string[];
37
37
  }) => Promise<void>;
38
+ /** Persist the purge-takeover switch (the own review gate). */
39
+ saveReviewOwnGate: (ownGate: boolean) => Promise<void>;
38
40
  /** The Host's live tool catalog; absent on a Host without the `tools` RPC endpoint. */
39
41
  scanTools?: () => Promise<{
40
42
  rows: readonly ToolCatalogRow[];
@@ -94,6 +94,13 @@ export declare const zh: {
94
94
  readonly reviewAllowlistAvailable: "本机存在但未放行的工具:";
95
95
  readonly reviewAllowlistRepo: "仓库";
96
96
  readonly reviewAllowlistSkipped: "本 Host 进程已为纯读调用跳过 {count} 次审查。";
97
+ readonly reviewOwnGate: "破甲接管:自有审查门";
98
+ readonly reviewOwnGateHelp: "安装破甲(dsh-purge)后,官方审查门的宿主代码会被其补丁短路;开启后由本插件的自有审查门接管 auto 会话的逐调用审查,路由仍用上方配置的审查模型。";
99
+ readonly reviewOwnGateUnavailable: "未检测到破甲插件:官方审查门正常服务,此开关不适用。";
100
+ readonly reviewOwnGatePatched: "官方门补丁状态:{neutered}。";
101
+ readonly reviewOwnGateNeuteredYes: "已被宿主补丁短路";
102
+ readonly reviewOwnGateNeuteredNo: "未见补丁标记";
103
+ readonly reviewOwnGateUnknown: "未知";
97
104
  readonly reviewAutomodeLinked: "Auto mode 已接入:审查路由 {route},直通 +{added} 个纯读工具({skipped} 条通配未同步)。";
98
105
  readonly reviewAutomodeRouteOwn: "沿用其自身配置";
99
106
  readonly reviewAutomodeRouteUnset: "未配置(沿用会话模型)";
@@ -14,6 +14,8 @@ export interface ModelSwitchSettingsView {
14
14
  autoReviewModel?: string;
15
15
  /** Opt-in read-only skip for the Auto reviewer; absent/false keeps every call reviewed. */
16
16
  autoReviewSkipReadOnly?: boolean;
17
+ /** Purge takeover: run the own review gate on a dsh-purge host (default on; grayed out without that bundle). */
18
+ autoReviewOwnGate?: boolean;
17
19
  /** Exact tool names, or trailing-`*` patterns, that skip the reviewer when enabled. */
18
20
  autoReviewAllowlist?: readonly string[];
19
21
  compactProvider?: string;
@@ -123,6 +125,12 @@ export interface ReviewStatusView {
123
125
  readonly held: boolean;
124
126
  readonly allowedCalls: number;
125
127
  readonly skippedReadOnly: number;
128
+ /** Whether this plugin's own review gate engages (purge bundle started and the setting not off). */
129
+ readonly ownGate: boolean;
130
+ /** Whether the profile started the dsh-purge-community bundle. */
131
+ readonly purgeInstalled: boolean;
132
+ /** Whether the official gate's host code carries a third-party patch marker; undefined when unreadable. */
133
+ readonly officialGateNeutered?: boolean | undefined;
126
134
  /** Third-party `@log.li/dsh-automode` row this Host adapted, when present. */
127
135
  readonly automode?: {
128
136
  readonly route?: string | undefined;
@@ -21,6 +21,15 @@ export interface ModelSwitchSettings {
21
21
  * reviewer. Absent/false keeps every call reviewed (the shipped default).
22
22
  */
23
23
  autoReviewSkipReadOnly?: boolean;
24
+ /**
25
+ * Whether this plugin's own review gate takes over the Auto review on a
26
+ * dsh-purge-patched host (default true). The official gate's host code can be
27
+ * short-circuited by that patch; the own gate runs the same review pipeline
28
+ * from this plugin's source. Only meaningful when the dsh-purge-community
29
+ * bundle started — on a clean host the switch is ignored (the official gate
30
+ * serves) and the settings card grays it out.
31
+ */
32
+ autoReviewOwnGate?: boolean;
24
33
  /**
25
34
  * Tool names that skip the reviewer when {@link autoReviewSkipReadOnly} is on.
26
35
  *
@@ -77,6 +86,8 @@ export interface Config {
77
86
  autoReviewModel: Volatile<string | undefined>;
78
87
  /** Opt-in: skip the Auto reviewer for allowlisted pure-read tools (default off). */
79
88
  autoReviewSkipReadOnly: Volatile<boolean>;
89
+ /** Purge takeover: run the own review gate on a dsh-purge host (default on). */
90
+ autoReviewOwnGate: Volatile<boolean>;
80
91
  /** Tool names (exact, or a trailing `*` pattern) that skip the Auto reviewer. */
81
92
  autoReviewAllowlist: Volatile<readonly string[]>;
82
93
  /** Reviewer stream idle timeout in ms; 0 = cancel (upstream ceiling); absent = provider default. */
@@ -72,7 +72,8 @@ export declare const SOURCE_BY_ID: ReadonlyMap<string, SourceCatalogEntry>;
72
72
  * Beyond that rule sit the entries the user explicitly released (2026-10-24:
73
73
  * goal/schedule/terminal creation; 2026-11-20: the vision model calls, the
74
74
  * mineru document_parse task, todo/goal updates, and the prime-memory write
75
- * tools). These do have side effects — vision calls spend the configured
75
+ * tools; 2026-11-22: the media-lens file reads and the video storyboard
76
+ * search/decode tools). These do have side effects — vision calls spend the configured
76
77
  * backend's quota and send image content to it, document_parse spawns a parse
77
78
  * task, the memory tools mutate the memory store — but everything they enable
78
79
  * stays inside the same session and auto preset. Releasing them only skips the
@@ -81,7 +82,11 @@ export declare const SOURCE_BY_ID: ReadonlyMap<string, SourceCatalogEntry>;
81
82
  * from the parent's in-flight bypass answer, so releasing them would strip the
82
83
  * child of the auto preset entirely (see DELEGATION_SPAWNS in the tests).
83
84
  * schedule_edit was reviewed on 2026-11-20 and deliberately kept OUT: it can
84
- * modify or delete tasks other sessions created.
85
+ * modify or delete tasks other sessions created. bilibili_login (writes the
86
+ * stored sign-in session), bilibili_transcribe (its first run builds a private
87
+ * virtualenv, downloads a whisper model and the audio) and meme_add (writes
88
+ * the meme library) were reviewed on 2026-11-22 and stay OUT for the same
89
+ * reason.
85
90
  */
86
91
  export declare const DEFAULT_READ_ONLY_TOOLS: readonly string[];
87
92
  /** Why one allowlist entry was refused. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-model-switch",
3
- "version": "0.4.71",
3
+ "version": "0.4.73",
4
4
  "description": "Explicit model routing plus composer/Plan picker for DeepSeek Harness",
5
5
  "repository": {
6
6
  "type": "git",