Skip to content

feat(schedule): record delivered Telegram results into the chat session#35

Merged
jkyberneees merged 2 commits into
mainfrom
feat/schedule-session-writeback
Jun 12, 2026
Merged

feat(schedule): record delivered Telegram results into the chat session#35
jkyberneees merged 2 commits into
mainfrom
feat/schedule-session-writeback

Conversation

@jkyberneees

Copy link
Copy Markdown
Contributor

Summary

Scheduled tasks ran fully headless and their delivered Telegram message was never written back to the chat's conversation, so a follow-up turn had amnesia about what the schedule just posted — "what did that scheduled task find?" hit an agent that never saw its own output.

This adds Option B: after a successful Telegram delivery, append the scheduled task + its result into the target chat's session as a labeled user turn (⏰ [scheduled task "…" ran]) + the assistant result. The scheduled run itself stays isolated and deterministic — only its output is recorded (Option A — feeding the live chat backlog into the run — was deliberately rejected as non-deterministic and a concurrency footgun).

Design decisions

  • Concurrency-safe — the write-back is serialized through the existing per-chat mutex (getChatMutex), so it can't interleave with a live interactive turn's read-modify-write of the same session, or with a concurrent scheduled delivery to the same chat.
  • Attaches only to an EXISTING session — a notification-only chat (never used interactively) is not given a session, avoiding an ever-growing transcript of scheduled posts that would bloat every future prompt. Continuity kicks in only where there's a real conversation to continue.
  • Best-effort — a write-back failure is logged, never fails the run (the message was already sent) and never triggers redelivery.
  • Scoped to the Telegram bot's embedded scheduler (which owns the SessionManager). The CLI schedule daemon, stdout, and log paths pass nil and are a safe no-op.

Tests (all under -race)

  • Records the labeled user + assistant turns into an existing session, and confirms the message was actually sent.
  • A no-session chat is not created (notification-only stays clean).
  • Empty result isn't recorded.
  • Nil SessionManager (daemon path) is a safe no-op.

Full suite green under go test ./... -race; clean go vet.

🤖 Generated with Claude Code

Scheduled tasks ran fully headless and their delivered message was never
written back to the chat's conversation, so a follow-up turn had amnesia
about what the schedule just posted ("what did that scheduled task find?"
hit an agent that never saw its own output).

After a successful Telegram delivery, append the scheduled task + its result
to the target chat's session as a labeled user turn + the assistant result.
The scheduled run itself stays isolated (deterministic) — only its output is
recorded.

Design choices:
- Write-back is serialized through the existing per-chat mutex
  (getChatMutex), so it can't interleave with a live interactive turn's
  read-modify-write or with a concurrent delivery to the same chat.
- It attaches only to an EXISTING session — a notification-only chat that's
  never been used interactively is not given an ever-growing transcript.
- Best-effort: a write-back failure is logged, never fails the run (the
  message was already sent) and never triggers redelivery.
- Wired only on the Telegram bot's embedded scheduler (which owns the
  SessionManager); the CLI daemon path passes nil and is a safe no-op.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 12, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
odek bc6589c Commit Preview URL

Branch Preview URL
Jun 12 2026, 04:49 AM

…g session

vprotocol axis 2.1: a chat session can already end on a bare user message —
a turn cancelled before the agent replied (telegram.go save-on-cancel) or a
context-injection command. The write-back appended its own user turn there,
producing two consecutive user messages, which Anthropic rejects with a 400
on the next interactive turn. When the session ends on a user message, fold
the scheduled label into a single assistant message instead; either way the
session ends on an assistant turn with no two same-role messages in a row.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@jkyberneees jkyberneees merged commit 3f10ddb into main Jun 12, 2026
7 checks passed
@jkyberneees jkyberneees deleted the feat/schedule-session-writeback branch June 12, 2026 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant