|
| 1 | +import { identityKey, type PlanIdentity } from './identity.ts'; |
| 2 | +import type { Plan, PlanItem } from './plan.ts'; |
| 3 | +import type { Segment } from './linking.ts'; |
| 4 | + |
| 5 | +/** Stable representation ignores object-key order and normalizes CRLF, not whitespace. */ |
| 6 | +function stable(value: unknown): string { |
| 7 | + if (typeof value === 'string') return JSON.stringify(value.replace(/\r\n/g, '\n')); |
| 8 | + if (Array.isArray(value)) return `[${value.map(stable).join(',')}]`; |
| 9 | + if (value !== null && typeof value === 'object') return `{${Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, val]) => `${JSON.stringify(key)}:${stable(val)}`).join(',')}}`; |
| 10 | + return JSON.stringify(value); |
| 11 | +} |
| 12 | +const contentKey = (s: Segment) => stable({ path: s.path, oldPath: s.oldPath, kind: s.kind, operation: s.operation, content: s.content }); |
| 13 | +export interface SegmentChoice { key: string; action: 'assign' | 'accept'; item: string | null } |
| 14 | +/** Position among identical segments and total copies prevent approval transfer. */ |
| 15 | +export function choiceKeys(segments: readonly Segment[], identity: PlanIdentity): string[] { |
| 16 | + const identityValue = identityKey(identity); |
| 17 | + const counts = new Map<string, number>(), seen = new Map<string, number>(); |
| 18 | + for (const segment of segments) { const key = contentKey(segment); counts.set(key, (counts.get(key) ?? 0) + 1); } |
| 19 | + return segments.map(segment => { |
| 20 | + const key = contentKey(segment), copy = (seen.get(key) ?? 0) + 1; seen.set(key, copy); |
| 21 | + return stable([identityValue, key, copy, counts.get(key)]); |
| 22 | + }); |
| 23 | +} |
| 24 | +export function applyChoices(plan: Plan, segments: readonly Segment[], choices: readonly SegmentChoice[], identity: PlanIdentity): Segment[] { |
| 25 | + const keys = choiceKeys(segments, identity); |
| 26 | + const byKey = new Map(choices.map(choice => [choice.key, choice])); |
| 27 | + return segments.map((segment, i) => { |
| 28 | + const choice = byKey.get(keys[i]!); |
| 29 | + if (!choice || !['Ambiguous', 'Unplanned'].includes(segment.row)) return { ...segment }; |
| 30 | + if (choice.action === 'accept') return { ...segment, row: 'Accepted' }; |
| 31 | + if (!plan.items.some(item => item.id === choice.item)) throw new Error('Assigned item does not exist.'); |
| 32 | + return { ...segment, row: choice.item! }; |
| 33 | + }); |
| 34 | +} |
| 35 | +export interface Approval { item: string; fingerprint: string } |
| 36 | +function fingerprint(item: PlanItem, segments: readonly Segment[], identity: PlanIdentity): string { |
| 37 | + return stable({ identity: identityKey(identity), item, segments: segments.filter(s => s.row === item.id).map(s => ({ |
| 38 | + path: s.path, oldPath: s.oldPath, kind: s.kind, operation: s.operation, |
| 39 | + content: s.content, context: s.context, owners: [...s.owners].sort(), |
| 40 | + })) }); |
| 41 | +} |
| 42 | +export function approveItem(plan: Plan, segments: readonly Segment[], itemId: string, identity: PlanIdentity, confirmNoChange = false): Approval { |
| 43 | + const item = plan.items.find(item => item.id === itemId); |
| 44 | + if (!item) throw new Error('Unknown item.'); |
| 45 | + if (!segments.some(segment => segment.row === itemId) && !confirmNoChange) throw new Error('Confirm no change needed before approving.'); |
| 46 | + return { item: itemId, fingerprint: fingerprint(item, segments, identity) }; |
| 47 | +} |
| 48 | +export function approvalStates(plan: Plan, segments: readonly Segment[], approvals: readonly Approval[], identity: PlanIdentity): Record<string, 'unreviewed' | 'approved' | 'stale'> { |
| 49 | + const result: Record<string, 'unreviewed' | 'approved' | 'stale'> = Object.create(null); |
| 50 | + for (const item of plan.items) { |
| 51 | + const approval = approvals.find(approval => approval.item === item.id); |
| 52 | + result[item.id] = !approval ? 'unreviewed' : approval.fingerprint !== fingerprint(item, segments, identity) || |
| 53 | + item.depends_on.some(dep => result[dep] === 'stale') ? 'stale' : 'approved'; |
| 54 | + } |
| 55 | + return result; |
| 56 | +} |
0 commit comments