forjajs 2.0.3 → 3.0.0
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/.claude/agents/release-auditor.md +19 -10
- package/AGENTS.md +1 -1
- package/CHANGELOG.md +80 -0
- package/DOC-MAP.md +9 -0
- package/README.md +85 -5
- package/README.pt-BR.md +34 -5
- package/dist/apps/dashboard/app/api/forja/[...path]/route.js +8 -1
- package/dist/apps/dashboard/app/api/forja/guard.d.ts +24 -0
- package/dist/apps/dashboard/app/api/forja/guard.js +63 -0
- package/dist/apps/server/src/forja-nest.module.js +9 -4
- package/dist/apps/server/src/main.js +10 -1
- package/dist/bin/forja.js +14 -2
- package/dist/lib/code-context.js +6 -0
- package/dist/lib/core/architecture-gate.d.ts +20 -0
- package/dist/lib/core/architecture-gate.js +60 -0
- package/dist/lib/core/doc-graph.js +6 -4
- package/dist/lib/core/drift-gate.d.ts +18 -0
- package/dist/lib/core/drift-gate.js +55 -0
- package/dist/lib/core/gates.d.ts +9 -2
- package/dist/lib/core/gates.js +13 -2
- package/dist/lib/core/health.js +15 -0
- package/dist/lib/core/incident-search.d.ts +19 -0
- package/dist/lib/core/incident-search.js +32 -0
- package/dist/lib/core/model-pricing.d.ts +31 -0
- package/dist/lib/core/model-pricing.js +74 -0
- package/dist/lib/core/registry.d.ts +248 -0
- package/dist/lib/core/registry.js +249 -0
- package/dist/lib/core/release.d.ts +247 -0
- package/dist/lib/core/release.js +5 -1
- package/dist/lib/core/risk-collect.d.ts +19 -0
- package/dist/lib/core/risk-collect.js +173 -0
- package/dist/lib/drift-sentinel.d.ts +59 -0
- package/dist/lib/drift-sentinel.js +126 -0
- package/dist/lib/generators/memory-generator.js +8 -6
- package/dist/lib/generators/nest-generator.js +24 -10
- package/dist/lib/project-upgrade.js +1 -1
- package/dist/lib/workspace.d.ts +7 -0
- package/dist/lib/workspace.js +34 -5
- package/dist/packages/adapter-git/src/index.d.ts +1 -1
- package/dist/packages/adapter-git/src/index.js +11 -6
- package/dist/packages/adapter-nest/src/index.d.ts +19 -0
- package/dist/packages/adapter-nest/src/index.js +26 -1
- package/dist/packages/adapter-sqlite/src/index.d.ts +27 -1
- package/dist/packages/adapter-sqlite/src/index.js +49 -0
- package/dist/packages/contracts/src/index.d.ts +33 -0
- package/dist/packages/contracts/src/index.js +13 -0
- package/dist/packages/core/src/index.d.ts +12 -0
- package/dist/packages/core/src/index.js +29 -0
- package/dist/packages/engineering/architecture/src/index.d.ts +76 -0
- package/dist/packages/engineering/architecture/src/index.js +131 -0
- package/dist/packages/engineering/evidence/src/index.d.ts +41 -0
- package/dist/packages/engineering/evidence/src/index.js +31 -0
- package/dist/packages/engineering/identity/src/index.d.ts +57 -0
- package/dist/packages/engineering/identity/src/index.js +88 -0
- package/dist/packages/engineering/monitoring/src/index.d.ts +37 -0
- package/dist/packages/engineering/monitoring/src/index.js +40 -0
- package/dist/packages/engineering/provenance/src/index.d.ts +25 -0
- package/dist/packages/engineering/provenance/src/index.js +21 -0
- package/dist/packages/engineering/risk/src/index.d.ts +89 -0
- package/dist/packages/engineering/risk/src/index.js +120 -0
- package/dist/packages/graph/src/index.d.ts +23 -1
- package/dist/packages/graph/src/index.js +88 -15
- package/dist/packages/llm/src/index.d.ts +43 -0
- package/dist/packages/llm/src/index.js +131 -0
- package/dist/packages/mcp/src/index.js +27 -8
- package/dist/packages/orchestration/src/index.d.ts +11 -1
- package/dist/packages/orchestration/src/index.js +34 -7
- package/dist/packages/policy/src/index.d.ts +40 -0
- package/dist/packages/policy/src/index.js +39 -4
- package/dist/packages/runtime/src/index.js +3 -0
- package/dist/packages/sandbox/src/index.d.ts +47 -0
- package/dist/packages/sandbox/src/index.js +59 -0
- package/dist/packages/validator/src/index.d.ts +8 -0
- package/dist/packages/validator/src/index.js +21 -6
- package/dist/scripts/adr.d.ts +17 -0
- package/dist/scripts/adr.js +152 -0
- package/dist/scripts/agent-harness.js +14 -4
- package/dist/scripts/agent.d.ts +27 -0
- package/dist/scripts/agent.js +249 -0
- package/dist/scripts/architecture.d.ts +16 -0
- package/dist/scripts/architecture.js +186 -0
- package/dist/scripts/cost-economy.d.ts +2 -0
- package/dist/scripts/cost-economy.js +79 -0
- package/dist/scripts/demo-autonomy.js +15 -8
- package/dist/scripts/demo-workspace.d.ts +6 -0
- package/dist/scripts/demo-workspace.js +101 -0
- package/dist/scripts/drift-check.d.ts +17 -0
- package/dist/scripts/drift-check.js +63 -0
- package/dist/scripts/engineer.d.ts +20 -0
- package/dist/scripts/engineer.js +208 -0
- package/dist/scripts/evidence.d.ts +12 -0
- package/dist/scripts/evidence.js +55 -0
- package/dist/scripts/gates.d.ts +6 -2
- package/dist/scripts/gates.js +14 -4
- package/dist/scripts/incident.d.ts +19 -0
- package/dist/scripts/incident.js +103 -0
- package/dist/scripts/llm-fit.js +160 -0
- package/dist/scripts/provenance.d.ts +14 -0
- package/dist/scripts/provenance.js +114 -0
- package/dist/scripts/risk.d.ts +15 -0
- package/dist/scripts/risk.js +81 -0
- package/dist/scripts/simulate.d.ts +20 -0
- package/dist/scripts/simulate.js +150 -0
- package/dist/scripts/sync-universal-memory.js +19 -6
- package/docs/architecture/FORJA-3-ENGINEERING-INTELLIGENCE-ARCHITECTURE.md +442 -0
- package/docs/llm-fit-loop.md +41 -0
- package/docs/modos-de-operacao.md +52 -0
- package/docs/produto/factsheet-marketing.md +151 -0
- package/docs/produto/roteiro-demo.md +47 -0
- package/memory/40-delivery/current-sprint.md +3 -6
- package/memory/90-decisions/0074-llm-fit-loop-cli-adapters.md +51 -0
- package/memory/90-decisions/0075-workspace-demo-isolado.md +43 -0
- package/memory/90-decisions/0076-modos-embedded-e-studio.md +43 -0
- package/memory/90-decisions/0077-cost-aware-autonomy-budget.md +95 -0
- package/memory/90-decisions/0078-forjajs-3-engineering-control-plane.md +116 -0
- package/memory/90-decisions/0079-autonomous-maintenance-readiness.md +102 -0
- package/package.json +9 -2
- package/specs/agent-identity-reputation/plan.md +133 -0
- package/specs/agent-identity-reputation/spec.md +125 -0
- package/specs/agent-identity-reputation/tasks.md +51 -0
- package/specs/agent-runtime-monitoring/plan.md +78 -0
- package/specs/agent-runtime-monitoring/spec.md +92 -0
- package/specs/agent-runtime-monitoring/tasks.md +39 -0
- package/specs/ai-code-provenance/plan.md +71 -0
- package/specs/ai-code-provenance/spec.md +97 -0
- package/specs/ai-code-provenance/tasks.md +39 -0
- package/specs/architecture-constitution/plan.md +110 -0
- package/specs/architecture-constitution/spec.md +114 -0
- package/specs/architecture-constitution/tasks.md +58 -0
- package/specs/change-risk-engine/plan.md +130 -0
- package/specs/change-risk-engine/spec.md +119 -0
- package/specs/change-risk-engine/tasks.md +72 -0
- package/specs/cost-aware-autonomy-budget/spec.md +102 -0
- package/specs/drift-sentinel/spec.md +117 -0
- package/specs/engineer-facade-extension/plan.md +71 -0
- package/specs/engineer-facade-extension/spec.md +98 -0
- package/specs/engineer-facade-extension/tasks.md +36 -0
- package/specs/engineering-evidence-ledger/plan.md +121 -0
- package/specs/engineering-evidence-ledger/spec.md +90 -0
- package/specs/engineering-evidence-ledger/tasks.md +54 -0
- package/specs/engineering-graph-extensions/plan.md +92 -0
- package/specs/engineering-graph-extensions/spec.md +101 -0
- package/specs/engineering-graph-extensions/tasks.md +60 -0
- package/specs/engineering-intelligence/spec.md +177 -0
- package/specs/learning-loop-incidents/plan.md +59 -0
- package/specs/learning-loop-incidents/spec.md +93 -0
- package/specs/learning-loop-incidents/tasks.md +36 -0
- package/specs/predictive-change-simulation/plan.md +109 -0
- package/specs/predictive-change-simulation/spec.md +107 -0
- package/specs/predictive-change-simulation/tasks.md +41 -0
- package/specs/release-auditor-health/plan.md +88 -0
- package/specs/release-auditor-health/spec.md +82 -0
- package/specs/release-auditor-health/tasks.md +72 -0
- package/specs/smart-agent-routing/plan.md +55 -0
- package/specs/smart-agent-routing/spec.md +85 -0
- package/specs/smart-agent-routing/tasks.md +26 -0
- package/dist/scripts/server-persistence-probe.d.ts +0 -1
- package/dist/scripts/server-persistence-probe.js +0 -52
- package/dist/scripts/server-persistence-probe.mjs +0 -52
- /package/dist/scripts/{server-persistence-probe.d.mts → llm-fit.d.ts} +0 -0
|
@@ -5,6 +5,7 @@ import { spawnSync } from 'node:child_process';
|
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
6
|
import { initWorkspace, getWorkspaceInfo, listProjects, resolveProject, getWorkspaceProjectsMemoryDir, } from "../lib/workspace.js";
|
|
7
7
|
import { resolveScript } from "../lib/core/registry.js";
|
|
8
|
+
import { isPathWithinRoot } from "../packages/contracts/src/index.js";
|
|
8
9
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
9
10
|
// `root` = recursos do FRAMEWORK (ex.: design:* lê design-md/) — resolvidos por __dirname.
|
|
10
11
|
// `projectRoot` = o PROJETO do usuário (gsd:* opera em specs/ e .context/ dele) — onde foi invocado.
|
|
@@ -77,7 +78,7 @@ function readText(file) {
|
|
|
77
78
|
// O brief do `design:check` é do PROJETO do usuário (cwd), não do pacote — a âncora e o guardrail
|
|
78
79
|
// de travessia são o projectRoot. Cravar `root` rejeitava o brief do consumidor ("fora do projeto").
|
|
79
80
|
const full = path.resolve(projectRoot, file);
|
|
80
|
-
if (!
|
|
81
|
+
if (!isPathWithinRoot(projectRoot, full))
|
|
81
82
|
fail(`Caminho fora do projeto: ${file}`);
|
|
82
83
|
if (!fs.existsSync(full))
|
|
83
84
|
fail(`Arquivo nao encontrado: ${file}`);
|
|
@@ -178,6 +179,13 @@ function cmdCodeCheck() {
|
|
|
178
179
|
function cmdCodeImpact([symbol, depthArg]) {
|
|
179
180
|
if (!symbol)
|
|
180
181
|
fail('Uso: agent-harness code:impact <simbolo> [profundidade]');
|
|
182
|
+
// `symbol` vira um elemento de argv passado direto para o binário `codegraph` — não há shell
|
|
183
|
+
// envolvido no caminho normal, então não é injeção de comando, mas um valor começando com `-`
|
|
184
|
+
// ainda pode ser lido como flag pelo `codegraph` em vez de símbolo posicional (injeção de
|
|
185
|
+
// argumento). O comando roda a partir de texto que pode ter vindo de um agente de IA, então
|
|
186
|
+
// vale recusar cedo em vez de confiar no parser do binário externo.
|
|
187
|
+
if (symbol.startsWith('-'))
|
|
188
|
+
fail(`Simbolo invalido (nao pode comecar com '-'): ${symbol}`);
|
|
181
189
|
const res0 = runCodegraph(['status', '--json']);
|
|
182
190
|
if (res0.missing) {
|
|
183
191
|
console.log('Codegraph nao instalado. Sem mapa de impacto automatico.');
|
|
@@ -247,7 +255,8 @@ function cmdHermesHandoff([jsonArg]) {
|
|
|
247
255
|
const missing = required.filter((field) => !payload[field] || typeof payload[field] !== 'string');
|
|
248
256
|
if (missing.length)
|
|
249
257
|
fail(`Campos ADR-0005 ausentes: ${missing.join(', ')}`);
|
|
250
|
-
const
|
|
258
|
+
const router = resolveScript(root, 'scripts/agent-router.mjs');
|
|
259
|
+
const result = spawnSync(process.execPath, [router, 'append', JSON.stringify(payload)], {
|
|
251
260
|
cwd: root,
|
|
252
261
|
encoding: 'utf8',
|
|
253
262
|
});
|
|
@@ -258,7 +267,8 @@ function cmdHermesHandoff([jsonArg]) {
|
|
|
258
267
|
process.exit(result.status ?? 1);
|
|
259
268
|
}
|
|
260
269
|
function appendHandoff(payload) {
|
|
261
|
-
const
|
|
270
|
+
const router = resolveScript(root, 'scripts/agent-router.mjs');
|
|
271
|
+
const result = spawnSync(process.execPath, [router, 'append', JSON.stringify(payload)], {
|
|
262
272
|
cwd: root,
|
|
263
273
|
encoding: 'utf8',
|
|
264
274
|
});
|
|
@@ -390,7 +400,7 @@ function cmdGsdCheck([slug, briefPath]) {
|
|
|
390
400
|
const fullBrief = path.resolve(root, briefPath);
|
|
391
401
|
let ok = false;
|
|
392
402
|
let detail = briefPath;
|
|
393
|
-
if (
|
|
403
|
+
if (isPathWithinRoot(root, fullBrief) && fs.existsSync(fullBrief)) {
|
|
394
404
|
const content = fs.readFileSync(fullBrief, 'utf8');
|
|
395
405
|
const missing = DESIGN_REQUIRED.filter((needle) => !content.includes(needle));
|
|
396
406
|
const placeholders = (content.match(/<[^>\n]+>/g) || []).length;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* agent:register / :list / :show / :score / :history / :recommend / :monitor
|
|
4
|
+
* (SPEC-036, SPEC-037, SPEC-040)
|
|
5
|
+
*
|
|
6
|
+
* forja agent:register <id> --role <role> [--provider <p>] [--model <m>]
|
|
7
|
+
* [--capabilities <c1,c2>] [--domains <d1,d2>]
|
|
8
|
+
* [--max-files <n>] [--max-cost-usd <n>]
|
|
9
|
+
* forja agent:list todos os agentes registrados, com trustLevel se já pontuado
|
|
10
|
+
* forja agent:show <id> detalhe de um agente
|
|
11
|
+
* forja agent:score <id> [--domain <d>] computa e persiste trustLevel/autonomyLevel via Observation reais
|
|
12
|
+
* forja agent:history <id> Observations do agente, mais recentes primeiro
|
|
13
|
+
* forja agent:recommend --role <role> [--domain <d>] ranking de agentes registrados por
|
|
14
|
+
* adequação — informação, não atribuição (SPEC-037 AC-4)
|
|
15
|
+
* forja agent:monitor <id> [--window-hours <n>] compara comportamento recente (últimas <n>h,
|
|
16
|
+
* default 24) contra a linha de base histórica —
|
|
17
|
+
* informação, nunca bloqueia nada sozinho (SPEC-040 AC-5)
|
|
18
|
+
*
|
|
19
|
+
* `packages/engineering/identity` faz o cálculo puro (computeReputationScore, recommendAgent);
|
|
20
|
+
* `packages/engineering/monitoring` faz a comparação pura (detectAnomaly). Este script busca
|
|
21
|
+
* `Observation`s reais (`SqliteObservationStore`, já existente), monta o `EvaluationReport` via
|
|
22
|
+
* `EvaluationEngine` (`packages/evals`, já existente — nenhuma métrica reimplementada aqui) e
|
|
23
|
+
* persiste `AgentProfile2` (`SqliteAgentProfileStore`, sem migration nova — D3 do plan de
|
|
24
|
+
* SPEC-036). `agent:register` deliberadamente não tem flag de trust-level/autonomy-level — só
|
|
25
|
+
* `agent:score` escreve esses campos (D2 do plan, AC-1/AC-3 de SPEC-036).
|
|
26
|
+
*/
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* agent:register / :list / :show / :score / :history / :recommend / :monitor
|
|
4
|
+
* (SPEC-036, SPEC-037, SPEC-040)
|
|
5
|
+
*
|
|
6
|
+
* forja agent:register <id> --role <role> [--provider <p>] [--model <m>]
|
|
7
|
+
* [--capabilities <c1,c2>] [--domains <d1,d2>]
|
|
8
|
+
* [--max-files <n>] [--max-cost-usd <n>]
|
|
9
|
+
* forja agent:list todos os agentes registrados, com trustLevel se já pontuado
|
|
10
|
+
* forja agent:show <id> detalhe de um agente
|
|
11
|
+
* forja agent:score <id> [--domain <d>] computa e persiste trustLevel/autonomyLevel via Observation reais
|
|
12
|
+
* forja agent:history <id> Observations do agente, mais recentes primeiro
|
|
13
|
+
* forja agent:recommend --role <role> [--domain <d>] ranking de agentes registrados por
|
|
14
|
+
* adequação — informação, não atribuição (SPEC-037 AC-4)
|
|
15
|
+
* forja agent:monitor <id> [--window-hours <n>] compara comportamento recente (últimas <n>h,
|
|
16
|
+
* default 24) contra a linha de base histórica —
|
|
17
|
+
* informação, nunca bloqueia nada sozinho (SPEC-040 AC-5)
|
|
18
|
+
*
|
|
19
|
+
* `packages/engineering/identity` faz o cálculo puro (computeReputationScore, recommendAgent);
|
|
20
|
+
* `packages/engineering/monitoring` faz a comparação pura (detectAnomaly). Este script busca
|
|
21
|
+
* `Observation`s reais (`SqliteObservationStore`, já existente), monta o `EvaluationReport` via
|
|
22
|
+
* `EvaluationEngine` (`packages/evals`, já existente — nenhuma métrica reimplementada aqui) e
|
|
23
|
+
* persiste `AgentProfile2` (`SqliteAgentProfileStore`, sem migration nova — D3 do plan de
|
|
24
|
+
* SPEC-036). `agent:register` deliberadamente não tem flag de trust-level/autonomy-level — só
|
|
25
|
+
* `agent:score` escreve esses campos (D2 do plan, AC-1/AC-3 de SPEC-036).
|
|
26
|
+
*/
|
|
27
|
+
import fs from 'node:fs';
|
|
28
|
+
import Database from 'better-sqlite3';
|
|
29
|
+
import { computeReputationScore, recommendAgent } from "../packages/engineering/identity/src/index.js";
|
|
30
|
+
import { detectAnomaly } from "../packages/engineering/monitoring/src/index.js";
|
|
31
|
+
import { EvaluationEngine } from "../packages/evals/src/index.js";
|
|
32
|
+
import { SqliteAgentProfileStore, SqliteMigrationRunner, SqliteObservationStore } from "../packages/adapter-sqlite/src/index.js";
|
|
33
|
+
import { getWorkspaceDbDir, getWorkspaceDbPath } from "../lib/workspace.js";
|
|
34
|
+
function flag(args, name) {
|
|
35
|
+
const index = args.indexOf(name);
|
|
36
|
+
return index === -1 ? undefined : args[index + 1];
|
|
37
|
+
}
|
|
38
|
+
function list(value) {
|
|
39
|
+
return value === undefined ? [] : value.split(',').map((item) => item.trim()).filter((item) => item.length > 0);
|
|
40
|
+
}
|
|
41
|
+
function openDatabase() {
|
|
42
|
+
fs.mkdirSync(getWorkspaceDbDir(), { recursive: true });
|
|
43
|
+
const database = new Database(process.env.FORJA_RUNTIME_DB ?? getWorkspaceDbPath());
|
|
44
|
+
new SqliteMigrationRunner(database).apply();
|
|
45
|
+
return database;
|
|
46
|
+
}
|
|
47
|
+
function cmdRegister(args) {
|
|
48
|
+
const [id] = args;
|
|
49
|
+
const role = flag(args, '--role');
|
|
50
|
+
if (!id || !role) {
|
|
51
|
+
console.error('Uso: forja agent:register <id> --role <role> [--provider <p>] [--model <m>] [--capabilities <c1,c2>] [--domains <d1,d2>] [--max-files <n>] [--max-cost-usd <n>]');
|
|
52
|
+
process.exitCode = 1;
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const database = openDatabase();
|
|
56
|
+
try {
|
|
57
|
+
const store = new SqliteAgentProfileStore(database);
|
|
58
|
+
const existing = store.get(id);
|
|
59
|
+
const now = new Date().toISOString();
|
|
60
|
+
const maxFiles = flag(args, '--max-files');
|
|
61
|
+
const maxCostUsd = flag(args, '--max-cost-usd');
|
|
62
|
+
const limits = maxFiles === undefined && maxCostUsd === undefined ? undefined : {
|
|
63
|
+
...(maxFiles === undefined ? {} : { maxFiles: Number(maxFiles) }),
|
|
64
|
+
...(maxCostUsd === undefined ? {} : { maxCostUsd: Number(maxCostUsd) }),
|
|
65
|
+
};
|
|
66
|
+
const profile = {
|
|
67
|
+
schemaVersion: '2.0', createdAt: existing?.createdAt ?? now, updatedAt: now, correlationId: `agent:${id}`,
|
|
68
|
+
id: id, role,
|
|
69
|
+
provider: flag(args, '--provider'), model: flag(args, '--model'),
|
|
70
|
+
capabilities: list(flag(args, '--capabilities')), architectureDomains: list(flag(args, '--domains')),
|
|
71
|
+
limits,
|
|
72
|
+
// trustLevel/autonomyLevel/lastScoredAt: nunca setados aqui (D2) — preserva o que já existia
|
|
73
|
+
// (registrar de novo não deve apagar a última pontuação).
|
|
74
|
+
trustLevel: existing?.trustLevel, autonomyLevel: existing?.autonomyLevel, lastScoredAt: existing?.lastScoredAt,
|
|
75
|
+
};
|
|
76
|
+
store.save(profile);
|
|
77
|
+
console.log(`${existing === undefined ? 'Registrado' : 'Atualizado'}: ${id} (role: ${role})`);
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
database.close();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function cmdList() {
|
|
84
|
+
const database = openDatabase();
|
|
85
|
+
try {
|
|
86
|
+
const profiles = new SqliteAgentProfileStore(database).list();
|
|
87
|
+
if (profiles.length === 0) {
|
|
88
|
+
console.log('Nenhum agente registrado — rode `forja agent:register <id> --role <role>` primeiro.');
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
for (const profile of profiles) {
|
|
92
|
+
const trust = profile.trustLevel === undefined ? '(sem pontuação — rode agent:score)' : `trustLevel ${profile.trustLevel}/5 (${profile.autonomyLevel})`;
|
|
93
|
+
console.log(`${profile.id} role:${profile.role} ${trust}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
database.close();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function cmdShow([id]) {
|
|
101
|
+
if (!id) {
|
|
102
|
+
console.error('Uso: forja agent:show <id>');
|
|
103
|
+
process.exitCode = 1;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const database = openDatabase();
|
|
107
|
+
try {
|
|
108
|
+
const profile = new SqliteAgentProfileStore(database).get(id);
|
|
109
|
+
if (profile === undefined) {
|
|
110
|
+
console.error(`Agente não encontrado: ${id}. Rode forja agent:register primeiro.`);
|
|
111
|
+
process.exitCode = 1;
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
console.log(JSON.stringify(profile, null, 2));
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
database.close();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async function cmdScore(args) {
|
|
121
|
+
const [id] = args;
|
|
122
|
+
const domain = flag(args, '--domain');
|
|
123
|
+
if (!id) {
|
|
124
|
+
console.error('Uso: forja agent:score <id> [--domain <d>]');
|
|
125
|
+
process.exitCode = 1;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const database = openDatabase();
|
|
129
|
+
try {
|
|
130
|
+
const profileStore = new SqliteAgentProfileStore(database);
|
|
131
|
+
const profile = profileStore.get(id);
|
|
132
|
+
if (profile === undefined) {
|
|
133
|
+
console.error(`Agente não encontrado: ${id}. Rode forja agent:register primeiro.`);
|
|
134
|
+
process.exitCode = 1;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const observations = new SqliteObservationStore(database).list().filter((observation) => observation.agentId === id && (domain === undefined || observation.capabilityId === domain));
|
|
138
|
+
const engine = new EvaluationEngine({ list: () => observations });
|
|
139
|
+
const report = await engine.evaluate({ scope: 'agent', scopeId: id, observations });
|
|
140
|
+
const score = computeReputationScore(report, { agentId: id, domain });
|
|
141
|
+
const now = new Date().toISOString();
|
|
142
|
+
profileStore.save({ ...profile, trustLevel: score.trustLevel, autonomyLevel: score.autonomyLevel, lastScoredAt: now, updatedAt: now });
|
|
143
|
+
console.log(`${id}${domain === undefined ? '' : ` (domain: ${domain})`} — trustLevel ${score.trustLevel}/5 → ${score.autonomyLevel}`);
|
|
144
|
+
console.log(`confidence ${(score.confidence * 100).toFixed(0)}% (amostra: ${score.sampleSize} observation(s))`);
|
|
145
|
+
for (const [name, value] of Object.entries(score.metrics))
|
|
146
|
+
console.log(` ${name}: ${value}`);
|
|
147
|
+
if (score.sampleSize === 0)
|
|
148
|
+
console.log('\nSem Observation ainda para este agente — score é o piso conservador (cold start, AC-3).');
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
database.close();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function cmdHistory([id]) {
|
|
155
|
+
if (!id) {
|
|
156
|
+
console.error('Uso: forja agent:history <id>');
|
|
157
|
+
process.exitCode = 1;
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const database = openDatabase();
|
|
161
|
+
try {
|
|
162
|
+
const observations = new SqliteObservationStore(database).list().filter((observation) => observation.agentId === id).sort((left, right) => right.createdAt.localeCompare(left.createdAt));
|
|
163
|
+
if (observations.length === 0) {
|
|
164
|
+
console.log(`Nenhuma Observation para ${id} ainda.`);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
for (const observation of observations)
|
|
168
|
+
console.log(`${observation.createdAt} ${observation.outcome.padEnd(12)} ${observation.capabilityId ?? '(sem capability)'} ${observation.id}`);
|
|
169
|
+
}
|
|
170
|
+
finally {
|
|
171
|
+
database.close();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function cmdRecommend(args) {
|
|
175
|
+
const role = flag(args, '--role');
|
|
176
|
+
const domain = flag(args, '--domain');
|
|
177
|
+
if (!role) {
|
|
178
|
+
console.error('Uso: forja agent:recommend --role <role> [--domain <d>]');
|
|
179
|
+
process.exitCode = 1;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const database = openDatabase();
|
|
183
|
+
try {
|
|
184
|
+
const profiles = new SqliteAgentProfileStore(database).list();
|
|
185
|
+
if (profiles.length === 0) {
|
|
186
|
+
console.log('Nenhum agente registrado — rode `forja agent:register <id> --role <role>` primeiro.');
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const ranking = recommendAgent(profiles, { role, domain });
|
|
190
|
+
for (const item of ranking)
|
|
191
|
+
console.log(`${item.agentId} score:${item.score} ${item.reasons.join(', ')}`);
|
|
192
|
+
console.log('\n(informação, não atribuição — a escolha final continua manual/do orquestrador, SPEC-037 AC-4)');
|
|
193
|
+
}
|
|
194
|
+
finally {
|
|
195
|
+
database.close();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async function cmdMonitor(args) {
|
|
199
|
+
const [id] = args;
|
|
200
|
+
const windowHours = Number(flag(args, '--window-hours') ?? '24');
|
|
201
|
+
if (!id) {
|
|
202
|
+
console.error('Uso: forja agent:monitor <id> [--window-hours <n>]');
|
|
203
|
+
process.exitCode = 1;
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (!Number.isFinite(windowHours) || windowHours <= 0) {
|
|
207
|
+
console.error('--window-hours deve ser um número positivo');
|
|
208
|
+
process.exitCode = 1;
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const database = openDatabase();
|
|
212
|
+
try {
|
|
213
|
+
const cutoff = new Date(Date.now() - windowHours * 60 * 60 * 1000).toISOString();
|
|
214
|
+
const allObservations = new SqliteObservationStore(database).list().filter((observation) => observation.agentId === id);
|
|
215
|
+
const baselineObservations = allObservations.filter((observation) => observation.createdAt < cutoff);
|
|
216
|
+
const recentObservations = allObservations.filter((observation) => observation.createdAt >= cutoff);
|
|
217
|
+
const engine = new EvaluationEngine({ list: () => [] });
|
|
218
|
+
const baseline = await engine.evaluate({ scope: 'agent', scopeId: id, observations: baselineObservations });
|
|
219
|
+
const recent = await engine.evaluate({ scope: 'agent', scopeId: id, observations: recentObservations });
|
|
220
|
+
const assessment = detectAnomaly(id, baseline, recent);
|
|
221
|
+
console.log(`${id} — janela de ${windowHours}h: score de anomalia ${assessment.score}/100 (confidence ${(assessment.confidence * 100).toFixed(0)}%)`);
|
|
222
|
+
console.log(`linha de base: ${baselineObservations.length} observation(s) | recente: ${recentObservations.length} observation(s)`);
|
|
223
|
+
for (const signal of assessment.signals)
|
|
224
|
+
console.log(` ${signal.metric}: ${signal.baseline.toFixed(2)} → ${signal.recent.toFixed(2)} (Δ${signal.delta >= 0 ? '+' : ''}${signal.delta.toFixed(2)})`);
|
|
225
|
+
console.log('\n(informação, não decisão automática — nenhum bloqueio ou aprovação foi disparado, SPEC-040 AC-5)');
|
|
226
|
+
}
|
|
227
|
+
finally {
|
|
228
|
+
database.close();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
async function main() {
|
|
232
|
+
const [subcommand, ...rest] = process.argv.slice(2);
|
|
233
|
+
switch (subcommand) {
|
|
234
|
+
case 'register': return cmdRegister(rest);
|
|
235
|
+
case 'list': return cmdList();
|
|
236
|
+
case 'show': return cmdShow(rest);
|
|
237
|
+
case 'score': return cmdScore(rest);
|
|
238
|
+
case 'history': return cmdHistory(rest);
|
|
239
|
+
case 'recommend': return cmdRecommend(rest);
|
|
240
|
+
case 'monitor': return cmdMonitor(rest);
|
|
241
|
+
default:
|
|
242
|
+
console.error('Uso: forja agent:<register|list|show|score|history|recommend|monitor> [args]');
|
|
243
|
+
process.exitCode = 1;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
main().catch((error) => {
|
|
247
|
+
console.error(`\nagent: falhou: ${error instanceof Error ? error.message : String(error)}`);
|
|
248
|
+
process.exitCode = 1;
|
|
249
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* architecture:compile / :check / :status / :explain / :approve (SPEC-033)
|
|
4
|
+
*
|
|
5
|
+
* forja architecture:compile ADRs (## Constraints) → .context/architecture/constitution.json
|
|
6
|
+
* forja architecture:check constitution.json vs. Engineering Graph real
|
|
7
|
+
* forja architecture:status resumo: N regras ativas, N propostas, última compilação
|
|
8
|
+
* forja architecture:explain <id> ADR de origem, texto original, severidade
|
|
9
|
+
* forja architecture:approve <id> promove uma regra proposed a active (via ApprovalLedger)
|
|
10
|
+
*
|
|
11
|
+
* `packages/engineering/architecture` faz o parsing/checagem pura; este script é o adapter que lê
|
|
12
|
+
* `memory/90-decisions/*.md`, escreve `.context/architecture/constitution.json` (versionado em
|
|
13
|
+
* git — revisável em PR, não uma linha de SQLite) e resolve as arestas DEPENDS_ON reais via o
|
|
14
|
+
* mesmo GraphIndexer.sync que `adr:*`/`drift:check` já usam.
|
|
15
|
+
*/
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* architecture:compile / :check / :status / :explain / :approve (SPEC-033)
|
|
4
|
+
*
|
|
5
|
+
* forja architecture:compile ADRs (## Constraints) → .context/architecture/constitution.json
|
|
6
|
+
* forja architecture:check constitution.json vs. Engineering Graph real
|
|
7
|
+
* forja architecture:status resumo: N regras ativas, N propostas, última compilação
|
|
8
|
+
* forja architecture:explain <id> ADR de origem, texto original, severidade
|
|
9
|
+
* forja architecture:approve <id> promove uma regra proposed a active (via ApprovalLedger)
|
|
10
|
+
*
|
|
11
|
+
* `packages/engineering/architecture` faz o parsing/checagem pura; este script é o adapter que lê
|
|
12
|
+
* `memory/90-decisions/*.md`, escreve `.context/architecture/constitution.json` (versionado em
|
|
13
|
+
* git — revisável em PR, não uma linha de SQLite) e resolve as arestas DEPENDS_ON reais via o
|
|
14
|
+
* mesmo GraphIndexer.sync que `adr:*`/`drift:check` já usam.
|
|
15
|
+
*/
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
import Database from 'better-sqlite3';
|
|
19
|
+
import { checkConstitution, compileConstitution, explainRule } from "../packages/engineering/architecture/src/index.js";
|
|
20
|
+
import { ApprovalLedger } from "../packages/policy/src/index.js";
|
|
21
|
+
import { GitGraphDocumentSource, SpawnCommandRunner } from "../packages/adapter-git/src/index.js";
|
|
22
|
+
import { GraphIndexer, GraphLoop } from "../packages/graph/src/index.js";
|
|
23
|
+
import { SqliteApprovalStore, SqliteGraphStore, SqliteMigrationRunner } from "../packages/adapter-sqlite/src/index.js";
|
|
24
|
+
import { getWorkspaceDbDir, getWorkspaceDbPath } from "../lib/workspace.js";
|
|
25
|
+
const CONSTITUTION_PATH = '.context/architecture/constitution.json';
|
|
26
|
+
function graphRoot() {
|
|
27
|
+
return process.env.FORJA_GRAPH_ROOT ?? process.cwd();
|
|
28
|
+
}
|
|
29
|
+
function readAdrDocuments() {
|
|
30
|
+
const dir = path.join(graphRoot(), 'memory', '90-decisions');
|
|
31
|
+
if (!fs.existsSync(dir))
|
|
32
|
+
return [];
|
|
33
|
+
return fs.readdirSync(dir)
|
|
34
|
+
.filter((name) => name.endsWith('.md') && name !== '_template.md')
|
|
35
|
+
.map((name) => ({ source: path.join('memory', '90-decisions', name), content: fs.readFileSync(path.join(dir, name), 'utf8') }));
|
|
36
|
+
}
|
|
37
|
+
function loadConstitution() {
|
|
38
|
+
const file = path.join(graphRoot(), CONSTITUTION_PATH);
|
|
39
|
+
if (!fs.existsSync(file))
|
|
40
|
+
return { rules: [] };
|
|
41
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
42
|
+
}
|
|
43
|
+
function saveConstitution(rules) {
|
|
44
|
+
const file = path.join(graphRoot(), CONSTITUTION_PATH);
|
|
45
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
46
|
+
fs.writeFileSync(file, `${JSON.stringify({ compiledAt: new Date().toISOString(), rules }, null, 2)}\n`);
|
|
47
|
+
}
|
|
48
|
+
function cmdCompile() {
|
|
49
|
+
const adrs = readAdrDocuments();
|
|
50
|
+
const rules = compileConstitution(adrs);
|
|
51
|
+
saveConstitution(rules);
|
|
52
|
+
const active = rules.filter((rule) => rule.status === 'active').length;
|
|
53
|
+
console.log(`Compilado: ${rules.length} regra(s) de ${adrs.length} ADR(s) — ${active} active, ${rules.length - active} proposed.`);
|
|
54
|
+
console.log(`Gravado em ${CONSTITUTION_PATH}`);
|
|
55
|
+
if (rules.length - active > 0)
|
|
56
|
+
console.log('Regras proposed precisam de `architecture:approve <rule-id>` para valer — ver `architecture:status`.');
|
|
57
|
+
}
|
|
58
|
+
async function dependencyEdges() {
|
|
59
|
+
fs.mkdirSync(getWorkspaceDbDir(), { recursive: true });
|
|
60
|
+
const database = new Database(process.env.FORJA_RUNTIME_DB ?? getWorkspaceDbPath());
|
|
61
|
+
new SqliteMigrationRunner(database).apply();
|
|
62
|
+
const store = new SqliteGraphStore(database);
|
|
63
|
+
const graph = new GraphLoop(store);
|
|
64
|
+
const source = new GitGraphDocumentSource(graphRoot(), new SpawnCommandRunner());
|
|
65
|
+
await new GraphIndexer(graph).sync(source);
|
|
66
|
+
try {
|
|
67
|
+
const edges = [];
|
|
68
|
+
for (const edge of store.listEdges()) {
|
|
69
|
+
if (edge.type !== 'DEPENDS_ON')
|
|
70
|
+
continue;
|
|
71
|
+
const fromNode = store.getNode(edge.from);
|
|
72
|
+
const toNode = store.getNode(edge.to);
|
|
73
|
+
if (fromNode === undefined || toNode === undefined)
|
|
74
|
+
continue;
|
|
75
|
+
edges.push({ fromPath: fromNode.label, targetLabel: toNode.label });
|
|
76
|
+
}
|
|
77
|
+
return edges;
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
database.close();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async function cmdCheck() {
|
|
84
|
+
const { rules } = loadConstitution();
|
|
85
|
+
if (rules.length === 0) {
|
|
86
|
+
console.log('Nenhuma regra compilada — rode `architecture:compile` primeiro.');
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const edges = await dependencyEdges();
|
|
90
|
+
const report = checkConstitution(rules, edges);
|
|
91
|
+
console.log(`\nARCHITECTURE DRIFT REPORT\n`);
|
|
92
|
+
console.log(`✓ ${report.compliant} regra(s) active em conformidade\n`);
|
|
93
|
+
if (report.violations.length === 0) {
|
|
94
|
+
console.log('Nenhuma violação.');
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
console.log('VIOLATIONS\n');
|
|
98
|
+
for (const violation of report.violations) {
|
|
99
|
+
console.log(`${violation.severity.toUpperCase()}`);
|
|
100
|
+
console.log(`${violation.file}`);
|
|
101
|
+
console.log(`viola: ${violation.ruleId} (${violation.source})`);
|
|
102
|
+
console.log(`alvo: ${violation.target}`);
|
|
103
|
+
console.log(`remediação: ${violation.remediation}\n`);
|
|
104
|
+
}
|
|
105
|
+
process.exitCode = 1;
|
|
106
|
+
}
|
|
107
|
+
function cmdStatus() {
|
|
108
|
+
const { compiledAt, rules } = loadConstitution();
|
|
109
|
+
const active = rules.filter((rule) => rule.status === 'active');
|
|
110
|
+
const proposed = rules.filter((rule) => rule.status === 'proposed');
|
|
111
|
+
console.log(`Última compilação: ${compiledAt ?? '(nunca — rode architecture:compile)'}`);
|
|
112
|
+
console.log(`${active.length} regra(s) active, ${proposed.length} proposed.\n`);
|
|
113
|
+
for (const rule of active)
|
|
114
|
+
console.log(` active ${rule.id} (${rule.constraint.kind}: ${rule.constraint.target})`);
|
|
115
|
+
for (const rule of proposed)
|
|
116
|
+
console.log(` proposed ${rule.id} (confidence ${rule.confidence}) — precisa de architecture:approve`);
|
|
117
|
+
}
|
|
118
|
+
function cmdExplain([ruleId]) {
|
|
119
|
+
if (!ruleId) {
|
|
120
|
+
console.error('Uso: forja architecture:explain <rule-id>');
|
|
121
|
+
process.exitCode = 1;
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const { rules } = loadConstitution();
|
|
125
|
+
const rule = explainRule(rules, ruleId);
|
|
126
|
+
if (rule === undefined) {
|
|
127
|
+
console.error(`Regra não encontrada: ${ruleId}. Rode architecture:status para ver os ids.`);
|
|
128
|
+
process.exitCode = 1;
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
console.log(`${rule.id} — ${rule.status} (severidade: ${rule.severity}, confidence: ${rule.confidence})`);
|
|
132
|
+
console.log(`origem: ${rule.source}`);
|
|
133
|
+
console.log(`escopo: ${rule.scope.paths.join(', ')}`);
|
|
134
|
+
console.log(`regra: ${rule.constraint.kind} → ${rule.constraint.target}`);
|
|
135
|
+
console.log(`texto original: "${rule.rationale}"`);
|
|
136
|
+
}
|
|
137
|
+
async function cmdApprove([ruleId]) {
|
|
138
|
+
if (!ruleId) {
|
|
139
|
+
console.error('Uso: forja architecture:approve <rule-id>');
|
|
140
|
+
process.exitCode = 1;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const { rules } = loadConstitution();
|
|
144
|
+
const rule = explainRule(rules, ruleId);
|
|
145
|
+
if (rule === undefined) {
|
|
146
|
+
console.error(`Regra não encontrada: ${ruleId}.`);
|
|
147
|
+
process.exitCode = 1;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (rule.status === 'active') {
|
|
151
|
+
console.log(`${ruleId} já está active.`);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
fs.mkdirSync(getWorkspaceDbDir(), { recursive: true });
|
|
155
|
+
const database = new Database(process.env.FORJA_RUNTIME_DB ?? getWorkspaceDbPath());
|
|
156
|
+
new SqliteMigrationRunner(database).apply();
|
|
157
|
+
const ledger = new ApprovalLedger(new SqliteApprovalStore(database));
|
|
158
|
+
const now = new Date().toISOString();
|
|
159
|
+
try {
|
|
160
|
+
const request = ledger.create({ action: 'architecture.rule.approve', justification: `Aprovação manual de regra de Constitution com confidence ${rule.confidence}: "${rule.rationale}"`, impact: `Regra ${ruleId} (${rule.constraint.kind} → ${rule.constraint.target}) passa a bloquear architecture:check`, expiresAt: '2099-01-01T00:00:00.000Z', correlationId: `architecture:${ruleId}` }, now);
|
|
161
|
+
ledger.decide(request.id, { decision: 'approved', approverId: (process.env.USER ?? 'operator'), decidedAt: now });
|
|
162
|
+
}
|
|
163
|
+
finally {
|
|
164
|
+
database.close();
|
|
165
|
+
}
|
|
166
|
+
const updated = rules.map((item) => (item.id === ruleId ? { ...item, status: 'active' } : item));
|
|
167
|
+
saveConstitution(updated);
|
|
168
|
+
console.log(`${ruleId} aprovado e promovido a active. Registrado no ApprovalLedger (correlationId: architecture:${ruleId}).`);
|
|
169
|
+
}
|
|
170
|
+
async function main() {
|
|
171
|
+
const [subcommand, ...rest] = process.argv.slice(2);
|
|
172
|
+
switch (subcommand) {
|
|
173
|
+
case 'compile': return cmdCompile();
|
|
174
|
+
case 'check': return cmdCheck();
|
|
175
|
+
case 'status': return cmdStatus();
|
|
176
|
+
case 'explain': return cmdExplain(rest);
|
|
177
|
+
case 'approve': return cmdApprove(rest);
|
|
178
|
+
default:
|
|
179
|
+
console.error('Uso: forja architecture:<compile|check|status|explain|approve> [args]');
|
|
180
|
+
process.exitCode = 1;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
main().catch((error) => {
|
|
184
|
+
console.error(`\narchitecture: falhou: ${error instanceof Error ? error.message : String(error)}`);
|
|
185
|
+
process.exitCode = 1;
|
|
186
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* cost-economy — SPEC-029 AC-5: `token:economy` mede tokens; isto mede o que realmente importa pra
|
|
4
|
+
* quem paga a conta, dólar. Novo comando (em vez de estender `token:economy` in-place) porque os
|
|
5
|
+
* dois eixos medem coisas diferentes: `token:economy` compara CENÁRIOS sintéticos de fixture
|
|
6
|
+
* (arquitetura clean vs flat, memória quente vs fria — nunca rodou um LLM de verdade). `cost:economy`
|
|
7
|
+
* agrega OBSERVAÇÕES REAIS já gravadas por `forja llm:run` (mesmo `ObservationStore` de `llm:eval`),
|
|
8
|
+
* multiplicadas pela tabela de preço local (`lib/core/model-pricing.json`, AC-3). Reaproveitar o
|
|
9
|
+
* comando de fixture faria "economia de token" parecer "economia de custo" sobre dados que nunca
|
|
10
|
+
* geraram custo real — daí o comando irmão em vez de uma seção dentro de `token:economy`.
|
|
11
|
+
*
|
|
12
|
+
* Escopo por projeto/domínio: cada projeto tem seu próprio workspace/DB (FORJA_WORKSPACE), então
|
|
13
|
+
* rodar `forja cost:economy` dentro do workspace de um projeto já reporta o custo DAQUELE projeto —
|
|
14
|
+
* sem precisar de um campo `projectId` novo em `Observation` (fora do escopo deste spec).
|
|
15
|
+
*
|
|
16
|
+
* Estimativa, não fatura: nomeado explicitamente nos dois formatos de saída (mitigação de risco do
|
|
17
|
+
* SPEC-029 — "confundir custo estimado com custo real cobrado").
|
|
18
|
+
*/
|
|
19
|
+
import fs from 'node:fs';
|
|
20
|
+
import Database from 'better-sqlite3';
|
|
21
|
+
import { SqliteMigrationRunner, SqliteObservationStore } from "../packages/adapter-sqlite/src/index.js";
|
|
22
|
+
import { getWorkspaceDbDir, getWorkspaceDbPath, getWorkspaceInfo } from "../lib/workspace.js";
|
|
23
|
+
import { loadPricingTable, lookupPrice } from "../lib/core/model-pricing.js";
|
|
24
|
+
function main() {
|
|
25
|
+
const info = getWorkspaceInfo();
|
|
26
|
+
fs.mkdirSync(getWorkspaceDbDir(), { recursive: true });
|
|
27
|
+
const db = new Database(process.env.FORJA_RUNTIME_DB ?? getWorkspaceDbPath());
|
|
28
|
+
new SqliteMigrationRunner(db).apply();
|
|
29
|
+
const store = new SqliteObservationStore(db);
|
|
30
|
+
const observations = store.list();
|
|
31
|
+
db.close();
|
|
32
|
+
const table = loadPricingTable();
|
|
33
|
+
const byModel = new Map();
|
|
34
|
+
let totalCostUsd = 0;
|
|
35
|
+
let unknownPricingCount = 0;
|
|
36
|
+
for (const observation of observations) {
|
|
37
|
+
const model = observation.model ?? '(sem modelo)';
|
|
38
|
+
const priceKnown = lookupPrice(table, observation.model) !== undefined;
|
|
39
|
+
const entry = byModel.get(model) ?? { count: 0, inputTokens: 0, outputTokens: 0, costUsd: 0, priceKnown };
|
|
40
|
+
entry.count += 1;
|
|
41
|
+
entry.inputTokens += observation.inputTokens;
|
|
42
|
+
entry.outputTokens += observation.outputTokens;
|
|
43
|
+
entry.costUsd += observation.cost ?? 0;
|
|
44
|
+
entry.priceKnown = entry.priceKnown || priceKnown;
|
|
45
|
+
byModel.set(model, entry);
|
|
46
|
+
totalCostUsd += observation.cost ?? 0;
|
|
47
|
+
if (!priceKnown)
|
|
48
|
+
unknownPricingCount += 1;
|
|
49
|
+
}
|
|
50
|
+
console.log(`\nCost economy — custo real acumulado (SPEC-029) — workspace: ${info.root}\n`);
|
|
51
|
+
console.log('Estimativa baseada na tabela local de preços (lib/core/model-pricing.json), NÃO a fatura do provider.\n');
|
|
52
|
+
if (observations.length === 0) {
|
|
53
|
+
console.log('Nenhuma observação registrada ainda. Rode `forja llm:run --profile <perfil> --prompt <texto>` para gerar execuções.');
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const rows = [...byModel.entries()].sort((left, right) => right[1].costUsd - left[1].costUsd);
|
|
57
|
+
for (const [model, entry] of rows) {
|
|
58
|
+
const flag = entry.priceKnown ? '' : ' (preço desconhecido nesta execução — custo pode estar subestimado)';
|
|
59
|
+
console.log(`■ ${model}${flag}`);
|
|
60
|
+
console.log(` execuções: ${entry.count} tokens: ${entry.inputTokens + entry.outputTokens} (in ${entry.inputTokens} / out ${entry.outputTokens})`);
|
|
61
|
+
console.log(` custo estimado: $${entry.costUsd.toFixed(4)}\n`);
|
|
62
|
+
}
|
|
63
|
+
console.log(`Total: $${totalCostUsd.toFixed(4)} estimado em ${observations.length} execução(ões).`);
|
|
64
|
+
if (unknownPricingCount > 0) {
|
|
65
|
+
console.log(`Aviso: ${unknownPricingCount} execução(ões) usaram modelo(s) sem preço na tabela local — custo real total pode ser maior que o mostrado. Atualize lib/core/model-pricing.json (\`forja llm:doctor\` lista os gaps).`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (process.argv.includes('--json')) {
|
|
69
|
+
console.log(JSON.stringify({
|
|
70
|
+
workspace: info.root,
|
|
71
|
+
currency: table.currency,
|
|
72
|
+
totalCostUsd,
|
|
73
|
+
observationCount: observations.length,
|
|
74
|
+
unknownPricingCount,
|
|
75
|
+
byModel: Object.fromEntries(byModel),
|
|
76
|
+
}, null, 2));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
main();
|