dsh-compaction-weave 0.0.0-stage → 0.3.4

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.
Files changed (92) hide show
  1. package/CHANGELOG.md +103 -0
  2. package/LICENSE +21 -0
  3. package/README.en.md +145 -0
  4. package/README.md +148 -2
  5. package/cordis.patch.yml +19 -0
  6. package/docs/architecture.md +84 -0
  7. package/docs/benchmarks/published-0.3.2-rc.1-tests.json +576 -0
  8. package/docs/benchmarks/rc2-documentation.json +41 -0
  9. package/docs/benchmarks/rc2-linux-tests.json +590 -0
  10. package/docs/benchmarks/rc2-live-and-cost.json +746 -0
  11. package/docs/benchmarks/rc3-host-compatibility.json +2314 -0
  12. package/docs/benchmarks/rc4-tests.json +617 -0
  13. package/docs/benchmarks/review35-compression.json +117 -0
  14. package/docs/benchmarks/v0.3.2-live-four-way.json +3495 -0
  15. package/docs/benchmarks/v0.3.2-tests.json +590 -0
  16. package/docs/configuration.md +131 -0
  17. package/docs/development.md +95 -0
  18. package/docs/en/architecture.md +75 -0
  19. package/docs/en/configuration.md +131 -0
  20. package/docs/en/development.md +92 -0
  21. package/docs/en/feedback.md +47 -0
  22. package/docs/en/principles.md +235 -0
  23. package/docs/en/testing.md +368 -0
  24. package/docs/en/troubleshooting.md +60 -0
  25. package/docs/feedback.md +47 -0
  26. package/docs/images/principles-context.en.svg +77 -0
  27. package/docs/images/principles-context.zh.svg +77 -0
  28. package/docs/images/principles-pipeline.en.svg +93 -0
  29. package/docs/images/principles-pipeline.zh.svg +93 -0
  30. package/docs/images/principles-recovery.en.svg +46 -0
  31. package/docs/images/principles-recovery.zh.svg +46 -0
  32. package/docs/principles.md +235 -0
  33. package/docs/test-results.json +634 -0
  34. package/docs/testing.md +358 -0
  35. package/docs/troubleshooting.md +60 -0
  36. package/lib/abort.js +13 -0
  37. package/lib/action-state.js +60 -0
  38. package/lib/atomic-replace.js +18 -0
  39. package/lib/attachments.js +76 -0
  40. package/lib/audit-reject.js +161 -0
  41. package/lib/audit.js +939 -0
  42. package/lib/cache-policy.js +71 -0
  43. package/lib/client.js +696 -0
  44. package/lib/command-compact.js +38 -0
  45. package/lib/command-heartbeat.js +61 -0
  46. package/lib/config.js +596 -0
  47. package/lib/constraint-scope.js +37 -0
  48. package/lib/continuation-budget.js +69 -0
  49. package/lib/continuation-pending.js +57 -0
  50. package/lib/continuation-progress.js +46 -0
  51. package/lib/continuation.js +287 -0
  52. package/lib/engine-entry.js +103 -0
  53. package/lib/engine.js +2485 -0
  54. package/lib/failure-diagnostics.js +220 -0
  55. package/lib/history-source.js +74 -0
  56. package/lib/history.js +712 -0
  57. package/lib/idle-compaction.js +64 -0
  58. package/lib/index.js +611 -0
  59. package/lib/instruction-source.js +163 -0
  60. package/lib/journal.js +164 -0
  61. package/lib/manual-error.js +179 -0
  62. package/lib/memory-policy.js +383 -0
  63. package/lib/meter.js +387 -0
  64. package/lib/observation-mask.js +390 -0
  65. package/lib/proactive.js +108 -0
  66. package/lib/progress-facts.js +347 -0
  67. package/lib/progress-state.js +30 -0
  68. package/lib/prompt.js +194 -0
  69. package/lib/reconstruct.js +72 -0
  70. package/lib/reference-input.js +107 -0
  71. package/lib/region.js +972 -0
  72. package/lib/source-provenance.js +58 -0
  73. package/lib/source-recovery.js +22 -0
  74. package/lib/source-refs.js +137 -0
  75. package/lib/state.js +46 -0
  76. package/lib/storage-path.js +42 -0
  77. package/lib/summary-budget.js +187 -0
  78. package/lib/summary-config.js +308 -0
  79. package/lib/summary-progress.js +56 -0
  80. package/lib/summary-retry.js +27 -0
  81. package/lib/summary-timeout.js +68 -0
  82. package/lib/summary.js +476 -0
  83. package/lib/tools.js +341 -0
  84. package/lib/transaction-host.js +252 -0
  85. package/lib/transaction.js +285 -0
  86. package/lib/validate.js +503 -0
  87. package/lib/version.js +3 -0
  88. package/lib/window.js +120 -0
  89. package/lib/work-report.js +55 -0
  90. package/locale/en.json +6 -0
  91. package/locale/zh.json +6 -0
  92. package/package.json +71 -4
@@ -0,0 +1,71 @@
1
+ import {collectWorkReport, renderWorkReport} from './work-report.js';
2
+ // Keep committed working notes byte-for-byte and compact only subsequent old
3
+ // work, provided it can free enough space. Real capacity and revised requirements
4
+ // always take priority over cache reuse. No provider cache hit is assumed here.
5
+ import {selectCompactableRange,toolPairingBalancedBefore,collectInstructionMessages,selectSessionMemory} from './region.js';
6
+ import {approxTokens,blocksToText} from './meter.js';
7
+ import {planContinuationMemory,residentConstraints} from './continuation.js';
8
+
9
+ export function selectCachePreservingRange(session, measurement, retainTokens, policy, targetTokens, {continuation=false} = {}) {
10
+ if(policy.cachePreservePrefix===false)return null;
11
+ const nodes=session.surface?.nodes??[];
12
+ const checkpoints=nodes.map((seq,index)=>({seq,index,event:session.eventAt(seq)})).filter(n=>n.event?.data?.source?.kind==='compact-checkpoint');
13
+ if(!checkpoints.length||checkpoints.length>=(policy.cacheMaxCheckpoints??4))return null;
14
+ const base=selectCompactableRange(session,measurement,retainTokens);
15
+ if(!base)return null;
16
+ const firstIndex=checkpoints.at(-1).index+1;
17
+ const endIndex=nodes.indexOf(base.end);
18
+ if(firstIndex>endIndex||!toolPairingBalancedBefore(session,nodes[firstIndex]))return null;
19
+ const cost=new Map(measurement.pricedBySeq??[]);
20
+ for(const item of measurement.nodes??[])if(typeof item==='object'&&Number.isFinite(item.tokens??item.heuristicTokens))cost.set(item.seq,item.tokens??item.heuristicTokens);
21
+ const tokens=seq=>cost.get(seq)??approxTokens(blocksToText(session.deriveEventMessage(session.eventAt(seq))?.content));
22
+ const latestReport=collectWorkReport(session,nodes.slice(firstIndex,endIndex+1));
23
+ // This interval follows the LAST checkpoint, so any report is a direct
24
+ // assistant event. It costs nothing when it remains in the untouched prefix.
25
+ const reportIndex=latestReport?nodes.indexOf(latestReport.seq,firstIndex):-1;
26
+ const reportTokens=latestReport?approxTokens(renderWorkReport(latestReport)):0;
27
+ const baseCheckpoint=(policy.maxTokens??8192)+(policy.preserveRecentUserTokens??4096);
28
+ const savingNeeded=Math.max(policy.cacheMinSavingsTokens??4096,Number.isFinite(targetTokens)?measurement.totalTokens-targetTokens:measurement.totalTokens*(policy.minimumReductionRatio??0.2));
29
+ let selection;
30
+ const globalSelection=()=>selection??=(selectSessionMemory(session,nodes,{maxMessages:Infinity,maxTokens:Infinity}));
31
+ const requirements=continuation?new Map(residentConstraints(globalSelection().active,session).map(m=>[m.seq,m])):null;
32
+ const included=[];
33
+ let available=0,footprint=null,footprintDirty=true,latestAllowed=endIndex,conflictsChecked=false;
34
+ for(let index=endIndex;index>=firstIndex;index--){
35
+ available+=tokens(nodes[index]);
36
+ if(requirements?.has(nodes[index])){included.push(requirements.get(nodes[index]));footprintDirty=true;}
37
+ if(index===reportIndex)footprintDirty=true;
38
+ const report=index<=reportIndex?latestReport:null;
39
+ let expectedCheckpoint=baseCheckpoint+(report?reportTokens:0);
40
+ if(index>latestAllowed||available-expectedCheckpoint<savingNeeded||!toolPairingBalancedBefore(session,nodes[index]))continue;
41
+ if(continuation){
42
+ try{
43
+ if(footprintDirty){footprint=planContinuationMemory([...included].reverse(),policy,{workReport:report});footprintDirty=false;}
44
+ expectedCheckpoint=Math.max(expectedCheckpoint,footprint.reservedTokens);
45
+ }catch(error){
46
+ // Earlier starts only add active source requirements (and possibly the
47
+ // latest report), so an already oversized envelope cannot shrink again.
48
+ if(error.code==='CONTINUATION_REQUIREMENTS_TOO_LARGE')return null;
49
+ throw error;
50
+ }
51
+ if(available-expectedCheckpoint<savingNeeded)continue;
52
+ }
53
+ if(!conflictsChecked){
54
+ const prefixSources=new Set(collectInstructionMessages(session,nodes.slice(0,index)).map(m=>m.seq));
55
+ const retired=globalSelection().decisions.filter(d=>['cancelled','superseded'].includes(d.reason)&&prefixSources.has(d.seq));
56
+ const positions=new Map(nodes.map((seq,position)=>[seq,position]));
57
+ for(const decision of retired){
58
+ const position=positions.get(decision.seq);
59
+ // A retired source carried by an older checkpoint requires a merge.
60
+ if(position===undefined||position<firstIndex)return null;
61
+ latestAllowed=Math.min(latestAllowed,position);
62
+ }
63
+ conflictsChecked=true;
64
+ // A revision within new work only needs to include its old source; the
65
+ // stable checkpoint can stay. Shrinking the prefix adds no new conflict.
66
+ if(index>latestAllowed)continue;
67
+ }
68
+ return {start:nodes[index],end:base.end};
69
+ }
70
+ return null;
71
+ }