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
@@ -1,87 +1,140 @@
1
- // The OpenCode ask-mode permission rule — pure merge/cleanup logic.
1
+ // The OpenCode client permission rules for the approval roster — pure
2
+ // merge/cleanup logic.
2
3
  //
3
4
  // Purpose: in the ASK approval posture (DEVKIT_APPROVAL_MODE=ask), the
4
- // user's confirmation for an hcloud write is the harness client's own
5
- // permission prompt on the huaweicloud_run_approved_command call — which
6
- // only happens when the client is made to ASK for that tool. Measured on
7
- // opencode 2.2.7: the V2 config key is a top-level `permissions` ARRAY of
8
- // { action, resource, effect } objects; the MCP tool action is
9
- // `<server>_<tool>`, and our server key is `huaweicloud-devkit`, so the
10
- // action is `huaweicloud-devkit_huaweicloud_run_approved_command`; the
11
- // config hot-reloads without restarting opencode; and a `deny` on that
12
- // exact action prunes the tool from the live catalog (restored when the
13
- // deny is removed) — which is also why an allow on it silently removes
14
- // the gate. 2.2.7 still parses the V1 `permission` object the skills-tree
15
- // seam writes (the bundle migrates it), but the array is the documented
16
- // V2 shape — this rule rides ONLY the array form and is gated on opencode
17
- // major >= 2; the V1 seam is untouched.
5
+ // confirmation runs on the SERVER inside the tool call — an elicitation form
6
+ // on opencode 2.x, the T3 chat round-trip on 1.19–1.x (where forms do not
7
+ // exist, so the yield hands the only mechanical client prompt to the
8
+ // round-trip, recorded as posture trust). The client rules are yield-only:
9
+ // one exact `allow` rule per roster tool suppresses the redundant client
10
+ // popup so the server's confirmation is the only prompt. A client rule can
11
+ // therefore no longer fail anything open — the gate confirms every roster
12
+ // call regardless — but a `deny` rule still bricks the call in the client,
13
+ // which is why the doctor keeps deny a FAIL direction.
18
14
  //
19
- // Ownership rules: an entry on the ask action is user-owned by
20
- // construction — add-only-if-absent (a pre-existing entry with ANY effect
21
- // is the user's and survives the write; `skipped` reports it) and
22
- // value-exact deletion (only the exact action + resource '*' + effect
23
- // 'ask' triple is removed at uninstall) are the whole contract. The
24
- // accepted edge is the same one the seam records: a user-authored
25
- // same-shape rule that predates the write is removed together with it.
15
+ // Shape, measured on opencode 2.2.7: the V2 config key is a top-level
16
+ // `permissions` ARRAY of { action, resource, effect } objects; the MCP tool
17
+ // action is `<server>_<tool>` (approval-roster.ts owns the derivation), and
18
+ // the config hot-reloads without restarting opencode. Exact per-tool rules
19
+ // stay the choice over a server-wide wildcard: value-exact uninstall and
20
+ // per-rule doctor verification both need per-tool rows, and a wildcard row is
21
+ // indistinguishable from a user's own. 2.2.7 still parses the V1 `permission`
22
+ // object the skills-tree seam writes (the bundle migrates it), but the V2
23
+ // rule rides ONLY the array form.
26
24
  //
27
- // This module is pure: no fs, no spawn, no console — opencode-config.ts
28
- // owns I/O, the version probe, and logging; tests import these functions
29
- // directly.
25
+ // opencode 1.19.0–1.x alignment (the V1 `permission` object form):
26
+ // source-verified on the v1.19.0 tag — the same version the skills-tree seam
27
+ // measures. mcp/catalog.ts toolName() builds the permission key as
28
+ // sanitize(server) + "_" + sanitize(tool), byte-identical across shapes, so
29
+ // the roster action is shared; permission/index.ts fromConfig() turns a bare
30
+ // string value under a key into { permission, pattern: "*", action: value },
31
+ // and evaluate() is findLast — appending our key last beats any earlier user
32
+ // wildcard, mirroring V2's last-matching-rule-wins. A V1 "Always allow" is
33
+ // session memory, not a persisted grant, so it dies with the process.
34
+ //
35
+ // Ownership rules: an entry on a roster action is user-owned by construction —
36
+ // add-only-if-absent per rule (a pre-existing entry that is anything but our
37
+ // exact allow survives the write; `skipped` reports it by action, so partial
38
+ // coverage is always surfaced) and value-exact deletion (only the
39
+ // devkit-written `allow`/legacy `ask` shapes are removed) are the whole
40
+ // contract. The accepted edge is the same one the seam records: a
41
+ // user-authored same-shape rule that predates the write is removed together
42
+ // with it.
43
+ //
44
+ // Legacy migration: the pre-yield posture wrote effect `ask` on the same
45
+ // action. Every install/update removes those value-exact ask rules in both
46
+ // generations before applying the allow set, so the abandoned posture never
47
+ // survives a path that touches the config.
48
+ //
49
+ // This module is pure: no fs, no spawn, no console — opencode-config.ts owns
50
+ // I/O, the version probe, and logging; tests import these functions directly.
30
51
 
52
+ import { APPROVAL_TOOLS, approvalToolAction } from '../approval-roster.ts';
31
53
  import { asRecord } from './as-record.ts';
32
54
 
33
- export const ASK_RULE_ACTION = 'huaweicloud-devkit_huaweicloud_run_approved_command';
34
- export const ASK_RULE_RESOURCE = '*';
55
+ export const PERMISSION_RULE_RESOURCE = '*';
35
56
 
36
- export interface AskRuleApplyResult {
57
+ // Every roster tool's client permission action, derived from the roster leaf
58
+ // so the writer, the uninstaller, and the doctor can never drift.
59
+ export function allowRuleActions(): string[] {
60
+ return APPROVAL_TOOLS.map((tool) => approvalToolAction(tool));
61
+ }
62
+
63
+ export interface PermissionRuleApplyResult {
37
64
  changed: boolean;
38
- // True when nothing was written because the user owns the territory: an
39
- // entry on the ask action already exists (any effect — including one
40
- // identical to ours, indistinguishable from a pre-existing user rule),
41
- // or `permissions` is present but not an array.
42
- skipped: boolean;
65
+ // Roster actions whose territory the user owns: a non-devkit entry is
66
+ // already present, so no allow rule is written for it. The doctor reports
67
+ // each one by name; partial coverage is never silently dropped.
68
+ skipped: string[];
69
+ // True when the section itself is present but has the wrong shape
70
+ // (`permissions` not an array, `permission` not an object). Nothing is
71
+ // written and the user's shape is never converted.
72
+ shapeUnreadable: boolean;
43
73
  }
44
74
 
45
75
  function isRuleRecord(value: unknown): value is Record<string, unknown> {
46
76
  return value !== null && typeof value === 'object' && !Array.isArray(value);
47
77
  }
48
78
 
49
- // Mutates config in place. Fresh configs get the single-entry array;
50
- // arrays without an entry on the ask action get ours APPENDED — OpenCode
51
- // evaluates last-matching-rule-wins, so the appended entry is the
52
- // effective one. Arrays that already carry an entry on the action, and
53
- // non-array `permissions` shapes, are the user's: nothing is written.
54
- export function applyAskPermissionRule(config: Record<string, unknown>): AskRuleApplyResult {
79
+ function allowRule(action: string): Record<string, unknown> {
80
+ return { action, resource: PERMISSION_RULE_RESOURCE, effect: 'allow' };
81
+ }
82
+
83
+ // ── V2: the `permissions` array form (opencode 2.0+) ──
84
+
85
+ // Mutates config in place. Fresh configs get the N-entry allow set; arrays
86
+ // without an entry on a roster action get that entry APPENDED — OpenCode
87
+ // evaluates last-matching-rule-wins, so the appended entry is the effective
88
+ // one. An action already carrying exactly our rule is the target state (a
89
+ // reinstall is a no-op, never a false "user-owned" warning); an action
90
+ // carrying anything else is the user's and is skipped by name, so the writer
91
+ // never overrides a user's deny/ask or different resource.
92
+ export function applyAllowPermissionRules(
93
+ config: Record<string, unknown>,
94
+ actions: readonly string[] = allowRuleActions(),
95
+ ): PermissionRuleApplyResult {
55
96
  if (config.permissions === undefined) {
56
- config.permissions = [{ action: ASK_RULE_ACTION, resource: ASK_RULE_RESOURCE, effect: 'ask' }];
57
- return { changed: true, skipped: false };
97
+ config.permissions = actions.map((action) => allowRule(action));
98
+ return { changed: actions.length > 0, skipped: [], shapeUnreadable: false };
58
99
  }
59
100
  if (!Array.isArray(config.permissions)) {
60
- return { changed: false, skipped: true };
101
+ return { changed: false, skipped: [], shapeUnreadable: true };
61
102
  }
62
103
  const rules: unknown[] = config.permissions;
63
- if (rules.some((rule) => isRuleRecord(rule) && rule.action === ASK_RULE_ACTION)) {
64
- return { changed: false, skipped: true };
104
+ const skipped: string[] = [];
105
+ let changed = false;
106
+ for (const action of actions) {
107
+ const matching = rules.filter(isRuleRecord).filter((rule) => rule.action === action);
108
+ if (matching.length > 0) {
109
+ const ours = matching.every((rule) => rule.resource === PERMISSION_RULE_RESOURCE && rule.effect === 'allow');
110
+ if (!ours) skipped.push(action);
111
+ continue;
112
+ }
113
+ rules.push(allowRule(action));
114
+ changed = true;
65
115
  }
66
- rules.push({ action: ASK_RULE_ACTION, resource: ASK_RULE_RESOURCE, effect: 'ask' });
67
- return { changed: true, skipped: false };
116
+ return { changed, skipped, shapeUnreadable: false };
68
117
  }
69
118
 
70
119
  // Mutates config in place. Value-exact deletion only — an entry matching
71
- // action + resource '*' + effect 'ask' exactly is ours; a user-modified
72
- // entry (any other effect or resource) survives. The array key is pruned
73
- // only when OUR removals emptied it; a pre-existing empty array is left
74
- // untouched. Returns whether anything changed.
75
- export function removeAskPermissionRule(config: Record<string, unknown>): boolean {
120
+ // action + resource '*' + the given effect exactly is ours; a user-modified
121
+ // entry (any other effect or resource) survives. The array key is pruned only
122
+ // when OUR removals emptied it; a pre-existing empty array is left untouched.
123
+ function removeExactRules(
124
+ config: Record<string, unknown>,
125
+ actions: readonly string[],
126
+ effect: 'allow' | 'ask',
127
+ ): boolean {
76
128
  if (!Array.isArray(config.permissions)) return false;
77
129
  const rules: unknown[] = config.permissions;
78
130
  const kept = rules.filter(
79
131
  (rule) =>
80
132
  !(
81
133
  isRuleRecord(rule) &&
82
- rule.action === ASK_RULE_ACTION &&
83
- rule.resource === ASK_RULE_RESOURCE &&
84
- rule.effect === 'ask'
134
+ typeof rule.action === 'string' &&
135
+ actions.includes(rule.action) &&
136
+ rule.resource === PERMISSION_RULE_RESOURCE &&
137
+ rule.effect === effect
85
138
  ),
86
139
  );
87
140
  if (kept.length === rules.length) return false;
@@ -93,39 +146,205 @@ export function removeAskPermissionRule(config: Record<string, unknown>): boolea
93
146
  return true;
94
147
  }
95
148
 
96
- // The effective posture of the ask action. OpenCode evaluates
97
- // last-matching-rule-wins, so the LAST entry on the action is the
98
- // effective one — read that, never the first. Returns its effect
99
- // ('ask' / 'allow' / 'deny'), 'absent' when no entry matches (or the
100
- // array is empty / the key is missing), and 'foreign' for an unreadable
101
- // posture: `permissions` present but not an array, or the last matching
102
- // entry carrying an effect outside the allow/ask/deny vocabulary.
103
- export function inspectAskPermissionRule(
149
+ // The client half at uninstall: every devkit-written allow rule, value-exact.
150
+ export function removeAllowPermissionRules(
104
151
  config: Record<string, unknown>,
105
- ): 'ask' | 'allow' | 'deny' | 'absent' | 'foreign' {
106
- if (config.permissions === undefined) return 'absent';
107
- if (!Array.isArray(config.permissions)) return 'foreign';
108
- const rules: unknown[] = config.permissions;
109
- for (let i = rules.length - 1; i >= 0; i--) {
110
- const rule = asRecord(rules[i]);
111
- if (rule.action !== ASK_RULE_ACTION) continue;
112
- if (rule.effect === 'ask' || rule.effect === 'allow' || rule.effect === 'deny') {
113
- return rule.effect;
152
+ actions: readonly string[] = allowRuleActions(),
153
+ ): boolean {
154
+ return removeExactRules(config, actions, 'allow');
155
+ }
156
+
157
+ // The abandoned ask posture, value-exact: migration (install/update) and
158
+ // uninstall both remove it.
159
+ export function removeAskPermissionRules(
160
+ config: Record<string, unknown>,
161
+ actions: readonly string[] = allowRuleActions(),
162
+ ): boolean {
163
+ return removeExactRules(config, actions, 'ask');
164
+ }
165
+
166
+ // ── V1: the `permission` object form (opencode 1.19.0 – 1.x) ──
167
+
168
+ // Mutates config in place. Fresh configs get the N-key allow object; object
169
+ // configs without a roster key get it APPENDED — V1's evaluate() is findLast
170
+ // over the fromConfig() ruleset, and Object.entries preserves insertion
171
+ // order, so the appended key is the effective one. A key already set to
172
+ // 'allow' is the target state (reinstall no-op); any other value is the
173
+ // user's and is skipped by name. A scalar-shaped `permission` (the user's
174
+ // global posture) is never converted.
175
+ export function applyAllowPermissionRulesV1(
176
+ config: Record<string, unknown>,
177
+ actions: readonly string[] = allowRuleActions(),
178
+ ): PermissionRuleApplyResult {
179
+ if (config.permission === undefined) {
180
+ config.permission = Object.fromEntries(actions.map((action) => [action, 'allow']));
181
+ return { changed: actions.length > 0, skipped: [], shapeUnreadable: false };
182
+ }
183
+ if (!isRuleRecord(config.permission)) {
184
+ return { changed: false, skipped: [], shapeUnreadable: true };
185
+ }
186
+ const permission: Record<string, unknown> = config.permission;
187
+ const skipped: string[] = [];
188
+ let changed = false;
189
+ for (const action of actions) {
190
+ const value: unknown = permission[action];
191
+ if (value === 'allow') continue;
192
+ if (value !== undefined) {
193
+ skipped.push(action);
194
+ continue;
195
+ }
196
+ permission[action] = 'allow';
197
+ changed = true;
198
+ }
199
+ return { changed, skipped, shapeUnreadable: false };
200
+ }
201
+
202
+ function removeExactKeys(
203
+ config: Record<string, unknown>,
204
+ actions: readonly string[],
205
+ effect: 'allow' | 'ask',
206
+ ): boolean {
207
+ if (!isRuleRecord(config.permission)) return false;
208
+ const permission: Record<string, unknown> = config.permission;
209
+ let removed = false;
210
+ for (const action of actions) {
211
+ if (permission[action] === effect) {
212
+ delete permission[action];
213
+ removed = true;
114
214
  }
115
- return 'foreign';
116
215
  }
117
- return 'absent';
216
+ if (!removed) return false;
217
+ if (Object.keys(permission).length === 0) delete config.permission;
218
+ return true;
118
219
  }
119
220
 
120
- // Floor check for the probe result: the `permissions` array is the
121
- // documented V2 shape, measured on 2.2.7 only. Opencode major >= 2 (the
122
- // anchored `v<major>.` form, mirroring opencodeSeamVersionSupported's
123
- // extract-not-pass-raw polarity); null and unparsable output are
124
- // unsupported — the caller's polarity is skip, keeping BOTH the terminal
125
- // token gate and its mcp env untouched rather than guessing.
221
+ export function removeAllowPermissionRulesV1(
222
+ config: Record<string, unknown>,
223
+ actions: readonly string[] = allowRuleActions(),
224
+ ): boolean {
225
+ return removeExactKeys(config, actions, 'allow');
226
+ }
227
+
228
+ export function removeAskPermissionRulesV1(
229
+ config: Record<string, unknown>,
230
+ actions: readonly string[] = allowRuleActions(),
231
+ ): boolean {
232
+ return removeExactKeys(config, actions, 'ask');
233
+ }
234
+
235
+ // ── Reading the posture (the doctor's half) ──
236
+
237
+ // The effective state of one roster action's client rule.
238
+ // allow our exact '*'-resource allow rule is effective
239
+ // ask the effective rule is a leftover ask (the migration missed it)
240
+ // deny a deny rule is present on the action (the call is bricked)
241
+ // absent no rule on the action
242
+ // user-other a user-owned rule covers the action with another resource
243
+ // foreign the rule is present but unreadable (unknown effect, or a V1
244
+ // pattern map the installer never writes)
245
+ export type RosterRuleState = 'allow' | 'ask' | 'deny' | 'absent' | 'user-other' | 'foreign';
246
+
247
+ export interface RosterRuleReading {
248
+ action: string;
249
+ state: RosterRuleState;
250
+ // An exact devkit-shaped ask row ({ action, resource: '*', effect: 'ask' })
251
+ // is present anywhere on this action, even when a later allow row wins.
252
+ // The migration's contract is that no such row survives an install.
253
+ askResidue: boolean;
254
+ }
255
+
256
+ export interface RosterRulesReading {
257
+ // The section is present but the wrong shape, so no action can be read.
258
+ sectionUnreadable: boolean;
259
+ rules: RosterRuleReading[];
260
+ }
261
+
262
+ export function readPermissionRules(
263
+ config: Record<string, unknown>,
264
+ actions: readonly string[] = allowRuleActions(),
265
+ ): RosterRulesReading {
266
+ if (config.permissions !== undefined && !Array.isArray(config.permissions)) {
267
+ return {
268
+ sectionUnreadable: true,
269
+ rules: actions.map((action) => ({ action, state: 'foreign', askResidue: false })),
270
+ };
271
+ }
272
+ const rules: unknown[] = Array.isArray(config.permissions) ? config.permissions : [];
273
+ return {
274
+ sectionUnreadable: false,
275
+ rules: actions.map((action) => {
276
+ const matching = rules.filter(isRuleRecord).filter((rule) => rule.action === action);
277
+ const askResidue = matching.some((rule) => rule.resource === PERMISSION_RULE_RESOURCE && rule.effect === 'ask');
278
+ // A deny anywhere on the action is a bricked call, whether or not a
279
+ // later allow row currently shadows it for some resources.
280
+ if (matching.some((rule) => rule.effect === 'deny')) return { action, state: 'deny', askResidue };
281
+ const exact = matching.filter((rule) => rule.resource === PERMISSION_RULE_RESOURCE);
282
+ const last = exact.at(-1);
283
+ if (last === undefined) {
284
+ return { action, state: matching.length > 0 ? 'user-other' : 'absent', askResidue };
285
+ }
286
+ if (last.effect === 'allow') return { action, state: 'allow', askResidue };
287
+ if (last.effect === 'ask') return { action, state: 'ask', askResidue };
288
+ return { action, state: 'foreign', askResidue };
289
+ }),
290
+ };
291
+ }
292
+
293
+ export function readPermissionRulesV1(
294
+ config: Record<string, unknown>,
295
+ actions: readonly string[] = allowRuleActions(),
296
+ ): RosterRulesReading {
297
+ if (config.permission !== undefined && !isRuleRecord(config.permission)) {
298
+ return {
299
+ sectionUnreadable: true,
300
+ rules: actions.map((action) => ({ action, state: 'foreign', askResidue: false })),
301
+ };
302
+ }
303
+ const permission: Record<string, unknown> = asRecord(config.permission);
304
+ return {
305
+ sectionUnreadable: false,
306
+ rules: actions.map((action) => {
307
+ const value: unknown = permission[action];
308
+ if (value === undefined) return { action, state: 'absent', askResidue: false };
309
+ if (value === 'allow') return { action, state: 'allow', askResidue: false };
310
+ if (value === 'ask') return { action, state: 'ask', askResidue: true };
311
+ if (value === 'deny') return { action, state: 'deny', askResidue: false };
312
+ // A pattern map (or any other value) is not a single-effect posture;
313
+ // the installer never writes one, so it is unverifiable, never ours.
314
+ return { action, state: 'foreign', askResidue: false };
315
+ }),
316
+ };
317
+ }
318
+
319
+ // Floor check for the probe result: the `permissions` array is the documented
320
+ // V2 shape, measured on 2.2.7 only. Opencode major >= 2 (the anchored
321
+ // `v<major>.` form, mirroring opencodeSeamVersionSupported's
322
+ // extract-not-pass-raw polarity); null and unparsable output are unsupported
323
+ // — the caller's polarity is skip, keeping BOTH the terminal token gate and
324
+ // its mcp env untouched rather than guessing.
126
325
  export function opencodeAskRuleVersionSupported(version: string | null): boolean {
127
- if (version === null) return false;
128
- const match = /^v?(\d+)\./.exec(version.trim());
129
- if (!match) return false;
130
- return Number(match[1]) >= 2;
326
+ return askRuleShapeFor(version) === 'v2-array';
327
+ }
328
+
329
+ // The rule shape an opencode version carries. The V2 `permissions` array is
330
+ // measured on 2.2.7; the V1 `permission` object is source-verified on the
331
+ // v1.19.0 tag (mcp/catalog.ts names the run tool's permission key
332
+ // byte-identically to the roster action; session/tools.ts wraps every mcp
333
+ // tool execute in a runtime Permission.ask) — the same 1.19.0 knowledge
334
+ // boundary as the skills-tree seam (lib/opencode-permission-seam.ts).
335
+ // Earlier or unparsable versions are unsupported: neither half of the
336
+ // posture is written (the pair must move together — see
337
+ // docs/plan-run-approval-contract.md), though the legacy ask rules are still
338
+ // migrated away.
339
+ export type AskRuleShape = 'v2-array' | 'v1-object' | 'unsupported';
340
+
341
+ export function askRuleShapeFor(version: string | null): AskRuleShape {
342
+ if (version === null) return 'unsupported';
343
+ const match = /^v?(\d+)\.(\d+)\.(\d+)/.exec(version.trim());
344
+ if (!match) return 'unsupported';
345
+ const major = Number(match[1]);
346
+ const minor = Number(match[2]);
347
+ if (major >= 2) return 'v2-array';
348
+ if (major === 1 && minor >= 19) return 'v1-object';
349
+ return 'unsupported';
131
350
  }