PLG-CAL-003 fix deferred slot replanning

This commit is contained in:
zhi
2026-04-01 08:52:11 +00:00
parent 97021f97c0
commit 24c4a7ad14
5 changed files with 71 additions and 2 deletions

View File

@@ -88,6 +88,12 @@ class CalendarScheduler {
await this.handleNonIdleAgent(response.slots, response.agent_status);
return;
}
// Agent is idle again - previously deferred slots should become eligible
// for selection in the next planning pass.
if (this.state.deferredSlotIds.size > 0) {
this.logDebug(`Agent returned to idle; clearing ${this.state.deferredSlotIds.size} deferred slot marker(s) for replanning`);
this.state.deferredSlotIds.clear();
}
// Agent is idle - handle pending slots
await this.handleIdleAgent(response.slots);
}
@@ -191,6 +197,10 @@ class CalendarScheduler {
// Revert slot to not_started status
await this.revertSlot(slot);
await this.config.bridge.reportAgentStatus({ status: 'idle' });
this.state.isProcessing = false;
this.state.currentSlot = null;
await this.triggerReplan('wake failure');
return;
}
// Note: isProcessing remains true until agent signals completion
// This is handled by external completion callback
@@ -386,6 +396,7 @@ Please use this time for the scheduled activity.`;
finally {
this.state.isProcessing = false;
this.state.currentSlot = null;
await this.triggerReplan('slot completion');
}
}
/**
@@ -418,6 +429,7 @@ Please use this time for the scheduled activity.`;
finally {
this.state.isProcessing = false;
this.state.currentSlot = null;
await this.triggerReplan('slot abort');
}
}
/**
@@ -471,6 +483,23 @@ Please use this time for the scheduled activity.`;
this.config.logger.error('Failed to resume slot:', err);
}
}
/**
* Trigger an immediate replanning pass after the current slot lifecycle ends.
* This lets previously deferred/not-started slots compete again as soon as
* the agent becomes idle.
*/
async triggerReplan(reason) {
if (!this.state.isRunning) {
return;
}
this.logDebug(`Triggering immediate replanning after ${reason}`);
try {
await this.runHeartbeat();
}
catch (err) {
this.config.logger.error(`Failed to trigger replanning after ${reason}:`, err);
}
}
/**
* Get a stable ID for a slot (real or virtual).
*/