mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
48ab492f49
Adds real-time git status watching for workspace agents, so the frontend
can subscribe over WebSocket and show
git file changes in near real-time.
1. Subscription is scoped to a **chat** via `GET
/api/experimental/chats/{chat}/git/watch`.
2. The workspace agent automatically determines which paths to watch
based on tool calls made by the chat (and its ancestor chats).
3. Workspace agent polls subscribed repo working trees on a 30s
interval, on tools calls, and on explicit `refresh` from the client.
4. Scans are rate-limited to at most once per second.
5. Edited paths are tracked **in-memory** inside the workspace agent.
There is no database persistence — state is lost on agent restart. This
will be addresses in a future PR.
6. Messages sent over WebSocket include a full-repo snapshot (unified
diff, branch, origin). A new message is emitted only when the snapshot
changes.
This PR was implemented with AI with me closely controlling what it's
doing. The code follows a plan file that was updated continuously during
implementation. Here's the file if you'd like to see it:
[project.md](https://gist.github.com/hugodutka/8722cf80c92f8a56555f7bc595b770e2).
It reflects the current state of the PR.
149 lines
3.9 KiB
Go
149 lines
3.9 KiB
Go
package agentgit_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/agent/agentgit"
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
)
|
|
|
|
func TestExtractChatContext(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
validID := uuid.MustParse("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")
|
|
ancestor1 := uuid.MustParse("11111111-2222-3333-4444-555555555555")
|
|
ancestor2 := uuid.MustParse("66666666-7777-8888-9999-aaaaaaaaaaaa")
|
|
|
|
tests := []struct {
|
|
name string
|
|
chatID string // empty means header not set
|
|
setChatID bool // whether to set the chat ID header at all
|
|
ancestors string // empty means header not set
|
|
setAncestors bool // whether to set the ancestor header at all
|
|
wantChatID uuid.UUID
|
|
wantAncestorIDs []uuid.UUID
|
|
wantOK bool
|
|
}{
|
|
{
|
|
name: "NoHeadersPresent",
|
|
setChatID: false,
|
|
setAncestors: false,
|
|
wantChatID: uuid.Nil,
|
|
wantAncestorIDs: nil,
|
|
wantOK: false,
|
|
},
|
|
{
|
|
name: "ValidChatID_NoAncestors",
|
|
chatID: validID.String(),
|
|
setChatID: true,
|
|
setAncestors: false,
|
|
wantChatID: validID,
|
|
wantAncestorIDs: nil,
|
|
wantOK: true,
|
|
},
|
|
{
|
|
name: "ValidChatID_ValidAncestors",
|
|
chatID: validID.String(),
|
|
setChatID: true,
|
|
ancestors: mustMarshalJSON(t, []string{
|
|
ancestor1.String(),
|
|
ancestor2.String(),
|
|
}),
|
|
setAncestors: true,
|
|
wantChatID: validID,
|
|
wantAncestorIDs: []uuid.UUID{ancestor1, ancestor2},
|
|
wantOK: true,
|
|
},
|
|
{
|
|
name: "MalformedChatID",
|
|
chatID: "not-a-uuid",
|
|
setChatID: true,
|
|
setAncestors: false,
|
|
wantChatID: uuid.Nil,
|
|
wantAncestorIDs: nil,
|
|
wantOK: false,
|
|
},
|
|
{
|
|
name: "ValidChatID_MalformedAncestorJSON",
|
|
chatID: validID.String(),
|
|
setChatID: true,
|
|
ancestors: `{this is not json}`,
|
|
setAncestors: true,
|
|
wantChatID: validID,
|
|
wantAncestorIDs: nil,
|
|
wantOK: true,
|
|
},
|
|
{
|
|
// Only valid UUIDs in the array are returned; invalid
|
|
// entries are silently skipped.
|
|
name: "ValidChatID_PartialValidAncestorUUIDs",
|
|
chatID: validID.String(),
|
|
setChatID: true,
|
|
ancestors: mustMarshalJSON(t, []string{
|
|
ancestor1.String(),
|
|
"bad-uuid",
|
|
ancestor2.String(),
|
|
}),
|
|
setAncestors: true,
|
|
wantChatID: validID,
|
|
wantAncestorIDs: []uuid.UUID{ancestor1, ancestor2},
|
|
wantOK: true,
|
|
},
|
|
{
|
|
// Header is explicitly set to an empty string, which
|
|
// Header.Get returns as "".
|
|
name: "EmptyChatIDHeader",
|
|
chatID: "",
|
|
setChatID: true,
|
|
setAncestors: false,
|
|
wantChatID: uuid.Nil,
|
|
wantAncestorIDs: nil,
|
|
wantOK: false,
|
|
},
|
|
{
|
|
name: "ValidChatID_EmptyAncestorHeader",
|
|
chatID: validID.String(),
|
|
setChatID: true,
|
|
ancestors: "",
|
|
setAncestors: true,
|
|
wantChatID: validID,
|
|
wantAncestorIDs: nil,
|
|
wantOK: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
if tt.setChatID {
|
|
r.Header.Set(workspacesdk.CoderChatIDHeader, tt.chatID)
|
|
}
|
|
if tt.setAncestors {
|
|
r.Header.Set(workspacesdk.CoderAncestorChatIDsHeader, tt.ancestors)
|
|
}
|
|
|
|
chatID, ancestorIDs, ok := agentgit.ExtractChatContext(r)
|
|
|
|
require.Equal(t, tt.wantOK, ok, "ok mismatch")
|
|
require.Equal(t, tt.wantChatID, chatID, "chatID mismatch")
|
|
require.Equal(t, tt.wantAncestorIDs, ancestorIDs, "ancestorIDs mismatch")
|
|
})
|
|
}
|
|
}
|
|
|
|
// mustMarshalJSON marshals v to a JSON string, failing the test on error.
|
|
func mustMarshalJSON(t *testing.T, v any) string {
|
|
t.Helper()
|
|
b, err := json.Marshal(v)
|
|
require.NoError(t, err)
|
|
return string(b)
|
|
}
|