Browse Source

fix: return a waited-for run's result even after it leaves the log history

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 1 day ago
parent
commit
b1e711ec60

+ 2 - 1
service/internal/api/api.go

@@ -265,7 +265,8 @@ func (api *oliveTinAPI) startActionAndWaitRun(binding *executor.ActionBinding, a
 	}
 	}
 	wg, _ := api.executor.ExecRequest(&execReq)
 	wg, _ := api.executor.ExecRequest(&execReq)
 	wg.Wait()
 	wg.Wait()
-	return api.executor.GetLog(execReq.TrackingID)
+	entry := execReq.LogEntry()
+	return entry, entry != nil
 }
 }
 
 
 func (api *oliveTinAPI) findBindingOrNotFound(actionId string) (*executor.ActionBinding, error) {
 func (api *oliveTinAPI) findBindingOrNotFound(actionId string) (*executor.ActionBinding, error) {

+ 14 - 0
service/internal/executor/executor.go

@@ -96,6 +96,20 @@ func (req *ExecutionRequest) mutateLogEntry(mutator func(*InternalLogEntry)) {
 	mutator(req.logEntry)
 	mutator(req.logEntry)
 }
 }
 
 
+// LogEntry returns a copy of this request's own log entry. Callers that wait
+// for a request use it instead of looking the entry up in the history, because
+// a later run can evict a finished entry from the history before it is read.
+func (req *ExecutionRequest) LogEntry() *InternalLogEntry {
+	if req.executor == nil {
+		return cloneInternalLogEntry(req.logEntry)
+	}
+
+	req.executor.logmutex.RLock()
+	defer req.executor.logmutex.RUnlock()
+
+	return cloneInternalLogEntry(req.logEntry)
+}
+
 // LogEntrySnapshot is a copy of selected log entry fields for race-safe reads.
 // LogEntrySnapshot is a copy of selected log entry fields for race-safe reads.
 type LogEntrySnapshot struct {
 type LogEntrySnapshot struct {
 	Output            string
 	Output            string

+ 25 - 0
service/internal/executor/log_retention_test.go

@@ -178,3 +178,28 @@ func TestLogHistoryLoadedFromDiskIsBounded(t *testing.T) {
 	e.logmutex.RUnlock()
 	e.logmutex.RUnlock()
 	assertLogStoresConsistent(t, e, binding.ID)
 	assertLogStoresConsistent(t, e, binding.ID)
 }
 }
+
+func TestRequestLogEntrySurvivesHistoryEviction(t *testing.T) {
+	e, cfg, binding := newRetentionTestExecutor(t, &config.Action{Title: "retention", Shell: "true"})
+
+	req := &ExecutionRequest{
+		Binding:           binding,
+		Cfg:               cfg,
+		AuthenticatedUser: auth.UserFromSystem(cfg, "test"),
+	}
+	wg, trackingID := e.ExecRequest(req)
+	wg.Wait()
+
+	for range cfg.LogHistoryLimit + 1 {
+		addRetentionTestLog(e, newQueueTestLogEntry(binding, true))
+	}
+
+	_, found := e.GetLog(trackingID)
+	require.False(t, found, "the request's entry is evicted from the history")
+
+	entry := req.LogEntry()
+	require.NotNil(t, entry)
+	assert.Equal(t, trackingID, entry.ExecutionTrackingID)
+	assert.True(t, entry.ExecutionFinished)
+	assert.Equal(t, int32(0), entry.ExitCode)
+}