huaweicloud-devkit 2.0.0-next.2 → 2.0.0-next.4

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.
Files changed (161) hide show
  1. package/README.md +3 -1
  2. package/package.json +3 -1
  3. package/plugins/huaweicloud-core/.claude-plugin/plugin.json +16 -3
  4. package/plugins/huaweicloud-core/.codex-plugin/plugin.json +16 -3
  5. package/plugins/huaweicloud-core/.cursor-plugin/plugin.json +16 -3
  6. package/plugins/huaweicloud-core/.hermes-plugin/plugin.json +16 -3
  7. package/plugins/huaweicloud-core/.mcp.json +55 -1
  8. package/plugins/huaweicloud-core/.workbuddy-plugin/plugin.json +16 -3
  9. package/plugins/huaweicloud-core/dist/agents/codex-config.js +374 -0
  10. package/plugins/huaweicloud-core/dist/agents/codex-desktop.js +8 -1
  11. package/plugins/huaweicloud-core/dist/agents/codex.js +9 -0
  12. package/plugins/huaweicloud-core/dist/agents/opencode-config.js +157 -31
  13. package/plugins/huaweicloud-core/dist/agents/opencode.js +129 -1
  14. package/plugins/huaweicloud-core/dist/approval-gate.js +1185 -0
  15. package/plugins/huaweicloud-core/dist/approval-ledger.js +2331 -0
  16. package/plugins/huaweicloud-core/dist/approval-roster.js +89 -0
  17. package/plugins/huaweicloud-core/dist/approval-types.js +1 -0
  18. package/plugins/huaweicloud-core/dist/approvals-cli.js +4 -4
  19. package/plugins/huaweicloud-core/dist/data/icons-manifest.v1.json +23 -5
  20. package/plugins/huaweicloud-core/dist/dep/receipts.js +65 -5
  21. package/plugins/huaweicloud-core/dist/hcloud-cli.js +1 -1
  22. package/plugins/huaweicloud-core/dist/hcloud-plan.js +77 -56
  23. package/plugins/huaweicloud-core/dist/lib/approval-mode.js +32 -0
  24. package/plugins/huaweicloud-core/dist/lib/approval-tokens.js +18 -4
  25. package/plugins/huaweicloud-core/dist/lib/call-trace.js +7 -14
  26. package/plugins/huaweicloud-core/dist/lib/confirm-language.js +50 -0
  27. package/plugins/huaweicloud-core/dist/lib/credentials.js +9 -9
  28. package/plugins/huaweicloud-core/dist/lib/hdkit/hdkit-gate.js +1398 -0
  29. package/plugins/huaweicloud-core/dist/lib/hdkit/hdkitservice-api.js +362 -32
  30. package/plugins/huaweicloud-core/dist/lib/hdkit/sandbox-pool.js +6 -16
  31. package/plugins/huaweicloud-core/dist/lib/opencode-ask-rule.js +262 -0
  32. package/plugins/huaweicloud-core/dist/lib/opencode-saved-perms.js +282 -0
  33. package/plugins/huaweicloud-core/dist/lib/supplemental-redaction.js +116 -0
  34. package/plugins/huaweicloud-core/dist/lib/tool-schema-parts.js +31 -0
  35. package/plugins/huaweicloud-core/dist/lib/workspace-glob.js +442 -0
  36. package/plugins/huaweicloud-core/dist/mcp-config-io.js +28 -1
  37. package/plugins/huaweicloud-core/dist/mcp-protocol.js +75 -4
  38. package/plugins/huaweicloud-core/dist/mcp-server.js +6560 -898
  39. package/plugins/huaweicloud-core/dist/mcp-server.js.map +4 -4
  40. package/plugins/huaweicloud-core/dist/mcp-stdio-transport.js +14 -4
  41. package/plugins/huaweicloud-core/dist/packs/auth/approval.js +249 -0
  42. package/plugins/huaweicloud-core/dist/packs/auth/tools.js +44 -44
  43. package/plugins/huaweicloud-core/dist/packs/huaweicloud-cli/tools.js +119 -112
  44. package/plugins/huaweicloud-core/dist/packs/obs/approval.js +110 -0
  45. package/plugins/huaweicloud-core/dist/packs/obs/tools.js +7 -2
  46. package/plugins/huaweicloud-core/dist/packs/registry.js +19 -1
  47. package/plugins/huaweicloud-core/dist/packs/sandbox/approval.js +245 -0
  48. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-account.js +156 -23
  49. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-credentials.js +13 -1
  50. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-hwexec.js +136 -0
  51. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-session.js +11 -1
  52. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-tunnel.js +1 -0
  53. package/plugins/huaweicloud-core/dist/packs/sandbox/schemas.js +79 -9
  54. package/plugins/huaweicloud-core/dist/packs/sandbox/tools.js +28 -9
  55. package/plugins/huaweicloud-core/dist/packs/secrets/approval.js +46 -0
  56. package/plugins/huaweicloud-core/dist/packs/secrets/tools.js +4 -1
  57. package/plugins/huaweicloud-core/dist/packs/update/approval.js +79 -0
  58. package/plugins/huaweicloud-core/dist/packs/update/tools.js +8 -1
  59. package/plugins/huaweicloud-core/dist/packs/voucher/tools.js +1 -1
  60. package/plugins/huaweicloud-core/dist/packs-configure.js +74 -0
  61. package/plugins/huaweicloud-core/dist/safety/classify-text.js +36 -0
  62. package/plugins/huaweicloud-core/dist/safety/policy.js +2 -0
  63. package/plugins/huaweicloud-core/dist/sandbox/deploy-check.js +1 -1
  64. package/plugins/huaweicloud-core/dist/sandbox/devbridge-auth.js +65 -14
  65. package/plugins/huaweicloud-core/dist/sandbox/devbridge-cleanup.js +422 -0
  66. package/plugins/huaweicloud-core/dist/sandbox/devbridge-expose-flow.js +66 -16
  67. package/plugins/huaweicloud-core/dist/sandbox/devbridge-expose.js +13 -10
  68. package/plugins/huaweicloud-core/dist/sandbox/devbridge-keepalive.js +42 -2
  69. package/plugins/huaweicloud-core/dist/sandbox/devbridge-key.js +11 -3
  70. package/plugins/huaweicloud-core/dist/sandbox/devbridge-tunnel.js +6 -0
  71. package/plugins/huaweicloud-core/dist/sandbox/devbridge-validation.js +10 -1
  72. package/plugins/huaweicloud-core/dist/sandbox/hwexec-task.js +244 -0
  73. package/plugins/huaweicloud-core/dist/setup-cli.js +42 -4
  74. package/plugins/huaweicloud-core/dist/tool-definitions.js +9 -1
  75. package/plugins/huaweicloud-core/dist/tool-schemas.js +32 -6
  76. package/plugins/huaweicloud-core/dist/tools.js +76 -9
  77. package/plugins/huaweicloud-core/dist/update-registry.js +358 -5
  78. package/plugins/huaweicloud-core/dist/update-upgrade.js +1083 -14
  79. package/plugins/huaweicloud-core/dist/vendor-gate.js +2 -2
  80. package/plugins/huaweicloud-core/hooks/huaweicloud-safety.mjs +150 -1
  81. package/plugins/huaweicloud-core/hooks/huaweicloud-safety.py +53 -3
  82. package/plugins/huaweicloud-core/openclaw.plugin.json +1 -1
  83. package/plugins/huaweicloud-core/safety/policy.json +18 -11
  84. package/plugins/huaweicloud-core/safety/rules/cloud-risk-rules.json +114 -10
  85. package/plugins/huaweicloud-core/skills/huawei-sandbox/SKILL.md +190 -138
  86. package/plugins/huaweicloud-core/skills/huawei-sandbox/references/resource-pool.md +36 -12
  87. package/plugins/huaweicloud-core/skills/huawei-voucher/SKILL.md +2 -0
  88. package/plugins/huaweicloud-core/skills/huaweicloud-cli-and-auth/SKILL.md +5 -5
  89. package/plugins/huaweicloud-core/skills/huaweicloud-safety/SKILL.md +13 -8
  90. package/plugins/huaweicloud-core/skills/huaweicloud-safety/references/approvals.md +78 -23
  91. package/plugins/huaweicloud-core/skills/huaweicloud-troubleshooting/SKILL.md +21 -19
  92. package/plugins/huaweicloud-core/src/agents/codex-config.ts +432 -0
  93. package/plugins/huaweicloud-core/src/agents/codex-desktop.ts +8 -1
  94. package/plugins/huaweicloud-core/src/agents/codex.ts +10 -0
  95. package/plugins/huaweicloud-core/src/agents/opencode-config.ts +198 -30
  96. package/plugins/huaweicloud-core/src/agents/opencode.ts +157 -1
  97. package/plugins/huaweicloud-core/src/approval-gate.ts +1306 -0
  98. package/plugins/huaweicloud-core/src/approval-ledger.ts +2446 -0
  99. package/plugins/huaweicloud-core/src/approval-roster.ts +127 -0
  100. package/plugins/huaweicloud-core/src/approval-types.ts +237 -0
  101. package/plugins/huaweicloud-core/src/approvals-cli.ts +4 -4
  102. package/plugins/huaweicloud-core/src/data/icons-manifest.v1.json +23 -5
  103. package/plugins/huaweicloud-core/src/dep/receipts.ts +84 -5
  104. package/plugins/huaweicloud-core/src/hcloud-cli.ts +3 -0
  105. package/plugins/huaweicloud-core/src/hcloud-plan.ts +93 -63
  106. package/plugins/huaweicloud-core/src/lib/approval-mode.ts +34 -0
  107. package/plugins/huaweicloud-core/src/lib/approval-tokens.ts +19 -4
  108. package/plugins/huaweicloud-core/src/lib/call-trace.ts +7 -17
  109. package/plugins/huaweicloud-core/src/lib/confirm-language.ts +51 -0
  110. package/plugins/huaweicloud-core/src/lib/credentials.ts +9 -8
  111. package/plugins/huaweicloud-core/src/lib/hdkit/hdkit-gate.ts +1428 -0
  112. package/plugins/huaweicloud-core/src/lib/hdkit/hdkitservice-api.ts +377 -30
  113. package/plugins/huaweicloud-core/src/lib/hdkit/sandbox-pool.ts +8 -15
  114. package/plugins/huaweicloud-core/src/lib/opencode-ask-rule.ts +350 -0
  115. package/plugins/huaweicloud-core/src/lib/opencode-saved-perms.ts +354 -0
  116. package/plugins/huaweicloud-core/src/lib/supplemental-redaction.ts +129 -0
  117. package/plugins/huaweicloud-core/src/lib/tool-schema-parts.ts +40 -0
  118. package/plugins/huaweicloud-core/src/lib/workspace-glob.ts +414 -0
  119. package/plugins/huaweicloud-core/src/mcp-config-io.ts +27 -1
  120. package/plugins/huaweicloud-core/src/mcp-protocol.ts +88 -5
  121. package/plugins/huaweicloud-core/src/mcp-server.ts +13 -0
  122. package/plugins/huaweicloud-core/src/mcp-stdio-transport.ts +14 -4
  123. package/plugins/huaweicloud-core/src/packs/auth/approval.ts +302 -0
  124. package/plugins/huaweicloud-core/src/packs/auth/tools.ts +54 -45
  125. package/plugins/huaweicloud-core/src/packs/huaweicloud-cli/tools.ts +139 -120
  126. package/plugins/huaweicloud-core/src/packs/obs/approval.ts +122 -0
  127. package/plugins/huaweicloud-core/src/packs/obs/tools.ts +7 -2
  128. package/plugins/huaweicloud-core/src/packs/registry.ts +42 -1
  129. package/plugins/huaweicloud-core/src/packs/sandbox/approval.ts +296 -0
  130. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-account.ts +168 -23
  131. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-credentials.ts +11 -1
  132. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-hwexec.ts +228 -0
  133. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-session.ts +11 -1
  134. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-tunnel.ts +1 -0
  135. package/plugins/huaweicloud-core/src/packs/sandbox/schemas.ts +100 -8
  136. package/plugins/huaweicloud-core/src/packs/sandbox/tools.ts +31 -8
  137. package/plugins/huaweicloud-core/src/packs/secrets/approval.ts +50 -0
  138. package/plugins/huaweicloud-core/src/packs/secrets/tools.ts +4 -1
  139. package/plugins/huaweicloud-core/src/packs/update/approval.ts +87 -0
  140. package/plugins/huaweicloud-core/src/packs/update/tools.ts +8 -1
  141. package/plugins/huaweicloud-core/src/packs/voucher/tools.ts +1 -1
  142. package/plugins/huaweicloud-core/src/packs-configure.ts +75 -0
  143. package/plugins/huaweicloud-core/src/safety/classify-text.ts +40 -0
  144. package/plugins/huaweicloud-core/src/safety/policy.ts +7 -0
  145. package/plugins/huaweicloud-core/src/sandbox/deploy-check.ts +1 -1
  146. package/plugins/huaweicloud-core/src/sandbox/devbridge-auth.ts +74 -14
  147. package/plugins/huaweicloud-core/src/sandbox/devbridge-cleanup.ts +509 -0
  148. package/plugins/huaweicloud-core/src/sandbox/devbridge-expose-flow.ts +70 -18
  149. package/plugins/huaweicloud-core/src/sandbox/devbridge-expose.ts +26 -10
  150. package/plugins/huaweicloud-core/src/sandbox/devbridge-keepalive.ts +41 -2
  151. package/plugins/huaweicloud-core/src/sandbox/devbridge-key.ts +9 -3
  152. package/plugins/huaweicloud-core/src/sandbox/devbridge-tunnel.ts +24 -0
  153. package/plugins/huaweicloud-core/src/sandbox/devbridge-validation.ts +11 -1
  154. package/plugins/huaweicloud-core/src/sandbox/hwexec-task.ts +304 -0
  155. package/plugins/huaweicloud-core/src/setup-cli.ts +45 -3
  156. package/plugins/huaweicloud-core/src/tool-definitions.ts +9 -1
  157. package/plugins/huaweicloud-core/src/tool-schemas.ts +39 -6
  158. package/plugins/huaweicloud-core/src/tools.ts +124 -10
  159. package/plugins/huaweicloud-core/src/update-registry.ts +340 -5
  160. package/plugins/huaweicloud-core/src/update-upgrade.ts +1130 -14
  161. package/plugins/huaweicloud-core/src/vendor-gate.ts +2 -2
@@ -0,0 +1,432 @@
1
+ // The codex client approval yield: per-tool `approval_mode` config for the
2
+ // devkit's MCP server — the codex half of the approval-chain yield (PR-5).
3
+ //
4
+ // Under codex's default approvalPolicy ("on-request") codex fires its OWN
5
+ // approval gate ahead of every MCP tool call, so a roster call is a double
6
+ // interaction: the codex dialog first, the devkit server's confirmation
7
+ // second (measured on codex-cli 0.167.2, probe home
8
+ // /tmp/opencode/probe-non-elicit-dlg — the same geometry PR-2 removed on
9
+ // opencode). The yield is the per-tool value the codex config understands,
10
+ // measured live on the same build: `approve` runs the call straight through
11
+ // with no codex dialog, so the devkit's own confirmation is the single
12
+ // surface. `prompt` shows an Allow/Cancel dialog with the tool name and up
13
+ // to 3 truncated argument lines (no persistence options); `auto` keeps
14
+ // codex's 4-option gate with persistence options. Neither fails anything
15
+ // open — the server gate confirms every roster call regardless of the
16
+ // client value — so the doctor treats every non-approve value as a WARN
17
+ // naming its direction, never a FAIL: a wrong value degrades UX (double
18
+ // interaction) or fails closed, and WARN is the honest ceiling. `prompt` is
19
+ // the taught fallback for the policy-off branch only, never a written value.
20
+ //
21
+ // WHERE the yield lives, settled by live measurement against codex 0.167.2
22
+ // plus the upstream source (core mcp_tool_call.rs,
23
+ // resolve_user_configured_approval_mode): codex reads per-tool
24
+ // approval_mode from TWO places, in order — the config layer stack's
25
+ // mcp_servers.<server>.tools.<tool> (the user's config.toml), then the
26
+ // PLUGIN-provided server definition (plugin.mcp_servers.<server>.tools.<tool>).
27
+ // The devkit server is plugin-provided on every current install (the CLI
28
+ // marketplace flow copies plugins/huaweicloud-core — including .mcp.json —
29
+ // into codex's cache; Codex Desktop reads the plugin dir), so the yield
30
+ // belongs in the devkit-owned .mcp.json. Writing config.toml instead is
31
+ // actively harmful: a tools table under [mcp_servers.huaweicloud-devkit]
32
+ // implies a config-declared server, and with no command/url codex refuses
33
+ // to load the whole config ("Error: failed to load bootstrap configuration
34
+ // — Caused by: invalid transport in `mcp_servers.huaweicloud-devkit`",
35
+ // measured live; the probe's config.toml always carried a transport for
36
+ // its probe server, which is why the plan's original config.toml dialect
37
+ // never met this shape). The installer therefore never touches the user's
38
+ // config.toml; a user's own per-tool value there WINS over the payload
39
+ // (the source's precedence), and the doctor surfaces it as a WARN.
40
+ //
41
+ // Ownership contracts: the payload .mcp.json is devkit-owned end to end —
42
+ // regenerated on every deploy, removed with the plugin dir / cache on
43
+ // uninstall — so the yield needs no add-only-if-absent or value-exact
44
+ // machinery. The user's config.toml is never written, so its values are
45
+ // user-owned by construction; the doctor reports them by name.
46
+ //
47
+ // Policy directions the doctor names, both measured on 0.167.2: codex's
48
+ // granular `mcp_elicitations = false` auto-declines every server form and
49
+ // substitutes codex's built-in gate, so every roster call fails CLOSED at
50
+ // the devkit gate until the policy is on — the remediation names the flip.
51
+ // A top-level `approval_policy = "never"` blocks MCP tool calls outright,
52
+ // ahead of the devkit gate.
53
+
54
+ import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
55
+ import { homedir } from 'node:os';
56
+ import { join } from 'node:path';
57
+
58
+ import { APPROVAL_MCP_SERVER, APPROVAL_TOOLS } from '../approval-roster.ts';
59
+ import { codexPluginProbe, getCodexPluginName, getMarketplaceName } from '../codex-probe.ts';
60
+ import { codexDesktopPluginsDir } from '../targets.ts';
61
+ import { asRecord } from '../lib/as-record.ts';
62
+ import type { DoctorReporter } from '../doctor-report.ts';
63
+
64
+ // The yield value the devkit owns. Never "prompt" or "auto": those are
65
+ // remediation shapes for the policy-off branch, taught by the doctor only.
66
+ export const CODEX_APPROVAL_MODE = 'approve';
67
+
68
+ // The ask-mode env the payload .mcp.json carries beside the yield: codex
69
+ // passes the plugin-provided server entry's env to the MCP child (live-
70
+ // measured on codex-cli 0.167.2 — a roster call returned ELICITATION_DECLINED,
71
+ // the headless auto-decline of the server-raised form, proving both the
72
+ // passthrough and the capable-client ladder). The payload is the devkit-owned
73
+ // channel: there is no codex-side override for plugin-provided env, so an
74
+ // operator wanting token mode on codex edits their cached copy (and re-runs
75
+ // the install to restore it) — the doctor flags the drift.
76
+ export const CODEX_APPROVAL_MODE_ENV = 'ask';
77
+
78
+ // The `tools` map the payload .mcp.json carries inside the devkit's server
79
+ // entry — one per-tool block per roster tool, roster-driven so a
80
+ // registration PR widens the yield with the roster, not by hand. JSON
81
+ // object key order matches the roster order; callers that deep-compare
82
+ // should compare structurally, not by serialization.
83
+ export function codexApprovalToolsBlock(): Record<string, { approval_mode: string }> {
84
+ return Object.fromEntries(APPROVAL_TOOLS.map((tool) => [tool, { approval_mode: CODEX_APPROVAL_MODE }]));
85
+ }
86
+
87
+ // The ask-mode env half of the payload posture, as a record for entry writes
88
+ // (writeMcpServersFile merges it under the same `agentKey !== 'openclaw'`
89
+ // condition as the tools block, with the same wholesale drift healing).
90
+ export function codexApprovalEnv(): Record<string, string> {
91
+ return { DEVKIT_APPROVAL_MODE: CODEX_APPROVAL_MODE_ENV };
92
+ }
93
+
94
+ // ── Reading the yield ──
95
+
96
+ // The effective state of one roster tool's approval_mode.
97
+ // approve the yield value — the call runs straight through
98
+ // prompt an Allow/Cancel dialog fires ahead of the server's confirmation
99
+ // auto codex's 4-option gate (with persistence) fires ahead of it
100
+ // foreign any other value (e.g. "writes"), or no approval_mode key
101
+ // absent no entry for the tool
102
+ export type CodexApprovalState = 'approve' | 'prompt' | 'auto' | 'foreign' | 'absent';
103
+
104
+ function classifyMode(mode: string | undefined): CodexApprovalState {
105
+ if (mode === 'approve' || mode === 'prompt' || mode === 'auto') return mode;
106
+ return mode === undefined ? 'absent' : 'foreign';
107
+ }
108
+
109
+ // The per-tool states from a parsed .mcp.json (the payload shape codex's
110
+ // plugin loader reads): mcpServers.<server>.tools.<tool>.approval_mode. A
111
+ // file without our server entry reads as all-absent (a foreign server map
112
+ // never leaks in — asRecord drops nothing, the server key is exact).
113
+ export function readMcpJsonApprovalStates(
114
+ config: Record<string, unknown>,
115
+ tools: readonly string[] = APPROVAL_TOOLS,
116
+ ): CodexApprovalState[] {
117
+ const server = asRecord(asRecord(config.mcpServers)[APPROVAL_MCP_SERVER]);
118
+ const blocks = asRecord(server.tools);
119
+ return tools.map((tool) => {
120
+ const mode = asRecord(blocks[tool]).approval_mode;
121
+ return classifyMode(typeof mode === 'string' ? mode : undefined);
122
+ });
123
+ }
124
+
125
+ // Any mcp_servers tool table header in config.toml: the server segment is
126
+ // captured so the reader can pin the devkit's own server name instead of a
127
+ // regex that would silently accept a lookalike server. Trailing comments
128
+ // are tolerated (valid TOML) so a commented header still reads as the block
129
+ // it is, not as absence.
130
+ const BLOCK_HEADER_RE = /^\s*\[mcp_servers\.([A-Za-z0-9_-]+)\.tools\.([A-Za-z0-9_-]+)\]\s*(?:#.*)?$/;
131
+ const APPROVAL_MODE_LINE_RE = /^\s*approval_mode\s*=\s*"([^"]*)"\s*(?:#.*)?$/;
132
+ const ANY_TABLE_RE = /^\s*\[/;
133
+
134
+ // Reads every roster tool's per-tool state from the USER's config.toml
135
+ // text — the config-layer override channel, which the upstream source
136
+ // resolves BEFORE the plugin payload, so a value here wins over our yield.
137
+ // A block runs from its header to the next table header (or EOF); the FIRST
138
+ // approval_mode assignment in the body decides. Blocks under a different
139
+ // server name, or on a non-roster tool, are the user's own config and never
140
+ // read.
141
+ export function readCodexApprovalBlocks(text: string, tools: readonly string[] = APPROVAL_TOOLS): CodexApprovalState[] {
142
+ const wanted = new Set(tools);
143
+ const state = new Map<string, CodexApprovalState>(tools.map((tool) => [tool, 'absent' as const]));
144
+ let inBlock: string | null = null;
145
+ let mode: string | undefined;
146
+ const closeBlock = (): void => {
147
+ if (inBlock !== null) state.set(inBlock, classifyMode(mode));
148
+ inBlock = null;
149
+ mode = undefined;
150
+ };
151
+ for (const line of text.split(/\r?\n/)) {
152
+ const header = BLOCK_HEADER_RE.exec(line);
153
+ if (header) {
154
+ closeBlock();
155
+ if (header[1] === APPROVAL_MCP_SERVER && wanted.has(header[2]!)) inBlock = header[2]!;
156
+ continue;
157
+ }
158
+ if (inBlock === null || mode !== undefined) continue;
159
+ const assignment = APPROVAL_MODE_LINE_RE.exec(line);
160
+ if (assignment) mode = assignment[1];
161
+ }
162
+ closeBlock();
163
+ return tools.map((tool) => state.get(tool)!);
164
+ }
165
+
166
+ // codex's granular approval policy, measured on 0.167.2: with
167
+ // `mcp_elicitations = false` codex auto-declines every server form and
168
+ // substitutes its built-in gate, so every roster call fails CLOSED at the
169
+ // devkit gate until the policy is on. codex accepts TWO spellings of the
170
+ // value (both live-measured): the [approval_policy.granular] table and
171
+ // the inline table `approval_policy = { granular = { mcp_elicitations =
172
+ // false, … } }` — the inline spelling is exactly the machine the
173
+ // remediation-naming contract exists for, so the detector is a whole-file
174
+ // match on the false spelling. The negative lookbehind keeps lookalike
175
+ // keys (`allow_mcp_elicitations = false`) from tripping it; `true` is the
176
+ // healthy granular spelling and never does.
177
+ export function codexElicitPolicyOff(text: string): boolean {
178
+ return /(?<![A-Za-z_])mcp_elicitations\s*=\s*false\b/.test(text);
179
+ }
180
+
181
+ // A top-level `approval_policy = "never"` blocks MCP tool calls outright,
182
+ // ahead of the devkit gate. TOML pins top-level keys before the first table
183
+ // header, so the scan stops at the first header — a [projects.*]-scoped
184
+ // spelling must not be misread as the machine's posture.
185
+ export function codexApprovalPolicyNever(text: string): boolean {
186
+ for (const line of text.split(/\r?\n/)) {
187
+ if (ANY_TABLE_RE.test(line)) return false;
188
+ if (/^\s*approval_policy\s*=\s*"never"\s*(#.*)?$/.test(line)) return true;
189
+ }
190
+ return false;
191
+ }
192
+
193
+ // The user-override spellings codex honors that this doctor cannot parse
194
+ // (a full TOML parser is deliberately out of scope): dotted keys
195
+ // (mcp_servers.<server>.tools.<tool>.approval_mode = "…") and inline
196
+ // tables (tools = { <tool> = { approval_mode = "…" } } inside our server's
197
+ // section). Both live-verified to win over the payload, so a value in
198
+ // either shape must never read as absence — the doctor reports the
199
+ // dialect instead, the same honesty as the unreadable-config WARN.
200
+ export function codexOverridesUnparseableDialect(text: string): boolean {
201
+ let inOurServer = false;
202
+ for (const line of text.split(/\r?\n/)) {
203
+ const header = /^\s*\[mcp_servers\.([A-Za-z0-9_-]+)[.\]]/.exec(line);
204
+ if (header) {
205
+ inOurServer = header[1] === APPROVAL_MCP_SERVER;
206
+ continue;
207
+ }
208
+ if (ANY_TABLE_RE.test(line)) {
209
+ inOurServer = false;
210
+ continue;
211
+ }
212
+ const dotted = /^\s*mcp_servers\.([A-Za-z0-9_-]+)\.tools\./.exec(line);
213
+ if (dotted && dotted[1] === APPROVAL_MCP_SERVER) return true;
214
+ if (inOurServer && /^\s*tools\s*=\s*\{/.test(line)) return true;
215
+ }
216
+ return false;
217
+ }
218
+
219
+ // ── The payload candidates (what codex actually loads) ──
220
+
221
+ // The devkit plugin's cache dir under CODEX_HOME — the CLI marketplace
222
+ // flow's payload home. CODEX_HOME mirrors the resolution codex.ts's cache
223
+ // cleanup uses; codex resolves its whole home from this override.
224
+ export function codexPluginCacheDir(): string {
225
+ return join(
226
+ process.env.CODEX_HOME || join(homedir(), '.codex'),
227
+ 'plugins',
228
+ 'cache',
229
+ getMarketplaceName(),
230
+ getCodexPluginName(),
231
+ );
232
+ }
233
+
234
+ // Every .mcp.json under the cache dir (one per version dir). Every copy is
235
+ // verified, not just the newest: which version codex loads is codex's
236
+ // call, and a stale copy missing the yield changes that machine's posture.
237
+ // A missing cache dir reads as no candidates (not installed that way).
238
+ export function codexCacheMcpJsonPaths(): string[] {
239
+ const cacheDir = codexPluginCacheDir();
240
+ if (!existsSync(cacheDir)) return [];
241
+ let entries: string[];
242
+ try {
243
+ entries = readdirSync(cacheDir);
244
+ } catch {
245
+ return [];
246
+ }
247
+ const out: string[] = [];
248
+ for (const name of entries) {
249
+ const path = join(cacheDir, name, '.mcp.json');
250
+ try {
251
+ if (statSync(path).isFile()) out.push(path);
252
+ } catch {
253
+ // unreadable or not a file — not a candidate
254
+ }
255
+ }
256
+ return out;
257
+ }
258
+
259
+ // The Codex Desktop payload's .mcp.json (the desktop deploy's plugin dir).
260
+ export function codexDesktopMcpJsonPath(): string {
261
+ return join(codexDesktopPluginsDir(), '.mcp.json');
262
+ }
263
+
264
+ // The doctor's shared reading: which roster tools sit in each non-approve
265
+ // state. Both channels (payload .mcp.json, config.toml overrides) ask the
266
+ // same question, so the derivation has one home.
267
+ function toolsInStates(
268
+ states: readonly CodexApprovalState[],
269
+ ): Record<'prompt' | 'auto' | 'foreign' | 'absent', string> {
270
+ const names = (wanted: CodexApprovalState): string =>
271
+ APPROVAL_TOOLS.filter((_tool, index) => states[index] === wanted).join(', ');
272
+ return {
273
+ prompt: names('prompt'),
274
+ auto: names('auto'),
275
+ foreign: names('foreign'),
276
+ absent: names('absent'),
277
+ };
278
+ }
279
+
280
+ // The payload half of the doctor row: every cached copy that carries our
281
+ // server must carry the full yield; a stale or hand-stripped copy is
282
+ // drift, healable by re-running the install (the payload is regenerated
283
+ // wholesale). A relevant machine with NO payload file at all gets a WARN,
284
+ // never a vacuous pass — the yield cannot be verified without the file
285
+ // codex reads it from.
286
+ function payloadDriftWarns(payloadPaths: readonly string[]): string[] {
287
+ const warns: string[] = [];
288
+ if (payloadPaths.length === 0) {
289
+ warns.push(
290
+ `no devkit payload .mcp.json was found in the codex plugin cache or the Codex Desktop plugin dir, so the yield cannot be verified (${RERUN})`,
291
+ );
292
+ return warns;
293
+ }
294
+ for (const path of payloadPaths) {
295
+ let config: Record<string, unknown>;
296
+ let states: CodexApprovalState[];
297
+ try {
298
+ config = asRecord(JSON.parse(readFileSync(path, 'utf8')));
299
+ states = readMcpJsonApprovalStates(config);
300
+ } catch {
301
+ warns.push(`${path} is unparseable, so its yield cannot be verified (${RERUN})`);
302
+ continue;
303
+ }
304
+ // The env half: a copy that carries our server entry but not the ask env
305
+ // runs the devkit in token mode (a stale pre-ask payload) — drift,
306
+ // healable by the same re-run.
307
+ if (
308
+ asRecord(asRecord(asRecord(config.mcpServers)[APPROVAL_MCP_SERVER]).env).DEVKIT_APPROVAL_MODE !==
309
+ CODEX_APPROVAL_MODE_ENV
310
+ ) {
311
+ warns.push(
312
+ `${path} does not set DEVKIT_APPROVAL_MODE="${CODEX_APPROVAL_MODE_ENV}" in the server env, so the devkit runs its default token mode under codex (${RERUN})`,
313
+ );
314
+ }
315
+ if (states.every((state) => state === 'approve')) continue;
316
+ const { prompt, auto, foreign, absent } = toolsInStates(states);
317
+ if (prompt) warns.push(`approval_mode "prompt" on ${prompt} in ${path} (double interaction) (${RERUN})`);
318
+ if (auto)
319
+ warns.push(
320
+ `approval_mode "auto" on ${auto} in ${path} keeps codex's own gate ahead of the devkit's confirmation (${RERUN})`,
321
+ );
322
+ if (foreign)
323
+ warns.push(
324
+ `approval_mode on ${foreign} in ${path} is not "approve", so codex's own approval gate may fire ahead of the devkit's confirmation (${RERUN})`,
325
+ );
326
+ if (absent) warns.push(`the approve block is missing for ${absent} in ${path} (${RERUN})`);
327
+ }
328
+ return warns;
329
+ }
330
+
331
+ // ── The doctor row ──
332
+
333
+ const CODEX_POSTURE_LABEL = 'Codex approval posture';
334
+ const RERUN = 're-run: npx huaweicloud-devkit install --target codex (or update)';
335
+
336
+ // Verifies the yield where codex reads it. Self-gating, silent on machines
337
+ // the devkit has no codex relationship with: no payload .mcp.json (cache or
338
+ // desktop), no codex plugin answering installed, no desktop marketplace
339
+ // registration. `desktopRegistered` is passed in by the doctor shell
340
+ // (codexDesktopDoctorMcpConfigured already runs there as an MCP-configured
341
+ // candidate).
342
+ export function codexDoctorApprovalRow(ctx: DoctorReporter, desktopRegistered: boolean): void {
343
+ const payloadPaths = [...codexCacheMcpJsonPaths(), codexDesktopMcpJsonPath()].filter((p) => existsSync(p));
344
+
345
+ // The user override + policy channel. An absent config.toml is the
346
+ // healthy no-overrides default (nothing to warn about); a present but
347
+ // unreadable one cannot be verified, which earns a WARN below when the
348
+ // machine has a devkit payload.
349
+ const configPath = join(process.env.CODEX_HOME || join(homedir(), '.codex'), 'config.toml');
350
+ let configText: string | null = null;
351
+ let configUnreadable = false;
352
+ try {
353
+ configText = readFileSync(configPath, 'utf8');
354
+ } catch (error) {
355
+ configUnreadable = (error as NodeJS.ErrnoException).code !== 'ENOENT';
356
+ }
357
+
358
+ let relevant = payloadPaths.length > 0 || desktopRegistered;
359
+ if (!relevant) {
360
+ const probe = codexPluginProbe();
361
+ relevant = probe.ok && probe.installed;
362
+ }
363
+ if (!relevant) return;
364
+
365
+ // 1. The payload half — every copy that carries our server must carry
366
+ // the full yield; a stale or hand-stripped copy is drift, healable by
367
+ // re-running the install (the payload is regenerated wholesale).
368
+ const payloadWarns = payloadDriftWarns(payloadPaths);
369
+
370
+ // 2. The user override half (config.toml) — a value here WINS over the
371
+ // payload (the config layer resolves first, upstream source), so it
372
+ // decides the machine's actual posture; WARN, never FAIL: no client
373
+ // value fails anything open. Overrides in a dialect this doctor cannot
374
+ // parse (dotted keys, inline tables) get the dialect WARN, never
375
+ // silence — reading them as absence would hide the winning value.
376
+ const overrideWarns: string[] = [];
377
+ if (configText !== null) {
378
+ if (codexOverridesUnparseableDialect(configText)) {
379
+ overrideWarns.push(
380
+ `the user's config.toml carries per-tool overrides in a dialect this doctor cannot parse (dotted keys or inline tables), so the winning values cannot be verified — codex's own approval gate may fire ahead of the devkit's confirmation`,
381
+ );
382
+ }
383
+ const { prompt, auto, foreign } = toolsInStates(readCodexApprovalBlocks(configText));
384
+ if (prompt) {
385
+ overrideWarns.push(
386
+ `the user's config.toml sets approval_mode "prompt" on ${prompt}, which wins over the payload and shows codex's own Allow/Cancel dialog ahead of the devkit's confirmation (double interaction)`,
387
+ );
388
+ }
389
+ if (auto) {
390
+ overrideWarns.push(
391
+ `the user's config.toml sets approval_mode "auto" on ${auto}, which wins over the payload and keeps codex's own approval gate (with persistence options) ahead of the devkit's confirmation`,
392
+ );
393
+ }
394
+ if (foreign) {
395
+ overrideWarns.push(
396
+ `the user's config.toml sets approval_mode on ${foreign}, which wins over the payload — codex's own approval gate may fire ahead of the devkit's confirmation`,
397
+ );
398
+ }
399
+ }
400
+
401
+ // 3. The policy directions, both config.toml-borne.
402
+ const policyWarns: string[] = [];
403
+ if (configText !== null) {
404
+ if (codexElicitPolicyOff(configText)) {
405
+ policyWarns.push(
406
+ `codex's granular policy sets mcp_elicitations = false, so codex auto-declines every devkit confirmation form and every roster call fails closed at the devkit gate until the policy is on — flip it: set mcp_elicitations = true (or drop the granular approval_policy) in ${configPath}`,
407
+ );
408
+ }
409
+ if (codexApprovalPolicyNever(configText)) {
410
+ policyWarns.push(
411
+ `approval_policy = "never" blocks MCP tool calls outright, ahead of the devkit gate — choose a policy that can ask, e.g. approval_policy = "on-request"`,
412
+ );
413
+ }
414
+ } else if (configUnreadable) {
415
+ // A devkit payload exists but the override channel is unreadable, so
416
+ // user per-tool overrides and the approval policy cannot be verified.
417
+ overrideWarns.push(
418
+ `the codex config at ${configPath} is unreadable, so user per-tool overrides and the approval policy cannot be verified`,
419
+ );
420
+ }
421
+
422
+ const warns = [...payloadWarns, ...overrideWarns, ...policyWarns];
423
+ if (warns.length === 0) {
424
+ ctx.check(
425
+ `${CODEX_POSTURE_LABEL} (${APPROVAL_TOOLS.length} per-tool approve blocks + DEVKIT_APPROVAL_MODE="${CODEX_APPROVAL_MODE_ENV}" in the devkit payload)`,
426
+ true,
427
+ '',
428
+ );
429
+ } else {
430
+ ctx.warn(`${CODEX_POSTURE_LABEL}: ${warns.join(' ')}`);
431
+ }
432
+ }
@@ -199,7 +199,14 @@ export function uninstallCodexDesktop(): void {
199
199
  console.log(' Codex plugin cache cleaned');
200
200
  }
201
201
 
202
- // Clean up old config.toml section from pre-marketplace era
202
+ // Clean up old config.toml section from pre-marketplace era. The sweep is
203
+ // wholesale trunk behavior: EVERY [mcp_servers.huaweicloud-devkit…] table
204
+ // goes, including user-authored subtables under our server name (a user
205
+ // tools block vanishes with ours, and the doctor never sees it after the
206
+ // uninstall). The devkit's own yield never lands in config.toml at all
207
+ // (agents/codex-config.ts records why), so this pass removes legacy
208
+ // server sections plus whatever users placed under that name — nothing
209
+ // the current installer writes.
203
210
  const configPath = join(homedir(), '.codex', 'config.toml');
204
211
  if (existsSync(configPath)) {
205
212
  const lines = readFileSync(configPath, 'utf8').split(/\r?\n/);
@@ -148,6 +148,16 @@ export function installCodex(): boolean {
148
148
  return false;
149
149
  }
150
150
 
151
+ // The client yield (PR-5) needs no step here: the per-tool approve blocks
152
+ // ship inside the plugin payload's own .mcp.json (the repo's
153
+ // plugins/huaweicloud-core/.mcp.json, which `codex plugin add` copies
154
+ // into its cache) — the file codex reads per-tool approval_mode from for
155
+ // a plugin-provided server (agents/codex-config.ts). The user's
156
+ // config.toml is never touched: a tools table under
157
+ // [mcp_servers.huaweicloud-devkit] would imply a config-declared server,
158
+ // and without a transport codex refuses to load the whole config
159
+ // (live-measured "invalid transport" on codex-cli 0.167.2).
160
+
151
161
  return true;
152
162
  }
153
163