@chrischall/warapp-mcp 0.5.0 → 0.7.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.
Files changed (125) hide show
  1. package/.claude-plugin/marketplace.json +3 -3
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +133 -8
  4. package/dist/analysis/accept-surrender.js +20 -2
  5. package/dist/analysis/calibrate.js +1 -0
  6. package/dist/analysis/card-support.js +177 -17
  7. package/dist/analysis/card-threats.js +126 -14
  8. package/dist/analysis/chat-advice.js +182 -0
  9. package/dist/analysis/chat-blocks.js +106 -0
  10. package/dist/analysis/chat-intent.js +559 -0
  11. package/dist/analysis/chat-read.js +159 -0
  12. package/dist/analysis/decision-replay.js +279 -0
  13. package/dist/analysis/decision-truth.js +146 -0
  14. package/dist/analysis/finished-game.js +62 -3
  15. package/dist/analysis/game-lessons.js +15 -10
  16. package/dist/analysis/hold-diff.js +3 -1
  17. package/dist/analysis/income-trend.js +38 -0
  18. package/dist/analysis/intel.js +75 -0
  19. package/dist/analysis/opens.js +64 -0
  20. package/dist/analysis/opponent-profile.js +121 -10
  21. package/dist/analysis/opponents.js +246 -20
  22. package/dist/analysis/outlook.js +360 -15
  23. package/dist/analysis/picks.js +91 -12
  24. package/dist/analysis/plan/after-leftover.js +4 -1
  25. package/dist/analysis/plan/bonus-attempts.js +105 -0
  26. package/dist/analysis/plan/capture-instead.js +116 -0
  27. package/dist/analysis/plan/captures.js +49 -7
  28. package/dist/analysis/plan/conceded-out.js +13 -2
  29. package/dist/analysis/plan/estimates.js +203 -30
  30. package/dist/analysis/plan/guard-off.js +2 -1
  31. package/dist/analysis/plan/holds.js +110 -9
  32. package/dist/analysis/plan/jabs.js +113 -0
  33. package/dist/analysis/plan/leftover.js +393 -8
  34. package/dist/analysis/plan/orders.js +76 -4
  35. package/dist/analysis/plan/relations.js +135 -2
  36. package/dist/analysis/plan/report.js +69 -3
  37. package/dist/analysis/plan/second-sizing.js +133 -0
  38. package/dist/analysis/plan/shared.js +279 -27
  39. package/dist/analysis/plan/siege.js +303 -0
  40. package/dist/analysis/plan/stand-still.js +9 -5
  41. package/dist/analysis/plan/state.js +8 -0
  42. package/dist/analysis/plan/stockpile.js +104 -0
  43. package/dist/analysis/plan/strike.js +39 -7
  44. package/dist/analysis/plan/targets.js +256 -110
  45. package/dist/analysis/plan/threats.js +148 -10
  46. package/dist/analysis/plan/transfers.js +57 -6
  47. package/dist/analysis/plan/two-move.js +70 -0
  48. package/dist/analysis/plan/vacated.js +124 -2
  49. package/dist/analysis/planner.js +527 -49
  50. package/dist/analysis/profile.js +13 -2
  51. package/dist/analysis/since-last.js +18 -5
  52. package/dist/analysis/stress.js +8 -2
  53. package/dist/board.js +36 -2
  54. package/dist/bonus-attempt-store.js +55 -0
  55. package/dist/build-id.js +106 -0
  56. package/dist/bundle.js +54138 -41059
  57. package/dist/cards.js +7 -5
  58. package/dist/chat-automation.js +22 -0
  59. package/dist/chat.js +35 -1
  60. package/dist/client.js +14 -0
  61. package/dist/commerce.js +111 -0
  62. package/dist/decision-log.js +1093 -0
  63. package/dist/finished-store.js +5 -1
  64. package/dist/index.js +9 -1
  65. package/dist/lessons-store.js +322 -21
  66. package/dist/live.js +43 -0
  67. package/dist/move-order.js +64 -3
  68. package/dist/normalize.js +17 -0
  69. package/dist/open-games.js +36 -16
  70. package/dist/outlook-log.js +42 -1
  71. package/dist/profile-store.js +12 -2
  72. package/dist/protocol/methods.js +62 -5
  73. package/dist/protocol/rpc.js +8 -0
  74. package/dist/relations-store.js +38 -4
  75. package/dist/review-export.js +198 -0
  76. package/dist/siege-store.js +49 -0
  77. package/dist/state-bundle.js +584 -0
  78. package/dist/state-cli.js +71 -0
  79. package/dist/strategy/notes.js +56 -0
  80. package/dist/team-cards.js +25 -0
  81. package/dist/tools/accept-surrender.js +23 -9
  82. package/dist/tools/award.js +20 -2
  83. package/dist/tools/card-advice.js +93 -28
  84. package/dist/tools/card-plays.js +2 -2
  85. package/dist/tools/card-support-advice.js +37 -7
  86. package/dist/tools/chat-advice.js +330 -0
  87. package/dist/tools/chat-inbox.js +19 -4
  88. package/dist/tools/chat.js +106 -17
  89. package/dist/tools/check-plan.js +68 -12
  90. package/dist/tools/commerce-deploy.js +33 -0
  91. package/dist/tools/common.js +31 -0
  92. package/dist/tools/decision-capture.js +459 -0
  93. package/dist/tools/end-of-game.js +151 -21
  94. package/dist/tools/game-kind.js +92 -0
  95. package/dist/tools/how-lost.js +7 -2
  96. package/dist/tools/index.js +10 -0
  97. package/dist/tools/join-game.js +10 -4
  98. package/dist/tools/live-context.js +6 -0
  99. package/dist/tools/live.js +10 -1
  100. package/dist/tools/map-curves.js +73 -0
  101. package/dist/tools/notifications.js +4 -3
  102. package/dist/tools/opponents.js +2 -0
  103. package/dist/tools/past-games.js +74 -8
  104. package/dist/tools/picks.js +61 -10
  105. package/dist/tools/profile-hook.js +62 -3
  106. package/dist/tools/profile-opponents.js +4 -0
  107. package/dist/tools/push.js +2 -2
  108. package/dist/tools/relations.js +139 -0
  109. package/dist/tools/scout.js +16 -2
  110. package/dist/tools/state.js +232 -0
  111. package/dist/tools/strategy.js +3 -3
  112. package/dist/tools/stress-opponents.js +7 -2
  113. package/dist/tools/surrender-pending.js +10 -1
  114. package/dist/tools/surrender.js +18 -3
  115. package/dist/tools/survival.js +11 -3
  116. package/dist/tools/team-brief.js +2 -2
  117. package/dist/tools/team.js +12 -3
  118. package/dist/tools/turn.js +643 -76
  119. package/dist/tools/where.js +3 -1
  120. package/dist/version.js +1 -1
  121. package/manifest.json +32 -3
  122. package/mint.yaml +37 -4
  123. package/package.json +4 -4
  124. package/server.json +20 -2
  125. package/skills/warapp-mcp/SKILL.md +73 -17
@@ -2,9 +2,9 @@ import { holdsSeat } from '../player-states.js';
2
2
  import { carriesTeam, isTeamGame } from '../teams.js';
3
3
  import { neutralizeSeats, staticSeats } from './static-seats.js';
4
4
  import { bonusTerritories } from './bonuses.js';
5
- import { DEFAULT_DEFENSIVE_KILL_RATE, DEFAULT_OFFENSIVE_KILL_RATE, straightKills } from './combat.js';
5
+ import { DEFAULT_DEFENSIVE_KILL_RATE, DEFAULT_OFFENSIVE_KILL_RATE, guaranteedKills, luckFraction, straightKills } from './combat.js';
6
6
  import { bfsDistances, neighborsOf } from './graph.js';
7
- import { bombPossibleOn, enemyDeployEstimates, failedAttackKills, frontierCoverage, holdLosses, localFreeOn, observeSigned, ownerShown, repelledFloor, coverageText, LOW_COVERAGE, LOW_COVERAGE_FACTOR, TRACKED_MIN_TURNS, trackCoverage, visibleState, } from './planner.js';
7
+ import { bombPossibleOn, captureFloor, enemyDeployEstimates, failedAttackKills, frontierCoverage, holdLosses, localFreeOn, observeSigned, ownerShown, repelledFloor, coverageText, LOW_COVERAGE, TRACKED_MIN_TURNS, trackCoverage, visibleState, } from './planner.js';
8
8
  import { boardIncome, DEFAULT_BASE_INCOME } from './player.js';
9
9
  /** #164: a stack is large when its armies are at least this many turns of its visible income. */
10
10
  const LOST_STACK_INCOMES = 2;
@@ -35,8 +35,8 @@ const NEUTRAL_CAPTURE_LOSS = 1;
35
35
  /**
36
36
  * {@link TrackGame} from GameFeed's players and settings: each player's starting armies are
37
37
  * TerritoryLimit × InitialPlayerArmiesPerTerritory, unless either is missing, the game uses full
38
- * distribution (DistributionMode -1: every territory is split among the players, TerritoryLimit
39
- * ignored — unverified, docs/WARAPP-API.md) or a map distribution that is not `normal` (a
38
+ * distribution ({@link fullDistribution}, #428: DistributionMode 0 with no TerritoryLimit — every
39
+ * territory is split among the players; -1 is a limited start) or a map distribution that is not `normal` (a
40
40
  * scenario sets its own starts). A Sanctions card is an income caveat: it can cut income below
41
41
  * the base the bound counts.
42
42
  */
@@ -57,7 +57,7 @@ endRules) {
57
57
  const seats = [...staticSeats(list, endRules).keys()].sort(byNum);
58
58
  const base = { players: list, ...(seats.length ? { staticSeats: seats } : {}), ...(caveats.length ? { incomeCaveats: caveats } : {}) };
59
59
  const perTerritory = settings?.initialPlayerArmiesPerTerritory !== undefined ? { startArmiesPerTerritory: Math.max(0, settings.initialPlayerArmiesPerTerritory) } : {};
60
- if (settings?.distributionMode === FULL_DISTRIBUTION)
60
+ if (fullDistribution(settings))
61
61
  return {
62
62
  ...base,
63
63
  startUnknown: 'the game uses full distribution, which splits every territory among the players whatever the territory limit',
@@ -87,8 +87,20 @@ export function staticSeatsFor(game, me) {
87
87
  }
88
88
  /** TerritoryLimit assumed when the settings do not say (war.app's usual default). */
89
89
  export const DEFAULT_TERRITORY_LIMIT = 3;
90
- /** GameFeed's DistributionMode for full distribution (unverified, docs/WARAPP-API.md). */
91
- const FULL_DISTRIBUTION = -1;
90
+ /**
91
+ * GameFeed's DistributionMode for full distribution: the client's id (docs/WARAPP-API.md, #235). #428:
92
+ * -1 is a limited start (the client's random warlords: TerritoryLimit territories each), verified on
93
+ * three finished games, where reading it as full distribution put an opponent's start at 189 for a real 9.
94
+ */
95
+ const FULL_DISTRIBUTION = 0;
96
+ /**
97
+ * #428: the game deals every territory among the players — DistributionMode 0 with no TerritoryLimit
98
+ * (unset or 0: the client's "no cap"). Mode 0 with a TerritoryLimit stays a limited start (the client
99
+ * caps each player's share by the limit; that a full-distribution game never sends one is unverified).
100
+ */
101
+ export function fullDistribution(settings) {
102
+ return settings?.distributionMode === FULL_DISTRIBUTION && !(settings.territoryLimit !== undefined && settings.territoryLimit > 0);
103
+ }
92
104
  /** GameFeed's DistributionMode for a custom scenario (the client's id; unverified on GameFeed, docs/WARAPP-API.md). */
93
105
  const CUSTOM_SCENARIO = -3;
94
106
  /**
@@ -118,6 +130,15 @@ export function symmetricStart(settings) {
118
130
  /** Player states still in the game (absent counts as playing), as the profile counts them. */
119
131
  const playing = (p) => (p.state ?? 'Playing') === 'Playing';
120
132
  const byNum = (a, b) => a - b;
133
+ /**
134
+ * #418: a rise no turn read in full confirmed ({@link confirmedBy}) is held to this many times the
135
+ * income carried before it, each turn: walk-ins beside the fog are read up to twice the carry (#123),
136
+ * so without it they compounded (10, 20, 40, …) while nothing backed them. Unmeasured beyond #418's
137
+ * calibration run.
138
+ */
139
+ export const RISE_CAP = 1.3;
140
+ /** #418: my armies in view against its own, at or above which I lead it on the board ({@link ObservedTurn.leadIncome}). */
141
+ export const LEAD_ARMIES = 3;
121
142
  /**
122
143
  * Income over a window of observed turns (#99). Each observed deploy is a LOWER BOUND on that
123
144
  * turn's income: deploys onto fogged territories are unseen, and so are armies moved into the
@@ -168,6 +189,16 @@ export function incomeBasis(t) {
168
189
  : `carried from ${span}, ${range}: no bonus it visibly held has been visibly broken since`;
169
190
  if (t.incomeMethod === 'max-observed')
170
191
  return `max observed over ${span}, ${range}`;
192
+ if (t.incomeMethod === 'rise-capped')
193
+ return t.incomeRise
194
+ ? t.incomeRise.proven !== undefined
195
+ ? `max observed over ${span}, ${range}, held at the +${t.incomeRise.proven} a capture with every source in view proved (above ${RISE_CAP}× the ~${t.incomeRise.before} before it): no turn read in full reached ${t.incomeRise.read}`
196
+ : `max observed over ${span}, ${range}, held to ${RISE_CAP}× the ~${t.incomeRise.before} before it: no turn read in full reached ${t.incomeRise.read}`
197
+ : `max observed over ${span}, ${range}, a rise held to ${RISE_CAP}× a turn`;
198
+ if (t.incomeMethod === 'tripped')
199
+ return t.incomeTrip
200
+ ? `stepped down from ~${t.incomeTrip.from} toward +${t.incomeTrip.proven}: every army it could deploy was in view on turn ${t.incomeTrip.turn}, which read +${t.incomeTrip.proven} (one ${RISE_CAP}× step a turn, never below that reading; ${span}, ${range})`
201
+ : `stepped down toward a turn read with every army it could deploy in view (${span}, ${range})`;
171
202
  if (t.incomeMethod === 'stock-flow')
172
203
  return `visible army growth plus losses (fog arrivals left out) over ${span}, ${range}`;
173
204
  if (t.incomeMethod === 'fog-captures')
@@ -507,13 +538,96 @@ function fogCapturePattern(observed) {
507
538
  const income = Math.min(last.cap, Math.ceil(armies / (last.from - hits[0].from + 1)));
508
539
  return { turns: hits.map((x) => x.from), armies, income };
509
540
  }
541
+ /**
542
+ * #418: {@link trackedIncome} with the tripwire's ceiling on the readings of the turns it covers: those
543
+ * count at most its value, the later ones whole; method `tripped` when that lowers the figure.
544
+ */
545
+ function ceiledIncome(window, visibleIncome, ceiling) {
546
+ const est = trackedIncome(window, visibleIncome);
547
+ if (!ceiling || !window.some((x) => x.from <= ceiling.through))
548
+ return est;
549
+ const later = window.filter((x) => x.from > ceiling.through);
550
+ const income = Math.max(visibleIncome, Math.min(est.income, ceiling.value), later.length ? trackedIncome(later, visibleIncome).income : 0);
551
+ return income >= est.income ? est : { ...est, income, method: 'tripped' };
552
+ }
553
+ /**
554
+ * #418, the over-read tripwire (owner decision of 2026-11-20): proof may lower an estimate only when every
555
+ * army the opponent could have deployed that turn is accounted for in view. Its deploy lands only on its
556
+ * own territories, so the turn is read in full when every territory it held on either board shows its
557
+ * armies on both, nothing beside them hides its owner on either board (#294's whole frontier read: a
558
+ * territory of its cut off in deep fog, beside none of its land in view, is the one gap), and nothing
559
+ * beside it could hide a fight that cost it armies the board does not show: no third player beside it
560
+ * (another's attack, or a failed attack of its on another, is unseen), no neutral beside it that shrank or
561
+ * changed hands (a failed attack of its), no territory of its lost, and — beside my land — my orders known
562
+ * in full (`sent` and `cards`, so `measureOrders` counts what my attacks killed and what its failed attacks
563
+ * on me cost) with no card of mine on its land. A failed attack must always show: one army kills at least
564
+ * one defender ({@link guaranteedKills}); with a capture, luck is off under straight rounding (else its cost
565
+ * varies). Not in a commerce game with an army price (it may save gold rather than deploy).
566
+ */
567
+ export function everyArmyInView(g, before, after, playerId, me, orders, settings = {}) {
568
+ if (settings.armyCostMultiplier)
569
+ return false;
570
+ const was = new Map(before.territories.map((t) => [t.territoryId, t]));
571
+ const now = new Map(after.territories.map((t) => [t.territoryId, t]));
572
+ const its = (t) => t?.owner.kind === 'player' && t.owner.playerId === playerId;
573
+ const land = new Set();
574
+ for (const id of g.territoryById.keys())
575
+ if (its(ownerShown(was.get(id))) || its(ownerShown(now.get(id))))
576
+ land.add(id);
577
+ if (!land.size)
578
+ return false;
579
+ let captured = false;
580
+ for (const id of land) {
581
+ const b = visibleState(was.get(id));
582
+ const a = visibleState(now.get(id));
583
+ if (!b || !a || !its(a))
584
+ return false;
585
+ if (!its(b))
586
+ captured = true;
587
+ }
588
+ let bordersMe = false;
589
+ for (const id of land)
590
+ for (const n of neighborsOf(g, id)) {
591
+ if (land.has(n))
592
+ continue;
593
+ const b = ownerShown(was.get(n));
594
+ const a = ownerShown(now.get(n));
595
+ if (!b || !a)
596
+ return false;
597
+ for (const o of [b.owner, a.owner]) {
598
+ if (o.kind !== 'player')
599
+ continue;
600
+ if (o.playerId !== me)
601
+ return false;
602
+ bordersMe = true;
603
+ }
604
+ if (b.owner.kind === 'neutral') {
605
+ const vb = visibleState(b);
606
+ const va = visibleState(a);
607
+ if (a.owner.kind !== 'neutral' || !vb || !va || va.armies < vb.armies)
608
+ return false;
609
+ }
610
+ }
611
+ if (bordersMe) {
612
+ if (!orders?.sent || !orders.cards)
613
+ return false;
614
+ if (orders.cards.some((c) => [c.from, c.to, c.target].some((t) => t !== undefined && land.has(t))))
615
+ return false;
616
+ }
617
+ const luck = luckFraction(settings.luckModifier);
618
+ const straight = (settings.roundingMode ?? 'StraightRound') === 'StraightRound';
619
+ if (captured && (luck > 0 || !straight))
620
+ return false;
621
+ return guaranteedKills(1, settings.offensiveKillRate ?? DEFAULT_OFFENSIVE_KILL_RATE, settings.luckModifier, settings.roundingMode) >= 1;
622
+ }
510
623
  /**
511
624
  * #123 review: a turn read in full — no ambiguous army was read as a move out of the fog (the
512
- * bound cut nothing) — whose observed deploy reaches `income` confirms it. A figure only bound-cut
625
+ * bound cut nothing) — whose observed deploy reaches `income` confirms it, and so does a pattern of
626
+ * fog captures (#271) or a capture floor (#418, {@link ObservedTurn.captureFloor}) that reaches it. A figure only bound-cut
513
627
  * readings reached (each up to twice the income carried so far) is a guess, however often it
514
628
  * compounded.
515
629
  */
516
- const confirmedBy = (window, income) => window.some((x) => (x.fogArmies === 0 && x.observed >= income) || (x.fogCaptures?.income ?? 0) >= income);
630
+ const confirmedBy = (window, income) => window.some((x) => (x.fogArmies === 0 && x.observed >= income) || (x.fogCaptures?.income ?? 0) >= income || (x.captureFloor ?? 0) >= income);
517
631
  /**
518
632
  * #119: no decay. Walk the observed turns: each window's figure (as `trackedIncome` reads it)
519
633
  * replaces the carried one only when larger; otherwise the carried figure stays, less the
@@ -526,9 +640,22 @@ const confirmedBy = (window, income) => window.some((x) => (x.fogArmies === 0 &&
526
640
  * staying pinned). The trade-off: a hidden income beyond twice the visible one, learnt only from
527
641
  * bound-cut readings (section 7's +20 read 10, 20, …), is dropped the first time it deploys into
528
642
  * the fog and shows less in full — unless a turn read in full had already confirmed it.
643
+ *
644
+ * #418: while I lead it ({@link ObservedTurn.leadIncome} above the carry before the turn, or that
645
+ * turn's visible income when larger — not above the reading, which an over-read would pass), a window
646
+ * figure above the carry that no turn read in full confirmed rises at most to {@link RISE_CAP} × that
647
+ * figure, rounded up; on a turn read in full that observed less than the carry it does not rise at all.
648
+ * #418: never below a capture floor in the window (proof may always raise an estimate).
649
+ *
650
+ * #418, the over-read tripwire: on a turn with every army it could have deployed in view
651
+ * ({@link ObservedTurn.accounted}), a carry above that turn's reading steps down at most one step,
652
+ * to ⌈carry ÷ {@link RISE_CAP}⌉, never below the reading nor its visible income; the readings of the
653
+ * turns up to it then count at most the stepped figure in every later window ({@link ceiledIncome}),
654
+ * so the next turn does not read the over-read back.
529
655
  */
530
656
  function carryIncome(observed) {
531
657
  let carry = null;
658
+ let ceiling = null;
532
659
  /**
533
660
  * #271 (2): the latest pattern of captures that needed attackers out of sight, as a carry: it is
534
661
  * evidence, not a bound-cut guess, so no later turn read in full disproves it; like any carry it
@@ -538,7 +665,7 @@ function carryIncome(observed) {
538
665
  for (let i = 0; i < observed.length; i++) {
539
666
  const x = observed[i];
540
667
  const window = observed.slice(Math.max(0, i - INCOME_TURNS + 1), i + 1);
541
- const est = trackedIncome(window, 0);
668
+ const est = ceiledIncome(window, 0, ceiling);
542
669
  const fresh = { income: est.income, set: est.income, window, est, confirmed: confirmedBy(window, est.income) };
543
670
  if (pattern)
544
671
  pattern = { ...pattern, income: Math.min(x.cap, pattern.income - x.brokenValue) };
@@ -552,12 +679,37 @@ function carryIncome(observed) {
552
679
  else {
553
680
  const disproved = !prev.confirmed && x.fogArmies === 0 && x.observed < prev.income;
554
681
  const kept = Math.min(x.cap, prev.income - x.brokenValue);
555
- carry = disproved || est.income >= kept ? fresh : { ...prev, income: kept };
682
+ if (!(disproved || est.income >= kept))
683
+ carry = { ...prev, income: kept };
684
+ else {
685
+ const before = Math.max(prev.income, x.visibleIncome);
686
+ // #418: proof may always raise an estimate: the hold never goes below a capture floor in the window.
687
+ const proof = Math.max(0, ...window.map((w) => w.captureFloor ?? 0));
688
+ const limit = Math.max(disproved ? prev.income : Math.ceil(before * RISE_CAP - 1e-9), proof);
689
+ // #418 review: the lead is judged against the carry before the rise (or its visible income), never
690
+ // against the reading being judged: an over-read above my income must not switch its own cap off.
691
+ carry =
692
+ (x.leadIncome ?? 0) > before && !fresh.confirmed && fresh.income > limit
693
+ ? { ...fresh, income: limit, set: limit, rise: { read: fresh.income, before, ...(proof >= limit ? { proven: proof } : {}) } }
694
+ : fresh;
695
+ }
556
696
  }
557
697
  if (pattern && pattern.income > carry.income)
558
698
  carry = pattern;
699
+ // #418, the over-read tripwire: on a turn with every army it could deploy in view, the carry steps
700
+ // down at most one RISE_CAP step toward that turn's reading, never below it (nor its visible income).
701
+ // It steps `carry`, never `pattern`: a #271 fog-capture pattern above the stepped figure is restored by
702
+ // the line above on the next turn, so a pattern-set reading is lowered for that one turn only.
703
+ if (x.accounted && carry.income > x.observed) {
704
+ const step = Math.max(x.observed, x.visibleIncome, Math.ceil(carry.income / RISE_CAP - 1e-9));
705
+ if (step < carry.income) {
706
+ const trip = { from: carry.income, proven: x.observed, turn: x.from };
707
+ carry = { ...carry, income: step, set: step, trip };
708
+ ceiling = { value: step, through: x.from, trip };
709
+ }
710
+ }
559
711
  }
560
- return carry;
712
+ return carry && ceiling ? { ...carry, ceiling } : carry;
561
713
  }
562
714
  /** #250: a turn that reads more than this many times the most it was seen to earn before it is a jump. */
563
715
  export const INCOME_JUMP_FACTOR = 2;
@@ -728,6 +880,29 @@ export function incomeJumpText(j, income, earlier) {
728
880
  tail = `${j.verdict === 'reveal' ? `: more only counting the ${j.cameIntoView} armies that came into view` : ': no more'}.${k ? ` ${rate[0].toUpperCase()}${rate.slice(1)} it could have received up to ${k.received} by turn ${j.turn} ${from}, enough for the ${had}.` : ''}`;
729
881
  return `${lead}${cut}${amb}${sum}${tail}`;
730
882
  }
883
+ /**
884
+ * #420: the most armies one of `playerId`'s stacks beside my land grew over one turn (board `before` →
885
+ * `after`): each territory its own on both boards, shown with its armies on both (`visibleState`), and
886
+ * next to a territory of mine on `before`; a stack that shrank or stood still reads 0. `undefined` when
887
+ * there is no such stack. Growth counts deploys and armies moved in alike: what the stack facing me
888
+ * gained, the top-up a border must answer.
889
+ */
890
+ export function borderTopUp(g, before, after, playerId, me) {
891
+ const was = new Map(before.territories.map((t) => [t.territoryId, t]));
892
+ const now = new Map(after.territories.map((t) => [t.territoryId, t]));
893
+ const owns = (t, p) => t?.owner.kind === 'player' && t.owner.playerId === p;
894
+ let most;
895
+ for (const id of [...g.territoryById.keys()].sort(byNum)) {
896
+ const b = visibleState(was.get(id));
897
+ const a = visibleState(now.get(id));
898
+ if (!owns(b, playerId) || !owns(a, playerId))
899
+ continue;
900
+ if (!neighborsOf(g, id).some((n) => owns(ownerShown(was.get(n)), me)))
901
+ continue;
902
+ most = Math.max(most ?? 0, a.armies - b.armies);
903
+ }
904
+ return most;
905
+ }
731
906
  /**
732
907
  * Per opponent: its observed deploys turn by turn, an income estimate, the armies that must be
733
908
  * in the fog and the bonuses that could explain an income gap. Those visible on the newest
@@ -741,9 +916,15 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
741
916
  // One board per turn (the last given wins), oldest first.
742
917
  const byTurn = new Map();
743
918
  const ordersOn = new Map();
919
+ /** #420: the turns whose board came from GameFeed history (army counts unverified). */
920
+ const fromHistory = new Set();
744
921
  for (const b of boards) {
745
922
  byTurn.set(b.turn, neutralizeSeats(b.board, seats));
746
923
  ordersOn.set(b.turn, b.orders);
924
+ if (b.source === 'history')
925
+ fromHistory.add(b.turn);
926
+ else
927
+ fromHistory.delete(b.turn);
747
928
  }
748
929
  const turns = [...byTurn.keys()].sort(byNum);
749
930
  if (!turns.length)
@@ -859,13 +1040,18 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
859
1040
  ...(expected !== null ? { expected: Math.max(expected, carriedSoFar) } : {}),
860
1041
  // #293: the same Local Deployment reading the board-only observation used.
861
1042
  ...(settings.localDeployments ? { localFree: localFreeOn(e.base, DEFAULT_BASE_INCOME, settings.opponentReinforcement, from) } : {}),
1043
+ // #418: a Bomb it may have played halves a capture's defenders before the proof reads them.
1044
+ ...(bombPossibleOn(settings, from) ? { bomb: true } : {}),
862
1045
  };
863
1046
  const { signed, ...o } = expected !== null && carriedSoFar > expected
864
1047
  ? observeSigned(g, after, before, playerId, settings, context)
865
1048
  : { ...e.observation, signed: e.observedRaw };
866
1049
  // #271 (1): a failed attack on a hold of mine bounds its deploy from below, fog or not.
867
1050
  const repelled = orders ? repelledFloor(g, before, after, me, playerId, orders, settings, guard, bombPossibleOn(settings, from)) : null;
868
- const floor = repelled?.floor ?? 0;
1051
+ // #418: a capture with every source army in view proves a minimum deploy (my orders known: one
1052
+ // beside my land reads too). Proof may always raise the reading.
1053
+ const proof = captureFloor(g, before, after, playerId, settings, guard, { me, orders, bomb: bombPossibleOn(settings, from) });
1054
+ const floor = Math.max(repelled?.floor ?? 0, proof.floor);
869
1055
  const raw = Math.max(measured ? Math.max(0, observeSigned(g, after, before, playerId, settings, context).signed + measured) : Math.max(0, signed), floor);
870
1056
  const losses = Math.max(0, change.losses + measured);
871
1057
  // #123: with my orders known, what stood on an emptied capture is measured: no high reading.
@@ -873,10 +1059,28 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
873
1059
  const high = Math.max(raw, o.high + measured - (orders ? o.emptiedHigh : 0));
874
1060
  const seenObserved = Math.min(raw, e.observedCap);
875
1061
  const broken = visiblyBroken(g, before, after, playerId);
1062
+ // #418: did I lead it on the board read?
1063
+ const myLand = new Set();
1064
+ let myArmies = 0;
1065
+ let itsArmies = 0;
1066
+ for (const t of after.territories) {
1067
+ if (!g.territoryById.has(t.territoryId) || t.owner.kind !== 'player')
1068
+ continue;
1069
+ if (t.owner.playerId === me) {
1070
+ myLand.add(t.territoryId);
1071
+ myArmies += t.armies ?? 0;
1072
+ }
1073
+ else if (t.owner.playerId === playerId)
1074
+ itsArmies += t.armies ?? 0;
1075
+ }
1076
+ const leadIncome = myArmies >= LEAD_ARMIES * itsArmies ? boardIncome(g, myLand, rules).total : null;
1077
+ const topUp = fromHistory.has(from) || fromHistory.has(to) ? undefined : borderTopUp(g, before, after, playerId, me);
876
1078
  observed.push({
877
1079
  from,
878
1080
  to,
879
1081
  coverage: frontierCoverage(g, after, before, playerId),
1082
+ ...(topUp !== undefined ? { topUp } : {}),
1083
+ ...(leadIncome !== null ? { leadIncome } : {}),
880
1084
  observed: seenObserved,
881
1085
  raw,
882
1086
  ...(context.expected !== undefined ? { expected: context.expected } : {}),
@@ -898,7 +1102,9 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
898
1102
  unseenAttackers: o.unseenAttackers,
899
1103
  unseenAttackerArmies: o.unseenAttackerArmies,
900
1104
  repelled: repelled?.territories.length ? repelled : null,
901
- repelledFloor: floor,
1105
+ repelledFloor: repelled?.floor ?? 0,
1106
+ ...(proof.floor > 0 ? { proven: proof.territories, captureFloor: proof.floor } : {}),
1107
+ ...(everyArmyInView(g, before, after, playerId, me, orders, settings) ? { accounted: true } : {}),
902
1108
  combat: combatLosses(g, before, after, playerId, orders
903
1109
  ? {
904
1110
  me,
@@ -929,15 +1135,26 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
929
1135
  const turnsUnseen = visible ? 0 : lastTurn - back;
930
1136
  const turnsUnseenAtLeast = turnsUnseen > 0 && back >= 1 && !byTurn.has(back);
931
1137
  const recent = observed.slice(-INCOME_TURNS);
932
- const lastWindow = recent.length ? trackedIncome(recent, visibleIncome) : null;
933
1138
  // #119: no decay ({@link carryIncome}). The last three turns alone read a real 31 as 12 once
934
1139
  // it aged out.
935
1140
  const carry = carryIncome(observed);
1141
+ // #418: the readings an over-read tripwire stepped down from count at most its ceiling.
1142
+ const lastWindow = recent.length ? ceiledIncome(recent, visibleIncome, carry?.ceiling) : null;
936
1143
  const carried = carry !== null && lastWindow !== null && carry.income > lastWindow.income ? carry : null;
1144
+ // #418: an unconfirmed rise held below what the last window reads keeps the held figure.
1145
+ const held = !carried && carry?.rise && lastWindow !== null && carry.income < lastWindow.income ? carry : null;
937
1146
  const estimate = carried
938
- ? { ...carried.est, income: carried.income, method: 'carried' }
939
- : lastWindow;
940
- const incomeWindow = carried ? carried.window : recent;
1147
+ ? { ...carried.est, income: carried.income, method: (carried.trip ? 'tripped' : 'carried') }
1148
+ : held
1149
+ ? {
1150
+ ...held.est,
1151
+ income: Math.max(visibleIncome, held.income),
1152
+ method: (visibleIncome >= held.income ? 'visible' : 'rise-capped'),
1153
+ }
1154
+ : lastWindow;
1155
+ const incomeRise = held && estimate.method === 'rise-capped' ? held.rise : null;
1156
+ const incomeTrip = estimate?.method === 'tripped' ? (carried?.trip ?? carry?.ceiling?.trip ?? null) : null;
1157
+ const incomeWindow = carried ? carried.window : held ? held.window : recent;
941
1158
  const income = estimate?.income ?? null;
942
1159
  // #250: an income set by one turn far above the turns before it, with the evidence for it. Its
943
1160
  // starting armies, when known (under full distribution, an even share: an upper bound).
@@ -998,8 +1215,14 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
998
1215
  incomeTurns: incomeWindow.map((x) => x.from),
999
1216
  observedRange: estimate.observedRange,
1000
1217
  incomeLowered: carried ? carried.set - carried.income : 0,
1218
+ incomeRise,
1219
+ incomeTrip,
1001
1220
  });
1002
- if (carried)
1221
+ if (incomeTrip)
1222
+ notes.push(`Income ~${income} (${basis}): proof lowers an estimate only on a turn with every army it could deploy in view, and then by one step a turn (#418). Each observed deploy is a lower bound under fog.`);
1223
+ else if (incomeRise)
1224
+ notes.push(`Income ~${income} (${basis}): a rise is held to ${RISE_CAP}× a turn until a turn read in full (no army read as a move out of the fog) confirms it, since armies walking in beside the fog are read up to twice the income carried (#418). Each observed deploy is a lower bound under fog.`);
1225
+ else if (carried)
1003
1226
  notes.push(`Income ~${income} (${basis}): an income does not decay — it is kept until a bonus it visibly held is visibly broken (less its value) or it could no longer earn it; its last ${recent.length} observed turns alone give ~${lastWindow.income}. Each observed deploy is a lower bound under fog.`);
1004
1227
  else
1005
1228
  notes.push(`Income ~${income} (${basis}): the larger of the most it was observed to deploy (each observation is a lower bound under fog, so the largest is the tightest) and its visible army growth plus losses per turn, fog arrivals left out (${estimate.stockFlow}), capped at the most it could earn (${estimate.cap}), at least its visible income ${visibleIncome}.`);
@@ -1222,7 +1445,7 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
1222
1445
  // #294: what its income was read from.
1223
1446
  const covered = estimate ? trackCoverage(incomeWindow) : null;
1224
1447
  if (covered && covered.share < 1)
1225
- notes.push(`Income read from ${coverageText(covered)}: the rest of where it can deploy was out of view, so the figure is a lower bound${covered.share < LOW_COVERAGE && income > visibleIncome && observed.length >= TRACKED_MIN_TURNS ? `, and the planner keeps at least ${LOW_COVERAGE_FACTOR} × it` : ''}.`);
1448
+ notes.push(`Income read from ${coverageText(covered)}: the rest of where it can deploy was out of view, so the figure is a lower bound${covered.share < LOW_COVERAGE && observed.length >= TRACKED_MIN_TURNS ? `, and the planner sizes it toward the midpoint between it and the most it could have (#368)` : ''}.`);
1226
1449
  notes.push(...warnings);
1227
1450
  return {
1228
1451
  playerId,
@@ -1238,8 +1461,11 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
1238
1461
  observedRange: estimate?.observedRange ?? null,
1239
1462
  incomeMethod: estimate?.method ?? null,
1240
1463
  incomeLowered: carried ? carried.set - carried.income : 0,
1464
+ ...(incomeRise ? { incomeRise } : {}),
1465
+ ...(incomeTrip ? { incomeTrip } : {}),
1241
1466
  unexplained,
1242
1467
  inferred,
1468
+ topUp: observed.reduce((n, x) => (x.topUp === undefined ? n : Math.max(n ?? 0, x.topUp)), null),
1243
1469
  visible,
1244
1470
  turnsUnseen,
1245
1471
  turnsUnseenAtLeast,