Skip to content

feat: refactor the chatd worker - #26109

Merged
hugodutka merged 1 commit into
hugodutka/chatd-refactorfrom
hugodutka/chatd-refactor-3
Jun 11, 2026
Merged

hugodutka merged 1 commit into
hugodutka/chatd-refactorfrom
hugodutka/chatd-refactor-3

Conversation

@hugodutka

@hugodutka hugodutka commented Jun 5, 2026 •

Copy link
Copy Markdown
Collaborator

PR 3 of the chatd refactor. Refactors the chatd worker to use the chatd state machine.

The CI doesn't pass on purpose. Only the tip of the chatd refactor PR stack will pass CI.

Previous PR in the stack: #25923

@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from d01e533 to 2c55f38 Compare June 5, 2026 18:05
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-3 branch from 0e10473 to 0840442 Compare June 5, 2026 18:05
@hugodutka hugodutka changed the title pr 3 feat: refactor the chatd worker Jun 8, 2026
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-3 branch from 0840442 to 965ee76 Compare June 8, 2026 13:07
@hugodutka
hugodutka marked this pull request as ready for review June 8, 2026 13:07
@hugodutka
hugodutka requested review from johnstcn and mafredri June 8, 2026 13:07

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only made it to messagepartbuffer so far, but I'm noticing a lot of added code is missing comments making it hard to review. I'll continue reviewing but will be harder to be thorough in these parts.

// one.
func New(ctx context.Context, logger slog.Logger, db database.Store, vals *codersdk.DeploymentValues, reg prometheus.Registerer, auditor *atomic.Pointer[audit.Auditor], opts ...Option) io.Closer {
// The auditor pointer is accepted for compatibility with other background
// services. Dbpurge does not emit audit logs directly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is some LLM-based reasoning right here, let's remove it if it's no longer used.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26344

var purgedChats, purgedChatFiles, purgedChatDebugRuns int64
if chatRetentionConfigErr == nil {
purgedChats, purgedChatFiles, archivedChats, err = i.purgeChatsInTx(ctx, tx, start, chatRetentionDays, chatAutoArchiveDays)
if chatRetentionErr == nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit worried about the cleverness of this code. The distance between getting the error and using it is too large and unusual for Go code, it'd be better to set a flag rather than re-use the err here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26344


// purgeChatsInTx MUST BE CALLED WITH A TRANSACTION
func (i *instance) purgeChatsInTx(ctx context.Context, tx database.Store, start time.Time, chatRetentionDays, chatAutoArchiveDays int32) (purgedChats, purgedChatFiles int64, archivedChats []database.AutoArchiveInactiveChatsRow, err error) {
func (*instance) purgeChatsInTx(ctx context.Context, tx database.Store, start time.Time, chatRetentionDays int32) (purgedChats, purgedChatFiles int64, err error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Side-note/future work: It would be nice to add support for database/Store to assert InTx and error out if not.)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// LaunchRunHeartbeat starts a goroutine that periodically touches an
// open run until done is closed or ctx is canceled.
func (s *Service) LaunchRunHeartbeat(ctx context.Context, runID uuid.UUID, chatID uuid.UUID, done <-chan struct{}) {
if s == nil || runID == uuid.Nil || chatID == uuid.Nil || done == nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to either be part of the method documentation or return an error, same for TouchRun.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26345

interval := s.heartbeatInterval()
ticker := s.clock.NewTicker(interval, "chatdebug", "run-heartbeat")
defer ticker.Stop()
resetTicker := func() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: maybeResetTicker.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26345

Comment on lines +192 to +194
if episode == nil || !episode.created {
return ErrEpisodeNotFound
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: helper method (e.g. getEpisode) that handles not-found check.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26508.

@@ -0,0 +1,492 @@
package messagepartbuffer

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this package could use a lot more documentation, also on the methods to clarify why the code is doing what it's doing. As-is, it's very hard to review for implementaiton matching intent.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26508.

Comment on lines +297 to +300
if !episode.created {
episode.created = true
close(episode.createdCh)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done in a few places, could be extracted to a method like episode.markCreated() or perhaps episode.finalize().

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26508.

episode := b.episodeLocked(key)
subscriber := &episodeSubscriber{
out: make(chan Part),
notifyCh: make(chan struct{}, 1),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment here explaing why it's buffered would be useful for readers. Perhaps also why the unbuffered channels are unbuffered/safe.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26508.

@@ -0,0 +1,377 @@
package messagepartbuffer_test

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect adding goleak to this package would surface some straggling goroutines. The logic seems to me like it's "eventually torn down". Worth considering if it should be added and whether or not code-paths should wait for teardown to happen.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred with high priority. Add goleak to all packages affected by the refactor.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26335

@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from 2c55f38 to 96fdf6f Compare June 9, 2026 12:03
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-3 branch from 965ee76 to 3bbda07 Compare June 9, 2026 12:03

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've glanced everything, but I still have a few more core-files to review in chatd with proper focus.

if d.created {
return d.contextLocked(ctx)
}
if d.disabled || d.finalized {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't d.created check above always bypass d.finalized, what are the ramifications of allowing that?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address now

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the d.finalized check before d.created.

for {
select {
case tick := <-ticker.C:
w.archiveOnce(ctx, dbtime.Time(tick).UTC())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: A slow archiveOnce or congested DB connection may create a constant archival loop. I'd suggest archiveOnce -> createTicker -> select -> archiveOnce + ticker.Reset(intval), but I leave it to the author. I don't think this will run often enough for this to become an issue.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26512.

return
}

archiveCutoff := dbtime.StartOfDay(start).Add(-time.Duration(autoArchiveDays) * 24 * time.Hour)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have docs on why we've e.g. selected UTC 00:00 - days to be the cutoff.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by #26512.


archived := make([]autoArchivedChat, 0, len(rows))
for _, row := range rows {
family, err := w.archiveCandidateSafely(ctx, row)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think archival = activity. Right now it reflects the last chat activity meaning an archived chat could almost immediately be wiped by dbpurge.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address now by adding a postgres trigger that automatically bumps updated_at.

@hugodutka hugodutka Jun 10, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: every chat state transition already bumps updated_at via the UpdateChatExecutionState query, archival included. Nothing needs to be done here now, but adding a Postgres trigger would be more robust. Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not going to address since UpdateChatExecutionState already handles it well enough.

Comment thread coderd/x/chatd/chatd.go
// truth for clients.
type generatedChatTitle struct {
mu sync.RWMutex
title string

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: An atomic value seems more appropriate here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

}

func (s *taskStarter) StartGeneration(ctx context.Context, input chatWorkerTaskStartInput) error {
if s.server == nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need all these runtime checks for required options?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #26517.

Comment thread coderd/x/chatd/generation.go Outdated
return s.finishGenerationError(ctx, machine, input, 0, err, generationAttemptNotRequired)
}
if prepared.Messages == nil {
prepared.Messages = messages

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this already handled in prepareGeneration? It either returns an err or it returns input.Messages.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address now.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the redundant code.

Comment thread coderd/x/chatd/runner.go Resolved
select {
case <-ticker.C:
if err := m.syncOnce(m.ctx); err != nil {
m.opts.Logger.Warn(m.ctx, "chatworker runner sync failed", slogError(err))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ctx is cancelled, this is noise.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

select {
case m.cleanupDoneCh <- key:
case <-m.ctx.Done():
m.mu.Lock()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution: Let's be extra careful about potential deadlocks between m.wg.Wait and m.mu.Lock.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred follow up: ensure this is corrected from a concurrency perspective

@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-3 branch from 3bbda07 to c48520d Compare June 9, 2026 16:32
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from 96fdf6f to b504e1d Compare June 9, 2026 16:32
Comment on lines +17 to +19
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = server.chatWorker.WaitIdle(ctx)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd caution against a hard-coded timeout here and instead accept a context.Context as a function parameter.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Comment thread coderd/x/chatd/tasks.go
Comment on lines +123 to +125
routeStateHint func(context.Context, runnerStateUpdate)
requestCleanup func(context.Context, runnerKey)
afterInterruptionOutcome func(context.Context, interruptionOutcome) error

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, non-blocking: see my comment on taskSideEffectRecorder. Extracting these to an interface would make the seam clearer and easier to mock ro spy on in tests.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Comment on lines +1035 to +1040
type taskSideEffectRecorder struct {
mu sync.Mutex
hints []runnerStateUpdate
cleanups []runnerKey
interrupts []interruptionOutcome
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, non-blocking: this is doing a reasonable job as a test spy, but it also highlights that taskStarter has a number of dependencies that cause side-effects. Extracting these to an interface would make the seam clearer, plus then you could potentially use gomock here to handle the test assertions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

@johnstcn johnstcn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed runner_manager.go

for _, rec := range targets {
select {
case rec.stateCh <- state:
case <-rec.done:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intended to also be an explicit return?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address now

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. We should continue here. Addressed.

Comment thread coderd/x/chatd/runner_manager.go Outdated
key := runnerKey{ChatID: req.ChatID, RunnerID: req.RunnerID}
m.mu.Lock()
if _, ok := m.runners[key]; ok {
m.opts.Logger.Warn(m.ctx, "invalid spawn request: chat runner already spawned", slog.F("key", key))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we either
a) log it at debug if it's just something that can happen and not to be concerned about
b) log it at error if it's a true logic error so that we can potentially catch it in tests

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log as error, this would be a true logic error

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

Comment thread coderd/x/chatd/runner_manager.go Outdated
return
}
if _, ok := m.cleaning[key]; ok {
m.opts.Logger.Warn(m.ctx, "invalid spawn request: chat runner in cleanup", slog.F("key", key))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above; we should either downgrade this to debug if it's not critical or raise it to error so we can catch it in tests

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Downgrade to debug now

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: this should be an error for the same reason as https://github.com/coder/coder/pull/26109/changes#r3387343995

select {
case <-ticker.C:
if err := m.heartbeatOnce(m.ctx); err != nil {
m.opts.Logger.Warn(m.ctx, "chatworker heartbeat failed", slogError(err))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Mathias' above comment is also applicable here -- skip logging this if it's a context canceled error.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred

}
// Detach from the request lifetime so title generation can finish
// even after the create response is written.
titleCtx := context.WithoutCancel(ctx)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still bound this with a separate timeout?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gone through the last files now, review done for this PR.

Comment thread coderd/x/chatd/options.go Outdated
defaultHeartbeatInterval = 9 * time.Second
defaultHeartbeatCleanupEvery = 30 * time.Second
defaultHeartbeatStaleSeconds = int32(30)
defaultArchiveInterval = 10 * time.Minute

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we run the archival loop every 10 minutes when we only apply the cutoff once every 24h? Don't know if we need to consider ticker going out-of-sync with clock but even once per hour should suffice.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address now

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to once per hour

Comment thread coderd/x/chatd/chatd.go
// result is committed and exits the local-tool commit as
// stale. Workspace context is persisted by the
// persist_workspace_context generation action in a later
// pass.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand this note exists in part because of the old implementation. But the way I read this is that it's easy to mis-use the implementation. Is that true or does the implementation guard against making a mistake like this? If it guards, is this comment even necessary? To further prevent the class of bug we should rename appendRootChatTools to appendRootChatToolsWithoutWorkspaceContext or something similar making the intent clear.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred. We should take a look at refactoring workspace context gathering in general.

Comment thread coderd/x/chatd/generation.go Outdated
Comment on lines +393 to +396
cleanup := prepared.Cleanup
if cleanup == nil {
cleanup = func() {}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should happen after prepared is created (before decision so that there's only ever one cleanup path, matches code below).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address now

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

Comment thread coderd/x/chatd/generation.go Outdated
decisionChat := locked
if prepared.Chat.ID != uuid.Nil {
decisionChat = prepared.Chat
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we're jugging too many variables here and leaving old-variables referenceable that are no longer of use. It might be better focusing on using one name like chat instead of locked at top, and overriding once locked is no longer useful, etc.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #26517.

Comment on lines +493 to +498
if errors.Is(err, sql.ErrNoRows) {
return errTaskExpectedExit
}
if err != nil {
return xerrors.Errorf("load chat: %w", err)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if errors.Is(err, sql.ErrNoRows) {
return errTaskExpectedExit
}
if err != nil {
return xerrors.Errorf("load chat: %w", err)
}
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return errTaskExpectedExit
}
return xerrors.Errorf("load chat: %w", err)
}

Same case as in feedback on previous PRs, prefer structures that prevent mis-use. (Logic: all cases of err are handled after this one if statement.)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #26517.

Comment thread coderd/x/chatd/tasks.go Outdated
}

func (s *taskStarter) StartAbandon(ctx context.Context, input chatWorkerTaskStartInput) error {
machine := chatstate.NewChatMachine(s.opts.Store, s.opts.Pubsub, input.ChatID, chatstate.Options{})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed the chatstate.Options is reserved for future use. Are there actual plans for what this would entail? Seems like a premature optimization right now.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address now. Remove it

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

Comment thread coderd/x/chatd/tasks.go
Comment on lines +428 to +435
if !ownedByTask(locked, input) {
mismatch = true
return errTaskExpectedExit
}
if err := verifyTaskFence(locked, input, input.Status, taskFenceOptions{requireHistory: true, allowArchived: true}); err != nil {
return err
}
if _, err := tx.Abandon(chatstate.AbandonInput{}); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like this is something that should be enforced by the machine rather than verified in "each" Update. 🤔

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Comment thread coderd/x/chatd/worker.go
}

// Start starts the acquisition and runner manager loops.
func (w *chatWorker) Start(ctx context.Context) error {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better option is to rename this ctx parentCtx and use ctx inline. This helps avoid bugs where the wrong ctx is referenced in the function body.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

Comment thread coderd/x/chatd/worker.go
if manager == nil || manager.idle() {
return nil
}
timer := w.opts.Clock.NewTimer(10*time.Millisecond, "chatworker", "wait-idle")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic number, consider const + docs.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred.

defer wsCtx.close()

parts, discoveredSkillsPart, _, expectedAgentID := server.fetchContextForBuild(ctx, chat, &wsCtx, logger)
_ = discoveredSkillsPart

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😔

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address now.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from b504e1d to 7e3810e Compare June 11, 2026 11:43
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-3 branch 3 times, most recently from 47d102f to bb37788 Compare June 11, 2026 16:27
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from 7e3810e to 61bb879 Compare June 11, 2026 16:27
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-3 branch from bb37788 to fd9d643 Compare June 11, 2026 17:03
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch 2 times, most recently from 5b6d034 to 85a3bab Compare June 11, 2026 17:04
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-3 branch from fd9d643 to c9de302 Compare June 11, 2026 17:04
@hugodutka hugodutka mentioned this pull request Jun 11, 2026
Base automatically changed from hugodutka/chatd-refactor-2 to hugodutka/chatd-refactor June 11, 2026 17:08
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-3 branch from c9de302 to b688bfb Compare June 11, 2026 17:11
@hugodutka
hugodutka merged commit 42fb0f6 into hugodutka/chatd-refactor Jun 11, 2026
2 of 4 checks passed
@hugodutka
hugodutka deleted the hugodutka/chatd-refactor-3 branch June 11, 2026 17:12
hugodutka added a commit that referenced this pull request Jun 11, 2026
PR 4 of the chatd refactor. Implements the `/chats/$id/stream` and
`/chats/$id/stream/parts` endpoints.

The CI doesn't pass on purpose. Only the tip of the chatd refactor PR
stack will pass CI.

Previous PR in the stack: #26109
hugodutka added a commit that referenced this pull request Jun 12, 2026
Implements the chatd stabilization RFC.

Combines:
- #25908
- #25923
- #26109
- #26110
- #26111
- #26112
hugodutka added a commit that referenced this pull request Jun 16, 2026
Enable goleak in chatd tests and fix some leaks. Addresses
#26109 (comment)
hugodutka added a commit that referenced this pull request Jun 18, 2026
Prevents slow chat auto-archive runs from causing a constant archival
loop by resetting the ticker only after each run completes.

Also documents the UTC midnight cutoff used for archive eligibility so
chats with activity on the same UTC calendar date stay eligible or
ineligible for the full day.

Addresses deferred review comments:
- #26109 (comment)
- #26109 (comment)

Generated by Coder Agents and closely reviewed by Hugo.
hugodutka added a commit that referenced this pull request Jun 18, 2026
…implementation (#26508)

Address deferred review feedback for `messagepartbuffer` by documenting
its episode lifecycle, extracting the repeated episode lookup and
finalization helpers, and documenting subscriber channel buffering
decisions.

Addresses these PR #26109 comments:

- #26109 (comment)
- #26109 (comment)
- #26109 (comment)
- #26109 (comment)
hugodutka added a commit that referenced this pull request Jul 6, 2026
Addresses the deferred `coderd/x/chatd/generation.go` review comments
from PR #26109: [required generation
dependencies](#26109 (comment)),
[scoped chat
variables](#26109 (comment)),
[generation state error
handling](#26109 (comment)),
[generation attempt return
values](#26109 (comment)),
[generation fence
verification](#26109 (comment)),
and [chatdebug outcome
recording](#26109 (comment)).

This makes generation task dependencies explicit, packages generation
attempt episode state into a struct, and centralizes generation task
fence checks for generation transitions.

Generated by Coder Agents, closely reviewed by Hugo.
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.

3 participants