@@ -651,6 +651,9 @@ func (m *Mysis) SendMessageFrom(content string, source store.MemorySource, sende
651651
652652 a.updateActivityFromToolResult(result, execErr)
653653
654+ // Cache snapshot tool results for game state summary
655+ a.cacheSnapshotToolResult(tc.Name, result, execErr)
656+
654657 // Store the tool result
655658 resultContent := a.formatToolResult(tc.ID, tc.Name, result, execErr)
656659 if err := a.store.AddMemory(a.id, store.MemoryRoleTool, store.MemorySourceTool, resultContent, "", ""); err != nil {
@@ -1712,9 +1715,121 @@ func (m *Mysis) buildSystemPrompt() string {
17121715
17131716 prompt = strings.Replace(prompt, "{{ACCOUNT_DETAILS}}", accountDetails, 1)
17141717
1718+ // Replace {{GAME_STATE_SUMMARY}} with cached game state
1719+ gameStateSummary := m.buildGameStateSummary()
1720+ prompt = strings.Replace(prompt, "{{GAME_STATE_SUMMARY}}", gameStateSummary, 1)
1721+
17151722 return prompt
17161723 }
17171724
1725+// buildGameStateSummary creates a compact summary of cached game state.
1726+func (m *Mysis) buildGameStateSummary() string {
1727+ username := m.CurrentAccountUsername()
1728+ if username == "" {
1729+ return constants.GameStateSummaryFallback
1730+ }
1731+
1732+ snapshots, err := m.store.GetAllGameStateSnapshots(username)
1733+ if err != nil || len(snapshots) == 0 {
1734+ return constants.GameStateSummaryFallback
1735+ }
1736+
1737+ // Get current tick for recency calculation
1738+ currentTick := m.lastServerTick
1739+
1740+ var summary strings.Builder
1741+ summary.WriteString("\n## Cached Game State\n")
1742+ summary.WriteString(fmt.Sprintf("Account: %s\n\n", username))
1743+
1744+ // Group snapshots by category
1745+ statusTools := []string{"get_status", "get_player"}
1746+ shipTools := []string{"get_ship", "get_cargo"}
1747+ locationTools := []string{"get_system", "get_location", "get_poi"}
1748+ mapTools := []string{"get_map", "get_galaxy"}
1749+
1750+ writeSnapshotGroup := func(title string, toolNames []string) {
1751+ for _, snapshot := range snapshots {
1752+ for _, toolName := range toolNames {
1753+ if snapshot.ToolName == toolName {
1754+ recency := snapshot.RecencyMessage(currentTick)
1755+ summary.WriteString(fmt.Sprintf("### %s (%s)\n", toolName, recency))
1756+
1757+ // Parse and extract key fields for compact display
1758+ compactInfo := extractCompactInfo(snapshot.Content, toolName)
1759+ if compactInfo != "" {
1760+ summary.WriteString(compactInfo)
1761+ summary.WriteString("\n")
1762+ }
1763+ summary.WriteString("\n")
1764+ }
1765+ }
1766+ }
1767+ }
1768+
1769+ writeSnapshotGroup("Status", statusTools)
1770+ writeSnapshotGroup("Ship", shipTools)
1771+ writeSnapshotGroup("Location", locationTools)
1772+ writeSnapshotGroup("Map", mapTools)
1773+
1774+ // Add note about refreshing stale data
1775+ summary.WriteString("Note: Call get_notifications to see events since last update.\n")
1776+ summary.WriteString("Refresh any stale snapshots (>50 ticks old) before making decisions.\n")
1777+
1778+ return summary.String()
1779+}
1780+
1781+// extractCompactInfo extracts key information from a snapshot for compact display.
1782+func extractCompactInfo(content, toolName string) string {
1783+ // Parse JSON content
1784+ var data interface{}
1785+ decoder := json.NewDecoder(strings.NewReader(content))
1786+ decoder.UseNumber()
1787+ if err := decoder.Decode(&data); err != nil {
1788+ return ""
1789+ }
1790+
1791+ var info strings.Builder
1792+
1793+ switch toolName {
1794+ case "get_status", "get_player":
1795+ if credits, ok := findIntField(data, "credits", "balance"); ok {
1796+ info.WriteString(fmt.Sprintf("- Credits: %d\n", credits))
1797+ }
1798+ if location, ok := findStringField(data, "location", "current_location"); ok {
1799+ info.WriteString(fmt.Sprintf("- Location: %s\n", location))
1800+ }
1801+ if ship, ok := findStringField(data, "ship", "ship_name"); ok {
1802+ info.WriteString(fmt.Sprintf("- Ship: %s\n", ship))
1803+ }
1804+
1805+ case "get_ship":
1806+ if hull, ok := findIntField(data, "hull", "hull_health"); ok {
1807+ info.WriteString(fmt.Sprintf("- Hull: %d\n", hull))
1808+ }
1809+ if fuel, ok := findIntField(data, "fuel", "fuel_level"); ok {
1810+ info.WriteString(fmt.Sprintf("- Fuel: %d\n", fuel))
1811+ }
1812+ if cargo, ok := findIntField(data, "cargo_used", "cargo"); ok {
1813+ info.WriteString(fmt.Sprintf("- Cargo: %d\n", cargo))
1814+ }
1815+
1816+ case "get_system", "get_location":
1817+ if system, ok := findStringField(data, "system", "system_name"); ok {
1818+ info.WriteString(fmt.Sprintf("- System: %s\n", system))
1819+ }
1820+ if faction, ok := findStringField(data, "faction", "controlling_faction"); ok {
1821+ info.WriteString(fmt.Sprintf("- Faction: %s\n", faction))
1822+ }
1823+
1824+ case "get_map", "get_galaxy":
1825+ if systemCount, ok := findIntField(data, "system_count", "systems"); ok {
1826+ info.WriteString(fmt.Sprintf("- Systems: %d\n", systemCount))
1827+ }
1828+ }
1829+
1830+ return info.String()
1831+}
1832+
17181833 // rebuildSystemMemory rebuilds the system memory with current state
17191834 func (m *Mysis) rebuildSystemMemory() error {
17201835 // Build new system prompt with current credentials and broadcasts
@@ -1780,6 +1895,48 @@ func (m *Mysis) updateActivityFromToolResult(result *mcp.ToolResult, err error)
17801895
17811896 }
17821897
1898+// cacheSnapshotToolResult stores successful snapshot tool results in the game state cache.
1899+func (m *Mysis) cacheSnapshotToolResult(toolName string, result *mcp.ToolResult, err error) {
1900+ // Only cache successful snapshot tools
1901+ if err != nil || result == nil || result.IsError {
1902+ return
1903+ }
1904+
1905+ // Only cache get_* tools
1906+ if !strings.HasPrefix(toolName, "get_") {
1907+ return
1908+ }
1909+
1910+ // Need active account to cache
1911+ username := m.CurrentAccountUsername()
1912+ if username == "" {
1913+ return
1914+ }
1915+
1916+ // Extract game tick from result
1917+ payload, ok := parseToolResultPayload(result)
1918+ if !ok {
1919+ return
1920+ }
1921+
1922+ gameTick, _ := findCurrentTick(payload)
1923+
1924+ // Get full result content
1925+ var content strings.Builder
1926+ for _, block := range result.Content {
1927+ if block.Type == "text" {
1928+ content.WriteString(block.Text)
1929+ }
1930+ }
1931+
1932+ if content.Len() == 0 {
1933+ return
1934+ }
1935+
1936+ // Store in cache (ignore errors - caching is best-effort)
1937+ _ = m.store.StoreGameStateSnapshot(username, toolName, content.String(), gameTick)
1938+}
1939+
17831940 func (m *Mysis) setActivity(state ActivityState, until time.Time) {
17841941 a := m
17851942 a.mu.Lock()
@@ -1942,6 +2099,39 @@ func findFloatField(payload interface{}, keys ...string) (float64, bool) {
19422099 return 0, false
19432100 }
19442101
2102+func findStringField(payload interface{}, keys ...string) (string, bool) {
2103+ queue := []interface{}{payload}
2104+
2105+ for len(queue) > 0 {
2106+ current := queue[0]
2107+ queue = queue[1:]
2108+
2109+ switch value := current.(type) {
2110+ case map[string]interface{}:
2111+ for _, key := range keys {
2112+ if raw, ok := value[key]; ok {
2113+ if str, ok := raw.(string); ok {
2114+ return str, true
2115+ }
2116+ }
2117+ }
2118+
2119+ childKeys := make([]string, 0, len(value))
2120+ for key := range value {
2121+ childKeys = append(childKeys, key)
2122+ }
2123+ sort.Strings(childKeys)
2124+ for _, key := range childKeys {
2125+ queue = append(queue, value[key])
2126+ }
2127+ case []interface{}:
2128+ queue = append(queue, value...)
2129+ }
2130+ }
2131+
2132+ return "", false
2133+}
2134+
19452135 func findCurrentTick(payload interface{}) (int64, bool) {
19462136 if number, ok := findIntFieldAtKeys(payload, "current_tick"); ok {
19472137 return number, true