Skip to content
8 changes: 6 additions & 2 deletions control-plane/internal/adapter/agent/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
// has no work when the truth is that this image cannot be asked.
//
// The write is a POST of a person's words, which the slot turns into the next
// turn of a task. It is as passive as the reads: it starts no turn of the
// session's own and wakes nothing.
// turn of a task. It restores no snapshot and sends nothing into the person's
// chat. What it can do, and says in its answer, is hand the task and the turn
// to the session's own monitor lane when the task was waiting for a person's
// judgement — a hop inside that pod, which this client neither makes nor needs
// to know the shape of beyond relaying what came of it
// (data-plane/cmd/agent/session_work_relay_wake.go).
//
// Keep the paths in sync with data-plane/cmd/agent (agentWorkTasksPath,
// agentWorkTurnsPath, agentWorkRequestsPath).
Expand Down
8 changes: 5 additions & 3 deletions control-plane/internal/api/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ type createWorkRequestReq struct {
//
// It writes no task field: the slot appends a turn, so a request arriving while
// the session is updating that same task cannot take the update out from under
// it. It starts no turn of the session's own and wakes nothing — the answer says
// the request is on the slot and waiting, never that it has been dealt with,
// which only the parent can say by working the turn.
// it. It starts no turn of the person's own conversation and restores no
// snapshot. The answer never says the request has been dealt with — only the
// turn ending says that — and it distinguishes a second thing from the first:
// whether the slot was able to hand the task and turn to its monitor lane,
// which it does when the task was waiting for a person's judgement.
//
// The author is the caller's own derivation from whatever boundary
// authenticated the person; like a task's owner it is bookkeeping, and nothing
Expand Down
35 changes: 35 additions & 0 deletions control-plane/internal/session/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,43 @@ type WorkRequestInput struct {
type WorkRequestStored struct {
Turn WorkTurn `json:"turn"`
Replayed bool `json:"replayed"`
// Relay is the handover to the manager's monitor lane, present only when
// there was one to make: a request on a task that was waiting for a
// person's judgement is handed to that lane at once, because it is the lane
// that stopped looking at the task for exactly that reason. Absent is "no
// handover was due" — an ordinary request, or a session running a single
// lane — and a reader must not show it as a failed one.
Relay *WorkRequestRelay `json:"relay,omitempty"`
}

// WorkRequestRelay is what became of that handover. It carries no receipt: a
// receipt names a run of a lane inside the session's pod, and the reader of this
// answer is outside the session.
//
// The three facts a caller can tell apart are deliberately separate: the
// request is stored (this answer exists at all), the monitor has been given it
// (Accepted), and the work is done (which only the turn ending says). A caller
// that collapses them tells a person their request has been dealt with when it
// has only been filed.
type WorkRequestRelay struct {
// State is "accepted" when the lane took it — immediately if that lane was
// idle, queued behind its current turn if it was busy — or "pending" when
// the intent is recorded and the lane could not be reached. Pending is not
// lost: the record is durable and the monitor's own standing round reads
// the same turn.
State string `json:"state"`
// Attempts is how many hops this handover has taken, so a caller can tell a
// first delivery from a re-delivery after a lost acknowledgement.
Attempts int64 `json:"attempts"`
}

// The two states a handover is reported in. Keep in sync with
// data-plane/cmd/agent (workRelayAccepted, workRelayPending).
const (
WorkRelayAccepted = "accepted"
WorkRelayPending = "pending"
)

// WorkStatuses is the task status domain, the ledger's own unchanged: a
// migration that renamed a status would move work a person had already seen as
// blocked or completed.
Expand Down
13 changes: 12 additions & 1 deletion data-plane/cmd/agent/session_work.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
// token and the outside action it recorded — sits in records of its own beside
// them, each named for what it is: sp_work_task_book
// (session_work_parent.go), sp_work_monitor (session_work_monitor.go),
// sp_work_turn_claim and sp_work_turn_admin.
// sp_work_request_wake (session_work_relay_wake.go), sp_work_turn_claim and
// sp_work_turn_admin.
//
// The ledger these records came from is gone, and so is everything that moved
// it: the migration, the rollback export, the retirement, the snapshot tools
Expand Down Expand Up @@ -112,6 +113,16 @@ CREATE TABLE IF NOT EXISTS sp_work_monitor (
version INTEGER NOT NULL,
updated_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS sp_work_request_wake (
turn_id INTEGER PRIMARY KEY,
task_id TEXT NOT NULL,
source_id TEXT NOT NULL,
state TEXT NOT NULL,
attempts INTEGER NOT NULL,
ticket TEXT NOT NULL,
recorded_at TEXT NOT NULL,
claimed_at TEXT NOT NULL
);
`

// The records that existed only to manage the move onto the work records, and
Expand Down
76 changes: 62 additions & 14 deletions data-plane/cmd/agent/session_work_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ type workRequestBody struct {

// serveWorkRequests stores one person's request as the next turn of a task.
//
// It starts no turn of the session's own, wakes nothing and restores no
// snapshot: the parent finds it the next time it lists the task's open turns.
// It restores no snapshot and starts no turn of the *person's* conversation:
// the request is a turn of the work, not a message in the chat. One thing it
// does do, in the one case where there is somebody to tell: a request on a task
// that was waiting for a person's judgement is handed to this manager's monitor
// lane at once, because that is the lane that stopped looking at the task
// precisely because it was waiting (session_work_relay_wake.go). Every other
// request is found the next time the records are read, as before.
//
// Idempotency is the caller's own id — the same id with the same words answers
// with the turn already appended.
// with the turn already appended, and with the handover that was already made
// rather than a second one.
func (s *sessionDB) serveWorkRequests(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
Expand All @@ -78,7 +85,7 @@ func (s *sessionDB) serveWorkRequests(w http.ResponseWriter, r *http.Request) {
}
ctx, cancel := context.WithTimeout(r.Context(), sessionDBCallTimeout)
defer cancel()
stored, replayed, err := s.storeWorkRequest(ctx, body, workClock())
stored, replayed, relay, err := s.storeWorkRequest(ctx, body, workClock())
if err != nil {
var known workError
if errors.As(err, &known) {
Expand All @@ -98,51 +105,92 @@ func (s *sessionDB) serveWorkRequests(w http.ResponseWriter, r *http.Request) {
if replayed {
status = http.StatusOK
}
answer := map[string]any{"turn": stored, "replayed": replayed}
if relay != nil {
// Only present when there was a monitor lane to tell. Absent means no
// handover was due, which a reader must not show as a failed one.
answer["relay"] = relay
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(map[string]any{"turn": stored, "replayed": replayed}); err != nil {
if err := json.NewEncoder(w).Encode(answer); err != nil {
s.logger.Info("work request reply could not be written", "err", err)
}
}

// storeWorkRequest validates the request and appends it in one transaction, so
// the turn and the record of where it came from commit together.
//
// The third answer is the handover to this manager's monitor lane, when there
// was one to make: a request on a task that was waiting for a person's
// judgement is the one case where the slot has somebody to tell, and it tells
// them once, here (session_work_relay_wake.go). It is nil when no handover was
// due, which is every other request and every pod that runs a single lane.
func (s *sessionDB) storeWorkRequest(
ctx context.Context, body workRequestBody, now time.Time,
) (workTurnView, bool, error) {
) (workTurnView, bool, *workRequestRelay, error) {
if !workID.MatchString(body.ID) {
return workTurnView{}, false, workFail("invalid_argument", "id must be 1..128 letters, digits, _ or -")
return workTurnView{}, false, nil, workFail("invalid_argument", "id must be 1..128 letters, digits, _ or -")
}
if !workID.MatchString(body.TaskID) {
return workTurnView{}, false, workFail("invalid_argument", "task_id must be 1..128 letters, digits, _ or -")
return workTurnView{}, false, nil, workFail("invalid_argument", "task_id must be 1..128 letters, digits, _ or -")
}
text := strings.TrimSpace(body.Text)
if text == "" || len(body.Text) > workTurnTextMax {
return workTurnView{}, false, workFail("invalid_argument",
return workTurnView{}, false, nil, workFail("invalid_argument",
"text must be a person's words, at most 65536 characters")
}
if len(body.Author) > workTitleMax {
return workTurnView{}, false, workFail("invalid_argument", "author too long")
return workTurnView{}, false, nil, workFail("invalid_argument", "author too long")
}
if body.ChoiceTurnID != nil && (*body.ChoiceTurnID < 1 || *body.ChoiceTurnID > requestSafeInt) {
return workTurnView{}, false, workFail("invalid_argument", "choice_turn_id must be a turn id")
return workTurnView{}, false, nil, workFail("invalid_argument", "choice_turn_id must be a turn id")
}
db, err := s.handle(ctx)
if err != nil {
return workTurnView{}, false, err
return workTurnView{}, false, nil, err
}
var stored workTurnView
replayed := false
wake := false
paired := s.wakesMonitorLane()
err = s.workTx(ctx, db, func(tx *sql.Tx) error {
// Asked before the store, because storing is allowed to move the very
// status that decides this: a task shown as `review` is received as
// `planned` in this same transaction, and a question put afterwards
// would read that receipt and conclude nobody had been waiting — losing
// the request that proved somebody was. Skipped entirely when there is
// no lane to tell, so the ordinary single-lane pod pays nothing for it.
awaited := false
if paired {
var judgeErr error
if awaited, judgeErr = requestAwaitedPersonsJudgement(ctx, tx, body.TaskID); judgeErr != nil {
return judgeErr
}
}
var storeErr error
stored, replayed, storeErr = storeRequestAsTurn(
ctx, tx, body.ID, body.TaskID, body.Text, body.Author, body.ChoiceTurnID, now)
if storeErr != nil {
return storeErr
}
// The intent to tell the monitor lane is written in the same
// transaction as the turn, so a crash between the two cannot leave a
// request nobody was told about (session_work_relay_wake.go), and a
// store that fails takes the intent down with it. The hop itself is
// afterwards: it is a network call, and this transaction holds the
// slot's only connection.
wake, storeErr = noteRequestWake(
ctx, tx, body.ID, body.TaskID, awaited, stored.workTurn, paired, now)
return storeErr
})
if err != nil {
return workTurnView{}, false, err
return workTurnView{}, false, nil, err
}
if !wake {
return stored, replayed, nil, nil
}
return stored, replayed, nil
return stored, replayed, s.relayRequestWake(ctx, stored.ID), nil
}

// serveWorkTasks answers one keyset page of work tasks, reusing the same
Expand Down
Loading
Loading