oficina / equipo Public

Find slice-1 Assistant question mirrors through their source card

settleStaleLegacyQuestions looked up mirrors by handoff_id, so mirrors made
before job cards stored it (empty handoff_id) stayed open for ever. The
minute's sweep now takes open Assistant question mirrors with a source card
and no handoff_id: it finds the legacy hand-off through the source
(handoffFor) and backfills handoff_id, so settleLegacyQuestions handles it;
with no hand-off, it resolves the mirror (approved resolved line, card event)
once the source is no longer open or is over staleQuestion old. The team
card is never touched. Budgets re-measured.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 9f7dc36d236c92318dbc6c816892bd03dcf85f05 parent 4b89ba1
3 files changed, +124 −4
  • .rastrillo/budgets.txt +2 −2
  • internal/equipo/chat/outcome.go +38 −2
  • internal/thintest/assistant_legacy_question_contract_test.go +84 −0

CI passed — run details

diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt
index aecb6460..07e31b88 100644
--- a/.rastrillo/budgets.txt
+++ b/.rastrillo/budgets.txt
@@ -56,7 +56,7 @@ perf GET /api/v1/tasks/{id} skip Thin v1 person API: bearer-only JSON for device
perf GET /api/v1/assistant/snapshot skip Assistant person API: bearer-only JSON for Base's chat, not a browser screen.
perf GET /api/v1/assistant/messages skip Assistant person API: bearer-only JSON for Base's chat, not a browser screen.
perf GET /api/v1/assistant/events skip Assistant event stream (Server-Sent Events), bearer-only and long-lived; not a screen.
-size internal/equipo/chat 28611 - Transactional conversation store shares authority and dispatch helpers; Roster visibility and batched reads; legacy mirrored questions resolve once moot. Measured 28611 source lines.
+size internal/equipo/chat 28647 - Transactional conversation store shares authority and dispatch helpers; roster reads; legacy mirrored questions resolve once moot or linked. Measured 28647 source lines.
perf GET /api/v1/questions/{id} skip Thin v1 person API: bearer-only JSON for devices; the browser reads questions on /tasks/{id}.
perf GET /api/v1/steps/{id}/log skip Thin v1 person API: bearer-only JSON for devices; the browser shows the log tail on /tasks/{id}.
perf GET /api/v1/connections/preference skip Connections slice 1 person API: bearer-only JSON for devices; the browser shows the preference on /conversation/connections.
@@ -71,7 +71,7 @@ perf GET /tasks/{id}/files/{file} skip Research kept images: the bytes of one im
perf GET /api/v1/runners skip Mac runners person API: bearer-only JSON for devices; the browser shows runners in the Team section.
time cmd/equipo-runner 60s TestReleaseRunnerHasNoFakeWorker builds the release runner and the thin package under testbuild (about 12s locally).
size internal/headless - 10904 End-to-end scenarios through a real runner and instance: Copy setting, wording flag, the Assistant's job threads, streamed replies and warm start. Measured 10904 test lines.
-size internal/thintest - 28814 Contract and browser tests share one instance harness: Assistant hand-off, outcome, job and legacy question contracts, Roster guards and captures. Measured 28814 test lines.
+size internal/thintest - 28898 Contract and browser tests share one instance harness: Assistant hand-off, outcome, job and legacy question contracts, Roster guards and captures. Measured 28898 test lines.
perf GET /api/v1/projects/{id}/merge-policy skip Merge authority person API: bearer-only JSON for devices; the browser reads the policy on /projects/{id}.
perf GET /api/v1/usage skip Thin v1 person API: bearer-only JSON for devices; the browser reads usage through /usage.
perf GET /api/v1/advice skip Thin v1 person API: bearer-only JSON for devices; the browser reads advice through /usage and /conversation.
diff --git a/internal/equipo/chat/outcome.go b/internal/equipo/chat/outcome.go
index f13d09be..42cb14dd 100644
--- a/internal/equipo/chat/outcome.go
+++ b/internal/equipo/chat/outcome.go
@@ -598,10 +598,46 @@ func (t *txn) handoffRunning(h handoffRecord) (bool, error) {
// settleStaleLegacyQuestions is the minute's look at every legacy
// hand-off with an open mirrored question: nothing else may move one
-// that waits for a day, or that settled before this rule.
+// that waits for a day, or that settled before this rule. Mirrors slice
+// 1 made carry no hand-off: each is linked through its source card
+// (handoffFor), or, with no hand-off to follow it, resolved once its
+// source is no longer open or is over staleQuestion old.
func (t *txn) settleStaleLegacyQuestions() error {
+ const open = "lane = ? AND kind = ? AND state = ? AND coalesce(source_card, '') <> ''"
+ var orphans []cardRecord
+ if err := t.db.Where(open+" AND coalesce(handoff_id, '') = ''", wire.LaneAssistant, wire.CardQuestion, wire.CardOpen).Order("created_at, id").Find(&orphans).Error; err != nil {
+ return err
+ }
+ for _, m := range orphans {
+ var src cardRecord
+ if err := t.db.Where("id = ? AND person = ?", m.SourceCard, m.Person).Limit(1).Find(&src).Error; err != nil {
+ return err
+ }
+ h, found, err := t.handoffFor(src)
+ if err != nil {
+ return err
+ }
+ if src.ID != "" && found {
+ if err := t.db.Model(&cardRecord{}).Where("id = ?", m.ID).Update("handoff_id", h.ID).Error; err != nil {
+ return err
+ }
+ continue
+ }
+ if src.ID != "" && src.State == wire.CardOpen && !src.CreatedAt.Before(t.now().Add(-staleQuestion)) {
+ continue
+ }
+ m.State, m.Result = wire.CardResolved, t.copy("assistant.outcome.resolved")
+ m.Revision++
+ m.UpdatedAt = t.now()
+ if err := t.db.Model(&cardRecord{}).Where("id = ?", m.ID).Select("state", "result", "revision", "updated_at").Updates(&m).Error; err != nil {
+ return err
+ }
+ if err := t.cardEvent(m); err != nil {
+ return err
+ }
+ }
var ids []string
- if err := t.db.Model(&cardRecord{}).Where("lane = ? AND kind = ? AND state = ? AND coalesce(source_card, '') <> '' AND coalesce(handoff_id, '') <> ''", wire.LaneAssistant, wire.CardQuestion, wire.CardOpen).Distinct().Order("handoff_id").Pluck("handoff_id", &ids).Error; err != nil || len(ids) == 0 {
+ if err := t.db.Model(&cardRecord{}).Where(open+" AND coalesce(handoff_id, '') <> ''", wire.LaneAssistant, wire.CardQuestion, wire.CardOpen).Distinct().Order("handoff_id").Pluck("handoff_id", &ids).Error; err != nil || len(ids) == 0 {
return err
}
var rows []handoffRecord
diff --git a/internal/thintest/assistant_legacy_question_contract_test.go b/internal/thintest/assistant_legacy_question_contract_test.go
index 0d1f759f..a3c655e1 100644
--- a/internal/thintest/assistant_legacy_question_contract_test.go
+++ b/internal/thintest/assistant_legacy_question_contract_test.go
@@ -219,3 +219,87 @@ func TestContractAssistantLegacyQuestionStale(t *testing.T) {
t.Fatalf("the team's own question moved to %s", st)
}
}
+
+// slice1Mirror makes the mirror as slice 1 wrote it, before mirrors
+// stored their hand-off: its handoff_id is empty.
+func (o *teamRig) slice1Mirror(mirror string) {
+ o.t.Helper()
+ o.exec("UPDATE chat_cards SET handoff_id = '' WHERE id = ?", mirror)
+}
+
+// mirrorHandoff is the mirror's stored handoff_id.
+func (o *teamRig) mirrorHandoff(id string) string {
+ o.t.Helper()
+ var h string
+ if err := o.inst.DB.Writer().QueryRow("SELECT coalesce(handoff_id, '') FROM chat_cards WHERE id = ?", id).Scan(&h); err != nil {
+ o.t.Fatal(err)
+ }
+ return h
+}
+
+// TestContractAssistantLegacyQuestionSlice1Stopped: a mirror from before
+// mirrors stored their hand-off is found through its source card; its
+// hand-off's work stopped, so it is resolved, and linked for next time.
+func TestContractAssistantLegacyQuestionSlice1Stopped(t *testing.T) {
+ o := newTeamRig(t)
+ var id string
+ request, _, source, mirror := o.legacyMirror("q1", &id)
+ o.slice1Mirror(mirror)
+ o.exec("INSERT INTO chat_steps(id, task_id, specialist, state, terminated, attempt, created_at, updated_at) VALUES ('s_q1', ?, ?, ?, 1, 1, datetime('now'), datetime('now'))", id, wire.SpecialistEngineering, wire.StepFailed)
+ o.tick()
+ o.resolvedMirror(mirror)
+ if got := o.mirrorHandoff(mirror); got != request {
+ t.Fatalf("handoff_id %q, want %q", got, request)
+ }
+ if st := o.cardState(source); st != wire.CardOpen {
+ t.Fatalf("the team's own question moved to %s", st)
+ }
+}
+
+// TestContractAssistantLegacyQuestionSlice1Stale: the same, found and
+// resolved because its source has waited over a day with nothing running.
+func TestContractAssistantLegacyQuestionSlice1Stale(t *testing.T) {
+ o := newTeamRig(t)
+ var id string
+ request, _, source, mirror := o.legacyMirror("q2", &id)
+ o.slice1Mirror(mirror)
+ o.exec("UPDATE chat_cards SET created_at = ? WHERE id = ?", time.Now().Add(-25*time.Hour), source)
+ o.tick()
+ o.resolvedMirror(mirror)
+ if got := o.mirrorHandoff(mirror); got != request {
+ t.Fatalf("handoff_id %q, want %q", got, request)
+ }
+}
+
+// TestContractAssistantLegacyQuestionSlice1NoHandoff: no hand-off follows
+// the source (it is cancelled): the mirror is resolved once its source is
+// no longer open or is over a day old, and stays while young and open.
+func TestContractAssistantLegacyQuestionSlice1NoHandoff(t *testing.T) {
+ o := newTeamRig(t)
+ request, _, source, mirror := o.legacyMirror("q3", nil)
+ o.slice1Mirror(mirror)
+ o.exec("UPDATE chat_handoffs SET state = 'cancelled' WHERE id = ?", request)
+ o.tick()
+ o.openMirror(mirror)
+ o.exec("UPDATE chat_cards SET created_at = ? WHERE id = ?", time.Now().Add(-25*time.Hour), source)
+ o.tick()
+ o.resolvedMirror(mirror)
+ if got := o.mirrorHandoff(mirror); got != "" {
+ t.Fatalf("handoff_id %q, want none", got)
+ }
+ if st := o.cardState(source); st != wire.CardOpen {
+ t.Fatalf("the team's own question moved to %s", st)
+ }
+}
+
+// TestContractAssistantLegacyQuestionSlice1SourceGone: no hand-off and the
+// source no longer open: the mirror is resolved.
+func TestContractAssistantLegacyQuestionSlice1SourceGone(t *testing.T) {
+ o := newTeamRig(t)
+ request, _, source, mirror := o.legacyMirror("q4", nil)
+ o.slice1Mirror(mirror)
+ o.exec("UPDATE chat_handoffs SET state = 'cancelled' WHERE id = ?", request)
+ o.exec("UPDATE chat_cards SET state = ? WHERE id = ?", wire.CardResolved, source)
+ o.tick()
+ o.resolvedMirror(mirror)
+}