@llm4ts/flow 4.12.0 → 4.13.0
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/dist/Approval.d.ts +1 -1
- package/dist/Approval.d.ts.map +1 -1
- package/dist/Artifacts.d.ts +2 -2
- package/dist/Artifacts.d.ts.map +1 -1
- package/dist/BenchReport.d.ts +2 -2
- package/dist/BenchReport.d.ts.map +1 -1
- package/dist/CostLedger.d.ts +2 -2
- package/dist/CostLedger.d.ts.map +1 -1
- package/dist/Equiv.d.ts +3 -3
- package/dist/Equiv.d.ts.map +1 -1
- package/dist/Flow.d.ts +1 -1
- package/dist/Flow.d.ts.map +1 -1
- package/dist/FlowError.d.ts +30 -1
- package/dist/FlowError.d.ts.map +1 -1
- package/dist/FlowError.js +37 -1
- package/dist/FlowError.js.map +1 -1
- package/dist/Gates.d.ts +1 -1
- package/dist/Gates.d.ts.map +1 -1
- package/dist/GraphCache.d.ts +1 -1
- package/dist/GraphCache.d.ts.map +1 -1
- package/dist/Judgment.d.ts +2 -2
- package/dist/Judgment.d.ts.map +1 -1
- package/dist/Landing.d.ts +1 -1
- package/dist/Landing.d.ts.map +1 -1
- package/dist/NodePreflight.d.ts +1 -1
- package/dist/NodePreflight.d.ts.map +1 -1
- package/dist/Persistence.d.ts +2 -2
- package/dist/Persistence.d.ts.map +1 -1
- package/dist/PlanExecution.d.ts +2 -2
- package/dist/PlanExecution.d.ts.map +1 -1
- package/dist/ProgramJudge.d.ts +1 -1
- package/dist/ProgramJudge.d.ts.map +1 -1
- package/dist/Replay.d.ts +2 -2
- package/dist/Replay.d.ts.map +1 -1
- package/dist/Retro.d.ts +3 -3
- package/dist/Retro.d.ts.map +1 -1
- package/dist/Review.d.ts +5 -5
- package/dist/Review.d.ts.map +1 -1
- package/dist/ReviewCache.d.ts +1 -1
- package/dist/ReviewCache.d.ts.map +1 -1
- package/dist/Roster.d.ts +1 -1
- package/dist/Roster.d.ts.map +1 -1
- package/dist/RosterSeats.d.ts +1 -1
- package/dist/RosterSeats.d.ts.map +1 -1
- package/dist/Stories.d.ts +1 -1
- package/dist/Stories.d.ts.map +1 -1
- package/dist/WorkQueue.d.ts +1 -1
- package/dist/WorkQueue.d.ts.map +1 -1
- package/dist/WriterLock.d.ts +73 -0
- package/dist/WriterLock.d.ts.map +1 -0
- package/dist/WriterLock.js +142 -0
- package/dist/WriterLock.js.map +1 -0
- package/dist/settings/Registry.d.ts +8 -1
- package/dist/settings/Registry.d.ts.map +1 -1
- package/dist/settings/Registry.js +8 -0
- package/dist/settings/Registry.js.map +1 -1
- package/package.json +3 -2
- package/src/FlowError.ts +39 -1
- package/src/WriterLock.ts +225 -0
- package/src/settings/Registry.ts +9 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect"
|
|
2
|
+
import * as Option from "effect/Option"
|
|
3
|
+
import * as Schema from "effect/Schema"
|
|
4
|
+
import type * as Scope from "effect/Scope"
|
|
5
|
+
import * as Semaphore from "effect/Semaphore"
|
|
6
|
+
import { CheckoutBusy, PersistenceError } from "./FlowError.ts"
|
|
7
|
+
import { makeMemoryPlainFileStore, type PlainFileStoreShape } from "./Persistence.ts"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* One writing run per checkout, across processes (ADR 0058). The flow process
|
|
11
|
+
* itself holds a lock file outside the checkout for as long as it runs, so a
|
|
12
|
+
* run started from a terminal, from the operator server or from anything
|
|
13
|
+
* else is refused while another one writes.
|
|
14
|
+
*
|
|
15
|
+
* The file names the run's pid and the process groups of the children it has
|
|
16
|
+
* running. A lock whose run is gone still holds while one of those groups is
|
|
17
|
+
* alive: a coding agent that outlived a killed flow keeps writing.
|
|
18
|
+
*/
|
|
19
|
+
export class WriterHolder extends Schema.Class<WriterHolder>("WriterHolder")({
|
|
20
|
+
pid: Schema.Int,
|
|
21
|
+
runId: Schema.String,
|
|
22
|
+
flow: Schema.optionalKey(Schema.String),
|
|
23
|
+
workDir: Schema.String,
|
|
24
|
+
startedAt: Schema.Number,
|
|
25
|
+
/** Process groups of the children the run has running right now. */
|
|
26
|
+
groups: Schema.Array(Schema.Int)
|
|
27
|
+
}) {}
|
|
28
|
+
|
|
29
|
+
const decodeHolder = Schema.decodeUnknownOption(Schema.fromJsonString(WriterHolder))
|
|
30
|
+
const encodeHolder = Schema.encodeSync(Schema.fromJsonString(WriterHolder))
|
|
31
|
+
|
|
32
|
+
/** What the lock needs from the machine; the Node host is in the runner. */
|
|
33
|
+
export interface WriterLockHost {
|
|
34
|
+
readonly files: PlainFileStoreShape
|
|
35
|
+
/** Creates the file with its contents only when it does not exist; false when it does. */
|
|
36
|
+
readonly createExclusive: (
|
|
37
|
+
path: string,
|
|
38
|
+
contents: string
|
|
39
|
+
) => Effect.Effect<boolean, PersistenceError>
|
|
40
|
+
readonly isAlive: (pid: number) => boolean
|
|
41
|
+
/** True while any process of the group `pgid` leads is alive. */
|
|
42
|
+
readonly groupAlive: (pgid: number) => boolean
|
|
43
|
+
/** This process. */
|
|
44
|
+
readonly pid: number
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface WriterLockRequest {
|
|
48
|
+
/** The lock file of the checkout. */
|
|
49
|
+
readonly path: string
|
|
50
|
+
readonly workDir: string
|
|
51
|
+
readonly runId: string
|
|
52
|
+
readonly flow?: string
|
|
53
|
+
readonly startedAt: number
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface HeldWriterLock {
|
|
57
|
+
/** Records the process groups of the children running now. */
|
|
58
|
+
readonly setGroups: (groups: ReadonlyArray<number>) => Effect.Effect<void>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Who holds a checkout: its run, or only processes that run left behind. */
|
|
62
|
+
export interface LiveHolder {
|
|
63
|
+
readonly holder: WriterHolder
|
|
64
|
+
readonly alive: "run" | "children"
|
|
65
|
+
readonly groups: ReadonlyArray<number>
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface WriterLockShape {
|
|
69
|
+
/** Holds the checkout until the scope closes; `CheckoutBusy` when another run has it. */
|
|
70
|
+
readonly hold: (
|
|
71
|
+
request: WriterLockRequest
|
|
72
|
+
) => Effect.Effect<HeldWriterLock, CheckoutBusy | PersistenceError, Scope.Scope>
|
|
73
|
+
/** The live holder of a lock file, if any. */
|
|
74
|
+
readonly holder: (path: string) => Effect.Effect<LiveHolder | undefined, PersistenceError>
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const checkoutBusy = (path: string, live: LiveHolder): CheckoutBusy =>
|
|
78
|
+
CheckoutBusy.make({
|
|
79
|
+
workDir: live.holder.workDir,
|
|
80
|
+
lockPath: path,
|
|
81
|
+
pid: live.holder.pid,
|
|
82
|
+
runId: live.holder.runId,
|
|
83
|
+
alive: live.alive,
|
|
84
|
+
groups: live.groups,
|
|
85
|
+
...(live.holder.flow === undefined ? {} : { flow: live.holder.flow })
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const guardStepMs = 25
|
|
89
|
+
const guardAttempts = 200
|
|
90
|
+
|
|
91
|
+
export const makeWriterLock = (host: WriterLockHost): WriterLockShape => {
|
|
92
|
+
const readHolder = (path: string) =>
|
|
93
|
+
host.files
|
|
94
|
+
.read(path)
|
|
95
|
+
.pipe(
|
|
96
|
+
Effect.map((text) =>
|
|
97
|
+
text === undefined ? undefined : Option.getOrUndefined(decodeHolder(text))
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
const liveOf = (holder: WriterHolder | undefined): LiveHolder | undefined => {
|
|
102
|
+
if (holder === undefined) return undefined
|
|
103
|
+
if (host.isAlive(holder.pid)) return { holder, alive: "run", groups: [] }
|
|
104
|
+
const groups = holder.groups.filter((group) => host.groupAlive(group))
|
|
105
|
+
return groups.length === 0 ? undefined : { holder, alive: "children", groups }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Check-and-take must be one step across processes. A guard file, created
|
|
109
|
+
// exclusively, serializes it; a guard whose process is gone is removed.
|
|
110
|
+
const takeGuard = (guard: string): Effect.Effect<void, PersistenceError> =>
|
|
111
|
+
Effect.gen(function* () {
|
|
112
|
+
for (let attempt = 0; attempt < guardAttempts; attempt++) {
|
|
113
|
+
if (yield* host.createExclusive(guard, String(host.pid))) return
|
|
114
|
+
const owner = Number((yield* host.files.read(guard)) ?? "")
|
|
115
|
+
if (!Number.isInteger(owner) || !host.isAlive(owner)) {
|
|
116
|
+
yield* host.files.remove(guard)
|
|
117
|
+
} else {
|
|
118
|
+
yield* Effect.sleep(guardStepMs)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return yield* PersistenceError.make({
|
|
122
|
+
message: `could not take ${guard}: another llm4ts process holds it; remove the file if none is starting`
|
|
123
|
+
})
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
const guarded = <A, E>(path: string, body: Effect.Effect<A, E>) => {
|
|
127
|
+
const guard = `${path}.guard`
|
|
128
|
+
return Effect.acquireUseRelease(
|
|
129
|
+
takeGuard(guard),
|
|
130
|
+
() => body,
|
|
131
|
+
() => Effect.ignore(host.files.remove(guard))
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const hold: WriterLockShape["hold"] = (request) => {
|
|
136
|
+
const mine = (groups: ReadonlyArray<number>): string =>
|
|
137
|
+
encodeHolder(
|
|
138
|
+
new WriterHolder({
|
|
139
|
+
pid: host.pid,
|
|
140
|
+
runId: request.runId,
|
|
141
|
+
workDir: request.workDir,
|
|
142
|
+
startedAt: request.startedAt,
|
|
143
|
+
groups: [...groups],
|
|
144
|
+
...(request.flow === undefined ? {} : { flow: request.flow })
|
|
145
|
+
})
|
|
146
|
+
)
|
|
147
|
+
const take = guarded(
|
|
148
|
+
request.path,
|
|
149
|
+
Effect.gen(function* () {
|
|
150
|
+
const current = yield* readHolder(request.path)
|
|
151
|
+
// A run nested in this process shares the outer run's hold.
|
|
152
|
+
if (current?.pid === host.pid) return false
|
|
153
|
+
const live = liveOf(current)
|
|
154
|
+
if (live !== undefined) return yield* checkoutBusy(request.path, live)
|
|
155
|
+
yield* host.files.writeAtomic(request.path, mine([]))
|
|
156
|
+
return true
|
|
157
|
+
})
|
|
158
|
+
)
|
|
159
|
+
const release = (owned: boolean) =>
|
|
160
|
+
owned
|
|
161
|
+
? Effect.ignore(
|
|
162
|
+
Effect.gen(function* () {
|
|
163
|
+
const current = yield* readHolder(request.path)
|
|
164
|
+
if (current?.pid === host.pid && current.runId === request.runId) {
|
|
165
|
+
yield* host.files.remove(request.path)
|
|
166
|
+
}
|
|
167
|
+
})
|
|
168
|
+
)
|
|
169
|
+
: Effect.void
|
|
170
|
+
return Effect.acquireRelease(take, release).pipe(
|
|
171
|
+
Effect.map((owned) => ({
|
|
172
|
+
setGroups: (groups) =>
|
|
173
|
+
owned ? Effect.ignore(host.files.writeAtomic(request.path, mine(groups))) : Effect.void
|
|
174
|
+
}))
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return { hold, holder: (path) => readHolder(path).pipe(Effect.map(liveOf)) }
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface MemoryWriterLockHost {
|
|
182
|
+
readonly host: WriterLockHost
|
|
183
|
+
readonly files: Effect.Effect<Readonly<Record<string, string>>>
|
|
184
|
+
readonly setAlive: (pid: number, alive: boolean) => void
|
|
185
|
+
readonly setGroupAlive: (pgid: number, alive: boolean) => void
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The in-memory host for tests: `pid` is alive from the start. */
|
|
189
|
+
export const makeMemoryWriterLockHost = (
|
|
190
|
+
pid: number,
|
|
191
|
+
shared?: MemoryWriterLockHost
|
|
192
|
+
): Effect.Effect<MemoryWriterLockHost> =>
|
|
193
|
+
Effect.gen(function* () {
|
|
194
|
+
if (shared !== undefined) {
|
|
195
|
+
shared.setAlive(pid, true)
|
|
196
|
+
return { ...shared, host: { ...shared.host, pid } }
|
|
197
|
+
}
|
|
198
|
+
const memory = yield* makeMemoryPlainFileStore()
|
|
199
|
+
const exclusive = yield* Semaphore.make(1)
|
|
200
|
+
const alive = new Set<number>([pid])
|
|
201
|
+
const groups = new Set<number>()
|
|
202
|
+
const toggle = (set: Set<number>) => (member: number, on: boolean) => {
|
|
203
|
+
if (on) set.add(member)
|
|
204
|
+
else set.delete(member)
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
host: {
|
|
208
|
+
files: memory.store,
|
|
209
|
+
createExclusive: (path, contents) =>
|
|
210
|
+
exclusive.withPermits(1)(
|
|
211
|
+
Effect.gen(function* () {
|
|
212
|
+
if ((yield* memory.store.read(path)) !== undefined) return false
|
|
213
|
+
yield* memory.store.writeAtomic(path, contents)
|
|
214
|
+
return true
|
|
215
|
+
})
|
|
216
|
+
),
|
|
217
|
+
isAlive: (candidate) => alive.has(candidate),
|
|
218
|
+
groupAlive: (candidate) => groups.has(candidate),
|
|
219
|
+
pid
|
|
220
|
+
},
|
|
221
|
+
files: memory.files,
|
|
222
|
+
setAlive: toggle(alive),
|
|
223
|
+
setGroupAlive: toggle(groups)
|
|
224
|
+
}
|
|
225
|
+
})
|
package/src/settings/Registry.ts
CHANGED
|
@@ -783,6 +783,15 @@ export const settings = {
|
|
|
783
783
|
doc: "The Gemini model the bridge reasons with."
|
|
784
784
|
}),
|
|
785
785
|
|
|
786
|
+
// ---- run lifecycle ---------------------------------------------------------
|
|
787
|
+
LLM4TS_WRITER_LOCK: setting({
|
|
788
|
+
group: "launch",
|
|
789
|
+
scope: "global",
|
|
790
|
+
value: Value.toggle,
|
|
791
|
+
fallback: fallback.value(true, "on"),
|
|
792
|
+
doc: "One writing run per checkout: a second one is refused while the first, or a process it started, is alive. off lets runs write side by side."
|
|
793
|
+
}),
|
|
794
|
+
|
|
786
795
|
// ---- operator ---------------------------------------------------------------
|
|
787
796
|
LLM4TS_MCP_TOKEN: setting({
|
|
788
797
|
group: "operator",
|