langfun 0.1.2.dev202511010804__py3-none-any.whl → 0.1.2.dev202511030805__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of langfun might be problematic. Click here for more details.

@@ -41,6 +41,35 @@ class IdTest(unittest.TestCase):
41
41
  )
42
42
  self.assertIsNone(sandbox_id.working_dir(root_dir=None))
43
43
 
44
+ def test_feature_id(self):
45
+ # For non-sandboxed feature.
46
+ feature_id = interface.Feature.Id(
47
+ container_id=interface.Environment.Id('env'),
48
+ feature_name='feature'
49
+ )
50
+ self.assertEqual(str(feature_id), 'env/feature')
51
+ self.assertEqual(
52
+ feature_id.working_dir(root_dir='/tmp'),
53
+ '/tmp/env/feature'
54
+ )
55
+ self.assertIsNone(feature_id.working_dir(root_dir=None))
56
+
57
+ # For sandboxed feature.
58
+ feature_id = interface.Feature.Id(
59
+ container_id=interface.Sandbox.Id(
60
+ environment_id=interface.Environment.Id('env'),
61
+ image_id='image1',
62
+ sandbox_id='0'
63
+ ),
64
+ feature_name='feature'
65
+ )
66
+ self.assertEqual(str(feature_id), 'env/image1:0/feature')
67
+ self.assertEqual(
68
+ feature_id.working_dir(root_dir='/tmp'),
69
+ '/tmp/env/image1/0/feature'
70
+ )
71
+ self.assertIsNone(feature_id.working_dir(root_dir=None))
72
+
44
73
 
45
74
  class TestingSandbox(interface.Sandbox):
46
75
 
@@ -109,7 +138,7 @@ class DecoratorTest(unittest.TestCase):
109
138
 
110
139
  class SandboxB(TestingSandbox):
111
140
 
112
- @interface.log_sandbox_activity()
141
+ @interface.log_activity()
113
142
  def bar(self, x: str) -> None:
114
143
  pass
115
144
 
langfun/env/test_utils.py CHANGED
@@ -112,7 +112,7 @@ class TestingSandbox(base_sandbox.BaseSandbox):
112
112
  super()._shutdown()
113
113
 
114
114
  @interface.treat_as_sandbox_state_error(errors=(RuntimeError,))
115
- @interface.log_sandbox_activity()
115
+ @interface.log_activity()
116
116
  def shell(
117
117
  self,
118
118
  code: str,
@@ -184,19 +184,19 @@ class TestingFeature(base_feature.BaseFeature):
184
184
  if self.call_end_session_on_teardown_session:
185
185
  self.sandbox.end_session()
186
186
 
187
- @interface.log_sandbox_activity()
187
+ @interface.log_activity()
188
188
  def num_shell_calls(self) -> int:
189
189
  return len(self.sandbox._shell_history) # pylint: disable=protected-access
190
190
 
191
- @interface.log_sandbox_activity()
191
+ @interface.log_activity()
192
192
  def bad_shell_call(self) -> None:
193
193
  self.sandbox.shell('bad command', raise_error=RuntimeError)
194
194
 
195
- @interface.log_sandbox_activity()
195
+ @interface.log_activity()
196
196
  def show_session_id(self):
197
197
  return self.session_id
198
198
 
199
- @interface.log_sandbox_activity()
199
+ @interface.log_activity()
200
200
  def call_with_varargs(self, code: str, *args, **kwargs):
201
201
  del code, args, kwargs
202
202
  return 0
@@ -220,6 +220,43 @@ class TestingFeature(base_feature.BaseFeature):
220
220
  )
221
221
 
222
222
 
223
+ class TestingNonSandboxBasedFeature(base_feature.BaseFeature):
224
+ """Testing non-sandbox based feature for unit tests."""
225
+ is_sandbox_based: bool = False
226
+ simulate_setup_error: Type[BaseException] | None = None
227
+ simulate_teardown_error: Type[BaseException] | None = None
228
+ simulate_setup_session_error: Type[BaseException] | None = None
229
+ simulate_teardown_session_error: Type[BaseException] | None = None
230
+ simulate_housekeep_error: Type[BaseException] | None = None
231
+
232
+ def _setup(self) -> None:
233
+ if self.simulate_setup_error:
234
+ raise self.simulate_setup_error('Feature setup error')
235
+
236
+ def _teardown(self) -> None:
237
+ if self.simulate_teardown_error:
238
+ raise self.simulate_teardown_error('Feature teardown error')
239
+
240
+ def _setup_session(self) -> None:
241
+ if self.simulate_setup_session_error:
242
+ raise self.simulate_setup_session_error('Feature session setup error')
243
+
244
+ def _teardown_session(self) -> None:
245
+ if self.simulate_teardown_session_error:
246
+ raise self.simulate_teardown_session_error(
247
+ 'Feature session teardown error'
248
+ )
249
+
250
+ def _housekeep(self) -> None:
251
+ if self.simulate_housekeep_error:
252
+ raise self.simulate_housekeep_error('Feature housekeeping error')
253
+ _ = self.foo(1)
254
+
255
+ @interface.log_activity()
256
+ def foo(self, x: int) -> int:
257
+ return x + 1
258
+
259
+
223
260
  class TestingEventHandler(pg.Object, interface.EventHandler):
224
261
  """Testing environment event handler for unit tests."""
225
262
 
@@ -281,7 +318,6 @@ class TestingEventHandler(pg.Object, interface.EventHandler):
281
318
 
282
319
  def on_sandbox_start(
283
320
  self,
284
- environment: interface.Environment,
285
321
  sandbox: interface.Sandbox,
286
322
  duration: float,
287
323
  error: BaseException | None
@@ -291,7 +327,6 @@ class TestingEventHandler(pg.Object, interface.EventHandler):
291
327
 
292
328
  def on_sandbox_status_change(
293
329
  self,
294
- environment: interface.Environment,
295
330
  sandbox: interface.Sandbox,
296
331
  old_status: interface.Sandbox.Status,
297
332
  new_status: interface.Sandbox.Status,
@@ -306,7 +341,6 @@ class TestingEventHandler(pg.Object, interface.EventHandler):
306
341
 
307
342
  def on_sandbox_shutdown(
308
343
  self,
309
- environment: interface.Environment,
310
344
  sandbox: interface.Sandbox,
311
345
  duration: float,
312
346
  lifetime: float,
@@ -315,9 +349,53 @@ class TestingEventHandler(pg.Object, interface.EventHandler):
315
349
  assert duration > 0 and lifetime is not None
316
350
  self._add_message(f'[{sandbox.id}] sandbox shutdown', error)
317
351
 
352
+ def on_sandbox_session_start(
353
+ self,
354
+ sandbox: interface.Sandbox,
355
+ session_id: str,
356
+ duration: float,
357
+ error: BaseException | None
358
+ ) -> None:
359
+ """Called when a sandbox session starts."""
360
+ assert duration > 0
361
+ self._add_message(
362
+ f'[{sandbox.id}] session {session_id!r} started', error
363
+ )
364
+
365
+ def on_sandbox_session_end(
366
+ self,
367
+ sandbox: interface.Sandbox,
368
+ session_id: str,
369
+ duration: float,
370
+ lifetime: float,
371
+ error: BaseException | None
372
+ ) -> None:
373
+ """Called when a sandbox session ends."""
374
+ assert duration > 0 and lifetime > 0
375
+ self._add_message(
376
+ f'[{sandbox.id}] session {session_id!r} ended', error
377
+ )
378
+
379
+ def on_sandbox_activity(
380
+ self,
381
+ name: str,
382
+ sandbox: interface.Sandbox,
383
+ session_id: str | None,
384
+ duration: float,
385
+ error: BaseException | None,
386
+ *,
387
+ code: str | None = None,
388
+ **kwargs
389
+ ) -> None:
390
+ """Called when a sandbox activity is performed."""
391
+ del kwargs
392
+ log_id = f'{sandbox.id}@{session_id or "<idle>"}'
393
+ self._add_message(
394
+ f'[{log_id}] {name}: {code}', error
395
+ )
396
+
318
397
  def on_sandbox_housekeep(
319
398
  self,
320
- environment: interface.Environment,
321
399
  sandbox: interface.Sandbox,
322
400
  counter: int,
323
401
  duration: float,
@@ -332,8 +410,6 @@ class TestingEventHandler(pg.Object, interface.EventHandler):
332
410
 
333
411
  def on_feature_setup(
334
412
  self,
335
- environment: interface.Environment,
336
- sandbox: interface.Sandbox,
337
413
  feature: interface.Feature,
338
414
  duration: float,
339
415
  error: BaseException | None
@@ -342,13 +418,11 @@ class TestingEventHandler(pg.Object, interface.EventHandler):
342
418
  assert duration > 0
343
419
  if self.log_feature_setup:
344
420
  self._add_message(
345
- f'[{sandbox.id}/{feature.name}] feature setup', error
421
+ f'[{feature.id}] feature setup', error
346
422
  )
347
423
 
348
424
  def on_feature_teardown(
349
425
  self,
350
- environment: interface.Environment,
351
- sandbox: interface.Sandbox,
352
426
  feature: interface.Feature,
353
427
  duration: float,
354
428
  error: BaseException | None
@@ -357,13 +431,11 @@ class TestingEventHandler(pg.Object, interface.EventHandler):
357
431
  assert duration > 0
358
432
  if self.log_feature_setup:
359
433
  self._add_message(
360
- f'[{sandbox.id}/{feature.name}] feature teardown', error
434
+ f'[{feature.id}] feature teardown', error
361
435
  )
362
436
 
363
437
  def on_feature_setup_session(
364
438
  self,
365
- environment: interface.Environment,
366
- sandbox: interface.Sandbox,
367
439
  feature: interface.Feature,
368
440
  session_id: str | None,
369
441
  duration: float,
@@ -373,13 +445,12 @@ class TestingEventHandler(pg.Object, interface.EventHandler):
373
445
  assert duration > 0
374
446
  if self.log_session_setup:
375
447
  self._add_message(
376
- f'[{sandbox.id}/{feature.name}] feature setup session', error
448
+ f'[{feature.id}@{session_id or "<idle>"}] feature setup session',
449
+ error
377
450
  )
378
451
 
379
452
  def on_feature_teardown_session(
380
453
  self,
381
- environment: interface.Environment,
382
- sandbox: interface.Sandbox,
383
454
  feature: interface.Feature,
384
455
  session_id: str,
385
456
  duration: float,
@@ -389,77 +460,38 @@ class TestingEventHandler(pg.Object, interface.EventHandler):
389
460
  assert duration > 0
390
461
  if self.log_session_setup:
391
462
  self._add_message(
392
- f'[{sandbox.id}/{feature.name}] feature teardown session', error
463
+ f'[{feature.id}@{session_id}] feature teardown session', error
393
464
  )
394
465
 
395
- def on_feature_housekeep(
466
+ def on_feature_activity(
396
467
  self,
397
- environment: interface.Environment,
398
- sandbox: interface.Sandbox,
468
+ name: str,
399
469
  feature: interface.Feature,
400
- counter: int,
470
+ session_id: str | None,
401
471
  duration: float,
402
472
  error: BaseException | None,
473
+ *,
474
+ code: str | None = None,
403
475
  **kwargs
404
476
  ) -> None:
405
- """Called when a sandbox feature is housekeeping."""
406
- assert duration > 0
407
- if self.log_housekeep:
408
- self._add_message(
409
- f'[{sandbox.id}/{feature.name}] feature housekeeping {counter}', error
410
- )
411
-
412
- def on_session_start(
413
- self,
414
- environment: interface.Environment,
415
- sandbox: interface.Sandbox,
416
- session_id: str,
417
- duration: float,
418
- error: BaseException | None
419
- ) -> None:
420
- """Called when a sandbox session starts."""
421
- assert duration > 0
422
- self._add_message(
423
- f'[{sandbox.id}] session {session_id!r} started', error
424
- )
425
-
426
- def on_session_end(
427
- self,
428
- environment: interface.Environment,
429
- sandbox: interface.Sandbox,
430
- session_id: str,
431
- duration: float,
432
- lifetime: float,
433
- error: BaseException | None
434
- ) -> None:
435
- """Called when a sandbox session ends."""
436
- assert duration > 0 and lifetime > 0
477
+ """Called when a sandbox activity is performed."""
478
+ del kwargs
479
+ log_id = f'{feature.id}@{session_id or "<idle>"}'
437
480
  self._add_message(
438
- f'[{sandbox.id}] session {session_id!r} ended', error
481
+ f'[{log_id}] {name}: {code}', error
439
482
  )
440
483
 
441
- def on_sandbox_activity(
484
+ def on_feature_housekeep(
442
485
  self,
443
- name: str,
444
- environment: interface.Environment,
445
- sandbox: interface.Sandbox,
446
- feature: interface.Feature | None,
447
- session_id: str | None,
486
+ feature: interface.Feature,
487
+ counter: int,
448
488
  duration: float,
449
489
  error: BaseException | None,
450
- *,
451
- code: str | None = None,
452
490
  **kwargs
453
491
  ) -> None:
454
- """Called when a sandbox activity is performed."""
455
- del environment, kwargs
456
- if session_id is None:
457
- log_id = sandbox.id
458
- else:
459
- log_id = f'{sandbox.id}/{session_id}'
460
-
461
- if feature is not None:
462
- log_id = f'{log_id}/{feature.name}'
463
- self._add_message(
464
- f'[{log_id}] {name}: {code}', error
465
- )
492
+ """Called when a sandbox feature is housekeeping."""
493
+ assert duration > 0
494
+ if self.log_housekeep:
495
+ self._add_message(
496
+ f'[{feature.id}] feature housekeeping {counter}', error
497
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langfun
3
- Version: 0.1.2.dev202511010804
3
+ Version: 0.1.2.dev202511030805
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -89,7 +89,7 @@ langfun/core/eval/v2/progress_tracking_test.py,sha256=omOvSTvF-KgzwmXiOUj8sr5Dha
89
89
  langfun/core/eval/v2/reporting.py,sha256=yUIPCAMnp7InIzpv1DDWrcLO-75iiOUTpscj7smkfrA,8335
90
90
  langfun/core/eval/v2/reporting_test.py,sha256=CMK-vwho8cNRJwlbkCqm_v5fykE7Y3V6SaIOCY0CDyA,5671
91
91
  langfun/core/eval/v2/runners.py,sha256=bEniZDNu44AQgvqpwLsvBU4V_7WltAe-NPhYgIsLj1E,16848
92
- langfun/core/eval/v2/runners_test.py,sha256=spjkmqlls_vyERdZMdjv6dhIN9ZfxsDDvIQAWTj2kMk,11954
92
+ langfun/core/eval/v2/runners_test.py,sha256=Fw_OGaJZbMSwxBMRZ_PnCu4vPIlIfmzUYA9U2hq3FwQ,11996
93
93
  langfun/core/llms/__init__.py,sha256=UJ1UE41JPgkS56uNzJQ5ag9VWJN3OJuNna7aFR4CyOM,10184
94
94
  langfun/core/llms/anthropic.py,sha256=O_OuBiFhHou1Y15W2GwYFY1gV3FUsSUBOsAJp9UORqI,30710
95
95
  langfun/core/llms/anthropic_test.py,sha256=qA9vByp_cwwXNlXzcwHpPWFnO9lfFo8NKfDi5nBNqgI,9052
@@ -174,26 +174,27 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
174
174
  langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
175
175
  langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
176
176
  langfun/core/templates/selfplay_test.py,sha256=Ot__1P1M8oJfoTp-M9-PQ6HUXqZKyMwvZ5f7yQ3yfyM,2326
177
- langfun/env/__init__.py,sha256=EOKAU4CrlUAQtGORBaXXP1q_fAS6NHd6rIMOnRwWrjg,1688
178
- langfun/env/base_environment.py,sha256=vU0IL6-Fmx7xAYeefRLPSjMfwuhCz5b23h5T1HeILTk,24742
179
- langfun/env/base_environment_test.py,sha256=zXaCMA6JrWFtit68SESx_jGWzO6h7NQZpux4FqCMueQ,15432
180
- langfun/env/base_feature.py,sha256=5vC-zXYRaImh_nAc0mkgV9vW4ObKYarLUepQeCVAEMw,8189
181
- langfun/env/base_sandbox.py,sha256=-TZZ4JeQ5PaH0qhfF_HxPPosAHAJLfyD7rGETrbyYhQ,28015
182
- langfun/env/base_sandbox_test.py,sha256=zs3HL90dyp2YybEhS7splyQNmODSO8J1B8BT8KUBlAY,59450
183
- langfun/env/interface.py,sha256=zjmrXJcN2cV7a-Ja3V1b-JdpKA11ajWQqk46obHxfsI,41656
184
- langfun/env/interface_test.py,sha256=d8vdXL1PkrNQGwzfEI2r6esd6SBnTMgc-3GNArsnuj4,3295
177
+ langfun/env/__init__.py,sha256=Iwl5JQk6bGuScJcjx8NqKUCaZw1r8oRiuqFadu8x_Ss,1680
178
+ langfun/env/base_environment.py,sha256=CEDi4BXxfsGbkRZam2ywP0scpb-rbRb6bdMTFhNitB4,27013
179
+ langfun/env/base_environment_test.py,sha256=JltE2uPrruCpj0V50T3NGG3cf_8WDss67dEMk-REPRA,15457
180
+ langfun/env/base_feature.py,sha256=ohotob1Cnjd9M1BzHUS2ezGZgifcvXs6Ce0bOTGTkf8,8473
181
+ langfun/env/base_feature_test.py,sha256=n1IrU6tHRwHjkyD7yHJtfAQ8yrSSAteVdIqfTK49JI8,7094
182
+ langfun/env/base_sandbox.py,sha256=hTomEzky4Jfunt2Kse7uaboBYCCGAUf_xTY9kcPZDcY,27942
183
+ langfun/env/base_sandbox_test.py,sha256=j1WugtZBjvI9zo2_1tvuBhORMFENp8TanQwGfXwEUKE,60308
184
+ langfun/env/interface.py,sha256=tZGJ1RLmcsctz62pyBdravibUQL439Lkc4O40l7FtzY,51760
185
+ langfun/env/interface_test.py,sha256=AOosmhIIi4HVQ_WFmWha05oyj3-md4eJ4ODMTR6oJVw,4212
185
186
  langfun/env/load_balancers.py,sha256=qRhCthqzjZIQBwta8qC1C0s0J-VQAVomJQqI7Nqv-r4,1948
186
187
  langfun/env/load_balancers_test.py,sha256=Bg0h-AL7LpWb_aixFXs_FpgQp4dWLvodcsQj-mys6zs,4125
187
- langfun/env/test_utils.py,sha256=i6OadjYfLl5IBrDhEzWiJWsq_GWlplHnrBDD0LEB_HM,14090
188
+ langfun/env/test_utils.py,sha256=07Nszmqo3Xf5O5d_O2Qp7OcTB_wJ7QG53t6TqMgAchc,14946
188
189
  langfun/env/event_handlers/__init__.py,sha256=EE3pV0BlhvYfTG3njhVTMqj88IO_gwjLqZDGSGL95DE,449
189
- langfun/env/event_handlers/chain.py,sha256=elOAeY38rL0PzNKBq-jwsEqb6ZLE0aOI0LkRSQlQofs,7608
190
- langfun/env/event_handlers/chain_test.py,sha256=FI0hnDdzmyPe6nEJDeVDrczM49SL9Mhch3STCtgNp7w,8657
191
- langfun/env/event_handlers/event_logger.py,sha256=J5v9qPZk20ZscYU1Ko0cHaYaRa45Yurz9y8Yn-UATpY,12879
192
- langfun/env/event_handlers/event_logger_test.py,sha256=HDSO09eFEKIdMYSM_8UHDKhNhSXuqM6Tif63d0kAGjs,9130
193
- langfun/env/event_handlers/metric_writer.py,sha256=ABxvIkCJXtGpVb2KoaMqc61yMPhaHCoWB1gQraUqlEk,19793
190
+ langfun/env/event_handlers/chain.py,sha256=MrAF7BTVJxQyYEFrBdyGCb0b3RotBVInJm0CHySYXNo,6946
191
+ langfun/env/event_handlers/chain_test.py,sha256=92xBFgL6NitbH8HSKZWQzK9jOm_SWyZwVywFyrxG6fc,8129
192
+ langfun/env/event_handlers/event_logger.py,sha256=ga8RN8qjwtAOCnV_MnhNPTktN8EJ-x1qw4Z_MsnvI5A,12554
193
+ langfun/env/event_handlers/event_logger_test.py,sha256=qSAcirtRz00H-1RL9ShELBiZKiPxsk_v6cVA6XdAk4k,9274
194
+ langfun/env/event_handlers/metric_writer.py,sha256=7ZrUp0rYvs7TfNpQ16Xbxg8vp-6ZbjuJ-qrhVSbhv2I,21085
194
195
  langfun/env/event_handlers/metric_writer_test.py,sha256=bjdYXoXMPWpWz_-HUPM6vFP1ez5G386u0fmPfe-SR_M,5952
195
- langfun-0.1.2.dev202511010804.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
196
- langfun-0.1.2.dev202511010804.dist-info/METADATA,sha256=GkWja_YtYyrdLzgc8XtzAn4aL8nSz01TSIdgxZIq7iA,7522
197
- langfun-0.1.2.dev202511010804.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
198
- langfun-0.1.2.dev202511010804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
199
- langfun-0.1.2.dev202511010804.dist-info/RECORD,,
196
+ langfun-0.1.2.dev202511030805.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
197
+ langfun-0.1.2.dev202511030805.dist-info/METADATA,sha256=SfPYy0XHEltJkf1m7wkd3_Hy3jJ2QWCb8cNRRv7duOo,7522
198
+ langfun-0.1.2.dev202511030805.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
199
+ langfun-0.1.2.dev202511030805.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
200
+ langfun-0.1.2.dev202511030805.dist-info/RECORD,,