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

Function generateLearningsSearch

scripts/resolvers/learnings.ts:23–88  ·  view source on GitHub ↗
(ctx: TemplateContext, args?: string[])

Source from the content-addressed store, hash-verified

21const QUERY_SAFE_RE = /^[A-Za-z0-9 _-]+$/;
22
23export function generateLearningsSearch(ctx: TemplateContext, args?: string[]): string {
24 // Parse query= arg. Empty value falls through to no-query (principle of least surprise:
25 // a stray {{LEARNINGS_SEARCH:query=}} placeholder gets today's behavior, not a build error).
26 const queryArg = (args || [])
27 .filter(a => a.startsWith('query='))
28 .map(a => a.slice(6))
29 .filter(Boolean)[0];
30 if (queryArg && !QUERY_SAFE_RE.test(queryArg)) {
31 throw new Error(
32 `{{LEARNINGS_SEARCH:query=...}} value must match ${QUERY_SAFE_RE} (alphanumeric, space, hyphen, underscore). Got: ${JSON.stringify(queryArg)}`
33 );
34 }
35 const queryFlag = queryArg ? ` --query "${queryArg}"` : '';
36
37 if (ctx.host === 'codex') {
38 // Codex: simpler version, no cross-project, uses $GSTACK_BIN
39 return `## Prior Learnings
40
41Search for relevant learnings from previous sessions on this project:
42
43\`\`\`bash
44$GSTACK_BIN/gstack-learnings-search --limit 10${queryFlag} 2>/dev/null || true
45\`\`\`
46
47If learnings are found, incorporate them into your analysis. When a review finding
48matches a past learning, note it: "Prior learning applied: [key] (confidence N, from [date])"`;
49 }
50
51 return `## Prior Learnings
52
53Search for relevant learnings from previous sessions:
54
55\`\`\`bash
56_CROSS_PROJ=$(${ctx.paths.binDir}/gstack-config get cross_project_learnings 2>/dev/null || echo "unset")
57echo "CROSS_PROJECT: $_CROSS_PROJ"
58if [ "$_CROSS_PROJ" = "true" ]; then
59 ${ctx.paths.binDir}/gstack-learnings-search --limit 10${queryFlag} --cross-project 2>/dev/null || true
60else
61 ${ctx.paths.binDir}/gstack-learnings-search --limit 10${queryFlag} 2>/dev/null || true
62fi
63\`\`\`
64
65If \`CROSS_PROJECT\` is \`unset\` (first time): Use AskUserQuestion:
66
67> gstack can search learnings from your other projects on this machine to find
68> patterns that might apply here. This stays local (no data leaves your machine).
69> Recommended for solo developers. Skip if you work on multiple client codebases
70> where cross-contamination would be a concern.
71
72Options:
73- A) Enable cross-project learnings (recommended)
74- B) Keep learnings project-scoped only
75
76If A: run \`${ctx.paths.binDir}/gstack-config set cross_project_learnings true\`
77If B: run \`${ctx.paths.binDir}/gstack-config set cross_project_learnings false\`
78
79Then re-run the search with the appropriate flag.
80

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected