@nathapp/nax 0.50.2 → 0.51.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 +30 -0
- package/dist/nax.js +579 -373
- package/package.json +1 -3
- package/bin/nax.ts +0 -1195
- package/src/acceptance/fix-generator.ts +0 -322
- package/src/acceptance/generator.ts +0 -423
- package/src/acceptance/index.ts +0 -42
- package/src/acceptance/refinement.ts +0 -224
- package/src/acceptance/templates/cli.ts +0 -47
- package/src/acceptance/templates/component.ts +0 -78
- package/src/acceptance/templates/e2e.ts +0 -43
- package/src/acceptance/templates/index.ts +0 -21
- package/src/acceptance/templates/snapshot.ts +0 -50
- package/src/acceptance/templates/unit.ts +0 -48
- package/src/acceptance/types.ts +0 -135
- package/src/agents/acp/adapter.ts +0 -888
- package/src/agents/acp/cost.ts +0 -9
- package/src/agents/acp/index.ts +0 -7
- package/src/agents/acp/interaction-bridge.ts +0 -126
- package/src/agents/acp/parser.ts +0 -119
- package/src/agents/acp/spawn-client.ts +0 -373
- package/src/agents/acp/types.ts +0 -22
- package/src/agents/aider/adapter.ts +0 -135
- package/src/agents/claude/adapter.ts +0 -258
- package/src/agents/claude/complete.ts +0 -80
- package/src/agents/claude/cost.ts +0 -16
- package/src/agents/claude/execution.ts +0 -215
- package/src/agents/claude/index.ts +0 -3
- package/src/agents/claude/interactive.ts +0 -77
- package/src/agents/claude/plan.ts +0 -179
- package/src/agents/codex/adapter.ts +0 -153
- package/src/agents/cost/calculate.ts +0 -154
- package/src/agents/cost/index.ts +0 -10
- package/src/agents/cost/parse.ts +0 -97
- package/src/agents/cost/pricing.ts +0 -59
- package/src/agents/cost/types.ts +0 -45
- package/src/agents/gemini/adapter.ts +0 -177
- package/src/agents/index.ts +0 -18
- package/src/agents/opencode/adapter.ts +0 -106
- package/src/agents/registry.ts +0 -136
- package/src/agents/shared/decompose.ts +0 -154
- package/src/agents/shared/model-resolution.ts +0 -43
- package/src/agents/shared/types-extended.ts +0 -164
- package/src/agents/shared/validation.ts +0 -69
- package/src/agents/shared/version-detection.ts +0 -109
- package/src/agents/types.ts +0 -205
- package/src/analyze/classifier.ts +0 -282
- package/src/analyze/index.ts +0 -16
- package/src/analyze/scanner.ts +0 -171
- package/src/analyze/types.ts +0 -51
- package/src/cli/accept.ts +0 -108
- package/src/cli/agents.ts +0 -87
- package/src/cli/analyze-parser.ts +0 -291
- package/src/cli/analyze.ts +0 -352
- package/src/cli/config-descriptions.ts +0 -218
- package/src/cli/config-diff.ts +0 -103
- package/src/cli/config-display.ts +0 -285
- package/src/cli/config-get.ts +0 -55
- package/src/cli/config.ts +0 -14
- package/src/cli/constitution.ts +0 -17
- package/src/cli/diagnose-analysis.ts +0 -159
- package/src/cli/diagnose-formatter.ts +0 -87
- package/src/cli/diagnose.ts +0 -203
- package/src/cli/generate.ts +0 -250
- package/src/cli/index.ts +0 -42
- package/src/cli/init-context.ts +0 -405
- package/src/cli/init-detect.ts +0 -303
- package/src/cli/init.ts +0 -296
- package/src/cli/interact.ts +0 -295
- package/src/cli/plan.ts +0 -509
- package/src/cli/plugins.ts +0 -122
- package/src/cli/prompts-export.ts +0 -58
- package/src/cli/prompts-init.ts +0 -200
- package/src/cli/prompts-main.ts +0 -183
- package/src/cli/prompts-shared.ts +0 -70
- package/src/cli/prompts-tdd.ts +0 -88
- package/src/cli/prompts.ts +0 -17
- package/src/cli/runs.ts +0 -174
- package/src/cli/status-cost.ts +0 -151
- package/src/cli/status-features.ts +0 -405
- package/src/cli/status.ts +0 -13
- package/src/commands/common.ts +0 -171
- package/src/commands/diagnose.ts +0 -17
- package/src/commands/index.ts +0 -9
- package/src/commands/logs-formatter.ts +0 -201
- package/src/commands/logs-reader.ts +0 -171
- package/src/commands/logs.ts +0 -103
- package/src/commands/precheck.ts +0 -86
- package/src/commands/runs.ts +0 -220
- package/src/commands/unlock.ts +0 -96
- package/src/config/defaults.ts +0 -217
- package/src/config/index.ts +0 -22
- package/src/config/loader.ts +0 -143
- package/src/config/merge.ts +0 -106
- package/src/config/merger.ts +0 -147
- package/src/config/path-security.ts +0 -121
- package/src/config/paths.ts +0 -27
- package/src/config/permissions.ts +0 -63
- package/src/config/runtime-types.ts +0 -520
- package/src/config/schema-types.ts +0 -53
- package/src/config/schema.ts +0 -60
- package/src/config/schemas.ts +0 -425
- package/src/config/test-strategy.ts +0 -71
- package/src/config/types.ts +0 -57
- package/src/config/validate.ts +0 -103
- package/src/constitution/generator.ts +0 -158
- package/src/constitution/generators/aider.ts +0 -41
- package/src/constitution/generators/claude.ts +0 -35
- package/src/constitution/generators/cursor.ts +0 -36
- package/src/constitution/generators/opencode.ts +0 -38
- package/src/constitution/generators/types.ts +0 -33
- package/src/constitution/generators/windsurf.ts +0 -36
- package/src/constitution/index.ts +0 -11
- package/src/constitution/loader.ts +0 -121
- package/src/constitution/types.ts +0 -31
- package/src/context/auto-detect.ts +0 -228
- package/src/context/builder.ts +0 -299
- package/src/context/elements.ts +0 -122
- package/src/context/formatter.ts +0 -107
- package/src/context/generator.ts +0 -343
- package/src/context/generators/aider.ts +0 -34
- package/src/context/generators/claude.ts +0 -28
- package/src/context/generators/codex.ts +0 -28
- package/src/context/generators/cursor.ts +0 -28
- package/src/context/generators/gemini.ts +0 -28
- package/src/context/generators/opencode.ts +0 -30
- package/src/context/generators/windsurf.ts +0 -28
- package/src/context/greenfield.ts +0 -114
- package/src/context/index.ts +0 -34
- package/src/context/injector.ts +0 -279
- package/src/context/parent-context.ts +0 -39
- package/src/context/test-scanner.ts +0 -370
- package/src/context/types.ts +0 -98
- package/src/decompose/apply.ts +0 -50
- package/src/decompose/builder.ts +0 -181
- package/src/decompose/index.ts +0 -8
- package/src/decompose/sections/codebase.ts +0 -26
- package/src/decompose/sections/constraints.ts +0 -32
- package/src/decompose/sections/index.ts +0 -4
- package/src/decompose/sections/sibling-stories.ts +0 -25
- package/src/decompose/sections/target-story.ts +0 -31
- package/src/decompose/types.ts +0 -55
- package/src/decompose/validators/complexity.ts +0 -45
- package/src/decompose/validators/coverage.ts +0 -134
- package/src/decompose/validators/dependency.ts +0 -91
- package/src/decompose/validators/index.ts +0 -35
- package/src/decompose/validators/overlap.ts +0 -128
- package/src/errors.ts +0 -67
- package/src/execution/batching.ts +0 -157
- package/src/execution/crash-heartbeat.ts +0 -77
- package/src/execution/crash-recovery.ts +0 -79
- package/src/execution/crash-signals.ts +0 -165
- package/src/execution/crash-writer.ts +0 -154
- package/src/execution/deferred-review.ts +0 -105
- package/src/execution/dry-run.ts +0 -81
- package/src/execution/escalation/escalation.ts +0 -46
- package/src/execution/escalation/index.ts +0 -13
- package/src/execution/escalation/tier-escalation.ts +0 -346
- package/src/execution/escalation/tier-outcome.ts +0 -143
- package/src/execution/executor-types.ts +0 -73
- package/src/execution/helpers.ts +0 -38
- package/src/execution/index.ts +0 -27
- package/src/execution/iteration-runner.ts +0 -160
- package/src/execution/lifecycle/acceptance-loop.ts +0 -280
- package/src/execution/lifecycle/headless-formatter.ts +0 -83
- package/src/execution/lifecycle/index.ts +0 -11
- package/src/execution/lifecycle/parallel-lifecycle.ts +0 -101
- package/src/execution/lifecycle/precheck-runner.ts +0 -140
- package/src/execution/lifecycle/run-cleanup.ts +0 -81
- package/src/execution/lifecycle/run-completion.ts +0 -247
- package/src/execution/lifecycle/run-initialization.ts +0 -187
- package/src/execution/lifecycle/run-regression.ts +0 -305
- package/src/execution/lifecycle/run-setup.ts +0 -240
- package/src/execution/lifecycle/story-size-prompts.ts +0 -123
- package/src/execution/lock.ts +0 -129
- package/src/execution/parallel-coordinator.ts +0 -281
- package/src/execution/parallel-executor-rectification-pass.ts +0 -117
- package/src/execution/parallel-executor-rectify.ts +0 -136
- package/src/execution/parallel-executor.ts +0 -330
- package/src/execution/parallel-worker.ts +0 -149
- package/src/execution/parallel.ts +0 -13
- package/src/execution/pid-registry.ts +0 -275
- package/src/execution/pipeline-result-handler.ts +0 -221
- package/src/execution/progress.ts +0 -27
- package/src/execution/queue-handler.ts +0 -109
- package/src/execution/runner-completion.ts +0 -171
- package/src/execution/runner-execution.ts +0 -243
- package/src/execution/runner-setup.ts +0 -86
- package/src/execution/runner.ts +0 -265
- package/src/execution/sequential-executor.ts +0 -219
- package/src/execution/status-file.ts +0 -264
- package/src/execution/status-writer.ts +0 -181
- package/src/execution/story-context.ts +0 -266
- package/src/execution/story-selector.ts +0 -76
- package/src/execution/test-output-parser.ts +0 -14
- package/src/execution/timeout-handler.ts +0 -100
- package/src/hooks/index.ts +0 -2
- package/src/hooks/runner.ts +0 -280
- package/src/hooks/types.ts +0 -79
- package/src/interaction/chain.ts +0 -170
- package/src/interaction/index.ts +0 -61
- package/src/interaction/init.ts +0 -84
- package/src/interaction/plugins/auto.ts +0 -243
- package/src/interaction/plugins/cli.ts +0 -300
- package/src/interaction/plugins/telegram.ts +0 -384
- package/src/interaction/plugins/webhook.ts +0 -286
- package/src/interaction/state.ts +0 -171
- package/src/interaction/triggers.ts +0 -250
- package/src/interaction/types.ts +0 -170
- package/src/logger/formatters.ts +0 -84
- package/src/logger/index.ts +0 -16
- package/src/logger/logger.ts +0 -296
- package/src/logger/types.ts +0 -48
- package/src/logging/formatter.ts +0 -355
- package/src/logging/index.ts +0 -22
- package/src/logging/types.ts +0 -93
- package/src/metrics/aggregator.ts +0 -191
- package/src/metrics/index.ts +0 -14
- package/src/metrics/tracker.ts +0 -200
- package/src/metrics/types.ts +0 -115
- package/src/optimizer/index.ts +0 -63
- package/src/optimizer/noop.optimizer.ts +0 -24
- package/src/optimizer/rule-based.optimizer.ts +0 -248
- package/src/optimizer/types.ts +0 -53
- package/src/pipeline/event-bus.ts +0 -297
- package/src/pipeline/events.ts +0 -130
- package/src/pipeline/index.ts +0 -19
- package/src/pipeline/runner.ts +0 -149
- package/src/pipeline/stages/acceptance-setup.ts +0 -140
- package/src/pipeline/stages/acceptance.ts +0 -215
- package/src/pipeline/stages/autofix.ts +0 -262
- package/src/pipeline/stages/completion.ts +0 -110
- package/src/pipeline/stages/constitution.ts +0 -63
- package/src/pipeline/stages/context.ts +0 -122
- package/src/pipeline/stages/execution.ts +0 -359
- package/src/pipeline/stages/index.ts +0 -86
- package/src/pipeline/stages/optimizer.ts +0 -74
- package/src/pipeline/stages/prompt.ts +0 -79
- package/src/pipeline/stages/queue-check.ts +0 -103
- package/src/pipeline/stages/rectify.ts +0 -101
- package/src/pipeline/stages/regression.ts +0 -99
- package/src/pipeline/stages/review.ts +0 -94
- package/src/pipeline/stages/routing.ts +0 -276
- package/src/pipeline/stages/verify.ts +0 -286
- package/src/pipeline/subscribers/events-writer.ts +0 -135
- package/src/pipeline/subscribers/hooks.ts +0 -179
- package/src/pipeline/subscribers/interaction.ts +0 -103
- package/src/pipeline/subscribers/registry.ts +0 -73
- package/src/pipeline/subscribers/reporters.ts +0 -174
- package/src/pipeline/types.ts +0 -220
- package/src/plugins/extensions.ts +0 -225
- package/src/plugins/index.ts +0 -33
- package/src/plugins/loader.ts +0 -352
- package/src/plugins/plugin-logger.ts +0 -41
- package/src/plugins/registry.ts +0 -168
- package/src/plugins/types.ts +0 -206
- package/src/plugins/validator.ts +0 -352
- package/src/prd/index.ts +0 -220
- package/src/prd/schema.ts +0 -268
- package/src/prd/types.ts +0 -273
- package/src/prd/validate.ts +0 -41
- package/src/precheck/checks-agents.ts +0 -63
- package/src/precheck/checks-blockers.ts +0 -23
- package/src/precheck/checks-cli.ts +0 -68
- package/src/precheck/checks-config.ts +0 -102
- package/src/precheck/checks-git.ts +0 -117
- package/src/precheck/checks-system.ts +0 -101
- package/src/precheck/checks-warnings.ts +0 -221
- package/src/precheck/checks.ts +0 -36
- package/src/precheck/index.ts +0 -374
- package/src/precheck/story-size-gate.ts +0 -144
- package/src/precheck/types.ts +0 -31
- package/src/prompts/builder.ts +0 -166
- package/src/prompts/index.ts +0 -2
- package/src/prompts/loader.ts +0 -43
- package/src/prompts/sections/conventions.ts +0 -19
- package/src/prompts/sections/hermetic.ts +0 -41
- package/src/prompts/sections/index.ts +0 -12
- package/src/prompts/sections/isolation.ts +0 -70
- package/src/prompts/sections/role-task.ts +0 -182
- package/src/prompts/sections/story.ts +0 -55
- package/src/prompts/sections/verdict.ts +0 -70
- package/src/prompts/types.ts +0 -21
- package/src/queue/index.ts +0 -2
- package/src/queue/manager.ts +0 -254
- package/src/queue/types.ts +0 -54
- package/src/review/index.ts +0 -8
- package/src/review/orchestrator.ts +0 -154
- package/src/review/runner.ts +0 -303
- package/src/review/types.ts +0 -70
- package/src/routing/batch-route.ts +0 -35
- package/src/routing/builder.ts +0 -81
- package/src/routing/chain.ts +0 -75
- package/src/routing/content-hash.ts +0 -25
- package/src/routing/index.ts +0 -20
- package/src/routing/loader.ts +0 -62
- package/src/routing/router.ts +0 -305
- package/src/routing/strategies/adaptive.ts +0 -215
- package/src/routing/strategies/index.ts +0 -8
- package/src/routing/strategies/keyword.ts +0 -180
- package/src/routing/strategies/llm-prompts.ts +0 -224
- package/src/routing/strategies/llm.ts +0 -320
- package/src/routing/strategies/manual.ts +0 -50
- package/src/routing/strategy.ts +0 -102
- package/src/tdd/cleanup.ts +0 -120
- package/src/tdd/index.ts +0 -22
- package/src/tdd/isolation.ts +0 -117
- package/src/tdd/orchestrator.ts +0 -406
- package/src/tdd/prompts.ts +0 -40
- package/src/tdd/rectification-gate.ts +0 -274
- package/src/tdd/session-runner.ts +0 -263
- package/src/tdd/types.ts +0 -84
- package/src/tdd/verdict-reader.ts +0 -266
- package/src/tdd/verdict.ts +0 -152
- package/src/tui/App.tsx +0 -265
- package/src/tui/components/AgentPanel.tsx +0 -75
- package/src/tui/components/CostOverlay.tsx +0 -118
- package/src/tui/components/HelpOverlay.tsx +0 -107
- package/src/tui/components/StatusBar.tsx +0 -63
- package/src/tui/components/StoriesPanel.tsx +0 -177
- package/src/tui/hooks/useKeyboard.ts +0 -142
- package/src/tui/hooks/useLayout.ts +0 -137
- package/src/tui/hooks/usePipelineEvents.ts +0 -183
- package/src/tui/hooks/usePty.ts +0 -189
- package/src/tui/index.tsx +0 -38
- package/src/tui/types.ts +0 -76
- package/src/utils/errors.ts +0 -12
- package/src/utils/git.ts +0 -245
- package/src/utils/json-file.ts +0 -72
- package/src/utils/log-test-output.ts +0 -25
- package/src/utils/path-security.ts +0 -73
- package/src/utils/queue-writer.ts +0 -54
- package/src/verification/crash-detector.ts +0 -34
- package/src/verification/executor.ts +0 -250
- package/src/verification/index.ts +0 -12
- package/src/verification/orchestrator-types.ts +0 -154
- package/src/verification/orchestrator.ts +0 -76
- package/src/verification/parser.ts +0 -220
- package/src/verification/rectification-loop.ts +0 -172
- package/src/verification/rectification.ts +0 -108
- package/src/verification/runners.ts +0 -129
- package/src/verification/smart-runner.ts +0 -307
- package/src/verification/strategies/acceptance.ts +0 -136
- package/src/verification/strategies/regression.ts +0 -90
- package/src/verification/strategies/scoped.ts +0 -154
- package/src/verification/types.ts +0 -117
- package/src/version.ts +0 -40
- package/src/worktree/dispatcher.ts +0 -6
- package/src/worktree/index.ts +0 -2
- package/src/worktree/manager.ts +0 -193
- package/src/worktree/merge.ts +0 -302
- package/src/worktree/types.ts +0 -4
package/src/metrics/tracker.ts
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Metrics Tracker
|
|
3
|
-
*
|
|
4
|
-
* Collects and persists per-story and per-run metrics.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import path from "node:path";
|
|
8
|
-
import { resolveModel } from "../config/schema";
|
|
9
|
-
import type { PipelineContext } from "../pipeline/types";
|
|
10
|
-
import { loadJsonFile, saveJsonFile } from "../utils/json-file";
|
|
11
|
-
import type { RunMetrics, StoryMetrics } from "./types";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Collect metrics for a single story execution.
|
|
15
|
-
*
|
|
16
|
-
* Extracts timing, cost, model usage, and escalation data from the pipeline context.
|
|
17
|
-
*
|
|
18
|
-
* @param ctx - Pipeline context with execution results
|
|
19
|
-
* @param storyStartTime - Story start timestamp (ISO string)
|
|
20
|
-
* @returns Story metrics object
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```ts
|
|
24
|
-
* const metrics = collectStoryMetrics(ctx, "2026-02-17T10:30:00.000Z");
|
|
25
|
-
* // {
|
|
26
|
-
* // storyId: "US-001",
|
|
27
|
-
* // complexity: "medium",
|
|
28
|
-
* // modelTier: "balanced",
|
|
29
|
-
* // modelUsed: "claude-sonnet-4.5",
|
|
30
|
-
* // attempts: 1,
|
|
31
|
-
* // finalTier: "balanced",
|
|
32
|
-
* // success: true,
|
|
33
|
-
* // cost: 0.0234,
|
|
34
|
-
* // durationMs: 45000,
|
|
35
|
-
* // firstPassSuccess: true,
|
|
36
|
-
* // ...
|
|
37
|
-
* // }
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export function collectStoryMetrics(ctx: PipelineContext, storyStartTime: string): StoryMetrics {
|
|
41
|
-
const story = ctx.story;
|
|
42
|
-
const routing = ctx.routing;
|
|
43
|
-
const agentResult = ctx.agentResult;
|
|
44
|
-
|
|
45
|
-
// Calculate attempts (initial + escalations)
|
|
46
|
-
// BUG-067: priorFailures captures cross-tier attempts that story.escalations never records
|
|
47
|
-
const escalationCount = story.escalations?.length || 0;
|
|
48
|
-
const priorFailureCount = story.priorFailures?.length || 0;
|
|
49
|
-
const attempts = priorFailureCount + Math.max(1, story.attempts || 1);
|
|
50
|
-
|
|
51
|
-
// Determine final tier (from last escalation or initial routing)
|
|
52
|
-
const finalTier = escalationCount > 0 ? story.escalations[escalationCount - 1].toTier : routing.modelTier;
|
|
53
|
-
|
|
54
|
-
// First pass success = succeeded with no prior failures and no escalations (BUG-067)
|
|
55
|
-
const firstPassSuccess = agentResult?.success === true && escalationCount === 0 && priorFailureCount === 0;
|
|
56
|
-
|
|
57
|
-
// Extract model name from config
|
|
58
|
-
const modelEntry = ctx.config.models[routing.modelTier];
|
|
59
|
-
const modelDef = modelEntry ? resolveModel(modelEntry) : null;
|
|
60
|
-
const modelUsed = modelDef?.model || routing.modelTier;
|
|
61
|
-
|
|
62
|
-
// initialComplexity: prefer story.routing.initialComplexity (first classify),
|
|
63
|
-
// fall back to routing.complexity for backward compat
|
|
64
|
-
const initialComplexity = story.routing?.initialComplexity ?? routing.complexity;
|
|
65
|
-
|
|
66
|
-
// fullSuiteGatePassed: true only for TDD strategies when gate passes
|
|
67
|
-
const isTddStrategy =
|
|
68
|
-
routing.testStrategy === "three-session-tdd" || routing.testStrategy === "three-session-tdd-lite";
|
|
69
|
-
const fullSuiteGatePassed = isTddStrategy ? (ctx.fullSuiteGatePassed ?? false) : false;
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
storyId: story.id,
|
|
73
|
-
complexity: routing.complexity,
|
|
74
|
-
initialComplexity,
|
|
75
|
-
modelTier: routing.modelTier,
|
|
76
|
-
modelUsed,
|
|
77
|
-
attempts,
|
|
78
|
-
finalTier,
|
|
79
|
-
success: agentResult?.success || false,
|
|
80
|
-
cost: (ctx.accumulatedAttemptCost ?? 0) + (agentResult?.estimatedCost || 0),
|
|
81
|
-
durationMs: agentResult?.durationMs || 0,
|
|
82
|
-
firstPassSuccess,
|
|
83
|
-
startedAt: storyStartTime,
|
|
84
|
-
completedAt: new Date().toISOString(),
|
|
85
|
-
fullSuiteGatePassed,
|
|
86
|
-
runtimeCrashes: ctx.storyRuntimeCrashes ?? 0,
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Collect metrics for a batch of stories.
|
|
92
|
-
*
|
|
93
|
-
* Creates individual story metrics for each story in the batch,
|
|
94
|
-
* distributing the total cost and duration proportionally.
|
|
95
|
-
*
|
|
96
|
-
* @param ctx - Pipeline context with batch execution results
|
|
97
|
-
* @param storyStartTime - Batch start timestamp (ISO string)
|
|
98
|
-
* @returns Array of story metrics (one per story in batch)
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```ts
|
|
102
|
-
* const batchMetrics = collectBatchMetrics(ctx, "2026-02-17T10:30:00.000Z");
|
|
103
|
-
* // [
|
|
104
|
-
* // { storyId: "US-001", cost: 0.0078, ... },
|
|
105
|
-
* // { storyId: "US-002", cost: 0.0078, ... },
|
|
106
|
-
* // { storyId: "US-003", cost: 0.0078, ... },
|
|
107
|
-
* // ]
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
export function collectBatchMetrics(ctx: PipelineContext, storyStartTime: string): StoryMetrics[] {
|
|
111
|
-
const stories = ctx.stories;
|
|
112
|
-
const routing = ctx.routing;
|
|
113
|
-
const agentResult = ctx.agentResult;
|
|
114
|
-
|
|
115
|
-
const totalCost = agentResult?.estimatedCost || 0;
|
|
116
|
-
const totalDuration = agentResult?.durationMs || 0;
|
|
117
|
-
const costPerStory = totalCost / stories.length;
|
|
118
|
-
const durationPerStory = totalDuration / stories.length;
|
|
119
|
-
|
|
120
|
-
const modelEntry = ctx.config.models[routing.modelTier];
|
|
121
|
-
const modelDef = modelEntry ? resolveModel(modelEntry) : null;
|
|
122
|
-
const modelUsed = modelDef?.model || routing.modelTier;
|
|
123
|
-
|
|
124
|
-
return stories.map((story) => {
|
|
125
|
-
// initialComplexity: prefer story.routing.initialComplexity (if individual routing exists),
|
|
126
|
-
// fall back to shared routing.complexity (batch stories classified together)
|
|
127
|
-
const initialComplexity = story.routing?.initialComplexity ?? routing.complexity;
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
storyId: story.id,
|
|
131
|
-
complexity: routing.complexity,
|
|
132
|
-
initialComplexity,
|
|
133
|
-
modelTier: routing.modelTier,
|
|
134
|
-
modelUsed,
|
|
135
|
-
attempts: 1, // batch stories don't escalate individually
|
|
136
|
-
finalTier: routing.modelTier,
|
|
137
|
-
success: true, // if batch succeeded, all stories succeeded
|
|
138
|
-
cost: costPerStory,
|
|
139
|
-
durationMs: durationPerStory,
|
|
140
|
-
firstPassSuccess: true, // batch = first pass success
|
|
141
|
-
startedAt: storyStartTime,
|
|
142
|
-
completedAt: new Date().toISOString(),
|
|
143
|
-
fullSuiteGatePassed: false, // batches are not TDD-gated
|
|
144
|
-
runtimeCrashes: 0, // batch stories don't have individual crash tracking
|
|
145
|
-
};
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Save run metrics to nax/metrics.json.
|
|
151
|
-
*
|
|
152
|
-
* Appends the run metrics to the existing metrics file (or creates it if missing).
|
|
153
|
-
* Each run is a separate entry in the JSON array.
|
|
154
|
-
*
|
|
155
|
-
* @param workdir - Project root directory
|
|
156
|
-
* @param runMetrics - Run metrics to persist
|
|
157
|
-
*
|
|
158
|
-
* @example
|
|
159
|
-
* ```ts
|
|
160
|
-
* await saveRunMetrics("/home/user/project", {
|
|
161
|
-
* runId: "run-20260217-103045",
|
|
162
|
-
* feature: "auth-system",
|
|
163
|
-
* totalCost: 0.1234,
|
|
164
|
-
* stories: [...],
|
|
165
|
-
* ...
|
|
166
|
-
* });
|
|
167
|
-
* ```
|
|
168
|
-
*/
|
|
169
|
-
export async function saveRunMetrics(workdir: string, runMetrics: RunMetrics): Promise<void> {
|
|
170
|
-
const metricsPath = path.join(workdir, "nax", "metrics.json");
|
|
171
|
-
|
|
172
|
-
// Load existing metrics (returns empty array if file doesn't exist or is invalid)
|
|
173
|
-
const existing = await loadJsonFile<RunMetrics[]>(metricsPath, "metrics");
|
|
174
|
-
const allMetrics = Array.isArray(existing) ? existing : [];
|
|
175
|
-
|
|
176
|
-
// Append new run
|
|
177
|
-
allMetrics.push(runMetrics);
|
|
178
|
-
|
|
179
|
-
// Write back
|
|
180
|
-
await saveJsonFile(metricsPath, allMetrics, "metrics");
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Load all run metrics from nax/metrics.json.
|
|
185
|
-
*
|
|
186
|
-
* @param workdir - Project root directory
|
|
187
|
-
* @returns Array of run metrics, or empty array if file doesn't exist
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* ```ts
|
|
191
|
-
* const runs = await loadRunMetrics("/home/user/project");
|
|
192
|
-
* console.log(`Total runs: ${runs.length}`);
|
|
193
|
-
* ```
|
|
194
|
-
*/
|
|
195
|
-
export async function loadRunMetrics(workdir: string): Promise<RunMetrics[]> {
|
|
196
|
-
const metricsPath = path.join(workdir, "nax", "metrics.json");
|
|
197
|
-
|
|
198
|
-
const content = await loadJsonFile<RunMetrics[]>(metricsPath, "metrics");
|
|
199
|
-
return Array.isArray(content) ? content : [];
|
|
200
|
-
}
|
package/src/metrics/types.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Metrics Tracking Types
|
|
3
|
-
*
|
|
4
|
-
* Structured cost and performance metrics for tracking agent execution.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Per-story execution metrics
|
|
9
|
-
*/
|
|
10
|
-
export interface StoryMetrics {
|
|
11
|
-
/** Story ID */
|
|
12
|
-
storyId: string;
|
|
13
|
-
/** Classified complexity */
|
|
14
|
-
complexity: string;
|
|
15
|
-
/** Initial complexity from first classification — preserved across escalations */
|
|
16
|
-
initialComplexity?: string;
|
|
17
|
-
/** Initial model tier */
|
|
18
|
-
modelTier: string;
|
|
19
|
-
/** Actual model used (e.g., "claude-sonnet-4.5") */
|
|
20
|
-
modelUsed: string;
|
|
21
|
-
/** Number of attempts (includes escalations) */
|
|
22
|
-
attempts: number;
|
|
23
|
-
/** Final tier that succeeded */
|
|
24
|
-
finalTier: string;
|
|
25
|
-
/** Whether the story succeeded */
|
|
26
|
-
success: boolean;
|
|
27
|
-
/** Total cost for this story (including all attempts) */
|
|
28
|
-
cost: number;
|
|
29
|
-
/** Total duration in milliseconds */
|
|
30
|
-
durationMs: number;
|
|
31
|
-
/** Whether it passed on the first attempt */
|
|
32
|
-
firstPassSuccess: boolean;
|
|
33
|
-
/** Timestamp when started */
|
|
34
|
-
startedAt: string;
|
|
35
|
-
/** Timestamp when completed */
|
|
36
|
-
completedAt: string;
|
|
37
|
-
/** Number of runtime crashes (RUNTIME_CRASH verify status) encountered for this story (BUG-070) */
|
|
38
|
-
runtimeCrashes?: number;
|
|
39
|
-
/** Whether TDD full-suite gate passed (only true for TDD strategies when gate passes) */
|
|
40
|
-
fullSuiteGatePassed?: boolean;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Per-run execution metrics
|
|
45
|
-
*/
|
|
46
|
-
export interface RunMetrics {
|
|
47
|
-
/** Unique run ID */
|
|
48
|
-
runId: string;
|
|
49
|
-
/** Feature name */
|
|
50
|
-
feature: string;
|
|
51
|
-
/** Run start timestamp */
|
|
52
|
-
startedAt: string;
|
|
53
|
-
/** Run completion timestamp */
|
|
54
|
-
completedAt: string;
|
|
55
|
-
/** Total cost for the run */
|
|
56
|
-
totalCost: number;
|
|
57
|
-
/** Total number of stories in the run */
|
|
58
|
-
totalStories: number;
|
|
59
|
-
/** Number of stories completed successfully */
|
|
60
|
-
storiesCompleted: number;
|
|
61
|
-
/** Number of stories that failed */
|
|
62
|
-
storiesFailed: number;
|
|
63
|
-
/** Total duration in milliseconds */
|
|
64
|
-
totalDurationMs: number;
|
|
65
|
-
/** Per-story metrics */
|
|
66
|
-
stories: StoryMetrics[];
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Aggregate metrics across all runs
|
|
71
|
-
*/
|
|
72
|
-
export interface AggregateMetrics {
|
|
73
|
-
/** Total number of runs */
|
|
74
|
-
totalRuns: number;
|
|
75
|
-
/** Total cost across all runs */
|
|
76
|
-
totalCost: number;
|
|
77
|
-
/** Total stories across all runs */
|
|
78
|
-
totalStories: number;
|
|
79
|
-
/** Percentage of stories passing on first attempt */
|
|
80
|
-
firstPassRate: number;
|
|
81
|
-
/** Percentage of stories needing escalation */
|
|
82
|
-
escalationRate: number;
|
|
83
|
-
/** Average cost per story */
|
|
84
|
-
avgCostPerStory: number;
|
|
85
|
-
/** Average cost per feature run */
|
|
86
|
-
avgCostPerFeature: number;
|
|
87
|
-
/** Per-model efficiency metrics */
|
|
88
|
-
modelEfficiency: Record<
|
|
89
|
-
string,
|
|
90
|
-
{
|
|
91
|
-
/** Total attempts with this model */
|
|
92
|
-
attempts: number;
|
|
93
|
-
/** Successful attempts */
|
|
94
|
-
successes: number;
|
|
95
|
-
/** Success rate (0-1) */
|
|
96
|
-
passRate: number;
|
|
97
|
-
/** Average cost per story */
|
|
98
|
-
avgCost: number;
|
|
99
|
-
/** Total cost for this model */
|
|
100
|
-
totalCost: number;
|
|
101
|
-
}
|
|
102
|
-
>;
|
|
103
|
-
/** Complexity prediction accuracy */
|
|
104
|
-
complexityAccuracy: Record<
|
|
105
|
-
string,
|
|
106
|
-
{
|
|
107
|
-
/** Number of stories predicted at this complexity */
|
|
108
|
-
predicted: number;
|
|
109
|
-
/** Most common final tier used */
|
|
110
|
-
actualTierUsed: string;
|
|
111
|
-
/** Rate at which prediction didn't match actual tier needed */
|
|
112
|
-
mismatchRate: number;
|
|
113
|
-
}
|
|
114
|
-
>;
|
|
115
|
-
}
|
package/src/optimizer/index.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Prompt Optimizer
|
|
3
|
-
*
|
|
4
|
-
* Exports optimizer types, implementations, and factory function.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
export type {
|
|
8
|
-
IPromptOptimizer,
|
|
9
|
-
PromptOptimizerInput,
|
|
10
|
-
PromptOptimizerResult,
|
|
11
|
-
} from "./types.js";
|
|
12
|
-
export { estimateTokens } from "./types.js";
|
|
13
|
-
export { NoopOptimizer } from "./noop.optimizer.js";
|
|
14
|
-
export { RuleBasedOptimizer } from "./rule-based.optimizer.js";
|
|
15
|
-
|
|
16
|
-
import type { NaxConfig } from "../config/schema.js";
|
|
17
|
-
import { getSafeLogger } from "../logger/index.js";
|
|
18
|
-
import type { PluginRegistry } from "../plugins/registry.js";
|
|
19
|
-
import { NoopOptimizer } from "./noop.optimizer.js";
|
|
20
|
-
import { RuleBasedOptimizer } from "./rule-based.optimizer.js";
|
|
21
|
-
import type { IPromptOptimizer } from "./types.js";
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Resolve the prompt optimizer to use for this run.
|
|
25
|
-
*
|
|
26
|
-
* Resolution order:
|
|
27
|
-
* 1. Plugin-provided optimizer (if any plugins provide "optimizer")
|
|
28
|
-
* 2. Built-in strategy from config (rule-based, noop)
|
|
29
|
-
* 3. Fallback to NoopOptimizer
|
|
30
|
-
*
|
|
31
|
-
* @param config - Nax configuration
|
|
32
|
-
* @param pluginRegistry - Plugin registry (optional, for plugin-provided optimizers)
|
|
33
|
-
* @returns Resolved optimizer instance
|
|
34
|
-
*/
|
|
35
|
-
export function resolveOptimizer(config: NaxConfig, pluginRegistry?: PluginRegistry): IPromptOptimizer {
|
|
36
|
-
// Check if optimizer is disabled
|
|
37
|
-
if (!config.optimizer?.enabled) {
|
|
38
|
-
return new NoopOptimizer();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// 1. Check plugin registry first
|
|
42
|
-
if (pluginRegistry) {
|
|
43
|
-
const pluginOptimizers = pluginRegistry.getOptimizers();
|
|
44
|
-
if (pluginOptimizers.length > 0) {
|
|
45
|
-
// Use first plugin optimizer (plugin optimizers use the same interface)
|
|
46
|
-
return pluginOptimizers[0];
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// 2. Use built-in strategy from config
|
|
51
|
-
const strategy = config.optimizer.strategy ?? "noop";
|
|
52
|
-
|
|
53
|
-
switch (strategy) {
|
|
54
|
-
case "rule-based":
|
|
55
|
-
return new RuleBasedOptimizer();
|
|
56
|
-
case "noop":
|
|
57
|
-
return new NoopOptimizer();
|
|
58
|
-
default:
|
|
59
|
-
// Unknown strategy, fallback to noop
|
|
60
|
-
getSafeLogger()?.warn("optimizer", `Unknown optimizer strategy '${strategy}', using noop`);
|
|
61
|
-
return new NoopOptimizer();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { IPromptOptimizer, PromptOptimizerInput, PromptOptimizerResult } from "./types.js";
|
|
2
|
-
import { estimateTokens } from "./types.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Passthrough optimizer that returns the prompt unchanged.
|
|
6
|
-
*
|
|
7
|
-
* Used as the default when optimization is disabled or
|
|
8
|
-
* when the configured strategy is unrecognized.
|
|
9
|
-
*/
|
|
10
|
-
export class NoopOptimizer implements IPromptOptimizer {
|
|
11
|
-
public readonly name = "noop";
|
|
12
|
-
|
|
13
|
-
async optimize(input: PromptOptimizerInput): Promise<PromptOptimizerResult> {
|
|
14
|
-
const tokens = estimateTokens(input.prompt);
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
prompt: input.prompt,
|
|
18
|
-
originalTokens: tokens,
|
|
19
|
-
optimizedTokens: tokens,
|
|
20
|
-
savings: 0,
|
|
21
|
-
appliedRules: [],
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import type { IPromptOptimizer, PromptOptimizerInput, PromptOptimizerResult } from "./types.js";
|
|
2
|
-
import { estimateTokens } from "./types.js";
|
|
3
|
-
|
|
4
|
-
interface RuleBasedConfig {
|
|
5
|
-
stripWhitespace?: boolean;
|
|
6
|
-
compactCriteria?: boolean;
|
|
7
|
-
deduplicateContext?: boolean;
|
|
8
|
-
maxPromptTokens?: number;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const DEFAULT_CONFIG: RuleBasedConfig = {
|
|
12
|
-
stripWhitespace: true,
|
|
13
|
-
compactCriteria: true,
|
|
14
|
-
deduplicateContext: true,
|
|
15
|
-
maxPromptTokens: 8000,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Rule-based optimizer that applies deterministic transformations
|
|
20
|
-
* to reduce token usage without external dependencies.
|
|
21
|
-
*/
|
|
22
|
-
export class RuleBasedOptimizer implements IPromptOptimizer {
|
|
23
|
-
public readonly name = "rule-based";
|
|
24
|
-
|
|
25
|
-
async optimize(input: PromptOptimizerInput): Promise<PromptOptimizerResult> {
|
|
26
|
-
const originalTokens = estimateTokens(input.prompt);
|
|
27
|
-
const appliedRules: string[] = [];
|
|
28
|
-
|
|
29
|
-
let optimized = input.prompt;
|
|
30
|
-
|
|
31
|
-
// Get config with defaults
|
|
32
|
-
const config = {
|
|
33
|
-
...DEFAULT_CONFIG,
|
|
34
|
-
...input.config.optimizer?.strategies?.["rule-based"],
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
// Rule 1: Strip whitespace
|
|
38
|
-
if (config.stripWhitespace) {
|
|
39
|
-
const before = optimized;
|
|
40
|
-
optimized = this.stripWhitespace(optimized);
|
|
41
|
-
if (optimized !== before) {
|
|
42
|
-
appliedRules.push("stripWhitespace");
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Rule 2: Compact acceptance criteria
|
|
47
|
-
if (config.compactCriteria) {
|
|
48
|
-
const before = optimized;
|
|
49
|
-
optimized = this.compactCriteria(optimized);
|
|
50
|
-
if (optimized !== before) {
|
|
51
|
-
appliedRules.push("compactCriteria");
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Rule 3: Deduplicate context
|
|
56
|
-
if (config.deduplicateContext && input.contextMarkdown) {
|
|
57
|
-
const before = optimized;
|
|
58
|
-
optimized = this.deduplicateContext(optimized, input.contextMarkdown);
|
|
59
|
-
if (optimized !== before) {
|
|
60
|
-
appliedRules.push("deduplicateContext");
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Rule 4: Enforce max prompt tokens
|
|
65
|
-
if (config.maxPromptTokens) {
|
|
66
|
-
const currentTokens = estimateTokens(optimized);
|
|
67
|
-
if (currentTokens > config.maxPromptTokens) {
|
|
68
|
-
optimized = this.trimToMaxTokens(optimized, config.maxPromptTokens);
|
|
69
|
-
appliedRules.push("maxPromptTokens");
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const optimizedTokens = estimateTokens(optimized);
|
|
74
|
-
const savings = originalTokens > 0 ? (originalTokens - optimizedTokens) / originalTokens : 0;
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
prompt: optimized,
|
|
78
|
-
originalTokens,
|
|
79
|
-
optimizedTokens,
|
|
80
|
-
savings,
|
|
81
|
-
appliedRules,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Collapse multiple blank lines to single blank line and trim trailing whitespace.
|
|
87
|
-
*/
|
|
88
|
-
private stripWhitespace(prompt: string): string {
|
|
89
|
-
return (
|
|
90
|
-
prompt
|
|
91
|
-
// Trim trailing whitespace from each line
|
|
92
|
-
.split("\n")
|
|
93
|
-
.map((line) => line.trimEnd())
|
|
94
|
-
.join("\n")
|
|
95
|
-
// Collapse 3+ consecutive newlines to 2
|
|
96
|
-
.replace(/\n{3,}/g, "\n\n")
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Convert verbose acceptance criteria to terse bullet format.
|
|
102
|
-
*/
|
|
103
|
-
private compactCriteria(prompt: string): string {
|
|
104
|
-
return (
|
|
105
|
-
prompt
|
|
106
|
-
// Remove verbose prefixes
|
|
107
|
-
.replace(/The user should be able to /gi, "")
|
|
108
|
-
.replace(/The system must /gi, "")
|
|
109
|
-
.replace(/The system should /gi, "")
|
|
110
|
-
.replace(/When the /gi, "")
|
|
111
|
-
.replace(/When a /gi, "")
|
|
112
|
-
// Compact common verbose patterns
|
|
113
|
-
.replace(/it should validate all fields/gi, "validate all fields")
|
|
114
|
-
.replace(/display an error message/gi, "show error")
|
|
115
|
-
.replace(/error message/gi, "error")
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Remove context sections that duplicate constitution content.
|
|
121
|
-
*/
|
|
122
|
-
private deduplicateContext(prompt: string, contextMarkdown: string): string {
|
|
123
|
-
// Find context section
|
|
124
|
-
const contextSectionMatch = prompt.match(/# Context\n([\s\S]*?)(?=\n#|$)/i);
|
|
125
|
-
if (!contextSectionMatch) {
|
|
126
|
-
return prompt;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const contextSection = contextSectionMatch[1];
|
|
130
|
-
const contextLines = contextSection.split("\n");
|
|
131
|
-
|
|
132
|
-
// Remove lines that appear in constitution
|
|
133
|
-
const dedupedLines = contextLines.filter((line) => {
|
|
134
|
-
const trimmed = line.trim();
|
|
135
|
-
if (!trimmed) return true; // Keep blank lines for now
|
|
136
|
-
// Check if this line appears in the context markdown (which may duplicate constitution)
|
|
137
|
-
return !contextMarkdown.includes(trimmed);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
// If we removed content, replace the context section
|
|
141
|
-
if (dedupedLines.length < contextLines.length) {
|
|
142
|
-
const newContextSection = dedupedLines.join("\n");
|
|
143
|
-
return prompt.replace(contextSectionMatch[0], `# Context\n${newContextSection}`);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return prompt;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Trim context aggressively if prompt exceeds max tokens.
|
|
151
|
-
* Preserve Task and Acceptance Criteria sections.
|
|
152
|
-
*/
|
|
153
|
-
private trimToMaxTokens(prompt: string, maxTokens: number): string {
|
|
154
|
-
const currentTokens = estimateTokens(prompt);
|
|
155
|
-
if (currentTokens <= maxTokens) {
|
|
156
|
-
return prompt;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Extract sections
|
|
160
|
-
const sections = this.extractSections(prompt);
|
|
161
|
-
const targetChars = maxTokens * 4; // Reverse of token estimation
|
|
162
|
-
const trimmedMessage = "\n... (context trimmed)";
|
|
163
|
-
|
|
164
|
-
// Preserve task and AC, trim context
|
|
165
|
-
let result = "";
|
|
166
|
-
let remainingChars = targetChars;
|
|
167
|
-
|
|
168
|
-
// Add task section (always preserve)
|
|
169
|
-
if (sections.task) {
|
|
170
|
-
result += sections.task;
|
|
171
|
-
remainingChars -= sections.task.length;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Add AC section (always preserve)
|
|
175
|
-
if (sections.acceptanceCriteria) {
|
|
176
|
-
result += sections.acceptanceCriteria;
|
|
177
|
-
remainingChars -= sections.acceptanceCriteria.length;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Add as much context as fits
|
|
181
|
-
if (sections.context && remainingChars > 0) {
|
|
182
|
-
// Reserve space for the trimmed message if we're going to add it
|
|
183
|
-
const reserveForMessage = sections.context.length > remainingChars ? trimmedMessage.length : 0;
|
|
184
|
-
const maxContextChars = Math.max(0, remainingChars - reserveForMessage);
|
|
185
|
-
const trimmedContext = sections.context.substring(0, maxContextChars);
|
|
186
|
-
result += trimmedContext;
|
|
187
|
-
if (trimmedContext.length < sections.context.length) {
|
|
188
|
-
result += trimmedMessage;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// Add other sections if there's room
|
|
193
|
-
if (sections.other && remainingChars > sections.other.length) {
|
|
194
|
-
result += sections.other;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return result;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Extract common prompt sections for targeted trimming.
|
|
202
|
-
*/
|
|
203
|
-
private extractSections(prompt: string): {
|
|
204
|
-
task?: string;
|
|
205
|
-
context?: string;
|
|
206
|
-
acceptanceCriteria?: string;
|
|
207
|
-
other?: string;
|
|
208
|
-
} {
|
|
209
|
-
const sections: {
|
|
210
|
-
task?: string;
|
|
211
|
-
context?: string;
|
|
212
|
-
acceptanceCriteria?: string;
|
|
213
|
-
other?: string;
|
|
214
|
-
} = {};
|
|
215
|
-
|
|
216
|
-
const taskMatch = prompt.match(/# Task\n([\s\S]*?)(?=\n#|$)/i);
|
|
217
|
-
if (taskMatch) {
|
|
218
|
-
sections.task = taskMatch[0];
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
const contextMatch = prompt.match(/# Context\n([\s\S]*?)(?=\n#|$)/i);
|
|
222
|
-
if (contextMatch) {
|
|
223
|
-
sections.context = contextMatch[0];
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const acMatch = prompt.match(/# Acceptance Criteria\n([\s\S]*?)(?=\n#|$)/i);
|
|
227
|
-
if (acMatch) {
|
|
228
|
-
sections.acceptanceCriteria = acMatch[0];
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// Collect everything else
|
|
232
|
-
let other = prompt;
|
|
233
|
-
if (sections.task) {
|
|
234
|
-
other = other.replace(sections.task, "");
|
|
235
|
-
}
|
|
236
|
-
if (sections.context) {
|
|
237
|
-
other = other.replace(sections.context, "");
|
|
238
|
-
}
|
|
239
|
-
if (sections.acceptanceCriteria) {
|
|
240
|
-
other = other.replace(sections.acceptanceCriteria, "");
|
|
241
|
-
}
|
|
242
|
-
if (other.trim()) {
|
|
243
|
-
sections.other = other;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
return sections;
|
|
247
|
-
}
|
|
248
|
-
}
|
package/src/optimizer/types.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import type { NaxConfig } from "../config/schema.js";
|
|
2
|
-
import type { UserStory } from "../prd/types.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Interface for prompt optimizers.
|
|
6
|
-
*
|
|
7
|
-
* Optimizers transform assembled prompts to reduce token usage while
|
|
8
|
-
* preserving semantic meaning and all technical requirements.
|
|
9
|
-
*/
|
|
10
|
-
export interface IPromptOptimizer {
|
|
11
|
-
/** Unique optimizer name */
|
|
12
|
-
name: string;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Optimize a prompt before it is sent to the coding agent.
|
|
16
|
-
*
|
|
17
|
-
* Implementations MUST preserve all technical requirements,
|
|
18
|
-
* acceptance criteria semantics, and code references.
|
|
19
|
-
*/
|
|
20
|
-
optimize(input: PromptOptimizerInput): Promise<PromptOptimizerResult>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface PromptOptimizerInput {
|
|
24
|
-
/** Assembled prompt from promptStage */
|
|
25
|
-
prompt: string;
|
|
26
|
-
/** Stories being executed (for context) */
|
|
27
|
-
stories: UserStory[];
|
|
28
|
-
/** Raw context markdown (pre-assembly, for dedup detection) */
|
|
29
|
-
contextMarkdown?: string;
|
|
30
|
-
/** Nax configuration */
|
|
31
|
-
config: NaxConfig;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface PromptOptimizerResult {
|
|
35
|
-
/** Optimized prompt */
|
|
36
|
-
prompt: string;
|
|
37
|
-
/** Estimated token count before optimization */
|
|
38
|
-
originalTokens: number;
|
|
39
|
-
/** Estimated token count after optimization */
|
|
40
|
-
optimizedTokens: number;
|
|
41
|
-
/** Savings percentage (0-1) */
|
|
42
|
-
savings: number;
|
|
43
|
-
/** List of applied optimization rules/passes */
|
|
44
|
-
appliedRules: string[];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Estimate token count using simple heuristic.
|
|
49
|
-
* ~4 chars per token for English text (rough estimate).
|
|
50
|
-
*/
|
|
51
|
-
export function estimateTokens(text: string): number {
|
|
52
|
-
return Math.ceil(text.length / 4);
|
|
53
|
-
}
|