@@ -23,6 +23,7 @@ import {
2323 logAttachedKnotsLease ,
2424 terminateKnotsRuntimeLease ,
2525} from "@/lib/knots-lease-runtime" ;
26+ import { logLeaseAudit } from "@/lib/lease-audit" ;
2627import {
2728 forwardTransitionTarget ,
2829 resolveStep ,
@@ -69,14 +70,23 @@ export async function prepareTakeKnots(
6970 leaseId : string ,
7071 loadSnapshot : LoadSnapshotFn ,
7172) : Promise < BackendResult < ExecutionLease > > {
72- const knotsLeaseId = await ensureKnotsLease ( {
73- repoPath : input . repoPath ,
74- source : "structured_prepare_take" ,
75- executionLeaseId : leaseId ,
76- beatId : input . beatId ,
77- interactionType : input . mode ,
78- agentInfo : input . agentInfo ,
79- } ) ;
73+ let knotsLeaseId : string ;
74+ try {
75+ knotsLeaseId = await ensureKnotsLease ( {
76+ repoPath : input . repoPath ,
77+ source : "structured_prepare_take" ,
78+ executionLeaseId : leaseId ,
79+ beatId : input . beatId ,
80+ interactionType : input . mode ,
81+ agentInfo : input . agentInfo ,
82+ } ) ;
83+ } catch ( err ) {
84+ return fail (
85+ err instanceof Error
86+ ? err . message
87+ : "Failed to create Knots lease" ,
88+ ) ;
89+ }
8090 const claimResult = await claimKnot (
8191 input . beatId , input . repoPath , {
8292 agentName : input . agentInfo ?. agentName ,
@@ -115,7 +125,7 @@ async function finalizeTakeKnots(
115125 backend : BackendPort ,
116126 input : PrepareTakeInput ,
117127 leaseId : string ,
118- knotsLeaseId : string | undefined ,
128+ knotsLeaseId : string ,
119129 claimData : { prompt : string ; state : string } ,
120130 loadSnapshot : LoadSnapshotFn ,
121131) : Promise < BackendResult < ExecutionLease > > {
@@ -172,6 +182,27 @@ async function finalizeTakeKnots(
172182 agentInfo : input . agentInfo ,
173183 knotsLeaseId,
174184 } ) ;
185+ void logLeaseAudit ( {
186+ event : "prompt_delivered" ,
187+ repoPath : input . repoPath ,
188+ executionLeaseId : leaseId ,
189+ knotsLeaseId,
190+ beatId : input . beatId ,
191+ interactionType : input . mode ,
192+ agentName : input . agentInfo ?. agentName ,
193+ agentModel : input . agentInfo ?. agentModel ,
194+ agentVersion : input . agentInfo ?. agentVersion ,
195+ outcome : "success" ,
196+ message :
197+ `Execution prompt includes lease ` +
198+ `${ knotsLeaseId } for ${ input . beatId } .` ,
199+ data : {
200+ source : "structured_prepare_take" ,
201+ promptLength : claimData . prompt . length ,
202+ hasLeaseInPrompt :
203+ claimData . prompt . includes ( "--lease" ) ,
204+ } ,
205+ } ) ;
175206 return ok ( lease ) ;
176207}
177208
@@ -297,15 +328,27 @@ export async function preparePollKnots(
297328 loadSnapshot : LoadSnapshotFn ,
298329) : Promise < BackendResult < PollLeaseResult > > {
299330 const executionLeaseId = generateLeaseId ( ) ;
300- const knotsLeaseId = await ensureKnotsLease ( {
301- repoPath : input . repoPath ,
302- source : "structured_prepare_poll" ,
303- executionLeaseId,
304- interactionType : "poll" ,
305- agentInfo : input . agentInfo ,
306- } ) ;
331+ let knotsLeaseId : string ;
332+ try {
333+ knotsLeaseId = await ensureKnotsLease ( {
334+ repoPath : input . repoPath ,
335+ source : "structured_prepare_poll" ,
336+ executionLeaseId,
337+ interactionType : "poll" ,
338+ agentInfo : input . agentInfo ,
339+ } ) ;
340+ } catch ( err ) {
341+ return fail (
342+ err instanceof Error
343+ ? err . message
344+ : "Failed to create Knots lease" ,
345+ ) ;
346+ }
307347 const pollResult = await pollKnot (
308- input . repoPath , input . agentInfo ,
348+ input . repoPath , {
349+ ...input . agentInfo ,
350+ leaseId : knotsLeaseId ,
351+ } ,
309352 ) ;
310353 if ( ! pollResult . ok || ! pollResult . data ) {
311354 await terminateKnotsRuntimeLease ( {
@@ -333,7 +376,7 @@ async function finalizePollKnots(
333376 backend : BackendPort ,
334377 input : PreparePollInput ,
335378 executionLeaseId : string ,
336- knotsLeaseId : string | undefined ,
379+ knotsLeaseId : string ,
337380 pollData : { id : string ; prompt : string ; state : string } ,
338381 loadSnapshot : LoadSnapshotFn ,
339382) : Promise < BackendResult < PollLeaseResult > > {
@@ -391,6 +434,28 @@ async function finalizePollKnots(
391434 agentInfo : input . agentInfo ,
392435 knotsLeaseId,
393436 } ) ;
437+ void logLeaseAudit ( {
438+ event : "prompt_delivered" ,
439+ repoPath : input . repoPath ,
440+ executionLeaseId,
441+ knotsLeaseId,
442+ beatId : pollData . id ,
443+ claimedId : pollData . id ,
444+ interactionType : "poll" ,
445+ agentName : input . agentInfo ?. agentName ,
446+ agentModel : input . agentInfo ?. agentModel ,
447+ agentVersion : input . agentInfo ?. agentVersion ,
448+ outcome : "success" ,
449+ message :
450+ `Poll prompt includes lease ` +
451+ `${ knotsLeaseId } for ${ pollData . id } .` ,
452+ data : {
453+ source : "structured_prepare_poll" ,
454+ promptLength : pollData . prompt . length ,
455+ hasLeaseInPrompt :
456+ pollData . prompt . includes ( "--lease" ) ,
457+ } ,
458+ } ) ;
394459 return ok ( { lease, claimedId : pollData . id } ) ;
395460}
396461
0 commit comments