Fix consumed instruction ordering and clear history

This commit is contained in:
Brandon Zhang
2026-03-27 18:32:18 +08:00
parent 27592f2750
commit 07e31ce215
3 changed files with 30 additions and 11 deletions

View File

@@ -106,14 +106,21 @@ func handleDeleteInstruction(stores Stores, broker *events.Broker) http.HandlerF
func handleClearConsumed(stores Stores, broker *events.Broker) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
items, err := stores.Instructions.List("consumed")
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
if err := stores.Instructions.DeleteConsumed(); err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
counts, _ := stores.Instructions.Counts()
broker.Broadcast("history.cleared", nil)
cleared := len(items)
broker.Broadcast("history.cleared", map[string]any{"count": cleared})
broker.Broadcast("status.changed", map[string]any{"queue": counts})
w.WriteHeader(http.StatusNoContent)
writeJSON(w, http.StatusOK, map[string]any{"cleared": cleared})
}
}

View File

@@ -135,9 +135,10 @@ func (s *InstructionStore) Create(content string) (*models.Instruction, error) {
id := uuid.New().String()
now := time.Now().UTC()
// Assign next position
// Assign next position from the full instruction history so positions remain
// globally stable even after the pending queue empties.
var maxPos sql.NullInt64
_ = s.db.QueryRow(`SELECT MAX(position) FROM instructions WHERE status = 'pending'`).Scan(&maxPos)
_ = s.db.QueryRow(`SELECT MAX(position) FROM instructions`).Scan(&maxPos)
position := int(maxPos.Int64) + 1
_, err := s.db.Exec(`