@goodandready/dsh-kanban 0.2.34 → 0.2.35
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/lib/client.js +30 -34
- package/lib/config.js +6 -0
- package/lib/index.js +110 -1255
- package/lib/routes/board.js +282 -0
- package/lib/routes/security.js +274 -0
- package/lib/routes/task-actions.js +345 -0
- package/lib/routes/task-worktree.js +107 -0
- package/lib/routes/task.js +211 -0
- package/lib/routes.js +71 -1209
- package/lib/server/route-handlers.js +367 -0
- package/lib/server/services.js +225 -0
- package/lib/server/task-handler.js +530 -0
- package/lib/server/timers.js +204 -0
- package/lib/store.js +9 -2
- package/lib/watchdog.js +169 -0
- package/package.json +1 -1
package/lib/routes.js
CHANGED
|
@@ -1,1210 +1,72 @@
|
|
|
1
|
-
import crypto from 'node:crypto'
|
|
2
|
-
// HTTP route handlers for the board.
|
|
3
|
-
//
|
|
4
|
-
// The client bundle runs in the browser without direct access to SQLite or host services;
|
|
5
|
-
// it interacts with these handlers. Handlers accept parsed inputs and the store instance,
|
|
6
|
-
// returning response objects to facilitate testing without full HTTP server setups.
|
|
7
|
-
// Request/response wrapper integration lives in `lib/index.js`.
|
|
8
|
-
|
|
9
|
-
import { COLUMN_ORDER, columnsOf, wipLimitField, columnNamesOf } from './config.js'
|
|
10
|
-
import { parsePlan, planItems, planProgress, taskState, queuePosition } from './plan.js'
|
|
11
|
-
import { columnStats, doneWithin, stale } from './metrics.js'
|
|
12
|
-
import { isStale } from './time.js'
|
|
13
|
-
import { facetsOf, facetsOfTask, groupByRepo, sortByLife } from './filters.js'
|
|
14
|
-
import { parseBody } from './markdown.js'
|
|
15
|
-
import { computeNextRun } from './cron.js'
|
|
16
|
-
|
|
17
|
-
/** Maximum request payload size to prevent denial of service. */
|
|
18
|
-
export const MAX_BODY_BYTES = 256 * 1024
|
|
19
|
-
|
|
20
|
-
export function isLoopbackAddress(addr) {
|
|
21
|
-
if (!addr || typeof addr !== 'string') return false
|
|
22
|
-
const clean = addr.toLowerCase().replace(/^::ffff:/, '').replace(/^\[|\]$/g, '')
|
|
23
|
-
return (
|
|
24
|
-
clean === '127.0.0.1' ||
|
|
25
|
-
clean === '::1' ||
|
|
26
|
-
clean === 'localhost' ||
|
|
27
|
-
clean === 'localhost.' ||
|
|
28
|
-
clean.startsWith('127.')
|
|
29
|
-
)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function isLanAddress(addr) {
|
|
33
|
-
if (!addr || typeof addr !== 'string') return false
|
|
34
|
-
const clean = addr.toLowerCase().replace(/^::ffff:/, '').replace(/^\[|\]$/g, '')
|
|
35
|
-
if (isLoopbackAddress(clean)) return true
|
|
36
|
-
if (clean.startsWith('10.') || clean.startsWith('192.168.')) return true
|
|
37
|
-
const m = clean.match(/^172\.(\d+)\./)
|
|
38
|
-
if (m) {
|
|
39
|
-
const octet = parseInt(m[1], 10)
|
|
40
|
-
if (octet >= 16 && octet <= 31) return true
|
|
41
|
-
}
|
|
42
|
-
return false
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function isTrustedHost(host) {
|
|
46
|
-
if (!host || typeof host !== 'string') return false
|
|
47
|
-
let hostname = host.trim()
|
|
48
|
-
if (hostname.startsWith('[')) {
|
|
49
|
-
const end = hostname.indexOf(']')
|
|
50
|
-
if (end > 0) hostname = hostname.slice(1, end)
|
|
51
|
-
} else {
|
|
52
|
-
hostname = hostname.split(':')[0]
|
|
53
|
-
}
|
|
54
|
-
hostname = hostname.toLowerCase()
|
|
55
|
-
if (!hostname) return false
|
|
56
|
-
if (hostname === 'localhost' || hostname === 'localhost.' || hostname.endsWith('.local')) return true
|
|
57
|
-
if (isLoopbackAddress(hostname) || isLanAddress(hostname)) return true
|
|
58
|
-
return false
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function normalizeIp(addr) {
|
|
62
|
-
if (!addr || typeof addr !== 'string') return ''
|
|
63
|
-
return addr.toLowerCase().replace(/^::ffff:/, '').replace(/^\[|\]$/g, '')
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function cookieNameForAuthority(authority) {
|
|
67
|
-
if (!authority || typeof authority !== 'string') return ''
|
|
68
|
-
const hash = crypto.createHash('sha256').update(authority.trim()).digest('base64url')
|
|
69
|
-
return 'dsh-auth-' + hash
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function isLoopbackCookieName(name, host = '') {
|
|
73
|
-
if (!name || typeof name !== 'string' || !name.startsWith('dsh-auth-')) return false
|
|
74
|
-
const authorities = [
|
|
75
|
-
host,
|
|
76
|
-
'127.0.0.1:3000',
|
|
77
|
-
'127.0.0.1:3080',
|
|
78
|
-
'127.0.0.1:3082',
|
|
79
|
-
'127.0.0.1',
|
|
80
|
-
'localhost:3000',
|
|
81
|
-
'localhost:3080',
|
|
82
|
-
'localhost:3082',
|
|
83
|
-
'localhost',
|
|
84
|
-
'[::1]:3000',
|
|
85
|
-
'[::1]',
|
|
86
|
-
]
|
|
87
|
-
for (const auth of authorities) {
|
|
88
|
-
if (auth && isLoopbackAddress(auth.split(':')[0])) {
|
|
89
|
-
if (cookieNameForAuthority(auth) === name) return true
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return false
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function hasClientCredentials(req) {
|
|
96
|
-
if (!req || !req.headers) return false
|
|
97
|
-
if (req.headers.authorization) return true
|
|
98
|
-
if (req.headers['x-dsh-lan-pin']) return true
|
|
99
|
-
const cookie = req.headers.cookie
|
|
100
|
-
if (!cookie || typeof cookie !== 'string') return false
|
|
101
|
-
if (cookie.includes('dsh_token=') || cookie.includes('dsh_lan_pin=')) return true
|
|
102
|
-
|
|
103
|
-
const parts = cookie.split(';')
|
|
104
|
-
for (const part of parts) {
|
|
105
|
-
const trimmed = part.trim()
|
|
106
|
-
const eqIdx = trimmed.indexOf('=')
|
|
107
|
-
if (eqIdx === -1) continue
|
|
108
|
-
const name = trimmed.slice(0, eqIdx).trim()
|
|
109
|
-
if (name.startsWith('dsh-auth-')) {
|
|
110
|
-
if (!isLoopbackCookieName(name, req.headers.host)) {
|
|
111
|
-
return true
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return false
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function getClientIp(req) {
|
|
119
|
-
const socketAddress =
|
|
120
|
-
req?.socket?.remoteAddress ||
|
|
121
|
-
req?.connection?.remoteAddress ||
|
|
122
|
-
req?.info?.remoteAddress ||
|
|
123
|
-
''
|
|
124
|
-
const socketIp = normalizeIp(socketAddress)
|
|
125
|
-
const xff = req?.headers?.['x-forwarded-for']
|
|
126
|
-
if (xff && typeof xff === 'string' && (isLoopbackAddress(socketIp) || !socketIp)) {
|
|
127
|
-
const first = xff.split(',')[0].trim()
|
|
128
|
-
if (first) {
|
|
129
|
-
return normalizeIp(first)
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
return socketIp
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Cross-site request, DNS rebinding, and authentication validation (#261, #369, #384).
|
|
137
|
-
*
|
|
138
|
-
* Requirements:
|
|
139
|
-
* 1. If DSH connection service is available (options.connection), requestRejection(req)
|
|
140
|
-
* is the authoritative gate for browser session authentication and trusted network access.
|
|
141
|
-
* If rejection is returned, the numeric status (e.g. 401 or 403) is returned.
|
|
142
|
-
* 2. Host header is required and must target a trusted host (loopback, LAN, .local).
|
|
143
|
-
* 3. Cross-site requests ('sec-fetch-site: cross-site' or origin mismatch) are rejected with 403.
|
|
144
|
-
* 4. Origin & Referer headers (if present) must match Host or a trusted host.
|
|
145
|
-
* 5. Network boundary validation:
|
|
146
|
-
* - Loopback callers (127.0.0.1, ::1) are allowed.
|
|
147
|
-
* - Non-loopback / LAN callers require valid connection authentication (fail-closed, return 401).
|
|
148
|
-
* - Unknown remote address (socket present but empty remoteAddress) fails closed with 403.
|
|
149
|
-
*
|
|
150
|
-
* @param {import('node:http').IncomingMessage} req
|
|
151
|
-
* @param {{ connection?: { requestRejection?: (req: any) => number | undefined } }} [options]
|
|
152
|
-
* @returns {number|undefined} Rejection HTTP status code (401, 403), or undefined if allowed.
|
|
153
|
-
*/
|
|
154
|
-
export function getRejectionStatus(req, options = {}) {
|
|
155
|
-
const connection = options?.connection
|
|
156
|
-
if (connection && typeof connection.requestRejection === 'function') {
|
|
157
|
-
try {
|
|
158
|
-
const rejection = connection.requestRejection(req)
|
|
159
|
-
if (rejection !== undefined) {
|
|
160
|
-
if (typeof rejection === 'number' && Number.isInteger(rejection) && rejection >= 400 && rejection < 600) {
|
|
161
|
-
return rejection
|
|
162
|
-
}
|
|
163
|
-
return rejection === 401 ? 401 : 403
|
|
164
|
-
}
|
|
165
|
-
} catch {
|
|
166
|
-
// Fail closed if connection service throws
|
|
167
|
-
return 403
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (!req || !req.headers) return 403
|
|
172
|
-
|
|
173
|
-
const host = req.headers.host
|
|
174
|
-
if (!host || !isTrustedHost(host)) {
|
|
175
|
-
return 403
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const site = req.headers['sec-fetch-site']
|
|
179
|
-
|
|
180
|
-
// 1. Explicit cross-site or same-site requests are rejected
|
|
181
|
-
if (site === 'cross-site' || site === 'same-site') {
|
|
182
|
-
return 403
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// 2. Validate origin against trusted hosts and host header
|
|
186
|
-
const origin = req.headers.origin
|
|
187
|
-
if (origin !== undefined) {
|
|
188
|
-
if (origin === 'null' || !origin) return 403
|
|
189
|
-
try {
|
|
190
|
-
const parsed = new URL(origin)
|
|
191
|
-
if (!isTrustedHost(parsed.hostname)) return 403
|
|
192
|
-
if (parsed.host && parsed.host !== host) return 403
|
|
193
|
-
} catch {
|
|
194
|
-
return 403
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// 3. Validate referer against trusted hosts and host header if present
|
|
199
|
-
const referer = req.headers.referer
|
|
200
|
-
if (referer !== undefined && referer) {
|
|
201
|
-
try {
|
|
202
|
-
const parsedRef = new URL(referer)
|
|
203
|
-
if (!isTrustedHost(parsedRef.hostname)) return 403
|
|
204
|
-
if (parsedRef.host && parsedRef.host !== host) return 403
|
|
205
|
-
} catch {
|
|
206
|
-
return 403
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// 4. Validate remote address and network boundary
|
|
211
|
-
const hasSocket = !!(req.socket || req.connection)
|
|
212
|
-
const remote = getClientIp(req)
|
|
213
|
-
|
|
214
|
-
if (hasSocket) {
|
|
215
|
-
if (!remote) {
|
|
216
|
-
return 403
|
|
217
|
-
}
|
|
218
|
-
if (!isLoopbackAddress(remote) && !isLanAddress(remote)) {
|
|
219
|
-
return 403
|
|
220
|
-
}
|
|
221
|
-
// Remote / LAN callers without connection authentication are rejected (#384)
|
|
222
|
-
if (!isLoopbackAddress(remote)) {
|
|
223
|
-
if (!connection) {
|
|
224
|
-
return 401
|
|
225
|
-
}
|
|
226
|
-
// If request was forwarded through a reverse proxy (x-forwarded-for),
|
|
227
|
-
// the proxy may have injected loopback credentials. Validate that the client
|
|
228
|
-
// itself supplied authentic credentials (bearer token, PIN, or client session cookie).
|
|
229
|
-
const xff = req.headers['x-forwarded-for']
|
|
230
|
-
if (xff && !hasClientCredentials(req)) {
|
|
231
|
-
return 401
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
} else {
|
|
235
|
-
// Synthetic mock request without socket
|
|
236
|
-
const xff = req.headers['x-forwarded-for']
|
|
237
|
-
if (xff) {
|
|
238
|
-
const first = xff.split(',')[0].trim()
|
|
239
|
-
if (first && !isLoopbackAddress(normalizeIp(first))) {
|
|
240
|
-
if (!connection || !hasClientCredentials(req)) {
|
|
241
|
-
return 401
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
} else if (!connection) {
|
|
245
|
-
const hostname = String(host).split(':')[0].toLowerCase()
|
|
246
|
-
if (!isLoopbackAddress(hostname) && hostname !== 'localhost') {
|
|
247
|
-
return 401
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// 5. Browser request with declared site:
|
|
253
|
-
// Must be same-origin or none
|
|
254
|
-
if (site && site !== 'same-origin' && site !== 'none') {
|
|
255
|
-
return 403
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// 6. Request without declared site from non-loopback requires connection
|
|
259
|
-
if (!site && remote && !isLoopbackAddress(remote) && !connection) {
|
|
260
|
-
return 401
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
return undefined
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
export function isTrustedRequest(req, options = {}) {
|
|
267
|
-
return getRejectionStatus(req, options) === undefined
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
export function rejectUntrustedRequest(req, res, options = {}) {
|
|
271
|
-
const status = getRejectionStatus(req, options)
|
|
272
|
-
if (status !== undefined) {
|
|
273
|
-
try {
|
|
274
|
-
res.writeHead(status, {
|
|
275
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
276
|
-
'Cache-Control': 'no-store',
|
|
277
|
-
})
|
|
278
|
-
res.end(
|
|
279
|
-
JSON.stringify({
|
|
280
|
-
error: status === 401 ? 'unauthorized' : 'cross-site',
|
|
281
|
-
})
|
|
282
|
-
)
|
|
283
|
-
} catch {
|
|
284
|
-
/* socket closed */
|
|
285
|
-
}
|
|
286
|
-
return true
|
|
287
|
-
}
|
|
288
|
-
return false
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/** Parse `/dsh-kanban/task/<id>[/<action>]`; returns undefined if non-matching. */
|
|
292
|
-
export function parseTaskPath(pathname) {
|
|
293
|
-
const m = /^\/dsh-kanban\/task\/([^/]+)(?:\/([a-z-]+))?\/?$/.exec(pathname ?? '')
|
|
294
|
-
if (m === null) return undefined
|
|
295
|
-
return { id: decodeURIComponent(m[1]), action: m[2] }
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Enrich task with parsed plan, state, and filters.
|
|
300
|
-
*
|
|
301
|
-
* Plan is pre-parsed on server because client bundle cannot import `lib/plan.js`.
|
|
302
|
-
* `liveOf` returns active agent state or undefined if session is inactive.
|
|
303
|
-
*/
|
|
304
|
-
function withPlan(task, liveOf, config, now, sameSession) {
|
|
305
|
-
const plan = parsePlan(task.plan)
|
|
306
|
-
// Registry error implies agent unavailable; do not fail whole board render.
|
|
307
|
-
let live
|
|
308
|
-
try {
|
|
309
|
-
if (typeof liveOf === 'function' && task.sessionId) live = liveOf(task.sessionId)
|
|
310
|
-
} catch { live = undefined }
|
|
311
|
-
const liveStatus = typeof live === 'object' && live !== null ? live.status : live
|
|
312
|
-
const liveStep = typeof live === 'object' && live !== null
|
|
313
|
-
? (live.step || live.activity || live.stateDetail || '')
|
|
314
|
-
: ''
|
|
315
|
-
|
|
316
|
-
const costMetrics = task.costMetrics || null
|
|
317
|
-
const costLimitExceeded = (config?.costMeterEnabled !== false && Number(config?.taskCostLimit) > 0 && costMetrics?.costUsd)
|
|
318
|
-
? costMetrics.costUsd >= Number(config.taskCostLimit)
|
|
319
|
-
: Boolean(task.costLimitExceeded)
|
|
320
|
-
|
|
321
|
-
return {
|
|
322
|
-
...task,
|
|
323
|
-
plan: { items: planItems(plan), progress: planProgress(plan) },
|
|
324
|
-
state: taskState({ task, live: liveStatus }),
|
|
325
|
-
liveStep: liveStep || task.liveStep || '',
|
|
326
|
-
costMetrics,
|
|
327
|
-
costLimitExceeded,
|
|
328
|
-
// Queue position calculated dynamically across active tasks in same session.
|
|
329
|
-
queuePos: queuePosition(task, sameSession),
|
|
330
|
-
// Label facets extracted on server for consistent client filtering.
|
|
331
|
-
facets: facetsOfTask(task),
|
|
332
|
-
// Stale calculation performed on host where configuration resides.
|
|
333
|
-
stale: isStale({
|
|
334
|
-
now,
|
|
335
|
-
updatedAt: task.updatedAt,
|
|
336
|
-
sessionId: task.sessionId,
|
|
337
|
-
afterMinutes: config?.staleAfterMin,
|
|
338
|
-
}),
|
|
339
|
-
// Overdue calculated on host using server clock.
|
|
340
|
-
overdue: typeof task.dueAt === 'number' && task.dueAt > 0 && task.dueAt < now,
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/** Order tasks: by column, grouped by repository, with active tasks on top. */
|
|
345
|
-
function orderForBoard(tasks, columns) {
|
|
346
|
-
const out = []
|
|
347
|
-
for (const column of columns) {
|
|
348
|
-
const inColumn = tasks.filter((t) => t.column === column)
|
|
349
|
-
for (const group of groupByRepo(sortByLife(inColumn))) out.push(...group.tasks)
|
|
350
|
-
}
|
|
351
|
-
// Retain tasks in columns not visible on current board to prevent data loss.
|
|
352
|
-
const seen = new Set(out.map((t) => t.id))
|
|
353
|
-
for (const task of tasks) if (!seen.has(task.id)) out.push(task)
|
|
354
|
-
return out
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* Build board response payload: columns in canonical order, tasks, and limits.
|
|
359
|
-
*/
|
|
360
|
-
export function buildBoard({ store, config, board = 'main', repo, liveOf, sync, projectRoot, costMeterAvailable = false }) {
|
|
361
|
-
// Uniform timestamp across snapshot calculation.
|
|
362
|
-
const now = Date.now()
|
|
363
|
-
// Tasks are sorted and grouped on server: repo grouping with active items first.
|
|
364
|
-
const raw = store.listTasks({ board, repo })
|
|
365
|
-
// Queue position scoped per session.
|
|
366
|
-
const bySession = new Map()
|
|
367
|
-
for (const task of raw) {
|
|
368
|
-
const id = typeof task.sessionId === 'string' ? task.sessionId : ''
|
|
369
|
-
if (id === '') continue
|
|
370
|
-
if (!bySession.has(id)) bySession.set(id, [])
|
|
371
|
-
bySession.get(id).push(task)
|
|
372
|
-
}
|
|
373
|
-
const tasks = orderForBoard(
|
|
374
|
-
raw.map((task) => {
|
|
375
|
-
const enriched = withPlan(task, liveOf, config, now, bySession.get(task.sessionId) ?? [])
|
|
376
|
-
if (task.isEpic) {
|
|
377
|
-
enriched.subtasksCount = store.countSubtasks(task.id)
|
|
378
|
-
}
|
|
379
|
-
return enriched
|
|
380
|
-
}),
|
|
381
|
-
columnsOf(store.boardKind(board)),
|
|
382
|
-
)
|
|
383
|
-
const kind = store.boardKind(board)
|
|
384
|
-
// Column custom names sent from server; locale translation handled by client.
|
|
385
|
-
const names = columnNamesOf(config)
|
|
386
|
-
const columns = columnsOf(kind).map((id) => {
|
|
387
|
-
const count = tasks.filter((t) => t.column === id).length
|
|
388
|
-
const limit = wipLimitField(config, id)
|
|
389
|
-
return { id, name: names[id], count, limit, overLimit: limit !== undefined && count > limit }
|
|
390
|
-
})
|
|
391
|
-
// Filter counts calculated over all tasks on board.
|
|
392
|
-
return {
|
|
393
|
-
board, kind, now, boards: store.listBoards(), columns, tasks,
|
|
394
|
-
// Resolved project root {path, set} computed on host.
|
|
395
|
-
projectRoot: projectRoot ?? { path: '', set: false },
|
|
396
|
-
|
|
397
|
-
facets: facetsOf(tasks),
|
|
398
|
-
// Sync telemetry state attached directly to board snapshot.
|
|
399
|
-
sync: sync ?? { state: 'never' },
|
|
400
|
-
costMeterAvailable: Boolean(costMeterAvailable),
|
|
401
|
-
remoteMode: Boolean(config?.remoteMode),
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* Card move transition. WIP limit is advisory: move executes, and exceeded status
|
|
407
|
-
* is returned for UI highlighting.
|
|
408
|
-
*/
|
|
409
|
-
export function applyMove({ store, config, id, column, beforeId, afterId, source = 'manual', detail = '' }) {
|
|
410
|
-
const before = store.getTask(id)
|
|
411
|
-
if (before === undefined) return { error: 'task-not-found', status: 404 }
|
|
412
|
-
if (column !== undefined && column !== null) {
|
|
413
|
-
const allowed = columnsOf(store.boardKind(before.board))
|
|
414
|
-
if (!allowed.includes(column)) {
|
|
415
|
-
return { error: 'invalid-column', status: 400 }
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
const task = store.moveTask(id, { column, beforeId, afterId })
|
|
419
|
-
if (task.column !== before.column) {
|
|
420
|
-
store.addTransition({
|
|
421
|
-
taskId: id, fromCol: before.column, toCol: task.column, source, detail,
|
|
422
|
-
})
|
|
423
|
-
}
|
|
424
|
-
const limit = wipLimitField(config, task.column)
|
|
425
|
-
const count = store.countInColumn(task.board, task.column)
|
|
426
|
-
return { task, overLimit: limit !== undefined && count > limit }
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
/** Create local board task (not linked to Gitea). */
|
|
430
|
-
export function createTask({ store, input }) {
|
|
431
|
-
const title = typeof input?.title === 'string' ? input.title.trim() : ''
|
|
432
|
-
if (title === '') return { error: 'title-required', status: 400 }
|
|
433
|
-
const board = input?.board ?? 'main'
|
|
434
|
-
// Validate column against board kind; simple boards do not have deploy/cleanup.
|
|
435
|
-
const allowed = columnsOf(store.boardKind(board))
|
|
436
|
-
const column = allowed.includes(input?.column) ? input.column : 'backlog'
|
|
437
|
-
const task = store.createTask({
|
|
438
|
-
board,
|
|
439
|
-
column,
|
|
440
|
-
title,
|
|
441
|
-
body: typeof input?.body === 'string' ? input.body : '',
|
|
442
|
-
labels: Array.isArray(input?.labels) ? input.labels.filter((l) => typeof l === 'string') : [],
|
|
443
|
-
})
|
|
444
|
-
return { task }
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
/** Update card fields. Column transitions are handled via applyMove. */
|
|
448
|
-
export function updateTask({ store, id, input }) {
|
|
449
|
-
const current = store.getTask(id)
|
|
450
|
-
if (current === undefined) return { error: 'task-not-found', status: 404 }
|
|
451
|
-
const patch = {}
|
|
452
|
-
if (typeof input?.title === 'string' && input.title.trim() !== '') patch.title = input.title.trim()
|
|
453
|
-
if (typeof input?.body === 'string') patch.body = input.body
|
|
454
|
-
if (Array.isArray(input?.labels)) patch.labels = input.labels.filter((l) => typeof l === 'string')
|
|
455
|
-
// Priority is local to board and not synced back to Gitea.
|
|
456
|
-
if (PRIORITIES.includes(input?.priority)) patch.priority = input.priority
|
|
457
|
-
// Deadline is local. 0 clears deadline; positive value sets it.
|
|
458
|
-
if (typeof input?.dueAt === 'number' && Number.isFinite(input.dueAt) && input.dueAt >= 0) {
|
|
459
|
-
patch.dueAt = Math.floor(input.dueAt)
|
|
460
|
-
}
|
|
461
|
-
if (typeof input?.cron === 'string') {
|
|
462
|
-
patch.cron = input.cron.trim()
|
|
463
|
-
patch.nextRunAt = patch.cron ? (computeNextRun(patch.cron) || 0) : 0
|
|
464
|
-
}
|
|
465
|
-
if (Array.isArray(input?.checklist)) patch.checklist = input.checklist
|
|
466
|
-
if (input?.report && typeof input.report === 'object') patch.report = input.report
|
|
467
|
-
if (Array.isArray(input?.comments)) patch.comments = input.comments
|
|
468
|
-
if (typeof input?.rejectReason === 'string') patch.rejectReason = input.rejectReason
|
|
469
|
-
if (typeof input?.progressDump === 'string') {
|
|
470
|
-
patch.progressDump = input.progressDump
|
|
471
|
-
}
|
|
472
|
-
if (typeof input?.permission === 'string') {
|
|
473
|
-
patch.permission = input.permission
|
|
474
|
-
if (input.permission !== current.permission) {
|
|
475
|
-
patch.permissionConfirmedAt = 0
|
|
476
|
-
patch.permissionConfirmedTier = ''
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
if (typeof input?.reasoningEffort === 'string') {
|
|
481
|
-
const val = input.reasoningEffort.trim().toLowerCase()
|
|
482
|
-
if (['', 'low', 'medium', 'high', 'default', 'none', 'minimal', 'thorough'].includes(val)) {
|
|
483
|
-
patch.reasoningEffort = val
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
if (typeof input?.forge === 'string') {
|
|
487
|
-
const val = input.forge.trim().toLowerCase()
|
|
488
|
-
if (val === 'github' || val === 'gitea') patch.forge = val
|
|
489
|
-
}
|
|
490
|
-
return { task: store.updateTask(id, patch) }
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
export function confirmPermission({ store, id, tier }) {
|
|
494
|
-
const task = store.getTask(id)
|
|
495
|
-
if (task === undefined) return { error: 'task-not-found', status: 404 }
|
|
496
|
-
const effectiveTier = typeof tier === 'string' && tier ? tier : (task.permission || 'default')
|
|
497
|
-
return { task: store.updateTask(id, { permissionConfirmedAt: Date.now(), permissionConfirmedTier: effectiveTier }) }
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
/** Known priority values. Empty string indicates unprioritized. */
|
|
501
|
-
const PRIORITIES = ['', 'high', 'medium', 'low']
|
|
502
|
-
|
|
503
|
-
/**
|
|
504
|
-
* Append note to task body.
|
|
505
|
-
* Handled on server to prevent overwriting edits made elsewhere.
|
|
506
|
-
*/
|
|
507
|
-
export function appendNote({ store, id, input }) {
|
|
508
|
-
const task = store.getTask(id)
|
|
509
|
-
if (task === undefined) return { error: 'task-not-found', status: 404 }
|
|
510
|
-
const note = typeof input?.text === 'string' ? input.text.trim() : ''
|
|
511
|
-
if (note === '') return { error: 'note-required', status: 400 }
|
|
512
|
-
const body = typeof task.body === 'string' ? task.body : ''
|
|
513
|
-
return { task: store.updateTask(id, { body: body === '' ? note : `${body}\n\n${note}` }) }
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
/**
|
|
517
|
-
* Archive or unarchive card.
|
|
518
|
-
* Unarchiving restores card to its original column and position.
|
|
519
|
-
*/
|
|
520
|
-
export function setArchived({ store, id, archived, now = Date.now() }) {
|
|
521
|
-
const task = store.getTask(id)
|
|
522
|
-
if (task === undefined) return { error: 'task-not-found', status: 404 }
|
|
523
|
-
return { task: store.setArchived(id, archived ? now : 0) }
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
/** Query archived tasks list. */
|
|
527
|
-
export function listArchive({ store }) {
|
|
528
|
-
return { tasks: store.listArchived() }
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
/**
|
|
532
|
-
* Assign task or clear assignment.
|
|
533
|
-
* Empty login unassigns the task.
|
|
534
|
-
*
|
|
535
|
-
* @returns {{task: object}|{error: string, status: number}}
|
|
536
|
-
*/
|
|
537
|
-
export function assignTask({ store, id, login }) {
|
|
538
|
-
const task = store.getTask(id)
|
|
539
|
-
if (task === undefined) return { error: 'task-not-found', status: 404 }
|
|
540
|
-
const who = typeof login === 'string' ? login.trim() : ''
|
|
541
|
-
if (who === (task.assignee ?? '')) return { task }
|
|
542
|
-
store.updateTask(id, { assignee: who })
|
|
543
|
-
store.addTransition({
|
|
544
|
-
taskId: id,
|
|
545
|
-
fromCol: task.column,
|
|
546
|
-
toCol: task.column,
|
|
547
|
-
source: 'manual',
|
|
548
|
-
detail: who === '' ? 'unassigned' : `assigned: ${who}`,
|
|
549
|
-
})
|
|
550
|
-
return { task: store.getTask(id) }
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
export function deleteTask({ store, id }) {
|
|
554
|
-
if (store.getTask(id) === undefined) return { error: 'task-not-found', status: 404 }
|
|
555
|
-
store.deleteTask(id)
|
|
556
|
-
return { ok: true }
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
/**
|
|
560
|
-
* Parse task markdown body into structured block elements.
|
|
561
|
-
* Done on server so client bundle does not need markdown parser.
|
|
562
|
-
*/
|
|
563
|
-
/**
|
|
564
|
-
* Board metrics: WIP distribution and stale tasks.
|
|
565
|
-
*
|
|
566
|
-
* @returns {{columns: object[], done: object, stale: object[], tasks: number}}
|
|
567
|
-
*/
|
|
568
|
-
export function boardMetrics({ store, board = 'main', now = Date.now(), staleDays = 14 }) {
|
|
569
|
-
const tasks = store.listTasks({ board })
|
|
570
|
-
const byTask = new Map()
|
|
571
|
-
for (const row of store.listAllTransitions()) {
|
|
572
|
-
if (!byTask.has(row.taskId)) byTask.set(row.taskId, [])
|
|
573
|
-
byTask.get(row.taskId).push(row)
|
|
574
|
-
}
|
|
575
|
-
const rows = tasks.map((task) => ({ task, moves: byTask.get(task.id) ?? [] }))
|
|
576
|
-
const day = 24 * 60 * 60 * 1000
|
|
577
|
-
|
|
578
|
-
return {
|
|
579
|
-
tasks: tasks.length,
|
|
580
|
-
columns: columnStats(rows, now),
|
|
581
|
-
done: {
|
|
582
|
-
week: doneWithin(rows, now - 7 * day, now),
|
|
583
|
-
month: doneWithin(rows, now - 30 * day, now),
|
|
584
|
-
},
|
|
585
|
-
staleDays,
|
|
586
|
-
stale: stale(tasks, now, staleDays * day).slice(0, 50),
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
/**
|
|
591
|
-
* Check whether a task can be resumed.
|
|
592
|
-
* Eligible when task has recorded sessionId but no active agent is running.
|
|
593
|
-
*/
|
|
594
|
-
export function canRevive(task, live) {
|
|
595
|
-
const has = typeof task?.sessionId === 'string' && task.sessionId !== ''
|
|
596
|
-
return has && (live === undefined || live === null)
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
export function taskBody({ store, id }) {
|
|
600
|
-
const task = store.getTask(id)
|
|
601
|
-
if (task === undefined) return { error: 'task-not-found', status: 404 }
|
|
602
|
-
return { blocks: parseBody(task.body) }
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
export function taskLog({ store, id }) {
|
|
606
|
-
if (store.getTask(id) === undefined) return { error: 'task-not-found', status: 404 }
|
|
607
|
-
return { transitions: store.listTransitions(id) }
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
/** Find task associated with session; absence is normal response. */
|
|
611
|
-
export function taskBySession({ store, sessionId, agents }) {
|
|
612
|
-
const task = store.findTaskBySession(sessionId)
|
|
613
|
-
if (task === undefined) return { task: null }
|
|
614
|
-
let live
|
|
615
|
-
try { live = agents?.get?.(sessionId) } catch { live = undefined }
|
|
616
|
-
// Board kind returned with task so UI displays matching column choices.
|
|
617
|
-
return {
|
|
618
|
-
task,
|
|
619
|
-
columns: columnsOf(store.boardKind(task.board)),
|
|
620
|
-
canRevive: canRevive(task, live),
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
/**
|
|
625
|
-
* Export full board snapshot for backup or migration.
|
|
626
|
-
*/
|
|
627
|
-
export function exportBoard({ store }) {
|
|
628
|
-
return store.exportAll()
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
/**
|
|
632
|
-
* Restore board from backup snapshot (idempotent; skips existing IDs).
|
|
633
|
-
*
|
|
634
|
-
* @returns {{imported: number, skipped: number, error?: string}}
|
|
635
|
-
*/
|
|
636
|
-
export function importBoard({ store, input }) {
|
|
637
|
-
let backupMeta = undefined
|
|
638
|
-
if (typeof store.backupDatabase === 'function') {
|
|
639
|
-
try {
|
|
640
|
-
backupMeta = store.backupDatabase()
|
|
641
|
-
} catch { /* backup best effort */ }
|
|
642
|
-
}
|
|
643
|
-
if (input === null || typeof input !== 'object') return { error: 'bad-import', status: 400 }
|
|
644
|
-
if (input.version !== 1) return { error: 'bad-import-version', status: 400 }
|
|
645
|
-
if (!Array.isArray(input.tasks)) return { error: 'bad-import', status: 400 }
|
|
646
|
-
|
|
647
|
-
let imported = 0
|
|
648
|
-
let skipped = 0
|
|
649
|
-
|
|
650
|
-
const doImport = () => {
|
|
651
|
-
for (const board of Array.isArray(input.boards) ? input.boards : []) {
|
|
652
|
-
if (typeof board?.id !== 'string' || board.id === '') continue
|
|
653
|
-
store.createBoard({
|
|
654
|
-
id: board.id,
|
|
655
|
-
title: board.title,
|
|
656
|
-
kind: board.kind,
|
|
657
|
-
position: board.position,
|
|
658
|
-
createdAt: board.createdAt,
|
|
659
|
-
})
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
for (const task of input.tasks) {
|
|
663
|
-
if (typeof task?.id !== 'string' || task.id === '') continue
|
|
664
|
-
if (store.getTask(task.id) !== undefined) { skipped += 1; continue }
|
|
665
|
-
if (typeof store.restoreTask === 'function') {
|
|
666
|
-
store.restoreTask(task)
|
|
667
|
-
} else {
|
|
668
|
-
store.createTask(task)
|
|
669
|
-
}
|
|
670
|
-
imported += 1
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
for (const trans of Array.isArray(input.transitions) ? input.transitions : []) {
|
|
674
|
-
if (typeof trans?.taskId !== 'string' || trans.taskId === '') continue
|
|
675
|
-
if (typeof store.restoreTransition === 'function') {
|
|
676
|
-
store.restoreTransition(trans)
|
|
677
|
-
} else if (typeof store.addTransition === 'function') {
|
|
678
|
-
store.addTransition(trans)
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
if (typeof store.withTransaction === 'function') {
|
|
684
|
-
store.withTransaction(doImport)
|
|
685
|
-
} else {
|
|
686
|
-
doImport()
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
return { imported, skipped, backup: backupMeta?.backup }
|
|
690
|
-
}
|
|
691
|
-
|
|
692
1
|
/**
|
|
693
|
-
*
|
|
694
|
-
*
|
|
695
|
-
*
|
|
696
|
-
*
|
|
697
|
-
*/
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
/**
|
|
767
|
-
* Clean up task worktree.
|
|
768
|
-
*/
|
|
769
|
-
export async function handleCleanupWorktree({
|
|
770
|
-
store, task, workspaceRegistry, force, cleanupTaskWorktree,
|
|
771
|
-
config, repoDir, cwdOf, env, gitRunner,
|
|
772
|
-
}) {
|
|
773
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
774
|
-
const out = await cleanupTaskWorktree({
|
|
775
|
-
store,
|
|
776
|
-
task,
|
|
777
|
-
workspaceRegistry,
|
|
778
|
-
force: force === true,
|
|
779
|
-
config,
|
|
780
|
-
repoDir,
|
|
781
|
-
cwdOf,
|
|
782
|
-
env,
|
|
783
|
-
gitRunner,
|
|
784
|
-
})
|
|
785
|
-
const status = out.status || (out.error ? (out.error === 'unfenced-worktree-path' ? 403 : 500) : 200)
|
|
786
|
-
return { status, body: out }
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
/**
|
|
790
|
-
* Get working copy diff for task.
|
|
791
|
-
*/
|
|
792
|
-
export async function handleTaskDiff({ task, getTaskDiff, getTaskDiffInspector }) {
|
|
793
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
794
|
-
if (typeof getTaskDiffInspector === 'function') {
|
|
795
|
-
const inspectorResult = await getTaskDiffInspector({ worktreePath: task.worktree })
|
|
796
|
-
return { status: 200, body: inspectorResult }
|
|
797
|
-
}
|
|
798
|
-
const diffResult = await getTaskDiff({ worktreePath: task.worktree })
|
|
799
|
-
return { status: 200, body: diffResult }
|
|
800
|
-
}
|
|
801
|
-
|
|
802
|
-
/**
|
|
803
|
-
* Discard file changes in task worktree (#371).
|
|
804
|
-
*/
|
|
805
|
-
export async function handleTaskDiscard({ task, file, discardTaskFile, gitRunner }) {
|
|
806
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
807
|
-
if (!file || typeof file !== 'string') return { status: 400, body: { error: 'file-required' } }
|
|
808
|
-
if (!task.worktree) return { status: 400, body: { error: 'no-worktree' } }
|
|
809
|
-
const result = await discardTaskFile({ worktreePath: task.worktree, file, gitRunner })
|
|
810
|
-
return { status: result.success ? 200 : 400, body: result }
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
/**
|
|
814
|
-
* Get commit list for task.
|
|
815
|
-
*/
|
|
816
|
-
export async function handleTaskCommits({ task, getTaskCommits, gitRunner }) {
|
|
817
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
818
|
-
const commits = await getTaskCommits({ worktreePath: task.worktree, gitRunner })
|
|
819
|
-
return { status: 200, body: { commits } }
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
/**
|
|
823
|
-
* Merge task worktree branch and transition to done.
|
|
824
|
-
*/
|
|
825
|
-
export async function handleTaskMerge({ store, task, targetBranch, workspaceRegistry, mergeTaskWorktree, gitRunner }) {
|
|
826
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
827
|
-
const mergeResult = await mergeTaskWorktree({
|
|
828
|
-
task,
|
|
829
|
-
targetBranch: typeof targetBranch === 'string' ? targetBranch.trim() : undefined,
|
|
830
|
-
workspaceRegistry,
|
|
831
|
-
gitRunner,
|
|
832
|
-
})
|
|
833
|
-
if (!mergeResult.success) {
|
|
834
|
-
return {
|
|
835
|
-
status: 400,
|
|
836
|
-
body: { error: mergeResult.error, detail: mergeResult.detail, files: mergeResult.files },
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
const updated = store.acceptTask(task.id)
|
|
840
|
-
store.addTransition({
|
|
841
|
-
taskId: task.id,
|
|
842
|
-
fromCol: task.column,
|
|
843
|
-
toCol: 'done',
|
|
844
|
-
source: 'manual',
|
|
845
|
-
detail: `accepted and merged branch ${task.branch} into ${mergeResult.targetBranch}`,
|
|
846
|
-
})
|
|
847
|
-
return { status: 200, body: { task: updated, merged: true, targetBranch: mergeResult.targetBranch } }
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
/**
|
|
851
|
-
* Check task worktree status (branch, dirty files, workspace).
|
|
852
|
-
*/
|
|
853
|
-
export async function handleTaskWorktreeStatus({ task, checkWorktreeDirty }) {
|
|
854
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
855
|
-
const dirtyCheck = await checkWorktreeDirty({ worktreePath: task.worktree })
|
|
856
|
-
return {
|
|
857
|
-
status: 200,
|
|
858
|
-
body: {
|
|
859
|
-
worktree: task.worktree || '',
|
|
860
|
-
branch: task.branch || '',
|
|
861
|
-
workspaceId: task.workspaceId || '',
|
|
862
|
-
dirty: dirtyCheck.dirty,
|
|
863
|
-
files: dirtyCheck.files,
|
|
864
|
-
},
|
|
865
|
-
}
|
|
866
|
-
}
|
|
867
|
-
|
|
868
|
-
/**
|
|
869
|
-
* Update Definition of Done checklist for task.
|
|
870
|
-
*/
|
|
871
|
-
export function handleTaskChecklist({ store, task, input }) {
|
|
872
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
873
|
-
const list = Array.isArray(input?.checklist) ? input.checklist : Array.isArray(input) ? input : []
|
|
874
|
-
const updated = store.updateChecklist(task.id, list)
|
|
875
|
-
return { status: 200, body: { task: updated } }
|
|
876
|
-
}
|
|
877
|
-
|
|
878
|
-
/**
|
|
879
|
-
* Set completion report for task.
|
|
880
|
-
*/
|
|
881
|
-
export function handleTaskReport({ store, task, input }) {
|
|
882
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
883
|
-
const updated = store.setReport(task.id, input)
|
|
884
|
-
return { status: 200, body: { task: updated } }
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
/**
|
|
888
|
-
* Get comments for task.
|
|
889
|
-
*/
|
|
890
|
-
export function handleGetTaskComments({ task }) {
|
|
891
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
892
|
-
return { status: 200, body: { comments: task.comments || [] } }
|
|
893
|
-
}
|
|
894
|
-
|
|
895
|
-
/**
|
|
896
|
-
* Add comment to task.
|
|
897
|
-
*/
|
|
898
|
-
export function handleAddTaskComment({ store, task, input }) {
|
|
899
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
900
|
-
const author = input?.author || 'user'
|
|
901
|
-
const role = input?.role || 'user'
|
|
902
|
-
const text = input?.text || ''
|
|
903
|
-
const updated = store.addComment(task.id, { author, role, text })
|
|
904
|
-
return { status: 200, body: { task: updated } }
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
/**
|
|
908
|
-
* Accept task review (transition to done).
|
|
909
|
-
*/
|
|
910
|
-
export function handleAcceptTask({ store, task }) {
|
|
911
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
912
|
-
const updated = store.acceptTask(task.id)
|
|
913
|
-
return { status: 200, body: { task: updated } }
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
/**
|
|
917
|
-
* Reject task review (return to in_progress).
|
|
918
|
-
*/
|
|
919
|
-
export function handleRejectTask({ store, task, input }) {
|
|
920
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
921
|
-
const reason = String(input?.reason || '').trim()
|
|
922
|
-
if (!reason) return { status: 400, body: { error: 'reason-required' } }
|
|
923
|
-
const updated = store.rejectTask(task.id, reason)
|
|
924
|
-
return { status: 200, body: { task: updated } }
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
/**
|
|
928
|
-
* Save task progress dump snapshot.
|
|
929
|
-
*/
|
|
930
|
-
export function handleProgressDump({ store, task, input, formatProgressDump }) {
|
|
931
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
932
|
-
const formatted = typeof input === 'string' ? input : formatProgressDump(input)
|
|
933
|
-
const updated = store.updateTask(task.id, { progressDump: formatted })
|
|
934
|
-
return { status: 200, body: { task: updated } }
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
/**
|
|
938
|
-
* Decompose a task into subtasks and make the parent an Epic.
|
|
939
|
-
*/
|
|
940
|
-
export async function handleTriageTask({ store, gitea, task, input }) {
|
|
941
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
942
|
-
const subtasksInput = Array.isArray(input?.subtasks) ? input.subtasks : []
|
|
943
|
-
if (subtasksInput.length === 0) {
|
|
944
|
-
return { status: 400, body: { error: 'subtasks-required' } }
|
|
945
|
-
}
|
|
946
|
-
const createGitea = input?.createGiteaIssues === true && Boolean(task.owner && task.repo && gitea)
|
|
947
|
-
|
|
948
|
-
// Convert parent into Epic
|
|
949
|
-
store.updateTask(task.id, { isEpic: true })
|
|
950
|
-
|
|
951
|
-
const created = []
|
|
952
|
-
for (const item of subtasksInput) {
|
|
953
|
-
const title = String(item?.title ?? '').trim()
|
|
954
|
-
if (!title) continue
|
|
955
|
-
const body = String(item?.body ?? '').trim()
|
|
956
|
-
let issueNumber = null
|
|
957
|
-
let issueUrl = null
|
|
958
|
-
|
|
959
|
-
if (createGitea) {
|
|
960
|
-
try {
|
|
961
|
-
const issueBody = body ? `${body}\n\nParent epic: #${task.issueNumber || task.id}` : `Parent epic: #${task.issueNumber || task.id}`
|
|
962
|
-
const giteaIssue = await gitea.createIssue({
|
|
963
|
-
owner: task.owner,
|
|
964
|
-
repo: task.repo,
|
|
965
|
-
title,
|
|
966
|
-
body: issueBody,
|
|
967
|
-
})
|
|
968
|
-
if (giteaIssue?.number) {
|
|
969
|
-
issueNumber = giteaIssue.number
|
|
970
|
-
issueUrl = giteaIssue.html_url || null
|
|
971
|
-
}
|
|
972
|
-
} catch { /* ignore gitea subtask creation failure */ }
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
const sub = store.createTask({
|
|
976
|
-
board: task.board || 'main',
|
|
977
|
-
column: 'backlog',
|
|
978
|
-
title,
|
|
979
|
-
body,
|
|
980
|
-
parentId: task.id,
|
|
981
|
-
owner: task.owner || null,
|
|
982
|
-
repo: task.repo || null,
|
|
983
|
-
issueNumber,
|
|
984
|
-
issueUrl,
|
|
985
|
-
labels: Array.isArray(item?.labels) ? item.labels : [],
|
|
986
|
-
workspaceId: task.workspaceId || '',
|
|
987
|
-
})
|
|
988
|
-
created.push(sub)
|
|
989
|
-
}
|
|
990
|
-
|
|
991
|
-
store.addTransition({
|
|
992
|
-
taskId: task.id,
|
|
993
|
-
fromCol: task.column,
|
|
994
|
-
toCol: task.column,
|
|
995
|
-
source: 'manual',
|
|
996
|
-
detail: `decomposed into ${created.length} subtasks (epic triage)`,
|
|
997
|
-
})
|
|
998
|
-
|
|
999
|
-
return {
|
|
1000
|
-
status: 200,
|
|
1001
|
-
body: {
|
|
1002
|
-
epic: store.getTask(task.id),
|
|
1003
|
-
subtasks: store.listSubtasks(task.id),
|
|
1004
|
-
counts: store.countSubtasks(task.id),
|
|
1005
|
-
createdCount: created.length,
|
|
1006
|
-
},
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
/**
|
|
1011
|
-
* Get subtasks of a task.
|
|
1012
|
-
*/
|
|
1013
|
-
export function handleGetSubtasks({ store, task }) {
|
|
1014
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
1015
|
-
return {
|
|
1016
|
-
status: 200,
|
|
1017
|
-
body: {
|
|
1018
|
-
subtasks: store.listSubtasks(task.id),
|
|
1019
|
-
counts: store.countSubtasks(task.id),
|
|
1020
|
-
},
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
|
|
1024
|
-
/**
|
|
1025
|
-
* Create a single subtask linked to a parent task.
|
|
1026
|
-
*/
|
|
1027
|
-
export async function handleCreateSubtask({ store, gitea, task, input }) {
|
|
1028
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
1029
|
-
const title = String(input?.title ?? '').trim()
|
|
1030
|
-
if (!title) return { status: 400, body: { error: 'title-required' } }
|
|
1031
|
-
const body = String(input?.body ?? '').trim()
|
|
1032
|
-
let issueNumber = null
|
|
1033
|
-
let issueUrl = null
|
|
1034
|
-
|
|
1035
|
-
if (input?.createGiteaIssue === true && task.owner && task.repo && gitea) {
|
|
1036
|
-
try {
|
|
1037
|
-
const issueBody = body ? `${body}\n\nParent epic: #${task.issueNumber || task.id}` : `Parent epic: #${task.issueNumber || task.id}`
|
|
1038
|
-
const giteaIssue = await gitea.createIssue({
|
|
1039
|
-
owner: task.owner,
|
|
1040
|
-
repo: task.repo,
|
|
1041
|
-
title,
|
|
1042
|
-
body: issueBody,
|
|
1043
|
-
})
|
|
1044
|
-
if (giteaIssue?.number) {
|
|
1045
|
-
issueNumber = giteaIssue.number
|
|
1046
|
-
issueUrl = giteaIssue.html_url || null
|
|
1047
|
-
}
|
|
1048
|
-
} catch { /* ignore gitea subtask creation failure */ }
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
|
-
store.updateTask(task.id, { isEpic: true })
|
|
1052
|
-
const sub = store.createTask({
|
|
1053
|
-
board: task.board || 'main',
|
|
1054
|
-
column: input?.column || 'backlog',
|
|
1055
|
-
title,
|
|
1056
|
-
body,
|
|
1057
|
-
parentId: task.id,
|
|
1058
|
-
owner: task.owner || null,
|
|
1059
|
-
repo: task.repo || null,
|
|
1060
|
-
issueNumber,
|
|
1061
|
-
issueUrl,
|
|
1062
|
-
labels: Array.isArray(input?.labels) ? input.labels : [],
|
|
1063
|
-
workspaceId: task.workspaceId || '',
|
|
1064
|
-
})
|
|
1065
|
-
return { status: 200, body: { subtask: sub, counts: store.countSubtasks(task.id) } }
|
|
1066
|
-
}
|
|
1067
|
-
|
|
1068
|
-
/**
|
|
1069
|
-
* Toggle a checklist item atomically by index or text.
|
|
1070
|
-
*/
|
|
1071
|
-
export function handleToggleChecklistItem({ store, task, input }) {
|
|
1072
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
1073
|
-
const current = Array.isArray(task.checklist) ? [...task.checklist] : []
|
|
1074
|
-
let targetIndex = -1
|
|
1075
|
-
|
|
1076
|
-
if (typeof input?.index === 'number' && input.index >= 0 && input.index < current.length) {
|
|
1077
|
-
targetIndex = input.index
|
|
1078
|
-
} else if (typeof input?.text === 'string' && input.text.trim()) {
|
|
1079
|
-
const q = input.text.trim().toLowerCase()
|
|
1080
|
-
targetIndex = current.findIndex((item) => {
|
|
1081
|
-
const t = typeof item === 'string' ? item : item?.text
|
|
1082
|
-
return typeof t === 'string' && t.toLowerCase().includes(q)
|
|
1083
|
-
})
|
|
1084
|
-
}
|
|
1085
|
-
|
|
1086
|
-
if (targetIndex === -1) {
|
|
1087
|
-
return { status: 404, body: { error: 'checklist-item-not-found' } }
|
|
1088
|
-
}
|
|
1089
|
-
|
|
1090
|
-
const item = current[targetIndex]
|
|
1091
|
-
let updatedItem
|
|
1092
|
-
if (typeof item === 'string') {
|
|
1093
|
-
const checked = input?.checked !== undefined ? Boolean(input.checked) : true
|
|
1094
|
-
updatedItem = { text: item, completed: checked, done: checked }
|
|
1095
|
-
} else {
|
|
1096
|
-
const isCurrentlyDone = item.completed !== undefined ? Boolean(item.completed) : Boolean(item.done)
|
|
1097
|
-
const checked = input?.checked !== undefined ? Boolean(input.checked) : !isCurrentlyDone
|
|
1098
|
-
updatedItem = { ...item, completed: checked, done: checked }
|
|
1099
|
-
}
|
|
1100
|
-
current[targetIndex] = updatedItem
|
|
1101
|
-
const updated = store.updateChecklist(task.id, current)
|
|
1102
|
-
return { status: 200, body: { task: updated, item: updatedItem, index: targetIndex } }
|
|
1103
|
-
}
|
|
1104
|
-
|
|
1105
|
-
/**
|
|
1106
|
-
* Create a Gitea Pull Request for a task.
|
|
1107
|
-
*/
|
|
1108
|
-
export async function handleCreateTaskPr({ store, gitea, task, input }) {
|
|
1109
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
1110
|
-
if (!task.owner || !task.repo) {
|
|
1111
|
-
return { status: 400, body: { error: 'task-no-repo' } }
|
|
1112
|
-
}
|
|
1113
|
-
const head = String(input?.head || task.branch || '').trim()
|
|
1114
|
-
if (!head) return { status: 400, body: { error: 'branch-required' } }
|
|
1115
|
-
const base = String(input?.base || 'main').trim()
|
|
1116
|
-
const title = String(input?.title || task.title || `PR: ${task.title}`).trim()
|
|
1117
|
-
const body = String(input?.body || (task.issueNumber ? `Closes #${task.issueNumber}` : '')).trim()
|
|
1118
|
-
|
|
1119
|
-
try {
|
|
1120
|
-
const pr = await gitea.createPullRequest({
|
|
1121
|
-
owner: task.owner,
|
|
1122
|
-
repo: task.repo,
|
|
1123
|
-
title,
|
|
1124
|
-
body,
|
|
1125
|
-
head,
|
|
1126
|
-
base,
|
|
1127
|
-
})
|
|
1128
|
-
store.addTransition({
|
|
1129
|
-
taskId: task.id,
|
|
1130
|
-
fromCol: task.column,
|
|
1131
|
-
toCol: task.column,
|
|
1132
|
-
source: 'manual',
|
|
1133
|
-
detail: `Created PR #${pr?.number || ''}: ${head} -> ${base}`,
|
|
1134
|
-
})
|
|
1135
|
-
return { status: 200, body: { pr, prUrl: pr?.html_url } }
|
|
1136
|
-
} catch (err) {
|
|
1137
|
-
return { status: 502, body: { error: 'gitea-pr-failed', detail: err?.message } }
|
|
1138
|
-
}
|
|
1139
|
-
}
|
|
1140
|
-
/**
|
|
1141
|
-
* Handle interactive live steering (nudge) for an active task agent (#373).
|
|
1142
|
-
*
|
|
1143
|
-
* @param {{
|
|
1144
|
-
* store: object,
|
|
1145
|
-
* id: string,
|
|
1146
|
-
* prompt: string,
|
|
1147
|
-
* agents?: object,
|
|
1148
|
-
* createMessage?: Function,
|
|
1149
|
-
* eventHub?: object,
|
|
1150
|
-
* now?: number
|
|
1151
|
-
* }} options
|
|
1152
|
-
* @returns {Promise<{ status: number, body: object }>}
|
|
1153
|
-
*/
|
|
1154
|
-
export async function handleTaskNudge({ store, id, prompt, agents, createMessage, eventHub, now = Date.now() }) {
|
|
1155
|
-
if (!store || !id) return { status: 400, body: { error: 'invalid-parameters' } }
|
|
1156
|
-
const task = store.getTask(id)
|
|
1157
|
-
if (!task) return { status: 404, body: { error: 'task-not-found' } }
|
|
1158
|
-
|
|
1159
|
-
const text = typeof prompt === 'string' ? prompt.trim() : ''
|
|
1160
|
-
if (!text) return { status: 400, body: { error: 'prompt-required' } }
|
|
1161
|
-
|
|
1162
|
-
const sessionId = typeof task.sessionId === 'string' ? task.sessionId : ''
|
|
1163
|
-
if (!sessionId) return { status: 409, body: { error: 'no-session' } }
|
|
1164
|
-
|
|
1165
|
-
let agent
|
|
1166
|
-
try { agent = agents?.get?.(sessionId) } catch { agent = undefined }
|
|
1167
|
-
if (!agent || typeof agent.followup !== 'function') {
|
|
1168
|
-
return { status: 409, body: { error: 'session-not-live' } }
|
|
1169
|
-
}
|
|
1170
|
-
|
|
1171
|
-
const msg = typeof createMessage === 'function'
|
|
1172
|
-
? createMessage({
|
|
1173
|
-
content: [{ type: 'text', text }],
|
|
1174
|
-
source: { kind: 'dsh-kanban', plugin: 'dsh-kanban', form: 'task-nudge' },
|
|
1175
|
-
})
|
|
1176
|
-
: { content: [{ type: 'text', text }] }
|
|
1177
|
-
|
|
1178
|
-
agent.followup(msg)
|
|
1179
|
-
|
|
1180
|
-
// Append user steering comment to task
|
|
1181
|
-
if (typeof store.addComment === 'function') {
|
|
1182
|
-
store.addComment(id, {
|
|
1183
|
-
author: 'user',
|
|
1184
|
-
role: 'user',
|
|
1185
|
-
text: `[Steering Nudge] ${text}`,
|
|
1186
|
-
})
|
|
1187
|
-
} else if (typeof store.addTaskComment === 'function') {
|
|
1188
|
-
store.addTaskComment(id, {
|
|
1189
|
-
author: 'user',
|
|
1190
|
-
role: 'user',
|
|
1191
|
-
text: `[Steering Nudge] ${text}`,
|
|
1192
|
-
})
|
|
1193
|
-
}
|
|
1194
|
-
|
|
1195
|
-
// Broadcast change via eventHub
|
|
1196
|
-
if (eventHub && typeof eventHub.broadcast === 'function') {
|
|
1197
|
-
eventHub.broadcast('task:change', { id, col: task.col || task.column })
|
|
1198
|
-
}
|
|
1199
|
-
|
|
1200
|
-
return {
|
|
1201
|
-
status: 200,
|
|
1202
|
-
body: {
|
|
1203
|
-
success: true,
|
|
1204
|
-
id,
|
|
1205
|
-
sessionId,
|
|
1206
|
-
prompt: text,
|
|
1207
|
-
nudgedAt: now,
|
|
1208
|
-
},
|
|
1209
|
-
}
|
|
1210
|
-
}
|
|
2
|
+
* Unified routes facade (#389).
|
|
3
|
+
* Decomposed into domain modules under lib/routes/ (security, board, task, task-worktree, task-actions).
|
|
4
|
+
* Re-exports all functions to maintain 100% backward compatibility with existing tests and imports.
|
|
5
|
+
* Zero Cyrillic in lib/ according to Issue #366.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
MAX_BODY_BYTES,
|
|
10
|
+
isLoopbackAddress,
|
|
11
|
+
isLanAddress,
|
|
12
|
+
isTrustedHost,
|
|
13
|
+
normalizeIp,
|
|
14
|
+
cookieNameForAuthority,
|
|
15
|
+
isLoopbackCookieName,
|
|
16
|
+
hasClientCredentials,
|
|
17
|
+
getClientIp,
|
|
18
|
+
getRejectionStatus,
|
|
19
|
+
isTrustedRequest,
|
|
20
|
+
rejectUntrustedRequest,
|
|
21
|
+
} from './routes/security.js'
|
|
22
|
+
|
|
23
|
+
export {
|
|
24
|
+
withPlan,
|
|
25
|
+
buildBoard,
|
|
26
|
+
boardMetrics,
|
|
27
|
+
exportBoard,
|
|
28
|
+
importBoard,
|
|
29
|
+
exportTasksCsv,
|
|
30
|
+
} from './routes/board.js'
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
parseTaskPath,
|
|
34
|
+
applyMove,
|
|
35
|
+
createTask,
|
|
36
|
+
updateTask,
|
|
37
|
+
confirmPermission,
|
|
38
|
+
appendNote,
|
|
39
|
+
setArchived,
|
|
40
|
+
listArchive,
|
|
41
|
+
assignTask,
|
|
42
|
+
deleteTask,
|
|
43
|
+
canRevive,
|
|
44
|
+
taskBody,
|
|
45
|
+
taskLog,
|
|
46
|
+
taskBySession,
|
|
47
|
+
} from './routes/task.js'
|
|
48
|
+
|
|
49
|
+
export {
|
|
50
|
+
handleCleanupWorktree,
|
|
51
|
+
handleTaskDiff,
|
|
52
|
+
handleTaskDiscard,
|
|
53
|
+
handleTaskCommits,
|
|
54
|
+
handleTaskMerge,
|
|
55
|
+
handleTaskWorktreeStatus,
|
|
56
|
+
} from './routes/task-worktree.js'
|
|
57
|
+
|
|
58
|
+
export {
|
|
59
|
+
handleTaskChecklist,
|
|
60
|
+
handleTaskReport,
|
|
61
|
+
handleGetTaskComments,
|
|
62
|
+
handleAddTaskComment,
|
|
63
|
+
handleAcceptTask,
|
|
64
|
+
handleRejectTask,
|
|
65
|
+
handleProgressDump,
|
|
66
|
+
handleTriageTask,
|
|
67
|
+
handleGetSubtasks,
|
|
68
|
+
handleCreateSubtask,
|
|
69
|
+
handleToggleChecklistItem,
|
|
70
|
+
handleCreateTaskPr,
|
|
71
|
+
handleTaskNudge,
|
|
72
|
+
} from './routes/task-actions.js'
|