[object Object]

← back to Norma Platform

IG post skill: poll container readiness before publish (fixes 'Media ID is not available' race)

cf737ee5df5927a518a630646b3ec6aa9bd3c5f0 · 2026-08-11 10:56:51 -0700 · Steve Abrams

Files touched

Diff

commit cf737ee5df5927a518a630646b3ec6aa9bd3c5f0
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Tue Aug 11 10:56:51 2026 -0700

    IG post skill: poll container readiness before publish (fixes 'Media ID is not available' race)
---
 agents/instagram-agent/skills/post.js | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/agents/instagram-agent/skills/post.js b/agents/instagram-agent/skills/post.js
index e62e917..bde4abc 100644
--- a/agents/instagram-agent/skills/post.js
+++ b/agents/instagram-agent/skills/post.js
@@ -91,7 +91,19 @@ module.exports = async function post(params) {
   });
   console.log(`[${AGENT}] Post container created: ${containerId}`);
 
-  // Step 2: publish (image containers are ready immediately)
+  // Step 2: wait for the container to finish, THEN publish. Image containers
+  // are USUALLY ready immediately, but Meta occasionally hasn't finished
+  // processing when publish is called and returns "Media ID is not available".
+  // Polling status_code -> FINISHED first fixes that race. Publishing only ever
+  // fires once, after readiness, so this can never cause a double-post.
+  try {
+    await ig.waitForContainer(containerId, accessToken, { tries: 12, delayMs: 2000 });
+  } catch (e) {
+    // A real processing ERROR is surfaced clearly (mirrors the reel path); a
+    // bare timeout still falls through to one publish attempt as a last resort.
+    if (/processing error/i.test(e.message)) throw e;
+    console.log(`[${AGENT}] container wait note: ${e.message} — attempting publish anyway`);
+  }
   const mediaId = await ig.publishContainer(userId, accessToken, containerId);
   const permalink = await ig.permalinkOf(mediaId, accessToken);
   console.log(`[${AGENT}] Post published: media=${mediaId} ${permalink || ''}`);

← 56427de auto-data-snapshot: 2026-08-11T10:12:37 (1 data files) — age  ·  back to Norma Platform  ·  auto-data-snapshot: 2026-08-11T11:45:12 (1 data files) — age c9d175d →