@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
@@ -0,0 +1,198 @@
1
+ // The review export (#437 task 5, `scripts/review-export.mjs`, a LOCAL dev script: never a tool, never in
2
+ // CI). A strategy review reads one file per game instead of reconstructing it from six stores: this joins,
3
+ // for each game, what this server kept of it — the decision log (#412, with #437's per-turn additions, its
4
+ // events, seats, join context, opponents and picks phase), the end-of-game record (src/finished-store.ts:
5
+ // outcome, kinds, the history's truth), the game's entry in its map's record and in the lessons
6
+ // (src/lessons-store.ts, by `finishedGameKey`), the settings record (src/replay.ts: state, turns, seats by
7
+ // id) and the stored map's name and size — plus an `index.json` of every game with which sources exist.
8
+ //
9
+ // Name-free: every store it reads is (player ids, the map's own names, HMAC keys); the settings record's
10
+ // player names are never read back (`readGameRecord` puts the id in their place) and only ids are written.
11
+ // The decision log's encoded boards and the planner's replay `input` are left out (`hasInput` says whether
12
+ // a turn kept one: `scripts/replay-decisions.mjs` replays those); `boards: true` adds each logged turn's
13
+ // board rebuilt whole (`boardAt`). It makes no request and writes nothing to the data dir. The export is
14
+ // 0600 files in a 0700 folder; an existing folder others can read is refused, never chmod-ed.
15
+ import { existsSync } from 'node:fs';
16
+ import { chmod, mkdir, readdir, rename, stat, unlink, writeFile } from 'node:fs/promises';
17
+ import { join } from 'node:path';
18
+ import { randomBytes } from 'node:crypto';
19
+ import { profileFormatOf } from './analysis/opponent-profile.js';
20
+ import { boardAt, decisionLogPath, decisionsDir, readDecisionLog, } from './decision-log.js';
21
+ import { finishedPath, readFinishedRecord } from './finished-store.js';
22
+ import { lessonsPath, mapRecordPath, readLessons, readMapRecord, } from './lessons-store.js';
23
+ import { readStoredMap } from './map-store.js';
24
+ import { finishedGameKey, playerRecordSalt } from './player-record-store.js';
25
+ import { gameRecordPath, gamesDir, readGameRecord } from './replay.js';
26
+ import { readCachedPlayer } from './trend.js';
27
+ const idsIn = async (folder) => (await readdir(folder).catch(() => [])).filter((n) => /^\d{1,12}\.json$/.test(n)).map((n) => Number(n.slice(0, -5)));
28
+ /** Every game with a decision log (readable or not) or a settings record, ascending. Never throws. */
29
+ export async function reviewGameIds(dir) {
30
+ const ids = new Set([...(await idsIn(decisionsDir(dir))), ...(await idsIn(gamesDir(dir)))]);
31
+ return [...ids].sort((a, b) => a - b);
32
+ }
33
+ function reviewTurns(log, boards) {
34
+ const out = {};
35
+ for (const [k, e] of Object.entries(log.turns)) {
36
+ const { board, input, ...rest } = e;
37
+ const t = { ...rest, hasInput: input !== undefined };
38
+ if (boards && board)
39
+ t.board = boardAt(log, Number(k)) ?? null;
40
+ out[k] = t;
41
+ }
42
+ return out;
43
+ }
44
+ const gameOf = (g) => ({
45
+ state: g.state,
46
+ ...(g.numberOfTurns !== undefined ? { numberOfTurns: g.numberOfTurns } : {}),
47
+ players: g.players.map((p) => ({
48
+ id: p.id,
49
+ ...(p.team !== undefined ? { team: p.team } : {}),
50
+ ...(p.state !== undefined ? { state: p.state } : {}),
51
+ ...(p.isAI ? { ai: true } : {}),
52
+ })),
53
+ });
54
+ const newestTurn = (log) => {
55
+ const ks = Object.keys(log.turns).filter((k) => /^\d+$/.test(k)).map(Number);
56
+ return ks.length ? Math.max(...ks) : null;
57
+ };
58
+ /**
59
+ * The finished record for `gameId`: tried as `me` when known, else as each seat of the settings record
60
+ * (a legacy game keeps no log or board cache naming my account; the record's name is an HMAC, so only a
61
+ * seat id can match it). Never throws.
62
+ */
63
+ async function findFinished(dir, salt, gameId, me, game) {
64
+ if (!salt)
65
+ return { record: 'missing' };
66
+ const candidates = me !== undefined ? [me] : (game?.players.map((p) => p.id) ?? []);
67
+ for (const id of candidates) {
68
+ const r = await readFinishedRecord(finishedPath(dir, salt, id, gameId));
69
+ if (r !== 'missing')
70
+ return { me: id, record: r };
71
+ }
72
+ return { record: 'missing' };
73
+ }
74
+ /** What a store keyed by `finishedGameKey` holds for the game: its entry, or that it was counted apart. */
75
+ function keyedEntry(path, store, key) {
76
+ if (store === 'unreadable')
77
+ return { state: 'unreadable', value: null };
78
+ if (!key || !existsSync(path))
79
+ return { state: 'missing', value: null };
80
+ const entry = store.entries.find((e) => e.key === key);
81
+ if (entry)
82
+ return { state: 'present', value: { entry } };
83
+ const apart = store.excluded?.keys.find((k) => k.key === key);
84
+ if (apart)
85
+ return { state: 'excluded', value: { entry: null, excluded: apart.kind } };
86
+ return { state: 'missing', value: null };
87
+ }
88
+ /** One game joined from the data dir; `undefined` when it has neither a decision log nor a settings record. Never throws. */
89
+ export async function reviewGame(dir, gameId, opts = {}) {
90
+ const logExists = existsSync(decisionLogPath(dir, gameId));
91
+ const log = logExists ? await readDecisionLog(dir, gameId) : undefined;
92
+ const game = await readGameRecord(dir, gameId);
93
+ if (!logExists && !game && !existsSync(gameRecordPath(dir, gameId)))
94
+ return undefined;
95
+ const salt = await playerRecordSalt(dir).catch(() => undefined);
96
+ const cached = log?.playerId ?? (await readCachedPlayer(dir, gameId));
97
+ const found = await findFinished(dir, salt, gameId, cached, game);
98
+ const me = cached ?? found.me;
99
+ const finished = typeof found.record === 'object' ? found.record : null;
100
+ const mapId = finished?.mapId ?? game?.settings?.mapId;
101
+ const stored = mapId !== undefined ? await readStoredMap(dir, mapId, { maxAgeMs: Number.POSITIVE_INFINITY }) : undefined;
102
+ const key = salt && me !== undefined ? finishedGameKey(salt, me, gameId) : undefined;
103
+ const mapRecord = mapId !== undefined ? keyedEntry(mapRecordPath(dir, mapId), await readMapRecord(dir, mapId), key) : { state: 'missing', value: null };
104
+ const lessons = keyedEntry(lessonsPath(dir), await readLessons(dir), key);
105
+ const format = finished?.format ?? log?.join?.flags.format ?? (game?.players.length ? profileFormatOf(game.players) : null);
106
+ return {
107
+ version: 1,
108
+ gameId,
109
+ playerId: me ?? null,
110
+ kinds: finished?.kinds ?? null,
111
+ format,
112
+ map: mapId !== undefined ? { id: mapId, ...(stored ? { name: stored.name, territories: stored.territories.length } : {}) } : null,
113
+ outcome: finished?.outcome ?? null,
114
+ turns: finished?.outcome?.turn ?? game?.numberOfTurns ?? (log ? newestTurn(log) : null),
115
+ game: game ? gameOf(game) : null,
116
+ decisionLog: log
117
+ ? {
118
+ status: log.status,
119
+ turns: reviewTurns(log, opts.boards === true),
120
+ ...(log.accuracySummary ? { accuracySummary: log.accuracySummary } : {}),
121
+ ...(log.truthMissing ? { truthMissing: log.truthMissing } : {}),
122
+ }
123
+ : null,
124
+ events: log?.events ?? null,
125
+ ...(log?.eventsDropped ? { eventsDropped: log.eventsDropped } : {}),
126
+ seats: log?.seats ?? null,
127
+ join: log?.join ?? null,
128
+ opponents: log?.opponents ?? null,
129
+ picks: log?.picks ?? null,
130
+ finished,
131
+ mapRecord: mapRecord.value,
132
+ lessons: lessons.value,
133
+ sources: {
134
+ decisionLog: log ? 'present' : logExists ? 'unreadable' : 'missing',
135
+ finished: found.record === 'unreadable' ? 'unreadable' : finished ? 'present' : 'missing',
136
+ settingsRecord: game ? 'present' : 'missing',
137
+ map: stored ? 'present' : 'missing',
138
+ mapRecord: mapRecord.state,
139
+ lessons: lessons.state,
140
+ },
141
+ };
142
+ }
143
+ const indexEntry = (g) => ({
144
+ gameId: g.gameId,
145
+ kinds: g.kinds,
146
+ format: g.format,
147
+ mapId: g.map?.id ?? null,
148
+ mapName: g.map?.name ?? null,
149
+ outcome: g.outcome?.result ?? null,
150
+ turns: g.turns,
151
+ sources: g.sources,
152
+ });
153
+ /** The export folder: made 0700, or an existing one only when no one else can read it. Throws otherwise. */
154
+ async function privateFolder(out) {
155
+ const s = await stat(out).catch(() => undefined);
156
+ if (!s) {
157
+ await mkdir(out, { recursive: true, mode: 0o700 });
158
+ // `mode` is masked by the umask: set it outright on the folder made here.
159
+ await chmod(out, 0o700);
160
+ return;
161
+ }
162
+ if (!s.isDirectory())
163
+ throw new Error(`${out} is not a directory`);
164
+ if (s.mode & 0o077)
165
+ throw new Error(`${out} is readable by others: use a new folder or chmod 700 it`);
166
+ }
167
+ async function writePrivate(path, body) {
168
+ const tmp = `${path}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`;
169
+ try {
170
+ await writeFile(tmp, `${JSON.stringify(body, null, 2)}\n`, { mode: 0o600 });
171
+ await chmod(tmp, 0o600);
172
+ await rename(tmp, path);
173
+ }
174
+ catch (e) {
175
+ await unlink(tmp).catch(() => { });
176
+ throw e;
177
+ }
178
+ }
179
+ /**
180
+ * Write `game-<id>.json` per game (every game `reviewGameIds` lists, or `game` alone) and `index.json`
181
+ * into `out`. Throws when the folder is not private or `game` has no data.
182
+ */
183
+ export async function exportReview(dir, out, opts = {}) {
184
+ const ids = opts.game !== undefined ? [opts.game] : await reviewGameIds(dir);
185
+ const games = [];
186
+ for (const id of ids) {
187
+ const g = await reviewGame(dir, id, opts);
188
+ if (g)
189
+ games.push(g);
190
+ }
191
+ if (opts.game !== undefined && !games.length)
192
+ throw new Error(`game ${opts.game}: no data (no decision log or settings record)`);
193
+ await privateFolder(out);
194
+ for (const g of games)
195
+ await writePrivate(join(out, `game-${g.gameId}.json`), g);
196
+ await writePrivate(join(out, 'index.json'), { version: 1, games: games.map(indexEntry) });
197
+ return { games: games.length, out };
198
+ }
@@ -0,0 +1,49 @@
1
+ // The siege's memory on disk (#382): per game and account, at `<dataDir>/siege/<gameId>-<playerId>.json`,
2
+ // the target and each planned siege turn (the target's income, mine, the bonuses that turn's plan broke).
3
+ // Ids and numbers only, never a name; 0600 in a 0700 directory, written atomically under the in-process
4
+ // path lock. Written by warapp_plan_turn under `goal: 'siege'` (not with `enemy_deploy`, a what-if) and
5
+ // read back as `PlanInput.siegeMemory`. Reads are loose: an unusable file is no memory, an unusable turn
6
+ // is dropped; nothing here throws.
7
+ import { randomUUID } from 'node:crypto';
8
+ import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
9
+ import { join } from 'node:path';
10
+ import { withPathLock } from './path-lock.js';
11
+ const isRec = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
12
+ const isInt = (v) => typeof v === 'number' && Number.isSafeInteger(v);
13
+ const isNum = (v) => typeof v === 'number' && Number.isFinite(v);
14
+ export const siegeDir = (dir) => join(dir, 'siege');
15
+ export const siegePath = (dir, gameId, playerId) => join(siegeDir(dir), `${gameId}-${playerId}.json`);
16
+ function turnOf(v) {
17
+ if (!isRec(v) || !isInt(v.turn) || !isNum(v.opponentIncome) || !isNum(v.myIncome))
18
+ return undefined;
19
+ const breaks = Array.isArray(v.breaks) && v.breaks.every(isInt) ? [...v.breaks] : [];
20
+ return { turn: v.turn, opponentIncome: v.opponentIncome, myIncome: v.myIncome, breaks };
21
+ }
22
+ /** The siege memory kept for `gameId` as `playerId`; `undefined` when none or unusable. Never throws. */
23
+ export async function readSiegeMemory(dir, gameId, playerId) {
24
+ try {
25
+ const c = JSON.parse(await readFile(siegePath(dir, gameId, playerId), 'utf8'));
26
+ if (!isRec(c) || c.gameId !== gameId || c.playerId !== playerId || !isInt(c.target) || !Array.isArray(c.turns))
27
+ return undefined;
28
+ return { target: c.target, turns: c.turns.map(turnOf).filter((t) => t !== undefined) };
29
+ }
30
+ catch {
31
+ return undefined;
32
+ }
33
+ }
34
+ /** Write the siege memory, atomically, 0600. `true` when written; never throws. */
35
+ export async function writeSiegeMemory(dir, gameId, playerId, memory) {
36
+ const path = siegePath(dir, gameId, playerId);
37
+ return withPathLock(path, async () => {
38
+ try {
39
+ await mkdir(siegeDir(dir), { recursive: true, mode: 0o700 });
40
+ const tmp = `${path}.${process.pid}.${randomUUID()}.tmp`;
41
+ await writeFile(tmp, JSON.stringify({ gameId, playerId, target: memory.target, turns: memory.turns }), { mode: 0o600 });
42
+ await rename(tmp, path);
43
+ return true;
44
+ }
45
+ catch {
46
+ return false;
47
+ }
48
+ });
49
+ }