fix(web): repair duplicate-key watchers that emptied the model edit form

ModelEditDialog.vue had two dialogVisible handlers inside its watch
block (leftover from the 2026-10-11 rebase merge). The later, trivial
one ($emit only) clobbered the earlier one that also called
loadModelData()/resetForm(), so opening the model edit dialog never
fetched or populated the config. Collapsing them restores population.

Upgrading integrity-check.js to parse the export-default object and its
watch/computed/methods/data maps for duplicate keys surfaced two more
from the same merge:

- home.vue: a dead handleSearch(regex) clobbered the live handleSearch
  (which reads this.search and hits Api.agent.searchAgent). HeaderBar no
  longer emits @search, so the old regex variant was unreachable.
- KnowledgeBaseManagement.vue: a second formatFileSize shadowed the
  first; keeping upstream's (which the template's file.size uses).

integrity-check.js now catches nested duplicate option keys, not just
top-level ones. Full rebase gate green.
This commit is contained in:
lapy 2026-10-13 17:46:35 +01:00
parent d51c54f483
commit a2bf0fce7d
5 changed files with 100 additions and 53 deletions

View file

@ -134,54 +134,105 @@ for (const f of walk(SRC)) {
} }
} }
// ---- Pass 3: duplicated top-level Vue options keys (class 3) ------------- // ---- Pass 3: duplicated Vue option keys, AST-based (class 3) -------------
// A duplicate key inside an options object is legal JS but silently clobbers
// the first property at object-literal eval time. Rebase merges have produced
// both top-level duplicates (two `computed:` blocks) and nested ones (two
// `dialogVisible` handlers inside `watch:`), the latter disabling the model
// edit dialog's data-population trigger. Check the export-default object at
// depth 1 (Vue options) and inside the option maps at depth 2.
const OPTION_KEYS = new Set([ const OPTION_KEYS = new Set([
'name', 'components', 'directives', 'mixins', 'props', 'data', 'computed', 'name', 'components', 'directives', 'mixins', 'props', 'data', 'computed',
'methods', 'watch', 'created', 'mounted', 'beforeMount', 'updated', 'methods', 'watch', 'created', 'mounted', 'beforeMount', 'updated',
'beforeUpdate', 'destroyed', 'beforeDestroy', 'activated', 'deactivated', 'beforeUpdate', 'destroyed', 'beforeDestroy', 'activated', 'deactivated',
'render', 'template', 'inheritAttrs', 'model', 'provide', 'inject', 'filters', 'render', 'template', 'inheritAttrs', 'model', 'provide', 'inject', 'filters',
]); ]);
function dupOptionKeys(scriptCode) { const OPTION_MAP_KEYS = new Set(['watch', 'computed', 'methods', 'filters', 'directives']);
// Find `export default { ... }` object and read depth-1 keys.
const i = scriptCode.indexOf('export default'); function nodeKey(p) {
if (i < 0) return []; if (!p.key) return null;
const start = scriptCode.indexOf('{', i); if (p.key.type === 'Identifier') return p.key.name;
if (start < 0) return []; if (p.key.type === 'Literal') return String(p.key.value);
const keys = []; return null;
let depth = 0, k = start, str = null;
while (k < scriptCode.length) {
const ch = scriptCode[k];
if (str) {
if (ch === '\\') { k += 2; continue; }
if (ch === str) str = null;
k++; continue;
}
if (ch === '"' || ch === "'" || ch === '`') { str = ch; k++; continue; }
if (ch === '{') { depth++; k++; continue; }
if (ch === '}') { depth--; if (depth === 0) break; k++; continue; }
if (depth === 1) {
const mm = /^([A-Za-z_$][\w$]*)\s*:/.exec(scriptCode.slice(k, k + 60));
if (mm) {
const ls = scriptCode.lastIndexOf('\n', k);
if (scriptCode.slice(ls + 1, k).trim() === '') {
keys.push(mm[1]);
k += mm[0].length - 1;
}
}
}
k++;
}
const seen = new Map();
for (const key of keys) seen.set(key, (seen.get(key) || 0) + 1);
return [...seen].filter(([key, n]) => n > 1 && OPTION_KEYS.has(key)).map(([key]) => key);
} }
function dupKeys(objExpr) {
if (!objExpr || objExpr.type !== 'ObjectExpression') return [];
const seen = new Map();
for (const p of objExpr.properties) {
if (p.type !== 'Property') continue; // spread etc.
const k = nodeKey(p);
if (k === null) continue;
seen.set(k, (seen.get(k) || 0) + 1);
}
return [...seen].filter(([, n]) => n > 1).map(([k]) => k);
}
function optionObject(node) {
// Property value is either an object literal (watch/computed/methods) or a
// function whose single return statement yields one (data).
if (!node) return null;
if (node.type === 'ObjectExpression') return node;
if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
const body = node.body;
if (body.type === 'BlockStatement') {
const ret = body.body.find((s) => s.type === 'ReturnStatement');
if (ret && ret.argument && ret.argument.type === 'ObjectExpression') return ret.argument;
}
}
return null;
}
function exportDefaultObject(ast) {
for (const n of ast.body) {
if (n.type === 'ExportDefaultDeclaration') {
if (n.declaration.type === 'ObjectExpression') return n.declaration;
if (n.declaration.type === 'VariableDeclaration') {
for (const d of n.declaration.declarations) {
if (d.init && d.init.type === 'ObjectExpression') return d.init;
}
}
}
}
return null;
}
for (const f of walk(SRC)) { for (const f of walk(SRC)) {
if (!f.endsWith('.vue')) continue; if (!f.endsWith('.vue')) continue;
const raw = fs.readFileSync(f, 'utf8'); const raw = fs.readFileSync(f, 'utf8');
const sfc = parseComponent(raw); for (const s of scriptsOf(f, raw)) {
const opts = sfc.script ? sfc.script.content : ''; let ast;
for (const dup of dupOptionKeys(opts)) { try {
addError('DUP-OPT', f, null, `duplicate options key "${dup}" (later block clobbers the first)`); ast = acorn.parse(s.code, {
ecmaVersion: 'latest', sourceType: 'module', locations: true, ranges: true,
allowReturnOutsideFunction: true, allowAwaitOutsideFunction: true,
});
} catch {
continue; // PARRESS already reported in pass 1
}
const root = exportDefaultObject(ast);
if (!root) continue;
for (const dup of dupKeys(root)) {
if (OPTION_KEYS.has(dup)) {
addError('DUP-OPT', f, s.offset, `duplicate options key "${dup}" (later value clobbers the first)`);
}
}
for (const p of root.properties) {
const k = nodeKey(p);
if (k === null) continue;
if (OPTION_MAP_KEYS.has(k)) {
for (const dup of dupKeys(optionObject(p.value))) {
addError('DUP-OPT', f, s.offset + (p.loc ? p.loc.start.line - 1 : 0),
`duplicate "${k}" key "${dup}" (later handler clobbers the first)`);
}
}
if (k === 'data') {
for (const dup of dupKeys(optionObject(p.value))) {
addError('DUP-OPT', f, s.offset + (p.loc ? p.loc.start.line - 1 : 0),
`duplicate data() key "${dup}" (later value clobbers the first)`);
}
}
}
} }
} }

View file

@ -153,6 +153,9 @@ export default {
this.resetProviders(); this.resetProviders();
this.loadProviders(); this.loadProviders();
}, },
visible(val) {
this.dialogVisible = val;
},
dialogVisible(val) { dialogVisible(val) {
this.$emit('update:visible', val); this.$emit('update:visible', val);
if (!val) { if (!val) {
@ -161,12 +164,6 @@ export default {
this.loadModelData(); this.loadModelData();
} }
}, },
visible(val) {
this.dialogVisible = val;
},
dialogVisible(val) {
this.$emit("update:visible", val);
},
'form.configJson.type'(newVal) { 'form.configJson.type'(newVal) {
if (newVal && this.providersLoaded) { if (newVal && this.providersLoaded) {
this.loadProviderFields(newVal); this.loadProviderFields(newVal);

View file

@ -487,14 +487,6 @@ export default {
this.selectedFilesList.splice(index, 1); this.selectedFilesList.splice(index, 1);
}, },
formatFileSize(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
},
handleBatchUploadSubmit() { handleBatchUploadSubmit() {
if (this.selectedFilesList.length === 0) { if (this.selectedFilesList.length === 0) {
this.$message.error(this.$t('knowledgeFileUpload.fileRequired')); this.$message.error(this.$t('knowledgeFileUpload.fileRequired'));

View file

@ -181,11 +181,6 @@ export default {
handleDeviceManage() { handleDeviceManage() {
this.$router.push('/device-management'); this.$router.push('/device-management');
}, },
handleSearch(regex) {
this.isSearching = true;
this.searchRegex = regex;
this.applySearchFilter();
},
handleSearchReset() { handleSearchReset() {
this.isSearching = false; this.isSearching = false;
this.searchRegex = null; this.searchRegex = null;

View file

@ -181,7 +181,7 @@ provider management rendered as garbage). All are now machine-checked:
| Check | Command | Catches | | Check | Command | Catches |
| --- | --- | --- | | --- | --- | --- |
| Integrity | `node main/manager-web/scripts/integrity-check.js` | undefined identifiers (lost imports, dropped function params/`const request` bindings), mismatched template tags, duplicated Vue option keys (`computed:` twice clobbers the first) | | Integrity | `node main/manager-web/scripts/integrity-check.js` | undefined identifiers (lost imports, dropped function params/`const request` bindings), mismatched template tags, duplicated keys in `export default` options and inside `watch`/`computed`/`methods`/`data` (later value silently clobbers the first) |
| i18n | `cd main/manager-web && npm run check:i18n` | duplicate keys, cross-locale drift vs `en`, `$t('...')` keys with no definition in any locale | | i18n | `cd main/manager-web && npm run check:i18n` | duplicate keys, cross-locale drift vs `en`, `$t('...')` keys with no definition in any locale |
| Snapshot contract | `npm run test:snapshot` | agent snapshot API shape (`restoreAgentSnapshot` etc. were silently deleted once, while `AgentSnapshotDialog.vue` still called them) | | Snapshot contract | `npm run test:snapshot` | agent snapshot API shape (`restoreAgentSnapshot` etc. were silently deleted once, while `AgentSnapshotDialog.vue` still called them) |
| Han audit | `./scripts/westernization_audit.sh` | Chinese text/paths outside exception zones | | Han audit | `./scripts/westernization_audit.sh` | Chinese text/paths outside exception zones |
@ -219,6 +219,18 @@ of them, but knowing the shapes speeds up conflict resolution:
5. **Half-converted components** - root tag converted (`el-dialog` -> 5. **Half-converted components** - root tag converted (`el-dialog` ->
`CustomDialog`) but closing tag/footer wiring left behind `CustomDialog`) but closing tag/footer wiring left behind
(FirmwareDialog, AddWisdomBodyDialog, CustomDialog's duplicated `computed`). (FirmwareDialog, AddWisdomBodyDialog, CustomDialog's duplicated `computed`).
6. **Duplicate keys inside option maps** - the 2026-10-11 merge left two
`dialogVisible` handlers inside `watch:` in ModelEditDialog.vue (the
later, trivial one clobbered the version holding the `loadModelData()`
open-trigger, so the model edit form opened empty), plus dead duplicate
`handleSearch` in home.vue and duplicate `formatFileSize` in
KnowledgeBaseManagement.vue (the latter two survived into the pushed
tree). Top-level `computed:` duplication was caught earlier; keys nested
in `watch`/`methods` are just as silent. `integrity-check.js` now parses
the options object and its `watch`/`computed`/`methods`/`data` maps and
fails on any duplicate key. After any rebase touching a `.vue` file that
exists on both sides, diff its option maps against upstream, not just
the template.
### Translation tool ### Translation tool