Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4824,6 +4824,10 @@ components:
enum: [primary, scheduled, rerun]
scheduleJobId:
type: string
scheduleExecutionId:
type: string
readOnly: true
description: Schedule execution identifier ("sjx-<trace>") 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:
Expand Down
10 changes: 7 additions & 3 deletions internal/domain/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-<trace>"). 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.
Expand Down
28 changes: 16 additions & 12 deletions internal/domain/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-<trace>"). 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"`
}
4 changes: 4 additions & 0 deletions internal/engine/run_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
111 changes: 111 additions & 0 deletions internal/engine/schedule_execution_context_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
})
}
}
11 changes: 7 additions & 4 deletions internal/httpapi/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion internal/httpapi/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
121 changes: 121 additions & 0 deletions internal/httpapi/schedule_execution_attribution_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}