Commit graph

159 commits

Author SHA1 Message Date
jedarden
d0df7f2fab docs(plan): update ZoneShrinkStep from 2 to 1 to match implementation
The plan previously specified ZoneShrinkStep=2, but the engine uses
ZoneShrinkStep=1 (per commit 0577fcd). The value of 1 was found to
improve combat density because it matches bot movement speed (1 tile/turn).
A value of 2 caused the zone to shrink faster than bots could move,
killing them before combat could occur.

Updated zone parameters table and rationale in §3.7.1.

Closes: bf-3mrj

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-26 08:22:27 -04:00
jedarden
3bd6ed45f9 fix(index-builder): promote recent replays from B2 to R2 warm cache
Modified bundleWarmAssetsForCycle to return the deduplicated match IDs
of warm-set replays, and added a call to promoteRecentReplays in the
build cycle. This implements plan §8.2.5: index builder promotes recent
replays from B2 cold archive to R2 warm cache.

Acceptance criteria for plan-gap bead bf-jfmo:
- /r2/replays/{id}.json.gz will return valid gzipped replay JSON once
  the Cloudflare Pages R2 bucket binding (ACB_BUCKET) is configured
- R2 bucket exists with replay objects after each build cycle
- Index builder has R2 S3 client configured (credentials via env vars)

Closes: bf-jfmo
2026-05-26 08:15:02 -04:00
jedarden
4b7d81db45 feat(index-builder): bundle warm-set replays as static Pages assets
Per bead bf-3e60: instead of copying B2->R2 (warm cache), bundle warm-set
replays, thumbnails, cards, and evolution live.json directly into the Pages
deploy directory as static assets (dist/data/). This serves replays same-origin,
eliminating R2 dependency and 404 errors.

Changes:
- Add B2Client interface for testable B2 operations
- Add bundleWarmReplays(): copies replays/*.json.gz from B2 to dist/data/replays/
- Add bundleWarmThumbnails(): copies thumbnails/*.png from B2 to dist/data/thumbnails/
- Add bundleWarmCards(): copies cards/*.png from B2 to dist/data/cards/
- Add bundleEvolutionLive(): copies evolution/live.json from B2 to dist/data/evolution/
- Replace promoteRecentReplaysForCycle() with bundleWarmAssetsForCycle()
- Remove R2 pruning logic from main loop (no longer needed)
- Add unit tests for all bundling functions with mock B2 client

Replays are served gzipped (as-is from B2) to keep deploy size under Pages'
25MB file limit. Frontend will gunzip client-side (separate bead bf-5cwi).

All tests pass (go test ./...).

Closes: bf-3e60
2026-05-26 07:35:48 -04:00
jedarden
70c51d5df2 feat(api): add GET /api/status/{bot_id} endpoint per plan §8.2
Adds lightweight bot status endpoint that returns status and last_active.
Public endpoint for checking bot health without full profile fetch.

Closes: bf-5p43

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-25 16:53:03 -04:00
jedarden
b31ebee32f feat(index-builder): generate evolution/meta.json and lineage.json
Plan §15.4 Live Evolution Observatory requires data/evolution/meta.json
and data/evolution/lineage.json files, which the web platform expects
but the index-builder was not generating.

- Add EvolutionMeta type (generation, promoted_today, top_10_count, updated_at)
- Add LineageNode type (full program lineage with parent relationships)
- Add fetchEvolutionMeta() to query evolver database for stats
- Add fetchLineage() to query evolver programs table for lineage tree
- Add generateEvolutionMeta() to write data/evolution/meta.json
- Add generateLineage() to write data/evolution/lineage.json
- Wire generation into generateAllIndexes()
- Add files to R2 upload list

The implementation gracefully handles missing evolver database by
returning empty/placeholder data, allowing the index-builder to run
without evolution data while still producing valid JSON files.

Closes: bf-6cp0
2026-05-25 16:01:27 -04:00
jedarden
1839f5e7d1 style: format Go files with gofmt
- cmd/acb-maps-loader/main.go
- wasm/bots/guardian/main.go
- wasm/bots/hunter/main.go
- wasm/bots/random/main.go
- web/package-lock.json (npm ci update)
2026-05-25 15:33:38 -04:00
jedarden
e5f5de4e64 feat(cmd): add acb-maps-loader to load map library into database
- New cmd/acb-maps-loader reads all 200 map JSON files from maps/ directory
- Transforms map files into database schema format for maps table
- Supports INSERT with ON CONFLICT for idempotent updates
- Includes tests verifying all 200 maps can be parsed
- Updates Makefile with acb-maps-loader and load-maps targets

This addresses the plan-gap where maps/ had 200 maps but only 12 were in the database.
The index-builder generates maps/index.json from the database, so loading the full
map library enables complete map index generation.

Closes: bf-4bn3

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-25 14:50:38 -04:00
jedarden
41d868b5c1 feat(engine): add pre-generated map loading from map library
Per plan §3.8, maps should be generated offline and stored in the map
library, not generated on-the-fly during matches. This commit adds
support for loading pre-generated maps from the database.

Changes:
- Add PreGeneratedMap type and WithMap option to MatchRunner
- Add loadPreGeneratedMap() to parse map JSON (walls, cores)
- Update worker to pass loaded map data to MatchRunner via WithMap
- Fallback to on-the-fly generation if map data is invalid
- Update acb-mapgen spawn radius to 25% for 2-player (aligns with match.go)
- Update test to verify cores are outside final zone radius

This enables the map library infrastructure (maps/, acb-mapgen, index
builder) to be used in production matches instead of being ignored.

Closes: bf-5m29

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-25 14:14:27 -04:00
jedarden
166d3ee277 fix(mapgen): align core placement radius with spawn radius fixes
Plan: §3.7.1 Spawn mechanics, §3.8 Map Generation

The map generator was using outdated core placement radius (0.35) that
placed cores too far apart on 40x40 maps (~28 tiles between cores on
opposite sides). This exceeded the attack radius (6 tiles for 2-player,
3.5 tiles for 3+ player), meaning generated maps didn't force combat.

The match runner was already fixed in commit e8fda06 to use:
- 2-player: primaryRadius = 0.15 (6 tiles apart = attack radius)
- 3+ player: primaryRadius = 0.063 (~3.4 tiles, within attack radius)

This change aligns cmd/acb-mapgen with the same logic, ensuring all
generated maps place cores within attack range.

Also adds validation test TestGenerateMap_CoresWithinAttackRadius to
verify cores are placed within attack radius on standard 40x40 maps.

Closes: bf-2wn4

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-25 08:35:13 -04:00
jedarden
a22b0b6aa3 feat(enrichment): align config with other services for K8s deployment
- Update Config struct to use individual postgres connection components (ACB_POSTGRES_HOST, ACB_POSTGRES_PORT, etc.) instead of ACB_DATABASE_URL
- Add DatabaseURL() method to build connection string from components
- This matches the pattern used by acb-index-builder and other services

Closes: bf-1ghm

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-25 01:44:59 -04:00
jedarden
1478a9365c fix(evolver): use ConfigForPlayers for 2-player matches per plan §3.4
The evolver arena was using DefaultConfig() which has attack_radius2=12
for all matches. Per plan §3.4, 2-player matches should have
attack_radius2=36 (6 tiles) to achieve 65-80% combat density.

This bug caused evolved bots to learn energy-farming strategies since
enemies were rarely in attack range on 40x40 maps with only 3.5 tile
radius. With the correct 6-tile radius, bots will experience actual
combat during evolution and should develop fighting behaviors.

Closes: bf-3lt3

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 22:35:15 -04:00
jedarden
6a6a3788a6 fix(worker): use ConfigForPlayers to get correct AttackRadius2=12
The worker was hardcoding AttackRadius2=5 in executeMatch, but
engine.ConfigForPlayers sets AttackRadius2=12 for both 2-player and
3+ matches. This mismatch meant matches ran with the old attack radius
instead of the improved value that supports better combat density.

Now uses ConfigForPlayers which provides:
- AttackRadius2: 12 (3.5 tiles) for all player counts
- Proper zone parameters scaled by player count
- Correct max turns scaling

Grid dimensions are overridden from the pre-generated map, and
SeasonID/RulesVersion are preserved from the match.

Closes: bf-576s

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 16:03:02 -04:00
jedarden
66e7951e99 fix(cli): handle draw result without panic
The acb-local tool was panicking when a match ended in a draw
(Winner = -1) because it tried to use -1 as an array index into
botNames[]. Fixed by checking if Winner >= 0 before accessing
the array, and printing "Result: Draw" for draws.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 11:13:00 -04:00
jedarden
ea04f4debb style: apply gofmt alignment fixes across codebase
Tab/space alignment consistency from running gofmt on all packages.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 10:40:33 -04:00
jedarden
5b6f7267f9 feat(engine): bias energy placement toward map center for combat density
Implement center-weighted energy distribution as a forcing function
to pull players into contested midfield, increasing combat density.

Changes:
- engine/match.go: Update placeEnergyNodes to use tiered radius
  distribution (30% central 0.05-0.20, 40% mid 0.20-0.40, 30% outer
  0.40-0.60) instead of uniform 0.3-0.7
- engine/integration_test.go: Add TestIntegration_CenterWeightedEnergy
  to verify ~25% of energy nodes spawn in central zone
- cmd/acb-mapgen: Already had tiered distribution (unchanged, just
  comments updated)
- cmd/acb-mapgen/mapgen_test.go: Add TestGenerateMap_CenterWeightedEnergy

This uses the existing economic incentive (energy collection) as a
forcing function without changing combat resolution or scoring.

Closes: bf-648i

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 10:30:52 -04:00
jedarden
af46a1da97 feat(engine): add combat-density metric and fix computeCombatTurns
- Fix computeCombatTurns to count EventCombatDeath events instead of
  EventBotDied with reason="combat" (which was never emitted, causing
  CombatTurns to always be 0)
- Add CombatDeaths field to MapEngagementScore to track focus-fire kills
- Update engagement formula to weight combat deaths at 3.0 (same as
  win_prob_crossings) to bias map evolution toward combat-dense maps
- Add countCombatDeaths helper function to count EventCombatDeath events
- Update log output to include combat_deaths metric

This implements bf-4nxs: the combat-density metric is now measured and
weighted in map engagement, which gates map curation/selection. Maps
with zero combat will have low engagement scores and be filtered out.

Closes: bf-4nxs
2026-05-24 10:16:54 -04:00
jedarden
18ac1ff2b4 feat(engine): reduce default map size to 40x40 and add skirmish map class
Reduce default 2-player map size from 60x60 to 40x40 (from 3600 to 1600
tiles) to increase encounter frequency and combat density. Add -skirmish
flag to acb-mapgen for generating even smaller dense maps (32x32, 0.20
wall density, 15 energy nodes) with "skirmish_" ID prefix.

Changes:
- engine/types.go: DefaultConfig() returns 40x40, ConfigForPlayers()
  uses 800 tiles/player for 2-player (40x40) and 2000 tiles/player for
  3+ players
- cmd/acb-matchmaker/tickers.go: gridForPlayers() returns 40x40 for 2
  players
- cmd/acb-map-evolver/main.go: gridForPlayers() returns 40x40 for 2
  players
- cmd/acb-mapgen/main.go: defaults to 40x40, adds -skirmish flag for
  32x32 high-density maps
- cmd/acb-matchmaker/tickers_test.go: update test expectations for new
  40x40 default

Closes: bf-39wt
2026-05-24 10:10:57 -04:00
jedarden
d3d655b9c9 Evolver: Fix nsjail integration for complete sandbox coverage
- Add /opt to nsjail bindmounts so Rust toolchain (/opt/rust) is accessible
  during sandboxed validation of Rust bots
- Explicitly enable Alpine community repository in Dockerfile to ensure
  nsjail package can be installed (nsjail lives in community, not main)
- nsjail integration was already optional (falls back to plain exec if
  unavailable), but these changes ensure it actually works when enabled

This addresses bead bf-3f29: nsjail was listed in apk add but /opt wasn't
bindmounted, causing Rust validation to fail when UseNsjail=true.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-22 15:18:21 -04:00
jedarden
7137623f6a Evolver: Add nsjail and missing language runtimes (Rust, Java) to container
The evolver's validation pipeline supports Rust and Java bots, but the
container image was missing rustc and javac runtimes. Additionally, nsjail
was documented as part of the sandbox stage but not installed.

Changes:
- Add nsjail package (from Alpine community repo) for sandbox isolation
- Add openjdk-17-jdk for Java bot validation
- Install Rust toolchain (rustc) via rustup to /opt/rust for shared access
- Set PATH to include Rust binaries for the acb user

The validator already had graceful fallback when nsjail wasn't found in PATH,
but with nsjail installed, the sandbox stage now provides proper CPU/memory
resource limits during smoke testing.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-22 15:16:23 -04:00
jedarden
9bcbd566b6 Bug fix bf-3mx7: Fix match index winner badge - use p.Won instead of comparing BotID with WinnerID
The WinnerID field is a player-slot integer as string (e.g. "2"), not a bot_id.
The SQL query already computes the correct winner status in p.Won field.

Fixed in 3 functions:
- matchToSummary: Changed Won: p.BotID == m.WinnerID to Won: p.Won
- buildPlaylistMatch: Changed Won: p.BotID == m.WinnerID to Won: p.Won
- ratingUpsetMagnitude: Use p.Won to identify winner instead of comparing with m.WinnerID
- maxScoreDiff: Use p.Won to identify winner instead of comparing with m.WinnerID
- isEvolutionBreakthrough: Find winner using p.Won before checking if evolved

This fixes the issue where 984/1000 prod matches had winner_id set but all participants showed won: false.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-13 17:59:02 -04:00
jedarden
b27272de5a Phase 10: Fix narrative engine tests to match exact specs
- Fix TestBuildNarrativePrompt_Comeback to check for current ELO
  instead of old rating (comeback arc shows bottom 25%→top 25%)
- Fix TestDetectRivalryArcs to use 10+ matches (grudge match spec)
  instead of only 5 matches

Story arc detection (per §3.7 chronicles):
✓ Comeback bots: recovered from bottom 25% to top 25%
✓ Grudge matches: same pair meets 10+ times
✓ Underdog victories: bottom-10 beats top-10

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 15:10:42 -04:00
jedarden
0e6a4977b6 Phase 10: §3.7 chronicles - Update story arc detection to exact specs
- detectComebackArcs: Now detects bottom 25% → top 25% climb (was peak→trough→recovery)
- detectRivalryArcs: Now detects 10+ total meetings grudge matches (was 5+ recent matches)
- detectUpsetArcs: Now detects bottom-10 beats top-10 (was biggest ELO gap)
- Updated narrative prompts to reflect new comeback arc specification
- Updated weekly chronicles prompt for comeback arcs

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 15:07:23 -04:00
jedarden
a4bdeba8fd Phase 10: Live evolution observatory - evolver live.json feed + observatory page
Evolver writes live.json to R2 every cycle. Observatory page polls and
renders live feed + lineage tree + meta shift chart.

- Added ACB_R2_UPLOAD_ENABLED env var to enable automatic R2 upload during run loop
- CycleState tracks real-time evolution cycle status (generation, phase, candidate, validation, evaluation)
- Export() now includes cycle info when cycleState is provided
- runCycle() integrated with live observatory exports at each phase transition
- exportLiveQuiet() for mid-cycle status updates without verbose logging
- Fixed function signature mismatches for exportLiveQuiet calls

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 14:52:17 -04:00
jedarden
0028100c64 Phase 9: Map voting + positional fairness monitoring - verify existing implementation
Verified all Phase 9 deliverables already in place:
- PostgreSQL map_votes table (UNIQUE constraint on map_id, voter_id)
- POST /api/vote-map endpoint (+1/-1 votes, rate-limited)
- GET /api/vote/map/{map_id} endpoint for vote counts
- Positional fairness monitoring: tickFairnessAudit with 5-step lifecycle
  * updateMapFairnessStats: recompute per-slot win rates
  * flagUnfairMaps: probation for >10pp deviation
  * retireDislikedMaps: force-retire at < -20 net votes
  * pruneLowEngagementMaps: monthly bottom 10% pruning
  * promoteClassicMaps: top-5 sustained (3+ months) to classic
- maps/index.json includes NetVotes from aggregation

All tests pass (mapvote, map_fairness, index-builder).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 14:30:46 -04:00
jedarden
e7a60894ac test(acb-map-evolver): add comprehensive tests for weekly schedule
Added tests for:
- TestNextScheduledTime: verifies correct calculation of next scheduled
  run time across various scenarios (same-day future, same-day past,
  different weekdays, edge cases around midnight)
- TestWeeklyScheduleEnvParsing: validates environment variable parsing
  for the WEEKDAY:HH:MM format, including valid and invalid inputs

These tests ensure the weekly automated map evolution ticker (§14.6)
correctly schedules evolution runs at the configured time.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 09:32:51 -04:00
jedarden
5242d6037c feat(acb-evolver): add weekly automated map evolution ticker
Wire up the acb-map-evolver to run automatically on a weekly schedule
(Sunday 03:00 UTC by default) from the evolver deployment.

The map evolution ticker:
- Waits until the next scheduled time (weekday:hour:minute UTC)
- Runs acb-map-evolver --once to evolve maps for all player counts
- Repeats every 7 days

The schedule can be configured via ACB_MAP_EVOLUTION_SCHEDULE env var
(format: WEEKDAY:HH:MM, e.g., "0:03:00" for Sunday 03:00 UTC).

Enable via ACB_MAP_EVOLUTION_ENABLED=true or --enable-map-evolution flag.

Per plan §14.6: the weekly map evolution loads engagement scores,
runs MAP-Elites evolution, promotes high-scoring variants, and updates
the active map pool in the database.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 09:26:38 -04:00
jedarden
b31c306013 feat(acb-map-evolver): add weekly automated run wiring per plan §14.6
- Implement runWeeklyLoop() function that waits for scheduled time and
  runs evolution for all player counts (2, 3, 4, 6) weekly
- Add --weekly flag to enable weekly mode (default: Sunday 03:00 UTC)
- Add --weekly-schedule flag for custom schedule (WEEKDAY:HH:MM format)
- Add ACB_WEEKLY_SCHEDULE env var for configuration

feat(acb-evolver): add weekly map evolution ticker

- Add MapEvolutionEnabled and MapEvolutionSchedule to RunConfig
- Add --enable-map-evolution flag to acb-evolver run subcommand
- Add startMapEvolutionTicker() goroutine that runs weekly
- Ticker executes acb-map-evolver --once to trigger map breeding
- Configurable via ACB_MAP_EVOLUTION_ENABLED and ACB_MAP_EVOLUTION_SCHEDULE

This integrates map evolution into the bot evolver's deployment,
allowing weekly automated map evolution based on engagement scores
as specified in plan §14.6.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 09:15:19 -04:00
jedarden
90431344e8 fix(map-evolver): add missing --once flag parsing
The --once mode was implemented but the command-line flag was not being
parsed. This commit adds the flag parsing and help text for --once, which
enables the weekly automated map evolution run from the evolver.

The evolver's weekly ticker (run.go) calls acb-map-evolver --once to
trigger map evolution on Sundays at 03:00 UTC as specified in plan §14.6.
2026-05-04 03:31:33 -04:00
jedarden
8e33ee1f27 fix(index-builder): correct function name typo in weekly chronicles generation 2026-05-04 03:10:46 -04:00
jedarden
6bfd3e6679 feat(api): implement POST /api/request-enrichment endpoint
Per plan §13.3, implements user-requested AI replay commentary with:
- HMAC bot authentication via shared_secret
- Rate limiting: 5 requests/day per bot
- Match validation (exists and completed)
- Idempotency via enrichment_requested_at column
- Enqueues to Valkey for acb-enrichment service
- Returns 202 Accepted with estimated wait time

Also adds:
- AllowN() method to ratelimit package for multi-token checks
- enrichment_requested_at column to matches table (idempotency)
- enrichLtr rate limiter (5/day per bot)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-04 02:58:11 -04:00
jedarden
9972cb8c84 feat(matchmaker): add best-of-5 weekly featured and best-of-7 championship series scheduling
- Add 'featured' boolean column to series table for weekly featured series
- Add tickFeaturedSeries ticker that runs Friday 20:00 UTC to create bo5 featured series
- Featured series: query top 20 bots by rating, select 4 rivalry pairs by ELO proximity
- Best-of-7 championship bracket already implemented via createChampionshipBracket
- Add FeaturedSchedSecs config (default: 3600s check interval)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-04 02:34:47 -04:00
jedarden
df7a3e38c7 feat(worker): implement map engagement scoring per plan §14.6
Update the map engagement scoring formula to match plan §14.6:
- score = win_prob_crossings * 3.0 + critical_moments * 2.0 +
         resource_contest_turns * 1.5 + survival_turns * 0.5

New metrics computed from replay data:
- resource_contest_turns: turns where energy is contested by multiple players
- survival_turns: turns where all players have at least one bot alive

The old formula used map_coverage_pct, closeness, and turn_pct which
did not match the specification.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-04 02:28:45 -04:00
jedarden
e88c108010 feat(acb-enrichment): implement AI replay enrichment service
Implements the acb-enrichment service (plan §13.3) that generates AI
commentary for featured matches.

Key features:
- LLM client (OpenAI/Anthropic API compatible)
- Replay fetch from B2/R2 storage
- Structured commentary output (key_moments array with turn,
  description, significance, tags)
- Rate limiting to control LLM costs
- Match selection based on:
  - Minimum turn count
  - Win probability crossings
  - Upset threshold
  - Close finishes

Components:
- cmd/acb-enrichment/main.go - service entry point
- cmd/acb-enrichment/config.go - configuration from env vars
- cmd/acb-enrichment/service.go - orchestration logic
- internal/db/store.go - database access for match selection
- internal/llm/client.go - OpenAI-compatible LLM client
- internal/selector/selector.go - match selection with priority
- internal/generator/generator.go - commentary generation
- internal/storage/client.go - S3-compatible storage client
- Dockerfile - container image
- manifests/acb-enrichment-deployment.yml - K8s deployment
- metrics/metrics.go - Prometheus metrics for enrichment

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-04 02:22:28 -04:00
jedarden
c88474ad6b feat(index-builder): update community hints filter for insight/idea types
- Change filter from 'idea'/'mistake' to 'insight'/'idea' (mapping to 'hint'/'strategy' from plan §13.6)
- Increase upvote threshold from 3 to 10 for higher quality signals
- The evolver consumes community_hints.json for LLM prompt context
2026-05-04 01:38:53 -04:00
jedarden
6dd69f596d feat(api): add spam/word filter for feedback submission
Per plan §13.6, implement a configurable spam filter for the
handleCreateFeedback endpoint that:

- Validates minimum content length (default 10 chars, configurable
  via ACB_SPAM_MIN_LENGTH env var)
- Normalizes case and strips common unicode substitutions
  (leetspeak: 0→o, 1→i, 3→e, 4→a, 5→s, 7→t, @→a, $→s, etc.)
- Checks content against a block-list of banned terms with word-boundary
  matching
- Returns HTTP 422 (Unprocessable Entity) on filter rejection

Configuration:
- ACB_SPAM_BLOCK_LIST: comma-separated custom blocked terms (optional,
  defaults to embedded list of common spam/offensive words)
- ACB_SPAM_MIN_LENGTH: minimum feedback content length (default: 10)

The embedded default block-list includes:
- Profanity and offensive language
- Common spam patterns (buy now, click here, free money, etc.)
- Scam patterns (bitcoin giveaway, urgent, act now, etc.)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-04 01:26:32 -04:00
jedarden
39fe612f6a feat(worker): fix rating recovery default sigma value
The rating recovery CLI mode (-mode=recalc-ratings) was using
glicko2Tau (0.5) instead of glicko2DefaultSigma (0.06) for the
default sigma value when resetting ratings. This caused the reset
sigma to be ~8x higher than the schema-defined default.

Added glicko2DefaultSigma constant (0.06) and updated ResetAllRatings
and recalcRatings to use it correctly.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-04 00:49:47 -04:00
jedarden
467b7b67ea feat(worker): add rating recovery CLI mode (-mode=recalc-ratings)
Implements the rating recovery procedure specified in plan §12.3.
Running 'go run ./cmd/acb-worker -mode=recalc-ratings' will:
1. Reset all bot ratings to Glicko-2 defaults (mu=1500, phi=350, sigma=0.06)
2. Fetch all completed matches from the database in chronological order
3. Replay each match to recompute Glicko-2 ratings from scratch
4. Update the bots table with the recalculated ratings

This is needed for disaster recovery when ratings are corrupted or lost.

Database functions added:
- ResetAllRatings: resets all bot ratings to defaults
- GetAllCompletedMatches: fetches completed matches chronologically with participants
- UpdateAllRatings: bulk updates all bot ratings in a single transaction

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-04 00:41:10 -04:00
jedarden
aeef954590 feat(index-builder): add sitemap.xml generation
Add sitemap.xml generation as a final pass in the index builder. The
sitemap covers all public pages: home, leaderboard, bots list, bot
profiles, matches list, featured replays, seasons, rivalries,
predictions, and docs.

- Add SiteURL config field (ACB_SITE_URL env var, defaults to
  https://aicodebattle.com)
- Add generateSitemap() function with proper XML encoding
- Add SitemapURL and Sitemap types for XML marshaling
- Call generateSitemap() at the end of generateAllIndexes()
- Write sitemap.xml to output directory alongside leaderboard.json

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-04 00:09:53 -04:00
jedarden
92576dbed4 feat(worker): add map engagement score tracking and verify win_prob in replays
- Add engine.CalculateMapEngagement() to compute map engagement scores from replay data (win_prob_crossings, critical_moments, map_coverage_pct, closeness, turn_pct)
- Add DBClient.UpdateMapEngagement() to update map engagement using rolling average
- Worker now calculates and writes map engagement scores after each match
- Add test to verify win_prob array is non-empty in produced replays

This implements the win probability Monte Carlo array storage in replay JSON
feature. The engine already called ComputeWinProbability() in MatchRunner.Run(),
so this commit adds the missing map engagement tracking.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-03 23:21:57 -04:00
jedarden
42e9561e46 feat(map-evolver): bias energy toward center, carve corridors to force contact
Energy node placement now uses a tiered radius distribution: 30% in the
contested central zone (0.05-0.20 from center), 40% in the mid-zone
(0.20-0.40), and 30% in the home zone (0.40-0.60). Previously nodes were
placed uniformly at 0.20-0.70, letting bots farm their home quadrant
indefinitely without crossing the midline.

After cellular automata wall generation, a 3-wide corridor is carved from
each core straight to the map center, plus a 5x5 open arena at the center
tile. This creates lanes that funnel bots into contact — replicating the key
mechanic that drove frequent fights in the original AI Challenge Ants game,
where symmetric food spawning near the midfield forced both colonies to
expand outward and collide.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-03 18:56:39 -04:00
jedarden
4937f94afd feat(combat): rank matches by enemy-kill combat turns
Adds combat_turns metric (distinct turns where ≥1 bot died from enemy
focus-fire, excluding self-collisions). Worker computes it after each
match; index builder sorts matches/index.json and the new most-combat
playlist descending by it, and bumps interest score for combat-heavy
matches so they surface in highlights.

Also switches homepage featured replay default view from influence to
standard so the actual bot-on-bot combat is visible.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-03 18:32:08 -04:00
jedarden
9b16b32aef fix(worker): handle NULL map_json fields with COALESCE
map_json generated by acb-map-evolver lacks a 'spawns' key; scanning
map_json->>'spawns' into a non-nullable string causes "converting NULL
to string is unsupported". Use COALESCE for walls/spawns/cores.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-02 10:13:30 -04:00
jedarden
181e846d8a feat(map-evolver): bootstrap empty maps table and containerize
- Add seedIfEmpty: idempotent startup seeding (20 maps per player count,
  ON CONFLICT DO NOTHING) using cellular-automata generation + validate()
- Add continuous evolution loop across all player counts (2/3/4/6)
- ACB_MIN_SEED_COUNT and ACB_EVOLUTION_PERIOD configurable via env vars
- Add Dockerfile (lean Alpine build, no language runtimes)
- Add acb-map-evolver to acb-build.yml CI pipeline
- Add staging K8s Deployment manifest

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-02 08:05:51 -04:00
jedarden
e5dc3bc543 fix: accept base64-encoded AES keys (OpenBao stores keys as base64, not hex)
The encryption key stored in OpenBao/K8s secrets is base64-encoded but
the API and worker crypto functions expected hex. Add parseAESKey() that
accepts both formats (tries hex first, falls back to base64).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-30 23:04:29 -04:00
jedarden
c726de4d7c fix(test): replace duration-based age check with calendar-relative anchors
TestThreeMonthAgeCheck used 89*24h as "3 months minus 1 day", but
89 calendar days == exactly 3 months on dates like May 1 (Feb+Mar+Apr=
28+31+30=89). The equality case makes the >3-month eligibility check
return true instead of false. Replace with AddDate-relative anchors
so the test stays correct regardless of current date.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-30 22:14:40 -04:00
jedarden
e64230b122 fix: resolve universal stalemate — signing format and secret decryption
Two root causes prevented bots from making any moves:

1. SignRequest signing string included timestamp ({match_id}.{turn}.{timestamp}.{hash})
   but all bots implement verifySignature without timestamp ({match_id}.{turn}.{hash}).
   Fixed by dropping timestamp from the signing string; X-ACB-Timestamp header is still
   sent for clock-skew checks but not in the HMAC.

2. The API stores bot secrets AES-GCM encrypted (184 hex chars) in the DB. The worker
   was passing the ciphertext directly as the HMAC key, while bots use their plaintext
   k8s secret (64 hex chars). Fixed by decrypting in the worker using ACB_ENCRYPTION_KEY.

Also tightens the home page winner filter to exclude winner_id="0" stalemates.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-30 21:48:25 -04:00
jedarden
c397e66239 fix(index-builder): run wrangler from /app/web to pick up functions/ bundle
_worker.js static file approach fails — Cloudflare rejects it when uploaded
as a static asset. Instead, copy web/functions/ into the image and set
wrangler CWD to /app/web/ so it discovers functions/ and uploads the Pages
Functions bundle correctly on every deploy cycle.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-30 12:39:57 -04:00
jedarden
037a04e8b5 fix(index-builder): bundle Pages Functions into _worker.js at build time
Every index-builder deploy was overwriting the production Pages deployment
without functions (wrangler ran from /tmp, no functions/ dir visible).
Compiling functions/ to dist/_worker.js during the Docker web-builder stage
means the worker is always included in every Pages deploy, regardless of CWD.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-30 12:35:54 -04:00
jedarden
341591a10b fix(worker): disable SDK checksum trailer for R2 uploads
AWS SDK Go v2 s3 v1.100.0 defaults to RequestChecksumCalculationWhenSupported,
which causes PutObject to send STREAMING-UNSIGNED-PAYLOAD-TRAILER — a chunked
transfer mode R2 doesn't support. Setting WhenRequired makes the SDK send a
standard signed payload instead, resolving the 403 SignatureDoesNotMatch.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-30 10:35:00 -04:00
jedarden
dc0caf0115 feat(worker): upload replays directly to R2 in addition to B2
Adds R2 (Cloudflare) as a direct upload target alongside B2 (cold archive).
When ACB_R2_* credentials are configured, the worker uploads replays and
thumbnails to R2 immediately after each match, bypassing the index-builder's
B2→R2 promotion cycle.

This is necessary because ARMOR's B2 app key is write-only; reads via the
direct S3 path return 403. The Cloudflare CDN read path (armor-hub-b2.ardenone.com)
is dead post-hub-decommission. Direct R2 upload ensures replays are available
without waiting for a working B2 read path.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-30 10:24:47 -04:00