midscene icon indicating copy to clipboard operation
midscene copied to clipboard

fix(core): validate yamlWorkflow before executing cached aiAction plan

Open quanru opened this issue 3 weeks ago • 1 comments

Summary

This PR fixes a critical bug where aiAction() could silently fail when the cache contains an empty or undefined yamlWorkflow field.

Problem

When the following conditions were met:

  1. Cache was enabled and a matching cache record was found
  2. The cached yamlWorkflow field was empty or undefined (due to corrupted cache, version incompatibility, or write failure)

The code would call runYaml(undefined), which would:

  • Call parseYamlScript(undefined)
  • Fail to parse and throw an error
  • Result: aiAction() would not execute at all

Solution

Added validation to check if yamlWorkflow exists before using the cached plan:

if (
  matchedCache &&
  this.taskCache?.isCacheResultUsed &&
  matchedCache.cacheContent?.yamlWorkflow  // ✅ Validate not empty
) {
  // Safe to use cached workflow
}

// Fallback to normal execution if cache is invalid
if (matchedCache && !matchedCache.cacheContent?.yamlWorkflow) {
  debug('cache matched but yamlWorkflow is empty, will execute normally instead');
}

Changes

  • Added validation for yamlWorkflow before executing cached plans
  • Added debug logging when cache is matched but workflow is empty
  • Gracefully falls back to normal execution when cache is invalid
  • Improved type safety by removing optional chaining where value is guaranteed

Testing

  • ✅ Lint checks passed
  • ✅ Code follows project conventions

Related

Location: packages/core/src/agent/agent.ts:827-848

quanru avatar Dec 01 '25 11:12 quanru

Deploy Preview for midscene ready!

Name Link
Latest commit f71c2de6bdbbb6c655e0823aad10ba3210a4b51e
Latest deploy log https://app.netlify.com/projects/midscene/deploys/692ed087ace89b0009a0c38f
Deploy Preview https://deploy-preview-1524--midscene.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify[bot] avatar Dec 01 '25 11:12 netlify[bot]