99 * DIFFERENT aspect of the documentation (its own choice of scope — a folder,
1010 * feature, topic, etc.).
1111 *
12- * After the AI finishes, the script updates automated_updates_data.json so
13- * future runs don't repeat the same improvements.
12+ * The AI agent directly adds a new entry to the "last_improved_things" array
13+ * inside automated_updates_data.json, so the change is tracked in the same
14+ * commit as the documentation edits.
1415 */
1516
1617const { execSync } = require ( "child_process" ) ;
@@ -24,7 +25,7 @@ const GDEVELOP_REPO = "https://github.com/4ian/GDevelop.git";
2425const GDEVELOP_DIR = process . env . GDEVELOP_DIR || "/tmp/GDevelop" ;
2526const REPO_ROOT = path . resolve ( __dirname , ".." ) ;
2627const DATA_FILE = path . join ( REPO_ROOT , "automated_updates_data.json" ) ;
27- const SUMMARY_FILE = path . join ( REPO_ROOT , "improvement_summary .txt") ;
28+ const SUMMARY_OUT = "/tmp/ai_summary .txt";
2829
2930// AI provider — switch by changing this value (or set AI_PROVIDER env var).
3031// Supported values: "claude" | "codex"
@@ -37,6 +38,14 @@ function run(cmd, opts = {}) {
3738 return execSync ( cmd , { encoding : "utf8" , ...opts } ) . trim ( ) ;
3839}
3940
41+ function writeDataFile ( data ) {
42+ let json = JSON . stringify ( data , null , 2 ) ;
43+ // Keep empty arrays on separate lines so future additions produce clean diffs
44+ json = json . replace ( / " l a s t _ i m p r o v e d _ t h i n g s " : \[ \s * \] / g,
45+ '"last_improved_things": [\n\n ]' ) ;
46+ fs . writeFileSync ( DATA_FILE , json + "\n" ) ;
47+ }
48+
4049// ---------------------------------------------------------------------------
4150// Main
4251// ---------------------------------------------------------------------------
@@ -48,6 +57,7 @@ function main() {
4857 }
4958
5059 const alreadyImproved = data . last_improved_things || [ ] ;
60+ const previousCount = alreadyImproved . length ;
5161
5262 // 2. Clone / update GDevelop
5363 if ( ! fs . existsSync ( path . join ( GDEVELOP_DIR , ".git" ) ) ) {
@@ -71,35 +81,50 @@ function main() {
7181 { cwd : REPO_ROOT }
7282 ) ;
7383
74- // 4. Clean up any leftover summary file
75- if ( fs . existsSync ( SUMMARY_FILE ) ) fs . unlinkSync ( SUMMARY_FILE ) ;
76-
77- // 5. Build prompt
84+ // 4. Build prompt
7885 const prompt = buildPrompt ( alreadyImproved , docTree ) ;
7986
8087 const promptFile = "/tmp/doc_improve_prompt.txt" ;
8188 fs . writeFileSync ( promptFile , prompt ) ;
8289 console . log ( `Prompt written to ${ promptFile } (${ prompt . length } chars)` ) ;
8390
84- // 6 . Invoke AI agent
91+ // 5 . Invoke AI agent
8592 console . log ( `\nInvoking AI agent (${ AI_PROVIDER } )…\n` ) ;
86- invokeAI ( promptFile , REPO_ROOT ) ;
87-
88- // 7. Read back the improvement summary the AI wrote
89- let summary = "(no summary provided)" ;
90- if ( fs . existsSync ( SUMMARY_FILE ) ) {
91- summary = fs . readFileSync ( SUMMARY_FILE , "utf8" ) . trim ( ) ;
92- // Clean up temp file — we don't want it committed
93- fs . unlinkSync ( SUMMARY_FILE ) ;
93+ const aiOutput = invokeAI ( promptFile , REPO_ROOT ) ;
94+
95+ // 6. Read back the data file to find what the AI added
96+ let updatedData = data ;
97+ try {
98+ updatedData = JSON . parse ( fs . readFileSync ( DATA_FILE , "utf8" ) ) ;
99+ } catch ( err ) {
100+ console . error ( "Warning: could not re-read automated_updates_data.json:" , err . message ) ;
101+ }
102+
103+ const updatedList = updatedData . last_improved_things || [ ] ;
104+ let summary ;
105+
106+ if ( updatedList . length > previousCount ) {
107+ // The AI added one or more entries — take the last one
108+ const newEntry = updatedList [ updatedList . length - 1 ] ;
109+ summary = newEntry . summary || "(no summary in new entry)" ;
110+ console . log ( `\nAI added improvement entry: [${ newEntry . date } ] ${ summary } ` ) ;
111+ } else {
112+ // AI didn't update the file — add a fallback entry ourselves
113+ summary = aiOutput
114+ ? aiOutput . split ( "\n" ) . filter ( Boolean ) . slice ( 0 , 3 ) . join ( " " ) . substring ( 0 , 200 )
115+ : "(no summary provided)" ;
116+ updatedData . last_improved_things = updatedList ;
117+ updatedData . last_improved_things . push ( {
118+ date : new Date ( ) . toISOString ( ) . split ( "T" ) [ 0 ] ,
119+ summary,
120+ } ) ;
121+ writeDataFile ( updatedData ) ;
122+ console . log ( `\nFallback: script added improvement entry: ${ summary } ` ) ;
94123 }
95124
96- // 8. Update tracking data
97- data . last_improved_things . push ( {
98- date : new Date ( ) . toISOString ( ) . split ( "T" ) [ 0 ] ,
99- summary,
100- } ) ;
101- fs . writeFileSync ( DATA_FILE , JSON . stringify ( data , null , 2 ) + "\n" ) ;
102- console . log ( `\nImprovement recorded: ${ summary } ` ) ;
125+ // 7. Write summary for the workflow to pick up
126+ fs . writeFileSync ( SUMMARY_OUT , summary ) ;
127+ console . log ( `Summary written to ${ SUMMARY_OUT } ` ) ;
103128}
104129
105130// ---------------------------------------------------------------------------
@@ -111,6 +136,8 @@ function buildPrompt(alreadyImproved, docTree) {
111136 ? alreadyImproved . map ( ( e ) => ` • [${ e . date } ] ${ e . summary } ` ) . join ( "\n" )
112137 : " (none yet — this is the first run)" ;
113138
139+ const today = new Date ( ) . toISOString ( ) . split ( "T" ) [ 0 ] ;
140+
114141 return `You are an expert technical writer improving GDevelop's documentation.
115142
116143DOCUMENTATION LOCATION
@@ -147,14 +174,23 @@ IMPORTANT CONSTRAINTS
147174 – Every "reference.md" file under docs/gdevelop5/all-features/*/reference.md
148175 – docs/gdevelop5/all-features/expressions-reference.md
149176• NEVER create new files unless absolutely necessary.
150- • NEVER edit files outside docs/gdevelop5/.
177+ • NEVER edit files outside docs/gdevelop5/ (except automated_updates_data.json as described below) .
151178• Do NOT touch images or binary files.
152179
153180WHEN YOU ARE DONE
154181-----------------
155- Create a file called "improvement_summary.txt" in ${ REPO_ROOT } / containing
156- a single line that describes what you improved.
157- Example: "Improved getting_started/index.md — added clearer first-project walkthrough steps"
182+ Edit the file "automated_updates_data.json" in ${ REPO_ROOT } / and add a new
183+ entry at the END of the "last_improved_things" array with this format:
184+
185+ { "date": "${ today } ", "summary": "<one-line description of what you improved>" }
186+
187+ For example, the file might look like:
188+ {
189+ "last_automated_updates_commit": null,
190+ "last_improved_things": [
191+ { "date": "${ today } ", "summary": "Improved objects/sprite/index.md — clarified animation looping behaviour" }
192+ ]
193+ }
158194
159195Make your changes now.` ;
160196}
@@ -178,11 +214,11 @@ function invokeAI(promptFile, cwd) {
178214 throw new Error ( `Unknown AI_PROVIDER: "${ AI_PROVIDER } ". Use "claude" or "codex".` ) ;
179215 }
180216
181- // Run with stdout/stderr piped so we can capture and log the full output.
182217 const output = execSync ( cmd , { ...opts , encoding : "utf8" , stdio : [ "pipe" , "pipe" , "pipe" ] } ) ;
183218 console . log ( "── AI agent output ─────────────────────────────────────────" ) ;
184219 console . log ( output ) ;
185220 console . log ( "── End of AI agent output ──────────────────────────────────" ) ;
221+ return output ;
186222}
187223
188224// ---------------------------------------------------------------------------
0 commit comments