Skip to content

Commit 5dcd29e

Browse files
committed
Guard taskCounters against a non-numeric updatedAt
workerSeconds added (task.updatedAt - lease.takenAt) while guarding only the lease, so a stored card whose updatedAt was missing or non-numeric made the counter NaN, and JSON handed every reader null instead of a number. Guard the task operand the way the sibling readers of the same card fields do (room-context.js, isoStamp, project-board.js). Red first: core/test/task-counters-updated-at.test.js seeds an accepted card with a lease and updatedAt 'not-a-number' and failed with workerSeconds = NaN before this change; reverting the one-line guard turns it red again. Fixes #2588 Docs-not-needed: a numeric guard on an existing counter; no subsystem behaviour changes.
1 parent 45f52b3 commit 5dcd29e

4 files changed

Lines changed: 182 additions & 2 deletions

File tree

‎core/src/host/tasks.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ export function install(rt) {
569569
if (task.status === 'accepted') {
570570
if (countsAsDelivery(task)) row.deliveries += 1
571571
const lease = leaseOf(task)
572-
if (lease !== undefined && Number.isFinite(lease.takenAt)) row.workerSeconds += Math.max(0, Math.round((task.updatedAt - lease.takenAt) / 1000))
572+
if (lease !== undefined && Number.isFinite(lease.takenAt) && Number.isFinite(task.updatedAt)) row.workerSeconds += Math.max(0, Math.round((task.updatedAt - lease.takenAt) / 1000))
573573
}
574574
if (run !== '') {
575575
const runRow = bucket(runs, run)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// `rt.taskCounters` adds a card's own `updatedAt` into `workerSeconds`. A card may be stored by
2+
// an older version or edited by hand, so the field can be missing or non-numeric while the lease
3+
// is valid. The arithmetic must guard that operand too: `NaN` would become `null` through JSON
4+
// for every reader of the counter. The numeric case still measures the lease window.
5+
import assert from 'node:assert/strict'
6+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
7+
import { tmpdir } from 'node:os'
8+
import { join } from 'node:path'
9+
import { test } from 'node:test'
10+
import { createRuntime } from '../index.js'
11+
import { fakeContext } from './fake-ctx.js'
12+
13+
const TAKEN_AT = 1_700_000_000_000
14+
15+
const acceptedCard = (updatedAt) => ({
16+
id: 'task-1',
17+
title: 'Count the delivery',
18+
status: 'accepted',
19+
taskClass: 'work',
20+
ownerBotId: 'd1',
21+
createdAt: TAKEN_AT,
22+
...(updatedAt === undefined ? {} : { updatedAt }),
23+
lease: { holder: 'd1', takenAt: TAKEN_AT, expiresAt: TAKEN_AT + 1_800_000 },
24+
})
25+
26+
const seed = (task) => ({
27+
version: 2,
28+
revision: 1,
29+
mainBotIds: ['a'],
30+
members: { 'owner-1': { id: 'owner-1', createdAt: 1, designatedChiefId: 'a' } },
31+
bots: {
32+
a: { id: 'a', name: 'Chief', role: 'Lead', sessionId: 'a', createdAt: 1, ownerId: 'owner-1' },
33+
d1: { id: 'd1', name: 'Dev1', role: 'Dev', sessionId: 'd1', createdAt: 2, ownerId: 'owner-1', createdBy: 'a' },
34+
},
35+
rooms: {},
36+
grants: {},
37+
agents: {},
38+
exchanges: [],
39+
exchangeTurns: {},
40+
questions: {},
41+
prefs: {},
42+
tasks: { [task.id]: task },
43+
})
44+
45+
async function boot(t, task) {
46+
const home = mkdtempSync(join(tmpdir(), 'ds-bot-counters-'))
47+
writeFileSync(join(home, 'state.json'), JSON.stringify(seed(task)))
48+
const ctx = fakeContext()
49+
const agents = new Map()
50+
const agentOf = (sessionId) => {
51+
if (!agents.has(sessionId)) {
52+
const inbox = { nextStep: [], nextTurn: [], remove() { return true } }
53+
agents.set(sessionId, {
54+
session: { id: sessionId }, id: sessionId, calls: [], status: 'idle', inbox,
55+
followup() {}, steer() {}, inject() {}, cancel() {}, async whenIdle() {},
56+
})
57+
}
58+
return agents.get(sessionId)
59+
}
60+
Object.assign(ctx, {
61+
llm: { listProviders: () => [], listModels: async () => [], resolveModelInfo: async () => ({}) },
62+
workspaceRegistry: { get: () => undefined, resolveByPath: async () => undefined, create: async path => ({ id: 'w1', path }), archiveSession: async () => {} },
63+
sessionController: { create: async () => ({ sessionId: 'made-1' }), resolveAgent: async id => ({ agent: agentOf(id) }), selectModel: async () => {}, page: async () => ({ records: [], hasMore: false }) },
64+
sessionTitle: { rename: async () => {} },
65+
sessions: { flush: async () => {} },
66+
})
67+
const rt = createRuntime(ctx, { home })
68+
await rt.load()
69+
t.after(async () => {
70+
try { await ctx.dispose() } catch {}
71+
rmSync(home, { recursive: true, force: true, maxRetries: 5 })
72+
})
73+
return rt
74+
}
75+
76+
test('a non-numeric or missing updatedAt keeps workerSeconds finite (#2588)', async (t) => {
77+
for (const updatedAt of ['not-a-number', undefined, null]) {
78+
const rt = await boot(t, acceptedCard(updatedAt))
79+
const counts = rt.taskCounters()
80+
const workerSeconds = counts.roles.Dev.workerSeconds
81+
assert.equal(Number.isFinite(workerSeconds), true, `updatedAt=${String(updatedAt)} gave workerSeconds=${workerSeconds}`)
82+
const throughJson = JSON.parse(JSON.stringify(counts)).roles.Dev.workerSeconds
83+
assert.notEqual(throughJson, null, `updatedAt=${String(updatedAt)} serialised to null`)
84+
}
85+
})
86+
87+
test('a numeric updatedAt still measures the lease window (#2588)', async (t) => {
88+
const rt = await boot(t, acceptedCard(TAKEN_AT + 90_000))
89+
assert.equal(rt.taskCounters().roles.Dev.workerSeconds, 90)
90+
})

‎patches/ds-bot/files/src/host/tasks.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ export function install(rt) {
569569
if (task.status === 'accepted') {
570570
if (countsAsDelivery(task)) row.deliveries += 1
571571
const lease = leaseOf(task)
572-
if (lease !== undefined && Number.isFinite(lease.takenAt)) row.workerSeconds += Math.max(0, Math.round((task.updatedAt - lease.takenAt) / 1000))
572+
if (lease !== undefined && Number.isFinite(lease.takenAt) && Number.isFinite(task.updatedAt)) row.workerSeconds += Math.max(0, Math.round((task.updatedAt - lease.takenAt) / 1000))
573573
}
574574
if (run !== '') {
575575
const runRow = bucket(runs, run)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// `rt.taskCounters` adds a card's own `updatedAt` into `workerSeconds`. A card may be stored by
2+
// an older version or edited by hand, so the field can be missing or non-numeric while the lease
3+
// is valid. The arithmetic must guard that operand too: `NaN` would become `null` through JSON
4+
// for every reader of the counter. The numeric case still measures the lease window.
5+
import assert from 'node:assert/strict'
6+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
7+
import { tmpdir } from 'node:os'
8+
import { join } from 'node:path'
9+
import { test } from 'node:test'
10+
import { createRuntime } from '../index.js'
11+
import { fakeContext } from './fake-ctx.js'
12+
13+
const TAKEN_AT = 1_700_000_000_000
14+
15+
const acceptedCard = (updatedAt) => ({
16+
id: 'task-1',
17+
title: 'Count the delivery',
18+
status: 'accepted',
19+
taskClass: 'work',
20+
ownerBotId: 'd1',
21+
createdAt: TAKEN_AT,
22+
...(updatedAt === undefined ? {} : { updatedAt }),
23+
lease: { holder: 'd1', takenAt: TAKEN_AT, expiresAt: TAKEN_AT + 1_800_000 },
24+
})
25+
26+
const seed = (task) => ({
27+
version: 2,
28+
revision: 1,
29+
mainBotIds: ['a'],
30+
members: { 'owner-1': { id: 'owner-1', createdAt: 1, designatedChiefId: 'a' } },
31+
bots: {
32+
a: { id: 'a', name: 'Chief', role: 'Lead', sessionId: 'a', createdAt: 1, ownerId: 'owner-1' },
33+
d1: { id: 'd1', name: 'Dev1', role: 'Dev', sessionId: 'd1', createdAt: 2, ownerId: 'owner-1', createdBy: 'a' },
34+
},
35+
rooms: {},
36+
grants: {},
37+
agents: {},
38+
exchanges: [],
39+
exchangeTurns: {},
40+
questions: {},
41+
prefs: {},
42+
tasks: { [task.id]: task },
43+
})
44+
45+
async function boot(t, task) {
46+
const home = mkdtempSync(join(tmpdir(), 'ds-bot-counters-'))
47+
writeFileSync(join(home, 'state.json'), JSON.stringify(seed(task)))
48+
const ctx = fakeContext()
49+
const agents = new Map()
50+
const agentOf = (sessionId) => {
51+
if (!agents.has(sessionId)) {
52+
const inbox = { nextStep: [], nextTurn: [], remove() { return true } }
53+
agents.set(sessionId, {
54+
session: { id: sessionId }, id: sessionId, calls: [], status: 'idle', inbox,
55+
followup() {}, steer() {}, inject() {}, cancel() {}, async whenIdle() {},
56+
})
57+
}
58+
return agents.get(sessionId)
59+
}
60+
Object.assign(ctx, {
61+
llm: { listProviders: () => [], listModels: async () => [], resolveModelInfo: async () => ({}) },
62+
workspaceRegistry: { get: () => undefined, resolveByPath: async () => undefined, create: async path => ({ id: 'w1', path }), archiveSession: async () => {} },
63+
sessionController: { create: async () => ({ sessionId: 'made-1' }), resolveAgent: async id => ({ agent: agentOf(id) }), selectModel: async () => {}, page: async () => ({ records: [], hasMore: false }) },
64+
sessionTitle: { rename: async () => {} },
65+
sessions: { flush: async () => {} },
66+
})
67+
const rt = createRuntime(ctx, { home })
68+
await rt.load()
69+
t.after(async () => {
70+
try { await ctx.dispose() } catch {}
71+
rmSync(home, { recursive: true, force: true, maxRetries: 5 })
72+
})
73+
return rt
74+
}
75+
76+
test('a non-numeric or missing updatedAt keeps workerSeconds finite (#2588)', async (t) => {
77+
for (const updatedAt of ['not-a-number', undefined, null]) {
78+
const rt = await boot(t, acceptedCard(updatedAt))
79+
const counts = rt.taskCounters()
80+
const workerSeconds = counts.roles.Dev.workerSeconds
81+
assert.equal(Number.isFinite(workerSeconds), true, `updatedAt=${String(updatedAt)} gave workerSeconds=${workerSeconds}`)
82+
const throughJson = JSON.parse(JSON.stringify(counts)).roles.Dev.workerSeconds
83+
assert.notEqual(throughJson, null, `updatedAt=${String(updatedAt)} serialised to null`)
84+
}
85+
})
86+
87+
test('a numeric updatedAt still measures the lease window (#2588)', async (t) => {
88+
const rt = await boot(t, acceptedCard(TAKEN_AT + 90_000))
89+
assert.equal(rt.taskCounters().roles.Dev.workerSeconds, 90)
90+
})

0 commit comments

Comments
 (0)