Read job cards and their attachments from the Assistant
equipo.Card gains job, detail and started_at, and Link gains rel. Clean keeps a job card only in one of its five states, with no options, and keeps detail and started_at on jobs only, rel only when "equipo", and job only when it looks like a card id. Shown always shows a job; Go and Equipo pick a job's links. The relay passes them through Clean. The fake gains AskJob, SetJob, AskOutcomeFor and EmitTurn.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+310
−27
internal/equipo/chat.go+99 −18internal/equipo/equipo_test.go+115 −0internal/equipo/equipotest/chat.go+71 −8internal/web/ask_relay.go+2 −1internal/web/relay_internal_test.go+23 −0
diff --git a/internal/equipo/chat.go b/internal/equipo/chat.go| index 190b49d..958a0be 100644 |
| --- a/internal/equipo/chat.go |
| +++ b/internal/equipo/chat.go |
| @@ -43,16 +43,22 @@ type Turn struct { |
| // Card is something in the conversation to act on, drawn from its fields |
| // alone: the question, a button per option (its label, answering with its |
| // value), a link per link, and once it has one, a line saying how it |
| -// ended (Result). Base knows no kind's words of its own. |
| +// ended (Result). Base knows no kind's words of its own. A job card's |
| +// question is the job's title, Result its short status, Detail one |
| +// sentence more and StartedAt when it began; Job is the job card a |
| +// go-ahead or Publish card belongs to. |
| type Card struct { |
| - ID string `json:"id"` |
| - Revision int64 `json:"revision"` |
| - Kind string `json:"kind"` |
| - State string `json:"state"` |
| - Question string `json:"question"` |
| - Options []Option `json:"options"` |
| - Links []Link `json:"links"` |
| - Result string `json:"result"` |
| + ID string `json:"id"` |
| + Revision int64 `json:"revision"` |
| + Kind string `json:"kind"` |
| + State string `json:"state"` |
| + Question string `json:"question"` |
| + Options []Option `json:"options"` |
| + Links []Link `json:"links"` |
| + Result string `json:"result"` |
| + Job string `json:"job,omitempty"` |
| + Detail string `json:"detail,omitempty"` |
| + StartedAt *time.Time `json:"started_at,omitempty"` |
| } |
| // Option is one answer a card offers: Label on the button, Value sent. |
| @@ -61,10 +67,12 @@ type Option struct { |
| Label string `json:"label"` |
| } |
| -// Link is a link a card shows. |
| +// Link is a link a card shows. Rel, when set, says what it is to |
| +// (LinkEquipo). |
| type Link struct { |
| Label string `json:"label"` |
| URL string `json:"url"` |
| + Rel string `json:"rel,omitempty"` |
| } |
| // The kinds of card the Assistant raises. |
| @@ -73,36 +81,109 @@ const ( |
| CardConfirm = "confirm" |
| CardOutcome = "outcome" |
| CardConnect = "connect" |
| + // CardJob is a job card: one per piece of work handed on, updated in |
| + // place. |
| + CardJob = "job" |
| ) |
| +// A job card's states. |
| +const ( |
| + JobWorking = "working" |
| + JobAsking = "asking" |
| + JobReady = "ready" |
| + JobDone = "done" |
| + JobStopped = "stopped" |
| +) |
| + |
| +// LinkEquipo is the rel of a job card's link to the job's page in Equipo. |
| +const LinkEquipo = "equipo" |
| + |
| // Clean is the card as Base passes it on: its own fields only, never-null |
| -// options and links, and a link only if it is an http(s) URL (WebURL). |
| -// ok is false for every other kind of card. |
| +// options and links, and a link only if it is an http(s) URL (WebURL), |
| +// with a rel only if it is LinkEquipo. A job card has no options, and is |
| +// kept only in a state Base knows; Detail and StartedAt are kept on a job |
| +// card only, and Job only when it looks like a card id (validJob). ok is |
| +// false for every other kind of card. |
| func (c Card) Clean() (Card, bool) { |
| switch c.Kind { |
| case CardQuestion, CardConfirm, CardOutcome, CardConnect: |
| + case CardJob: |
| + switch c.State { |
| + case JobWorking, JobAsking, JobReady, JobDone, JobStopped: |
| + default: |
| + return Card{}, false |
| + } |
| default: |
| return Card{}, false |
| } |
| out := Card{ID: c.ID, Revision: c.Revision, Kind: c.Kind, State: c.State, Question: c.Question, |
| Options: []Option{}, Links: []Link{}, Result: c.Result} |
| - for _, o := range c.Options { |
| - out.Options = append(out.Options, Option{Value: o.Value, Label: o.Label}) |
| + if c.Kind == CardJob { |
| + out.Detail, out.StartedAt = c.Detail, c.StartedAt |
| + } else { |
| + for _, o := range c.Options { |
| + out.Options = append(out.Options, Option{Value: o.Value, Label: o.Label}) |
| + } |
| + } |
| + if validJob(c.Job) { |
| + out.Job = c.Job |
| } |
| for _, l := range c.Links { |
| if u := WebURL(l.URL); u != "" { |
| - out.Links = append(out.Links, Link{Label: l.Label, URL: u}) |
| + link := Link{Label: l.Label, URL: u} |
| + if l.Rel == LinkEquipo { |
| + link.Rel = l.Rel |
| + } |
| + out.Links = append(out.Links, link) |
| } |
| } |
| return out, true |
| } |
| +// validJob reports whether id is 1 to 128 of [A-Za-z0-9_-]. |
| +func validJob(id string) bool { |
| + if id == "" || len(id) > 128 { |
| + return false |
| + } |
| + for _, r := range id { |
| + if !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '_' || r == '-') { |
| + return false |
| + } |
| + } |
| + return true |
| +} |
| + |
| +// Go is a job card's first link without a rel: where to see the work. |
| +func (c Card) Go() (Link, bool) { |
| + if c.Kind == CardJob { |
| + for _, l := range c.Links { |
| + if l.Rel == "" { |
| + return l, true |
| + } |
| + } |
| + } |
| + return Link{}, false |
| +} |
| + |
| +// Equipo is a job card's link to the job's page in Equipo. |
| +func (c Card) Equipo() (Link, bool) { |
| + if c.Kind == CardJob { |
| + for _, l := range c.Links { |
| + if l.Rel == LinkEquipo { |
| + return l, true |
| + } |
| + } |
| + } |
| + return Link{}, false |
| +} |
| + |
| // Open reports whether the card waits for an answer. |
| func (c Card) Open() bool { return c.State == "open" } |
| -// Shown reports whether a conversation read afresh shows the card: while |
| -// it is open, and afterwards if it says how it ended. |
| -func (c Card) Shown() bool { return c.Open() || c.Result != "" } |
| +// Shown reports whether a conversation read afresh shows the card: a job |
| +// card always; any other while it is open, and afterwards if it says how |
| +// it ended. |
| +func (c Card) Shown() bool { return c.Kind == CardJob || c.Open() || c.Result != "" } |
| // WebURL is raw if it is an absolute http or https URL with a host and no |
| // user info, else "". |
diff --git a/internal/equipo/equipo_test.go b/internal/equipo/equipo_test.go| index 60ee4d5..4c4ca37 100644 |
| --- a/internal/equipo/equipo_test.go |
| +++ b/internal/equipo/equipo_test.go |
| @@ -1,6 +1,7 @@ |
| package equipo_test |
| import ( |
| + "bufio" |
| "context" |
| "crypto/ed25519" |
| "crypto/rand" |
| @@ -287,3 +288,117 @@ func TestCardShown(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// A job card keeps what Base draws of it: no options, the Equipo link's |
| +// rel and no other, its detail and start time. A job in a state it does |
| +// not know stays in Equipo; detail is a job's only, and an attached |
| +// card's job id must look like one. |
| +func TestCardCleanJob(t *testing.T) { |
| + start := time.Date(2026, 11, 13, 9, 30, 0, 0, time.UTC) |
| + in := equipo.Card{ID: "c_1", Revision: 3, Kind: equipo.CardJob, State: equipo.JobReady, Question: "Add a booking page", |
| + Options: []equipo.Option{{Value: "go", Label: "Go"}}, Result: "Ready to look at", Detail: "Have a look before it goes live.", StartedAt: &start, |
| + Links: []equipo.Link{{Label: "See it", URL: "https://preview.test/"}, {Label: "Odd", URL: "https://odd.test/", Rel: "other"}, |
| + {Label: "Open in Equipo", URL: "https://equipo.test/jobs/h_1", Rel: equipo.LinkEquipo}}} |
| + got, ok := in.Clean() |
| + raw, _ := json.Marshal(got) |
| + const want = `{"id":"c_1","revision":3,"kind":"job","state":"ready","question":"Add a booking page","options":[],"links":[{"label":"See it","url":"https://preview.test/"},{"label":"Odd","url":"https://odd.test/"},{"label":"Open in Equipo","url":"https://equipo.test/jobs/h_1","rel":"equipo"}],"result":"Ready to look at","detail":"Have a look before it goes live.","started_at":"2026-11-13T09:30:00Z"}` |
| + if !ok || string(raw) != want { |
| + t.Errorf("job: %v\n got %s\nwant %s", ok, raw, want) |
| + } |
| + if l, ok := got.Go(); !ok || l.URL != "https://preview.test/" { |
| + t.Errorf("Go: %v %+v", ok, l) |
| + } |
| + if l, ok := got.Equipo(); !ok || l.URL != "https://equipo.test/jobs/h_1" { |
| + t.Errorf("Equipo: %v %+v", ok, l) |
| + } |
| + only := equipo.Card{Kind: equipo.CardJob, State: equipo.JobWorking, Links: in.Links[2:]} |
| + if l, ok := only.Go(); ok { |
| + t.Errorf("a job with only its Equipo link has a Go link: %+v", l) |
| + } |
| + for _, state := range []string{equipo.JobWorking, equipo.JobAsking, equipo.JobDone, equipo.JobStopped} { |
| + if _, ok := (equipo.Card{ID: "c_2", Kind: equipo.CardJob, State: state}).Clean(); !ok { |
| + t.Errorf("a %s job was dropped", state) |
| + } |
| + } |
| + for _, state := range []string{"pending", "open", ""} { |
| + if _, ok := (equipo.Card{ID: "c_2", Kind: equipo.CardJob, State: state}).Clean(); ok { |
| + t.Errorf("a %q job was kept", state) |
| + } |
| + } |
| + outcome, ok := equipo.Card{ID: "c_3", Kind: equipo.CardOutcome, State: "open", Detail: "More", StartedAt: &start, Job: "c_1", |
| + Links: []equipo.Link{{Label: "See it", URL: "https://preview.test/"}}}.Clean() |
| + raw, _ = json.Marshal(outcome) |
| + const wantOutcome = `{"id":"c_3","revision":0,"kind":"outcome","state":"open","question":"","options":[],"links":[{"label":"See it","url":"https://preview.test/"}],"result":"","job":"c_1"}` |
| + if !ok || string(raw) != wantOutcome { |
| + t.Errorf("outcome: %v\n got %s\nwant %s", ok, raw, wantOutcome) |
| + } |
| + if l, ok := outcome.Go(); ok { |
| + t.Errorf("an outcome card has a job's Go link: %+v", l) |
| + } |
| + if l, ok := (equipo.Card{Kind: equipo.CardOutcome, Links: in.Links[2:]}).Equipo(); ok { |
| + t.Errorf("an outcome card has a job's Equipo link: %+v", l) |
| + } |
| + for job, keep := range map[string]bool{"c_1": true, "C-9_x": true, strings.Repeat("a", 128): true, |
| + "../x": false, "c 1": false, "c/1": false, strings.Repeat("a", 129): false, "": false} { |
| + got, _ := (equipo.Card{ID: "c_4", Kind: equipo.CardOutcome, State: "open", Job: job}).Clean() |
| + if (got.Job == job && job != "") != keep || !keep && got.Job != "" { |
| + t.Errorf("job %q: kept %q", job, got.Job) |
| + } |
| + } |
| + if !(equipo.Card{Kind: equipo.CardJob, State: equipo.JobWorking}).Shown() { |
| + t.Error("a working job is not shown") |
| + } |
| +} |
| + |
| +// The fake raises and moves job cards as Equipo does: the Equipo link |
| +// stays last, each change is a new revision and an event; a go-ahead can |
| +// belong to a job, and a turn of any kind can appear. |
| +func TestFakeJobCards(t *testing.T) { |
| + f := equipotest.New(t) |
| + c := equipo.New(f.URL) |
| + cred := signIn(t, f, c) |
| + ctx := context.Background() |
| + id := f.AskJob("Add a booking page") |
| + f.SetJob(id, equipo.JobReady, "Ready to look at", "Have a look.", equipo.Link{Label: "See it", URL: "https://preview.test/"}) |
| + go1 := f.AskOutcomeFor(id, "This is ready. Go ahead?", []equipo.Option{{Value: "go", Label: "Go ahead"}}, nil) |
| + tid := f.EmitTurn("team_update", equipo.TurnRunning) |
| + snap, err := c.Snapshot(ctx, cred.Token) |
| + if err != nil || len(snap.Cards) != 2 || len(snap.Turns) != 1 { |
| + t.Fatalf("snapshot: %+v %v", snap, err) |
| + } |
| + job, ok := snap.Cards[0].Clean() |
| + if !ok || job.ID != id || job.Revision != 2 || job.State != equipo.JobReady || job.Question != "Add a booking page" || |
| + job.Result != "Ready to look at" || job.Detail != "Have a look." || job.StartedAt == nil || time.Since(*job.StartedAt) > time.Minute { |
| + t.Errorf("job: %v %+v", ok, job) |
| + } |
| + if l, ok := job.Go(); !ok || l.URL != "https://preview.test/" { |
| + t.Errorf("Go: %v %+v", ok, l) |
| + } |
| + if l, ok := job.Equipo(); !ok || len(job.Links) != 2 || job.Links[1] != l || l.Label != "Open in Equipo" || |
| + !strings.HasPrefix(l.URL, f.URL+"/jobs/h_") { |
| + t.Errorf("Equipo link: %v %+v", ok, job.Links) |
| + } |
| + if o := snap.Cards[1]; o.ID != go1 || o.Kind != equipo.CardOutcome || o.Job != id || o.State != "open" { |
| + t.Errorf("attached card: %+v", o) |
| + } |
| + if tr := snap.Turns[0]; tr.ID != tid || tr.Kind != "team_update" || tr.State != equipo.TurnRunning { |
| + t.Errorf("turn: %+v", tr) |
| + } |
| + sctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
| + defer cancel() |
| + res, err := c.Events(sctx, cred.Token, "40") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer res.Body.Close() |
| + var seen []string |
| + sc := bufio.NewScanner(res.Body) |
| + for len(seen) < 4 && sc.Scan() { |
| + if line, ok := strings.CutPrefix(sc.Text(), "event: "); ok { |
| + seen = append(seen, line) |
| + } |
| + } |
| + if strings.Join(seen, " ") != "card card card turn" { |
| + t.Errorf("events: %v", seen) |
| + } |
| +} |
diff --git a/internal/equipo/equipotest/chat.go b/internal/equipo/equipotest/chat.go| index 487ef01..432ba78 100644 |
| --- a/internal/equipo/equipotest/chat.go |
| +++ b/internal/equipo/equipotest/chat.go |
| @@ -47,14 +47,17 @@ type turn struct { |
| // card is wire.AssistantCard: options and links are never null. |
| type card struct { |
| - ID string `json:"id"` |
| - Revision int64 `json:"revision"` |
| - Kind string `json:"kind"` |
| - State string `json:"state"` |
| - Question string `json:"question"` |
| - Options []equipo.Option `json:"options"` |
| - Links []equipo.Link `json:"links"` |
| - Result string `json:"result"` |
| + ID string `json:"id"` |
| + Revision int64 `json:"revision"` |
| + Kind string `json:"kind"` |
| + State string `json:"state"` |
| + Question string `json:"question"` |
| + Options []equipo.Option `json:"options"` |
| + Links []equipo.Link `json:"links"` |
| + Result string `json:"result"` |
| + Job string `json:"job,omitempty"` |
| + Detail string `json:"detail,omitempty"` |
| + StartedAt *time.Time `json:"started_at,omitempty"` |
| } |
| type event struct { |
| @@ -139,6 +142,66 @@ func (f *Fake) AskKind(kind, question string, options []equipo.Option, links []e |
| return c.ID |
| } |
| +// AskJob puts a job card in the conversation, as Equipo does when the |
| +// Assistant hands work on: working, getting started, linked to the job's |
| +// page in Equipo. It returns the card's id. |
| +func (f *Fake) AskJob(title string) string { |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + now := time.Now().UTC() |
| + c := &card{ID: f.next("c"), Revision: 1, Kind: equipo.CardJob, State: equipo.JobWorking, Question: title, |
| + Options: []equipo.Option{}, Result: "Getting started", Detail: "Working out what's needed.", StartedAt: &now, |
| + Links: []equipo.Link{{Label: "Open in Equipo", URL: f.URL + "/jobs/" + f.next("h"), Rel: equipo.LinkEquipo}}} |
| + f.cards = append(f.cards, c) |
| + f.emit("card", "card", c.ID, c.Revision, "", *c) |
| + return c.ID |
| +} |
| + |
| +// SetJob moves job card id to state with result and detail, and links |
| +// before its Equipo link, which stays last: a new revision, announced. |
| +func (f *Fake) SetJob(id, state, result, detail string, links ...equipo.Link) { |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + for _, c := range f.cards { |
| + if c.ID != id { |
| + continue |
| + } |
| + next := append([]equipo.Link{}, links...) |
| + for _, l := range c.Links { |
| + if l.Rel == equipo.LinkEquipo { |
| + next = append(next, l) |
| + } |
| + } |
| + c.State, c.Result, c.Detail, c.Links, c.Revision = state, result, detail, next, c.Revision+1 |
| + f.emit("card", "card", c.ID, c.Revision, "", *c) |
| + } |
| +} |
| + |
| +// AskOutcomeFor puts an open outcome card in the conversation that |
| +// belongs to job card job, as Equipo mirrors a job's go-ahead or Publish. |
| +func (f *Fake) AskOutcomeFor(job, question string, options []equipo.Option, links []equipo.Link) string { |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + c := &card{ID: f.next("c"), Revision: 1, Kind: equipo.CardOutcome, State: "open", Question: question, Job: job, |
| + Options: append([]equipo.Option{}, options...), Links: append([]equipo.Link{}, links...)} |
| + f.cards = append(f.cards, c) |
| + f.emit("card", "card", c.ID, c.Revision, "", *c) |
| + return c.ID |
| +} |
| + |
| +// EmitTurn adds a turn of kind in state that no message of the person's |
| +// started, such as a team_update turn, announcing it. It returns its id. |
| +func (f *Fake) EmitTurn(kind, state string) string { |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + now := time.Now().UTC() |
| + t := &turn{ID: f.next("t"), Revision: 1, Kind: kind, State: state, Attempt: 1, MessageIDs: []string{}, |
| + CreatedAt: now, UpdatedAt: now} |
| + f.turns[t.ID] = t |
| + f.emit("turn", "turn", t.ID, t.Revision, t.ID, *t) |
| + return t.ID |
| +} |
| + |
| // SetCard moves card id to state with result, a new revision, announcing |
| // it. |
| func (f *Fake) SetCard(id, state, result string) { |
diff --git a/internal/web/ask_relay.go b/internal/web/ask_relay.go| index ce31604..d9beef5 100644 |
| --- a/internal/web/ask_relay.go |
| +++ b/internal/web/ask_relay.go |
| @@ -368,7 +368,8 @@ func (e sseEvent) frame() string { |
| // project reduces an event's data to the conversation's shape, the one |
| // /ask/state uses; ok is false for every other kind of event, and for |
| -// cards of a kind Base does not draw (equipo.Card.Clean). |
| +// cards of a kind Base does not draw, or jobs in a state it does not know |
| +// (equipo.Card.Clean). |
| func project(typ string, raw json.RawMessage) (any, bool) { |
| switch typ { |
| case "message": |
diff --git a/internal/web/relay_internal_test.go b/internal/web/relay_internal_test.go| index 2e8a4ae..b13a544 100644 |
| --- a/internal/web/relay_internal_test.go |
| +++ b/internal/web/relay_internal_test.go |
| @@ -116,3 +116,26 @@ func TestRelayGenericCards(t *testing.T) { |
| t.Errorf("relay lacks %s:\n%s", want, out) |
| } |
| } |
| + |
| +// A job card passes with its Equipo link's rel, its detail and its start |
| +// time; a card attached to a job keeps the job's id. A job in a state |
| +// Base does not know is not passed on, and Equipo's own fields still go. |
| +func TestRelayJobCards(t *testing.T) { |
| + job := `{"cursor":50,"type":"card","data":{"id":"c_5","revision":2,"kind":"job","state":"working","question":"Add a booking page","options":[],"links":[{"label":"Open in Equipo","url":"https://equipo.test/jobs/h_1","rel":"equipo"}],"result":"Building it","detail":"Putting the page together.","started_at":"2026-11-13T09:30:00Z","task_id":"tk0002"}}` |
| + outcome := `{"cursor":51,"type":"card","data":{"id":"c_6","revision":1,"kind":"outcome","state":"open","question":"This is ready. Go ahead?","options":[{"value":"go","label":"Go ahead"}],"links":[],"result":"","job":"c_5","task_id":"tk0003"}}` |
| + odd := `{"cursor":52,"type":"card","data":{"id":"c_7","revision":1,"kind":"job","state":"pending","question":"Odd","options":[],"links":[],"result":""}}` |
| + out := relayTo(t, strings.NewReader(frame("50", "card", job)+frame("51", "card", outcome)+frame("52", "card", odd)), time.Second, func() {}) |
| + for _, banned := range []string{"task_id", "tk0002", "tk0003", "c_7", "id: 52"} { |
| + if strings.Contains(out, banned) { |
| + t.Errorf("relayed %q:\n%s", banned, out) |
| + } |
| + } |
| + for _, want := range []string{ |
| + `data: {"type":"card","data":{"id":"c_5","revision":2,"kind":"job","state":"working","question":"Add a booking page","options":[],"links":[{"label":"Open in Equipo","url":"https://equipo.test/jobs/h_1","rel":"equipo"}],"result":"Building it","detail":"Putting the page together.","started_at":"2026-11-13T09:30:00Z"}}`, |
| + `data: {"type":"card","data":{"id":"c_6","revision":1,"kind":"outcome","state":"open","question":"This is ready. Go ahead?","options":[{"value":"go","label":"Go ahead"}],"links":[],"result":"","job":"c_5"}}`, |
| + } { |
| + if !strings.Contains(out, want) { |
| + t.Errorf("relay lacks %s:\n%s", want, out) |
| + } |
| + } |
| +} |