@kervnet/opencode-kiro-auth 1.7.16 → 1.7.18

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.
@@ -42,10 +42,20 @@ export class AuthHandler {
42
42
  .get();
43
43
  cliDb.close();
44
44
  if (!tokenRow || !tokenRow.expires || tokenRow.expires < Date.now()) {
45
- logger.log('Background token refresh: kiro-cli token expired or missing, skipping');
46
- return;
45
+ logger.log('Background token refresh: kiro-cli token expired, forcing refresh via kiro-cli whoami...');
46
+ try {
47
+ const { execSync } = await import('node:child_process');
48
+ execSync('kiro-cli whoami', { timeout: 15000, stdio: 'pipe' });
49
+ logger.log('Background token refresh: kiro-cli token refreshed successfully');
50
+ }
51
+ catch {
52
+ logger.log('Background token refresh: kiro-cli refresh failed, skipping');
53
+ return;
54
+ }
55
+ }
56
+ else {
57
+ logger.log(`Background token refresh: kiro-cli token valid until ${new Date(tokenRow.expires).toLocaleString()}`);
47
58
  }
48
- logger.log(`Background token refresh: kiro-cli token valid until ${new Date(tokenRow.expires).toLocaleString()}`);
49
59
  }
50
60
  catch (e) {
51
61
  logger.warn('Background token refresh: failed to check kiro-cli token status', e);
@@ -18,60 +18,75 @@ export class TokenRefresher {
18
18
  if (!accessTokenExpired(auth, this.config.token_expiry_buffer_ms)) {
19
19
  return { account, shouldContinue: false };
20
20
  }
21
+ const logger = await import('../../plugin/logger.js');
22
+ logger.log(`[TokenRefresher] Access token expired for ${account.email}, attempting OIDC refresh...`);
21
23
  try {
22
24
  const newAuth = await refreshAccessToken(auth);
25
+ logger.log(`[TokenRefresher] OIDC refresh succeeded for ${account.email}`);
23
26
  this.accountManager.updateFromAuth(account, newAuth);
24
27
  await this.repository.batchSave(this.accountManager.getAccounts());
25
28
  return { account, shouldContinue: false };
26
29
  }
27
30
  catch (e) {
31
+ logger.log(`[TokenRefresher] OIDC refresh failed for ${account.email}: ${e.message}`);
28
32
  return await this.handleRefreshError(e, account, showToast);
29
33
  }
30
34
  }
31
35
  async handleRefreshError(error, account, showToast) {
32
- // Only sync if cooldown period has passed
36
+ const logger = await import('../../plugin/logger.js');
33
37
  const now = Date.now();
34
38
  if (this.config.auto_sync_kiro_cli && now - this.lastSyncTime > this.SYNC_COOLDOWN_MS) {
35
39
  this.lastSyncTime = now;
36
- // Force kiro-cli to refresh its token first
40
+ // Force kiro-cli to refresh its tokens
41
+ logger.log('[TokenRefresher] Forcing kiro-cli token refresh via whoami...');
37
42
  try {
38
- const { exec } = await import('node:child_process');
39
- await new Promise((resolve) => {
40
- exec('kiro-cli whoami', (error) => {
41
- resolve(); // Continue even if it fails
42
- });
43
- });
43
+ const { execSync } = await import('node:child_process');
44
+ execSync('kiro-cli whoami', { timeout: 15000, stdio: 'pipe' });
45
+ logger.log('[TokenRefresher] kiro-cli whoami succeeded');
44
46
  }
45
47
  catch (e) {
46
- // Silent fail - continue with sync anyway
48
+ logger.warn(`[TokenRefresher] kiro-cli whoami failed: ${e.message}`);
47
49
  }
50
+ logger.log('[TokenRefresher] Syncing accounts from kiro-cli DB...');
48
51
  await this.syncFromKiroCli();
52
+ // Reload fresh accounts from DB into accountManager
53
+ this.repository.invalidateCache();
54
+ const freshAccounts = await this.repository.findAll();
55
+ this.accountManager.replaceAccounts(freshAccounts);
49
56
  }
50
- this.repository.invalidateCache();
51
- const accounts = await this.repository.findAll();
57
+ else if (this.config.auto_sync_kiro_cli) {
58
+ logger.log(`[TokenRefresher] Sync cooldown active (${Math.ceil((this.SYNC_COOLDOWN_MS - (now - this.lastSyncTime)) / 1000)}s remaining)`);
59
+ }
60
+ // Re-check the account after sync
61
+ const accounts = this.accountManager.getAccounts();
52
62
  const stillAcc = accounts.find((a) => a.id === account.id);
53
- if (stillAcc &&
54
- !accessTokenExpired(this.accountManager.toAuthDetails(stillAcc), this.config.token_expiry_buffer_ms)) {
55
- // Reset health status since we have fresh credentials
56
- stillAcc.isHealthy = true;
57
- stillAcc.failCount = 0;
58
- stillAcc.unhealthyReason = undefined;
59
- await this.repository.batchSave([stillAcc]);
60
- showToast('Credentials recovered from Kiro CLI sync.', 'info');
61
- return { account: stillAcc, shouldContinue: false };
63
+ if (stillAcc) {
64
+ const freshAuth = this.accountManager.toAuthDetails(stillAcc);
65
+ const stillExpired = accessTokenExpired(freshAuth, this.config.token_expiry_buffer_ms);
66
+ logger.log(`[TokenRefresher] After sync: account ${stillAcc.email} token expired=${stillExpired}`);
67
+ if (!stillExpired) {
68
+ stillAcc.isHealthy = true;
69
+ stillAcc.failCount = 0;
70
+ stillAcc.unhealthyReason = undefined;
71
+ await this.repository.batchSave([stillAcc]);
72
+ logger.log('[TokenRefresher] Recovery successful!');
73
+ return { account: stillAcc, shouldContinue: false };
74
+ }
62
75
  }
76
+ // Token still expired after sync — mark unhealthy
63
77
  if (error instanceof KiroTokenRefreshError &&
64
78
  (error.code === 'ExpiredTokenException' ||
65
79
  error.code === 'InvalidTokenException' ||
66
80
  error.code === 'HTTP_401' ||
67
81
  error.message.includes('Invalid refresh token provided'))) {
82
+ logger.log(`[TokenRefresher] Marking account ${account.email} as permanently unhealthy: ${error.message}`);
68
83
  this.accountManager.markUnhealthy(account, error.message);
69
84
  await this.repository.batchSave(this.accountManager.getAccounts());
70
85
  return { account, shouldContinue: true };
71
86
  }
72
- // For HTTP_403, only mark unhealthy after multiple failures
73
87
  if (error instanceof KiroTokenRefreshError && error.code === 'HTTP_403') {
74
88
  account.failCount = (account.failCount || 0) + 1;
89
+ logger.log(`[TokenRefresher] HTTP 403, failCount=${account.failCount}`);
75
90
  if (account.failCount >= 3) {
76
91
  this.accountManager.markUnhealthy(account, error.message);
77
92
  }
@@ -22,6 +22,7 @@ export declare class RequestHandler {
22
22
  private logResponse;
23
23
  private logError;
24
24
  private allAccountsPermanentlyUnhealthy;
25
+ private forceKiroCliRefreshAndSync;
25
26
  private sleep;
26
27
  }
27
28
  export {};
@@ -52,20 +52,8 @@ export class RequestHandler {
52
52
  throw new Error(check.error);
53
53
  }
54
54
  if (this.allAccountsPermanentlyUnhealthy()) {
55
- // Try to recover by syncing from kiro-cli
56
55
  if (this.config.auto_sync_kiro_cli) {
57
- showToast('All accounts unhealthy. Attempting recovery from Kiro CLI...', 'warning');
58
- const { syncFromKiroCli } = await import('../../plugin/sync/kiro-cli.js');
59
- await syncFromKiroCli();
60
- // Reset all accounts to healthy and clear fail counts
61
- const accounts = this.accountManager.getAccounts();
62
- for (const acc of accounts) {
63
- acc.isHealthy = true;
64
- acc.failCount = 0;
65
- acc.unhealthyReason = undefined;
66
- }
67
- await this.repository.batchSave(accounts);
68
- // Give it one more chance
56
+ await this.forceKiroCliRefreshAndSync(showToast);
69
57
  continue;
70
58
  }
71
59
  throw new Error('All accounts are permanently unhealthy (quota exceeded or suspended)');
@@ -82,6 +70,7 @@ export class RequestHandler {
82
70
  const tokenResult = await this.tokenRefresher.refreshIfNeeded(acc, auth, showToast);
83
71
  if (tokenResult.shouldContinue) {
84
72
  acc = tokenResult.account;
73
+ logger.log(`[RequestHandler] Token refresh returned shouldContinue=true for ${acc.email}, retrying in 500ms (iteration ${retryContext.iterations})`);
85
74
  await this.sleep(500);
86
75
  continue;
87
76
  }
@@ -209,6 +198,33 @@ export class RequestHandler {
209
198
  }
210
199
  return accounts.every((acc) => !acc.isHealthy && isPermanentError(acc.unhealthyReason));
211
200
  }
201
+ async forceKiroCliRefreshAndSync(showToast) {
202
+ logger.log('[RequestHandler] All accounts permanently unhealthy. Forcing kiro-cli token refresh...');
203
+ showToast('All accounts unhealthy. Forcing kiro-cli token refresh...', 'warning');
204
+ try {
205
+ const { execSync } = await import('node:child_process');
206
+ execSync('kiro-cli whoami', { timeout: 15000, stdio: 'pipe' });
207
+ logger.log('[RequestHandler] kiro-cli whoami succeeded');
208
+ }
209
+ catch (e) {
210
+ logger.warn(`[RequestHandler] kiro-cli whoami failed: ${e.message}`);
211
+ throw new Error('All accounts are unhealthy and kiro-cli token refresh failed. Please run: kiro-cli login');
212
+ }
213
+ const { syncFromKiroCli } = await import('../../plugin/sync/kiro-cli.js');
214
+ await syncFromKiroCli();
215
+ // Reload fresh accounts from DB into accountManager
216
+ this.repository.invalidateCache();
217
+ const freshAccounts = await this.repository.findAll();
218
+ this.accountManager.replaceAccounts(freshAccounts);
219
+ // Reset health on all accounts
220
+ const accounts = this.accountManager.getAccounts();
221
+ for (const acc of accounts) {
222
+ acc.isHealthy = true;
223
+ acc.failCount = 0;
224
+ acc.unhealthyReason = undefined;
225
+ }
226
+ await this.repository.batchSave(accounts);
227
+ }
212
228
  sleep(ms) {
213
229
  return new Promise((resolve) => setTimeout(resolve, ms));
214
230
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kervnet/opencode-kiro-auth",
3
- "version": "1.7.16",
3
+ "version": "1.7.18",
4
4
  "description": "OpenCode plugin for AWS Kiro (CodeWhisperer) with IAM Identity Center profile support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",