package com.correx.apps.server.tasks

import com.correx.core.events.types.SessionId
import com.correx.core.kernel.orchestration.ReadyTaskCounter
import com.correx.core.tasks.TaskService

/**
 * [ReadyTaskCounter] over the task projection. Resolves the session to the project its tasks were
 * actually filed under ([TaskService.projectForSession] — via the CONTEXT/SESSION link the task
 * tools record) and counts the ready tasks there, so the loop predicate agrees with the claim stage
 * regardless of the free-form `project` key the agent chose. Pure read over event-derived state.
 */
class ProjectReadyTaskCounter(
    private val taskService: TaskService,
) : ReadyTaskCounter {
    override fun count(sessionId: SessionId): Int =
        taskService.projectForSession(sessionId.value)?.let { taskService.ready(it).size } ?: 0
}
