@xmodelin/dsh-collab-sessions 0.0.0-stage → 0.0.1
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 +685 -2
- package/client.js +2172 -0
- package/cordis.patch.yml +195 -0
- package/icon.svg +7 -0
- package/index.js +2583 -0
- package/lib/auth.js +187 -0
- package/lib/layout.js +821 -0
- package/lib/notices.js +135 -0
- package/lib/relationships.js +157 -0
- package/lib/requests.js +315 -0
- package/lib/tasks.js +375 -0
- package/lib/write-scope.js +96 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +61 -4
package/lib/notices.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable at-least-once outbox for task/request protocol notices.
|
|
3
|
+
*
|
|
4
|
+
* The task and request stores are authoritative; a notice is only a wake-up
|
|
5
|
+
* payload. Persisting it before delivery prevents a committed state change from
|
|
6
|
+
* losing its notification when a peer workdir is temporarily unavailable.
|
|
7
|
+
*/
|
|
8
|
+
import crypto from 'node:crypto'
|
|
9
|
+
|
|
10
|
+
import { fileStamp, isSessionId, readStateJson, withFileLock, writeJson } from './layout.js'
|
|
11
|
+
|
|
12
|
+
const MAX_NOTICES = 500
|
|
13
|
+
|
|
14
|
+
function normalizeNotice(entry) {
|
|
15
|
+
if (entry === null || typeof entry !== 'object') return undefined
|
|
16
|
+
if (typeof entry.id !== 'string' || entry.id.length === 0 || entry.id.length > 128) return undefined
|
|
17
|
+
if (!isSessionId(entry.from) || !isSessionId(entry.to)) return undefined
|
|
18
|
+
if (entry.payload === null || typeof entry.payload !== 'object') return undefined
|
|
19
|
+
return {
|
|
20
|
+
id: entry.id,
|
|
21
|
+
from: entry.from,
|
|
22
|
+
to: entry.to,
|
|
23
|
+
payload: entry.payload,
|
|
24
|
+
createdAt: typeof entry.createdAt === 'number' ? entry.createdAt : Date.now(),
|
|
25
|
+
updatedAt: typeof entry.updatedAt === 'number' ? entry.updatedAt : Date.now(),
|
|
26
|
+
attempts: Number.isInteger(entry.attempts) ? entry.attempts : 0,
|
|
27
|
+
lastError: typeof entry.lastError === 'string' ? entry.lastError : null,
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Durable notice queue stored beside the request/task state. */
|
|
32
|
+
export class NoticeStore {
|
|
33
|
+
constructor(file) {
|
|
34
|
+
this.file = file
|
|
35
|
+
this.state = undefined
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
read() {
|
|
39
|
+
const stamp = fileStamp(this.file)
|
|
40
|
+
if (this.state !== undefined && this.stamp === stamp) return this.state
|
|
41
|
+
const raw = readStateJson(this.file)
|
|
42
|
+
const source = raw?.notices !== null && typeof raw?.notices === 'object' ? raw.notices : {}
|
|
43
|
+
const notices = {}
|
|
44
|
+
for (const entry of Object.values(source)) {
|
|
45
|
+
const normalized = normalizeNotice(entry)
|
|
46
|
+
if (normalized !== undefined) notices[normalized.id] = normalized
|
|
47
|
+
}
|
|
48
|
+
this.state = { version: 1, notices }
|
|
49
|
+
this.stamp = stamp
|
|
50
|
+
return this.state
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
write(state) {
|
|
54
|
+
this.state = state
|
|
55
|
+
writeJson(this.file, { version: 1, notices: state.notices }, 0o600)
|
|
56
|
+
this.stamp = fileStamp(this.file)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
enqueue({ from, to, payload }, now = Date.now()) {
|
|
60
|
+
if (!isSessionId(from) || !isSessionId(to) || from === to) {
|
|
61
|
+
throw new Error('notice requires two different session ids')
|
|
62
|
+
}
|
|
63
|
+
if (payload === null || typeof payload !== 'object') throw new Error('notice payload must be an object')
|
|
64
|
+
return withFileLock(this.file, () => {
|
|
65
|
+
this.state = undefined
|
|
66
|
+
const state = this.read()
|
|
67
|
+
const id = `notice-${crypto.randomUUID()}`
|
|
68
|
+
const notice = {
|
|
69
|
+
id,
|
|
70
|
+
from,
|
|
71
|
+
to,
|
|
72
|
+
payload,
|
|
73
|
+
createdAt: now,
|
|
74
|
+
updatedAt: now,
|
|
75
|
+
attempts: 0,
|
|
76
|
+
lastError: null,
|
|
77
|
+
}
|
|
78
|
+
state.notices[id] = notice
|
|
79
|
+
// Bound the queue; the oldest notice is dropped only after the cap is exceeded.
|
|
80
|
+
const entries = Object.values(state.notices).sort((left, right) => left.createdAt - right.createdAt)
|
|
81
|
+
while (entries.length > MAX_NOTICES) {
|
|
82
|
+
const dropped = entries.shift()
|
|
83
|
+
if (dropped !== undefined) delete state.notices[dropped.id]
|
|
84
|
+
}
|
|
85
|
+
this.write(state)
|
|
86
|
+
return notice
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
remove(id) {
|
|
91
|
+
return withFileLock(this.file, () => {
|
|
92
|
+
this.state = undefined
|
|
93
|
+
const state = this.read()
|
|
94
|
+
if (state.notices[id] === undefined) return false
|
|
95
|
+
delete state.notices[id]
|
|
96
|
+
this.write(state)
|
|
97
|
+
return true
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
fail(id, error, now = Date.now()) {
|
|
102
|
+
return withFileLock(this.file, () => {
|
|
103
|
+
this.state = undefined
|
|
104
|
+
const state = this.read()
|
|
105
|
+
const notice = state.notices[id]
|
|
106
|
+
if (notice === undefined) return undefined
|
|
107
|
+
notice.attempts += 1
|
|
108
|
+
notice.lastError = String(error instanceof Error ? error.message : error)
|
|
109
|
+
notice.updatedAt = now
|
|
110
|
+
this.write(state)
|
|
111
|
+
return notice
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
prune(now = Date.now(), maxAgeMs = 7 * 24 * 60 * 60 * 1000) {
|
|
116
|
+
return withFileLock(this.file, () => {
|
|
117
|
+
this.state = undefined
|
|
118
|
+
const state = this.read()
|
|
119
|
+
let changed = false
|
|
120
|
+
for (const [id, notice] of Object.entries(state.notices)) {
|
|
121
|
+
if (now - notice.createdAt <= maxAgeMs) continue
|
|
122
|
+
delete state.notices[id]
|
|
123
|
+
changed = true
|
|
124
|
+
}
|
|
125
|
+
if (changed) this.write(state)
|
|
126
|
+
return changed
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
list() {
|
|
131
|
+
return Object.values(this.read().notices)
|
|
132
|
+
.sort((left, right) => left.createdAt - right.createdAt)
|
|
133
|
+
.map(notice => ({ ...notice, payload: structuredClone(notice.payload) }))
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supervisor-worker relationships: authorization flags plus an optional
|
|
3
|
+
* human-pinned capability override. The worker's live capability is derived
|
|
4
|
+
* by the Host, so a base-prompt update never silently keeps a stale snapshot.
|
|
5
|
+
*/
|
|
6
|
+
import { fileStamp, isSessionId, readStateJson, withFileLock, writeJson } from './layout.js'
|
|
7
|
+
|
|
8
|
+
function normalizeCapability(input) {
|
|
9
|
+
if (input === null || typeof input !== 'object') return null
|
|
10
|
+
const name = typeof input.name === 'string' ? input.name.trim() : ''
|
|
11
|
+
if (name.length === 0) return null
|
|
12
|
+
return {
|
|
13
|
+
name,
|
|
14
|
+
...(typeof input.description === 'string' && input.description.trim().length > 0
|
|
15
|
+
? { description: input.description.trim() }
|
|
16
|
+
: {}),
|
|
17
|
+
...(Array.isArray(input.tags) ? { tags: input.tags.filter(tag => typeof tag === 'string') } : {}),
|
|
18
|
+
...(Array.isArray(input.inputs) ? { inputs: input.inputs.filter(value => typeof value === 'string') } : {}),
|
|
19
|
+
...(Array.isArray(input.outputs) ? { outputs: input.outputs.filter(value => typeof value === 'string') } : {}),
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Durable supervisor-worker relationship registry. */
|
|
24
|
+
export class RelationshipStore {
|
|
25
|
+
constructor(file) {
|
|
26
|
+
this.file = file
|
|
27
|
+
this.state = undefined
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
read() {
|
|
31
|
+
const stamp = fileStamp(this.file)
|
|
32
|
+
if (this.state !== undefined && this.stamp === stamp) return this.state
|
|
33
|
+
const raw = readStateJson(this.file)
|
|
34
|
+
const relationships = Array.isArray(raw?.relationships)
|
|
35
|
+
? raw.relationships.filter(entry => (
|
|
36
|
+
isSessionId(entry?.supervisor)
|
|
37
|
+
&& isSessionId(entry?.worker)
|
|
38
|
+
&& entry.supervisor !== entry.worker
|
|
39
|
+
)).map(entry => {
|
|
40
|
+
const override = normalizeCapability(entry.capabilityOverride)
|
|
41
|
+
const legacy = normalizeCapability(entry.legacyCapability) ?? normalizeCapability(entry.capability)
|
|
42
|
+
return {
|
|
43
|
+
supervisor: entry.supervisor,
|
|
44
|
+
worker: entry.worker,
|
|
45
|
+
capabilityOverride: override,
|
|
46
|
+
...(legacy === null ? {} : { legacyCapability: legacy }),
|
|
47
|
+
...(typeof entry.capabilityVersion === 'string' && entry.capabilityVersion.length > 0
|
|
48
|
+
? { capabilityVersion: entry.capabilityVersion }
|
|
49
|
+
: {}),
|
|
50
|
+
offer: entry.offer === true,
|
|
51
|
+
request: entry.request === true,
|
|
52
|
+
createdAt: typeof entry.createdAt === 'number' ? entry.createdAt : Date.now(),
|
|
53
|
+
updatedAt: typeof entry.updatedAt === 'number' ? entry.updatedAt : Date.now(),
|
|
54
|
+
...(typeof entry.createdBy === 'string' ? { createdBy: entry.createdBy } : {}),
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
: []
|
|
58
|
+
this.state = { version: 1, relationships }
|
|
59
|
+
this.stamp = stamp
|
|
60
|
+
return this.state
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
write(state) {
|
|
64
|
+
this.state = state
|
|
65
|
+
writeJson(this.file, { version: 1, relationships: state.relationships })
|
|
66
|
+
this.stamp = fileStamp(this.file)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
list() {
|
|
70
|
+
return this.read().relationships.map(entry => ({ ...entry }))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
listForSupervisor(supervisor) {
|
|
74
|
+
return this.list().filter(entry => entry.supervisor === supervisor)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
listForWorker(worker) {
|
|
78
|
+
return this.list().filter(entry => entry.worker === worker)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get(supervisor, worker) {
|
|
82
|
+
return this.read().relationships.find(entry => entry.supervisor === supervisor && entry.worker === worker)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Whether this session is a registered executor for at least one supervisor. */
|
|
86
|
+
hasOffer(sessionId) {
|
|
87
|
+
return this.read().relationships.some(entry => entry.worker === sessionId && entry.offer === true)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Backward-compatible worker check: only offered executors count as workers. */
|
|
91
|
+
isWorker(sessionId) {
|
|
92
|
+
return this.hasOffer(sessionId)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
offers(supervisor, worker) {
|
|
96
|
+
return this.get(supervisor, worker)?.offer === true
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
upsert({
|
|
100
|
+
supervisor, worker, capabilityOverride = undefined, capabilityVersion = undefined,
|
|
101
|
+
offer = true, request = false, createdBy,
|
|
102
|
+
}, now = Date.now()) {
|
|
103
|
+
if (!isSessionId(supervisor) || !isSessionId(worker) || supervisor === worker) {
|
|
104
|
+
throw new Error('relationship requires two different session ids')
|
|
105
|
+
}
|
|
106
|
+
if (offer !== true && request !== true) throw new Error('a relationship must offer or request')
|
|
107
|
+
return withFileLock(this.file, () => {
|
|
108
|
+
this.state = undefined
|
|
109
|
+
const state = this.read()
|
|
110
|
+
const existing = state.relationships.find(entry => entry.supervisor === supervisor && entry.worker === worker)
|
|
111
|
+
const override = capabilityOverride === undefined
|
|
112
|
+
? (existing?.capabilityOverride ?? null)
|
|
113
|
+
: normalizeCapability(capabilityOverride)
|
|
114
|
+
const nextVersion = typeof capabilityVersion === 'string' && capabilityVersion.length > 0
|
|
115
|
+
? capabilityVersion
|
|
116
|
+
: (existing?.capabilityVersion ?? undefined)
|
|
117
|
+
if (existing !== undefined) {
|
|
118
|
+
Object.assign(existing, {
|
|
119
|
+
capabilityOverride: override,
|
|
120
|
+
...(nextVersion === undefined ? { capabilityVersion: undefined } : { capabilityVersion: nextVersion }),
|
|
121
|
+
offer,
|
|
122
|
+
request,
|
|
123
|
+
updatedAt: now,
|
|
124
|
+
...(typeof createdBy === 'string' ? { createdBy } : {}),
|
|
125
|
+
})
|
|
126
|
+
if (nextVersion === undefined) delete existing.capabilityVersion
|
|
127
|
+
this.write(state)
|
|
128
|
+
return existing
|
|
129
|
+
}
|
|
130
|
+
const relationship = {
|
|
131
|
+
supervisor,
|
|
132
|
+
worker,
|
|
133
|
+
capabilityOverride: override,
|
|
134
|
+
...(nextVersion === undefined ? {} : { capabilityVersion: nextVersion }),
|
|
135
|
+
offer,
|
|
136
|
+
request,
|
|
137
|
+
createdAt: now,
|
|
138
|
+
updatedAt: now,
|
|
139
|
+
...(typeof createdBy === 'string' ? { createdBy } : {}),
|
|
140
|
+
}
|
|
141
|
+
state.relationships.push(relationship)
|
|
142
|
+
this.write(state)
|
|
143
|
+
return relationship
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
remove(supervisor, worker) {
|
|
148
|
+
return withFileLock(this.file, () => {
|
|
149
|
+
this.state = undefined
|
|
150
|
+
const state = this.read()
|
|
151
|
+
const next = state.relationships.filter(entry => !(entry.supervisor === supervisor && entry.worker === worker))
|
|
152
|
+
if (next.length === state.relationships.length) return false
|
|
153
|
+
this.write({ ...state, relationships: next })
|
|
154
|
+
return true
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
}
|
package/lib/requests.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker-to-supervisor work requests. A request asks a supervisor to evaluate
|
|
3
|
+
* available worker capabilities and, when feasible, decompose the work into
|
|
4
|
+
* assigned tasks. The supervisor agent owns that evaluation; this store owns
|
|
5
|
+
* only the durable request lifecycle and result.
|
|
6
|
+
*/
|
|
7
|
+
import crypto from 'node:crypto'
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
|
|
10
|
+
import { fileStamp, isSessionId, readStateJson, withFileLock, writeJson } from './layout.js'
|
|
11
|
+
|
|
12
|
+
const REQUEST_STATUSES = new Set(['pending', 'accepted', 'delivered', 'rejected', 'cancelled', 'expired'])
|
|
13
|
+
|
|
14
|
+
function normalizeText(value, fallback = '') {
|
|
15
|
+
return typeof value === 'string' ? value.trim() : fallback
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function requestSummary(request) {
|
|
19
|
+
return {
|
|
20
|
+
id: request.id,
|
|
21
|
+
requester: request.requester,
|
|
22
|
+
supervisor: request.supervisor,
|
|
23
|
+
title: request.title,
|
|
24
|
+
requirement: request.requirement,
|
|
25
|
+
status: request.status,
|
|
26
|
+
...(request.reason === null ? {} : { reason: request.reason }),
|
|
27
|
+
...(request.taskId === null ? {} : { taskId: request.taskId }),
|
|
28
|
+
...(request.subtaskIds.length === 0 ? {} : { subtaskIds: [...request.subtaskIds] }),
|
|
29
|
+
...(request.response === null ? {} : { response: request.response }),
|
|
30
|
+
createdAt: request.createdAt,
|
|
31
|
+
updatedAt: request.updatedAt,
|
|
32
|
+
...(request.expiresAt === null ? {} : { expiresAt: request.expiresAt }),
|
|
33
|
+
...(request.executionDeadlineAt === null ? {} : { executionDeadlineAt: request.executionDeadlineAt }),
|
|
34
|
+
...(request.lastHeartbeatAt === null ? {} : { lastHeartbeatAt: request.lastHeartbeatAt }),
|
|
35
|
+
...(request.heartbeatNote === null ? {} : { heartbeatNote: request.heartbeatNote }),
|
|
36
|
+
...(request.overdue === true ? { overdue: true, overdueAt: request.overdueAt } : {}),
|
|
37
|
+
...(request.resolvedAt === null ? {} : { resolvedAt: request.resolvedAt }),
|
|
38
|
+
...(request.delivery === null ? {} : { delivery: request.delivery }),
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Requester-facing view: status only, with no internal project/task scheduling. */
|
|
43
|
+
function requesterSummary(request) {
|
|
44
|
+
return {
|
|
45
|
+
id: request.id,
|
|
46
|
+
requester: request.requester,
|
|
47
|
+
supervisor: request.supervisor,
|
|
48
|
+
title: request.title,
|
|
49
|
+
requirement: request.requirement,
|
|
50
|
+
status: request.status,
|
|
51
|
+
...(request.reason === null ? {} : { reason: request.reason }),
|
|
52
|
+
createdAt: request.createdAt,
|
|
53
|
+
updatedAt: request.updatedAt,
|
|
54
|
+
...(request.expiresAt === null ? {} : { expiresAt: request.expiresAt }),
|
|
55
|
+
...(request.executionDeadlineAt === null ? {} : { executionDeadlineAt: request.executionDeadlineAt }),
|
|
56
|
+
...(request.lastHeartbeatAt === null ? {} : { lastHeartbeatAt: request.lastHeartbeatAt }),
|
|
57
|
+
...(request.heartbeatNote === null ? {} : { heartbeatNote: request.heartbeatNote }),
|
|
58
|
+
...(request.overdue === true ? { overdue: true, overdueAt: request.overdueAt } : {}),
|
|
59
|
+
...(request.resolvedAt === null ? {} : { resolvedAt: request.resolvedAt }),
|
|
60
|
+
...(request.delivery === null ? {} : { delivery: request.delivery }),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Durable worker requests, keyed by request id. */
|
|
65
|
+
export class RequestStore {
|
|
66
|
+
constructor(file) {
|
|
67
|
+
this.file = file
|
|
68
|
+
this.state = undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
read() {
|
|
72
|
+
const stamp = fileStamp(this.file)
|
|
73
|
+
if (this.state !== undefined && this.stamp === stamp) return this.state
|
|
74
|
+
const raw = readStateJson(this.file)
|
|
75
|
+
const source = raw?.requests !== null && typeof raw?.requests === 'object' ? raw.requests : {}
|
|
76
|
+
const requests = {}
|
|
77
|
+
for (const [id, entry] of Object.entries(source)) {
|
|
78
|
+
if (entry === null || typeof entry !== 'object') continue
|
|
79
|
+
const requester = typeof entry.requester === 'string' ? entry.requester : entry.worker
|
|
80
|
+
if (!isSessionId(requester) || !isSessionId(entry.supervisor)) continue
|
|
81
|
+
const normalized = { ...entry, requester }
|
|
82
|
+
delete normalized.worker
|
|
83
|
+
if (normalized.delivery === undefined) normalized.delivery = null
|
|
84
|
+
if (normalized.expiresAt === undefined) normalized.expiresAt = null
|
|
85
|
+
if (normalized.executionDeadlineAt === undefined) normalized.executionDeadlineAt = null
|
|
86
|
+
if (normalized.lastHeartbeatAt === undefined) normalized.lastHeartbeatAt = null
|
|
87
|
+
if (normalized.heartbeatNote === undefined) normalized.heartbeatNote = null
|
|
88
|
+
if (normalized.overdue === undefined) normalized.overdue = false
|
|
89
|
+
if (normalized.overdueAt === undefined) normalized.overdueAt = null
|
|
90
|
+
normalized.taskId = typeof normalized.taskId === 'string'
|
|
91
|
+
? normalized.taskId
|
|
92
|
+
: (typeof normalized.projectId === 'string' ? normalized.projectId : null)
|
|
93
|
+
normalized.subtaskIds = Array.isArray(normalized.subtaskIds)
|
|
94
|
+
? normalized.subtaskIds.filter(value => typeof value === 'string')
|
|
95
|
+
: (Array.isArray(normalized.taskIds) ? normalized.taskIds.filter(value => typeof value === 'string') : [])
|
|
96
|
+
delete normalized.projectId
|
|
97
|
+
delete normalized.taskIds
|
|
98
|
+
if (normalized.response === undefined) normalized.response = null
|
|
99
|
+
if (normalized.reason === undefined) normalized.reason = null
|
|
100
|
+
requests[id] = normalized
|
|
101
|
+
}
|
|
102
|
+
this.state = { version: 2, requests }
|
|
103
|
+
this.stamp = stamp
|
|
104
|
+
return this.state
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
write(state) {
|
|
108
|
+
this.state = state
|
|
109
|
+
writeJson(this.file, { version: 2, requests: state.requests })
|
|
110
|
+
this.stamp = fileStamp(this.file)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Persist a legacy document as the current task-centric schema once. */
|
|
114
|
+
migrate() {
|
|
115
|
+
if (!fs.existsSync(this.file)) return false
|
|
116
|
+
const raw = readStateJson(this.file)
|
|
117
|
+
if (raw?.version === 2) return false
|
|
118
|
+
return withFileLock(this.file, () => {
|
|
119
|
+
this.state = undefined
|
|
120
|
+
const state = this.read()
|
|
121
|
+
fs.copyFileSync(this.file, `${this.file}.v1.bak`)
|
|
122
|
+
this.write(state)
|
|
123
|
+
return true
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
get(id) {
|
|
128
|
+
return this.read().requests[id]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
list() {
|
|
132
|
+
return Object.values(this.read().requests)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
listForRequester(requester, status) {
|
|
136
|
+
return Object.values(this.read().requests)
|
|
137
|
+
.filter(request => request.requester === requester && (status === undefined || request.status === status))
|
|
138
|
+
.sort((left, right) => left.createdAt - right.createdAt)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
listForSupervisor(supervisor, status) {
|
|
142
|
+
return Object.values(this.read().requests)
|
|
143
|
+
.filter(request => request.supervisor === supervisor && (status === undefined || request.status === status))
|
|
144
|
+
.sort((left, right) => left.createdAt - right.createdAt)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
create({ requester, supervisor, title, requirement, createdTurn = null, expiresAt = null }, now = Date.now()) {
|
|
148
|
+
if (!isSessionId(requester) || !isSessionId(supervisor) || requester === supervisor) {
|
|
149
|
+
throw new Error('request requires two different session ids')
|
|
150
|
+
}
|
|
151
|
+
const normalizedTitle = normalizeText(title)
|
|
152
|
+
if (normalizedTitle.length === 0) throw new Error('request title must not be empty')
|
|
153
|
+
const normalizedRequirement = normalizeText(requirement)
|
|
154
|
+
if (normalizedRequirement.length === 0) throw new Error('request requirement must not be empty')
|
|
155
|
+
const id = `request-${crypto.randomUUID()}`
|
|
156
|
+
const request = {
|
|
157
|
+
id,
|
|
158
|
+
requester,
|
|
159
|
+
supervisor,
|
|
160
|
+
title: normalizedTitle,
|
|
161
|
+
requirement: normalizedRequirement,
|
|
162
|
+
status: 'pending',
|
|
163
|
+
reason: null,
|
|
164
|
+
taskId: null,
|
|
165
|
+
subtaskIds: [],
|
|
166
|
+
response: null,
|
|
167
|
+
createdTurn: Number.isInteger(createdTurn) ? createdTurn : null,
|
|
168
|
+
createdAt: now,
|
|
169
|
+
updatedAt: now,
|
|
170
|
+
resolvedAt: null,
|
|
171
|
+
delivery: null,
|
|
172
|
+
expiresAt: Number.isFinite(expiresAt) ? expiresAt : null,
|
|
173
|
+
executionDeadlineAt: null,
|
|
174
|
+
lastHeartbeatAt: null,
|
|
175
|
+
heartbeatNote: null,
|
|
176
|
+
overdue: false,
|
|
177
|
+
overdueAt: null,
|
|
178
|
+
}
|
|
179
|
+
return withFileLock(this.file, () => {
|
|
180
|
+
this.state = undefined
|
|
181
|
+
const state = this.read()
|
|
182
|
+
state.requests[id] = request
|
|
183
|
+
this.write(state)
|
|
184
|
+
return request
|
|
185
|
+
})
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
update(id, mutate, now = Date.now()) {
|
|
189
|
+
return withFileLock(this.file, () => {
|
|
190
|
+
this.state = undefined
|
|
191
|
+
const state = this.read()
|
|
192
|
+
const request = state.requests[id]
|
|
193
|
+
if (request === undefined) throw new Error(`unknown request: ${String(id)}`)
|
|
194
|
+
mutate(request, now)
|
|
195
|
+
request.updatedAt = now
|
|
196
|
+
this.write(state)
|
|
197
|
+
return request
|
|
198
|
+
})
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
accept(id, { taskId, subtaskIds = [], response = null, executionDeadlineAt = null }, now = Date.now()) {
|
|
202
|
+
const normalizedTaskId = normalizeText(taskId)
|
|
203
|
+
if (normalizedTaskId.length === 0) throw new Error('accept requires a task id')
|
|
204
|
+
return this.update(id, (request) => {
|
|
205
|
+
if (request.status !== 'pending') throw new Error(`request is already ${request.status}`)
|
|
206
|
+
if (Number.isFinite(request.expiresAt) && request.expiresAt <= now) throw new Error('request has expired')
|
|
207
|
+
request.status = 'accepted'
|
|
208
|
+
request.taskId = normalizedTaskId
|
|
209
|
+
request.subtaskIds = Array.isArray(subtaskIds) ? subtaskIds.filter(value => typeof value === 'string') : []
|
|
210
|
+
request.response = response === null ? null : String(response)
|
|
211
|
+
request.reason = null
|
|
212
|
+
request.resolvedAt = now
|
|
213
|
+
request.executionDeadlineAt = Number.isFinite(executionDeadlineAt) ? executionDeadlineAt : null
|
|
214
|
+
request.lastHeartbeatAt = null
|
|
215
|
+
request.heartbeatNote = null
|
|
216
|
+
request.overdue = false
|
|
217
|
+
request.overdueAt = null
|
|
218
|
+
}, now)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Revert an acceptance that could not be bound/notified; the timer is left to the caller. */
|
|
222
|
+
reopen(id, now = Date.now()) {
|
|
223
|
+
return this.update(id, (request) => {
|
|
224
|
+
if (request.status !== 'accepted') throw new Error(`request is ${request.status}`)
|
|
225
|
+
request.status = 'pending'
|
|
226
|
+
request.taskId = null
|
|
227
|
+
request.subtaskIds = []
|
|
228
|
+
request.response = null
|
|
229
|
+
request.reason = null
|
|
230
|
+
request.resolvedAt = null
|
|
231
|
+
request.delivery = null
|
|
232
|
+
request.executionDeadlineAt = null
|
|
233
|
+
request.lastHeartbeatAt = null
|
|
234
|
+
request.heartbeatNote = null
|
|
235
|
+
request.overdue = false
|
|
236
|
+
request.overdueAt = null
|
|
237
|
+
}, now)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
reject(id, reason, now = Date.now()) {
|
|
241
|
+
const normalizedReason = normalizeText(reason)
|
|
242
|
+
if (normalizedReason.length === 0) throw new Error('reject requires a reason')
|
|
243
|
+
return this.update(id, (request) => {
|
|
244
|
+
if (request.status !== 'pending') throw new Error(`request is already ${request.status}`)
|
|
245
|
+
if (Number.isFinite(request.expiresAt) && request.expiresAt <= now) throw new Error('request has expired')
|
|
246
|
+
request.status = 'rejected'
|
|
247
|
+
request.reason = normalizedReason
|
|
248
|
+
request.resolvedAt = now
|
|
249
|
+
}, now)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
expire(id, now = Date.now()) {
|
|
253
|
+
return this.update(id, (request) => {
|
|
254
|
+
if (request.status !== 'pending') throw new Error(`request is already ${request.status}`)
|
|
255
|
+
request.status = 'expired'
|
|
256
|
+
request.reason = 'supervisor did not respond before the deadline'
|
|
257
|
+
request.resolvedAt = now
|
|
258
|
+
}, now)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
markDelivered(id, delivery, now = Date.now()) {
|
|
262
|
+
return this.update(id, (request) => {
|
|
263
|
+
if (request.status !== 'accepted' && request.status !== 'delivered') {
|
|
264
|
+
throw new Error(`request is ${request.status}, not delivery-ready`)
|
|
265
|
+
}
|
|
266
|
+
request.status = 'delivered'
|
|
267
|
+
request.delivery = delivery
|
|
268
|
+
request.executionDeadlineAt = null
|
|
269
|
+
request.updatedAt = now
|
|
270
|
+
}, now)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
heartbeat(id, { note = null, extendByMs = 0 }, now = Date.now()) {
|
|
274
|
+
return this.update(id, (request) => {
|
|
275
|
+
if (request.status !== 'accepted') throw new Error(`request is ${request.status}, not active`)
|
|
276
|
+
const extension = Number.isFinite(extendByMs) ? Math.max(0, Math.min(extendByMs, 7 * 24 * 60 * 60 * 1000)) : 0
|
|
277
|
+
if (extension > 0) {
|
|
278
|
+
const base = Number.isFinite(request.executionDeadlineAt)
|
|
279
|
+
? Math.max(request.executionDeadlineAt, now)
|
|
280
|
+
: now
|
|
281
|
+
request.executionDeadlineAt = base + extension
|
|
282
|
+
request.overdue = false
|
|
283
|
+
request.overdueAt = null
|
|
284
|
+
}
|
|
285
|
+
request.lastHeartbeatAt = now
|
|
286
|
+
request.heartbeatNote = note === null ? null : String(note)
|
|
287
|
+
}, now)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
markOverdue(id, now = Date.now()) {
|
|
291
|
+
return this.update(id, (request) => {
|
|
292
|
+
if (request.status !== 'accepted' || request.overdue === true) return
|
|
293
|
+
request.overdue = true
|
|
294
|
+
request.overdueAt = now
|
|
295
|
+
}, now)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
cancel(id, requester, now = Date.now()) {
|
|
299
|
+
return this.update(id, (request) => {
|
|
300
|
+
if (request.requester !== requester) throw new Error('request belongs to another session')
|
|
301
|
+
if (request.status !== 'pending' && request.status !== 'accepted') {
|
|
302
|
+
throw new Error(`request is already ${request.status}`)
|
|
303
|
+
}
|
|
304
|
+
request.status = 'cancelled'
|
|
305
|
+
request.executionDeadlineAt = null
|
|
306
|
+
request.overdue = false
|
|
307
|
+
request.overdueAt = null
|
|
308
|
+
request.resolvedAt = now
|
|
309
|
+
}, now)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
static statuses = REQUEST_STATUSES
|
|
313
|
+
static summary = requestSummary
|
|
314
|
+
static requesterSummary = requesterSummary
|
|
315
|
+
}
|