huaweicloud-devkit 2.0.0-next.3 → 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 +141 -86
  13. package/plugins/huaweicloud-core/dist/agents/opencode.js +106 -39
  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 +62 -61
  23. package/plugins/huaweicloud-core/dist/lib/approval-mode.js +7 -5
  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 +224 -78
  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 +68 -16
  38. package/plugins/huaweicloud-core/dist/mcp-server.js +6506 -912
  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 +85 -130
  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 +36 -1
  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 +2 -2
  89. package/plugins/huaweicloud-core/skills/huaweicloud-safety/SKILL.md +11 -7
  90. package/plugins/huaweicloud-core/skills/huaweicloud-safety/references/approvals.md +74 -29
  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 +176 -88
  96. package/plugins/huaweicloud-core/src/agents/opencode.ts +125 -54
  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 +68 -70
  106. package/plugins/huaweicloud-core/src/lib/approval-mode.ts +7 -5
  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 +302 -83
  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 +81 -17
  121. package/plugins/huaweicloud-core/src/mcp-server.ts +7 -7
  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 +99 -137
  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 +39 -1
  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,374 @@
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
+ import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
54
+ import { homedir } from 'node:os';
55
+ import { join } from 'node:path';
56
+ import { APPROVAL_MCP_SERVER, APPROVAL_TOOLS } from "../approval-roster.js";
57
+ import { codexPluginProbe, getCodexPluginName, getMarketplaceName } from "../codex-probe.js";
58
+ import { codexDesktopPluginsDir } from "../targets.js";
59
+ import { asRecord } from "../lib/as-record.js";
60
+ // The yield value the devkit owns. Never "prompt" or "auto": those are
61
+ // remediation shapes for the policy-off branch, taught by the doctor only.
62
+ export const CODEX_APPROVAL_MODE = 'approve';
63
+ // The ask-mode env the payload .mcp.json carries beside the yield: codex
64
+ // passes the plugin-provided server entry's env to the MCP child (live-
65
+ // measured on codex-cli 0.167.2 — a roster call returned ELICITATION_DECLINED,
66
+ // the headless auto-decline of the server-raised form, proving both the
67
+ // passthrough and the capable-client ladder). The payload is the devkit-owned
68
+ // channel: there is no codex-side override for plugin-provided env, so an
69
+ // operator wanting token mode on codex edits their cached copy (and re-runs
70
+ // the install to restore it) — the doctor flags the drift.
71
+ export const CODEX_APPROVAL_MODE_ENV = 'ask';
72
+ // The `tools` map the payload .mcp.json carries inside the devkit's server
73
+ // entry — one per-tool block per roster tool, roster-driven so a
74
+ // registration PR widens the yield with the roster, not by hand. JSON
75
+ // object key order matches the roster order; callers that deep-compare
76
+ // should compare structurally, not by serialization.
77
+ export function codexApprovalToolsBlock() {
78
+ return Object.fromEntries(APPROVAL_TOOLS.map((tool) => [tool, { approval_mode: CODEX_APPROVAL_MODE }]));
79
+ }
80
+ // The ask-mode env half of the payload posture, as a record for entry writes
81
+ // (writeMcpServersFile merges it under the same `agentKey !== 'openclaw'`
82
+ // condition as the tools block, with the same wholesale drift healing).
83
+ export function codexApprovalEnv() {
84
+ return { DEVKIT_APPROVAL_MODE: CODEX_APPROVAL_MODE_ENV };
85
+ }
86
+ function classifyMode(mode) {
87
+ if (mode === 'approve' || mode === 'prompt' || mode === 'auto')
88
+ return mode;
89
+ return mode === undefined ? 'absent' : 'foreign';
90
+ }
91
+ // The per-tool states from a parsed .mcp.json (the payload shape codex's
92
+ // plugin loader reads): mcpServers.<server>.tools.<tool>.approval_mode. A
93
+ // file without our server entry reads as all-absent (a foreign server map
94
+ // never leaks in — asRecord drops nothing, the server key is exact).
95
+ export function readMcpJsonApprovalStates(config, tools = APPROVAL_TOOLS) {
96
+ const server = asRecord(asRecord(config.mcpServers)[APPROVAL_MCP_SERVER]);
97
+ const blocks = asRecord(server.tools);
98
+ return tools.map((tool) => {
99
+ const mode = asRecord(blocks[tool]).approval_mode;
100
+ return classifyMode(typeof mode === 'string' ? mode : undefined);
101
+ });
102
+ }
103
+ // Any mcp_servers tool table header in config.toml: the server segment is
104
+ // captured so the reader can pin the devkit's own server name instead of a
105
+ // regex that would silently accept a lookalike server. Trailing comments
106
+ // are tolerated (valid TOML) so a commented header still reads as the block
107
+ // it is, not as absence.
108
+ const BLOCK_HEADER_RE = /^\s*\[mcp_servers\.([A-Za-z0-9_-]+)\.tools\.([A-Za-z0-9_-]+)\]\s*(?:#.*)?$/;
109
+ const APPROVAL_MODE_LINE_RE = /^\s*approval_mode\s*=\s*"([^"]*)"\s*(?:#.*)?$/;
110
+ const ANY_TABLE_RE = /^\s*\[/;
111
+ // Reads every roster tool's per-tool state from the USER's config.toml
112
+ // text — the config-layer override channel, which the upstream source
113
+ // resolves BEFORE the plugin payload, so a value here wins over our yield.
114
+ // A block runs from its header to the next table header (or EOF); the FIRST
115
+ // approval_mode assignment in the body decides. Blocks under a different
116
+ // server name, or on a non-roster tool, are the user's own config and never
117
+ // read.
118
+ export function readCodexApprovalBlocks(text, tools = APPROVAL_TOOLS) {
119
+ const wanted = new Set(tools);
120
+ const state = new Map(tools.map((tool) => [tool, 'absent']));
121
+ let inBlock = null;
122
+ let mode;
123
+ const closeBlock = () => {
124
+ if (inBlock !== null)
125
+ state.set(inBlock, classifyMode(mode));
126
+ inBlock = null;
127
+ mode = undefined;
128
+ };
129
+ for (const line of text.split(/\r?\n/)) {
130
+ const header = BLOCK_HEADER_RE.exec(line);
131
+ if (header) {
132
+ closeBlock();
133
+ if (header[1] === APPROVAL_MCP_SERVER && wanted.has(header[2]))
134
+ inBlock = header[2];
135
+ continue;
136
+ }
137
+ if (inBlock === null || mode !== undefined)
138
+ continue;
139
+ const assignment = APPROVAL_MODE_LINE_RE.exec(line);
140
+ if (assignment)
141
+ mode = assignment[1];
142
+ }
143
+ closeBlock();
144
+ return tools.map((tool) => state.get(tool));
145
+ }
146
+ // codex's granular approval policy, measured on 0.167.2: with
147
+ // `mcp_elicitations = false` codex auto-declines every server form and
148
+ // substitutes its built-in gate, so every roster call fails CLOSED at the
149
+ // devkit gate until the policy is on. codex accepts TWO spellings of the
150
+ // value (both live-measured): the [approval_policy.granular] table and
151
+ // the inline table `approval_policy = { granular = { mcp_elicitations =
152
+ // false, … } }` — the inline spelling is exactly the machine the
153
+ // remediation-naming contract exists for, so the detector is a whole-file
154
+ // match on the false spelling. The negative lookbehind keeps lookalike
155
+ // keys (`allow_mcp_elicitations = false`) from tripping it; `true` is the
156
+ // healthy granular spelling and never does.
157
+ export function codexElicitPolicyOff(text) {
158
+ return /(?<![A-Za-z_])mcp_elicitations\s*=\s*false\b/.test(text);
159
+ }
160
+ // A top-level `approval_policy = "never"` blocks MCP tool calls outright,
161
+ // ahead of the devkit gate. TOML pins top-level keys before the first table
162
+ // header, so the scan stops at the first header — a [projects.*]-scoped
163
+ // spelling must not be misread as the machine's posture.
164
+ export function codexApprovalPolicyNever(text) {
165
+ for (const line of text.split(/\r?\n/)) {
166
+ if (ANY_TABLE_RE.test(line))
167
+ return false;
168
+ if (/^\s*approval_policy\s*=\s*"never"\s*(#.*)?$/.test(line))
169
+ return true;
170
+ }
171
+ return false;
172
+ }
173
+ // The user-override spellings codex honors that this doctor cannot parse
174
+ // (a full TOML parser is deliberately out of scope): dotted keys
175
+ // (mcp_servers.<server>.tools.<tool>.approval_mode = "…") and inline
176
+ // tables (tools = { <tool> = { approval_mode = "…" } } inside our server's
177
+ // section). Both live-verified to win over the payload, so a value in
178
+ // either shape must never read as absence — the doctor reports the
179
+ // dialect instead, the same honesty as the unreadable-config WARN.
180
+ export function codexOverridesUnparseableDialect(text) {
181
+ let inOurServer = false;
182
+ for (const line of text.split(/\r?\n/)) {
183
+ const header = /^\s*\[mcp_servers\.([A-Za-z0-9_-]+)[.\]]/.exec(line);
184
+ if (header) {
185
+ inOurServer = header[1] === APPROVAL_MCP_SERVER;
186
+ continue;
187
+ }
188
+ if (ANY_TABLE_RE.test(line)) {
189
+ inOurServer = false;
190
+ continue;
191
+ }
192
+ const dotted = /^\s*mcp_servers\.([A-Za-z0-9_-]+)\.tools\./.exec(line);
193
+ if (dotted && dotted[1] === APPROVAL_MCP_SERVER)
194
+ return true;
195
+ if (inOurServer && /^\s*tools\s*=\s*\{/.test(line))
196
+ return true;
197
+ }
198
+ return false;
199
+ }
200
+ // ── The payload candidates (what codex actually loads) ──
201
+ // The devkit plugin's cache dir under CODEX_HOME — the CLI marketplace
202
+ // flow's payload home. CODEX_HOME mirrors the resolution codex.ts's cache
203
+ // cleanup uses; codex resolves its whole home from this override.
204
+ export function codexPluginCacheDir() {
205
+ return join(process.env.CODEX_HOME || join(homedir(), '.codex'), 'plugins', 'cache', getMarketplaceName(), getCodexPluginName());
206
+ }
207
+ // Every .mcp.json under the cache dir (one per version dir). Every copy is
208
+ // verified, not just the newest: which version codex loads is codex's
209
+ // call, and a stale copy missing the yield changes that machine's posture.
210
+ // A missing cache dir reads as no candidates (not installed that way).
211
+ export function codexCacheMcpJsonPaths() {
212
+ const cacheDir = codexPluginCacheDir();
213
+ if (!existsSync(cacheDir))
214
+ return [];
215
+ let entries;
216
+ try {
217
+ entries = readdirSync(cacheDir);
218
+ }
219
+ catch {
220
+ return [];
221
+ }
222
+ const out = [];
223
+ for (const name of entries) {
224
+ const path = join(cacheDir, name, '.mcp.json');
225
+ try {
226
+ if (statSync(path).isFile())
227
+ out.push(path);
228
+ }
229
+ catch {
230
+ // unreadable or not a file — not a candidate
231
+ }
232
+ }
233
+ return out;
234
+ }
235
+ // The Codex Desktop payload's .mcp.json (the desktop deploy's plugin dir).
236
+ export function codexDesktopMcpJsonPath() {
237
+ return join(codexDesktopPluginsDir(), '.mcp.json');
238
+ }
239
+ // The doctor's shared reading: which roster tools sit in each non-approve
240
+ // state. Both channels (payload .mcp.json, config.toml overrides) ask the
241
+ // same question, so the derivation has one home.
242
+ function toolsInStates(states) {
243
+ const names = (wanted) => APPROVAL_TOOLS.filter((_tool, index) => states[index] === wanted).join(', ');
244
+ return {
245
+ prompt: names('prompt'),
246
+ auto: names('auto'),
247
+ foreign: names('foreign'),
248
+ absent: names('absent'),
249
+ };
250
+ }
251
+ // The payload half of the doctor row: every cached copy that carries our
252
+ // server must carry the full yield; a stale or hand-stripped copy is
253
+ // drift, healable by re-running the install (the payload is regenerated
254
+ // wholesale). A relevant machine with NO payload file at all gets a WARN,
255
+ // never a vacuous pass — the yield cannot be verified without the file
256
+ // codex reads it from.
257
+ function payloadDriftWarns(payloadPaths) {
258
+ const warns = [];
259
+ if (payloadPaths.length === 0) {
260
+ warns.push(`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})`);
261
+ return warns;
262
+ }
263
+ for (const path of payloadPaths) {
264
+ let config;
265
+ let states;
266
+ try {
267
+ config = asRecord(JSON.parse(readFileSync(path, 'utf8')));
268
+ states = readMcpJsonApprovalStates(config);
269
+ }
270
+ catch {
271
+ warns.push(`${path} is unparseable, so its yield cannot be verified (${RERUN})`);
272
+ continue;
273
+ }
274
+ // The env half: a copy that carries our server entry but not the ask env
275
+ // runs the devkit in token mode (a stale pre-ask payload) — drift,
276
+ // healable by the same re-run.
277
+ if (asRecord(asRecord(asRecord(config.mcpServers)[APPROVAL_MCP_SERVER]).env).DEVKIT_APPROVAL_MODE !==
278
+ CODEX_APPROVAL_MODE_ENV) {
279
+ warns.push(`${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})`);
280
+ }
281
+ if (states.every((state) => state === 'approve'))
282
+ continue;
283
+ const { prompt, auto, foreign, absent } = toolsInStates(states);
284
+ if (prompt)
285
+ warns.push(`approval_mode "prompt" on ${prompt} in ${path} (double interaction) (${RERUN})`);
286
+ if (auto)
287
+ warns.push(`approval_mode "auto" on ${auto} in ${path} keeps codex's own gate ahead of the devkit's confirmation (${RERUN})`);
288
+ if (foreign)
289
+ warns.push(`approval_mode on ${foreign} in ${path} is not "approve", so codex's own approval gate may fire ahead of the devkit's confirmation (${RERUN})`);
290
+ if (absent)
291
+ warns.push(`the approve block is missing for ${absent} in ${path} (${RERUN})`);
292
+ }
293
+ return warns;
294
+ }
295
+ // ── The doctor row ──
296
+ const CODEX_POSTURE_LABEL = 'Codex approval posture';
297
+ const RERUN = 're-run: npx huaweicloud-devkit install --target codex (or update)';
298
+ // Verifies the yield where codex reads it. Self-gating, silent on machines
299
+ // the devkit has no codex relationship with: no payload .mcp.json (cache or
300
+ // desktop), no codex plugin answering installed, no desktop marketplace
301
+ // registration. `desktopRegistered` is passed in by the doctor shell
302
+ // (codexDesktopDoctorMcpConfigured already runs there as an MCP-configured
303
+ // candidate).
304
+ export function codexDoctorApprovalRow(ctx, desktopRegistered) {
305
+ const payloadPaths = [...codexCacheMcpJsonPaths(), codexDesktopMcpJsonPath()].filter((p) => existsSync(p));
306
+ // The user override + policy channel. An absent config.toml is the
307
+ // healthy no-overrides default (nothing to warn about); a present but
308
+ // unreadable one cannot be verified, which earns a WARN below when the
309
+ // machine has a devkit payload.
310
+ const configPath = join(process.env.CODEX_HOME || join(homedir(), '.codex'), 'config.toml');
311
+ let configText = null;
312
+ let configUnreadable = false;
313
+ try {
314
+ configText = readFileSync(configPath, 'utf8');
315
+ }
316
+ catch (error) {
317
+ configUnreadable = error.code !== 'ENOENT';
318
+ }
319
+ let relevant = payloadPaths.length > 0 || desktopRegistered;
320
+ if (!relevant) {
321
+ const probe = codexPluginProbe();
322
+ relevant = probe.ok && probe.installed;
323
+ }
324
+ if (!relevant)
325
+ return;
326
+ // 1. The payload half — every copy that carries our server must carry
327
+ // the full yield; a stale or hand-stripped copy is drift, healable by
328
+ // re-running the install (the payload is regenerated wholesale).
329
+ const payloadWarns = payloadDriftWarns(payloadPaths);
330
+ // 2. The user override half (config.toml) — a value here WINS over the
331
+ // payload (the config layer resolves first, upstream source), so it
332
+ // decides the machine's actual posture; WARN, never FAIL: no client
333
+ // value fails anything open. Overrides in a dialect this doctor cannot
334
+ // parse (dotted keys, inline tables) get the dialect WARN, never
335
+ // silence — reading them as absence would hide the winning value.
336
+ const overrideWarns = [];
337
+ if (configText !== null) {
338
+ if (codexOverridesUnparseableDialect(configText)) {
339
+ overrideWarns.push(`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`);
340
+ }
341
+ const { prompt, auto, foreign } = toolsInStates(readCodexApprovalBlocks(configText));
342
+ if (prompt) {
343
+ overrideWarns.push(`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)`);
344
+ }
345
+ if (auto) {
346
+ overrideWarns.push(`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`);
347
+ }
348
+ if (foreign) {
349
+ overrideWarns.push(`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`);
350
+ }
351
+ }
352
+ // 3. The policy directions, both config.toml-borne.
353
+ const policyWarns = [];
354
+ if (configText !== null) {
355
+ if (codexElicitPolicyOff(configText)) {
356
+ policyWarns.push(`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}`);
357
+ }
358
+ if (codexApprovalPolicyNever(configText)) {
359
+ policyWarns.push(`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"`);
360
+ }
361
+ }
362
+ else if (configUnreadable) {
363
+ // A devkit payload exists but the override channel is unreadable, so
364
+ // user per-tool overrides and the approval policy cannot be verified.
365
+ overrideWarns.push(`the codex config at ${configPath} is unreadable, so user per-tool overrides and the approval policy cannot be verified`);
366
+ }
367
+ const warns = [...payloadWarns, ...overrideWarns, ...policyWarns];
368
+ if (warns.length === 0) {
369
+ ctx.check(`${CODEX_POSTURE_LABEL} (${APPROVAL_TOOLS.length} per-tool approve blocks + DEVKIT_APPROVAL_MODE="${CODEX_APPROVAL_MODE_ENV}" in the devkit payload)`, true, '');
370
+ }
371
+ else {
372
+ ctx.warn(`${CODEX_POSTURE_LABEL}: ${warns.join(' ')}`);
373
+ }
374
+ }
@@ -170,7 +170,14 @@ export function uninstallCodexDesktop() {
170
170
  if (removeIfExists(cacheDir)) {
171
171
  console.log(' Codex plugin cache cleaned');
172
172
  }
173
- // Clean up old config.toml section from pre-marketplace era
173
+ // Clean up old config.toml section from pre-marketplace era. The sweep is
174
+ // wholesale trunk behavior: EVERY [mcp_servers.huaweicloud-devkit…] table
175
+ // goes, including user-authored subtables under our server name (a user
176
+ // tools block vanishes with ours, and the doctor never sees it after the
177
+ // uninstall). The devkit's own yield never lands in config.toml at all
178
+ // (agents/codex-config.ts records why), so this pass removes legacy
179
+ // server sections plus whatever users placed under that name — nothing
180
+ // the current installer writes.
174
181
  const configPath = join(homedir(), '.codex', 'config.toml');
175
182
  if (existsSync(configPath)) {
176
183
  const lines = readFileSync(configPath, 'utf8').split(/\r?\n/);
@@ -125,6 +125,15 @@ export function installCodex() {
125
125
  console.log(` \x1b[31mCodex plugin installation failed.\x1b[0m`);
126
126
  return false;
127
127
  }
128
+ // The client yield (PR-5) needs no step here: the per-tool approve blocks
129
+ // ship inside the plugin payload's own .mcp.json (the repo's
130
+ // plugins/huaweicloud-core/.mcp.json, which `codex plugin add` copies
131
+ // into its cache) — the file codex reads per-tool approval_mode from for
132
+ // a plugin-provided server (agents/codex-config.ts). The user's
133
+ // config.toml is never touched: a tools table under
134
+ // [mcp_servers.huaweicloud-devkit] would imply a config-declared server,
135
+ // and without a transport codex refuses to load the whole config
136
+ // (live-measured "invalid transport" on codex-cli 0.167.2).
128
137
  return true;
129
138
  }
130
139
  export function uninstallCodex() {