#2: approval pauses narrate from ApprovalRequestedEvent (carries tool/tier/preview/stage directly) instead of OrchestrationPaused + a pending-approvals map the next event had not yet populated — the narrator always lost that race and produced generic text. #3: NarrationTrigger now carries the triggering event's stageId; the narration status line and RouterNarrationEvent.stageId use it instead of the router projection's currentStageId, which terminal events have already cleared ('Stage: none'). #9: ExecutionPlanLocked -> plan.locked and ArtifactValidated -> artifact.validated mapped to the TUI (Go: feed lines, plan shows the compiled stage chain); ArtifactValidating explicitly dropped. #10: blank router turn guard — a length-finish with empty text now emits an explanatory turn naming the max_tokens budget instead of a silent blank; blank steering responses no longer emit steering notes. #12: resumeAbandoned only auto-resumes sessions whose last event is within orchestration.resume_abandoned_max_age_minutes (default 24h, 0 disables); staler sessions stay parked for POST /resume. Knob is TUI-editable and persisted.
This commit is contained in:
@@ -34,6 +34,8 @@ import com.correx.core.sessions.SessionSummaryProjector
|
||||
import com.correx.core.tools.registry.ToolRegistry
|
||||
import com.correx.core.events.orchestration.OrchestrationStatus
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.DateTimeUnit
|
||||
import kotlinx.datetime.minus
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -348,6 +350,12 @@ class ServerModule(
|
||||
}
|
||||
|
||||
private fun resumeAbandonedSessions() {
|
||||
val maxAgeMinutes = configHolder.get().orchestration.resumeAbandonedMaxAgeMinutes
|
||||
if (maxAgeMinutes <= 0) {
|
||||
log.info("resumeAbandoned: disabled (resume_abandoned_max_age_minutes={})", maxAgeMinutes)
|
||||
return
|
||||
}
|
||||
val cutoff = Clock.System.now().minus(maxAgeMinutes, DateTimeUnit.MINUTE)
|
||||
eventStore.allSessionIds().forEach { sessionId ->
|
||||
val orchState = orchestrationRepository.getState(sessionId)
|
||||
// Skip PAUSED sessions — they are waiting for user approval, not for computation.
|
||||
@@ -355,6 +363,14 @@ class ServerModule(
|
||||
// approves, submitApprovalDecision emits OrchestrationResumedEvent which triggers
|
||||
// the subscription above to continue the session.
|
||||
if (orchState.status != OrchestrationStatus.RUNNING) return@forEach
|
||||
val lastEventAt = eventStore.read(sessionId).lastOrNull()?.metadata?.timestamp ?: return@forEach
|
||||
if (lastEventAt < cutoff) {
|
||||
log.info(
|
||||
"resumeAbandoned: skipping stale session={} (last event {}, cutoff {}m) — POST /resume revives it",
|
||||
sessionId.value, lastEventAt, maxAgeMinutes,
|
||||
)
|
||||
return@forEach
|
||||
}
|
||||
val graph = workflowRegistry.find(orchState.workflowId) ?: run {
|
||||
log.warn("resumeAbandoned: no graph for session={} workflowId={}", sessionId.value, orchState.workflowId)
|
||||
return@forEach
|
||||
|
||||
@@ -10,6 +10,9 @@ import com.correx.core.events.events.ApprovalDecisionResolvedEvent
|
||||
import com.correx.core.events.events.ApprovalRequestedEvent
|
||||
import com.correx.core.events.events.ArtifactContentStoredEvent
|
||||
import com.correx.core.events.events.ArtifactCreatedEvent
|
||||
import com.correx.core.events.events.ArtifactValidatedEvent
|
||||
import com.correx.core.events.events.ArtifactValidatingEvent
|
||||
import com.correx.core.events.events.ExecutionPlanLockedEvent
|
||||
import com.correx.core.events.events.ChatSessionStartedEvent
|
||||
import com.correx.core.events.events.ChatTurnEvent
|
||||
import com.correx.core.events.events.InferenceCompletedEvent
|
||||
@@ -243,6 +246,23 @@ suspend fun domainEventToServerMessage(
|
||||
sequence = seq,
|
||||
sessionSequence = sessionSequence,
|
||||
)
|
||||
is ArtifactValidatedEvent -> ServerMessage.ArtifactValidated(
|
||||
sessionId = p.sessionId,
|
||||
stageId = p.stageId,
|
||||
artifactId = p.artifactId,
|
||||
sequence = seq,
|
||||
sessionSequence = sessionSequence,
|
||||
)
|
||||
// Transient pre-validation marker, emitted microseconds before Validated or a
|
||||
// stage failure — either of those carries the outcome the operator cares about.
|
||||
is ArtifactValidatingEvent -> null
|
||||
is ExecutionPlanLockedEvent -> ServerMessage.PlanLocked(
|
||||
sessionId = p.sessionId,
|
||||
workflowId = p.workflowId,
|
||||
stageIds = p.stageIds,
|
||||
sequence = seq,
|
||||
sessionSequence = sessionSequence,
|
||||
)
|
||||
is ModelLoadedEvent -> ServerMessage.ModelChanged(
|
||||
modelId = p.modelId,
|
||||
providerId = p.providerId.value,
|
||||
|
||||
+56
-28
@@ -30,12 +30,7 @@ class NarrationSubscriber(
|
||||
) {
|
||||
private data class SessionLane(val channel: Channel<NarrationTrigger>, var used: Int)
|
||||
|
||||
// Latest approval awaiting a decision per session, so a pause narration can name the
|
||||
// tool and show the proposed change instead of the bare "APPROVAL_PENDING" reason.
|
||||
private data class PendingApproval(val toolName: String, val tier: String, val preview: String)
|
||||
|
||||
private val lanes = ConcurrentHashMap<String, SessionLane>()
|
||||
private val pendingApprovals = ConcurrentHashMap<String, PendingApproval>()
|
||||
|
||||
fun start() {
|
||||
eventStore.subscribeAll().onEach { handle(it) }.launchIn(scope)
|
||||
@@ -45,51 +40,84 @@ class NarrationSubscriber(
|
||||
val sid = event.metadata.sessionId
|
||||
when (val p = event.payload) {
|
||||
is WorkflowStartedEvent -> lanes[sid.value]?.let { it.used = 0 }
|
||||
is ApprovalRequestedEvent -> pendingApprovals[sid.value] = PendingApproval(
|
||||
toolName = p.toolName ?: "a tool",
|
||||
tier = p.tier.toString(),
|
||||
preview = p.preview.orEmpty(),
|
||||
// Approval pauses narrate off this event, not OrchestrationPaused: it carries the
|
||||
// tool/tier/preview facts directly, where the paused event forced a racy lookup
|
||||
// against state the next event had not yet written.
|
||||
is ApprovalRequestedEvent -> enqueue(
|
||||
sid,
|
||||
NarrationTrigger(
|
||||
kind = "paused",
|
||||
instruction = approvalInstruction(p),
|
||||
stageId = p.stageId?.value,
|
||||
),
|
||||
)
|
||||
is StageCompletedEvent -> enqueue(
|
||||
sid,
|
||||
NarrationTrigger(kind = "stage_completed", instruction = "Stage ${p.stageId.value} completed. Summarise what was accomplished."),
|
||||
NarrationTrigger(
|
||||
kind = "stage_completed",
|
||||
instruction = "Stage ${p.stageId.value} completed. Summarise what was accomplished.",
|
||||
stageId = p.stageId.value,
|
||||
),
|
||||
)
|
||||
is StageFailedEvent -> enqueue(
|
||||
sid,
|
||||
NarrationTrigger(kind = "stage_failed", instruction = "Stage ${p.stageId.value} failed: ${p.reason}. Explain what went wrong."),
|
||||
NarrationTrigger(
|
||||
kind = "stage_failed",
|
||||
instruction = "Stage ${p.stageId.value} failed: ${p.reason}. Explain what went wrong.",
|
||||
stageId = p.stageId.value,
|
||||
),
|
||||
)
|
||||
is WorkflowCompletedEvent -> enqueue(
|
||||
sid,
|
||||
NarrationTrigger(kind = "workflow_completed", instruction = "The workflow finished successfully. Provide a brief summary."),
|
||||
NarrationTrigger(
|
||||
kind = "workflow_completed",
|
||||
instruction = "The workflow finished successfully. Provide a brief summary.",
|
||||
stageId = p.terminalStageId.value,
|
||||
),
|
||||
)
|
||||
is WorkflowFailedEvent -> enqueue(
|
||||
sid,
|
||||
NarrationTrigger(kind = "workflow_failed", instruction = "The workflow failed: ${p.reason}. Explain the failure to the user."),
|
||||
NarrationTrigger(
|
||||
kind = "workflow_failed",
|
||||
instruction = "The workflow failed in stage ${p.stageId.value}: ${p.reason}. Explain the failure to the user.",
|
||||
stageId = p.stageId.value,
|
||||
),
|
||||
)
|
||||
is ExecutionPlanRejectedEvent -> enqueue(
|
||||
sid,
|
||||
NarrationTrigger(kind = "plan_rejected", instruction = "The execution plan was rejected (${p.source}): ${p.reason}. Explain to the user what happened and what they can do next."),
|
||||
)
|
||||
is OrchestrationPausedEvent -> enqueue(
|
||||
sid,
|
||||
NarrationTrigger(kind = "paused", instruction = pauseInstruction(sid, p)),
|
||||
NarrationTrigger(
|
||||
kind = "plan_rejected",
|
||||
instruction = "The execution plan was rejected (${p.source}): ${p.reason}. Explain to the user what happened and what they can do next.",
|
||||
),
|
||||
)
|
||||
is OrchestrationPausedEvent -> {
|
||||
// Approval pauses are narrated from ApprovalRequestedEvent above.
|
||||
if (!p.reason.contains("APPROVAL", ignoreCase = true)) {
|
||||
enqueue(
|
||||
sid,
|
||||
NarrationTrigger(
|
||||
kind = "paused",
|
||||
instruction = "Orchestration paused in stage ${p.stageId.value}: ${p.reason}. Inform the user.",
|
||||
stageId = p.stageId.value,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
private fun pauseInstruction(sessionId: SessionId, event: OrchestrationPausedEvent): String {
|
||||
val pending = pendingApprovals[sessionId.value]
|
||||
if (!event.reason.contains("APPROVAL", ignoreCase = true) || pending == null) {
|
||||
return "Orchestration paused in stage ${event.stageId.value}: ${event.reason}. Inform the user."
|
||||
}
|
||||
private fun approvalInstruction(event: ApprovalRequestedEvent): String {
|
||||
val stage = event.stageId?.value ?: "the current stage"
|
||||
val subject = event.toolName?.let { "of the '$it' tool (tier ${event.tier})" }
|
||||
?: "(tier ${event.tier})"
|
||||
return buildString {
|
||||
append("The workflow paused in stage ${event.stageId.value} awaiting your approval of the ")
|
||||
append("'${pending.toolName}' tool (tier ${pending.tier}). ")
|
||||
append("Tell the operator what it is about to do and why it needs sign-off.")
|
||||
if (pending.preview.isNotBlank()) {
|
||||
append("The workflow paused in stage $stage awaiting your approval $subject. ")
|
||||
append("Tell the operator what is proposed and why it needs sign-off.")
|
||||
val preview = event.preview.orEmpty()
|
||||
if (preview.isNotBlank()) {
|
||||
append("\n\nProposed change:\n")
|
||||
append(pending.preview.take(MAX_PREVIEW_CHARS))
|
||||
append(preview.take(MAX_PREVIEW_CHARS))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -391,6 +391,27 @@ sealed interface ServerMessage {
|
||||
override val sessionSequence: Long,
|
||||
) : ServerMessage, SessionMessage
|
||||
|
||||
@Serializable
|
||||
@SerialName("artifact.validated")
|
||||
data class ArtifactValidated(
|
||||
val sessionId: SessionId,
|
||||
val stageId: StageId,
|
||||
val artifactId: ArtifactId,
|
||||
override val sequence: Long,
|
||||
override val sessionSequence: Long,
|
||||
) : ServerMessage, SessionMessage
|
||||
|
||||
/** A freestyle execution plan was locked; phase 2 runs [stageIds] as workflow [workflowId]. */
|
||||
@Serializable
|
||||
@SerialName("plan.locked")
|
||||
data class PlanLocked(
|
||||
val sessionId: SessionId,
|
||||
val workflowId: String,
|
||||
val stageIds: List<String>,
|
||||
override val sequence: Long,
|
||||
override val sessionSequence: Long,
|
||||
) : ServerMessage, SessionMessage
|
||||
|
||||
/**
|
||||
* Full artifact listing for a session, returned in response to a ListArtifacts request.
|
||||
* Not event-derived (it is a snapshot read), so cursors are null.
|
||||
|
||||
@@ -227,6 +227,15 @@ func (m *Model) applyServer(msg protocol.ServerMessage) {
|
||||
if s := m.session(msg.SessionID); s != nil {
|
||||
s.addEvent(nowMillis(), "ArtifactCreated", msg.ArtifactID)
|
||||
}
|
||||
case protocol.TypeArtifactValid:
|
||||
if s := m.session(msg.SessionID); s != nil {
|
||||
s.addEvent(nowMillis(), "ArtifactValidated", msg.ArtifactID)
|
||||
}
|
||||
case protocol.TypePlanLocked:
|
||||
if s := m.session(msg.SessionID); s != nil {
|
||||
s.addEvent(nowMillis(), "PlanLocked", strings.Join(msg.StageIDs, " → "))
|
||||
s.LastOutput = "plan locked: " + strings.Join(msg.StageIDs, " → ")
|
||||
}
|
||||
case protocol.TypeWorkspaceBound:
|
||||
if s := m.session(msg.SessionID); s != nil {
|
||||
s.WorkspaceRoot = msg.WorkspaceRoot
|
||||
|
||||
@@ -39,6 +39,8 @@ const (
|
||||
TypeProviderStatus = "provider.status_changed"
|
||||
TypeProtocolError = "protocol_error"
|
||||
TypeArtifactCreated = "artifact.created"
|
||||
TypeArtifactValid = "artifact.validated"
|
||||
TypePlanLocked = "plan.locked"
|
||||
TypeRouterResponse = "router.response"
|
||||
TypeSnapshotComplete = "snapshot_complete"
|
||||
TypeWorkflowList = "workflow.list"
|
||||
@@ -81,6 +83,9 @@ type ServerMessage struct {
|
||||
Outcome string `json:"outcome"` // approval.resolved: APPROVED | REJECTED | AUTO_APPROVED
|
||||
WorkspaceRoot string `json:"workspaceRoot"` // session.workspace_bound: the bound cwd
|
||||
|
||||
// plan.locked: the compiled phase-2 stage ids in execution order
|
||||
StageIDs []string `json:"stageIds"`
|
||||
|
||||
// inference.retry
|
||||
AttemptNumber int `json:"attemptNumber"`
|
||||
MaxAttempts int `json:"maxAttempts"`
|
||||
@@ -224,6 +229,7 @@ func (m ServerMessage) IsEventBearing() bool {
|
||||
TypeToolStarted, TypeToolCompleted, TypeToolFailed, TypeToolRejected,
|
||||
TypeToolAssessed,
|
||||
TypeApprovalRequired, TypeApprovalResolved, TypeWorkspaceBound, TypeArtifactCreated,
|
||||
TypeArtifactValid, TypePlanLocked,
|
||||
TypeRouterNarration:
|
||||
return true
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user