sidekick-shared 0.27.6 → 0.28.0
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/CHANGELOG.md +28 -0
- package/README.md +58 -18
- package/dist/browser.d.ts +13 -2
- package/dist/browser.js +30 -2
- package/dist/claudePaths.d.ts +11 -0
- package/dist/claudePaths.js +61 -0
- package/dist/claudeProfiles.d.ts +2 -9
- package/dist/claudeProfiles.js +10 -25
- package/dist/codexPaths.d.ts +22 -0
- package/dist/codexPaths.js +67 -0
- package/dist/codexProfiles.d.ts +1 -4
- package/dist/codexProfiles.js +5 -43
- package/dist/doctor.js +2 -2
- package/dist/esm/accountChangeSignal.js +15 -0
- package/dist/esm/accountChanges.js +152 -0
- package/dist/esm/accountHealth.js +288 -0
- package/dist/esm/accountLaunch.js +145 -0
- package/dist/esm/accountLease.js +60 -0
- package/dist/esm/accountManager.js +606 -0
- package/dist/esm/accountRegistry.js +214 -0
- package/dist/esm/accountStatus.js +51 -0
- package/dist/esm/accountSwitch.js +74 -0
- package/dist/esm/accountSync.js +156 -0
- package/dist/esm/accountSyncTypes.js +4 -0
- package/dist/esm/accounts.js +1291 -0
- package/dist/esm/aggregation/EventAggregator.js +2034 -0
- package/dist/esm/aggregation/FrequencyTracker.js +69 -0
- package/dist/esm/aggregation/HeatmapTracker.js +89 -0
- package/dist/esm/aggregation/PatternExtractor.js +168 -0
- package/dist/esm/aggregation/costProvenance.js +31 -0
- package/dist/esm/aggregation/snapshot.js +161 -0
- package/dist/esm/aggregation/types.js +5 -0
- package/dist/esm/analytics/codeImpact.js +18 -0
- package/dist/esm/analytics/compactionLedger.js +37 -0
- package/dist/esm/analytics/qualityScore.js +112 -0
- package/dist/esm/autoSwitch.js +185 -0
- package/dist/esm/browser.js +34 -0
- package/dist/esm/claudeCredentialDoctor.js +239 -0
- package/dist/esm/claudeCredentialLedger.js +91 -0
- package/dist/esm/claudeCredentials.js +111 -0
- package/dist/esm/claudeLiveGuard.js +43 -0
- package/dist/esm/claudePaths.js +22 -0
- package/dist/esm/claudeProfiles.js +142 -0
- package/dist/esm/claudeRefreshLock.js +106 -0
- package/dist/esm/codexAuth.js +101 -0
- package/dist/esm/codexCredentialDoctor.js +265 -0
- package/dist/esm/codexCredentialStore.js +164 -0
- package/dist/esm/codexKeepAlive.js +112 -0
- package/dist/esm/codexPaths.js +172 -0
- package/dist/esm/codexProfiles.js +1251 -0
- package/dist/esm/codexQuota.js +564 -0
- package/dist/esm/codexQuotaWatcher.js +363 -0
- package/dist/esm/context/composer.js +71 -0
- package/dist/esm/context/sessionContext.js +546 -0
- package/dist/esm/credentialIO.js +283 -0
- package/dist/esm/credentials.js +49 -0
- package/dist/esm/doctor.js +305 -0
- package/dist/esm/ensureDefaultAccounts.js +62 -0
- package/dist/esm/errorHistory.js +56 -0
- package/dist/esm/extractors/errorTaxonomy.js +43 -0
- package/dist/esm/extractors/gatherAssets.js +32 -0
- package/dist/esm/extractors/sessionAssets.js +176 -0
- package/dist/esm/extractors/sources/claudeAssets.js +158 -0
- package/dist/esm/extractors/sources/codexAssets.js +234 -0
- package/dist/esm/extractors/tokenUsage.js +41 -0
- package/dist/esm/extractors/toolCall.js +142 -0
- package/dist/esm/failingTools.js +65 -0
- package/dist/esm/fileHead.js +27 -0
- package/dist/esm/formatters/eventHighlighter.js +332 -0
- package/dist/esm/formatters/noiseClassifier.js +209 -0
- package/dist/esm/formatters/sessionDump.js +328 -0
- package/dist/esm/formatters/toolSummary.js +226 -0
- package/dist/esm/formatting.js +82 -0
- package/dist/esm/handoffUrl.js +23 -0
- package/dist/esm/historicalStore.js +305 -0
- package/dist/esm/humanPrompt.js +88 -0
- package/dist/esm/index.js +213 -0
- package/dist/esm/keepAlive.js +189 -0
- package/dist/esm/liveCredentialGuard.js +33 -0
- package/dist/esm/modelAliases.js +51 -0
- package/dist/esm/modelCatalog.js +139 -0
- package/dist/esm/modelContext.js +280 -0
- package/dist/esm/modelInfo.js +832 -0
- package/dist/esm/multiProviderQuotaService.js +450 -0
- package/dist/esm/node.js +46 -0
- package/dist/esm/observedContextWindows.js +165 -0
- package/dist/esm/observedSessionCollector.js +939 -0
- package/dist/esm/parsers/changelogParser.js +71 -0
- package/dist/esm/parsers/codexHistory.js +89 -0
- package/dist/esm/parsers/codexParser.js +906 -0
- package/dist/esm/parsers/debugLogParser.js +126 -0
- package/dist/esm/parsers/jsonl.js +66 -0
- package/dist/esm/parsers/mcpToolName.js +28 -0
- package/dist/esm/parsers/openCodeParser.js +625 -0
- package/dist/esm/parsers/planExtractor.js +335 -0
- package/dist/esm/parsers/sessionActivityDetector.js +246 -0
- package/dist/esm/parsers/sessionPathResolver.js +713 -0
- package/dist/esm/parsers/subagentScanner.js +360 -0
- package/dist/esm/parsers/subagentStatsCache.js +45 -0
- package/dist/esm/parsers/subagentTraceParser.js +325 -0
- package/dist/esm/paths.js +134 -0
- package/dist/esm/peakHours.js +194 -0
- package/dist/esm/phrases.js +1330 -0
- package/dist/esm/pricingCatalog.js +247 -0
- package/dist/esm/processDetection.js +200 -0
- package/dist/esm/projectMigration.js +59 -0
- package/dist/esm/promptHistory.js +1221 -0
- package/dist/esm/providerFailure.js +362 -0
- package/dist/esm/providerIds.js +6 -0
- package/dist/esm/providerQuota.js +1 -0
- package/dist/esm/providerServiceStatus.js +145 -0
- package/dist/esm/providerServiceStatusTypes.js +1 -0
- package/dist/esm/providerStatus.js +117 -0
- package/dist/esm/providers/claudeCode.js +575 -0
- package/dist/esm/providers/claudeListing.js +121 -0
- package/dist/esm/providers/codex.js +1034 -0
- package/dist/esm/providers/codexDatabase.js +297 -0
- package/dist/esm/providers/codexRollouts.js +215 -0
- package/dist/esm/providers/detect.js +185 -0
- package/dist/esm/providers/diagnostics.js +83 -0
- package/dist/esm/providers/directoryListingCache.js +206 -0
- package/dist/esm/providers/factory.js +54 -0
- package/dist/esm/providers/openCode.js +2008 -0
- package/dist/esm/providers/openCodeDatabase.js +419 -0
- package/dist/esm/providers/rolloutWalker.js +252 -0
- package/dist/esm/providers/types.js +12 -0
- package/dist/esm/quota.js +182 -0
- package/dist/esm/quotaHistory.js +364 -0
- package/dist/esm/quotaPoller.js +230 -0
- package/dist/esm/quotaPresentation.js +97 -0
- package/dist/esm/quotaResolver.js +231 -0
- package/dist/esm/quotaSnapshots.js +130 -0
- package/dist/esm/quotaThresholds.js +85 -0
- package/dist/esm/readers/decisions.js +24 -0
- package/dist/esm/readers/handoff.js +20 -0
- package/dist/esm/readers/helpers.js +27 -0
- package/dist/esm/readers/history.js +9 -0
- package/dist/esm/readers/notes.js +48 -0
- package/dist/esm/readers/plans.js +225 -0
- package/dist/esm/readers/tasks.js +19 -0
- package/dist/esm/report/htmlHelpers.js +280 -0
- package/dist/esm/report/htmlReportGenerator.js +704 -0
- package/dist/esm/report/index.js +7 -0
- package/dist/esm/report/logo.js +2 -0
- package/dist/esm/report/openBrowser.js +19 -0
- package/dist/esm/report/sessionReportInputs.js +33 -0
- package/dist/esm/report/transcriptParser.js +83 -0
- package/dist/esm/report/types.js +4 -0
- package/dist/esm/schemas/accountManager.js +204 -0
- package/dist/esm/schemas/accountStatus.js +25 -0
- package/dist/esm/schemas/assistantTurn.js +74 -0
- package/dist/esm/schemas/index.js +25 -0
- package/dist/esm/schemas/observedSessionV1.js +77 -0
- package/dist/esm/schemas/providerFailure.js +37 -0
- package/dist/esm/schemas/quota.js +132 -0
- package/dist/esm/schemas/quotaHistory.js +40 -0
- package/dist/esm/schemas/sessionEvent.js +216 -0
- package/dist/esm/schemas/stateFile.js +75 -0
- package/dist/esm/schemas/tokenEstimation.js +7 -0
- package/dist/esm/schemas/transcript.js +86 -0
- package/dist/esm/schemas/usageNormalization.js +38 -0
- package/dist/esm/search/advancedFilter.js +197 -0
- package/dist/esm/search/sessionSearch.js +66 -0
- package/dist/esm/sessionFingerprint.js +40 -0
- package/dist/esm/sessionMonitor.js +198 -0
- package/dist/esm/sessionPreviews.js +522 -0
- package/dist/esm/sessionPromptHistory.js +254 -0
- package/dist/esm/sessionPromptSignals.js +364 -0
- package/dist/esm/sessionStats.js +160 -0
- package/dist/esm/sessionTokenTotals.js +98 -0
- package/dist/esm/sessionTranscripts.js +49 -0
- package/dist/esm/stateFile.js +119 -0
- package/dist/esm/statusline/BurnRateCalculator.js +40 -0
- package/dist/esm/statusline/claudeStatuslinePayload.js +148 -0
- package/dist/esm/statusline/formatter.js +122 -0
- package/dist/esm/statusline/index.js +8 -0
- package/dist/esm/terminalSync.js +95 -0
- package/dist/esm/testing/credentialGuard.js +197 -0
- package/dist/esm/testing/index.js +12 -0
- package/dist/esm/testing/sessionLogFixtures.js +128 -0
- package/dist/esm/tokenEstimation.js +67 -0
- package/dist/esm/tokenSummary.js +113 -0
- package/dist/esm/toolCategory.js +82 -0
- package/dist/esm/transcript.js +257 -0
- package/dist/esm/turns/assistantTurn.js +373 -0
- package/dist/esm/types/codex.js +23 -0
- package/dist/esm/types/decisionLog.js +5 -0
- package/dist/esm/types/historicalData.js +47 -0
- package/dist/esm/types/knowledgeNote.js +15 -0
- package/dist/esm/types/observedSessionV1.js +257 -0
- package/dist/esm/types/opencode.js +12 -0
- package/dist/esm/types/plan.js +7 -0
- package/dist/esm/types/sessionEvent.js +10 -0
- package/dist/esm/types/taskPersistence.js +12 -0
- package/dist/esm/usage/billingBlocks.js +148 -0
- package/dist/esm/usage/claudeUsageDedupe.js +189 -0
- package/dist/esm/usage/importSessionHistory.js +84 -0
- package/dist/esm/usage/subagentUsage.js +18 -0
- package/dist/esm/usage/usageEvents.js +402 -0
- package/dist/esm/usage/usageReports.js +202 -0
- package/dist/esm/usageNormalization.js +95 -0
- package/dist/esm/watchers/eventBridge.js +219 -0
- package/dist/esm/watchers/factory.js +63 -0
- package/dist/esm/watchers/jsonlTail.js +289 -0
- package/dist/esm/watchers/jsonlWatcher.js +435 -0
- package/dist/esm/watchers/providerReaderWatcher.js +118 -0
- package/dist/esm/watchers/sqliteWatcher.js +285 -0
- package/dist/esm/watchers/types.js +4 -0
- package/dist/esm/workspaceRoots.js +56 -0
- package/dist/esm/writers/atomic.js +375 -0
- package/dist/esm/writers/decisions.js +51 -0
- package/dist/esm/writers/notes.js +46 -0
- package/dist/esm/writers/tasks.js +63 -0
- package/dist/esm/zaiQuota.js +323 -0
- package/dist/esm/zaiQuotaApi.js +271 -0
- package/dist/esm/zaiQuotaWatcher.js +265 -0
- package/dist/fileHead.d.ts +22 -0
- package/dist/fileHead.js +33 -0
- package/dist/index.d.ts +16 -7
- package/dist/index.js +49 -15
- package/dist/node.d.ts +13 -1
- package/dist/node.js +43 -2
- package/dist/parsers/codexHistory.js +2 -2
- package/dist/parsers/codexParser.d.ts +12 -0
- package/dist/parsers/codexParser.js +34 -12
- package/dist/parsers/debugLogParser.js +2 -2
- package/dist/parsers/sessionPathResolver.js +5 -4
- package/dist/promptHistory.d.ts +32 -0
- package/dist/promptHistory.js +115 -59
- package/dist/providerFailure.d.ts +12 -6
- package/dist/providerFailure.js +45 -0
- package/dist/providers/claudeCode.js +7 -99
- package/dist/providers/claudeListing.d.ts +22 -0
- package/dist/providers/claudeListing.js +158 -0
- package/dist/providers/codex.d.ts +3 -1
- package/dist/providers/codex.js +26 -227
- package/dist/providers/codexRollouts.d.ts +44 -0
- package/dist/providers/codexRollouts.js +261 -0
- package/dist/providers/detect.js +4 -4
- package/dist/readers/plans.js +3 -3
- package/dist/schemas/index.d.ts +2 -0
- package/dist/schemas/index.js +7 -2
- package/dist/schemas/providerFailure.d.ts +154 -0
- package/dist/schemas/providerFailure.js +40 -0
- package/dist/sessionPromptHistory.d.ts +12 -7
- package/dist/sessionPromptHistory.js +15 -3
- package/dist/sessionPromptSignals.d.ts +9 -0
- package/dist/sessionPromptSignals.js +13 -0
- package/dist/testing/credentialGuard.d.ts +23 -0
- package/dist/testing/credentialGuard.js +233 -0
- package/dist/testing/index.d.ts +14 -0
- package/dist/testing/index.js +22 -0
- package/dist/testing/sessionLogFixtures.d.ts +28 -0
- package/dist/testing/sessionLogFixtures.js +169 -0
- package/dist/tokenEstimation.d.ts +2 -0
- package/dist/tokenEstimation.js +21 -11
- package/dist/toolCategory.d.ts +18 -0
- package/dist/toolCategory.js +86 -0
- package/dist/types/codex.d.ts +4 -0
- package/dist/usage/usageEvents.d.ts +16 -0
- package/dist/usage/usageEvents.js +53 -9
- package/dist/watchers/jsonlTail.d.ts +26 -1
- package/dist/watchers/jsonlTail.js +136 -28
- package/dist/watchers/jsonlWatcher.js +7 -0
- package/dist/watchers/providerReaderWatcher.js +1 -0
- package/dist/watchers/types.d.ts +6 -0
- package/dist/workspaceRoots.d.ts +23 -0
- package/dist/workspaceRoots.js +93 -0
- package/package.json +22 -2
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import { getActiveCodexAccount, resolveActiveCodexAccount } from './codexProfiles';
|
|
3
|
+
import { quotaFromCodexRateLimits, resolveCodexQuotaFromLocalSources } from './codexQuota';
|
|
4
|
+
import { readQuotaSnapshot, writeQuotaSnapshot } from './quotaSnapshots';
|
|
5
|
+
import { appendQuotaHistorySample } from './quotaHistory';
|
|
6
|
+
import { CodexProvider } from './providers/codex';
|
|
7
|
+
import { onAccountsChanged } from './accountChanges';
|
|
8
|
+
const DEFAULT_DISCOVERY_POLL_INTERVAL_MS = 30_000;
|
|
9
|
+
const DEFAULT_LOCAL_SCAN_CACHE_MS = 5 * 60_000;
|
|
10
|
+
function accountEmail(account) {
|
|
11
|
+
return account?.email ?? account?.metadata?.email;
|
|
12
|
+
}
|
|
13
|
+
function enrichQuotaState(state, account) {
|
|
14
|
+
return {
|
|
15
|
+
...state,
|
|
16
|
+
runtimeProvider: 'codex',
|
|
17
|
+
providerId: 'codex',
|
|
18
|
+
accountLabel: account?.label,
|
|
19
|
+
accountDetail: accountEmail(account),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function makeUnavailableState(account, error = 'Run a Codex session to view rate limits') {
|
|
23
|
+
return {
|
|
24
|
+
runtimeProvider: 'codex',
|
|
25
|
+
fiveHour: { utilization: 0, resetsAt: '' },
|
|
26
|
+
sevenDay: { utilization: 0, resetsAt: '' },
|
|
27
|
+
available: false,
|
|
28
|
+
error,
|
|
29
|
+
providerId: 'codex',
|
|
30
|
+
accountLabel: account?.label,
|
|
31
|
+
accountDetail: accountEmail(account),
|
|
32
|
+
fiveHourLabel: 'Primary',
|
|
33
|
+
sevenDayLabel: 'Secondary',
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Watches the active Codex rollout for quota snapshots and falls back to the
|
|
38
|
+
* latest account-scoped cache when no live rate limits are present.
|
|
39
|
+
*/
|
|
40
|
+
export class CodexQuotaWatcher {
|
|
41
|
+
workspacePath;
|
|
42
|
+
discoveryPollIntervalMs;
|
|
43
|
+
providerFactory;
|
|
44
|
+
getActiveAccount;
|
|
45
|
+
readSnapshot;
|
|
46
|
+
writeSnapshot;
|
|
47
|
+
watchFile;
|
|
48
|
+
maxTailBytes;
|
|
49
|
+
maxSessionFiles;
|
|
50
|
+
localScanCacheMs;
|
|
51
|
+
now;
|
|
52
|
+
workspaceId;
|
|
53
|
+
appendHistorySample;
|
|
54
|
+
subscribeAccountsChanged;
|
|
55
|
+
listeners = [];
|
|
56
|
+
discoveryTimer;
|
|
57
|
+
provider = null;
|
|
58
|
+
reader = null;
|
|
59
|
+
fileWatcher = null;
|
|
60
|
+
sessionPath = null;
|
|
61
|
+
lastEmissionKey = null;
|
|
62
|
+
lastLocalScanAt = Number.NEGATIVE_INFINITY;
|
|
63
|
+
lastLocalScanState = null;
|
|
64
|
+
running = false;
|
|
65
|
+
accountSubscription = null;
|
|
66
|
+
constructor(workspacePath, options = {}) {
|
|
67
|
+
this.workspacePath = workspacePath;
|
|
68
|
+
this.discoveryPollIntervalMs =
|
|
69
|
+
options.discoveryPollIntervalMs ?? DEFAULT_DISCOVERY_POLL_INTERVAL_MS;
|
|
70
|
+
this.providerFactory = options.providerFactory ?? (() => new CodexProvider());
|
|
71
|
+
// Self-heal the registry pointer to the live login before reading it, so the
|
|
72
|
+
// history key (account.id) and the display label track the currently
|
|
73
|
+
// logged-in account even after a native `codex login`. Overridable for tests.
|
|
74
|
+
this.getActiveAccount =
|
|
75
|
+
options.getActiveAccount ??
|
|
76
|
+
(() => {
|
|
77
|
+
resolveActiveCodexAccount();
|
|
78
|
+
return getActiveCodexAccount();
|
|
79
|
+
});
|
|
80
|
+
this.readSnapshot = options.readSnapshot ?? readQuotaSnapshot;
|
|
81
|
+
this.writeSnapshot = options.writeSnapshot ?? writeQuotaSnapshot;
|
|
82
|
+
this.watchFile = options.watchFile ?? fs.watch;
|
|
83
|
+
this.maxTailBytes = options.maxTailBytes;
|
|
84
|
+
this.maxSessionFiles = options.maxSessionFiles;
|
|
85
|
+
this.localScanCacheMs = options.localScanCacheMs ?? DEFAULT_LOCAL_SCAN_CACHE_MS;
|
|
86
|
+
this.now = options.now ?? Date.now;
|
|
87
|
+
this.workspaceId = options.workspaceId;
|
|
88
|
+
this.appendHistorySample = options.appendHistorySample ?? appendQuotaHistorySample;
|
|
89
|
+
this.subscribeAccountsChanged = options.subscribeAccountsChanged ?? onAccountsChanged;
|
|
90
|
+
}
|
|
91
|
+
start() {
|
|
92
|
+
if (this.running)
|
|
93
|
+
return;
|
|
94
|
+
this.running = true;
|
|
95
|
+
this.accountSubscription ??= this.subscribeAccountsChanged(() => this.handleAccountsChanged());
|
|
96
|
+
this.handleAccountsChanged();
|
|
97
|
+
}
|
|
98
|
+
stop() {
|
|
99
|
+
if (!this.running)
|
|
100
|
+
return;
|
|
101
|
+
this.running = false;
|
|
102
|
+
if (this.discoveryTimer) {
|
|
103
|
+
clearInterval(this.discoveryTimer);
|
|
104
|
+
this.discoveryTimer = undefined;
|
|
105
|
+
}
|
|
106
|
+
this.accountSubscription?.dispose();
|
|
107
|
+
this.accountSubscription = null;
|
|
108
|
+
this.teardownActiveSession();
|
|
109
|
+
}
|
|
110
|
+
dispose() {
|
|
111
|
+
this.stop();
|
|
112
|
+
this.listeners.splice(0, this.listeners.length);
|
|
113
|
+
}
|
|
114
|
+
onUpdate(cb) {
|
|
115
|
+
this.listeners.push(cb);
|
|
116
|
+
return {
|
|
117
|
+
dispose: () => {
|
|
118
|
+
const index = this.listeners.indexOf(cb);
|
|
119
|
+
if (index >= 0)
|
|
120
|
+
this.listeners.splice(index, 1);
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
onQuotaUpdate(cb) {
|
|
125
|
+
return this.onUpdate(cb);
|
|
126
|
+
}
|
|
127
|
+
refresh() {
|
|
128
|
+
this.handleAccountsChanged();
|
|
129
|
+
}
|
|
130
|
+
handleAccountsChanged() {
|
|
131
|
+
if (!this.running)
|
|
132
|
+
return;
|
|
133
|
+
const account = this.safeGetActiveAccount();
|
|
134
|
+
if (!account) {
|
|
135
|
+
if (this.discoveryTimer) {
|
|
136
|
+
clearInterval(this.discoveryTimer);
|
|
137
|
+
this.discoveryTimer = undefined;
|
|
138
|
+
}
|
|
139
|
+
this.teardownActiveSession();
|
|
140
|
+
this.emitState(makeUnavailableState(null, 'No Codex account configured'));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (!this.discoveryTimer) {
|
|
144
|
+
this.discoveryTimer = setInterval(() => {
|
|
145
|
+
this.refreshActiveSession();
|
|
146
|
+
}, this.discoveryPollIntervalMs);
|
|
147
|
+
this.discoveryTimer.unref?.();
|
|
148
|
+
}
|
|
149
|
+
this.refreshActiveSession(account);
|
|
150
|
+
}
|
|
151
|
+
refreshActiveSession(account = this.safeGetActiveAccount()) {
|
|
152
|
+
if (!account) {
|
|
153
|
+
this.handleAccountsChanged();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const provider = this.getProvider();
|
|
157
|
+
const nextSessionPath = provider.findActiveSession(this.workspacePath);
|
|
158
|
+
if (!nextSessionPath) {
|
|
159
|
+
this.teardownActiveSession();
|
|
160
|
+
this.emitCachedOrUnavailable(account);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (nextSessionPath !== this.sessionPath || this.reader == null) {
|
|
164
|
+
this.attachToSession(nextSessionPath);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
this.ingestSessionUpdate('readNew', account);
|
|
168
|
+
}
|
|
169
|
+
attachToSession(nextSessionPath) {
|
|
170
|
+
this.teardownActiveSession();
|
|
171
|
+
this.provider = this.providerFactory();
|
|
172
|
+
this.reader = this.provider.createReader(nextSessionPath);
|
|
173
|
+
this.sessionPath = nextSessionPath;
|
|
174
|
+
this.ingestSessionUpdate('readAll');
|
|
175
|
+
try {
|
|
176
|
+
this.fileWatcher = this.watchFile(nextSessionPath, (eventType) => {
|
|
177
|
+
if (!this.running)
|
|
178
|
+
return;
|
|
179
|
+
if (eventType === 'change') {
|
|
180
|
+
this.ingestSessionUpdate('readNew');
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
this.refreshActiveSession();
|
|
184
|
+
});
|
|
185
|
+
if (typeof this.fileWatcher.on === 'function') {
|
|
186
|
+
this.fileWatcher.on('error', () => {
|
|
187
|
+
try {
|
|
188
|
+
this.fileWatcher?.close();
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
// Discovery polling remains active.
|
|
192
|
+
}
|
|
193
|
+
this.fileWatcher = null;
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
this.emitCachedOrUnavailable();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
ingestSessionUpdate(mode, knownAccount) {
|
|
202
|
+
const account = knownAccount ?? this.safeGetActiveAccount();
|
|
203
|
+
if (!account) {
|
|
204
|
+
this.handleAccountsChanged();
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (!this.provider || !this.reader) {
|
|
208
|
+
this.emitCachedOrUnavailable(account);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (!this.reader.exists()) {
|
|
212
|
+
this.refreshActiveSession();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (mode === 'readAll') {
|
|
216
|
+
this.reader.readAll();
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
this.reader.readNew();
|
|
220
|
+
}
|
|
221
|
+
const liveQuota = quotaFromCodexRateLimits(this.provider.getLastRateLimits(), 'session');
|
|
222
|
+
if (!liveQuota) {
|
|
223
|
+
this.emitCachedOrUnavailable(account);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const cached = this.readSnapshot('codex', account.id);
|
|
227
|
+
const liveQuotaWithResetCredits = {
|
|
228
|
+
...liveQuota,
|
|
229
|
+
resetCredits: liveQuota.resetCredits ?? cached?.resetCredits,
|
|
230
|
+
};
|
|
231
|
+
this.writeSnapshot('codex', account.id, liveQuotaWithResetCredits);
|
|
232
|
+
if (this.workspaceId) {
|
|
233
|
+
const sample = {
|
|
234
|
+
timestamp: liveQuotaWithResetCredits.capturedAt ?? new Date().toISOString(),
|
|
235
|
+
runtimeProvider: 'codex',
|
|
236
|
+
providerId: account.id,
|
|
237
|
+
workspaceId: this.workspaceId,
|
|
238
|
+
fiveHour: {
|
|
239
|
+
utilization: liveQuotaWithResetCredits.fiveHour.utilization,
|
|
240
|
+
resetsAt: liveQuotaWithResetCredits.fiveHour.resetsAt,
|
|
241
|
+
},
|
|
242
|
+
sevenDay: {
|
|
243
|
+
utilization: liveQuotaWithResetCredits.sevenDay.utilization,
|
|
244
|
+
resetsAt: liveQuotaWithResetCredits.sevenDay.resetsAt,
|
|
245
|
+
},
|
|
246
|
+
available: liveQuotaWithResetCredits.available,
|
|
247
|
+
error: liveQuotaWithResetCredits.error,
|
|
248
|
+
source: liveQuotaWithResetCredits.source,
|
|
249
|
+
stale: liveQuotaWithResetCredits.stale,
|
|
250
|
+
};
|
|
251
|
+
try {
|
|
252
|
+
const result = this.appendHistorySample(sample);
|
|
253
|
+
if (result && typeof result.catch === 'function') {
|
|
254
|
+
result.catch(() => {
|
|
255
|
+
// History append must never break the live emission path.
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
// Synchronous errors swallowed for the same reason.
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
this.emitState(enrichQuotaState({
|
|
264
|
+
...liveQuotaWithResetCredits,
|
|
265
|
+
runtimeProvider: 'codex',
|
|
266
|
+
providerId: 'codex',
|
|
267
|
+
}, account));
|
|
268
|
+
}
|
|
269
|
+
emitCachedOrUnavailable(knownAccount) {
|
|
270
|
+
const account = knownAccount ?? this.safeGetActiveAccount();
|
|
271
|
+
if (!account) {
|
|
272
|
+
this.emitState(makeUnavailableState(null, 'No Codex account configured'));
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const scanNow = this.now();
|
|
276
|
+
if (scanNow - this.lastLocalScanAt < this.localScanCacheMs) {
|
|
277
|
+
if (this.lastLocalScanState) {
|
|
278
|
+
this.emitState(this.lastLocalScanState);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
let localProvider = null;
|
|
284
|
+
this.lastLocalScanAt = scanNow;
|
|
285
|
+
this.lastLocalScanState = null;
|
|
286
|
+
try {
|
|
287
|
+
localProvider = this.providerFactory();
|
|
288
|
+
this.lastLocalScanState = resolveCodexQuotaFromLocalSources({
|
|
289
|
+
workspacePath: this.workspacePath,
|
|
290
|
+
activeAccount: account,
|
|
291
|
+
readSnapshot: this.readSnapshot,
|
|
292
|
+
writeSnapshot: this.writeSnapshot,
|
|
293
|
+
provider: localProvider,
|
|
294
|
+
maxTailBytes: this.maxTailBytes,
|
|
295
|
+
maxSessionFiles: this.maxSessionFiles,
|
|
296
|
+
});
|
|
297
|
+
if (this.lastLocalScanState) {
|
|
298
|
+
this.emitState(this.lastLocalScanState);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
// Fall through to account-scoped cache or unavailable state.
|
|
304
|
+
}
|
|
305
|
+
finally {
|
|
306
|
+
localProvider?.dispose();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const cached = this.readSnapshot('codex', account.id);
|
|
310
|
+
if (cached) {
|
|
311
|
+
this.emitState(enrichQuotaState({
|
|
312
|
+
...cached,
|
|
313
|
+
runtimeProvider: 'codex',
|
|
314
|
+
providerId: 'codex',
|
|
315
|
+
source: 'cache',
|
|
316
|
+
stale: true,
|
|
317
|
+
fiveHourLabel: cached.fiveHourLabel ?? 'Primary',
|
|
318
|
+
sevenDayLabel: cached.sevenDayLabel ?? 'Secondary',
|
|
319
|
+
}, account));
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
this.emitState(makeUnavailableState(account));
|
|
323
|
+
}
|
|
324
|
+
emitState(state) {
|
|
325
|
+
const nextKey = JSON.stringify(state);
|
|
326
|
+
if (this.lastEmissionKey === nextKey)
|
|
327
|
+
return;
|
|
328
|
+
this.lastEmissionKey = nextKey;
|
|
329
|
+
for (const listener of this.listeners) {
|
|
330
|
+
try {
|
|
331
|
+
listener(state);
|
|
332
|
+
}
|
|
333
|
+
catch {
|
|
334
|
+
// Listener failures should not stop session watching.
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
getProvider() {
|
|
339
|
+
if (!this.provider) {
|
|
340
|
+
this.provider = this.providerFactory();
|
|
341
|
+
}
|
|
342
|
+
return this.provider;
|
|
343
|
+
}
|
|
344
|
+
safeGetActiveAccount() {
|
|
345
|
+
try {
|
|
346
|
+
return this.getActiveAccount();
|
|
347
|
+
}
|
|
348
|
+
catch {
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
teardownActiveSession() {
|
|
353
|
+
if (this.fileWatcher) {
|
|
354
|
+
this.fileWatcher.close();
|
|
355
|
+
this.fileWatcher = null;
|
|
356
|
+
}
|
|
357
|
+
this.reader?.flush();
|
|
358
|
+
this.reader = null;
|
|
359
|
+
this.provider?.dispose();
|
|
360
|
+
this.provider = null;
|
|
361
|
+
this.sessionPath = null;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context composer — aggregates all readers with fidelity tiers.
|
|
3
|
+
*/
|
|
4
|
+
import { readTasks } from '../readers/tasks';
|
|
5
|
+
import { readDecisions } from '../readers/decisions';
|
|
6
|
+
import { readNotes } from '../readers/notes';
|
|
7
|
+
import { readHistory } from '../readers/history';
|
|
8
|
+
import { readLatestHandoff } from '../readers/handoff';
|
|
9
|
+
export async function composeContext(project, fidelity, provider, workspacePath) {
|
|
10
|
+
// Fetch all data in parallel
|
|
11
|
+
const [allTasks, allDecisions, allNotes, history, handoff] = await Promise.all([
|
|
12
|
+
readTasks(project),
|
|
13
|
+
readDecisions(project),
|
|
14
|
+
readNotes(project),
|
|
15
|
+
readHistory(),
|
|
16
|
+
readLatestHandoff(project),
|
|
17
|
+
]);
|
|
18
|
+
// Get session summaries
|
|
19
|
+
const sessionFiles = workspacePath ? provider.findAllSessions(workspacePath) : [];
|
|
20
|
+
const sessionSummaries = [];
|
|
21
|
+
// Read stats for session files based on fidelity
|
|
22
|
+
const FIDELITY_SESSION_LIMIT = { full: 5, compact: 2, brief: 1 };
|
|
23
|
+
const sessionLimit = FIDELITY_SESSION_LIMIT[fidelity];
|
|
24
|
+
const sessionPaths = sessionFiles.slice(0, sessionLimit);
|
|
25
|
+
for (const sp of sessionPaths) {
|
|
26
|
+
try {
|
|
27
|
+
sessionSummaries.push(provider.readSessionStats(sp));
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
/* skip */
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// Apply fidelity filtering
|
|
34
|
+
let tasks;
|
|
35
|
+
let decisions;
|
|
36
|
+
let notes;
|
|
37
|
+
let stats = null;
|
|
38
|
+
switch (fidelity) {
|
|
39
|
+
case 'full':
|
|
40
|
+
tasks = allTasks;
|
|
41
|
+
decisions = allDecisions;
|
|
42
|
+
notes = allNotes.filter((n) => n.status === 'active' || n.status === 'needs_review');
|
|
43
|
+
if (history) {
|
|
44
|
+
stats = { tokens: { ...history.allTime.tokens }, cost: history.allTime.totalCost };
|
|
45
|
+
}
|
|
46
|
+
break;
|
|
47
|
+
case 'compact':
|
|
48
|
+
tasks = allTasks.filter((t) => t.status === 'pending');
|
|
49
|
+
decisions = allDecisions.slice(0, 10);
|
|
50
|
+
notes = allNotes.filter((n) => n.status === 'active');
|
|
51
|
+
if (history) {
|
|
52
|
+
stats = { tokens: { ...history.allTime.tokens }, cost: history.allTime.totalCost };
|
|
53
|
+
}
|
|
54
|
+
break;
|
|
55
|
+
case 'brief':
|
|
56
|
+
tasks = allTasks.filter((t) => t.status === 'pending').slice(0, 3);
|
|
57
|
+
decisions = []; // Count only
|
|
58
|
+
notes = allNotes.filter((n) => n.importance === 'critical');
|
|
59
|
+
stats = null;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
provider: provider.id,
|
|
64
|
+
tasks: { items: tasks, total: allTasks.length },
|
|
65
|
+
decisions: { items: decisions, total: allDecisions.length },
|
|
66
|
+
notes: { items: notes, total: allNotes.length },
|
|
67
|
+
handoff,
|
|
68
|
+
stats,
|
|
69
|
+
sessionSummaries,
|
|
70
|
+
};
|
|
71
|
+
}
|