@semiont/jobs 0.6.10 → 0.6.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,148 +6,60 @@
6
6
  [![npm downloads](https://img.shields.io/npm/dm/@semiont/jobs.svg)](https://www.npmjs.com/package/@semiont/jobs)
7
7
  [![License](https://img.shields.io/npm/l/@semiont/jobs.svg)](https://github.com/The-AI-Alliance/semiont/blob/main/LICENSE)
8
8
 
9
- The job worker for [Semiont](https://github.com/The-AI-Alliance/semiont): the processors for each job type, the job types, and the worker process that claims jobs over the bus. The job queue is the dispatcher's ([apps/dispatcher](../../apps/dispatcher/README.md)), and what it does with a job is [docs/protocol/JOBS.md](../../docs/protocol/JOBS.md).
9
+ The worker: the process that claims jobs and does them. A job is an annotation pass over a resource (references, highlights, comments, assessments, tags) or the generation of a new resource, done with a model through [`@semiont/inference`](../inference/README.md).
10
10
 
11
- ## Architecture Context
11
+ The queue is not here. It is the [dispatcher](../../apps/dispatcher/README.md)'s, and the protocol between the two is [Jobs](../../docs/protocol/JOBS.md).
12
12
 
13
- Workers run in a separate process and connect to the knowledge base's gateway over HTTP/SSE using a `SemiontSession` (from `@semiont/sdk`), with a `JobClaimAdapter` on the session's bus connection. Whenever a worker is idle it claims the next pending job of its types with `job:claim` (a `job:queued` announcement wakes a parked worker), and it emits its lifecycle events onto the bus via `session.client.transport.emit(...)`. The gateway relays them over SSE to the dispatcher, the Stower and clients such as the Browser.
13
+ ## Who uses it
14
14
 
15
- ## Installation
15
+ - **The Worker service** is this package's `worker-main`, run as the `semiont-worker` image ([apps/worker](../../apps/worker/README.md)).
16
+ - **[`@semiont/make-meaning`](../make-meaning/README.md)** takes one constant from it: the stall threshold that its own gather deadlines are checked against.
16
17
 
17
- ```bash
18
- npm install @semiont/jobs
19
- ```
18
+ **Building an application?** You do not need this package. An application starts a job through [`@semiont/sdk`](../sdk/README.md) (`mark.assist` for an annotation pass, `yield.fromContext` for a generation) and follows it with the `job` namespace.
20
19
 
21
- **Dependencies:**
22
- - `@semiont/core` — Core types, the bus protocol (`busRequest`, `BusRequestPrimitive`), the config loader
23
- - `@semiont/sdk` — `SemiontSession`, `SemiontClient` (worker process)
24
- - `@semiont/http-transport` — `HttpTransport`, `HttpContentTransport`
25
- - `@semiont/inference` — InferenceClient for AI operations
26
- - `@semiont/content` — Resource bytes read from the Archivist (`archivistContentReads`), PDF text-layer extraction, the output byte budget
27
- - `@semiont/event-sourcing` — Annotation id generation
28
- - `@semiont/observability` — Spans and job-outcome metrics
20
+ ## What is in it
29
21
 
30
- ## Quick Start
22
+ | | |
23
+ |---|---|
24
+ | `@semiont/jobs/worker-main` | The Worker's entry point. It signs in as an agent, opens a client for each model it is configured with, and claims jobs until it is stopped |
25
+ | `createJobClaimAdapter` | Claiming over the bus: `job:claim` whenever the worker is idle, woken by `job:queued` |
26
+ | `processReferenceJob`, `processHighlightJob`, `processCommentJob`, `processAssessmentJob`, `processTagJob`, `processGenerationJob` | One function per job type. There are no worker classes |
27
+ | `AnnotationDetection`, `generateResourceFromTopic` | What the processors call: the detection passes, and generation |
28
+ | `JobType`, `AnyJob`, and each job's params | The job types. `JobType` and `JobStatus` are generated from the spec |
29
+ | `isPendingJob`, `isRunningJob`, `isCompleteJob`, `isFailedJob`, `isCancelledJob` | Guards that narrow a job to its state |
31
30
 
32
- A job is created on the bus (`job:create`), answered by the dispatcher, and claimed by a worker. A
33
- client starts one through the SDK — `semiont.mark.assist(...)` for an annotation pass,
34
- `semiont.yield.fromContext(...)` for generation — and follows it with `semiont.job`. A worker is this
35
- package's `worker-main`, run as the `semiont-worker` image.
31
+ ## Example
36
32
 
37
- ## Job Types
33
+ A job is a union discriminated by its `status`, so what a job carries follows from the state it is in.
38
34
 
39
35
  ```typescript
40
- type JobType =
41
- | 'reference-annotation' // Entity reference detection
42
- | 'generation' // AI content generation
43
- | 'highlight-annotation' // Key passage highlighting
44
- | 'assessment-annotation' // Evaluative assessments
45
- | 'comment-annotation' // Explanatory comments
46
- | 'tag-annotation' // Structural role tagging
47
- ```
48
-
49
- ## Job Metadata
50
-
51
- All jobs share common metadata:
36
+ import { isCompleteJob, isRunningJob, type AnyJob } from '@semiont/jobs';
52
37
 
53
- ```typescript
54
- interface JobMetadata {
55
- id: JobId;
56
- type: JobType;
57
- userId: UserId; // Who requested it — the verified DID, the job's only identity
58
- created: string;
59
- retryCount: number;
60
- maxRetries: number;
61
- completedUnits?: string[]; // Checkpoint: work units already persisted —
62
- // the retry skips them
63
- unitCursors?: Record<string, UnitCursor>; // How far each unfinished unit got
38
+ function describe(job: AnyJob): string {
39
+ if (isRunningJob(job)) return `${job.metadata.type}: running`; // job.progress is here, job.result is not
40
+ if (isCompleteJob(job)) return `${job.metadata.type}: done`; // job.result is here
41
+ return `${job.metadata.type}: ${job.status}`;
64
42
  }
65
43
  ```
66
44
 
67
- `userId` is the job's only identity — the DID the gateway verified on the `job:create`. The dispatcher records it as the requester when it accepts a claim, and that record is what lets the knowledge base attribute a write citing this job; a worker never states it. `completedUnits` and `unitCursors` are written by the dispatcher, from `job:checkpoint` (as work lands) and from `job:fail`, merged across attempts — see Failure discipline below.
68
-
69
- ## Progress
70
-
71
- A running job's `progress` is `StoredProgress`: the last `JobProgress` its worker reported with `job:report-progress`, or `{}` before the first report. It is one shape for every job type — the spec's `JobRunning.progress` — so there are no per-type progress types. See [Job Types Guide](./docs/JobTypes.md#progress).
72
-
73
- ## Annotation Workers
74
-
75
- The worker process (`worker-main.ts` → `startAgentWorker` in `worker-runtime.ts` → `startWorkerProcess` in `worker-process.ts`) claims jobs over the bus via a `JobClaimAdapter` and dispatches by `jobType` to a processor function. There are no per-type worker classes; each job type maps to one `process*Job` function:
76
-
77
- | Job Type | Processor |
78
- |----------|-----------|
79
- | `reference-annotation` | `processReferenceJob` |
80
- | `generation` | `processGenerationJob` |
81
- | `highlight-annotation` | `processHighlightJob` |
82
- | `assessment-annotation` | `processAssessmentJob` |
83
- | `comment-annotation` | `processCommentJob` |
84
- | `tag-annotation` | `processTagJob` |
85
-
86
- Detection logic lives in the `AnnotationDetection` class (`src/workers/annotation-detection.ts`) and, for references, in `extractEntities` (`src/workers/detection/entity-extractor.ts`); generation synthesis in `generateResourceFromTopic()` (`src/workers/generation/resource-generation.ts`). Processors never fetch content themselves — the worker process prepares it with `prepareDetection` (bytes through `contentReads` for text media, the Smelter's anchored text for geometry-bearing media such as PDF) and passes it in.
87
-
88
- Workers emit lifecycle events via `session.client.transport.emit('job:start' | 'job:report-progress' | 'job:checkpoint' | 'job:complete' | 'job:fail', payload)` and persist annotations through the **awaited `mark:commit` operation** — a batch per unit of work that resolves only once the Stower actor in @semiont/make-meaning has appended every annotation to the event log. Unit completion and `job:complete` gate on that acknowledgement, never on emission, so a down persistence sink is a retryable failure instead of silent loss. The dispatcher's job command handlers mirror the lifecycle events into the queue (completion, checkpoints, retry-on-failure with `maxRetries`, progress-as-heartbeat). `job:fail` carries the fields the worker computes: `completedUnits` and `unitCursors` (the checkpoint), `failureClass`, and `willRetry` — see Failure discipline.
89
-
90
- ## Failure discipline
91
-
92
- Long inference work fails in bounded, classified, resumable ways:
45
+ ## What a change must keep
93
46
 
94
- - **Every inference call is bounded and truly cancelled.** A call gets 10 minutes (`INFERENCE_TIMEOUT_MS`); at the bound the worker aborts it at the transport (`AbortSignal` through the provider SDK to the socket — milliseconds to rejection, no zombie billing on) and fails the job with a typed `InferenceTimeoutError`. An in-flight heartbeat reports elapsed-time liveness every 15 s during long calls.
95
- - **Budgets are derived, never tuned** (`workers/detection/detection-chunking.ts`). Input:output allocation is 1:2 per entity type asked for (`input ≤ outputBudget / (2 × typesPerCall)`), and **every** provider gets a duration cap: per-call output is bounded at what the provider's worst-case rate finishes in HALF the bound — the published rate when there is one, a conservative assumed floor (`ASSUMED_OUTPUT_TOKENS_PER_HOUR`, 30 tok/s) for rate-silent providers like Ollama, where an unbounded budget turns a model repetition loop into an hour-long transient burn.
96
- - **Size-shaped failures subdivide in place** (`callChunkSubdividing`). Five failure families descend, each with its own floor: a **truncation** descends by size and gets one same-size re-roll at the floor; a **timeout** descends two levels then propagates; an **`'unknown'`-stop unreadable response** (garbage output with no stop reason — measured size-correlated on real documents) descends by size and propagates at the floor; an **unreadable response from a model that finished** (`end_turn`) descends two levels then propagates, as a timeout does; and a **flagged under-report** (below) descends by size, and at the floor its salvage — everything it did find, every span write-time-verified — is **accepted loudly** rather than discarded. A piece that cannot actually shrink is at its floor regardless of arithmetic: at temperature 0, an identical re-run returns the identical failure. Sub-piece overlap duplicates fall to the existing span-keyed dedupe.
97
- - **Successful-looking extractions are verified** (`assertYieldNotCollapsed`). A local model can return a clean, schema-conforming response carrying a fraction of the entities present — deterministic and otherwise invisible. When the provider declares `verifyDetectionYield` (all real providers do), each chunk's item count is checked against a cheap count call over the same text; an extraction under half the count is flagged and subdivided. Every anchoring outcome and every call — including flagged and failed ones — is recorded to `semiont.detection.*` metrics (`@semiont/observability`).
98
- - **Entity types run concurrently up to the provider's declared capacity** (`client.maxConcurrency`): a hosted API with rate headroom runs several types at once; a local single-model server runs them sequentially, because concurrent requests only split one GPU. Jobs never switches on provider identity — both behaviors are capabilities declared on the `InferenceClient`.
99
- - **Failures are classified at the worker, where errors are still typed** (`failure-class.ts`). Only KNOWN-deterministic failures — truncation at the subdivision floor, unsupported media, a 4xx other than 408 or 429 — skip the retry budget; everything unrecognized stays retryable. The class rides `job:fail` as `failureClass`.
100
- - **Retries resume from the checkpoint.** Every detection job commits its annotations chunk by chunk; each committed chunk's cursor rides `job:checkpoint` and `job:fail` into `metadata.unitCursors`, and reference-annotation also records each finished entity type in `metadata.completedUnits`. The retry skips the completed units and resumes each unfinished one from its cursor.
47
+ - **A worker holds nothing of the knowledge base.** It has no mount and no broker credential. It reaches the gateway with a URL and a token, reads a resource's bytes from the Archivist, and writes annotations by the awaited `mark:commit`, a batch per unit of work.
48
+ - **A worker sees a job only as it is handed over.** It never reads the queue's storage. What it knows of a job is what `job:claimed` carried.
49
+ - **A worker never says who asked.** A job's requester is the identity the gateway verified on `job:create`, recorded by the dispatcher. What the worker contributes is itself, as the software agent that generated the result.
50
+ - **A processor is plain work.** It is given content, an inference client, the job's params and callbacks for progress and for committing a chunk. It fetches nothing and knows no transport.
51
+ - **One progress shape.** Every job type reports the spec's `JobProgress`. There are no per-type progress types.
52
+ - **Failure is bounded, classified and resumable.** Every inference call has a deadline and is truly cancelled. A failure caused by size subdivides the work in place. A retry resumes from the last checkpoint and skips what was committed. [Failure discipline](docs/FailureDiscipline.md) has the rules.
101
53
 
102
- ## Adding a Job Type
103
-
104
- Workers are not subclassed. To add a job type:
105
-
106
- 1. Add the new type to the spec: its name in `JobType.json` (`JobType` in `src/types.ts` is generated from it), its category in `specs/src/jobs/storage.json`, and its result schema in the `JobResult` union. Add its params type in `src/types.ts`. There is no progress type — every job reports `JobProgress`.
107
- 2. Add a `process*Job` function in `src/processors.ts` that runs the inference, commits its annotations through the per-chunk callback and returns the result.
108
- 3. Dispatch the new `jobType` to that processor in `handleJobInner()` in `src/worker-process.ts`.
109
-
110
- Processors are transport-agnostic: they take content, an `InferenceClient`, the job params, a `buildAnnotation` closure (which carries the `generator` — the worker's own `Software` agent), an `onProgress` callback and a per-chunk commit callback, and return a result. No user identity reaches a processor: an annotation states what produced it, and who requested it is derived by the knowledge base from the job the commit cites. The worker process handles claiming, content fetching, committing, and lifecycle event emission.
111
-
112
- ## Discriminated Unions
113
-
114
- Jobs use TypeScript discriminated unions for type safety:
115
-
116
- ```typescript
117
- function handleJob(job: AnyJob) {
118
- if (job.status === 'running') {
119
- console.log(job.progress); // Available — StoredProgress, `{}` until the first report
120
- // console.log(job.result); // Compile error
121
- }
122
- if (job.status === 'complete') {
123
- console.log(job.result); // Available
124
- // console.log(job.progress); // Compile error
125
- }
126
- }
127
- ```
128
-
129
- ## Storage Format
130
-
131
- The dispatcher keeps one `JobRecord` per job in a JetStream key-value bucket, in the layout
132
- `specs/src/jobs/storage.json` states. A worker never reads it: it sees a job only as the dispatcher
133
- hands it over on `job:claimed`.
54
+ Adding a job type starts in the spec, not here: [Workers](docs/Workers.md#adding-a-custom-job-type) lists the steps.
134
55
 
135
56
  ## Documentation
136
57
 
137
- - **[Workers Guide](./docs/Workers.md)** — Building custom workers
138
- - **[Job Types Guide](./docs/JobTypes.md)** — All job type definitions
139
- - **[Type System Guide](./docs/TYPES.md)** — Discriminated unions and type safety
140
- - **[Configuration Guide](./docs/Configuration.md)** — Running a worker
141
- - **[API Reference](./docs/API.md)** — Complete API reference
58
+ - [Workers](docs/Workers.md): how a worker runs, where content comes from, how it emits, a worker written outside this package, adding a job type.
59
+ - [Job types](docs/JobTypes.md): a job's states, the six types, and what each one's params, result and progress mean.
60
+ - [Failure discipline](docs/FailureDiscipline.md): deadlines, budgets, subdivision, verification, classification, resumption.
61
+ - [The worker service](../../apps/worker/README.md): running it.
142
62
 
143
63
  ## License
144
64
 
145
65
  Apache-2.0
146
-
147
- ## Related Packages
148
-
149
- - [`@semiont/core`](../core/) — Domain types, `SemiontProject`, EventBus, `BusRequestPrimitive`
150
- - [`@semiont/sdk`](../sdk/) — `SemiontSession`, `SemiontClient`
151
- - [`@semiont/http-transport`](../http-transport/) — `HttpTransport`, `HttpContentTransport`
152
- - [`@semiont/inference`](../inference/) — AI inference client
153
- - [`@semiont/make-meaning`](../make-meaning/) — Actor model, Knowledge Base, service orchestration