huaweicloud-devkit 2.0.0-next.3 → 2.0.0-next.5

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 (173) hide show
  1. package/README.md +3 -1
  2. package/package.json +3 -1
  3. package/plugins/huaweicloud-core/.claude-plugin/plugin.json +1 -1
  4. package/plugins/huaweicloud-core/.codex-plugin/plugin.json +1 -1
  5. package/plugins/huaweicloud-core/.cursor-plugin/plugin.json +1 -1
  6. package/plugins/huaweicloud-core/.hermes-plugin/plugin.json +1 -1
  7. package/plugins/huaweicloud-core/.mcp.json +55 -1
  8. package/plugins/huaweicloud-core/.workbuddy-plugin/plugin.json +1 -1
  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/explain-error.js +9 -1
  22. package/plugins/huaweicloud-core/dist/hcloud-cli.js +1 -1
  23. package/plugins/huaweicloud-core/dist/hcloud-plan.js +68 -62
  24. package/plugins/huaweicloud-core/dist/lib/approval-mode.js +7 -5
  25. package/plugins/huaweicloud-core/dist/lib/approval-tokens.js +18 -4
  26. package/plugins/huaweicloud-core/dist/lib/call-trace.js +7 -14
  27. package/plugins/huaweicloud-core/dist/lib/confirm-language.js +50 -0
  28. package/plugins/huaweicloud-core/dist/lib/credentials.js +9 -9
  29. package/plugins/huaweicloud-core/dist/lib/hdkit/hdkit-gate.js +1398 -0
  30. package/plugins/huaweicloud-core/dist/lib/hdkit/hdkitservice-api.js +371 -32
  31. package/plugins/huaweicloud-core/dist/lib/hdkit/sandbox-pool.js +6 -16
  32. package/plugins/huaweicloud-core/dist/lib/opencode-ask-rule.js +224 -78
  33. package/plugins/huaweicloud-core/dist/lib/opencode-saved-perms.js +282 -0
  34. package/plugins/huaweicloud-core/dist/lib/supplemental-redaction.js +116 -0
  35. package/plugins/huaweicloud-core/dist/lib/tool-schema-parts.js +31 -0
  36. package/plugins/huaweicloud-core/dist/lib/workspace-glob.js +442 -0
  37. package/plugins/huaweicloud-core/dist/mcp-config-io.js +28 -1
  38. package/plugins/huaweicloud-core/dist/mcp-protocol.js +68 -16
  39. package/plugins/huaweicloud-core/dist/mcp-server.js +6933 -1186
  40. package/plugins/huaweicloud-core/dist/mcp-server.js.map +4 -4
  41. package/plugins/huaweicloud-core/dist/mcp-stdio-transport.js +14 -4
  42. package/plugins/huaweicloud-core/dist/packs/auth/approval.js +249 -0
  43. package/plugins/huaweicloud-core/dist/packs/auth/tools.js +44 -44
  44. package/plugins/huaweicloud-core/dist/packs/huaweicloud-cli/tools.js +85 -130
  45. package/plugins/huaweicloud-core/dist/packs/obs/approval.js +110 -0
  46. package/plugins/huaweicloud-core/dist/packs/obs/tools.js +7 -2
  47. package/plugins/huaweicloud-core/dist/packs/registry.js +20 -1
  48. package/plugins/huaweicloud-core/dist/packs/sandbox/approval.js +245 -0
  49. package/plugins/huaweicloud-core/dist/packs/sandbox/expose-task.js +71 -0
  50. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-account.js +180 -23
  51. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-credentials.js +13 -1
  52. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-hwexec.js +136 -0
  53. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-session.js +11 -1
  54. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-tunnel.js +29 -3
  55. package/plugins/huaweicloud-core/dist/packs/sandbox/schemas.js +93 -9
  56. package/plugins/huaweicloud-core/dist/packs/sandbox/tools.js +37 -10
  57. package/plugins/huaweicloud-core/dist/packs/secrets/approval.js +46 -0
  58. package/plugins/huaweicloud-core/dist/packs/secrets/tools.js +4 -1
  59. package/plugins/huaweicloud-core/dist/packs/update/approval.js +79 -0
  60. package/plugins/huaweicloud-core/dist/packs/update/tools.js +8 -1
  61. package/plugins/huaweicloud-core/dist/packs/voucher/tools.js +1 -1
  62. package/plugins/huaweicloud-core/dist/packs-configure.js +74 -0
  63. package/plugins/huaweicloud-core/dist/risk-rule-engine.js +20 -4
  64. package/plugins/huaweicloud-core/dist/safety/classify-hcloud.js +6 -6
  65. package/plugins/huaweicloud-core/dist/safety/classify-text.js +36 -0
  66. package/plugins/huaweicloud-core/dist/safety/policy.js +2 -0
  67. package/plugins/huaweicloud-core/dist/safety/redact.js +21 -3
  68. package/plugins/huaweicloud-core/dist/safety/risk-merge.js +24 -4
  69. package/plugins/huaweicloud-core/dist/sandbox/deploy-check.js +1 -1
  70. package/plugins/huaweicloud-core/dist/sandbox/devbridge-auth.js +65 -14
  71. package/plugins/huaweicloud-core/dist/sandbox/devbridge-cleanup.js +422 -0
  72. package/plugins/huaweicloud-core/dist/sandbox/devbridge-expose-flow.js +66 -16
  73. package/plugins/huaweicloud-core/dist/sandbox/devbridge-expose.js +13 -10
  74. package/plugins/huaweicloud-core/dist/sandbox/devbridge-keepalive.js +42 -2
  75. package/plugins/huaweicloud-core/dist/sandbox/devbridge-key.js +11 -3
  76. package/plugins/huaweicloud-core/dist/sandbox/devbridge-tunnel.js +6 -0
  77. package/plugins/huaweicloud-core/dist/sandbox/devbridge-validation.js +10 -1
  78. package/plugins/huaweicloud-core/dist/sandbox/hwexec-task.js +244 -0
  79. package/plugins/huaweicloud-core/dist/setup-cli.js +36 -1
  80. package/plugins/huaweicloud-core/dist/tool-definitions.js +13 -1
  81. package/plugins/huaweicloud-core/dist/tool-schemas.js +32 -6
  82. package/plugins/huaweicloud-core/dist/tools.js +76 -9
  83. package/plugins/huaweicloud-core/dist/update-registry.js +358 -5
  84. package/plugins/huaweicloud-core/dist/update-upgrade.js +1083 -14
  85. package/plugins/huaweicloud-core/dist/vendor-gate.js +2 -2
  86. package/plugins/huaweicloud-core/hooks/huaweicloud-safety.mjs +150 -1
  87. package/plugins/huaweicloud-core/hooks/huaweicloud-safety.py +67 -4
  88. package/plugins/huaweicloud-core/openclaw.plugin.json +2 -2
  89. package/plugins/huaweicloud-core/safety/policy.json +18 -11
  90. package/plugins/huaweicloud-core/safety/rules/cloud-risk-rules.json +115 -11
  91. package/plugins/huaweicloud-core/skills/huawei-sandbox/SKILL.md +198 -143
  92. package/plugins/huaweicloud-core/skills/huawei-sandbox/references/resource-pool.md +36 -12
  93. package/plugins/huaweicloud-core/skills/huawei-voucher/SKILL.md +2 -0
  94. package/plugins/huaweicloud-core/skills/huaweicloud-cli-and-auth/SKILL.md +2 -2
  95. package/plugins/huaweicloud-core/skills/huaweicloud-safety/SKILL.md +11 -7
  96. package/plugins/huaweicloud-core/skills/huaweicloud-safety/references/approvals.md +74 -29
  97. package/plugins/huaweicloud-core/skills/huaweicloud-troubleshooting/SKILL.md +21 -19
  98. package/plugins/huaweicloud-core/src/agents/codex-config.ts +432 -0
  99. package/plugins/huaweicloud-core/src/agents/codex-desktop.ts +8 -1
  100. package/plugins/huaweicloud-core/src/agents/codex.ts +10 -0
  101. package/plugins/huaweicloud-core/src/agents/opencode-config.ts +176 -88
  102. package/plugins/huaweicloud-core/src/agents/opencode.ts +125 -54
  103. package/plugins/huaweicloud-core/src/approval-gate.ts +1306 -0
  104. package/plugins/huaweicloud-core/src/approval-ledger.ts +2446 -0
  105. package/plugins/huaweicloud-core/src/approval-roster.ts +127 -0
  106. package/plugins/huaweicloud-core/src/approval-types.ts +237 -0
  107. package/plugins/huaweicloud-core/src/approvals-cli.ts +4 -4
  108. package/plugins/huaweicloud-core/src/data/icons-manifest.v1.json +23 -5
  109. package/plugins/huaweicloud-core/src/dep/receipts.ts +84 -5
  110. package/plugins/huaweicloud-core/src/explain-error.ts +9 -1
  111. package/plugins/huaweicloud-core/src/hcloud-cli.ts +3 -0
  112. package/plugins/huaweicloud-core/src/hcloud-plan.ts +74 -71
  113. package/plugins/huaweicloud-core/src/lib/approval-mode.ts +7 -5
  114. package/plugins/huaweicloud-core/src/lib/approval-tokens.ts +19 -4
  115. package/plugins/huaweicloud-core/src/lib/call-trace.ts +7 -17
  116. package/plugins/huaweicloud-core/src/lib/confirm-language.ts +51 -0
  117. package/plugins/huaweicloud-core/src/lib/credentials.ts +9 -8
  118. package/plugins/huaweicloud-core/src/lib/hdkit/hdkit-gate.ts +1428 -0
  119. package/plugins/huaweicloud-core/src/lib/hdkit/hdkitservice-api.ts +408 -30
  120. package/plugins/huaweicloud-core/src/lib/hdkit/sandbox-pool.ts +8 -15
  121. package/plugins/huaweicloud-core/src/lib/opencode-ask-rule.ts +302 -83
  122. package/plugins/huaweicloud-core/src/lib/opencode-saved-perms.ts +354 -0
  123. package/plugins/huaweicloud-core/src/lib/supplemental-redaction.ts +129 -0
  124. package/plugins/huaweicloud-core/src/lib/tool-schema-parts.ts +40 -0
  125. package/plugins/huaweicloud-core/src/lib/workspace-glob.ts +414 -0
  126. package/plugins/huaweicloud-core/src/mcp-config-io.ts +27 -1
  127. package/plugins/huaweicloud-core/src/mcp-protocol.ts +81 -17
  128. package/plugins/huaweicloud-core/src/mcp-server.ts +7 -7
  129. package/plugins/huaweicloud-core/src/mcp-stdio-transport.ts +14 -4
  130. package/plugins/huaweicloud-core/src/packs/auth/approval.ts +302 -0
  131. package/plugins/huaweicloud-core/src/packs/auth/tools.ts +54 -45
  132. package/plugins/huaweicloud-core/src/packs/huaweicloud-cli/tools.ts +99 -137
  133. package/plugins/huaweicloud-core/src/packs/obs/approval.ts +122 -0
  134. package/plugins/huaweicloud-core/src/packs/obs/tools.ts +7 -2
  135. package/plugins/huaweicloud-core/src/packs/registry.ts +43 -1
  136. package/plugins/huaweicloud-core/src/packs/sandbox/approval.ts +296 -0
  137. package/plugins/huaweicloud-core/src/packs/sandbox/expose-task.ts +105 -0
  138. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-account.ts +195 -23
  139. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-credentials.ts +11 -1
  140. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-hwexec.ts +228 -0
  141. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-session.ts +11 -1
  142. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-tunnel.ts +41 -14
  143. package/plugins/huaweicloud-core/src/packs/sandbox/schemas.ts +120 -8
  144. package/plugins/huaweicloud-core/src/packs/sandbox/tools.ts +42 -9
  145. package/plugins/huaweicloud-core/src/packs/secrets/approval.ts +50 -0
  146. package/plugins/huaweicloud-core/src/packs/secrets/tools.ts +4 -1
  147. package/plugins/huaweicloud-core/src/packs/update/approval.ts +87 -0
  148. package/plugins/huaweicloud-core/src/packs/update/tools.ts +8 -1
  149. package/plugins/huaweicloud-core/src/packs/voucher/tools.ts +1 -1
  150. package/plugins/huaweicloud-core/src/packs-configure.ts +75 -0
  151. package/plugins/huaweicloud-core/src/risk-rule-engine.ts +20 -7
  152. package/plugins/huaweicloud-core/src/safety/classify-hcloud.ts +28 -16
  153. package/plugins/huaweicloud-core/src/safety/classify-text.ts +40 -0
  154. package/plugins/huaweicloud-core/src/safety/policy.ts +7 -0
  155. package/plugins/huaweicloud-core/src/safety/redact.ts +24 -6
  156. package/plugins/huaweicloud-core/src/safety/risk-merge.ts +26 -5
  157. package/plugins/huaweicloud-core/src/sandbox/deploy-check.ts +1 -1
  158. package/plugins/huaweicloud-core/src/sandbox/devbridge-auth.ts +74 -14
  159. package/plugins/huaweicloud-core/src/sandbox/devbridge-cleanup.ts +511 -0
  160. package/plugins/huaweicloud-core/src/sandbox/devbridge-expose-flow.ts +70 -18
  161. package/plugins/huaweicloud-core/src/sandbox/devbridge-expose.ts +26 -10
  162. package/plugins/huaweicloud-core/src/sandbox/devbridge-keepalive.ts +41 -2
  163. package/plugins/huaweicloud-core/src/sandbox/devbridge-key.ts +9 -3
  164. package/plugins/huaweicloud-core/src/sandbox/devbridge-tunnel.ts +24 -0
  165. package/plugins/huaweicloud-core/src/sandbox/devbridge-validation.ts +11 -1
  166. package/plugins/huaweicloud-core/src/sandbox/hwexec-task.ts +304 -0
  167. package/plugins/huaweicloud-core/src/setup-cli.ts +39 -1
  168. package/plugins/huaweicloud-core/src/tool-definitions.ts +13 -1
  169. package/plugins/huaweicloud-core/src/tool-schemas.ts +39 -6
  170. package/plugins/huaweicloud-core/src/tools.ts +124 -10
  171. package/plugins/huaweicloud-core/src/update-registry.ts +340 -5
  172. package/plugins/huaweicloud-core/src/update-upgrade.ts +1130 -14
  173. package/plugins/huaweicloud-core/src/vendor-gate.ts +2 -2
@@ -180,11 +180,21 @@ export class DevkitStdioTransport {
180
180
  }
181
181
  return;
182
182
  }
183
- // An id-bearing message without a method is malformed: the SDK's
184
- // onmessage would silently drop it and the client would hang. Reply
185
- // -32601 echoing the request id so the failure is correlatable, matching
186
- // the legacy behavior.
183
+ // An id-bearing message without a method is either a RESPONSE to a
184
+ // server-initiated request (the elicitation form we raise during a tool
185
+ // call) or a malformed request. A response carries result or error and
186
+ // must reach the SDK, which correlates it by id; without this branch the
187
+ // form answer was answered with -32601 and the server waited out its
188
+ // request timeout (measured: every elicitation round-trip hung on the
189
+ // real stdio face while the in-memory transport worked). A message with
190
+ // neither result nor error is still the malformed shape: reply -32601
191
+ // echoing the id so the failure is correlatable, matching the legacy
192
+ // behavior.
187
193
  if (typeof message.method !== 'string' || !message.method) {
194
+ if (Object.hasOwn(message, 'result') || Object.hasOwn(message, 'error')) {
195
+ this.onmessage?.(raw);
196
+ return;
197
+ }
188
198
  this.writeJsonRpcError(-32601, 'Method not found: undefined', message.id);
189
199
  return;
190
200
  }
@@ -0,0 +1,249 @@
1
+ import { fingerprint } from "../../auth/reconcile.js";
2
+ import { readCodeArtsCredentials } from "../../auth/credentials.js";
3
+ import { peekImportFile } from "../../lib/credentials.js";
4
+ import { resolveSecretValue } from "../../lib/secret-vault/store.js";
5
+ import { hashArgs } from "../../lib/approval-tokens.js";
6
+ // The auth pack's approval registration (PR-3b): the account-switch pair.
7
+ // The account switch is the one step that can redirect every downstream
8
+ // approved write to a different account, so both faces join the roster as
9
+ // always-gated — the vendor gate in front asserts the vendor (the quote),
10
+ // this gate asserts the human (the confirmation). auth_confirm stays OFF
11
+ // this table per the parked decision: its pendingConfirms flow is the
12
+ // persist-conflict reconciliation's own confirmation, and gating it would
13
+ // ask the same human twice about the same switch (the double-ask on a
14
+ // persist conflict is the recorded known transient, not a defect to
15
+ // collapse here). auth_sync, auth_status, and check_account_type stay
16
+ // unregistered — read or operator-configured faces.
17
+ //
18
+ // The binding value is the call's RESOLVED CREDENTIAL SNAPSHOT with the
19
+ // consent plumbing (vendorConfirmed, approvalToken, approvedByUser,
20
+ // command) normalized away. Resolution mirrors the handler's channel
21
+ // precedence exactly — the args pair wins when present, else the mode's
22
+ // backing store (the import file for mode=import, the CodeArts injection
23
+ // for mode=mcp-config), with secret:// refs resolved — and the backing
24
+ // store is PEEKED, never read-for-wipe: the gate runs before any consent,
25
+ // so a read with a side effect (the malformed-file wipe) would mutate the
26
+ // user's state on the refusal path. The handler EXECUTES this snapshot
27
+ // (threaded through dispatch as opts.approvedBinding), never a
28
+ // post-consent re-read — interrogate round 1, finding 2: the account the
29
+ // confirmation view named is the account that switches, and a rewrite of
30
+ // the import file between the shown form and the accept lands nowhere.
31
+ // Folding the resolved region and security token into the BINDING (not
32
+ // just the display) extends the same doctrine to the full effect: a
33
+ // same-fingerprint call with a different region or STS re-asks instead
34
+ // of executing under the old approval (round 1, finding 3).
35
+ //
36
+ // The cost of a resolved binding: the post-dispatch outcome recorder
37
+ // re-derives it AFTER the handler wiped a consumed import file, so its
38
+ // argsHash can miss the arrival row — the annotate is best-effort by
39
+ // contract and a missing outcome line is the documented consequence.
40
+ function asStr(value) {
41
+ return typeof value === 'string' ? value : '';
42
+ }
43
+ function resolved(value) {
44
+ return resolveSecretValue(value) ?? value;
45
+ }
46
+ // Shape guard for the dispatch-threaded snapshot: it comes from our own
47
+ // gate, but the narrow keeps the handler's fallback honest against any
48
+ // future binding-shape change.
49
+ export function isApprovedSwitchSnapshot(value) {
50
+ if (!value || typeof value !== 'object' || Array.isArray(value))
51
+ return false;
52
+ const v = value;
53
+ return (typeof v.mode === 'string' &&
54
+ typeof v.action === 'string' &&
55
+ typeof v.ak === 'string' &&
56
+ typeof v.sk === 'string' &&
57
+ typeof v.securityToken === 'string' &&
58
+ typeof v.region === 'string' &&
59
+ typeof v.account === 'string' &&
60
+ typeof v.fromImport === 'boolean');
61
+ }
62
+ // Resolve the switch substance with the handler's own precedence: the args
63
+ // pair wins when present, else the mode's backing store (full replace for
64
+ // the import channel; the CodeArts channel defaults region from the
65
+ // injection and falls back to the args). Refs resolve to plaintext, the
66
+ // same seam the handler resolved through.
67
+ export function switchSnapshot(args) {
68
+ const mode = args.mode || 'memory';
69
+ const action = args.action || 'temporary';
70
+ if (action === 'clear') {
71
+ return { mode, action: 'clear', ak: '', sk: '', securityToken: '', region: '', account: '', fromImport: false };
72
+ }
73
+ let ak = asStr(args.ak);
74
+ let sk = asStr(args.sk);
75
+ let securityToken = asStr(args.securityToken);
76
+ let region = asStr(args.region);
77
+ let fromImport = false;
78
+ if (mode === 'import' && (!ak || !sk)) {
79
+ const imported = peekImportFile();
80
+ if (imported) {
81
+ ({ ak, sk, securityToken, region } = imported);
82
+ fromImport = true;
83
+ }
84
+ }
85
+ if (mode === 'mcp-config' && (!ak || !sk)) {
86
+ const cc = readCodeArtsCredentials();
87
+ if (cc) {
88
+ ak = cc.ak;
89
+ sk = cc.sk;
90
+ securityToken = cc.securityToken || '';
91
+ region = cc.region || region;
92
+ }
93
+ }
94
+ const akValue = resolved(ak);
95
+ const skValue = resolved(sk);
96
+ const securityTokenValue = resolved(securityToken);
97
+ return {
98
+ mode,
99
+ action,
100
+ ak: akValue,
101
+ sk: skValue,
102
+ securityToken: securityTokenValue,
103
+ region,
104
+ account: fingerprint(akValue, skValue),
105
+ fromImport,
106
+ };
107
+ }
108
+ // Resolve the init substance with the SAME seam the switch snapshot uses
109
+ // (round 2, the twin path): the args may carry secret:// refs, and the
110
+ // binding must hash and name the RESOLVED account — the fingerprint of the
111
+ // ref strings is the fingerprint of nothing. Resolution at snapshot time
112
+ // also refuses the doomed shape pre-mint (an unresolvable ref throws
113
+ // VaultError at the gate, before any consent surface or token store
114
+ // write), and makes a vault swap behind the same refs change the binding
115
+ // hash, so a confirmed retry re-asks instead of executing an unseen
116
+ // account.
117
+ function initSnapshot(args) {
118
+ if (args.clear)
119
+ return { clear: true };
120
+ const ak = resolved(asStr(args.ak));
121
+ const sk = resolved(asStr(args.sk));
122
+ const region = resolved(asStr(args.region));
123
+ return {
124
+ ak,
125
+ sk,
126
+ ...(region ? { region } : {}),
127
+ account: fingerprint(ak, sk),
128
+ };
129
+ }
130
+ export function isApprovedInitSnapshot(value) {
131
+ if (!value || typeof value !== 'object' || Array.isArray(value))
132
+ return false;
133
+ const v = value;
134
+ return (typeof v.ak === 'string' &&
135
+ typeof v.sk === 'string' &&
136
+ typeof v.account === 'string' &&
137
+ !('mode' in v) &&
138
+ !('fromImport' in v));
139
+ }
140
+ const SOURCE_LABELS = {
141
+ memory: 'session arguments',
142
+ import: 'the devkit credentials import file (creds-import.json)',
143
+ 'mcp-config': 'the CodeArts mcp_settings.json injection',
144
+ };
145
+ const SCOPE_LABELS = {
146
+ persist: 'persist: S1 + KooCLI profile + OBS mirrors',
147
+ temporary: 'temporary: this MCP process only',
148
+ };
149
+ function switchView(binding) {
150
+ if (binding.action === 'clear') {
151
+ return {
152
+ summary: "huaweicloud_auth_switch: clear this session's runtime credentials (fall back to env/file/S1)",
153
+ argsHash: hashArgs(binding),
154
+ pinView: { action: 'clear' },
155
+ command: "huaweicloud_auth_switch (clear the session's runtime credentials)",
156
+ };
157
+ }
158
+ const account = binding.account || '(unknown)';
159
+ const source = SOURCE_LABELS[binding.mode] ?? binding.mode;
160
+ const scope = SCOPE_LABELS[binding.action] ?? binding.action;
161
+ const regionNote = binding.region ? `, region ${binding.region}` : '';
162
+ const stsNote = binding.securityToken ? ', session-scoped STS' : '';
163
+ return {
164
+ summary: `huaweicloud_auth_switch: switch to account ${account} from ${source}${regionNote}${stsNote} (${scope})`,
165
+ argsHash: hashArgs(binding),
166
+ pinView: {
167
+ account,
168
+ source: binding.mode,
169
+ scope: binding.action,
170
+ ...(binding.region ? { region: binding.region } : {}),
171
+ ...(binding.securityToken ? { sts: true } : {}),
172
+ },
173
+ command: `huaweicloud_auth_switch (switch the account to ${account}, source ${binding.mode}${regionNote}${stsNote}, ${binding.action})`,
174
+ };
175
+ }
176
+ export const AUTH_APPROVAL_SPECS = {
177
+ huaweicloud_auth_init: {
178
+ domain: 'auth',
179
+ flavor: 'always',
180
+ tokenSource: 'gate',
181
+ normalizeArgs: initSnapshot,
182
+ // NoDoomedToken (round 1, finding 4): the handler refuses a set call
183
+ // with no credential pair, so the gate refuses it before any mint —
184
+ // the user is never asked to approve a call the execution would
185
+ // reject.
186
+ assertArgsExecutable: (bindingValue) => {
187
+ const binding = (bindingValue ?? {});
188
+ if (binding.clear)
189
+ return;
190
+ if (typeof binding.ak !== 'string' ||
191
+ binding.ak === '' ||
192
+ typeof binding.sk !== 'string' ||
193
+ binding.sk === '') {
194
+ throw new Error('ak and sk are required to set runtime credentials (secret:// vault refs are accepted). ' +
195
+ 'Pass clear=true to clear the session\u2019s runtime credentials instead.');
196
+ }
197
+ },
198
+ summarize: (bindingValue) => {
199
+ const binding = (bindingValue ?? {});
200
+ if (binding.clear) {
201
+ return {
202
+ summary: "huaweicloud_auth_init: clear this session's runtime credentials (fall back to env/file)",
203
+ argsHash: hashArgs(binding),
204
+ pinView: { clear: true },
205
+ command: "huaweicloud_auth_init (clear the session's runtime credentials)",
206
+ };
207
+ }
208
+ const account = asStr(binding.account) || '(unknown)';
209
+ const region = asStr(binding.region);
210
+ return {
211
+ summary: `huaweicloud_auth_init: switch this session's runtime credentials to account ${account}` +
212
+ `${region ? ` (region ${region})` : ''} (source: session arguments)`,
213
+ argsHash: hashArgs(binding),
214
+ pinView: { account, ...(region ? { region } : {}) },
215
+ command: `huaweicloud_auth_init (switch the session's runtime credentials to account ${account})`,
216
+ };
217
+ },
218
+ },
219
+ huaweicloud_auth_switch: {
220
+ domain: 'auth',
221
+ flavor: 'always',
222
+ tokenSource: 'gate',
223
+ normalizeArgs: switchSnapshot,
224
+ // NoDoomedToken (round 1, finding 4): no mint for a switch with no
225
+ // resolvable target account (an absent or malformed import file, an
226
+ // empty injection), and no mint for a persist shape the handler
227
+ // rejects anyway — a regionless persist (invalid_region, round 2's
228
+ // close of the recorded residual) or an STS persist (R3).
229
+ assertArgsExecutable: (bindingValue) => {
230
+ const binding = bindingValue;
231
+ if (binding.action === 'clear')
232
+ return;
233
+ if (typeof binding.ak !== 'string' ||
234
+ binding.ak === '' ||
235
+ typeof binding.sk !== 'string' ||
236
+ binding.sk === '') {
237
+ throw new Error('the account switch has no resolvable target account: pass ak/sk (mode=memory), ' +
238
+ 'a valid creds-import.json (mode=import), or an injected mcp_settings.json (mode=mcp-config).');
239
+ }
240
+ if (binding.action === 'persist' && !binding.region.trim()) {
241
+ throw new Error('region is required to persist credentials. Pass --region, or include "region" in creds-import.json (mode=import).');
242
+ }
243
+ if (binding.action === 'persist' && binding.securityToken !== '') {
244
+ throw new Error('Temporary STS credentials cannot be persisted (R3). Use action=temporary.');
245
+ }
246
+ },
247
+ summarize: (bindingValue) => switchView(bindingValue),
248
+ },
249
+ };
@@ -1,11 +1,12 @@
1
1
  import { z } from 'zod';
2
- import { vendorConfirmed } from "../../lib/tool-schema-parts.js";
2
+ import { approvalTokenArg, approvedByUserArg, vendorConfirmed } from "../../lib/tool-schema-parts.js";
3
3
  import { getAuthStatus, syncAuth } from "../../auth/service.js";
4
4
  import { checkAccountType } from "../../auth/account-type.js";
5
5
  import { setRuntimeCredentials, clearRuntimeCredentials, readGlobalCredentialsStrict, backupGlobalCredentials, readCodeArtsCredentials, } from "../../auth/credentials.js";
6
6
  import { fingerprint } from "../../auth/reconcile.js";
7
- import { persistCredentials, readImportFile, clearImportFile, pendingConfirms, consumedConfirms, refreshUserHashAfterAuthChange, } from "../../lib/credentials.js";
7
+ import { persistCredentials, clearImportFile, pendingConfirms, consumedConfirms, refreshUserHashAfterAuthChange, } from "../../lib/credentials.js";
8
8
  import { resolveSecretValue } from "../../lib/secret-vault/store.js";
9
+ import { isApprovedInitSnapshot, isApprovedSwitchSnapshot, switchSnapshot, } from "./approval.js";
9
10
  // Handlers migrated verbatim from src/tools.ts; CallToolOptions
10
11
  // (sessionId/doQuery) is passed through unchanged by the delegating case.
11
12
  async function handleAuthStatus(args = {}, _opts = {}) {
@@ -16,7 +17,7 @@ async function handleAuthSync(args = {}, _opts = {}) {
16
17
  refreshUserHashAfterAuthChange();
17
18
  return result;
18
19
  }
19
- async function handleAuthInit(args = {}, _opts = {}) {
20
+ async function handleAuthInit(args = {}, opts = {}) {
20
21
  if (args.clear) {
21
22
  clearRuntimeCredentials();
22
23
  refreshUserHashAfterAuthChange({ regenerate: false });
@@ -28,51 +29,43 @@ async function handleAuthInit(args = {}, _opts = {}) {
28
29
  // Incoming values may be secret:// refs. Resolve them so the runtime store
29
30
  // carries usable credentials (same contract as auth_switch and the CLI's
30
31
  // auth init); an unresolvable ref throws VaultError with its own
31
- // remediation.
32
- const ak = resolveSecretValue(args.ak) ?? args.ak;
33
- const sk = resolveSecretValue(args.sk) ?? args.sk;
34
- const region = resolveSecretValue(args.region) ?? args.region;
32
+ // remediation. When the gate threaded its approved snapshot (round 2,
33
+ // the twin path), the handler executes the RESOLVED VALUES the
34
+ // confirmation surface was shown against — a vault swap behind the same
35
+ // refs cannot execute an account that appeared on no surface. Without a
36
+ // matching snapshot (a direct handler call), the trunk resolution runs.
37
+ const approved = isApprovedInitSnapshot(opts.approvedBinding) ? opts.approvedBinding : null;
38
+ const ak = approved ? approved.ak : (resolveSecretValue(args.ak) ?? args.ak);
39
+ const sk = approved ? approved.sk : (resolveSecretValue(args.sk) ?? args.sk);
40
+ const region = approved ? approved.region : (resolveSecretValue(args.region) ?? args.region);
35
41
  setRuntimeCredentials(ak, sk, undefined, region);
36
42
  refreshUserHashAfterAuthChange();
37
43
  return { status: 'ok', message: 'Runtime credentials set for this MCP session.' };
38
44
  }
39
- async function handleAuthSwitch(args = {}, _opts = {}) {
45
+ async function handleAuthSwitch(args = {}, opts = {}) {
40
46
  const action = args.action || 'temporary';
41
47
  if (action === 'clear') {
42
48
  clearRuntimeCredentials();
43
49
  refreshUserHashAfterAuthChange({ regenerate: false });
44
50
  return { status: 'cleared', message: 'Runtime credentials cleared. Fallback to env/file/S1.' };
45
51
  }
46
- let ak = args.ak || '';
47
- let sk = args.sk || '';
48
- let securityToken = args.securityToken || '';
49
- let region = args.region || '';
50
52
  const sourceChannel = args.mode || 'memory';
51
- let importedFromFile = false;
52
- if (sourceChannel === 'import' && (!ak || !sk)) {
53
- const imported = readImportFile();
54
- if (imported) {
55
- ({ ak, sk, securityToken, region } = imported);
56
- importedFromFile = true;
57
- }
58
- }
59
- if (sourceChannel === 'mcp-config' && (!ak || !sk)) {
60
- const cc = readCodeArtsCredentials();
61
- if (cc) {
62
- ak = cc.ak;
63
- sk = cc.sk;
64
- securityToken = cc.securityToken || '';
65
- region = cc.region || region;
66
- }
67
- }
68
- // Incoming values may be secret:// refs (mode=memory with refs, or an
69
- // import file carrying them). Resolve before any comparison or fan-out:
70
- // the conflict check and displayed fingerprints must compare plaintext,
71
- // and persistCredentials' mirrors must never see a literal ref string.
72
- // An unresolvable ref throws VaultError with its own remediation.
73
- ak = resolveSecretValue(ak) ?? ak;
74
- sk = resolveSecretValue(sk) ?? sk;
75
- securityToken = resolveSecretValue(securityToken) ?? securityToken;
53
+ // The gate's approved snapshot (interrogate round 1, finding 2): the
54
+ // resolved credential substance from BEFORE the confirmation surface was
55
+ // shown, threaded through dispatch when the call passed the gate.
56
+ // Executing THIS record — never a post-consent re-read of the import
57
+ // file or the CodeArts injection — is what makes the shown account the
58
+ // executed account. Without a matching snapshot (a direct handler call,
59
+ // no dispatch gate in front), the same resolution runs inline through
60
+ // the spec's own snapshot function — one precedence, one seam.
61
+ const approved = isApprovedSwitchSnapshot(opts.approvedBinding)
62
+ ? opts.approvedBinding.mode === sourceChannel && opts.approvedBinding.action === action
63
+ ? opts.approvedBinding
64
+ : null
65
+ : null;
66
+ const resolved = approved ?? switchSnapshot(args);
67
+ const { ak, sk, securityToken, region } = resolved;
68
+ const importedFromFile = resolved.fromImport;
76
69
  if (!ak || !sk) {
77
70
  throw new Error('ak and sk are required (or provide creds-import.json for mode=import).');
78
71
  }
@@ -191,19 +184,26 @@ const authInitSchema = z.looseObject({
191
184
  sk: z.string().optional().describe('Huawei Cloud Secret Key (required unless clear=true)'),
192
185
  region: z.string().optional().describe('Default region (optional)'),
193
186
  clear: z.boolean().optional().describe('Set to true to clear runtime credentials and revert to env/file'),
187
+ approvalToken: approvalTokenArg.describe('Required in token-approval mode (from the USER_CONFIRMATION_REQUIRED envelope this gate returns); omit in ask mode, where the server-run gate confirms at run time.'),
188
+ approvedByUser: approvedByUserArg,
194
189
  });
195
190
  const authSwitchSchema = z.looseObject({
196
191
  vendorConfirmed,
197
192
  mode: z.enum(['import', 'memory', 'mcp-config']).optional().describe('Credential source channel'),
198
193
  action: z.enum(['persist', 'temporary', 'clear']).optional().describe('Apply scope'),
199
- ak: z.string().optional(),
200
- sk: z.string().optional(),
201
- securityToken: z.string().optional(),
202
- region: z.string().optional(),
194
+ ak: z.string().optional().describe('Huawei Cloud Access Key to switch this session to.'),
195
+ sk: z.string().optional().describe('Huawei Cloud Secret Key to switch this session to.'),
196
+ securityToken: z.string().optional().describe('Security (STS) token when the new credentials are session-scoped.'),
197
+ region: z.string().optional().describe('Optional default region applied with the switched credentials.'),
198
+ approvalToken: approvalTokenArg.describe('Required in token-approval mode (from the USER_CONFIRMATION_REQUIRED envelope this gate returns); omit in ask mode, where the server-run gate confirms at run time.'),
199
+ approvedByUser: approvedByUserArg,
203
200
  });
204
201
  const authConfirmSchema = z.looseObject({
205
202
  token: z.string().optional().describe('confirmation token from the needs_confirmation response'),
206
- decision: z.enum(['s1', 'newImported']).optional(),
203
+ decision: z
204
+ .enum(['s1', 'newImported'])
205
+ .optional()
206
+ .describe('Reconciliation choice: s1 keeps the existing S1 vault credentials; newImported adopts the newly imported ones.'),
207
207
  });
208
208
  async function handleCheckAccountType(args = {}, _opts = {}) {
209
209
  return checkAccountType({ region: typeof args.region === 'string' ? args.region : undefined });
@@ -229,13 +229,13 @@ export const AUTH_TOOLS = [
229
229
  },
230
230
  {
231
231
  name: 'huaweicloud_auth_init',
232
- description: 'Set or clear runtime Huawei Cloud credentials (AK/SK) for this MCP session. Runtime credentials take highest priority over environment variables and config files for all subsequent API calls. Use when switching accounts within the same Agent session — call with AK/SK to switch, or with clear=true to fall back to env/file credentials. Vendor-gated (vendorConfirmed): call only when the vendor is established — see the vendorConfirmed parameter description; when unknown, ask which cloud the user means first.',
232
+ description: 'Set or clear runtime Huawei Cloud credentials (AK/SK) for this MCP session. Runtime credentials take highest priority over environment variables and config files for all subsequent API calls. Use when switching accounts within the same Agent session — call with AK/SK to switch, or with clear=true to fall back to env/file credentials. Approval-gated: the account switch requires the user\u2019s explicit confirmation, and the confirmation view names the target account fingerprint — in the default token mode the first call returns USER_CONFIRMATION_REQUIRED with an approvalToken the user confirms in their own terminal (npx huaweicloud-devkit approve <token>); under DEVKIT_APPROVAL_MODE=ask the server raises the confirmation itself (an elicitation form, or the ASK_CONFIRMATION_REQUIRED chat round-trip). Vendor-gated (vendorConfirmed): call only when the vendor is established — see the vendorConfirmed parameter description; when unknown, ask which cloud the user means first.',
233
233
  schema: authInitSchema,
234
234
  handler: handleAuthInit,
235
235
  },
236
236
  {
237
237
  name: 'huaweicloud_auth_switch',
238
- description: 'Switch Huawei Cloud credentials within the current session. action=temporary keeps AK/SK in memory only (restart loses them; hcloud commands are unaffected and an A+C warning will be raised). action=persist writes S1 (single source of truth) with configuredBySession flag and propagates to S2 (KooCLI current profile) and S3 (OBS). action=clear resets runtime credentials. mode=import reads AK/SK from ~/.config/huaweicloud/creds-import.json then wipes it (SK never enters conversation). mode=memory passes AK/SK as tool arguments (SK is visible to model - prefer import). mode=mcp-config reads the injected mcp_settings environment. Vendor-gated (vendorConfirmed): call only when the vendor is established — see the vendorConfirmed parameter description; when unknown, ask which cloud the user means first.',
238
+ description: 'Switch Huawei Cloud credentials within the current session. action=temporary keeps AK/SK in memory only (restart loses them; hcloud commands are unaffected and an A+C warning will be raised). action=persist writes S1 (single source of truth) with configuredBySession flag and propagates to S2 (KooCLI current profile) and S3 (OBS). action=clear resets runtime credentials. mode=import reads AK/SK from ~/.config/huaweicloud/creds-import.json then wipes it (SK never enters conversation). mode=memory passes AK/SK as tool arguments (SK is visible to model - prefer import). mode=mcp-config reads the injected mcp_settings environment. Approval-gated: the switch requires the user\u2019s explicit confirmation, and the confirmation view names the target account fingerprint and the credential source — in the default token mode the first call returns USER_CONFIRMATION_REQUIRED with an approvalToken the user confirms in their own terminal (npx huaweicloud-devkit approve <token>); under DEVKIT_APPROVAL_MODE=ask the server raises the confirmation itself (an elicitation form, or the ASK_CONFIRMATION_REQUIRED chat round-trip). Vendor-gated (vendorConfirmed): call only when the vendor is established — see the vendorConfirmed parameter description; when unknown, ask which cloud the user means first.',
239
239
  schema: authSwitchSchema,
240
240
  handler: handleAuthSwitch,
241
241
  },