From 0c860dbbb97732d90eb38462e3536dbe7cf85ba7 Mon Sep 17 00:00:00 2001 From: Kris <2040188027@qq.com> Date: Sat, 21 Nov 2026 20:58:31 +0800 Subject: [PATCH] fix(schedule): attribute dispatched tasks and runs to their schedule execution Add a server-derived scheduleExecutionId (sjx-) to the scheduled dispatch path so each dispatched Task and Run, its child SOPs, and ingested telemetry/log carry the specific ScheduleJobExecution that produced them. - dispatchScheduledRun derives the ID from the claimed execution trace; an empty trace never fabricates an sjx- ID. - dispatchIssue writes it onto the Task before ReserveIssueDispatch and into the Run TelemetryContext; public manual dispatch never sets it. - worker-log ingestion unconditionally overwrites the value from the persisted Run context (including clearing it for manual runs). - child SOPs inherit it via the existing ExecutionContext value copy; TraceID == Run.ID is unchanged. No new tables, no migration, no backfill. - document the read-only Task.scheduleExecutionId in the OpenAPI schema. This is a backend slice of #460 only; it does not close the issue. Related to #460. Does not duplicate #1128 or #1145. --- docs/api/openapi.yaml | 4 + internal/domain/observability.go | 10 +- internal/domain/task.go | 28 ++-- internal/engine/run_events.go | 4 + .../engine/schedule_execution_context_test.go | 111 ++++++++++++++++ internal/httpapi/dispatch.go | 11 +- internal/httpapi/schedule.go | 9 +- .../schedule_execution_attribution_test.go | 121 ++++++++++++++++++ 8 files changed, 278 insertions(+), 20 deletions(-) create mode 100644 internal/engine/schedule_execution_context_test.go create mode 100644 internal/httpapi/schedule_execution_attribution_test.go diff --git a/docs/api/openapi.yaml b/docs/api/openapi.yaml index 6d4f25c73..5b41d2602 100644 --- a/docs/api/openapi.yaml +++ b/docs/api/openapi.yaml @@ -4824,6 +4824,10 @@ components: enum: [primary, scheduled, rerun] scheduleJobId: type: string + scheduleExecutionId: + type: string + readOnly: true + description: Schedule execution identifier ("sjx-") the server assigned when a scheduled job dispatched this task; inherited by the run and its child SOPs. Empty for manual dispatch and legacy records. Read-only and never accepted from request payloads. triggerOrigin: type: string repository: diff --git a/internal/domain/observability.go b/internal/domain/observability.go index 381f1020b..d9c45bb72 100644 --- a/internal/domain/observability.go +++ b/internal/domain/observability.go @@ -45,9 +45,13 @@ type TelemetryContext struct { IssueNumber int `json:"issueNumber,omitempty"` TaskID string `json:"taskId,omitempty"` ScheduleJobID string `json:"scheduleJobId,omitempty"` - RunID string `json:"runId"` - ParentRunID string `json:"parentRunId,omitempty"` - ParentNodeID string `json:"parentNodeId,omitempty"` + // ScheduleExecutionID names the schedule execution this context belongs to + // ("sjx-"). The server derives it from the persisted Run context and + // overwrites any worker-reported value; empty for manual or legacy runs. + ScheduleExecutionID string `json:"scheduleExecutionId,omitempty"` + RunID string `json:"runId"` + ParentRunID string `json:"parentRunId,omitempty"` + ParentNodeID string `json:"parentNodeId,omitempty"` // WorkspaceRunID names the run that owns the workspace a child SOP works // in: the top-level run of its SOP chain. Empty means the run owns its // own workspace. diff --git a/internal/domain/task.go b/internal/domain/task.go index f6c889127..388bef602 100644 --- a/internal/domain/task.go +++ b/internal/domain/task.go @@ -3,16 +3,20 @@ package domain import "time" type Task struct { - ID string `json:"id"` - IssueID string `json:"issueId"` - AgentID string `json:"agentId"` - WorkflowID string `json:"workflowId"` - RunID string `json:"runId"` - Status string `json:"status"` // queued, dispatched, running, completed, failed, cancelled - DispatchMode string `json:"dispatchMode,omitempty"` // primary, scheduled, rerun - ScheduleJobID string `json:"scheduleJobId,omitempty"` - TriggerOrigin string `json:"triggerOrigin,omitempty"` // manual, timer, manual_run - Repository RepositoryContext `json:"repository,omitempty"` - CreatedAt time.Time `json:"createdAt"` - UpdatedAt time.Time `json:"updatedAt"` + ID string `json:"id"` + IssueID string `json:"issueId"` + AgentID string `json:"agentId"` + WorkflowID string `json:"workflowId"` + RunID string `json:"runId"` + Status string `json:"status"` // queued, dispatched, running, completed, failed, cancelled + DispatchMode string `json:"dispatchMode,omitempty"` // primary, scheduled, rerun + ScheduleJobID string `json:"scheduleJobId,omitempty"` + // ScheduleExecutionID names the specific ScheduleJobExecution that dispatched + // this task ("sjx-"). Server-side only: the scheduled dispatch path + // sets it from the claimed execution trace; manual dispatch leaves it empty. + ScheduleExecutionID string `json:"scheduleExecutionId,omitempty"` + TriggerOrigin string `json:"triggerOrigin,omitempty"` // manual, timer, manual_run + Repository RepositoryContext `json:"repository,omitempty"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` } diff --git a/internal/engine/run_events.go b/internal/engine/run_events.go index 95041e204..954154409 100644 --- a/internal/engine/run_events.go +++ b/internal/engine/run_events.go @@ -109,6 +109,10 @@ func (e *Engine) IngestWorkerEnvelope(runID string, envelope domain.WorkerEnvelo } log.Context.RunID = runID log.Context.TraceID = runID + // Attribution is server-authoritative: the persisted Run context always + // overwrites worker-reported values, including clearing them for manual + // runs that carry no schedule execution. + log.Context.ScheduleExecutionID = r.ExecutionContext.ScheduleExecutionID if log.Level == "" { log.Level = "info" } diff --git a/internal/engine/schedule_execution_context_test.go b/internal/engine/schedule_execution_context_test.go new file mode 100644 index 000000000..3789f8015 --- /dev/null +++ b/internal/engine/schedule_execution_context_test.go @@ -0,0 +1,111 @@ +package engine + +import ( + "encoding/json" + "testing" + "time" + + "agentflow/internal/domain" + "agentflow/internal/repo" + "agentflow/internal/runtime" +) + +func executionAttributionContext(t *testing.T, id string) domain.TelemetryContext { + t.Helper() + data, err := json.Marshal(map[string]string{"scheduleJobId": "job", "scheduleExecutionId": id}) + if err != nil { + t.Fatal(err) + } + var ctx domain.TelemetryContext + if err := json.Unmarshal(data, &ctx); err != nil { + t.Fatal(err) + } + return ctx +} + +func executionAttributionID(t *testing.T, value any) string { + t.Helper() + data, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + var wire struct { + ID string `json:"scheduleExecutionId"` + } + if err := json.Unmarshal(data, &wire); err != nil { + t.Fatal(err) + } + return wire.ID +} + +func TestScheduledExecutionContextSurvivesChildSOP(t *testing.T) { + st, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + t.Cleanup(st.Close) + child := domain.WorkflowDefinition{ID: "execution-child", Name: "Child", SandboxProfileID: "local", Nodes: []domain.WorkflowNode{{ID: "gate", Type: "approval"}}} + parent := domain.WorkflowDefinition{ID: "execution-parent", Name: "Parent", SandboxProfileID: "local", Nodes: []domain.WorkflowNode{{ID: "sub", Type: "sop", SOPWorkflowID: child.ID, MaxAttempts: 1}}} + for _, wf := range []domain.WorkflowDefinition{child, parent} { + if err := st.PutWorkflow(wf); err != nil { + t.Fatal(err) + } + } + e := New(st, runtime.NewManagerWithLimit(t.TempDir(), 2)) + want := "sjx-occurrence-one" + run, err := e.StartWithRepositoriesContext(parent.ID, "x", nil, executionAttributionContext(t, want)) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = e.Cancel(run.ID) }) + started := waitRun(t, st, run.ID, 5*time.Second, func(r domain.Run) bool { + return r.Nodes["sub"] != nil && r.Nodes["sub"].ChildRunID != "" + }) + childRun := waitRun(t, st, started.Nodes["sub"].ChildRunID, 5*time.Second, func(r domain.Run) bool { return r.Status == "waiting" }) + for _, r := range []domain.Run{started, childRun} { + if got := executionAttributionID(t, r.ExecutionContext); got != want { + t.Errorf("run %s lost schedule execution: %q", r.ID, got) + } + if r.ExecutionContext.TraceID != r.ID { + t.Errorf("Run trace changed: %+v", r.ExecutionContext) + } + } + if childRun.ExecutionContext.ParentRunID != run.ID { + t.Fatal("child did not inherit its parent") + } +} + +func TestScheduledExecutionContextOverridesWorkerClaims(t *testing.T) { + for _, want := range []string{"sjx-authoritative", ""} { + t.Run(want, func(t *testing.T) { + e, st := cvEEngine(t) + run := domain.Run{ID: "attribution-worker", Status: "running", WorkflowID: "wf", ExecutionContext: executionAttributionContext(t, want), Nodes: map[string]*domain.NodeRun{}} + if err := st.PutRun(run); err != nil { + t.Fatal(err) + } + source := domain.TelemetrySource{Layer: "executor", Adapter: "test"} + for _, envelope := range []domain.WorkerEnvelope{ + {SpecVersion: domain.ObservabilitySpecVersion, Type: "observation", Observation: &domain.TelemetryEvent{Name: "worker.example", Source: source, Context: executionAttributionContext(t, "sjx-forged")}}, + {SpecVersion: domain.ObservabilitySpecVersion, Type: "log", Log: &domain.LogRecord{Body: "worker example", Source: source, Context: executionAttributionContext(t, "sjx-forged")}}, + } { + if err := e.IngestWorkerEnvelope(run.ID, envelope); err != nil { + t.Fatal(err) + } + } + got, _ := st.Run(run.ID) + if len(got.Telemetry) == 0 || len(got.Logs) == 0 { + t.Fatal("worker evidence was not persisted") + } + for _, event := range got.Telemetry { + if id := executionAttributionID(t, event.Context); id != want { + t.Errorf("telemetry attribution = %q, want %q", id, want) + } + } + for _, log := range got.Logs { + if id := executionAttributionID(t, log.Context); id != want { + t.Errorf("log attribution = %q, want %q", id, want) + } + } + }) + } +} diff --git a/internal/httpapi/dispatch.go b/internal/httpapi/dispatch.go index 6b1385188..e695549b4 100644 --- a/internal/httpapi/dispatch.go +++ b/internal/httpapi/dispatch.go @@ -14,8 +14,11 @@ import ( type dispatchRequest struct { IssueID, WorkflowID, ScheduleJobID, TriggerOrigin string - Rerun bool - TriggerUserID int64 + // ScheduleExecutionID is server-derived attribution for scheduled + // dispatches only. Public manual dispatch never sets it. + ScheduleExecutionID string + Rerun bool + TriggerUserID int64 } func dispatchIssue(d *deps, req dispatchRequest) (domain.Task, error) { @@ -188,7 +191,7 @@ func dispatchIssue(d *deps, req dispatchRequest) (domain.Task, error) { if len(repositories) > 0 { displayRepository = repositories[0] } - task := domain.Task{ID: taskID, IssueID: issue.ID, AgentID: issue.AssigneeID, WorkflowID: workflowID, Status: "queued", Repository: displayRepository, DispatchMode: dispatchMode, ScheduleJobID: strings.TrimSpace(req.ScheduleJobID), TriggerOrigin: triggerOrigin} + task := domain.Task{ID: taskID, IssueID: issue.ID, AgentID: issue.AssigneeID, WorkflowID: workflowID, Status: "queued", Repository: displayRepository, DispatchMode: dispatchMode, ScheduleJobID: strings.TrimSpace(req.ScheduleJobID), ScheduleExecutionID: req.ScheduleExecutionID, TriggerOrigin: triggerOrigin} stored, created, err := d.repo.ReserveIssueDispatch(task) if err != nil { if errors.Is(err, repo.ErrIssueNotFound) { @@ -210,7 +213,7 @@ func dispatchIssue(d *deps, req dispatchRequest) (domain.Task, error) { return domain.Task{}, err } tapdID := issue.RequirementTAPDID() - run, err := d.engine.StartWithRepositoriesContext(workflowID, input, repositories, domain.TelemetryContext{ProjectID: issue.ProjectID, IssueID: issue.ID, TAPDID: tapdID, ResponsibleUserID: runUserID, IssueNumber: issue.Number, TaskID: taskID, ScheduleJobID: strings.TrimSpace(req.ScheduleJobID)}) + run, err := d.engine.StartWithRepositoriesContext(workflowID, input, repositories, domain.TelemetryContext{ProjectID: issue.ProjectID, IssueID: issue.ID, TAPDID: tapdID, ResponsibleUserID: runUserID, IssueNumber: issue.Number, TaskID: taskID, ScheduleJobID: strings.TrimSpace(req.ScheduleJobID), ScheduleExecutionID: req.ScheduleExecutionID}) if err != nil { task.Status = "failed" _ = d.repo.PutTask(task) diff --git a/internal/httpapi/schedule.go b/internal/httpapi/schedule.go index c12274021..86e02af09 100644 --- a/internal/httpapi/schedule.go +++ b/internal/httpapi/schedule.go @@ -417,7 +417,14 @@ func (h *scheduleHandlers) dispatchScheduled(job domain.ScheduleJob, issue domai // dispatchScheduledRun is dispatchScheduled reporting the started run ID for // the execution record. func (h *scheduleHandlers) dispatchScheduledRun(job domain.ScheduleJob, issue domain.Issue, triggerOrigin string) (string, error) { - task, err := dispatchIssue(h.deps, dispatchRequest{IssueID: issue.ID, WorkflowID: job.ResolveWorkflow(issue), ScheduleJobID: job.ID, TriggerOrigin: triggerOrigin}) + // The claimed execution's trace is the only source of attribution. An empty + // trace (a direct dispatch without a claimed execution) never fabricates an + // "sjx-" ID. + scheduleExecutionID := "" + if strings.TrimSpace(job.LastTraceID) != "" { + scheduleExecutionID = domain.ScheduleJobExecutionID(job.LastTraceID) + } + task, err := dispatchIssue(h.deps, dispatchRequest{IssueID: issue.ID, WorkflowID: job.ResolveWorkflow(issue), ScheduleJobID: job.ID, TriggerOrigin: triggerOrigin, ScheduleExecutionID: scheduleExecutionID}) return task.RunID, err } diff --git a/internal/httpapi/schedule_execution_attribution_test.go b/internal/httpapi/schedule_execution_attribution_test.go new file mode 100644 index 000000000..f9fe3cb9b --- /dev/null +++ b/internal/httpapi/schedule_execution_attribution_test.go @@ -0,0 +1,121 @@ +package httpapi + +import ( + "encoding/json" + "path/filepath" + "testing" + + "agentflow/internal/domain" + "agentflow/internal/engine" + "agentflow/internal/repo" + "agentflow/internal/runtime" + "agentflow/internal/schedule" + "agentflow/internal/tracker" +) + +func scheduledExecutionIDOnWire(t *testing.T, value any) string { + t.Helper() + data, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + var fields struct { + ID string `json:"scheduleExecutionId"` + } + if err := json.Unmarshal(data, &fields); err != nil { + t.Fatal(err) + } + return fields.ID +} + +func TestScheduleExecutionAttributionSeparatesOccurrences(t *testing.T) { + st, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + t.Cleanup(st.Close) + wf := domain.WorkflowDefinition{ID: "execution-attribution", Name: "Attribution", SandboxProfileID: "local", Nodes: []domain.WorkflowNode{{ID: "gate", Type: "approval"}}} + if err := st.PutWorkflow(wf); err != nil { + t.Fatal(err) + } + e := engine.New(st, runtime.NewManager(filepath.Join(t.TempDir(), "workspaces"))) + h := &scheduleHandlers{deps: &deps{repo: st, engine: e}} + svc := schedule.New(st, tracker.NewService(), nil) + svc.SetRunDispatcher(h.dispatchScheduledRun) + job := domain.ScheduleJob{ID: "attribution-job", Name: "same job", ProjectID: "platform", WorkflowID: wf.ID, Enabled: true, MaxDispatch: 1} + if err := st.PutScheduleJob(job); err != nil { + t.Fatal(err) + } + var firstTask domain.Task + var executionIDs []string + // The scheduler excludes issues that already have a task. Two occurrences + // therefore dispatch different issues, and must retain distinct attribution. + for _, issueID := range []string{"occurrence-one", "occurrence-two"} { + issue := domain.Issue{ID: issueID, ProjectID: "platform", Title: issueID, Status: "todo", WorkflowID: wf.ID} + if err := st.PutIssue(issue); err != nil { + t.Fatal(err) + } + finished, err := svc.RunNow(job.ID) + if err != nil || finished.LastDispatched != 1 { + t.Fatalf("schedule did not dispatch exactly once: %+v, %v", finished, err) + } + want := domain.ScheduleJobExecutionID(finished.LastTraceID) + executionIDs = append(executionIDs, want) + execution, ok := st.ScheduleJobExecution(want) + if !ok || len(execution.Runs) != 1 { + t.Fatalf("execution missing its downstream run: %+v", execution) + } + task, ok := st.Task(primaryTaskID(issueID, wf.ID)) + if !ok || task.RunID != execution.Runs[0].RunID { + t.Fatalf("task/run link is missing: %+v", task) + } + t.Cleanup(func() { _ = e.Cancel(task.RunID) }) + run, ok := st.RunState(task.RunID) + if !ok || run.ExecutionContext.TraceID != run.ID { + t.Fatalf("existing Run trace contract changed: %+v", run.ExecutionContext) + } + if got := scheduledExecutionIDOnWire(t, task); got != want { + t.Errorf("task execution = %q, want %q", got, want) + } + if got := scheduledExecutionIDOnWire(t, run.ExecutionContext); got != want { + t.Errorf("run execution = %q, want %q", got, want) + } + if firstTask.ID == "" { + firstTask = task + } + } + if executionIDs[0] == executionIDs[1] { + t.Fatal("separate occurrences reused the same execution") + } + retained, _ := st.Task(firstTask.ID) + if got := scheduledExecutionIDOnWire(t, retained); got != executionIDs[0] { + t.Errorf("second occurrence rewrote the first task's attribution: %q", got) + } + for _, legacy := range []bool{false, true} { + id := "manual-no-attribution" + if legacy { + id = "legacy-no-trace" + } + issue := domain.Issue{ID: id, ProjectID: "platform", Title: id, Status: "todo", WorkflowID: wf.ID} + if err := st.PutIssue(issue); err != nil { + t.Fatal(err) + } + var runID string + if legacy { + runID, err = h.dispatchScheduledRun(job, issue, "timer") + } else { + var task domain.Task + task, err = dispatchIssue(h.deps, dispatchRequest{IssueID: id, WorkflowID: wf.ID}) + runID = task.RunID + } + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = e.Cancel(runID) }) + task, _ := st.Task(primaryTaskID(id, wf.ID)) + run, _ := st.RunState(runID) + if scheduledExecutionIDOnWire(t, task) != "" || scheduledExecutionIDOnWire(t, run.ExecutionContext) != "" { + t.Fatal("manual or legacy dispatch fabricated an execution ID") + } + } +}