-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: harden chat generation runtime instrumentation for billing #27451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jaaydenh
merged 18 commits into
main
from
jaayden/codagt-835-b1-audit-and-harden-generation-time-instrumentation
Aug 6, 2026
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a3b2283
feat: harden chat generation runtime instrumentation for billing
jaaydenh 7c2486b
fix: do not bill tool-batch episode spans on interrupted attachments
jaaydenh 525b0ad
refactor(coderd/x/chatd): trim comments to edge cases and invariants
jaaydenh f122655
Merge branch 'main' into jaayden/codagt-835-b1-audit-and-harden-gener…
jaaydenh 359c93a
fix(coderd): bill interrupted turns for the model invocation window
jaaydenh 4673ffe
Merge remote-tracking branch 'origin/main' into jaayden/codagt-835-b1…
jaaydenh 4b6c128
fix(coderd/database/migrations): renumber runtime_ms comment migration
jaaydenh d958346
Merge branch 'main' into jaayden/codagt-835-b1-audit-and-harden-gener…
jaaydenh 9e58122
Merge branch 'main' into jaayden/codagt-835-b1-audit-and-harden-gener…
jaaydenh ca4334b
Merge branch 'main' into jaayden/codagt-835-b1-audit-and-harden-gener…
jaaydenh 5de844f
Merge branch 'main' into jaayden/codagt-835-b1-audit-and-harden-gener…
jaaydenh e23efa7
Merge branch 'main' into jaayden/codagt-835-b1-audit-and-harden-gener…
jaaydenh 2ec3cb8
Update coderd/x/chatd/ARCHITECTURE.md
jaaydenh 7d1a13f
Update coderd/x/chatd/chatloop/chatloop.go
jaaydenh e7abe67
Update coderd/x/chatd/chatloop/chatloop.go
jaaydenh 8a043cb
Update coderd/x/chatd/messagepartbuffer/message_part_buffer.go
jaaydenh 1b85a75
updates for PR review
jaaydenh 9d1374d
chore: update for buffer interface changes
jaaydenh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package chatloop_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "charm.land/fantasy" | ||
| "github.com/stretchr/testify/require" | ||
| "golang.org/x/xerrors" | ||
|
|
||
| "github.com/coder/coder/v2/coderd/x/chatd/chatloop" | ||
| "github.com/coder/coder/v2/coderd/x/chatd/chattest" | ||
| "github.com/coder/quartz" | ||
| ) | ||
|
|
||
| func TestGenerateAssistant_RecordsModelInvocationRuntime(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| clock := quartz.NewMock(t) | ||
| model := &chattest.FakeModel{ | ||
| ProviderName: "test-provider", | ||
| ModelName: "test-model", | ||
| StreamFn: func(_ context.Context, _ fantasy.Call) (fantasy.StreamResponse, error) { | ||
| return func(yield func(fantasy.StreamPart) bool) { | ||
| if !yield(fantasy.StreamPart{Type: fantasy.StreamPartTypeTextStart, ID: "t"}) { | ||
| return | ||
| } | ||
| if !yield(fantasy.StreamPart{Type: fantasy.StreamPartTypeTextDelta, ID: "t", Delta: "hello"}) { | ||
| return | ||
| } | ||
| if !yield(fantasy.StreamPart{Type: fantasy.StreamPartTypeTextEnd, ID: "t"}) { | ||
| return | ||
| } | ||
| clock.Advance(1500 * time.Millisecond) | ||
| yield(fantasy.StreamPart{ | ||
| Type: fantasy.StreamPartTypeFinish, | ||
| FinishReason: fantasy.FinishReasonStop, | ||
| }) | ||
| }, nil | ||
| }, | ||
| } | ||
|
|
||
| outcome, err := chatloop.GenerateAssistant(context.Background(), chatloop.GenerateAssistantOptions{ | ||
| Model: model, | ||
| Clock: clock, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 1500*time.Millisecond, outcome.Step.Runtime) | ||
| } | ||
|
|
||
| // The interrupt path bills the window OnModelStreamStart opens, so that | ||
| // hook must fire at the instant PersistedStep.Runtime starts measuring. | ||
| func TestGenerateAssistant_ModelStreamStartMatchesRuntimeWindow(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| clock := quartz.NewMock(t) | ||
| model := &chattest.FakeModel{ | ||
| ProviderName: "test-provider", | ||
| ModelName: "test-model", | ||
| StreamFn: func(_ context.Context, _ fantasy.Call) (fantasy.StreamResponse, error) { | ||
| return func(yield func(fantasy.StreamPart) bool) { | ||
| if !yield(fantasy.StreamPart{Type: fantasy.StreamPartTypeTextStart, ID: "t"}) { | ||
| return | ||
| } | ||
| if !yield(fantasy.StreamPart{Type: fantasy.StreamPartTypeTextDelta, ID: "t", Delta: "hello"}) { | ||
| return | ||
| } | ||
| if !yield(fantasy.StreamPart{Type: fantasy.StreamPartTypeTextEnd, ID: "t"}) { | ||
| return | ||
| } | ||
| clock.Advance(1500 * time.Millisecond) | ||
| yield(fantasy.StreamPart{ | ||
| Type: fantasy.StreamPartTypeFinish, | ||
| FinishReason: fantasy.FinishReasonStop, | ||
| }) | ||
| }, nil | ||
| }, | ||
| } | ||
|
|
||
| var startedAt []time.Time | ||
| outcome, err := chatloop.GenerateAssistant(context.Background(), chatloop.GenerateAssistantOptions{ | ||
| Model: model, | ||
| Clock: clock, | ||
| OnModelStreamStart: func() { | ||
| startedAt = append(startedAt, clock.Now()) | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Len(t, startedAt, 1) | ||
| require.Equal(t, outcome.Step.Runtime, clock.Since(startedAt[0])) | ||
| } | ||
|
|
||
| func TestGenerateAssistant_ErroredStreamReturnsNoStep(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| clock := quartz.NewMock(t) | ||
| model := &chattest.FakeModel{ | ||
| ProviderName: "test-provider", | ||
| ModelName: "test-model", | ||
| StreamFn: func(_ context.Context, _ fantasy.Call) (fantasy.StreamResponse, error) { | ||
| return func(yield func(fantasy.StreamPart) bool) { | ||
| if !yield(fantasy.StreamPart{Type: fantasy.StreamPartTypeTextStart, ID: "t"}) { | ||
| return | ||
| } | ||
| clock.Advance(1500 * time.Millisecond) | ||
| yield(fantasy.StreamPart{ | ||
| Type: fantasy.StreamPartTypeError, | ||
| Error: xerrors.New("stream blew up"), | ||
| }) | ||
| }, nil | ||
| }, | ||
| } | ||
|
|
||
| outcome, err := chatloop.GenerateAssistant(context.Background(), chatloop.GenerateAssistantOptions{ | ||
| Model: model, | ||
| Clock: clock, | ||
| }) | ||
| require.Error(t, err) | ||
| require.Zero(t, outcome.Step.Runtime) | ||
| require.Empty(t, outcome.Step.Content) | ||
| } | ||
|
|
||
| func TestGenerateCompaction_RecordsRuntime(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| clock := quartz.NewMock(t) | ||
| model := &chattest.FakeModel{ | ||
| ProviderName: "test-provider", | ||
| ModelName: "test-model", | ||
| GenerateFn: func(_ context.Context, _ fantasy.Call) (*fantasy.Response, error) { | ||
| clock.Advance(1500 * time.Millisecond) | ||
| return &fantasy.Response{ | ||
| Content: []fantasy.Content{ | ||
| fantasy.TextContent{Text: "summary"}, | ||
| }, | ||
| }, nil | ||
| }, | ||
| } | ||
|
|
||
| var startedAt []time.Time | ||
| result, err := chatloop.GenerateCompaction(context.Background(), chatloop.GenerateCompactionOptions{ | ||
| Model: model, | ||
| Messages: []fantasy.Message{{ | ||
| Role: fantasy.MessageRoleUser, | ||
| Content: []fantasy.MessagePart{fantasy.TextPart{Text: "hello"}}, | ||
| }}, | ||
| ThresholdPercent: 70, | ||
| ContextLimit: 100, | ||
| StepUsage: fantasy.Usage{InputTokens: 90}, | ||
| Clock: clock, | ||
| OnModelStreamStart: func() { | ||
| startedAt = append(startedAt, clock.Now()) | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "summary", result.SummaryReport) | ||
| require.Equal(t, 1500*time.Millisecond, result.Runtime) | ||
| require.Len(t, startedAt, 1) | ||
| require.Equal(t, result.Runtime, clock.Since(startedAt[0])) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.