@@ -626,7 +626,8 @@ pub fn review_prompt(cfg: &RepoCfg, issue: &Value, comments: &[Value], extra: &s
How to work :\ n \
How to work :\ n \
- The issue names its spec as ` Spec : < path > ` ( relative to the repository at your working directory ) . Read the spec and look at the existing code . Do NOT change any file . \ n \
- The issue names its spec as ` Spec : < path > ` ( relative to the repository at your working directory ) . Read the spec and look at the existing code . Do NOT change any file . \ n \
- decision ` approved ` : spec is clear , consistent with the code , buildable . ` question ` : something essential is missing – ask precise questions ( max 5 ) . ` rejected ` : impossible , contradictory or out of scope – say why . \ n \
- decision ` approved ` : spec is clear , consistent with the code , buildable . ` question ` : something essential is missing – ask precise questions ( max 5 ) . ` rejected ` : impossible , contradictory or out of scope – say why . \ n \
- Also write acceptance scenarios ( markdown , checkable by someone who sees only the running app , with exact steps and expected results ) . They are kept away from the builder . \ n \
- Scope : if the issue has a ` Scope : < name > ` line , only that part of the spec is to be built ; judge and write scenarios for it alone . If the spec is split into milestones and the issue names no scope , the scope is the first milestone that does not exist in the code yet – say which one in ` summary ` . Everything outside the scope is out of scope for build and acceptance , however much the spec says about it .
- Also write acceptance scenarios ( markdown , checkable by someone who sees only the running app , with exact steps and expected results ) , only for the scope . They are kept away from the builder . \ n \
- Finish with exactly one fenced json block and nothing after it :\ n \
- Finish with exactly one fenced json block and nothing after it :\ n \
` ` ` json \ n { { \ " decision \" : \" approved|question|rejected \" , \" summary \" : \" one or two sentences \" , \" questions \" : [ \" … \" ], \" reason \" : \" … \" , \" scenarios \" : \" markdown \" }} \n ``` \n {extra} " ,
` ` ` json \ n { { \ " decision \" : \" approved|question|rejected \" , \" summary \" : \" one or two sentences \" , \" questions \" : [ \" … \" ], \" reason \" : \" … \" , \" scenarios \" : \" markdown \" }} \n ``` \n {extra} " ,
url = cfg . url ,
url = cfg . url ,
@@ -834,6 +835,7 @@ pub fn build_prompt(cfg: &RepoCfg, issue: &Value, comments: &[Value], feedback:
How to work :\ n \
How to work :\ n \
- The issue names its spec as ` Spec : < path > ` . Read it and the existing code first , follow the conventions you find . \ n \
- The issue names its spec as ` Spec : < path > ` . Read it and the existing code first , follow the conventions you find . \ n \
- Do NOT edit the spec file and do NOT run ` git commit ` , ` git push ` or anything that talks to the remote – the factory commits and pushes your changes itself . \ n \
- Do NOT edit the spec file and do NOT run ` git commit ` , ` git push ` or anything that talks to the remote – the factory commits and pushes your changes itself . \ n \
- Scope : build only what the issue ' s ` Scope : < name > ` line names ; without one , the next milestone of the spec that does not exist in the code yet ( state which in ` summary ` ) . Everything else in the spec is out of scope . \ n \
- Build what is specified , nothing more . Add or update tests where the project has a test setup , then run the tests / linters / build that are available and fix what breaks . \ n \
- Build what is specified , nothing more . Add or update tests where the project has a test setup , then run the tests / linters / build that are available and fix what breaks . \ n \
- If you cannot verify something ( no browser , no network , missing tools ) , say so openly in ` notes ` instead of claiming it works . \ n \
- If you cannot verify something ( no browser , no network , missing tools ) , say so openly in ` notes ` instead of claiming it works . \ n \
- If the request cannot be built as specified , make no changes and answer ` blocked ` with the reason . \ n \
- If the request cannot be built as specified , make no changes and answer ` blocked ` with the reason . \ n \
@@ -979,6 +981,7 @@ pub fn qa_prompt(cfg: &RepoCfg, issue: &Value, scenarios: &str, extra: &str) ->
How to work :\ n \
How to work :\ n \
- The repository at your working directory is the built branch . Do NOT change , commit or push anything ; you are the checker , not the builder . \ n \
- The repository at your working directory is the built branch . Do NOT change , commit or push anything ; you are the checker , not the builder . \ n \
- Start the app as ` . factory / setup . json ` says ( keys : ` install ` – optional setup command , ` start ` – command that starts the app , ` url ` – where it listens , e . g . http ://localhost:8080). Without that file work out the start from the README and package files and say so.\n\
- Start the app as ` . factory / setup . json ` says ( keys : ` install ` – optional setup command , ` start ` – command that starts the app , ` url ` – where it listens , e . g . http ://localhost:8080). Without that file work out the start from the README and package files and say so.\n\
- Judge ONLY the scenarios listed above . Everything else in the issue or the spec – other milestones , features no scenario mentions , things that are simply not built yet – is out of scope : do not test it , do not fail anything because of it . Mention such gaps at most in ` summary ` , never as ` fail ` and never in ` findings ` . \ n \
- Go through every scenario with the real running app . Use the ` browser ` tool for anything visual or interactive , ` curl ` or the project ' s own test runner for the rest . Judge only what you saw , not what the code suggests . \ n \
- Go through every scenario with the real running app . Use the ` browser ` tool for anything visual or interactive , ` curl ` or the project ' s own test runner for the rest . Judge only what you saw , not what the code suggests . \ n \
- Per scenario answer ` pass ` , ` fail ` ( it behaves differently – say how ) or ` unverified ` ( you could not check it , e . g . a missing tool ; say why ) . Never mark something ` pass ` you did not observe . \ n \
- Per scenario answer ` pass ` , ` fail ` ( it behaves differently – say how ) or ` unverified ` ( you could not check it , e . g . a missing tool ; say why ) . Never mark something ` pass ` you did not observe . \ n \
- Stop everything you started ( app server , background jobs ) before you finish . \ n \
- Stop everything you started ( app server , background jobs ) before you finish . \ n \
@@ -1018,10 +1021,17 @@ async fn qa(app: Arc<App>, user: User, cfg: RepoCfg, issue: Value) {
Some ( b ) = > b . to_string ( ) ,
Some ( b ) = > b . to_string ( ) ,
None = > return qa_fail ( & app , & g , & cfg , n , " the issue has no built branch " ) . await ,
None = > return qa_fail ( & app , & g , & cfg , n , " the issue has no built branch " ) . await ,
} ;
} ;
let ( work , _ ) = match prepare_worktree ( & app , & user , & cfg , n , & branch , true ) . await {
let ( work , base ) = match prepare_worktree ( & app , & user , & cfg , n , & branch , true ) . await {
Ok ( r ) = > r ,
Ok ( r ) = > r ,
Err ( e ) = > return qa_fail ( & app , & g , & cfg , n , & format! ( " could not check out the branch: {e:#} " ) ) . await ,
Err ( e ) = > return qa_fail ( & app , & g , & cfg , n , & format! ( " could not check out the branch: {e:#} " ) ) . await ,
} ;
} ;
// The branch may predate the setup file: fall back to the one on the default branch.
if ! work . join ( " .factory/setup.json " ) . exists ( ) {
if let Ok ( text ) = git ( & cfg , & work , & [ " show " , & format! ( " origin/ {base} :.factory/setup.json " ) ] ) . await {
let _ = std ::fs ::create_dir_all ( work . join ( " .factory " ) ) ;
let _ = std ::fs ::write ( work . join ( " .factory/setup.json " ) , text ) ;
}
}
let prompt = qa_prompt ( & cfg , & issue , row [ " scenarios " ] . as_str ( ) . unwrap_or ( " " ) , & cfg . qa . extra_prompt ) ;
let prompt = qa_prompt ( & cfg , & issue , row [ " scenarios " ] . as_str ( ) . unwrap_or ( " " ) , & cfg . qa . extra_prompt ) ;
let title = format! ( " Factory # {n} – acceptance {} – {} " , if round > 0 { format! ( " (round {} ) " , round + 1 ) } else { String ::new ( ) } , issue [ " title " ] . as_str ( ) . unwrap_or ( " " ) ) ;
let title = format! ( " Factory # {n} – acceptance {} – {} " , if round > 0 { format! ( " (round {} ) " , round + 1 ) } else { String ::new ( ) } , issue [ " title " ] . as_str ( ) . unwrap_or ( " " ) ) ;
let started = start_phase ( & app , & user , & cfg , n , " qa " , round , prompt , & title , Some ( & work ) ) ;
let started = start_phase ( & app , & user , & cfg , n , " qa " , round , prompt , & title , Some ( & work ) ) ;
@@ -1052,8 +1062,9 @@ async fn qa(app: Arc<App>, user: User, cfg: RepoCfg, issue: Value) {
let cell = | k : & str | r [ k ] . as_str ( ) . unwrap_or ( " " ) . replace ( '|' , " \\ | " ) . replace ( '\n' , " " ) ;
let cell = | k : & str | r [ k ] . as_str ( ) . unwrap_or ( " " ) . replace ( '|' , " \\ | " ) . replace ( '\n' , " " ) ;
table . push_str ( & format! ( " | {} | {} | {} | \n " , cell ( " scenario " ) , cell ( " result " ) , cell ( " evidence " ) ) ) ;
table . push_str ( & format! ( " | {} | {} | {} | \n " , cell ( " scenario " ) , cell ( " result " ) , cell ( " evidence " ) ) ) ;
}
}
// One failed scenario fails the acceptance, whatever the model's own verdict says .
// The scenarios decide, not the model's overall verdict (which tends to count out-of-scope gaps) .
if res [ " decision " ] . as_str ( ) ! = Some ( " pass " ) | | fail > 0 {
let failed = if results . is_empty ( ) { res [ " decision " ] . as_str ( ) ! = Some ( " pass " ) } else { fail > 0 } ;
if failed {
let findings = res [ " findings " ] . as_str ( ) . filter ( | f | ! f . trim ( ) . is_empty ( ) ) . unwrap_or ( & summary ) . trim ( ) . to_string ( ) ;
let findings = res [ " findings " ] . as_str ( ) . filter ( | f | ! f . trim ( ) . is_empty ( ) ) . unwrap_or ( & summary ) . trim ( ) . to_string ( ) ;
if round > = MAX_REWORK {
if round > = MAX_REWORK {
set ( & [ ( " state " , json! ( " failed " ) ) , ( " note " , json! ( findings . clone ( ) ) ) ] ) ;
set ( & [ ( " state " , json! ( " failed " ) ) , ( " note " , json! ( findings . clone ( ) ) ) ] ) ;
@@ -1188,7 +1199,10 @@ How to work:
2. Keep the spec in the file ` spec . md ` in your working directory and update it as the conversation moves on . Structure : Goal · Users · Features ( numbered , each with acceptance criteria that can be checked by looking at the running app ) · Non - functional requirements · Tech stack · Out of scope · Open questions .
2. Keep the spec in the file ` spec . md ` in your working directory and update it as the conversation moves on . Structure : Goal · Users · Features ( numbered , each with acceptance criteria that can be checked by looking at the running app ) · Non - functional requirements · Tech stack · Out of scope · Open questions .
3. Be concrete : exact texts , limits , behaviour on errors . Point out contradictions and gaps instead of guessing .
3. Be concrete : exact texts , limits , behaviour on errors . Point out contradictions and gaps instead of guessing .
4. Do not build the software and do not touch other files . Do not create repositories yourself .
4. Do not build the software and do not touch other files . Do not create repositories yourself .
5. When the user says the spec is done , propose a short project name and a repository slug ( lowercase , digits and dashes ) and end your message with exactly one fenced json block and nothing after it :
5. Also keep the file ` setup . json ` in your working directory : it tells the acceptance stage how to start the finished app , so the app can be checked by looking at it . Once the tech stack is settled write it , and update it when the stack changes . Format ( one JSON object , all values strings , no comments ) :
` { " install " : " optional one-off setup command, e.g. npm ci " , " start " : " command that runs the app in the foreground " , " url " : " where it listens, e.g. http://localhost:8080 " } `
Ask the user for the port and commands if the spec leaves them open . A static site may use e . g . ` " start " : " python3 -m http.server 8080 " ` . Without a start command acceptance has to guess , so do not skip this file for anything that runs .
6. When the user says the spec is done , propose a short project name and a repository slug ( lowercase , digits and dashes ) and end your message with exactly one fenced json block and nothing after it :
` ` ` json
` ` ` json
{ " decision " : " ready " , " name " : " Project name " , " slug " : " project-name " , " description " : " one sentence " }
{ " decision " : " ready " , " name " : " Project name " , " slug " : " project-name " , " description " : " one sentence " }
` ` `
` ` `
@@ -1269,9 +1283,14 @@ pub fn slugify(s: &str) -> String {
out . trim_end_matches ( '-' ) . chars ( ) . take ( 60 ) . collect ::< String > ( ) . trim_end_matches ( '-' ) . to_string ( )
out . trim_end_matches ( '-' ) . chars ( ) . take ( 60 ) . collect ::< String > ( ) . trim_end_matches ( '-' ) . to_string ( )
}
}
/// Is this a usable `.factory/setup.json`: a JSON object with a non-empty `start` command?
pub fn valid_setup ( text : & str ) -> bool {
serde_json ::from_str ::< Value > ( text ) . ok ( ) . is_some_and ( | v | v [ " start " ] . as_str ( ) . is_some_and ( | s | ! s . trim ( ) . is_empty ( ) ) )
}
/// Create the repository, file README and spec, register the repository and open the issue
/// Create the repository, file README and spec, register the repository and open the issue
/// that starts the workflow. Safe to repeat after a failure: an empty repository is reused.
/// that starts the workflow. Safe to repeat after a failure: an empty repository is reused.
pub async fn create_project ( app : & Arc < App > , user : & User , settings : & mut FactorySettings , spec : & str , connection : & str , name : & str , slug : & str , description : & str , owner : & str ) -> Result < Value > {
pub async fn create_project ( app : & Arc < App > , user : & User , settings : & mut FactorySettings , spec : & str , setup : & str , connection : & str , name : & str , slug : & str , description : & str , owner : & str ) -> Result < Value > {
let slug = slugify ( slug ) ;
let slug = slugify ( slug ) ;
if slug . len ( ) < 2 {
if slug . len ( ) < 2 {
bail! ( " the repository name is too short " ) ;
bail! ( " the repository name is too short " ) ;
@@ -1308,7 +1327,14 @@ pub async fn create_project(app: &Arc<App>, user: &User, settings: &mut FactoryS
let id : crate ::gitcreds ::GitSettings = app . store . user_setting ( & user . id , " git " ) . unwrap_or_default ( ) ;
let id : crate ::gitcreds ::GitSettings = app . store . user_setting ( & user . id , " git " ) . unwrap_or_default ( ) ;
let ( who , mail ) = ( Some ( id . name . trim ( ) ) . filter ( | n | ! n . is_empty ( ) ) . unwrap_or ( " CodeCraft Factory " ) . to_string ( ) , Some ( id . email . trim ( ) ) . filter ( | n | ! n . is_empty ( ) ) . unwrap_or ( " factory@codecraft.local " ) . to_string ( ) ) ;
let ( who , mail ) = ( Some ( id . name . trim ( ) ) . filter ( | n | ! n . is_empty ( ) ) . unwrap_or ( " CodeCraft Factory " ) . to_string ( ) , Some ( id . email . trim ( ) ) . filter ( | n | ! n . is_empty ( ) ) . unwrap_or ( " factory@codecraft.local " ) . to_string ( ) ) ;
git ( & resolved , & dir , & [ " checkout " , " -B " , " main " ] ) . await ? ;
git ( & resolved , & dir , & [ " checkout " , " -B " , " main " ] ) . await ? ;
git ( & resolved , & dir , & [ " add " , " README.md " , " docs " ] ) . await ? ;
let mut add = vec! [ " add " , " README.md " , " docs " ] ;
let setup_filed = valid_setup ( setup ) ;
if setup_filed {
std ::fs ::create_dir_all ( dir . join ( " .factory " ) ) ? ;
std ::fs ::write ( dir . join ( " .factory/setup.json " ) , setup ) ? ;
add . push ( " .factory " ) ;
}
git ( & resolved , & dir , & add ) . await ? ;
git ( & resolved , & dir , & [ " -c " , & format! ( " user.name= {who} " ) , " -c " , & format! ( " user.email= {mail} " ) , " commit " , " -m " , & format! ( " Add specification for {name} " ) ] ) . await ? ;
git ( & resolved , & dir , & [ " -c " , & format! ( " user.name= {who} " ) , " -c " , & format! ( " user.email= {mail} " ) , " commit " , " -m " , & format! ( " Add specification for {name} " ) ] ) . await ? ;
git ( & resolved , & dir , & [ " push " , " -u " , " origin " , " main " ] ) . await ? ;
git ( & resolved , & dir , & [ " push " , " -u " , " origin " , " main " ] ) . await ? ;
@@ -1318,7 +1344,7 @@ pub async fn create_project(app: &Arc<App>, user: &User, settings: &mut FactoryS
let issue = g . create_issue ( name , & format! ( " {} \n \n Spec: {spec_path} " , description . trim ( ) ) , & [ cfg . label . clone ( ) ] ) . await ? ;
let issue = g . create_issue ( name , & format! ( " {} \n \n Spec: {spec_path} " , description . trim ( ) ) , & [ cfg . label . clone ( ) ] ) . await ? ;
let n = issue_no ( & issue ) ;
let n = issue_no ( & issue ) ;
app . store . factory_issue_add ( & cfg . url , n , & user . id , name , & login ) ? ;
app . store . factory_issue_add ( & cfg . url , n , & user . id , name , & login ) ? ;
Ok ( json! ( { " url " : cfg . url , " repo " : created [ " full_name " ] , " issue " : n , " issue_url " : issue [ " html_url " ] , " spec " : spec_path } ) )
Ok ( json! ( { " url " : cfg . url , " repo " : created [ " full_name " ] , " issue " : n , " issue_url " : issue [ " html_url " ] , " spec " : spec_path , " setup " : setup_filed } ) )
}
}
#[ cfg(test) ]
#[ cfg(test) ]
@@ -1447,6 +1473,16 @@ mod tests {
assert! ( SPEC_INSTRUCTIONS . contains ( " \" decision \" : \" ready \" " ) ) ;
assert! ( SPEC_INSTRUCTIONS . contains ( " \" decision \" : \" ready \" " ) ) ;
}
}
#[ test ]
fn setup_json_needs_a_start_command ( ) {
assert! ( valid_setup ( r # "{"start": "npm start", "url": "http://localhost:3000"}"# ) ) ;
assert! ( ! valid_setup ( r # "{"start": " "}"# ) ) ;
assert! ( ! valid_setup ( r # "{"url": "x"}"# ) ) ;
assert! ( ! valid_setup ( " not json " ) ) ;
assert! ( ! valid_setup ( " " ) ) ;
assert! ( SPEC_INSTRUCTIONS . contains ( " setup.json " ) ) ;
}
#[ test ]
#[ test ]
fn encodes_labels ( ) {
fn encodes_labels ( ) {
assert_eq! ( urlenc ( " factory:qa " ) , " factory%3Aqa " ) ;
assert_eq! ( urlenc ( " factory:qa " ) , " factory%3Aqa " ) ;