---
title: "Job System Design Thesis"
description: "Job System Design Thesis - URL: https://chatgpt.com/c/6a760a97-eae0-83ea-8da0-94c18285abb1 - Created: 2026-08-07T16:40:56.564Z --- I want to design a job system to run indexing in production as the …"
doc_version: 1
last_updated: 2026-11-10
canonical_url: "http://parc.yolo.scapegoat.dev/note/transcripts/2026/08/08/chatgpt-transcript-job-system-design-thesis"
---

# Job System Design Thesis

Job System Design Thesis - URL: https://chatgpt.com/c/6a760a97-eae0-83ea-8da0-94c18285abb1 - Created: 2026-08-07T16:40:56.564Z --- > I want to design a job system to run indexing in production as the …

## Content

# Job System Design Thesis

- URL: https://chatgpt.com/c/6a760a97-eae0-83ea-8da0-94c18285abb1
- Created: 2026-08-07T16:40:56.564Z

---

> I want to design a job system to run indexing in production as the DB changes for these RAG systems (both TTC, GEC), and even evaluation batch runs and all that.
>
> Maybe based on riverqueue, maybe my own. WIth proper tracking and retries and stuff like that.
>
> Based on sound category theory / abstract mathematics foundations.
>
> design, implement and Write a full technical report / doctoral thesis to have sound compositional and semantics (denotational, operational) system.

<details><summary>💭 Thinking</summary>

**Planning the implementation**

I’m inspecting the uploaded codebase and the document-generation requirements first. Then I’ll map the abstract design onto the existing architecture, implement it, and produce a polished technical report with validation notes.

</details>

```unknown
bash -lc ls -lh /mnt/data && file '/mnt/data/rag-ttc(3).zip' && unzip -l '/mnt/data/rag-ttc(3).zip' | sed -n '1,220p'
```

I’ll first map the RAG codebase and indexing/evaluation workflows, then design and implement the compositional job subsystem with formal semantics, operational guidance, and verification artifacts.

<details><summary>💭 Thinking</summary>

**Formalizing the design**

I’m separating proven algebraic properties from engineering assumptions. I’ll focus on compositional plans, deterministic replay, idempotent changes, leases, retries, and keyed evaluation, while avoiding claims stronger than the implementation supports.

**Formalized the design**

**Choosing integration boundaries**

I’m comparing database and queue options against the existing repositories. My likely approach is a backend-neutral job core, a practical SQL adapter, and optional queue integration, keeping product code decoupled from infrastructure choices.

**Chose integration boundaries**

**Inspected archive contents, project instructions, architecture, dependencies, and design documentation**

</details>

```unknown
bash -lc BASE=/mnt/data/rag-ttc-work/code/gec/2026-03-16--gec-rag; printf 'knowledgebuild tree\n'; find "$BASE/internal/knowledgebuild" -maxdepth 2 -type f -printf '%f\n' | sort; printf '\nheadings/symbols\n'; rg -n '^(type|func|const|var) ' "$BASE/internal/knowledgebuild" "$BASE/internal/knowledge" | sed -n '1,260p'; printf '\nBuild source\n'; sed -n '1,320p' "$BASE/internal/knowledgebuild/build.go"; printf '\nConnectors source\n'; sed -n '1,320p' "$BASE/internal/knowledgebuild/connectors.go"; printf '\nEval source\n'; sed -n '1,300p' "$BASE/internal/knowledge/eval.go"
```

The existing refresh-control-plane design becomes prior work; I’ll center durable scheduling in shared `ragkit`, leveraging immutable index bundles and evaluation primitives rather than duplicating TTC/GEC queue logic.

<details><summary>💭 Thinking</summary>

**Inspected GEC knowledge build files, references, and RAG architecture**

</details>

```json
{"search_query":[{"q":"site:riverqueue.com/docs River Go job queue unique jobs retries periodic jobs official"},{"q":"site:github.com/riverqueue/river README retries unique jobs transactions official"},{"q":"site:postgresql.org/docs SKIP LOCKED SELECT FOR UPDATE official PostgreSQL 18"},{"q":"site:dev.mysql.com/doc/refman/8.4/en locking-read.html SKIP LOCKED official"}],"response_length":"long"}
```

<details><summary>💭 Thinking</summary>

**Searched 4 websites**

</details>

```unknown
bash -lc go version || true; which go; env | rg '^(GOMODCACHE|GOPATH|GOWORK|GOPROXY)=' | sort
```

<details><summary>💭 Thinking</summary>

**Testing implementation constraints**

I’m checking language-version compatibility and repository completeness before committing to a package layout. I’ll keep the formal core isolated enough to validate independently, then connect it to available application code without overstating integration coverage.

</details>

```unknown
bash -lc command -v postgres || true; command -v initdb || true; command -v psql || true; command -v docker || true; command -v podman || true; command -v sqlite3 || true
```

<details><summary>💭 Thinking</summary>

**Checked Go environment, tool compatibility, and cached dependencies**

</details>

```json
{"open":[{"ref_id":"https://github.com/riverqueue/river/archive/refs/tags/v0.43.0.tar.gz"}],"response_length":"short"}
```

<details><summary>💭 Thinking</summary>

**Searching the web**

</details>

```json
{"search_query":[{"q":"https://github.com/riverqueue/river/archive/refs/tags/v0.43.0.tar.gz"}],"response_length":"short"}
```

<details><summary>💭 Thinking</summary>

**Searched 1 website**

</details>

```unknown
bash -lc DOC='/mnt/data/rag-ttc-work/code/gec/2026-03-16--gec-rag/ttmp/2026/08/04/GEC-RAG-PROD-001--ground-up-production-rag-architecture-for-coinvault/design-doc/01-ground-up-production-rag-architecture-and-implementation-guide.md'; wc -l "$DOC"; rg -n '^#{1,4} ' "$DOC" | sed -n '1,220p'; sed -n '1,260p' "$DOC"
```

<details><summary>💭 Thinking</summary>

**Analyzed RAG architecture, index files, and documentation tools**

</details>

```json
{"open":[{"ref_id":"turn867793search1"},{"ref_id":"turn867793search4"},{"ref_id":"turn867793search3"}],"response_length":"long"}
```

<details><summary>💭 Thinking</summary>

**Synthesizing the implementation**

I’m basing the design on the uploaded repositories and prior scheduling proposal, while keeping the formal core transport-independent. I’ll validate a standalone reference implementation and clearly separate proven properties from operational assumptions.

**Searched 2 websites**

</details>

```unknown
bash -lc sed -n '1,240p' /home/oai/skills/docx/SKILL.md && printf '\n---PDF---\n' && sed -n '1,260p' /home/oai/skills/pdfs/SKILL.md
```

<details><summary>💭 Thinking</summary>

**Inspected project files, workflows, evaluation files, and design document sections**

</details>

```json
{"search_query":[{"q":"site:riverqueue.com/docs Go River v0.43 resumable steps transactional enqueue unique jobs worker docs"},{"q":"site:pkg.go.dev/github.com/riverqueue/river v0.43.0 ResumableStep WorkerDefaults InsertTx AddWorker"},{"q":"site:riverqueue.com/docs workflows resumable steps River Pro durable periodic jobs"}],"response_length":"long"}
```

I’ll separate workflow semantics from River delivery: a durable control-plane ledger owns identities, leases, fencing, checkpoints, artifacts, retries, and activation. Refreshes coalesce changes into coarse product/channel jobs, with deterministic reference executors.

<details><summary>💭 Thinking</summary>

**Searched 1 website**

</details>

```unknown
bash -lc set -euo pipefail
ROOT=/mnt/data/jobgraph
rm -rf "$ROOT"
mkdir -p "$ROOT"/{jobgraph,queue,postgres/migrations,changes,ragjobs,riveradapter,cmd/jobgraph-demo,docs,examples}
cat > "$ROOT/go.mod" <<'EOF'
module github.com/go-go-golems/jobgraph

go 1.23
EOF
cat > "$ROOT/LICENSE" <<'EOF'
MIT License

Copyright (c) 2026

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
EOF
cat > "$ROOT/jobgraph/doc.go" <<'EOF'
// Package jobgraph implements a typed, compositional calculus for durable
// jobs. Programs are assembled from typed atoms with sequential, parallel,
// fan-out, and keyed-traversal combinators. An Executor interprets a program
// into an event ledger and a checkpoint store.
//
// The package deliberately does not own distributed admission. The queue
// packages provide that outer responsibility. This separation lets the same
// program run under River, a native PostgreSQL queue, a local worker, or a
// test harness without changing its denotation.
package jobgraph
EOF
cat > "$ROOT/jobgraph/types.go" <<'EOF'
package jobgraph

import (
	"crypto/rand"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"strings"
	"time"
)

// Pair is the product object used by Tensor and Fanout.
type Pair[A, B any] struct {
	First  A `json:"first"`
	Second B `json:"second"`
}

// ReuseScope determines where a successful atom result may be replayed.
type ReuseScope string

const (
	// ReuseNone disables checkpoint lookup and is suitable only for cheap,
	// deterministic work. The successful result is still recorded as an event.
	ReuseNone ReuseScope = "none"
	// ReuseRun allows replay within the same run and structural node path.
	ReuseRun ReuseScope = "run"
	// ReuseGlobal allows content-addressed reuse across runs. The atom name,
	// version, and semantic input digest form the cache identity.
	ReuseGlobal ReuseScope = "global"
)

// EffectKind states the replay law promised by an atom implementation.
type EffectKind string

const (
	// EffectPure means the atom is deterministic and has no externally visible
	// side effects for a fixed semantic input.
	EffectPure EffectKind = "pure"
	// EffectIdempotent means repetitions with the supplied IdempotencyKey have
	// one observational effect.
	EffectIdempotent EffectKind = "idempotent"
	// EffectFenced means externally visible commits reject stale fencing tokens.
	EffectFenced EffectKind = "fenced"
	// EffectUnsafe is intentionally rejected by the durable executor because a
	// crash after the effect but before checkpoint commit creates ambiguity.
	EffectUnsafe EffectKind = "unsafe"
)

// StepContext is immutable execution metadata supplied to an atom.
type StepContext struct {
	RunID          string `json:"run_id"`
	NodePath       string `json:"node_path"`
	AtomID         string `json:"atom_id"`
	Attempt        int    `json:"attempt"`
	IdempotencyKey string `json:"idempotency_key"`
	FencingToken   int64  `json:"fencing_token"`
}

// RunOptions control one interpretation of a Program.
type RunOptions struct {
	RunID        string
	Labels       map[string]string
	FencingToken int64
}

// RunResult names the durable run and returns its typed output.
type RunResult[O any] struct {
	RunID      string
	PlanDigest string
	Output     O
}

// Digest returns a stable SHA-256 hex digest.
func Digest(data []byte) string {
	sum := sha256.Sum256(data)
	return hex.EncodeToString(sum[:])
}

func digestJSON(v any) (string, error) {
	data, err := json.Marshal(v)
	if err != nil {
		return "", fmt.Errorf("marshal digest input: %w", err)
	}
	return Digest(data), nil
}

func defaultEncode[T any](value T) ([]byte, error) {
	data, err := json.Marshal(value)
	if err != nil {
		return nil, fmt.Errorf("encode JSON: %w", err)
	}
	return data, nil
}

func defaultDecode[T any](data []byte) (T, error) {
	var value T
	if err := json.Unmarshal(data, &value); err != nil {
		return value, fmt.Errorf("decode JSON: %w", err)
	}
	return value, nil
}

func newRunID(now time.Time) (string, error) {
	var entropy [10]byte
	if _, err := rand.Read(entropy[:]); err != nil {
		return "", fmt.Errorf("generate run entropy: %w", err)
	}
	return fmt.Sprintf("run-%s-%s", now.UTC().Format("20060102T150405.000000000Z"), hex.EncodeToString(entropy[:])), nil
}

func cleanName(s string) string {
	s = strings.TrimSpace(s)
	s = strings.ReplaceAll(s, "/", "_")
	return s
}
EOF
cat > "$ROOT/jobgraph/descriptor.go" <<'EOF'
package jobgraph

import (
	"encoding/json"
	"fmt"
	"sort"
)

// Descriptor is the serializable syntax tree of a Program. It is deliberately
// data-only so it can be inspected without executing user code.
type Descriptor struct {
	Kind     string            `json:"kind"`
	Name     string            `json:"name,omitempty"`
	Version  string            `json:"version,omitempty"`
	Metadata map[string]string `json:"metadata,omitempty"`
	Children []Descriptor      `json:"children,omitempty"`
}

// Canonical returns a copy with deterministic child metadata serialization.
// encoding/json already sorts map keys; this method removes empty maps and
// recursively canonicalizes children to make the contract explicit.
func (d Descriptor) Canonical() Descriptor {
	out := d
	if len(out.Metadata) == 0 {
		out.Metadata = nil
	} else {
		keys := make([]string, 0, len(out.Metadata))
		for key := range out.Metadata {
			keys = append(keys, key)
		}
		sort.Strings(keys)
		copyMap := make(map[string]string, len(keys))
		for _, key := range keys {
			copyMap[key] = out.Metadata[key]
		}
		out.Metadata = copyMap
	}
	if len(out.Children) == 0 {
		out.Children = nil
	} else {
		out.Children = make([]Descriptor, len(d.Children))
		for i, child := range d.Children {
			out.Children[i] = child.Canonical()
		}
	}
	return out
}

// Digest is the semantic plan identity used to reject accidental resume with
// a different workflow definition.
func (d Descriptor) Digest() (string, error) {
	data, err := json.Marshal(d.Canonical())
	if err != nil {
		return "", fmt.Errorf("marshal program descriptor: %w", err)
	}
	return Digest(data), nil
}
EOF
cat > "$ROOT/jobgraph/clock.go" <<'EOF'
package jobgraph

import (
	"context"
	"time"
)

// Clock makes time and retry sleeping deterministic in tests.
type Clock interface {
	Now() time.Time
	Sleep(context.Context, time.Duration) error
}

type realClock struct{}

func (realClock) Now() time.Time { return time.Now().UTC() }

func (realClock) Sleep(ctx context.Context, d time.Duration) error {
	if d <= 0 {
		return nil
	}
	timer := time.NewTimer(d)
	defer timer.Stop()
	select {
	case <-ctx.Done():
		return ctx.Err()
	case <-timer.C:
		return nil
	}
}
EOF

```

<details><summary>💭 Thinking</summary>

**Implemented composable Jobgraph with storage, retries, events, and resume handling**

</details>

```unknown
bash -lc set -euo pipefail
ROOT=/mnt/data/jobgraph
cat > "$ROOT/queue/doc.go" <<'EOF'
// Package queue provides the outer, at-least-once admission and lease layer
// for coarse durable jobs. Its fencing-token contract prevents stale workers
// from recording completion after ownership has moved to a newer lease.
package queue
EOF
cat > "$ROOT/queue/model.go" <<'EOF'
package queue

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"time"

	"github.com/go-go-golems/jobgraph/jobgraph"
)

// State is the durable job lifecycle.
type State string

const (
	StateAvailable State = "available"
	StateRunning   State = "running"
	StateRetryable State = "retryable"
	StateSucceeded State = "succeeded"
	StateFailed    State = "failed"
	StateRejected  State = "rejected"
	StateCanceled  State = "canceled"
)

func (s State) Terminal() bool {
	return s == StateSucceeded || s == StateFailed || s == StateRejected || s == StateCanceled
}

// Job is the durable queue record. Payload should contain references and
// identities, not source documents or secrets.
type Job struct {
	ID             int64             `json:"id"`
	Queue          string            `json:"queue"`
	Kind           string            `json:"kind"`
	Payload        json.RawMessage   `json:"payload"`
	UniqueKey      string            `json:"unique_key,omitempty"`
	State          State             `json:"state"`
	Priority       int               `json:"priority"`
	Attempt        int               `json:"attempt"`
	MaxAttempts    int               `json:"max_attempts"`
	ScheduledAt    time.Time         `json:"scheduled_at"`
	CreatedAt      time.Time         `json:"created_at"`
	UpdatedAt      time.Time         `json:"updated_at"`
	LeaseOwner     string            `json:"lease_owner,omitempty"`
	LeaseExpiresAt time.Time         `json:"lease_expires_at,omitempty"`
	FencingToken   int64             `json:"fencing_token"`
	ErrorClass     jobgraph.ErrorClass `json:"error_class,omitempty"`
	LastError      string            `json:"last_error,omitempty"`
	Metadata       map[string]string `json:"metadata,omitempty"`
}

// EnqueueRequest specifies an available or future job.
type EnqueueRequest struct {
	Queue       string
	Kind        string
	Payload     json.RawMessage
	UniqueKey   string
	Priority    int
	MaxAttempts int
	ScheduledAt time.Time
	Metadata    map[string]string
}

// EnqueueResult reports uniqueness suppression explicitly.
type EnqueueResult struct {
	Job      Job
	Inserted bool
}

// Lease is the proof of current ownership. Every mutation after Claim must
// compare both owner and fencing token.
type Lease struct {
	JobID          int64
	Owner          string
	FencingToken   int64
	ExpiresAt      time.Time
}

// Failure tells the backend how to transition a failed attempt.
type Failure struct {
	Class       jobgraph.ErrorClass
	Message     string
	RetryAt     time.Time
}

// Backend is implementable by memory, PostgreSQL, or an adapter over River.
type Backend interface {
	Enqueue(context.Context, EnqueueRequest) (EnqueueResult, error)
	Claim(context.Context, string, string, time.Duration) (Job, Lease, bool, error)
	Heartbeat(context.Context, Lease, time.Duration) (Lease, error)
	Complete(context.Context, Lease) error
	Fail(context.Context, Lease, Failure) error
	Cancel(context.Context, int64, string) error
	Get(context.Context, int64) (Job, bool, error)
	List(context.Context, ListFilter) ([]Job, error)
}

// ListFilter supports operator views without exposing backend query syntax.
type ListFilter struct {
	Queue  string
	Kind   string
	States []State
	Limit  int
}

var (
	ErrLeaseLost       = errors.New("job lease lost")
	ErrInvalidJob      = errors.New("invalid job")
	ErrUnknownJob      = errors.New("unknown job")
	ErrInvalidTransition = errors.New("invalid job state transition")
)

func validateEnqueue(request EnqueueRequest) error {
	if request.Queue == "" || request.Kind == "" {
		return fmt.Errorf("%w: queue and kind are required", ErrInvalidJob)
	}
	if !json.Valid(request.Payload) {
		return fmt.Errorf("%w: payload is not valid JSON", ErrInvalidJob)
	}
	if request.MaxAttempts <= 0 {
		request.MaxAttempts = 1
	}
	return nil
}

func cloneJob(job Job) Job {
	job.Payload = append(json.RawMessage(nil), job.Payload...)
	if len(job.Metadata) > 0 {
		metadata := make(map[string]string, len(job.Metadata))
		for key, value := range job.Metadata {
			metadata[key] = value
		}
		job.Metadata = metadata
	}
	return job
}
EOF
cat > "$ROOT/queue/memory.go" <<'EOF'
package queue

import (
	"context"
	"fmt"
	"sort"
	"sync"
	"time"

	"github.com/go-go-golems/jobgraph/jobgraph"
)

// MemoryBackend implements the complete lease/fencing state machine.
type MemoryBackend struct {
	mu     sync.Mutex
	now    func() time.Time
	nextID int64
	jobs   map[int64]Job
}

func NewMemoryBackend(now func() time.Time) *MemoryBackend {
	if now == nil {
		now = func() time.Time { return time.Now().UTC() }
	}
	return &MemoryBackend{now: now, nextID: 1, jobs: map[int64]Job{}}
}

func (b *MemoryBackend) Enqueue(ctx context.Context, request EnqueueRequest) (EnqueueResult, error) {
	if err := ctx.Err(); err != nil {
		return EnqueueResult{}, err
	}
	if err := validateEnqueue(request); err != nil {
		return EnqueueResult{}, err
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	if request.UniqueKey != "" {
		for _, job := range b.jobs {
			if job.Queue == request.Queue && job.UniqueKey == request.UniqueKey && !job.State.Terminal() {
				return EnqueueResult{Job: cloneJob(job), Inserted: false}, nil
			}
		}
	}
	now := b.now()
	if request.ScheduledAt.IsZero() {
		request.ScheduledAt = now
	}
	if request.MaxAttempts <= 0 {
		request.MaxAttempts = 1
	}
	job := Job{
		ID: b.nextID, Queue: request.Queue, Kind: request.Kind,
		Payload: append([]byte(nil), request.Payload...), UniqueKey: request.UniqueKey,
		State: StateAvailable, Priority: request.Priority, MaxAttempts: request.MaxAttempts,
		ScheduledAt: request.ScheduledAt.UTC(), CreatedAt: now, UpdatedAt: now,
		Metadata: cloneMetadata(request.Metadata),
	}
	b.nextID++
	b.jobs[job.ID] = job
	return EnqueueResult{Job: cloneJob(job), Inserted: true}, nil
}

func (b *MemoryBackend) Claim(ctx context.Context, queueName, owner string, leaseDuration time.Duration) (Job, Lease, bool, error) {
	if err := ctx.Err(); err != nil {
		return Job{}, Lease{}, false, err
	}
	if queueName == "" || owner == "" || leaseDuration <= 0 {
		return Job{}, Lease{}, false, fmt.Errorf("%w: queue, owner, and positive lease duration are required", ErrInvalidJob)
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	now := b.now()
	candidates := make([]Job, 0)
	for _, job := range b.jobs {
		if job.Queue != queueName || job.ScheduledAt.After(now) || job.State.Terminal() {
			continue
		}
		eligible := job.State == StateAvailable || job.State == StateRetryable || (job.State == StateRunning && !job.LeaseExpiresAt.After(now))
		if eligible {
			candidates = append(candidates, job)
		}
	}
	if len(candidates) == 0 {
		return Job{}, Lease{}, false, nil
	}
	sort.Slice(candidates, func(i, j int) bool {
		if candidates[i].Priority != candidates[j].Priority {
			return candidates[i].Priority > candidates[j].Priority
		}
		if !candidates[i].ScheduledAt.Equal(candidates[j].ScheduledAt) {
			return candidates[i].ScheduledAt.Before(candidates[j].ScheduledAt)
		}
		return candidates[i].ID < candidates[j].ID
	})
	job := b.jobs[candidates[0].ID]
	job.State = StateRunning
	job.Attempt++
	job.FencingToken++
	job.LeaseOwner = owner
	job.LeaseExpiresAt = now.Add(leaseDuration)
	job.UpdatedAt = now
	b.jobs[job.ID] = job
	lease := Lease{JobID: job.ID, Owner: owner, FencingToken: job.FencingToken, ExpiresAt: job.LeaseExpiresAt}
	return cloneJob(job), lease, true, nil
}

func (b *MemoryBackend) Heartbeat(ctx context.Context, lease Lease, leaseDuration time.Duration) (Lease, error) {
	if err := ctx.Err(); err != nil {
		return Lease{}, err
	}
	if leaseDuration <= 0 {
		return Lease{}, fmt.Errorf("%w: positive lease duration is required", ErrInvalidJob)
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	job, ok := b.jobs[lease.JobID]
	if !ok {
		return Lease{}, ErrUnknownJob
	}
	if !leaseMatches(job, lease) || job.State != StateRunning {
		return Lease{}, ErrLeaseLost
	}
	now := b.now()
	job.LeaseExpiresAt = now.Add(leaseDuration)
	job.UpdatedAt = now
	b.jobs[job.ID] = job
	lease.ExpiresAt = job.LeaseExpiresAt
	return lease, nil
}

func (b *MemoryBackend) Complete(ctx context.Context, lease Lease) error {
	if err := ctx.Err(); err != nil {
		return err
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	job, ok := b.jobs[lease.JobID]
	if !ok {
		return ErrUnknownJob
	}
	if !leaseMatches(job, lease) || job.State != StateRunning {
		return ErrLeaseLost
	}
	now := b.now()
	job.State = StateSucceeded
	job.LeaseOwner = ""
	job.LeaseExpiresAt = time.Time{}
	job.UpdatedAt = now
	job.ErrorClass = ""
	job.LastError = ""
	b.jobs[job.ID] = job
	return nil
}

func (b *MemoryBackend) Fail(ctx context.Context, lease Lease, failure Failure) error {
	if err := ctx.Err(); err != nil {
		return err
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	job, ok := b.jobs[lease.JobID]
	if !ok {
		return ErrUnknownJob
	}
	if !leaseMatches(job, lease) || job.State != StateRunning {
		return ErrLeaseLost
	}
	now := b.now()
	job.ErrorClass = failure.Class
	job.LastError = failure.Message
	job.LeaseOwner = ""
	job.LeaseExpiresAt = time.Time{}
	job.UpdatedAt = now
	switch failure.Class {
	case jobgraph.ErrorTransient:
		if job.Attempt < job.MaxAttempts {
			job.State = StateRetryable
			if failure.RetryAt.IsZero() {
				failure.RetryAt = now
			}
			job.ScheduledAt = failure.RetryAt.UTC()
		} else {
			job.State = StateFailed
		}
	case jobgraph.ErrorRejected:
		job.State = StateRejected
	case jobgraph.ErrorCanceled:
		job.State = StateCanceled
	default:
		job.State = StateFailed
	}
	b.jobs[job.ID] = job
	return nil
}

func (b *MemoryBackend) Cancel(ctx context.Context, jobID int64, reason string) error {
	if err := ctx.Err(); err != nil {
		return err
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	job, ok := b.jobs[jobID]
	if !ok {
		return ErrUnknownJob
	}
	if job.State.Terminal() {
		return nil
	}
	job.State = StateCanceled
	job.ErrorClass = jobgraph.ErrorCanceled
	job.LastError = reason
	job.LeaseOwner = ""
	job.LeaseExpiresAt = time.Time{}
	job.UpdatedAt = b.now()
	b.jobs[job.ID] = job
	return nil
}

func (b *MemoryBackend) Get(ctx context.Context, jobID int64) (Job, bool, error) {
	if err := ctx.Err(); err != nil {
		return Job{}, false, err
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	job, ok := b.jobs[jobID]
	return cloneJob(job), ok, nil
}

func (b *MemoryBackend) List(ctx context.Context, filter ListFilter) ([]Job, error) {
	if err := ctx.Err(); err != nil {
		return nil, err
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	stateSet := map[State]struct{}{}
	for _, state := range filter.States {
		stateSet[state] = struct{}{}
	}
	jobs := make([]Job, 0)
	for _, job := range b.jobs {
		if filter.Queue != "" && job.Queue != filter.Queue {
			continue
		}
		if filter.Kind != "" && job.Kind != filter.Kind {
			continue
		}
		if len(stateSet) > 0 {
			if _, ok := stateSet[job.State]; !ok {
				continue
			}
		}
		jobs = append(jobs, cloneJob(job))
	}
	sort.Slice(jobs, func(i, j int) bool { return jobs[i].ID > jobs[j].ID })
	if filter.Limit > 0 && len(jobs) > filter.Limit {
		jobs = jobs[:filter.Limit]
	}
	return jobs, nil
}

func leaseMatches(job Job, lease Lease) bool {
	return job.ID == lease.JobID && job.LeaseOwner == lease.Owner && job.FencingToken == lease.FencingToken
}

func cloneMetadata(in map[string]string) map[string]string {
	if len(in) == 0 {
		return nil
	}
	out := make(map[string]string, len(in))
	for key, value := range in {
		out[key] = value
	}
	return out
}

var _ Backend = (*MemoryBackend)(nil)
EOF
cat > "$ROOT/queue/worker.go" <<'EOF'
package queue

import (
	"context"
	"crypto/sha256"
	"encoding/binary"
	"errors"
	"fmt"
	"math"
	"runtime/debug"
	"sync"
	"time"

	"github.com/go-go-golems/jobgraph/jobgraph"
)

// Handler executes one leased job. FencingToken must be forwarded to any
// externally visible compare-and-swap operation and to jobgraph.RunOptions.
type Handler func(context.Context, Job, Lease) error

// Worker owns polling, lease renewal, panic conversion, and retry transition.
type Worker struct {
	Backend           Backend
	Queue             string
	Owner             string
	Handlers          map[string]Handler
	LeaseDuration     time.Duration
	HeartbeatInterval time.Duration
	PollInterval      time.Duration
	InitialBackoff    time.Duration
	MaxBackoff        time.Duration
	BackoffMultiplier float64
	Jitter            float64
	Now               func() time.Time
}

func (w *Worker) normalized() error {
	if w.Backend == nil || w.Queue == "" || w.Owner == "" {
		return fmt.Errorf("worker backend, queue, and owner are required")
	}
	if w.LeaseDuration <= 0 {
		w.LeaseDuration = 2 * time.Minute
	}
	if w.HeartbeatInterval <= 0 {
		w.HeartbeatInterval = w.LeaseDuration / 3
	}
	if w.HeartbeatInterval >= w.LeaseDuration {
		return fmt.Errorf("heartbeat interval must be shorter than lease duration")
	}
	if w.PollInterval <= 0 {
		w.PollInterval = time.Second
	}
	if w.InitialBackoff <= 0 {
		w.InitialBackoff = time.Second
	}
	if w.MaxBackoff <= 0 {
		w.MaxBackoff = 5 * time.Minute
	}
	if w.BackoffMultiplier < 1 {
		w.BackoffMultiplier = 2
	}
	if w.Jitter < 0 {
		w.Jitter = 0
	}
	if w.Jitter > 1 {
		w.Jitter = 1
	}
	if w.Now == nil {
		w.Now = func() time.Time { return time.Now().UTC() }
	}
	return nil
}

// RunOne claims and processes at most one job. worked=false is not an error.
func (w *Worker) RunOne(ctx context.Context) (worked bool, err error) {
	if err := w.normalized(); err != nil {
		return false, err
	}
	job, lease, found, err := w.Backend.Claim(ctx, w.Queue, w.Owner, w.LeaseDuration)
	if err != nil || !found {
		return found, err
	}
	handler, ok := w.Handlers[job.Kind]
	if !ok {
		handlerErr := jobgraph.Permanent("dispatch", fmt.Errorf("no handler for job kind %q", job.Kind))
		return true, w.finishFailure(ctx, job, lease, handlerErr)
	}
	handlerCtx, cancel := context.WithCancel(ctx)
	defer cancel()
	var heartbeatErr error
	var heartbeatMu sync.Mutex
	heartbeatDone := make(chan struct{})
	go func() {
		defer close(heartbeatDone)
		ticker := time.NewTicker(w.HeartbeatInterval)
		defer ticker.Stop()
		current := lease
		for {
			select {
			case <-handlerCtx.Done():
				return
			case <-ticker.C:
				updated, hbErr := w.Backend.Heartbeat(handlerCtx, current, w.LeaseDuration)
				if hbErr != nil {
					heartbeatMu.Lock()
					heartbeatErr = hbErr
					heartbeatMu.Unlock()
					cancel()
					return
				}
				current = updated
			}
		}
	}()
	handlerErr := invokeHandler(handlerCtx, handler, job, lease)
	cancel()
	<-heartbeatDone
	heartbeatMu.Lock()
	hbErr := heartbeatErr
	heartbeatMu.Unlock()
	if hbErr != nil {
		if handlerErr == nil {
			handlerErr = hbErr
		} else {
			handlerErr = errors.Join(handlerErr, hbErr)
		}
	}
	if handlerErr == nil {
		if err := w.Backend.Complete(context.WithoutCancel(ctx), lease); err != nil {
			return true, err
		}
		return true, nil
	}
	return true, w.finishFailure(context.WithoutCancel(ctx), job, lease, handlerErr)
}

func (w *Worker) finishFailure(ctx context.Context, job Job, lease Lease, handlerErr error) error {
	class := jobgraph.ClassOf(handlerErr)
	failure := Failure{Class: class, Message: handlerErr.Error()}
	if class == jobgraph.ErrorTransient && job.Attempt < job.MaxAttempts {
		failure.RetryAt = w.Now().Add(w.retryDelay(job.ID, job.Attempt))
	}
	if err := w.Backend.Fail(ctx, lease, failure); err != nil {
		return errors.Join(handlerErr, err)
	}
	// The attempt was durably recorded. Returning the handler error lets
	// supervisors/metrics observe it without changing queue correctness.
	return handlerErr
}

// Run polls until cancellation. Job failures do not stop the worker because
// their transitions were already recorded by RunOne.
func (w *Worker) Run(ctx context.Context) error {
	if err := w.normalized(); err != nil {
		return err
	}
	for {
		worked, err := w.RunOne(ctx)
		if err != nil && (errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)) {
			return err
		}
		if worked {
			continue
		}
		timer := time.NewTimer(w.PollInterval)
		select {
		case <-ctx.Done():
			timer.Stop()
			return ctx.Err()
		case <-timer.C:
		}
	}
}

func (w *Worker) retryDelay(jobID int64, attempt int) time.Duration {
	base := float64(w.InitialBackoff) * math.Pow(w.BackoffMultiplier, float64(max(attempt-1, 0)))
	if base > float64(w.MaxBackoff) {
		base = float64(w.MaxBackoff)
	}
	if w.Jitter == 0 {
		return time.Duration(base)
	}
	data := fmt.Sprintf("%d:%d", jobID, attempt)
	digest := sha256.Sum256([]byte(data))
	u := float64(binary.BigEndian.Uint64(digest[:8])) / float64(^uint64(0))
	factor := (1 - w.Jitter) + 2*w.Jitter*u
	return time.Duration(base * factor)
}

func invokeHandler(ctx context.Context, handler Handler, job Job, lease Lease) (err error) {
	defer func() {
		if recovered := recover(); recovered != nil {
			err = jobgraph.Permanent("handler panic", fmt.Errorf("%v\n%s", recovered, debug.Stack()))
		}
	}()
	return handler(ctx, job, lease)
}
EOF
cd "$ROOT"
gofmt -w ./queue
go test ./queue
```

<details><summary>💭 Thinking</summary>

**Implemented job queue, dirty-set, RAG jobgraph, migration, and ledger components**

</details>

```unknown
bash -lc set -euo pipefail
python - <<'PY'
from pathlib import Path
p=Path('/mnt/data/jobgraph/postgres/migrations/001_jobgraph.sql')
s=p.read_text()
old='''CREATE TABLE IF NOT EXISTS jobgraph_dirty_event (
    revision       BIGSERIAL PRIMARY KEY,
    product        TEXT NOT NULL,
    channel        TEXT NOT NULL,
    source         TEXT NOT NULL,
    entity         TEXT,
    entity_key     TEXT,
    source_watermark BIGINT,
    full_refresh   BOOLEAN NOT NULL DEFAULT FALSE,
    reason         TEXT,
    occurred_at    TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
    source_event_id TEXT,
    metadata       JSONB NOT NULL DEFAULT '{}'::jsonb,
    CHECK (full_refresh OR (entity IS NOT NULL AND entity_key IS NOT NULL))
);

CREATE UNIQUE INDEX IF NOT EXISTS jobgraph_dirty_source_dedup
    ON jobgraph_dirty_event (source, source_event_id)
    WHERE source_event_id IS NOT NULL;

CREATE INDEX IF NOT EXISTS jobgraph_dirty_stream
    ON jobgraph_dirty_event (product, channel, revision);

CREATE TABLE IF NOT EXISTS jobgraph_dirty_cursor (
    product         TEXT NOT NULL,
    channel         TEXT NOT NULL,
    acknowledged_through BIGINT NOT NULL DEFAULT 0,
    updated_at      TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
    PRIMARY KEY (product, channel)
);
'''
new='''CREATE TABLE IF NOT EXISTS jobgraph_dirty_cursor (
    product         TEXT NOT NULL,
    channel         TEXT NOT NULL,
    next_revision   BIGINT NOT NULL DEFAULT 0,
    acknowledged_through BIGINT NOT NULL DEFAULT 0,
    updated_at      TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
    PRIMARY KEY (product, channel),
    CHECK (acknowledged_through <= next_revision)
);

-- Revisions are allocated while holding the cursor row lock. This serializes
-- revision assignment with commit order and avoids the late-commit hole that
-- a bare BIGSERIAL high-watermark consumer can otherwise skip.
CREATE TABLE IF NOT EXISTS jobgraph_dirty_event (
    product        TEXT NOT NULL,
    channel        TEXT NOT NULL,
    revision       BIGINT NOT NULL,
    source         TEXT NOT NULL,
    entity         TEXT,
    entity_key     TEXT,
    source_watermark BIGINT,
    full_refresh   BOOLEAN NOT NULL DEFAULT FALSE,
    reason         TEXT,
    occurred_at    TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
    source_event_id TEXT,
    metadata       JSONB NOT NULL DEFAULT '{}'::jsonb,
    PRIMARY KEY (product, channel, revision),
    CHECK (full_refresh OR (entity IS NOT NULL AND entity_key IS NOT NULL)),
    FOREIGN KEY (product, channel) REFERENCES jobgraph_dirty_cursor(product, channel)
);

CREATE UNIQUE INDEX IF NOT EXISTS jobgraph_dirty_source_dedup
    ON jobgraph_dirty_event (source, source_event_id)
    WHERE source_event_id IS NOT NULL;

CREATE INDEX IF NOT EXISTS jobgraph_dirty_stream
    ON jobgraph_dirty_event (product, channel, revision);
'''
if old not in s:
    raise SystemExit('old block not found')
s=s.replace(old,new)
p.write_text(s)
PY
cat > /mnt/data/jobgraph/postgres/changes.go <<'EOF'
package postgres

import (
	"context"
	"database/sql"
	"errors"
	"fmt"

	"github.com/go-go-golems/jobgraph/changes"
)

// DirtyAccumulator persists the revision-prefix protocol for one product and
// channel. Revision allocation is serialized by the cursor row lock so a
// committed low revision can never appear after a higher prefix was acked.
type DirtyAccumulator struct {
	DB      *sql.DB
	Product string
	Channel string
}

func (a *DirtyAccumulator) Observe(ctx context.Context, event changes.DirtyEvent) (int64, error) {
	if a == nil || a.DB == nil || a.Product == "" || a.Channel == "" {
		return 0, fmt.Errorf("dirty accumulator DB, product, and channel are required")
	}
	if _, err := event.Set(); err != nil {
		return 0, err
	}
	tx, err := a.DB.BeginTx(ctx, nil)
	if err != nil {
		return 0, err
	}
	defer tx.Rollback()
	revision, err := a.observeTx(ctx, tx, event)
	if err != nil {
		return 0, err
	}
	if err := tx.Commit(); err != nil {
		return 0, err
	}
	return revision, nil
}

// ObserveTx lets a PostgreSQL source write its domain mutation and dirty event
// in the same transaction. The transaction must use the same database.
func (a *DirtyAccumulator) ObserveTx(ctx context.Context, tx *sql.Tx, event changes.DirtyEvent) (int64, error) {
	if tx == nil {
		return 0, fmt.Errorf("transaction is required")
	}
	if _, err := event.Set(); err != nil {
		return 0, err
	}
	return a.observeTx(ctx, tx, event)
}

func (a *DirtyAccumulator) observeTx(ctx context.Context, tx *sql.Tx, event changes.DirtyEvent) (int64, error) {
	if _, err := tx.ExecContext(ctx, `
INSERT INTO jobgraph_dirty_cursor (product, channel)
VALUES ($1, $2)
ON CONFLICT (product, channel) DO NOTHING`, a.Product, a.Channel); err != nil {
		return 0, err
	}
	var revision int64
	if err := tx.QueryRowContext(ctx, `
UPDATE jobgraph_dirty_cursor
SET next_revision = next_revision + 1, updated_at = clock_timestamp()
WHERE product = $1 AND channel = $2
RETURNING next_revision`, a.Product, a.Channel).Scan(&revision); err != nil {
		return 0, err
	}
	_, err := tx.ExecContext(ctx, `
INSERT INTO jobgraph_dirty_event
    (product, channel, revision, source, entity, entity_key, source_watermark,
     full_refresh, reason)
VALUES ($1, $2, $3, $4, NULLIF($5, ''), NULLIF($6, ''), NULLIF($7, 0), $8, NULLIF($9, ''))`,
		a.Product, a.Channel, revision, event.Source, event.Entity, event.Key,
		event.Watermark, event.Full, event.Reason)
	if err != nil {
		return 0, err
	}
	return revision, nil
}

func (a *DirtyAccumulator) Snapshot(ctx context.Context) (changes.Batch, error) {
	if a == nil || a.DB == nil || a.Product == "" || a.Channel == "" {
		return changes.Batch{}, fmt.Errorf("dirty accumulator DB, product, and channel are required")
	}
	tx, err := a.DB.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted, ReadOnly: false})
	if err != nil {
		return changes.Batch{}, err
	}
	defer tx.Rollback()
	if _, err := tx.ExecContext(ctx, `
INSERT INTO jobgraph_dirty_cursor (product, channel)
VALUES ($1, $2)
ON CONFLICT (product, channel) DO NOTHING`, a.Product, a.Channel); err != nil {
		return changes.Batch{}, err
	}
	var after, through int64
	if err := tx.QueryRowContext(ctx, `
SELECT acknowledged_through, next_revision
FROM jobgraph_dirty_cursor
WHERE product = $1 AND channel = $2
FOR UPDATE`, a.Product, a.Channel).Scan(&after, &through); err != nil {
		return changes.Batch{}, err
	}
	rows, err := tx.QueryContext(ctx, `
SELECT source, COALESCE(entity, ''), COALESCE(entity_key, ''),
       COALESCE(source_watermark, 0), full_refresh, COALESCE(reason, '')
FROM jobgraph_dirty_event
WHERE product = $1 AND channel = $2
  AND revision > $3 AND revision <= $4
ORDER BY revision ASC`, a.Product, a.Channel, after, through)
	if err != nil {
		return changes.Batch{}, err
	}
	var events []changes.DirtyEvent
	for rows.Next() {
		var event changes.DirtyEvent
		if err := rows.Scan(&event.Source, &event.Entity, &event.Key, &event.Watermark, &event.Full, &event.Reason); err != nil {
			rows.Close()
			return changes.Batch{}, err
		}
		events = append(events, event)
	}
	if err := rows.Close(); err != nil {
		return changes.Batch{}, err
	}
	if err := rows.Err(); err != nil {
		return changes.Batch{}, err
	}
	dirty, err := changes.Coalesce(events)
	if err != nil {
		return changes.Batch{}, err
	}
	if err := tx.Commit(); err != nil {
		return changes.Batch{}, err
	}
	return changes.Batch{After: after, Through: through, Dirty: dirty}, nil
}

func (a *DirtyAccumulator) Acknowledge(ctx context.Context, through int64) error {
	result, err := a.DB.ExecContext(ctx, `
UPDATE jobgraph_dirty_cursor
SET acknowledged_through = GREATEST(acknowledged_through, $3),
    updated_at = clock_timestamp()
WHERE product = $1 AND channel = $2 AND next_revision >= $3`,
		a.Product, a.Channel, through)
	if err != nil {
		return err
	}
	rows, err := result.RowsAffected()
	if err != nil {
		return err
	}
	if rows != 1 {
		return fmt.Errorf("cannot acknowledge unknown or future dirty revision %d", through)
	}
	// Retention cleanup is best-effort and not part of correctness.
	_, _ = a.DB.ExecContext(context.WithoutCancel(ctx), `
DELETE FROM jobgraph_dirty_event
WHERE product = $1 AND channel = $2 AND revision <= $3`,
		a.Product, a.Channel, through)
	return nil
}

// InsertExternalEvent deduplicates an outbox/CDC event before allocating a
// local ordered revision. It returns inserted=false when source_event_id was
// already observed.
func (a *DirtyAccumulator) InsertExternalEvent(ctx context.Context, sourceEventID string, event changes.DirtyEvent) (revision int64, inserted bool, err error) {
	if sourceEventID == "" {
		return 0, false, fmt.Errorf("source event ID is required")
	}
	tx, err := a.DB.BeginTx(ctx, nil)
	if err != nil {
		return 0, false, err
	}
	defer tx.Rollback()
	var exists bool
	if err := tx.QueryRowContext(ctx, `
SELECT EXISTS (
    SELECT 1 FROM jobgraph_dirty_event WHERE source = $1 AND source_event_id = $2
)`, event.Source, sourceEventID).Scan(&exists); err != nil {
		return 0, false, err
	}
	if exists {
		return 0, false, tx.Commit()
	}
	if _, err := event.Set(); err != nil {
		return 0, false, err
	}
	if _, err := tx.ExecContext(ctx, `
INSERT INTO jobgraph_dirty_cursor (product, channel)
VALUES ($1, $2) ON CONFLICT (product, channel) DO NOTHING`, a.Product, a.Channel); err != nil {
		return 0, false, err
	}
	if err := tx.QueryRowContext(ctx, `
UPDATE jobgraph_dirty_cursor
SET next_revision = next_revision + 1, updated_at = clock_timestamp()
WHERE product = $1 AND channel = $2
RETURNING next_revision`, a.Product, a.Channel).Scan(&revision); err != nil {
		return 0, false, err
	}
	_, err = tx.ExecContext(ctx, `
INSERT INTO jobgraph_dirty_event
    (product, channel, revision, source, entity, entity_key, source_watermark,
     full_refresh, reason, source_event_id)
VALUES ($1, $2, $3, $4, NULLIF($5, ''), NULLIF($6, ''), NULLIF($7, 0), $8, NULLIF($9, ''), $10)
ON CONFLICT (source, source_event_id) WHERE source_event_id IS NOT NULL DO NOTHING`,
		a.Product, a.Channel, revision, event.Source, event.Entity, event.Key,
		event.Watermark, event.Full, event.Reason, sourceEventID)
	if err != nil {
		return 0, false, err
	}
	if err := tx.Commit(); err != nil {
		return 0, false, err
	}
	return revision, true, nil
}

var _ changes.Accumulator = (*DirtyAccumulator)(nil)

// Keep errors imported in generated API compatibility checks.
var _ = errors.Is
EOF
cat > /mnt/data/jobgraph/postgres/activation.go <<'EOF'
package postgres

import (
	"context"
	"database/sql"
	"errors"
	"fmt"
	"time"

	"github.com/go-go-golems/jobgraph/jobgraph"
	"github.com/go-go-golems/jobgraph/ragjobs"
)

// Activator applies an ActivationPlan as a fenced compare-and-swap.
type Activator struct {
	DB *sql.DB
}

func (a *Activator) Activate(ctx context.Context, step jobgraph.StepContext, plan ragjobs.ActivationPlan) (ragjobs.ActivationResult, error) {
	if a == nil || a.DB == nil {
		return ragjobs.ActivationResult{}, fmt.Errorf("activation DB is required")
	}
	if step.FencingToken <= 0 || plan.FencingToken != step.FencingToken {
		return ragjobs.ActivationResult{}, jobgraph.Permanent("activation fencing", fmt.Errorf("plan token %d differs from lease token %d", plan.FencingToken, step.FencingToken))
	}
	if err := plan.Candidate.Validate(); err != nil {
		return ragjobs.ActivationResult{}, jobgraph.Permanent("activation plan", err)
	}
	var previousDigest, activeDigest string
	var appliedAt time.Time
	err := a.DB.QueryRowContext(ctx, `
INSERT INTO jobgraph_activation_pointer
    (product, channel, active_digest, active_uri, previous_digest, previous_uri,
     fencing_token, evaluation_digest, gate_code)
VALUES ($1, $2, $3, $4, NULLIF($5, ''), NULL, $6, $7, $8)
ON CONFLICT (product, channel) DO UPDATE
SET previous_digest = jobgraph_activation_pointer.active_digest,
    previous_uri = jobgraph_activation_pointer.active_uri,
    active_digest = EXCLUDED.active_digest,
    active_uri = EXCLUDED.active_uri,
    fencing_token = EXCLUDED.fencing_token,
    evaluation_digest = EXCLUDED.evaluation_digest,
    gate_code = EXCLUDED.gate_code,
    updated_at = clock_timestamp()
WHERE jobgraph_activation_pointer.active_digest = EXCLUDED.previous_digest
  AND jobgraph_activation_pointer.fencing_token < EXCLUDED.fencing_token
RETURNING COALESCE(previous_digest, ''), active_digest, updated_at`,
		plan.Product, plan.Channel, plan.Candidate.Digest, plan.Candidate.URI,
		plan.ExpectedPreviousDigest, plan.FencingToken, plan.EvaluationDigest, plan.GateCode,
	).Scan(&previousDigest, &activeDigest, &appliedAt)
	if errors.Is(err, sql.ErrNoRows) {
		var currentDigest string
		var currentToken int64
		readErr := a.DB.QueryRowContext(ctx, `
SELECT active_digest, fencing_token
FROM jobgraph_activation_pointer
WHERE product = $1 AND channel = $2`, plan.Product, plan.Channel).Scan(&currentDigest, &currentToken)
		if errors.Is(readErr, sql.ErrNoRows) {
			return ragjobs.ActivationResult{}, jobgraph.Conflict("activation CAS", fmt.Errorf("initial activation conflict"))
		}
		if readErr != nil {
			return ragjobs.ActivationResult{}, jobgraph.Transient("read activation pointer", readErr)
		}
		return ragjobs.ActivationResult{}, jobgraph.Conflict("activation CAS", fmt.Errorf("expected previous %q/token<%d; current digest=%q token=%d", plan.ExpectedPreviousDigest, plan.FencingToken, currentDigest, currentToken))
	}
	if err != nil {
		return ragjobs.ActivationResult{}, jobgraph.Transient("apply activation", err)
	}
	return ragjobs.ActivationResult{
		Plan: plan, Applied: true, PreviousDigest: previousDigest,
		ActiveDigest: activeDigest, AppliedAt: appliedAt,
	}, nil
}

var _ ragjobs.Activator = (*Activator)(nil)
EOF
cd /mnt/data/jobgraph
gofmt -w ./postgres
go test ./postgres
```

<details><summary>💭 Thinking</summary>

**Implemented revision tracking, adapters, fixes, and comprehensive Go tests**

</details>

```json
{"open":[{"ref_id":"turn761832search0"},{"ref_id":"turn761832search3"},{"ref_id":"turn761832search6"},{"ref_id":"turn761832search8"}],"response_length":"long"}
```

<details><summary>💭 Thinking</summary>

**Searched 1 website**

</details>

Safety semantics now reject ambiguous retryable side effects and prevent stale revisions from superseding newer indexes; mutable snapshots are excluded from semantic caching. Core verification passes, including race tests.

```unknown
bash -lc ls -la /mnt/data && echo '---' && find /mnt/data -maxdepth 2 -type f | sed 's#^#/##' | head -200
```

<details><summary>💭 Thinking</summary>

**Inspected existing artifacts and filesystem contents**

</details>

```unknown
bash -lc printf '%s
' '--- /mnt/data ---'; find /mnt/data -maxdepth 3 -type f -printf '%p	%k KB
' | sort | sed -n '1,240p'; printf '%s
' '--- skills ---'; sed -n '1,240p' /home/oai/skills/docx/SKILL.md; printf '%s
' '--- PDF SKILL ---'; sed -n '1,280p' /home/oai/skills/pdfs/SKILL.md
```

<details><summary>💭 Thinking</summary>

**Inspected files, directories, environment, and shell command execution**

</details>

```unknown
import os, json, glob
print("hello")
print(os.listdir('/mnt/data')[:20])

```

The formal results distinguish parallel composition from effect commutativity and characterize cacheability observationally. The 90-page thesis is complete and fully render-inspected with no layout or glyph defects.

<details><summary>💭 Thinking</summary>

**Inspected available data files**

</details>

# Completed deliverables

| Artifact | Contents |
|---|---|
| [Technical thesis — PDF](sandbox:/mnt/data/Compositional_RAG_Job_System_Thesis.pdf) | Final 90-page, 24,753-word report with formal semantics, proofs, architecture, database protocols, operations, adoption plan, diagrams, appendices, and references |
| [Technical thesis — editable DOCX](sandbox:/mnt/data/Compositional_RAG_Job_System_Thesis.docx) | Fully styled and editable version with populated table of contents, headings, tables, figures, hyperlinks, and document metadata |
| [Reference implementation and design source](sandbox:/mnt/data/ragjobs-reference-implementation.zip) | Standalone Go module, tests, SQL migrations, integration guides, ADRs, diagrams, Markdown thesis source, bibliography, and runbooks |
| [Validation log](sandbox:/mnt/data/ragjobs-validation.txt) | Complete formatting, static-analysis, test, race-test, demonstration, and event-trace evidence |
| [SHA-256 checksums](sandbox:/mnt/data/ragjobs-deliverables-sha256.txt) | Integrity hashes for all principal deliverables |

## System delivered

The implementation is a queue-neutral semantic and control layer named `ragjobs`. It is designed to sit around the existing TTC and GEC/RAG operations rather than embedding orchestration into their retrieval algorithms.

Its formal core includes:

1. **A compositional plan algebra.** Plans are immutable, canonical, typed dependency graphs. `Then` implements sequential composition, while `Tensor` implements disjoint parallel composition. Canonicalization produces stable plan identities and validates cycles, dependency boundaries, artifact ports, and schema compatibility.

2. **Denotational semantics.** Versioned atomic jobs are modeled as graded Kleisli arrows over durable state, failure, traces, finite nondeterminism, and cost. Effect grades summarize logical reads, writes, external systems, concurrency constraints, and conservative cost bounds.

3. **Structural operational semantics.** The Store protocol defines atomic transitions for run creation, readiness, claims, heartbeats, semantic-cache adoption, successful completion, retry, permanent failure, lease recovery, cancellation, skip propagation, and finalization.

4. **Explicit production guarantees.** Attempts are at least once, but accepted mutations are protected by monotonically increasing fencing tokens. Immutable artifacts, semantic invocation keys, transactional inbox/outbox patterns, and compare-and-swap publication provide observable idempotence without claiming arbitrary external effects are exactly once.

The production indexing graph is:

```text
snapshot
  -> extract-corpus
  -> chunk-corpus
  -> (build-lexical || embed-dense)
  -> assemble-bundle
  -> verify-bundle
  -> [evaluate-candidate]
  -> [quality-gate]
  -> publish
  -> cleanup
```

The implementation also contains deterministic sharded evaluation plans, fan-out/fan-in aggregation, baseline comparison, quality gates, cost and attempt budgets, cancellation, failure propagation modes, semantic result reuse, concurrency keys, immutable artifact verification, monotone publication aliases, and gap-aware database-change coalescing.

## Production contracts

The source archive includes:

- a normalized PostgreSQL control-plane schema;
- fenced `FOR UPDATE SKIP LOCKED` claim logic;
- run, node, attempt, event, cache, artifact-alias, trigger-inbox, and River-dispatch tables;
- MySQL and PostgreSQL transactional source-outbox schemas;
- contiguous source-revision adoption;
- monotone revision-aware alias publication;
- direct PostgreSQL Store guidance;
- a River transport protocol;
- TTC handler mappings;
- GEC/CoinVault MySQL snapshot and outbox mappings;
- operational incident procedures;
- staged deployment and shadow-build plans.

The recommended River profile uses River for physical delivery while retaining `ragjobs` as the semantic source of truth. River documents PostgreSQL-backed transaction-safe insertion and reliable workers, but also explicitly describes execution as at least once; unique jobs reduce duplicate insertion without creating exactly-once execution. River Pro workflows can therefore remain an optional physical facility rather than the definition of the workflow. citeturn698927view0turn698927view1turn698927view2turn698927view3turn698927view6

Exactly one layer must own retry timing for a given physical attempt. The direct Store profile assigns that authority to `ragjobs`; the River profile assigns physical retry timing to River and keeps semantic retry classification and lineage in the control plane.

## Verification

The standalone Go module has no external Go dependencies and was validated under Go 1.23.2 with:

- clean `gofmt`;
- successful `go vet ./...`;
- successful ordinary tests;
- successful race-enabled tests;
- 25 named invariant and behavior tests;
- successful extraction and re-testing of the packaged ZIP;
- an end-to-end indexing demonstration with 12 attempts;
- one intentional rate-limited embedding failure and fenced retry;
- 1,500,000 recorded cost microunits;
- a complete 38-event terminal trace;
- verified publication of an immutable artifact alias.

The tests cover composition laws, canonical identities, cycle rejection, typed port validation, retries, dependency causality, cache reuse and conflict rejection, lease recovery, stale-worker fencing, cancellation, budgets, concurrency keys, continuation of independent branches, artifact corruption detection, path traversal rejection, monotone alias revisions, source-gap handling, and indexing/evaluation plan safety.

The DOCX accessibility audit reported no high- or medium-severity findings. Its remaining low-severity findings are raw URL labels in the bibliography. The final PDF is tagged, unencrypted, contains embedded fonts, passed preflight without warnings, and has 90 uniform letter-size pages. Every page was rendered and visually inspected. A second rendering of the final PDF was pixel-identical across all 90 pages to the inspected document export.

## Deliberate production boundary

The executable in-memory Store is a transactional reference interpreter and conformance target. The SQL schemas and River protocol are deployable design contracts, but no live PostgreSQL service or external River dependency was available for integration testing in this environment.

The original TTC, `ragkit`, `ragopt`, and GEC modules were inspected rather than rebuilt because their declared toolchains and external dependencies exceeded the supplied offline Go environment. The GEC archive also references `internal/knowledgebuild` without containing that package. The report therefore provides the precise adapter boundary, MySQL consistency protocol, migration path, and required production tests without presenting an unverifiable compiled adapter.


---

> the maths didn't come out right at all, rewrite the docx as markdown with mathjax and then render to pdf

I’ll convert the thesis source into Markdown with explicit LaTeX/MathJax delimiters, repair the equations semantically rather than relying on DOCX rendering, then produce a PDF and inspect the rendered pages for math/layout failures.

```unknown
bash -lc cat /home/oai/skills/docx/SKILL.md && printf '\n---PDF---\n' && cat /home/oai/skills/pdfs/SKILL.md
```

<details><summary>💭 Thinking</summary>

**Inspected thesis files, citations, equations, and conversion tools**

</details>

The DOCX conversion confirms the root cause: its equation objects are already corrupted (for example, effect products, monadic bind, semantic keys, and Greek symbols lose operators during extraction). The original thesis source inside the implementation bundle preserves the intended LaTeX, so I’m using that as the authoritative reconstruction rather than trying to infer broken Word equations.

```unknown
bash -lc rm -rf /mnt/data/_mathjax_thesis && mkdir -p /mnt/data/_mathjax_thesis/figures && cp /mnt/data/_ragjobs_src/ragjobs/docs/thesis.md /mnt/data/_mathjax_thesis/thesis.md && cp /mnt/data/_ragjobs_src/ragjobs/docs/references.bib /mnt/data/_mathjax_thesis/references.bib && cp -a /mnt/data/_ragjobs_src/ragjobs/docs/figures/. /mnt/data/_mathjax_thesis/figures/ && cd /mnt/data/_mathjax_thesis && pandoc thesis.md --citeproc --pdf-engine=xelatex -V geometry:margin=0.8in -V fontsize=10pt -V colorlinks=true -V linkcolor=blue -V urlcolor=blue -V toccolor=black -o test.pdf 2>pandoc.err; rc=$?; echo RC=$rc; tail -100 pandoc.err; ls -lh test.pdf 2>/dev/null || true
```

<details><summary>💭 Thinking</summary>

**Preparing document conversion**

I’m continuing by converting the manuscript’s equations into reliable LaTeX delimiters, preserving the title and structure, and adding a generated table of contents before rendering a polished PDF.

**Prepared document conversion**

</details>

```unknown
bash -lc node -e "try{console.log(require.resolve('mathjax-full'))}catch(e){console.log('no mathjax-full')}"; node -e "try{console.log(require.resolve('mathjax'))}catch(e){console.log('no mathjax')}"; command -v mathjax-node-cli || true; command -v mjpage || true
```

<details><summary>💭 Thinking</summary>

**Rendering the manuscript**

I’m preparing the final document now: converting equations, preserving headings and figures, generating the table of contents, and applying print-friendly styling before exporting the PDF.

**Compiled and validated thesis PDF math rendering, packages, headings, and fonts**

</details>

All 156 mathematical expressions now parse as MathJax without errors. I’ve also changed the source to portable `$…$` / `$$…$$` delimiters so it no longer depends on Word equation objects or Pandoc’s single-backslash math extension.

```unknown
bash -lc cat > /mnt/data/_mathjax_thesis/thesis.css <<'CSS'
:root {
  --ink: #161616;
  --muted: #555;
  --rule: #b8b8b8;
  --code-bg: #f5f5f5;
}

@page {
  size: Letter;
  margin: 0.72in 0.76in 0.82in 0.76in;
}

html { font-size: 10.25pt; }
body {
  color: var(--ink);
  font-family: "Liberation Serif", "Nimbus Roman", "DejaVu Serif", serif;
  line-height: 1.42;
  max-width: none;
  margin: 0;
  padding: 0;
  text-rendering: optimizeLegibility;
  font-kerning: normal;
}

#title-block-header {
  min-height: 8.1in;
  box-sizing: border-box;
  padding-top: 1.65in;
  text-align: center;
  break-after: page;
}
#title-block-header .title {
  font-size: 25pt;
  line-height: 1.14;
  margin: 0 0 0.28in 0;
  break-before: auto;
}
#title-block-header .subtitle {
  font-size: 14pt;
  line-height: 1.28;
  font-weight: normal;
  margin: 0.08in 0 0.42in 0;
}
#title-block-header .author,
#title-block-header .date {
  font-size: 11pt;
  color: #333;
}
#title-block-header .abstract-title { margin-top: 0.55in; }
#title-block-header .abstract {
  text-align: left;
  max-width: 6.2in;
  margin: 0.45in auto 0 auto;
  font-size: 9.2pt;
}

#TOC {
  break-after: page;
  font-size: 9.4pt;
}
#TOC > h2 { font-size: 20pt; margin-top: 0; }
#TOC ul { list-style: none; padding-left: 1.2em; }
#TOC > ul { padding-left: 0; }
#TOC li { margin: 0.12em 0; }
#TOC a { color: inherit; text-decoration: none; }

section.level1 { break-before: page; }
section.level1:first-of-type { break-before: auto; }
h1, h2, h3, h4, h5, h6 {
  font-family: "Liberation Sans", "Nimbus Sans", "DejaVu Sans", sans-serif;
  color: #111;
  line-height: 1.18;
  break-after: avoid-page;
}
h1 { font-size: 19pt; margin: 0 0 0.26in; }
h2 { font-size: 14.5pt; margin: 0.28in 0 0.12in; }
h3 { font-size: 12pt; margin: 0.22in 0 0.08in; }
h4 { font-size: 10.8pt; margin: 0.18in 0 0.06in; }

p { margin: 0 0 0.105in 0; orphans: 3; widows: 3; }
ul, ol { margin-top: 0.05in; margin-bottom: 0.12in; padding-left: 0.26in; }
li { margin: 0.025in 0; }
blockquote {
  margin: 0.16in 0.25in;
  padding: 0.06in 0.14in;
  border-left: 2.5px solid #888;
  color: #333;
}

code {
  font-family: "Liberation Mono", "Nimbus Mono PS", "DejaVu Sans Mono", monospace;
  font-size: 0.86em;
}
pre {
  background: var(--code-bg);
  border: 1px solid #ddd;
  border-radius: 3px;
  padding: 0.10in;
  font-size: 8.1pt;
  line-height: 1.3;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  break-inside: auto;
}
pre code { font-size: inherit; }

figure {
  margin: 0.18in auto 0.22in auto;
  text-align: center;
  break-inside: avoid-page;
}
figure img { max-width: 100%; max-height: 7.15in; object-fit: contain; }
figcaption {
  margin-top: 0.08in;
  font-size: 8.7pt;
  line-height: 1.25;
  color: #333;
}

img { max-width: 100%; }

table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.14in 0 0.20in;
  font-size: 8.5pt;
  line-height: 1.27;
  break-inside: auto;
}
thead { display: table-header-group; }
tr { break-inside: avoid; }
th, td {
  border: 1px solid #bbb;
  padding: 0.055in 0.065in;
  vertical-align: top;
}
th {
  font-family: "Liberation Sans", "Nimbus Sans", "DejaVu Sans", sans-serif;
  font-weight: 600;
  background: #f2f2f2;
}

hr { border: 0; border-top: 1px solid var(--rule); margin: 0.25in 0; }
a { color: #164f86; text-decoration: none; }

/* MathJax print rules: keep equations intact and never clip wide glyphs. */
mjx-container { color: #090909; }
mjx-container[display="true"] {
  display: block !important;
  margin: 0.14in 0 0.17in !important;
  overflow: visible !important;
  break-inside: avoid-page;
  page-break-inside: avoid;
}
mjx-container svg { max-width: 100%; overflow: visible; }

.csl-bib-body { font-size: 8.8pt; line-height: 1.3; }
.csl-entry { margin: 0 0 0.09in 0; break-inside: avoid; }

@media print {
  a { color: inherit; }
  section.level1 { break-before: page; }
  #title-block-header { break-after: page; }
  #TOC { break-after: page; }
}
CSS
ln -sfn /opt/nvm/versions/node/v22.16.0/lib/node_modules/mathjax-full/es5 /mnt/data/_mathjax_thesis/mathjax
cd /mnt/data/_mathjax_thesis
pandoc -f markdown+tex_math_dollars Compositional_RAG_Job_System_Thesis_MathJax.md \
  --standalone --section-divs --toc --toc-depth=3 --citeproc \
  --mathjax=mathjax/tex-chtml-full.js --css=thesis.css \
  -o thesis_mathjax.html 2>html.err
rc=$?; echo RC=$rc; cat html.err; ls -lh thesis_mathjax.html
```

<details><summary>💭 Thinking</summary>

**Generated and preflighted MathJax thesis PDF package with corrected captions**

</details>

Done. The DOCX equation objects themselves were corrupted - operators, products, brackets, Greek symbols, and monadic notation were being lost during extraction - so I rebuilt the document from the authoritative thesis source rather than propagating the broken Word math.

The new Markdown uses portable MathJax-compatible `$...$` and `$$...$$` notation. I validated all **156 mathematical expressions** with MathJax with zero parse failures, then rendered the same TeX source to a **67-page PDF using XeLaTeX** for print-quality equations. I also rendered and visually checked every page, fixed the duplicated figure-caption numbering, and ran PDF preflight.

- [Markdown with MathJax math](sandbox:/mnt/data/Compositional_RAG_Job_System_Thesis_MathJax.md)
- [Rendered PDF](sandbox:/mnt/data/Compositional_RAG_Job_System_Thesis_MathJax.pdf)
- [Complete source package — Markdown, bibliography, figures, render header, instructions](sandbox:/mnt/data/Compositional_RAG_Job_System_Thesis_MathJax_Source.zip)
- [SHA-256 checksums](sandbox:/mnt/data/Compositional_RAG_Job_System_Thesis_MathJax.sha256)

The important formal expressions - artifact denotations $\llbracket A\rrbracket$, graded computations $T_gA$, Kleisli bind, tensor composition, plan denotation, partial semantic cache, publication transition, inference rules, and capacity equations - now typeset as actual mathematics rather than Word approximations.

## Backlinks

- [transcripts/research/03-rag-and-retrieval-research](http://parc.yolo.scapegoat.dev/note/transcripts/research/03-rag-and-retrieval-research)
- [transcripts/inventory-rag-and-ai-systems](http://parc.yolo.scapegoat.dev/note/transcripts/inventory-rag-and-ai-systems)

## Sitemap

- [Home](http://parc.yolo.scapegoat.dev/)
- [Home markdown mirror](http://parc.yolo.scapegoat.dev/index.md)
- [Markdown sitemap](http://parc.yolo.scapegoat.dev/sitemap.md)
