Compare commits

...

2 Commits

Author SHA1 Message Date
770c253625 Merge pull request 'feat: capacity-aware scheduling (#8) — real focus factors drive every forecast' (#50) from feat/capacity into main
Reviewed-on: #50
2026-07-09 05:00:20 +00:00
Croissant Le Doux
1636d6bada feat: capacity-aware scheduling (#8) — real focus factors drive every forecast
Turns the single-serial-worker scheduler into a capacity-aware, multi-lane one.
Configured team members become lanes; an issue runs on its assignee's lane (or the
earliest-free lane), its duration scaled by that lane's throughput
(focusFactor × allocation). Every forecast — Focus cone, Runway, milestone
drill-in — is now capacity-aware.

core (@commitea/core):
- capacity/capacity-v0: CapacityMember + capacityPerWorkday + parseCapacityConfig
  (clamps, drops invalid; degrades to []).
- scheduler/scheduler-capacity-v0: scheduleWithCapacity reuses the v0 topo order +
  critical path, re-lays work across lanes (layoutOnLanes, resolveLanes, makespan).
  Empty workers → the single serial plan verbatim.
- forecast() gains options.workers: each MC trial lays sampled durations across the
  lanes and takes the makespan; serial path unchanged. SchedulableIssue gains
  assignee; ScheduledItem gains worker.
- 11 new tests (parse/clamp, parallelism halves makespan, speed scaling, assignee
  routing, cross-lane deps, forecast makespan shrinks with lanes).

app:
- pm-state capacity/members.json read (readCapacity + pmstate:capacity bridge);
  useCapacity hook → workers; forecastBacklog/runwayView/milestoneView pass workers.
- Runway Capacity card shows the real config (person · focus · alloc · pd/day).

Config lives in pm-state (D4); seeded christian(0.8)/stephen(0.6×0.5). Degrades to
the fixture/serial when absent.

Verified: 128 core tests green, desktop typecheck clean, 14 fixture e2e green. Live:
the capacity card is real, and the P2 forecast shifts 32d→37d — honest, since real
focus factors (<1) replace the v0 focus-1.0 assumption.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 00:59:03 -04:00
15 changed files with 416 additions and 13 deletions

View File

@@ -44,6 +44,9 @@ test.describe('live backlog', () => {
// Real per-milestone forecasts — these milestone names come from gitea, not the // Real per-milestone forecasts — these milestone names come from gitea, not the
// fixture (which lists Beta / Pilot-ready / v1.0). // fixture (which lists Beta / Pilot-ready / v1.0).
await expect(win.getByText(/P2 — Scheduler/)).toBeVisible() await expect(win.getByText(/P2 — Scheduler/)).toBeVisible()
// Real capacity config from pm-state (christian/stephen), not the fixture (Stephen/Ana K.)
await expect(win.getByText('christian', { exact: true })).toBeVisible()
await expect(win.getByText(/pd\/day/).first()).toBeVisible()
await win.screenshot({ path: join(here, '.artifacts', 'screens', 'live-runway.png'), fullPage: true, animations: 'disabled' }) await win.screenshot({ path: join(here, '.artifacts', 'screens', 'live-runway.png'), fullPage: true, animations: 'disabled' })
// Milestone drill-in — clicking a real milestone opens its real detail // Milestone drill-in — clicking a real milestone opens its real detail

View File

@@ -20,6 +20,7 @@ import {
type IssueChange, type IssueChange,
type LifecycleEvent, type LifecycleEvent,
makeDirectiveEntry, makeDirectiveEntry,
parseCapacityConfig,
parseDirectiveLog, parseDirectiveLog,
planIssueChange, planIssueChange,
type ProjectSnapshot, type ProjectSnapshot,
@@ -109,6 +110,19 @@ export async function readDirectives(client: GiteaClient) {
return parseDirectiveLog(text) return parseDirectiveLog(text)
} }
const CAPACITY_PATH = 'capacity/members.json'
/** Read the capacity config from the pm-state repo (empty when absent). */
export async function readCapacity(client: GiteaClient) {
const file = await client.getFile(CAPACITY_PATH)
if (!file) return []
try {
return parseCapacityConfig(JSON.parse(Buffer.from(file.contentBase64, 'base64').toString('utf8')))
} catch {
return []
}
}
/** Full reconcile: issues + milestones + native deps + lifecycle timelines. */ /** Full reconcile: issues + milestones + native deps + lifecycle timelines. */
export async function reconcileSnapshot( export async function reconcileSnapshot(
client: GiteaClient, client: GiteaClient,
@@ -266,4 +280,15 @@ export function registerGiteaIpc(): void {
return { ok: false as const, reason: 'error' as const, message: e instanceof Error ? e.message : String(e) } return { ok: false as const, reason: 'error' as const, message: e instanceof Error ? e.message : String(e) }
} }
}) })
// Read the capacity config from the pm-state repo (for capacity-aware forecasts).
ipcMain.handle('pmstate:capacity', async () => {
const pm = getPmStateClient()
if (!pm) return { ok: false as const, reason: 'unconfigured' as const, members: [] }
try {
return { ok: true as const, members: await readCapacity(pm) }
} catch {
return { ok: true as const, members: [] }
}
})
} }

View File

@@ -19,6 +19,8 @@ const api = {
pmstate: { pmstate: {
/** Read the directive ledger from the pm-state repo. */ /** Read the directive ledger from the pm-state repo. */
directives: () => ipcRenderer.invoke('pmstate:directives'), directives: () => ipcRenderer.invoke('pmstate:directives'),
/** Read the capacity config from the pm-state repo. */
capacity: () => ipcRenderer.invoke('pmstate:capacity'),
}, },
model: { model: {
/** Whether a model endpoint is configured (else the UI keeps the scripted Reginald). */ /** Whether a model endpoint is configured (else the UI keeps the scripted Reginald). */

View File

@@ -1,23 +1,35 @@
import React from 'react' import React from 'react'
import { type CapacityMember, capacityPerWorkday } from '@commitea/core'
import { RunwayBar } from '../charts/chart.js' import { RunwayBar } from '../charts/chart.js'
import { Card, Badge, Tag, Icon, IconButton } from '../ui/index.js' import { Card, Badge, Tag, Icon, IconButton } from '../ui/index.js'
import { RUNWAY, CAPACITY, type RunwayMilestone } from '../../data/fixtures.js' import { RUNWAY, CAPACITY, type RunwayMilestone } from '../../data/fixtures.js'
// Runway — capacity vs milestone dates; ranges, never points. // Runway — capacity vs milestone dates; ranges, never points.
// `milestones` (real per-milestone forecasts) overrides the demo when present. // `milestones` (real per-milestone forecasts) + `capacity` (real config) override the demo.
export function RunwayScreen({ export function RunwayScreen({
onOpenCalibration, onOpenCalibration,
onOpenMilestone, onOpenMilestone,
calibration, calibration,
milestones, milestones,
capacity,
}: { }: {
onOpenCalibration: () => void onOpenCalibration: () => void
onOpenMilestone: (id?: number) => void onOpenMilestone: (id?: number) => void
calibration?: { n: number; coldStart: boolean } calibration?: { n: number; coldStart: boolean }
milestones?: RunwayMilestone[] milestones?: RunwayMilestone[]
capacity?: CapacityMember[]
}) { }) {
const rows = milestones && milestones.length ? milestones : RUNWAY const rows = milestones && milestones.length ? milestones : RUNWAY
const capacityRows =
capacity && capacity.length
? capacity.map((m) => ({
who: m.person,
slices: `focus ${m.focusFactor} · alloc ${Math.round(m.allocation * 100)}%`,
hours: `${capacityPerWorkday(m).toFixed(2)} pd/day`,
}))
: CAPACITY
const calibNote = calibration const calibNote = calibration
? calibration.coldStart ? calibration.coldStart
? `cold-start priors · ${calibration.n}/20 closed issues estimated` ? `cold-start priors · ${calibration.n}/20 closed issues estimated`
@@ -59,7 +71,7 @@ export function RunwayScreen({
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 14, alignItems: 'start' }}> <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 14, alignItems: 'start' }}>
<Card overline="Capacity" flush> <Card overline="Capacity" flush>
<div> <div>
{CAPACITY.map((p, i) => ( {capacityRows.map((p, i) => (
<div key={p.who} style={{ <div key={p.who} style={{
display: 'flex', alignItems: 'center', gap: 12, padding: '12px 20px', display: 'flex', alignItems: 'center', gap: 12, padding: '12px 20px',
borderTop: i === 0 ? 'none' : '1px solid var(--line-1)', borderTop: i === 0 ? 'none' : '1px solid var(--line-1)',

View File

@@ -6,6 +6,7 @@ import type { IssueChange } from '@commitea/core'
import type { IssueRef } from '../../data/fixtures.js' import type { IssueRef } from '../../data/fixtures.js'
import { import {
backlogCalibration, backlogCalibration,
capacityWorkers,
forecastBacklog, forecastBacklog,
issuesToBoardColumns, issuesToBoardColumns,
milestoneView, milestoneView,
@@ -13,6 +14,7 @@ import {
scheduleFocus, scheduleFocus,
} from '../../lib/backlog.js' } from '../../lib/backlog.js'
import { useBacklog } from '../../lib/use-backlog.js' import { useBacklog } from '../../lib/use-backlog.js'
import { useCapacity } from '../../lib/use-capacity.js'
import { PrimitivesGallery } from '../gallery.js' import { PrimitivesGallery } from '../gallery.js'
import { BoardScreen } from '../screens/board-screen.js' import { BoardScreen } from '../screens/board-screen.js'
import { CalibrationScreen } from '../screens/calibration-screen.js' import { CalibrationScreen } from '../screens/calibration-screen.js'
@@ -96,6 +98,8 @@ export function AppShell() {
const [readIds, setReadIds] = useState<number[]>([]) const [readIds, setReadIds] = useState<number[]>([])
const [milestoneId, setMilestoneId] = useState<number | null>(null) const [milestoneId, setMilestoneId] = useState<number | null>(null)
const [backlog, refetchBacklog] = useBacklog() const [backlog, refetchBacklog] = useBacklog()
const capacityMembers = useCapacity()
const workers = capacityWorkers(capacityMembers)
const boardColumns = const boardColumns =
backlog.status === 'ready' ? issuesToBoardColumns(backlog.issues, backlog.timelines) : undefined backlog.status === 'ready' ? issuesToBoardColumns(backlog.issues, backlog.timelines) : undefined
const focus = const focus =
@@ -104,10 +108,12 @@ export function AppShell() {
backlog.status === 'ready' ? backlogCalibration(backlog.issues, backlog.timelines) : undefined backlog.status === 'ready' ? backlogCalibration(backlog.issues, backlog.timelines) : undefined
const forecast = const forecast =
backlog.status === 'ready' backlog.status === 'ready'
? (forecastBacklog(backlog.issues, backlog.deps, new Date(), calibration?.model) ?? undefined) ? (forecastBacklog(backlog.issues, backlog.deps, new Date(), calibration?.model, workers) ?? undefined)
: undefined : undefined
const runwayMilestones = const runwayMilestones =
backlog.status === 'ready' ? runwayView(backlog.issues, backlog.milestones, backlog.deps) : undefined backlog.status === 'ready'
? runwayView(backlog.issues, backlog.milestones, backlog.deps, new Date(), workers)
: undefined
const milestone = const milestone =
backlog.status === 'ready' && milestoneId != null backlog.status === 'ready' && milestoneId != null
? (milestoneView( ? (milestoneView(
@@ -117,6 +123,8 @@ export function AppShell() {
backlog.deps, backlog.deps,
backlog.timelines, backlog.timelines,
calibration?.model, calibration?.model,
new Date(),
workers,
) ?? undefined) ) ?? undefined)
: undefined : undefined
@@ -221,6 +229,7 @@ export function AppShell() {
}} }}
calibration={calibration ? { n: calibration.model.n, coldStart: calibration.model.coldStart } : undefined} calibration={calibration ? { n: calibration.model.n, coldStart: calibration.model.coldStart } : undefined}
milestones={runwayMilestones} milestones={runwayMilestones}
capacity={capacityMembers}
/> />
) )
case 'calibration': case 'calibration':

View File

@@ -1,5 +1,6 @@
import type { import type {
AgentStep, AgentStep,
CapacityMember,
ChangeProposal, ChangeProposal,
ChatMessage, ChatMessage,
CaptureProposal, CaptureProposal,
@@ -77,9 +78,15 @@ export type DirectivesResult =
| { ok: false; reason: 'unconfigured' | 'error'; message?: string } | { ok: false; reason: 'unconfigured' | 'error'; message?: string }
| { ok: true; directives: DirectiveRecord[] } | { ok: true; directives: DirectiveRecord[] }
/** The result of reading the capacity config. */
export type CapacityResult =
| { ok: false; reason: 'unconfigured'; members: CapacityMember[] }
| { ok: true; members: CapacityMember[] }
/** The pm-state bridge (machine-derived state) exposed by the preload over IPC. */ /** The pm-state bridge (machine-derived state) exposed by the preload over IPC. */
export interface PmStateBridge { export interface PmStateBridge {
directives(): Promise<DirectivesResult> directives(): Promise<DirectivesResult>
capacity(): Promise<CapacityResult>
} }
declare global { declare global {

View File

@@ -2,10 +2,13 @@ import {
type CalibrationModel, type CalibrationModel,
type CalibrationSample, type CalibrationSample,
calibrationSamples, calibrationSamples,
type CapacityMember,
capacityPerWorkday,
COLD_START_THRESHOLD, COLD_START_THRESHOLD,
type DependencyEdge, type DependencyEdge,
fitCalibration, fitCalibration,
forecast, forecast,
type Worker,
type GiteaIssue, type GiteaIssue,
type GiteaMilestone, type GiteaMilestone,
inferLifecycle, inferLifecycle,
@@ -107,9 +110,15 @@ function toSchedulable(issues: GiteaIssue[]) {
labels: i.labels, labels: i.labels,
estimateDays: i.facts.estimateDays, estimateDays: i.facts.estimateDays,
priority: i.facts.priority, priority: i.facts.priority,
assignee: i.assignee,
})) }))
} }
/** gitea CapacityMembers → scheduler lanes (person + throughput). */
export function capacityWorkers(members: CapacityMember[]): Worker[] {
return members.map((m) => ({ person: m.person, speed: capacityPerWorkday(m) }))
}
export interface ForecastView { export interface ForecastView {
scope: number scope: number
cone: BurnUpData cone: BurnUpData
@@ -132,9 +141,10 @@ export function forecastBacklog(
deps: DependencyEdge[], deps: DependencyEdge[],
today: Date = new Date(), today: Date = new Date(),
calibration?: CalibrationModel, calibration?: CalibrationModel,
workers: Worker[] = [],
): ForecastView | null { ): ForecastView | null {
const model = calibration ? toDurationModel(calibration) : undefined const model = calibration ? toDurationModel(calibration) : undefined
const f = forecast(toSchedulable(issues), deps, model ? { model } : {}) const f = forecast(toSchedulable(issues), deps, { ...(model ? { model } : {}), workers })
const cone = buildBurnUpData(f, today) const cone = buildBurnUpData(f, today)
if (!cone) return null if (!cone) return null
return { return {
@@ -252,6 +262,7 @@ export function runwayView(
milestones: GiteaMilestone[], milestones: GiteaMilestone[],
deps: DependencyEdge[], deps: DependencyEdge[],
today: Date = new Date(), today: Date = new Date(),
workers: Worker[] = [],
): RunwayMilestone[] { ): RunwayMilestone[] {
const open = issues.filter((i) => i.state === 'open') const open = issues.filter((i) => i.state === 'open')
const rows = milestones const rows = milestones
@@ -266,8 +277,10 @@ export function runwayView(
labels: i.labels, labels: i.labels,
estimateDays: i.facts.estimateDays, estimateDays: i.facts.estimateDays,
priority: i.facts.priority, priority: i.facts.priority,
assignee: i.assignee,
})), })),
deps, deps,
{ workers },
) )
const p90 = f.curve.length ? f.curve[f.curve.length - 1].p90Day : f.p95Day const p90 = f.curve.length ? f.curve[f.curve.length - 1].p90Day : f.p95Day
const dueDay = m.dueOn ? workingDaysBetween(today, new Date(m.dueOn)) : null const dueDay = m.dueOn ? workingDaysBetween(today, new Date(m.dueOn)) : null
@@ -324,6 +337,7 @@ export function milestoneView(
timelines: Timelines = {}, timelines: Timelines = {},
calibration?: CalibrationModel, calibration?: CalibrationModel,
today: Date = new Date(), today: Date = new Date(),
workers: Worker[] = [],
): MilestoneView | null { ): MilestoneView | null {
const m = milestones.find((x) => x.id === id) const m = milestones.find((x) => x.id === id)
if (!m) return null if (!m) return null
@@ -340,9 +354,10 @@ export function milestoneView(
labels: i.labels, labels: i.labels,
estimateDays: i.facts.estimateDays, estimateDays: i.facts.estimateDays,
priority: i.facts.priority, priority: i.facts.priority,
assignee: i.assignee,
})), })),
deps, deps,
model ? { model } : {}, { ...(model ? { model } : {}), workers },
) )
const cone = buildBurnUpData(f, today) const cone = buildBurnUpData(f, today)

View File

@@ -0,0 +1,24 @@
import { useEffect, useState } from 'react'
import type { CapacityMember } from '@commitea/core'
/**
* The team's capacity config from the pm-state repo. Empty when unconfigured —
* forecasts then fall back to a single serial worker. Read once on mount.
*/
export function useCapacity(): CapacityMember[] {
const [members, setMembers] = useState<CapacityMember[]>([])
useEffect(() => {
let alive = true
window.commitea.pmstate
.capacity()
.then((r) => {
if (alive) setMembers(r.members)
})
.catch(() => {})
return () => {
alive = false
}
}, [])
return members
}

View File

@@ -0,0 +1,88 @@
import { describe, expect, it } from 'vitest'
import { type DependencyEdge, type SchedulableIssue } from '../scheduler/scheduler-v0.js'
import { makespan, scheduleWithCapacity, type Worker } from '../scheduler/scheduler-capacity-v0.js'
import { capacityPerWorkday, parseCapacityConfig } from './capacity-v0.js'
describe('capacity model', () => {
it('capacityPerWorkday = focusFactor × allocation', () => {
expect(capacityPerWorkday({ person: 'a', focusFactor: 0.8, allocation: 0.5 })).toBeCloseTo(0.4)
})
it('parses + clamps config, drops invalid members', () => {
const members = parseCapacityConfig({
members: [
{ person: 'christian', focusFactor: 0.8, allocation: 1 },
{ person: 'ak', focusFactor: 1.5, allocation: -1 }, // clamps to 1 / 0 → zero capacity → dropped
{ focusFactor: 0.8 }, // no person → dropped
],
})
expect(members).toEqual([{ person: 'christian', focusFactor: 0.8, allocation: 1 }])
})
it('returns [] for a non-array/absent members field', () => {
expect(parseCapacityConfig({})).toEqual([])
expect(parseCapacityConfig({ members: 'nope' })).toEqual([])
})
})
function issue(number: number, over: Partial<SchedulableIssue> = {}): SchedulableIssue {
return { number, title: `#${number}`, labels: [], estimateDays: 4, priority: 2, ...over }
}
describe('scheduleWithCapacity', () => {
it('with no workers, falls back to the single serial plan', () => {
const issues = [issue(1), issue(2)]
const plan = scheduleWithCapacity(issues, [], [])
expect(makespan(plan)).toBe(8) // 4 + 4 serial
})
it('parallelizes independent work across lanes (makespan shrinks)', () => {
const issues = [issue(1), issue(2), issue(3), issue(4)] // 4×4d = 16d serial
const one: Worker[] = [{ person: 'a', speed: 1 }]
const two: Worker[] = [
{ person: 'a', speed: 1 },
{ person: 'b', speed: 1 },
]
expect(makespan(scheduleWithCapacity(issues, [], one))).toBe(16)
expect(makespan(scheduleWithCapacity(issues, [], two))).toBe(8) // two lanes → half
})
it('scales duration by a lane speed (slower lane takes longer)', () => {
const plan = scheduleWithCapacity([issue(1, { estimateDays: 4 })], [], [{ person: 'a', speed: 0.5 }])
expect(plan.items[0].durationDays).toBe(8) // 4 / 0.5
expect(plan.items[0].worker).toBe('a')
})
it('routes an issue to its assignee lane', () => {
const issues = [issue(1, { assignee: 'ak' }), issue(2, { assignee: 'sm' })]
const workers: Worker[] = [
{ person: 'ak', speed: 1 },
{ person: 'sm', speed: 1 },
]
const plan = scheduleWithCapacity(issues, [], workers)
const byN = Object.fromEntries(plan.items.map((i) => [i.number, i]))
expect(byN[1].worker).toBe('ak')
expect(byN[2].worker).toBe('sm')
// both start at 0 (different lanes) → parallel
expect(byN[1].startDay).toBe(0)
expect(byN[2].startDay).toBe(0)
})
it('adds a lane for an assignee not in the config (mean speed)', () => {
const plan = scheduleWithCapacity([issue(1, { assignee: 'newbie' })], [], [{ person: 'a', speed: 0.5 }])
expect(plan.items[0].worker).toBe('newbie')
expect(plan.items[0].durationDays).toBe(8) // mean speed 0.5 → 4/0.5
})
it('respects dependencies across lanes (a blocker finishes before its dependent starts)', () => {
const edges: DependencyEdge[] = [{ issue: 2, dependsOn: 1 }]
const workers: Worker[] = [
{ person: 'a', speed: 1 },
{ person: 'b', speed: 1 },
]
const plan = scheduleWithCapacity([issue(1), issue(2)], edges, workers)
const byN = Object.fromEntries(plan.items.map((i) => [i.number, i]))
expect(byN[2].startDay).toBeGreaterThanOrEqual(byN[1].endDay) // #2 waits for #1 even on another lane
})
})

View File

@@ -0,0 +1,49 @@
/**
* Capacity model (#8). A member's throughput is `focusFactor × allocation` =
* ideal person-days of project work delivered per calendar working day. The
* scheduler treats each member as a lane whose task durations are scaled by that
* rate (a 0.5-capacity person takes twice as long on an est/2d task). Config
* lives in the pm-state repo (`capacity/members.yaml/json`, D4); estimates are
* in ideal person-days (pm-state.md).
*/
export interface CapacityMember {
/** gitea login. */
person: string
/** Productive fraction of a working day (0..1). */
focusFactor: number
/** Fraction of that allocated to this project (0..1). */
allocation: number
}
/** Ideal person-days delivered per calendar working day. */
export function capacityPerWorkday(m: CapacityMember): number {
return m.focusFactor * m.allocation
}
function clamp01(n: unknown, fallback: number): number {
return typeof n === 'number' && Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : fallback
}
/**
* Validate a raw capacity config (`{ members: [...] }`) into members. Unknown
* shapes degrade to `[]` (→ the scheduler falls back to a single worker), never
* throw. focusFactor/allocation clamp to [0,1]; a member without a person is dropped.
*/
export function parseCapacityConfig(raw: unknown): CapacityMember[] {
const list = (raw as { members?: unknown })?.members
if (!Array.isArray(list)) return []
const out: CapacityMember[] = []
for (const m of list) {
const r = (m ?? {}) as Record<string, unknown>
const person = typeof r.person === 'string' ? r.person.trim() : ''
if (!person) continue
const member: CapacityMember = {
person,
focusFactor: clamp01(r.focusFactor, 0.8),
allocation: clamp01(r.allocation, 1),
}
if (capacityPerWorkday(member) > 0) out.push(member)
}
return out
}

View File

@@ -120,4 +120,20 @@ describe('forecast', () => {
expect(fitted.coldStart).toBe(false) expect(fitted.coldStart).toBe(false)
expect(fitted.p50Day).toBeLessThan(priors.p50Day) expect(fitted.p50Day).toBeLessThan(priors.p50Day)
}) })
it('capacity lanes parallelize the sim — the makespan shrinks with more workers', () => {
const independent = [issue(1), issue(2), issue(3), issue(4)] // no deps → fully parallelizable
const serial = forecast(independent, [], { trials: 2000, seed: 7 })
const twoLanes = forecast(independent, [], {
trials: 2000,
seed: 7,
workers: [
{ person: 'a', speed: 1 },
{ person: 'b', speed: 1 },
],
})
// two equal lanes ≈ half the serial landing (independent work)
expect(twoLanes.p80Day).toBeLessThan(serial.p80Day)
expect(twoLanes.p80Day).toBeLessThan(serial.p80Day * 0.7)
})
}) })

View File

@@ -18,6 +18,7 @@ import {
schedule, schedule,
type SchedulableIssue, type SchedulableIssue,
} from '../scheduler/scheduler-v0.js' } from '../scheduler/scheduler-v0.js'
import { type LaneInputs, layoutOnLanes, resolveLanes, type Worker } from '../scheduler/scheduler-capacity-v0.js'
export interface LognormalPrior { export interface LognormalPrior {
/** Median log-ratio: sampled median duration = estimate * e^mu. */ /** Median log-ratio: sampled median duration = estimate * e^mu. */
@@ -76,6 +77,12 @@ export interface ForecastOptions {
* the sim; otherwise the code-resident cold-start priors do. * the sim; otherwise the code-resident cold-start priors do.
*/ */
model?: DurationModel model?: DurationModel
/**
* Capacity lanes. When provided, each trial lays sampled durations across the
* lanes (parallel) instead of a single serial worker — the makespan shrinks
* toward the critical path. Empty/absent → single serial worker.
*/
workers?: Worker[]
} }
/** Resolve the lognormal params for an estimate, preferring a fitted model. */ /** Resolve the lognormal params for an estimate, preferring a fitted model. */
@@ -158,17 +165,34 @@ export function forecast(
const priors = order.map((it) => durationParams(it.durationDays, options.model)) const priors = order.map((it) => durationParams(it.durationDays, options.model))
const rng = mulberry32(seed) const rng = mulberry32(seed)
// endByRank[k][t] = working day the (k+1)-th scheduled issue completes on trial t. // Capacity lanes, when configured — the per-trial layout goes parallel.
const laneInputs: LaneInputs = {
order: order.map((it) => it.number),
blockedBy: new Map(order.map((it) => [it.number, it.blockedBy])),
assignee: new Map(issues.map((i) => [i.number, i.assignee ?? null])),
}
const lanes =
options.workers && options.workers.length ? resolveLanes(options.workers, [...laneInputs.assignee.values()]) : null
// endByRank[k][t] = working day the (k+1)-th issue *to finish* completes on trial t.
const endByRank: number[][] = Array.from({ length: n }, () => new Array<number>(trials)) const endByRank: number[][] = Array.from({ length: n }, () => new Array<number>(trials))
for (let t = 0; t < trials; t++) { for (let t = 0; t < trials; t++) {
const sampled = order.map((it, k) => it.durationDays * Math.exp(priors[k].mu + priors[k].sigma * standardNormal(rng)))
if (lanes) {
// parallel: lay the sampled durations across lanes, then sort finish days
const byNumber = new Map(order.map((it, k) => [it.number, sampled[k]]))
const { finishAt } = layoutOnLanes(laneInputs, lanes, (num) => byNumber.get(num)!)
const finishes = order.map((it) => finishAt.get(it.number)!).sort((a, b) => a - b)
for (let k = 0; k < n; k++) endByRank[k][t] = finishes[k]
} else {
// single serial worker: cumulative sum (already sorted ascending)
let cursor = 0 let cursor = 0
for (let k = 0; k < n; k++) { for (let k = 0; k < n; k++) {
const p = priors[k] cursor += sampled[k]
const sampled = order[k].durationDays * Math.exp(p.mu + p.sigma * standardNormal(rng))
cursor += sampled
endByRank[k][t] = cursor endByRank[k][t] = cursor
} }
} }
}
const curve: BurnUpPoint[] = endByRank.map((row, k) => { const curve: BurnUpPoint[] = endByRank.map((row, k) => {
const sorted = [...row].sort((a, b) => a - b) const sorted = [...row].sort((a, b) => a - b)

View File

@@ -49,6 +49,12 @@ export type {
SchedulePlan, SchedulePlan,
} from './scheduler/scheduler-v0.js' } from './scheduler/scheduler-v0.js'
export { makespan, scheduleWithCapacity } from './scheduler/scheduler-capacity-v0.js'
export type { Worker } from './scheduler/scheduler-capacity-v0.js'
export { capacityPerWorkday, parseCapacityConfig } from './capacity/capacity-v0.js'
export type { CapacityMember } from './capacity/capacity-v0.js'
export { export {
COLD_START_PRIORS, COLD_START_PRIORS,
durationParams, durationParams,

View File

@@ -0,0 +1,119 @@
/**
* Capacity-aware scheduler (#8). Reuses the single-worker scheduler's topological
* order + critical-path marking, then re-lays the work across lanes: an issue
* runs on its assignee's lane (or the earliest-free lane when unassigned), its
* duration scaled by that lane's speed (ideal person-days/workday). Makespan
* shrinks toward the critical path as lanes are added. Deterministic; falls back
* to the single serial worker when no capacity is configured. The lane layout is
* factored so the Monte Carlo forecast reuses it per trial with sampled durations.
*/
import {
type DependencyEdge,
schedule,
type SchedulableIssue,
type SchedulePlan,
} from './scheduler-v0.js'
/** A scheduling lane: a person and their throughput (ideal person-days / workday). */
export interface Worker {
person: string
speed: number
}
/** The topological order + per-issue relations the layout needs (from the base plan). */
export interface LaneInputs {
order: number[]
blockedBy: Map<number, number[]>
assignee: Map<number, string | null>
}
/** Ensure a lane exists for every assignee; unconfigured assignees get the mean speed. */
export function resolveLanes(workers: Worker[], assignees: (string | null)[]): Worker[] {
const lanes = [...workers]
const known = new Set(lanes.map((w) => w.person))
const meanSpeed = lanes.length ? lanes.reduce((s, w) => s + w.speed, 0) / lanes.length : 1
for (const a of assignees) {
if (a && !known.has(a)) {
lanes.push({ person: a, speed: meanSpeed })
known.add(a)
}
}
return lanes
}
function pickWorker(lanes: Worker[], freeAt: Map<string, number>, assignee: string | null | undefined): Worker {
if (assignee) {
const own = lanes.find((w) => w.person === assignee)
if (own) return own
}
let best = lanes[0]
for (const w of lanes) if (freeAt.get(w.person)! < freeAt.get(best.person)!) best = w
return best
}
/**
* Lay a topologically-ordered set out across lanes. `duration(n)` supplies each
* issue's duration for this layout (estimate, or a sampled value in a MC trial).
* Returns each issue's finish day + the lane it ran on. Order guarantees a
* dependency is always laid out before its dependents.
*/
export function layoutOnLanes(
inputs: LaneInputs,
lanes: Worker[],
duration: (n: number) => number,
): { finishAt: Map<number, number>; startAt: Map<number, number>; laneOf: Map<number, string> } {
const freeAt = new Map(lanes.map((w) => [w.person, 0]))
const finishAt = new Map<number, number>()
const startAt = new Map<number, number>()
const laneOf = new Map<number, string>()
for (const n of inputs.order) {
const worker = pickWorker(lanes, freeAt, inputs.assignee.get(n))
const blockers = inputs.blockedBy.get(n) ?? []
const depFinish = blockers.length ? Math.max(...blockers.map((d) => finishAt.get(d) ?? 0)) : 0
const start = Math.max(freeAt.get(worker.person)!, depFinish)
const end = start + duration(n) / worker.speed
startAt.set(n, start)
finishAt.set(n, end)
laneOf.set(n, worker.person)
freeAt.set(worker.person, end)
}
return { finishAt, startAt, laneOf }
}
/**
* Capacity-aware plan. An empty `workers` means no capacity is configured → the
* single-worker plan verbatim.
*/
export function scheduleWithCapacity(
issues: SchedulableIssue[],
edges: DependencyEdge[],
workers: Worker[],
): SchedulePlan {
if (workers.length === 0) return schedule(issues, edges)
const base = schedule(issues, edges)
if (base.cycle || base.items.length === 0) return base
const inputs: LaneInputs = {
order: base.items.map((it) => it.number),
blockedBy: new Map(base.items.map((it) => [it.number, it.blockedBy])),
assignee: new Map(issues.map((i) => [i.number, i.assignee ?? null])),
}
const lanes = resolveLanes(workers, [...inputs.assignee.values()])
const durationOf = new Map(base.items.map((it) => [it.number, it.durationDays]))
const { finishAt, startAt, laneOf } = layoutOnLanes(inputs, lanes, (n) => durationOf.get(n)!)
const items = base.items.map((it) => ({
...it,
startDay: startAt.get(it.number)!,
endDay: finishAt.get(it.number)!,
durationDays: finishAt.get(it.number)! - startAt.get(it.number)!,
worker: laneOf.get(it.number),
}))
return { items, cycle: null }
}
/** Makespan (last finish) of a plan — the project's landing day. */
export function makespan(plan: SchedulePlan): number {
return plan.items.reduce((m, it) => Math.max(m, it.endDay), 0)
}

View File

@@ -21,6 +21,8 @@ export interface SchedulableIssue {
estimateDays: number | null estimateDays: number | null
/** 1 (most urgent) … 4; null when unset. */ /** 1 (most urgent) … 4; null when unset. */
priority: number | null priority: number | null
/** gitea assignee login, for capacity-aware lane routing; optional. */
assignee?: string | null
} }
export interface DependencyEdge { export interface DependencyEdge {
@@ -45,6 +47,8 @@ export interface ScheduledItem {
/** On a longest-duration dependency chain. */ /** On a longest-duration dependency chain. */
critical: boolean critical: boolean
rationale: string rationale: string
/** Lane it's scheduled on, when capacity-aware; absent for the single-worker plan. */
worker?: string
} }
export interface SchedulePlan { export interface SchedulePlan {