@niracler/encore 0.3.0 → 0.4.1

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/src/timeline.mjs CHANGED
@@ -17,6 +17,7 @@ function resolveWindowCapacity(tool = 'codex', model = '', contextWindow = 0) {
17
17
  return { tokens: contextWindow, label }
18
18
  }
19
19
  const t = String(tool || '').toLowerCase()
20
+ if (t.startsWith('antigravity')) return { tokens: null, label: null }
20
21
  const m = String(model || '').toLowerCase()
21
22
  if (
22
23
  t.startsWith('claude') ||
@@ -449,7 +450,7 @@ function assignFramesToGroups(rawGroups, frames = [], t) {
449
450
  }
450
451
 
451
452
  function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames = [], sessionStart = '', sessionMeta = {}, t) {
452
- const windowSpec = resolveWindowCapacity(sessionTool, sessionModel, sessionMeta.contextWindow)
453
+ let windowSpec = resolveWindowCapacity(sessionTool, sessionModel, sessionMeta.contextWindow)
453
454
  const initCtx = sessionMeta.initialContext || {}
454
455
  const rawGroups = []
455
456
  let work = []
@@ -480,25 +481,32 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
480
481
  id: rec.id,
481
482
  tag: rec.tag || '',
482
483
  title: rec.title,
483
- meta: rec.meta,
484
+ meta: observedOnly && rec.stepIndex >= 0 ? undefined : rec.meta,
484
485
  payload: rec.payload,
485
486
  ...(rec.images?.length ? { images: rec.images } : {}),
486
487
  }
487
488
  return entry
488
489
  }
489
490
 
490
- const sysRecords = (initCtx.sys?.length ? initCtx.sys : defaultSystemPrompts(sessionTool)).map(r => registerRecord({ ...r, stepIndex: -1 }))
491
- const schemaRecords = (initCtx.schema?.length ? initCtx.schema : defaultToolSchemas(sessionTool)).map(r => registerRecord({ ...r, stepIndex: -1 }))
491
+ // Antigravity metadata may contain only part of the mounted context.
492
+ const observedOnly = sessionTool.startsWith('antigravity')
493
+ function initialRecords(key, defaults) {
494
+ const records = initCtx[key] || []
495
+ const entries = records.length || observedOnly ? records : defaults(sessionTool, sessionStart)
496
+ return entries.map(record => registerRecord({ ...record, stepIndex: -1 }))
497
+ }
498
+ const sysRecords = initialRecords('sys', defaultSystemPrompts)
499
+ const schemaRecords = initialRecords('schema', defaultToolSchemas)
492
500
  const schemaIdSet = new Set(schemaRecords.map(r => r.id))
493
501
  for (const r of schemaRecords) if (r.title) schemaIdSet.add(`schema-dyn-${String(r.title).replace(/[^a-zA-Z0-9_-]/g, '_')}`)
494
502
  const userRecords = []
495
503
  let toolRecords = []
496
- const skillRecords = (initCtx.skill?.length ? initCtx.skill : defaultSkillCatalog()).map(r => registerRecord({ ...r, stepIndex: -1 }))
504
+ const skillRecords = initialRecords('skill', defaultSkillCatalog)
497
505
  const knownSkillNames = new Set(skillRecords.map(r => r.title).filter(Boolean))
498
506
  const msgRecords = []
499
- const memRecords = (initCtx.mem?.length ? initCtx.mem : defaultInjectedContext(sessionTool, sessionStart)).map(r => registerRecord({ ...r, stepIndex: -1 }))
507
+ const memRecords = initialRecords('mem', defaultInjectedContext)
500
508
 
501
- const sumTokEst = list => list.reduce((acc, r) => acc + (r.tokEst || 600), 0)
509
+ const sumTokEst = list => list.reduce((acc, r) => acc + (r.tokEst ?? (observedOnly ? 0 : 600)), 0)
502
510
  const sysTokens = sumTokEst(sysRecords)
503
511
  let schemaTokens = sumTokEst(schemaRecords)
504
512
  let userTokens = 0
@@ -511,7 +519,7 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
511
519
  let uncalibratedDelta = 0
512
520
  const allTools = []
513
521
  const allSkills = []
514
- const allMemories = ['AGENTS.md']
522
+ const allMemories = observedOnly ? [] : ['AGENTS.md']
515
523
  const allChanges = []
516
524
  let latestUserPrompt = ''
517
525
  let toolSeq = 0
@@ -528,7 +536,7 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
528
536
  let dUser = userTokens
529
537
  let dMsg = msgTokens
530
538
  let dTool = toolTokens
531
- if (lastRealTotal > 0) {
539
+ if (lastRealTotal > 0 && !observedOnly) {
532
540
  const target = lastRealTotal + uncalibratedDelta
533
541
  const fixed = dSys + dSchema + dMem + dSkill
534
542
  const dyn = dUser + dMsg + dTool
@@ -551,6 +559,11 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
551
559
  return { sys: dSys, schema: dSchema, user: dUser, mem: dMem, skill: dSkill, msg: dMsg, tool: dTool }
552
560
  }
553
561
 
562
+ function reportedBuckets(disp, recorded) {
563
+ if (!observedOnly) return disp
564
+ return Object.fromEntries(Object.keys(disp).map(key => [key, recorded?.[key] ?? null]))
565
+ }
566
+
554
567
  const assembleCategoryList = (stepIdx, countsBefore, toksBefore, disp) => {
555
568
  const curTotal = disp.sys + disp.schema + disp.user + disp.mem + disp.msg + disp.tool + disp.skill
556
569
  const kindPct = value => Math.round((value / Math.max(1, curTotal)) * 100)
@@ -575,10 +588,10 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
575
588
  color,
576
589
  itemCount: records.length,
577
590
  count: t('itemCount', { n: records.length }),
578
- tok: `≈${(curTok / 1000).toFixed(1)}k`,
579
- pct: `${kindPct(curTok)}%`,
591
+ tok: curTok == null ? t('notRecorded') : `≈${(curTok / 1000).toFixed(1)}k`,
592
+ pct: observedOnly || curTok == null ? '' : `${kindPct(curTok)}%`,
580
593
  delta: stepIdx >= 0 ? fmtCountDelta(records.length, prevCount) : '',
581
- tokDelta: stepIdx >= 0 ? fmtTokDelta(curTok, prevTok) : '',
594
+ tokDelta: stepIdx >= 0 && !observedOnly ? fmtTokDelta(curTok, prevTok) : '',
582
595
  itemIds,
583
596
  newIds,
584
597
  items: itemIds.map(rid => ({ ...contextRecords[rid], isNew: newSet.has(rid) })),
@@ -597,9 +610,15 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
597
610
 
598
611
  let prevDisp = computeDisplayBuckets()
599
612
  const initTotal = prevDisp.sys + prevDisp.schema + prevDisp.user + prevDisp.mem + prevDisp.skill + prevDisp.msg + prevDisp.tool
600
- const initCap = Math.max(windowSpec.tokens, Math.ceil(initTotal * 1.25))
601
- const initCapPct = val => (val <= 0 ? 0 : Number((Math.max(0.4, (val / initCap) * 100)).toFixed(2)))
613
+ let observedUsage = initCtx.estimatedTokens ?? null
614
+ const openingTokens = observedOnly ? observedUsage : initTotal
615
+ const initCap = observedOnly ? windowSpec.tokens : Math.max(windowSpec.tokens, Math.ceil(initTotal * 1.25))
616
+ const initCapPct = val => (!initCap || val <= 0 ? 0 : Number((Math.max(0.4, (val / initCap) * 100)).toFixed(2)))
602
617
  const initKindPct = val => Math.round((val / Math.max(1, initTotal)) * 100)
618
+ const openingByKind = Object.fromEntries(Object.entries(initCtx)
619
+ .filter(([, records]) => Array.isArray(records) && records.some(record => record.tokEst != null))
620
+ .map(([key, records]) => [key, sumTokEst(records)]))
621
+ const initialBuckets = reportedBuckets(prevDisp, openingByKind)
603
622
  const zeroCounts = {
604
623
  sys: sysRecords.length,
605
624
  schema: schemaRecords.length,
@@ -610,12 +629,13 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
610
629
  tool: 0,
611
630
  }
612
631
  const initialSnapshot = {
613
- pct: Number(Math.min(99, (initTotal / initCap) * 100).toFixed(1)),
632
+ pct: initCap && openingTokens != null ? Number(((openingTokens / initCap) * 100).toFixed(1)) : null,
614
633
  windowTokens: windowSpec.tokens,
615
634
  windowLabel: windowSpec.label,
616
- totalTokens: initTotal,
635
+ totalTokens: openingTokens,
636
+ ...(observedOnly && openingTokens != null && { estimated: true }),
617
637
  deltaTokens: 0,
618
- segments: {
638
+ segments: observedOnly ? { used: initCap && openingTokens != null ? Math.min(openingTokens / initCap * 100, 100) : 0 } : {
619
639
  sys: initCapPct(prevDisp.sys),
620
640
  schema: initCapPct(prevDisp.schema),
621
641
  user: 0,
@@ -624,7 +644,7 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
624
644
  msg: 0,
625
645
  tool: 0,
626
646
  },
627
- pctByKind: {
647
+ pctByKind: observedOnly ? {} : {
628
648
  sys: initKindPct(prevDisp.sys),
629
649
  schema: initKindPct(prevDisp.schema),
630
650
  user: 0,
@@ -633,8 +653,8 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
633
653
  msg: 0,
634
654
  tool: 0,
635
655
  },
636
- tokensByKind: { ...prevDisp },
637
- categories: assembleCategoryList(-1, zeroCounts, prevDisp, prevDisp),
656
+ tokensByKind: initialBuckets,
657
+ categories: assembleCategoryList(-1, zeroCounts, prevDisp, initialBuckets),
638
658
  }
639
659
 
640
660
  const steps = rawGroups.map((group, index) => {
@@ -690,7 +710,7 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
690
710
  payload: String(first.text || ''),
691
711
  ...(first.images?.length ? { images: first.images } : {}),
692
712
  }))
693
- for (const skill of extractSkillNames(first, knownSkillNames)) {
713
+ for (const skill of observedOnly ? [] : extractSkillNames(first, knownSkillNames)) {
694
714
  if (!allSkills.includes(skill)) {
695
715
  allSkills.push(skill)
696
716
  const skTok = 1800
@@ -718,59 +738,68 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
718
738
  } else if (group.groupType === 'compact') {
719
739
  compactSeq++
720
740
  const cItem = first
721
- const preTokens = cItem.preTokens || prevTotal
722
- const archivedTools = toolRecords.length
723
- const postTokens = cItem.postTokens || Math.min(preTokens, Math.max(24000, Math.round(preTokens * 0.28)))
724
- const droppedTokens = cItem.droppedTokens || Math.max(0, preTokens - postTokens)
725
- const summaryText = String(cItem.text || cItem.summary || 'Context Compaction Summary').trim()
726
- const sumTok = Math.max(1200, Math.round(summaryText.length * 0.35))
727
- const docId = `compact-doc-${index}`
728
- const recId = `compact-summary-${compactSeq}`
741
+ if (observedOnly) {
742
+ compactInfo = { seq: compactSeq, title: t('compactTitle', { seq: compactSeq }) }
743
+ if (cItem.text) {
744
+ compactInfo.content = cItem.text
745
+ compactInfo.docId = `compact-doc-${index}`
746
+ memRecords.push(registerRecord({ id: `compact-summary-${compactSeq}`, tag: 'Checkpoint',
747
+ title: compactInfo.title, payload: cItem.text, stepIndex: index }))
748
+ }
749
+ } else {
750
+ const preTokens = cItem.preTokens || prevTotal
751
+ const archivedTools = toolRecords.length
752
+ const postTokens = cItem.postTokens || Math.min(preTokens, Math.max(24000, Math.round(preTokens * 0.28)))
753
+ const droppedTokens = cItem.droppedTokens || Math.max(0, preTokens - postTokens)
754
+ const summaryText = String(cItem.text || cItem.summary || 'Context Compaction Summary').trim()
755
+ const sumTok = Math.max(1200, Math.round(summaryText.length * 0.35))
756
+ const docId = `compact-doc-${index}`
757
+ const recId = `compact-summary-${compactSeq}`
729
758
 
730
- toolRecords = []
731
- toolTokens = 0
732
- userTokens = Math.round(userTokens * 0.35)
733
- msgTokens = Math.round(msgTokens * 0.25)
734
- memTokens += sumTok
735
- lastRealTotal = postTokens
736
- uncalibratedDelta = 0
737
- const rawFixed = sysTokens + schemaTokens + memTokens + skillTokens
738
- if (postTokens > 0 && rawFixed > postTokens * 0.85) {
739
- mountScale = (postTokens * 0.72) / rawFixed
740
- }
759
+ toolRecords = []
760
+ toolTokens = 0
761
+ userTokens = Math.round(userTokens * 0.35)
762
+ msgTokens = Math.round(msgTokens * 0.25)
763
+ memTokens += sumTok
764
+ lastRealTotal = postTokens
765
+ uncalibratedDelta = 0
766
+ const rawFixed = sysTokens + schemaTokens + memTokens + skillTokens
767
+ if (postTokens > 0 && rawFixed > postTokens * 0.85) {
768
+ mountScale = (postTokens * 0.72) / rawFixed
769
+ }
741
770
 
742
- memRecords.push(registerRecord({
743
- id: recId,
744
- stepIndex: index,
745
- tokEst: sumTok,
746
- tag: 'Compact',
747
- title: `${t('compactTitle', { seq: cItem.seq || compactSeq })} (${(preTokens / 1000).toFixed(1)}k → ${(postTokens / 1000).toFixed(1)}k)`,
748
- meta: `-${(droppedTokens / 1000).toFixed(1)}k tok · ${stepTime}s`,
749
- payload: summaryText,
750
- }))
771
+ memRecords.push(registerRecord({
772
+ id: recId,
773
+ stepIndex: index,
774
+ tokEst: sumTok,
775
+ tag: 'Compact',
776
+ title: `${t('compactTitle', { seq: cItem.seq || compactSeq })} (${(preTokens / 1000).toFixed(1)}k → ${(postTokens / 1000).toFixed(1)}k)`,
777
+ meta: `-${(droppedTokens / 1000).toFixed(1)}k tok · ${stepTime}s`,
778
+ payload: summaryText,
779
+ }))
751
780
 
752
- compactInfo = {
753
- seq: cItem.seq || compactSeq,
754
- trigger: cItem.trigger || 'auto',
755
- preTokens,
756
- postTokens,
757
- droppedTokens,
758
- archivedTools,
759
- docId,
760
- recId,
761
- title: cItem.summary || t('compactTitle', { seq: cItem.seq || compactSeq }),
762
- content: summaryText,
781
+ compactInfo = {
782
+ seq: cItem.seq || compactSeq,
783
+ trigger: cItem.trigger || 'auto',
784
+ preTokens,
785
+ postTokens,
786
+ droppedTokens,
787
+ archivedTools,
788
+ docId,
789
+ recId,
790
+ title: cItem.summary || t('compactTitle', { seq: cItem.seq || compactSeq }),
791
+ content: summaryText,
792
+ }
793
+ inBrief = `${(preTokens / 1000).toFixed(1)}k → ${(postTokens / 1000).toFixed(1)}k`
794
+ responseBrief = t('archivedTools', { n: archivedTools, tokens: (droppedTokens / 1000).toFixed(1) })
763
795
  }
764
-
765
796
  kind = 'compact'
766
797
  primaryCat = 'mem'
767
798
  speedLabel = '0.8×'
768
799
  dwellMs = 2100
769
800
  spotlight = 'context-compact'
770
801
  detailTitle = t('compactTitle', { seq: compactInfo.seq })
771
- detailSnippet = summaryText.slice(0, 260)
772
- inBrief = `${(preTokens / 1000).toFixed(1)}k → ${(postTokens / 1000).toFixed(1)}k`
773
- responseBrief = t('archivedTools', { n: archivedTools, tokens: (droppedTokens / 1000).toFixed(1) })
802
+ detailSnippet = (compactInfo.content || '').slice(0, 260)
774
803
  } else if (group.groupType === 'assistant') {
775
804
  msgSeq++
776
805
  const cleanReply = (first.text || '').replace(/\s+/g, ' ').trim()
@@ -811,7 +840,7 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
811
840
  allTools.push(...extracted.tools)
812
841
  allChanges.push(...extracted.changes)
813
842
 
814
- for (const memName of extracted.memories) {
843
+ for (const memName of observedOnly ? [] : extracted.memories) {
815
844
  if (!allMemories.includes(memName)) {
816
845
  allMemories.push(memName)
817
846
  const memTok = 1100
@@ -848,14 +877,14 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
848
877
 
849
878
  const rawMontage = []
850
879
  for (const item of toolItems) {
851
- const dynSchema = resolveDynamicSchema(item, index, schemaIdSet)
880
+ const dynSchema = !observedOnly && resolveDynamicSchema(item, index, schemaIdSet)
852
881
  if (dynSchema) {
853
882
  schemaIdSet.add(dynSchema.id)
854
883
  schemaTokens += dynSchema.tokEst
855
884
  stepEstimatedDelta += dynSchema.tokEst
856
885
  schemaRecords.push(registerRecord(dynSchema))
857
886
  }
858
- for (const skill of extractSkillNames(item, knownSkillNames)) {
887
+ for (const skill of observedOnly ? [] : extractSkillNames(item, knownSkillNames)) {
859
888
  if (!allSkills.includes(skill)) {
860
889
  allSkills.push(skill)
861
890
  const skTok = 1800
@@ -990,13 +1019,23 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
990
1019
 
991
1020
  const disp = computeDisplayBuckets()
992
1021
  prevDisp = disp
993
- const totalTokens = disp.sys + disp.schema + disp.user + disp.mem + disp.msg + disp.tool + disp.skill
994
- const deltaTokens = totalTokens - prevTotal
995
- const capacity = Math.max(windowSpec.tokens, Math.ceil(totalTokens * 1.25))
996
- const pct = Number(Math.min(99, (totalTokens / capacity) * 100).toFixed(1))
997
- const capPct = value => (value <= 0 ? 0 : Number((Math.max(0.4, (value / capacity) * 100)).toFixed(2)))
998
- const kindPct = value => Math.round((value / Math.max(1, totalTokens)) * 100)
999
- const categories = assembleCategoryList(index, prevCounts, prevToks, disp)
1022
+ const bucketTotal = disp.sys + disp.schema + disp.user + disp.mem + disp.msg + disp.tool + disp.skill
1023
+ const previousUsage = observedOnly ? observedUsage : prevTotal
1024
+ const request = observedOnly && group.items.findLast(item => item.contextTokens != null || item.contextTokenEstimates || item.contextWindow != null)
1025
+ const sample = request?.contextTokens
1026
+ if (observedOnly) {
1027
+ const limit = group.items.findLast(item => item.contextWindow != null)?.contextWindow
1028
+ if (limit != null) windowSpec = resolveWindowCapacity(sessionTool, sessionModel, limit)
1029
+ observedUsage = sample ?? null
1030
+ }
1031
+ const totalTokens = observedOnly ? observedUsage : bucketTotal
1032
+ const deltaTokens = totalTokens == null || previousUsage == null ? null : totalTokens - previousUsage
1033
+ const capacity = observedOnly ? windowSpec.tokens : Math.max(windowSpec.tokens, Math.ceil(totalTokens * 1.25))
1034
+ const pct = capacity && totalTokens != null ? Number(((totalTokens / capacity) * 100).toFixed(1)) : null
1035
+ const capPct = value => (!capacity || value <= 0 ? 0 : Number((Math.max(0.4, (value / capacity) * 100)).toFixed(2)))
1036
+ const kindPct = value => Math.round((value / Math.max(1, bucketTotal)) * 100)
1037
+ const buckets = reportedBuckets(disp, request?.contextTokenEstimates)
1038
+ const categories = assembleCategoryList(index, prevCounts, prevToks, buckets)
1000
1039
 
1001
1040
  return {
1002
1041
  index,
@@ -1022,8 +1061,9 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
1022
1061
  windowTokens: windowSpec.tokens,
1023
1062
  windowLabel: windowSpec.label,
1024
1063
  totalTokens,
1064
+ ...(observedOnly && totalTokens != null && { estimated: false }),
1025
1065
  deltaTokens,
1026
- segments: {
1066
+ segments: observedOnly ? { used: Math.min(pct ?? 0, 100) } : {
1027
1067
  sys: capPct(disp.sys),
1028
1068
  schema: capPct(disp.schema),
1029
1069
  user: capPct(disp.user),
@@ -1032,7 +1072,7 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
1032
1072
  skill: capPct(disp.skill),
1033
1073
  mem: capPct(disp.mem),
1034
1074
  },
1035
- pctByKind: {
1075
+ pctByKind: observedOnly ? {} : {
1036
1076
  sys: kindPct(disp.sys),
1037
1077
  schema: kindPct(disp.schema),
1038
1078
  user: kindPct(disp.user),
@@ -1041,15 +1081,7 @@ function buildSteps(items, end, sessionTool = 'codex', sessionModel = '', frames
1041
1081
  skill: kindPct(disp.skill),
1042
1082
  mem: kindPct(disp.mem),
1043
1083
  },
1044
- tokensByKind: {
1045
- sys: disp.sys,
1046
- schema: disp.schema,
1047
- user: disp.user,
1048
- msg: disp.msg,
1049
- tool: disp.tool,
1050
- skill: disp.skill,
1051
- mem: disp.mem,
1052
- },
1084
+ tokensByKind: buckets,
1053
1085
  chips: {
1054
1086
  tools: uniqueRecent(allTools, 6),
1055
1087
  skills: uniqueRecent(allSkills, 4),