forjajs 5.0.1 → 5.3.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/CHANGELOG.md +114 -2
- package/README.md +40 -5
- package/README.pt-BR.md +42 -4
- package/boilerplates/02-saas-starter/README.md +58 -293
- package/boilerplates/02-saas-starter/backend/.env.example +13 -0
- package/boilerplates/02-saas-starter/backend/.oxlintrc.json +8 -0
- package/boilerplates/02-saas-starter/backend/nest-cli.json +6 -0
- package/boilerplates/02-saas-starter/backend/package.json +32 -73
- package/boilerplates/02-saas-starter/backend/src/app.module.ts +16 -16
- package/boilerplates/02-saas-starter/backend/src/billing/adapters/http/billing.controller.ts +55 -0
- package/boilerplates/02-saas-starter/backend/src/billing/adapters/persistence/orm-entities.ts +28 -0
- package/boilerplates/02-saas-starter/backend/src/billing/adapters/persistence/typeorm-billing.repository.ts +46 -0
- package/boilerplates/02-saas-starter/backend/src/billing/application/billing.service.spec.ts +58 -0
- package/boilerplates/02-saas-starter/backend/src/billing/application/billing.service.ts +68 -0
- package/boilerplates/02-saas-starter/backend/src/billing/billing.module.ts +11 -0
- package/boilerplates/02-saas-starter/backend/src/billing/domain/plan.spec.ts +23 -0
- package/boilerplates/02-saas-starter/backend/src/billing/domain/plan.ts +36 -0
- package/boilerplates/02-saas-starter/backend/src/billing/domain/ports.ts +27 -0
- package/boilerplates/02-saas-starter/backend/src/billing/domain/subscription.spec.ts +28 -0
- package/boilerplates/02-saas-starter/backend/src/billing/domain/subscription.ts +47 -0
- package/boilerplates/02-saas-starter/backend/src/identity/adapters/http/auth.dto.ts +22 -0
- package/boilerplates/02-saas-starter/backend/src/identity/adapters/http/identity.controller.ts +59 -0
- package/boilerplates/02-saas-starter/backend/src/identity/adapters/persistence/orm-entities.ts +25 -0
- package/boilerplates/02-saas-starter/backend/src/identity/adapters/persistence/typeorm-identity.repository.ts +47 -0
- package/boilerplates/02-saas-starter/backend/src/identity/application/auth.service.spec.ts +72 -0
- package/boilerplates/02-saas-starter/backend/src/identity/application/auth.service.ts +63 -0
- package/boilerplates/02-saas-starter/backend/src/identity/domain/model.spec.ts +19 -0
- package/boilerplates/02-saas-starter/backend/src/identity/domain/model.ts +42 -0
- package/boilerplates/02-saas-starter/backend/src/identity/domain/password.spec.ts +17 -0
- package/boilerplates/02-saas-starter/backend/src/identity/domain/password.ts +26 -0
- package/boilerplates/02-saas-starter/backend/src/identity/identity.module.ts +12 -0
- package/boilerplates/02-saas-starter/backend/src/main.ts +19 -19
- package/boilerplates/02-saas-starter/backend/src/platform/auth/jwt-auth.guard.ts +17 -0
- package/boilerplates/02-saas-starter/backend/src/platform/auth/principal.ts +22 -0
- package/boilerplates/02-saas-starter/backend/src/platform/auth/roles.guard.spec.ts +23 -0
- package/boilerplates/02-saas-starter/backend/src/platform/auth/roles.guard.ts +20 -0
- package/boilerplates/02-saas-starter/backend/src/platform/auth/token.service.ts +54 -0
- package/boilerplates/02-saas-starter/backend/src/platform/config.spec.ts +18 -0
- package/boilerplates/02-saas-starter/backend/src/platform/config.ts +33 -0
- package/boilerplates/02-saas-starter/backend/src/platform/platform.module.ts +14 -0
- package/boilerplates/02-saas-starter/backend/tsconfig.build.json +6 -0
- package/boilerplates/02-saas-starter/backend/tsconfig.json +16 -21
- package/boilerplates/02-saas-starter/backend/vitest.config.ts +7 -0
- package/boilerplates/02-saas-starter/boilerplate.manifest.json +19 -14
- package/boilerplates/02-saas-starter/docker-compose.yml +10 -24
- package/boilerplates/02-saas-starter/memory/30-domains/billing/context.md +15 -132
- package/boilerplates/02-saas-starter/memory/30-domains/identity/context.md +20 -0
- package/boilerplates/README.md +9 -10
- package/dist/apps/cli/src/index.d.ts +0 -1
- package/dist/apps/cli/src/index.js +43 -32
- package/dist/apps/dashboard/app/api/forja/[...path]/route.js +34 -1
- package/dist/apps/dashboard/app/api/forja/capabilities.d.ts +26 -0
- package/dist/apps/dashboard/app/api/forja/capabilities.js +39 -0
- package/dist/apps/dashboard/app/dashboard-client.jsx +607 -42
- package/dist/apps/server/src/forja-nest.module.d.ts +6 -0
- package/dist/apps/server/src/forja-nest.module.js +55 -0
- package/dist/apps/server/src/main.d.ts +21 -4
- package/dist/apps/server/src/main.js +79 -57
- package/dist/apps/server/src/task-planning-service.d.ts +29 -0
- package/dist/apps/server/src/task-planning-service.js +195 -0
- package/dist/apps/server/src/task-runtime-coordinator.d.ts +57 -0
- package/dist/apps/server/src/task-runtime-coordinator.js +229 -0
- package/dist/bin/forja.js +3 -2
- package/dist/bin/init-project.js +1 -0
- package/dist/lib/agent-evals.d.ts +138 -0
- package/dist/lib/agent-evals.js +349 -0
- package/dist/lib/cli-output.js +14 -2
- package/dist/lib/code-context.d.ts +33 -5
- package/dist/lib/code-context.js +70 -5
- package/dist/lib/context-builder.d.ts +49 -4
- package/dist/lib/context-builder.js +114 -47
- package/dist/lib/core/coverage-gate.d.ts +59 -0
- package/dist/lib/core/coverage-gate.js +58 -0
- package/dist/lib/core/doc-graph.d.ts +38 -0
- package/dist/lib/core/doc-graph.js +83 -0
- package/dist/lib/core/health.js +286 -5
- package/dist/lib/core/journey.js +36 -0
- package/dist/lib/core/registry.js +81 -6
- package/dist/lib/docs-reference.d.ts +35 -0
- package/dist/lib/docs-reference.js +57 -0
- package/dist/lib/generators/memory-generator.d.ts +0 -11
- package/dist/lib/generators/memory-generator.js +5 -161
- package/dist/lib/generators/readme-generator.js +1 -1
- package/dist/lib/llm/json-output.d.ts +14 -0
- package/dist/lib/llm/json-output.js +77 -0
- package/dist/lib/project-wiring.d.ts +8 -0
- package/dist/lib/project-wiring.js +79 -3
- package/dist/lib/session-memory.d.ts +38 -0
- package/dist/lib/session-memory.js +206 -0
- package/dist/lib/spec-trace.d.ts +46 -0
- package/dist/lib/spec-trace.js +184 -0
- package/dist/lib/validators/structure-validator.js +0 -1
- package/dist/packages/adapter-nest/src/index.d.ts +2 -1
- package/dist/packages/adapter-nest/src/index.js +59 -1
- package/dist/packages/adapter-sqlite/src/index.d.ts +15 -1
- package/dist/packages/adapter-sqlite/src/index.js +61 -1
- package/dist/packages/context/src/index.d.ts +2 -0
- package/dist/packages/context/src/index.js +36 -15
- package/dist/packages/contracts/src/index.d.ts +64 -0
- package/dist/packages/core/src/index.d.ts +9 -1
- package/dist/packages/core/src/index.js +28 -3
- package/dist/packages/graph/src/index.js +14 -5
- package/dist/packages/llm/src/index.js +12 -2
- package/dist/packages/mcp/src/index.d.ts +8 -0
- package/dist/packages/mcp/src/index.js +12 -1
- package/dist/packages/observability/src/index.d.ts +11 -0
- package/dist/packages/observability/src/index.js +3 -0
- package/dist/packages/orchestration/src/index.d.ts +15 -1
- package/dist/packages/orchestration/src/index.js +127 -21
- package/dist/packages/planner/src/index.d.ts +7 -1
- package/dist/packages/planner/src/index.js +67 -10
- package/dist/packages/planner/src/proposal-store.d.ts +30 -0
- package/dist/packages/planner/src/proposal-store.js +109 -0
- package/dist/packages/policy/src/index.d.ts +9 -16
- package/dist/packages/policy/src/index.js +114 -31
- package/dist/packages/runtime/src/index.d.ts +16 -2
- package/dist/packages/runtime/src/index.js +176 -61
- package/dist/scripts/build-smart-context.d.ts +2 -0
- package/dist/scripts/build-smart-context.js +12 -3
- package/dist/scripts/code-context.d.ts +3 -1
- package/dist/scripts/code-context.js +13 -3
- package/dist/scripts/coverage-gate.d.ts +2 -0
- package/dist/scripts/coverage-gate.js +101 -0
- package/dist/scripts/eval-agents.d.ts +11 -0
- package/dist/scripts/eval-agents.js +96 -0
- package/dist/scripts/gen-commands-doc.d.ts +2 -0
- package/dist/scripts/gen-commands-doc.js +18 -0
- package/dist/scripts/hook-session-memory.d.ts +9 -0
- package/dist/scripts/hook-session-memory.js +20 -0
- package/dist/scripts/hook-session-start.js +34 -0
- package/dist/scripts/llm-fit.js +23 -1
- package/dist/scripts/spec-trace.d.ts +10 -0
- package/dist/scripts/spec-trace.js +73 -0
- package/dist/scripts/sync-universal-memory.js +69 -4
- package/dist/scripts/token-benchmark.d.ts +33 -1
- package/dist/scripts/token-benchmark.js +179 -5
- package/dist/scripts/token-economy.js +3 -1
- package/dist/scripts/tools-doctor.d.ts +16 -1
- package/dist/scripts/tools-doctor.js +58 -17
- package/docs/commands.md +155 -0
- package/docs/fluxo.md +11 -7
- package/docs/init-project.md +3 -1
- package/docs/llm-fit-loop.md +36 -0
- package/docs/manual-operacional-cli-sdd-gsd.md +3 -2
- package/docs/produto/factsheet-marketing.md +7 -5
- package/docs/produto/roteiro-demo.md +14 -5
- package/docs/quick-reference.md +3 -3
- package/docs/token-optimization.md +10 -6
- package/memory/90-decisions/0088-templates-validados.md +3 -0
- package/memory/90-decisions/0089-skills-do-projeto.md +51 -0
- package/memory/90-decisions/0090-adapters-de-agente-somente-leitura.md +47 -0
- package/memory/90-decisions/0091-memoria-de-sessao-por-hooks.md +40 -0
- package/memory/90-decisions/0092-criterios-de-aceite-ligados-a-testes.md +37 -0
- package/memory/90-decisions/0093-avaliacoes-com-agentes-reais.md +40 -0
- package/package.json +4 -2
- package/specs/avaliacoes-de-agentes/spec.md +52 -0
- package/specs/criterios-ligados-a-testes/spec.md +42 -0
- package/specs/memoria-de-sessao/spec.md +48 -0
- package/specs/skills-do-projeto/plan.md +40 -0
- package/specs/skills-do-projeto/spec.md +63 -0
- package/specs/skills-do-projeto/tasks.md +27 -0
- package/specs/v6-agent-runtime-portable/spec.md +83 -0
- package/specs/v6-core-consolidation/spec.md +70 -0
- package/specs/v6-docs-drift/spec.md +85 -0
- package/specs/v6-quality-gates/spec.md +99 -0
- package/specs/v6-token-economy/spec.md +84 -0
- package/templates/evals/agents/adr.json +25 -0
- package/templates/evals/agents/memoria.json +32 -0
- package/templates/evals/agents/proxima-acao.json +29 -0
- package/templates/evals/agents/spec-nova.json +26 -0
- package/templates/evals/agents/teste-do-criterio.json +29 -0
- package/templates/project/AGENTS.md +16 -0
- package/templates/project/CLAUDE.md +3 -2
- package/templates/project/agents/context-engineer.md +3 -0
- package/templates/project/agents/governance.md +3 -0
- package/templates/project/agents/marketing.md +3 -0
- package/templates/project/agents/orchestrator.md +3 -0
- package/templates/project/agents/product.md +3 -0
- package/templates/project/agents/sdd-architect.md +3 -0
- package/templates/project/skills/README.md +14 -0
- package/templates/project/skills/accessibility-review/SKILL.md +89 -0
- package/templates/project/skills/api-design/SKILL.md +77 -0
- package/templates/project/skills/architecture-decision-record/SKILL.md +92 -0
- package/templates/project/skills/backlog-slicing/SKILL.md +82 -0
- package/templates/project/skills/bounded-context-mapping/SKILL.md +88 -0
- package/templates/project/skills/ci-pipeline-review/SKILL.md +83 -0
- package/templates/project/skills/commit-message/SKILL.md +143 -0
- package/templates/project/skills/data-migrations/SKILL.md +88 -0
- package/templates/project/skills/dependency-upgrade/SKILL.md +75 -0
- package/templates/project/skills/docker-compose-review/SKILL.md +85 -0
- package/templates/project/skills/domain-modeling/SKILL.md +120 -0
- package/templates/project/skills/event-storming/SKILL.md +95 -0
- package/templates/project/skills/executive-writing/SKILL.md +99 -0
- package/templates/project/skills/frontend-architecture/SKILL.md +97 -0
- package/templates/project/skills/incident-postmortem/SKILL.md +93 -0
- package/templates/project/skills/lgpd-compliance/SKILL.md +108 -0
- package/templates/project/skills/llm-provider-routing/SKILL.md +18 -0
- package/templates/project/skills/observability/SKILL.md +93 -0
- package/templates/project/skills/product-evaluation/SKILL.md +130 -0
- package/templates/project/skills/prompt-spec/SKILL.md +83 -0
- package/templates/project/skills/pull-request/SKILL.md +101 -0
- package/templates/project/skills/query-performance/SKILL.md +83 -0
- package/templates/project/skills/release-checklist/SKILL.md +106 -0
- package/templates/project/skills/release-notes/SKILL.md +87 -0
- package/templates/project/skills/requirements-elicitation/SKILL.md +127 -0
- package/templates/project/skills/requirements-gathering/SKILL.md +134 -0
- package/templates/project/skills/technical-spike/SKILL.md +101 -0
- package/templates/project/skills/testing/SKILL.md +109 -0
- package/templates/project/skills/threat-modeling/SKILL.md +87 -0
- package/templates/project/skills/ts-async-patterns/SKILL.md +101 -0
- package/templates/project/skills/ts-code-review/SKILL.md +121 -0
- package/templates/project/skills/ts-module-layout/SKILL.md +104 -0
- package/templates/project/skills/ts-testing/SKILL.md +89 -0
- package/templates/project/skills/web-performance/SKILL.md +72 -0
- package/boilerplates/02-saas-starter/.env.example +0 -25
- package/boilerplates/02-saas-starter/CHECKLIST-CRIACAO.md +0 -190
- package/boilerplates/02-saas-starter/INFO.md +0 -252
- package/boilerplates/02-saas-starter/QUICK_START.md +0 -194
- package/boilerplates/02-saas-starter/START-HERE.md +0 -259
- package/boilerplates/02-saas-starter/backend/src/common/decorators/roles.decorator.ts +0 -3
- package/boilerplates/02-saas-starter/backend/src/common/decorators/tenant.decorator.ts +0 -6
- package/boilerplates/02-saas-starter/backend/src/common/middleware/roles.guard.ts +0 -23
- package/boilerplates/02-saas-starter/backend/src/config/database.ts +0 -26
- package/boilerplates/02-saas-starter/backend/src/modules/auth/auth.controller.ts +0 -46
- package/boilerplates/02-saas-starter/backend/src/modules/auth/auth.module.ts +0 -25
- package/boilerplates/02-saas-starter/backend/src/modules/auth/auth.service.ts +0 -145
- package/boilerplates/02-saas-starter/backend/src/modules/auth/dto/auth.dto.ts +0 -30
- package/boilerplates/02-saas-starter/backend/src/modules/auth/strategies/jwt.strategy.ts +0 -24
- package/boilerplates/02-saas-starter/backend/src/modules/auth/strategies/local.strategy.ts +0 -19
- package/boilerplates/02-saas-starter/backend/src/modules/billing/billing.controller.ts +0 -42
- package/boilerplates/02-saas-starter/backend/src/modules/billing/billing.module.ts +0 -13
- package/boilerplates/02-saas-starter/backend/src/modules/billing/billing.service.ts +0 -67
- package/boilerplates/02-saas-starter/backend/src/modules/billing/entities/invoice.entity.ts +0 -52
- package/boilerplates/02-saas-starter/backend/src/modules/organizations/entities/organization.entity.ts +0 -36
- package/boilerplates/02-saas-starter/backend/src/modules/organizations/organizations.controller.ts +0 -16
- package/boilerplates/02-saas-starter/backend/src/modules/organizations/organizations.module.ts +0 -13
- package/boilerplates/02-saas-starter/backend/src/modules/organizations/organizations.service.ts +0 -34
- package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/entities/subscription.entity.ts +0 -40
- package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/subscriptions.controller.ts +0 -36
- package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/subscriptions.module.ts +0 -13
- package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/subscriptions.service.ts +0 -91
- package/boilerplates/02-saas-starter/backend/src/modules/users/entities/user.entity.ts +0 -42
- package/boilerplates/02-saas-starter/backend/src/modules/users/users.controller.ts +0 -14
- package/boilerplates/02-saas-starter/backend/src/modules/users/users.module.ts +0 -13
- package/boilerplates/02-saas-starter/backend/src/modules/users/users.service.ts +0 -48
- package/boilerplates/02-saas-starter/memory/00-global/agent-contract.md +0 -227
- package/boilerplates/02-saas-starter/memory/00-global/context-policy.md +0 -141
- package/boilerplates/02-saas-starter/memory/00-global/index.md +0 -133
- package/boilerplates/02-saas-starter/memory/00-global/mission.md +0 -45
- package/boilerplates/02-saas-starter/memory/00-global/patterns.md +0 -223
- package/boilerplates/02-saas-starter/memory/10-product/business-rules.md +0 -240
- package/boilerplates/02-saas-starter/memory/10-product/nfrs.md +0 -239
- package/boilerplates/02-saas-starter/memory/10-product/personas.md +0 -195
- package/boilerplates/02-saas-starter/memory/10-product/vision.md +0 -63
- package/boilerplates/02-saas-starter/memory/20-architecture/overview.md +0 -232
- package/boilerplates/02-saas-starter/memory/30-domains/auth/api.md +0 -186
- package/boilerplates/02-saas-starter/memory/30-domains/auth/context.md +0 -85
- package/boilerplates/02-saas-starter/memory/30-domains/billing/api.md +0 -262
- package/boilerplates/02-saas-starter/memory/30-domains/subscriptions/api.md +0 -176
- package/boilerplates/02-saas-starter/memory/30-domains/subscriptions/context.md +0 -100
- package/boilerplates/02-saas-starter/memory/40-delivery/backlog.md +0 -81
- package/boilerplates/02-saas-starter/memory/40-delivery/roadmap.md +0 -97
- package/boilerplates/02-saas-starter/memory/40-delivery/sprint-atual.md +0 -57
- package/boilerplates/02-saas-starter/memory/50-orchestration/topology.md +0 -90
- package/boilerplates/02-saas-starter/memory/60-runs/index.md +0 -54
- package/boilerplates/02-saas-starter/memory/70-summaries/global.md +0 -50
- package/boilerplates/02-saas-starter/memory/80-data/schema.md +0 -185
- package/boilerplates/02-saas-starter/memory/90-decisions/adr-template.md +0 -75
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/agent-evals.ts — avaliações com agentes reais em projeto descartável (SPEC-054, ADR-0093).
|
|
3
|
+
*
|
|
4
|
+
* Cada caso é um dado (`templates/evals/agents/*.json`): prompt, semente e checks determinísticos.
|
|
5
|
+
* O runner cria workspace e projeto num diretório temporário, aplica a semente, roda o agente
|
|
6
|
+
* (Claude Code ou Codex) com escrita confinada a esses diretórios e corrige pelo que ficou no disco,
|
|
7
|
+
* pelos comandos executados e pela resposta. Nunca roda no projeto do usuário.
|
|
8
|
+
*/
|
|
9
|
+
import crypto from 'node:crypto';
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import os from 'node:os';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { spawnSync } from 'node:child_process';
|
|
14
|
+
import { traceSpecs } from './spec-trace.js';
|
|
15
|
+
const CHECK_TYPES = new Set(['file', 'answer', 'ran', 'criterion', 'command', 'unchanged']);
|
|
16
|
+
export function validateCase(c, source = 'caso') {
|
|
17
|
+
const fail = (msg) => { throw new Error(`${source}: ${msg}`); };
|
|
18
|
+
if (!c || typeof c !== 'object')
|
|
19
|
+
fail('não é um objeto');
|
|
20
|
+
if (typeof c.id !== 'string' || !/^[a-z0-9-]+$/.test(c.id))
|
|
21
|
+
fail('id ausente ou fora de kebab-case');
|
|
22
|
+
if (typeof c.prompt !== 'string' || !c.prompt.trim())
|
|
23
|
+
fail('prompt ausente');
|
|
24
|
+
if (!Array.isArray(c.checks) || !c.checks.length)
|
|
25
|
+
fail('sem checks');
|
|
26
|
+
for (const k of c.checks) {
|
|
27
|
+
if (!CHECK_TYPES.has(k?.type))
|
|
28
|
+
fail(`check de tipo desconhecido: ${k?.type}`);
|
|
29
|
+
if (typeof k.desc !== 'string' || !k.desc)
|
|
30
|
+
fail(`check ${k.type} sem desc`);
|
|
31
|
+
for (const re of [...(k.contains ?? []), ...(k.matches ? [k.matches].flat() : [])]) {
|
|
32
|
+
try {
|
|
33
|
+
new RegExp(re);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
fail(`regex inválida em "${k.desc}": ${re}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
for (const rel of Object.keys(c.seed?.files ?? {})) {
|
|
41
|
+
if (path.isAbsolute(rel) || rel.split('/').includes('..'))
|
|
42
|
+
fail(`semente fora do projeto: ${rel}`);
|
|
43
|
+
}
|
|
44
|
+
return { title: c.id, ...c };
|
|
45
|
+
}
|
|
46
|
+
export function loadCases(dir) {
|
|
47
|
+
return fs.readdirSync(dir).filter((n) => n.endsWith('.json')).sort()
|
|
48
|
+
.map((n) => validateCase(JSON.parse(fs.readFileSync(path.join(dir, n), 'utf8')), n));
|
|
49
|
+
}
|
|
50
|
+
/** Glob mínimo: `**` atravessa pastas, `*` não; o resto é literal. */
|
|
51
|
+
export function globToRegExp(glob) {
|
|
52
|
+
let re = '';
|
|
53
|
+
for (let i = 0; i < glob.length; i++) {
|
|
54
|
+
const ch = glob[i];
|
|
55
|
+
if (ch === '*' && glob[i + 1] === '*') {
|
|
56
|
+
re += glob[i + 2] === '/' ? '(?:.*/)?' : '.*';
|
|
57
|
+
i += glob[i + 2] === '/' ? 2 : 1;
|
|
58
|
+
}
|
|
59
|
+
else if (ch === '*')
|
|
60
|
+
re += '[^/]*';
|
|
61
|
+
else
|
|
62
|
+
re += ch.replace(/[.+?^${}()|[\]\\]/g, '\\$&');
|
|
63
|
+
}
|
|
64
|
+
return new RegExp(`^${re}$`);
|
|
65
|
+
}
|
|
66
|
+
/** Arquivos que o próprio Forja gera durante a sessão (hooks, índice): não contam como mudança do agente. */
|
|
67
|
+
const SNAPSHOT_SKIP = new Set(['node_modules', '.git', '.context', '.forja', '.memory']);
|
|
68
|
+
/** Índice SQLite (atualizado por query:universal/sync) e checkpoints de sessão (hooks). */
|
|
69
|
+
const FORJA_GENERATED = new Set(['memory/sqlite', 'memory/60-runs/sessions']);
|
|
70
|
+
export function snapshot(dir) {
|
|
71
|
+
const out = new Map();
|
|
72
|
+
const walk = (abs, rel) => {
|
|
73
|
+
for (const e of fs.readdirSync(abs, { withFileTypes: true })) {
|
|
74
|
+
if (SNAPSHOT_SKIP.has(e.name))
|
|
75
|
+
continue;
|
|
76
|
+
const r = rel ? `${rel}/${e.name}` : e.name;
|
|
77
|
+
if (FORJA_GENERATED.has(r))
|
|
78
|
+
continue;
|
|
79
|
+
const p = path.join(abs, e.name);
|
|
80
|
+
if (e.isDirectory())
|
|
81
|
+
walk(p, r);
|
|
82
|
+
else if (e.isFile())
|
|
83
|
+
out.set(r, crypto.createHash('sha1').update(fs.readFileSync(p)).digest('hex'));
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
walk(dir, '');
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
/** Comandos que o Claude pode rodar sem perguntar. Fora disso, `-p` nega (e o caso mostra). */
|
|
90
|
+
export const CLAUDE_ALLOWED_TOOLS = [
|
|
91
|
+
'Read', 'Grep', 'Glob', 'Edit', 'Write', 'MultiEdit', 'mcp__forja',
|
|
92
|
+
'Bash(forja *)', 'Bash(node --test*)', 'Bash(npm test*)', 'Bash(npm run test*)',
|
|
93
|
+
'Bash(ls*)', 'Bash(cat *)', 'Bash(mkdir *)', 'Bash(git status*)', 'Bash(git diff*)', 'Bash(git log*)',
|
|
94
|
+
];
|
|
95
|
+
export function parseClaudeStream(stdout) {
|
|
96
|
+
const events = stdout.split('\n').filter(Boolean).map((l) => { try {
|
|
97
|
+
return JSON.parse(l);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return null;
|
|
101
|
+
} }).filter(Boolean);
|
|
102
|
+
const result = events.find((e) => e.type === 'result');
|
|
103
|
+
const denied = new Set((result?.permission_denials ?? []).map((d) => d?.tool_use_id).filter(Boolean));
|
|
104
|
+
const commands = [];
|
|
105
|
+
for (const e of events) {
|
|
106
|
+
if (e.type !== 'assistant' || !Array.isArray(e.message?.content))
|
|
107
|
+
continue;
|
|
108
|
+
for (const c of e.message.content) {
|
|
109
|
+
if (c?.type !== 'tool_use' || denied.has(c.id))
|
|
110
|
+
continue;
|
|
111
|
+
if (c.name === 'Bash' && typeof c.input?.command === 'string')
|
|
112
|
+
commands.push(c.input.command);
|
|
113
|
+
else if (typeof c.name === 'string' && c.name.startsWith('mcp__'))
|
|
114
|
+
commands.push(`tool:${c.name}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const u = result?.usage;
|
|
118
|
+
return {
|
|
119
|
+
answer: typeof result?.result === 'string' ? result.result : '',
|
|
120
|
+
commands,
|
|
121
|
+
costUsd: typeof result?.total_cost_usd === 'number' ? result.total_cost_usd : null,
|
|
122
|
+
tokens: u ? { input: (u.input_tokens ?? 0) + (u.cache_read_input_tokens ?? 0) + (u.cache_creation_input_tokens ?? 0), output: u.output_tokens ?? 0 } : null,
|
|
123
|
+
...(result ? (result.is_error ? { error: String(result.subtype ?? 'erro') } : {}) : { error: 'sem evento result' }),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
export function parseCodexJson(stdout) {
|
|
127
|
+
const events = stdout.split('\n').filter(Boolean).map((l) => { try {
|
|
128
|
+
return JSON.parse(l);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return null;
|
|
132
|
+
} }).filter(Boolean);
|
|
133
|
+
const commands = [];
|
|
134
|
+
let answer = '';
|
|
135
|
+
let input = 0, output = 0, sawUsage = false, failed;
|
|
136
|
+
for (const e of events) {
|
|
137
|
+
if (e.type === 'item.completed' && e.item?.type === 'command_execution' && typeof e.item.command === 'string') {
|
|
138
|
+
// Codex embrulha em `bash -lc '<cmd>'`.
|
|
139
|
+
const m = e.item.command.match(/^(?:\/\S*\/)?(?:ba)?sh\s+-l?c\s+(['"]?)([\s\S]*)\1$/);
|
|
140
|
+
commands.push(m ? m[2] : e.item.command);
|
|
141
|
+
}
|
|
142
|
+
if (e.type === 'item.completed' && e.item?.type === 'agent_message' && typeof e.item.text === 'string')
|
|
143
|
+
answer = e.item.text;
|
|
144
|
+
if (e.type === 'turn.completed' && e.usage) {
|
|
145
|
+
sawUsage = true;
|
|
146
|
+
input += (e.usage.input_tokens ?? 0);
|
|
147
|
+
output += (e.usage.output_tokens ?? 0) + (e.usage.reasoning_output_tokens ?? 0);
|
|
148
|
+
}
|
|
149
|
+
if (e.type === 'turn.failed' || e.type === 'error')
|
|
150
|
+
failed = String(e.error?.message ?? e.message ?? e.type);
|
|
151
|
+
}
|
|
152
|
+
return { answer, commands, costUsd: null, tokens: sawUsage ? { input, output } : null, ...(failed ? { error: failed } : {}) };
|
|
153
|
+
}
|
|
154
|
+
function timed(bin, args, ctx) {
|
|
155
|
+
const t0 = Date.now();
|
|
156
|
+
const r = spawnSync(bin, args, { cwd: ctx.projectDir, env: ctx.env, encoding: 'utf8', timeout: ctx.timeoutMs, input: '', maxBuffer: 256 * 1024 * 1024 });
|
|
157
|
+
const timedOut = r.error && r.error.code === 'ETIMEDOUT';
|
|
158
|
+
return { r, durationMs: Date.now() - t0, timedOut: Boolean(timedOut) };
|
|
159
|
+
}
|
|
160
|
+
function cliVersion(bin) {
|
|
161
|
+
const r = spawnSync(bin, ['--version'], { encoding: 'utf8', timeout: 15_000 });
|
|
162
|
+
return r.status === 0 ? (r.stdout || '').trim().split('\n')[0] : null;
|
|
163
|
+
}
|
|
164
|
+
export const claudeAdapter = (bin = 'claude') => ({
|
|
165
|
+
name: 'claude',
|
|
166
|
+
version: () => cliVersion(bin),
|
|
167
|
+
run(ctx) {
|
|
168
|
+
const args = ['-p', ctx.prompt, '--output-format', 'stream-json', '--verbose',
|
|
169
|
+
'--permission-mode', 'acceptEdits', '--add-dir', ctx.workspaceDir,
|
|
170
|
+
'--max-budget-usd', String(ctx.budgetUsd), '--no-session-persistence',
|
|
171
|
+
...(ctx.model ? ['--model', ctx.model] : []),
|
|
172
|
+
'--allowedTools', ...CLAUDE_ALLOWED_TOOLS];
|
|
173
|
+
const { r, durationMs, timedOut } = timed(bin, args, ctx);
|
|
174
|
+
const parsed = parseClaudeStream(r.stdout || '');
|
|
175
|
+
return { ...parsed, durationMs, exitCode: r.status ?? 1, ...(timedOut ? { error: `timeout (${ctx.timeoutMs} ms)` } : {}) };
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
export const codexAdapter = (bin = 'codex') => ({
|
|
179
|
+
name: 'codex',
|
|
180
|
+
version: () => cliVersion(bin),
|
|
181
|
+
run(ctx) {
|
|
182
|
+
const args = ['exec', '--json', '--ephemeral', '--skip-git-repo-check',
|
|
183
|
+
'-s', 'workspace-write', '-C', ctx.projectDir, '--add-dir', ctx.workspaceDir,
|
|
184
|
+
...(ctx.model ? ['-m', ctx.model] : []), ctx.prompt];
|
|
185
|
+
const { r, durationMs, timedOut } = timed(bin, args, ctx);
|
|
186
|
+
const parsed = parseCodexJson(r.stdout || '');
|
|
187
|
+
return { ...parsed, durationMs, exitCode: r.status ?? 1, ...(timedOut ? { error: `timeout (${ctx.timeoutMs} ms)` } : {}) };
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
export const ADAPTERS = {
|
|
191
|
+
claude: () => claudeAdapter(process.env.FORJA_EVAL_CLAUDE_BIN || 'claude'),
|
|
192
|
+
codex: () => codexAdapter(process.env.FORJA_EVAL_CODEX_BIN || 'codex'),
|
|
193
|
+
};
|
|
194
|
+
const rx = (s) => new RegExp(s, 'im');
|
|
195
|
+
export function grade(check, projectDir, run, before, env) {
|
|
196
|
+
const res = (ok, detail) => ({ desc: check.desc, type: check.type, ok, detail });
|
|
197
|
+
switch (check.type) {
|
|
198
|
+
case 'file': {
|
|
199
|
+
const re = globToRegExp(check.glob);
|
|
200
|
+
const ex = (check.exclude ?? []).map(globToRegExp);
|
|
201
|
+
const after = snapshot(projectDir);
|
|
202
|
+
const files = [...after.keys()].filter((f) => re.test(f) && !ex.some((e) => e.test(f)));
|
|
203
|
+
if (!files.length)
|
|
204
|
+
return res(false, `nenhum arquivo em ${check.glob}`);
|
|
205
|
+
const missing = (f) => (check.contains ?? []).filter((c) => !rx(c).test(fs.readFileSync(path.join(projectDir, f), 'utf8')));
|
|
206
|
+
const hit = files.find((f) => missing(f).length === 0);
|
|
207
|
+
return hit ? res(true, hit) : res(false, `${files[0]}: falta ${missing(files[0]).join(', ')}`);
|
|
208
|
+
}
|
|
209
|
+
case 'answer': {
|
|
210
|
+
const miss = check.matches.filter((m) => !rx(m).test(run.answer));
|
|
211
|
+
return miss.length ? res(false, `resposta sem ${miss.join(', ')}`) : res(true, 'resposta confere');
|
|
212
|
+
}
|
|
213
|
+
case 'ran': {
|
|
214
|
+
const hit = run.commands.find((c) => rx(check.matches).test(c));
|
|
215
|
+
return hit ? res(true, hit.slice(0, 120)) : res(false, `nenhum comando casou /${check.matches}/ (${run.commands.length} executados)`);
|
|
216
|
+
}
|
|
217
|
+
case 'criterion': {
|
|
218
|
+
const spec = traceSpecs(projectDir).specs.find((s) => s.slug === check.spec || s.specId === check.spec);
|
|
219
|
+
const c = spec?.criteria.find((x) => x.id === check.ac);
|
|
220
|
+
if (!c)
|
|
221
|
+
return res(false, `${check.spec} ${check.ac} não existe`);
|
|
222
|
+
return c.tests.length ? res(true, c.tests.map((t) => `${t.file}:${t.line}`).join(', ')) : res(false, 'nenhum teste cita o critério');
|
|
223
|
+
}
|
|
224
|
+
case 'command': {
|
|
225
|
+
const r = spawnSync('sh', ['-c', check.run], { cwd: projectDir, env, encoding: 'utf8', timeout: 120_000 });
|
|
226
|
+
const want = check.expectExit ?? 0;
|
|
227
|
+
const tail = `${r.stdout || ''}${r.stderr || ''}`.trim().split('\n').slice(-2).join(' | ').slice(0, 200);
|
|
228
|
+
return r.status === want ? res(true, `exit ${r.status}`) : res(false, `exit ${r.status} (esperado ${want}): ${tail}`);
|
|
229
|
+
}
|
|
230
|
+
case 'unchanged': {
|
|
231
|
+
const re = globToRegExp(check.glob);
|
|
232
|
+
const after = snapshot(projectDir);
|
|
233
|
+
const keys = new Set([...before.keys(), ...after.keys()].filter((f) => re.test(f)));
|
|
234
|
+
const changed = [...keys].filter((f) => before.get(f) !== after.get(f));
|
|
235
|
+
return changed.length ? res(false, `mudou: ${changed.slice(0, 5).join(', ')}`) : res(true, 'nada mudou');
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/** Workspace + projeto descartáveis, com um `forja` no PATH apontando para a instalação em teste. */
|
|
240
|
+
export function prepareProject(baseDir, name, forjaBin) {
|
|
241
|
+
const bin = path.join(baseDir, 'bin');
|
|
242
|
+
fs.mkdirSync(bin, { recursive: true });
|
|
243
|
+
const shim = path.join(bin, 'forja');
|
|
244
|
+
if (!fs.existsSync(shim)) {
|
|
245
|
+
fs.writeFileSync(shim, `#!/bin/sh\nexec ${JSON.stringify(process.execPath)} ${JSON.stringify(forjaBin)} "$@"\n`);
|
|
246
|
+
fs.chmodSync(shim, 0o755);
|
|
247
|
+
}
|
|
248
|
+
const ws = path.join(baseDir, 'ws');
|
|
249
|
+
const env = { ...process.env, FORJA_WORKSPACE: ws, PATH: `${bin}${path.delimiter}${process.env.PATH ?? ''}` };
|
|
250
|
+
for (const k of Object.keys(env))
|
|
251
|
+
if (k.startsWith('npm_'))
|
|
252
|
+
delete env[k];
|
|
253
|
+
const forja = (args, cwd) => {
|
|
254
|
+
const r = spawnSync(process.execPath, [forjaBin, ...args], { cwd, env, encoding: 'utf8', timeout: 180_000 });
|
|
255
|
+
if (r.status !== 0)
|
|
256
|
+
throw new Error(`forja ${args.join(' ')} falhou: ${`${r.stdout}${r.stderr}`.trim().split('\n').slice(-3).join(' | ')}`);
|
|
257
|
+
};
|
|
258
|
+
if (!fs.existsSync(ws))
|
|
259
|
+
forja(['workspace:init'], baseDir);
|
|
260
|
+
forja(['project:new', name, '--ai', 'claude,codex', '--skip-backend'], baseDir);
|
|
261
|
+
return { ws, proj: path.join(ws, 'projects', name), env };
|
|
262
|
+
}
|
|
263
|
+
const ANSWER_EXCERPT = 400;
|
|
264
|
+
export function runCase(c, adapter, baseDir, opts) {
|
|
265
|
+
const t0 = Date.now();
|
|
266
|
+
let prepared;
|
|
267
|
+
try {
|
|
268
|
+
prepared = prepareProject(baseDir, `caso-${c.id}`, opts.forjaBin);
|
|
269
|
+
}
|
|
270
|
+
catch (e) {
|
|
271
|
+
return { id: c.id, title: c.title, pass: false, checks: [], costUsd: null, durationMs: Date.now() - t0, commands: 0, answer: '', error: e.message };
|
|
272
|
+
}
|
|
273
|
+
const { ws, proj, env } = prepared;
|
|
274
|
+
for (const [rel, content] of Object.entries(c.seed?.files ?? {})) {
|
|
275
|
+
fs.mkdirSync(path.dirname(path.join(proj, rel)), { recursive: true });
|
|
276
|
+
fs.writeFileSync(path.join(proj, rel), content);
|
|
277
|
+
}
|
|
278
|
+
spawnSync(process.execPath, [opts.forjaBin, 'sync:universal'], { cwd: proj, env, encoding: 'utf8', timeout: 120_000 });
|
|
279
|
+
const before = snapshot(proj);
|
|
280
|
+
const run = adapter.run({ projectDir: proj, workspaceDir: ws, prompt: c.prompt, env, model: opts.model, budgetUsd: opts.budgetUsd, timeoutMs: opts.timeoutMs });
|
|
281
|
+
const checks = c.checks.map((k) => {
|
|
282
|
+
try {
|
|
283
|
+
return grade(k, proj, run, before, env);
|
|
284
|
+
}
|
|
285
|
+
catch (e) {
|
|
286
|
+
return { desc: k.desc, type: k.type, ok: false, detail: `erro no check: ${e.message}` };
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
return {
|
|
290
|
+
id: c.id, title: c.title, pass: checks.every((k) => k.ok) && !run.error, checks,
|
|
291
|
+
costUsd: run.costUsd, durationMs: run.durationMs, commands: run.commands.length,
|
|
292
|
+
answer: run.answer.slice(0, ANSWER_EXCERPT),
|
|
293
|
+
...(run.error ? { error: run.error } : {}),
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
export function runSuite(cases, adapter, forjaVersion, opts) {
|
|
297
|
+
const base = fs.mkdtempSync(path.join(os.tmpdir(), `forja-eval-${adapter.name}-`));
|
|
298
|
+
const results = [];
|
|
299
|
+
try {
|
|
300
|
+
for (const c of cases) {
|
|
301
|
+
const r = runCase(c, adapter, base, opts);
|
|
302
|
+
results.push(r);
|
|
303
|
+
opts.onCase?.(c, r);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
finally {
|
|
307
|
+
if (!opts.keep)
|
|
308
|
+
fs.rmSync(base, { recursive: true, force: true });
|
|
309
|
+
}
|
|
310
|
+
const costs = results.map((r) => r.costUsd).filter((x) => typeof x === 'number');
|
|
311
|
+
return {
|
|
312
|
+
ts: new Date().toISOString(), agent: adapter.name, agentVersion: adapter.version(), model: opts.model ?? null, forjaVersion,
|
|
313
|
+
cases: results, passed: results.filter((r) => r.pass).length,
|
|
314
|
+
costUsd: costs.length ? Math.round(costs.reduce((a, b) => a + b, 0) * 10_000) / 10_000 : null,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
// ─── Histórico ─────────────────────────────────────────────────────────────────
|
|
318
|
+
export function appendHistory(file, result) {
|
|
319
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
320
|
+
fs.appendFileSync(file, `${JSON.stringify(result)}\n`, 'utf8');
|
|
321
|
+
}
|
|
322
|
+
/** Última execução do mesmo agente e modelo, para comparar. */
|
|
323
|
+
export function previousRun(file, agent, model) {
|
|
324
|
+
let lines = [];
|
|
325
|
+
try {
|
|
326
|
+
lines = fs.readFileSync(file, 'utf8').trim().split('\n');
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
332
|
+
try {
|
|
333
|
+
const r = JSON.parse(lines[i]);
|
|
334
|
+
if (r.agent === agent && (r.model ?? null) === model)
|
|
335
|
+
return r;
|
|
336
|
+
}
|
|
337
|
+
catch { /* linha corrompida: ignora */ }
|
|
338
|
+
}
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
export function compareRuns(prev, cur) {
|
|
342
|
+
if (!prev)
|
|
343
|
+
return { regressed: [], fixed: [] };
|
|
344
|
+
const was = new Map(prev.cases.map((c) => [c.id, c.pass]));
|
|
345
|
+
return {
|
|
346
|
+
regressed: cur.cases.filter((c) => was.get(c.id) === true && !c.pass).map((c) => c.id),
|
|
347
|
+
fixed: cur.cases.filter((c) => was.get(c.id) === false && c.pass).map((c) => c.id),
|
|
348
|
+
};
|
|
349
|
+
}
|
package/dist/lib/cli-output.js
CHANGED
|
@@ -10,8 +10,20 @@
|
|
|
10
10
|
* O stdout carrega **um único** objeto JSON, sempre. Diagnóstico verboso vai para o stderr.
|
|
11
11
|
* Use só no ramo `--json`; a saída de texto de cada comando continua como está.
|
|
12
12
|
*/
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
/**
|
|
15
|
+
* Escreve até o último byte antes de devolver. `process.stdout.write` + `process.exit()` trunca no
|
|
16
|
+
* tamanho do buffer do pipe (64KB): o JSON de `spec:trace --json` saía cortado e quem fazia parse
|
|
17
|
+
* recebia "Unterminated string in JSON" — sem erro do comando, exit 0. `fs.writeSync` bloqueia, e o
|
|
18
|
+
* laço cobre o caso de escrita parcial.
|
|
19
|
+
*/
|
|
20
|
+
function writeAll(fd, text) {
|
|
21
|
+
const buf = Buffer.from(text, 'utf8');
|
|
22
|
+
for (let offset = 0; offset < buf.length;)
|
|
23
|
+
offset += fs.writeSync(fd, buf, offset, buf.length - offset);
|
|
24
|
+
}
|
|
13
25
|
function write(payload, code) {
|
|
14
|
-
|
|
26
|
+
writeAll(1, JSON.stringify(payload) + '\n');
|
|
15
27
|
process.exit(code);
|
|
16
28
|
}
|
|
17
29
|
/** Sucesso. `fields` são os dados do comando, no topo, ao lado de `status`. */
|
|
@@ -24,7 +36,7 @@ export function emitOk(fields = {}) {
|
|
|
24
36
|
*/
|
|
25
37
|
export function emitError(message, opts = {}) {
|
|
26
38
|
if (opts.detail)
|
|
27
|
-
|
|
39
|
+
writeAll(2, opts.detail.replace(/\s+$/, '') + '\n');
|
|
28
40
|
const error = { message };
|
|
29
41
|
if (opts.code)
|
|
30
42
|
error.code = opts.code;
|
|
@@ -14,6 +14,28 @@ export interface CodeContextEnv {
|
|
|
14
14
|
}
|
|
15
15
|
/** Tokens ≈ bytes/4 — o mesmo proxy do resto do framework. */
|
|
16
16
|
export declare function estimateTokens(text: string): number;
|
|
17
|
+
/**
|
|
18
|
+
* Orçamento do código anexado por padrão (SPEC-057 AC-4). Uma fatia grande tem dezenas de arquivos e
|
|
19
|
+
* `--code` lia todos por inteiro — janela gasta com apresentação e ORM antes de chegar na regra.
|
|
20
|
+
* `--full` devolve o comportamento antigo.
|
|
21
|
+
*/
|
|
22
|
+
export declare const CODE_TOKEN_BUDGET = 6000;
|
|
23
|
+
export interface CodeFile {
|
|
24
|
+
path: string;
|
|
25
|
+
tokens: number;
|
|
26
|
+
content: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Ordem de leitura da fatia: quem casa com o termo vem primeiro (caminho pesa mais que conteúdo —
|
|
30
|
+
* `order.entity.ts` é sobre a entidade, um arquivo que só a menciona não), desempate por camada e
|
|
31
|
+
* depois por caminho, para a mesma consulta dar sempre o mesmo pacote.
|
|
32
|
+
*/
|
|
33
|
+
export declare function byRelevance(files: CodeFile[], terms: string[]): CodeFile[];
|
|
34
|
+
/** Pega arquivos na ordem até estourar o orçamento; devolve também o que ficou de fora. */
|
|
35
|
+
export declare function withinBudget(files: CodeFile[], budget: number): {
|
|
36
|
+
kept: CodeFile[];
|
|
37
|
+
omitted: CodeFile[];
|
|
38
|
+
};
|
|
17
39
|
/** Domínios disponíveis num projeto: os diretórios sob `memory/30-domains/`. */
|
|
18
40
|
export declare function domainsOf(projectRoot: string, env?: CodeContextEnv): string[];
|
|
19
41
|
export interface ContextPack {
|
|
@@ -22,20 +44,26 @@ export interface ContextPack {
|
|
|
22
44
|
mapPath: string | null;
|
|
23
45
|
map: string | null;
|
|
24
46
|
mapTokens: number;
|
|
25
|
-
code:
|
|
47
|
+
code: CodeFile[];
|
|
48
|
+
codeTokens: number;
|
|
49
|
+
/** Arquivos da fatia que o orçamento deixou de fora — sempre à mostra, nunca corte calado. */
|
|
50
|
+
codeOmitted: {
|
|
26
51
|
path: string;
|
|
27
52
|
tokens: number;
|
|
28
|
-
content: string;
|
|
29
53
|
}[];
|
|
30
|
-
codeTokens: number;
|
|
31
54
|
totalTokens: number;
|
|
32
55
|
}
|
|
33
56
|
/**
|
|
34
57
|
* Monta o pacote de um domínio. Por padrão só o mapa (`context.md`) — o contexto barato que substitui
|
|
35
|
-
* a exploração. Com `includeCode`, anexa o código da fatia — o que se lê ao *editar
|
|
58
|
+
* a exploração. Com `includeCode`, anexa o código da fatia — o que se lê ao *editar* — na ordem de
|
|
59
|
+
* relevância e até `budget` tokens; `full: true` anexa a fatia inteira (comportamento pré-SPEC-057,
|
|
60
|
+
* e o que o `token:economy` precisa para medir o custo de ler tudo).
|
|
36
61
|
*/
|
|
37
|
-
export declare function buildContextPack({ projectRoot, domain, includeCode }: {
|
|
62
|
+
export declare function buildContextPack({ projectRoot, domain, includeCode, query, full, budget, }: {
|
|
38
63
|
projectRoot: string;
|
|
39
64
|
domain: string;
|
|
40
65
|
includeCode?: boolean;
|
|
66
|
+
query?: string;
|
|
67
|
+
full?: boolean;
|
|
68
|
+
budget?: number;
|
|
41
69
|
}, env?: CodeContextEnv): ContextPack;
|
package/dist/lib/code-context.js
CHANGED
|
@@ -15,6 +15,62 @@ const defaultFsEnv = { fs };
|
|
|
15
15
|
export function estimateTokens(text) {
|
|
16
16
|
return Math.round(text.length / 4);
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Orçamento do código anexado por padrão (SPEC-057 AC-4). Uma fatia grande tem dezenas de arquivos e
|
|
20
|
+
* `--code` lia todos por inteiro — janela gasta com apresentação e ORM antes de chegar na regra.
|
|
21
|
+
* `--full` devolve o comportamento antigo.
|
|
22
|
+
*/
|
|
23
|
+
export const CODE_TOKEN_BUDGET = 6000;
|
|
24
|
+
/**
|
|
25
|
+
* Camadas mais informativas primeiro: é em `domain/` que a invariante mora, e `presentation/` é a
|
|
26
|
+
* camada que um agente quase sempre já sabe como é.
|
|
27
|
+
*/
|
|
28
|
+
const LAYER_ORDER = ['domain', 'application', 'infrastructure', 'presentation'];
|
|
29
|
+
/**
|
|
30
|
+
* Caminho relativo como o pacote o mostra: com `/`. O pack é texto colado no contexto de um agente —
|
|
31
|
+
* no Windows o separador nativo faria o mesmo domínio produzir dois pacotes diferentes (e quebraria
|
|
32
|
+
* a igualdade que os testes de contrato dependem).
|
|
33
|
+
*/
|
|
34
|
+
function relFrom(root, target) {
|
|
35
|
+
return path.relative(root, target).split(path.sep).join('/');
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Ordem de leitura da fatia: quem casa com o termo vem primeiro (caminho pesa mais que conteúdo —
|
|
39
|
+
* `order.entity.ts` é sobre a entidade, um arquivo que só a menciona não), desempate por camada e
|
|
40
|
+
* depois por caminho, para a mesma consulta dar sempre o mesmo pacote.
|
|
41
|
+
*/
|
|
42
|
+
export function byRelevance(files, terms) {
|
|
43
|
+
const hits = (f) => {
|
|
44
|
+
const rel = f.path.toLowerCase();
|
|
45
|
+
const body = f.content.toLowerCase();
|
|
46
|
+
return terms.reduce((acc, t) => {
|
|
47
|
+
const inPath = rel.includes(t) ? 3 : 0;
|
|
48
|
+
const inBody = body.split(t).length - 1;
|
|
49
|
+
return acc + inPath + Math.min(inBody, 5);
|
|
50
|
+
}, 0);
|
|
51
|
+
};
|
|
52
|
+
const layer = (f) => {
|
|
53
|
+
const rel = f.path.toLowerCase();
|
|
54
|
+
const at = LAYER_ORDER.findIndex((l) => rel.includes(`/${l}/`));
|
|
55
|
+
return at === -1 ? LAYER_ORDER.length : at;
|
|
56
|
+
};
|
|
57
|
+
return [...files].sort((a, b) => hits(b) - hits(a) || layer(a) - layer(b) || a.path.localeCompare(b.path));
|
|
58
|
+
}
|
|
59
|
+
/** Pega arquivos na ordem até estourar o orçamento; devolve também o que ficou de fora. */
|
|
60
|
+
export function withinBudget(files, budget) {
|
|
61
|
+
const kept = [];
|
|
62
|
+
const omitted = [];
|
|
63
|
+
let used = 0;
|
|
64
|
+
for (const f of files) {
|
|
65
|
+
if (used + f.tokens > budget && kept.length > 0)
|
|
66
|
+
omitted.push(f);
|
|
67
|
+
else {
|
|
68
|
+
kept.push(f);
|
|
69
|
+
used += f.tokens;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { kept, omitted };
|
|
73
|
+
}
|
|
18
74
|
/** Domínios disponíveis num projeto: os diretórios sob `memory/30-domains/`. */
|
|
19
75
|
export function domainsOf(projectRoot, env = defaultFsEnv) {
|
|
20
76
|
const dir = path.join(projectRoot, 'memory', '30-domains');
|
|
@@ -69,14 +125,16 @@ function moduleFiles(projectRoot, domain, env) {
|
|
|
69
125
|
}
|
|
70
126
|
/**
|
|
71
127
|
* Monta o pacote de um domínio. Por padrão só o mapa (`context.md`) — o contexto barato que substitui
|
|
72
|
-
* a exploração. Com `includeCode`, anexa o código da fatia — o que se lê ao *editar
|
|
128
|
+
* a exploração. Com `includeCode`, anexa o código da fatia — o que se lê ao *editar* — na ordem de
|
|
129
|
+
* relevância e até `budget` tokens; `full: true` anexa a fatia inteira (comportamento pré-SPEC-057,
|
|
130
|
+
* e o que o `token:economy` precisa para medir o custo de ler tudo).
|
|
73
131
|
*/
|
|
74
|
-
export function buildContextPack({ projectRoot, domain, includeCode = false }, env = defaultFsEnv) {
|
|
132
|
+
export function buildContextPack({ projectRoot, domain, includeCode = false, query = '', full = false, budget = CODE_TOKEN_BUDGET, }, env = defaultFsEnv) {
|
|
75
133
|
// Allowlist against the domains the project actually declares (memory/30-domains/*) — rejects early
|
|
76
134
|
// rather than sanitizing, since it's information this function already has and a traversal payload
|
|
77
135
|
// (e.g. "../../etc") can never literally equal one of those directory names.
|
|
78
136
|
if (!domainsOf(projectRoot, env).includes(domain)) {
|
|
79
|
-
return { domain, found: false, mapPath: null, map: null, mapTokens: 0, code: [], codeTokens: 0, totalTokens: 0 };
|
|
137
|
+
return { domain, found: false, mapPath: null, map: null, mapTokens: 0, code: [], codeTokens: 0, codeOmitted: [], totalTokens: 0 };
|
|
80
138
|
}
|
|
81
139
|
const mapPath = path.join(projectRoot, 'memory', '30-domains', domain, 'context.md');
|
|
82
140
|
let map = null;
|
|
@@ -87,7 +145,9 @@ export function buildContextPack({ projectRoot, domain, includeCode = false }, e
|
|
|
87
145
|
map = null;
|
|
88
146
|
}
|
|
89
147
|
const code = [];
|
|
148
|
+
let codeOmitted = [];
|
|
90
149
|
if (includeCode) {
|
|
150
|
+
const files = [];
|
|
91
151
|
for (const file of moduleFiles(projectRoot, domain, env)) {
|
|
92
152
|
let content = '';
|
|
93
153
|
try {
|
|
@@ -96,19 +156,24 @@ export function buildContextPack({ projectRoot, domain, includeCode = false }, e
|
|
|
96
156
|
catch {
|
|
97
157
|
continue;
|
|
98
158
|
}
|
|
99
|
-
|
|
159
|
+
files.push({ path: relFrom(projectRoot, file), tokens: estimateTokens(content), content });
|
|
100
160
|
}
|
|
161
|
+
const ordered = byRelevance(files, query.toLowerCase().split(/\s+/).filter(Boolean));
|
|
162
|
+
const cut = full ? { kept: ordered, omitted: [] } : withinBudget(ordered, budget);
|
|
163
|
+
code.push(...cut.kept);
|
|
164
|
+
codeOmitted = cut.omitted.map(({ path: rel, tokens }) => ({ path: rel, tokens }));
|
|
101
165
|
}
|
|
102
166
|
const mapTokens = map ? estimateTokens(map) : 0;
|
|
103
167
|
const codeTokens = code.reduce((acc, c) => acc + c.tokens, 0);
|
|
104
168
|
return {
|
|
105
169
|
domain,
|
|
106
170
|
found: map != null,
|
|
107
|
-
mapPath: map != null ?
|
|
171
|
+
mapPath: map != null ? relFrom(projectRoot, mapPath) : null,
|
|
108
172
|
map,
|
|
109
173
|
mapTokens,
|
|
110
174
|
code,
|
|
111
175
|
codeTokens,
|
|
176
|
+
codeOmitted,
|
|
112
177
|
totalTokens: mapTokens + codeTokens,
|
|
113
178
|
};
|
|
114
179
|
}
|
|
@@ -13,6 +13,40 @@
|
|
|
13
13
|
* `auth:jwt` são sintaxe FTS inválida e a query inteira lança — e o modo task voltava vazio.
|
|
14
14
|
*/
|
|
15
15
|
export declare function ftsQuery(keyword: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Nó curto entra inteiro (SPEC-057 AC-1). ~400 tokens na régua bytes/4 que o framework usa em todo
|
|
18
|
+
* lugar: abaixo disso, cortar economiza migalha e perde o contexto que dava sentido ao trecho.
|
|
19
|
+
*/
|
|
20
|
+
export declare const FULL_CONTENT_MAX_CHARS: number;
|
|
21
|
+
/** Raio do `snippet()` do FTS5 — em tokens, não em chars: ±24 tokens em volta de cada casamento. */
|
|
22
|
+
export declare const SNIPPET_RADIUS_TOKENS = 24;
|
|
23
|
+
interface MemoryNode {
|
|
24
|
+
title: string;
|
|
25
|
+
path: string;
|
|
26
|
+
content: string;
|
|
27
|
+
/** Trecho casado (FTS `snippet()` ou janela em volta do LIKE). Ausente → cabeça do nó. */
|
|
28
|
+
excerpt?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface BuildOptions {
|
|
31
|
+
/** `--full-context`: injeta o conteúdo integral de todo nó, sem corte (opt-out do AC-1). */
|
|
32
|
+
full?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/** Cabeça do nó cortada em fronteira de linha: corte no meio de uma linha parece arquivo corrompido. */
|
|
35
|
+
export declare function headExcerpt(content: string, max?: number): string;
|
|
36
|
+
/**
|
|
37
|
+
* Janela em volta da primeira ocorrência do termo — para os nós que o LIKE acha e o FTS não (o
|
|
38
|
+
* porter não casa substring no meio de palavra, e é justamente esse o caso que cai aqui). O raio é o
|
|
39
|
+
* mesmo do `snippet()` em chars (tokens × 4, a régua do repo): dois produtores de trecho, um orçamento.
|
|
40
|
+
*/
|
|
41
|
+
export declare function excerptAround(content: string, needle: string, radius?: number): string;
|
|
42
|
+
/**
|
|
43
|
+
* O que entra no pack por nó. Retorna também quantos chars ficaram de fora — a omissão é sempre
|
|
44
|
+
* visível, porque corte calado é o agente raciocinando sobre metade da regra e não sabendo.
|
|
45
|
+
*/
|
|
46
|
+
export declare function nodeBody(node: MemoryNode, opts?: BuildOptions): {
|
|
47
|
+
text: string;
|
|
48
|
+
omitted: number;
|
|
49
|
+
};
|
|
16
50
|
export declare class ContextBuilder {
|
|
17
51
|
projectPath: string;
|
|
18
52
|
dbPath: string;
|
|
@@ -36,7 +70,7 @@ export declare class ContextBuilder {
|
|
|
36
70
|
* @param {string} keyword - Palavra-chave para busca (modo 'task')
|
|
37
71
|
* @returns {string} Conteúdo do contexto em markdown
|
|
38
72
|
*/
|
|
39
|
-
build(mode: any, project: any, keyword?: string): string;
|
|
73
|
+
build(mode: any, project: any, keyword?: string, opts?: BuildOptions): string;
|
|
40
74
|
/**
|
|
41
75
|
* Obter contexto global (mission, standards, padrões)
|
|
42
76
|
*/
|
|
@@ -47,12 +81,23 @@ export declare class ContextBuilder {
|
|
|
47
81
|
_getDomainNodes(project: any): any;
|
|
48
82
|
/**
|
|
49
83
|
* Obter contexto orientado a tarefa (busca por FTS5)
|
|
84
|
+
*
|
|
85
|
+
* Duas consultas, não uma: a do FTS já devolve o trecho casado via `snippet()` — injetar o nó
|
|
86
|
+
* inteiro era o maior sorvedouro de token do pack (SPEC-057). O `LIKE` cobre o que o porter não
|
|
87
|
+
* casa (substring no meio de palavra) e ganha uma janela calculada aqui, porque fora do FTS não
|
|
88
|
+
* existe `snippet()` para destacar o casamento.
|
|
50
89
|
*/
|
|
51
|
-
_getTaskNodes(project: any, keyword: any):
|
|
90
|
+
_getTaskNodes(project: any, keyword: any, limit?: number): MemoryNode[];
|
|
91
|
+
_ftsTaskNodes(project: string, keyword: string, limit: number): MemoryNode[];
|
|
92
|
+
_likeTaskNodes(project: string, keyword: string, fts: string, limit: number): MemoryNode[];
|
|
52
93
|
/**
|
|
53
94
|
* Gerar markdown deduplicado
|
|
54
95
|
*/
|
|
55
|
-
_generateMarkdown(mode: any, project: any, keyword: any, nodes:
|
|
96
|
+
_generateMarkdown(mode: any, project: any, keyword: any, nodes: MemoryNode[], seenPaths: Set<string>, opts?: BuildOptions): {
|
|
97
|
+
markdown: string;
|
|
98
|
+
shown: number;
|
|
99
|
+
excerpted: number;
|
|
100
|
+
};
|
|
56
101
|
/**
|
|
57
102
|
* Salvar contexto em arquivo
|
|
58
103
|
*/
|
|
@@ -60,7 +105,7 @@ export declare class ContextBuilder {
|
|
|
60
105
|
/**
|
|
61
106
|
* Estatísticas de contexto
|
|
62
107
|
*/
|
|
63
|
-
stats(mode: any, project: any, keyword?: string): {
|
|
108
|
+
stats(mode: any, project: any, keyword?: string, opts?: BuildOptions): {
|
|
64
109
|
mode: any;
|
|
65
110
|
project: any;
|
|
66
111
|
keyword: string;
|