#!/usr/bin/env node /** * graph-seed.js — seeds data/graph.sqlite from the on-disk agent/template/company sources. * * 依次跑 seedGlobalAgents()(T1)→ seedTemplates()(T2)→ seedCompanyAgents()(T3) * → verifyCounts()(T4,独立重新扫描目录/文件做行数交叉核对)。 * 打印按 source_kind 分组的汇总报告;核对不一致时以非零状态码退出。 * * 用法: * node scripts/graph-seed.js */ const { seedGlobalAgents, seedTemplates, seedCompanyAgents, verifyCounts } = require('../server/services/graph-seed-service'); function main() { console.log('[graph-seed] 开始种子导入\n'); const globalAgents = seedGlobalAgents(); console.log(`[global_agent] 扫描 system/agents/* 目录: ${globalAgents.scanned} 个,含 agent.md 且已导入: ${globalAgents.seeded} 个`); console.log(` ids: ${globalAgents.ids.join(', ')}`); const templates = seedTemplates(); console.log(`\n[template] 扫描 system/templates/* 目录: ${templates.scanned} 个,已导入: ${templates.seeded} 个`); console.log(` registry 匹配到 benchmark_score 的 ids (${templates.registryMatched.length}): ${templates.registryMatched.join(', ')}`); if (templates.skipped.length > 0) { console.log(` ⚠️ id 与其他 source_kind 冲突,已跳过写入 (${templates.skipped.length}): ${templates.skipped.map(s => `${s.id}(已属于 ${s.existingSourceKind})`).join(', ')}`); } const companyAgents = seedCompanyAgents(); console.log(`\n[company_hired] 扫描到 ${companyAgents.companies.length} 家有 company.json 的公司: ${companyAgents.companies.join(', ')}`); console.log(` org_graph_nodes 本次新建: ${companyAgents.nodesCreated} 行,已存在跳过: ${companyAgents.nodesExisting} 行`); console.log(` 引用已有 system_agents 行 (${companyAgents.reusedSystemAgents.length}): ${companyAgents.reusedSystemAgents.join(', ')}`); console.log(` 新建 scope='company' 行 (${companyAgents.newSystemAgents.length}): ${companyAgents.newSystemAgents.join(', ')}`); console.log('\n[verify] 行数对账(独立重新扫描目录/文件,不复用上面的计数器)'); const v = verifyCounts(); console.log(` system_agents[global_agent]: 期望 ${v.expected.global_agent},实际 ${v.actual.global_agent}`); console.log(` system_agents[template]: 期望 ${v.expected.template},实际 ${v.actual.template}`); console.log(` system_agents[company_hired]:期望 ${v.expected.company_hired},实际 ${v.actual.company_hired}`); const companyIds = Object.keys({ ...v.expected.nodesPerCompany, ...v.actual.nodesPerCompany }); for (const companyId of companyIds) { const expected = v.expected.nodesPerCompany[companyId] || 0; const actual = v.actual.nodesPerCompany[companyId] || 0; console.log(` org_graph_nodes[${companyId}]: 期望 ${expected},实际 ${actual}`); } if (!v.ok) { console.error('\n❌ 行数对账失败,发现以下不一致:'); for (const m of v.mismatches) { console.error(` - ${m.check}: 期望 ${m.expected},实际 ${m.actual}`); } process.exitCode = 1; return; } console.log('\n✅ 种子导入完成,行数对账通过'); } main();