huaweicloud-devkit 2.0.0-next.1 → 2.0.0-next.3
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.
- package/README.md +3 -1
- package/package.json +1 -1
- package/plugins/huaweicloud-core/.claude-plugin/plugin.json +1 -1
- package/plugins/huaweicloud-core/.codex-plugin/plugin.json +1 -1
- package/plugins/huaweicloud-core/.cursor-plugin/plugin.json +1 -1
- package/plugins/huaweicloud-core/.hermes-plugin/plugin.json +1 -1
- package/plugins/huaweicloud-core/.workbuddy-plugin/plugin.json +1 -1
- package/plugins/huaweicloud-core/dist/agents/codearts-work.js +4 -0
- package/plugins/huaweicloud-core/dist/agents/codearts.js +4 -0
- package/plugins/huaweicloud-core/dist/agents/opencode-config.js +73 -2
- package/plugins/huaweicloud-core/dist/agents/opencode.js +62 -1
- package/plugins/huaweicloud-core/dist/auth/s1-store.js +43 -22
- package/plugins/huaweicloud-core/dist/hcloud-plan.js +25 -5
- package/plugins/huaweicloud-core/dist/lib/approval-mode.js +30 -0
- package/plugins/huaweicloud-core/dist/lib/approval-tokens.js +3 -3
- package/plugins/huaweicloud-core/dist/lib/opencode-ask-rule.js +116 -0
- package/plugins/huaweicloud-core/dist/lib/pack-state.js +23 -19
- package/plugins/huaweicloud-core/dist/lib/param-warnings.js +70 -0
- package/plugins/huaweicloud-core/dist/lib/state-dir.js +125 -0
- package/plugins/huaweicloud-core/dist/mcp-protocol.js +20 -1
- package/plugins/huaweicloud-core/dist/mcp-server.js +849 -585
- package/plugins/huaweicloud-core/dist/mcp-server.js.map +4 -4
- package/plugins/huaweicloud-core/dist/packs/huaweicloud-cli/tools.js +74 -22
- package/plugins/huaweicloud-core/dist/setup-cli.js +22 -3
- package/plugins/huaweicloud-core/dist/tool-schemas.js +15 -0
- package/plugins/huaweicloud-core/dist/tools.js +36 -4
- package/plugins/huaweicloud-core/dist/update-skip.js +5 -6
- package/plugins/huaweicloud-core/openclaw.plugin.json +1 -1
- package/plugins/huaweicloud-core/safety/rules/cloud-risk-rules.json +78 -2
- package/plugins/huaweicloud-core/skills/huaweicloud-cli-and-auth/SKILL.md +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/SKILL.md +8 -7
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/references/approvals.md +10 -0
- package/plugins/huaweicloud-core/src/agents/codearts-work.ts +3 -0
- package/plugins/huaweicloud-core/src/agents/codearts.ts +4 -0
- package/plugins/huaweicloud-core/src/agents/opencode-config.ts +82 -2
- package/plugins/huaweicloud-core/src/agents/opencode.ts +86 -1
- package/plugins/huaweicloud-core/src/auth/s1-store.ts +41 -20
- package/plugins/huaweicloud-core/src/hcloud-plan.ts +38 -6
- package/plugins/huaweicloud-core/src/lib/approval-mode.ts +32 -0
- package/plugins/huaweicloud-core/src/lib/approval-tokens.ts +3 -3
- package/plugins/huaweicloud-core/src/lib/opencode-ask-rule.ts +131 -0
- package/plugins/huaweicloud-core/src/lib/pack-state.ts +23 -20
- package/plugins/huaweicloud-core/src/lib/param-warnings.ts +69 -0
- package/plugins/huaweicloud-core/src/lib/state-dir.ts +128 -0
- package/plugins/huaweicloud-core/src/mcp-protocol.ts +20 -1
- package/plugins/huaweicloud-core/src/mcp-server.ts +13 -0
- package/plugins/huaweicloud-core/src/packs/huaweicloud-cli/tools.ts +81 -24
- package/plugins/huaweicloud-core/src/setup-cli.ts +27 -2
- package/plugins/huaweicloud-core/src/tool-schemas.ts +16 -0
- package/plugins/huaweicloud-core/src/tools.ts +39 -4
- package/plugins/huaweicloud-core/src/update-skip.ts +5 -6
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// The OpenCode ask-mode permission rule — pure merge/cleanup logic.
|
|
2
|
+
//
|
|
3
|
+
// 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.
|
|
18
|
+
//
|
|
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.
|
|
26
|
+
//
|
|
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.
|
|
30
|
+
import { asRecord } from "./as-record.js";
|
|
31
|
+
export const ASK_RULE_ACTION = 'huaweicloud-devkit_huaweicloud_run_approved_command';
|
|
32
|
+
export const ASK_RULE_RESOURCE = '*';
|
|
33
|
+
function isRuleRecord(value) {
|
|
34
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
35
|
+
}
|
|
36
|
+
// Mutates config in place. Fresh configs get the single-entry array;
|
|
37
|
+
// arrays without an entry on the ask action get ours APPENDED — OpenCode
|
|
38
|
+
// evaluates last-matching-rule-wins, so the appended entry is the
|
|
39
|
+
// effective one. Arrays that already carry an entry on the action, and
|
|
40
|
+
// non-array `permissions` shapes, are the user's: nothing is written.
|
|
41
|
+
export function applyAskPermissionRule(config) {
|
|
42
|
+
if (config.permissions === undefined) {
|
|
43
|
+
config.permissions = [{ action: ASK_RULE_ACTION, resource: ASK_RULE_RESOURCE, effect: 'ask' }];
|
|
44
|
+
return { changed: true, skipped: false };
|
|
45
|
+
}
|
|
46
|
+
if (!Array.isArray(config.permissions)) {
|
|
47
|
+
return { changed: false, skipped: true };
|
|
48
|
+
}
|
|
49
|
+
const rules = config.permissions;
|
|
50
|
+
if (rules.some((rule) => isRuleRecord(rule) && rule.action === ASK_RULE_ACTION)) {
|
|
51
|
+
return { changed: false, skipped: true };
|
|
52
|
+
}
|
|
53
|
+
rules.push({ action: ASK_RULE_ACTION, resource: ASK_RULE_RESOURCE, effect: 'ask' });
|
|
54
|
+
return { changed: true, skipped: false };
|
|
55
|
+
}
|
|
56
|
+
// Mutates config in place. Value-exact deletion only — an entry matching
|
|
57
|
+
// action + resource '*' + effect 'ask' exactly is ours; a user-modified
|
|
58
|
+
// entry (any other effect or resource) survives. The array key is pruned
|
|
59
|
+
// only when OUR removals emptied it; a pre-existing empty array is left
|
|
60
|
+
// untouched. Returns whether anything changed.
|
|
61
|
+
export function removeAskPermissionRule(config) {
|
|
62
|
+
if (!Array.isArray(config.permissions))
|
|
63
|
+
return false;
|
|
64
|
+
const rules = config.permissions;
|
|
65
|
+
const kept = rules.filter((rule) => !(isRuleRecord(rule) &&
|
|
66
|
+
rule.action === ASK_RULE_ACTION &&
|
|
67
|
+
rule.resource === ASK_RULE_RESOURCE &&
|
|
68
|
+
rule.effect === 'ask'));
|
|
69
|
+
if (kept.length === rules.length)
|
|
70
|
+
return false;
|
|
71
|
+
if (kept.length === 0) {
|
|
72
|
+
delete config.permissions;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
config.permissions = kept;
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
// The effective posture of the ask action. OpenCode evaluates
|
|
80
|
+
// last-matching-rule-wins, so the LAST entry on the action is the
|
|
81
|
+
// effective one — read that, never the first. Returns its effect
|
|
82
|
+
// ('ask' / 'allow' / 'deny'), 'absent' when no entry matches (or the
|
|
83
|
+
// array is empty / the key is missing), and 'foreign' for an unreadable
|
|
84
|
+
// posture: `permissions` present but not an array, or the last matching
|
|
85
|
+
// entry carrying an effect outside the allow/ask/deny vocabulary.
|
|
86
|
+
export function inspectAskPermissionRule(config) {
|
|
87
|
+
if (config.permissions === undefined)
|
|
88
|
+
return 'absent';
|
|
89
|
+
if (!Array.isArray(config.permissions))
|
|
90
|
+
return 'foreign';
|
|
91
|
+
const rules = config.permissions;
|
|
92
|
+
for (let i = rules.length - 1; i >= 0; i--) {
|
|
93
|
+
const rule = asRecord(rules[i]);
|
|
94
|
+
if (rule.action !== ASK_RULE_ACTION)
|
|
95
|
+
continue;
|
|
96
|
+
if (rule.effect === 'ask' || rule.effect === 'allow' || rule.effect === 'deny') {
|
|
97
|
+
return rule.effect;
|
|
98
|
+
}
|
|
99
|
+
return 'foreign';
|
|
100
|
+
}
|
|
101
|
+
return 'absent';
|
|
102
|
+
}
|
|
103
|
+
// Floor check for the probe result: the `permissions` array is the
|
|
104
|
+
// documented V2 shape, measured on 2.2.7 only. Opencode major >= 2 (the
|
|
105
|
+
// anchored `v<major>.` form, mirroring opencodeSeamVersionSupported's
|
|
106
|
+
// extract-not-pass-raw polarity); null and unparsable output are
|
|
107
|
+
// unsupported — the caller's polarity is skip, keeping BOTH the terminal
|
|
108
|
+
// token gate and its mcp env untouched rather than guessing.
|
|
109
|
+
export function opencodeAskRuleVersionSupported(version) {
|
|
110
|
+
if (version === null)
|
|
111
|
+
return false;
|
|
112
|
+
const match = /^v?(\d+)\./.exec(version.trim());
|
|
113
|
+
if (!match)
|
|
114
|
+
return false;
|
|
115
|
+
return Number(match[1]) >= 2;
|
|
116
|
+
}
|
|
@@ -1,23 +1,29 @@
|
|
|
1
1
|
import { chmodSync, lstatSync, mkdirSync, realpathSync, writeFileSync } from 'node:fs';
|
|
2
|
-
import {
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
3
|
import { basename, dirname, join, resolve, sep } from 'node:path';
|
|
4
|
+
import { devkitStateBase, huaweiConfigBase } from "./state-dir.js";
|
|
5
|
+
export { devkitStateBase, huaweiConfigBase };
|
|
4
6
|
// The pack-scoped state space and the single resolver for the devkit's
|
|
5
7
|
// config tree (~/.config/huaweicloud, relocatable whole-tree via
|
|
6
|
-
// HUAWEICLOUD_HOME).
|
|
8
|
+
// HUAWEICLOUD_HOME) and its writable state tree (state-dir.ts: the same tree
|
|
9
|
+
// by default, the agent-owned devkit-state directory on a read-only home).
|
|
7
10
|
//
|
|
8
11
|
// Ownership criterion, stated here so the next state file does not have to
|
|
9
12
|
// guess: the writer/reader set decides where state lives. approvals.json
|
|
10
13
|
// is the CLI approval face's store (plan_cli_command /
|
|
11
14
|
// run_approved_command mint and consume), credentials.json is the
|
|
12
15
|
// devkit-wide S1 store, and deps/ belongs to the dependency domain — all
|
|
13
|
-
// three
|
|
16
|
+
// three live in the state tree. State with exactly one pack as writer
|
|
14
17
|
// and reader (the open-mcp gateway's policy file) lives under packs/<id>/,
|
|
15
18
|
// so ownership is derivable from the directory name and an uninstall can
|
|
16
19
|
// preserve user state without a manifest. Modules that still hand-roll
|
|
17
|
-
// the base (
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
20
|
+
// the base (dep/receipts, update-check, proxy-config, officeace-paths)
|
|
21
|
+
// migrate when next touched; the guard below already resolves through this
|
|
22
|
+
// module, so guard and store cannot disagree about where the trees are.
|
|
23
|
+
//
|
|
24
|
+
// S1 is the one exception: credentials are WRITTEN to the state tree but
|
|
25
|
+
// READ from the state tree first and the config tree second, so a
|
|
26
|
+
// pre-provisioned credentials.json in a read-only home keeps resolving.
|
|
21
27
|
//
|
|
22
28
|
// The same resolver backs the protected-tree guard the gateway face uses:
|
|
23
29
|
// anything the devkit or its operator considers consent infrastructure (the
|
|
@@ -30,15 +36,8 @@ import { basename, dirname, join, resolve, sep } from 'node:path';
|
|
|
30
36
|
// Runtime import graph stays at node builtins plus a type-only edge (PackId)
|
|
31
37
|
// — the installer chain (dist/setup-cli.js, plain tsc output) must never
|
|
32
38
|
// pull zod, same contract as dependency-model.ts and dep-resolve.ts.
|
|
33
|
-
export function huaweiConfigBase(env = process.env) {
|
|
34
|
-
// resolve() absolutizes once, here, so every consumer — store writes and
|
|
35
|
-
// the guard below — shares one anchor even for a relative
|
|
36
|
-
// HUAWEICLOUD_HOME (a relative base previously made the store land at
|
|
37
|
-
// the process cwd while the guard protected a tmpdir-anchored tree).
|
|
38
|
-
return resolve(env.HUAWEICLOUD_HOME || homedir(), '.config', 'huaweicloud');
|
|
39
|
-
}
|
|
40
39
|
export function packStateDir(packId, env = process.env) {
|
|
41
|
-
return join(
|
|
40
|
+
return join(devkitStateBase(env), 'packs', packId);
|
|
42
41
|
}
|
|
43
42
|
// Idempotent provisioning, called at lazy-spawn time (a trimmed pack never
|
|
44
43
|
// calls it, so trimming keeps its zero-files-on-disk guarantee). Permissions
|
|
@@ -136,22 +135,27 @@ function resolveForWrite(target) {
|
|
|
136
135
|
return null;
|
|
137
136
|
}
|
|
138
137
|
}
|
|
139
|
-
// The roots a gateway file write must never touch: the devkit config tree
|
|
140
|
-
//
|
|
141
|
-
//
|
|
138
|
+
// The roots a gateway file write must never touch: the devkit config tree,
|
|
139
|
+
// the writable state tree (they differ on a read-only-home fallback), plus
|
|
140
|
+
// whatever policy/audit paths the operator configured (the LIVE paths, not
|
|
141
|
+
// what the devkit would have provisioned). Returns null when a root is
|
|
142
142
|
// present-but-unresolvable — callers deny everything in that case.
|
|
143
143
|
export function protectedTreeRoots(env = process.env) {
|
|
144
|
-
const candidates = [huaweiConfigBase(env)];
|
|
144
|
+
const candidates = [huaweiConfigBase(env), devkitStateBase(env)];
|
|
145
145
|
for (const key of ['HUAWEICLOUD_MCP_POLICY_FILE', 'HUAWEICLOUD_MCP_AUDIT_FILE']) {
|
|
146
146
|
const value = (env[key] ?? '').trim();
|
|
147
147
|
if (value !== '')
|
|
148
148
|
candidates.push(value);
|
|
149
149
|
}
|
|
150
150
|
const roots = [];
|
|
151
|
+
const seen = new Set();
|
|
151
152
|
for (const candidate of candidates) {
|
|
152
153
|
const resolved = resolveForWrite(candidate);
|
|
153
154
|
if (resolved === null)
|
|
154
155
|
return null;
|
|
156
|
+
if (seen.has(resolved))
|
|
157
|
+
continue;
|
|
158
|
+
seen.add(resolved);
|
|
155
159
|
roots.push(resolved);
|
|
156
160
|
}
|
|
157
161
|
return roots;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Unknown-parameter warnings for the MCP boundary. The #530 leniency keeps
|
|
2
|
+
// accepting keys a tool does not declare (model wire shapes are noisy and a
|
|
3
|
+
// rejection used to cost the whole call), but silently dropping a typo makes
|
|
4
|
+
// `pack` vs `pak` invisible — this module turns that into an additive
|
|
5
|
+
// `warnings` entry without changing the acceptance decision.
|
|
6
|
+
const MAX_WARNINGS = 5;
|
|
7
|
+
const SUGGESTION_DISTANCE = 2;
|
|
8
|
+
function levenshtein(a, b) {
|
|
9
|
+
const rows = a.length + 1;
|
|
10
|
+
const cols = b.length + 1;
|
|
11
|
+
const previous = new Array(cols);
|
|
12
|
+
const current = new Array(cols);
|
|
13
|
+
for (let j = 0; j < cols; j++)
|
|
14
|
+
previous[j] = j;
|
|
15
|
+
for (let i = 1; i < rows; i++) {
|
|
16
|
+
current[0] = i;
|
|
17
|
+
for (let j = 1; j < cols; j++) {
|
|
18
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
19
|
+
current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
|
|
20
|
+
}
|
|
21
|
+
for (let j = 0; j < cols; j++)
|
|
22
|
+
previous[j] = current[j];
|
|
23
|
+
}
|
|
24
|
+
return previous[cols - 1];
|
|
25
|
+
}
|
|
26
|
+
export function suggestParam(key, knownKeys) {
|
|
27
|
+
let best = null;
|
|
28
|
+
let bestDistance = SUGGESTION_DISTANCE + 1;
|
|
29
|
+
const lowerKey = key.toLowerCase();
|
|
30
|
+
for (const candidate of knownKeys) {
|
|
31
|
+
if (candidate === key)
|
|
32
|
+
continue;
|
|
33
|
+
const distance = levenshtein(lowerKey, candidate.toLowerCase());
|
|
34
|
+
if (distance > SUGGESTION_DISTANCE)
|
|
35
|
+
continue;
|
|
36
|
+
if (distance < bestDistance || (distance === bestDistance && best !== null && candidate < best)) {
|
|
37
|
+
best = candidate;
|
|
38
|
+
bestDistance = distance;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return best;
|
|
42
|
+
}
|
|
43
|
+
export function unknownParamWarnings(args, knownKeys) {
|
|
44
|
+
const unknown = Object.keys(args)
|
|
45
|
+
.filter((key) => !knownKeys.has(key))
|
|
46
|
+
.sort((a, b) => a.localeCompare(b));
|
|
47
|
+
return unknown.slice(0, MAX_WARNINGS).map((key) => {
|
|
48
|
+
const suggestion = suggestParam(key, knownKeys);
|
|
49
|
+
return suggestion
|
|
50
|
+
? `Unknown parameter "${key}" was ignored. Did you mean "${suggestion}"?`
|
|
51
|
+
: `Unknown parameter "${key}" was ignored (no matching parameter).`;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
// Additive by construction: a warnings array is merged, an absent field is
|
|
55
|
+
// created, and any other existing shape is left untouched under paramWarnings.
|
|
56
|
+
// Non-object results (there are none on the shipped surface) pass through.
|
|
57
|
+
export function attachParamWarnings(result, warnings) {
|
|
58
|
+
if (warnings.length === 0)
|
|
59
|
+
return result;
|
|
60
|
+
if (result === null || typeof result !== 'object' || Array.isArray(result))
|
|
61
|
+
return result;
|
|
62
|
+
const record = result;
|
|
63
|
+
if (Array.isArray(record.warnings)) {
|
|
64
|
+
return { ...record, warnings: [...record.warnings, ...warnings] };
|
|
65
|
+
}
|
|
66
|
+
if (record.warnings === undefined) {
|
|
67
|
+
return { ...record, warnings };
|
|
68
|
+
}
|
|
69
|
+
return { ...record, paramWarnings: warnings };
|
|
70
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { existsSync, lstatSync, mkdirSync, rmSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
4
|
+
// The devkit's two directory roots. `configBase` is the historical
|
|
5
|
+
// ~/.config/huaweicloud tree (credentials, approvals, pack state, proxy and
|
|
6
|
+
// update state); `stateBase` is the same tree by default and moves to a
|
|
7
|
+
// writable agent-owned directory only when the config tree cannot be written.
|
|
8
|
+
//
|
|
9
|
+
// The split exists because some agent sandboxes ship a read-only home: the
|
|
10
|
+
// gateway's pack state, approval tokens, S1 writes and update state all need
|
|
11
|
+
// a writable directory, while the pre-provisioned credentials.json must stay
|
|
12
|
+
// readable at its original location. The installer/CLI and the MCP server
|
|
13
|
+
// resolve the same paths from the same filesystem, so no env plumbing is
|
|
14
|
+
// required for them to agree.
|
|
15
|
+
//
|
|
16
|
+
// Runtime import graph stays at node builtins — pack-state.ts (the stable
|
|
17
|
+
// resolver face) and the installer graph import this module.
|
|
18
|
+
const STATE_DIR_ENV = 'HUAWEICLOUD_STATE_DIR';
|
|
19
|
+
const AGENT_STATE_MARKERS = ['.codearts', '.codeartswork', '.codeartsdoer'];
|
|
20
|
+
const stateBaseCache = new Map();
|
|
21
|
+
export function huaweiConfigBase(env = process.env) {
|
|
22
|
+
// resolve() absolutizes once, here, so every consumer — store writes and
|
|
23
|
+
// the protected-tree guard — shares one anchor even for a relative
|
|
24
|
+
// HUAWEICLOUD_HOME (a relative base previously made the store land at
|
|
25
|
+
// the process cwd while the guard protected a tmpdir-anchored tree).
|
|
26
|
+
return resolve(env.HUAWEICLOUD_HOME || homedir(), '.config', 'huaweicloud');
|
|
27
|
+
}
|
|
28
|
+
function agentHomeBase(env) {
|
|
29
|
+
return resolve(env.HUAWEICLOUD_HOME || homedir());
|
|
30
|
+
}
|
|
31
|
+
function nearestExisting(path) {
|
|
32
|
+
let current = resolve(path);
|
|
33
|
+
for (;;) {
|
|
34
|
+
try {
|
|
35
|
+
lstatSync(current);
|
|
36
|
+
return current;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
const parent = dirname(current);
|
|
40
|
+
if (parent === current)
|
|
41
|
+
return null;
|
|
42
|
+
current = parent;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// A real create/remove probe, not access(W_OK): Windows ACLs and WSL drvfs
|
|
47
|
+
// can report a writable mode while mkdir still fails, and the gateway's first
|
|
48
|
+
// pack-state provisioning is exactly a mkdir under the config tree.
|
|
49
|
+
export function probeWritableDir(path) {
|
|
50
|
+
const anchor = nearestExisting(path);
|
|
51
|
+
if (!anchor)
|
|
52
|
+
return false;
|
|
53
|
+
const probe = join(anchor, `.devkit-write-probe-${process.pid}-${Math.random().toString(36).slice(2)}`);
|
|
54
|
+
try {
|
|
55
|
+
mkdirSync(probe, { mode: 0o700 });
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
try {
|
|
63
|
+
rmSync(probe, { recursive: true, force: true });
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// best-effort cleanup; the probe directory is empty and 0700
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// Agent-owned writable roots, checked in the same order the installer targets
|
|
71
|
+
// them: CodeArts Work's new layout, its legacy home, then CodeArts.
|
|
72
|
+
function codeArtsStateBase(env) {
|
|
73
|
+
const home = agentHomeBase(env);
|
|
74
|
+
for (const marker of AGENT_STATE_MARKERS) {
|
|
75
|
+
const root = join(home, marker);
|
|
76
|
+
if (!existsSync(root) || !probeWritableDir(root))
|
|
77
|
+
continue;
|
|
78
|
+
return join(root, 'devkit-state');
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
function explicitStateBase(env) {
|
|
83
|
+
const value = (env[STATE_DIR_ENV] ?? '').trim();
|
|
84
|
+
if (value === '')
|
|
85
|
+
return null;
|
|
86
|
+
if (!isAbsolute(value)) {
|
|
87
|
+
throw new Error(`${STATE_DIR_ENV} must be an absolute path (got "${value}"): the CLI and the MCP server resolve relative paths against different working directories.`);
|
|
88
|
+
}
|
|
89
|
+
return resolve(value);
|
|
90
|
+
}
|
|
91
|
+
export function devkitStateBase(env = process.env) {
|
|
92
|
+
const explicit = explicitStateBase(env);
|
|
93
|
+
if (explicit)
|
|
94
|
+
return explicit;
|
|
95
|
+
const configBase = huaweiConfigBase(env);
|
|
96
|
+
const cached = stateBaseCache.get(configBase);
|
|
97
|
+
if (cached)
|
|
98
|
+
return cached;
|
|
99
|
+
const stateBase = probeWritableDir(configBase) ? configBase : (codeArtsStateBase(env) ?? configBase);
|
|
100
|
+
stateBaseCache.set(configBase, stateBase);
|
|
101
|
+
return stateBase;
|
|
102
|
+
}
|
|
103
|
+
// Doctor/installer display form: home-relative paths stay stable in the
|
|
104
|
+
// golden fixtures and read better to a human. Forward slashes keep the
|
|
105
|
+
// rendered form identical on Windows and POSIX (the goldens are committed
|
|
106
|
+
// once, not per platform).
|
|
107
|
+
export function displayStatePath(path, env = process.env) {
|
|
108
|
+
const home = agentHomeBase(env).replace(/\\/g, '/');
|
|
109
|
+
const normalized = path.replace(/\\/g, '/');
|
|
110
|
+
if (normalized === home)
|
|
111
|
+
return '~';
|
|
112
|
+
return normalized.startsWith(home + '/') ? `~${normalized.slice(home.length)}` : normalized;
|
|
113
|
+
}
|
|
114
|
+
export function stateDirNotice(env = process.env) {
|
|
115
|
+
const configBase = huaweiConfigBase(env);
|
|
116
|
+
const stateBase = devkitStateBase(env);
|
|
117
|
+
if (stateBase === configBase)
|
|
118
|
+
return null;
|
|
119
|
+
return `state directory ${displayStatePath(stateBase, env)} (${displayStatePath(configBase, env)} is not writable; set ${STATE_DIR_ENV} to pin it)`;
|
|
120
|
+
}
|
|
121
|
+
// Tests swap HUAWEICLOUD_HOME between cases; the cache is keyed by configBase,
|
|
122
|
+
// so this is only for a case that changes permissions under one home.
|
|
123
|
+
export function _resetStateDirCache() {
|
|
124
|
+
stateBaseCache.clear();
|
|
125
|
+
}
|
|
@@ -8,6 +8,7 @@ import { peekCachedUpdateInfo, applyUpdateHint, readInstalledVersion } from "./u
|
|
|
8
8
|
import { initTelemetry } from "./telemetry/telemetry.js";
|
|
9
9
|
import { detectAgent } from "./telemetry/agent-detect.js";
|
|
10
10
|
import { asRecord } from "./lib/as-record.js";
|
|
11
|
+
import { approvalModeFromEnv } from "./lib/approval-mode.js";
|
|
11
12
|
const pkgVersion = readInstalledVersion() || '0.0.0';
|
|
12
13
|
// Server-level instructions ride the initialize handshake result — the one
|
|
13
14
|
// teaching surface that needs neither a skill load nor a description read.
|
|
@@ -217,6 +218,11 @@ export function normalizeToolCallParams(message) {
|
|
|
217
218
|
}
|
|
218
219
|
export function createDevkitMcpServer({ sessionId, runtimePacks = false, remoteSurface = false, } = {}) {
|
|
219
220
|
assertConstructedSchemas();
|
|
221
|
+
// The approval posture resolves once at construction: a bogus
|
|
222
|
+
// DEVKIT_APPROVAL_MODE throws here (boot fail-fast, the same doctrine
|
|
223
|
+
// DEVKIT_PACKS applies through syncDisclosure), and registerOne below is
|
|
224
|
+
// the one place the mode changes the wire surface.
|
|
225
|
+
const askMode = approvalModeFromEnv(process.env) === 'ask';
|
|
220
226
|
// The contradictory construction is refused, not cautioned: a remote that
|
|
221
227
|
// also carried runtime pack toggling would attach the onPacksChanged seam
|
|
222
228
|
// to every callOpts, publishing the server host's absolute install path
|
|
@@ -270,7 +276,20 @@ export function createDevkitMcpServer({ sessionId, runtimePacks = false, remoteS
|
|
|
270
276
|
(runtimePacks || tool.name !== 'huaweicloud_packs_configure') &&
|
|
271
277
|
(!remoteSurface || (PACK_OF[tool.name] !== 'secrets' && !REMOTE_WITHHELD_LOCAL_WRITERS.has(tool.name)));
|
|
272
278
|
const registerOne = (tool) => {
|
|
273
|
-
registered.set(tool.name, server.registerTool(tool.name, {
|
|
279
|
+
registered.set(tool.name, server.registerTool(tool.name, {
|
|
280
|
+
description: tool.description,
|
|
281
|
+
inputSchema: getToolSchema(tool.name),
|
|
282
|
+
// Ask mode only: Claude Code honors this flag by prompting the
|
|
283
|
+
// user on every call of this tool — even in acceptEdits/auto/
|
|
284
|
+
// bypassPermissions modes, with no "don't ask again" — and that
|
|
285
|
+
// client prompt IS the user's confirmation the mode trusts.
|
|
286
|
+
// Other clients ignore the flag. Declared only in ask mode so
|
|
287
|
+
// token-mode Claude Code users don't gain a second prompt on top
|
|
288
|
+
// of the terminal approve they already run.
|
|
289
|
+
...(askMode && tool.name === 'huaweicloud_run_approved_command'
|
|
290
|
+
? { _meta: { 'anthropic/requiresUserInteraction': true } }
|
|
291
|
+
: {}),
|
|
292
|
+
}, async (args) => {
|
|
274
293
|
const callOpts = runtimePacks
|
|
275
294
|
? { sessionId, onPacksChanged: () => syncDisclosure() }
|
|
276
295
|
: { sessionId };
|