Stop committing credentials; fix posting length, partial posts, and data safety

- Untrack config.yaml (now gitignored) and add config.example.yaml with
  blank credentials; install.sh copies the example and sets mode 600
- Reserve room for the appended source link when asking Claude for a
  post, count length the way X does, and refuse over-length tweets
  before posting
- Return https://x.com/i/status/<id> for posted tweets
- Track which platforms each queued post reached so a partial failure
  can be retried without double-posting
- Write config, state, posts and dismissed files atomically with 0600
- Keep refreshed Twitter tokens in memory if saving them fails, and log
  the failure instead of writing to the hidden stderr
- Exit with a clear message on corrupt data files instead of panicking
- Surface save errors in the UI; show status/error messages on home
- Add tests for X length counting, the post store, and token updates

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 06:14:40 -07:00
co-authored by Claude Opus 5.5
parent c48ee22f99
commit 4d304cd65b
18 changed files with 391 additions and 96 deletions
+16 -16
View File
@@ -32,27 +32,27 @@ Your posts should:
Output ONLY the post text. No explanations, no alternatives — just the post.`
type Client struct {
client *anthropic.Client
model string
maxLength int
logFile string
client *anthropic.Client
model string
logFile string
}
func New(apiKey, model, stateDir string, maxLength int) *Client {
func New(apiKey, model, stateDir string) *Client {
c := anthropic.NewClient(option.WithAPIKey(apiKey))
return &Client{
client: c,
model: model,
maxLength: maxLength,
logFile: stateDir + "/coremarketing.log",
client: c,
model: model,
logFile: stateDir + "/coremarketing.log",
}
}
// GeneratePost creates a post in the voice of a product manager based on selected content.
func (c *Client) GeneratePost(ctx context.Context, app *models.Application, items []models.ContentItem) (string, error) {
// GeneratePost creates a post in the voice of a product manager based on
// selected content. maxLength is the space available for the post text itself,
// excluding anything appended at posting time (such as the source URL).
func (c *Client) GeneratePost(ctx context.Context, app *models.Application, items []models.ContentItem, maxLength int) (string, error) {
// Combine system instructions with the user prompt in a single user message.
// This avoids SDK version ambiguity around the System parameter content block type.
fullPrompt := systemPrompt + "\n\n---\n\n" + c.buildUserPrompt(app, items)
fullPrompt := systemPrompt + "\n\n---\n\n" + c.buildUserPrompt(app, items, maxLength)
msg, err := c.client.Messages.New(ctx, anthropic.MessageNewParams{
Model: anthropic.F(c.model),
@@ -76,11 +76,11 @@ func (c *Client) GeneratePost(ctx context.Context, app *models.Application, item
return "", fmt.Errorf("no text content in claude response (stop_reason=%s)", msg.StopReason)
}
func (c *Client) buildUserPrompt(app *models.Application, items []models.ContentItem) string {
func (c *Client) buildUserPrompt(app *models.Application, items []models.ContentItem, maxLength int) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("Application: %s\n", app.Name))
sb.WriteString(fmt.Sprintf("Description: %s\n\n", app.Description))
sb.WriteString(fmt.Sprintf("Character limit: %d\n\n", c.maxLength))
sb.WriteString(fmt.Sprintf("Character limit: %d\n\n", maxLength))
sb.WriteString("Selected content from social media and community:\n\n")
for i, item := range items {
@@ -96,14 +96,14 @@ func (c *Client) buildUserPrompt(app *models.Application, items []models.Content
sb.WriteString(fmt.Sprintf(
"Based on this content, write ONE X.com post (max %d characters including hashtags) in the voice of a product manager helping users better use %s.",
c.maxLength, app.Name,
maxLength, app.Name,
))
return sb.String()
}
func (c *Client) log(format string, args ...any) {
f, err := os.OpenFile(c.logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(c.logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return
}
+43 -2
View File
@@ -21,7 +21,7 @@ const apiBase = "https://api.twitter.com/2"
type TokenRefresher func(ctx context.Context) (accessToken, refreshToken string, err error)
type Client struct {
bearerToken string
bearerToken string
oauth2AccessToken string
maxResults int
http *http.Client
@@ -141,7 +141,7 @@ func (c *Client) postTweet(ctx context.Context, text string) (tweetURL string, u
if err := json.Unmarshal(raw, &out); err != nil {
return "", false, err
}
return "https://x.com/" + out.Data.ID, false, nil
return "https://x.com/i/status/" + out.Data.ID, false, nil
}
// BuildSearchQuery constructs an X search query from hashtags.
@@ -280,3 +280,44 @@ func clamp(v, min, max int) int {
}
return v
}
// URLLength is the fixed weight X assigns to any link (t.co wrapping).
const URLLength = 23
// TweetLength returns the length X will count for a post consisting of
// content followed by "\n\n" and sourceURL (when non-empty).
func TweetLength(content, sourceURL string) int {
n := weightedLength(content)
if sourceURL != "" {
n += 2 + URLLength
}
return n
}
// URLReserve is how many characters to hold back from the post limit so the
// source URL appended at posting time still fits.
func URLReserve(sourceURL string) int {
if sourceURL == "" {
return 0
}
return 2 + URLLength
}
// weightedLength approximates X's character counting: Latin and common
// punctuation count as 1, everything else (CJK, emoji, …) counts as 2. It may
// overcount multi-codepoint emoji, which errs on the side of fitting.
func weightedLength(s string) int {
n := 0
for _, r := range s {
switch {
case r <= 0x10FF,
r >= 0x2000 && r <= 0x200D,
r >= 0x2010 && r <= 0x201F,
r >= 0x2032 && r <= 0x2037:
n++
default:
n += 2
}
}
return n
}
+32
View File
@@ -0,0 +1,32 @@
package twitter
import (
"strings"
"testing"
)
func TestTweetLength(t *testing.T) {
cases := []struct {
content, url string
want int
}{
{"hello", "", 5},
{"hello", "https://example.com/a/very/long/path/that/x/shortens", 5 + 2 + 23},
{"café — “quoted”", "", 15},
{"🚀", "", 2},
{"日本", "", 4},
}
for _, c := range cases {
if got := TweetLength(c.content, c.url); got != c.want {
t.Errorf("TweetLength(%q, %q) = %d, want %d", c.content, c.url, got, c.want)
}
}
}
func TestReservedLimitFits(t *testing.T) {
url := "https://x.com/someone/status/123"
limit := 280 - URLReserve(url)
if got := TweetLength(strings.Repeat("a", limit), url); got != 280 {
t.Errorf("post at reserved limit counts as %d, want 280", got)
}
}