summaryrefslogtreecommitdiff
path: root/internal/api/stories.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/api/stories.go')
-rw-r--r--internal/api/stories.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/internal/api/stories.go b/internal/api/stories.go
index 337da21..b93f8c3 100644
--- a/internal/api/stories.go
+++ b/internal/api/stories.go
@@ -3,8 +3,10 @@ package api
import (
"encoding/json"
"net/http"
+ "strconv"
"github.com/google/uuid"
+ "github.com/thepeterstone/claudomator/internal/event"
"github.com/thepeterstone/claudomator/internal/storage"
"github.com/thepeterstone/claudomator/internal/story"
)
@@ -168,3 +170,82 @@ func (s *Server) handleStoryTaskTree(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, resp)
}
+
+// handleListStoryEvents returns a story's observability event stream in seq
+// order. Mirrors handleListTaskEvents exactly (same ?since_seq=N incremental
+// polling contract), but resolves existence via GetStory rather than
+// GetTask: the internal/scheduler.StoryOrchestrator (Phase 7b) attaches
+// KindEvalVerdict/KindArbitrationDecided/KindHumanAccepted events to the
+// story's ID rather than to any single task's ID (see that package's doc
+// comments for why) — events.task_id has no enforced FK, so this is exactly
+// the tolerance 7a's event.Kind doc comments anticipated ("a later phase's
+// orchestration logic writes them against a story or epic ID").
+func (s *Server) handleListStoryEvents(w http.ResponseWriter, r *http.Request) {
+ id := r.PathValue("id")
+ if _, err := s.store.GetStory(id); err != nil {
+ writeJSON(w, http.StatusNotFound, map[string]string{"error": "story not found"})
+ return
+ }
+
+ var sinceSeq int64
+ if v := r.URL.Query().Get("since_seq"); v != "" {
+ n, err := strconv.ParseInt(v, 10, 64)
+ if err != nil || n < 0 {
+ writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid since_seq"})
+ return
+ }
+ sinceSeq = n
+ }
+
+ events, err := s.store.ListEvents(id, sinceSeq)
+ if err != nil {
+ writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
+ return
+ }
+ if events == nil {
+ events = []*event.Event{}
+ }
+ writeJSON(w, http.StatusOK, events)
+}
+
+// handleAcceptStory is the human accept-gate for a story: it mirrors
+// handleAcceptTask's pattern exactly (validate current state, transition,
+// emit an event) but operates on story.Story, which (unlike task.Task) has
+// no enforced state machine (story.UpdateStory is unvalidated pass-through —
+// see internal/story's doc comments) — so the "only valid from REVIEW_READY"
+// check is done here, not in the storage layer.
+func (s *Server) handleAcceptStory(w http.ResponseWriter, r *http.Request) {
+ id := r.PathValue("id")
+ st, err := s.store.GetStory(id)
+ if err != nil {
+ writeJSON(w, http.StatusNotFound, map[string]string{"error": "story not found"})
+ return
+ }
+ if st.Status != "REVIEW_READY" {
+ writeJSON(w, http.StatusConflict, map[string]string{"error": "story cannot be accepted from status " + st.Status})
+ return
+ }
+
+ from := st.Status
+ st.Status = "DONE"
+ if err := s.store.UpdateStory(st); err != nil {
+ writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
+ return
+ }
+
+ payload, _ := json.Marshal(struct {
+ StoryID string `json:"story_id"`
+ From string `json:"from"`
+ To string `json:"to"`
+ }{StoryID: id, From: from, To: "DONE"})
+ if err := s.store.CreateEvent(&event.Event{
+ TaskID: id,
+ Kind: event.KindHumanAccepted,
+ Actor: event.ActorUser,
+ Payload: payload,
+ }); err != nil {
+ s.logger.Error("failed to record human_accepted event", "storyID", id, "error", err)
+ }
+
+ writeJSON(w, http.StatusOK, map[string]string{"message": "story accepted", "story_id": id})
+}