Skip to content

Factory job outcomes: a durable cursor read and a pushed factory.event - #2416

Merged
AbirAbbas merged 4 commits into
workstreams/mainfrom
workstreams/fx-events
Nov 5, 2026
Merged

AbirAbbas merged 4 commits into
workstreams/mainfrom
workstreams/fx-events

Conversation

@AbirAbbas

Copy link
Copy Markdown
Collaborator

Summary

Apps that watch Factory jobs (the desktop app first) need to learn when a job finished, merged, failed or needs a person without re-reading the whole queue, including what happened while no app was connected.

Event source: the Factory's own job state, recorded at the store write, not a diff of factory.queue in the engine. The engine runs only while an app is open, so a queue diff there sees nothing while it is closed; a Factory that a team or a terminal serves keeps writing; and recording inside live.Commit (before the job snapshot is visible) misses no transition that a later read could no longer see.

  • Factory record (internal/factory/live/outcomes.go): job-outcomes.json beside the live record, written under the same lock: its own sequence, the newest 2,000 outcomes, and a floor below which a reader resets. The live record, its live_N ids and /v1/events are unchanged (outcome events never enter the live window). An older binary writing the same folder drops the outcomes_through marker in the live record; the next commit turns that into a gap so readers reset instead of skipping. A create records nothing.
  • GET /v1/job-events?after=&limit=&wait=: outcomes after a cursor for the jobs the caller may see (the cursor moves past hidden ones), optional long poll up to 15 s.
  • Engine wire: factory.events_since {factory, after?, limit?, follow?} → {factory, supported, events, cursor, reset, more}. reset (no cursor, a cursor older than kept, a gap, a replaced record) means "read the Factory's current state once, then continue from cursor". With follow, one follower per factory long-polls and pushes each later outcome as factory.event. A Factory older than the route answers supported: false. Hello feature factory.job_events, capability job_events.
  • Store change is two lines (the update paths also pass live.Outcomes); live.Changes and FixtureLiveEvents are untouched.

Validation contract → tests

Item Tests
E1 one outcome per transition; at = the job read's own fact time; none on create internal/factory/live/outcomes_test.go, internal/factory/store/outcomes_test.go
E2 ordered across writers, paging yields each once outcomes_test.go
E3 kept while nobody reads; survives Factory and engine restarts outcomes_test.go, serve/job_events_test.go TestJobEventsReadCursorAndHandlerRestart, cmd/codeaf TestAppFactoryJobEventsReadAndEngineRestart
E4 bounded at 2,000 with a floor outcomes_test.go
E5 old/absent/ahead cursor and older-writer gap reset, never skip; cursor 0 on an empty record is a normal read outcomes_test.go, serve/job_events_test.go
E6 caught-up cursor moves past other notifications outcomes_test.go, serve/job_events_test.go
E7 visibility TestJobEventsVisibilityMovesPastHiddenJobs
E8 push once, in order; one follower; stops on close/detach; backoff TestAppFactoryJobEventsPushOnceAndClose, …DetachEndsFollower, …RetryAndReset, …EndingErrors, …CloseCancelsBlockedDelivery, …FollowerBound
E9 older Factory → supported: false TestAppFactoryJobEventsOlderFactory
E10 feature + capability internal/appwire TestFactoryJobEventsCapability, cmd/codeaf read test
E11 long poll TestJobEventsLongPollReturnsOnOutcome, …ImmediateAndTimeout
E12 live record unchanged existing internal/factory/live and serve tests unedited; contiguous-ids test in outcomes_test.go
E13 refused write restores both records outcomes_test.go
E14 params and shapes TestFactoryJobEventsParamsAndShape, TestAppFactoryJobEventsQueryAndMapping

Left

  • group is carried on the wire but empty until the Factory item has a group field (another package adds it); one assignment is owed then.
  • Built for the Workstreams plan (Factory job events), in Factory terms only.

🤖 Generated with Claude Code

AbirAbbas and others added 4 commits November 5, 2026 12:23
… cursor

A job that merged, finished, failed or needs a person is recorded at the store
write, beside the live notifications and under the same lock, in its own
job-outcomes.json: a sequence of its own, the newest 2,000 kept, and a floor
below which a reader resets instead of skipping. The live record, its ids and
/v1/events are unchanged. An older binary writing the same folder drops the
outcomes_through marker, which the next commit turns into a gap.

GET /v1/job-events answers the outcomes after a cursor for the jobs the caller
may see, with an optional long poll of up to 15 seconds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… them

factory.events_since {factory, after?, limit?, follow?} answers the Factory's
job outcomes after a cursor, or a reset when the cursor is absent, older than
what the Factory keeps, or crosses a gap. With follow, one follower per
factory long-polls the Factory and pushes each later outcome as factory.event.
A Factory older than the route answers supported false. Hello feature
factory.job_events, capability job_events.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tach test deterministic

The store's update snapshot shares the stream, merging and follow-up with the
item the callback edits, so an in-place change could hide a needs-you change
from the outcome comparison. Outcomes now compare against their own deep copy
taken before the callback; live.Changes keeps its snapshot as it was.

The follower test now drives its retry wait and observes the outstanding long
poll instead of racing a real backoff; a follower whose connection is gone ends
at once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@AbirAbbas
AbirAbbas force-pushed the workstreams/fx-events branch from 0d5464a to cf60cfc Compare November 5, 2026 17:23
@AbirAbbas
AbirAbbas merged commit 1013f05 into workstreams/main Nov 5, 2026
7 checks passed
@AbirAbbas
AbirAbbas deleted the workstreams/fx-events branch November 5, 2026 18:22
AbirAbbas added a commit that referenced this pull request Nov 13, 2026
#2616)

* Factory job outcomes: a durable cursor read and a pushed factory.event

Carried from #2416 on workstreams/main (closed integration PR #2306), with the
follow-up that reads a job's visibility from its saved copy. On dev a job a
person stopped waits in Backlog and is not needs-you, so a stop records no
outcome; the two stop cases of the stored-outcome test say so.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>

* Factory job events: the capability sits on lines of its own choosing

So this change and the other Factory additions carried from workstreams/main
merge past each other.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>

* changes: entry for this pull request

Co-Authored-By: Claude Mythos 5.2 <[email protected]>

* Factory job outcomes: a wait on a person is recorded once

A job waiting at the plan gate recorded another identical approve_plan
outcome on every write while it waited: each runner report, priority and
budget change pushed one more factory.event (six for one approval in the
hand test). Store.update compared the new job against live.OutcomeBasis, a
copy of only some fields. The copy dropped each step's kind and round, so
the waiting gate read as a hold before a work step (approve_step) while the
job answered approve_plan, and it dropped the paused-decision mark, so a
paused question's reason differed too; the need looked changed on every
write.

Store.update now hands its callback a decoded copy of the job, so the job
it read is intact after the change. Outcomes compare that whole prior job,
as Save already did, and the partial copy is gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a person's stop is no failed job

factory.job_stop at the plan gate recorded and pushed a failed outcome
(code stopped, step approve), which the desktop app takes as a failed job
and wakes its agent for. Every road that ends a run on a person's word
cuts the step it finds under way and writes the stop on that step and on
the job as a failure coded stopped: Stop at a gate or in a running step
(the service's Stop door and the local runner's), a step's own Stop, a no
to the stop after a step, Close, Reject, the reset before a reassignment
and a cancel out of attempts. live.Outcomes recorded every new failure as
failed, so each of these did, and Close and Reject recorded a failed
beside their finished. The store test that says a stop is quiet only
flipped the person-stop mark and never wrote that failure.

A failure coded stopped, or any failure on a job a person stopped (it
waits in Backlog), is no failed outcome now, as the history record already
skips it. Tests drive the real stop writes: the store with the service
door's write, every lease door, and the local loop's Stop at a gate, Stop
mid-step and the stop after a step. A step's own failure and a lost runner
still record failed once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a refused publish is recorded once

A check-only job whose publish GitHub refused recorded publish_failed
twice, at 17:38:54.849 and 17:38:55.360, two occurrences for the desktop
app. On a Factory service the runner reports the failure while the
service's copy of the job still runs the publish, and the runner's done
then lands the job. factory.NeedsSince read the landed run's clock only
for a landed job, so the same wait answered the current step's clock at
the report and the run's end at the done; the outcome saw its wait start
move and recorded it again, and factory.job's needs_you.since moved with
it.

A failed publish now waits from its own clock, the time the publish
failed, whatever state the job's copy is in. factory.job answers that
time too, so a publish retried and refused again is a new wait rather
than the first refusal's. The change entry says so, and names the stop,
Close and Reject rule the earlier fix enforces.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: pin a local close as one finished outcome

The stored-outcome table had merged, answered, closed and dropped but no
job closed locally, the finish a person's Close writes; it records one
finished outcome with how closed_locally at the job's done time.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a parked budget is recorded once

A worker parked at its budget sends the cap question again, with no
clock, on every later report that carries thread messages, usage or
acknowledgements. reportQuestion stamped each one with the report's
arrival, so needs_you.since moved and every report recorded another
budget needs_you the desktop app read as a new wait. The same wait sent
again (the ask still stands, same kind and step, same words or a cap)
now keeps the clock it was first asked at and adds no second ask line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a gap resets once and the follower waits

While a Factory's live record had a gap an older writer left
(outcomes_through != next), every job-events read answered reset at
once, even with wait=15, and the engine's follower, reset to the
cursor it asked, read again with no delay: back-to-back requests, each
settling every job, until something committed.

The Factory read that finds the gap now closes it (live.HealOutcomes,
the same move the next commit makes), so a reader resets once to a
cursor past it. The follower waits after a reset that left its cursor
where it was, 2 seconds doubling to a minute, for Factories without
the repair.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: an undecided repository is not passed

A signed-in caller's job-events cursor moved past every outcome it was
not shown, including one whose repository check had only failed for a
moment (GitHub 5xx or a transport error, not cached): the caller saved
the cursor and lost the outcome for good.

repoAllowed now says whether GitHub answered (repoAccessDecided names
the keys it did not). The job-events page ends before an undecided
outcome with more: true, and a read that would start at one answers
503 try again, so the cursor stays and the caller reads it later.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a failed outcome says what factory.job says

personStop suppressed every failure coded stopped, but only a job
StoppedByPerson waits in Backlog. Roads that end a run without the
person-stop mark left factory.job showing a failed job in Needs you
(failure stopped, Stopped by you) while events recorded nothing: the
reset before a change of assignee (PersonStopped=&false), an unheld
cancel out of attempts, and the loop's own stops on a cancel, a no to
the budget or a send back with nowhere to go, once a Start had cleared
an earlier stop.

live.Outcomes now records failed by JobState: nothing in Backlog,
always in Needs you, and on a done job anything but a stop (a Close or
Reject is its finished). Each person's-word road now marks the stop
as the person's (AnswerUnheld cancel, floorLoop.stop when the loop's
stop is a person's), and a reset hands the job on in Backlog: it drops
the job-level stop it wrote, the cut step keeps it.

The served copy of a loop-made stop is the next commit; the change
entry, PROTOCOL and manual text here already describe both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a runner's stop is the person's on the served copy

On a Factory service a cancel out of attempts or a no to the budget
reaches the runner that holds the job; its loop ends the run on its own
copy, marks the stop there, and reports the cut step. The service's
copy, which factory.job reads and outcomes are recorded from, learned
of the stop only through StampFailure and never of the mark, so a job
a Start had cleared read as a failed job in Needs you while the
runner's copy waited in Backlog.

reportStep now takes a reported step failure coded stopped as the
person's stop: only a person's word writes that code on a runner.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* PROTOCOL: how a full factory.job read keys the same occurrences

Events and a full factory.job read gave different occurrence keys for
a person's stop: factory.job still carries failure {code: stopped, at}
on a Backlog job (and on a closed or rejected one), and a client keying
failed:<at> from every failure woke for a stop events never record. A
failed job's wait also keyed needs_you:<kind>:<since> from the full
read, which events never record beside failed.

factory.job already says which is which (state backlog, done or
needs_you; desk.person_stopped), so nothing is added to the wire. The
outcomes section now states the rule a full read follows; the desktop
app's workstream_watch_occurrences needs the matching filter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a full factory.job read keys only recorded occurrences

Each stop road in the agreement table now also checks that the
occurrence keys a full factory.job read forms, by the rule PROTOCOL.md
gives (failed only in needs_you, or on a done job when it is no stop;
a wait only on a job with no failure), are all keys the outcomes
recorded. Keyed the way the desktop app keys a full read today (every
failure, every wait), a person's stop, a Close, a Reject and a runner's
stop each key a failed occurrence no outcome has, and a failed job a
needs_you:failed one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs: which answers stop a job when no runner holds it

With no runner holding the job, a send back with no earlier step is
refused ("there is no previous work step to send back") and a no to the
budget is kept as the answer for the next round; only a cancel stops
it there. The manual and the change entry now say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a wait with no clock of its own waits from when it began (item 1)

A start held for approval, a take-over, an AgentField run's approval, a
Stop undone at a gate, a failed merge and a pull request's review or
conflict wait on a person with no failure, ask or step clock. NeedsSince
fell back to Changed, which every stamped write moves, so a rename, a
priority, a budget or a note each recorded another needs_you outcome,
which the desktop app keys as a new occurrence, and moved factory.job's
needs_you.since with it.

Every store write now keeps Item.NeedsAt (factory.NoteWait): stamped by
the write that began a wait, kept while the job waits with the same need,
question kind and question, cleared when it waits on nobody. NeedsSince
reads it before Changed. A waiting job written before the clock was kept
goes on from the clock it showed, so its next write records nothing.
NeedsAt is store bookkeeping: it is outside the action revision and the
wire carries it as needs_you.since.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a job handed on takes its ask down (item 2)

A change of assignee or an unassign of a job waiting at a gate or out of
attempts left it in Backlog with the cut run's ask standing (Asking With
you), so factory.job answered backlog with waiting_since, which the
desktop app's full read keys as a wait on a person.

The lease reset that cuts the run now takes the ask down with it, as a
person's stop does, and the unassign write takes down an ask a failed run
left (no reset cuts a run that already ended). The stop-road table now
checks that a job in any state but needs_you shows no waiting_since and
no needs_you, and its full-read keys model the desktop app's
waiting_since fallback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a run a hand-on cut offers Run as new (item 4)

The reset that hands a job on stopped writing its stop on the job as a
failure, so RunEnded and CanRunAsNew turned false and Run as new went
away for the new person (the run_as_new door refused it), for a run that
had ended all the same. CanRunAsNew now also reads a new job whose run
ended with its current step cut by a stop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs: wait clocks, a hand-on's ask and Run as new (items 1, 2, 4)

The change entry, PROTOCOL's job outcomes and run-as-new sections and the
manual's hand-on section say that needs_you.since is when the wait began,
that a change of assignee or an unassign takes down the cut run's
question and offers Run as new, and how a full read keys a wait.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Factory job outcomes: a GitHub rate limit is an undecided repository (item 3)

A rate-limited visibility check (403 with no requests remaining, or a 429
secondary limit) was decided as hidden: the job-events cursor passed the
outcome, the denial was kept for the visibility TTL, and a signed-in
caller whose token was rate-limited lost the outcome for good.

Since the merge of origin/dev (#2618) a spent limit returns GitHub's retry
time and is never cached as a denial, and the merge's resolution answers a
remembered limit as not known. The page now ends before such an outcome, a
read that would start at it answers try again (503), and once the limit
resets the outcome is shown. TestJobEventsWaitOutAGitHubRateLimit pins both
a primary and a secondary limit; the GitHub fake can answer a limit with
its headers. PROTOCOL and the change entry name the rate limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Mythos 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant