MCPcopy Create free account
hub / github.com/garrytan/gstack / generateBrainWriteBack

Function generateBrainWriteBack

scripts/resolvers/gbrain.ts:222–270  ·  view source on GitHub ↗
(ctx: TemplateContext)

Source from the content-addressed store, hash-verified

220 * write-back behavior without any template changes.
221 */
222export function generateBrainWriteBack(ctx: TemplateContext): string {
223 if (!isPreflightSkill(ctx.skillName)) return '';
224 const weight = SKILL_CALIBRATION_WEIGHTS[ctx.skillName];
225 if (weight == null) return '';
226 // List the cache digests this skill's writes should invalidate. Multiple
227 // skills write to multiple entities; the invalidation map captures this.
228 const invalidatesEntities = getInvalidationTargets(`/${ctx.skillName}`);
229 const invalidateBash = invalidatesEntities
230 .map((e) => ` ${ctx.paths.binDir}/gstack-brain-cache invalidate ${e} --project "$SLUG" 2>/dev/null || true`)
231 .join('\n');
232
233 return `## Brain Calibration Write-Back (Phase 2 / gated)
234
235When the skill makes a typed prediction worth tracking (scope decision,
236TTHW target, architectural bet, wedge commitment), it MAY write a
237\`kind=bet\` take to the brain so a calibration profile builds over time.
238
239**Gated on two things:**
2401. Brain trust policy for the active endpoint is \`personal\` (check via
241 \`${ctx.paths.binDir}/gstack-config get brain_trust_policy@<endpoint-hash>\`).
242 Shared brains skip write-back to avoid polluting team calibration.
2432. Feature flag \`BRAIN_CALIBRATION_WRITEBACK\` is set (today: false; flips
244 to true when upstream gbrain v0.42+ ships \`takes_add\` MCP op).
245
246When both gates pass, the write-back path uses \`mcp__gbrain__takes_add\`
247to record a take with weight ${weight} (per SKILL_CALIBRATION_WEIGHTS).
248If the MCP op is unavailable, fall back to \`mcp__gbrain__put_page\` with
249a gstack:takes fence block (documented but uglier path).
250
251Mandatory take frontmatter shape:
252\`\`\`yaml
253kind: bet
254holder: <user identity from whoami>
255claim: <one-line prediction the skill is making>
256weight: ${weight}
257since_date: <today's date>
258expected_resolution: <date in 1-3 months depending on skill>
259source_skill: ${ctx.skillName}
260\`\`\`
261
262After write, invalidate the affected digests so the next preflight reflects
263the new state:
264
265\`\`\`bash
266eval "$(${ctx.paths.binDir}/gstack-slug 2>/dev/null)" 2>/dev/null || true
267${invalidateBash || ' # (no per-skill invalidation targets configured)'}
268\`\`\`
269`;
270}

Callers 3

totalBrainBytesFunction · 0.90

Calls 2

getInvalidationTargetsFunction · 0.90
isPreflightSkillFunction · 0.85

Tested by 1

totalBrainBytesFunction · 0.72