Files
whetstone_DSL/editor/tests/step1883_test.cpp
Bill d98fc26c05 Sprint 271: LanguageFitnessScorer — polyglot Phase 1 (steps 1883-1887)
Add language fitness scoring infrastructure for the polyglot orchestrator.

New components:
- ASTFeatureExtractor.h: extract 5 scalar features from JSON AST subtrees
  (mutation ratio, recursion shape, concurrency primitive, I/O pattern, type complexity)
- LanguageIdiomProfile.h: static ideal feature vectors for 8 languages
  (Python, Rust, Go, Haskell, C++, TypeScript, Elixir, Lisp)
- LanguageFitnessScorer.h: weighted scoring against profiles, returns JSON
  array sorted descending with per-language score and rationale
- RegisterLanguageFitnessTools.h: whetstone_score_language_fitness MCP tool
- Sprint271IntegrationSummary.h: sprint metadata

25/25 tests passing (steps 1883-1887). whetstone_mcp rebuilt with new tool.

Also: merge PROGRESS.md into progress.md (single authoritative log, 15k lines),
update CLAUDE.md to current state (step 1887, sprint 271, 91 tools).

LoRA session recorded: sprint271-polyglot-fitness-2026-03-01
(5 tool calls: architect_intake, generate_taskitems, 3x generate_code)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-01 13:55:38 -07:00

125 lines
4.3 KiB
C++

// Step 1883 TDD: ASTFeatureExtractor — scalar feature extraction from AST subtrees
//
// Extracts: mutation ratio, recursion shape, concurrency primitives, I/O pattern, type complexity.
//
// t1: flat assignment-heavy AST → high mutationRatio, FlatLoop shape
// t2: recursive call tree → TreeRecursive shape, no concurrency
// t3: channel-based AST → Channels concurrency, Blocking I/O
// t4: async/await AST → Async I/O pattern, SimpleGenerics type complexity
// t5: pure functional AST (no writes, tail calls) → low mutationRatio, TailRecursive
#include "ASTFeatureExtractor.h"
#include <iostream>
#include <string>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using AFF = whetstone::ASTFeatures;
static int p=0,f=0;
#define T(n) { std::cout<<" "<<#n<<"... "; }
#define P() { std::cout<<"PASS\n"; ++p; }
#define F(m) { std::cout<<"FAIL: "<<m<<"\n"; ++f; }
#define C(c,m) if(!(c)){F(m);return;}
void t1(){
T(flat_loop_high_mutation);
auto ast = json::parse(R"({
"type": "module",
"children": [
{"type": "for_stmt", "children": [
{"type": "assign_stmt", "children": []},
{"type": "assign_stmt", "children": []},
{"type": "assign_stmt", "children": []}
]}
]
})");
auto feat = whetstone::ASTFeatureExtractor::extract(ast);
C(feat.mutationRatio > 0.3f,
"expected high mutationRatio for assign-heavy AST, got " + std::to_string(feat.mutationRatio));
C(feat.recursionShape == AFF::RecursionShape::FlatLoop,
"expected FlatLoop for for_stmt AST");
P();
}
void t2(){
T(tree_recursive_no_concurrency);
auto ast = json::parse(R"({
"type": "function_decl",
"children": [
{"type": "call_expr", "children": [
{"type": "call_expr", "children": []}
]},
{"type": "call_expr", "children": []}
]
})");
auto feat = whetstone::ASTFeatureExtractor::extract(ast);
C(feat.recursionShape == AFF::RecursionShape::TreeRecursive,
"expected TreeRecursive for multiple nested call_expr nodes");
C(feat.concurrencyPrimitive == AFF::ConcurrencyPrimitive::None,
"expected None concurrency for plain call tree");
P();
}
void t3(){
T(channel_concurrency_blocking_io);
auto ast = json::parse(R"({
"type": "function_decl",
"children": [
{"type": "chan_send", "children": []},
{"type": "chan_recv", "children": []},
{"type": "write_file", "children": []}
]
})");
auto feat = whetstone::ASTFeatureExtractor::extract(ast);
C(feat.concurrencyPrimitive == AFF::ConcurrencyPrimitive::Channels,
"expected Channels for chan_send/chan_recv nodes");
C(feat.ioPattern == AFF::IOPattern::Blocking,
"expected Blocking I/O for write_file node");
P();
}
void t4(){
T(async_io_simple_generics);
auto ast = json::parse(R"({
"type": "function_decl",
"children": [
{"type": "await_expr", "children": []},
{"type": "async_call", "children": []},
{"type": "type_param_decl", "children": []}
]
})");
auto feat = whetstone::ASTFeatureExtractor::extract(ast);
C(feat.ioPattern == AFF::IOPattern::Async,
"expected Async for await_expr/async_call nodes");
C(feat.typeComplexity == AFF::TypeComplexity::SimpleGenerics,
"expected SimpleGenerics for type_param_decl node");
P();
}
void t5(){
T(pure_functional_tail_recursive_low_mutation);
// Tail recursive: call_expr as direct child of return_stmt, no loops, no writes
auto ast = json::parse(R"({
"type": "function_decl",
"children": [
{"type": "return_stmt", "children": [
{"type": "call_expr", "children": []}
]}
]
})");
auto feat = whetstone::ASTFeatureExtractor::extract(ast);
C(feat.mutationRatio < 0.1f,
"expected low mutationRatio for pure functional AST, got " + std::to_string(feat.mutationRatio));
C(feat.recursionShape == AFF::RecursionShape::TailRecursive,
"expected TailRecursive for call_expr inside return_stmt");
P();
}
int main(){
std::cout << "Step 1883: ASTFeatureExtractor\n";
t1(); t2(); t3(); t4(); t5();
std::cout << "\n" << p << "/" << (p+f) << " passed\n";
return f > 0 ? 1 : 0;
}