@systemfsoftware/agent-platform-node 0.0.0-bootstrap.0 → 0.1.0-beta.192
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/LICENSE +21 -0
- package/dist/NodeDurableAgentRuntime-C5SFn9kf.mjs +218 -0
- package/dist/NodeDurableAgentRuntime-C5SFn9kf.mjs.map +1 -0
- package/dist/NodeDurableAgentRuntime-CUrOl39W.d.mts +163 -0
- package/dist/NodeDurableAgentRuntime.d.mts +2 -0
- package/dist/NodeDurableAgentRuntime.mjs +1 -0
- package/dist/NodeDurableHost-BztkzCGh.mjs +271 -0
- package/dist/NodeDurableHost-BztkzCGh.mjs.map +1 -0
- package/dist/NodeDurableHost-CFvVn1w1.d.mts +153 -0
- package/dist/NodeDurableHost.d.mts +2 -0
- package/dist/NodeDurableHost.mjs +1 -0
- package/dist/NodeScheduling-CD_CZvQ6.d.mts +20 -0
- package/dist/NodeScheduling-gNj8jB4g.mjs +64 -0
- package/dist/NodeScheduling-gNj8jB4g.mjs.map +1 -0
- package/dist/NodeScheduling.d.mts +2 -0
- package/dist/NodeScheduling.mjs +1 -0
- package/dist/NodeSubscriptions-Bd_3VxqD.d.mts +23 -0
- package/dist/NodeSubscriptions-KgvkfIMl.mjs +62 -0
- package/dist/NodeSubscriptions-KgvkfIMl.mjs.map +1 -0
- package/dist/NodeSubscriptions.d.mts +2 -0
- package/dist/NodeSubscriptions.mjs +1 -0
- package/dist/NodeWakeScheduler-Dyha4GZJ.d.mts +28 -0
- package/dist/NodeWakeScheduler-dVNAHWsN.mjs +58 -0
- package/dist/NodeWakeScheduler-dVNAHWsN.mjs.map +1 -0
- package/dist/NodeWakeScheduler.d.mts +2 -0
- package/dist/NodeWakeScheduler.mjs +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +6 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/package.json +66 -5
- package/src/NodeDurableAgentRuntime.ts +603 -0
- package/src/NodeDurableHost.ts +403 -0
- package/src/NodeScheduling.ts +122 -0
- package/src/NodeSubscriptions.ts +124 -0
- package/src/NodeWakeScheduler.ts +94 -0
- package/src/index.ts +5 -0
- package/src/internal/application-context.ts +25 -0
- package/src/internal/exclusive-host.ts +7 -0
- package/src/internal/message-delivery.ts +54 -0
- package/src/internal/prepared-admission.ts +79 -0
- package/src/internal/worker-dispatch.ts +116 -0
- package/README.md +0 -3
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { type ThreadId } from '@systemfsoftware/agent/identifiers'
|
|
2
|
+
import type { ThreadStore, ThreadStoreError } from '@systemfsoftware/agent/thread-store'
|
|
3
|
+
import { ThreadWorkDiscovery } from '@systemfsoftware/agent/thread-work'
|
|
4
|
+
import { makeWakeSubscriptionHub, WakeScheduler } from '@systemfsoftware/agent/wake-scheduler'
|
|
5
|
+
import * as Clock from 'effect/Clock'
|
|
6
|
+
import * as Context from 'effect/Context'
|
|
7
|
+
import type * as Duration from 'effect/Duration'
|
|
8
|
+
import * as Effect from 'effect/Effect'
|
|
9
|
+
import * as Layer from 'effect/Layer'
|
|
10
|
+
import * as Option from 'effect/Option'
|
|
11
|
+
import * as PubSub from 'effect/PubSub'
|
|
12
|
+
import * as Stream from 'effect/Stream'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Bounded in-process wake buffer. Wake hints are droppable by contract (native work inventory
|
|
16
|
+
* keeps liveness), so a full buffer slides out the oldest hint instead of growing without bound.
|
|
17
|
+
*/
|
|
18
|
+
const WAKE_BUFFER_CAPACITY = 1_024
|
|
19
|
+
|
|
20
|
+
/** Cadence authority for the Node wake scheduler's native inventory fallback. */
|
|
21
|
+
export class NodeWakeSchedulerConfig extends Context.Service<
|
|
22
|
+
NodeWakeSchedulerConfig,
|
|
23
|
+
{
|
|
24
|
+
/** Interval between scans of Threads with outstanding work, including terminal obligations. */
|
|
25
|
+
readonly scanInterval: Duration.Duration
|
|
26
|
+
}
|
|
27
|
+
>()('@effect-agent/platform-node/NodeWakeSchedulerConfig') {
|
|
28
|
+
static layer(options: {
|
|
29
|
+
readonly scanInterval: Duration.Duration
|
|
30
|
+
}): Layer.Layer<NodeWakeSchedulerConfig> {
|
|
31
|
+
return Layer.succeed(NodeWakeSchedulerConfig)({ scanInterval: options.scanInterval })
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const makeWakeScheduler = Effect.gen(function*() {
|
|
36
|
+
const work = yield* ThreadWorkDiscovery.pipe(Effect.provide(ThreadWorkDiscovery.layer))
|
|
37
|
+
const config = yield* NodeWakeSchedulerConfig
|
|
38
|
+
const clock = yield* Clock.Clock
|
|
39
|
+
const hints = yield* PubSub.sliding<ThreadId>(WAKE_BUFFER_CAPACITY)
|
|
40
|
+
const progress = yield* makeWakeSubscriptionHub
|
|
41
|
+
const settlements = yield* makeWakeSubscriptionHub
|
|
42
|
+
|
|
43
|
+
yield* Effect.addFinalizer(() => PubSub.shutdown(hints))
|
|
44
|
+
|
|
45
|
+
const scanOnce = Stream.paginate(undefined, (afterThreadId: ThreadId | undefined) =>
|
|
46
|
+
work
|
|
47
|
+
.threads({ limit: 32, ...(afterThreadId === undefined ? {} : { afterThreadId }) })
|
|
48
|
+
.pipe(
|
|
49
|
+
Effect.map(
|
|
50
|
+
(page): readonly [ReadonlyArray<ThreadId>, Option.Option<ThreadId | undefined>] => [
|
|
51
|
+
page.threadIds,
|
|
52
|
+
page.afterThreadId === undefined ? Option.none() : Option.some(page.afterThreadId),
|
|
53
|
+
],
|
|
54
|
+
),
|
|
55
|
+
)).pipe(
|
|
56
|
+
Stream.catch((error) =>
|
|
57
|
+
Stream.fromEffect(
|
|
58
|
+
Effect.logWarning('NodeWakeScheduler native work inventory failed', error),
|
|
59
|
+
).pipe(Stream.drain)
|
|
60
|
+
),
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
// Each subscriber owns its cursor. A slow subscriber retains one page and resumes it;
|
|
64
|
+
// faster subscribers keep scanning without an unbounded shared snapshot or dropped pages.
|
|
65
|
+
const fallbackScans = Stream.fromEffectRepeat(clock.sleep(config.scanInterval)).pipe(
|
|
66
|
+
Stream.flatMap(() => scanOnce),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
return WakeScheduler.of({
|
|
70
|
+
notify: (threadId, kind) =>
|
|
71
|
+
(kind === 'progress'
|
|
72
|
+
? progress.notify(threadId)
|
|
73
|
+
: settlements.notify(threadId).pipe(Effect.andThen(progress.notify(threadId)))).pipe(
|
|
74
|
+
Effect.andThen(PubSub.publish(hints, threadId)),
|
|
75
|
+
Effect.asVoid,
|
|
76
|
+
),
|
|
77
|
+
subscribe: (threadId, kind) => (kind === 'settlement' ? settlements : progress).subscribe(threadId),
|
|
78
|
+
wakes: Stream.merge(Stream.fromPubSub(hints), fallbackScans),
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* In-process Node `WakeScheduler`: `notify` publishes to a bounded sliding PubSub for prompt
|
|
84
|
+
* same-process worker wakeups. Thread waiters use separate progress and settlement registrations;
|
|
85
|
+
* broad hints wake both. Each active `wakes` subscription pages the native work inventory
|
|
86
|
+
* periodically so a dropped, coalesced, or never-sent notification cannot strand accepted work
|
|
87
|
+
* or a terminal side obligation. Delivery may duplicate; consumers already
|
|
88
|
+
* treat wakes as pure liveness hints.
|
|
89
|
+
*/
|
|
90
|
+
export const nodeWakeSchedulerLayer: Layer.Layer<
|
|
91
|
+
WakeScheduler,
|
|
92
|
+
ThreadStoreError,
|
|
93
|
+
ThreadStore | NodeWakeSchedulerConfig
|
|
94
|
+
> = Layer.effect(WakeScheduler)(makeWakeScheduler)
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * as NodeDurableAgentRuntime from './NodeDurableAgentRuntime'
|
|
2
|
+
export * as NodeDurableHost from './NodeDurableHost'
|
|
3
|
+
export * as NodeScheduling from './NodeScheduling'
|
|
4
|
+
export * as NodeSubscriptions from './NodeSubscriptions'
|
|
5
|
+
export * as NodeWakeScheduler from './NodeWakeScheduler'
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as Clock from 'effect/Clock'
|
|
2
|
+
import * as Context from 'effect/Context'
|
|
3
|
+
import * as References from 'effect/References'
|
|
4
|
+
import * as Scope from 'effect/Scope'
|
|
5
|
+
import * as Tracer from 'effect/Tracer'
|
|
6
|
+
|
|
7
|
+
/** Live lifetime and diagnostics may cross the managed host's private storage boundary. */
|
|
8
|
+
export const applicationInvocationContext = Context.pick(
|
|
9
|
+
Scope.Scope,
|
|
10
|
+
Clock.Clock,
|
|
11
|
+
Tracer.ParentSpan,
|
|
12
|
+
Tracer.Tracer,
|
|
13
|
+
Tracer.MinimumTraceLevel,
|
|
14
|
+
Tracer.CurrentTraceLevel,
|
|
15
|
+
References.TracerEnabled,
|
|
16
|
+
References.TracerTimingEnabled,
|
|
17
|
+
References.TracerSpanAnnotations,
|
|
18
|
+
References.TracerSpanLinks,
|
|
19
|
+
References.CurrentLoggers,
|
|
20
|
+
References.CurrentLogLevel,
|
|
21
|
+
References.MinimumLogLevel,
|
|
22
|
+
References.CurrentStackFrame,
|
|
23
|
+
References.CurrentLogAnnotations,
|
|
24
|
+
References.CurrentLogSpans,
|
|
25
|
+
)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as Context from 'effect/Context'
|
|
2
|
+
|
|
3
|
+
/** Only the automatically managed host owns the SQLite connection for its entire lifetime. */
|
|
4
|
+
export const ExclusiveSqliteHost = Context.Reference<boolean>(
|
|
5
|
+
'@effect-agent/platform-node/internal/ExclusiveSqliteHost',
|
|
6
|
+
{ defaultValue: () => false },
|
|
7
|
+
)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MessageDeliveryDriver,
|
|
3
|
+
type MessageDeliveryFailure,
|
|
4
|
+
MessageDeliveryStore,
|
|
5
|
+
} from '@systemfsoftware/agent/message-delivery'
|
|
6
|
+
import * as Cause from 'effect/Cause'
|
|
7
|
+
import * as Clock from 'effect/Clock'
|
|
8
|
+
import * as Effect from 'effect/Effect'
|
|
9
|
+
import * as Exit from 'effect/Exit'
|
|
10
|
+
import * as Option from 'effect/Option'
|
|
11
|
+
|
|
12
|
+
const reportFailure = (cause: Cause.Cause<MessageDeliveryFailure>): Effect.Effect<void> =>
|
|
13
|
+
Cause.hasInterruptsOnly(cause)
|
|
14
|
+
? Effect.interrupt
|
|
15
|
+
: Effect.logWarning('Node message delivery pass failed').pipe(
|
|
16
|
+
Effect.annotateLogs({
|
|
17
|
+
failureTag: Option.match(Cause.findErrorOption(cause), {
|
|
18
|
+
onNone: () => 'Defect',
|
|
19
|
+
onSome: (failure) => failure._tag,
|
|
20
|
+
}),
|
|
21
|
+
}),
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
/** Caller-owned loop. Indexed scans repair absent hints even when both Threads have settled. */
|
|
25
|
+
export const runNodeMessageDeliveries = Effect.fnUntraced(function*(scanInterval: number) {
|
|
26
|
+
const driver = yield* MessageDeliveryDriver
|
|
27
|
+
const store = yield* MessageDeliveryStore
|
|
28
|
+
|
|
29
|
+
while (true) {
|
|
30
|
+
const pass = yield* driver.runDue().pipe(Effect.exit)
|
|
31
|
+
|
|
32
|
+
if (Exit.isFailure(pass)) {
|
|
33
|
+
yield* reportFailure(pass.cause)
|
|
34
|
+
yield* Effect.sleep(scanInterval)
|
|
35
|
+
continue
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const deadline = yield* store.nextDeadline().pipe(Effect.exit)
|
|
39
|
+
|
|
40
|
+
if (Exit.isFailure(deadline)) {
|
|
41
|
+
yield* reportFailure(deadline.cause)
|
|
42
|
+
yield* Effect.sleep(scanInterval)
|
|
43
|
+
continue
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const nowMillis = yield* Clock.currentTimeMillis
|
|
47
|
+
|
|
48
|
+
const delay = deadline.value === null
|
|
49
|
+
? scanInterval
|
|
50
|
+
: Math.max(1, Math.min(deadline.value - nowMillis, scanInterval))
|
|
51
|
+
|
|
52
|
+
yield* Effect.sleep(delay)
|
|
53
|
+
}
|
|
54
|
+
})
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type DurableSubmitAgent } from '@systemfsoftware/agent/durable-agent-runtime'
|
|
2
|
+
import { type AgentId } from '@systemfsoftware/agent/identifiers'
|
|
3
|
+
import {
|
|
4
|
+
PreparedInputAdmission,
|
|
5
|
+
PreparedInputRefused,
|
|
6
|
+
PreparedInputRetryable,
|
|
7
|
+
} from '@systemfsoftware/agent/prepared-input-admission'
|
|
8
|
+
import { PersistedJson } from '@systemfsoftware/agent/records'
|
|
9
|
+
import { ScheduleStorageError } from '@systemfsoftware/agent/schedule'
|
|
10
|
+
import * as Context from 'effect/Context'
|
|
11
|
+
import * as Effect from 'effect/Effect'
|
|
12
|
+
|
|
13
|
+
import { type NodeDurableHost } from '../NodeDurableHost'
|
|
14
|
+
|
|
15
|
+
const passthroughSubmitAgent = (agentId: AgentId): DurableSubmitAgent<typeof PersistedJson> => ({
|
|
16
|
+
definition: { id: agentId, input: PersistedJson },
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const ambiguous = (): PreparedInputRetryable => PreparedInputRetryable.make({ reason: 'ambiguous' })
|
|
20
|
+
|
|
21
|
+
const corrupt = (operation: string): ScheduleStorageError => ScheduleStorageError.make({ operation, reason: 'corrupt' })
|
|
22
|
+
|
|
23
|
+
/** Host-owned admission gate, available while the host's worker pool is being assembled. */
|
|
24
|
+
export class NodeAdmission extends Context.Service<
|
|
25
|
+
NodeAdmission,
|
|
26
|
+
Pick<NodeDurableHost['Service'], 'submit' | 'submissionStatus'>
|
|
27
|
+
>()('@effect-agent/platform-node/internal/NodeAdmission') {}
|
|
28
|
+
|
|
29
|
+
/** Acquire the gated source once; worker callers cannot replace its admission authority. */
|
|
30
|
+
export const makeNodePreparedInputAdmission = Effect.gen(function*() {
|
|
31
|
+
const host = yield* NodeAdmission
|
|
32
|
+
|
|
33
|
+
return PreparedInputAdmission.of({
|
|
34
|
+
submissionStatus: (receipt) =>
|
|
35
|
+
host
|
|
36
|
+
.submissionStatus(receipt)
|
|
37
|
+
.pipe(Effect.mapError(() => PreparedInputRetryable.make({ reason: 'storage' }))),
|
|
38
|
+
submit: (envelope) =>
|
|
39
|
+
host
|
|
40
|
+
.submit(passthroughSubmitAgent(envelope.agentId), envelope.input, {
|
|
41
|
+
threadId: envelope.threadId,
|
|
42
|
+
principal: envelope.deliveryPrincipal,
|
|
43
|
+
idempotencyKey: envelope.admissionKey,
|
|
44
|
+
...(envelope.admissionGroup === undefined
|
|
45
|
+
? {}
|
|
46
|
+
: { admissionGroup: envelope.admissionGroup }),
|
|
47
|
+
...(envelope.admissionFence === undefined
|
|
48
|
+
? {}
|
|
49
|
+
: { admissionFence: envelope.admissionFence }),
|
|
50
|
+
...(envelope.workerAdmission === undefined
|
|
51
|
+
? {}
|
|
52
|
+
: { workerAdmission: envelope.workerAdmission }),
|
|
53
|
+
...(envelope.messageAdmission === undefined
|
|
54
|
+
? {}
|
|
55
|
+
: { messageAdmission: envelope.messageAdmission }),
|
|
56
|
+
definitions: envelope.definitions,
|
|
57
|
+
})
|
|
58
|
+
.pipe(
|
|
59
|
+
Effect.catchTags({
|
|
60
|
+
AdmissionClosed: () => Effect.fail(PreparedInputRetryable.make({ reason: 'host-closed' })),
|
|
61
|
+
AgentInputError: () => Effect.fail(corrupt('prepared admission input')),
|
|
62
|
+
AdmissionConflict: () => Effect.fail(corrupt('prepared admission conflict')),
|
|
63
|
+
DigestError: () => Effect.fail(ambiguous()),
|
|
64
|
+
AdmissionPolicyError: (error) =>
|
|
65
|
+
error.reason === 'refused'
|
|
66
|
+
? PreparedInputRefused.make({ code: error.code })
|
|
67
|
+
: PreparedInputRetryable.make({
|
|
68
|
+
reason: error.reason === 'occupied' ? 'capacity' : 'storage',
|
|
69
|
+
}),
|
|
70
|
+
LedgerError: () => PreparedInputRetryable.make({ reason: 'storage' }),
|
|
71
|
+
ThreadStoreError: () => Effect.fail(ambiguous()),
|
|
72
|
+
ThreadNotMaterialized: () => Effect.fail(ambiguous()),
|
|
73
|
+
AppendConflict: () => Effect.fail(ambiguous()),
|
|
74
|
+
OwnershipLost: () => Effect.fail(ambiguous()),
|
|
75
|
+
DurableRuntimeFailpointError: () => Effect.fail(ambiguous()),
|
|
76
|
+
}),
|
|
77
|
+
),
|
|
78
|
+
})
|
|
79
|
+
})
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { DurableBindingFailure } from '@systemfsoftware/agent/agent-registration'
|
|
2
|
+
import { DurableAgentRuntime, type DurableWorkerFailure } from '@systemfsoftware/agent/durable-agent-runtime'
|
|
3
|
+
import type { ThreadId } from '@systemfsoftware/agent/identifiers'
|
|
4
|
+
import { WakeScheduler } from '@systemfsoftware/agent/wake-scheduler'
|
|
5
|
+
import * as Effect from 'effect/Effect'
|
|
6
|
+
import * as Option from 'effect/Option'
|
|
7
|
+
import * as Queue from 'effect/Queue'
|
|
8
|
+
import * as Stream from 'effect/Stream'
|
|
9
|
+
|
|
10
|
+
const PENDING_CAPACITY = 1_024
|
|
11
|
+
|
|
12
|
+
type DispatchState = 'pending' | 'active' | 'dirty'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* One discovery path feeds bounded concurrent Thread drains. Hints carry no authority:
|
|
16
|
+
* the runtime still claims, fences, parks and releases every Attempt. Concurrency has
|
|
17
|
+
* already been validated by the host configuration.
|
|
18
|
+
*/
|
|
19
|
+
export const runNodeWorkerDispatch = (
|
|
20
|
+
concurrency: number,
|
|
21
|
+
): Effect.Effect<
|
|
22
|
+
void,
|
|
23
|
+
DurableWorkerFailure | DurableBindingFailure,
|
|
24
|
+
DurableAgentRuntime | WakeScheduler
|
|
25
|
+
> =>
|
|
26
|
+
Effect.scoped(
|
|
27
|
+
Effect.gen(function*() {
|
|
28
|
+
const runtime = yield* DurableAgentRuntime
|
|
29
|
+
const wake = yield* WakeScheduler
|
|
30
|
+
const pending = yield* Queue.bounded<ThreadId>(PENDING_CAPACITY)
|
|
31
|
+
const states = new Map<ThreadId, DispatchState>()
|
|
32
|
+
|
|
33
|
+
yield* Effect.addFinalizer(() => Queue.shutdown(pending))
|
|
34
|
+
|
|
35
|
+
const enqueue = Effect.fnUntraced(function*(threadId: ThreadId) {
|
|
36
|
+
const offer = yield* Effect.sync(() => {
|
|
37
|
+
const state = states.get(threadId)
|
|
38
|
+
|
|
39
|
+
if (state === 'pending' || state === 'dirty') return false
|
|
40
|
+
if (state === 'active') {
|
|
41
|
+
states.set(threadId, 'dirty')
|
|
42
|
+
|
|
43
|
+
return false
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
states.set(threadId, 'pending')
|
|
47
|
+
|
|
48
|
+
return true
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
// The single ingress can reserve at most one entry beyond queue capacity.
|
|
52
|
+
// Backpressure retains scan tails instead of repeatedly dropping later lanes.
|
|
53
|
+
if (offer) yield* Queue.offer(pending, threadId)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const initial = Stream.paginate(undefined, (afterThreadId: ThreadId | undefined) =>
|
|
57
|
+
runtime
|
|
58
|
+
.discoverWorkThreads({
|
|
59
|
+
limit: 32,
|
|
60
|
+
...(afterThreadId === undefined ? {} : { afterThreadId }),
|
|
61
|
+
})
|
|
62
|
+
.pipe(
|
|
63
|
+
Effect.map(
|
|
64
|
+
(page): readonly [ReadonlyArray<ThreadId>, Option.Option<ThreadId | undefined>] => [
|
|
65
|
+
page.threadIds,
|
|
66
|
+
page.afterThreadId === undefined ? Option.none() : Option.some(page.afterThreadId),
|
|
67
|
+
],
|
|
68
|
+
),
|
|
69
|
+
))
|
|
70
|
+
|
|
71
|
+
// Merge acquires both streams concurrently; it does not guarantee that the wake
|
|
72
|
+
// subscription precedes the initial read. The scheduler's fallback repairs lost hints.
|
|
73
|
+
// Initial completion is normal; wake completion must stop the entire pool.
|
|
74
|
+
const ingress = Stream.merge(initial, wake.wakes, { haltStrategy: 'right' }).pipe(
|
|
75
|
+
Stream.runForEach(enqueue),
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
const consume = Effect.gen(function*() {
|
|
79
|
+
const threadId = yield* Queue.take(pending)
|
|
80
|
+
|
|
81
|
+
yield* Effect.sync(() => states.set(threadId, 'active'))
|
|
82
|
+
|
|
83
|
+
let again = true
|
|
84
|
+
let recoveryCursor: string | undefined
|
|
85
|
+
|
|
86
|
+
while (again) {
|
|
87
|
+
const recovered = yield* runtime.runRecovery({
|
|
88
|
+
threadId,
|
|
89
|
+
...(recoveryCursor === undefined ? {} : { cursor: recoveryCursor }),
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
const blocked = recovered.blocked[0]
|
|
93
|
+
|
|
94
|
+
if (blocked !== undefined) return yield* blocked
|
|
95
|
+
recoveryCursor = recovered.cursor
|
|
96
|
+
yield* runtime.processThreadResolved(threadId)
|
|
97
|
+
again = yield* Effect.sync(() => {
|
|
98
|
+
if (states.get(threadId) === 'dirty' || recoveryCursor !== undefined) {
|
|
99
|
+
states.set(threadId, 'active')
|
|
100
|
+
|
|
101
|
+
return true
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
states.delete(threadId)
|
|
105
|
+
|
|
106
|
+
return false
|
|
107
|
+
})
|
|
108
|
+
// Re-drain inline: workers re-enqueuing into a full queue can all deadlock.
|
|
109
|
+
// This retains the runtime's drain-to-empty fairness, including parked returns.
|
|
110
|
+
}
|
|
111
|
+
}).pipe(Effect.forever)
|
|
112
|
+
|
|
113
|
+
// Any exit stops and joins every other participant; no detached producer or worker.
|
|
114
|
+
yield* Effect.raceAllFirst([ingress, ...Array.from({ length: concurrency }, () => consume)])
|
|
115
|
+
}),
|
|
116
|
+
)
|
package/README.md
DELETED