@@ -32,7 +32,6 @@ import (
3232 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3333 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3434 "k8s.io/client-go/kubernetes"
35- "k8s.io/client-go/util/retry"
3635)
3736
3837const (
@@ -448,22 +447,8 @@ func persistStreamedEvent(sessionID, runID, threadID, jsonData string) {
448447 }
449448 }
450449
451- // Update agentStatus on CR for status-changing events (not debounced).
452- if projectName , ok := sessionProjectMap .Load (sessionID ); ok {
453- proj := projectName .(string )
454- switch eventType {
455- case types .EventTypeRunStarted :
456- updateAgentStatus (proj , sessionID , "working" )
457- case types .EventTypeRunFinished :
458- updateAgentStatus (proj , sessionID , "idle" )
459- case types .EventTypeRunError :
460- updateAgentStatus (proj , sessionID , "idle" )
461- case types .EventTypeToolCallStart :
462- if toolName , _ := event ["toolCallName" ].(string ); isAskUserQuestionToolCall (toolName ) {
463- updateAgentStatus (proj , sessionID , "waiting_input" )
464- }
465- }
466- }
450+ // agentStatus is derived at query time from the event log (DeriveAgentStatus).
451+ // No CR updates needed here — the persisted events ARE the source of truth.
467452}
468453
469454// ─── POST /agui/interrupt ────────────────────────────────────────────
@@ -978,75 +963,3 @@ func isAskUserQuestionToolCall(name string) bool {
978963 return clean .String () == "askuserquestion"
979964}
980965
981- // updateAgentStatus updates the agentStatus field on the AgenticSession CR status.
982- // Unlike updateLastActivityTime, this is NOT debounced because status changes are
983- // infrequent (2-4 per run) and should be reflected immediately.
984- // It also updates lastActivityTime in the same CR write to avoid double API calls.
985- func updateAgentStatus (projectName , sessionName , newStatus string ) {
986- if handlers .DynamicClient == nil {
987- log .Printf ("Agent status: DynamicClient is nil, skipping update for %s/%s" , projectName , sessionName )
988- return
989- }
990-
991- // Bound concurrency: reuse the activity semaphore.
992- select {
993- case activityUpdateSem <- struct {}{}:
994- default :
995- log .Printf ("Agent status: semaphore full, dropping update %s for %s/%s" , newStatus , projectName , sessionName )
996- return
997- }
998-
999- go func () {
1000- defer func () { <- activityUpdateSem }()
1001-
1002- gvr := handlers .GetAgenticSessionV1Alpha1Resource ()
1003- ctx , cancel := context .WithTimeout (context .Background (), activityUpdateTimeout )
1004- defer cancel ()
1005-
1006- var now time.Time
1007- err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
1008- obj , err := handlers .DynamicClient .Resource (gvr ).Namespace (projectName ).Get (ctx , sessionName , metav1.GetOptions {})
1009- if err != nil {
1010- return err
1011- }
1012-
1013- status , found , err := unstructured .NestedMap (obj .Object , "status" )
1014- if err != nil {
1015- log .Printf ("Agent status: failed to read nested status for %s/%s: %v" , projectName , sessionName , err )
1016- return nil // non-retryable
1017- }
1018- if ! found || status == nil {
1019- status = make (map [string ]any )
1020- }
1021-
1022- // Skip RUN_FINISHED → "idle" if current status is "waiting_input".
1023- // waiting_input is more informative and should persist until the next RUN_STARTED.
1024- if newStatus == "idle" {
1025- if current , _ := status ["agentStatus" ].(string ); current == "waiting_input" {
1026- return nil
1027- }
1028- }
1029-
1030- status ["agentStatus" ] = newStatus
1031- // Also update lastActivityTime to avoid a separate API call.
1032- now = time .Now ()
1033- status ["lastActivityTime" ] = now .UTC ().Format (time .RFC3339 )
1034-
1035- if err := unstructured .SetNestedField (obj .Object , status , "status" ); err != nil {
1036- log .Printf ("Agent status: failed to set status for %s/%s: %v" , projectName , sessionName , err )
1037- return nil // non-retryable
1038- }
1039-
1040- _ , err = handlers .DynamicClient .Resource (gvr ).Namespace (projectName ).UpdateStatus (ctx , obj , metav1.UpdateOptions {})
1041- return err
1042- })
1043-
1044- if err != nil {
1045- log .Printf ("Agent status: failed to update agentStatus to %s for %s/%s: %v" , newStatus , projectName , sessionName , err )
1046- } else {
1047- // Update lastActivity timestamp in the debounce map to avoid redundant activity updates.
1048- key := projectName + "/" + sessionName
1049- lastActivityUpdateTimes .Store (key , now )
1050- }
1051- }()
1052- }
0 commit comments