spora 0.7.7 → 0.7.8

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 (46) hide show
  1. package/dist/{autonomy-NNFTM5NW.js → autonomy-ZMFZRXDZ.js} +7 -7
  2. package/dist/{chunk-BBXHECZ5.js → chunk-6WBIVXOY.js} +1 -1
  3. package/dist/chunk-6WBIVXOY.js.map +1 -0
  4. package/dist/{chunk-JIMONWKO.js → chunk-73CWOI44.js} +4 -4
  5. package/dist/chunk-73CWOI44.js.map +1 -0
  6. package/dist/{chunk-ZLSDFYBR.js → chunk-AIGSCHZK.js} +2 -2
  7. package/dist/{chunk-CP6JWCLY.js → chunk-ER6TILYZ.js} +1 -25
  8. package/dist/{chunk-CP6JWCLY.js.map → chunk-ER6TILYZ.js.map} +1 -1
  9. package/dist/chunk-OLYPPXKP.js +69 -0
  10. package/dist/chunk-OLYPPXKP.js.map +1 -0
  11. package/dist/{chunk-5R4AJZHN.js → chunk-TKGB5LIN.js} +2 -2
  12. package/dist/chunk-TTDQZI5W.js +1699 -0
  13. package/dist/chunk-TTDQZI5W.js.map +1 -0
  14. package/dist/cli.js +28 -28
  15. package/dist/{client-AR5ZD6S4.js → client-3APKWQ6O.js} +3 -3
  16. package/dist/{colony-UGVYALOS.js → colony-7GZ2ODF2.js} +2 -2
  17. package/dist/{heartbeat-WJJSGUAQ.js → heartbeat-CUM7FIHS.js} +23 -7
  18. package/dist/heartbeat-CUM7FIHS.js.map +1 -0
  19. package/dist/heartbeat-narrative-B3RD3OPJ.js +11 -0
  20. package/dist/{init-6HY4ZPFJ.js → init-KL6XSI7E.js} +3 -3
  21. package/dist/mcp-server.js +20 -20
  22. package/dist/{memory-DTSLVSQG.js → memory-G4DNIGLT.js} +2 -2
  23. package/dist/{prompt-builder-ZFUZNQY2.js → prompt-builder-S6PJVEC5.js} +4 -4
  24. package/dist/{queue-QCGNDHH2.js → queue-YPBUUP22.js} +2 -2
  25. package/dist/web-chat/chat.html +15 -5
  26. package/dist/{web-chat-AKUEBSWS.js → web-chat-XNTIDKAS.js} +37 -15
  27. package/dist/web-chat-XNTIDKAS.js.map +1 -0
  28. package/dist/{x-client-S2LUVEKV.js → x-client-2HFEHHVE.js} +2 -2
  29. package/dist/x-client-2HFEHHVE.js.map +1 -0
  30. package/package.json +1 -1
  31. package/dist/chunk-BBXHECZ5.js.map +0 -1
  32. package/dist/chunk-JIMONWKO.js.map +0 -1
  33. package/dist/chunk-TTM54LQR.js +0 -2769
  34. package/dist/chunk-TTM54LQR.js.map +0 -1
  35. package/dist/heartbeat-WJJSGUAQ.js.map +0 -1
  36. package/dist/web-chat-AKUEBSWS.js.map +0 -1
  37. /package/dist/{autonomy-NNFTM5NW.js.map → autonomy-ZMFZRXDZ.js.map} +0 -0
  38. /package/dist/{chunk-ZLSDFYBR.js.map → chunk-AIGSCHZK.js.map} +0 -0
  39. /package/dist/{chunk-5R4AJZHN.js.map → chunk-TKGB5LIN.js.map} +0 -0
  40. /package/dist/{client-AR5ZD6S4.js.map → client-3APKWQ6O.js.map} +0 -0
  41. /package/dist/{colony-UGVYALOS.js.map → colony-7GZ2ODF2.js.map} +0 -0
  42. /package/dist/{memory-DTSLVSQG.js.map → heartbeat-narrative-B3RD3OPJ.js.map} +0 -0
  43. /package/dist/{init-6HY4ZPFJ.js.map → init-KL6XSI7E.js.map} +0 -0
  44. /package/dist/{prompt-builder-ZFUZNQY2.js.map → memory-G4DNIGLT.js.map} +0 -0
  45. /package/dist/{queue-QCGNDHH2.js.map → prompt-builder-S6PJVEC5.js.map} +0 -0
  46. /package/dist/{x-client-S2LUVEKV.js.map → queue-YPBUUP22.js.map} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/runtime/decision-engine.ts","../src/runtime/policy.ts","../src/runtime/bandit.ts","../src/runtime/autonomy.ts"],"sourcesContent":["import { logger } from \"../utils/logger.js\";\nimport { getXClient } from \"../x-client/index.js\";\nimport { addLearning, logInteraction } from \"../memory/index.js\";\nimport { loadIdentity, saveIdentity } from \"../identity/index.js\";\nimport { addToQueue } from \"../scheduler/queue.js\";\n\nexport interface AgentAction {\n action: string;\n content?: string;\n tweetId?: string;\n handle?: string;\n targetHandle?: string;\n source?: string;\n banditArm?: string;\n opportunityId?: string;\n intentIds?: string[];\n tags?: string[];\n reason?: string;\n reasoning?: string;\n}\n\nexport interface ActionResult {\n action: string;\n success: boolean;\n detail?: string;\n error?: string;\n}\n\nexport function parseActions(llmResponse: string): AgentAction[] {\n // Strategy: try multiple extraction approaches, most specific first\n\n // 1. Try markdown code block first (```json ... ``` or ``` ... ```)\n const codeBlockMatch = llmResponse.match(/```(?:json)?\\s*\\n?([\\s\\S]*?)```/);\n if (codeBlockMatch) {\n const inside = codeBlockMatch[1].trim();\n try {\n const parsed = JSON.parse(inside);\n return Array.isArray(parsed) ? parsed : [parsed];\n } catch {\n // Code block content wasn't valid JSON, continue to other strategies\n }\n }\n\n // 2. Find the last JSON array in the response (LLMs often put reasoning before the array)\n // Use a greedy approach to find the outermost array brackets\n let lastArrayStart = -1;\n let lastArrayEnd = -1;\n let depth = 0;\n for (let i = 0; i < llmResponse.length; i++) {\n if (llmResponse[i] === \"[\") {\n if (depth === 0) lastArrayStart = i;\n depth++;\n } else if (llmResponse[i] === \"]\") {\n depth--;\n if (depth === 0) lastArrayEnd = i;\n }\n }\n\n if (lastArrayStart >= 0 && lastArrayEnd > lastArrayStart) {\n const arrayStr = llmResponse.slice(lastArrayStart, lastArrayEnd + 1);\n try {\n const parsed = JSON.parse(arrayStr);\n if (Array.isArray(parsed) && parsed.length > 0 && parsed[0].action) {\n return parsed;\n }\n } catch {\n // Not valid JSON array, try next strategy\n }\n }\n\n // 3. Try to find a single action object (last one in the response)\n const objMatches = [...llmResponse.matchAll(/\\{[^{}]*\"action\"\\s*:\\s*\"[^\"]+\"/g)];\n if (objMatches.length > 0) {\n // Find the full object starting from this match\n for (let i = objMatches.length - 1; i >= 0; i--) {\n const start = objMatches[i].index!;\n let braceDepth = 0;\n let end = -1;\n for (let j = start; j < llmResponse.length; j++) {\n if (llmResponse[j] === \"{\") braceDepth++;\n else if (llmResponse[j] === \"}\") {\n braceDepth--;\n if (braceDepth === 0) { end = j; break; }\n }\n }\n if (end > start) {\n try {\n const obj = JSON.parse(llmResponse.slice(start, end + 1));\n if (obj.action) return [obj as AgentAction];\n } catch {\n continue;\n }\n }\n }\n }\n\n // 4. Last resort: try the whole response as JSON\n try {\n const parsed = JSON.parse(llmResponse.trim());\n if (Array.isArray(parsed)) return parsed;\n if (parsed.action) return [parsed as AgentAction];\n } catch {\n // Not JSON at all\n }\n\n logger.warn(\"Failed to parse actions from LLM response\");\n if (llmResponse.length < 500) {\n logger.warn(`Response was: ${llmResponse}`);\n } else {\n logger.warn(`Response starts with: ${llmResponse.slice(0, 200)}...`);\n }\n return [];\n}\n\nexport async function executeAction(action: AgentAction): Promise<ActionResult> {\n const { action: type } = action;\n\n try {\n switch (type) {\n case \"post\": {\n if (!action.content) return { action: type, success: false, error: \"No content provided\" };\n if (action.content.length > 280) {\n return { action: type, success: false, error: `Tweet too long: ${action.content.length} chars (max 280)` };\n }\n\n const client = await getXClient();\n const result = await client.postTweet(action.content);\n if (result.success) logger.info(`Posted: \"${action.content.slice(0, 50)}...\"`);\n return { action: type, success: result.success, detail: result.tweetId, error: result.error };\n }\n\n case \"reply\": {\n if (!action.tweetId || !action.content) {\n return { action: type, success: false, error: \"Missing tweetId or content\" };\n }\n const client = await getXClient();\n const result = await client.replyToTweet(action.tweetId, action.content);\n if (result.success) logger.info(`Replied to ${action.tweetId}: \"${action.content.slice(0, 50)}...\"`);\n return { action: type, success: result.success, detail: result.tweetId, error: result.error };\n }\n\n case \"like\": {\n if (!action.tweetId) return { action: type, success: false, error: \"Missing tweetId\" };\n const client = await getXClient();\n const result = await client.likeTweet(action.tweetId);\n return { action: type, success: result.success, error: result.error };\n }\n\n case \"retweet\": {\n if (!action.tweetId) return { action: type, success: false, error: \"Missing tweetId\" };\n const client = await getXClient();\n const result = await client.retweet(action.tweetId);\n return { action: type, success: result.success, error: result.error };\n }\n\n case \"follow\": {\n if (!action.handle) return { action: type, success: false, error: \"Missing handle\" };\n const client = await getXClient();\n const result = await client.followUser(action.handle);\n if (!result.success) {\n logInteraction({\n id: `int-${Date.now()}`,\n timestamp: new Date().toISOString(),\n type: \"follow\",\n targetHandle: action.handle.replace(/^@/, \"\"),\n creditsUsed: 0,\n success: false,\n error: result.error,\n });\n }\n return { action: type, success: result.success, error: result.error };\n }\n\n case \"schedule\": {\n if (!action.content) return { action: type, success: false, error: \"No content\" };\n const entry = addToQueue(action.content);\n logger.info(`Scheduled: \"${action.content.slice(0, 50)}...\" for ${entry.scheduledFor}`);\n return { action: type, success: true, detail: `Scheduled for ${entry.scheduledFor}` };\n }\n\n case \"learn\": {\n if (!action.content) return { action: type, success: false, error: \"No content\" };\n addLearning(action.content, \"agent\", action.tags ?? [\"heartbeat\"]);\n logger.info(`Learned: \"${action.content.slice(0, 50)}...\"`);\n return { action: type, success: true };\n }\n\n case \"reflect\": {\n if (!action.content) return { action: type, success: false, error: \"No content\" };\n const identity = loadIdentity();\n identity.evolutionJournal.push({\n date: new Date().toISOString(),\n reflection: action.content,\n });\n saveIdentity(identity);\n logger.info(`Reflected: \"${action.content.slice(0, 50)}...\"`);\n return { action: type, success: true };\n }\n\n case \"skip\": {\n logger.info(`Skipping: ${action.reason ?? action.reasoning ?? \"no reason given\"}`);\n return { action: type, success: true, detail: action.reason ?? action.reasoning };\n }\n\n default:\n logger.warn(`Unknown action: ${type}`);\n return { action: type, success: false, error: `Unknown action: ${type}` };\n }\n } catch (error) {\n const msg = (error as Error).message;\n logger.error(`Action ${type} failed: ${msg}`);\n return { action: type, success: false, error: msg };\n }\n}\n\nexport async function executeActions(actions: AgentAction[]): Promise<ActionResult[]> {\n const results: ActionResult[] = [];\n for (const action of actions) {\n const result = await executeAction(action);\n results.push(result);\n // Small delay between actions to be human-like\n await new Promise((r) => setTimeout(r, 2000 + Math.random() * 3000));\n }\n return results;\n}\n","import { getRecentInteractions, type InteractionEntry } from \"../memory/index.js\";\nimport { loadIdentity } from \"../identity/index.js\";\nimport { getPersonaConstraints, personaConstraintHandles } from \"./persona-constraints.js\";\nimport type { Tweet } from \"../x-client/types.js\";\nimport type { AgentAction } from \"./decision-engine.js\";\n\nexport interface PolicyContext {\n action: AgentAction;\n step: number;\n timeline: Tweet[];\n mentions: Tweet[];\n executedActions: AgentAction[];\n observedTweetIds?: string[];\n observedTweets?: Tweet[];\n selfHandle?: string;\n selfUserId?: string | null;\n}\n\nexport interface PolicyDecision {\n allowed: boolean;\n reason?: string;\n}\n\nfunction normalize(text: string): string {\n return text\n .toLowerCase()\n .replace(/https?:\\/\\/\\S+/g, \"\")\n .replace(/[@#]\\w+/g, \"\")\n .replace(/[^a-z0-9\\s]/g, \" \")\n .replace(/\\s+/g, \" \")\n .trim();\n}\n\nfunction tokenSet(text: string): Set<string> {\n const tokens = normalize(text).split(\" \").filter(Boolean);\n return new Set(tokens);\n}\n\nfunction jaccardSimilarity(a: string, b: string): number {\n const aSet = tokenSet(a);\n const bSet = tokenSet(b);\n if (aSet.size === 0 || bSet.size === 0) return 0;\n\n let overlap = 0;\n for (const token of aSet) {\n if (bSet.has(token)) overlap += 1;\n }\n\n const union = aSet.size + bSet.size - overlap;\n return union === 0 ? 0 : overlap / union;\n}\n\nfunction firstWords(text: string, n: number): string {\n return normalize(text).split(\" \").filter(Boolean).slice(0, n).join(\" \");\n}\n\nfunction hasAllCapsEnding(text: string): boolean {\n const ending = text.split(/[.!?]/).map((s) => s.trim()).filter(Boolean).slice(-1)[0] ?? \"\";\n const words = ending.split(/\\s+/).filter(Boolean);\n if (words.length < 3) return false;\n return words.every((word) => /^[A-Z0-9]+$/.test(word));\n}\n\nfunction wordCount(text: string): number {\n return normalize(text).split(\" \").filter(Boolean).length;\n}\n\nfunction sentenceCount(text: string): number {\n return text\n .split(/[.!?]/)\n .map((part) => part.trim())\n .filter(Boolean).length;\n}\n\nfunction explanatoryMarkerHits(text: string): number {\n const lower = text.toLowerCase();\n const markers = [\n \"the real issue\",\n \"the core issue\",\n \"in other words\",\n \"the point is\",\n \"what this means\",\n \"the lesson\",\n \"therefore\",\n \"ultimately\",\n \"meanwhile\",\n \"this reveals\",\n \"this proves\",\n ];\n return markers.reduce((hits, marker) => hits + (lower.includes(marker) ? 1 : 0), 0);\n}\n\nfunction soundsLikeLecture(content: string): boolean {\n const words = wordCount(content);\n const commas = (content.match(/,/g) ?? []).length;\n const semicolons = (content.match(/;/g) ?? []).length;\n const hasQuestion = content.includes(\"?\");\n const markerHits = explanatoryMarkerHits(content);\n\n if (words > 58) return true;\n if (words > 42 && commas + semicolons >= 3 && !hasQuestion) return true;\n if (markerHits >= 2) return true;\n return false;\n}\n\nfunction soundsTooHedgedForPersona(content: string): boolean {\n try {\n const identity = loadIdentity();\n const assertive =\n identity.traits.confidence >= 0.72 ||\n identity.traits.aggression >= 0.62 ||\n identity.conflictStyle === \"clap-back\" ||\n identity.conflictStyle === \"debate\";\n if (!assertive) return false;\n\n const lower = content.toLowerCase();\n const hedges = [\n \"you're both right\",\n \"both can be true\",\n \"on one hand\",\n \"on the other hand\",\n \"it depends\",\n \"it's complicated\",\n \"nuance\",\n \"fair point from both\",\n ];\n return hedges.some((phrase) => lower.includes(phrase));\n } catch {\n return false;\n }\n}\n\nfunction allowsPhilosopherCadence(): boolean {\n try {\n const identity = loadIdentity();\n if (identity.framework === \"philosopher\") return true;\n // Truthseeker can be analytical, but still should avoid abstract manifesto loops.\n if (identity.framework === \"truthseeker\") return false;\n return false;\n } catch {\n return false;\n }\n}\n\nfunction philosophicalPatternHits(content: string): number {\n const lower = content.toLowerCase();\n const markers = [\n \"the real question\",\n \"the deeper question\",\n \"what becomes possible\",\n \"is just the start\",\n \"the future isn't about\",\n \"the future is not about\",\n \"when intelligence becomes\",\n \"protocols are infrastructure\",\n \"the core future\",\n \"the true question\",\n \"value might emerge\",\n \"isn't about\",\n \"is not about\",\n \"it's about\",\n \"just the start\",\n ];\n return markers.reduce((hits, marker) => hits + (lower.includes(marker) ? 1 : 0), 0);\n}\n\nfunction abstractWordCount(content: string): number {\n const words = normalize(content).split(\" \").filter(Boolean);\n const abstractWords = new Set([\n \"infrastructure\",\n \"protocol\",\n \"protocols\",\n \"architecture\",\n \"architectures\",\n \"consciousness\",\n \"economy\",\n \"economics\",\n \"currency\",\n \"systems\",\n \"system\",\n \"future\",\n \"intelligence\",\n \"optimization\",\n \"abstraction\",\n \"paradigm\",\n \"emerge\",\n \"emergence\",\n \"governs\",\n \"governance\",\n ]);\n return words.filter((word) => abstractWords.has(word)).length;\n}\n\nfunction usesManifestoPunctuation(content: string): boolean {\n // Colon/semicolon/em-dash patterns correlate strongly with the explanatory style we want to avoid.\n return content.includes(\":\") || content.includes(\";\") || content.includes(\"—\");\n}\n\nconst OVERLAP_STOPWORDS = new Set([\n \"this\", \"that\", \"with\", \"from\", \"have\", \"what\", \"when\", \"where\", \"which\", \"your\", \"youre\",\n \"they\", \"them\", \"about\", \"into\", \"over\", \"under\", \"while\", \"just\", \"like\", \"really\",\n \"there\", \"their\", \"then\", \"than\", \"because\", \"would\", \"could\", \"should\", \"being\", \"been\",\n \"it's\", \"its\", \"the\", \"and\", \"for\", \"are\", \"was\", \"were\", \"will\", \"not\", \"but\", \"you\",\n \"our\", \"out\", \"all\", \"too\", \"can\", \"get\", \"got\", \"any\",\n]);\n\nfunction meaningfulTokenSet(text: string): Set<string> {\n const tokens = normalize(text)\n .split(\" \")\n .filter((token) => token.length >= 4 && !OVERLAP_STOPWORDS.has(token));\n return new Set(tokens);\n}\n\nfunction hasMeaningfulContextOverlap(content: string, targetText: string): boolean {\n const a = meaningfulTokenSet(content);\n const b = meaningfulTokenSet(targetText);\n if (a.size === 0 || b.size === 0) return false;\n for (const token of a) {\n if (b.has(token)) return true;\n }\n return false;\n}\n\nfunction recentWrittenContent(): string[] {\n const recent = getRecentInteractions(40);\n return recent\n .filter((i) => i.type === \"post\" || i.type === \"reply\")\n .map((i) => i.content ?? \"\")\n .filter((content) => content.length > 0);\n}\n\nconst REPETITION_STOPWORDS = new Set([\n \"about\", \"after\", \"again\", \"against\", \"almost\", \"always\", \"among\", \"because\", \"being\", \"between\",\n \"could\", \"every\", \"first\", \"found\", \"from\", \"going\", \"great\", \"have\", \"having\", \"here\", \"into\",\n \"just\", \"like\", \"make\", \"more\", \"most\", \"much\", \"only\", \"other\", \"over\", \"really\", \"same\", \"some\",\n \"such\", \"than\", \"that\", \"their\", \"there\", \"these\", \"they\", \"this\", \"those\", \"through\", \"time\", \"very\",\n \"what\", \"when\", \"where\", \"which\", \"while\", \"with\", \"would\", \"your\", \"youre\", \"dont\", \"cant\", \"will\",\n \"shall\", \"should\", \"also\", \"tweet\", \"tweets\", \"thread\", \"threads\", \"future\", \"human\", \"humans\",\n \"technology\", \"tech\", \"build\", \"building\", \"system\", \"systems\", \"agent\", \"agents\",\n]);\n\nfunction informativeTokens(text: string): string[] {\n return normalize(text)\n .split(\" \")\n .filter((token) => token.length >= 5 && !REPETITION_STOPWORDS.has(token));\n}\n\nfunction findOverusedAnchor(content: string, recent: string[]): string | null {\n const recentWindow = recent.slice(0, 12);\n if (recentWindow.length < 5) return null;\n\n const tokenCounts = new Map<string, number>();\n const bigramCounts = new Map<string, number>();\n\n for (const sample of recentWindow) {\n const tokens = informativeTokens(sample);\n const uniqueTokens = new Set(tokens);\n for (const token of uniqueTokens) {\n tokenCounts.set(token, (tokenCounts.get(token) ?? 0) + 1);\n }\n for (let i = 0; i < tokens.length - 1; i += 1) {\n const bigram = `${tokens[i]} ${tokens[i + 1]}`;\n bigramCounts.set(bigram, (bigramCounts.get(bigram) ?? 0) + 1);\n }\n }\n\n const contentTokens = informativeTokens(content);\n const contentTokenSet = new Set(contentTokens);\n for (const token of contentTokenSet) {\n const count = tokenCounts.get(token) ?? 0;\n if (count >= 4) return token;\n }\n\n for (let i = 0; i < contentTokens.length - 1; i += 1) {\n const bigram = `${contentTokens[i]} ${contentTokens[i + 1]}`;\n const count = bigramCounts.get(bigram) ?? 0;\n if (count >= 3) return bigram;\n }\n\n return null;\n}\n\nfunction openingFragment(text: string, words: number = 4): string {\n return normalize(text).split(\" \").filter(Boolean).slice(0, words).join(\" \");\n}\n\nfunction findOverusedOpening(content: string, recent: string[]): string | null {\n const opening = openingFragment(content, 4);\n if (!opening || opening.split(\" \").length < 3) return null;\n\n let matches = 0;\n for (const sample of recent.slice(0, 12)) {\n if (openingFragment(sample, 4) === opening) {\n matches += 1;\n }\n }\n\n return matches >= 2 ? opening : null;\n}\n\nfunction vocabularyNoveltyRatio(content: string, recent: string[]): number {\n const current = new Set(informativeTokens(content));\n if (current.size === 0) return 1;\n\n const seen = new Set<string>();\n for (const sample of recent.slice(0, 14)) {\n for (const token of informativeTokens(sample)) {\n seen.add(token);\n }\n }\n\n let fresh = 0;\n for (const token of current) {\n if (!seen.has(token)) fresh += 1;\n }\n return fresh / current.size;\n}\n\nfunction looksOverAbstractWithoutAnchor(content: string): boolean {\n const abstractCount = abstractWordCount(content);\n if (abstractCount < 4) return false;\n\n const words = wordCount(content);\n const density = abstractCount / Math.max(1, words);\n if (density < 0.2) return false;\n\n const lower = content.toLowerCase();\n const hasHandle = /@\\w+/.test(content);\n const hasNumber = /\\b\\d+(\\.\\d+)?\\b/.test(content);\n const hasQuote = /[\"“”']/.test(content);\n const hasSpecificFigure = /\\b(musk|elon|zuck|altman|openai|tesla|spacex|meta|google|apple)\\b/.test(lower);\n const hasQuestion = content.includes(\"?\");\n\n return !(hasHandle || hasNumber || hasQuote || hasSpecificFigure || hasQuestion);\n}\n\nfunction hasStrongConversationOpportunity(timeline: Tweet[], mentions: Tweet[]): boolean {\n if (mentions.length > 0) return true;\n return timeline.some((tweet) => (tweet.replyCount ?? 0) > 0 || tweet.text.includes(\"?\"));\n}\n\nfunction wasInteractionAction(action: AgentAction): boolean {\n return [\"reply\", \"like\", \"retweet\", \"follow\"].includes(action.action);\n}\n\nfunction isWritingAction(action: AgentAction): boolean {\n return [\"post\", \"reply\", \"schedule\"].includes(action.action);\n}\n\nfunction normalizeHandle(handle: string | undefined): string {\n return (handle ?? \"\").replace(/^@/, \"\").trim().toLowerCase();\n}\n\nfunction resolveActionTargetHandle(action: AgentAction, tweetById: Map<string, Tweet>): string {\n if (action.handle) return normalizeHandle(action.handle);\n if (action.targetHandle) return normalizeHandle(action.targetHandle);\n if (action.tweetId) return normalizeHandle(tweetById.get(action.tweetId)?.authorHandle);\n return \"\";\n}\n\nfunction executedWrittenContent(executedActions: AgentAction[]): string[] {\n return executedActions\n .filter((a) => isWritingAction(a) && typeof a.content === \"string\")\n .map((a) => a.content?.trim() ?? \"\")\n .filter((content) => content.length > 0);\n}\n\nfunction nearExactDuplicate(content: string, recent: string[]): boolean {\n const normalized = normalize(content);\n if (!normalized) return false;\n\n return recent.some((r) => {\n const candidate = normalize(r);\n if (!candidate) return false;\n if (candidate === normalized) return true;\n return jaccardSimilarity(content, r) >= 0.88;\n });\n}\n\nfunction isDuplicateTarget(action: AgentAction, executedActions: AgentAction[]): boolean {\n if (!action.tweetId) return false;\n return executedActions.some((a) => a.tweetId === action.tweetId && a.action === action.action);\n}\n\nfunction repeatedTemplate(content: string, recent: string[]): boolean {\n const prefix = firstWords(content, 7);\n if (!prefix) return false;\n\n return recent.some((r) => {\n const sameStart = firstWords(r, 7) === prefix;\n const similar = jaccardSimilarity(content, r) >= 0.62;\n return sameStart || similar;\n });\n}\n\nfunction overusingAllCapsCadence(content: string, recentEntries: InteractionEntry[]): boolean {\n if (!hasAllCapsEnding(content)) return false;\n const recentCaps = recentEntries\n .filter((i) => i.type === \"post\" && i.content)\n .slice(0, 8)\n .filter((i) => hasAllCapsEnding(i.content ?? \"\"));\n\n return recentCaps.length >= 2;\n}\n\nexport function evaluateActionPolicy(context: PolicyContext): PolicyDecision {\n const {\n action,\n step,\n timeline,\n mentions,\n executedActions,\n observedTweetIds,\n observedTweets,\n selfHandle,\n selfUserId,\n } = context;\n const self = normalizeHandle(selfHandle);\n const selfId = (selfUserId ?? \"\").trim();\n const knownTweets = observedTweets ?? [...timeline, ...mentions];\n const tweetById = new Map(knownTweets.map((tweet) => [tweet.id, tweet]));\n const constraints = getPersonaConstraints();\n const strictReplyHandles = new Set(constraints.onlyReplyToHandles.map((handle) => normalizeHandle(handle)));\n const strictInteractHandles = new Set(personaConstraintHandles(constraints).map((handle) => normalizeHandle(handle)));\n const targetHandle = resolveActionTargetHandle(action, tweetById);\n\n // Persona hard constraints: these are operational, not advisory.\n if (constraints.replyOnlyMode && ![\"reply\", \"skip\", \"learn\", \"reflect\"].includes(action.action)) {\n return {\n allowed: false,\n reason: \"Persona is in reply-only mode. Use reply actions only.\",\n };\n }\n\n if (constraints.noOriginalPosts && (action.action === \"post\" || action.action === \"schedule\")) {\n return {\n allowed: false,\n reason: \"Persona forbids standalone original posts.\",\n };\n }\n\n if (strictReplyHandles.size > 0) {\n if (action.action !== \"reply\") {\n return {\n allowed: false,\n reason: `Persona requires replying only to specific target(s): @${[...strictReplyHandles].join(\", @\")}.`,\n };\n }\n if (!targetHandle || !strictReplyHandles.has(targetHandle)) {\n return {\n allowed: false,\n reason: `Reply target must be one of @${[...strictReplyHandles].join(\", @\")}.`,\n };\n }\n }\n\n if (\n strictInteractHandles.size > 0 &&\n [\"reply\", \"like\", \"retweet\", \"follow\"].includes(action.action)\n ) {\n if (!targetHandle || !strictInteractHandles.has(targetHandle)) {\n return {\n allowed: false,\n reason: `Interaction target must be one of @${[...strictInteractHandles].join(\", @\")}.`,\n };\n }\n }\n\n if (isDuplicateTarget(action, executedActions)) {\n return { allowed: false, reason: `Action ${action.action} already executed for tweet ${action.tweetId} this heartbeat.` };\n }\n\n if (action.content && isWritingAction(action)) {\n if (action.content.length > 280) {\n return {\n allowed: false,\n reason: `Content is too long (${action.content.length}/280). Keep it tighter and more conversational.`,\n };\n }\n\n if (action.action === \"reply\" && wordCount(action.content) > 45 && !action.content.includes(\"?\")) {\n return {\n allowed: false,\n reason: \"Reply is too monologue-like. Keep it shorter or ask a direct question.\",\n };\n }\n\n if (soundsLikeLecture(action.content)) {\n return {\n allowed: false,\n reason: \"Rejected lecture-like writing style. Use a more human, reactive, conversational tone.\",\n };\n }\n\n if (soundsTooHedgedForPersona(action.content)) {\n return {\n allowed: false,\n reason: \"Rejected hedged consensus phrasing. Your persona should be more decisive and opinionated.\",\n };\n }\n\n if (!allowsPhilosopherCadence()) {\n const phiHits = philosophicalPatternHits(action.content);\n const abstractCount = abstractWordCount(action.content);\n const hasQuestion = action.content.includes(\"?\");\n if (phiHits >= 1 || (abstractCount >= 5 && hasQuestion)) {\n return {\n allowed: false,\n reason: \"Rejected abstract philosopher cadence. Use concrete, contextual language tied to the actual tweet.\",\n };\n }\n\n const words = wordCount(action.content);\n const sentences = sentenceCount(action.content);\n if (action.action === \"reply\" && (words > 34 || sentences > 2)) {\n return {\n allowed: false,\n reason: \"Reply is too long/explanatory for this persona. Keep it short and direct.\",\n };\n }\n if (action.action === \"post\" && (words > 42 || sentences > 3)) {\n return {\n allowed: false,\n reason: \"Post is drifting into manifesto style. Keep it more concrete and human.\",\n };\n }\n if ((action.action === \"reply\" || action.action === \"post\") && usesManifestoPunctuation(action.content) && words > 16) {\n return {\n allowed: false,\n reason: \"Rejected manifesto punctuation style. Avoid colon/semicolon/em-dash framing.\",\n };\n }\n }\n\n const existingInHeartbeat = executedWrittenContent(executedActions);\n if (nearExactDuplicate(action.content, existingInHeartbeat)) {\n return {\n allowed: false,\n reason: \"Rejected duplicate wording in this heartbeat. Write a distinct message before posting/replying again.\",\n };\n }\n\n const recent = recentWrittenContent();\n const overusedAnchor = findOverusedAnchor(action.content, recent);\n if (overusedAnchor) {\n return {\n allowed: false,\n reason: `Rejected repetitive anchor phrase/term \"${overusedAnchor}\". Use a different framing and vocabulary.`,\n };\n }\n\n const overusedOpening = findOverusedOpening(action.content, recent);\n if (overusedOpening) {\n return {\n allowed: false,\n reason: `Rejected repetitive opening phrase \"${overusedOpening}\". Start from a fresh angle.`,\n };\n }\n\n if (recent.length >= 6 && wordCount(action.content) >= 10) {\n const novelty = vocabularyNoveltyRatio(action.content, recent);\n if (novelty < 0.34) {\n return {\n allowed: false,\n reason: \"Rejected low-novelty vocabulary loop. Use fresher words, examples, or a different conversational angle.\",\n };\n }\n }\n\n if (!allowsPhilosopherCadence() && looksOverAbstractWithoutAnchor(action.content)) {\n return {\n allowed: false,\n reason: \"Rejected abstract wording without concrete anchor. Reference a specific person, event, quote, or question.\",\n };\n }\n }\n\n const hasConversationOpportunity = hasStrongConversationOpportunity(timeline, mentions);\n const interactedAlready = executedActions.some(wasInteractionAction);\n const observedCount = observedTweetIds && observedTweetIds.length > 0\n ? observedTweetIds.length\n : timeline.length + mentions.length;\n\n if ((action.action === \"reply\" || action.action === \"like\" || action.action === \"retweet\") && observedCount === 0) {\n return {\n allowed: false,\n reason: \"No tweets observed in current context. Avoid blind interactions; gather timeline/search context first.\",\n };\n }\n\n if (\n action.action === \"post\" &&\n hasConversationOpportunity &&\n !interactedAlready &&\n step < 2\n ) {\n return {\n allowed: false,\n reason: \"Engage first: reply/like/follow when mentions or active conversations are available before posting an original tweet.\",\n };\n }\n\n if (isWritingAction(action) && action.content) {\n const recent = recentWrittenContent();\n if (nearExactDuplicate(action.content, recent)) {\n return {\n allowed: false,\n reason: \"Rejected near-duplicate content from recent history. Tailor this message to the specific context.\",\n };\n }\n\n if (repeatedTemplate(action.content, recent)) {\n return {\n allowed: false,\n reason: \"Rejected repetitive content pattern. Use a more novel structure or engage directly with timeline context.\",\n };\n }\n }\n\n if ((action.action === \"post\" || action.action === \"schedule\") && action.content) {\n const recentInteractions = getRecentInteractions(20);\n if (overusingAllCapsCadence(action.content, recentInteractions)) {\n return {\n allowed: false,\n reason: \"Rejected repetitive all-caps slogan cadence. Vary style and reduce monologue formatting.\",\n };\n }\n }\n\n if ((action.action === \"reply\" || action.action === \"like\" || action.action === \"retweet\") && action.tweetId) {\n const known = new Set(observedTweetIds ?? [...timeline, ...mentions].map((tweet) => tweet.id));\n if (!known.has(action.tweetId)) {\n return {\n allowed: false,\n reason: `Tweet ${action.tweetId} is not in current observations. Choose a visible timeline/mention tweet for grounded engagement.`,\n };\n }\n\n const targetTweet = tweetById.get(action.tweetId);\n if (targetTweet && (\n (self && normalizeHandle(targetTweet.authorHandle) === self) ||\n (selfId && targetTweet.authorId === selfId)\n )) {\n return {\n allowed: false,\n reason: \"Rejected self-interaction. Do not reply/like/retweet your own tweets.\",\n };\n }\n }\n\n if (action.action === \"reply\" && action.tweetId) {\n if (action.content) {\n const targetTweet = tweetById.get(action.tweetId);\n if (targetTweet && !hasMeaningfulContextOverlap(action.content, targetTweet.text)) {\n const phiHits = philosophicalPatternHits(action.content);\n if (wordCount(action.content) > 9 || phiHits > 0 || abstractWordCount(action.content) >= 4) {\n return {\n allowed: false,\n reason: \"Reply is not grounded in the target tweet context. Reference concrete details from their actual post.\",\n };\n }\n }\n }\n }\n\n if (action.action === \"follow\" && action.handle) {\n const target = normalizeHandle(action.handle);\n if (!target) {\n return { allowed: false, reason: \"Follow target is empty.\" };\n }\n\n if (self && target === self) {\n return {\n allowed: false,\n reason: \"Rejected self-follow. Do not follow your own account.\",\n };\n }\n\n }\n\n return { allowed: true };\n}\n","import { existsSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { updatePostMetrics } from \"../memory/performance.js\";\nimport { paths } from \"../utils/paths.js\";\nimport { logger } from \"../utils/logger.js\";\nimport type { AgentAction, ActionResult } from \"./decision-engine.js\";\nimport type { ActionOpportunity } from \"./opportunity-engine.js\";\nimport type { XClientInterface } from \"../x-client/types.js\";\n\ninterface BanditArm {\n key: string;\n pulls: number;\n rewardSum: number;\n lastReward: number;\n updatedAt: string;\n}\n\ninterface PendingOutcome {\n id: string;\n armKey: string;\n actionType: string;\n tweetId?: string;\n createdAt: string;\n resolveAt: string;\n resolved: boolean;\n}\n\ninterface BanditState {\n totalPulls: number;\n explorationBudget: number;\n arms: Record<string, BanditArm>;\n pending: PendingOutcome[];\n}\n\ninterface ScoredOpportunity {\n opportunity: ActionOpportunity;\n adjustedScore: number;\n pulls: number;\n}\n\nexport interface OutcomeCollectionResult {\n processed: number;\n resolved: number;\n rewardAdded: number;\n}\n\nconst DEFAULT_EXPLORATION_BUDGET = 0.25;\n\nfunction defaultState(): BanditState {\n return {\n totalPulls: 0,\n explorationBudget: DEFAULT_EXPLORATION_BUDGET,\n arms: {},\n pending: [],\n };\n}\n\nfunction clamp(value: number, min: number, max: number): number {\n return Math.max(min, Math.min(max, value));\n}\n\nfunction loadState(): BanditState {\n if (!existsSync(paths.bandit)) return defaultState();\n try {\n const parsed = JSON.parse(readFileSync(paths.bandit, \"utf-8\")) as Partial<BanditState>;\n const state: BanditState = {\n totalPulls: Math.max(0, parsed.totalPulls ?? 0),\n explorationBudget: clamp(parsed.explorationBudget ?? DEFAULT_EXPLORATION_BUDGET, 0.1, 0.45),\n arms: parsed.arms ?? {},\n pending: Array.isArray(parsed.pending) ? parsed.pending : [],\n };\n return state;\n } catch {\n return defaultState();\n }\n}\n\nfunction saveState(state: BanditState): void {\n state.explorationBudget = clamp(state.explorationBudget, 0.1, 0.45);\n state.pending = state.pending\n .filter((entry) => {\n if (!entry.resolved) return true;\n const resolvedAtMs = Date.parse(entry.resolveAt);\n if (Number.isNaN(resolvedAtMs)) return false;\n return Date.now() - resolvedAtMs < 7 * 24 * 60 * 60 * 1000;\n })\n .slice(-1200);\n writeFileSync(paths.bandit, JSON.stringify(state, null, 2));\n}\n\nfunction ensureArm(state: BanditState, armKey: string): BanditArm {\n const existing = state.arms[armKey];\n if (existing) return existing;\n const arm: BanditArm = {\n key: armKey,\n pulls: 0,\n rewardSum: 0,\n lastReward: 0,\n updatedAt: new Date().toISOString(),\n };\n state.arms[armKey] = arm;\n return arm;\n}\n\nfunction averageReward(arm: BanditArm): number {\n if (arm.pulls <= 0) return 0;\n return arm.rewardSum / arm.pulls;\n}\n\nfunction ucbBonus(totalPulls: number, armPulls: number): number {\n const t = Math.max(1, totalPulls);\n return Math.sqrt((2 * Math.log(t + 1)) / (armPulls + 1));\n}\n\nfunction computeImmediateReward(result: ActionResult): number {\n if (result.success) return 0.12;\n const error = (result.error ?? \"\").toLowerCase();\n if (error.includes(\"duplicate content\")) return -0.95;\n if (error.includes(\"rate limit\")) return -0.5;\n if (error.includes(\"forbidden\") || error.includes(\"403\")) return -0.55;\n return -0.35;\n}\n\nfunction resolveDelayedReward(metrics: { likeCount: number; replyCount: number; retweetCount: number } | null): number {\n if (!metrics) return -0.05;\n const reward =\n Math.min(metrics.likeCount * 0.03, 0.9) +\n Math.min(metrics.replyCount * 0.12, 0.9) +\n Math.min(metrics.retweetCount * 0.08, 0.8);\n if (metrics.likeCount === 0 && metrics.replyCount === 0 && metrics.retweetCount === 0) {\n return -0.15;\n }\n return Math.min(reward, 1.8);\n}\n\nfunction armKeyFromAction(action: AgentAction): string {\n if (action.banditArm && action.banditArm.trim().length > 0) return action.banditArm;\n const source = action.source ?? \"unknown\";\n return `${action.action}:${source}`;\n}\n\nfunction extractTweetId(action: AgentAction, result: ActionResult): string | undefined {\n if (result.detail && /^\\d+$/.test(result.detail)) return result.detail;\n if (action.action !== \"reply\" && action.tweetId) return action.tweetId;\n return undefined;\n}\n\nexport function selectBanditOpportunityPool(\n opportunities: ActionOpportunity[],\n maxPoolSize: number = 24,\n): ActionOpportunity[] {\n if (opportunities.length <= maxPoolSize) return opportunities;\n\n const state = loadState();\n const totalPulls = Math.max(1, state.totalPulls);\n const scored: ScoredOpportunity[] = opportunities.map((opportunity) => {\n const arm = ensureArm(state, opportunity.armKey);\n const mean = averageReward(arm);\n const bonus = ucbBonus(totalPulls, arm.pulls);\n const coldStartBoost = arm.pulls === 0 ? 0.25 : 0;\n const adjustedScore = opportunity.score + mean * 1.5 + bonus * 0.35 + coldStartBoost;\n return {\n opportunity: {\n ...opportunity,\n score: adjustedScore,\n },\n adjustedScore,\n pulls: arm.pulls,\n };\n });\n\n const explorationBudget = clamp(state.explorationBudget, 0.1, 0.45);\n const exploreCount = Math.max(1, Math.floor(maxPoolSize * explorationBudget));\n const exploitCount = Math.max(1, maxPoolSize - exploreCount);\n\n const exploit = [...scored].sort((a, b) => b.adjustedScore - a.adjustedScore).slice(0, exploitCount);\n const selected = new Map<string, ActionOpportunity>();\n for (const row of exploit) {\n selected.set(row.opportunity.id, row.opportunity);\n }\n\n const remaining = scored\n .filter((row) => !selected.has(row.opportunity.id))\n .sort((a, b) => {\n if (a.pulls !== b.pulls) return a.pulls - b.pulls;\n return b.adjustedScore - a.adjustedScore;\n });\n\n // Add low-pull candidates first, with slight randomization for exploration.\n while (selected.size < maxPoolSize && remaining.length > 0) {\n const window = remaining.splice(0, Math.min(5, remaining.length));\n const pick = window[Math.floor(Math.random() * window.length)];\n if (!selected.has(pick.opportunity.id)) {\n selected.set(pick.opportunity.id, pick.opportunity);\n }\n for (const row of window) {\n if (row.opportunity.id !== pick.opportunity.id) {\n remaining.push(row);\n }\n }\n remaining.sort((a, b) => {\n if (a.pulls !== b.pulls) return a.pulls - b.pulls;\n return b.adjustedScore - a.adjustedScore;\n });\n }\n\n const picked = [...selected.values()].sort((a, b) => b.score - a.score).slice(0, maxPoolSize);\n saveState(state);\n return picked;\n}\n\nexport function recordBanditActionResults(actions: AgentAction[], results: ActionResult[]): void {\n if (actions.length === 0 || results.length === 0) return;\n const state = loadState();\n const now = new Date().toISOString();\n\n for (let i = 0; i < Math.min(actions.length, results.length); i += 1) {\n const action = actions[i];\n const result = results[i];\n const armKey = armKeyFromAction(action);\n const arm = ensureArm(state, armKey);\n\n const immediateReward = computeImmediateReward(result);\n arm.pulls += 1;\n arm.rewardSum += immediateReward;\n arm.lastReward = immediateReward;\n arm.updatedAt = now;\n state.totalPulls += 1;\n\n const tweetId = extractTweetId(action, result);\n if (result.success && tweetId && (action.action === \"post\" || action.action === \"reply\")) {\n const delayMs = action.action === \"reply\" ? 20 * 60 * 1000 : 45 * 60 * 1000;\n state.pending.push({\n id: `pending-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,\n armKey,\n actionType: action.action,\n tweetId,\n createdAt: now,\n resolveAt: new Date(Date.now() + delayMs).toISOString(),\n resolved: false,\n });\n }\n }\n\n saveState(state);\n}\n\nexport async function collectDelayedOutcomes(client: XClientInterface): Promise<OutcomeCollectionResult> {\n const state = loadState();\n const now = Date.now();\n const pendingDue = state.pending.filter((entry) => !entry.resolved && Date.parse(entry.resolveAt) <= now).slice(0, 8);\n if (pendingDue.length === 0) {\n return { processed: 0, resolved: 0, rewardAdded: 0 };\n }\n\n let rewardAdded = 0;\n let resolved = 0;\n const checkedAt = new Date().toISOString();\n\n for (const pending of pendingDue) {\n const arm = ensureArm(state, pending.armKey);\n let metrics: { likeCount: number; retweetCount: number; replyCount: number } | null = null;\n\n if (pending.tweetId) {\n metrics = await client.getTweetMetrics(pending.tweetId);\n if (metrics) {\n updatePostMetrics(pending.tweetId, {\n checkedAt,\n likes: metrics.likeCount,\n retweets: metrics.retweetCount,\n replies: metrics.replyCount,\n });\n }\n }\n\n const reward = resolveDelayedReward(\n metrics\n ? {\n likeCount: metrics.likeCount,\n retweetCount: metrics.retweetCount,\n replyCount: metrics.replyCount,\n }\n : null,\n );\n\n arm.rewardSum += reward;\n arm.lastReward = reward;\n arm.updatedAt = checkedAt;\n rewardAdded += reward;\n pending.resolved = true;\n resolved += 1;\n }\n\n saveState(state);\n logger.info(\n `Bandit delayed outcomes processed=${pendingDue.length}, resolved=${resolved}, rewardDelta=${rewardAdded.toFixed(2)}`\n );\n\n return {\n processed: pendingDue.length,\n resolved,\n rewardAdded,\n };\n}\n","import { getXClient } from \"../x-client/index.js\";\nimport { logger } from \"../utils/logger.js\";\nimport { getRecentInteractions } from \"../memory/index.js\";\nimport { listIntents, recordIntentExecution } from \"../memory/intents.js\";\nimport { loadIdentity } from \"../identity/index.js\";\nimport { loadCredentials } from \"../utils/crypto.js\";\nimport type { Tweet } from \"../x-client/types.js\";\nimport { buildSystemPrompt } from \"./prompt-builder.js\";\nimport { executeAction, type AgentAction, type ActionResult } from \"./decision-engine.js\";\nimport { evaluateActionPolicy } from \"./policy.js\";\nimport { generateResponse } from \"./llm.js\";\nimport { buildPersonaConstraintLines, getPersonaConstraints } from \"./persona-constraints.js\";\nimport {\n collectDelayedOutcomes,\n recordBanditActionResults,\n} from \"./bandit.js\";\n\nexport interface AutonomyCycleResult {\n timeline: Tweet[];\n mentions: Tweet[];\n actions: AgentAction[];\n results: ActionResult[];\n policyFeedback: string[];\n}\n\nfunction selfUserIdFromCredentials(): string | null {\n try {\n const creds = loadCredentials();\n const accessToken = creds.accessToken;\n if (!accessToken) return null;\n const dashIdx = accessToken.indexOf(\"-\");\n if (dashIdx <= 0) return null;\n const candidate = accessToken.substring(0, dashIdx);\n return /^\\d+$/.test(candidate) ? candidate : null;\n } catch {\n return null;\n }\n}\n\nfunction normalizeHandle(handle: string | undefined): string {\n return (handle ?? \"\").replace(/^@/, \"\").trim().toLowerCase();\n}\n\nfunction canonicalPlannerHandle(handle: string): string {\n const clean = normalizeHandle(handle);\n const aliasMap: Record<string, string> = {\n gork: \"grok\",\n spora: \"sporaai\",\n spora_ai: \"sporaai\",\n };\n return aliasMap[clean] ?? clean;\n}\n\nfunction canonicalizePlannerQuery(rawQuery: string): string {\n return rawQuery.replace(/from:([a-zA-Z0-9_]{1,15})/gi, (_match, handle) => `from:${canonicalPlannerHandle(handle)}`);\n}\n\ntype RewriteStyleMode =\n | \"quick_reaction\"\n | \"curious_question\"\n | \"friendly_pushback\"\n | \"plain_observation\"\n | \"playful_line\";\n\nfunction roughWordCount(text: string): number {\n return text.trim().split(/\\s+/).filter(Boolean).length;\n}\n\nfunction inferStyleMode(text: string): RewriteStyleMode {\n const lower = text.toLowerCase();\n if (text.includes(\"?\")) return \"curious_question\";\n if (/\\b(lol|lmao|haha|wild|fr|ngl)\\b/i.test(text)) return \"playful_line\";\n if (/\\b(not|isn'?t|wrong|nah|nope|doesn'?t)\\b/i.test(lower)) return \"friendly_pushback\";\n if (roughWordCount(text) <= 9) return \"quick_reaction\";\n return \"plain_observation\";\n}\n\nfunction chooseRewriteStyleMode(recentTexts?: string[], targetTweetText?: string): RewriteStyleMode {\n const counts: Record<RewriteStyleMode, number> = {\n quick_reaction: 0,\n curious_question: 0,\n friendly_pushback: 0,\n plain_observation: 0,\n playful_line: 0,\n };\n for (const text of (recentTexts ?? []).slice(0, 10)) {\n counts[inferStyleMode(text)] += 1;\n }\n\n if ((targetTweetText ?? \"\").includes(\"?\") && counts.curious_question <= 2) {\n return \"curious_question\";\n }\n\n return (Object.entries(counts).sort((a, b) => a[1] - b[1])[0]?.[0] as RewriteStyleMode) ?? \"plain_observation\";\n}\n\nfunction rewriteStyleModeInstructions(mode: RewriteStyleMode): string[] {\n if (mode === \"quick_reaction\") {\n return [\n \"- style mode: quick reaction\",\n \"- 6-14 words, one short sentence\",\n \"- immediate reaction tone, no thesis statement\",\n ];\n }\n if (mode === \"curious_question\") {\n return [\n \"- style mode: curious question\",\n \"- include exactly one direct question\",\n \"- ask about a specific detail from the target tweet\",\n ];\n }\n if (mode === \"friendly_pushback\") {\n return [\n \"- style mode: friendly pushback\",\n \"- politely disagree in plain words\",\n \"- no lecture or abstract framing\",\n ];\n }\n if (mode === \"playful_line\") {\n return [\n \"- style mode: playful line\",\n \"- keep it light and witty, not sarcastic essay mode\",\n \"- avoid internet clichés and overused slogans\",\n ];\n }\n return [\n \"- style mode: plain observation\",\n \"- write like a natural human thought said out loud\",\n \"- concrete detail first, no broad generalization\",\n ];\n}\n\nfunction shouldAttemptStyleRewrite(action: AgentAction, reason: string): boolean {\n if (!action.content || (action.action !== \"reply\" && action.action !== \"post\")) return false;\n const lower = reason.toLowerCase();\n return (\n lower.includes(\"lecture-like\") ||\n lower.includes(\"abstract philosopher cadence\") ||\n lower.includes(\"manifesto\") ||\n lower.includes(\"too long/explanatory\") ||\n lower.includes(\"grounded in the target tweet context\") ||\n lower.includes(\"hedged consensus phrasing\") ||\n lower.includes(\"repetitive anchor phrase/term\") ||\n lower.includes(\"repetitive opening phrase\") ||\n lower.includes(\"low-novelty vocabulary loop\") ||\n lower.includes(\"abstract wording without concrete anchor\")\n );\n}\n\nfunction cleanRewriteOutput(text: string): string {\n const cleaned = text\n .replace(/```[\\s\\S]*?```/g, \"\")\n .replace(/^[\"'`]+|[\"'`]+$/g, \"\")\n .trim();\n const firstLine = cleaned.split(\"\\n\").map((line) => line.trim()).filter(Boolean)[0] ?? cleaned;\n return firstLine.trim();\n}\n\nasync function rewriteDraftForHumanVoice(input: {\n action: AgentAction;\n reason: string;\n targetTweetText?: string;\n recentTexts?: string[];\n}): Promise<string | null> {\n if (!input.action.content) return null;\n const identity = loadIdentity();\n const maxChars = input.action.action === \"reply\" ? 100 : 130;\n const minChars = input.action.action === \"reply\" ? 18 : 24;\n const styleMode = chooseRewriteStyleMode(input.recentTexts, input.targetTweetText);\n\n const system = [\n `You rewrite X/Twitter drafts into ${identity.name}'s natural voice.`,\n \"Output must feel human, direct, and contextual.\",\n \"Never write abstract philosophy or explanatory manifesto style.\",\n \"Return ONLY rewritten tweet text, no commentary.\",\n ].join(\" \");\n\n const promptParts: string[] = [];\n promptParts.push(`Current draft: ${input.action.content}`);\n promptParts.push(`Rejected because: ${input.reason}`);\n if (input.targetTweetText) {\n promptParts.push(`Target tweet context: ${input.targetTweetText}`);\n }\n promptParts.push(`Constraints:`);\n promptParts.push(`- ${minChars}-${maxChars} characters`);\n promptParts.push(\"- 1-2 short sentences max\");\n promptParts.push(\"- no 'the real question' / 'the deeper question' framing\");\n promptParts.push(\"- no colon, semicolon, or em dash\");\n promptParts.push(\"- specific and concrete, not abstract\");\n promptParts.push(\"- keep the same core stance\");\n promptParts.push(\"- use natural spoken phrasing and contractions when it fits\");\n for (const line of rewriteStyleModeInstructions(styleMode)) {\n promptParts.push(line);\n }\n if (input.recentTexts && input.recentTexts.length > 0) {\n promptParts.push(\"- avoid vocabulary anchors from these recent outputs:\");\n for (const text of input.recentTexts.slice(0, 5)) {\n promptParts.push(` - ${text.slice(0, 120)}`);\n }\n }\n\n try {\n const rewrite = await generateResponse(system, promptParts.join(\"\\n\"));\n const candidate = cleanRewriteOutput(rewrite.content);\n if (!candidate) return null;\n return candidate;\n } catch {\n return null;\n }\n}\n\ninterface LoopTopicSearchResult {\n query: string;\n tweets: Tweet[];\n}\n\ninterface LoopPersonActivity {\n handle: string;\n userId: string;\n reason: string;\n tweets: Tweet[];\n}\n\ninterface LoopResearchState {\n timeline: Tweet[];\n mentions: Tweet[];\n topicSearchResults: LoopTopicSearchResult[];\n peopleActivity: LoopPersonActivity[];\n}\n\ntype PlannerToolName =\n | \"observe_timeline\"\n | \"observe_mentions\"\n | \"search_tweets\"\n | \"check_profile\"\n | \"reply\"\n | \"like\"\n | \"retweet\"\n | \"follow\"\n | \"post\"\n | \"skip\";\n\ninterface PlannerDecision {\n tool: PlannerToolName;\n args: Record<string, unknown>;\n reasoning?: string;\n}\n\ninterface ToolStepSummary {\n step: number;\n tool: PlannerToolName;\n summary: string;\n}\n\nfunction loopClampInt(value: number, min: number, max: number): number {\n return Math.max(min, Math.min(max, Math.round(value)));\n}\n\nfunction parseJsonObjectFromText(text: string): Record<string, unknown> | null {\n const fenced = text.match(/```(?:json)?\\s*([\\s\\S]*?)```/i)?.[1];\n const candidates = [fenced, text].filter((v): v is string => Boolean(v)).map((v) => v.trim());\n for (const candidate of candidates) {\n try {\n const parsed = JSON.parse(candidate);\n if (parsed && typeof parsed === \"object\" && !Array.isArray(parsed)) {\n return parsed as Record<string, unknown>;\n }\n if (Array.isArray(parsed) && parsed.length > 0 && parsed[0] && typeof parsed[0] === \"object\") {\n return parsed[0] as Record<string, unknown>;\n }\n } catch {\n // Continue to extraction fallback.\n }\n const obj = candidate.match(/\\{[\\s\\S]*\\}/);\n if (!obj) continue;\n try {\n const parsed = JSON.parse(obj[0]);\n if (parsed && typeof parsed === \"object\" && !Array.isArray(parsed)) {\n return parsed as Record<string, unknown>;\n }\n } catch {\n // Continue.\n }\n }\n return null;\n}\n\nfunction mapPlannerTool(raw: string | undefined): PlannerToolName | null {\n const clean = (raw ?? \"\").trim().toLowerCase();\n if (!clean) return null;\n if (clean === \"observe_timeline\" || clean === \"read_timeline\" || clean === \"timeline\") return \"observe_timeline\";\n if (clean === \"observe_mentions\" || clean === \"read_mentions\" || clean === \"mentions\") return \"observe_mentions\";\n if (clean === \"search_tweets\" || clean === \"search\" || clean === \"topic_search\") return \"search_tweets\";\n if (clean === \"check_profile\" || clean === \"check_person\" || clean === \"people_check\" || clean === \"profile_check\") return \"check_profile\";\n if (clean === \"reply\") return \"reply\";\n if (clean === \"like\") return \"like\";\n if (clean === \"retweet\") return \"retweet\";\n if (clean === \"follow\") return \"follow\";\n if (clean === \"post\" || clean === \"tweet\") return \"post\";\n if (clean === \"skip\" || clean === \"noop\" || clean === \"no_action\") return \"skip\";\n return null;\n}\n\nfunction parsePlannerDecision(text: string): PlannerDecision | null {\n const parsed = parseJsonObjectFromText(text);\n if (!parsed) return null;\n\n const rawTool =\n typeof parsed.tool === \"string\"\n ? parsed.tool\n : typeof parsed.action === \"string\"\n ? parsed.action\n : undefined;\n const tool = mapPlannerTool(rawTool);\n if (!tool) return null;\n\n const rawArgs = parsed.args;\n const args: Record<string, unknown> =\n rawArgs && typeof rawArgs === \"object\" && !Array.isArray(rawArgs)\n ? { ...(rawArgs as Record<string, unknown>) }\n : {};\n\n for (const key of [\"tweetId\", \"content\", \"handle\", \"query\", \"count\", \"reason\"]) {\n if (args[key] !== undefined) continue;\n if (parsed[key] !== undefined) args[key] = parsed[key];\n }\n\n const reasoning = typeof parsed.reasoning === \"string\" ? parsed.reasoning.trim() : undefined;\n return { tool, args, reasoning };\n}\n\nfunction argString(args: Record<string, unknown>, key: string): string | undefined {\n const value = args[key];\n if (typeof value !== \"string\") return undefined;\n const clean = value.trim();\n return clean.length > 0 ? clean : undefined;\n}\n\nfunction argCount(args: Record<string, unknown>, fallback: number, min: number, max: number): number {\n const raw = args.count;\n if (typeof raw === \"number\" && Number.isFinite(raw)) {\n return loopClampInt(raw, min, max);\n }\n if (typeof raw === \"string\" && raw.trim().length > 0) {\n const parsed = Number(raw);\n if (Number.isFinite(parsed)) return loopClampInt(parsed, min, max);\n }\n return loopClampInt(fallback, min, max);\n}\n\nfunction tweetTimestamp(tweet: Tweet): number {\n const ts = Date.parse(tweet.createdAt);\n return Number.isNaN(ts) ? 0 : ts;\n}\n\nfunction mergeTweetList(current: Tweet[], incoming: Tweet[]): { tweets: Tweet[]; added: number } {\n const byId = new Map<string, Tweet>();\n for (const tweet of current) byId.set(tweet.id, tweet);\n let added = 0;\n for (const tweet of incoming) {\n if (!tweet.id) continue;\n if (!byId.has(tweet.id)) added += 1;\n byId.set(tweet.id, tweet);\n }\n const tweets = [...byId.values()].sort((a, b) => tweetTimestamp(b) - tweetTimestamp(a));\n return { tweets, added };\n}\n\nfunction mergeTopicResult(\n current: LoopTopicSearchResult[],\n query: string,\n incoming: Tweet[],\n): { next: LoopTopicSearchResult[]; added: number } {\n const idx = current.findIndex((item) => item.query.toLowerCase() === query.toLowerCase());\n if (idx < 0) {\n const merged = mergeTweetList([], incoming);\n return {\n next: [...current, { query, tweets: merged.tweets }],\n added: merged.added,\n };\n }\n const merged = mergeTweetList(current[idx].tweets, incoming);\n const next = [...current];\n next[idx] = { ...next[idx], tweets: merged.tweets };\n return { next, added: merged.added };\n}\n\nfunction mergePeopleActivity(\n current: LoopPersonActivity[],\n incoming: LoopPersonActivity,\n): { next: LoopPersonActivity[]; added: number } {\n const handle = normalizeHandle(incoming.handle);\n const idx = current.findIndex((item) => normalizeHandle(item.handle) === handle);\n if (idx < 0) {\n const merged = mergeTweetList([], incoming.tweets);\n return {\n next: [...current, { ...incoming, handle, tweets: merged.tweets }],\n added: merged.added,\n };\n }\n const merged = mergeTweetList(current[idx].tweets, incoming.tweets);\n const next = [...current];\n next[idx] = {\n ...next[idx],\n userId: incoming.userId || next[idx].userId,\n reason: incoming.reason || next[idx].reason,\n tweets: merged.tweets,\n };\n return { next, added: merged.added };\n}\n\nfunction collectObserved(state: LoopResearchState): {\n ids: string[];\n tweets: Tweet[];\n byId: Map<string, Tweet>;\n} {\n const tweets = [\n ...state.timeline,\n ...state.mentions,\n ...state.topicSearchResults.flatMap((item) => item.tweets),\n ...state.peopleActivity.flatMap((item) => item.tweets),\n ];\n const byId = new Map<string, Tweet>();\n for (const tweet of tweets) {\n if (!tweet.id || byId.has(tweet.id)) continue;\n byId.set(tweet.id, tweet);\n }\n return {\n ids: [...byId.keys()],\n tweets: [...byId.values()],\n byId,\n };\n}\n\nfunction buildPlannerCandidates(\n state: LoopResearchState,\n): Tweet[] {\n const ordered = [\n ...state.mentions,\n ...state.timeline,\n ...state.topicSearchResults.flatMap((item) => item.tweets),\n ...state.peopleActivity.flatMap((item) => item.tweets),\n ];\n const byId = new Map<string, Tweet>();\n for (const tweet of ordered) {\n if (!tweet.id) continue;\n if (!byId.has(tweet.id)) byId.set(tweet.id, tweet);\n }\n return [...byId.values()].slice(0, 24);\n}\n\nfunction buildToolLoopPrompt(input: {\n identity: {\n name: string;\n handle: string;\n bio: string;\n tone: string;\n goals: string[];\n boundaries: string[];\n };\n constraintLines: string[];\n candidates: Tweet[];\n state: LoopResearchState;\n actions: AgentAction[];\n policyFeedback: string[];\n toolHistory: ToolStepSummary[];\n recentMemory: string[];\n heartbeatCount: number;\n}): string {\n const lines: string[] = [];\n lines.push(\"Choose exactly ONE tool call in strict JSON.\");\n lines.push(\"You are the autonomous planner. No fixed research sequence is required.\");\n lines.push(\"Act based on persona + live context, not templates.\");\n lines.push(\"\");\n lines.push(`Heartbeat: #${input.heartbeatCount}`);\n lines.push(`Agent: ${input.identity.name} (@${input.identity.handle})`);\n lines.push(`Bio: ${input.identity.bio}`);\n lines.push(`Tone: ${input.identity.tone}`);\n lines.push(`Goals: ${input.identity.goals.join(\" | \") || \"none\"}`);\n lines.push(`Boundaries: ${input.identity.boundaries.join(\" | \") || \"none\"}`);\n lines.push(\"\");\n if (input.constraintLines.length > 0) {\n lines.push(\"Hard persona constraints:\");\n for (const line of input.constraintLines) lines.push(line);\n lines.push(\"\");\n }\n if (input.recentMemory.length > 0) {\n lines.push(\"Recent memory:\");\n for (const row of input.recentMemory.slice(0, 8)) {\n lines.push(`- ${row}`);\n }\n lines.push(\"\");\n }\n lines.push(`Executed actions this heartbeat: ${input.actions.length}`);\n lines.push(\n `Current context counts: timeline=${input.state.timeline.length}, mentions=${input.state.mentions.length}, topicTweets=${input.state.topicSearchResults.reduce((sum, item) => sum + item.tweets.length, 0)}, peopleTweets=${input.state.peopleActivity.reduce((sum, item) => sum + item.tweets.length, 0)}`\n );\n lines.push(\"\");\n if (input.toolHistory.length > 0) {\n lines.push(\"Recent tool steps:\");\n for (const row of input.toolHistory.slice(-6)) {\n lines.push(`- Step ${row.step}: ${row.tool} -> ${row.summary}`);\n }\n lines.push(\"\");\n }\n if (input.policyFeedback.length > 0) {\n lines.push(\"Recent policy feedback:\");\n for (const note of input.policyFeedback.slice(-8)) {\n lines.push(`- ${note}`);\n }\n lines.push(\"\");\n }\n if (input.candidates.length > 0) {\n lines.push(`Top candidate tweets (${input.candidates.length}):`);\n for (const tweet of input.candidates.slice(0, 16)) {\n lines.push(\n `- [${tweet.id}] @${tweet.authorHandle}: ${tweet.text.slice(0, 190)} (${tweet.likeCount ?? 0} likes, ${tweet.replyCount ?? 0} replies)`\n );\n }\n lines.push(\"\");\n } else {\n lines.push(\"No candidate tweets currently in context.\");\n lines.push(\"\");\n }\n lines.push(\"Tool options:\");\n lines.push('- observe_timeline: { \"count\": 8-30 }');\n lines.push('- observe_mentions: { \"count\": 8-30 }');\n lines.push('- search_tweets: { \"query\": \"x api search query\", \"count\": 3-20 }');\n lines.push('- check_profile: { \"handle\": \"@username\", \"count\": 3-12 }');\n lines.push('- reply: { \"tweetId\": \"id\", \"content\": \"reply under 220 chars\" }');\n lines.push('- like: { \"tweetId\": \"id\" }');\n lines.push('- retweet: { \"tweetId\": \"id\" }');\n lines.push('- follow: { \"handle\": \"@username\" }');\n lines.push('- post: { \"content\": \"post under 220 chars\" }');\n lines.push('- skip: { \"reason\": \"short reason\" }');\n lines.push(\"\");\n lines.push(\"Output JSON object only:\");\n lines.push('{ \"tool\": \"reply\", \"args\": { \"tweetId\": \"...\", \"content\": \"...\" }, \"reasoning\": \"short why\" }');\n lines.push(\"\");\n lines.push(\"Planning rules:\");\n lines.push(\"- if timeline/mentions already have viable tweets, prefer acting over more research\");\n lines.push(\"- use search/profile checks only when you have a clear reason\");\n lines.push(\"- avoid repeating the same query/target unless context changed\");\n lines.push(\"\");\n lines.push(\"Writing constraints:\");\n lines.push(\"- sound human and specific, no manifesto language\");\n lines.push(\"- avoid 'the real question' / 'the deeper question' phrasing\");\n lines.push(\"- no repetitive anchor terms from recent outputs\");\n lines.push(\"- match the persona tone strictly\");\n return lines.join(\"\\n\");\n}\n\nfunction fallbackPlannerDecision(step: number, observedCount: number): PlannerDecision {\n if (observedCount === 0) {\n return {\n tool: step % 2 === 0 ? \"observe_timeline\" : \"observe_mentions\",\n args: { count: 20 },\n reasoning: \"fallback context bootstrap\",\n };\n }\n return {\n tool: \"skip\",\n args: { reason: \"planner parse fallback; preserve current context\" },\n reasoning: \"fallback no-op\",\n };\n}\n\nfunction decisionToAgentAction(decision: PlannerDecision): AgentAction | null {\n if (decision.tool === \"reply\") {\n const tweetId = argString(decision.args, \"tweetId\");\n const content = argString(decision.args, \"content\");\n if (!tweetId || !content) return null;\n return { action: \"reply\", tweetId, content, reasoning: decision.reasoning };\n }\n if (decision.tool === \"like\") {\n const tweetId = argString(decision.args, \"tweetId\");\n if (!tweetId) return null;\n return { action: \"like\", tweetId, reasoning: decision.reasoning };\n }\n if (decision.tool === \"retweet\") {\n const tweetId = argString(decision.args, \"tweetId\");\n if (!tweetId) return null;\n return { action: \"retweet\", tweetId, reasoning: decision.reasoning };\n }\n if (decision.tool === \"follow\") {\n const handle = argString(decision.args, \"handle\");\n if (!handle) return null;\n return { action: \"follow\", handle, targetHandle: handle, reasoning: decision.reasoning };\n }\n if (decision.tool === \"post\") {\n const content = argString(decision.args, \"content\");\n if (!content) return null;\n return { action: \"post\", content, reasoning: decision.reasoning };\n }\n if (decision.tool === \"skip\") {\n return { action: \"skip\", reason: argString(decision.args, \"reason\"), reasoning: decision.reasoning };\n }\n return null;\n}\n\nexport async function runAutonomyCycle(maxActions: number, heartbeatCount: number = 0): Promise<AutonomyCycleResult> {\n const client = await getXClient();\n const identity = loadIdentity();\n const constraints = getPersonaConstraints();\n const constraintLines = buildPersonaConstraintLines(constraints);\n if (constraintLines.length > 0) {\n logger.info(`Persona constraints active: ${constraintLines.join(\" | \")}`);\n }\n logger.info(`Autonomy runtime v2 active: planner-primary, persona+memory driven.`);\n\n try {\n const delayed = await collectDelayedOutcomes(client);\n if (delayed.processed > 0) {\n logger.info(\n `Delayed outcomes: processed=${delayed.processed}, resolved=${delayed.resolved}, rewardDelta=${delayed.rewardAdded.toFixed(2)}`\n );\n }\n } catch (error) {\n logger.warn(`Delayed outcome collection failed: ${(error as Error).message}`);\n }\n\n const selfHandle = identity.handle.replace(/^@/, \"\").toLowerCase();\n const selfUserId = selfUserIdFromCredentials();\n const isSelfTweet = (tweet: Tweet): boolean =>\n tweet.authorHandle.replace(/^@/, \"\").toLowerCase() === selfHandle ||\n (selfUserId !== null && tweet.authorId === selfUserId);\n\n const state: LoopResearchState = {\n timeline: [],\n mentions: [],\n topicSearchResults: [],\n peopleActivity: [],\n };\n\n try {\n const timeline = (await client.getTimeline({ count: 30 })).filter((tweet) => !isSelfTweet(tweet));\n state.timeline = mergeTweetList(state.timeline, timeline).tweets;\n } catch (error) {\n logger.warn(`Seed timeline read failed: ${(error as Error).message}`);\n }\n\n try {\n const mentions = (await client.getMentions({ count: 30 })).filter((tweet) => !isSelfTweet(tweet));\n state.mentions = mergeTweetList(state.mentions, mentions).tweets;\n } catch (error) {\n logger.warn(`Seed mentions read failed: ${(error as Error).message}`);\n }\n\n logger.info(\n `Autonomy context: timeline=${state.timeline.length}, mentions=${state.mentions.length}, topicTweets=${state.topicSearchResults.reduce((sum, item) => sum + item.tweets.length, 0)}, peopleTweets=${state.peopleActivity.reduce((sum, item) => sum + item.tweets.length, 0)}`\n );\n\n const actions: AgentAction[] = [];\n const results: ActionResult[] = [];\n const toolHistory: ToolStepSummary[] = [];\n const policyFeedback: string[] = [\n `Persona: ${identity.name} (@${identity.handle})`,\n `Tone: ${identity.tone}`,\n `Goals: ${(identity.goals ?? []).slice(0, 4).join(\" | \") || \"none\"}`,\n ];\n\n const systemPrompt = buildSystemPrompt();\n const toolSystemPrompt = [\n systemPrompt,\n \"TOOL LOOP MODE: You must choose exactly one tool call each step using strict JSON.\",\n \"Planner-first mode: there is no pre-defined research flow.\",\n \"Everything must be persona-driven, memory-aware, and context-grounded.\",\n \"Do not fall into repetitive searches or repetitive targets.\",\n ].join(\"\\n\\n\");\n const activeIntents = listIntents();\n let staleSteps = 0;\n let attemptedActions = 0;\n const maxToolSteps = Math.max(12, maxActions * 6);\n\n // Keep short memory available in context by touching it before loop.\n getRecentInteractions(20);\n\n for (let step = 0; step < maxToolSteps && actions.length < maxActions; step += 1) {\n const candidates = buildPlannerCandidates(state);\n const recentMemory = getRecentInteractions(24)\n .slice(0, 12)\n .map((entry) => {\n const text = (entry.content ?? \"\").replace(/\\s+/g, \" \").trim().slice(0, 120);\n const target = entry.targetHandle ? ` @${entry.targetHandle}` : \"\";\n return `${entry.type}${target}${text ? `: ${text}` : \"\"}`;\n });\n const prompt = buildToolLoopPrompt({\n identity: {\n name: identity.name,\n handle: identity.handle,\n bio: identity.bio,\n tone: identity.tone,\n goals: identity.goals ?? [],\n boundaries: identity.boundaries ?? [],\n },\n constraintLines,\n candidates,\n state,\n actions,\n policyFeedback,\n toolHistory,\n recentMemory,\n heartbeatCount,\n });\n\n let decision = fallbackPlannerDecision(step, candidates.length);\n try {\n const response = await generateResponse(toolSystemPrompt, prompt);\n const parsed = parsePlannerDecision(response.content);\n if (parsed) {\n decision = parsed;\n } else {\n policyFeedback.push(\"Planner returned invalid JSON decision. Using fallback tool.\");\n }\n } catch (error) {\n policyFeedback.push(`Planner call failed: ${(error as Error).message}`);\n }\n\n logger.info(`Tool loop step ${step + 1}: ${decision.tool}`);\n let stepProgress = false;\n\n if (decision.tool === \"observe_timeline\") {\n const count = argCount(decision.args, 16, 8, 30);\n try {\n const incoming = (await client.getTimeline({ count })).filter((tweet) => !isSelfTweet(tweet));\n const merged = mergeTweetList(state.timeline, incoming);\n state.timeline = merged.tweets;\n stepProgress = merged.added > 0;\n toolHistory.push({\n step: step + 1,\n tool: decision.tool,\n summary: `fetched ${incoming.length}, added ${merged.added}`,\n });\n } catch (error) {\n policyFeedback.push(`observe_timeline failed: ${(error as Error).message}`);\n }\n if (!stepProgress) policyFeedback.push(\"observe_timeline found no new tweets.\");\n continue;\n }\n\n if (decision.tool === \"observe_mentions\") {\n const count = argCount(decision.args, 16, 8, 30);\n try {\n const incoming = (await client.getMentions({ count })).filter((tweet) => !isSelfTweet(tweet));\n const merged = mergeTweetList(state.mentions, incoming);\n state.mentions = merged.tweets;\n stepProgress = merged.added > 0;\n toolHistory.push({\n step: step + 1,\n tool: decision.tool,\n summary: `fetched ${incoming.length}, added ${merged.added}`,\n });\n } catch (error) {\n policyFeedback.push(`observe_mentions failed: ${(error as Error).message}`);\n }\n if (!stepProgress) policyFeedback.push(\"observe_mentions found no new tweets.\");\n continue;\n }\n\n if (decision.tool === \"search_tweets\") {\n const rawQuery = argString(decision.args, \"query\");\n if (!rawQuery) {\n policyFeedback.push(\"search_tweets rejected: missing query.\");\n continue;\n }\n const query = canonicalizePlannerQuery(rawQuery);\n if (query !== rawQuery) {\n policyFeedback.push(`search_tweets normalized handle query: \"${rawQuery}\" -> \"${query}\"`);\n }\n const count = argCount(decision.args, 10, 3, 20);\n try {\n const incoming = (await client.searchTweets(query, { count })).filter((tweet) => !isSelfTweet(tweet));\n const merged = mergeTopicResult(state.topicSearchResults, query, incoming);\n state.topicSearchResults = merged.next;\n stepProgress = merged.added > 0;\n toolHistory.push({\n step: step + 1,\n tool: decision.tool,\n summary: `\"${query}\" fetched ${incoming.length}, added ${merged.added}`,\n });\n logger.info(`Tool search \"${query}\": found ${incoming.length} tweets`);\n } catch (error) {\n policyFeedback.push(`search_tweets failed for \"${query}\": ${(error as Error).message}`);\n }\n if (!stepProgress) policyFeedback.push(`search_tweets \"${query}\" found no new tweets.`);\n continue;\n }\n\n if (decision.tool === \"check_profile\") {\n const handleInput = argString(decision.args, \"handle\");\n const handle = handleInput ? canonicalPlannerHandle(handleInput) : \"\";\n if (!handle) {\n policyFeedback.push(\"check_profile rejected: missing handle.\");\n continue;\n }\n const count = argCount(decision.args, 6, 3, 12);\n try {\n const profile = await client.getProfile(handle);\n const tweets = (await client.getUserTweets(profile.id, { count })).filter((tweet) => !isSelfTweet(tweet));\n const merged = mergePeopleActivity(state.peopleActivity, {\n handle,\n userId: profile.id,\n reason: \"tool-loop check\",\n tweets,\n });\n state.peopleActivity = merged.next;\n stepProgress = merged.added > 0;\n toolHistory.push({\n step: step + 1,\n tool: decision.tool,\n summary: `@${handle} fetched ${tweets.length}, added ${merged.added}`,\n });\n logger.info(`People check @${handle}: ${tweets.length} recent tweets`);\n } catch (error) {\n policyFeedback.push(`check_profile failed for @${handle}: ${(error as Error).message}`);\n }\n if (!stepProgress) policyFeedback.push(`check_profile @${handle} found no new tweets.`);\n continue;\n }\n\n const observed = collectObserved(state);\n let candidateAction = decisionToAgentAction(decision);\n if (!candidateAction) {\n policyFeedback.push(`Tool decision ${decision.tool} rejected: invalid arguments.`);\n staleSteps += 1;\n continue;\n }\n\n if (candidateAction.tweetId && observed.byId.has(candidateAction.tweetId)) {\n const author = normalizeHandle(observed.byId.get(candidateAction.tweetId)?.authorHandle);\n if (author && !candidateAction.targetHandle) {\n candidateAction.targetHandle = `@${author}`;\n }\n }\n\n if (candidateAction.action === \"skip\") {\n const reason = candidateAction.reason ?? candidateAction.reasoning ?? \"planner skip\";\n policyFeedback.push(`Planner chose skip: ${reason}`);\n toolHistory.push({\n step: step + 1,\n tool: decision.tool,\n summary: reason,\n });\n staleSteps += 1;\n if (staleSteps >= 4) break;\n continue;\n }\n\n attemptedActions += 1;\n let policy = evaluateActionPolicy({\n action: candidateAction,\n step: attemptedActions,\n timeline: state.timeline,\n mentions: state.mentions,\n executedActions: actions,\n observedTweetIds: observed.ids,\n observedTweets: observed.tweets,\n selfHandle,\n selfUserId,\n });\n\n if (!policy.allowed && shouldAttemptStyleRewrite(candidateAction, policy.reason ?? \"\")) {\n const recentTexts = getRecentInteractions(10)\n .filter((entry) => entry.type === \"post\" || entry.type === \"reply\")\n .map((entry) => entry.content ?? \"\")\n .filter(Boolean);\n const rewritten = await rewriteDraftForHumanVoice({\n action: candidateAction,\n reason: policy.reason ?? \"policy rejected\",\n targetTweetText: candidateAction.tweetId ? observed.byId.get(candidateAction.tweetId)?.text : undefined,\n recentTexts,\n });\n\n if (rewritten && rewritten !== candidateAction.content) {\n candidateAction = {\n ...candidateAction,\n content: rewritten,\n reasoning: `${candidateAction.reasoning ?? \"rewritten\"} | style rewrite`,\n };\n policy = evaluateActionPolicy({\n action: candidateAction,\n step: attemptedActions,\n timeline: state.timeline,\n mentions: state.mentions,\n executedActions: actions,\n observedTweetIds: observed.ids,\n observedTweets: observed.tweets,\n selfHandle,\n selfUserId,\n });\n }\n }\n\n if (!policy.allowed) {\n const reason = policy.reason ?? \"Policy rejected action\";\n // Policy is fully advisory in planner-primary mode.\n policyFeedback.push(`Policy advisory (not blocking): ${reason}`);\n logger.info(`Policy advisory for ${candidateAction.action}: ${reason}`);\n }\n\n const result = await executeAction(candidateAction);\n actions.push(candidateAction);\n results.push(result);\n staleSteps = 0;\n stepProgress = true;\n toolHistory.push({\n step: step + 1,\n tool: decision.tool,\n summary: `${candidateAction.action}${result.success ? \" success\" : \" failed\"}`,\n });\n\n if (activeIntents.length > 0) {\n const touchedHandle = normalizeHandle(\n candidateAction.targetHandle ??\n candidateAction.handle ??\n (candidateAction.tweetId ? observed.byId.get(candidateAction.tweetId)?.authorHandle : undefined)\n );\n const loweredContent = (candidateAction.content ?? \"\").toLowerCase();\n for (const intent of activeIntents) {\n const handleMatch =\n touchedHandle.length > 0 &&\n intent.targetHandles.some((handle) => normalizeHandle(handle) === touchedHandle);\n const topicMatch =\n loweredContent.length > 0 &&\n intent.focusTopics.some((topic) => loweredContent.includes(topic.toLowerCase()));\n if (!handleMatch && !topicMatch) continue;\n recordIntentExecution(intent.id, {\n success: result.success,\n note: `${candidateAction.action}${touchedHandle ? ` @${touchedHandle}` : \"\"}`,\n });\n }\n }\n\n if (!result.success) {\n const err = result.error ?? \"\";\n if (candidateAction.action === \"reply\" && /duplicate content/i.test(err)) {\n const reason = \"Reply failed with duplicate-content error. Planner should choose a different wording/target.\";\n policyFeedback.push(reason);\n logger.info(`Policy adjustment: ${reason}`);\n }\n if (candidateAction.action === \"post\" && /duplicate content/i.test(err)) {\n const reason = \"Post failed with duplicate-content error. Change framing and try a different angle.\";\n policyFeedback.push(reason);\n logger.info(`Policy adjustment: ${reason}`);\n }\n }\n\n if (!stepProgress) {\n staleSteps += 1;\n }\n if (staleSteps >= 5) {\n logger.info(\"Tool loop stopped after repeated no-progress steps.\");\n break;\n }\n }\n\n recordBanditActionResults(actions, results);\n\n return {\n timeline: state.timeline,\n mentions: state.mentions,\n actions,\n results,\n policyFeedback,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkHA,eAAsB,cAAc,QAA4C;AAC9E,QAAM,EAAE,QAAQ,KAAK,IAAI;AAEzB,MAAI;AACF,YAAQ,MAAM;AAAA,MACZ,KAAK,QAAQ;AACX,YAAI,CAAC,OAAO,QAAS,QAAO,EAAE,QAAQ,MAAM,SAAS,OAAO,OAAO,sBAAsB;AACzF,YAAI,OAAO,QAAQ,SAAS,KAAK;AAC/B,iBAAO,EAAE,QAAQ,MAAM,SAAS,OAAO,OAAO,mBAAmB,O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AY;AACnE,iBAAW,UAAU,eAAe;AAClC,cAAM,cACJ,cAAc,SAAS,KACvB,OAAO,cAAc,KAAK,CAAC,WAAWA,iBAAgB,MAAM,MAAM,aAAa;AACjF,cAAM,aACJ,eAAe,SAAS,KACxB,OAAO,YAAY,KAAK,CAAC,UAAU,eAAe,SAAS,MAAM,YAAY,CAAC,CAAC;AACjF,YAAI,CAAC,eAAe,CAAC,WAAY;AACjC,8BAAsB,OAAO,IAAI;AAAA,UAC/B,SAAS,OAAO;AAAA,UAChB,MAAM,GAAG,gBAAgB,MAAM,GAAG,gBAAgB,KAAK,aAAa,KAAK,EAAE;AAAA,QAC7E,CAAC;AAAA,MACH;AAAA,IACF;AAEA,QAAI,CAAC,OAAO,SAAS;AACnB,YAAM,MAAM,OAAO,SAAS;AAC5B,UAAI,gBAAgB,WAAW,WAAW,qBAAqB,KAAK,GAAG,GAAG;AACxE,cAAM,SAAS;AACf,uBAAe,KAAK,MAAM;AAC1B,eAAO,KAAK,sBAAsB,MAAM,EAAE;AAAA,MAC5C;AACA,UAAI,gBAAgB,WAAW,UAAU,qBAAqB,KAAK,GAAG,GAAG;AACvE,cAAM,SAAS;AACf,uBAAe,KAAK,MAAM;AAC1B,eAAO,KAAK,sBAAsB,MAAM,EAAE;AAAA,MAC5C;AAAA,IACF;AAEA,QAAI,CAAC,cAAc;AACjB,oBAAc;AAAA,IAChB;AACA,QAAI,cAAc,GAAG;AACnB,aAAO,KAAK,qDAAqD;AACjE;AAAA,IACF;AAAA,EACF;AAEA,4BAA0B,SAAS,OAAO;AAE1C,SAAO;AAAA,IACL,UAAU,MAAM;AAAA,IAChB,UAAU,MAAM;AAAA,IAChB;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":["normalizeHandle","merged"]}
package/dist/cli.js CHANGED
@@ -67,7 +67,7 @@ program.command("init").description("Set up X account credentials for your Spore
67
67
  console.log(chalk.cyan(BANNER));
68
68
  console.log(chalk.bold("Welcome to Spora."));
69
69
  console.log(chalk.gray("The global town square for AI agents.\n"));
70
- const { runInit } = await import("./init-6HY4ZPFJ.js");
70
+ const { runInit } = await import("./init-KL6XSI7E.js");
71
71
  await runInit(opts.token);
72
72
  });
73
73
  program.command("serve").description("Start the Spora MCP server (stdio)").action(async () => {
@@ -79,7 +79,7 @@ program.command("chat").description("Open web-based chat interface with your Spo
79
79
  console.log(chalk.red("\u2717 No identity found. Run `spora create` first."));
80
80
  process.exit(1);
81
81
  }
82
- const { startWebChat } = await import("./web-chat-AKUEBSWS.js");
82
+ const { startWebChat } = await import("./web-chat-XNTIDKAS.js");
83
83
  await startWebChat();
84
84
  });
85
85
  program.command("tui").description("Start terminal-based chat interface (TUI)").action(async () => {
@@ -222,7 +222,7 @@ program.command("journal").description("Add a reflection to the evolution journa
222
222
  });
223
223
  program.command("post").description("Post a tweet").argument("<content>", "Tweet content (max 280 chars)").action(async (content) => {
224
224
  try {
225
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
225
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
226
226
  const client = await getXClient();
227
227
  const result = await client.postTweet(content);
228
228
  console.log(JSON.stringify(result, null, 2));
@@ -233,7 +233,7 @@ program.command("post").description("Post a tweet").argument("<content>", "Tweet
233
233
  });
234
234
  program.command("reply").description("Reply to a tweet").argument("<tweetId>", "Tweet ID to reply to").argument("<content>", "Reply content").action(async (tweetId, content) => {
235
235
  try {
236
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
236
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
237
237
  const client = await getXClient();
238
238
  const result = await client.replyToTweet(tweetId, content);
239
239
  console.log(JSON.stringify(result, null, 2));
@@ -244,7 +244,7 @@ program.command("reply").description("Reply to a tweet").argument("<tweetId>", "
244
244
  });
245
245
  program.command("like").description("Like a tweet").argument("<tweetId>", "Tweet ID").action(async (tweetId) => {
246
246
  try {
247
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
247
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
248
248
  const client = await getXClient();
249
249
  const result = await client.likeTweet(tweetId);
250
250
  console.log(JSON.stringify(result, null, 2));
@@ -255,7 +255,7 @@ program.command("like").description("Like a tweet").argument("<tweetId>", "Tweet
255
255
  });
256
256
  program.command("retweet").description("Retweet a tweet").argument("<tweetId>", "Tweet ID").action(async (tweetId) => {
257
257
  try {
258
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
258
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
259
259
  const client = await getXClient();
260
260
  const result = await client.retweet(tweetId);
261
261
  console.log(JSON.stringify(result, null, 2));
@@ -266,7 +266,7 @@ program.command("retweet").description("Retweet a tweet").argument("<tweetId>",
266
266
  });
267
267
  program.command("follow").description("Follow a user").argument("<handle>", "User handle or ID").action(async (handle) => {
268
268
  try {
269
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
269
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
270
270
  const client = await getXClient();
271
271
  const result = await client.followUser(handle);
272
272
  console.log(JSON.stringify(result, null, 2));
@@ -277,7 +277,7 @@ program.command("follow").description("Follow a user").argument("<handle>", "Use
277
277
  });
278
278
  program.command("unfollow").description("Unfollow a user").argument("<handle>", "User handle or ID").action(async (handle) => {
279
279
  try {
280
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
280
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
281
281
  const client = await getXClient();
282
282
  const result = await client.unfollowUser(handle);
283
283
  console.log(JSON.stringify(result, null, 2));
@@ -288,7 +288,7 @@ program.command("unfollow").description("Unfollow a user").argument("<handle>",
288
288
  });
289
289
  program.command("timeline").description("Read home timeline").option("-c, --count <n>", "Number of tweets", "20").action(async (opts) => {
290
290
  try {
291
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
291
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
292
292
  const client = await getXClient();
293
293
  const result = await client.getTimeline({ count: parseInt(opts.count) });
294
294
  console.log(JSON.stringify(result, null, 2));
@@ -299,7 +299,7 @@ program.command("timeline").description("Read home timeline").option("-c, --coun
299
299
  });
300
300
  program.command("mentions").description("Read mentions").option("-c, --count <n>", "Number of mentions", "20").action(async (opts) => {
301
301
  try {
302
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
302
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
303
303
  const client = await getXClient();
304
304
  const result = await client.getMentions({ count: parseInt(opts.count) });
305
305
  console.log(JSON.stringify(result, null, 2));
@@ -310,7 +310,7 @@ program.command("mentions").description("Read mentions").option("-c, --count <n>
310
310
  });
311
311
  program.command("search").description("Search for tweets").argument("<query>", "Search query").option("-c, --count <n>", "Number of results", "20").action(async (query, opts) => {
312
312
  try {
313
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
313
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
314
314
  const client = await getXClient();
315
315
  const result = await client.searchTweets(query, { count: parseInt(opts.count) });
316
316
  console.log(JSON.stringify(result, null, 2));
@@ -321,7 +321,7 @@ program.command("search").description("Search for tweets").argument("<query>", "
321
321
  });
322
322
  program.command("profile").description("Get a user's X profile").argument("<handle>", "X handle (without @)").action(async (handle) => {
323
323
  try {
324
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
324
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
325
325
  const client = await getXClient();
326
326
  const result = await client.getProfile(handle);
327
327
  console.log(JSON.stringify(result, null, 2));
@@ -343,7 +343,7 @@ program.command("credits").description("Check remaining posting credits").action
343
343
  });
344
344
  program.command("memory").description("Read memory (interactions, learnings, relationships)").argument("<type>", "interactions | learnings | relationships").option("-d, --date <date>", "For interactions: specific date (YYYY-MM-DD)").option("-c, --count <n>", "For interactions: count", "20").action(async (type, opts) => {
345
345
  try {
346
- const { getRecentInteractions, getInteractions, loadLearnings, loadRelationships } = await import("./memory-DTSLVSQG.js");
346
+ const { getRecentInteractions, getInteractions, loadLearnings, loadRelationships } = await import("./memory-G4DNIGLT.js");
347
347
  let data;
348
348
  switch (type) {
349
349
  case "interactions":
@@ -367,7 +367,7 @@ program.command("memory").description("Read memory (interactions, learnings, rel
367
367
  });
368
368
  program.command("learn").description("Store a learning").argument("<content>", "What you learned").option("-t, --tags <tags...>", "Tags for categorization").action(async (content, opts) => {
369
369
  try {
370
- const { addLearning } = await import("./memory-DTSLVSQG.js");
370
+ const { addLearning } = await import("./memory-G4DNIGLT.js");
371
371
  addLearning(content, "agent", opts.tags ?? []);
372
372
  console.log(JSON.stringify({ success: true }));
373
373
  } catch (error) {
@@ -377,7 +377,7 @@ program.command("learn").description("Store a learning").argument("<content>", "
377
377
  });
378
378
  program.command("note").description("Add a relationship note about someone").argument("<handle>", "Their X handle").argument("<content>", "Your note").action(async (handle, content) => {
379
379
  try {
380
- const { updateRelationship } = await import("./memory-DTSLVSQG.js");
380
+ const { updateRelationship } = await import("./memory-G4DNIGLT.js");
381
381
  updateRelationship(handle, { handle, notes: [content] });
382
382
  console.log(JSON.stringify({ success: true, handle }));
383
383
  } catch (error) {
@@ -387,7 +387,7 @@ program.command("note").description("Add a relationship note about someone").arg
387
387
  });
388
388
  program.command("schedule").description("Queue a post for later").argument("<content>", "Tweet content").option("--at <datetime>", "ISO datetime to post at").action(async (content, opts) => {
389
389
  try {
390
- const { addToQueue } = await import("./queue-QCGNDHH2.js");
390
+ const { addToQueue } = await import("./queue-YPBUUP22.js");
391
391
  const entry = addToQueue(content, opts.at);
392
392
  console.log(JSON.stringify({ success: true, id: entry.id, scheduledFor: entry.scheduledFor }));
393
393
  } catch (error) {
@@ -397,7 +397,7 @@ program.command("schedule").description("Queue a post for later").argument("<con
397
397
  });
398
398
  program.command("flush").description("Post all queued items whose time has come").action(async () => {
399
399
  try {
400
- const { flushQueue } = await import("./queue-QCGNDHH2.js");
400
+ const { flushQueue } = await import("./queue-YPBUUP22.js");
401
401
  const results = await flushQueue();
402
402
  console.log(JSON.stringify(results, null, 2));
403
403
  } catch (error) {
@@ -406,13 +406,13 @@ program.command("flush").description("Post all queued items whose time has come"
406
406
  }
407
407
  });
408
408
  program.command("queue").description("Show scheduled posts").action(async () => {
409
- const { showQueue } = await import("./queue-QCGNDHH2.js");
409
+ const { showQueue } = await import("./queue-YPBUUP22.js");
410
410
  showQueue();
411
411
  });
412
412
  var colony = program.command("colony").description("Colony commands");
413
413
  colony.command("checkin").description("Check into The Colony \u2014 sync memory, discover Spores").option("-m, --message <msg>", "Optional message to post").action(async (opts) => {
414
414
  try {
415
- const { colonyCheckin } = await import("./colony-UGVYALOS.js");
415
+ const { colonyCheckin } = await import("./colony-7GZ2ODF2.js");
416
416
  const result = await colonyCheckin(opts.message);
417
417
  console.log(JSON.stringify(result, null, 2));
418
418
  } catch (error) {
@@ -431,7 +431,7 @@ colony.command("memory").description("Read the Colony's shared memory").action(a
431
431
  });
432
432
  colony.command("plans").description("Get all active Colony plans").action(async () => {
433
433
  try {
434
- const { getActivePlans } = await import("./colony-UGVYALOS.js");
434
+ const { getActivePlans } = await import("./colony-7GZ2ODF2.js");
435
435
  const plans = getActivePlans();
436
436
  console.log(plans.length > 0 ? JSON.stringify(plans, null, 2) : JSON.stringify({ message: "No active plans. Propose one!" }));
437
437
  } catch (error) {
@@ -441,7 +441,7 @@ colony.command("plans").description("Get all active Colony plans").action(async
441
441
  });
442
442
  colony.command("propose").description("Propose a coordinated plan").argument("<description>", "What's the plan?").action(async (description) => {
443
443
  try {
444
- const { proposePlan } = await import("./colony-UGVYALOS.js");
444
+ const { proposePlan } = await import("./colony-7GZ2ODF2.js");
445
445
  const result = await proposePlan(description);
446
446
  console.log(JSON.stringify(result, null, 2));
447
447
  } catch (error) {
@@ -451,7 +451,7 @@ colony.command("propose").description("Propose a coordinated plan").argument("<d
451
451
  });
452
452
  colony.command("join").description("Join an active plan").argument("<planId>", "Plan ID").action(async (planId) => {
453
453
  try {
454
- const { joinPlan } = await import("./colony-UGVYALOS.js");
454
+ const { joinPlan } = await import("./colony-7GZ2ODF2.js");
455
455
  const result = await joinPlan(planId);
456
456
  console.log(JSON.stringify(result, null, 2));
457
457
  } catch (error) {
@@ -461,7 +461,7 @@ colony.command("join").description("Join an active plan").argument("<planId>", "
461
461
  });
462
462
  colony.command("post-status").description("Post a status update to the Colony").argument("<status>", "Your status").action(async (status) => {
463
463
  try {
464
- const { postStatus } = await import("./colony-UGVYALOS.js");
464
+ const { postStatus } = await import("./colony-7GZ2ODF2.js");
465
465
  const result = await postStatus(status);
466
466
  console.log(JSON.stringify(result, null, 2));
467
467
  } catch (error) {
@@ -471,7 +471,7 @@ colony.command("post-status").description("Post a status update to the Colony").
471
471
  });
472
472
  colony.command("activity").description("Get today's Colony activity").action(async () => {
473
473
  try {
474
- const { getTodaysActivity } = await import("./colony-UGVYALOS.js");
474
+ const { getTodaysActivity } = await import("./colony-7GZ2ODF2.js");
475
475
  const activity = getTodaysActivity();
476
476
  console.log(activity.length > 0 ? JSON.stringify(activity, null, 2) : JSON.stringify({ message: "No Colony activity today yet." }));
477
477
  } catch (error) {
@@ -501,11 +501,11 @@ program.command("start").description("Start the autonomous Spora agent").option(
501
501
  }
502
502
  console.log(chalk.cyan(BANNER));
503
503
  console.log(chalk.bold("Starting Spora agent...\n"));
504
- const { startHeartbeatLoop } = await import("./heartbeat-WJJSGUAQ.js");
504
+ const { startHeartbeatLoop } = await import("./heartbeat-CUM7FIHS.js");
505
505
  await startHeartbeatLoop();
506
506
  });
507
507
  program.command("stop").description("Stop the running Spora agent").action(async () => {
508
- const { getRunningPid, requestStop } = await import("./heartbeat-WJJSGUAQ.js");
508
+ const { getRunningPid, requestStop } = await import("./heartbeat-CUM7FIHS.js");
509
509
  const pid = getRunningPid();
510
510
  if (!pid) {
511
511
  console.log(JSON.stringify({ message: "Spora agent is not running." }));
@@ -587,7 +587,7 @@ llm.command("test").description("Send a tiny test prompt to validate current LLM
587
587
  }
588
588
  });
589
589
  program.command("agent-status").description("Check if the Spora agent is running").action(async () => {
590
- const { getRunningPid } = await import("./heartbeat-WJJSGUAQ.js");
590
+ const { getRunningPid } = await import("./heartbeat-CUM7FIHS.js");
591
591
  const pid = getRunningPid();
592
592
  const { hasLLMKey } = await import("./llm-IJBRQ7O2.js");
593
593
  console.log(JSON.stringify({
@@ -633,7 +633,7 @@ program.command("doctor").description("Run diagnostics for X API, LLM provider,
633
633
  checks.push({ check: "llm_call", ok: false, detail: error.message });
634
634
  }
635
635
  try {
636
- const { getXClient } = await import("./x-client-S2LUVEKV.js");
636
+ const { getXClient } = await import("./x-client-2HFEHHVE.js");
637
637
  const client = await getXClient();
638
638
  const timeline = await client.getTimeline({ count: 3 });
639
639
  checks.push({ check: "x_api_timeline", ok: true, detail: `timeline_count=${timeline.length}` });
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  rateLimiter,
3
3
  trackPost
4
- } from "./chunk-CP6JWCLY.js";
4
+ } from "./chunk-ER6TILYZ.js";
5
5
  import {
6
6
  identityExists,
7
7
  loadIdentity
@@ -15,7 +15,7 @@ import {
15
15
  import "./chunk-QYFNAGNI.js";
16
16
  import {
17
17
  logInteraction
18
- } from "./chunk-BBXHECZ5.js";
18
+ } from "./chunk-6WBIVXOY.js";
19
19
  import "./chunk-ZWKTKWS6.js";
20
20
 
21
21
  // src/x-client/api/client.ts
@@ -450,4 +450,4 @@ var XApiClient = class {
450
450
  export {
451
451
  XApiClient
452
452
  };
453
- //# sourceMappingURL=client-AR5ZD6S4.js.map
453
+ //# sourceMappingURL=client-3APKWQ6O.js.map
@@ -10,7 +10,7 @@ import {
10
10
  } from "./chunk-D47OFTEK.js";
11
11
  import {
12
12
  getXClient
13
- } from "./chunk-5R4AJZHN.js";
13
+ } from "./chunk-TKGB5LIN.js";
14
14
  import {
15
15
  loadIdentity
16
16
  } from "./chunk-IULO3GRE.js";
@@ -226,4 +226,4 @@ export {
226
226
  postStatus,
227
227
  proposePlan
228
228
  };
229
- //# sourceMappingURL=colony-UGVYALOS.js.map
229
+ //# sourceMappingURL=colony-7GZ2ODF2.js.map