Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

8.7 KB
Raw Blame
// The sample's sub focuses and the trimmed fixture's, made the way the worker makes them (design pass Phase 8): an area
// row, the router's placements and its sub focus answer go through my-focus.mjs's own row builders (routerFocusRows,
// subFocusPlacementRowFields for the owner's move), then its reducer (buildFocusAreas) and placeRequests, so what the
// sample and the fixture serve is what a worker would serve for that answer, never typed by hand as if it were live.
// Only the answer is written here, as the router's model would give it. Run with bun in src/ui/my-work:
//   bun scripts/sample-sub-focuses.ts --write
// rewrites src/data-sub-focuses.ts and the trimmed fixture's areas and placements; without --write it prints them.
// tests/ui/my-work/sample-sub-focuses.test.ts checks that both files hold exactly what this makes.
import { readFileSync, writeFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { buildFocusAreas, focusAreaRowFields, MY_FOCUS_LIST, placeRequests, routerFocusRows, subFocusPlacementRowFields, type FocusArea as SavedFocusArea } from '../../../services/work-view/my-focus.mjs'
import { reduceWorkState } from '../../../services/work-view/work-state-reduce.mjs'
import { SAMPLE_REQUESTS } from '../src/data.ts'
import { SAMPLE_FOCUS_AREAS } from '../src/data-goals.ts'
import type { WorkViewHumanRequest, WorkViewSnapshot, WorkViewSubFocus } from '../src/live/work-view-api-types.ts'

/** One area's sub focus answer, by request id: new groups in the order the router names them, and the owner's moves after. */
interface SubFocusAnswer {
  focusAreaId: string
  /** Each new group's id (the worker's are random; a fixture's are readable), name and requests. */
  groups: { subFocusId: string; name: string; requests: string[] }[]
  /** Requests the router left ungrouped. */
  ungrouped: string[]
  /** The owner's moves made afterwards in My Work (move-to-sub-focus): a request to a sub focus of its area, or to none. */
  ownerMoves: { request: string; subFocusId: string | null }[]
}

/**
 * The sample Viewer area's answer: two groups of two, and a third the owner then took one request out of, so it holds
 * one and is no card (its request shows under "More in Viewer", with the ones the router never grouped).
 */
const SAMPLE_ANSWER: SubFocusAnswer = {
  focusAreaId: 'fa-viewer',
  groups: [
    { subFocusId: 'sf-viewer-live', name: 'Live updates', requests: ['r1', 'r6'] },
    { subFocusId: 'sf-viewer-lists', name: 'Cards and lists', requests: ['r4', 'r7'] },
    { subFocusId: 'sf-viewer-notes', name: 'Release notes', requests: ['r8', 'r9'] },
  ],
  ungrouped: [],
  ownerMoves: [{ request: 'r9', subFocusId: null }],
}

/** The trimmed fixture's one area, the garden planner its owner-goals list asks for, and its five requests in it. */
const FIXTURE_AREA = { focusAreaId: 'fa_garden_planner', name: 'Garden planner', goal: 'Ship the garden planner and keep its reminders right.' }
const FIXTURE_ANSWER = (ids: string[]): SubFocusAnswer => ({
  focusAreaId: FIXTURE_AREA.focusAreaId,
  groups: [
    { subFocusId: 'sf_seed_list', name: 'Seed list', requests: [ids[0], ids[1]] },
    { subFocusId: 'sf_watering', name: 'Watering reminders', requests: [ids[2], ids[3]] },
  ],
  ungrouped: ids.slice(4),
  ownerMoves: [],
})

type Row = Record<string, string | number | null>
/** Rows as the work-state table holds them, in write order, one millisecond apart. */
const entriesOf = (rows: Row[]) => rows.map((fields, index) => ({ id: index + 1, project: 'home', list_name: MY_FOCUS_LIST, fields: JSON.stringify(fields), created_at_epoch: 1_760_000_000_000 + index }))
const reduce = (rows: Row[]) => buildFocusAreas(reduceWorkState(entriesOf(rows), { currentProject: 'home', localDeviceId: '' }))

/**
 * Writes `area` and places `requestIds` in it as the router does, then the sub focus answer and the owner's moves, and
 * reads them all back. Each request's anchor is its own id, as a request with no group's anchor is its conversation.
 */
function served(area: Parameters<typeof focusAreaRowFields>[0], requestIds: string[], answer: SubFocusAnswer) {
  const rows: Row[] = [focusAreaRowFields(area)]
  rows.push(...routerFocusRows({
    placements: requestIds.map((id) => ({ anchorConversationKey: id, focusAreaId: area.focusAreaId, basis: 'sample', evidence: 'sample' })), proposals: [],
  }, reduce(rows).focusAreas).rows)
  rows.push(...routerFocusRows({
    placements: [], proposals: [],
    subFocus: {
      focusAreaId: answer.focusAreaId, passHash: null,
      newSubFocuses: answer.groups.map((group) => ({ name: group.name, anchorConversationKeys: group.requests })),
      placements: answer.ungrouped.map((id) => ({ anchorConversationKey: id, subFocusId: null })),
    },
  }, reduce(rows).focusAreas, undefined, (index) => answer.groups[index].subFocusId).rows)
  for (const move of answer.ownerMoves) rows.push(subFocusPlacementRowFields({ anchorConversationKey: move.request, subFocusId: move.subFocusId, focusAreaId: answer.focusAreaId, placedBy: 'owner' }))
  const focus = reduce(rows)
  const placed = placeRequests(requestIds.map((id) => ({ id, groupId: null, conversationKey: id })), focus.focusAreaIdByAnchor, null, focus.subFocusIdByAnchor)
  return { focus, placed, area: focus.focusAreas.find((entry) => entry.focusAreaId === area.focusAreaId)! }
}

/** The sample's sub focuses: the Viewer area's, and each sample request's sub focus. */
export function sampleSubFocuses(): { subFocuses: Record<string, WorkViewSubFocus[]>; subFocusOfRequest: Record<string, string> } {
  const viewer = SAMPLE_FOCUS_AREAS.find((area) => area.id === SAMPLE_ANSWER.focusAreaId)!
  const requestIds = SAMPLE_REQUESTS.filter((request) => request.focusAreaId === viewer.id).map((request) => request.id)
  const { area, placed } = served({
    focusAreaId: viewer.id, name: viewer.name, goal: viewer.goal, status: viewer.status, priorityRank: viewer.priorityRank, ownerRank: viewer.ownerRank,
    source: viewer.source, mergedInto: viewer.mergedInto, evidence: viewer.evidence, decidedBy: 'owner',
  }, requestIds, SAMPLE_ANSWER)
  return {
    subFocuses: { [viewer.id]: area.subFocuses },
    subFocusOfRequest: Object.fromEntries(placed.flatMap((request) => request.subFocusId ? [[request.id, request.subFocusId]] : [])),
  }
}

/** The trimmed fixture with its area and each request's placement, as a worker serving that answer would send them. */
export function trimmedSnapshotWithSubFocuses(snapshot: WorkViewSnapshot): WorkViewSnapshot {
  const ids = snapshot.humanRequests.map((request) => request.id)
  const { focus, placed } = served({
    ...FIXTURE_AREA, status: 'kept', priorityRank: 1, ownerRank: 1, source: 'owner-goals', mergedInto: null, evidence: null, decidedBy: 'owner',
  }, ids, FIXTURE_ANSWER(ids))
  const humanRequests: WorkViewHumanRequest[] = snapshot.humanRequests.map((request, index) => {
    const { anchorConversationKey, focusAreaId, subFocusId } = placed[index]
    const { anchorConversationKey: _anchor, focusAreaId: _area, subFocusId: _subFocus, ...rest } = request
    return { ...rest, anchorConversationKey, ...(focusAreaId ? { focusAreaId } : {}), ...(subFocusId ? { subFocusId } : {}) }
  })
  const focusAreas: SavedFocusArea[] = focus.focusAreas
  return { ...snapshot, humanRequests, focusAreas }
}

/** The generated module's text. */
export function sampleSubFocusesModule(): string {
  const { subFocuses, subFocusOfRequest } = sampleSubFocuses()
  return `// Generated by scripts/sample-sub-focuses.ts: the sample's sub focuses, made by the worker's own row builders and reducer
// (my-focus.mjs) from the router answer written there. Do not edit; change the answer and run
// \`bun scripts/sample-sub-focuses.ts --write\` in src/ui/my-work.
import type { WorkViewSubFocus } from './live/work-view-api-types.ts'

/** Each sample area's live sub focuses, as the worker serves them (WorkViewFocusArea.subFocuses). */
export const SAMPLE_SUB_FOCUSES: Record<string, WorkViewSubFocus[]> = ${JSON.stringify(subFocuses, null, 2)}

/** Each sample request's sub focus, as the worker serves it (WorkViewHumanRequest.subFocusId); a request with none is left out. */
export const SAMPLE_SUB_FOCUS_OF_REQUEST: Record<string, string> = ${JSON.stringify(subFocusOfRequest, null, 2)}
`
}

export const GENERATED_MODULE = fileURLToPath(new URL('../src/data-sub-focuses.ts', import.meta.url))
export const TRIMMED_FIXTURE = fileURLToPath(new URL('../test-fixtures/work-view-snapshot.trimmed.json', import.meta.url))

if (import.meta.main) {
  const module = sampleSubFocusesModule()
  const fixture = `${JSON.stringify(trimmedSnapshotWithSubFocuses(JSON.parse(readFileSync(TRIMMED_FIXTURE, 'utf8'))), null, 2)}\n`
  if (process.argv.includes('--write')) {
    writeFileSync(GENERATED_MODULE, module)
    writeFileSync(TRIMMED_FIXTURE, fixture)
  } else {
    process.stdout.write(module)
  }
}