You've already forked agentic-coding-workflow
bugfix
This commit is contained in:
@@ -168,11 +168,13 @@ type GitOpsService interface {
|
||||
CommitOnMain(ctx context.Context, c Caller, wsID uuid.UUID, in CommitInput) (string, error)
|
||||
PushOnMain(ctx context.Context, c Caller, wsID uuid.UUID) error
|
||||
LogOnMain(ctx context.Context, c Caller, wsID uuid.UUID, n int) ([]git.Commit, error)
|
||||
DiffOnMain(ctx context.Context, c Caller, wsID uuid.UUID, opts git.DiffOptions) (git.DiffResult, error)
|
||||
|
||||
StatusOnWorktree(ctx context.Context, c Caller, wtID uuid.UUID) ([]git.FileStatus, error)
|
||||
CommitOnWorktree(ctx context.Context, c Caller, wtID uuid.UUID, in CommitInput) (string, error)
|
||||
PushOnWorktree(ctx context.Context, c Caller, wtID uuid.UUID) error
|
||||
LogOnWorktree(ctx context.Context, c Caller, wtID uuid.UUID, n int) ([]git.Commit, error)
|
||||
DiffOnWorktree(ctx context.Context, c Caller, wtID uuid.UUID, opts git.DiffOptions) (git.DiffResult, error)
|
||||
}
|
||||
|
||||
// ===== Input DTO =====
|
||||
|
||||
@@ -131,3 +131,10 @@ type commitLogResp struct {
|
||||
func toCommitLogResp(c git.Commit) commitLogResp {
|
||||
return commitLogResp{Hash: c.Hash, Author: c.Author, Email: c.Email, Date: c.Date, Subject: c.Subject}
|
||||
}
|
||||
|
||||
// diffResp 是 GET /diff 的响应体:原始 unified-diff 文本 + 截断标志。
|
||||
// 不在后端解析 hunk,由前端 DiffViewer 负责。
|
||||
type diffResp struct {
|
||||
Diff string `json:"diff"`
|
||||
Truncated bool `json:"truncated"`
|
||||
}
|
||||
|
||||
@@ -13,12 +13,18 @@ import (
|
||||
// CredentialResolver 是 service 共享的解密回调,避免 GitOpsService 直接持有 *crypto.Encryptor。
|
||||
type CredentialResolver func(ctx context.Context, wsID uuid.UUID) (*git.Credential, error)
|
||||
|
||||
// CommitSummaryHook is a best-effort callback fired after a successful commit to
|
||||
// enqueue auto-doc/PR-summary generation. Injected from app.go via
|
||||
// SetCommitSummaryHook to avoid a workspace→docs import cycle. May be nil.
|
||||
type CommitSummaryHook func(ctx context.Context, wsID uuid.UUID, worktreeID *uuid.UUID, branch, commitSHA string) error
|
||||
|
||||
type gitOpsService struct {
|
||||
repo Repository
|
||||
rec audit.Recorder
|
||||
gitr git.Runner
|
||||
pa ProjectAccess
|
||||
cred CredentialResolver
|
||||
repo Repository
|
||||
rec audit.Recorder
|
||||
gitr git.Runner
|
||||
pa ProjectAccess
|
||||
cred CredentialResolver
|
||||
summaryHook CommitSummaryHook
|
||||
}
|
||||
|
||||
// NewGitOpsService 构造 GitOpsService。cred 由 WorkspaceService.loadDecryptedCredential 适配过来。
|
||||
@@ -26,6 +32,9 @@ func NewGitOpsService(repo Repository, rec audit.Recorder, gitr git.Runner, pa P
|
||||
return &gitOpsService{repo: repo, rec: rec, gitr: gitr, pa: pa, cred: cred}
|
||||
}
|
||||
|
||||
// SetCommitSummaryHook injects the auto-doc enqueue callback (app wiring).
|
||||
func (s *gitOpsService) SetCommitSummaryHook(h CommitSummaryHook) { s.summaryHook = h }
|
||||
|
||||
// ===== Main =====
|
||||
|
||||
func (s *gitOpsService) StatusOnMain(ctx context.Context, c Caller, wsID uuid.UUID) ([]git.FileStatus, error) {
|
||||
@@ -50,6 +59,7 @@ func (s *gitOpsService) CommitOnMain(ctx context.Context, c Caller, wsID uuid.UU
|
||||
return "", errs.Wrap(err, errs.CodeGitCmdFailed, "git commit")
|
||||
}
|
||||
s.auditCommit(ctx, &c.UserID, wsID.String(), "main", in.Message, sha)
|
||||
s.fireCommitSummary(ctx, &c.UserID, wsID, nil, ws.DefaultBranch, sha)
|
||||
if in.Push {
|
||||
cred, err := s.cred(ctx, wsID)
|
||||
if err != nil {
|
||||
@@ -63,6 +73,20 @@ func (s *gitOpsService) CommitOnMain(ctx context.Context, c Caller, wsID uuid.UU
|
||||
return sha, nil
|
||||
}
|
||||
|
||||
// fireCommitSummary invokes the auto-doc hook best-effort; failures are audited
|
||||
// but never surfaced to the commit caller.
|
||||
func (s *gitOpsService) fireCommitSummary(ctx context.Context, userID *uuid.UUID, wsID uuid.UUID, worktreeID *uuid.UUID, branch, sha string) {
|
||||
if s.summaryHook == nil || sha == "" {
|
||||
return
|
||||
}
|
||||
if herr := s.summaryHook(ctx, wsID, worktreeID, branch, sha); herr != nil {
|
||||
_ = s.rec.Record(ctx, audit.Entry{
|
||||
UserID: userID, Action: "docs.summary_enqueue_failed", TargetType: "workspace",
|
||||
TargetID: wsID.String(), Metadata: map[string]any{"err": herr.Error()},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *gitOpsService) PushOnMain(ctx context.Context, c Caller, wsID uuid.UUID) error {
|
||||
ws, err := s.guardWorkspaceWrite(ctx, c, wsID)
|
||||
if err != nil {
|
||||
@@ -91,6 +115,18 @@ func (s *gitOpsService) LogOnMain(ctx context.Context, c Caller, wsID uuid.UUID,
|
||||
return commits, nil
|
||||
}
|
||||
|
||||
func (s *gitOpsService) DiffOnMain(ctx context.Context, c Caller, wsID uuid.UUID, opts git.DiffOptions) (git.DiffResult, error) {
|
||||
ws, err := s.guardWorkspaceRead(ctx, c, wsID)
|
||||
if err != nil {
|
||||
return git.DiffResult{}, err
|
||||
}
|
||||
res, err := s.gitr.Diff(ctx, ws.MainPath, opts)
|
||||
if err != nil {
|
||||
return git.DiffResult{}, errs.Wrap(err, errs.CodeGitCmdFailed, "git diff")
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ===== Worktree =====
|
||||
|
||||
func (s *gitOpsService) StatusOnWorktree(ctx context.Context, c Caller, wtID uuid.UUID) ([]git.FileStatus, error) {
|
||||
@@ -116,6 +152,8 @@ func (s *gitOpsService) CommitOnWorktree(ctx context.Context, c Caller, wtID uui
|
||||
return "", errs.Wrap(err, errs.CodeGitCmdFailed, "git commit")
|
||||
}
|
||||
s.auditCommit(ctx, &c.UserID, ws.ID.String(), wt.Branch, in.Message, sha)
|
||||
wtIDCopy := wt.ID
|
||||
s.fireCommitSummary(ctx, &c.UserID, ws.ID, &wtIDCopy, wt.Branch, sha)
|
||||
if in.Push {
|
||||
cred, err := s.cred(ctx, ws.ID)
|
||||
if err != nil {
|
||||
@@ -157,6 +195,18 @@ func (s *gitOpsService) LogOnWorktree(ctx context.Context, c Caller, wtID uuid.U
|
||||
return commits, nil
|
||||
}
|
||||
|
||||
func (s *gitOpsService) DiffOnWorktree(ctx context.Context, c Caller, wtID uuid.UUID, opts git.DiffOptions) (git.DiffResult, error) {
|
||||
wt, _, err := s.guardWorktreeRead(ctx, c, wtID)
|
||||
if err != nil {
|
||||
return git.DiffResult{}, err
|
||||
}
|
||||
res, err := s.gitr.Diff(ctx, wt.Path, opts)
|
||||
if err != nil {
|
||||
return git.DiffResult{}, errs.Wrap(err, errs.CodeGitCmdFailed, "git diff")
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ===== guards =====
|
||||
|
||||
func (s *gitOpsService) guardWorkspaceRead(ctx context.Context, c Caller, wsID uuid.UUID) (*Workspace, error) {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
@@ -61,6 +62,7 @@ func (h *Handler) Mount(r chi.Router) {
|
||||
r.Post("/main/commit", h.commitOnMain)
|
||||
r.Post("/main/push", h.pushOnMain)
|
||||
r.Get("/main/log", h.logOnMain)
|
||||
r.Get("/diff", h.diffOnMain)
|
||||
|
||||
r.Get("/worktrees", h.listWorktrees)
|
||||
r.Post("/worktrees", h.createWorktree)
|
||||
@@ -74,6 +76,7 @@ func (h *Handler) Mount(r chi.Router) {
|
||||
r.Post("/commit", h.commitOnWorktree)
|
||||
r.Post("/push", h.pushOnWorktree)
|
||||
r.Get("/log", h.logOnWorktree)
|
||||
r.Get("/diff", h.diffOnWorktree)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -595,3 +598,63 @@ func parseLogN(r *http.Request) int {
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (h *Handler) diffOnMain(w http.ResponseWriter, r *http.Request) {
|
||||
h.diffGeneric(w, r, true)
|
||||
}
|
||||
|
||||
func (h *Handler) diffOnWorktree(w http.ResponseWriter, r *http.Request) {
|
||||
h.diffGeneric(w, r, false)
|
||||
}
|
||||
|
||||
func (h *Handler) diffGeneric(w http.ResponseWriter, r *http.Request, onMain bool) {
|
||||
c, err := h.caller(r)
|
||||
if err != nil {
|
||||
writeErr(w, r, err)
|
||||
return
|
||||
}
|
||||
param := "wsID"
|
||||
if !onMain {
|
||||
param = "wtID"
|
||||
}
|
||||
id, err := parseUUID(chi.URLParam(r, param))
|
||||
if err != nil {
|
||||
writeErr(w, r, err)
|
||||
return
|
||||
}
|
||||
opts := parseDiffOptions(r)
|
||||
var res git.DiffResult
|
||||
if onMain {
|
||||
res, err = h.gop.DiffOnMain(r.Context(), c, id, opts)
|
||||
} else {
|
||||
res, err = h.gop.DiffOnWorktree(r.Context(), c, id, opts)
|
||||
}
|
||||
if err != nil {
|
||||
writeErr(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, diffResp{Diff: res.Diff, Truncated: res.Truncated})
|
||||
}
|
||||
|
||||
// parseDiffOptions reads the diff query params (ref, against, staged, paths, context).
|
||||
func parseDiffOptions(r *http.Request) git.DiffOptions {
|
||||
q := r.URL.Query()
|
||||
opts := git.DiffOptions{
|
||||
Ref: q.Get("ref"),
|
||||
Against: q.Get("against"),
|
||||
Staged: q.Get("staged") == "true" || q.Get("staged") == "1",
|
||||
}
|
||||
if raw := q.Get("paths"); raw != "" {
|
||||
for _, p := range strings.Split(raw, ",") {
|
||||
if p = strings.TrimSpace(p); p != "" {
|
||||
opts.Paths = append(opts.Paths, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
if cl := q.Get("context"); cl != "" {
|
||||
if n, err := strconv.Atoi(cl); err == nil && n >= 0 {
|
||||
opts.ContextLines = n
|
||||
}
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package workspace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWorktreeCreate_FiresIndexHookOnce(t *testing.T) {
|
||||
t.Parallel()
|
||||
svc, _, _, _, wsID := newWtSvc(t)
|
||||
|
||||
var calls int
|
||||
var gotWS uuid.UUID
|
||||
var gotWT *uuid.UUID
|
||||
svc.SetIndexBuildHook(func(_ context.Context, w uuid.UUID, wt *uuid.UUID, _ string) error {
|
||||
calls++
|
||||
gotWS = w
|
||||
gotWT = wt
|
||||
return nil
|
||||
})
|
||||
|
||||
wt, err := svc.Create(context.Background(), Caller{UserID: uuid.New()}, wsID, CreateWorktreeInput{Branch: "feat-idx"})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, calls, "index hook must fire exactly once on worktree create")
|
||||
require.Equal(t, wsID, gotWS)
|
||||
require.NotNil(t, gotWT)
|
||||
require.Equal(t, wt.ID, *gotWT)
|
||||
}
|
||||
|
||||
func TestWorktreeCreate_HookErrorDoesNotFailCreate(t *testing.T) {
|
||||
t.Parallel()
|
||||
svc, _, _, _, wsID := newWtSvc(t)
|
||||
svc.SetIndexBuildHook(func(context.Context, uuid.UUID, *uuid.UUID, string) error {
|
||||
return context.DeadlineExceeded // simulate enqueue failure
|
||||
})
|
||||
_, err := svc.Create(context.Background(), Caller{UserID: uuid.New()}, wsID, CreateWorktreeInput{Branch: "feat-e"})
|
||||
require.NoError(t, err, "hook failure must not fail worktree create")
|
||||
}
|
||||
|
||||
func TestCommitOnMain_FiresSummaryHook(t *testing.T) {
|
||||
t.Parallel()
|
||||
svc, _, _, wsID := newGitOpsSvc(t)
|
||||
|
||||
var calls int
|
||||
var gotSHA, gotBranch string
|
||||
var gotWT *uuid.UUID
|
||||
svc.SetCommitSummaryHook(func(_ context.Context, _ uuid.UUID, wt *uuid.UUID, branch, sha string) error {
|
||||
calls++
|
||||
gotBranch, gotSHA, gotWT = branch, sha, wt
|
||||
return nil
|
||||
})
|
||||
|
||||
sha, err := svc.CommitOnMain(context.Background(), Caller{UserID: uuid.New()}, wsID, CommitInput{Message: "m", AddAll: true})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, calls)
|
||||
require.Equal(t, sha, gotSHA)
|
||||
require.Equal(t, "main", gotBranch)
|
||||
require.Nil(t, gotWT, "main commits carry no worktree id")
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
const getCredentialByWorkspace = `-- name: GetCredentialByWorkspace :one
|
||||
SELECT id, workspace_id, kind, username, encrypted_secret, fingerprint, created_at, updated_at FROM git_credentials WHERE workspace_id = $1
|
||||
SELECT id, workspace_id, kind, username, encrypted_secret, fingerprint, created_at, updated_at, key_version FROM git_credentials WHERE workspace_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetCredentialByWorkspace(ctx context.Context, workspaceID pgtype.UUID) (GitCredential, error) {
|
||||
@@ -27,6 +27,7 @@ func (q *Queries) GetCredentialByWorkspace(ctx context.Context, workspaceID pgty
|
||||
&i.Fingerprint,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.KeyVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -40,7 +41,7 @@ ON CONFLICT (workspace_id) DO UPDATE
|
||||
encrypted_secret = EXCLUDED.encrypted_secret,
|
||||
fingerprint = EXCLUDED.fingerprint,
|
||||
updated_at = now()
|
||||
RETURNING id, workspace_id, kind, username, encrypted_secret, fingerprint, created_at, updated_at
|
||||
RETURNING id, workspace_id, kind, username, encrypted_secret, fingerprint, created_at, updated_at, key_version
|
||||
`
|
||||
|
||||
type UpsertCredentialParams struct {
|
||||
@@ -71,6 +72,7 @@ func (q *Queries) UpsertCredential(ctx context.Context, arg UpsertCredentialPara
|
||||
&i.Fingerprint,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.KeyVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"net/netip"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/pgvector/pgvector-go"
|
||||
)
|
||||
|
||||
type AcpAgentKind struct {
|
||||
@@ -25,6 +26,11 @@ type AcpAgentKind struct {
|
||||
ToolAllowlist []string `json:"tool_allowlist"`
|
||||
ClientType string `json:"client_type"`
|
||||
EncryptedMcpServers []byte `json:"encrypted_mcp_servers"`
|
||||
ModelID pgtype.UUID `json:"model_id"`
|
||||
MaxCostUsd pgtype.Numeric `json:"max_cost_usd"`
|
||||
MaxTokens *int64 `json:"max_tokens"`
|
||||
MaxWallClockSeconds *int32 `json:"max_wall_clock_seconds"`
|
||||
KeyVersion int16 `json:"key_version"`
|
||||
}
|
||||
|
||||
type AcpAgentKindConfigFile struct {
|
||||
@@ -35,6 +41,7 @@ type AcpAgentKindConfigFile struct {
|
||||
UpdatedBy pgtype.UUID `json:"updated_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
KeyVersion int16 `json:"key_version"`
|
||||
}
|
||||
|
||||
type AcpEvent struct {
|
||||
@@ -64,23 +71,64 @@ type AcpPermissionRequest struct {
|
||||
}
|
||||
|
||||
type AcpSession struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
IssueID pgtype.UUID `json:"issue_id"`
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
AgentSessionID *string `json:"agent_session_id"`
|
||||
Branch string `json:"branch"`
|
||||
CwdPath string `json:"cwd_path"`
|
||||
IsMainWorktree bool `json:"is_main_worktree"`
|
||||
Status string `json:"status"`
|
||||
Pid *int32 `json:"pid"`
|
||||
ExitCode *int32 `json:"exit_code"`
|
||||
LastError *string `json:"last_error"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
EndedAt pgtype.Timestamptz `json:"ended_at"`
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
IssueID pgtype.UUID `json:"issue_id"`
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
AgentSessionID *string `json:"agent_session_id"`
|
||||
Branch string `json:"branch"`
|
||||
CwdPath string `json:"cwd_path"`
|
||||
IsMainWorktree bool `json:"is_main_worktree"`
|
||||
Status string `json:"status"`
|
||||
Pid *int32 `json:"pid"`
|
||||
ExitCode *int32 `json:"exit_code"`
|
||||
LastError *string `json:"last_error"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
EndedAt pgtype.Timestamptz `json:"ended_at"`
|
||||
LastStopReason *string `json:"last_stop_reason"`
|
||||
OrchestratorStepID pgtype.UUID `json:"orchestrator_step_id"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
ThinkingTokens int64 `json:"thinking_tokens"`
|
||||
TotalCostUsd pgtype.Numeric `json:"total_cost_usd"`
|
||||
LastActivityAt pgtype.Timestamptz `json:"last_activity_at"`
|
||||
BudgetMaxCostUsd pgtype.Numeric `json:"budget_max_cost_usd"`
|
||||
BudgetMaxTokens *int64 `json:"budget_max_tokens"`
|
||||
BudgetMaxWallClockSeconds *int32 `json:"budget_max_wall_clock_seconds"`
|
||||
TerminatedReason *string `json:"terminated_reason"`
|
||||
SandboxMode *string `json:"sandbox_mode"`
|
||||
CostUsd pgtype.Numeric `json:"cost_usd"`
|
||||
TokensTotal int64 `json:"tokens_total"`
|
||||
}
|
||||
|
||||
type AcpSessionUsage struct {
|
||||
ID int64 `json:"id"`
|
||||
SessionID pgtype.UUID `json:"session_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
ModelID pgtype.UUID `json:"model_id"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
ThinkingTokens int64 `json:"thinking_tokens"`
|
||||
CostUsd pgtype.Numeric `json:"cost_usd"`
|
||||
SourceEventID *int64 `json:"source_event_id"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type AcpTurn struct {
|
||||
ID int64 `json:"id"`
|
||||
SessionID pgtype.UUID `json:"session_id"`
|
||||
TurnIndex int32 `json:"turn_index"`
|
||||
PromptRequestID *string `json:"prompt_request_id"`
|
||||
Status string `json:"status"`
|
||||
StopReason *string `json:"stop_reason"`
|
||||
UpdateCount int32 `json:"update_count"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
CompletedAt pgtype.Timestamptz `json:"completed_at"`
|
||||
}
|
||||
|
||||
type AuditLog struct {
|
||||
@@ -94,6 +142,81 @@ type AuditLog struct {
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type ChangeRequest struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
IssueID pgtype.UUID `json:"issue_id"`
|
||||
Number int32 `json:"number"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
SourceBranch string `json:"source_branch"`
|
||||
TargetBranch string `json:"target_branch"`
|
||||
State string `json:"state"`
|
||||
ReviewVerdict string `json:"review_verdict"`
|
||||
CiState string `json:"ci_state"`
|
||||
Provider string `json:"provider"`
|
||||
ExternalID *int64 `json:"external_id"`
|
||||
ExternalUrl string `json:"external_url"`
|
||||
MergeCommitSha string `json:"merge_commit_sha"`
|
||||
ReviewedBy pgtype.UUID `json:"reviewed_by"`
|
||||
ReviewedAt pgtype.Timestamptz `json:"reviewed_at"`
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
MergedAt pgtype.Timestamptz `json:"merged_at"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type CodeChunk struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
RunID pgtype.UUID `json:"run_id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
CommitSha string `json:"commit_sha"`
|
||||
FilePath string `json:"file_path"`
|
||||
Lang *string `json:"lang"`
|
||||
StartLine int32 `json:"start_line"`
|
||||
EndLine int32 `json:"end_line"`
|
||||
Content string `json:"content"`
|
||||
ContentSha []byte `json:"content_sha"`
|
||||
TokenCount *int32 `json:"token_count"`
|
||||
Embedding *pgvector.Vector `json:"embedding"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type CodeIndexRun struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
WorktreeID pgtype.UUID `json:"worktree_id"`
|
||||
Branch string `json:"branch"`
|
||||
CommitSha string `json:"commit_sha"`
|
||||
Status string `json:"status"`
|
||||
EmbeddingEndpointID pgtype.UUID `json:"embedding_endpoint_id"`
|
||||
EmbeddingModel string `json:"embedding_model"`
|
||||
Dims int32 `json:"dims"`
|
||||
FilesIndexed int32 `json:"files_indexed"`
|
||||
ChunksIndexed int32 `json:"chunks_indexed"`
|
||||
Error *string `json:"error"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
FinishedAt pgtype.Timestamptz `json:"finished_at"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type CommitSummary struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
WorktreeID pgtype.UUID `json:"worktree_id"`
|
||||
Branch string `json:"branch"`
|
||||
CommitSha string `json:"commit_sha"`
|
||||
Kind string `json:"kind"`
|
||||
Title *string `json:"title"`
|
||||
BodyMd string `json:"body_md"`
|
||||
Model *string `json:"model"`
|
||||
Status string `json:"status"`
|
||||
Error *string `json:"error"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type Conversation struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
@@ -110,6 +233,14 @@ type Conversation struct {
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
}
|
||||
|
||||
type CryptoKey struct {
|
||||
Version int32 `json:"version"`
|
||||
Provider string `json:"provider"`
|
||||
KeyRef *string `json:"key_ref"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type GitCredential struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
@@ -119,6 +250,7 @@ type GitCredential struct {
|
||||
Fingerprint *string `json:"fingerprint"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
KeyVersion int16 `json:"key_version"`
|
||||
}
|
||||
|
||||
type Issue struct {
|
||||
@@ -135,6 +267,17 @@ type Issue struct {
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
ParentID pgtype.UUID `json:"parent_id"`
|
||||
Priority int32 `json:"priority"`
|
||||
}
|
||||
|
||||
type IssueDependency struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
BlockedID pgtype.UUID `json:"blocked_id"`
|
||||
BlockerID pgtype.UUID `json:"blocker_id"`
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type Job struct {
|
||||
@@ -162,6 +305,7 @@ type LlmEndpoint struct {
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
KeyVersion int16 `json:"key_version"`
|
||||
}
|
||||
|
||||
type LlmModel struct {
|
||||
@@ -252,6 +396,50 @@ type Notification struct {
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type NotificationPref struct {
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
EmailEnabled bool `json:"email_enabled"`
|
||||
ImEnabled bool `json:"im_enabled"`
|
||||
ImWebhookUrlEnc []byte `json:"im_webhook_url_enc"`
|
||||
MinSeverity string `json:"min_severity"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type OrchestratorRun struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
OwnerID pgtype.UUID `json:"owner_id"`
|
||||
Status string `json:"status"`
|
||||
CurrentPhase string `json:"current_phase"`
|
||||
Config []byte `json:"config"`
|
||||
LastError *string `json:"last_error"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
EndedAt pgtype.Timestamptz `json:"ended_at"`
|
||||
}
|
||||
|
||||
type OrchestratorStep struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
RunID pgtype.UUID `json:"run_id"`
|
||||
Phase string `json:"phase"`
|
||||
Attempt int32 `json:"attempt"`
|
||||
ParentStepID pgtype.UUID `json:"parent_step_id"`
|
||||
Depth int32 `json:"depth"`
|
||||
Status string `json:"status"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
AcpSessionID pgtype.UUID `json:"acp_session_id"`
|
||||
Prompt string `json:"prompt"`
|
||||
StopReason *string `json:"stop_reason"`
|
||||
GateResult []byte `json:"gate_result"`
|
||||
LastError *string `json:"last_error"`
|
||||
JobID pgtype.UUID `json:"job_id"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
EndedAt pgtype.Timestamptz `json:"ended_at"`
|
||||
}
|
||||
|
||||
type Project struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
@@ -264,6 +452,30 @@ type Project struct {
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type ProjectMember struct {
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
Role string `json:"role"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
AddedBy pgtype.UUID `json:"added_by"`
|
||||
}
|
||||
|
||||
type ProjectMemory struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
Kind string `json:"kind"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Tags []string `json:"tags"`
|
||||
Source string `json:"source"`
|
||||
Embedding *pgvector.Vector `json:"embedding"`
|
||||
EmbeddingModel *string `json:"embedding_model"`
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type PromptTemplate struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
@@ -299,6 +511,16 @@ type RequirementArtifact struct {
|
||||
SourceMessageID *int64 `json:"source_message_id"`
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
Verdict string `json:"verdict"`
|
||||
}
|
||||
|
||||
type RequirementPhasePointer struct {
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
Phase string `json:"phase"`
|
||||
ApprovedArtifactID pgtype.UUID `json:"approved_artifact_id"`
|
||||
ApprovedBy pgtype.UUID `json:"approved_by"`
|
||||
ApprovedAt pgtype.Timestamptz `json:"approved_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
@@ -354,6 +576,7 @@ type WorkspaceRunProfile struct {
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
KeyVersion int16 `json:"key_version"`
|
||||
}
|
||||
|
||||
type WorkspaceWorktree struct {
|
||||
|
||||
@@ -42,8 +42,13 @@ type workspaceService struct {
|
||||
clones CloneScheduler
|
||||
dataDir string
|
||||
fetchTimout time.Duration
|
||||
indexHook IndexBuildHook
|
||||
}
|
||||
|
||||
// SetIndexBuildHook injects the code-index enqueue callback (app wiring). On Sync
|
||||
// the workspace marks prior runs stale and enqueues a fresh build at new HEAD.
|
||||
func (s *workspaceService) SetIndexBuildHook(h IndexBuildHook) { s.indexHook = h }
|
||||
|
||||
// NewWorkspaceService 构造 WorkspaceService 实现。
|
||||
func NewWorkspaceService(
|
||||
repo Repository,
|
||||
@@ -281,6 +286,13 @@ func (s *workspaceService) Sync(ctx context.Context, c Caller, wsID uuid.UUID) (
|
||||
return nil, err
|
||||
}
|
||||
s.audit(writeCtx, &c.UserID, "workspace.sync", "workspace", wsID.String(), nil)
|
||||
// Best-effort: re-index the main worktree at the new HEAD. The hook marks
|
||||
// prior runs stale then enqueues a build (coalesced on commit). Never blocks.
|
||||
if s.indexHook != nil {
|
||||
if herr := s.indexHook(writeCtx, wsID, nil, ""); herr != nil {
|
||||
s.audit(writeCtx, &c.UserID, "workspace.index_enqueue_failed", "workspace", wsID.String(), map[string]any{"err": herr.Error()})
|
||||
}
|
||||
}
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -238,6 +238,11 @@ func (f *fakeGit) ListRemoteBranches(_ context.Context, _ string, _ *git.Credent
|
||||
return nil, nil
|
||||
}
|
||||
func (f *fakeGit) Log(_ context.Context, _ string, _ int) ([]git.Commit, error) { return nil, nil }
|
||||
func (f *fakeGit) Diff(_ context.Context, _ string, _ git.DiffOptions) (git.DiffResult, error) {
|
||||
return git.DiffResult{}, nil
|
||||
}
|
||||
func (f *fakeGit) ListTrackedFiles(_ context.Context, _ string) ([]string, error) { return nil, nil }
|
||||
func (f *fakeGit) DiffStat(_ context.Context, _, _ string) (string, error) { return "", nil }
|
||||
|
||||
// trackGit 包装 git.Runner,记录 FetchRemoteBranch / Checkout 调用以供断言。
|
||||
// 嵌入 Runner 接口本身可避免为每个方法重复转发。
|
||||
|
||||
@@ -12,12 +12,18 @@ import (
|
||||
"github.com/yan1h/agent-coding-workflow/internal/infra/git"
|
||||
)
|
||||
|
||||
// IndexBuildHook is a best-effort callback fired after a worktree is created (or
|
||||
// a workspace is synced) to enqueue a code-index build. Injected from app.go via
|
||||
// SetIndexBuildHook to avoid a workspace→codeindex import cycle. May be nil.
|
||||
type IndexBuildHook func(ctx context.Context, wsID uuid.UUID, worktreeID *uuid.UUID, commitSHA string) error
|
||||
|
||||
type worktreeService struct {
|
||||
repo Repository
|
||||
rec audit.Recorder
|
||||
gitr git.Runner
|
||||
pa ProjectAccess
|
||||
dataDir string
|
||||
repo Repository
|
||||
rec audit.Recorder
|
||||
gitr git.Runner
|
||||
pa ProjectAccess
|
||||
dataDir string
|
||||
indexHook IndexBuildHook
|
||||
}
|
||||
|
||||
// NewWorktreeService 构造 WorktreeService。
|
||||
@@ -25,6 +31,10 @@ func NewWorktreeService(repo Repository, rec audit.Recorder, gitr git.Runner, pa
|
||||
return &worktreeService{repo: repo, rec: rec, gitr: gitr, pa: pa, dataDir: dataDir}
|
||||
}
|
||||
|
||||
// SetIndexBuildHook injects the code-index enqueue callback (app wiring). Safe to
|
||||
// call once at construction; nil disables index builds on worktree create.
|
||||
func (s *worktreeService) SetIndexBuildHook(h IndexBuildHook) { s.indexHook = h }
|
||||
|
||||
func (s *worktreeService) Create(ctx context.Context, c Caller, wsID uuid.UUID, in CreateWorktreeInput) (*Worktree, error) {
|
||||
ws, err := s.repo.GetWorkspaceByID(ctx, wsID)
|
||||
if err != nil {
|
||||
@@ -71,6 +81,14 @@ func (s *worktreeService) Create(ctx context.Context, c Caller, wsID uuid.UUID,
|
||||
return nil, err
|
||||
}
|
||||
s.audit(ctx, &c.UserID, "worktree.create", "worktree", saved.ID.String(), map[string]any{"branch": in.Branch})
|
||||
// Best-effort: enqueue a code-index build for the new worktree at its HEAD.
|
||||
// Failures are logged via audit but never block worktree creation.
|
||||
if s.indexHook != nil {
|
||||
wtID := saved.ID
|
||||
if herr := s.indexHook(ctx, wsID, &wtID, ""); herr != nil {
|
||||
s.audit(ctx, &c.UserID, "worktree.index_enqueue_failed", "worktree", saved.ID.String(), map[string]any{"err": herr.Error()})
|
||||
}
|
||||
}
|
||||
return saved, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user