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
+83 -45
View File
@@ -15,6 +15,7 @@ import (
"github.com/charmbracelet/lipgloss"
claudeapi "coremarketing/internal/api/claude"
"coremarketing/internal/api/twitter"
"coremarketing/internal/config"
"coremarketing/internal/fetcher"
"coremarketing/internal/models"
@@ -58,9 +59,9 @@ type batchGenerateMsg struct {
}
type batchPostedMsg struct {
postedIDs []string
urls []string
errors []string
posted map[string][]string // post ID → platforms newly published to
urls []string
errors []string
}
type errMsg struct {
@@ -102,8 +103,8 @@ type selectableDelegate struct {
const maxBodyLines = 4
func (d selectableDelegate) Height() int { return 2 + maxBodyLines + 1 }
func (d selectableDelegate) Spacing() int { return 0 }
func (d selectableDelegate) Height() int { return 2 + maxBodyLines + 1 }
func (d selectableDelegate) Spacing() int { return 0 }
func (d selectableDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d selectableDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
@@ -181,11 +182,12 @@ func (i genPostItem) Title() string { return i.AppName }
func (i genPostItem) Description() string { return i.Content }
type genPostDelegate struct {
selected map[string]bool
selected map[string]bool
maxLength int
}
func (d genPostDelegate) Height() int { return 4 }
func (d genPostDelegate) Spacing() int { return 0 }
func (d genPostDelegate) Spacing() int { return 0 }
func (d genPostDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d genPostDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
@@ -203,7 +205,13 @@ func (d genPostDelegate) Render(w io.Writer, m list.Model, index int, item list.
line1 := fmt.Sprintf("%s %s · %s", check, pi.AppName, pi.GeneratedAt.Format("Jan 2, 3:04pm"))
line2 := " " + truncate(pi.Content, 80)
line3 := " " + fmt.Sprintf("%d chars", utf8.RuneCountInString(pi.Content))
line3 := " " + fmt.Sprintf("%d / %d chars on X", twitter.TweetLength(pi.Content, pi.SourceURL), d.maxLength)
if pi.SourceURL != "" {
line3 += " (incl. link)"
}
if len(pi.PostedTo) > 0 {
line3 += " · already posted to " + strings.Join(pi.PostedTo, ", ")
}
var s1, s2, s3 lipgloss.Style
if isFocused {
@@ -246,8 +254,8 @@ type Model struct {
dismissed *store.DismissedStore
fetch *fetcher.Fetcher
claude *claudeapi.Client
poster postingClient
liPoster linkedinPostingClient
poster postingClient
liPoster linkedinPostingClient
appList list.Model
contentList list.Model
@@ -261,8 +269,8 @@ type Model struct {
spinner spinner.Model
textarea textarea.Model
selectedApp *models.Application
fetchResult fetcher.Result
selectedApp *models.Application
fetchResult fetcher.Result
prefetchResults map[string]fetcher.Result
pendingItemIDs map[string]bool
@@ -384,7 +392,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.textarea.SetValue(msg.post)
focusCmd := m.textarea.Focus()
m.appState.SetLastChecked(m.selectedApp.Name, time.Now())
_ = m.appState.Save()
if err := m.appState.Save(); err != nil {
m.errorMsg = "Could not save state: " + err.Error()
}
removeUsedItems(&m)
m.view = viewPostPreview
return m, focusCmd
@@ -393,11 +403,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
for i, content := range msg.contents {
m.postStore.Add(m.selectedApp.Name, content, msg.sourceURLs[i])
}
_ = m.postStore.Save()
m.appState.SetLastChecked(m.selectedApp.Name, time.Now())
_ = m.appState.Save()
removeUsedItems(&m)
m.statusMsg = successStyle.Render(fmt.Sprintf("✓ %d posts saved to queue — press p to review", len(msg.contents)))
if err := m.postStore.Save(); err != nil {
m.statusMsg = ""
m.errorMsg = "Could not save generated posts: " + err.Error()
} else if err := m.appState.Save(); err != nil {
m.errorMsg = "Could not save state: " + err.Error()
}
m.view = viewHome
return m, nil
@@ -407,15 +421,27 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
case batchPostedMsg:
// Remove successfully posted items from the store
postedSet := map[string]bool{}
for _, id := range msg.postedIDs {
postedSet[id] = true
// Record per-platform success, then drop posts that are now published
// everywhere posting is configured. Partially posted ones stay queued
// so the failed platform can be retried without double-posting.
for id, platforms := range msg.posted {
for _, p := range platforms {
m.postStore.MarkPosted(id, p)
}
}
m.postStore.Remove(postedSet)
_ = m.postStore.Save()
done := map[string]bool{}
for _, p := range m.postStore.Posts {
if (m.poster == nil || p.HasPosted(store.PlatformX)) &&
(m.liPoster == nil || p.HasPosted(store.PlatformLinkedIn)) {
done[p.ID] = true
}
}
m.postStore.Remove(done)
m.resultURLs = msg.urls
m.resultErrors = msg.errors
if err := m.postStore.Save(); err != nil {
m.resultErrors = append(m.resultErrors, "Could not save post queue (posted items may reappear): "+err.Error())
}
m.view = viewResult
case errMsg:
@@ -706,6 +732,12 @@ func (m Model) renderHome() string {
if n := len(m.postStore.Posts); n > 0 {
sb.WriteString(" " + successStyle.Render(fmt.Sprintf("● %d post(s) ready to publish", n)) + "\n")
}
if m.errorMsg != "" {
sb.WriteString(" " + errorStyle.Render(m.errorMsg) + "\n")
}
if m.statusMsg != "" {
sb.WriteString(" " + m.statusMsg + "\n")
}
sb.WriteString(m.appList.View())
sb.WriteString("\n")
@@ -774,7 +806,7 @@ func (m Model) renderPostPreview() string {
sb.WriteString("\n")
text := m.textarea.Value()
charCount := utf8.RuneCountInString(text)
charCount := twitter.TweetLength(text, m.pendingSourceURL)
limit := m.cfg.Posting.MaxLength
var countStyle lipgloss.Style
@@ -788,6 +820,9 @@ func (m Model) renderPostPreview() string {
}
countLine := countStyle.Render(fmt.Sprintf(" %d / %d characters", charCount, limit))
if m.pendingSourceURL != "" {
countLine += countStyle.Render(fmt.Sprintf(" (incl. %d for the source link)", twitter.URLReserve(m.pendingSourceURL)))
}
if charCount > limit {
countLine += " " + errorStyle.Render(fmt.Sprintf("(%d over!)", charCount-limit))
}
@@ -881,7 +916,7 @@ func (m Model) buildGenList() list.Model {
for i, p := range m.postStore.Posts {
items[i] = genPostItem{p}
}
m.genDelegate = genPostDelegate{selected: m.genSelected}
m.genDelegate = genPostDelegate{selected: m.genSelected, maxLength: m.cfg.Posting.MaxLength}
l := list.New(items, m.genDelegate, m.width-4, m.height-10)
l.Title = "Generated Posts [Space=select Ctrl+P=post d=delete]"
l.SetShowStatusBar(false)
@@ -999,10 +1034,11 @@ func (m Model) doFetch(app models.Application, since time.Time) tea.Cmd {
func (m Model) doGenerate(items []models.ContentItem) tea.Cmd {
app := m.selectedApp
cl := m.claude
limit := m.cfg.Posting.MaxLength - twitter.URLReserve(firstSourceURL(items))
return func() tea.Msg {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
post, err := cl.GeneratePost(ctx, app, items)
post, err := cl.GeneratePost(ctx, app, items, limit)
if err != nil {
return errMsg{err: err, from: viewContentBrowser}
}
@@ -1013,12 +1049,14 @@ func (m Model) doGenerate(items []models.ContentItem) tea.Cmd {
func (m Model) doBatchGenerate(items []models.ContentItem) tea.Cmd {
app := m.selectedApp
cl := m.claude
maxLength := m.cfg.Posting.MaxLength
return func() tea.Msg {
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
var contents, sourceURLs []string
for _, item := range items {
post, err := cl.GeneratePost(ctx, app, []models.ContentItem{item})
limit := maxLength - twitter.URLReserve(item.URL)
post, err := cl.GeneratePost(ctx, app, []models.ContentItem{item}, limit)
if err != nil {
return errMsg{err: err, from: viewContentBrowser}
}
@@ -1032,41 +1070,41 @@ func (m Model) doBatchGenerate(items []models.ContentItem) tea.Cmd {
func (m Model) doPostBatch(posts []store.GeneratedPost) tea.Cmd {
poster := m.poster
liPoster := m.liPoster
maxLength := m.cfg.Posting.MaxLength
return func() tea.Msg {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
var postedIDs, urls, errs []string
posted := map[string][]string{}
var urls, errs []string
for _, p := range posts {
anySuccess := false
if poster != nil {
text := p.Content
if p.SourceURL != "" {
text += "\n\n" + p.SourceURL
}
u, err := poster.PostTweet(ctx, text)
if err != nil {
errs = append(errs, fmt.Sprintf("%s (X): %v", p.AppName, err))
if poster != nil && !p.HasPosted(store.PlatformX) {
if n := twitter.TweetLength(p.Content, p.SourceURL); n > maxLength {
errs = append(errs, fmt.Sprintf("%s (X): post is %d characters including the link, limit is %d — delete it and regenerate a shorter one", p.AppName, n, maxLength))
} else {
urls = append(urls, u)
anySuccess = true
text := p.Content
if p.SourceURL != "" {
text += "\n\n" + p.SourceURL
}
u, err := poster.PostTweet(ctx, text)
if err != nil {
errs = append(errs, fmt.Sprintf("%s (X): %v", p.AppName, err))
} else {
urls = append(urls, u)
posted[p.ID] = append(posted[p.ID], store.PlatformX)
}
}
}
if liPoster != nil {
if liPoster != nil && !p.HasPosted(store.PlatformLinkedIn) {
u, err := liPoster.PostUpdate(ctx, p.Content, p.SourceURL)
if err != nil {
errs = append(errs, fmt.Sprintf("%s (LinkedIn): %v", p.AppName, err))
} else {
urls = append(urls, u)
anySuccess = true
posted[p.ID] = append(posted[p.ID], store.PlatformLinkedIn)
}
}
if anySuccess {
postedIDs = append(postedIDs, p.ID)
}
}
return batchPostedMsg{postedIDs: postedIDs, urls: urls, errors: errs}
return batchPostedMsg{posted: posted, urls: urls, errors: errs}
}
}
+8 -8
View File
@@ -3,13 +3,13 @@ package ui
import "github.com/charmbracelet/lipgloss"
var (
colorPurple = lipgloss.Color("135")
colorGreen = lipgloss.Color("82")
colorRed = lipgloss.Color("196")
colorYellow = lipgloss.Color("226")
colorGray = lipgloss.Color("240")
colorFaint = lipgloss.Color("238")
colorWhite = lipgloss.Color("255")
colorPurple = lipgloss.Color("135")
colorGreen = lipgloss.Color("82")
colorRed = lipgloss.Color("196")
colorYellow = lipgloss.Color("226")
colorGray = lipgloss.Color("240")
colorFaint = lipgloss.Color("238")
colorWhite = lipgloss.Color("255")
titleStyle = lipgloss.NewStyle().
Bold(true).
@@ -71,7 +71,7 @@ var (
warningStyle = lipgloss.NewStyle().
Foreground(colorYellow)
charCountOK = lipgloss.NewStyle().Foreground(colorGreen)
charCountOK = lipgloss.NewStyle().Foreground(colorGreen)
charCountWarn = lipgloss.NewStyle().Foreground(colorYellow)
charCountOver = lipgloss.NewStyle().Foreground(colorRed).Bold(true)